Top Banner
Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University
32

Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

Compiler Construction

Recap

Rina Zviel-Girshin and Ohad ShachamSchool of Computer Science

Tel-Aviv University

Page 2: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

22

Admin

You may submit PA4 until Jan 27FIRM

Page 3: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

33

Exam

31/01/2010 at 9:00Look for previous exams on website

Possible questionsParsingExtend ICActivation records…

Page 4: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

44

Register allocation

Sethi Ullman can only handle expressions without side effect

Global register allocation

IR registers are treated as local variables

When we have an actual spill we use the stack

Page 5: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

55

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 6: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

66

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 7: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

77

Sethi Ullman

On what type of tree SU achieved the worst case respect to the tree’s height?

Page 8: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

88

Sethi Ullman

On what type of tree SU achieved the best case respect to the tree’s height?

.

.

.

Page 9: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

99

Sethi Ullman

On what type of tree, the maximal ratio between registers allocated by traversing the tree from left to right or from right to left

.

.

.

Page 10: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

1010

Global Register Allocation

Register allocation technique based on graph coloring Graph coloring – NP complete

Uses heuristics to approximate

Page 11: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

1111

Build: Construct the interference graph

Simplify: Recursively remove nodes with less than K neighbors ; Push removed nodes into stack

Potential-Spill: Spill some nodes and remove nodes

Push removed nodes into stack

Select: Assign actual registers (from simplify/spill stack)

Actual-Spill: Spill some potential spills and repeat the process

Page 12: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

1212

Global Register Allocation

Given k registers on which graph the algorithm will generate an actual spill?

Page 13: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

1313

Global Register Allocation

Given an interference graph How can we compute the best case of the global register

allocation on the graph?

Page 14: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

1414

Global Register Allocation

Given an interference graph How can we compute the worst case of the global register

allocation on the graph?

Page 15: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

1515

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 16: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

1616

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 registersPop callee-save registersPop callee activation recordPop old base-pointerpop return addressJump to address

Pop parametersPop caller-save registers

Call sequences

… …

Return address

Local 1Local 2

……

Local n

Previous fp

Param n…

param1

FP

SPReg 1

…Reg n

SP

SP

SP

SP

FP

SP

Page 17: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

1717

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 18: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

1818

Question – Activation record

Run the program on the input and show the stack at after …

Add a “newS” operation that allocates on the stack How to do it?

Page 19: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

1919

Question – Activation record

Lexical analyzer Add a new token “NEWS” "newS“ { return token(sym.NEWS); }

ParserExpr ::= NEWS CLASS_ID:name LP RP

{: RESULT = new NewSClass(parser.getLine(),name); :} |

NEWS Type:type LB Expr:size RB {: RESULT = new NewSArray(type, size); :} | …

Page 20: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

2020

Question – Activation record

Semantic analysis Use the types of “newS” as the ones of “new”

Intermediate representation Introduce new allocation methods __allocateStackObject(s), R1 __allocateStackArray(s), R1 Translate newS exactly as new while using the above

methods Assume that s is size in bytes for object Assume that s is the size of array for arrays

Page 21: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

2121

Question – Activation record

Code generation Need to implement allocation methods in assembly

__allocateStackObject(s) __allocateStackArray(s)

__allocateStackObject(s), R1mov -4(%ebp), %eax // assume s is the first local var

sub %eax,%esp

mov %esp, -8(%ebp) // assume R1 is the second local var (spill)

Page 22: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

2222

Question – Activation record

__allocateStackArray(s), R1mov -4(%ebp), %eax // assume s is the first local var

mov %eax, %ebx

add $1, %ebx

mul $4, %ebx

sub %ebx,%esp

mov %eax, (%esp)

mov %esp, %eax

add $4, %eax

mov %eax, -8(%ebp) // assume R1 is the second local var (spill)

Page 23: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

2323

3מועד ב, שאלה 2006

class ByRefExample { static void main(string [] args) { int x = 5; increment(x); Library.println(x); }

static void increment(int & y) { y = y + 1; }}

Page 24: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

2424

3מועד ב, שאלה 2006

Treat by reference as address Lexer

Add token for &

Parser Add reference type Can appear only as a formal parameter

Semantic analysis New rules for type checking

Compare the actual parameter type and the reference type Treat the scope checking

Page 25: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

2525

3מועד ב, שאלה 2006

IR Add support for by reference Usage of a referenced var should be marked

Code generation Caller

Add the pointer of the actual parameter to the stack instead of the actual parameter itself

Callee Treat the parameter as a pointer

Page 26: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

2626

Question

Add constructors to IC

Treat the constructor as a function and call the function on allocation

Lexical analysis Nothing

Parsing Add a method without return type and allocate a new constructor type

for the AST Allow Object allocation with parameters inside the parenthesis

NEW CLASS_ID:name LP argumentsList RP Extend class allocation node to support include arguments list

Page 27: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

2727

Question

Semantic analysis Check that the constructor name and the class name are the same Check that the actual parameter’s type and the formal parameter are

the same Intermediate representation

Add a new allocation function that keeps parameters for the constructor Allocate the first entry in the DV for the constructor

Code generation Add a call to the constructor after allocation Of course, add the parameters to the stuck and save registers as

needed in every function call

Page 28: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

2828

Question

After implementing constructors add “super” to IC

class A {A(int a) {…}

}

class B extends A { B(int b) {

super(b); … }

}

•Super should be the first call inside the constructor

Page 29: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

2929

Question

Lexical analysis Add a new token SUPER

Parsing Add a rule for super? Add a new AST node for super?

Page 30: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

3030

Question

Semantic analysis Check that super appears only inside constructors Check that super is the first action Check that the super class contains constructor with

matching parameter Bind the call to the proper constructor

Page 31: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

3131

Question

Intermediate representation Add a new call type to a super constructor

Known at compile time Should get “this” at runtime

Code generation Translate the super call to call that gets this as the

first argument

Page 32: Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

3232

Questions

Extend IC with function overloading Show the problem that can achieve by doing static

function overloading Do we know a solution for this problem? Add runtime handling for function overloading in order to

solve this problem Can we use some compile time optimization to reduce

the runtime overhead?