25
Code Generation III

Code Generation III

Embed Size (px)

DESCRIPTION

Code Generation III. PAs. PA4 5.1 – 7.2 PA5 (bonus) 24.1 – 24.2. 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. - PowerPoint PPT Presentation

Citation preview

Page 1: Code Generation III

Code Generation III

Page 2: Code Generation III

PAs

PA45.1 – 7.2

PA5 (bonus)24.1 – 24.2

22

Page 3: Code Generation III

33

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

Page 4: Code Generation III

44

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

Page 5: Code Generation III

55

Compiler

ICProgram

ic

x86 executable

exeLexicalAnalysi

s

Syntax Analysi

s

Parsing

AST Symbol

Tableetc.

Inter.Rep.(IR)

CodeGeneration

IC compiler

Page 6: Code Generation III

66

Translating static callsStaticCall _A_foo(a=R1,b=5,c=x),R3LIR code:

# push parametersmov -4(%ebp),%eax # push xpush %eaxpush $5 # push 5mov -8(%ebp),%eax # push R1push %eax

# push caller-saved registerspush %eaxpush %ecxpush %edx

call _A_foo

# pop parameters (3 params*4 bytes = 12)add $12,%esp

# pop caller-saved registerspop %edxpop %ecxpop %eax

mov %eax,-16(%ebp) # store returned value in R3

Page 7: Code Generation III

77

Translating virtual callsVirtualCall R1.2(b=5,c=x),R3

# push parametersmov -4(%ebp),%eax # push xpush %eaxpush $5 # push 5

# push caller-saved registerspush %eaxpush %ecxpush %edx

LIR code:

# pop parameters (2 params+this * 4 bytes = 12)add $12,%esp

# pop caller-saved registerspop %edxpop %ecxpop %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 itmov -8(%ebp),%eax # load thispush %eax # push thismov 0(%eax),%eax # Load dispatch table addresscall *8(%eax) # Call table entry 2 (2*4=8)

Page 8: Code Generation III

Library function calls

Static function callsReference type arguments require non-

null runtime check

88

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. ...

Page 9: Code Generation III

# push local variables of foosub $12,%esp # 3 local vars+regs * 4 = 12

99

Function prologue/epilogue_A_foo:# prologuepush %ebpmov %esp,%ebp

# push callee-saved registerspush %ebxpush %esipush %edi

function body

# pop callee-saved registerspop %edipop %esipop %ebx

mov %ebp,%esppop %ebpret

_A_foo_epilogoue: # extra label for each function

Page 10: Code Generation III

1010

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

Page 11: Code Generation III

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

1111

Page 12: Code Generation III

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

1212

Page 13: Code Generation III

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

1313

Page 14: Code Generation III

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

1414

Page 15: Code Generation III

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

1515

Page 16: Code Generation III

1616

class Library { void println(string s); ...}

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

Hello world example

Page 17: Code Generation III

1717

Representing strings

Array preceded by a word indicating the length of the array

H e l l o w o r l d \13

String reference

4 1 1 1 1 1 1 1 1 1 1 1 1

n

1

Page 18: Code Generation III

1818

Assembly file structure.title "hello.ic“

# global declarations.global __ic_main

# data section.data

.align 4 .int 13str1: .string "Hello world\n“

# text (code) section.text

#----------------------------------------------------.align 4

__ic_main:push %ebp # prologuemov %esp,%ebp

push $str1 # print(...)call __printadd $4, %esp

mov $0,%eax # return 0

mov %ebp,%esp # epiloguepop %ebpret

header

statically-allocateddata: string literalsand dispatch tables

Method bodiesand error handlers

Page 19: Code Generation III

Implementation

Hacker’s approach: translate directly to text Not flexible Hard to debug

Use objects representation

Page 20: Code Generation III

Possible instruction hierarchy

ASMInstr

BinOpInstr UnOpInstr CallInstrLabelInstr RetInstr

JumpInstr CondJumpInstr

ASMInstr

Page 21: Code Generation III

Possible operand hierarchy

ASMOperand

Immediate Memory

Reg

Variable

Parameter

Displacement

Integer Literal

X86Reg

Page 22: Code Generation III

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

Page 23: Code Generation III

2323

From assembly to executable

LexicalAnalysi

s

Syntax Analysi

s

Parsing

AST Symbol

Tableetc.

Inter.Rep.(IR)

CodeGeneration

ICProgram

prog.ic

x86 assembly

prog.s

x86 assembly

prog.s

libic.a(libic + gc)

GNU assembler

prog.o GNUlinker prog.exe

Page 24: Code Generation III

2424

Optimizations

More efficient register allocation for statements IC statement Basic Block Method …

Eliminate unnecessary labels and jumpsPost-translation pass

Page 25: Code Generation III

2525

Optimizing labels/jumps

If we have subsequent labels:_label1:_label2:

We can merge labels and redirect jumps to the merged label

If we havejump label1_label1:Can eliminate jump

Eliminate labels not mentioned by any instruction