Professional Documents
Culture Documents
3 - 2 - Modified - INTERMEDIATE CODE SESSION
3 - 2 - Modified - INTERMEDIATE CODE SESSION
3 - 2 - Modified - INTERMEDIATE CODE SESSION
Outline
• Importance of Intermediate code generation
• Intermediate code Representations
• Types of Three Address code Statements
• Implementation of Three-address code
• Comparison of TAC Representions
Intermediate Code Generator
Advantages
• Closer to target machine – Easy to generate
code.
t1 := 2
t2 := t1 * n
t3 := t2 + k
i := t3
L1: if i = 0 goto L2
t4 := i - k
i := t4
goto L1
L2:
Intermediate Representations
• High-level intermediate representation:
- Expresses the high-level structure of a program.
Features :
- Closer to the source language program.
- Easy to generate from the input program.
-Code optimization may not be straight forward.
Intermediate Representations..
• Low-level intermediate representation:
- Expresses the low-level structure of a program.
Features :
- Closer to the target machine.
- Easy to generate the final code.
- Requires a good amount of effort to generate
intermediate code from the input program.
Intermediate Representations..
• High Level Representation
-- Abstract Syntax Tree(AST)
-- Directed Acyclic Graph(DAG)
-- P-Code(Postfix)
c c
• Polish notation
a b c - * b c - * + :=
Abstract Syntax Trees versus DAGs
a := b * -c + b * -c
:= :=
a + a +
* * *
c c c
Tree DAG
Two representations of the syntax tree
a := b * -c + b * -c
Three-Address Code
• Sequence of instructions of the form
x = y op z
• Only one operator is permitted on the right-
hand side.
x := y op z
where x, y, z are names, constants, or temporaries
a := b * -c + b * -c
x+y*z
t1 := -c
t1 := y * z t2 := b * t1
t2 := x + t1 t3 := -c
t4 := b * t3
t5 := t2 + t4
a := t5
Three-Address Code..
a := b * -c + b * -c
t1 := - c t1 := - c
t2 := b * t1 t2 := b * t1
t3 := - c t5 := t2 + t2
t4 := b * t3 a := t5
t5 := t2 + t4
a := t5
• Assignment statement x := y op z
• Assignment Instructions x := op y
• Copy statement x := y
• Unconditional jump goto L
• Conditional jump if x relop y goto L
• Procedural call param x
call p
return y
Types of Three-Address Code Statements..
a) Quadruple
b) Triple
c) Indirect triple
Implementation of Three-Address Code
a) Quadruples t1 := - c
t2 := b * t1
t3 := - c
Operator Argument1 Argument2 Result t4 := b * t3
t5 := t2 + t4
a := t5
For statement a = -c * b + -c * b
op arg1 arg2 result
(0) - c t1
(1) * b t1 t2
(2) - c t3
(3) * b t3 t4
(4) + t2 t4 t5
(5) := t5 a
Example: Quadraples
u:= (d + h) / n+ (-c) * k
Quadraples
Three Address Code: # Op Arg1 Arg2 Res
t1 = d + h (0) + d h t1
t2 = t1 / n (1) / t1 n t2
t3 = -c (2) - c t3
t4 = t3 * k (3) * t3 k t4
t5 = t2 + t4 (4) + t2 t4 t5
u = t5
(5) := t5 u
Quadruples
– direct access of the location for temporaries
– easier for optimization
• Indirect Triples
– easier for optimization
– space efficiency
Pro: temporaries are implicit & easier to rearrange code