All of the preceding chapters of this guide have one thing in common: we never generated any executable machine code at all! With this chapter, all of that changes.
So far, we've shown how the compiler can take raw source code in text format and transform it into MIR. We have also shown how the compiler does various analyses on the code to detect things like type or lifetime errors. Now, we will finally take the MIR and produce some executable machine code.
NOTE: This part of a compiler is often called the backend. The term is a bit overloaded because in the compiler source, it usually refers to the “codegen backend” (i.e. LLVM, Cranelift, or GCC). Usually, when you see the word “backend” in this part, we are referring to the “codegen backend”.
So what do we need to do?
The code for codegen is actually a bit complex due to a few factors:
Generally, the rustc_codegen_ssa
crate contains backend-agnostic code, while the rustc_codegen_llvm
crate contains code specific to LLVM codegen.
At a very high level, the entry point is rustc_codegen_ssa::base::codegen_crate
. This function starts the process discussed in the rest of this chapter.