Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a practical reason for a compiler to be self-hosting?

If your compiler in the bootstrap language works well and is maintainable, why change it? Go, for example, re-wrote its compiler to be self-hosting in version 1.5, which caused compile times to become much slower: an obvious detriment when Go's goal is fast compilation.

like image 606
EMBLEM Avatar asked Mar 13 '23 16:03

EMBLEM


1 Answers

One practical reason is community. The people that program in your language might prefer to program in the compiler if it's written in the same language. If my compiler is in Fortran/COBOL and it generates Go I'm unlikely to attract Go developers to the compiler.

Another one is build chain ie dependencies. If you have a compiler written in one language and generating another you have two sets of tests to write when you could settle for one. This also leads to reducing the barrier to entry ie you don't need developers to know multiple tool chains etc. Knowing two languages well enough to be a competent compiler writer in both is hard work and narrows your potential audience for help. Getting help is very important for most open source projects and anything that increases your potential developer base is a definite practical advantage.

You could also list testing as an added benefit. If you've written a self hosting compiler there's a lot of things that the language needs to make it relatively easy (as opposed to pulling teeth) to self host ie File IO, String manipulation, Symbol tables, trees and lists etc. Obviously you can survive without all of these but it starts to make writing the compiler a lot more difficult. This sort of sits in the eat your own dog food camp.

It's considered a Rite of Passage but I don't consider that to be a very practical reason unless you can prove that it attracts developers or some other reason to do it, perhaps feeling good about the achievement is a practical benefit ie you are less likely to abandon it.

There's an interesting topic on it here...

https://softwareengineering.stackexchange.com/questions/263651/why-are-self-hosting-compilers-considered-a-rite-of-passage-for-new-languages

For some concrete reasons have a read of Rob Pikes slides on why they moved the Go compiler to Go instead of C. The conclusions in the slides are:

  1. Getting rid of C was a huge advance for the project.
  2. Code is cleaner, testable, profilable, easier to work on.
  3. New unified tool chain reduces code size, increases maintainability. Flexible tool chain, portability still paramount.

Depending on the language your moving from and to the benefits might be different.

like image 139
Harry Avatar answered Apr 07 '23 06:04

Harry