Sub topic 10
Quadruples, triples and indirect triples
This chapter assumes you can read three-address code (instructions with at most one operator and three addresses) and know what a syntax tree is. It also assumes basic C-style programming. Three-address code is an abstract idea. A compiler must store it in some data structure, and GATE questions often ask you to write that structure out and compare the options.
Three ways to store the same code
Take the statement a = b * -c + b * -c. As three-address code it is:
(1) t1 = minus c
(2) t2 = b * t1
(3) t3 = minus c
(4) t4 = b * t3
(5) t5 = t2 + t4
(6) a = t5Each instruction becomes one record. The three storage forms differ in how a record names its result and how it refers to earlier results.
Quadruples
A quadruple has four fields: op, arg1, arg2 and result. The result field names a temporary or a variable, so every value has an explicit name.
| Index | op | arg1 | arg2 | result |
|---|---|---|---|---|
| 0 | minus | c | - | t1 |
| 1 | * | b | t1 | t2 |
| 2 | minus | c | - | t3 |
| 3 | * | b | t3 | t4 |
| 4 | + | t2 | t4 | t5 |
| 5 | = | t5 | - | a |
Unary operators and copies leave arg2 empty. A jump uses the result field to hold the target label or instruction number.
Because results are named, you can move an instruction and no other record changes. The cost is that the compiler must create temporary names and enter them in the symbol table.
Triples
A triple has three fields: op, arg1 and arg2. There is no result field. A result is identified by the position of the triple that computes it, so an operand can be either a name or a reference to an earlier triple.
| Index | op | arg1 | arg2 |
|---|---|---|---|
| 0 | minus | c | - |
| 1 | * | b | (0) |
| 2 | minus | c | - |
| 3 | * | b | (2) |
| 4 | + | (1) | (3) |
| 5 | = | a | (4) |
This saves one field per record and needs no temporaries. The drawback is that the references are positions. If an optimizer moves triple 2, every reference to it must be updated.
Indirect triples
Indirect triples keep the triples table unchanged but add a separate list of pointers that gives the execution order. Reordering code means reordering this list. The triples themselves, and the positions they refer to, stay fixed.
| Instruction list | Points to triple |
|---|---|
| 35 | (0) |
| 36 | (1) |
| 37 | (2) |
| 38 | (3) |
| 39 | (4) |
| 40 | (5) |
The numbers 35 to 40 are arbitrary list positions. The triples table can be shared by several instruction lists.
Comparing the three forms
| Property | Quadruples | Triples | Indirect triples |
|---|---|---|---|
| Fields per record | 4 | 3 | 3, plus a pointer list |
| Temporaries in symbol table | Yes | No | No |
| Cost of moving an instruction | Low | High, references must be fixed | Low, reorder the list |
| Extra space | None | None | One pointer per instruction |
| Convenience for optimizers | Best | Worst | Good |
For a statement with N operations, quadruples use 4N fields, triples use 3N and indirect triples use 3N plus N pointers. When a triples table is shared or reused, indirect triples can also save space.
Common gotchas
- Counting fields. GATE questions often ask for a total table size. Count empty fields too, such as
arg2inminus. They still occupy space. - Indexing. Triples are numbered from 0 or 1 depending on the source. Use the numbering the question uses.
- Assignments. In triples,
a = t5is written(=, a, (4)). The targetagoes inarg1, because no result field exists. In quadruples the target goes inresult. - Array stores.
x[i] = yneeds two triples with a store-style operator. Quadruples handle it in one record, such as[]=withx,iandy. - Repeated subexpressions. The code above computes
minus candb * t1twice. A DAG for the expression shares them, and the resulting code has only 4 operations instead of 5 plus a copy. Removing these repeats is common subexpression elimination.
A small program to build both forms
The following Python builds quadruples and triples for a tree of binary and unary operations. It reuses no subexpressions, so it matches the example above.
def gen_quads(node, quads, counter):
"""node: a name string, ('minus', x) or (op, x, y). Returns the operand name."""
if isinstance(node, str):
return node
if node[0] == "minus":
x = gen_quads(node[1], quads, counter)
counter[0] += 1
t = f"t{counter[0]}"
quads.append(("minus", x, "-", t))
return t
x = gen_quads(node[1], quads, counter)
y = gen_quads(node[2], quads, counter)
counter[0] += 1
t = f"t{counter[0]}"
quads.append((node[0], x, y, t))
return t
def gen_triples(node, triples):
"""Returns an operand: a name, or '(i)' for triple i."""
if isinstance(node, str):
return node
if node[0] == "minus":
x = gen_triples(node[1], triples)
triples.append(("minus", x, "-"))
else:
x = gen_triples(node[1], triples)
y = gen_triples(node[2], triples)
triples.append((node[0], x, y))
return f"({len(triples) - 1})"
expr = ("+", ("*", "b", ("minus", "c")), ("*", "b", ("minus", "c")))
quads, counter = [], [0]
result = gen_quads(expr, quads, counter)
quads.append(("=", result, "-", "a"))
triples = []
result = gen_triples(expr, triples)
triples.append(("=", "a", result))
print("Quadruples")
for i, q in enumerate(quads):
print(i, q)
print("Triples")
for i, t in enumerate(triples):
print(i, t)The output matches the tables above. To get indirect triples, add order = list(range(len(triples))) and treat order as the instruction list. An optimizer that moves code edits order and leaves triples alone.
Which form to expect
Quadruples are the usual textbook choice and the default in GATE answers unless the question says otherwise. Triples appear in questions about space, because they use one fewer field. Indirect triples appear in questions about reordering.
Exercise
The statement x = (a + b) * (a + b) is translated with no common subexpression sharing. (a) Write the quadruples. (b) Write the triples. (c) If each field takes one word, how many words do quadruples and triples need? (d) Which form needs no changes to existing records if the second addition is moved before the first?
Show answer
(a) (+, a, b, t1); (+, a, b, t2); (, t1, t2, t3); (=, t3, -, x). (b) 0: (+, a, b); 1: (+, a, b); 2: (, (0), (1)); 3: (=, x, (2)). (c) Quadruples: 4 records x 4 fields = 16 words. Triples: 4 records x 3 fields = 12 words. (d) Quadruples need no edits because operands use temporary names. Indirect triples also need no edits to the triples, since only the instruction list is reordered. Plain triples would need their references updated.
Exercise
A compiler generates 200 three-address instructions. Compare the storage needed, in fields, for quadruples, triples and indirect triples, counting one field per pointer in the instruction list.
Show answer
Quadruples: 200 x 4 = 800 fields. Triples: 200 x 3 = 600 fields. Indirect triples: 200 x 3 = 600 fields for the triples table plus 200 pointers = 800 fields.