360 likes | 445 Views
Lesson 13. CDT301 – Compiler Theory , Spring 2011 Teacher : Linus Källberg. Outline. Overview of Trac42VM Activation records. Overview of Trac42VM. Overview of Trac42VM. A stack A code memory area Registers: PC: Points to the next instruction to execute
E N D
Lesson 13 CDT301 – CompilerTheory, Spring 2011 Teacher: Linus Källberg
Outline • Overview of Trac42VM • Activationrecords
Overview of Trac42VM • A stack • A codememory area • Registers: • PC: Points to the nextinstruction to execute • SP: Points to the top of the stack • FP: Points to the currentactivationrecord
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
Lab 3 vs. lab 1 • No named program points, i.e., labels or functions • Weuse absolute codeaddresses • No names on variables or (an no @) • Weuse relative stack addresses • New data types: int, bool, and string • Need different operations • Need different amount of stack space
Instructions in Trac42VM • PUSHINT, PUSHBOOL, PUSHSTRING • RVALINT, RVALBOOL, RVALSTRING • ASSINT, ASSBOOL, ASSSTRING • EQINT, EQBOOL, EQSTRING, • LTSTRING, LESTRING • WRITEINT, WRITEBOOL, WRITESTRING • LINK, UNLINK
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
(Possible) layout of anactivation record Bottom Return value Actual arguments Return address Frame pointer Local variables Temporary variables Top
FP-relative addresses intfuncA() { int x = funcB(23); return x + funcC(11, 17); } intfuncB(intx) { if (x == 0) returnfuncC(12, 13); return x * funcB(x – 1); } intfuncC(intx, inty) { return x + y; }
Example Caller’s record 42 int twice(int x) { 3int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x = ... Start here FP 42 42 SP
Example Caller’s record 42 Return value ?? 41 int twice(int x) { 3int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x = ... FP 42 41 SP
Example Caller’s record 42 Return value 41 int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x = ... Actual arguments 13 40 FP 42 40 SP
Example Caller’s record 42 * does not map to high-level view Return value 41 int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x = ... Actual arguments 13 40 Return address 8 * 39 FP 42 39 SP
Example Caller’s record 42 Return value 41 int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x = ... Actual arguments 13 40 Return address 8 39 Frame pointer 42 38 FP 38 38 SP
Example Callers record 42 Return value 41 int twice(int x) { 3int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x = ... Actual arguments 13 40 Return address 8 39 Frame pointer 42 38 Local variables ?? 37 FP 38 37 SP
Example Callers record 42 Return value 41 int twice(int x) { 3int y; 4y = 2 * x; 5 return y; } 7 a = twice(13); 8 x = ... Actual arguments 13 40 Return address 8 39 Frame pointer 42 38 Local variables 26 37 38 FP SP 37
Example Callers record 42 Return value 26 41 int twice(int x) { 3 int y; 4 y = 2 * x; 5return y; } 7 a = twice(13); 8 x = ... Actual arguments 13 40 Return address 8 39 Frame pointer 42 38 Local variables 26 37 38 FP SP 37
Example Callers record 42 Return value 26 41 int twice(int x) { 3 int y; 4 y = 2 * x; 5return y; } 7 a = twice(13); 8 x = ... Actual arguments 13 40 Return address 8 39 42 FP SP 39
Example Callers record 42 Return value 26 41 int twice(int x) { 3 int y; 4 y = 2 * x; 5return y; } 7 a = twice(13); 8 x = ... Actual arguments 13 40 FP 42 SP 40
Example Callers record 42 Return value 26 41 int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); /*-> 26 */ 8 x = ... FP 42 SP 41
Calling conventions – responsibility Calling function Called function Save and re-set FP Reserve space for local variables Assign the return value Restore FP Return to caller • Reserve return value space • Push actual arguments (in reverse order) • Push the return-to address • Call the function • Pop actual arguments
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
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
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 What are the offsets for y, x and the return value? Callers record 42 Return value 26 41 int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x = ... Actual arguments 13 40 Return address 8 39 Frame pointer 42 38 Local variables 26 37 38 FP SP 37
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 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 A completeexample Run the code below and assume that initially, FP = 101 and SP = 100. int twice(intx) { int y; y = 2 * x; return y; } void trac42() { int a; a = twice(13); }
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 pastelse &@ x @ = x restore FP return save FP a &a @ 7 arg. -7 callabs pop the arg. a = @ restore FP return Exercise (1) Show the stack contents as they are just before instruction 26 if initially, FP = 201 andSP = 200. intabs(intx) { if (x < 0) x = -x; return x; } void trac42() { int a; a = abs(-7); }
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 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!
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); }
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
Local functions void trac42(void) { int x; foo(inty) { 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?
Local functions Return value Actual arguments Access link Return address Frame pointer Local variables
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