Topic 5 · Class activity 2 · Week 12 · Friday

Lower Nested Conditionals

Count the labels, draw the flow graph, execute all four cases, then remove one jump and see which cases still look fine.

Format pairsTime 45 minBring paper

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

  1. 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);
    }
  2. Count the labels. How many did you need? Write the general formula for k nested if-else statements.
  3. 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?
  4. Hand-execute all four combinations of a and b being positive or not. Confirm each prints exactly one number.
  5. Sabotage. Remove the goto after at the end of the then-branch. Re-execute all four cases. Which now print TWO numbers, and which still look correct?
  6. Implement and confirm the compiler produces the same shape you did.
  7. 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.