2. Intermediate Code Generation
.
Three Address Code
2
Submitted To
Shah Md. Tanvir Siddiquee
Assistant Professor
Department of CSE
Daffodil International University
Submitted by:
Sourov Kumar Mondal
ID:173-15-1688
Section:PC-A
Department of CSE
Daffodil International University
2
3. Discussion Topics
• Intermediate Code Generation
• Three address code in Compiler
• Implementation of Three Address
Code
• following expression
• Conclusion
Three Address Code
3
4. Intermediate Code Generation
In the analysis-synthesis model of a compiler, the front end of a compiler translates a
source program into an independent intermediate code, then the back end of the
compiler uses this intermediate code to generate the target code (which can be
understood by the machine
Three Address Code 4
5. Three address code in Compiler
Three address code is a type of intermediate code which is easy to generate and
can be easily converted to machine code.It makes use of at most three addresses
and one operator to represent an expression and the value computed at each
instruction is stored in temporary variable generated by compiler. The compiler
decides the order of operation given by three address code.
Converting the expression x * (y - z) into three address code:
t1 = y - z
t2 = x * t1
Three Address Code 5
6. Implementation of Three Address Code
There are 3 representations of three address code:
1. Quadruple
2. Triples
3. Indirect Triples
Three Address Code 6
7. Three address code for the following expression:
((a+b)-((a+b)*(a-b)))+((a+b)*(a-b))
t1 = a + b
t2 = a - b
t3 = t1 * t2
t4 = t1 - t3
t5 = t1 * t2
t6 = t4 + t5
Three Address Code 7
8. For the following expression: ((a+b)-((a+b)*(a-b)))+((a+b)*(a-b))
It is structure with consist 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.
Three Address Code 8
Three Address Code:
t1 = a + b
t2 = a - b
t3 = t1 * t2
t4 = t1 - t3
t5 = t1 * t2
t6 = t4 + t5
Quadruples
# op arg1 arg2 result
0 + a b t1
1 - a b t2
2 * t1 t2 t3
3 - t1 t3 t4
4 * t1 t2 t5
5 + t4 t5 t6
9. For the following expression: ((a+b)-((a+b)*(a-b)))+((a+b)*(a-b))
This representation doesn’t make use of extra temporary variable to represent a single operation instead
when a reference to another triple’s value is needed, a pointer to that triple is used. So, it consist of only
three fields namely op, arg1 and arg2.
Three Address Code 9
Quadruples Triples
# op arg1 arg2 result
0 + a b t1
1 - a b t2
2 * t1 t2 t3
3 - t1 t3 t4
4 * t1 t2 t5
5 + t4 t5 t6
# op arg1 arg2
0 + a b
1 - a b
2 * (0) (1)
3 - (0) (2)
4 * (0) (1)
5 + (3) (4)
10. For the following expression: ((a+b)-((a+b)*(a-b)))+((a+b)*(a-b))
This representation makes use of pointer to the listing of all references to computations which is made
separately and stored. Its similar in utility as compared to quadruple representation but requires less
space than it. Temporaries are implicit and easier to rearrange code.
Three Address Code 10
Triples Indirect Triples
# op arg1 arg2
0 + a b
1 - a b
2 * (0) (1)
3 - (0) (2)
4 * (0) (1)
5 + (3) (4)
• .
# op arg1 arg2
0 + a b
1 - a b
2 * (0) (1)
3 - (0) (2)
4 * (0) (1)
5 + (3) (4)
# Statement
0 21
1 22
2 23
3 24
4 25
5 26
11. Directed Acyclic Graph (DAG)
For the following expression: ((a+b)-((a+b)*(a-b)))+((a+b)*(a-b))
Three Address Code 11
12. Conclusion
Since three-address code is used as an intermediate language within
compilers, the operands will most likely not be concrete memory
addresses or processor registers, but rather symbolic addresses that
will be translated into actual addresses during register allocation.
Three Address Code 12