11
$\begingroup$

The article "Reflections on Trusting Trust", published in 1984, shows how it's possible to inject a vulnerability into a self-hosting compiler and cover up your tracks. However, self-hosting compilers are not uncommon.

The Swift core team at one point explicitly rejected rewriting the compiler in Swift, but they went back on it a few years later. Zig has a bootstrapping process involving a multi-stage compiler to avoid the trust issue that even involves rebuilding llvm from source, but most of the compiler is still written in Zig, so clearly there's want for self-hosting. Other examples of self-hosting compilers include TypeScript and GCC.

Why are self-hosting languages so popular? What advantages are there over writing a compiler in a different language?

$\endgroup$
1

3 Answers 3

8
$\begingroup$

The claimed benefits are:

  1. By building the compiler in the language, it is tested and gets a strong feedback loop into the language design.
  2. The community will be more enthusiastic in wanting to contribute to your compiler if it's self-hosted.

In practice, these points don't hold up to scrutiny:

  1. A compiler is a very specialized piece of software. Thus, if your main feedback for your language is your compiler then unless the language is geared towards writing compilers, you have the wrong kind of feedback.
  2. Unless your community is large in the first place, you're unlikely to have people help you out anyway, so a large community comes first, which is the actual tricky part. I've encountered a lot of language designers with self-hosted languages, they didn't automatically have communities helping out.

Furthermore there are strong drawbacks:

  1. A bootstrap process is required, often meaning that you have the language written in 0.x, that then builds the compiler of 0.x+n. Bootstrapping adds another layer of complexity to the toolchain.
  2. When self-hosting before 1.0, this creates a natural inertia against major changes to the language (regardless of how necessary), which in turn is not beneficial for the language's overall design.
  3. If it is early in the design of the compiler, it is harder for contributors to help, as they need to learn about your language at a point where there is relatively little written about the language.

Bootstrapping in itself doesn't need to be bad, but doing it before the language has matured to 1.0 is in general a bad idea. There are exceptions like Zig1 that made it work, but few languages have the luxury of a large community ready to contribute and maintain more than one compiler at the same time.


[1] Also, Zig's bootstrapping was further motivated by architectural problems with the original C++ codebase. So bootstrapping was not the primary goal, but rather this was addressing a need to rewrite the compiler from scratch. So bootstrapping was more of a side-effect of the rewrite.

$\endgroup$
6
$\begingroup$

Some of the reason for self-hosting a compiler is marketing: it shows that your language has reached a level of maturity; the language is capable of expressing a significant piece of software; and the language's implementation is "complete" enough to compile a significant piece of software and have it work. A compiler which can compile itself must work well and not be missing any "major" features. And unlike many other marketing claims, having a self-hosted compiler is an objective claim that's relatively easy to independently verify.

Another reason is to encourage your language's community to contribute improvements to the compiler. If it is written in a different language, then contributors must know that language well enough to write in it, in addition to knowing your language well enough to work on its implementation. If the compiler is written in the same language it compiles, relatively more people will be able to contribute to the project.

$\endgroup$
1
$\begingroup$

Just to add another point, writing a compiler in the same language is an example of the concept of eating your own dog food. This is where you use your own product to demonstrate its merits.

Self hosting allows you to test the language with a non-trival use case which can make it easy to spot issues and iron out problem areas before others use it for their own programs.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .