Search code examples
compiler-constructionprogramming-languageslow-levelmachine-code

Does a compiler that compiles to machine code need to be written in assembly?


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


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?