Run-Time Environment
Run-time Environment
• Compiler must cooperate with OS and other system software to
support implementation of different abstractions (names, scopes,
bindings, data types, operators, procedures, parameters, flow-of-
control) on the target machine
• Compiler does this by Run-Time Environment in which it
assumes its target programs are being executed
• Run-Time Environment deals with
– Layout and allocation of storage
– Access to variable and data
– Linkage between procedures
– Parameter passing
– Interface to OS, I/O devices etc
Storage Organization
Code
Static
Heap
Free Memory
Stack
• Compiler deals with logical address space
• OS maps the logical addresses to physical addresses
Memory locations for code are determined at compile time. Usually placed in the
low end of memory
Static- Size of some program data are known at
compile time can be placed another statically
determined area
Dynamic space areas – size changes during program
execution.
• Heap
• Grows towards higher address
• Stores data allocated under program control
• Stack
• Grows towards lower address
• Stores activation records-It is Datastructure
Typical subdivision of run-time memory
Run-Time Environments
• How do we allocate the space for the generated target code and the
data object of our source programs?
• The places of the data objects that can be determined at compile
time will be allocated statically.
• But the places for the some of data objects will be
allocated at run-time.
• The allocation and de-allocation of the data objects is
managed by the run-time support package.
– run-time support package is loaded together with the generated
target code.
– the structure of the run-time support package depends on the
semantics of the programming language (especially the
semantics of procedures in that language).
Storage Allocation Strategies
•static allocation lays out storage for all data objects at compile
time.
–Restrictions:
»size of object must be known and alignment
requirements must be known at compile time.
»No recursion.
»No dynamic data structure
•Stack allocation manages the run time storage as a stack
–The activation record is pushed on as a function is entered.
–The activation record is popped off as a function exits.
–Restrictions:
»values of locals cannot be retained when an activation
ends.
»A called activation cannot outlive a caller.
•Heap allocation -- allocates and deallocates stroage as needed
at runtime from a data area known as heap.
–Most flexible: no longer requires the activation of
procedures to be LIFO.
–Most inefficient: need true dynamic memory management.
•Note: static allocation too restricted, heap allocation too
inefficient. Most current compiler/language/processor uses the
stack allocation scheme.
Procedure Activations
• Each execution of a procedure is called as activation of that
procedure.
• An execution of a procedure P starts at the beginning of the
procedure body;
• When a procedure P is completed, it returns control to the point
immediately after the place where P was called.
• Lifetime of an activation of a procedure P is the sequence of
steps between the first and the last steps in execution of P
(including the other procedures called by P).
• 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.
–Example of stack allocation:
Program sort
var
procedure readarray;
….
function partition(…)
….
procedure quicksort(…)
……
partition
quicksort
quicksort
….
Begin
……
readarray
quicksort
end
Main
readarray quicksort(1, 9)
partition(1, 9) quicksort(1, 3)
partition(1, 3) quicksort(1, 0)
Activation Tree/ Call 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.
Activation Tree (cont.)
program main; procedure s;
begin ... end;
procedure p; procedure q;
begin ... end; begin q; s; end;
begin p; s; end;
enter main
enter p
enter q exit
q enter s
exit s
exit p enter
s exit s
exit main
A Nested
Structure
Activation Tree (cont.)
main
p s
q s
Control stack
• The Control stack is a data structure used at
runtime to keep track of live procedure
activations.
• When the life time of a procedure begins then
push a node onto the stack and pop it at the end
of its life time.
• Dynamic – grows and shrinks
•Activation Records:
•also called frames
•Information(memory) needed by a single
execution of a procedure
•A general activation record:
Return value
actual parameters
optional control link
optional access link
machine status
local variables
temporaries
Content of Activation
Record
• Temporary variables
• Control Link-It points to the activation record of
the calling procedure. This link is also called
dynamic link.
• Access Link-It refers to the nonlocal data in other
activation record. It is also called Static link.
• Return Value-Store the result of activation call.
Activation Records (Ex1)
program main;
procedure p;
var a:real;
procedure q;
var b:integer;
begin ... end;
begin q; end;
procedure s;
var c:integer;
begin ... end; begin p; s;
end;
main
p
a:
q
b:
stack
main
p
q
s
•How would this happen (push and pop the
activation record)?
–Everything must be done by the compiler.
–What makes this happen is known as calling sequence
(how to implement a procedure call).
•A calling sequence allocates an activation record and enters
information into its fields (push the activation record).
–On the opposite of the calling sequence is the return
sequence.
•Return sequence restores the state of the machine so that the
calling procedure can continue execution.
•A possible calling sequence:
–The caller evaluates actuals and push the actuals on the stack
–The caller saves return address(pc) the old value of sp into the stack
–The caller increments the sp
–The callee saves registers and other status information
–The callee initializes its local variables and begin execution.
•A possible return sequence:
–The callee places a return value next to the activation record of the
caller.
–The callee restores other registers and sp and return (jump to pc).
–The caller copies the return value to its activation record.
•In today’s processors, there is usually special support for
efficiently realizing calling/return sequence: executing
procedures is too important!!
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)
a. is local
b. is non-local
procedure p; var b:real;
procedure q;
var a: integer;
begin
a:= 1; b := 2; end;
begin ... end;
Scope test()
{
Int p,q;
{
Int p;
{
Int r; B3 B2 B1
}
}
{
int q,s,t; B4
}
}
Solution
Block Local Variable Non Local
Variable
B1 pB1,qB1 -
B2 pB2 pB1,qB1
B3 rB3 pB2, qB1
B4 qB4,SB4,TB4 pB1,qB1
Access to Non Local Data on
the stack
• A procedure may sometimes refer to a variables which are not
local to it, such variables are called as non local variable.
For Non local names
Two Scope Rule
• Static Scope Rule
• Dynamic Scope Rule
Handling Non Local Data
Static Scope or
Lexical Scope
Dynamic scope
Used by Block
structured
language
Used by Non
block Structured
language
Access Link
Display
Deep Access
Shallow Access
Static Scope
Block
• A block is a statement containing its own local data
declarations
C, a block has the syntax
•{Declarations statements}
{-Beginning
}-End
• Block is in nesting Fashion Means
• The block B2 completely can be inside the block B1.
• Scope of a declaration in a block structured
language is given by the most closely nested or
scope rule
The declaration is visible at a program
• The declaration that are made locally in the
procedure
• The names of all enclosing procedures
• The declarations of names made immediately
within such procedures.
Example
Scope test()
{
Int p,q;
{
Int p;
{
Int r; B3 B2 B1
}
}
{
int q,s,t; B4
}
}
•Storage for a complete procedure body at one time.
•Storage for a names corresponding to particular block can be
as shown below.
p(B1)
q(B1)
p(B1)
q(B1)
p(B2)
Top
Top
p(B1)
q(B1)
p(B2)
r(B3)
Top
p(B1)
q(B1)
p(B2)
p(B1)
q(B1)
p(B1)
q(B1)
q(B4)
s(B4)
t(B4)
Lexical Scope for nested
procedure
Nested procedure
• A procedure that can be declared within another procedure
• A procedure p can call any procedure that its direct ancestor or
older siblings of its direct ancestor.
The nested procedure
Procedure P1
Procedure P2
Procedure P3
Procedure P4
Nesting Depth
Nesting depth used to implement Lexical scope.
Nesting depth can be calculated as follows:
• The nesting depth of the main program is 1
• Add 1 to depth of each time when a new procedure begins.
• Subtract 1 from depth each time when you exit from a nested
procedure.
• The variable declared in specific procedure is associated with nesting
depths.
Lexical Scope for a nested
Procedure
Nesting Depth
•Procedure main
–Procedure A
–Procedure B
–End
end
–Procedure C
–End
End Depth=1
Depth=1
D
e
p
t
h
=
2
Depth=3
Depth=2
Access Link
•The implementation of lexical scope can be obtained
by using pointer to each activation record. These
pointers are called access links.
•If a procedure p is nested within a procedure q then
access link of p points to access link of most recent
activation record of procedure q.
•For example
•Program test
var a:int;
procedure A;
var d:int;
begin a=1
end
procedure B(i:int);
var b:int;
procedure C;
var k:int;
begin A; end;
begin
if(i<0) then B(i-1)
else C;
end
begin B(1);end;
test
Access link
a:
B(1)
Access link
I,b
test
Access link
a:
B(1)
Access link
I,p
B(0)
Access link
I,b
test
Access link
a:
B(1)
Access link
I,b
B(0)
Access link
I,b
C
Access link
k
To setup access link at compile time
Suppose procedure A at nesting depth nA calls procedure B at depth nB The
storage for a can be found as follows.
Case 1:if nA<nB then B is enclosed in A and nA=nB-1
Case2: If nA>=nB, then it is either a recursive call or calling previously
declared procedure.
•The access link of activation record of procedure A point to Activation
record of Procedure B where the procedure B has procedure A nested within
it.
•The Activation record of B must be active at the time of pointing.
•If there are several activation record for B then the most recent activation
record will be pointed.
Displays
It is expensive to traverse down access link every time when a
particular non local variable is accessed. To speed up access to
non-locals can be obtained using an array of pointers to
activation records, called a display.
In Display
a)An array of pointers to activation record is maintained.
b)Array is indexed by nesting level.
c)The pointers point to only accessible activation record
d)The display changes when a new activation occurs and it must
be reset when control returns from the new activation.
Example
Consider the scope of procedure as shown below
Nesting depth y calls a procedure at depth x then
i)y<x then x=y+1 and the called procedure is nested within caller. Then by
keeping first y elements of display array as it is we can set display[x] to the
new activation record.
ii)y>=x then first x-1 elements will be kept as it is in display array but
display[x] points to new activation record.
Display
P0P1P2
P3
P4
P0
P1
P2
P0
P3
P4
0
1
2
0
1
2
Comparison
1.Access link take more time to access non local variables especially
when non local variables are at many nested levels away.
2.Display used to generate code efficiently.
3.Display requires more space at the run time than access links.
Dynamic Scope
• In dynamic scoping symbol table can be required at
run time.
• Deep Access
The idea is a stack of active variable, use control links instead of
access links and when you want to find a variable then search the stack
from top to bottom looking for most recent activation record that contains
the space for desired variables.
This method of accessing non local variables is called deep access. The
search is made deep in the stack.
It takes longer time to access non locals.

Runtimeenvironment

  • 1.
  • 2.
    Run-time Environment • Compilermust cooperate with OS and other system software to support implementation of different abstractions (names, scopes, bindings, data types, operators, procedures, parameters, flow-of- control) on the target machine • Compiler does this by Run-Time Environment in which it assumes its target programs are being executed • Run-Time Environment deals with – Layout and allocation of storage – Access to variable and data – Linkage between procedures – Parameter passing – Interface to OS, I/O devices etc
  • 3.
    Storage Organization Code Static Heap Free Memory Stack •Compiler deals with logical address space • OS maps the logical addresses to physical addresses Memory locations for code are determined at compile time. Usually placed in the low end of memory Static- Size of some program data are known at compile time can be placed another statically determined area Dynamic space areas – size changes during program execution. • Heap • Grows towards higher address • Stores data allocated under program control • Stack • Grows towards lower address • Stores activation records-It is Datastructure Typical subdivision of run-time memory
  • 4.
    Run-Time Environments • Howdo we allocate the space for the generated target code and the data object of our source programs? • The places of the data objects that can be determined at compile time will be allocated statically. • But the places for the some of data objects will be allocated at run-time.
  • 5.
    • The allocationand de-allocation of the data objects is managed by the run-time support package. – run-time support package is loaded together with the generated target code. – the structure of the run-time support package depends on the semantics of the programming language (especially the semantics of procedures in that language).
  • 6.
    Storage Allocation Strategies •staticallocation lays out storage for all data objects at compile time. –Restrictions: »size of object must be known and alignment requirements must be known at compile time. »No recursion. »No dynamic data structure •Stack allocation manages the run time storage as a stack –The activation record is pushed on as a function is entered. –The activation record is popped off as a function exits. –Restrictions: »values of locals cannot be retained when an activation ends. »A called activation cannot outlive a caller.
  • 7.
    •Heap allocation --allocates and deallocates stroage as needed at runtime from a data area known as heap. –Most flexible: no longer requires the activation of procedures to be LIFO. –Most inefficient: need true dynamic memory management. •Note: static allocation too restricted, heap allocation too inefficient. Most current compiler/language/processor uses the stack allocation scheme.
  • 8.
    Procedure Activations • Eachexecution of a procedure is called as activation of that procedure. • An execution of a procedure P starts at the beginning of the procedure body; • When a procedure P is completed, it returns control to the point immediately after the place where P was called. • Lifetime of an activation of a procedure P is the sequence of steps between the first and the last steps in execution of P (including the other procedures called by P).
  • 9.
    • If Aand 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.
  • 10.
    –Example of stackallocation: Program sort var procedure readarray; …. function partition(…) …. procedure quicksort(…) …… partition quicksort quicksort …. Begin …… readarray quicksort end Main readarray quicksort(1, 9) partition(1, 9) quicksort(1, 3) partition(1, 3) quicksort(1, 0)
  • 11.
    Activation Tree/ CallTree • 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.
  • 12.
    Activation Tree (cont.) programmain; procedure s; begin ... end; procedure p; procedure q; begin ... end; begin q; s; end; begin p; s; end; enter main enter p enter q exit q enter s exit s exit p enter s exit s exit main A Nested Structure
  • 13.
  • 14.
    Control stack • TheControl stack is a data structure used at runtime to keep track of live procedure activations. • When the life time of a procedure begins then push a node onto the stack and pop it at the end of its life time. • Dynamic – grows and shrinks
  • 15.
    •Activation Records: •also calledframes •Information(memory) needed by a single execution of a procedure •A general activation record: Return value actual parameters optional control link optional access link machine status local variables temporaries
  • 16.
    Content of Activation Record •Temporary variables • Control Link-It points to the activation record of the calling procedure. This link is also called dynamic link. • Access Link-It refers to the nonlocal data in other activation record. It is also called Static link. • Return Value-Store the result of activation call.
  • 17.
    Activation Records (Ex1) programmain; procedure p; var a:real; procedure q; var b:integer; begin ... end; begin q; end; procedure s; var c:integer; begin ... end; begin p; s; end; main p a: q b: stack main p q s
  • 18.
    •How would thishappen (push and pop the activation record)? –Everything must be done by the compiler. –What makes this happen is known as calling sequence (how to implement a procedure call). •A calling sequence allocates an activation record and enters information into its fields (push the activation record). –On the opposite of the calling sequence is the return sequence. •Return sequence restores the state of the machine so that the calling procedure can continue execution.
  • 19.
    •A possible callingsequence: –The caller evaluates actuals and push the actuals on the stack –The caller saves return address(pc) the old value of sp into the stack –The caller increments the sp –The callee saves registers and other status information –The callee initializes its local variables and begin execution. •A possible return sequence: –The callee places a return value next to the activation record of the caller. –The callee restores other registers and sp and return (jump to pc). –The caller copies the return value to its activation record. •In today’s processors, there is usually special support for efficiently realizing calling/return sequence: executing procedures is too important!!
  • 20.
    Variable Scopes – Thesame 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) a. is local b. is non-local procedure p; var b:real; procedure q; var a: integer; begin a:= 1; b := 2; end; begin ... end;
  • 21.
    Scope test() { Int p,q; { Intp; { Int r; B3 B2 B1 } } { int q,s,t; B4 } }
  • 22.
    Solution Block Local VariableNon Local Variable B1 pB1,qB1 - B2 pB2 pB1,qB1 B3 rB3 pB2, qB1 B4 qB4,SB4,TB4 pB1,qB1
  • 23.
    Access to NonLocal Data on the stack • A procedure may sometimes refer to a variables which are not local to it, such variables are called as non local variable. For Non local names Two Scope Rule • Static Scope Rule • Dynamic Scope Rule
  • 24.
    Handling Non LocalData Static Scope or Lexical Scope Dynamic scope Used by Block structured language Used by Non block Structured language Access Link Display Deep Access Shallow Access
  • 25.
    Static Scope Block • Ablock is a statement containing its own local data declarations C, a block has the syntax •{Declarations statements} {-Beginning }-End • Block is in nesting Fashion Means • The block B2 completely can be inside the block B1.
  • 26.
    • Scope ofa declaration in a block structured language is given by the most closely nested or scope rule The declaration is visible at a program • The declaration that are made locally in the procedure • The names of all enclosing procedures • The declarations of names made immediately within such procedures.
  • 27.
    Example Scope test() { Int p,q; { Intp; { Int r; B3 B2 B1 } } { int q,s,t; B4 } }
  • 28.
    •Storage for acomplete procedure body at one time. •Storage for a names corresponding to particular block can be as shown below. p(B1) q(B1) p(B1) q(B1) p(B2) Top Top p(B1) q(B1) p(B2) r(B3) Top p(B1) q(B1) p(B2) p(B1) q(B1) p(B1) q(B1) q(B4) s(B4) t(B4)
  • 29.
    Lexical Scope fornested procedure Nested procedure • A procedure that can be declared within another procedure • A procedure p can call any procedure that its direct ancestor or older siblings of its direct ancestor. The nested procedure Procedure P1 Procedure P2 Procedure P3 Procedure P4
  • 30.
    Nesting Depth Nesting depthused to implement Lexical scope. Nesting depth can be calculated as follows: • The nesting depth of the main program is 1 • Add 1 to depth of each time when a new procedure begins. • Subtract 1 from depth each time when you exit from a nested procedure. • The variable declared in specific procedure is associated with nesting depths.
  • 31.
    Lexical Scope fora nested Procedure Nesting Depth •Procedure main –Procedure A –Procedure B –End end –Procedure C –End End Depth=1 Depth=1 D e p t h = 2 Depth=3 Depth=2
  • 32.
    Access Link •The implementationof lexical scope can be obtained by using pointer to each activation record. These pointers are called access links. •If a procedure p is nested within a procedure q then access link of p points to access link of most recent activation record of procedure q. •For example
  • 33.
    •Program test var a:int; procedureA; var d:int; begin a=1 end procedure B(i:int); var b:int; procedure C; var k:int; begin A; end; begin if(i<0) then B(i-1) else C; end begin B(1);end;
  • 34.
    test Access link a: B(1) Access link I,b test Accesslink a: B(1) Access link I,p B(0) Access link I,b test Access link a: B(1) Access link I,b B(0) Access link I,b C Access link k
  • 35.
    To setup accesslink at compile time Suppose procedure A at nesting depth nA calls procedure B at depth nB The storage for a can be found as follows. Case 1:if nA<nB then B is enclosed in A and nA=nB-1 Case2: If nA>=nB, then it is either a recursive call or calling previously declared procedure. •The access link of activation record of procedure A point to Activation record of Procedure B where the procedure B has procedure A nested within it. •The Activation record of B must be active at the time of pointing. •If there are several activation record for B then the most recent activation record will be pointed.
  • 36.
    Displays It is expensiveto traverse down access link every time when a particular non local variable is accessed. To speed up access to non-locals can be obtained using an array of pointers to activation records, called a display. In Display a)An array of pointers to activation record is maintained. b)Array is indexed by nesting level. c)The pointers point to only accessible activation record d)The display changes when a new activation occurs and it must be reset when control returns from the new activation.
  • 37.
    Example Consider the scopeof procedure as shown below Nesting depth y calls a procedure at depth x then i)y<x then x=y+1 and the called procedure is nested within caller. Then by keeping first y elements of display array as it is we can set display[x] to the new activation record. ii)y>=x then first x-1 elements will be kept as it is in display array but display[x] points to new activation record.
  • 38.
  • 39.
    Comparison 1.Access link takemore time to access non local variables especially when non local variables are at many nested levels away. 2.Display used to generate code efficiently. 3.Display requires more space at the run time than access links.
  • 40.
    Dynamic Scope • Indynamic scoping symbol table can be required at run time. • Deep Access The idea is a stack of active variable, use control links instead of access links and when you want to find a variable then search the stack from top to bottom looking for most recent activation record that contains the space for desired variables. This method of accessing non local variables is called deep access. The search is made deep in the stack. It takes longer time to access non locals.