Various statement in TAC
1. Assignment x= y op z
x= op y
x=y
2. Jump
Conditional
eg. If x relop y then goto L ;- relop-?????? relational operator
Unconditional
eg. Goto L
•If the condition “x relop y” gets satisfied, then-
•The control is sent directly to the location specified by label L.
•All the statements in between are skipped.
•Example:- -(a x b) + (c + d) – (a + b + c + d)
•Solution :- (1) T1 = a x b
(2) T2 = uminus T1
(3) T3 = c + d
(4) T4 = T2 + T3
(5) T5 = a + b
(6) T6 = T3 + T5
(7) T7 = T4 – T6
Uminus :- unary minus
•Example:- Write Three Address Code for the following
expression-
•If A < B then 1 else 0
•Solution-
Three Address Code for the given expression is-
(1) If (A < B) goto (4)
(2) T1 = 0
(3) goto (5)
(4) T1 = 1
(5)
•Write Three Address Code for the following expression-
•If A < B and C < D then t = 1 else t = 0
•Solution-
Three Address Code for the given expression is-
(1) If (A < B) goto (3)
(2) goto (4)
(3) If (C < D) goto (6)
(4) t = 0
(5) goto (7)
(6) t = 1
(7)
•Implementation of Three Address Code
•Quadruple
•Triples
1. Quadruple – It is a structure which consists of 4 fields namely op, arg1, arg2 and
result. op denotes the operator and arg1 and arg2 denotes the two operands and result is
used to store the result of the expression.
Advantage –
Easy to rearrange code for global optimization.
One can quickly access value of temporary variables using symbol table.
Disadvantage –
Contain lot of temporaries.
Temporary variable creation increases time and space complexity.
2. Triples-
In triples representation,
•References to the instructions are made.
•Temporary variables are not used.
Example ▪ a = b * minus c + b * minus c
Three address code t1 = minus c
t2 = b * t1
t3 = minus c
t4 = b * t3
t5 = t2 + t4
a = t5