EXAMPLE 1
Generate the three-address code for the following C program:
main()
{ int i = 1;
int a[10];
while(i <= 10)
a[i] = ;
}
The three-address code for the above C program is:
-
i = 1
-
if i <= 10 goto(4)
-
goto(8)
-
t1 = i * width
-
t2 = addr(a) − width
-
t2[t1] = 0
-
goto(2)
where width is the number of bytes required for each element.
EXAMPLE 2
Generate the three-address code for the following program fragment:
while (A < C and B > D) do
if A = 1 then C = C+1
else
while A <= D do
A = A + 3
The three-address code is:
-
if a < c goto(3)
-
goto(16)
-
if b >d goto(5)
-
goto(16)
-
if a = 1 goto(7)
-
goto(10)
-
t1 = c+1
-
c = t1
-
goto(1)
-
if a <= d goto
-
goto(1)
-
t2 = a+3
-
a = t2
-
goto(10)
-
goto(1)
EXAMPLE 3
Generate the three-address code for the following program fragment, where a and b are arrays of size 20 � 20, and there are four bytes per word.
begin
add = 0;
i = 1;
j = 1;
do
begin
add = add + a[i,j] * b[j,i]
i = i + 1;
j = j + 1;
end
while i <= 20 and j <= 20;
end
The three-address code is:
-
add = 0
-
i = 1
-
j = 1
-
t1 = i * 20
-
t1 = t1 + j
-
t1 = t1 * 4
-
t2 = addr(a) − 84
-
t3 = t2[t1]
-
t4 = j * 20
-
t4 = t4 + i
-
t4 = t4 * 4
-
t5 = addr(b) − 84
-
t6 = t5[t4]
-
t7 = t3 * t6
-
t7 = add + t7
-
t8 = i + 1
-
i = t8
-
t9 = j + 1
-
j = t9
-
if i <= 20 goto(22)
-
goto NEXT
-
if j <= 20 goto(4)
-
goto NEXT
EXAMPLE 4
Consider the program fragment:
sum = 0
for(i = 1; i<= 20; i++)
sum = sum + a[i] + b[i];
and generate the three-address code for it. There are four bytes per word.
The three address code is:
-
sum = 0
-
i = 1
-
if i <= 20 goto(8)
-
goto NEXT
-
t1 = i+1
-
i = t1
-
goto(3)
-
t2 = i * 4
-
t3 = addr(a) − 4
-
t4 = t3[t2]
-
t5 = i * 4
-
t6 = addr(b) − 4
-
t7 = t6[t5]
-
t8 = sum + t4
-
t8 = t8 + t7
-
sum = t8
-
goto(5)