lesson 13 cdt301 – compiler theory, spring 2011 teacher: linus källberg

36
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Upload: frederica-wright

Post on 02-Jan-2016

215 views

Category:

Documents


2 download

TRANSCRIPT

Lesson 13

CDT301 – Compiler Theory, Spring 2011Teacher: Linus Källberg

2

Outline

• Overview of Trac42VM• Activation records

OVERVIEW OF TRAC42VM

3

4

Overview of Trac42VM

• A stack• A code memory area• Registers:

– PC: Points to the next instruction to execute– SP: Points to the top of the stack– FP: Points to the current activation record

5

Memory arrangement

• Memory is partitioned into different sections– Executable code of a program*– Stack – e.g. local variables*– Static – global and static memory– Heap – dynamically allocated memory

• The stack and the heap grow and shrink during runtime

* Used in Trac42VM

6

Lab 3 vs. lab 1

• No named program points, i.e., labels or functions– We use absolute code addresses

• No names on variables or (an no @)– We use relative stack addresses

• New data types: int, bool, and string– Need different operations– Need different amount of stack space

Data types in Trac42VMData type SizeInt 1Bool 1String 100

7

Instructions in Trac42VM

• PUSHINT, PUSHBOOL, PUSHSTRING• RVALINT, RVALBOOL, RVALSTRING• ASSINT, ASSBOOL, ASSSTRING• EQINT, EQBOOL, EQSTRING,• LTSTRING, LESTRING• WRITEINT, WRITEBOOL, WRITESTRING• LINK, UNLINK 8

ACTIVATION RECORDS

9

10

Function calls and the stack

• Conveys information:– To called function: arguments– From called function: return value– Program state necessary to resume execution after

the call (e.g., the return-to address)• The information held for one function call:

activation record (or stack frame)– The frame pointer register points to the current

activation record– One activation record for each active function

11

(Possible) layout of anactivation record

Actual arguments

Return address

Frame pointer

Local variables

Temporary variables

Return value

Top

Bottom

FP-relative addresses

int funcA() { int x = funcB(23); return x + funcC(11, 17);}

int funcB(int x) { if (x == 0) return funcC(12, 13); return x * funcB(x – 1);}

int funcC(int x, int y) { return x + y;}

12

Example

13

Caller’srecord

42

Start here

42

42FP

SP

int twice(int x){3 int y;4 y = 2 * x;5 return y;}

7 a = twice(13);8 x = ...

14

Example

Return value

Caller’srecord

?? 41

42

int twice(int x){3 int y;4 y = 2 * x;5 return y;}

7 a = twice(13);8 x = ...41

42FP

SP

Example

15

Actual arguments

Return value

Caller’srecord

13 40

41

42

int twice(int x){3 int y;4 y = 2 * x;5 return y;}

7 a = twice(13);8 x = ...40

42FP

SP

Example

16

Actual arguments

Return address

Return value

Caller’srecord

13

8 *

42

int twice(int x){3 int y;4 y = 2 * x;5 return y;}

7 a = twice(13);8 x = ...39

42FP

SP

40

39

41* does not map to high-level view

Example

17

Actual arguments

Return address

Frame pointer

Return value

Caller’srecord

13

8

42

42

int twice(int x){3 int y;4 y = 2 * x;5 return y;}

7 a = twice(13);8 x = ...38

38FP

SP

40

39

38

41

Example

18

Actual arguments

Return address

Frame pointer

Local variables

Return value

Callersrecord

13

8

42

??

42

int twice(int x){3 int y;4 y = 2 * x;5 return y;}

7 a = twice(13);8 x = ...37

38FP

SP

40

39

38

37

41

Example

19

Actual arguments

Return address

Frame pointer

Local variables

Return value

Callersrecord

13

8

42

26

42

int twice(int x){3 int y;4 y = 2 * x;5 return y;}

7 a = twice(13);8 x = ...37

38FP

SP

40

39

38

37

41

Example

20

Actual arguments

Return address

Frame pointer

Local variables

Return value

Callersrecord

13

8

42

26

26

42

int twice(int x){3 int y;4 y = 2 * x;5 return y;}

7 a = twice(13);8 x = ...37

38FP

SP

40

39

38

37

41

Example

21

Actual arguments

Return address

Return value

Callersrecord

13

8

26

42

int twice(int x){3 int y;4 y = 2 * x;5 return y;}

7 a = twice(13);8 x = ...39

42FP

SP

40

39

41

Example

22

Actual arguments

Return value

Callersrecord

13

26

40

41

42

int twice(int x){3 int y;4 y = 2 * x;5 return y;}

7 a = twice(13);8 x = ...40

42FP

SP

Example

23

Return value

Callersrecord

26 41

42

int twice(int x){3 int y;4 y = 2 * x;5 return y;}

7 a = twice(13); /*-> 26 */8 x = ...41

42FP

SP

24

Calling conventions – responsibility

Calling function• Reserve return value space• Push actual arguments (in

reverse order)• Push the return-to address• Call the function• Pop actual arguments

Called function• Save and re-set FP• Reserve space for local

variables• Assign the return value• Restore FP• Return to caller

25

In the caller

• Reserve space for the return value

• Evaluate and push each argument onto the stack

• Call the function and push the return-to address at the same time

• Pop the arguments

DECL size (or PUSH)

BSR address

POP size

26

In the callee

• Push FP and set FP to a stack address that is fixed throughout the function’s execution

• Reserve space for all the local variables

• Do computations…

• Restore FP to the value it had when entering this function

• Return to the caller

LINK

DECL size

UNLINK

RTS

27

The tasks of the compiler

• Calculate the addresses relative to FP:– Variables– Parameters – Return value– Generate instructions using proper addresses

• Generate code using the offsets

Offset calculation

28

Actual arguments

Return address

Frame pointer

Local variables

Return value

Callersrecord

13

8

42

26

26

42

int twice(int x){3 int y;4 y = 2 * x;5 return y;}

7 a = twice(13);8 x = ...37

38FP

SP

What are the offsets for y, x and the return value?

40

39

38

37

41

A complete exampleRun the code below and assume that

initially, FP = 101 and SP = 100.

int twice(int x) {

int y;

y = 2 * x;

return y;

}

void trac42() {

int a;

a = twice(13);

}29

save FPy&y2x2 * xy = 2 * x&@y@ = yrestore FPreturnrestore FPreturn

save FPa&a@arg. 13call twicepop arg.a = @restore FPreturn

2 [twice] 3 LINK 4 DECL 1 5 LVAL -1(FP) 6 PUSHINT 2 7 RVALINT 2(FP) 8 MULT 9 ASSINT10 LVAL 3(FP)11 RVALINT -1(FP)12 ASSINT13 UNLINK14 RTS15 UNLINK16 RTS17 [trac42]18 LINK19 DECL 120 LVAL -1(FP)21 DECL 122 PUSHINT 1323 BSR 224 POP 125 ASSINT26 UNLINK27 RTS

Exercise (1)Show the stack contents as they are

just before instruction 26 if initially, FP = 201 andSP = 200.

int abs(int x) {if (x < 0)

x = -x;return x;

}

void trac42() {int a;a = abs(-7);

}

30

2 [abs] 3 LINK 4 RVALINT 2(FP) 5 PUSHINT 0 6 LTINT 7 BRF 13 8 LVAL 2(FP) 9 RVALINT 2(FP)10 NEG11 ASSINT12 BRA 1313 LVAL 3(FP)14 RVALINT 2(FP)15 ASSINT16 UNLINK17 RTS18 [trac42]19 LINK20 DECL 121 LVAL -1(FP)22 DECL 123 PUSHINT 724 NEG25 BSR 226 POP 127 ASSINT28 UNLINK29 RTS

save FPx0x < 0jump to else&xx-xx = -xjump past else&@x@ = xrestore FPreturn

save FPa&a@7arg. -7call abspop the arg.a = @restore FPreturn

31

Exercise (2)

Revert this to Trac42 code (invent names of identifiers as needed).

Hint 1: The WRITEINT instruction does not pop the written value

Hint 2: Do a trace first!

2 [id] 3 LINK 4 RVALINT 2(FP) 5 RVALINT 3(FP) 6 ADD 7 WRITEINT 8 POP 1 9 UNLINK 10 RTS 11 [trac42] 12 LINK 13 DECL 1 14 LVAL -1(FP) 15 PUSHINT 42 16 ASSINT 17 PUSHINT 27 18 RVALINT -1(FP) 19 PUSHINT 99 20 ADD 21 BSR 2 22 POP 1 23 POP 1 24 UNLINK 25 RTS

32

Exercise (3)Translate this to Trac42VM code.

void print(string s1, int x, string s2, int y){ write s1; write x; write s2; write y;}

void trac42 (){ print("One: ", 1, "Two:", 2);}

33

Parameter passing

• Call by value – push a copy of the value– Used in e.g. C and Trac42

• Call by reference – push a pointer to it– E.g. Java objects and references in C++– Usually combined with call by value

• Other:– Call by name – similar to macros in C

34

Local functionsvoid trac42(void) { int x; foo(int y) { if (y == 0) return; x *= 2; foo(y – 1); } x = 1; foo(2);}• How is x found on the stack?• How is y found on the stack, not confusing it with the y of the (recursive)

caller?

35

Local functions

Actual arguments

Return address

Frame pointer

Local variables

Return value

Access link

36

Conclusion

• An activation record holds information on the stack necessary in function calls

• The frame pointer points to the activation record of the current active function

• Local variables, actual arguments, and the return value are accessed through the frame pointer