Three-Address Code:
o Three-address code is an intermediate code. It is used by the optimizing compilers.
o In three-address code, the given expression is broken down into several separate
instructions. These instructions can easily translate into assembly language.
o Each Three-address code instruction has at most three operands. It is a combination of
assignment and a binary operator.
Example:
Given statement:
a := (-c * b) + (-g / d)
The three-address code for the above statement is as follows:
t1 := -c
t2 := t1 * b
t3 := -g
t4 := t3 / d
t5 := t2 + t4
a := t5
tis are used as registers in the target program.
The three-address code can be represented in two forms: quadruples and triples.
Quadruples:
The quadruples have four fields to implement the three-address code. The fields of quadruples
contain the name of the operator, the first source operand, the second source operand and the
result of the operation respectively.
Fig: Quadruples field
Example:
1. a := -b * (c + d)
The three-address code for the above statement is as follows:
t1 := -b
t2 := c + d
t3 := t1 * t2
a := t3
These statements are represented by quadruples as follows:
Operator Source 1 Source 2 Destination
(0) uminus b - t1
(1) + c d t2
(2) * t1 t2 t3
(3) := t3 - a
Notes:
The contents of fields Source1, Source 2 and Destination are normally pointers to the symbol
table for the names represented by these fields. If so, the temporary names (created by the
three-address code) must be entered into the symbol table as created.
Triples:
The triples have three fields to implement the three-address code. The fields of triples contain
the name of the operator, the first source operand and the second source operand respectively.
In triples, the results of the three-address statements are represented by the statements
themselves in order to avoid entering temporary names into the symbol table. These statements
are in turn represented by their respective pointers into the triples table itself.
Fig: Triples field
Example:
These above three-address statements are represented by triples as follows:
Operator Source 1 Source 2
(0) uminus b -
(1) + c d
(2) * (0) (1)
(3) := (2) -
Notes:
As was the case for the quadruples, when the fields Source1 and Source 2 correspond to names
of the source code itself (not the ones created by the three-address code), their contents are the
pointers to the symbol table for the names represented by these fields.
Common Three Address Instruction Forms:
1. Assignment Statement:
i) x = y op1 z
ii) x = op2 y
iii) x = y
Here,
• x, y and z are the operands.
• op1 is a binary operator and op2 is a unary operator
It assigns the result obtained after solving the expression to the right of the assignment
operator (=) to the operand (x) to the left of it.
2. Conditional Jump:
If x relop y goto L
Here,
• x & y are the operands.
• L is the label (of the target statement).
• relop is a relational operator.
If the condition “x relop y” is evaluated to be ture, then:
• The control is sent directly to the location specified by label L.
• All the statements in between are skipped.
If the condition “x relop y” is evaluated to be false, then:
• The control is not sent to the location specified by label L.
• The immediate next statement appearing in the usual sequence is executed.
3. Unconditional Jump:
goto L
Here,
• L is the label (of the target statement).
On executing the statement,
• The control is sent directly to the location specified by label L.
• All the statements in between are skipped.
4. Indexed Assignment:
Indexed assignment statements take the following forms:
i) x = a [ i ]
• sets x to the value in the location ‘i memory units (bytes) beyond
location a’.
ii) a [ i ] = x
• sets the location ‘i memory units (bytes) beyond location a’ to the
value of x.
Here, a is called the base and i is called the offset.
In both instructions, x, i, and a are assumed to refer data objects and will be represented
by their corresponding indices (pointers) to the symbol table.