7-Three-address-code
7-Three-address-code
Generation
Intermediate Code
▪ An language b/w source and target language
▪ Provides an intermediate level of abstraction
▪ More details than the source
▪ Fewer details than the target
Intermediate Code
SOURCELANGUAGE TARGETLANGUAGE
Benefits of intermediate code generation
Example : x + y * z
t1 = y * z
t2 = x +t1
t1 and t2 are compiler-generated temporary names
▪ Statements in this language are of the form:
x:=y op z
▪ where x, y and z are names, constants or compiler-generated
temporary variables, and ‘op’ stands for any operator
▪ ThreeAddress Code is a linearized representation of a syntax trees or a
DAG
T1 = b – c
T2 = a * t1
T3 = a +t2
T4 = t1 * d
T5 = t3 +t4
Data structures for three address codes
Quadruples
Has four fields: op, arg1, arg2 and result
Triples
Temporaries are not used and instead references to instructions are
made
Indirect triples
In addition to triples we use a list of pointers to triples
Example
Three address code
▪ b * minus c + b * minus c t1 = minus c
t2 = b * t1
t3 = minus c
t4 = b * t3
t5 = t2 + t4
a = t5
▪ Assignment statement x := y op z
▪ Assignment statement x := op y
▪ Copy statement x := y
▪ Unconditional jump goto L
▪ Conditional jump if x relop y gotoL
▪ Procedural call param x call p
return y
Assignment Statement
▪ Assignment statements can be in the following two forms
1. x:=op y
2. x:=y op z
First statement op is a unary operation. Essential unary operations
are unary minus, logical negation, shift operators and conversion
operators.
Second statement op is a binary arithmetic or logical operator.
Three-Address Statements