CD Notes
Detailed guide to quadruples as three-address code representation covering structure, advantages, comparison with triples, and practical implementation in intermediate code generation.
Definition
A quadruple is a record with four fields: (operator, arg1, arg2, result). It is the most straightforward implementation of three-address code, where each instruction is stored as a 4-tuple. The result field explicitly names the temporary or variable that receives the computation's output.
Structure
| op | arg1 | arg2 | result |
|---|---|---|---|
| + | a | b | t1 |
| * | t1 | c | t2 |
| - | d | e | t3 |
| / | t2 | t3 | t4 |
| = | t4 | x |
Complete Example
| # | op | arg1 | arg2 | result |
|---|---|---|---|---|
| 0 | * | b | c | t1 |
| 1 | * | b | c | t2 |
| 2 | + | t1 | t2 | t3 |
| 3 | = | t3 | a | |
| 4 | + | a | 1 | t4 |
| 5 | = | t4 | d | |
| 6 | if> | d | 10 | L1 |
Advantages of Quadruples
| 1. Easy to rearrange | Since results are named explicitly, |
| Original | Reordered: |
| 0 | t1 = a + b 0: t2 = c * d |
| 1 | t2 = c * d 1: t1 = a + b ← still correct! |
| 2 | t3 = t1 + t2 2: t3 = t1 + t2 |
| 2. Easy optimization | Can delete, move, or modify individual |
| 3. Simple implementation | Four-field record, array of structures. |
| 4. Direct mapping to code generation | Each quadruple maps |
Quadruples vs Triples
| Feature | Quadruples | Triples |
|---|---|---|
| Result name | Explicit (t1,t2..) | Implicit (position) |
| Storage | 4 fields per instr | 3 fields per instr |
| Reordering | Easy (names stable) | Hard (positions change) |
| Temp count | Many temporaries | No extra temps needed |
| Optimization | Straightforward | Must update references |
| Memory use | More (extra field) | Less (no result field) |
Quadruples for Control Flow
| # | op | arg1 | arg2 | result |
|---|---|---|---|---|
| 1 | - | b | a | t1 |
| 3 | - | a | b | t2 |
| 4 | = | t2 | x | |
| 0 | if> | a | b | 4 |
| 2 | = | t1 | x | |
| 3 | goto | 6 | ||
| 5 | = | t2 | x | |
| 6 | * | x | 2 | t3 |
| 7 | = | t3 | y |
Quadruples for Function Calls
| # | op | arg1 | arg2 | result |
|---|---|---|---|---|
| 0 | + | x | 1 | t1 |
| 1 | * | y | 2 | t2 |
| 2 | param | t1 | ||
| 3 | param | t2 | ||
| 4 | call | add | 2 | t3 |
| 5 | = | t3 | result |
Implementation in Code
Interview Questions
- Q: Why are quadruples preferred over triples in optimizing compilers?
A: Quadruples use explicit temporary names, so instructions can be freely reordered, deleted, or moved during optimization without updating references. Triples use positional references that break when instructions move. The extra storage for the result field is a small price for optimization flexibility.
- Q: How does backpatching work with quadruples?
A: When generating code for boolean expressions or control flow, branch targets may not be known yet. The compiler emits the quadruple with an empty target field and records its index. When the target becomes known (label defined), it patches the target field. Lists of quadruples needing the same target are maintained for efficient patching.
- Q: How many temporary variables does a quadruple representation typically need?
A: One temporary per intermediate computation. For expression a+b*c-d: three temporaries (t1=b*c, t2=a+t1, t3=t2-d). A basic block of n operations needs at most n temporaries, though reuse analysis can reduce this to the maximum number of simultaneously live temporaries.
- Q: How are quadruples stored in memory during compilation?
A: Typically as an array of structures (fixed-size records), with the nextQuad pointer tracking the next available position. This allows random access for backpatching and sequential access for code generation. Some implementations use linked lists for easier insertion/deletion during optimization.
- Q: Can quadruples represent modern language features like closures or exceptions?
A: Not directly with basic quadruples. Extensions are needed: closures require instructions for environment capture and function objects, exceptions need try/catch/throw pseudo-operations that map to runtime support code. The basic quadruple framework is extended rather than replaced for these features.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Quadruples Representation in Compiler Design.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Compiler Design topic.
Search Terms
compiler-design, compiler design, compiler, design, intermediate, code, generation, quadruples
Related Compiler Design Topics