RC RANDOM CHAOS

Building a Stack VM Breadboard in SBCL with Register-Resident Slots

· via Hacker News

Original source

SBCL: the ultimate assembly code breadboard (2014)

Hacker News →

A blog post explores an experimental stack-based virtual machine that keeps its entire data stack in CPU registers rather than memory, using SBCL’s assembler as a live machine-code prototyping environment. Inspired by Chuck Moore’s F18 chip and the x87’s rotating stack, the author limits the stack to eight slots indexed by a modular counter, then specializes every primitive operation for all eight possible counter values. Pushes and pops become compile-time stack-pointer changes rather than runtime data movement, since the counter is known when each primop variant is emitted.

The VM uses a direct-threaded dispatch loop where bytecode stores 32-bit offsets from a primop base address, and the NEXT sequence jumps into the appropriate variant by adding a fixed stride (4288 bytes) times the current stack counter. The author walks through implementations of swap, dup, sub, jumps, call/return, immediates, and an FFI entry point that copies stack values in and out, then builds up to conditionals and a decrement-and-jump-if-nonzero primitive. A simple counter loop runs at roughly 15 cycles per iteration, improving further when lit/sub is fused into dec and when the conditional move is replaced with a duplicated NEXT sequence that lets the branch predictor see the test.

The broader point is methodological: SBCL’s in-image assembler and SLIME REPL make it practical to iterate on low-level code generation interactively, treating machine code like a breadboard where variants can be emitted, disassembled, executed, and re-tuned in seconds. The result is a working sketch of how aggressive primop specialization can make a register-only stack VM competitive without the usual top-of-stack caching tricks.

Read the full article

Continue reading at Hacker News →

This is an AI-generated summary. Read the original for the full story.