The document discusses three address code, which is an intermediate code used by optimizing compilers. Three address code breaks expressions down into separate instructions that use at most three operands. Each instruction performs an assignment or binary operation on the operands. The code is implemented using quadruple, triple, or indirect triple representations. Quadruple representation stores each instruction in four fields for the operator, two operands, and result. Triple avoids temporaries by making two instructions. Indirect triple uses pointers to freely reorder subexpressions.
Three Address Code
PoojaDixit
Department of Computer Science
Sophia Girls’ College (Autonomous),
Ajmer
2.
Three address code
•Three-address code is an intermediate code. It is used by the
optimizing compilers.
• In three-address code, the given expression is broken down into
several separate instructions. These instructions can easily
translate into assembly language.
• In three address code, at most three addresses are used to
represent any statement.
• Each Three address code instruction has at most three
operands. It is a combination of assignment and a binary
operator.
• Example : Given Expression:
a := b op c
Where-
▫ a, b, c are operands
▫ Op= operators
• Example: a = b + c
▫ Here a, b, c = Operands
▫ +, = are Operator
3.
How to GenerateThree address code
• Eg:- a = b + c + d
this given expression has two operator +, =
Note: to generate 3-add code for given expression we
have follow these rules-
• ‘+’ operator has higher precedence over ‘=’ operator.
• ‘+’ operator is left associative.
• So the Address Code will be-
▫ T1 = b + c
▫ T2 = T1 + d
▫ a = T2
b + c + d
1
2
3
a =
According to the rule we
first solve b + c, than solve
d and at last store result
into a
4.
Implementation of ThreeAddress Code
• Three address code can be implemented as a record with the
address fields.
• There are three types of representation used for three
address code.
Representation
Quadruple
Triple
Indirect Triple
5.
Implementation of ThreeAddress Code –
1. Quadruple –
in quadruples representation each instruction is
divided into 4 fields.
Where-
▫ The op field is used to represent the internal code
for operator.
▫ The ag1, ag2 fields represent the two operands used.
▫ The result field is used to store the result of an
expression.
Operator
arg1
arg2
results
6.
Three Address Code
•Example
• Change this expression into three-address code:
▫ t1 := e f
▫ t2 := b * c
▫ t3 := t2 / t1
▫ t4 := b * a
▫ t5 := a + t3
▫ t6 := t5 + t4
• These statements are represented by quadruples as follows:
• Operator Source 1 Source 2 Destination
(0) e f T1
(1) * b c T2
(2) / T2 T1 T3
(3) * b a T4
(4) + a T3 T5
(5) + T5 T4 T6
Note: here we arrange the terms according to the
Operator priority
7.
Implementation of ThreeAddress Code –
• Advantage –
▫ Easy to rearrange code for global optimization.
▫ One can quickly access value of temporary variables
using symbol table.
• Disadvantage –
▫ Contain lot of temporaries.
▫ Temporary variable creation increases time and
space complexity.
8.
Triples
• The tripleshave three fields to implement the three address code. The
field of triples contains the name of the operator, the first arg1 operand
and the second arg2 operand.
• In triple representation the use of temporary variables are avoided and
instead to instructions are made.
Fig: Triples field
Operator
arg1
arg2
9.
Triples
Example: consider theexample of quadruple
• Three address code are same as quadruple
• These statements are represented by triples as follows:
• If there is any statement suffle
Operator Arg 1 Arg 2
(0) e f
(1) * b c
(2) / (1) (0)
(3) * b a
(4) + a (2)
(5) + (4) (3)
10.
Indirect Triples
• Thisrepresentation is an enhancement over triples representation. It
use an additional instruction array to list the pointers to the desired
order.
• Thus it use pointers instead of position to store result which enable the
optimizers to freely reposition the sub expression to produce an
optimize code.
Operator Arg 1 Arg 2
(0) e f
(1) * b c
(2) / (1) (0)
(3) * b a
(4) + a (2)
(5) + (4) (3)
Statement
35 (0)
36 (1)
37 (2)
38 (3)
39 (4)
40 (5)