Sie sind auf Seite 1von 13

Intermediate code generation

Intermediate Code Generation

Intermediate codes are machine independent codes, but they are close to machine instructions.

The given program in a source language is converted to an equivalent program in an intermediate language by the intermediate code generator.
Intermediate language can be many different languages, and the designer of the compiler decides this intermediate language.

syntax trees can be used as an intermediate language. postfix notation can be used as an intermediate language. three-address code (Quadruples) can be used as an intermediate language


Three Address Code

Statements of general form x:=y op z

x,y,z- names , constants or compiler-generated temporaries

No built-up arithmetic expressions are allowed. As a result, x:=y + z * w should be represented as t1:=z * w t1 , t2 compiler generated temporary names t2:=y + t1 x:=t2 Observe that given the syntax-tree or the dag of the graphical representation we can easily derive a three address code for assignments as above. In fact three-address code is a linearization of the syntax tree. Three-address code is useful: related to machine-language/ simple/ optimizable.

3-address codes are

Syntax tree


t1:=- c t2:=b * t1 t3:=- c t4:=b * t3 t5:=t2 + t4 a:=t5

t1:=- c t2:=b * t1 t5:=t2 + t2 a:=t5

Types of Three-Address Statements.

Assignment Statement: Assignment Statement: Copy Statement: Unconditional Jump: Conditional Jump: Stack Operations: More Advanced Procedure: param x1 param x2 param xn call p,n
Index Assignments:
Generated as part of call of proc. p(x1,x2,,xn)

x:=y op z x:=op z x:=z goto L if x relop y goto L Push/pop

x:=y[ i ] x[ i ]:=y

Address and Pointer Assignments:

x:=&y x:=*y *x:=y

Implementation of 3 address code

Quadruples Triples Indirect triples


A quadruple is a record structure with four fields: op, arg1, arg2, and result

The op field contains an internal code for an operator Statements with unary operators do not use arg2 Operators like param use neither arg2 nor result The target label for conditional and unconditional jumps are in result

The contents of fields arg1, arg2, and result are typically pointers to symbol table entries

Implementations of 3-address statements


Quadruples t1:=- c t2:=b * t1 t3:=- c t4:=b * t3 t5:=t2 + t4 a:=t5

(0) (1) (2) (3) uminus * uminus *

c b c b







(4) (5)

+ :=

t2 t5


t5 a


Triples refer to a temporary value by the position of the statement that computes it

Statements can be represented by a record with only three fields: op, arg1, and arg2 Avoids the need to enter temporary names into the symbol table Pointer into symbol table (for programmer defined names) Pointer into triple structure (for temporaries)

Contents of arg1 and arg2:

Implementations of 3-address statements, II a:=b*-c+b*-c:

Triples t1:=- c t2:=b * t1 t3:=- c t4:=b * t3 t5:=t2 + t4 a:=t5

(0) (1) (2) (3) uminus * uminus *

c b c b




(4) (5)

+ assign

(1) a

(3) (4)

Indirect Triples

Consist of a listing of pointer to triples, rather than a listing of triples themselves. With indirect triples an optimizing compiler can move an instruction by reordering the instructions list without affecting the triples themselves.

Implementations of 3-address statements, III

t1:=- c t2:=b * t1 t3:=- c t4:=b * t3 t5:=t2 + t4 a:=t5

stmt op (14) uminus (15) *

arg1 c b


(0) (1)

(14) (15)


(3) (4) (5)

(17) (18) (19)

(16) uminus
(17) * (18) + (19) assign

b (15) a (16) (17) (18)