Run-Time Environments: COP5621 Compiler Construction
Run-Time Environments: COP5621 Compiler Construction
Run-Time Environments: COP5621 Compiler Construction
Run-Time Environments
Chapter 7
r q(1,9)
Control Stack
Scope Rules
• Environment determines name-to-object
bindings: which objects are in scope?
program prg;
var y : real;
function x(a : real) : real;
begin … end;
procedure p;
var x : integer;
Variable x locally declared in p begin
x := 1;
…
end;
begin
y := x(0.0);
A function x …
end.
10
environment state
var i;
…
i := 0;
…
i := i + 1;
11
environment state
var i;
…
i := 0;
…
i := i + 1;
12
Stack Allocation
• Activation records (subroutine frames) on the run-
time stack hold the state of a subroutine
• Calling sequences are code statements to create
activations records on the stack and enter data in
them
– Caller’s calling sequence enters actual arguments,
control link, access link, and saved machine state
– Callee’s calling sequence initializes local data
– Callee’s return sequence enters return value
– Caller’s return sequence removes activation record
13
Activation Records
(Subroutine Frames)
fp
(frame pointer) Returned value
Actual parameters
Caller’s
Optional control link responsibility
Optional access link to initialize
Activation Records
• Temporary values, such as those arising from the evaluation
of expressions
• Local data belonging to the procedure
• A saved machine status, with information about the state of the
machine just before the call to the procedure
• An "access link" may be needed to locate data needed by the called
procedure
• A control link, pointing to the activation record of the caller
• Space for the return value of the called function, if any. Again, not
all called procedures return a value, and if one does, we may prefer
to place that value in a register for efficiency.
• The actual parameters used by the calling procedure. Commonly,
these values are not placed in the activation record but rather in
registers, when possible, for greater efficiency.
15
Control Links
Stack
growth
16