Skip to content

Commit 269b5f0

Browse files
mark-i-mJohnTitorchrissimpkins
authored
Reorganize the guide (#651)
* start on guide reorg * backend intro * part 2, 3, 4 intros * some missing files * Fix typos Co-Authored-By: Yuki Okushi <[email protected]> Co-Authored-By: Chris Simpkins <[email protected]> * add links from intro * rename compiler source chapter and add placeholder for overview * rename parts, mv rustc_driver to part 3, make syntax chapter * update part intros * add syntax ch intro * address review comments * Better overview chapter placeholder Co-Authored-By: Chris Simpkins <[email protected]> * fix link Co-authored-by: Yuki Okushi <[email protected]> Co-authored-by: Chris Simpkins <[email protected]>
1 parent c33f0c6 commit 269b5f0

13 files changed

+139
-60
lines changed

src/SUMMARY.md

+40-28
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@
3333
- ["Cleanup Crew" ICE-breakers](ice-breaker/cleanup-crew.md)
3434
- [LLVM ICE-breakers](ice-breaker/llvm.md)
3535
- [Licenses](./licenses.md)
36-
- [Part 2: How rustc works](./part-2-intro.md)
37-
- [High-level overview of the compiler source](./high-level-overview.md)
38-
- [The Rustc Driver and Interface](./rustc-driver.md)
39-
- [Rustdoc](./rustdoc.md)
40-
- [Ex: Type checking through `rustc_interface`](./rustc-driver-interacting-with-the-ast.md)
36+
37+
- [Part 2: High-level Compiler Architecture](./part-2-intro.md)
38+
- [Overview of the Compiler](./overview.md)
39+
- [The compiler source code](./compiler-src.md)
4140
- [Queries: demand-driven compilation](./query.md)
4241
- [The Query Evaluation Model in Detail](./queries/query-evaluation-model-in-detail.md)
4342
- [Incremental compilation](./queries/incremental-compilation.md)
@@ -46,15 +45,29 @@
4645
- [Profiling Queries](./queries/profiling.md)
4746
- [Salsa](./salsa.md)
4847
- [Memory Management in Rustc](./memory.md)
49-
- [Lexing and Parsing](./the-parser.md)
50-
- [`#[test]` Implementation](./test-implementation.md)
51-
- [Panic Implementation](./panic-implementation.md)
52-
- [Macro expansion](./macro-expansion.md)
53-
- [Name resolution](./name-resolution.md)
48+
49+
- [Part 3: Source Code Representations](./part-3-intro.md)
50+
- [The Rustc Driver and Interface](./rustc-driver.md)
51+
- [Rustdoc](./rustdoc.md)
52+
- [Ex: Type checking through `rustc_interface`](./rustc-driver-interacting-with-the-ast.md)
53+
- [Syntax and the AST](./syntax-intro.md)
54+
- [Lexing and Parsing](./the-parser.md)
55+
- [`#[test]` Implementation](./test-implementation.md)
56+
- [Panic Implementation](./panic-implementation.md)
57+
- [Macro expansion](./macro-expansion.md)
58+
- [Name resolution](./name-resolution.md)
59+
- [AST Validation](./ast-validation.md)
60+
- [Feature Gate Checking](./feature-gate-ck.md)
5461
- [The HIR (High-level IR)](./hir.md)
5562
- [Lowering AST to HIR](./lowering.md)
5663
- [Debugging](./hir-debugging.md)
64+
- [The MIR (Mid-level IR)](./mir/index.md)
65+
- [HAIR and MIR construction](./mir/construction.md)
66+
- [MIR visitor and traversal](./mir/visitor.md)
67+
- [MIR passes: getting the MIR for a function](./mir/passes.md)
5768
- [Closure expansion](./closure.md)
69+
70+
- [Part 4: Analysis](./part-4-intro.md)
5871
- [The `ty` module: representing types](./ty.md)
5972
- [Generics and substitutions](./generics.md)
6073
- [`TypeFolder` and `TypeFoldable`](./ty-fold.md)
@@ -82,12 +95,7 @@
8295
- [Method Lookup](./method-lookup.md)
8396
- [Variance](./variance.md)
8497
- [Opaque Types](./opaque-types-type-alias-impl-trait.md)
85-
- [The MIR (Mid-level IR)](./mir/index.md)
86-
- [MIR construction](./mir/construction.md)
87-
- [MIR visitor and traversal](./mir/visitor.md)
88-
- [MIR passes: getting the MIR for a function](./mir/passes.md)
89-
- [MIR optimizations](./mir/optimizations.md)
90-
- [Debugging](./mir/debugging.md)
98+
- [Pattern and Exhaustiveness Checking](./pat-exhaustive-checking.md)
9199
- [The borrow checker](./borrow_check.md)
92100
- [Tracking moves and initialization](./borrow_check/moves_and_initialization.md)
93101
- [Move paths](./borrow_check/moves_and_initialization/move_paths.md)
@@ -100,20 +108,24 @@
100108
- [Closure constraints](./borrow_check/region_inference/closure_constraints.md)
101109
- [Error reporting](./borrow_check/region_inference/error_reporting.md)
102110
- [Two-phase-borrows](./borrow_check/two_phase_borrows.md)
111+
- [Parameter Environments](./param_env.md)
112+
113+
- [Part 5: From MIR to binaries](./part-5-intro.md)
114+
- [The MIR (Mid-level IR)](./mir/index.md)
115+
- [MIR optimizations](./mir/optimizations.md)
116+
- [Debugging](./mir/debugging.md)
103117
- [Constant evaluation](./const-eval.md)
104118
- [miri const evaluator](./miri.md)
105-
- [Parameter Environments](./param_env.md)
106-
- [Compiler Backend](./backend/backend.md)
107-
- [Monomorphization](./backend/monomorph.md)
108-
- [Lowering MIR](./backend/lowering-mir.md)
109-
- [Code Generation](./backend/codegen.md)
110-
- [Updating LLVM](./backend/updating-llvm.md)
111-
- [Debugging LLVM](./backend/debugging.md)
112-
- [Backend Agnostic Codegen](./backend/backend-agnostic.md)
113-
- [Implicit Caller Location](./codegen/implicit-caller-location.md)
114-
- [Profile-guided Optimization](./profile-guided-optimization.md)
115-
- [Sanitizers Support](./sanitizers.md)
116-
- [Debugging Support in Rust Compiler](./debugging-support-in-rustc.md)
119+
- [Monomorphization](./backend/monomorph.md)
120+
- [Lowering MIR](./backend/lowering-mir.md)
121+
- [Code Generation](./backend/codegen.md)
122+
- [Updating LLVM](./backend/updating-llvm.md)
123+
- [Debugging LLVM](./backend/debugging.md)
124+
- [Backend Agnostic Codegen](./backend/backend-agnostic.md)
125+
- [Implicit Caller Location](./codegen/implicit-caller-location.md)
126+
- [Profile-guided Optimization](./profile-guided-optimization.md)
127+
- [Sanitizers Support](./sanitizers.md)
128+
- [Debugging Support in Rust Compiler](./debugging-support-in-rustc.md)
117129

118130
---
119131

src/about-this-guide.md

+23-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,29 @@
22

33
This guide is meant to help document how rustc – the Rust compiler –
44
works, as well as to help new contributors get involved in rustc
5-
development. It is not meant to replace code documentation – each
6-
chapter gives only high-level details, the kinds of things that
7-
(ideally) don't change frequently.
8-
9-
There are three parts to this guide. Part 1 contains information that should
10-
be useful no matter how you are contributing. Part 2 contains information
11-
about how the compiler works. Finally, there are some appendices at the
12-
end with useful reference information.
5+
development.
6+
7+
There are six parts to this guide:
8+
9+
1. [Contributing][p1]: Contains information that should be useful no matter how
10+
you are contributing, such as procedures for contribution, building the
11+
compiler, etc.
12+
2. [High-level Compiler Architecture][p2]: Discusses the high-level
13+
architecture of the compiler, especially the query system.
14+
3. [The Compiler Frontend][p3]: Discusses the compiler frontend and internal
15+
representations.
16+
4. [The Type System][p4]: Discusses the type system.
17+
5. [The Compiler Backend][p5]: Discusses the compiler backend, code generation,
18+
linking, and debug info.
19+
6. [Appendices][app] at the end with useful reference information. There are a
20+
few of these with different information, inluding a glossary.
21+
22+
[p1]: ./part-1-intro.md
23+
[p2]: ./part-2-intro.md
24+
[p3]: ./part-3-intro.md
25+
[p4]: ./part-4-intro.md
26+
[p5]: ./part-5-intro.md
27+
[app]: ./appendix/background.md
1328

1429
The guide itself is of course open-source as well, and the sources can
1530
be found at the [GitHub repository]. If you find any mistakes in the

src/ast-validation.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# AST Validation
2+
3+
AST validation is the process of checking various correctness properties about
4+
the AST after macro expansion.
5+
6+
**TODO**: write this chapter.
File renamed without changes.

src/feature-gate-ck.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Feature Gate Checking
2+
3+
**TODO**: this chapter

src/mir/construction.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# MIR construction
1+
# HAIR and MIR construction
22

33
The lowering of [HIR] to [MIR] occurs for the following (probably incomplete)
44
list of items:

src/overview.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Overview of the Compiler
2+
3+
Coming soon! Work is in progress on this chapter. See https://github.com/rust-lang/rustc-dev-guide/pull/633 for the source and the [project README](https://github.com/rust-lang/rustc-dev-guide) for local build instructions.

src/part-2-intro.md

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# Part 2: How rustc works
22

3-
This part of the guide describes how the compiler works. It goes through
4-
everything from high-level structure of the compiler to how each stage of
5-
compilation works.
3+
The remaining parts of this guide discuss how the compiler works. They go
4+
through everything from high-level structure of the compiler to how each stage
5+
of compilation works. They should be friendly to both readers interested in the
6+
end-to-end process of compilation _and_ readers interested in learning about a
7+
specific system they wish to contribute to. If anything is unclear, feel free
8+
to file an issue on the [rustc-dev-guide
9+
repo](https://github.com/rust-lang/rustc-dev-guide/issues) or contact the compiler
10+
team, as detailed in [this chapter from Part 1](./compiler-team.md).
611

7-
This section should be friendly to both readers interested in the end-to-end
8-
process of compilation _and_ readers interested in learning about a specific
9-
system they wish to contribute to. If anything is unclear, feel free to file
10-
an issue on the [rustc-dev-guide repo](https://github.com/rust-lang/rustc-dev-guide)
11-
or contact the compiler team, as detailed in [this chapter from Part
12-
1](./compiler-team.md).
12+
In this part, we will specifically look at the high-level architecture of the
13+
compiler. Specifically, will look at the query system, incremental compilation,
14+
and interning. These are three overarching design choices that impact the whole
15+
compiler.

src/part-3-intro.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Part 3: Source Code Representations
2+
3+
This part describes the process of taking raw source code from the user and
4+
transforming it into various forms that the compiler can work with easily.
5+
These are called intermediate representations.
6+
7+
This process starts with compiler understanding what the user has asked for:
8+
parsing the command line arguments given and determining what it is to compile.

src/part-4-intro.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Part 4: Analysis
2+
3+
This part discusses the many analyses that the compiler uses to check various
4+
properties of the code and to inform later stages. Typically, this is what people
5+
mean when they talk about "Rust's type system". This includes the
6+
representation, inference, and checking of types, the trait system, and the
7+
borrow checker. These analyses do not happen as one big pass or set of
8+
contiguous passes. Rather, they are spread out throughout various parts of the
9+
compilation process and use different intermediate representations. For example,
10+
type checking happens on the HIR, while borrow checking happens on the MIR.
11+
Nonetheless, for the sake of presentation, we will discuss all of these
12+
analyses in this part of the guide.
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
# The Compiler Backend
1+
# From MIR to binaries
22

33
All of the preceding chapters of this guide have one thing in common: we never
44
generated any executable machine code at all! With this chapter, all of that
55
changes.
66

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 usable by the backend. For rustc,
11-
this format is the MIR. The _backend_ refers to the parts of the compiler that
12-
turn rustc's MIR into actual executable code (e.g. an ELF or EXE binary) that
13-
can run on a processor. All of the previous chapters deal with rustc's
14-
frontend.
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.
1511

16-
rustc's backend does the following:
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?
1718

1819
0. First, we need to collect the set of things to generate code for. In
1920
particular, we need to find out which concrete types to substitute for
@@ -23,15 +24,15 @@ rustc's backend does the following:
2324
collecting all the concrete types is called _monomorphization collection_.
2425
1. Next, we need to actually lower the MIR to a codegen IR
2526
(usually LLVM IR) for each concrete type we collected.
26-
2. Finally, we need to invoke the codegen backend (e.g. LLVM or Cranelift),
27-
which runs a bunch of optimization passes, generates executable code, and
28-
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.
2930

3031
[codegen1]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/base/fn.codegen_crate.html
3132

3233
The code for codegen is actually a bit complex due to a few factors:
3334

34-
- 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
3536
backend code between them as possible, so a lot of it is generic over the
3637
codegen implementation. This means that there are often a lot of layers of
3738
abstraction.
@@ -48,3 +49,4 @@ crate contains code specific to LLVM codegen.
4849
At a very high level, the entry point is
4950
[`rustc_codegen_ssa::base::codegen_crate`][codegen1]. This function starts the
5051
process discussed in the rest of this chapter.
52+

src/pat-exhaustive-checking.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Pattern and Exhaustiveness Checking
2+
3+
In Rust, pattern matching and bindings have a few very helpful properties. The
4+
compiler will check that bindings are irrefutable when made and that match arms
5+
are exhaustive.
6+
7+
**TODO**: write this chapter.

src/syntax-intro.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Syntax and the AST
2+
3+
Working directly with source code is very inconvenient and error-prone. Thus,
4+
before we do anything else, we convert raw source code into an AST. It turns
5+
out that doing even this involves a lot of work, including lexing, parsing,
6+
macro expansion, name resolution, conditional compilation, feature-gate
7+
checking, and validation of the AST. In this chapter, we take a look at all
8+
of these steps.

0 commit comments

Comments
 (0)