Runtime Environment
Runtime Environment
1
10/21/2018
Heap
2
10/21/2018
3
10/21/2018
Procedure Activations
• An execution of a procedure starts at the beginning of the
procedure body;
• When the procedure is completed, it returns the control to
the point immediately after the place where that procedure is
called.
• Each execution of a procedure is called as its activation.
• Lifetime of an activation of a procedure is the sequence of
the steps between the first and the last steps in the execution
of that procedure (including the other procedures called by
that procedure).
• If a and b are procedure activations, then their lifetimes are
either non-overlapping or are nested.
• If a procedure is recursive, a new activation can begin before
an earlier activation of the same procedure has ended.
COE 401 Compiler Design 7
Activation Tree
• We can use a tree (called activation tree) or stack
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.
4
10/21/2018
A Nested Structure
p s
q s
5
10/21/2018
Control Stack
• The flow of the control in a program corresponds to a
depth-first traversal of the activation tree that:
– starts at the root,
– visits a node before its children, and
– recursively visits children at each node an a left-to-right order.
• A stack (called control stack) can also be used to keep
track of live procedure activations.
– An activation record is pushed onto the control stack as the activation
starts.
– That activation record is 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.
6
10/21/2018
Variable Scopes
• 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)
procedure p;
var b:real;
procedure p;
var a: integer; a is local
begin a := 1; b := 2; end; b is non-local
begin ... end;
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.
• Size of each field can be determined at compile
time (Although actual location of the activation
record is determined at run-time).
– Except that if the procedure has a local variable and its size
depends on a parameter, its size is determined at the run
time.
COE 401 Compiler Design 14
7
10/21/2018
actual parameters The field for actual parameters is used by the calling
procedure to supply parameters to the called procedure.
The optional control link points to the activation record
optional control of the caller.
link/ Dynamic Link
The optional access link points to latest Activation Record of the
immediately enclosing procedure and i is used to refer to nonlocal data
optional access held in other activation records.
link/static link The field for saved machine status holds information about
the state of the machine before the procedure is called. If called proc wants to use the
saved machine registers used by calling procedure, these have t o be saved before and restored after
the execution of called procedure
status
The field of local data holds data that local to an execution
local variables of a procedure..
Temporary variables are stored in the field of temporaries./
Temporaries return to appropriate control point of the calling procedure
return address COE 401 Compiler Design
15
8
10/21/2018
9
10/21/2018
q(2)
a:2
q(1)
a:1
10
10/21/2018
11
10/21/2018
12
10/21/2018
• Who deallocates?
– Callee de-allocates the part allocated by Callee.
– Caller de-allocates the part allocated by Caller.
CS416 Compiler Design 25
13
10/21/2018
14
10/21/2018
15
10/21/2018
16
10/21/2018
17
10/21/2018
18
10/21/2018
temporaries
array a
array b
19
10/21/2018
Lexical Scope
• The scope of a declaration in a block-structured
language is given by the mostly closed rule.
• Each procedure (block) will have its own activation
record.
– procedure
– begin-end blocks
• (treated same as procedure without creating most part of its
activation record)
• A procedure may access to a nonlocal name using:
– access links in activation records, or
– displays (an efficient way to access to nonlocal names)
Access Links
main
program main; access link
var a:int; a:
procedure p;
q(1)
var d:int;
access link
begin a:=1; end;
Access i,b:
procedure q(i:int);
Links q(0)
var b:int;
procedure s; access link
var c:int; i,b:
begin p; end; s
begin access link
if (i<>0) then q(i-1) c:
else s;
p
end;
access link
begin q(1); end;
d:
COE 401 Compiler Design 40
20
10/21/2018
Procedure Parameters
main
program main;
access link
procedure p(procedure a);
begin a; end;
procedure q; q
procedure s; access link
begin ... end;
begin p(s) end;
p
begin q; end;
access link
21
10/21/2018
Example
22
10/21/2018
23
10/21/2018
Displays
•Accessing the non-local variables involves going down the static link more than once.
•This can be made more efficient if An array of pointers to activation records can be used
to access activation records.
• This array is called as displays.
• For each level, there will be an array entry.
24
10/21/2018
25