The point
Lower nested conditionals by hand and count the labels. Nesting is where label generation goes wrong, and it goes wrong silently.
Do this
- Lower this by hand into TAC, generating labels as you go:
if (a > 0) { if (b > 0) { print(1); } else { print(2); } } else { print(3); } - Count the labels. How many did you need? Write the general formula for k nested if-else statements.
- Draw the flow graph. Apply the leader rules from Topic 4 to your listing. How many basic blocks? How many paths from entry to exit?
- Hand-execute all four combinations of a and b being positive or not. Confirm each prints exactly one number.
- Sabotage. Remove the
goto afterat the end of the then-branch. Re-execute all four cases. Which now print TWO numbers, and which still look correct? - Implement and confirm the compiler produces the same shape you did.
- Break the label generator — make it always return
L0. What does the assembler say? Is that message enough to find the cause?
Why step 5 matters
The cases that still look correct are exactly the ones a hastily written test would cover. A missing else-jump is invisible whenever the condition is false, which is why this bug survives casual testing and turns up in somebody else's program.
Deliverable
Your hand lowering; the label count and general formula; the flow graph with block and path counts; the four-case execution table; and which cases broke in step 5. Feeds Lab Question 26.