I understand that a self-hosting compiler is a compiler which can compile the code of the language that it is written in into different language but I don't understand why we would want to do that. What are the benefits (and drawbacks) of a compiler which is self-hosting?

  • $\begingroup$ Are you sure you haven't mixed bootstraping with transpiler? $\endgroup$ – Evil May 1 '16 at 17:11
  • $\begingroup$ I don't think so. A self-hosting compiler takes as input the language that it is written in, right? $\endgroup$ – Haych May 1 '16 at 17:13
  • $\begingroup$ Right, it compiles itself. What about the part of source-to-source compilation? $\endgroup$ – Evil May 1 '16 at 17:27
  • $\begingroup$ What do you mean source-to-source compilation? $\endgroup$ – Haych May 1 '16 at 18:44
  • 1
    $\begingroup$ "self-hosting compiler is a compiler which can compile it's source language into another" - this is the unclear part to me. $\endgroup$ – Evil May 1 '16 at 18:45

There's no direct technical benefit for a compiler to be self-hosting. If anything, it makes things more complicated, because you have to have a working compiler before you can compile the compiler — the bootstrapping problem. (Solved either by having another implementation of the same language — often an interpreter — or by keeping around binaries of a previous compiler version.)

Compiler writers often like the language that they're writing a compiler for. This is the primary reason why many compilers are self-hosting.

Having a compiler that compiles itself does have an indirect technical benefit: it's a litmus test. A compiler is a complex program. If the language is comfortable enough to write a program in, that's a sign that it's comfortable enough for a large class of programs. If the compiler can compile itself without bugs, that's a sign that it's in a good enough shape to compile other programs with confidence.


Think about how the first compiler got written, for the first programming language supported by some platform (probably a C compiler). In other words, think about writing a C compiler for a platform where there is no other compiler available for any other language.

Your only option is to write the C compiler in assembly language. But writing an entire compiler in assembly language would be incredibly painful. You'd much rather write the compiler in a high-level language than in assembly (for the same reason you'd rather write pretty much any program in a high-level language).

If you can write a self-hosting compiler, then you've achieved that. The usual way to construct a self-hosting compiler is through bootstrapping. Wikipedia has articles on both.

  • $\begingroup$ An additional reason: to extend the "reach" of the language. This was the motivation for making Clojurescript self-compiling, google "Bootstrapped-ClojureScript-FAQ Clojurescript FAQ". $\endgroup$ – user48832 May 1 '16 at 20:21
  • $\begingroup$ I think I am not understanding something obvious here. How have we achieve this? I don't understand the link between the code a compiler is written in and the input code of that compiler. $\endgroup$ – Haych May 2 '16 at 8:37

The first assembly language compilers ("assemblers") were written in raw machine code. Once you got the program "breathing" enough to process statements, you could build a small piece of it using V1 of the assembler. Once the assembler accepted enough of the language to be useful, you used it to build V2 to add more features, and then V3, and so on. Each subsequent development providing more functionality. Since there were no other languages, the first assemblers had to be self-hosting.

Now, once someone wanted to write a new programming language, they did the same thing, only they'd write it in Assembler. Once the minimal language was working, you could add more features, each language Version N compiler being built by a version N-1 compiler.

Alternatively, if there was another high-level language available, the compiler can be written using that. Nicklaus Wirth wrote the first Pascal Compiler in Fortran, a language for which the CDC mainframe has lots of support.

Once enough of the target language is present in the proposed compiler, further development can use the compiler itself. On the other hand, they may find the original language easier to write a compiler using, or there may be resource limitations which means you can't write the compiler in its own language. The Gnu Compiler Collection provides "frontends" that translate a particular language into the symbols that the backend uses, which means any frontend to compile some other language using the GCC toolchain pretty much has to be written using c or c++, depending on what the backend uses.

The advantage to self-hosting is it reduces the number of languages the compiler writers have to know to two: the machine's assembly language (or object code) and the language of the compiler. If the compiler is not-self hosting, then they also need to know a third language: the language the compiler source is written in.

The only disadvantage to self-hosting I can think of is if the computer you're using does not have a compiler for the language you're writing it for. On the other hand, that may be the reason they wrote the compiler in the first place.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.