compiler construction intermediate representation iii activation records ran shaham and ohad shacham...

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

Upload: beryl-brittney-payne

Post on 18-Dec-2015

234 views

Category:

Documents


5 download

TRANSCRIPT

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

Compiler Construction

Intermediate Representation IIIActivation Records

Ran Shaham and Ohad ShachamSchool of Computer Science

Tel-Aviv University

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

22

Intermediate representation

Allows language-independent, machine independent optimizations and transformations

Easy to translate from AST Easy to translate to assembly

AST IR

Pentium

Java bytecode

Sparc

optimize

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

33

Multiple IRs

Some optimizations require high-level structure Others more appropriate on low-level code

Solution: use multiple IR stages

AST LIR

Pentium

Java bytecode

Sparc

optimize

HIR

optimize

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

44

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

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

55

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

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

66

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

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

77

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,R1Mul c,R1Add a,R1

Move c,R1Move b,R2Mul R1,R2Move R2,R3Move a,R4Add R3,R4Move R4,R5

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

88

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 …

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

99

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

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

1010

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

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

1111

Example

R0 := TR[a+(b+(c*d))]

b

c d

*

+

+

aR0

R1

R2

Translation uses all optimizationsshown until now uses 3 registers

R2

R1

left child first

b

c d

*

+

+

a

R0

Managed to save two registers

R0

R0

right child firstR0R0

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

1212

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

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

1313

Weighted reg. alloc. example

b

5 c

*

array access

+

a

base index

W=1

W=0 W=1

W=1W=1

W=2

W=2

Phase 1: - check absence of side-effects in expression tree - assign weight to each AST node

R0 := TR[a+b[5*c]]

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

1414

Weighted reg. alloc. example

R0 := TR[a+b[5*c]]

b

5 c

*

array access

+

a

R0

R0

R0

R0

base index

W=1

W=0 W=1

W=1W=1

W=2

W=2

R1

Phase 2: use weights to decide on order of translation

Heavier sub-tree

Heavier sub-tree

Move c,R0

Mul 5,R0

Move b,R1

MoveArray R1[R0],R0

Add a,R0

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

1515

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

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

1616

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<ASTStringLiteral,String>) 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

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

1717

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

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

1818

LIR vs. assembly

LIRAssembly

#RegistersUnlimited Limited

Function callsImplicitRuntime stack

Instruction setAbstractConcrete

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

1919

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)

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

2020

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

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

2121

Runtime stack

Stack of activation recordsCall = push new activation recordReturn = pop activation recordOnly one “active” activation record – top of

stack

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

2222

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

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

2323

X86 runtime stack

RegisterUsage

ESPStack pointer

EBP Base 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

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

2424

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 registersCallee’s responsability

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

2525

call

caller

callee

return

caller

Caller push code

Callee push code

(prologue)

Callee pop code

(epilogue)

Caller pop code

Push caller-save registersPush actual parameters (in reverse order)

push return addressJump to call address

Push current base-pointerbp = spPush local variablesPush callee-save registers

Pop callee-save registersPop callee activation recordPop old base-pointer

pop return addressJump to address

Pop parametersPop caller-save registers

Call sequences

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

2626

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 registersPush actual parameters (in reverse order)

push return addressJump to call address

Push current base-pointerbp = spPush local variables (callee variables)Push callee-save registers

Pop callee-save registersPop callee activation recordPop old base-pointer

pop return addressJump to address

Pop parametersPop caller-save registers

Call sequences – Foo(42,21)

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

2727

“To Callee-save or toCaller-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 pointerUse %eax for return value

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

2828

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 1Local 2

…………

Local n-1Local n

Previous fp

Param n…

param1FP+8

FP-4

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

2929

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

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

3030

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

31EBP+12

y

main

’s fra

me

bar’s

fram

e

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