Intermediate Code
Generation
Importance of intermediate code
Intermediate code Representation
• Postfix notation
• Syntax Tree
• Directed Acyclic Graph(DAG)
• Three address code
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.
3. Array Assignment x= y[i]
y[i]=x
4. Pointer address assignment x=&y
x=*y
Example:- (a*b)+(c*d)
Three address code:- t1=a*b
t2=c*d
t3=t1+t2
•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
Quadruple Triples

Module 6 Intermediate Code Generation.pdf

  • 1.
  • 3.
  • 8.
    Intermediate code Representation •Postfix notation • Syntax Tree • Directed Acyclic Graph(DAG) • Three address code
  • 11.
    Various statement inTAC 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. 3. Array Assignment x= y[i] y[i]=x 4. Pointer address assignment x=&y x=*y
  • 12.
    Example:- (a*b)+(c*d) Three addresscode:- t1=a*b t2=c*d t3=t1+t2
  • 14.
    •Example:- -(a xb) + (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
  • 15.
    •Example:- Write ThreeAddress 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)
  • 16.
    •Write Three AddressCode 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)
  • 17.
    •Implementation of ThreeAddress 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.
  • 18.
    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 Quadruple Triples