Topic 4 · Weeks 9–11

Topic 4 — Compiling Loops

Control flow becomes labels and jumps; then the optimizer earns its keep.

1. Lexicalchars → tokensscanner.l2. Syntaxtokens → ASTparser.y3. SemanticAST → checked ASTsemantic.c4. IRAST → TACtac.c5. OptimizeTAC → better TACtac.c6. CodeGenTAC → MIPScodegen.c

Weeks 9–11  ·  Nov 2 – Nov 22, 2026  ·  Control flow becomes labels and jumps; then the optimizer earns its keep.

Start here

Lecture notes →

Lowering structured statements to jumps; basic blocks and the flow graph; and the data-flow framework that makes optimization something you can reason about rather than guess at.

The full theory for this topic, with diagrams, worked examples and Dragon Book references. This page you are on is only an index.

Your starter code

The compiler you are extending this topic, with every TODO spelled out.
student/topic-4-loops/

Assignment: Project 4 · Loops and Optimization

Full requirements and submission checklist. Due Nov 22, 2026.

Stuck on the tools rather than the theory? The toolchain tutorials cover make, Git and GitHub, flex and bison.

Class activities

One per class meeting. Ungraded — they exist so that the assignment is not the first time you meet the idea.

Activity 1 — Lower a Loop, Then Draw Its Flow Graph

Labels and jumps on paper, hand-execute it, find the basic blocks and the back edge, then move the test to the bottom and see what you built.

Activity 2 — for vs while — a Design Decision

Lower directly or desugar? Write both, settle the degenerate cases, and let continue tell you which is riskier.

Activity 3 — break, and the continue You Are Not Implementing

A label stack, a depth counter, and a two-sentence argument for why no grammar could have caught it.

Activity 4 — Optimize a Listing by Hand

Pass by pass to a fixed point, naming every transformation, then break the label rule and meet the worst failure mode in compilers.

Activity 5 — Measure It, and Defend the Number

Code size and work done are different numbers. Get both, repeat the run, and state a threshold you can justify.

Activity 6 — Unrolling, Code Motion, Strength Reduction

Three loop optimizations by hand — two you will not implement, and one you already have without noticing.