You are on page 1of 6

Chapter-7 : Run time Environments

7.1 Storage organization

7.2 Storage allocation strategies

7.3 Parameter Passing.

Run time Environments:

A Run-time environment in compiler design deals variety of issues such as

a. Managing the Processor Stack.


b. Allocation of memory for various variables used in the source program.
c. The Mechanism to pass parameters.

7.1 Storage organization:

Store organization run-time memory in a computer is organization as show below.

The above figure has consists of code, static data, stack and heap.

a. The code and static data are belongs to fixed objects.


b. The stack and heap are belongs to dynamic objects.
c. The stack is used to store procedure activations.
d. The heap storage is allocation and de-allocation at run-time from a data.

Activation Record:
a. A program is a sequence of instructions combined into a number of procedures.
b. Instructions in a procedure are executed sequentially.
c. The execution of a procedure is called its activation.
d. An activation record contains all the necessary information required to call a procedure.
e. An activation record may contain the following units (depending upon the source
language used).

Temporaries:

Temporaries Stores temporary and intermediate values of an expression.

Local data:

Local Data Stores local data of the called procedure.

Machine Status:

Machine Status Stores machine status such as Registers, Program Counter etc., before the
procedure is called.

Access link:

Access Link Stores the information of data which is outside the local scope.

Control link:

Control Link Stores the address of activation record of the caller procedure.

Actual Parameters:
Actual Parameters Stores actual parameters, i.e., parameters which are used to send input to the
called procedure.

Returned value:

Return Value Stores return values

7.2 Storage allocation strategies:

The various storage allocation strategies to allocate storage in different data areas of memory are:

a. Static allocation
b. Stack allocation
c. Heap allocation.

Static allocation:
i. Storage is allocated for all data objects at compile-time.
ii. In static allocation, names are bound to storage as the program is compiled. Since the
bindings do not change at run time, every time a procedure is activated, its names are
bound to the same storage locations.
iii. This property allows the values of local names to be retained across activations of a
procedure. That is, when control returns to a procedure, the values of the locals are the
same as they were when control left the last time.

Stack allocation:

a. The storage is managed as a stack.


b. Stack is a one of the data structure.
c. It can be operated has LIFO (Last-in First Out) (or) FILO (First-in Last Out).
Example: plates.
d. It uses two kinds of operations.
i) Push operation ii) Pop operation.
Push operation can used for elements are inserted into stack.
Example:
public void push(int a) { // code }
Pop operation can used for removing elements from the stack.

Example:

int pop() //return integer value


{
//code
}
Storage is organized as a stack, and activation records are pushed and popped as activations
begin and end, respectively.

Heap Allocation:

a. The storage is allocated and de allocated at run-time from data are known as heap.
b. Heap allocation parcels out pieces of contiguous storage, as needed for activation records
or other objects.
c. Pieces may be de allocated in any order, so over time the heap will consist of alternate
areas that are free and in use.
7.3 Parameter Passing.

In general, there are two ways that a computer language can pass an argument to a subroutine.

a) Call-by Value
b) Call-by Reference.

a) Call-by Value:

This approach copies the value of an argument into the formal parameter of the subroutine.
Therefore, changes made to the parameter of the subroutine have no effect on the argument.

Example:

// Primitive types are passed by value.

class Test {

void meth(int i, int j) {

i *= 2;

j /= 2;

} }

class CallByValue {

public static void main(String args[]) {


Test ob = new Test();

int a = 15, b = 20;

System.out.println("a and b before call: " + a + " " + b);

ob.meth(a, b);

System.out.println("a and b after call: " + a + " " + b);

The output from this program is shown here:

a and b before call: 15 20

a and b after call: 15 20

b) Call-by Reference:

In this approach, a reference to an argument (not the value of the argument) is passed to
the parameter. Inside the subroutine, this reference is used to access the actual argument
specified in the call.
This means that changes made to the parameter will affect the argument used to call the
subroutine.

Example:

// Objects are passed by reference.

class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
// pass an object
void meth(Test o) {
o.a *= 2;
o.b /= 2;
}
}
class CallByRef {
public static void main(String args[ ]) {
Test ob = new Test(15, 20);
System.out.println("ob.a and ob.b before call: " + ob.a + " " + ob.b);
ob.meth(ob);
System.out.println("ob.a and ob.b after call: " +ob.a + " " + ob.b);
}
}

This program generates the following output:

ob.a and ob.b before call: 15 20

ob.a and ob.b after call: 30 10

You might also like