CH4 1
CH4 1
1
Contents To Be Covered
Introduction
Storage Organization
2
Introduction
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.
Execution of a program is initially under the control of the operating system (OS)
Compilers create and manage a runtime environment in which the target programs are
executed.
To successfully implement the various issues of the source languages, like names, scope,
bindings, data types, procedures, parameters, etc.. And also the compiler must be cooperate
with OS.
The compiler must also work together with the other system software to support the issues on
the target machine.
3
Introduction
When a program is invoked: start to execute.
The OS allocates space for the program
The OS jumps to the entry point of the program (i.e., to the beginning of
4
Introduction
Issues handled by runtime environments are:
Allocation of storage for objects.
Access to variable and data.
Linkages between procedures
Parameter passing mechanisms
Interfaces to the OS, I/O and other programs.
5
Introduction
At run time, we need a system to map NAMES (in the source program) to
STORAGE on the machine.
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 is dynamic
allocation.
6
Procedures
A procedure definition is a declaration that associates an identifier with a
statement.
The identifier is the procedure name, and the statement is the procedure
body. For example
Example, the following is the definition of procedure named readarray :
procedure readarray; var i : integer;
begin
for i : = 1 to 9 do read(a[i]) end;
7
Procedures
When a procedure name appears within an executable statement,
the procedure is said to be called at that point.
8
Activation record
The state of activation is managed by flow control.
9
Activation record
Return Value Stores return values.(result of called procedure)
11
Activation
Every execution of a procedure is called an ACTIVATION.
12
Activation tree
We assume that the program control flows in a sequential manner and
when a procedure is called, its control is transferred to the called
procedure.
13
Activation tree
An activation tree is used to depict 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 for a is the parent of the node for b if and only if control flows
from activation a to b.
The node for a is to the left of the node for b if and only if the lifetime of a
occurs before the lifetime of b.
14
Activation tree
...
scanf(“%s”, username);
show_data(username);
...
return 0;
...
15
Control stacks
Control stack or runtime stack is used to keep track of the
live procedure activations. i.e. the procedures whose execution
have not been completed.
At any point in time, the control stack represents a path from the
root of the activation tree to one of the nodes.
16
Control stacks
This partial activation tree corresponds to control stack
(growing downward). (read array, partitioning and quick
Print f
sort procedure)
printf(“Enter Your Name: “); Print f
scanf(“%s”, username);
showdata
show_data(username);
{
Main function
printf(“Your name is %s”, username);
return 0;
17
Scope of declaration & binding of names
Syntactic construct that associates information with a name.
eg. Var i:integer; in pascal int I; in c++
Binding of names:
Environment: function that maps a name to a storage location
State: function that maps a storage location to the value held there.
environment State
18
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 complier, operating system and target machine.
The operating system maps the logical address into physical addresses, which are
usually spread throughout memory.
During the execution of a program, the same name in the source can denote
different data objects in the computer.
19
Storage Organization
Terminologies
Name storage space: The mapping of a name to a storage space is
Called environment.
Life time: The time between the first and last steps in a procedure. A
recursive procedure needs not to call itself directly.
20
General Run Time Storage
layout
21
General run time storage
layout
22
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.
23
STORAGE ALLOCATION
STRATEGIES
➢The different storage allocation strategies are :
➢ Static allocation - lays out storage for all data objects at
compile time
➢ Stack allocation - manages the run-time storage as a
stack.
➢ Heap allocation - allocates and deallocates storage as
needed at run time from a data area known as heap.
24
Static Allocation
In static allocation, names are bound to storage locations.
If memory is created at compile time then the memory will be created in static area
and only once.
Static allocation supports the dynamic data structure that means memory is created
only at compile time and deallocated after program completion.
In this the compiler can determine the amount of storage required by each data
object and therefore it becomes easy for a compiler to find the address of these data
in the activation record.
Since the bindings do not change at run-time, every time a process is activated, its
names are bound to the same storage locations.
25
Static Allocation
FORTRAN uses this kind of storage allocation strategies.
Advantages
It is easy to implement.
It allows type checking during compilation.
It eliminates the feasibility of running out of memory.
Disadvantages
It is incompatible with recursive subprograms.
It is not possible to use variables whose size has to be determined at run time.
26
Stack Allocation
The storage is organized as stack .This stack is also called
control stack.
The locals are stored in the each activation record. Hence locals
are bound to corresponding activation record on each fresh
activation.
27
Stack Allocation
The data structures can be created dynamically for stack
allocation.
28
Stack Allocation
On each execution of a procedure, An Activation Record is generated,
which contains information like Local data, actual parameter, return value,
return address of a procedure. The Activation Record for that procedure is
saved onto the stack.
.
29
Stack Allocation
Advantages
It supports recursion.
It creates a data structure for the data item dynamically.
Disadvantages
Memory Addressing can be done using pointers & index
Registers.
30
Heap Storage Allocation
Heap allocation is the most flexible allocation scheme.
31
Heap Storage Allocation
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.
32
Heap Storage Allocation
Advantages
A large block of storage can be partitioned into smaller
blocks at run time.
Disadvantages
It creates the problem of fragmentation.
33
Parameter Passing
The Parameter passing is a way of communication among the
variables.
The called procedure gets the value of the variable from the calling
procedure through a default mechanism
l-value:
The location of memory (address) where an expression is stored is
known as the l-value of that expression. It always appears at the left
hand side of an assignment operator.
35
Parameter Passing
For example:
day = 1;
week = day * 7;
month = 1;
year = month * 12;
From this example, we understand that constant values like 1, 7, 12, and
variables like day, week, month and year, all have r-values. Only variables have
l-values as they also represent the memory location assigned to them.
For example:
7 = x + y;
is an l-value error, as the constant 7 does not represent any memory location.
36
Parameter Passing
Pass by Value:
When the calling process passes the r-value of the actual parameter, and then it is transferred to the
activation record of the called process is known as the pass by value mechanism.
The formal parameter is responsible for holding the values passed by the calling procedure, and if
these values hold by formal parameter changes, then it should not affect the actual parameter.
Pass by Reference
When the l-value of the actual parameter is transferred to the activation record of the
called procedure and now the called procedure contains the address of the actual
parameter, and also the formal parameters refer to the same memory is known as pass by
reference.
Thus, If the value held by the formal parameter changes, there should be no change in the
actual parameter as they point to the same value.
37
Parameter Passing
Pass by Copy-restore
It is very much similar to pass by reference; the difference is that when the
procedure call ends, the changes are visible in actual parameters.
When there is the function call, the value of actual parameters is transferred to the
activation record of the called procedure.
Pass by Name:
When the name of the procedure is called and is replaced by its actual body, it is
called pass by name.
It substitutes the argument during procedure call for corresponding parameters in the
body, and thus, it can work on actual parameters, much similar to pass by reference.
38