Bytecode Machine
You'll design an instruction set, write the loop that runs it, compile a small language down to it, and add a garbage collector that actually collects.
About
This is how real language runtimes work underneath: a compiler turns source into compact instructions, a loop runs them, and a collector takes out the trash. If you've done Language From Scratch in the Coding track, this is the step after it, where the tree-walker gets replaced by something several times faster and you take over memory for every object the program makes. The garbage collector is where the season goes, because a bug there corrupts memory you aren't looking at. The free second half of Crafting Interpreters by Bob Nystrom covers exactly this.
At a glance
- Per requirement
- 50 XP
- Completion bonus
- 600 XP
- Requirements
- 6
- Takes
- A season
- Skill level
- Advanced (years of practice)
- Cost
- Free
- Supplies
- You need some
- Where
- At home
- Other people
- On your own
Requirements
-
1
Design a bytecode with a dozen instructions and write a stack machine that runs a hand-assembled program. Push, pop, add, subtract, compare, jump, jump-if-false, call, return and print are enough. Write the run loop as a switch over the opcode. Hand-write a program in bytes that prints the first ten squares. Take a screenshot of the instruction table and the output. Photo
-
2
Write a compiler from a small language with variables, if, while and functions down to your bytecode. Reuse your parser from the Coding track if you have one, or write a recursive descent parser now. Emit jumps with placeholder offsets and patch them once you know the target. Take a screenshot of a source program and its disassembled bytecode side by side. Photo
-
3
Add heap objects, strings and closures, with a mark-and-sweep garbage collector. Keep every object on a linked list. Mark from the stack, the globals and the call frames, then sweep anything unmarked. Add a stress mode that collects on every allocation, which is how you find the object you forgot to root. Take a screenshot of the stress mode passing your tests. Photo
-
4
Run recursive Fibonacci of 30 and compare the time with a tree-walking interpreter or Python. Your VM should beat CPython or at least land in the same neighborhood. If it's wildly slower, profile it and fix the hot spot, which is usually the value representation. Take a screenshot of the timings. Photo
-
5
Write a test suite of 50 programs with expected output and run it with one command under the sanitizers. Each test is a source file and an expected output file. Build the VM with -fsanitize=address,undefined for the test run, because a GC bug that doesn't crash today will next week. Take a screenshot of the runner reporting 50 passing under the sanitizer. Photo
-
6
Write a 200-line program in your language that allocates a lot, show memory level off, and publish the VM. A text adventure or a linked-list sort both allocate plenty. Run it with GC logging on so you can see the heap size climb and fall rather than climb forever. Put the VM on a public repo with the bytecode spec. Take a screenshot of the memory log and the repo page. Photo