HeadlinesBriefing favicon HeadlinesBriefing.com

Minimalist LLVM compiler nibble in 3000 lines of C

Hacker News •
×

A new compiler named nibble generates LLVM IR in roughly 3000 lines of C without using malloc or any external dependencies. Written as a single-pass front end, it skips building an AST, simplifying the codebase dramatically. The project explores minimal compiler construction while still producing functional output.

Nibble supports a range of C-like features: defer, recursion, integers, floats, booleans, structs, pointers, and basic C interoperability via generic pointers. To showcase its capabilities, the project includes four graphical demos that require SDL2 and Clang to compile and run. Among these are two multithreaded software renditions of popular shader-toy demos, a red-black tree implementation, and a basic game programming setup.

The single-pass design's free allocation of stack slots within loops causes stack overflows when the Clang back-end optimizer does not hoist allocas as expected. The author acknowledges this limitation but considers the project complete. Nibble's source code remains small and readable, offering a concrete example of minimal compiler construction.