Question

Excluding compilers that convert one high-level language to another, does any compiler that compiles to machine code need to be written in assembly?

Was it helpful?

Solution

The source code for a compiler does not need to be written in assembly. For example, (a good portion) of the CPython compiler (well, technically interpreter) is written in C: http://en.wikipedia.org/wiki/Cpython

In the very beginning, before there were compilers, the very first compiler did have to be written in assembly. But then someone used this compiler to compile their own compiler. Then someone else used this compiler to compile their own compiler. And so on and so forth.

This brings up the concept of "bootstrapping". A bootstrapping compiler is one that is written in the language it intends to compile. The clang compiler can compile C++ code, but the compiler itself is written in C++! How does this work? Well the very first clang compiler was compiled by a different compiler (g++ probably). After the clang compiler was mature enough, it was able to compile its own code. Now, any changes made to the clang compiler can be recompiled by the clang compiler into another clang compiler! Neat, huh?

OTHER TIPS

A compiler is not a special, magical program. If you squint at it, a compiler is just a program that turns one text file into another text file (well, okay, a binary file).

There is only one constraint about what language a program can be implemented in. If you're writing a program in a language, that language has to already have a compiler or interpreter implemented. Therefore, the first compiler/interpreter for language X cannot be written in X. (If you want to be able to use it, at least.) But this isn't special to compilers/interpreters; you can't usefully write anything in X until someone has implemented X.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top