You are on page 1of 25

10/21/2018

RUN TIME ENVIRONMENT


MANAGEMENT

COE 401 Compiler Design 1

• Run time environment refers to the program


snap-shot during execution
• A program consists of three main segments:
– code for the program
– Static and global variables
– Local variables and arguments

• Each of these segments require proper


allocation of memory to hold their values

COE 401 Compiler Design 2

1
10/21/2018

• Therefore three kinds of entities are to be


managed at run time
– Generated Code : for various procedures and
programs that form code segment. The size of this
segment is known at compile time hence space can be
allocated statically before the execution starts
– Data Objects : They are of three types
• Global variables/constants – size known at compile time
• Local variables - size known at compile time
• Variables created dynamically – these variables correspond
to the space created in response to the memory allocation
requests from the program during execution hence size is
unknown at compile time. Dynamic allocation is done in
heap
– Stack – to keep track of procedure activations

COE 401 Compiler Design 3

Run-Time Storage Organization(Logical


address space of a program)
Memory locations for code are
Code determined at compile time.

Locations of static data can also be


Static Data
determined at compile time.

Data objects allocated at run-time.


Stack (Activation Records)

Other dynamically allocated data


objects at run-time. (For example,
malloc area in C).

Heap

COE 401 Compiler Design 4

2
10/21/2018

COE 401 Compiler Design 5

COE 401 Compiler Design 6

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.

COE 401 Compiler Design 8

4
10/21/2018

Activation Tree (cont.)


program main; enter main
procedure s; enter p
begin ... end; enter q
procedure p; exit q
procedure q; enter s
begin ... end; exit s
begin q; s; end; exit p
begin p; s; end; enter s
exit s
exit main

A Nested Structure

COE 401 Compiler Design 9

Activation Tree (cont.)


main

p s

q s

COE 401 Compiler Design 10

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.

COE 401 Compiler Design 11

Downward-growing stack of activation records

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;

COE 401 Compiler Design 13

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

Activation Records (cont.)


The returned value of the called procedure is returned
return value in this field to the calling procedure. In practice, we may
use a machine register for the return value.

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

COE 401 Compiler Design 16

8
10/21/2018

Activation Record of avg during its


invocation from std

COE 401 Compiler Design 17

Activation Records (Ex1) stack


main
program main;
procedure p;
var a:real;
procedure q;
var b:integer;
begin ... end; p
begin q; end;
procedure s; a:
var c:integer;
begin ... end; main
begin p; s; end; q
p s
b:

COE 401 Compiler Design 18

9
10/21/2018

Activation Records for Recursive


program main;
Procedures
main
procedure p;
function q(a:integer):integer;
begin
if (a=1) then q:=1; p
else q:=a+q(a-1);
end;
begin q(3); end; q(3)
begin p; end; a: 3

q(2)
a:2

q(1)
a:1

COE 401 Compiler Design 19

COE 401 Compiler Design 20

10
10/21/2018

COE 401 Compiler Design 21

COE 401 Compiler Design 22

11
10/21/2018

COE 401 Compiler Design 23

• Register top_sp points to end of the machine


status field in the AR . This position is known
to caller so it can be made responsible for
setting top_sp before control flows to the
called procedure
• Register top marks the top of the stack. At run
time an AR can be allocated and deallocated
by incrementing and decrementing top by size
of the record

COE 401 Compiler Design 24

12
10/21/2018

Creation of An Activation Record


• Who allocates an activation record of a
procedure?
– Some part of the activation record of a procedure is
created by that procedure immediately after that
procedure is entered.
– Some part is created by the caller of that procedure before
that procedure is entered.

• Who deallocates?
– Callee de-allocates the part allocated by Callee.
– Caller de-allocates the part allocated by Caller.
CS416 Compiler Design 25

Creation of An Activation Record


return value Caller’s Activation Record
actual parameters
optional control link
optional access link
saved machine status
local data Caller’s Responsibility
temporaries
return value Callee’s Activation Record
actual parameters
optional control link
optional access link
saved machine status
Callee’s Responsibility
local data
temporaries
CS416 Compiler Design 26

13
10/21/2018

COE 401 Compiler Design 27

Compiling Procedure Calls


• During a Procedure Call
• Caller’s Activities :
1. Makes space on the stack for a return value.
2. Puts actual parameters on the stack
3. Sets the static link
4. Jumps to the called procedure. Return address is
saved on the stack
• Calles’s Activities
1. Sets the Dynamic Link
2. Sets the base of new activation record
3. Saves registers on stack
4. Makes space for local variables on stack

COE 401 Compiler Design 28

14
10/21/2018

• During Return from the procedure


• Callee’s Activities
1. Restores Registers
2. Sets the base pointer to the activation record of
calling procedure
3. Returns to the caller
• Caller’s Activities
1. Returns the stack pointer to the location
containing the returned value

COE 401 Compiler Design 29

COE 401 Compiler Design 30

15
10/21/2018

COE 401 Compiler Design 31

• Suppose a procedure p has three local arrays.


The storage for these arrays is not part of AR
of p; only pointer to beginning of each array
exists
• The relative addresses of these arrays are
known at compile time so target code can
access arrays through the pointers.
• As shown in fig procedure q is called by p. The
AR of q begins after the arrays of p and
variable length arrays of q begin beyond that

COE 401 Compiler Design 32

16
10/21/2018

COE 401 Compiler Design 33

COE 401 Compiler Design 34

17
10/21/2018

COE 401 Compiler Design 35

COE 401 Compiler Design 36

18
10/21/2018

Variable Length Data


return value
actual parameters Variable length data is allocated after
optional control link temporaries, and there is a link to from
local data to that array.
optional access link
saved machine status
local data
pointer a
pointer b

temporaries

array a

array b

COE 401 Compiler Design 37

Access to Nonlocal Names


• Scope rules of a language determine the
treatment of references to nonlocal names.
• Scope Rules:
– Lexical Scope (Static Scope)
• Determines the declaration that applies to a name by
examining the program text alone at compile-time.
• Most-closely nested rule is used.
• Pascal, C, ..
– Dynamic Scope
• Determines the declaration that applies to a name at run-
time.
• Lisp, APL, ...

COE 401 Compiler Design 38

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)

COE 401 Compiler Design 39

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

Access links must be passed with s


procedure parameters. access link

COE 401 Compiler Design 41

• Hence non local variables of a procedure are


accessed by chasing a chain of static link
pointers starting from the base of activation
record of the procedure
• But how one sets the static link during a
procedure call. It depends upon the relative
levels of the calling and called procedure
• Following rules are used:
– The level of main program is 1
– If The level of an enclosing procedure is l, then the
level of enclosed procedure is l+1

COE 401 Compiler Design 42

21
10/21/2018

Example

COE 401 Compiler Design 43

COE 401 Compiler Design 44

22
10/21/2018

COE 401 Compiler Design 45

• The rules for setting the static link in terms of


levels are
– If the calling procedure is at level l1 and the called
procedure is at level l2, then starting from the
activation record of the calling procedure the
static link is traversed l1-l2+1 times
– The static link of the called procedure is made to
point to the base of the activation record thus
reached
– E.g. T(level 3) calls Q(level 2) then we should start
fromAR of T and travel down the static link
twice(3-2+1) to reach the base of AR of S
COE 401 Compiler Design 46

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.

1: Current activation record at level 1

2: Current activation record at level 2

3: Current activation record at level 3

COE 401 Compiler Design 47

Accessing Nonlocal Variables


main addrC := offsetC(currAR)
program main; access
var a:int; t := *currAR
link addrB := offsetB(t)
procedure p;
var b:int; a: t := *t
begin q; end; p addrA := offsetA(t)
procedure q(); ADD addrA,addrB,addrC
access
var c:int; link
begin b:
c:=a+b;
end; q
begin p; end; access
link
c:

COE 401 Compiler Design 48

24
10/21/2018

Accessing Nonlocal Variables using


Display
main
program main; D[1] addrC := offsetC(D[3])
access addrB := offsetB(D[2])
var a:int; link
procedure p; addrA := offsetA(D[1])
var b:int; a: ADD addrA,addrB,addrC
D[2]
begin q; end; p
procedure q(); access
var c:int; link
begin D[3]
b:
c:=a+b;
end; q
begin p; end; access
link
c:

COE 401 Compiler Design 49

25

You might also like