Quadruples
A quadruple stores each three-address statement as a record with four fields — op, arg1, arg2, result. Because the result temporary is named explicitly, quadruples can be reordered freely during optimization, at the cost of an extra field and many temporary names.
Definition
Quadruple. A quadruple is a record structure with four fields —
op,arg1,arg2, andresult. Each three-address statement is stored as exactly one quadruple:opis the operator,arg1andarg2are its operands, andresultis where the value is placed.
The whole intermediate program is then just an array of quadruples, one per statement. Operands and results are referred to by name (a variable like a, or a compiler-generated temporary like t1).
The Example
Take the assignment:
A three-address code generator breaks it into one operation per statement, inventing temporaries t1, t2, t3 to hold partial results:
Each three-address statement becomes one quadruple:
| # | op | arg1 | arg2 | result |
|---|---|---|---|---|
| 0 | + | a | b | t1 |
| 1 | - | c | d | t2 |
| 2 | * | t1 | t2 | t3 |
| 3 | = | t3 | - | x |
Statement t1 = a + b maps to the quad (+, a, b, t1), and so on. Notice that the temporaries t1, t2, t3 appear in the result field where they are computed, and are then read back from arg1/arg2 by later quads. The copy x = t3 uses the assignment operator = with arg2 left empty.
Special Cases
Not every statement needs both operands or even a real result name. The four fields stretch to cover all of them — unused fields are simply left empty (-):
| Kind | Statement | Quadruple (op, arg1, arg2, result) |
|---|---|---|
| Unary op | x = minus y | (minus, y, -, x) |
| Copy | x = y | (=, y, -, x) |
| Unconditional jump | goto L | (goto, -, -, L) |
| Conditional jump | if a < b goto L | (if<, a, b, L) |
A unary operator uses only arg1, leaving arg2 empty. A copy is just the assignment operator with one source operand. For jumps, there is no value to store, so the target label is placed in the result field — goto ignores both args, while a conditional test such as if< compares arg1 and arg2 and jumps to result when the test holds.
Pros and Cons
Key advantage — easy to reorder. Because every quadruple names its result explicitly, other quads refer to that value by name (t1), not by the position of the instruction that produced it. So the optimizer can move or reorder a quadruple freely — its name references stay valid wherever it lands. This makes quadruples the friendliest form for code motion and other reordering optimizations.
Disadvantage — space and temporaries. A quadruple carries the extra result field, so it uses more space than alternatives, and it forces the generator to invent a fresh temporary name for almost every intermediate value — cluttering the symbol table.
Contrast with triples. A triple drops the result field and refers to a computed value by the position (index) of the triple that produced it. That saves space and avoids temporary names, but it ties references to positions: moving a triple breaks the indices, so triples are harder to reorder — the trade-off quadruples are designed to avoid.
Key Points
Key Points
- A quadruple is a 4-field record:
op,arg1,arg2,result— one quad per three-address statement. - The program is an array of quadruples; operands and results are referenced by name.
- Temporaries (
t1,t2, ...) are written into theresultfield and read back from the arg fields. - Unused fields are left empty: unary ops skip
arg2; jumps put the target label inresult. - Big win: explicit result names let quads be reordered freely during optimization.
- Cost: the extra field uses the most space and breeds many temporary names — the reason triples exist as a more compact alternative.