Translation of Different Language Features
1. Goal
Convert each source construct into equivalent three-address / IR that preserves meaning and exposes optimization chances.
2. Standard Translations
| Feature | IR pattern |
|---|---|
Arithmetic x=a+b*c |
t1=b*c; t2=a+t1; x=t2 |
Array x=a[i] |
t1=i*4; t2=a[t1]; x=t2 (width scaled) |
If if E then S1 else S2 |
ifFalse E goto L2; S1; goto L3; L2: S2; L3: |
While while E do S |
L1: ifFalse E goto L2; S; goto L1; L2: |
| For | desugar to while + init/incr |
Function call f(a,b) |
param a; param b; t=call f,2 |
| Switch | jump table or if-chain |
Short-circuit a&&b |
ifFalse a goto Lfalse; ifFalse b goto Lfalse; ... |
3. Worked — while a<b do x=x+1
L1: t1 = a < b
ifFalse t1 goto L2
t2 = x + 1
x = t2
goto L1
L2:
Backpatching fills L1/L2 after generation (two passes or pending lists).
4. Boolean Handling
Numerical (t=E) vs jump code (true/false lists) — jumps avoid extra temporaries for control flow.
5. Exam Points
- Translate if/while/array/call into 3-address — 7 marks, very frequent.
- Backpatching idea — 3-5 marks, frequent.
- Short-circuit translation — 5 marks, frequent.