Compiler Construction Code Generation Activation Records

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

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.
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
Lecture 10 – Activation Records Eran Yahav 1 Reference: Dragon 7.1,7.2. MCD 6.3,
Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University Prelim Review.
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:
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
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.
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 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.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
6.828: PC hardware and x86 Frans Kaashoek
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 ◦
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
University of Washington Today More on procedures, stack etc. Lab 2 due today!  We hope it was fun! What is a stack?  And how about a stack frame? 1.
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.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Recitation 2: Outline Assembly programming Using gdb L2 practice stuff Minglong Shao Office hours: Thursdays 5-6PM Wean Hall.
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.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
Functions/Methods in Assembly
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.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
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.
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.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
A job ad at a game programming company
Assembly function call convention
Reading Condition Codes (Cont.)
C function call conventions and the stack
143A: Principles of Operating Systems Lecture 4: Calling conventions
Introduction to Compilers Tim Teitelbaum
Assembly IA-32.
Machine-Level Programming 4 Procedures
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Machine-Level Programming III: Procedures Sept 18, 2001
MIPS Procedure Calls CSE 378 – Section 3.
The Runtime Environment
Multi-modules programming
X86 Assembly Review.
“Way easier than when we were students”
ICS51 Introductory Computer Organization
Computer Architecture and System Programming Laboratory
Presentation transcript:

Compiler Construction Code Generation Activation Records TODO: ensure consistency in displaying the values of instrumentation predicates for concrete heaps

LIR vs. assembly LIR Assembly #Registers Unlimited Limited Function calls Implicit Runtime stack Instruction set Abstract Concrete

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)

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

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

Runtime stack … Stack grows downwards (towards smaller addresses) Current frame … Previous frame SP FP 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)

X86 runtime stack Instruction Usage push, pusha,… Push on runtime stack pop, popa,… Pop from runtime stack call Transfer control to called routine ret Transfer control back to caller Register Usage ESP Stack pointer EBP Base pointer X86 stack registers pusha: Push all general-purpose registers onto stack. pushad: Push all double-word (32-bit) registers onto stack. X86 stack and call/ret instructions

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 Calling conventions describe the interface of called code: The order in which atomic (scalar) parameters, or individual parts of a complex parameter, are allocated How parameters are passed (pushed on the stack, placed in registers, or a mix of both) Which registers may be used by the callee without first being saved (i.e. pushed) How the task of setting up for and restoring the stack after a function call is divided between the caller and the callee

… … Call sequences call return caller callee caller FP SP SP SP SP SP Push caller-save registers Push actual parameters (in reverse order) FP … … caller Caller push code SP push return address Jump to call address Reg 1 … Reg n call SP Push current base-pointer bp = sp Push local variables Push callee-save registers Callee push code (prologue) Param n … param1 callee SP Return address Callee pop code (epilogue) Pop callee-save registers Pop callee activation record Pop old base-pointer SP Previous fp SP Local 1 Local 2 … Local n return FP pop return address Jump to address Caller pop code SP caller Pop parameters Pop caller-save registers

Call sequences – Foo(42,21) caller callee caller call return push %ecx call _foo Push caller-save registers Push actual parameters (in reverse order) caller push return address Jump to call address call push %ebp mov %esp, %ebp sub %8, %esp push %ebx Push current base-pointer bp = sp Push local variables (callee variables) Push callee-save registers callee pop %ebx mov %ebp, %esp pop %ebp ret Pop callee-save registers Pop callee activation record Pop old base-pointer Registers EAX, ECX, and EDX are caller-saved, and the rest are callee-saved. ECX is a general register. return pop return address Jump to address add $8, %esp pop %ecx caller Pop parameters Pop caller-save registers

“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 cdecl (which stands for C declaration) is a calling convention that originates from the C programming language and is used by many C compilers for the x86 architecture.[1] In cdecl, subroutine arguments are passed on the stack. Integer values and memory addresses are returned in the EAX register, floating point values—in the ST0 x87 register. Registers EAX, ECX, and EDX are caller-saved, and the rest are callee-saved.

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 Param n … param1 FP+8 Return address FP Previous fp Local 1 Local 2 … Local n-1 Local n FP-4 SP

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

main calling method bar 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 main’s frame EBP+12 31 EBP+8 this ≈ a Return address implicit argument EBP Previous fp bar’s frame y ESP, EBP-4 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

x86 assembly AT&T syntax and Intel syntax We’ll be using AT&T syntax Work with GNU Assembler (GAS) GAS instructions generally have the form mnemonic source, destination. For instance, the following mov instruction: movb $0x05, %al will move the value 5 into the register al.

IA-32 Eight 32-bit general-purpose registers EFLAGS register EAX, EBX, ECX, EDX, ESI, EDI EBP – stack frame (base) pointer ESP – stack pointer EFLAGS register info on results of arithmetic operations EIP (instruction pointer) register Machine-instructions add, sub, inc, dec, neg, mul, … generally suffixed with the letters "b", "s", "w", "l", "q" or "t" to determine what size operand is being manipulated. S-Sign, Z-Zero, C-Carry, P-Parity, O-Overflow

Immediate and register operands Value specified in the instruction itself Preceded by $ Example: add $4,%esp Register Register name is used Preceded by % Example: mov %esp,%ebp

Memory and base displacement operands Memory operands Obtain value at given address Example: mov (%eax), %eax Base displacement Obtain value at computed address Syntax: disp(base,index,scale) offset = base + (index * scale) + displacement Example: mov $42, 2(%eax) %eax + 2 Example: mov $42, (%eax,%ecx,4) %eax + %ecx*4 Addressing mode Example Meaning Register Add R4,R3 R4 <- R4+R3 Immediate Add R4,#3 R4 <- R4+3 Displacement Add R4,100(R1) R4 <- R4+Mem[100+R1] Register indirect Add R4,(R1) R4 <- R4+Mem[R1] Indexed / Base Add R3,(R1+R2) R3 <- R3+Mem[R1+R2] Direct or absolute Add R1,(1001) R1 <- R1+Mem[1001] Memory indirect Add R1,@(R3) R1 <- R1+Mem[Mem[R3]] Auto-increment Add R1,(R2)+ R1 <- R1+Mem[R2]; R2 mR2+d Auto-decrement Add R1,–(R2) R2 <- R2–d; R1 <- R1+Mem[R2] Scaled Add R1,100(R2)[R3] R1 <- R1+Mem[100+R2+R3*d]

Accessing Variables: Illustration … … Use offset from base pointer Above BP = parameters Below BP = locals (+ LIR reg.s) Examples: %eax = %ebp + 8 (%eax) = the value 572 8(%ebp) = the value 572 param n … 572 %eax,BP+8 Return address BP Previous bp local 1 … local n BP-4 SP

Base displacement addressing 4 4 4 4 4 4 4 4 7 2 4 5 6 7 1 Array base reference (%ecx,%ebx,4) mov (%ecx,%ebx,4), %eax %ecx = base %ebx = 3 offset = base + (index * scale) + displacement offset = %ecx + (3*4) + 0 = %ecx + 12

Instruction examples Translate a=p+q into mov 8(%ebp),%ecx (load p) add 12(%ebp),%ecx (arithmetic p + q) mov %ecx,-4(%ebp) (store a)

Instruction examples Array access: a[i]=1 Jumps: mov -4(%ebp),%ebx (load a) mov -8(%ebp),%ecx (load i) mov $1,(%ebx,%ecx,4) (store into the heap) Jumps: Unconditional: jmp label2 Conditional: cmp $0, %ecx jne cmpFailLabel