4 ms·
> 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
by 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.