Run-Time Environments: COP5621 Compiler Construction
Run-Time Environments: COP5621 Compiler Construction
Run-Time Environments: COP5621 Compiler Construction
Run-Time Environments
Chapter 7
COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009
program sort(input, output) var a : array [0..10] of integer; procedure readarray; var i : integer; begin for i := 1 to 9 do read(a[i]) end; function partition(y, z : integer) : integer var i, j, x, v : integer; begin end procedure quicksort(m, n : integer); var i : integer; begin if (n > m) then begin i := partition(m, n); quicksort(m, i - 1); quicksort(i + 1, n) end end; begin a[0] := -9999; a[10] := 9999; readarray; quicksort(1, 9) end.
r
p(1,9) p(1,3)
q(1,9) q(1,3) q(1,0) q(2,3) p(5,9) q(5,9) q(5,5) q(7,9) p(7,9) q(7,7) q(9,9)
Activation tree for the sort program Note: also referred to as the dynamic call graph
Control Stack
Activation tree:
s
Control stack:
s q(1,9)
Activations:
begin sort enter readarray leave readarray enter quicksort(1,9) enter partition(1,9) leave partition(1,9) enter quicksort(1,3) enter partition(1,3) leave partition(1,3) enter quicksort(1,0) leave quicksort(1,0) enter quicksort(2,3)
r
p(1,9) p(1,3)
q(1,3)
q(2,3)
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; begin x := 1; end; begin y := x(0.0); end.
A function x
name
storage
value
var i; i := 0; i := i + 1;
name
storage
value
var i; i := 0; i := i + 1;
Dynamic Notion Activations of the procedure Bindings of the name Lifetime of a binding
10
Stack Allocation
Activation records (subroutine frames) on the runtime stack hold the state of a subroutine Calling sequences are code statements to create activations records on the stack and enter data in them
Callers calling sequence enters actual arguments, control link, access link, and saved machine state Callees calling sequence initializes local data Callees return sequence enters return value Callers return sequence removes activation record
11
Returned value
Actual parameters Callers responsibility to initialize
Local data
Temporaries
12
Control Links
The control link is the old value of the fp
Callers activation record
fp sp
Control link
Callees activation record
Stack growth
13
14
q(1,3) access k v
p(1,3) access i j
The access link points to the activation record of the static parent procedure: s is parent of r, e, and q q is parent of p
15
16