Winter Compiler Construction T10 IR part 3 + Activation records. Today. LIR language

Similar documents
Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

THEORY OF COMPILATION

Winter Compiler Construction T9 IR part 2 + Runtime organization. Announcements. Today. Know thy group s code

x86 assembly CS449 Fall 2017

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

143A: Principles of Operating Systems. Lecture 4: Calling conventions. Anton Burtsev October, 2017

238P: Operating Systems. Lecture 3: Calling conventions. Anton Burtsev October, 2018

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

THEORY OF COMPILATION

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

CS LIR Language Specification and microlir Simulator Version 1.2

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

Summary: Direct Code Generation

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

Systems I. Machine-Level Programming V: Procedures

W4118: PC Hardware and x86. Junfeng Yang

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

Assembly Language: Function Calls

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Assembly Language: Function Calls" Goals of this Lecture"

CS 480 Fall Runtime Environments. Mike Lam, Professor. (a.k.a. procedure calls and heap management)

System Software Assignment 1 Runtime Support for Procedures

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

Practical Malware Analysis

x86 Assembly Crash Course Don Porter

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

Subprograms: Local Variables

Machine Program: Procedure. Zhaoguo Wang

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Run-time Environments

Run-time Environments

Compilation /17a Lecture 10. Register Allocation Noam Rinetzky

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

Assignment 11: functions, calling conventions, and the stack

Low-Level Essentials for Understanding Security Problems Aurélien Francillon

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

The x86 Architecture

Stack Discipline Jan. 19, 2018

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Where We Are. Optimizations. Assembly code. generation. Lexical, Syntax, and Semantic Analysis IR Generation. Low-level IR code.

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Microprocessors ( ) Fall 2010/2011 Lecture Notes # 15. Stack Operations. 10 top

Course Administration

Code Generation. Lecture 19

Compiler construction. x86 architecture. This lecture. Lecture 6: Code generation for x86. x86: assembly for a real machine.

CA Compiler Construction

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Procedure Call and Return Procedure call

G Programming Languages - Fall 2012

Assembly Language Lab # 9

Intel assembly language using gcc

CS 2210 Programming Project (Part IV)

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

ASSEMBLY III: PROCEDURES. Jo, Heeseung

An Experience Like No Other. Stack Discipline Aug. 30, 2006

Assembly III: Procedures. Jo, Heeseung

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

CIT Week13 Lecture

CS213. Machine-Level Programming III: Procedures

Reverse Engineering II: Basics. Gergely Erdélyi Senior Antivirus Researcher

x86 assembly CS449 Spring 2016

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

CSC 8400: Computer Systems. Using the Stack for Function Calls

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

CMSC 313 Lecture 12 [draft] How C functions pass parameters

IA-32 Architecture. CS 4440/7440 Malware Analysis and Defense

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

CS241 Computer Organization Spring 2015 IA

Register Allocation, iii. Bringing in functions & using spilling & coalescing

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

Review Questions. 1 The DRAM problem [5 points] Suggest a solution. 2 Big versus Little Endian Addressing [5 points]

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

CIS 341 Midterm February 28, Name (printed): Pennkey (login id): SOLUTIONS

Status. Lecture #15: Introduction to Runtime Organization. Run-time environments. Code Generation Goals and Considerations

The IA-32 Stack and Function Calls. CS4379/5375 Software Reverse Engineering Dr. Jaime C. Acosta

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

The remote testing experiment. It works! Code Generation. Lecture 12. Remote testing. From the cs164 newsgroup

CS61 Section Solutions 3

Implementing Functions at the Machine Level

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

Machine Programming 3: Procedures

Assembly Language Programming: Procedures. EECE416 uc. Charles Kim Howard University. Fall

Reverse Engineering II: The Basics

Lecture 4 CIS 341: COMPILERS

X86 Stack Calling Function POV

MIPS Functions and the Runtime Stack

Computer Architecture and Assembly Language. Practical Session 3

Transcription:

Winter 2006-2007 Compiler Construction T10 IR part 3 + Activation records Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University Today ic IC Language Lexical Analysis Syntax Analysis Parsing AST Symbol Table etc. Inter. Rep. (IR) Code Generation exe Executable code Today: LIR Spec. + microlir Strings and arrays PA4 Optimizations Runtime organization Activation records Next time: Introduction to x86 assembly Code generation Activation records in depth 2 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 3 1

Translating call/return TR[C.foo(e1,,en)] R1 := TR[e1] formal parameter name Rn := TR[en] StaticCall C_foo(x1=R1,,xn=Rn),R TR[e1.foo(e2)] actual argument register R1 := TR[e1] R2 := TR[e2] VirtualCall R1.c foo (x=r2),r TR[return e] R1 := TR[e] Return R1 Constant representing offset of method f in dispatch table of class type of e1 4 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); Translating virtual function calls Translating virtual functions (dispatch tables) Translating the main function 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; Translation for literal strings Translating.length operator 5 LIR program (manual trans.) str1: y= _DV_A: [_A_foo] _DV_B: [_B_foo] # Literal string in program # dispatch table for class A # 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: Library allocateobject(16),r1 MoveField _DV_B,R1.0 VirtualCall R1.0(y=5),Rdummy # A {static void main(string[] args) # A p = new B(); # Update DVPtr of new object # p.foo(5) 6 2

Class layout implementation class A { int x_1;... boolean x_n; void foo_1() {... int foo_n() { class ClassLayout { Map<Method,Integer> methodtooffset; // DVPtr = 0 Map<Field,Integer> fieldtooffset; 1: generate dispatch tables 2: determine method offsets in virtual calls 3: determine field offsets in field access statements fieldtooffset x_1 = 1 x_n = n methodtooffset foo_1 = 0 foo_n = n-1 2 3 1 file.lir _DV_A: [foo_1,,foo_n]... VirtualCall R1.7(),R3 MoveField R1.3,9 7 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) 8 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 9 3

Representing arrays/strings str1: Hello! 6 72 101 108 108 111 33 Array length allocatearray(12),r1 # 3*4=12 bytes R1:[1,2,3] 3 1 2 3 1 word = 4 bytes 10 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) 11 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 12 4

Accumulator registers Use same register for sub-expression and result Very natural for 2-address code and previous optimization TR[e1 OP e2] Naïve translation R1 := TR[e1] R2 := TR[e2] R3 := R1 OP R2 Better translation R1 := TR[e1] R2 := TR[e2] R1 := R1 OP R2 13 Accumulator registers TR[e1 OP e2] a+(b*c) Naïve translation R1 := TR[e1] R2 := TR[e2] R3 := R1 OP R2 Move a,r1 Move b,r2 Mul R1,R2 Move R2,R3 Move c,r4 Add R3,R4 Move R4,R5 Better translation R1 := TR[e1] R2 := TR[e2] R1 := R1 OP R2 Move b,r1 Mul c,r1 Add a,r1 14 Accumulator registers cont. Accumulating instructions, use: MoveArray R1[R2],R1 MoveField R1.7,R1 StaticCall _foo(r1,),r1 15 5

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 16 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 17 Example R0 := TR[((c*d)-(e*f))+(a*b)] c = 0 R0 := TR[(c*d)-(e*f)] R0 := c*d R1 := e*f R0 := R0-R1 c = c + 1 c = c - 1 R1 := a*b R0 := R0 + R1 c = c + 1 c = c - 1 18 6

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 algorithm Weighted register allocation translate heavier sub-tree first 19 Example R0 := TR[a+(b+(c*d))] left child first R0 + right child first R0 + R0 a R1 + a R0 + R1 b R2 * b R0 * R2 c d c R0 d Translation uses all optimizations shown until now uses 3 registers Managed to save two registers register 20 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) 21 7

LIR vs. assembly #Registers Function calls Instruction set Types LIR Unlimited Implicit Abstract Basic and user defined Assembly Limited Runtime stack Concrete Limited basic types Actually very limited in our LIR 22 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 23 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! 24 8

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 25 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) FP Previous frame Current frame SP 26 Pentium runtime stack Register Usage ESP Stack pointer EBP Base pointer Pentium stack registers Instruction push, pusha, pop, popa, call ret Usage Push on runtime stack Pop from runtime stack Transfer control to called routine Transfer control back to caller Pentium stack and call/ret instructions 27 9

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 28 Call sequences caller caller callee Caller push code call Callee push code (prologue) Callee pop code (epilogue) return 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 29 Call sequences Foo(42,21) caller caller push %ecx push $21 push $42 call _foo call push %ebp mov %esp, %ebp callee sub %8, %esp push %ebx pop %ebx mov %ebp, %esp pop %ebp ret return 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 30 10

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 31 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 FP+8 FP FP-4 Param n param1 Return address Previous fp Local 1 Local n Param n param1 SP Return address 32 Happy new year! 33 11