@@ -4,17 +4,17 @@ All of the preceding chapters of this guide have one thing in common: we never
4
4
generated any executable machine code at all! With this chapter, all of that
5
5
changes.
6
6
7
- It's often useful to think of compilers as being composed of a _ frontend _ and a
8
- _ backend _ (though in rustc, there's not a sharp line between frontend and
9
- backend). The _ frontend _ is responsible for taking raw source code, checking it
10
- for correctness, and getting it into a format from which we can generate code.
11
- For rustc, we typically consider MIR to be this format (though arguably, one
12
- could consider LLVM IR to be this format). The _ backend _ refers to the parts
13
- of the compiler that produce actual executable code (e.g. an ELF or EXE binary)
14
- that can run on a processor. All of the previous chapters deal with rustc's
15
- "frontend ".
16
-
17
- rustc's "backend" does the following:
7
+ So far, we've shown how the compiler can take raw source code in text format
8
+ and transform it into MIR. We have also shown how the compiler does various
9
+ analyses on the code to detect things like type or lifetime errors. Now, we
10
+ will finally take the MIR and produce some executable machine code.
11
+
12
+ > NOTE: This part of a compiler is often called the _ backend _ the term is a bit
13
+ > overloaded because in the compiler source, it usually refers to the "codegen
14
+ > backend" (i.e. LLVM or Cranelift). Usually, when you see the word "backend"
15
+ > in this part, we are refering to the "codegen backend ".
16
+
17
+ So what do we need to do?
18
18
19
19
0 . First, we need to collect the set of things to generate code for. In
20
20
particular, we need to find out which concrete types to substitute for
@@ -24,15 +24,15 @@ rustc's "backend" does the following:
24
24
collecting all the concrete types is called _ monomorphization collection_ .
25
25
1 . Next, we need to actually lower the MIR to a codegen IR
26
26
(usually LLVM IR) for each concrete type we collected.
27
- 2 . Finally, we need to invoke the codegen backend (e.g. LLVM or Cranelift),
28
- which runs a bunch of optimization passes, generates executable code, and
29
- links together an executable binary.
27
+ 2 . Finally, we need to invoke LLVM or Cranelift, which runs a bunch of
28
+ optimization passes, generates executable code, and links together an
29
+ executable binary.
30
30
31
31
[ codegen1 ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/base/fn.codegen_crate.html
32
32
33
33
The code for codegen is actually a bit complex due to a few factors:
34
34
35
- - Support for multiple backends (LLVM and Cranelift). We try to share as much
35
+ - Support for multiple codegen backends (LLVM and Cranelift). We try to share as much
36
36
backend code between them as possible, so a lot of it is generic over the
37
37
codegen implementation. This means that there are often a lot of layers of
38
38
abstraction.
0 commit comments