
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...
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 view and download past papers
Click on Show Past Papers to load available exam papers.