CS212 Spring 2002 3/13/2002 Lecture 8: Introduction to Part 3 (continued) ------------------------------------------------------------------------------- [0] Announcements: + point break down so far: 5% part 1, about 15% part 2 + Part 3 posted later tonight/thurs afternoon - add control structures to compiler - sam-code for functions (not part of compiler -- that's in Part 4) + late assignments dealt with case-by-case (grades might be delayed) + reading: see Lecture 7 (posted with answers), SaM.pdf in lecture notes ------------------------------------------------------------------------------- [1] Summary: + function calls: frames in stack, collection of frames is activation record + sam-code for functions: need to "remember" current position and last position in stack ------------------------------------------------------------------------------- [2] Overview: + review of FBR and frames in SaM + partial example of function call + complete sam-code for functions (basically, the "rest" of sam-code) + more examples ------------------------------------------------------------------------------- [3] Complete sam-code for the example in [7] (Lecture 7) (w/o main) + sam-code reminders: STOREOFF x : V[x+FBR] <- V[top] (store variable value) PUSHOFF x : push V[x+FBR] (retrieve variable value) PUSHFBR : push FBR onto stack (push current FBR) POPFBR : FBR <- V[top] (store new FBR) JSR t : push PC+1; PC <- t (store return address; jump to function) JUMPIND : PC <- V[top] (go back to point where function called) + we're writing sam-code for a call of "f(10,20)" where "f(int x,int y) { return x+y; }" (I added types) // sam-code from above // now dealing with current function call PUSHIMM 0 // leave space for rv PUSHFBR // push the current value of FBR PUSHIMM 10 // push param PUSHIMM 20 // push param PUSHSP ---- PUSHIMM 3 \_____ set up FBR: FBR now points to bottom of current frame SUB / (SP-params+1 gives new FBR) POPFBR ---- JSR f // jump to function (jump to callee) ADDSP -2 // remove params POPFBR // restore FBR (the FBR gets its previous value) f: // code for callee (the function that gets called) // ADDSP n not needed because no local vars besides formal params // you could say ADDSP 0 for completeness, which is a bit better PUSHOFF 1 // get x PUSHOFF 2 // get y STOREOFF -1 // store x+y in rv JUMPIND // return to caller |=================================| Page 1 |=================================| [4] Generic sam-code: PROGRAM: // loading starts at address 0 PUSHIMM 0 PUSHFBR PUSHIMM 1 POPFBR JSR main POPFBR STOP // code for all other functions, starting with main FUNCTION: // label of first command is function name label: // allocate local variables (params already taken care of!) ADDSP n // code in body // return code (see next segment): STOREOFF -1 // pop off local variables: ADDSP -n // return to caller JUMPIND RETURN CODE (giving a bit more detail): // evaluate return value // store return value into "bottom" slot STOREOFF -1 // takes into account that the FBR is 1 "higher" than rv // pop off local variables: ADDSP -n JUMPIND FUNCTION INVOCATION (calling function f): // make space for rv and saved FBR PUSHIMM 0 // or ADDSP 1 PUSHFBR // push params // set up new FBR for n params: PUSHSP PUSHIMM n+1 SUB POPFBR // call function f JSR f // pop params and restore "old" FBR (move FBR to previous saved FBR) ADDSP -n POPFBR |=================================| Page 2 |=================================|