Unit 5 - Runtime Environment
Unit 5 - Runtime Environment
Unit 5 - Runtime Environment
Runtime Environment
Prepared by:
Dr.R.I.Minu
Associate Professor
1
Run-Time Environments
• A compiler must accurately implement the abstractions
embodied in the source language definition.
• These abstractions typically include names, scopes,
bindings, data types, operators, procedures,
parameters, and flow-of-control constructs.
• The compiler must cooperate with the operating
system and other systems software to support these
abstractions on the target machine.
• To do so, the compiler creates and manages a run-time
environment in which it assumes its target programs
are being executed.
Run-Time Environments
This environment deals with a variety of issues
such as
• The layout and allocation of storage locations
for the objects named in the source program
• The mechanisms used by the target program to
access variables,
• The linkages between procedures,
• The mechanisms for passing parameters, and
• The interfaces to the operating system,
input/output devices, and other programs.
Source language issues
• Does the source language allow
recursion - There will be several
instances for a recursive procedure
thus the memory allocation can be
determined only at runtime
• How the parameters are passed to
the procedure - allocation strategies
for call by value and call by reference
are different
Storage Organization
• The executing target program runs in its own
logical address space in which each program
value has a location.
• The management and organization of this logical
address space is shared between the compiler,
operating system, and target machine.
• The operating system maps the logical
addresses into physical addresses, which are
usually spread throughout memory.
• The run-time representation of an object
program in the logical address space consists
of data and program areas
Runtime Environment
• Relationship between names and data objects (of
target machine)
9
Activation Tree
Sort
r q(1,9)
10
Possible
Activati
on
Quick
sort prg
Activati
on Tree
Control stack
• Flow of control in program corresponds to
depth first traversal of activation tree
• Name binding
environment state
name storage value
13
Storage organization
• The runtime storage
might be subdivided into code
stack
– Data objects
14
Activation Record
• temporaries: used in expression
evaluation
Temporaries
• local data: field for local data
local data
16
Layout of local data
• Assume byte is the smallest unit
• Constraints
Sort Sort
Sort Sort
readarray readarray
Sort Sort
qsort(1,9)
readarray qsort(1,9)
Sort Sort
readarray qsort(1,9) qsort(1,9)
23
Return Sequence
• Callee places a return value next to
activation record of caller
array A
array B
Long length
data
array C
activation of Q
arrays of Q
25
Variable-Length Data on
the Stack
• The run-time
memory-
management
system must deal
frequently with
the allocation of
space for objects
the sizes of which
are not known at
Dangling references
Referring to locations which have been deallocated
main()
{int *p;
p = dangle(); /* dangling reference */
}
int *dangle();
{
int i=23;
return &i;
}
27
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
30
Access to non-local names
31
Block
• A block statement contains its own data declarations
32
Example
main()
{ BEGINNING of B0
int a=0 Scope B0, B1, B3
int b=0 Scope B0
{ BEGINNING of B1
int b=1 Scope B1, B2
{ 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
33
Blocks …
• Blocks are simpler to handle
than procedures
a0
34
Lexical scope without nested
procedures
• A procedure definition cannot occur within another
end; end;
procedure exchange(i,j:integer) begin
begin .
end;
begin
end;
.
end.
36
Nesting Depth
• Main procedure is at depth 1
37
sort
quicksort(1,9)
quicksort(1,3)
Stack
partition(1,3)
exchange(i,j)
38
Access to non local names …
• Suppose procedure p at depth np refers to
a non-local a at depth na, then storage for
a can be found as
– follow (np-na) access links from the record at
the top of the stack
– after following (np-na) links we reach procedure
for which a is local
39
How to setup access links?
• suppose procedure p at depth np calls procedure x
at depth nx.
– np ≥ nx
From scoping rules enclosing procedure at the depth
1,2,… ,nx-1 must be same. Follow np-(nx-1) links from the
caller, we reach the most recent activation of the
procedure that encloses both called and calling procedure
40
Displays
• Faster access to
non locals d[1]
d[2] s
d[3]
• Uses an array of
q(1,9)
pointers to
saved d[2]
• Non locals at
p(1,3)
saved d[3]
depth i is in the
activation record e(1,3)
pointed to by d[i]
saved d[2]
44
Justification for Displays
• Suppose procedure at depth j calls procedure at
depth i
• Case j ≥ i
46
Dynamic Scoping: Example
• Consider the following program
procedure show;
begin write(r) end;
procedure small;
var r: real;
begin r := 0.125; show end;
begin
r := 0.25;
show; small; writeln;
show; small; writeln;
end.
47
Example …
• Output under lexical scoping
0.250 0.250
0.250 0.250
• Shallow Access
– hold current value of each name in static
memory
– when a new activation of p occurs a local name n
in p takes over the storage for n
– previous value of n is saved in the activation
record of p
49
Parameter Passing
• Call by value
swap(i,a[i])
temp = i
i = a[i]
a[i] = temp
53
Language Facility for
Dynamic Storage Allocation
• Storage is usually taken from heap
head 76 3 4 2 7 9 nil
head^.next := nil;
Dangling reference
dispose(head^.next )
55
Explicit Allocation of Fixed
Sized Blocks
• Link the blocks in a list
allocated
available
allocated
56
Explicit Allocation of Fixed
Sized Blocks …
• blocks are drawn from contiguous area of storage
57
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
60
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
61
Reference Count
• Keep track of number of blocks which point
directly to the present block
0: return address
/ *code for c */ 0: return address
action –1 8: 4:
call p
arr buf
action -2
halt
j
/ *code for p */ 56:
action –3 60: i
84: n
return
Activation record for c Activation record for
(64 bytes) p (88 bytes)
Three address code 64
Static Allocation
• A call statement is implemented by a
sequence of two instructions
GOTO *callee.static-area
66
Example
• Assume each action 100: ACTION-l
block takes 20 bytes 120: MOV 140, 364
of space 132: GOTO 200
140: ACTION-2
• Start address of 160: HALT
code for c and p is :
100 and 200 200: ACTION-3
220: GOTO *364
:
• The activation 300:
records are statically 304:
allocated starting at :
addresses 300 and 364:
364. 368:
67
Stack Allocation
• Position of the activation record is not known until
run time
MOV #Stackstart, SP
code for the first procedure
HALT
68
Stack Allocation …
• A procedure call sequence increments
SP, saves the return address and
transfers control to the called
procedure
ADD #caller.recordsize, SP
MOVE #here+ 16, *SP
GOTO callee.code_area
69
Stack Allocation …
• The return sequence consists of two parts.
GOTO *0(SP)
SUB #caller.recordsize, SP
70
Example
• Consider the quicksort action-l /* code for s * /
program
call q
• Assume activation action-2
records for procedures halt
s, p and q are ssize, psize
and qsize respectively
(determined at compile action-3 /* code for p * /
time) return
72