Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Run-Time Storage Management: 1. Implementation of Call Statement

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

RUN-TIME STORAGE MANAGEMENT

The information which required during an execution of a procedure is kept in a block of


storage called an activation record. The activation record includes storage for names
local to the procedure.

Since run-time allocation and deallocation of activation records occurs as part of the procedure call and
return sequences, we focus on the following three-address statements:

1. call,

2. return,

3. halt and

4. action, a placeholder for other statements.

Static Allocation
Consider the code needed to implement static allocation. A call statement in the intermediate code is
implemented by a sequence of two target-machine instructions. A MOV instruction saves the return
address, and a GOTO transfers control to the target code for the called procedure:

MOV #here + 20, callee.static_area

GOTO callee.code_area

•   We assume that the run-time memory is divided into areas for:
 
1.  Code
2.  Static data
3.  Stack

1. Implementation of call statement:


The following code is needed to implement static allocation:

1. MOV #here + 20, callee.static_area     /*it saves return address*/</p>  
2. GOTO callee.code_area     /* It transfers control to the target code for the called 
procedure*/  
1. MOV #here + 20, callee.static_area     /*it saves return address*/</p>  
2. GOTO callee.code_area     /* It transfers control to the target code for the called 
procedure*/  

Where,

callee.static_area shows the address of the activation record.


callee.code_area shows the address of the first instruction for called procedure.

#here + 20 literal are used to return address of the instruction following GOTO.

2. Implementation of return statement:


The following code is needed to implement return from procedure callee:

1. GOTO * callee.static_area  
1. GOTO * callee.static_area  

It is used to transfer the control to the address that is saved at the beginning of the
activation record.

3. Implementation of action statement:


The ACTION instruction is used to implement action statement.

4. Implementation of halt statement:


The HALT statement is the final instruction that is used to return the control to the
operating system.

Stack Allocation
Static allocation can become stack allocation by using relative addresses for storage in activation records.
The position of the record for an activation or a procedure is not known until run time. In stack
allocation, this position is usually stored in a register, so words in the activation record can be accessed
as offsets from the value in this register. The indexed address mode of our target machine is convenient
for this purpose.

Using the relative address, static allocation can become stack allocation for storage in
activation records.

In stack allocation, register is used to store the position of activation record so words in
activation records can be accessed as offsets from the value in this register.

The following code is needed to implement stack allocation:

1. Initialization of stack:

1. MOV #stackstart , SP    /*initializes stack*/  
2. HALT                             /*terminate execution*/  
1. MOV #stackstart , SP    /*initializes stack*/  
2. HALT                             /*terminate execution*/  
2. Implementation of Call statement:

1. ADD #caller.recordsize, SP/* increment stack pointer */   
2. MOV #here + 16, *SP                  /*Save return address */   
3. GOTO callee.code_area  
1. ADD #caller.recordsize, SP/* increment stack pointer */   
2. MOV #here + 16, *SP                  /*Save return address */   
3. GOTO callee.code_area  

Where,

caller.recordsize is the size of the activation record

#here + 16 is the address of the instruction following the GOTO

3. Implementation of Return statement:

1. GOTO *0 ( SP ) /*return to the caller */   
2. SUB #caller.recordsize, SP           /*decrement SP and restore to previous value 
*/  
1. GOTO *0 ( SP ) /*return to the caller */   
2. SUB #caller.recordsize, SP           /*decrement SP and restore to previous value 
*/  
BASIC BLOCKS AND FLOW GRAPHS

A graph representation of three-address statements called a flow graph, is useful for undersranding
code-generation algorithms, even if the graph is not explicitly constructed by a code-generation
algorithm. Nodes in the flow graph represent computations, and the edges represent the flow of control.
ln Chapter 10, we use the flow graph of a program extensively as a vehicle to collect information about
the intermediate program. Some register-assignment algorithms use flow graphs to find the inner loops
where a program is expected to spend most of its time.

Basic Blocks

A basic block is a sequence of consecutive statements in which now of control enters at the beginning
and leaves al the end without halt or possibility of branching except at the end. The following sequence
of three-address statements forms a basic block:

t1 ; = a * a

t2 ; a + b

t3 ; 2 * t2

t4 ; t4, + t5
A three_address statement x : = y + z is said to define x and to use (or reference) y and z. A name in a
basic block is said to be live at a given point if its value is used after that point in the program, perhaps in
another bask block. The following algorithm can be used to partition a sequence of three_address
statements into basic blocks.

Algorithm 9.1. Partition into basic blocks.

Input. A sequence of three_address statements.

Output. A list of basic blocks with each three_address statement in exactly one block.

Method.

1. We first determine the set of leaders, the first statements of basic blocks. The rules we use are the
following.

i) The first statement is a leader.

ii) Any statement that is the target of a conditional or unconditional goto is a leader.

iii) Any statement that immediately follows a goto or conditional goto slatement is a leader.

2. For each leader, its basic block consists or the leader and all statements up to but not including the
next leader or the end of the program.

Transformations on Basic Blocks:


A number of transformations can be applied to a basic block without expressions
computed by the block. Two important classes of transformation are :
 
•   Structure-preserving transformations
•   Algebraic transformations

A basic block computes a set of expressions. These expressions are the values of the names live on exit
from the block. Two basic blocks are said to be equivalent if they compute the same set of expressions.

There are two important classes of local transformations that can be applied to basic blocks; these are
the structure preserving transformations and the algebraic transformations.

Structure-Preserving Transformations

The primary structure-preserving tansformations on basic blocks are:

A, common subexpression elimination

B, dead-code elimination

C, renaming of temporary variables

D, interchange of two independent adjacent statements

 
1. Structure preserving transformations:
a) Common subexpression elimination:
a : = b + c    - -  >   a : = b + c
b : = a - d    - -  >   b : = a - d
c : = b + c   - -  >    c : = b + c
d : = a - d    - -  >   d : = b
 
Since the second and fourth expressions compute the same expression, the basic
block can be transformed as above.
 
b) Dead-code elimination:
 
Suppose x is dead, that is, never subsequently used, at the point where the
statement x : = y + z appears in a basic block. Then this statement may be safely
removed without changing the value of the basic block.
 
c) Renaming temporary variables:
 
A statement t : = b + c ( t is a temporary ) can be changed to u : = b + c (u is a new
temporary) and all uses of this instance of t can be changed to u without changing
the value of the basic block. Such a block is called a normal-form block.
 
d) Interchange of statements:
 
Suppose a block has the following two adjacent statements:
t1 : = b + c
t2 : = x + y
 
We can interchange the two statements without affecting the value of the block if
and only if neither x nor y is t1 and neither b nor c is t2.
 
2. Algebraic transformations:
 
Algebraic transformations can be used to change the set of expressions computed
by a basic block into an algebraically equivalent set.
Examples:
 
i) x : = x + 0 or x : = x * 1 can be eliminated from a basic block without changing
the set of expressions it computes.
ii) The exponential statement x : = y * * 2 can be replaced by x : = y * y.
Flow Graphs
• Flow graph is a directed graph containing the flow-of-control information for the
set of basic blocks making up a program.
The nodes of the flow graph are basic blocks. It has a distinguished initial node.
•   E.g.: Flow graph for the vector dot product is given as follows:

Fig. 4.2 Flow graph for program


 
•     B1 is the initial node. B2 immediately follows B1, so there is an edge from B1 to
B2. The target of jump from last statement of B1 is the first statement B2, so there
is an edge from B1 (last statement) to B2 (first statement).
 
•     B1 is the predecessor of B2, and B2 is a successor of B1.
 
Flow Graph
Flow graph is a directed graph. It contains the flow of control information for the set of
basic block.

A control flow graph is used to depict that how the program control is being parsed
among the blocks. It is useful in the loop optimization.

The nodes of the flow graph are basic blocks. It has a distinguished initial node.

Flow graph for the vector dot product is given as follows:


o Block B1 is the initial node. Block B2 immediately follows B1, so from B2 to B1
there is an edge.
o The target of jump from last statement of B1 is the first statement B2, so from
B1 to B2 there is an edge.
o B2 is a successor of B1 and B1 is the predecessor of B2.

Loops
• A loop is a collection of nodes in a flow graph such that
 
1.   All nodes in the collection are strongly connected.
2.   The collection of nodes has a unique entry.
 
• A loop that contains no other loops is called an inner loop.

You might also like