Runtime Environment
Runtime Environment
At run time, we need a system to map names (in the source program) to
storage on the machine.
Allocation and deallocation of memory is handled by a run- time support
system typically linked and loaded along with the compiled target code.
One of the primary responsibilities of the run-time system is to manage
activations of procedures
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.
Activation Tree
A tree that represents the way the control enters and leaves the
activations is known as the activation tree.
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 if the control flows from A to B.
The node A is left to the node B if the lifetime of A occurs before the
lifetime of B.
Activation Tree
Activation Tree
Control stack is a run time stack which is used to keep track of the live
procedure activations i.e. it is used to find out the procedures whose
execution have not been completed.
When it is called (activation begins) then the procedure name will push
on to the stack and when it returns (activation ends) then it will popped.
Activation record is used to manage the information needed by a single
execution of a procedure.
An activation record is pushed into the stack when a procedure is called
and it is popped when the control returns to the caller function
Activation Record
Activation Record
Storage Allocation
Runtime environment manages runtime memory requirements for the
following entities:
Code:
It is known as the text part of a program that does not change at
runtime. Its memory requirements are known at the compile time.
Procedures :
Their text part is static but they are called in a random manner. That is
why, stack storage is used to manage procedure calls and activations.
Variables :
Variables are known at the runtime only, unless they are global or
constant. Heap memory allocation scheme is used for managing
allocation and de allocation of memory for variables in runtime
They are:
• Static Allocation
• Stack Allocation
• Heap Allocation
Static Allocation
Storage is allocated at compile time
Static storage has fixed allocation that does not change during program
execution
As bindings do not change at runtime, no runtime support is required
At compile time, compiler can fill the address at which the target code
can find the data it operates on.
FORTRAN uses the static allocation strategy.
For each procedure in the program, allocate a space for its activation
record.
A.R.‘s can be allocated in the static data area.
Limitations
Size of data objects should be known at compile time.
Recursion is not supported.
Data structures cannot be created at runtime.
Waste lots of space when procedures are inactive.
No dynamic allocation
Advantages
No stack manipulation or indirect access to names, i.e., faster in
accessing variables.
Values are retained from one procedure call to the next if block
structure is not allowed.
For example: static variables in C
Stack Allocation
Stack allocation manages the runtime storage as a stack, i.e., control
stack
Activation records are pushed and popped as activation begins and end
respectively.
Locals are always bound to fresh storage in each activation, because a
new activation is onto a stack when a call is made.
Values of locals are deleted as activation ends.
The data structure can be created dynamically for stack allocation.
A register (stack pointer or SP) points to top of stack.
A register (frame pointer or FP) points to start of current A.R
Limitations
Values of locals cannot be retained once activation ends.
The memory addressing can be done using pointers and indexed
registers.
This type of allocation is slower than static allocation
Heap Allocation
Storage can be allocated and deallocated in any order.
If the values of non-local variables must be retained even after the
activation record then such a retaining is not possible by stack allocation.
It is used for retaining of local variables.
The heap allocation allocates the continuous block of memory when
required for storage of activation records.
This allocated memory can be deallocated when activation ends.
Free space can be further reused by heap manager.
It supports for recursion and data structures can be created at runtime
Limitations
It takes more time to compute.
Memory management is more complicated