2. Three Address Code
Three-address code (often abbreviated to TAC or 3AC) is an intermediate
code used by compilers to aid in the implementation of code-improving
transformations.
Three Address Code is sequence of statements, typically of a general form A:= B
op C, where A,B , and C are either programmer-Defined names, constants or
compiler-generated temporary names; op stands for any operator, such as a fixed- or
floating point arithmetic operator, or a logical operator on Boolean-valued data.
The reason for the name “Three –address code” is that each statements usually
contains three addresses, two for the operands and one for result.
Example: Write a Three Address Code for following expression: “ X+Y*Z ”
Solution: T1 := Y * Z
T2 := X + T1
Where T1 and T2 are compiler-generated temporary names.
3. TAC Example
Three Address Code Computing Dot product
BEGIN 1) PROD :=0
2) I:=1
PROD := 0;
I := 1; 3) T1 := 4*I
4) T2 := addr(A) – 4
DO
BEGIN 5) T3 := T2[T1] /*
Compute A[I] */
PROD :=PROD +
A[I] *B[I]; 6) T4 := addr(B) - 4
I :=I+1 7) T5 := T4[T1] /* Compute
END B[I] */
WHILE I ≤ 20 8) T6 := T3 * T5
END 9) PROD := PROD + T6
10) I := I+1
11) If I ≤ 20 goto (3)
4. Algorithm: Partition into Basic Blocks.
Basic Blocks
Input: A sequence of Three address statements.
Our first step is to break the code Output: A list of basic blocks with each three
of TAC Example in to basic address statement in exactly one block.
blocks, that is sequence of Method:
consecutive statements which may be 1) We first determine the set of leaders, the first
statements of basic blocks the rules we use
entered only at the beginning, and are as follow”
when entered are executed in i) The first statement is a leader
sequence without halt or possibility of ii) Any statement which is the target of a
branch(except at the end of the basic conditional or unconditional goto is a
block). leader
iii) Any statement which immediately
A useful algorithms for follows a conditional goto is a leader
partitioning three address statements 2) For each leader construct its basic
block, which consists of the leader and all the
into basic blocks is the following statements up to but not including the next
leader or the end of the program .Any
statements not placed in a block can ever be
executed and may now be removed, if
desired.
5. Basic Block
A basic block is a sequence of consecutive statements in which flow of control
enters at the beginning and leaves at the end without halt or possibility of
branching except at the end.
The code in a basic block has:
one entry point, meaning no code within it is the destination of a jump
instruction anywhere in the program;
one exit point, meaning only the last instruction can cause the program to
begin executing code in a different basic block.
A list of basic blocks with each three-address statement in exactly one block.
Step 1. Identify the leaders in the code. Leaders are instructions which come
under any of the following 3 categories :
The first instruction is a leader.
The target of a conditional or an unconditional goto/jump instruction is a
leader.
The instruction that immediately follows a conditional or an unconditional
goto/jump instruction is a leader.
6. Step 2. Starting from a leader, the set of all following instructions until and not
including the next leader is the basic block corresponding to the starting leader
Thus every basic block has a leader.
7. BASIC BLOCKS
A basic block is a sequence of consecutive
statements in which flow of control enters at the beginning
and leaves at 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 := t1+t3
t5 := b*b
t6 := t4+t5
8. A three-address statement x := y+z is said to define x and to use y or z. A
name in a basic block is said to live at a given point if its value is used after
that point in the program, perhaps in another basic block.
The following algorithm can be used to partition a sequence of three-address
statements into basic blocks.
9. Example 3: Consider the fragment of source code shown in fig. 7; it
computes the dot product of two vectors a and b of length 20. A list of three-
address statements performing this computation on our target machine is
shown in fig. 8.
begin
prod := 0;
i := 1;
do begin
prod := prod + a[i] * b[i];
i := i+1;
end
while i<= 20
end
fig 7: program to compute dot product
10. Let us apply Algorithm 1 to the three-address code in fig 8 to determine its basic
blocks. statement (1) is a leader by rule (I) and statement (3) is a leader by rule (II), since the
last statement can jump to it. By rule (III) the statement following (12) is a leader.
Therefore, statements (1) and (2) form a basic block. The remainder of the program beginning
with statement (3) forms a second basic block.
(1) prod := 0
(2) i := 1
(3) t1 := 4*i
(4) t2 := a [ t1 ]
(5) t3 := 4*i
(6) t4 :=b [ t3 ]
(7) t5 := t2*t4
(8) t6 := prod +t5
(9) prod := t6
(10) t7 := i+1
(11) i := t7
(12) if i<=20 goto (3)
fig 8. Three-address code computing dot product