Compiler Design and Construction
Compiler Design and Construction
Compiler Design and Construction
Run-Time Environments
Reading: Section 7.1 – 7.3 of chapter 7
By Bishnu Gautam 1
Compiler Construction Run Time Environments
By Bishnu Gautam 2
Compiler Construction Run Time Environments
By Bishnu Gautam 3
Compiler Construction Run Time Environments
Procedure Activations
Example
program sort(input, output) Activations:
var a : array [0..10] of integer;
procedure readarray; begin sort
var i : integer; enter readarray
begin
for i := 1 to 9 do read(a[i]) leave readarray
end; enter quicksort(1,9)
function partition(y, z : integer) : enter partition(1,9)
integer
var i, j, x, v : integer; leave partition(1,9)
begin … enter quicksort(1,3)
end
procedure quicksort(m, n : integer); …
var i : integer; leave quicksort(1,3)
begin
if (n > m) then begin enter quicksort(5,9)
i := partition(m, n); …
quicksort(m, i - 1); leave quicksort(5,9)
quicksort(i + 1, n)
end leave quicksort(1,9)
end; end sort.
begin
a[0] := -9999; a[10] := 9999;
readarray;
quicksort(1, 9)
end.
By Bishnu Gautam 4
Compiler Construction Run Time Environments
Activation Tree
We can use a tree (called activation tree) to show the way control enters and leaves
activations.
In an activation tree:
– Each node represents an activation of a procedure.
– The root represents the activation of the main program.
– The node a is a parent of the node b iff the control flows from a to b.
– The node a is left to to the node b iff the lifetime of a occurs before the lifetime
of b.
s
Activation tree for the sort program of page 4
r q(1,9)
Control Stack
The flow of the control in a program corresponds to a depth-first traversal of the
activation tree.
A stack (called control stack) can be used to keep track of live procedure
activations. An activation record is pushed onto the control stack as the activation
starts and popped when that activation ends.
When node n is at the top of the control stack, the stack contains the nodes along the
path from n to the root.
Activations:
begin sort
Activation tree: Control enter readarray
s stack: leave readarray
enter quicksort(1,9)
r
s enter partition(1,9)
q(1,9)
q(1,9) leave partition(1,9)
enter quicksort(1,3)
p(1,9) q(1,3) q(1,3) enter partition(1,3)
leave partition(1,3)
q(2,3) enter quicksort(1,0)
p(1,3) q(1,0) q(2,3)
leave quicksort(1,0)
By Bishnu Gautam 6
enter quicksort(2,3)
…
Compiler Construction Run Time Environments
Scope Rules
The same variable name can be used in the different parts of the program. The scope
rules of the language determine which declaration of a name applies when the name
appears in the program.
An occurrence of a variable (a name) is:
– local: If that occurrence is in the same procedure in which that name is declared.
– non-local: Otherwise (ie. it is declared outside of that procedure)
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.
By Bishnu Gautam 7
Compiler Construction Run Time Environments
Binding of Names
The same name may denote different data objects at run time.
Storage location s with name x, we say that x is bound to s; the
association is referred to as a binding of x.
At compile time At run time
environment state
var i;
…
i := 0; A binding is the dynamic counterpart of a declaration
…
i := i + 1;
By Bishnu Gautam 8
Compiler Construction Run Time Environments
Activation Records
Information needed by a single execution of a procedure is managed
using a contiguous block of storage called activation record.
An activation record is allocated when a procedure is entered, and it is
de-allocated when that procedure exited.
The returned value of the called procedure is returned in this
field to the calling procedure. This value return in machine
return value register for efficiency..
The field for actual parameters is used by the calling procedure
actual parameters to supply parameters to the called procedure.
optional control link The control link points to the activation record of the caller.
The access link is used to refer to nonlocal data held in other
optional access link activation records.
The field for saved machine status holds information about
saved machine status the state of the machine before the procedure is called.
local data The field of local data holds data that local to an execution
of a procedure..
temporaries Temporary variables is stored in the field of temporaries.
By Bishnu Gautam 10
Compiler Construction Run Time Environments
By Bishnu Gautam 11
Compiler Construction Run Time Environments
By Bishnu Gautam 13
Compiler Construction Run Time Environments
Exercise
By Bishnu Gautam 14