Run-Time Environments
Run-Time Environments
2
Source language issues
Procedures
• A procedure definition is a declaration that
associates an identifier with a statement
(procedure body)
4
Example of Procedures
program sort; procedure quicksort (m, n
var a : array[0..10] of integer; :integer);
var i :integer;
procedure readarray; :
i:= partition (m,n);
var i :integer;
quicksort (m,i-1);
:
quicksort(i+1, n);
function partition (y, z :
:integer) :integer; begin{main}
var i, j ,x, v :integer; readarray;
: quicksort(1,9)
end.
5
Output- Activation of procedures
Execution begins…
enter readarray
leave readarray
enter quicksort (1,9)
enter partition (1,9)
leave partition (1,9)
enter quicksort (1,3)
• An activation tree can be used to depict the way control enters and leaves
activations
• The node for a is parent of the node b if and only if control flows from activation
to b, and
• The node for a is to the left of node b if and only if the lifetime of a occurs
before the lifetime of b
7
Activation tree
Sort
r q(1,9)
8
Control Stacks
• Flow of control in program corresponds to a depth first
traversal of the activation tree that starts at the root and
recursively visits children at each node in a left-to-right order
• When the node n is at the top of the stack the stack contains
the nodes along the path from n to the root
9
Control Stack
s
r
q(1,9)
p(1,9)
q(1,3)
- otherwise declared
• Name binding
environment state
name storage value
11
Bindings of Names
• Even if each name is declared once in a
program, the same name may indicate
different objects at run time.
• “Data object” corresponds to a storage
location hold values.
• The term “environment” refers to a function
that maps a name to a storage location and
“state” refers to a function that maps a
storage location to the value held there.
Storage organization
• The runtime storage might
be subdivided into code
stack
– Data objects
13
Activation Record
Information needed by a single execution of a
procedure is managed using a contiguous block
of storage called activation record consisting of Temporaries
fields:
• temporaries: used in expression evaluation local data
• saved machine status: holds info about machine Optional Access links
status before procedure call
Optional Control links
• Optional access link : to access non local data
Parameters
• Optional control link :points to activation record of
caller
Return value
• actual parameters: field to hold actual parameters
15
Storage Allocation Strategies
• Static allocation: lays out storage at
compile time for all data objects
16
Static allocation
• Names are bound to storage as the program is
compiled
• Constraints
19
Stack Allocation
Position in Activation Tree Activation Records on the stack
Sort Sort
Sort Sort
r r
Sort Sort
r q (1,9) q(1,9)
Sort
Sort
r q (1,9)
q (1,9)
p(1,9) q (1,3)
q (1,3)
p(1,3) q (1,0) 20
Calling Sequence
• Procedure calls are …..
implemented by generating Parameter and
Return value
what are known as calling
Control link Caller’s
sequences in the target Activation
code. Links and saved values record
Space for temporaries
And local data
• A call sequence allocates an
Caller’s
responsibility Parameters and
Return value
activation record and enters
Control link Callee’s
information into its field Activation
Links and saved values record
Callee’s
responsibility Space for temporaries
• A return sequence restores And local data
22
Return Sequence
• Callee places a return value next to activation
record of caller
23
Variable Length Data
Dangling references
• Whenever storage can be deallocated, the problem of
dangling references arises
• Occurs when there is a reference to storage that has been
deallocated
• It is a logical error to use dangling references, since the
value of deallocated storage is undefined according to the
semantics of the value of most languages
25
Dangling references
Referring to locations which have been deallocated
main()
{int *p;
p = dangle(); /* dangling reference */
}
int *dangle();
{
int i=23;
return &i;
}
26
Heap Allocation
• Stack allocation cannot be used if:
– The values of the local variables must be retained when an
activation ends
– A called activation outlives the caller. This possibility cannot occur
for those languages where activation trees correctly depict the
flow of control between procedures.
28
Heap Allocation
• Fill a request of size s with block of size s′ where s′
is the smallest size greater than or equal to s
29
Access to non-local names
• Scope rules determine the treatment of
non-local names
30
Blocks
• A block statement contains its own data declarations
31
Example: Blocks in C program
main()
{ BEGINNING of B0
int a=0 Scope B0, B1, B2
int b=0 Scope B0, B1
{ BEGINNING of B1
int b=1 Scope B1, B2, B3
{ BEGINNING of B2
int a=2 Scope B2
print a, b
} END of B2
{ BEGINNING of B3
int b=3 Scope B3
print a, b
} END of B3
print a, b
} END of B1
print a, b
} END of B0
32
Blocks
• Blocks are simpler to handle
than procedures
a0
33
Parameter Passing
• When one procedure calls another, the usual
method of communication between them
through nonlocal names and through parameters
of the called procedure
34
Parameter Passing: Call by value
• Call by value
35
Parameter Passing: Call by reference
• Call by reference (call by address)
37
Parameter Passing : Call by name
• Call by name (used in Algol)
swap(i,a[i])
temp = i
i = a[i]
a[i] = temp
38
Symbol Table
• Keep track of scope and binding information about
names
• It is searched everytime a name is encountered in
the source text
• Changes to the table made if a new name or new
information about an existing name is discovered.
• Mechanism should allow the addition of new entries
and search for existing entries are efficient.
• Symbol table mechanisms are: linear list and hash
tables
Symbol Table
• Linear list- simple to implement but performance is
poor when number of entries (e) and enquiries(e)
get large.
• Hashing schemes- Better performance for somewhat
better programming effort and space overhead.
• Both mechanisms can be adopted readily to handle
the most closely nested scope rule.
• Study about hash table and linear list structure from
CD book.
Symbol Table
• When identifiers are found, they will be
entered into a symbol table, which will hold
all relevant information about identifiers.
• This information will be used later by the
semantic analyzer and the code generator.
Lexical Syntax Semantic Code
Analyzer Analyzer Analyzer Generator
Symbol
Table
Symbol Table Entries
• Each entry of the symbol table is for the
declaration of a name
• Format for entries may not be uniform as the
information saved about a name depends on
the usage of a name.
• Information is entered at various times
• Keywords are entered into table initially
Symbol Table Entries
• This information is stored in an object called
an IdEntry.
• This information may not all be known at
once.
• We may begin by knowing only the name and
data type, and then later learn the block level,
scope, and the offset.
Language Facility for Dynamic Storage
Allocation
• Storage is usually taken from heap
allocated
available
allocated
45
Explicit Allocation of Fixed Sized Blocks …
• Compiler routines need not know the type of objects to be held in the blocks
46
Explicit Allocation of Variable Size Blocks
• Storage can become fragmented
• Situation may arise
• If program allocates five blocks
• then de-allocates second and fourth block
47
First Fit Method
• When a block of size s is to be allocated
– search first free block of size f ≥ s
– sub divide into two blocks of size s and f-s
– time overhead for searching a free block
• When a block is de-allocated
– check if it is next to a free block
– combine with the free block to create a larger
free block
48
Implicit De-allocation
• Requires co-operation
between user program Optional Block size
49
Recognizing Block boundaries
• If block size is fixed then position information can be used
• Pointers are kept in fixed positions and user area does not contain
any pointers
50
Reference Count
• Keep track of number of blocks which point directly to the
present block
51
Marking Techniques
• Suspend execution of the user program
• Compaction: move all used blocks to the end of heap. All the
pointers must be adjusted to reflect the move
52