Ch7
Ch7
Ch7
Run-Time Environments
2
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;
Variable x locally declared in p var x : integer;
begin
x := 1;
…
end;
begin
A function x y := x(0.0);
…
7
environment state
var i;
…
i := 0;
…
i := i + 1;
8
environment state
var i;
…
i := 0;
…
i := i + 1;
9
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
11
Activation Records
(Subroutine Frames)
fp
(frame pointer) Returned value
Actual parameters
Caller’s
Optional control link responsibility
Optional access link to initialize
Control Links
Stack
growth
13
a x a x a x a x
q(1,9) q(1,9) q(1,9) q(1,9)
access access access access
k v k v k v k v
q(1,3) q(1,3) q(1,3)
access access access
k v k v k v
p(1,3) p(1,3)
The access link points to the access access
activation record of the static i j i j
parent procedure: e(1,3)
s is parent of r, e, and q access
q is parent of p
15