Ch7
Ch7
Run-Time Environments
Chapter 7
r q(1,9)
Control Stack
Activation tree:
Control Activations:
begin sort
s stack:
enter readarray
r s leave readarray
q(1,9) enter quicksort(1,9)
q(1,9) enter partition(1,9)
p(1,9) q(1,3) q(1,3) leave partition(1,9)
enter quicksort(1,3)
q(2,3) enter partition(1,3)
p(1,3) q(1,0) q(2,3) leave partition(1,3)
enter quicksort(1,0)
leave quicksort(1,0)
enter quicksort(2,3)
…
6
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.
7
environment
state
name
storage
value
var i;
…
i := 0;
…
i := i + 1;
8
environment
state
name
storage
value
var i;
…
i := 0;
…
i := i + 1;
9
Static Notion
Dynamic Notion
Activations of the
Definition of a procedure
procedure
Declaration of a name
Bindings of the name
Scope of a declaration
Lifetime of a binding
10
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
to initialize
Optional access link
Save machine status
Local data
Callee’s
responsibility
Temporaries
to initialize
12
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