Run-Time Storage Management: 1. Implementation of Call Statement
Run-Time Storage Management: 1. Implementation of Call Statement
Run-Time Storage Management: 1. Implementation of Call Statement
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
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:
GOTO callee.code_area
• We assume that the run-time memory is divided into areas for:
1. Code
2. Static data
3. Stack
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,
#here + 20 literal are used to return address of the instruction following GOTO.
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.
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.
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,
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.
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.
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.
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
B, dead-code elimination
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:
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.
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.