HeadlinesBriefing favicon HeadlinesBriefing.com

SBCL Assembly Fix Cuts Code Size, Enables Stack VM Optimization

Hacker News •
×

A SBCL developer fixed an inefficient NEXT sequence in the compiler's assembly output, reducing a common instruction from 14 to 9 bytes. The bug involved a wasteful encoding for an effective address with an index register but no base. This patch eliminates six useless bytes per occurrence, a meaningful saving in tight loops.

This correction inspired a deeper exploration of stack-based virtual machines with a hard limit of 8 slots. Drawing from x87 floating-point and Chuck Moore's F18 architecture, the author argues that a restricted stack, managed via a modular counter, can eliminate data movement during push/pop operations. The key is generating specialized primitive variants for each possible stack pointer value.

The implementation uses SBCL's internal assembler to emit position-independent code. Variants are spaced 64 * 67 bytes apart, allowing a simple offset calculation based on the current stack counter. This technique trades code size for execution speed by avoiding runtime stack shuffling. The prototype demonstrates how Common Lisp's powerful macro and assembly facilities enable such low-level experimentation directly in the REPL.