0% found this document useful (0 votes)
7 views16 pages

CH 7

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

CH 7

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

1

Run-Time Environments
2

Procedure Activation and


Lifetime
• A procedure is activated when called
• The lifetime of an activation of a procedure
is the sequence of steps between the first
and last steps in the execution of the
procedure body
• A procedure is recursive if a new activation
can begin before an earlier activation of the
same procedure has ended
3

Procedure Activations: Example


program sort(input, output)
var a : array [0..10] of integer;
procedure readarray;
var i : integer;
begin Activations:
for i := 1 to 9 do read(a[i]) begin sort
end;
function partition(y, z : integer) :
enter readarray
integer leave readarray
var i, j, x, v : integer; enter quicksort(1,9)
begin … enter partition(1,9)
end leave partition(1,9)
procedure quicksort(m, n : integer); enter quicksort(1,3)
var i : integer; …
begin
if (n > m) then begin
leave quicksort(1,3)
i := partition(m, n); enter quicksort(5,9)
quicksort(m, i - 1); …
quicksort(i + 1, n) leave quicksort(5,9)
end leave quicksort(1,9)
end; end sort.
begin
a[0] := -9999; a[10] := 9999;
readarray;
quicksort(1, 9)
4

Activation Trees: Example

r q(1,9)

p(1,9) q(1,3) q(5,9)

p(1,3) q(1,0) q(2,3) p(5,9) q(5,5) q(7,9)

p(2,3) q(2,1) q(3,3) p(7,9) q(7,7) q(9,9)

Activation tree for the sort program


Note: also referred to as the dynamic call graph
5

Control Stack

Activation tree: Control Activations:


stack: begin sort
s
enter readarray
s leave readarray
r q(1,9) enter quicksort(1,9)
q(1,9) enter partition(1,9)
leave partition(1,9)
p(1,9) q(1,3) q(1,3)
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;
Variable x locally declared in p var x : integer;
begin
x := 1;

end;
begin
A function x y := x(0.0);

7

Mapping Names to Values

environment state

name storage value

var i;

i := 0;

i := i + 1;
8

Mapping Names to Values


At compile time At run time

environment state

name storage value

var i;

i := 0;

i := i + 1;
9

Static and Dynamic Notions of


Bindings

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
Optional access link to initialize

Saved machine status


(w/ opt. return address)
Local data Callee’s
responsibility
Temporaries to initialize
12

Control Links

The control link is the old


value of the fp

Caller’s activation record


fp
Callee’s activation record
Control link
sp

Stack
growth
13

Scope with Nested Procedures


program sort(input, output)
var a : array [0..10] of integer;
x : integer;
procedure readarray;
var i : integer;
begin … end;
procedure exchange(i, j : integer);
begin x := a[i]; a[i] := a[j]; a[j] := x
end;
procedure quicksort(m, n : integer);
var k, v : integer;
function partition(y, z : integer) : integer
var i, j : integer;
begin … exchange(i, j) … end
begin
if (n > m) then begin
i := partition(m, n);
quicksort(m, i - 1);
quicksort(i + 1, n)
end
end;
begin

quicksort(1, 9)
end.
14

Access Links (Static Links)


s s s s

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

Accessing Nonlocal Data


• To implement access to nonlocal data a in
procedure p, the compiler generates code to
traverse np - na access links to reach the
activation record where a resides
– np is the nesting depth of procedure p
– na is the nesting depth of the procedure
containing a
16

Parameter Passing Modes


• Call-by-value: evaluate actual parameters and
enter r-values in activation record
• Call-by-reference: enter pointer to the storage of
the actual parameter
• Copy-restore (aka value-result): evaluate actual
parameters and enter r-values, after the call copy
r-values of formal parameters into actuals
• Call-by-name: use a form of in-line code
expansion (thunk) to evaluate parameters

You might also like