Reading — step 1 of 5
Read
~1 min readType Checking & IR
Control Flow Lowering
If/else/while in source maps to labels + branches in IR.
If:
if (x > 0) y = 1; else y = -1;
↓
t1 = LOAD x
CMP_GT t2, t1, 0
BR_IF t2, L_then, L_else
L_then:
STORE y, 1
BR L_end
L_else:
STORE y, -1
L_end:
While:
while (i < 10) i = i + 1;
↓
L_start:
t1 = LOAD i
CMP_LT t2, t1, 10
BR_IF t2, L_body, L_end
L_body:
t3 = LOAD i
t4 = ADD t3, 1
STORE i, t4
BR L_start
L_end:
For:
for (i = 0; i < 10; i++) sum = sum + i;
↓ (desugar to while)
i = 0
L_start:
CMP_LT t1, i, 10
BR_IF t1, L_body, L_end
L_body:
...sum = sum + i...
L_inc:
i = i + 1
BR L_start
L_end:
Continue jumps to L_inc; break jumps to L_end.
Boolean short-circuit:
if (a && b) ...
↓
t1 = LOAD a
BR_IF_NOT t1, L_false
t2 = LOAD b
BR_IF_NOT t2, L_false
BR L_true
L_true:
...
Switch:
- Small dense range: jump table (indexed branch).
- Sparse: chain of comparisons or binary search.
Return:
- BR to a single epilogue label that does cleanup + return.
- Or split: each function exit has its own epilogue.
The IR makes control flow explicit. Optimizers analyze the graph.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…