CSE443 Compilers
- Dr. Carl Alphonce
CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis - - PowerPoint PPT Presentation
CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall Phases of a compiler Target machine code generation Figure 1.6, page 5 of text 7.2.3 Calling Sequence What happens during a function call? actual parameters
Figure 1.6, page 5 of text
saved machine status local data actual parameters returned value control link access link temporaries
top_sp top
Prior to function call.
saved machine status local data actual parameters returned value control link access link temporaries saved machine status local data actual parameters returned value control link access link temporaries
top_sp top
During function call.
saved machine status local data actual parameters returned value control link access link temporaries … … … returned value … … …
After function call.
top_sp top
saved machine status local data actual parameters returned value control link access link temporaries
top_sp top
Prior to function call.
saved machine status local data actual parameters returned value control link access link temporaries actual parameters
top_sp top
Caller writes arguments (actual parameters) past the end of its own invocation record.
saved machine status local data actual parameters returned value control link access link temporaries actual parameters returned value
top_sp top
Caller knows the
returned value. When callee returns the caller will look at this location for the returned value.
saved machine status local data actual parameters returned value control link access link temporaries actual parameters returned value control link
top_sp top
The caller stores its stack pointer here.
saved machine status local data actual parameters returned value control link access link temporaries actual parameters returned value
top_sp
top_sp top
The caller stores its stack pointer
callee finishes the stack pointer's value will be reset to this value, thereby restoring the caller's invocation record as the active one (the
stack).
saved machine status
top_sp
Move top_sp saved machine status local data actual parameters returned value control link access link temporaries … actual parameters returned value access link …
top_sp
saved machine status local data actual parameters returned value control link access link temporaries
PC + machine status
actual parameters returned value access link
top top_sp
Write the return address, the current value of the Program Counter (PC), into the saved machine status. When the callee finishes execution will resume with the address pointed to by this saved address. … …
top_sp
PC + machine status top_sp
When control transfers to the callee, the top_sp and top are updated. Callee writes local data and temporaries into its invocation record. saved machine status local data actual parameters returned value control link access link temporaries local data actual parameters returned value access link temporaries
top_sp top
PC + machine status top_sp
If the number of arguments can vary from call to call (e.g. printf) then the caller writes the arguments to the "actual parameters" area, as well as information about the number of arguments to the status area saved machine status local data actual parameters returned value control link access link temporaries local data actual parameters returned value access link temporaries
top_sp top
PC + machine status top_sp
If the callee has variable length local data (e.g. local arrays whose size is determined by the value of a parameter) then the arrays are allocated space at the end of the invocation record, and pointers to those arrays are stored in the "locals" block. saved machine status local data actual parameters returned value control link access link temporaries local data actual parameters returned value access link temporaries
top_sp top
Compiler produces relocatable object code: addresses are not absolute, but relative to known boundaries (e.g. Stack Pointer, start of record, Program Counter). Linker combines object code files into an executable file, in which static relative addresses are made absolute (in virtual address space). Loader copies contents of executable file into memory and starts execution.
Compiler produces relocatable object code: addresses are not absolute, but relative to known boundaries (e.g. Stack Pointer, start of record, Program Counter). Linker combines object code files into an executable file, in which static relative addresses are made absolute (in virtual address space). Loader copies contents of executable file into memory and starts execution.
Leave relative
translation.
Associate costs with each instruction, then "minimize" (lower)
balance since execution time and code size can be in conflict.
Which variables are kept in registers?
Which specific register holds which value?
E.g. to minimize the number
<— load the value of y into register R0 <— put into R0 the result of adding R0 and the value of z <— store the value of register R0 to x
Consider applying the same template to a larger example: a = b + c d = a + e This might be translated as: LD R0, b ADD R0, R0, c ST a, R0 LD R0, a ADD R0, R0, e ST d, R0
Consider applying the same template to a larger example: a = b + c d = a + e This might be translated as: LD R0, b ADD R0, R0, c ST a, R0 LD R0, a ADD R0, R0, e ST d, R0
This instruction is redundant: it is loading into R0 the value that is already there.