36
Compiler construction in4020 – lecture 11 Koen Langendoen Delft University of Technology The Netherlands

Compiler construction in4020 – lecture 11

Embed Size (px)

DESCRIPTION

Compiler construction in4020 – lecture 11. Koen Langendoen Delft University of Technology The Netherlands. Assignment 1: results. test results (3 - 14) expression priorities error recovery corner cases code ( 5- 7) Makefile documentation ( 5- 7) 2 pages!!. - PowerPoint PPT Presentation

Citation preview

Page 1: Compiler construction in4020 –  lecture  11

Compiler constructionin4020 – lecture 11

Koen Langendoen

Delft University of TechnologyThe Netherlands

Page 2: Compiler construction in4020 –  lecture  11

Assignment 1: results

• test results (3-14)• expression priorities• error recovery• corner cases

• code (5-7)• Makefile

• documentation (5-7)• 2 pages!!

scores

0

5

10

15

20

25

30

1 2 3 4 5 6 7 8 9

thoroughly test your compiler

Page 3: Compiler construction in4020 –  lecture  11

Summary of lecture 10

• assemblers & linkers

• OO programming• translation to C

• method tablesB-object B-class

ptr to B

ptr to A inside B

a1

a2

b1

m1_A_A

m2_A_B

m3_B_B

Page 4: Compiler construction in4020 –  lecture  11

Quiz

[Assignment 2] Which of the following features are part of OO-Asterix?

• inheritance

• method overriding

• polymorphism

• dynamic binding

• (dependent) multiple inheritance

Do we need method tables? If so, which flavor?

Page 5: Compiler construction in4020 –  lecture  11

Overview

• routine types• activation records

• invocation

• values

• code generation for control flow statements• boolean expressions

• selection statements

• repetition statements

Page 6: Compiler construction in4020 –  lecture  11

Routines and their activations

• functionality of a routine call• supplying a new computing environment

• passing information to the new environment

• transfer of the flow-of-control to the new environment and (guaranteed) return

• returning info from the new environment

Page 7: Compiler construction in4020 –  lecture  11

Activation records

• activation record holds• local variables• parameters• working stack• administration part

• routine activation order• last-in-first-out stack

• otherwise heap

parameter k

...

parameter 1

lexical pointer

registers

dynamic link

return info

local variables

working stack

frame ptr

FP

stack ptr

SP

Page 8: Compiler construction in4020 –  lecture  11

working stack

Routine invocation registers

dynamic link

return address

local variables

FP

SP

parameter k

parameter 1...

• caller builds new stack frame for callee• push parameters• push admin

• transfer control to callee• stack return address, adjust FP• allocate space for local vars• execute code

Page 9: Compiler construction in4020 –  lecture  11

working stack

parameter n

parameter 1...

Routine invocation registers

dynamic link

return address

local variables

FP

SP

parameter k

parameter 1...

• caller builds new stack frame for callee• push parameters• push admin

• transfer control to callee• stack return address, adjust FP• allocate space for local vars• execute code

Page 10: Compiler construction in4020 –  lecture  11

Routine invocation

registers

dynamic link

return address

working stack

parameter n

parameter 1...

registers

dynamic link

return address

local variables

FP

SP

parameter k

parameter 1...

• caller builds new stack frame for callee• push parameters• push admin

• transfer control to callee• stack return address, adjust FP• allocate space for local vars• execute code

Page 11: Compiler construction in4020 –  lecture  11

Routine invocation

registers

dynamic link

return address

working stack

parameter n

parameter 1...

registers

dynamic link

return address

local variables

FP

parameter k

parameter 1...

SP

• caller builds new stack frame for callee• push parameters• push admin

• transfer control to callee• stack return address, adjust FP• allocate space for local vars• execute code

Page 12: Compiler construction in4020 –  lecture  11

Routine invocation

registers

dynamic link

return address

working stack

parameter n

parameter 1...

registers

dynamic link

return address

local variables

parameter k

parameter 1...

local variablesSP

FP

• caller builds new stack frame for callee• push parameters• push admin

• transfer control to callee• stack return address, adjust FP• allocate space for local vars• execute code

Page 13: Compiler construction in4020 –  lecture  11

Routine invocation

• caller builds new stack frame for callee• push parameters• push admin

• transfer control to callee• stack return address, adjust FP• allocate space for local vars• execute code

registers

dynamic link

return address

working stack

parameter n

parameter 1...

registers

dynamic link

return address

local variables

parameter k

parameter 1...

local variables

SP

FP

working stack

Page 14: Compiler construction in4020 –  lecture  11

Nested routines

• activation record contains a lexical pointer or static link that points to outer scope

int aap( int i) {

int noot() {return i+7;}

int mies(int i) {return i+noot();}

return mies(2)-3;

}

static link

dynamic link

return address

parameter i

aap

static link

dynamic link

return address

parameter i

mies

parameter i

nootstatic link

dynamic link

return address

Page 15: Compiler construction in4020 –  lecture  11

Exercise (5 min.)

• Given the GNU C routine:

a) draw the stack that results from the call A(2)

b) how does C access the parameter (a) from A?

void A(int a) {

void B(int b) {

void C(void) {

printf(“C called, a = %d\n”, a);

}

if (b == 0) C() else B(b-1);

}

B(a);

}

Page 16: Compiler construction in4020 –  lecture  11

Answers

Page 17: Compiler construction in4020 –  lecture  11

Answers

dynamic

links

static

links

A 2

B 2

B 1

B 0

CFP->static_link->static_link->param[#i]

Page 18: Compiler construction in4020 –  lecture  11

Break

Page 19: Compiler construction in4020 –  lecture  11

Operations on routines

• calling

• passing as a parameter

• returning as a value

• currying [functional programming, chap 7]

Page 20: Compiler construction in4020 –  lecture  11

Passing a routine as a parameter

int aap(int i) {return i+7;}

int noot(int i, int (*f)(int)) {return (*f)(i);}

int mies(int i) {return noot(i,aap);}

flat

int noot(int (*f)()) {return (*f)();}

int mies(int i) {

int aap() {return i+7;}

return noot(aap);

}

nested

static link

address

address

Page 21: Compiler construction in4020 –  lecture  11

Returning a routine as a value

typedef int (*fptr)(int);

int aap(int i) {return i+7;}

fptr mies() {return aap;}

flat

return routine address

typedef int (*fptr)();

fptr mies(int i) {

int aap() {return i+7;}

return aap;

}

nested

return routine descriptor and

allocate activation records on the heap

Page 22: Compiler construction in4020 –  lecture  11

Lambda lifting

• remove static links, only global routines

• out-of-scope variables• referencing: pass additional pointers

• creating: heap allocation

typedef int (*fptr)();

fptr mies(int i) {

int aap() {return i+7;}

return aap;

}

nested

int aap(int *i) {return *i+7;}

fptr mies(int i) {

int *_i = malloc(sizeof(i));

*_i = i;

return closure(aap,_i);

}

lifted

Page 23: Compiler construction in4020 –  lecture  11

Code generation forcontrol flow statements

• glue between basic blocks

transfer type target instruction

jump GOTO label

indirect jump GOTO register

conditional jump IF a b THEN GOTO label

cmpl %eax,%edx

jle L3

Page 24: Compiler construction in4020 –  lecture  11

Boolean expressionsin flow of control

• why special code generation?• performance: avoid conversions

• lazy semantics: || and && in C

if (list != NULL &&

list->key < key) {

...

}

Code expr( ”list != NULL”, r1)Code expr( ” list->key < key”, r2)IF NOT(r1 and r2) THEN GOTO label-end…label-end:

Page 25: Compiler construction in4020 –  lecture  11

PROCEDURE Boolean control code(Node, True label, False label):

SELECT Node.type

CASE Lazy and type:

Boolean control code( Node.left, No label, False label);

Boolean control code( Node.right, True label, False label);

CASE Lazy or type:

Boolean control code( Node.left, True label, No label);

Boolean control code( Node.right, True label, False label);

CASE Comparison type:

Code expr( Node.left, R1)

Code expr( Node.right, R2);

IF True label /= No label):

Emit(”IF” R1 Node.op R2 ”THEN GOTO” True label);

IF False label /= No label):

Emit( ”GOTO” False label);

ELSE

Emit(”IF” R1 Inv(Node.op) R2 ”THEN GOTO” False label);

PROCEDURE Boolean control code(Node, True label, False label):

SELECT Node.type

CASE Lazy and type:

Boolean control code( Node.left, No label, False label);

Boolean control code( Node.right, True label, False label);

CASE Lazy or type:

Boolean control code( Node.left, True label, No label);

Boolean control code( Node.right, True label, False label);

CASE Comparison type:

Code expr( Node.left, R1)

Code expr( Node.right, R2);

IF True label /= No label):

Emit(”IF” R1 Node.op R2 ”THEN GOTO” True label);

IF False label /= No label):

Emit( ”GOTO” False label);

ELSE

Emit(”IF” R1 Inv(Node.op) R2 ”THEN GOTO” False label);

PROCEDURE Boolean control code(Node, True label, False label):

SELECT Node.type

CASE Lazy and type:

Boolean control code( Node.left, No label, False label);

Boolean control code( Node.right, True label, False label);

CASE Lazy or type:

Boolean control code( Node.left, True label, No label);

Boolean control code( Node.right, True label, False label);

CASE Comparison type:

Code expr( Node.left, R1)

Code expr( Node.right, R2);

IF True label /= No label):

Emit(”IF” R1 Node.op R2 ”THEN GOTO” True label);

IF False label /= No label):

Emit( ”GOTO” False label);

ELSE

Emit(”IF” R1 Inv(Node.op) R2 ”THEN GOTO” False label);

PROCEDURE Boolean control code(Node, True label, False label):

SELECT Node.type

CASE Lazy and type:

Boolean control code( Node.left, No label, False label);

Boolean control code( Node.right, True label, False label);

CASE Lazy or type:

Boolean control code( Node.left, True label, No label);

Boolean control code( Node.right, True label, False label);

CASE Comparison type:

Code expr( Node.left, R1)

Code expr( Node.right, R2);

IF True label /= No label):

Emit(”IF” R1 Node.op R2 ”THEN GOTO” True label);

IF False label /= No label):

Emit( ”GOTO” False label);

ELSE

Emit(”IF” R1 Inv(Node.op) R2 ”THEN GOTO” False label);

PROCEDURE Boolean control code(Node, True label, False label):

SELECT Node.type

CASE Lazy and type:

Boolean control code( Node.left, No label, False label);

Boolean control code( Node.right, True label, False label);

CASE Lazy or type:

Boolean control code( Node.left, True label, No label);

Boolean control code( Node.right, True label, False label);

CASE Comparison type:

Code expr( Node.left, R1)

Code expr( Node.right, R2);

IF True label /= No label):

Emit(”IF” R1 Node.op R2 ”THEN GOTO” True label);

IF False label /= No label):

Emit( ”GOTO” False label);

ELSE

Emit(”IF” R1 Inv(Node.op) R2 ”THEN GOTO” False label);

Page 26: Compiler construction in4020 –  lecture  11

The if-statement

if -statement

IF THEN ELSEcondition true stmt false stmt

Boolean control code( condition, No_label, false_label)

Code statement( true stmt)

GOTO end_label;

false_label:

Code statement( false stmt)

end_label:

Page 27: Compiler construction in4020 –  lecture  11

The if-statement

if -statement

IF THEN ELSEcondition true stmt false stmt

Boolean control code( condition, No_label, false_label)

Code statement( true stmt)

GOTO end_label:

false_label:

Code statement( false stmt)

end_label:

Boolean control code( condition, No_label, end_label)

Code statement( true stmt)

end_label:

Page 28: Compiler construction in4020 –  lecture  11

The case-statement

CASE expression IN

I1 : statement1 ...

In : statementn

ELSE statementdefault

END CASE

Code expr( expression, Rexpr)

IF Rexpr = I1 THEN GOTO label_1

...

IF Rexpr = In THEN GOTO label_n

GOTO label_else;

label_1:

Code statement (statement1)

GOTO end_label;

...

label_n:

Code statement (statementn)

GOTO end_label;

label_else:

Code statement (statementdefault )

end_label:

Page 29: Compiler construction in4020 –  lecture  11

The case-statement

CASE expression IN

I1 : statement1 ...

In : statementn

ELSE statementdefault

END CASE Code expr( expression, Rexpr)

IF Rexpr < Ilow THEN GOTO label_else

IF Rexpr > Ihigh THEN GOTO label_else

GOTO case_table[Rexpr- Ilow];

label_1:

...

.data

case_table:

label_low

...

label_high

jump table

Page 30: Compiler construction in4020 –  lecture  11

test_label:

Boolean control code( condition, No_label, end_label)

Code statement( statement)

GOTO test_label;

end_label:

GOTO test_label;

loop_label:

Code statement( statement)

test_label:

Boolean control code( condition, loop_label , No_label)

The while-statement

while -statement

WHILE DO END WHILEcondition statement

Page 31: Compiler construction in4020 –  lecture  11

The for-statement

FOR i IN lower bound .. upper bound DO

statementEND FOR

i := lower bound;

tmp_ub := upper bound;WHILE i <= tmp_ub DO

statement i := i+1;

END FOR

Code expr( lower bound, Ri)

Code expr( upper bound, Rub)

IF Ri > Rub THEN GOTO end_label

loop_label:

Code statement (statement)

IF Ri = Rub THEN GOTO end_label

Ri := Ri+1;

GOTO loop_label;

end_label:

test before

increment

Page 32: Compiler construction in4020 –  lecture  11

Exercise (3 min.)

• give a translation schema for repeat statements

repeat -statement

REPEAT UNTIL END REPEATstatement condition

Page 33: Compiler construction in4020 –  lecture  11

Answers

Page 34: Compiler construction in4020 –  lecture  11

repeat_label:

Code statement( statement)

Boolean control code( condition, No_label , repeat_label)

Answers

repeat -statement

REPEAT UNTIL END REPEATstatement condition

Page 35: Compiler construction in4020 –  lecture  11

Summary

• (nested) routines• activation records

• routine descriptors

• code generation for control flow statements• conditional expressions: true & false labels

• case-statement: jump table

• for-statement: overflow

static link

dynamic link

return address

parameter i

mies

parameter i

nootstatic link

dynamic link

return address

Page 36: Compiler construction in4020 –  lecture  11

Homework

• study sections:• 6.5 Code generation for modules

• assignment 2:• make Asterix OO

• deadline June 4 08:59

• print handout for next week [blackboard]