Compiler Construction Intermediate Representation III Activation Records Ran Shaham and Ohad Shacham 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
Procedure Calls Prof. Sirer CS 316 Cornell University.
Ch. 8 Functions.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
Chapter 14: Building a Runnable Program Chapter 14: Building a runnable program 14.1 Back-End Compiler Structure 14.2 Intermediate Forms 14.3 Code.
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,
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:
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
Register Allocation Mooly Sagiv html://
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
Semantics of Calls and Returns
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.
4/6/08Prof. Hilfinger CS164 Lecture 291 Code Generation Lecture 29 (based on slides by R. Bodik)
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.
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University See P&H 2.8 and 2.12.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
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 –
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.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Winter Compiler Construction T11 – Activation records + Introduction to x86 assembly Mooly Sagiv and Roman Manevich School of Computer Science.
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.
Overview of Back-end for CComp Zhaopeng Li Software Security Lab. June 8, 2009.
Winter Compiler Construction T10 – IR part 3 + Activation records Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.
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.
CS 404 Introduction to Compiler Design
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
Code Generation.
Lecture 30 (based on slides by R. Bodik)
Winter Compiler Construction T10 – IR part 3
Code Generation Part I Chapter 9
Topic 3-a Calling Convention 1/10/2019.
Multi-modules programming
Presentation transcript:

Compiler Construction Intermediate Representation III Activation Records Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

2 Intermediate representation Allows language-independent, machine independent optimizations and transformations Easy to translate from AST Easy to translate to assembly ASTIR Pentium Java bytecode Sparc optimize

3 Multiple IRs Some optimizations require high-level structure Others more appropriate on low-level code Solution: use multiple IR stages ASTLIR Pentium Java bytecode Sparc optimize HIR optimize

4 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

5 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 Assign to register if both operands non-registers

6 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

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

8 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 …

9 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] Solution: Use a stack of LIR registers Stack corresponds to recursive invocations of t := TR[e] All the temporaries on the stack are alive

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

11 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

12 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

13 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] ]

14 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

15 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

16 Tips for PA4 Keep list of LIR instructions for each translated method Keep ClassLayout information for each class Field offsets Method offsets Don’t forget to take superclass fields and methods into account Two AST passes: Pass 1: Collect and name strings literals ( Map ) Create ClassLayout for each class Pass 2: use literals and field/method offsets to translate method bodies Finally: print string literals, dispatch tables, print translation list of each method body

17 microLIR simulator Written by Roman Manevich 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, #labels, etc.) Comes with sample inputs Read manual

18 LIR vs. assembly LIRAssembly #RegistersUnlimitedLimited Function callsImplicitRuntime stack Instruction setAbstractConcrete

19 Function calls 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)

20 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

21 Runtime stack Stack of activation records Call = push new activation record Return = pop activation record Only one “active” activation record – top of stack

22 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

23 X86 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 X86 stack registers X86 stack and call/ret instructions

24 Call sequences The processor does not save the content of registers on procedure calls So who will? Caller saves and restores registers Caller’s responsibility Callee saves and restores registers Callee’s responsability

25 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

26 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)

27 “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

28 Accessing stack variables Use offset from EBP 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 Local 1 Local 2 … Local n-1 Local n Previous fp Param n … param1 FP+8 FP-4

29 main calling method bar int bar(int x) { int y; … } static void main(string[] args) { int z; Foo a = new Foo(); z = a.bar(31); … }

30 main calling method bar Examples %ebp + 4 = return address %ebp + 8 = first parameter Always this in virtual function calls %ebp = old %ebp (pushed by callee) %ebp – 4 = first local …… ESP, EBP Return address Previous fp EBP+8 EBP-4 this ≈ a 31 EBP+12 y main’s frame bar’s frame int bar(Foo this, int x) { int y; … } static void main(string[] args) { int z; Foo a = new Foo(); z = a.bar(a,31); … } implicit parameter implicit argument