Module -5
Chapter 6
Intermediate Code Generation
Prof. Maya B S
Assistant Professor
Department of CS&E
BIT, Bangalore
Prof.Maya B S, Assistant Professor, CSE,BIT 1
Prof.Maya B S, Assistant Professor, CSE,BIT 2
Chapter 6
Intermediate Code Generation
6.1 Variants of Syntax tree
6.2 Three Address Code
Prof.Maya B S, Assistant Professor, CSE,BIT 3
Intermediate Code Generation
Prof.Maya B S, Assistant Professor, CSE,BIT 4
Prof.Maya B S, Assistant Professor, CSE,BIT 5
Prof.Maya B S, Assistant Professor, CSE,BIT 6
6.1 Variants of Syntax tree
Prof.Maya B S, Assistant Professor, CSE,BIT 7
6.1.1 Directed Acyclic Graph for Expressions
• The syntax tree for an expression, a DAG has leaves corresponding to
atomic operands and interior codes corresponding to operators.
• The difference is that a node ‘N’ in a DAG has more than one parent.
• if ‘N’ represents a common subexpression.
• In a syntax tree , the tree for the common subexpression would be
replicated as many times as the subexpression appears in the original
expression.
• DAG not only represent expression , it gives the compiler important clues
regarding the generation of efficient code to evaluate the expression.
Prof.Maya B S, Assistant Professor, CSE,BIT 8
1.write DAG for below expression a+a*(b-c)+(b-c)*d
• The leaf for ‘a’ has 2 parents, because ‘a’ appears in the expressions.
• Two occurrence of the common subexpression b-c are represented by one
node , he node labeled by –
• That node has 2 parents , representing its two uses in the subexpression
a*(b-c) and (b-c)*d.
• Even though ‘b’ and ‘c’ appears twice in the complete expression, their
nodes each have one parent, since both uses are in the common
subexpression b-c.
Prof.Maya B S, Assistant Professor, CSE,BIT 9
SDD for Directed Acyclic Graph for Expressions
Prof.Maya B S, Assistant Professor, CSE,BIT 10
• We assume that entry-1 points to the symbol table entry for ‘a’ and
similarly for other identifier.
• When the call to leaf(id,entry-1) is repeated at step2, the node created by
the previous call is returned so p2=p1.
• Similarly, the nodes returned at step8 and 9 are the same as those returned
at step 3 and 4(i.e p8=p3, p9=p4).
• Hence the nodes returned at step 10 must be the same at that returned at
step5. ie p10=p5.
Prof.Maya B S, Assistant Professor, CSE,BIT 11
6.1.2 The value number method for constructing DAG
• The nodes of a syntax tree or DAG are stored in an array of records as
shown in below figure.
• Ex: Nodes of a DAG i=i+10 allocated in an array.
• Each row of the array represents one record and therefore one node.
• In each record, the first field is an operation code, indicating the label of the
node.
• Fig b leaves have one additional field ,which holds the lexical value(either
a symbol table pointer or constant) and interior nodes have two additional
fields indicating the left and right children.
Prof.Maya B S, Assistant Professor, CSE,BIT 12
• In this array , we refer to nodes by giving the integer index of the record for that
node within the array.
• This integer historically has been called the value number for the node or for the
expression represented by the node.
• Above figure ,the node labeled + have value number 3 and its left and right children
have number 1 and 2 respectively.
• Use pointer to record or reference to objects instead of integer indexes ,but we
shall still refer to the reference to a node as its “valve No” .If stored in an
appropriate data structure, value number helps us construct expressions DAG
efficiently.
Prof.Maya B S, Assistant Professor, CSE,BIT 13
Algorithm for value number method for constructing DAG
Input: Label op,node l,and node r
Output: The value number of a node in the array with
signature <op,l,r >
Prof.Maya B S, Assistant Professor, CSE,BIT 14
Problems on DAG and value number method
1. ((x+y)-((x+y)*(x-y)))+((x+y)*(x-y))
2. Construct the DAG and identify the value number for the sub expression
of the following expressions, assuming + associates from left.
a) a+b+(a+b)
b) a+b+a+b
c) a+a+(a+a+a+(a+a+a+a))
Prof.Maya B S, Assistant Professor, CSE,BIT 15
6.2 Three Address Code
Prof.Maya B S, Assistant Professor, CSE,BIT 16
6.2.1 Address and Instruction
Prof.Maya B S, Assistant Professor, CSE,BIT 17
Instruction of 3-address code
Prof.Maya B S, Assistant Professor, CSE,BIT 18
Continue
Prof.Maya B S, Assistant Professor, CSE,BIT 19
Example of three address instruction
Prof.Maya B S, Assistant Professor, CSE,BIT 20
Consider the statement do i=i+1;while(a[i]<v);write 2possible translation of above
statement using three address code instruction.
Note: the multiplication i*8 is appropriate for an array of elements that each take
8 units of space.
Prof.Maya B S, Assistant Professor, CSE,BIT 21
1.Difference between syntax tree and DAG for x=(a+b*c)/(a-b*c)
Prof.Maya B S, Assistant Professor, CSE,BIT 22
2.Difference b/w syntax tree &DAG for a+b*c-d/b*c
Prof.Maya B S, Assistant Professor, CSE,BIT 23
6.2.2 Quadruples
• The description of 3-address instruction specify the components of each
type of instruction, but it does not specify the representation of these
instruction in a data structure.
• Ina compiler, these instruction can be implemented as objects or as records
with fields for the operator and operands.
• Three such representation are called “Quadruple”, “Triples”, and “Indirect
Triples ”.
Prof.Maya B S, Assistant Professor, CSE,BIT 24
Prof.Maya B S, Assistant Professor, CSE,BIT 25
Example of different form of records
Prof.Maya B S, Assistant Professor, CSE,BIT 26
Example for 3-address code
Prof.Maya B S, Assistant Professor, CSE,BIT 27
Quadruple
• A “Quadruple” has 4 fields, which we call op,arg1,arg2 and result.
• Op field contains an internal code for the operator.
• Ex: 3-address instruction x=y+z is represented by ‘+’ in op, y in arg1,z in
arg2 and ‘x’ in result.
• The following are some exception to this rule
• Instructions with unary operator like x=minus y or x=y do not use arg2.
note that for a copy statement like x=y, op is = while for most other
operations, the assignment operator is implied.
• Operator like param use neither arg2 nor result.
• Conditional and unconditional jumps put the target label in result.
Prof.Maya B S, Assistant Professor, CSE,BIT 28
1.Write the 3-addres code for the assignment a=b*-c+b*-c
and also quadruple.(VVVVIMP)
Prof.Maya B S, Assistant Professor, CSE,BIT 29
2 Write the 3-addres code for the assignment (a+b*c)-(d/b*c) and also
quadruple.
Prof.Maya B S, Assistant Professor, CSE,BIT 30
3.Write the 3-addres code for the assignment a=b+c*d and
also quadruple.
Prof.Maya B S, Assistant Professor, CSE,BIT 31
6.2.3 Triples
A Triple has only 3 fields, which we call op,arg1, and arg2.
The result field in a quadruples is used primarily for temporary names.
Using triples we refer to the result of an operation x op y by its position,
rather than by an explicit temporary name.
Instead of the temporary ‘t1’ in quadruple , a triple representation would
refer to position(0).
Parenthesized numbers represents pointer into the triple structure itself.
Prof.Maya B S, Assistant Professor, CSE,BIT 32
Write syntax tree and triples for expression a+a*(b-c)+(b-c)*d
• We construct syntax tree in fig a and fig b shows Triples.
Prof.Maya B S, Assistant Professor, CSE,BIT 33
2 Write the 3-addres code for the assignment (a+b*c)-(d/b*c) and also
Triples
• Triples shown in figure
Prof.Maya B S, Assistant Professor, CSE,BIT 34
3.Write the 3-addres code for the assignment a=b+c*d and also Triples.
Prof.Maya B S, Assistant Professor, CSE,BIT 35
Indirect Triples
• In this, an optimizing compiler can move an instruction by reordering the
instruction list, without affecting the triples themselves.
• When implemented in java, an array of instruction objects is analogous to
an indirect triple representation, since JAVA treats the array elements as
reference to objects.
Prof.Maya B S, Assistant Professor, CSE,BIT 36
Examples of Indirect Triples for (a+b*c)-(d/b*c)
Prof.Maya B S, Assistant Professor, CSE,BIT 37
Translate the arithmetic expression a+-(b+c) into
a) Syntax tree b) Quaduples c)Triples d)Indirect Triples
• Home work
Prof.Maya B S, Assistant Professor, CSE,BIT 38
6.2.4 Static single Assignment Form(SSA)
• SSA is an intermediate representation that facilities certain code
optimization.
• Two distinctive aspects distinguish SSA from 3 address code.
All assignment in SSA are to variables with distinct names; Hence the term
static single assignment .
Below figure shows the same intermediate program in 3 address code and
in static single assignment form .subscripts distinguish each definition of
variables p and q in the SSA representation.
Prof.Maya B S, Assistant Professor, CSE,BIT 39
The same variable may be defined in 2 different control flow path in a program.
Prof.Maya B S, Assistant Professor, CSE,BIT 40
Prof.Maya B S, Assistant Professor, CSE,BIT 41
• The source program,
• If(flag) x=-1;else x=1;
• Y=x*a;
• Has two control flow paths in which the variable ‘X’ gets defined.
• If we use different names for X in the true part and the false part of the
conditional statement , then which name should we use in assignment
y=x*a?
Prof.Maya B S, Assistant Professor, CSE,BIT 42
• Here is where the 2nd distinctive aspect of SSA comes into play?
SSA uses a notational convention called the ᶲ function to combine
the two definition of x:
If(flag) x1=-1;else x2=1;
X3= ᶲ(x1,x2);
Here, ᶲ(x1,x2) has value ‘x1’ if the control flow passes through the true
part of the conditional and the value x2 if the control flow passes
through the false part.
i.e ᶲ-function returns the value of its argument that corresponds to the
control flow path that was taken to get to the assignment statement
containing the ᶲ-function.
Prof.Maya B S, Assistant Professor, CSE,BIT 43
THANK YOU
Prof.Maya B S, Assistant Professor, CSE,BIT 44