2000 by Antony L. Hosking. Permission To Make Digital or Hard Copies of
2000 by Antony L. Hosking. Permission To Make Digital or Hard Copies of
Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for prot or commercial advantage and that copies bear this notice and full citation on the rst page. To copy otherwise, to republish, to post on servers, or to redistribute to lists, requires prior specic permission and/or fee. Request permission to publish from hosking@cs.purdue.edu. 1
Procedure linkages
higher addresses incoming arguments argument n . . . argument 2 argument 1 local variables previous frame
Assume that each procedure activation has an associated activation record or frame (at run time) Assumptions: RISC architecture can always expand an allocated block locals stored in frame
frame pointer
return address current frame temporaries saved registers argument m . . . argument 2 argument 1 next frame
stack pointer
outgoing arguments
lower addresses
Procedure linkages
The linkage divides responsibility between caller and callee
Call Caller pre-call 1. allocate basic frame 2. evaluate & store params. 3. store return address 4. jump to child Callee prologue 1. save registers, state 2. store FP (dynamic link) 3. set new FP 4. store static link 5. extend basic frame (for local data) 6. initialize locals 7. fall through to code epilogue 1. store return value 2. restore state 3. cut back to basic frame 4. restore parents FP 5. jump to return address
Return
post-call 1. copy return value 2. deallocate basic frame 3. restore parameters (if copy out)
At compile time, generate the code to do this At run time, that code manipulates the frame & data areas
5
free memory
The classical scheme allows both stack and heap maximal freedom code and static data may be separate or intermingled
7
Storage classes
Each variable must be assigned a storage class address) Static variables: addresses compiled into code (usually ) allocated at compile-time limited to xed size objects control access with naming scheme Global variables: almost identical to static variables layout may be important naming scheme ensures universal access Link editor must handle duplicate denitions
9
(base
(relocatable)
(exposed)
11
12
calling level k + 1 procedure 1. pass my FP as access link 2. my backward chain will work for lower levels calling procedure at level l < k 1. nd link to level l 1 and pass it 2. its access link will work for lower levels
13
The display
To improve run-time access costs, use a display : table of access links for lower levels lookup is index from known offset takes slight amount of time at call a single display or one per frame for level k procedure, need k 1 slots Access with the display Assume a value described by (l, o): nd slot as display[l] add offset to pointer from slot (display[l][o]) Setting up the basic frame now includes display manipulation
14
Display management
Single global display: Key insight: overallocate the display by 1 slot On entry to a procedure at level l save the level l display value push FP into level l display slot On return restore the level l display value simple method
Display management
Individual frame-based displays: Call from level k to level l if l k copy l 1 display entries into childs frame if l > k (l = k + 1) copy k 1 entries into childs frame copy own FP into kth slot in childs frame No work required on return display is deallocated with frame Display accessed by offset from FP one less register required
16
Parameter passing
What about parameters? Call-by-value store values, not addresses never restore on return arrays, structures, strings are a problem Call-by-reference pass address access to formal is indirect reference to actual Call-by-value-result store values, not addresses always restore on return arrays, structures, strings are a problem Call-by-name build and pass thunk access to parameter invokes thunk all parameters are same size in frame!
18
Parameter passing
What about variable length argument lists? 1. if caller knows that callee expects a variable number (a) caller can pass number as 0th parameter (b) callee can nd the number directly 2. if caller doesnt know anything about it (a) callee must be able to determine number (b) rst parameter must be closest to FP Consider printf : number of parameters determined by the format string it assumes the numbers match
19
1. Call includes bitmap of callers registers to save/restore (best with save/restore instructions to interpret bitmap) 2. Caller saves and restores its own registers Unstructured returns (e.g., non-local gotos, exceptions) create some problems, since code to restore must be located and executed 3. Backpatch code to save callees registers on entry, restore on exit e.g., VAX places bitmap in callees stack frame for use on call/return/non-local goto/exception Non-local gotos/exceptions unwind dynamic chain restoring callee-saved registers 4. Bitmap in callees stack frame is used by caller to save/restore (best with save/restore instructions to interpret bitmap) Unwind dynamic chain as for 3 5. Easy: Non-local gotos/exceptions restore all registers from outermost callee 6. Easy (use utility routine to keep calls compact) Non-local gotos/exceptions restore original registers from caller Top-left is best: saves fewer registers, compact calling sequences 20
23
24