Code Generation III. PAs PA4 5.1 – 7.2 PA5 (bonus) 24.1 – 24.2 2.

Slides:



Advertisements
Similar presentations
Intermediate Representation III. 2 PAs PA2 deadline is
Advertisements

A simple register allocation optimization scheme.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
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.
Compiler Construction Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Lecture 13 – Compiling Object-Oriented Programs Eran Yahav 1 Reference: MCD
Theory of Compilation Erez Petrank Lecture 9: Runtime (part 2); object oriented issues 1.
Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
Compiler Construction Code Generation II Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Accessing parameters from the stack and calling functions.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Position Independent Code self sufficiency of combining program.
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, Defining and Using Procedures Creating Procedures.
Compiler Construction Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
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 ◦
Lecture 10 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
Code Generation Gülfem Savrun Yeniçeri CS 142 (b) 02/26/2013.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
Winter Compiler Construction T12 – Code Generation Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.
Activation Records (in Tiger) CS 471 October 24, 2007.
Compiler Construction Dr. Noam Rinetzky and Orr Tamir School of Computer Science Tel Aviv University
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 22: Unconventional.
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.
Compiler Construction Code Generation Activation Records
Winter Compiler Construction Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
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.
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.
Assembly function call convention
Reading Condition Codes (Cont.)
Computer Architecture and Assembly Language
Instruction Set Architectures
Assembly language.
Winter Compiler Construction
143A: Principles of Operating Systems Lecture 4: Calling conventions
Introduction to Compilers Tim Teitelbaum
Assembly IA-32.
High-Level Language Interface
Recitation 2 – 2/4/01 Outline Machine Model
asum.ys A Y86 Programming Example
Machine-Level Programming 4 Procedures
Stack Frames and Advanced Procedures
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Winter Compiler Construction T10 – IR part 3
Machine-Level Programming III: Procedures Sept 18, 2001
Practical Session 4.
X86 Assembly Review.
Computer Architecture and System Programming Laboratory
Presentation transcript:

Code Generation III

PAs PA4 5.1 – 7.2 PA5 (bonus) 24.1 –

3 PA4 Translate AST to LIR (file.ic -> file.lir) Instruction list for every method Dispatch table for each class Literal strings (all literal strings in file.ic) Optimizations (optional) Use microLIR simulator

4 microLIR simulator Java application Accepts and executes file.lir 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

5 Compiler IC Program ic x86 executable exe Lexical Analysis Syntax Analysis Parsing ASTSymbol Table etc. Inter. Rep. (IR) Code Generation IC compiler

6 Translating static calls StaticCall _A_foo(a=R1,b=5,c=x),R3 LIR code: # push parameters mov -4(%ebp),%eax # push x push %eax push $5 # push 5 mov -8(%ebp),%eax # push R1 push %eax # push caller-saved registers push %eax push %ecx push %edx call _A_foo # pop parameters (3 params*4 bytes = 12) add $12,%esp # pop caller-saved registers pop %edx pop %ecx pop %eax mov %eax,-16(%ebp) # store returned value in R3

7 Translating virtual calls VirtualCall R1.2(b=5,c=x),R3 # push parameters mov -4(%ebp),%eax # push x push %eax push $5 # push 5 # push caller-saved registers push %eax push %ecx push %edx LIR code: # pop parameters (2 params+this * 4 bytes = 12) add $12,%esp # pop caller-saved registers pop %edx pop %ecx pop %eax mov %eax,-12(%ebp) # store returned value in R3 x y DVPtr R1 0_A_rise 1_A_shine 2_A_twinkle _DV_A # Find address of virtual method and call it mov -8(%ebp),%eax # load this push %eax # push this mov 0(%eax),%eax # Load dispatch table address call *8(%eax) # Call table entry 2 (2*4=8)

Library function calls Static function calls Reference type arguments require non- null runtime check 8 class Library { static void println(string s); // prints string s followed by a newline. static void print(string s); // prints string s. static void printi(int i); // prints integer i. static void printb(boolean b); // prints boolean b. static int readi(); // reads one character from the input....

# push local variables of foo sub $12,%esp # 3 local vars+regs * 4 = 12 9 Function prologue/epilogue _A_foo: # prologue push %ebp mov %esp,%ebp # push callee-saved registers push %ebx push %esi push %edi function body # pop callee-saved registers pop %edi pop %esi pop %ebx mov %ebp,%esp pop %ebp ret _A_foo_epilogoue: # extra label for each function

10 Representing dispatch tables class A { void sleep() {…} void rise() {…} void shine() {…} static void foo() {…} } class B extends A { void rise() {…} void shine() {…} void twinkle() {…} } _DV_A: [_A_sleep,_A_rise,_A_shine] _DV_B: [_A_sleep,_B_rise,_B_shine,_B_twinkle] file.ic file.lir # data section.data.align 4 _DV_A:.long _A_sleep.long _A_rise.long _A_shine _DV_B:.long _A_sleep.long _B_rise.long _B_shine.long _B_twinkle file.s PA4 PA5

Runtime checks Insert code to check attempt to perform illegal operations Null pointer check MoveField, MoveArray, ArrayLength, VirtualCall Reference arguments to library functions should not be null Array bounds check MoveArray Array allocation size check __allocateArray(size) – size needs to be >0 Do we need to check argument of __allocateObject(#) ? Division by zero Div, Mod If check fails jump to error handler code that prints a message and gracefully exists program 11

Null pointer check # null pointer check cmp $0,%eax je labelNPE labelNPE: push $strNPE# error message call __println push $1# error code call __exit Single generated handler for entire program 12

Array bounds check # array bounds check (eax = array, ecx = index) mov -4(%eax),%ebx # ebx = length cmp %ecx,%ebx jle labelABE # ebx <= ecx ? cmp $0,%ecx jl labelABE # ecx < 0 ? labelABE: push $strABE # error message call __println push $1 # error code call __exit Single generated handler for entire program 13

Array allocation size check # array size check cmp $0,%eax# eax == array size jle labelASE # eax <= 0 ? labelASE: push $strASE # error message call __println push $1 # error code call __exit Single generated handler for entire program 14

Division by zero check # division by zero check cmp $0,%eax# eax is divisor je labelDBE # eax == 0 ? labelDBE: push $strDBE # error message call __println push $1 # error code call __exit Single generated handler for entire program 15

16 class Library { void println(string s);... } class Hello { static void main(string[] args) { Library.println("Hello world\n"); } Hello world example

17 Representing strings Array preceded by a word indicating the length of the array Hello world\13 String reference n 1

18 Assembly file structure.title"hello.ic“ # global declarations.global __ic_main # data section.data.align 4.int 13 str1:.string "Hello world\n“ # text (code) section.text # align 4 __ic_main: push %ebp # prologue mov %esp,%ebp push $str1 # print(...) call __print add $4, %esp mov $0,%eax# return 0 mov %ebp,%esp# epilogue pop %ebp ret header statically-allocated data: string literals and dispatch tables Method bodies and error handlers

Implementation Hacker’s approach: translate directly to text Not flexible Hard to debug Use objects representation

Possible instruction hierarchy ASMInstr BinOpInstrUnOpInstrCallInstrLabelInstrRetInstr JumpInstrCondJumpInstr ASMInstr

Possible operand hierarchy ASMOperand ImmediateMemory Reg Variable Parameter Displacement IntegerLiteral X86Reg

Implementation example class BinOpInstr extends ASMInstr { public final Operand src; public final Operand dst; public final String name; public BinOpInstr(String name, Operand src, Operand dst) { // Sanity check if (src instanceof Memory && dst instanceof Memory) { throw new RuntimeException(“Too many memory operands for arith instruction!”); this.name = name; this.src = src; this.dst = dst; } public String toString() { if (ASMInstr.intelSyntax) { return name + “ “ + src + “,” + dst; } else { return name + “ “ + dst + “,” + src; } } }

23 From assembly to executable Lexical Analysis Syntax Analysis Parsing ASTSymbol Table etc. Inter. Rep. (IR) Code Generation IC Program prog.ic x86 assembly prog.s x86 assembly prog.s libic.a (libic + gc) GNU assembler prog.o GNU linker prog.exe

24 Optimizations More efficient register allocation for statements IC statement Basic Block Method … Eliminate unnecessary labels and jumps Post-translation pass

25 Optimizing labels/jumps If we have subsequent labels: _label1: _label2: We can merge labels and redirect jumps to the merged label If we have jump label1 _label1: Can eliminate jump Eliminate labels not mentioned by any instruction