These topics are part of Intermediate Code Generation and Code Generation phase in a compiler. They are very important for exams because they are often asked with diagrams and examples.
Backpatching is a technique used in compiler design to handle forward jumps in intermediate code by filling in (patching) missing addresses later.
👉 In simple words:
We write code first, and fill the correct jump addresses later when they become known.
In programming, sometimes we need to jump to a label that is not yet known.
Example:
if (a < b)
goto L1;
But at this time, we don’t know where L1 is located in code.
👉 So we leave it empty and fill it later.
1. if a < b goto __
2. goto __
3. ...
4. L1: t = a + b
Later we fill:
if a < b goto _
List = {1}
patch(1, L1)
Stores unresolved goto statements
makelist(i) → creates list with instruction imerge(p1, p2) → merges two listsbackpatch(p, addr) → fills address✔ Used in control flow statements ✔ Handles forward jumps ✔ Used in if, if-else, loops ✔ Uses three address code (TAC) ✔ Helps in code generation phase
A switch statement is a multi-way branch statement that allows selection of one block among many based on the value of an expression.
switch(x) {
case 1: a = 10; break;
case 2: a = 20; break;
default: a = 0;
}
Switch statements are converted into:
if x == 1 goto L1
if x == 2 goto L2
goto L3
L1: a = 10
goto end
L2: a = 20
goto end
L3: a = 0
end:
Used when cases are dense.
index = x - 1
goto table[index]
Where:
table[0] → L1
table[1] → L2
table[2] → L3
✔ Switch is a multi-way branching statement ✔ Implemented using:
| Concept | Role |
|---|---|
| Backpatching | Fills missing jump addresses |
| Switch statement | Uses multiple jumps to labels |
| Connection | Both involve control flow & jumps |
| Feature | Description |
|---|---|
| Purpose | Handle forward jumps |
| Used in | Control flow statements |
| Technique | Fill addresses later |
| Functions | makelist, merge, backpatch |
| Code type | Three address code |
| Feature | Description |
|---|---|
| Type | Multi-way branch |
| Implementation | if-else or jump table |
| Best method | Jump table |
| Use | Efficient selection |
| Optimization | Reduces comparisons |
Backpatching is a technique used in intermediate code generation where incomplete jump instructions are temporarily left with blank addresses and are filled later when target labels become known.
A switch statement is a multi-way decision construct that is translated into intermediate code using either conditional jumps or jump tables for efficient execution.
Open this section to load past papers