Crafting Interpreters · Week 8
Crafting Interpreters 15: A Virtual Machine: reading guide
The virtual machine: the instruction pointer, the main dispatch loop, a value stack, and arithmetic instructions, with tracing of the stack as it runs.
Time About 5 hours this week
Why it matters
This loop is the beating heart of every bytecode interpreter. Understanding stack machines also helps you read JVM or WebAssembly bytecode.
How to approach it
Enable the execution trace and step through a few hand-built chunks. Notice how each instruction pushes and pops.
Challenges
4 challenges at the end of the chapter.
Challenge 1 (write out bytecode for several expressions) is a quick and useful exercise. Challenge 3 (grow the stack dynamically) and challenge 4 (negate in place) are short C exercises worth doing.
Traps
- Stack overflow with the fixed-size stack, since pushes are unchecked.
- Pop order for binary operators: the right operand is on top.