3 - 2 - Modified - INTERMEDIATE CODE SESSION

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 29

Intermediate Code Generation

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.

• Machine independent – Easy to retarget the


compiler to various different target processors

• Allows a variety of optimizations.


Example
i := 2 * n + k
while i do
i := i - k

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)

• Low Level Representation


- Three-Address code
Abstract Syntax Tree(AST)
Directed Acyclic Graph(DAG)
Directed Acyclic Graph(DAG)..
The postfix notation
• If E is a variable or constant, then the postfix notation
for E is E itself
• If E is an expression of the form E1 op E2 then postfix
notation for E is E1 E2 op.
• the form (E), then the postfix notation for E is the same
as the postfix notation for E.
• For unary operation –E the postfix is E-
• postfix notation for 9 - (5 + 2) is 952+-
Intermediate Representations:
Example
a := b * - c + b * - c
• Syntax tree
:=
a +
* *
b - b -

c c

• Polish notation
a b c - * b c - * + :=
Abstract Syntax Trees versus DAGs
a := b * -c + b * -c

:= :=

a + a +

* * *

b uminus b uminus b uminus

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.

• Example: x = y * z + w * a may be translated


to three-address code as
t1 = y * z
t2 = w * a
x = t1 + t2
Three-Address Code..

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

Linearized representation Linearized representation


of a syntax tree of a syntax DAG
Types of Three-Address Code Statements

• 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..

• Indexed assignment x := y[i]


x[i] := y

• Address and pointer assignment


x := &y
x := *y
*x := y
Implementation of Three-Address Code
• Three Types of representation:

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

Pro: easy to rearrange code for global optimization


Con: lots of temporaries
Implementation of Three-Address Code..
b) Triples t1 := - c
t2 := b * t1
t3 := - c
Operator Argument1 Argument2
t4 := b * t3
t5 := t2 + t4
For statement a= (-c) * b + (-c) * b a := t5
op arg1 arg2
(0) - c
(1) * b (0)
(2) - c
(3) * b (2)
(4) + (1) (3)
(5) := a (4)
Example: Triples
u:= (d + h) / n+ (-c) * k

Three Address Code: Triples


t1 = d + h # Op Arg1 Arg2
t2 = t1 / n (0) + d h
t3 = -c (1) / (0) n
t4 = t3 * k (2) - c
t5 = t2 + t4 (3) * (2) k
u = t5 (4) + (1) (3)
(5) := u (4)

Pro: temporaries are implicit


Con: difficult to rearrange code
Implementation of Three-Address Code..
t1 := - c
t2 := b * t1
c) Indirect Triples t3
t4
:=
:=
- c
b * t3
For a := -c * b + -c * b t5 := t2 + t4
a := t5

# Stmt # Op Arg1 Arg2


(0) (14) (14) minus c
(1) (15) (15) * (14) b
(2) (16) (16) minus c
(3) (17) (17) * (16) b
(4) (18) (18) + (15) (17)
(5) (19) (19) := a (18)
Example: Indirect Triples
u:= (d+h) / n + (-c) * k

# Stmt # Op Arg1 Arg2


(0) (14) (14) plus d h
(1) (15) (15) / (14) n
(2) (16) (16) minus c
(3) (17) (17) * (16) k
(4) (18) (18) + (15) (17)
(5) (19) (19) := u (18)

Program Triple container

Pro: temporaries are implicit & easier to rearrange code


Comparison of TAC representations

Quadruples
– direct access of the location for temporaries
– easier for optimization

Pro: easy to rearrange code for global optimization


Cons: lots of temporaries
Comparison..
• Triples
– space efficiency
Pro: temporaries are implicit
Cons: difficult to rearrange code

• Indirect Triples
– easier for optimization
– space efficiency
Pro: temporaries are implicit & easier to rearrange code

You might also like