Code Generation and What an Optimiser May Change
The back end turns the intermediate form into instructions, assigns registers, and rearranges work. Every rearrangement is allowed only if the observable behaviour stays the same, which is why an optimiser can reorder arithmetic but not a write to a device.
What a learner can do afterwards
- Hand-generate instructions for a small expression and assign registers to the temporaries
- Name two transformations an optimiser is allowed to make and say what justifies each
- Explain why an optimiser must not remove a read that looks unused
1 · Read
You already know the compiler lowers a tree to a simpler middle form. The back end turns that form into real instructions. It picks instructions, assigns registers to temporaries, and rearranges work to run faster.
For a times b plus c, the back end loads each value into registers, multiplies the first two, then adds the third. With only two free registers it spills one value to memory and reloads it, since every temporary needs a home.
Every rearrangement must preserve observable behaviour: what the program prints, what it writes to devices, and what it returns. Reordering two independent sums is allowed because nobody can see a difference. Folding constants is allowed for the same reason.
Reads and writes that the outside world sees are fenced off. The optimiser must not remove a read that looks unused if a device sits behind it, and must never reorder across a visible write.
The back end assigns registers and reorders freely, but only where no observer could notice.
2 · Watch
Take it off screen
Where it sits
Learn first
This opens up
Nothing builds on it yet.
8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.