Winter 2006-2007 Compiler Construction T10 – IR part 3 + Activation records Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

The University of Adelaide, School of Computer Science
Intermediate Representation III. 2 PAs PA2 deadline is
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Ch. 8 Functions.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
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 10 – Activation Records Eran Yahav 1 Reference: Dragon 7.1,7.2. MCD 6.3,
Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Compiler Construction Code Generation II Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Register Allocation Mooly Sagiv html://
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
1 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
CS 536 Spring Code generation I Lecture 20.
Accessing parameters from the stack and calling functions.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Run time vs. Compile time
Compiler Summary Mooly Sagiv html://
Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
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.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Compiler Construction Code Generation I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Chapter 8 :: Subroutines and Control Abstraction
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
Code Generation Gülfem Savrun Yeniçeri CS 142 (b) 02/26/2013.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
Code Generation III. PAs PA4 5.1 – 7.2 PA5 (bonus) 24.1 –
Winter Compiler Construction T12 – Code Generation Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Activation Records (in Tiger) CS 471 October 24, 2007.
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.
CSC 8505 Compiler Construction Runtime Environments.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
Compiler Construction Code Generation Activation Records
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
1 Assembly Language: Function Calls Jennifer Rexford.
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.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
C function call conventions and the stack
Mooly Sagiv html://
143A: Principles of Operating Systems Lecture 4: Calling conventions
Introduction to Compilers Tim Teitelbaum
Code Generation Part I Chapter 9
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Code Generation Part I Chapter 8 (1st ed. Ch.9)
Winter Compiler Construction T10 – IR part 3
Code Generation Part I Chapter 9
The Runtime Environment
Topic 3-a Calling Convention 1/10/2019.
UNIT V Run Time Environments.
Mooly Sagiv and Roman Manevich School of Computer Science
Presentation transcript:

Winter Compiler Construction T10 – IR part 3 + Activation records Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University

2 Today: LIR Spec. + microLIR Strings and arrays PA4 Optimizations Runtime organization Activation records Today IC Language ic Executable code exe Lexical Analysis Syntax Analysis Parsing ASTSymbol Table etc. Inter. Rep. (IR) Code Generation Next time: Introduction to x86 assembly Code generation Activation records in depth

3 LIR language Supports Literal strings Dispatch tables Instruction set Unbounded number of registers Object/array allocation via library functions Updating DVPtr with LIR instructions Missing from printout: ArrayLength instruction Notice special syntax for parameter passing for static/virtual function calls No representation of activation records (frames) No calling sequence protocols Just Call/Return instructions this variable “magically” updated on call to virtual functions

4 Translating call/return TR[C.foo(e1,…,en)] R1 := TR[e1] … Rn := TR[en] StaticCall C_foo(x1=R1,…,xn=Rn),R TR[return e] R1 := TR[e] Return R1 TR[e1.foo(e2)] R1 := TR[e1] R2 := TR[e2] VirtualCall R1. c foo (x=R2),R Constant representing offset of method f in dispatch table of class type of e1 formal parameter name actual argument register

5 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; } } Translating.length operator Translating the main function Translation for literal stringsTranslating virtual functions (dispatch tables) Translating virtual function calls

6 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.3 MoveField R3.3,R4 Library __println(R4),Rdummy 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)

7 Class layout implementation class A { int x_1;... boolean x_n; void foo_1(…) {…}... int foo_n(…) {…} } x_1 = 1 … class ClassLayout { Map methodToOffset; // DVPtr = 0 Map fieldToOffset; } x_n = n foo_1 = 0 … foo_n = n-1 fieldToOffset methodToOffset _DV_A: [foo_1,…,foo_n] file.lir MoveField R1.3,9 VirtualCall R1.7(),R : generate dispatch tables 2: determine method offsets in virtual calls 3: determine field offsets in field access statements

8 microLIR simulator Java application Accepts file.lir (your translation) 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) Comes with sample inputs Read manual Comes with sources (allowed to use in PA4) Not heavily tested (better than nothing)

9 PA4 Translate AST to LIR (file.ic -> file.lir) Dispatch table for each class Literal strings (all literal strings in file.ic) Instruction list for every function Leading label for each function _CLASS_FUNC Label of main function should be _ic_main Maintain internally for each function List of LIR instructions Reference to method AST node Needed to generate frame information in PA5 Maintain for each call instruction Reference to method AST Needed to generate call sequence in PA5 Optimizations (WARNING: only after assignment works) Keep optimized and non-optimized translations separately

10 Representing arrays/strings str1: “Hello!” 3123 R1:[1,2,3] Array length __allocateArray(12),R1 # 3*4=12 bytes 1 word = 4 bytes

11 LIR optimizations Aim to reduce number of LIR registers Reduce size of activation records Allow better register allocation in PA5 Also reduces number of instructions Avoid storing variables and constants in registers Use accumulator registers Reuse “dead” registers Weighted register allocation (More complicated: reuse assigned values in block) Merge consecutive labels Left to PA5 (generates additional labels)

12 Avoid storing constants and variables in registers Naïve translation of AST leaves For a constant TR[5] = Move 5,Rj For a variable TR[x] = Move x,Rk Better translation For a constant TR[5] = 5 For a variable TR[x] = x What about TR[x+5]=? WRONG: TR[x+5] = Add TR[x],TR[5] = Add x,5 TR[x+5] = Move 5,R1 Add x,R1 Assign to register if both operands non-registers

13 Accumulator registers Use same register for sub-expression and result Very natural for 2-address code and previous optimization TR[e1 OP e2] R1 := TR[e1] R2 := TR[e2] R3 := R1 OP R2 Naïve translation R1 := TR[e1] R2 := TR[e2] R1 := R1 OP R2 Better translation

14 Accumulator registers TR[e1 OP e2] a+(b*c) R1 := TR[e1] R2 := TR[e2] R3 := R1 OP R2 Naïve translation R1 := TR[e1] R2 := TR[e2] R1 := R1 OP R2 Better translation Move b,R1 Mul c,R1 Add a,R1 Move a,R1 Move b,R2 Mul R1,R2 Move R2,R3 Move c,R4 Add R3,R4 Move R4,R5

15 Accumulator registers cont. Accumulating instructions, use: MoveArray R1[R2],R1 MoveField R1.7,R1 StaticCall _foo(R1,…),R1 …

16 Reuse registers Registers have very-limited lifetime Currently stored values used exactly once (All LIR registers become dead after statement) Suppose TR[e1 OP e2] translated as R1:=TR[e1], R2:=TR[e2], R1:=R1 OP R2 Registers from TR[e1] can be reused in TR[e2] Algorithm: Use a stack of temporaries (LIR registers) Stack corresponds to recursive invocations of t := TR[e] All the temporaries on the stack are alive

17 Reuse registers cont. Implementation: use counter c to implement live register stack Registers R(0)…R(c) are alive Registers R(c+1),R(c+2)… can be reused Push means increment c, pop means decrement c In the translation of R(c)=TR[e1 OP e2] R(c) := TR[e1] R(c) := TR[e2] R(c) := R(c) OP R(c+1) c = c + 1 c = c - 1

18 Example R0 := TR[((c*d)-(e*f))+(a*b)] R0 := c*d R1 := e*f R0 := R0-R1 c = c + 1 c = c - 1 c = 0 R0 := TR[(c*d)-(e*f)] R1 := a*b c = c + 1 R0 := R0 + R1 c = c - 1

19 Weighted register allocation Suppose we have expression e1 OP e2 e1,e2 without side-effects (function calls, assignments) OP is commutative: *,+ TR[e1 OP e2] = TR[e2 OP e1] Does order matter? Use the Sethi & Ullman algorithmSethi & Ullman algorithm Weighted register allocation – translate heavier sub-tree first

20 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 register R0 right child first R0

21 Weighted register allocation Can save registers by re-ordering (commutative) 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 WARNING: have to check that no side-effects exist before attempting to apply this optimization (pre-pass on the tree)

22 LIR vs. assembly LIRAssembly #RegistersUnlimitedLimited Function callsImplicitRuntime stack Instruction setAbstractConcrete TypesBasic and user defined Limited basic types Actually very limited in our LIR

23 Function calls LIR – simply call/return Conceptually Supply new environment (frame) with temporary memory for local variables Pass parameters to new environment Transfer flow of control (call/return) Return information from new environment (ret. value) Assembly – pass parameters (+this), save registers,call, restore registers, return, pass return value, virtual method lookup

24 Activation records New environment = activation record (a.k.a. frame) Activation record = data of current function / method call User data Local variables Parameters Return values Register contents Administration data Code addresses Pointers to other activation records (not in IC) In IC – a stack is sufficient !

25 Runtime stack Stack of activation records Call = push new activation record Return = pop activation record Only one “active” activation record – top of stack This is enough to handle recursion

26 Runtime stack Stack grows downwards (towards smaller addresses) SP – stack pointer – top of current frame FP – frame pointer – base of current frame Sometimes called BP (base pointer) Current frame …… Previous frame SP FP

27 Pentium runtime stack RegisterUsage ESPStack pointer EBPBase pointer InstructionUsage push, pusha,…Push on runtime stack pop, popa,…Pop from runtime stack callTransfer control to called routine retTransfer control back to caller Pentium stack registers Pentium stack and call/ret instructions

28 Call sequences The processor does not save the content of registers on procedure calls So who will? Caller saves and restores registers Callee saves and restores registers But can also have both save/restore some registers

29 call caller callee return caller Caller push code Callee push code (prologue) Callee pop code (epilogue) Caller pop code Push caller-save registers Push actual parameters (in reverse order) push return address Jump to call address Push current base-pointer bp = sp Push local variables Push callee-save registers Pop callee-save registers Pop callee activation record Pop old base-pointer pop return address Jump to address Pop parameters Pop caller-save registers Call sequences

30 call caller callee return caller push %ecx push $21 push $42 call _foo push %ebp mov %esp, %ebp sub %8, %esp push %ebx pop %ebx mov %ebp, %esp pop %ebp ret add $8, %esp pop %ecx Push caller-save registers Push actual parameters (in reverse order) push return address Jump to call address Push current base-pointer bp = sp Push local variables (callee variables) Push callee-save registers Pop callee-save registers Pop callee activation record Pop old base-pointer pop return address Jump to address Pop parameters Pop caller-save registers Call sequences – Foo(42,21)

31 “To Callee-save or to Caller-save?” Callee-saved registers need only be saved when callee modifies their value Some conventions exist (cdecl) %eax, %ecx, %edx – caller save %ebx, %esi, %edi – callee save %esp – stack pointer %ebp – frame pointer Use %eax for return value

32 Accessing stack variables Use offset from EBP Remember – stack grows downwards Above EBP = parameters Below EBP = locals Examples %ebp + 4 = return address %ebp + 8 = first parameter %ebp – 4 = first local …… SP FP Return address param n … param1 Local 1 … Local n Previous fp param n … param1 FP+8 FP-4

33 Happy new year!