CD Notes
Complete guide to three-address code covering instruction types, generation from expressions and control structures, boolean expression translation, and implementation techniques.
Definition
Three-address code (TAC) is an intermediate representation where each instruction has at most one operator on the right side (besides assignment). The general form is: x = y op z, meaning apply operator op to y and z and store the result in x. The name comes from the three "addresses" (operands) involved.
Instruction Types
Generating TAC from Expressions
Generating TAC for Control Structures
If-Else Statement
While Loop
Switch Statement
| Source | switch(x) { case 1: a=10; break; case 2: a=20; break; default: a=0; } |
| L1 | a = 10 |
| L2 | a = 20 |
| L_default | a = 0 |
Boolean Expression Translation
Short-Circuit Evaluation
| Source | if (a < b && c > d || e == f) goto L_true |
| if a < b goto L1 // a<b true | check c>d |
| goto L2 // a<b false | try e==f (OR) |
| if c > d goto L_true // c>d true | whole AND is true |
| goto L2 // c>d false | AND is false, try OR |
| if e == f goto L_true // e==f true | OR succeeds |
| Control flow for | E1 AND E2 OR E3 |
| E1_true | E2 |
| E1_false | E3 (short-circuit AND, try next OR clause) |
| E2_true | L_true |
| E2_false | E3 |
| E3_true | L_true |
| E3_false | L_false |
Numerical Representation of Booleans
Array Access Translation
| One-dimensional | x = a[i] |
| x = *t2 // or | x = a[t1] in TAC notation |
| Two-dimensional (row-major) | x = a[i][j] |
| With lower bounds | a[low1..high1][low2..high2] |
Function Call Translation
Source: result = max(a + b, c * d);
TAC:
t1 = a + b
t2 = c * d
param t1 // push first argument
param t2 // push second argument
t3 = call max, 2 // call with 2 arguments
result = t3
Source: int factorial(int n) { if (n<=1) return 1; return n*factorial(n-1); }
TAC:
factorial:
if n <= 1 goto L1
t1 = n - 1
param t1
t2 = call factorial, 1
t3 = n * t2
return t3
L1:
return 1Type Conversions in TAC
| Source | float f = 3 + (int)x; // x is double |
| t1 = (float) i // int | float |
| t1 = (int) d // double | int (truncate) |
| t2 = (char) i // int | char (truncate) |
Temporary Variable Management
Interview Questions
- Q: Why is three-address code called three-address?
A: Each instruction involves at most three addresses (operand locations): typically two source operands and one destination. For example, t1 = a + b has three addresses: a, b, and t1. Unary operations and copies use fewer, but three is the maximum.
- Q: How does short-circuit evaluation of boolean expressions translate to TAC?
A: Short-circuit AND: if left operand is false, skip right operand (jump to false label). Short-circuit OR: if left operand is true, skip right operand (jump to true label). This generates conditional jumps instead of evaluating both sides, potentially avoiding side effects and improving performance.
- Q: How are function calls represented in TAC?
A: Using param instructions to push arguments, followed by call p,n (call function p with n arguments). The return value is assigned to a temporary. This representation is ABI-independent — the code generator maps params to registers or stack positions according to the calling convention.
- Q: What is the advantage of TAC over stack-based IR?
A: TAC makes operands explicit (named temporaries), enabling optimization via dataflow analysis. Stack-based IR (like JVM bytecode) has implicit operands on the stack, making analysis harder. TAC also maps more naturally to register machines. However, stack IR is more compact and simpler to generate.
- Q: How many temporaries are needed for a basic block?
A: At most as many as the maximum number of simultaneously live temporaries — equivalent to the minimum number of registers needed. For expression trees, this equals the Ershov number (tree register labeling). Optimal temporary reuse within a block is efficiently computable using liveness analysis.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Three-Address Code 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, three
Related Compiler Design Topics