1. UNIT IV RUN TIME
ENVIRONMENT
Run-time Environment- Source Language
Issues-Storage Organization-Storage
Allocation-Parameter Passing-Symbol Tables-
Dynamic Storage Allocation
Ms.S.Kalavathi,ASP/CSE
2. Run time environment
Runtime environment is a state of the target machine,
which may include software libraries, environment
variables, etc., to provide services to the processes
running in the system.
•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.
•Facilitate the process communication between the process and
the runtime environment Ms.S.Kalavathi,ASP/CSE
3. Run time environment tasks
1. Memory management when program is running.
• Allocating and deallocating memory locations on the
fly.
2. Passing parameters to functions.
3. Interfacing between program and computer resources.
4. Manages runtime memory requirements such as
1. Code
2. Procedures
3. Variables
Ms.S.Kalavathi,ASP/CSE
4. Source language issues
1. Scope and lifetime of variables
1. Local and global variables
2. Static and dynamic variables
2. Procedures
1. Based on the types of parameter formal / actual
3. Activation tree
1. Corresponds to procedure call of a program
2. Whenever a procedure is called, an activation tree is created
to hold the local variables of the called procedure.
Ms.S.Kalavathi,ASP/CSE
5. Memory management
Compiler must do the storage allocation and provide access
to variables and data
Memory management
Stack allocation
Heap management
Garbage collection
Ms.S.Kalavathi,ASP/CSE
6. Storage organization
• 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
Ms.S.Kalavathi,ASP/CSE
9. Static and dynamic storage allocation
Static: Compile time, Dynamic: Runtime allocation
Many compilers use some combination of following
Stack storage: for local variables, parameters and so on
Heap storage: Data that may outlive the call to the
procedure that created it
Stack allocation is a valid allocation for procedures since
procedure calls are nested
Ms.S.Kalavathi,ASP/CSE
10. – 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.
Ms.S.Kalavathi,ASP/CSE
15. Static allocation – activation record
Activations of a procedure can be represented as a tree called activation tree.
1.Each node represents activation of a program.
2.Root represents activation of main program.
3.Node for procedure a is parent of node for procedure b if and only if control flows
from a to b.
4.Node for a is to the left of node for b if and only of lifetime of a occurs before lifetime
of b.
5.Lifetime of activation of a procedure is the sequence of steps between first and last
steps in procedure body, including time spent on executing other procedures called by
the procedure.
Ms.S.Kalavathi,ASP/CSE
16. Stack allocation – activation tree
Stack allocation would not be feasible if procedure calls, or
activations of procedures, did not nest in time.
Ms.S.Kalavathi,ASP/CSE
18. Activation record
• Procedure calls and returns are usually managed by a run-
time stack called the control stack.
• Each live activation has an activation record on the control
stack, with the root of the activation tree at the bottom, and
the entire sequence of activation records on the stack
corresponding to the path in the activation tree to the
activation where control currently resides.
• The latter activation has its record at the top of the stack.
Ms.S.Kalavathi,ASP/CSE
19. Activation record
Temporaries Stores temporary and intermediate values of an expression.
Local Data Stores local data of the called procedure.
Machine Status Stores machine status such as Registers, Program Counter etc.,
before the procedure is called.
Control Link Stores the address of activation record of the caller procedure.
Access Link Stores the information of data which is outside the local scope.
Actual
Parameters
Stores actual parameters, i.e., parameters which are used to
send input to the called procedure.
Return Value Stores return values.
Ms.S.Kalavathi,ASP/CSE
20. Activation record
Temporary values
Local data
A saved machine status
An “access link”
A control link
Space for the return value of the called function
The actual parameters used by the calling procedure
Ms.S.Kalavathi,ASP/CSE
21. Heap management
The heap is the portion of the store that is used for data that
lives indefinitely, or until the program explicitly deletes it.
The memory manager keeps track of all the free space in
heap storage at all times.
It performs two basic functions:
Allocation
Deallocation
•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.
Ms.S.Kalavathi,ASP/CSE
23. • 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!!
Ms.S.Kalavathi,ASP/CSE
24. • Access to nonlocal variables.
– Nonlocal variables in C (without nested procedures):
•Still have nested scopes (blocks).
•Solution:
–All data declared outside procedures are static.
–Other names must be at the activation record at the top of
the stack, can be accessed from sp.
»Treat a block as a parameter-less procedure
»Allocates space for all blocks in a procedure.
Ms.S.Kalavathi,ASP/CSE
26. • Access to nonlocal variables.
– Nonlocal variables in PASCAL (with nested
procedures):
•the scheme for C will break.
Ms.S.Kalavathi,ASP/CSE
27. • Access to nonlocal variables.
– Nonlocal variables in PASCAL (with nested
procedures):
•The scheme for C will break (static for all non-
locals).
•Access links
–If p is nested immediately within q in the source text, then
the access link in an activation record for p points to the
access link in the record for the most recent activation of q.
–A procedure p at nesting depth n_p accesses a nonlocal a at
nesting depth n_a: (1) following n_p – n_a links and (2)
using the relative offset in the activation record.
Ms.S.Kalavathi,ASP/CSE
30. • Display:
•An alternative to access link ( a faster method to
access nonlocals ).
•Using an array d of pointers to activation records, the
array is called a display.
–Referencing nonlocal variables always requires only two
memory references.
•Suppose control is in a procedure p at nesting depth j,
then the first j-1 elements of the display point to the
most recent activation of the procedures that lexically
enclose procedure p, and d[j] points to the activation
of p.
Ms.S.Kalavathi,ASP/CSE
31. • Setting up the display:
•When a new activation record for a procedure at
nesting depth k:
–save the value of d[k] in the new activation record
–set d[k] to point to the new activation record.
Ms.S.Kalavathi,ASP/CSE
32. – Parameter passing
•The method to associate actual parameters with
formal parameters.
•The parameter passing method will effect the code
generated.
•Call-by-value:
–The actual parameters are evaluated and their r-values are
passed to the called procedure.
–Implementation:
»a formal parameter is treated like a local name, so the
storage for the formals is in the activation record of the
called procedure.
»The caller evaluates the actual parameters and places
their r-values in the storage for the formals.
Ms.S.Kalavathi,ASP/CSE
33. – Call-by-reference:
•also called call-by address or call-by-location.
•The caller passes to the called procedure a pointer to
the storage address of each actual parameter.
–Actuall parameter must have an address -- only variables
make sense, an expression will not (location of the
temporary that holds the result of the expression will be
passed).
– Copy-restore:
•A hybrid between call-by-value and call-by-
reference.
–The actual parameters are evaluated and its r-values are
passed to the called procedure as in call-by-value.
–When the control returns, the r-value of the formal
parameters are copied back into the l-value of the actuals.
Ms.S.Kalavathi,ASP/CSE
34. Example 1: illustrates call by value, value-result, reference
begin
integer n;
procedure p(k: integer);
begin
n := n+1;
k := k+4;
print(n);
end;
n := 0;
p(n);
print(n);
end;
Note that when using call by reference, n and k are
aliased.
Output:
call by value: 1 1
call by value-result: 1 4
call by reference: 5 5
Ms.S.Kalavathi,ASP/CSE