Functions: flow of control (foc) Essentially the handling of functions is a flow of control (foc) problem (use goto) The two additional points are 1. Parameter passing 2. Return value passing function call next instruction halt (return) function return Code space pc 1
Types of Three Address Code Statements (ASU1 Ch 8.1) assignment x := y op z (op: binary / logical operation) assignment x := op y (op: unary / logical operation) copy x := y unconditional jump goto L conditional jump if x relop y goto L function/procedure param x / call p / return y indexed assignment x := y[i] / x[i] := y address assignment x := &y pointer assignment x := *y / *x := y see ASU pp 467-468 for details 2
Environments: Data Space There are 2 aspects to consider 1. The environment where the call takes place 2. The environment of the function For recursive functions (1) and (2) are the same EXCEPT for the initial call For recursive functions there will be a new INSTANCE of the environment on the stack The Symbol Table DESCRIBES the environment The INSTANCES of the environment are in either 1. The static data area - for the program (activation record) 2. The stack - for function calls (stack frame) 3
Function calls The function call is handled in 3 parts 1. Pre call Create (allocate) the stack space Evaluate & copy the actual parameters Save the return address in the stack Save the return value address in the stack Save the address of the calling environment (dynamic pointer) Save the address of the enclosing environment (static pointer) Set the current environment to f s environment (on the stack) 2. Call and execute the function Goto the start of the function code (call p goto) (pc = addr(f)) 3. Post call Set-up the return value Goto the instruction following the function call (return goto) 4
Function Call - example Function definition: int function f(int fp 1, bool fp 2 ); {fn body} Formal parameters fp 1, fp 2 (identifier) Function call (is also an expression) x := f (2 * a, b and c); Actual parameters 2 * a, b and c (expression) t1 := 2*a; t2 := b and c; t3 := f(t1, t2); x := t3; 5
Program & Diagram ST and stack: X A program X y=1, z=2: int int function A(p:int, q:int) h: int; { h = p + q; return h;} yintdo z int DO A int FO p, q int PO h int DO Memory Code A & X AR for X SF for A { y = A(y, z); } DO = Data Object FO = Function Object (CODE) PO = Parameter Object (DATA) 6
Execution sequence 7 106: st = ST(X) 107: ar = AR-X 108: pc = instr 1 X 109: t2 = y 110: aplist += t2 111: t3 = z 112: aplist += t3 113: t4 = A(aplist) 114: st = ST(A) 115: ar = SF-A (alloc) 116: p = t2 117: q = t3 118: rtval = &t4 119: rtaddr = 125 120: sptr = AR-X 121: dptr = AR-X 122: st = st 123: ar = ar 124: pc = instr 1 A 100: t1 = p + q 101: h = t1 102: t4 = h 103: st = parent(st) 104: ar = dptr 105: pc = rtaddr 125: y = t4 126: halt st = symbol table pc = program counter ar = activation record rtval = return value rtaddr = return address sptr = static pointer dptr = dynamic pointer Addresses are symbolic: Assume absolute addresses on execution pc, st, ar registers
Comments 1. 106-108: the pc, st and ar must be initialised on the start of X 2. 109-112: the actual parameters are evaluated 3. 113: is the call to A 4. 114: there are 2 environments, X (st, ar) and A (st, ar ) 5. 115: the stack frame for A is allocated 6. 116-117: the formal parameters are bound 7. 118-121: the stack frame is initialised 8. 122-124: switch to the new environment (A) 9. 100-102: execute A 10. 103-105: prepare the return 11. 125-126: finish the execution of X 8
Code-A = 100 ST-A = 200 AR-A = 800 Code-X = 106 ST-X = 400 AR-X = 600 9 name type role offset name type role offset parent Ptr-ST ptr nil parent Ptr-ST ptr 400 y int var 4 p int param 4 z int var 8 q int param 8 t2 int var 12 h int var 12 t3 int var 16 t1 int ar 16 t4 int var 20 sptr ptr ptr 20 A int fn 100 dptr ptr ptr 24 rtval int val 28 ST-X ST-A rtadd ptr ptr 32
Execution sequence 10 106: st = ST(X) 107: ar = AR-X 108: pc = instr 1 X 109: t2 = y 110: aplist += t2 111: t3 = z 112: aplist += t3 113: t4 = A(aplist) 114: st = ST(A) 115: ar = SF-A (alloc) 116: p = t2 117: q = t3 118: rtval = t4 119: rtaddr = 125 120: sptr = AR-X 121: dptr = AR-X 122: st = st 123: ar = ar 124: pc = instr 1 A 100: t1 = p + q 101: h = t1 102: t4 = h 103: st = parent(st) 104: ar = dptr 105: pc = rtaddr 125: y = t4 126: halt st = symbol table pc = program counter ar = activation record rtval = return value rtaddr = return address sptr = static pointer dptr = dynamic pointer Addresses are symbolic: Assume absolute addresses on execution pc, st, ar registers
Execution sequence 11 106: st = 400 107: ar = 600 108: pc = 109 109: 612 = 604 110: aplist += 612 111: 616 = 608 112: aplist += 616 113: 620 = A(aplist) 114: st = 200 115: ar = 800 (alloc) 116: 804 = 612 117: 808 = 616 118: 828 = 429 119: 832 = 125 120: 820 = 600 121: 824 = 600 122: st = 200 123: ar = 800 124: pc = 100 100: 816 = 804 + 808 101: 812 = 816 102: 620 = 812 103: st = 400 104: ar = 600 105: pc = 125 125: 604 = 620 126: halt st = symbol table pc = program counter ar = activation record rtval = return value rtaddr = return address sptr = static pointer dptr = dynamic pointer Addresses are symbolic: Assume absolute addresses on execution pc, st, ar registers
Comments Aplist (actual parameter list) has to be placed somewhere! The run-time system needs some mechanism for this Aplist = (612, 616) The number of formal parameters (2) is known from ST-A In the call y = A(y, z), y and z represent expressions hence t2 = y, t3 = z The actual/formal parameter binding is by position The alternative would be by name y = A(q = z, p = y) In 113 = A(Aplist) the AR-A needs to be allocated only then can the absolute addresses for variables etc in A be calculated The activation record for X is known at load time 12
Summary The above discussion is a MODEL The main points in a function call/return are Context switch from X to A call Allocate the stack frame (activation record for A AR-A) In AR-A save the return address & return value In AR-A save the static & dynamic pointers Set st, pc and ar (context switch) Context switch from A to X return Copy back the return value Set st, pc and ar (context switch) pc code ar data st describes ar 13