006chapter 6 - Intermediate Code Generation
006chapter 6 - Intermediate Code Generation
Introduction
then why at all we need to translate the source code into an intermediate code which is then translated to
its target code?
It becomes easier to apply the source code modifications to improve code performance
by applying code optimization techniques on the intermediate code.
What is Intermediate Code generation?
It is used to translate the source code into the machine code
i.e. Intermediate code is the interface between front end and back end in a compiler
What is Intermediate Code generation? …
ICG receives input from its predecessors phase and semantic analyser phase.
AST are more compact than a parse tree and can be easily used by compiler
A Directed Acyclic Graph (called a DAG) for an expression identifies the common
This way we can easily show the common sub-expressions and then use that
knowledge during code generation.
a DAG has leaves corresponding to atomic operands and interior codes
corresponding to operators.
a node N in a DAG has more than one parent if N represents a common sub-
expression.
Example #1: DAG
Shows the DAG for the expression.
ii. Polish Notation( Infix, prefix and postfix)
• Three types of polish notation:
• Postfix-notation:
– In postfix notation the operands occurs first and then operators are arranged. E.g
ab+
Example #1: Postfix-Notation
• Consider the following string: a+b*c+d*e^f
operators
Example #1: Postfix-Notation- Solution
iii. Three address code
Common intermediate representation of a program.
Where a,b,c are operands that can be names or constants and op stands for any
operator. Example #1: a=b+c*d
Three address code for above expressions is:
t1=c*d
t2=b+t1
a=t2 where t1 and t2 are the temporary names generated by compiler
Note: In three-address code, there is at most one operator on the right side of an
instruction.( two for operands and one for result)
Three Address Code: Example #2
x = y op z
There are three types of representation used for three address code:
i. Quadruples
ii. Triples
Note: Instructions with unary operators like x = minus y or x = y do not use arg2.
A triple has only three fields, which we call op, arg1, and arg2
i.e. Temporaries are not used and instead references to instructions are made.
Example: Triples
The syntax tree and triples in Fig.: correspond to the three-address code and
quadruples in Fig.: