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?
13
u/Coding-Kitten Jun 07 '26
From what I've read, the former is more common. In the bootstrap language you implement just the minimal feature set you'd want for the compiler, & then the in language compiler you write all the features you wanna add, but you write it in a way that's compatible with the original bootstrap version 1 compiler you wrote.
8
u/Inconstant_Moo š§æ Pipefish Jun 07 '26
That's some impressive planning.
Some proposed features made me raise my eyebrows.
$for inner functions to look at outer scope seems superfluous both from the user's point of view and the compiler. What's it for?break <number>semantics would be brittle when refactoring your code. Labels like in Go would be more robust.9
u/AustinVelonaut Admiran Jun 07 '26 edited Jun 07 '26
I would suggest not self-hosting too early, without having a stable, complete bootstrap version of the compiler, first.
For my self-hosted language Admiran, I wrote the initial bootstrap compiler in a very similar language Miranda. This was an implementation of the complete language, and I worked on the bootstrap compiler for about a year before it was stable enough to start self-hosting. Completing the first self-hosted compiler was pretty quick (1-2 months, part time) because the languages were fairly close. It helped that both languages had Hindley-Milner type checking and inference, which immediately pointed out any typing errors during the conversion.
9
u/runningOverA Jun 07 '26
You will need two versions.
A C version until the language can do its job.
Then port the C code to the new language.
Trying to bootstrap from day 1 will get you stuck frequently.
3
u/augustss Jun 07 '26
Write a simple compiler in a subset of C. Pick a subset that you can trivially map to your own language. When that works, hand translate your compiler to your own language. Voila! Then you can extend your language little by little.
2
u/phovos Jun 07 '26 edited Jun 07 '26
Most projects that I see that wish to play in the weeds do so using the IR-emit mode of the 20 year old standard compilers like GCC or CLang and a kind of 'JIT'/'lowering' to intermediate representation for getting closer to the metal, and to get away from any undesirable 'c-like' aspects you don't like, and perhaps even a self-hosting (bootstrapped) language but that would be quite an undertaking with unclear gains since most people I think learn HDLs instead of pushing the envelope with compilers; hardware description languages (HDLs) like VHDL or Verilog or Prolog -> ASIC/FPGA is the money route, completely sidestepping 'consumers' and crap and working on Milspec stuff like (random example) https://landandmaritimeapps.dla.mil/programs/milspec/ListDocs.aspx?BasicDoc=MIL-PRF-38535
Here is a good article about llvm IR, this is what many people do, instead of becoming compiler engineers, lol https://mcyoung.xyz/2023/08/01/llvm-ir/%7B%7Bsite.baseurl%7D%7D/2021/11/29/assembly-1 (or more technical https://mlir.llvm.org/docs/Tutorials/Toy/Ch-6/)
edit: Yes, Thompson wrote the original 'self-linking, bootstrapping compiler' for D/C lang and Unix which has been the substrate of every major (arbitrary label so I can't be too wrong) language, since. The only alternative I know just wraps assembly as FFI instead of C like many languages-do (OCAML/SmallTalk for ex); I believe that is what the HDLs-do; in-line assembly for when precise control over consumer hardware or for embedded.
2
u/Jaded_Following2089 Jun 08 '26
There's a very cool Tsoding video about this: https://www.youtube.com/watch?v=QGm-d5Ch5JM
But normally, starting with a manageable, perhaps "patchwork" implementation to get the language self-hosting quickly is the most used approach, I believe. Trying to build a perfect, fully-featured compiler from the very beginning may lead to paralysis analysis, which is a very common issue for people working on big projects
1
u/elszben Jun 08 '26
I think you should write a compiler in your chosen language (C for example) then as soon as possible try to self host. Either you succeed or you will realize it is a bad idea at that moment and you need a lot more features. In my opinion trying to self host your language gives you opportunity to realize what kind of features is your language lacking or to find bugs. So, I think you should try to self host quickly but should also abort quickly if you see it is forced and the language is not there yet. Do not sink time into trying to port a compiler to a language clearly not capable enough to host itself but you will not necessarily realize it without trying.
1
u/Ambitious_Fortune537 Jun 08 '26
Most self-hosted compilers start with a bootstrap compiler written in some other language (usually C or C++), which only implements a small subset of the target language just enough to compile simple programs. From there, the usual approach is iterative: you use that initial compiler to write a slightly more capable version of itself in the new language, gradually expanding features until it becomes expressive enough to compile its own full source code. The first version is typically very pragmatic and even a bit messy, since its only real purpose is to āget the language aliveā as quickly as possible, and concerns like elegance or completeness come later. Once self-hosting is achieved, itās common to rewrite or improve large parts of the compiler in its own language, but the key idea is that bootstrapping almost always comes before having a clean, fully self-hosted system.
1
u/EggplantExtra4946 Jun 08 '26
The bootstrap language doesn't need to be natively compiled, it can be compiled to bytecode for a simple VM that you wrote.
3
u/[deleted] Jun 08 '26
[removed] ā view removed comment