CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall
Annoucements PR03 due today PR04 posted Survey on T/W Recitations - None on Wednesday - F2F w/ 490 on Friday
Phases of a compiler Target machine code generation Figure 1.6, page 5 of text
Memory organization code static heap free memory stack
Stack frame organization actual parameters (arguments) returned value control link (dynamic link) access link (static link) saved machine status (return address) local data temporaries
Stack frame organization actual parameters (arguments) Initialized returned value by caller, used control link by callee. (dynamic link) access link (static link) May be in CPU saved machine status registers. (return address) local data temporaries
Stack frame organization actual parameters (arguments) Initialized returned value by callee, read control link by caller. (dynamic link) access link (static link) May be in a saved machine status CPU register. (return address) local data temporaries
Stack frame organization actual parameters (arguments) returned value The address control link of the caller's (dynamic link) invocation access link record (stack (static link) frame). saved machine status (return address) local data temporaries
Stack frame organization actual parameters (arguments) Used to returned value achieve static scope for nested control link function definitions. (dynamic link) access link Our language does not (static link) use this. saved machine status (return address) Scheme/ML do. local data temporaries
Stack frame organization actual parameters (arguments) Information returned value needed to restore machine to state at control link function call, (dynamic link) including the return access link address (the value of (static link) the Program Counter saved machine status at the time of the (return address) call). local data temporaries
Stack frame organization actual parameters (arguments) returned value control link Space for local (dynamic link) variables. access link (static link) saved machine status (return address) local data temporaries
Stack frame organization actual parameters (arguments) returned value Space for temporary variables, control link and variable-length (dynamic link) local data access link (static link) Temporaries may be saved machine status in CPU registers. (return address) local data temporaries
7.2.3 Calling Sequence What happens during a function call?
actual parameters caller's returned value invocation control link record access link saved machine status top_sp local data temporaries top Prior to function call.
7.2.3 Calling Sequence "Procedure calls are implemented by what are known as calling sequences, which consist of code that allocates an activation record on the stack and enters information into its fields." [p. 436]
actual parameters caller's returned value invocation control link record access link saved machine status local data temporaries actual parameters During function call. callee's returned value invocation control link record access link saved machine status top_sp local data temporaries top
7.2.3 Calling Sequence " A return sequence is similar code to restore the state of the machine so the calling procedure can continue its execution after the call." [p. 436]
actual parameters caller's returned value invocation control link record access link saved machine status top_sp local data temporaries top … After function call. returned value … … … … …
Caller vs Callee responsibilities "In general, if a procedure is called from n different points, then the portion of the calling sequence assigned to the caller is generated n times. However, the portion assigned to the callee is generated only once." [p. 436]
Typical calling sequence [p. 437] "1. The caller evaluates the actual parameters." Recall: formal parameter == parameter actual parameter == argument
actual parameters caller's returned value invocation control link record access link saved machine status top_sp local data temporaries top Prior to function call.
actual parameters caller's returned value invocation control link record access link saved machine status top_sp local data temporaries top Caller writes actual parameters arguments (actual parameters) past the end of its own invocation record.
Typical calling sequence [p. 437] "2. The caller stores a return address and the old value of top_sp into the callee's activation record. The caller then increments top_sp […] top_sp is moved past the caller's local data and temporaries and the callee's parameters and status fields."
actual parameters caller's returned value invocation control link record access link saved machine status top_sp local data Caller knows the temporaries top offset of the eventual actual parameters returned value. When callee returns the returned value caller will look at this location for the returned value.
Typical calling sequence [p. 437] "2. The caller stores a return address and the old value of top_sp into the callee's activation record. … "
actual parameters caller's returned value invocation control link record access link saved machine status top_sp local data temporaries top actual parameters The caller stores its stack pointer here. returned value control link
actual parameters caller's returned value invocation control link record access link saved machine status top_sp The caller stores local data its stack pointer here. When the temporaries callee finishes the top stack pointer's value actual parameters will be reset to this value, thereby returned value restoring the caller's top_sp invocation record as the active one (the one on top of the stack).
Typical calling sequence [p. 437] "2. The caller stores a return address and the old value of top_sp into the callee's activation record. The caller then increments top_sp […] top_sp is moved past the caller's local data and temporaries and the callee's parameters and status fields."
actual parameters caller's returned value invocation control link record access link saved machine status local data temporaries actual parameters Move top_sp returned value top_sp access link saved machine status top_sp … …
Typical calling sequence [p. 437] "3. The callee saves the register values and other status information."
actual parameters caller's returned value invocation control link record access link saved machine status top_sp Write the return local data address, the current value of the Program temporaries top Counter (PC), into the actual parameters saved machine status. When the callee returned value finishes execution will resume with the top_sp address pointed to by this saved access link address. PC + machine status top_sp … …
actual parameters caller's returned value invocation control link record access link saved machine status local data When control transfers to the temporaries callee, the top_sp and actual parameters top are updated. callee's returned value Callee writes local invocation data and temporaries top_sp into its invocation record record. access link PC + machine status top_sp local data temporaries top
actual parameters caller's returned value invocation control link record access link saved machine status If the number of local data arguments can vary from call to call temporaries (e.g. printf) then the caller writes the actual parameters arguments to the callee's "actual parameters" returned value area, as well as invocation top_sp information about the number of record access link arguments to the status area PC + machine status top_sp local data temporaries top
actual parameters caller's returned value invocation control link record access link saved machine status If the callee has variable local data length local data (e.g. local arrays temporaries whose size is determined by the actual parameters value of a parameter) callee's then the arrays are returned value allocated space at the invocation top_sp end of the invocation record, and pointers record access link to those arrays are stored in the PC + machine status "locals" block. top_sp local data temporaries top
Project status? Don't forget to submit tonight! Don't forget team meetings with me this week. If you don't have one scheduled yet, get in touch.
Recommend
More recommend