1 Intermediate Code Generation VNM
1 Intermediate Code Generation VNM
Unit 5
Construct three address code and VNM for the given DAG a+b+
(a+b)
DAG Three Address Code Value Number Method (VNM)
+
t1= a+ b
t2=t1+t1 1 id Entry for a
2 id Entry for b
+
a b 3 + 1 2
4 + 3 3
Three Address Code
• In three-address code, there is at most one
operator on the right side of an instruction.
• source-language expression like x+y*z might be
translated into the sequence of three-address
instructions
t1 = y * z
t2 = x + t1
where t1 and t2 are compiler-generated
temporary names.
Three Address Code From DAG and From Syntax
Tree
• a+b+(a+b) Three Address
Code
DAG
Syntax Tree t1=a+b
+
E t2=a+b
+
t3=t1+t2
E + T
a b
E + T ( E )
t1= a+ b Id T id
t2=t1+t1 id
Addresses and Instructions
• Three-address code is built from two concepts:
addresses and instructions.
An address can be one of the following:
• Name
• Constant
• A compiler-generated temporary
Common three-address instruction forms
• 1. Assignment instructions of the form x = y op z,
where op is a binary arithmetic or logical operation,
and x, y, and z are addresses.
• 2. Assignments of the form x = op y, where op is a
unary operation. Essential unary operations include
unary minus, logical negation, and conversion
operators, for example, convert an integer to a
floating-point number.
• 3. Copy instructions of the form x = y, where x is
assigned the value of y.
Common three-address instruction forms
contd..
• 4. An unconditional jump goto L. The three-address
instruction with label L is the next to be executed.
• 5. Conditional jumps of the form if x goto L and
ifFalse x goto L.
• 6. Conditional jumps such as if x relop y goto L,
which apply a relational operator (<, ==, >=, etc.) to
x and y, and execute the instruction with label L
next if x stands in relation relop to y.
Common three-address instruction forms contd..
7. Procedure calls and returns are implemented using the following
instructions: param x for parameters;
call p,n and y = call p,n for procedure and function calls, respectively;
return y, where y, representing a returned value, is optional.
p(x1,x2;…, xn) param x1
param x2
...
param xn
call p,n
Where n= no of arguments
8. Indexed copy instructions of the form x=y[i] and x[i]=y.
9. Address and pointer assignments of the form x =&y, x =*y, and *x = y.
The instruction x =&y sets the r-value of x to be the location (l-value)
of y.
Three Address Code Translation
1. a=b[i]+c[j]
2. a[i]=b*c + b*d
3. x=f(y+1)+2
Three address Translation of Control
Statements
• Consider the statement
do i = i+1; while (a[i] < v);
• Two possible translations of this statement are
The description of three-address
instructions
• Three address instructions can be implemented as
objects or as records with fields for the operator and
the operands. Three such representations are called
quadruples, triples, and indirect triples.
• Quadruples- A quadruple (or just quad) has four fields:
op, arg1, arg2, and result.
• The op field contains an internal code for the operator.
• For instance, the three-address instruction x = y +z is
represented by placing + in op, y in arg1, z in arg2, and
x in result.
Quadruple Representation
The following are some exceptions to this rule:
• 1. Instructions with unary operators like
x = minus y or x = y do not use arg2. For a copy
statement like x = y, op is =, while for most other
operations, the assignment operator is implied.
• 2. Operators like param use neither arg2 nor
result.
• 3. Conditional and unconditional jumps put the
target label in result.
Quadruple representation of
a = b*-c + b*-c
Triples
A triple has only three fields, which we call op, arg1, and arg2. With
triples, the result of an operation is referred to by its position, so
moving an instruction may require us to change all references to that
result.
Triple Representation of a = b*-c + b*-c
Indirect Triples
Indirect triples consist of a listing of pointers to triples, rather than a
listing of triples themselves. With indirect triples, an optimizing compiler
can move an instruction by reordering the instruction list, without a
directing the triples themselves
THANK YOU