Yasir Explains/Compiler Design/Intermediate Code Generation/Triples
Intermediate Code Generation

Triples

On this page

DefinitionThe ExampleSpecial CasesTriples vs Quadruples — The Trade-offKey Points
Intermediate Code Generation

Triples

A triple is a three-address instruction stored with just three fields — op, arg1, arg2 — and no result field. Instead of inventing a temporary name for each result, a triple is referred to by its own position (n). This saves space but makes the code fragile to reordering.

Definition

Triple. A triple is a record with exactly three fields — op, arg1, and arg2 — used to represent one three-address instruction. Unlike a quadruple, a triple has no result field: the value computed by a triple is referred to by the position (line number) (n) of that triple itself.

Because results are named by position rather than by an explicit temporary, no temporary names are stored at all. Wherever a quadruple would write t1 and read it back later, a triple simply refers to (0) — the triple sitting at index 0.

The Example

Take the shared assignment:

Example
x = (a + b) \* (c - d)

Broken into three-address code, it needs three temporaries:

Example
t1 = a + b
t2 = c - d
t3 = t1 \* t2
x = t3

As triples — three fields only, the result implied by position — this becomes:

#oparg1arg2
(0)+ab
(1)-cd
(2)*(0)(1)
(3)=x(2)

The key shift: t1 is gone. It was never a real name — it was just "the result of triple (0)". So triple (2) does not read t1 and t2; it reads (0) and (1), the positions of the triples that produced those values. Likewise the final assignment, triple (3), stores into x the result of (2).

Notice the count: three temporaries (t1, t2, t3) collapse to zero stored names, because each result is addressed by where it lives in the array.

Special Cases

A few forms need care with only three fields:

  • Unary operator — arg2 is simply left empty. For y = -a you write (minus, a, _) and then (=, y, (0)).
  • Copy / assignment — assignment is itself a triple. x = (2) is encoded as (=, x, (2)): the operator is =, arg1 is the target name, and arg2 references the value being copied.
  • Indexed access — array references usually need two triples, one to compute the address (offset) and one to load or store. For example a[i] becomes a triple for a[i]-style addressing whose position is then consumed by the surrounding operation.

Keep the idea simple: any value that another instruction must reuse is reached through a position (n), never through a stored name.

Triples vs Quadruples — The Trade-off

Triples buy you space. A quadruple carries four fields, including an explicit result holding a temporary name; a triple drops both the field and the name, so the representation is smaller and there are no temporaries to manage.

But that saving comes with a real cost for optimization. Because a result is identified by its position, the moment instructions are reordered, inserted, or deleted, every (n) reference can point at the wrong row — so all positions must be renumbered.

Illustration — suppose an optimizer moves triple (1) above triple (0):

Example
before after the move (positions shift!)
(0) + a b (0) - c d <- was (1)
(1) - c d (1) + a b <- was (0)
(2) \* (0) (1) (2) \* (0) (1) <- now MULTIPLIES THE WRONG ROWS

Triple (2) still says (0) \* (1), but (0) and (1) now refer to swapped instructions, so the meaning is broken. Fixing it means rewriting every reference — an O(n) renumbering pass after each move.

This fragility is exactly what motivates indirect triples (the next topic), which add a level of indirection so instructions can be reordered without renumbering.

Key Points

  • A triple has three fields: op, arg1, arg2 — and no result field.
  • A triple's result is named by its position (n); no temporary names are stored.
  • Operands are a name, a constant, or a reference (n) to another triple.
  • Saves space versus quadruples (no result field, no temporaries).
  • Drawback: reordering / inserting / deleting forces renumbering of all (n) references — bad for optimization.
  • Indirect triples fix this fragility.