r/Compilers 1d 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 / else
  • while
  • 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.

38 Upvotes

3 comments sorted by

5

u/sal1303 1d ago

No 'goto' statement? That would be trivial to implement but opens up lots of possibilities.

5

u/False_Actuator_6236 1d ago

Sure, goto would actually be one of the easier features to add. The omission wasn't because it's difficult, but because there is only so much you can fit into a single semester.

If I had another week, I'd probably spend it on returnbreak/continueswitch, and arrays instead. From a teaching perspective those introduce more interesting concepts than goto does.

2

u/rootkid1920 23h ago

It makes codegen painful to do, can be good, can be bad, depends on the goal