r/ProgrammingLanguages • u/someOfThisStuff • Jun 07 '26
Steps to a self-hosted compiler?
After having programmed in C for a while, I have finally decided to create my own language, very similar to it, in an attempt to fix it for my personal use.
It is my belief that, with some relatively minor tweaks and changes, C could be transformed into a language I am prepared to sink much time programming for. All of this, of course, is my own opinion; though if you are curious, here is a copy of the current draft for the standard: https://pastebin.com/eq1v7K9t
With the goal being to have a completely self-hosted transpiler (so, written in the language itself), I've been wondering how people usually go about this: do they write a sketchy, barely-working patchwork just to get the language "alive", and then code the true compiler? Or is it more common to do things well the first time, even if it means slower progress?
View all comments
Show parent comment
1
u/jsshapiro Jun 23 '26
This is good advice. Another approach, which we used in BitC, was to distinguish between the "fully elaborated" language and the [small] "core" language. The fully elaborated language has a purely syntactic reduction to the core language, and then the core language becomes the thing you self-host, and then you re-write the reducer in the fully elaborated language and you have a compiler.
Given modern optimizer technology, you're almost guaranteed to use LLVM (probably at MLIR level) rather than self-host the back end, so this is actually a fairly practical approach. Getting to "self" hosting becomes a whole lot quicker, because that core language is going to be something that has a fairly direct representation in MLIR.