Types of Three-address codes
Last Updated :
10 Sep, 2021
Three-address code is a sequence of statements of the general form A := B op C, where A, B, C are either programmer defined names, constants or compiler-generated temporary names; op stands for an operation which is applied on A, B.In simple words, a code having at most three addresses in a line is called three address codes.
Example:
(a+b)*(a+b+c)
The three address code for above expression is:
t1=a+b
t2=t1+c
t3=t1*t2
In compiler design the most popular intermediate code representation is Three-address code. It is globally accepted and is most widely used. There are a lot of three-address statements. All the complex three-address statements are generally a combination of simpler three-address statements.
These statements come under following seven categories and can be called as building block for three-address statements-
Statement |
Meaning |
X = Y op Z |
Binary Operation |
X= op Z |
Unary Operation |
X = Y |
Assignment |
if X(rel op)Y goto L |
Conditional Goto |
goto L |
Unconditional Goto |
A[i] = X Y= A[i] |
Array Indexing |
P = addr X Y = *P *P = Z |
Pointer Operations |
Now, by using the above statements let us convert some popular high level constructs in Three-address code.
1. While Statement –
while E do S
Three-address Code:
L:if(E==0) goto L1
S
goto L
L1:end
2. For Statement –
for(E1;E2;E3) do S
Three-address Code:
E1
L:if(E2) goto L1
goto L2
L1:S
E3
goto L
L2:end
3. Switch Statement –
switch(E){
case 1: S1
break;
case 2: S2
break;
default: S3
}
Three-address Code:
t=E
goto test
L1:S1
goto Last
L2:S2
goto Last
L3:S3
goto Last
test: if(E==1) goto L1
if(E==2) goto L2
goto L3
Last:end
Thus, we can see by using the seven statements we can replicate statements from higher level language into address codes.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...