Chapter 6 Intermediate Code Generation
Chapter 6 Intermediate Code Generation
Design
• Introduction
Graph(DAG),
– Postfix notation,
– Three-address code,
– The output of the Parser and the input to the Code Generator.
be retargeted.
were :
– Syntax Tree
– Direct Acyclic Graph (DAG)
– Postfix Notation
– 3 Address Code
Graphical Representation
• Includes-
– Syntax Tree
In addition, we use:
• Static Single Assignment (SSA)
– Direct Acyclic Graph(DAG)
• Control Flow Graph (CFG)
Syntax Tree or Abstract Syntax Tree(AST)
Syntax Tree depicts the hierarchical structure of a
source program.
Ex
Syntax Tree / Abstract Syntax Tree(AST)
as follows.
Parse Tree VS Syntax Tree
Parse Tree Syntax Tree
• Example -
a–4+c
• The tree is constructed bottom up
P1 = mkleaf(id,entry a)
P2 = mkleaf(num, 4)
P3 = mknode(-, P1, P2)
P4 = mkleaf(id,entry c)
P5 = mknode(+, P3, P4)
Syntax directed definition
• Syntax trees for assignment statements are produced
by the syntax-directed definition.
• Non terminal S generates an assignment statement.
• The two binary operators + and * are examples of the
full operator set in a typical language.
• Operator associates and precedencies are the usual
ones, even though they have not been put into the
grammar.
• This definition constructs the tree from the input a:=b*
-c + b* -c
Syntax directed definition
This definition constructs the tree from the input
a:=b* -c + b* -c
Syntax directed definition
Two representations of the syntax tree are as
follows.
Direct Acyclic Graph (DAG)
DAG : Direct Acyclic Graph
Variant of Syntax tree with a unique node for
each value. [contains no cycles]
Helps optimize the code by identifying common
sub expressions in syntax tree.
SDD used to generate Syntax tree will be used to
construct DAG too, with a simple check:
a = b op c where,
– a, b, c are the operands that can be names, constants
or compiler generated temporaries.
– op represents operator, such as fixed or floating point
arithmetic operator or a logical operator on Boolean
valued data.
Three-address Code
• Linearized representation of syntax tree or DAG.
• At most one operator on RHS of an instruction.
• Each instruction can have up to three addresses.
• Address can either be a
– name(identifier)
– constant(number)
– Temporary (holds intermediate result)
• Thus a source language expression like x + y * z
might be translated into a sequence
Three-address Code
t1 = i * 12
t2=j*4
t 3 = t 1+ t 2
t 4 = a [ t 3 ] or a + t3
t s = C + t4
Implementation of Three-Address Statements
– Quadruples
– Triples
– Indirect triples
Quadruples
in result.
2. 2. Triples
3. 3. Indirect Triples