Intermediate Code Generation
Intermediate Code Generation
❏ Intermediate code eliminates the need of a new full compiler for every unique machine
by keeping the analysis portion same for all the compilers.
❏ The second part of compiler, synthesis,
is changed according to the target machine.
❏ It becomes easier to apply the source
code modifications to improve code
performance by applying code
optimization techniques on the intermediate code.
❏ The commonly used intermediate code representations are:
1. Postfix notation
2. Syntax Tree
3. Three Address code
1. Postfix notation
❏ The ordinary(infix) way of writing the sum of a and b is the operator in
the middle: a+b
❏ The postfix notation: ab+
❏ In general,if e1 and e2 are any postfix expressions and X is any binary
operator, then the result of applying X to the values denoted by e1 and
e2 is indicated in postfix notation by e1e2X.
❏ No parentheses are needed in postfix notation.
Example:
1. (a-b)*(c+d)+(a-b) -> ab-cd+*ab-+
2. If e then x else y -> infix: e?x:y
Postfix: exy?
SDD for infix-postfix translation:
x = (a+b*c) / (a-b*c)
Direct Acyclic Graph for Expressions:
❏ The DAG is used to represent the structure of basic blocks, to visualize the flow between basic blocks, and to
provide optimization techniques in the basic block.
❏ DAG can be understood here:
❏ Leaf nodes represent identifiers, names and constants.
❏ Interior nodes represent operators also represent the results of expressions or the identifiers where the
values to be stored or assigned.
❏ A DAG for an expression identifies the common sub-expressions (more than once) in the
expression.
❏ Example: