patternModerate
Why would we want a self-hosting compiler?
Viewed 0 times
wantwhyhostingwouldcompilerself
Problem
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?
Solution
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.
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.
Context
StackExchange Computer Science Q#56897, answer score: 10
Revisions (0)
No revisions yet.