CS 342 Lecture -*- Outline -*- * Procedure call ** Calling sequence (general) ---------------------- 1. caller allocates space for return value and evaluates actuals \\\\\\\\\\\\\\\\\\\\\\\\\ <____ SP (stack pointer) new AR: | | actual parameters | for | |________________________| callee | | space for return | ______________ |________________________| | | | | TEMP:| temporaries | AR for | |________________________| caller | | | | VAR: | local data | | | (storage for vars) | | |________________________| | IP: | saved machine status | <_________ EP (env pointer) | | (ip and other regs) | | |________________________| | SL: | static link | | | (or display) | | |________________________| | DL: | dynamic link *______| V |________________________| | ///////////////////////// V 2. Caller saves status a. Caller stores return address, registers in IP (following MacLennan and store in caller's AR) b. Caller stores value of EP in callee's AR (in DL field) \\\\\\\\\\\\\\\\\\\\\\\\\ <____ SP (stack pointer) new AR | |________________________| for | | dynamic link *_________| callee | |________________________| | | | actual parameters | | | |________________________| | | | space for return | | |________________________| | | | | TEMP:| temporaries | | |________________________| | | | | VAR: | local data | | | (storage for vars) | | |________________________| | IP: | saved machine status | <_________ EP (env pointer) | (ip and other regs) | |________________________| SL: | static link | | (or display) | |________________________| DL: | dynamic link *______| |________________________| | ///////////////////////// V 3. Making the call a. The caller fills in the new value of the static link (or display) b. The caller allocates rest of fixed part of callee's AR c. The caller sets EP to new value \\\\\\\\\\\\\\\\\\\\\\\\\ <____ SP (stack pointer) | |________________________| | | saved machine status | <____ EP | | (space) | | |________________________| | | static link | | | (or display) | new AR | |________________________| for | | dynamic link *_________| callee | |________________________| | | | actual parameters | | | |________________________| | | | space for return | | |________________________| | | | | TEMP:| temporaries | | |________________________| | | | | VAR: | local data | | | (storage for vars) | | |________________________| | IP: | saved machine status | <_____ | (ip and other regs) | |________________________| SL: | static link | | (or display) | |________________________| DL: | dynamic link *______| |________________________| | ///////////////////////// V d. jump to start of callee's code 4. Callee allocates space for local data Procedure Return 1. Callee copies return value into allocated space 2. Callee deallocates its AR (by setting SP) \\\\\\\\\\\\\\\\\\\\\\\\\ | | | | | temporaries | | |________________________| | | | | | local data | | | (storage for vars) | | |________________________| | | saved machine status | <____ EP | | (space) | | |________________________| | | static link | | | (or display) | AR | |________________________| for | | dynamic link *_________| callee | |________________________| | | | actual parameters | <__________ SP (new value) | |________________________| | | | return value | | |________________________| | | | | | temporaries | | |________________________| | | | | | local data | | | (storage for vars) | | |________________________| | | saved machine status | <_____ | (ip and other regs) | |________________________| | static link | | (or display) | |________________________| | dynamic link *______| |________________________| | ///////////////////////// V 3. Restore caller's context a. Callee loads EP from it's DL AR | |________________________| for | | dynamic link *_________| callee | |________________________| | | | actual parameters | <__________ SP (new value) | |\\\\\\\\\\\\\\\\\\\\\\\\| | | | return value | | |________________________| | | | | | temporaries | | |________________________| | | | | | local data | | | (storage for vars) | | |________________________| | | saved machine status | <_________ EP (new value) | (ip and other regs) | |________________________| | static link | | (or display) | |________________________| | dynamic link *______| |________________________| | ///////////////////////// V b. jump to resumption address (saved IP in caller's AR) c. caller restores saved state from IP field (registers) ----------------------- ** Notes on general procedure call/return sequences *** Many optimizations (see book and references) esp. using resgisters these optimizations extremely important in practice (procedure call must be fast) don't want to change SP so much *** This organization is well-suited for evaluation of exprs (leaves function return on top of stack) ** Issues -difference between static and dynamic links -changes for different calling mechanisms (ref, value-result) -changes for return of structured objects - C's printf routine, which passes variable number/types of args -variable length temporaries (dope vectors for arrays) -uninitialized variables (how they arise, cost of initialization, checks) -dangling references -dynamic scoping (need static link?)