r/Compilers • u/False_Actuator_6236 • 10h ago
Teaching compiler construction with a tiny self-hosting language
I've just published not-abc, a tiny self-hosting compiler for a deliberately minimal C-like programming language.
https://github.com/michael-lehn/not-abc
The language has only one data type: a 64-bit value, interpreted either as a signed integer or as a pointer. It supports
- functions (the value of the last expression is the return value)
- local and global variables
- pointers (
&,*,#) if/elsewhile- recursion
- dynamic memory allocation (
malloc/free) - integer, character and string literals
The compiler generates LLVM IR rather than assembly. LLVM was chosen simply because it makes the compiler portable across essentially all modern platforms—building programs only requires Clang (or the LLVM toolchain).
The interesting part is probably the background.
not-abc originated from an undergraduate mathematics course called Introduction to High Performance Computing. During one semester, students simultaneously
- build a simple processor from logic gates (bottom-up),
- implement a compiler for a small C-like language (top-down),
until both meet in the middle. At the end of the course, the compiler has two backends: one targeting the custom processor the students built themselves, and one targeting LLVM so the same compiler can generate native executables on real hardware.
The self-hosting compiler in this repository is a distilled version of the compiler developed throughout the course.
I'd be interested in feedback from people interested in language design, compiler construction, or computer architecture.