4 ms·
I'm surprised this wasn't done earlier. It's actually a quite common practice for a language to eventually write a compiler for it in said language; this is cal
by stingraycharles 14y ago
I'm surprised this wasn't done earlier. It's actually a quite common practice for a language to eventually write a compiler for it in said language; this is called bootstrapping a compiler [1], and makes the language "self-hosted".
C is a notorious language in this case, and is the closest to the real chick-or-the-egg problem this is: how do you write a C compiler without a C compiler? C is often the lowest-level language compilers are written in, so in order to bootstrap a C compiler for a specific architecture / OS, you need to write the entire compiler in the target language (assembly).
You can still see the bootstrapping part well in GCC: when you compile a new version of GCC, you first compile the compiler using your old version (bootstrapping), and then re-compile the entire codebase again using your new version.
Nowadays, "real" compiler bootstrapping using assembly is hardly ever done. You can simply create a new architecture target for GCC in a different environment (for example, Linux), and compile GCC with your new environment as a target architecture on Linux. You then install this brand new compiler on your brand new OS, et voila: you skipped the nasty bit and have a working compiler for your new OS.
[1] http://en.wikipedia.org/wiki/Bootstrapping_(compilers) http://en.wikipedia.org/wiki/Bootstrapping_(compilers)
- lucian303 14y agoWhich you can then use to recompile your OS easily (FreeBSD, some Linux based OS's) and tune it to a specific machine / configuration. That's why people like Kernighan and Ritchie (RIP) are so well respected. They wrote C to the target language first (asm) then rewrote C in C once they had a good enough compiler. Considering that C is the cornerstone of UNIX and modern variants I'd say this is no small feat. C will be around for a long time after many of the current languages, domain specific or not, become just another wikipedia entry.
- pm215 14y agoActually (according to Ritchie's description of the history of C at http://cm.bell-labs.com/cm/cs/who/dmr/chist.html http://cm.bell-labs.com/cm/cs/who/dmr/chist.html) they never wrote a C compiler in asm. Instead it was a gradual process of evolution, so the first C compiler would have been written in B; and the B compiler was itself written in B and self-hosting, bootstrapped from a B compiler written in a thing I've never heard of called TMG. None of which is to downplay their achievements at all (I still write C full-time); but it's interesting that even in a period of computing history where operating systems were generally written in assembly language, compilers were bootstrapped from other high level languages.
- hnriot 14y agoCalling B a high level language is something of a stretch.
- rbanffy 14y agoNowadays, the same can be said of C.
- lucian303 14y agoNowadays? C was never a high level language except and wasn't designed to be. Mid-level yes. As in just above asm.
- rbanffy 14y agoThere was a time being above asm, having non-register data types, functions and libraries was considered being a high-level language. I'd consider C higher level than FORTRAN 77, for instance. You just need to be old enough ;-)
- lucian303 14y agoYes, I forgot about B. :) "Ritchie wrote a compiler using TMG which produced machine code." vs Ken Thompson's B compiler that produced threaded code.
- saraid216 14y ago> C will be around for a long time after many of the current languages, domain specific or not, become just another wikipedia entry. Off-topic, but this is a rather amusing benchmark for irrelevance. I suspect that deletionism would not permit it to be true, either.
- ambrop7 14y ago> how do you write a C compiler without a C compiler? C is often the lowest-level language compilers are written in, so in order to bootstrap a C compiler for a specific architecture / OS, you need to write the entire compiler in the target language (assembly). Nope, you probably don't do that. You use a computer (host) where you already have a C compiler, and you compile the new compiler (which you wrote/ported). Then you run this new compiler, on the host, to generate executable code for the new architecture (target), i.e. cross-compile. You can use this cross-compiler to (again) compile your ported compiler, but to run on the target architecture. You end up with a native compiler for your new architecture. Though, there is no essential need to have a native compiler for an architecture. For example, you can program an AVR microcontroller in C without running any C compilers on the AVR itself.
- stingraycharles 14y ago> Nope, you probably don't do that. You use a computer (host) where you already have a C compiler, and you compile the new compiler (which you wrote/ported). Then you run this new compiler, on the host, to generate executable code for the new architecture (target), i.e. cross-compile. You can use this cross-compiler to (again) compile your ported compiler, but to run on the target architecture. You end up with a native compiler for your new architecture. Ah, yes, I tried to explain exactly that in the last paragraph I wrote. Is what you're describing different from what I described there?
- ambrop7 14y agoSorry, I missed that, was distracted by your not-really-correct statement above :)
- stingraycharles 14y agoMy mistake. I was trying to illustrate the more theoretical problem of bootstrapping a compiler without a compiler, since I personally find that fascinating. These problems were really solved years ago, and their only place nowadays is in an educational context.
- DanWaterworth 14y agoIf I wanted to create a C compiler in C as quickly as possible and had no other tools except for an assembler, I'd probably write an interpreter for C in assembly first rather than a compiler. Then I'd write a compiler for C in C and then compile the compiler by running the compiler under the interpreter.
- hnriot 14y agoThe implication here being that writing an interpreter is easier? I've found it easier to write a compiler.
- gizmo686 14y agoThis isn't exactly bootstrapping a compiler. The concept there is that in the end you do not need the foreign compiler. In this case, you still need the native implementation to run the php implementation, giving it much less practicle purpose than bootsrapping compiled languages. From a theoratical perspective, this is equivelent to bootstrapping, as it is not an intrinsic property of PHP that it does not get compiled.