Intermediate Representation III. 2 PAs PA2 deadline is 16.12.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Symbol Table.
Intermediate Code Generation
Lecture 9: MIPS Instruction Set
1 CS 201 Compiler Construction Machine Code Generation.
A simple register allocation optimization scheme.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
SPIM and MIPS programming
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Compiler Construction Intermediate Representation III Activation Records Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Lecture 01 - Introduction Eran Yahav 1. 2 Who? Eran Yahav Taub 734 Tel: Monday 13:30-14:30
Lecture 13 – Compiling Object-Oriented Programs Eran Yahav 1 Reference: MCD
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
Compiler Construction Code Generation II Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Code Generation Simple Register Allocation Mooly Sagiv html:// Chapter
Arithmetic Expression Consider the expression arithmetic expression: (a – b) + ((c + d) + (e * f)) that can be represented as the following tree.
Compiler Summary Mooly Sagiv html://
Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Run-time Environment and Program Organization
Compiler Construction Intermediate Representation I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Activation records Code Generation Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Code Generation I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Semantic Analysis III + Intermediate Representation I.
Compilation /15a Lecture 7 Getting into the back-end Noam Rinetzky 1.
1 October 1, October 1, 2015October 1, 2015October 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
{ Graphite Grigory Arashkovich, Anuj Khanna, Anirban Gangopadhyay, Michael D’Egidio, Laura Willson.
Compiler Run-time Organization Lecture 7. 2 What we have covered so far… We have covered the front-end phases –Lexical analysis –Parsing –Semantic analysis.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Code Generation III. PAs PA4 5.1 – 7.2 PA5 (bonus) 24.1 –
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
Compiler Construction Dr. Noam Rinetzky and Orr Tamir School of Computer Science Tel Aviv University
Winter Compiler Construction T9 – IR part 2 + Runtime organization Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.
Winter Compiler Construction T11 – Activation records + Introduction to x86 assembly Mooly Sagiv and Roman Manevich School of Computer Science.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
1 Code Generation. 2 Position of a Code Generator in the Compiler Model Front-End Code Optimizer Source program Symbol Table Lexical error Syntax error.
Chapter 1 Introduction Major Data Structures in Compiler
Semantic Analysis II. Messages Please check lecturer notices in the Moodle Appeals  Legitimate: “I don’t have the bug you mentioned…”  Illegitimate:
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Semantic Analysis III + Intermediate Representation I.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Compiler Principles Fall Compiler Principles Lecture 8: Intermediate Representation Roman Manevich Ben-Gurion University.
Code Generation II. 2 Compiler IC Program ic x86 executable exe Lexical Analysis Syntax Analysis Parsing ASTSymbol Table etc. Inter. Rep. (IR) Code Generation.
Compiler Construction Recap. 2 Announcements PA4: extension until end of exam period – Not a single day more, for any reason! PA5: bonus exercise – Will.
Winter Compiler Construction T10 – IR part 3 + Activation records Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Dr. Hussien Sharaf Dr Emad Nabil. Dr. Hussien M. Sharaf 2 position := initial + rate * Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 *
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Design issues for Object-Oriented Languages
Lecture 9 Symbol Table and Attributed Grammars
CS 404 Introduction to Compiler Design
System Software Unit-1 (Language Processors) A TOY Compiler
Code Generation.
The Boolean (logical) data type boolean
Chapter 6 Intermediate-Code Generation
CSE401 Introduction to Compiler Construction
CS 201 Compiler Construction
Winter Compiler Construction T10 – IR part 3
Local Optimizations.
Intermediate Code Generation
Mooly Sagiv and Roman Manevich School of Computer Science
CSc 453 Interpreters & Interpretation
Presentation transcript:

Intermediate Representation III

2 PAs PA2 deadline is 16.12

3 class A{ int x; int y; … } Object types x y Runtime memory layout for object of class A DispacthVectorPtr Compile time information for A class type x y 1 2 Field offsets DispacthVectorPtr 0 size ?

4 Field selection dynType(f)=A q = f.x; Move f,R1 MoveField R1. 1,R2 Move R2,q x y Compile time information for A class type Runtime memory layout for object of class A DispacthVectorPtr x y 2 Field offsets DispacthVectorPtr 0 1

5 class A{ int x; int y; … } class B extends A { int z; … } Object types and inheritance x y Runtime memory layout for object of class B z DispacthVectorPtr Compile time information for B class type x y 1 2 Field offsets DispacthVectorPtr 0 z 3 prefix of A

6 Field selection dynType(f)≤ A q = f.x; Move f,R1 MoveField R1. 1,R2 Move R2,q Runtime memory layout for object pointed by f x y ???? DispacthVectorPtr prefix of A

7 Virtual Methods class A { int x; int y; void f() {…} void g() {…} } x y Runtime memory layout for object of class A DispacthVectorPtr Compile time information for A class type f g 0 1 Method offsets x y 1 2 Field offsets DispacthVectorPtr 0

8 Method Invocation dynType(w)=A w.f() Move w,R1 VirtualCall R1.0(),Rdummy x y Runtime memory layout for object of class A DispacthVectorPtr Compile time information for A class type f g 0 1 Method offsets x y 1 2 Field offsets DispacthVectorPtr 0

Methods and Inheritance 9 class A { int x; int y; void f() {…} void g() {…} } class B extends A { int z; void f() {…} void h() {…} } f g h prefix from A Method offsets of B

10 Method Invocation dynType(w)≤ A w.f() Move w,R1 VirtualCall R1.0(),Rdummy x y ???? DispacthVectorPtr g ???? f Runtime memory layout for object pointed by w Method offsets

11 Object creation A f = new B (); |B| = |x|+|y|+|z|+|DVPtr| = = 4 (16 bytes) Label generated for class type B during LIR translation Library __allocateObject(16),R1 MoveField _B_DV, R1.0 Move R1,f

12 LIR translation example class A { int x; string s; int foo(int y) { int z=y+1; return z; } static void main(string[] args) { A p = new B(); p.foo(5); } } class B extends A { int z; int foo(int y) { s = “y=“ + Library.itos(y); Library.println(s); int[] sarr = Library.stoa(s); int l = sarr.length; Library.printi(l); return l; } }

13 LIR program (manual trans.) str1: “y=“# Literal string in program _DV_A: [_A_foo]# dispatch table for class A _DV_B: [_B_foo]# dispatch table for class B _A_foo:# int foo(int y) Move y,R1 # int z=y+1; Add 1,R1 Move R1,z Return z# return z; _B_foo:# int foo(int y) Library __itos(y),R1 # Library.itos(y); Library __stringCat(str1,R1),R2 # "y=" + Library.itos(y); Move this,R3# this.s = "y=" + Library.itos(y); MoveField R2,R3.2 MoveField R3.2,R4 Library __println(R4),Rdummy # Library.println(s); Library __stoa(R4),R5# int[] sarr = Library.stoa(s); Move R5,sarr ArrayLength sarr,R6# int l = sarr.length; Move R6,l Library __printi(l),Rdummy# Library.printi(l) Return l# return l; # main in A _ic_main:# A {static void main(string[] args)…} Library __allocateObject(16),R1 # A p = new B(); MoveField _DV_B,R1.0# Update DVPtr of new object VirtualCall R1.0(y=5),Rdummy# p.foo(5)

14 Class layout implementation class A { int x_1;... boolean x_k; void foo_1(…) {…}... int foo_n(…) {…} } x_1 = 1 … class ClassLayout { Map methodToOffset; // DVPtr = 0 Map fieldToOffset; } x_k = k foo_1 = 0 … foo_n = n-1 fieldToOffset methodToOffset _DV_A: [foo_1,…,foo_n] lir file MoveField R1.3,R9 VirtualCall R1.7(),R3

15 Representing arrays/strings str1: “Hello!” 3123 R1:[1,2,3] 1 word = 4 bytes

16 LIR optimizations Aim to reduce number of LIR registers and number of instructions Avoid storing variables and constants in registers Use accumulator registers Reuse “dead” registers Weighted register allocation

17 Avoid storing constants and variables in registers Don’t allocate target register for each instruction TR[5] = Move 5,Rj TR[x] = Move x,Rk For a constant TR[5] = 5 For a variable TR[x] = x TR[x+5] = Move 5,R1 Add x,R1 Translation for a “simple” sub tree

18 Accumulator registers Use same register for sub-expression and result TR[e1 OP e2] R1 := TR[e1] R2 := TR[e2] R3 := R1 OP R2 R1 := TR[e1] R2 := TR[e2] R1 := R1 OP R2

19 Accumulator registers TR[e1 OP e2] c+(b*a) R1 := TR[e1] R2 := TR[e2] R3 := R1 OP R2 R1 := TR[e1] R2 := TR[e2] R1 := R1 OP R2 Move b,R1 Mul a,R1 Add c,R1 Move a,R1 Move b,R2 Mul R1,R2 Move R2,R3 Move c,R4 Add R3,R4 Move R4,R5

20 Accumulator registers cont. For instruction with N registers dedicate one register for accumulation Accumulating instructions, use: MoveArray R1[R2],R1 MoveField R1.7,R1 StaticCall _foo(R1,…),R1 …

21 Reuse registers Registers have very-limited lifetime TR[e1 OP e2] = R1:=TR[e1] R2:=TR[e2] R1:=R1 OP R2 Registers from TR[e1] can be reused in TR[e2]

22 Weighted register allocation Sethi & Ullman algorithm Two expression e1, e2 and an operation OP e1,e2 without side-effects TR[e1 OP e2] = TR[e2 OP e1] Weighted register allocation translate heavier sub-tree first

23 Example R0 := TR[a+(b+(c*d))] b cd * + + a R0 R1 R2 Translation uses all optimizations shown until now uses 3 registers R2 R1 left child first b cd * + + a R0 Managed to save two registers R0 right child first R0

24 Weighted register allocation Can save registers by re-ordering subtree computations Label each node with its weight Weight = number of registers needed Leaf weight known Internal node weight w(left) > w(right) then w = left w(right) > w(left) then w = right w(right) = w(left) then w = left + 1 Choose heavier child as first to be translated Have to check that no side-effects exist

25 Weighted reg. alloc. example b 5c * array access + a baseindex W=1W=0W=1 W=2 Phase 1: - check absence of side-effects in expression tree - assign weight to each AST node R0 := TR[ a+b[5*c] ]

26 Weighted reg. alloc. example R0 := TR[ a+b[5*c] ] b 5c * array access + a R0 baseindex W=1 W=0W=1 W=2 R1 Phase 2: use weights to decide on order of translation Heavier sub-tree Move c,R0 Mul 5,R0 Move b,R1 MoveArray R1[R0],R0 Add a,R0

27 microLIR simulator Written by Roman Manevich Java application Accepts file.lir Executes program Use it to test your translation Checks correct syntax Performs lightweight semantic checks Runtime semantic checks Debug modes (-verbose:1|2) Prints program statistics (#registers, #labels, etc.) Comes with sample inputs Read manual