Generating code

The back end: compiling to a stack machine with backpatched jumps, running it on a virtual machine, and assigning registers with linear scan.

About 4 hours of study

Learning objectives

Lessons

  1. 1

    Generating code for a stack machine Advanced

    Compile variables, loops and conditions to stack-machine instructions, fill in jump addresses with backpatching, and run the result on a small virtual machine, the way the JVM, CPython and WebAssembly work.

  2. 2

    Register allocation Advanced

    Decide which values live in the processor's few registers. Compute live ranges, measure register pressure, and assign registers with linear scan, spilling to memory when there are not enough.

Module practice

Module quiz

1. Which code does a = b - 1 compile to on a stack machine?
2. Where does the JUMP at the end of a while loop go?
3. Why is backpatching needed?
4. When can two values share a register?
5. Linear scan has no free register. Which value does it spill?
6. Four values are live at once and there are three registers. What must happen?

‹ Intermediate code and optimizationFinal assessment ›