Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg."— Presentation transcript:

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

2 Outline Overview of Trac42VM Activation records 2

3 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 4

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 5

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 6

7 Data types in Trac42VM Data typeSize Int1 Bool1 String100 7

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

9 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 10

11 (Possible) layout of an activation record 11 Actual arguments Return address Frame pointer Local variables Temporary variables Return value Top Bottom

12 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

13 Example 13 Caller’s record 42 Start here 42 FP SP int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x =...

14 Example 14 Return value Caller’s record ?? 41 42 int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x =... 41 42 FP SP

15 Example 15 Actual arguments Return value Caller’s record 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 42 FP SP

16 Example 16 Actual arguments Return address Return value Caller’s record 13 8 * 42 int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x =... 39 42 FP SP 40 39 41 * does not map to high-level view

17 Example 17 Actual arguments Return address Frame pointer Return value Caller’s record 13 8 42 int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x =... 38 FP SP 40 39 38 41

18 Example 18 Actual arguments Return address Frame pointer Local variables Return value Callers record 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 38 FP SP 40 39 38 37 41

19 Example 19 Actual arguments Return address Frame pointer Local variables Return value Callers record 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 38 FP SP 40 39 38 37 41

20 Example 20 Actual arguments Return address Frame pointer Local variables Return value Callers record 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 38 FP SP 40 39 38 37 41

21 Example 21 Actual arguments Return address Return value Callers record 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 42 FP SP 40 39 41

22 Example 22 Actual arguments Return value Callers record 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 42 FP SP

23 Example 23 Return value Callers record 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 42 FP 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 24

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 25

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 26

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 27

28 Offset calculation 28 Actual arguments Return address Frame pointer Local variables Return value Callers record 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 38 FP SP What are the offsets for y, x and the return value? 40 39 38 37 41

29 A complete example Run 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 FP y &y 2 x 2 * x y = 2 * x &@ y @ = y restore FP return restore FP return save FP a &a @ arg. 13 call twice pop arg. a = @ restore FP return 2 [twice] 3 LINK 4 DECL 1 5 LVAL -1(FP) 6 PUSHINT 2 7 RVALINT 2(FP) 8 MULT 9 ASSINT 10 LVAL 3(FP) 11 RVALINT -1(FP) 12 ASSINT 13 UNLINK 14 RTS 15 UNLINK 16 RTS 17 [trac42] 18 LINK 19 DECL 1 20 LVAL -1(FP) 21 DECL 1 22 PUSHINT 13 23 BSR 2 24 POP 1 25 ASSINT 26 UNLINK 27 RTS

30 Exercise (1) Show the stack contents as they are just before instruction 26 if initially, FP = 201 and SP = 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 NEG 11 ASSINT 12 BRA 13 13 LVAL 3(FP) 14 RVALINT 2(FP) 15 ASSINT 16 UNLINK 17 RTS 18 [trac42] 19 LINK 20 DECL 1 21 LVAL -1(FP) 22 DECL 1 23 PUSHINT 7 24 NEG 25 BSR 2 26 POP 1 27 ASSINT 28 UNLINK 29 RTS save FP x 0 x < 0 jump to else &x x -x x = -x jump past else &@ x @ = x restore FP return save FP a &a @ 7 arg. -7 call abs pop the arg. a = @ restore FP return

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! 31 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); } 32

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 33

34 Local functions void 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? 34

35 Local functions 35 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 36


Download ppt "Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg."

Similar presentations


Ads by Google