7 CD-PPT-5 Unit
7 CD-PPT-5 Unit
Compiler Design
Unit No 5: Run Time Environment
SEMESTER: 7
PREPARED BY:
Storage Organization
Static vs. Dynamic Allocation
⚫Static: Compile time, Dynamic: Runtime allocation
⚫Many compilers use some combination of following
⚫Stack storage: for local variables, parameters and so on
⚫Heap storage: Data that may outlive the call to the
procedure that created it
⚫Stack allocation is a valid allocation for procedures since
procedure calls are nested
Sketch of a quicksort program
Activation for Quicksort
Activation tree representing calls during an
execution of quicksort
Activation records
⚫Procedure calls and returns are usaully managed by a run-
time stack called the control stack.
⚫Each live activation has an activation record
(sometimes called a frame)
⚫The root of activation tree is at the bottom of the stack
⚫The current execution path specifies the content of the stack
with the last activation has record in the top of the stack.
A General Activation Record
Activation Record
⚫Temporary values
⚫Local data
⚫A saved machine status
⚫An “access link”
⚫A control link
⚫Space for the return value of the called function
⚫The actual parameters used by the calling procedure
Downward-growing stack of activation records
Designing Calling Sequences
⚫Values communicated between caller and callee are
generally placed at the beginning of callee’s activation
record
⚫Fixed-length items: are generally placed at the middle
⚫Items whose size may not be known early enough: are
placed at the end of activation record
⚫We must locate the top-of-stack pointer judiciously: a
common approach is to have it point to the end of fixed
length fields.
Division of tasks between caller and callee
calling sequence
⚫The caller evaluates the actual parameters
⚫The caller stores a return address and the old value of
top-sp into the callee's activation record.
⚫The callee saves the register values and other status
information.
⚫The callee initializes its local data and begins
execution.
corresponding return sequence
⚫The callee places the return value next to the
parameters
⚫Using information in the machine-status field, the callee
restores top-sp and other registers, and then branches to the
return address that the caller placed in the status field.
⚫Although top-sp has been decremented, the caller knows
where the return value is, relative to the current value of
top-sp; the caller therefore may use that value.
Access to dynamically allocated arrays
ML
⚫ML is a functional language
⚫Variables are defined, and have their unchangeable
values initialized, by a statement of the form:
val (name) = (expression)
⚫Functions are defined using the syntax:
fun (name) ( (arguments) ) = (body)
⚫For function bodies we shall use let-statements of the form:
let (list of definitions) in (statements) end
A version of quicksort, in ML style, using nested
functions
Access links for finding nonlocal data
Sketch of ML program that uses function- parameters
Actual parameters carry their access
link with them
Maintaining the Display
Maintaining the Display (Cont.)
Memory Manager
⚫Two basic functions:
⚫Allocation
⚫Deallocation
⚫Properties of memory managers:
⚫Space efficiency
⚫Program efficiency
⚫Low overhead
Typical Memory Hierarchy Configurations
Locality in Programs
The conventional wisdom is that programs spend 90% of their
time executing 10% of the code:
⚫Programs often contain many instructions that are never
executed.
⚫Only a small fraction of the code that could be invoked is
actually executed in a typical run of the program.
⚫The typical program spends most of its time executing
innermost loops and tight recursive cycles in a program.
Part of a Heap