A narrative walkthrough of implementing a conservative garbage collector for the lone lisp interpreter. Starting from a precise mark-and-sweep GC, the author explains why objects stored outside the lisp stack (in native C stack frames and CPU registers) were being incorrectly collected. The solution involves scanning the native stack for heap pointers using `__builtin_frame_address`, then spilling CPU registers onto the stack via hand-written x86_64 and aarch64 assembly (inspired by the Boehm GC's use of `setjmp`) so they too can be scanned. The result is a working conservative GC that passes the full test suite.

14m read timeFrom matheusmoreira.com
Post cover image
Table of contents
Trouble in the primitive landsInto the nether realmsThe native stackThe heap pointersThe shark attacksThe registersAnd just like that...

Sort: