Three Address Code
Three Address Code
Example:
Given statement:
a := (-c * b) + (-g / d)
t1 := -c
t2 := t1 * b
t3 := -g
t4 := t3 / d
t5 := t2 + t4
a := t5
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.
t1 := -b
t2 := c + d
t3 := t1 * t2
a := t3
These statements are represented by quadruples as follows:
(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.
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.
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.
3. Unconditional Jump:
goto L
Here,
• L is the label (of the target statement).
4. Indexed Assignment:
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.