4
$\begingroup$

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?

$\endgroup$
  • $\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
5
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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.

$\endgroup$
  • $\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

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.