Skip to content

Commit e7b1ed0

Browse files
authored
Merge pull request #18 from nikomatsakis/master
remove chap-NNN labels, move some content from rustc
2 parents 66973d5 + 761119e commit e7b1ed0

21 files changed

+822
-19
lines changed

src/SUMMARY.md

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
# Summary
22

3-
- [How to build the compiler and run what you built](./chap-010-how-to-build-and-run.md)
4-
- [Using the compiler testing framework](./chap-020-running-tests.md)
5-
- [Walkthrough: a typical contribution](./chap-030-walkthrough.md)
6-
- [Conventions used in the compiler](./chap-040-compiler-conventions.md)
7-
- [The parser](./chap-050-the-parser.md)
8-
- [Macro expansion](./chap-060-macro-expansion.md)
9-
- [Name resolution](./chap-070-name-resolution.md)
10-
- [HIR lowering](./chap-080-hir-lowering.md)
11-
- [Representing types (`ty` module in depth)](./chap-090-ty.md)
12-
- [Type inference](./chap-100-type-inference.md)
13-
- [Trait resolution](./chap-110-trait-resolution.md)
14-
- [Type checking](./chap-120-type-checking.md)
15-
- [MIR construction](./chap-130-mir-construction.md)
16-
- [MIR borrowck](./chap-140-mir-borrowck.md)
17-
- [MIR optimizations](./chap-150-mir-optimizations.md)
18-
- [trans: generating LLVM IR](./chap-160-trans.md)
3+
- [About this guide](./about-this-guide.md)
4+
- [How to build the compiler and run what you built](./how-to-build-and-run.md)
5+
- [Using the compiler testing framework](./running-tests.md)
6+
- [Walkthrough: a typical contribution](./walkthrough.md)
7+
- [High-level overview of the compiler source](./high-level-overview.md)
8+
- [The parser](./the-parser.md)
9+
- [Macro expansion](./macro-expansion.md)
10+
- [Name resolution](./name-resolution.md)
11+
- [HIR lowering](./hir-lowering.md)
12+
- [The `ty` module: representing types](./ty.md)
13+
- [Type inference](./type-inference.md)
14+
- [Trait resolution](./trait-resolution.md)
15+
- [Type checking](./type-checking.md)
16+
- [MIR construction](./mir-construction.md)
17+
- [MIR borrowck](./mir-borrowck.md)
18+
- [MIR optimizations](./mir-optimizations.md)
19+
- [trans: generating LLVM IR](./trans.md)
1920
- [Glossary](./glossary.md)

src/about-this-guide.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# About this guide
2+
3+
This guide is meant to help document how rustc -- the Rust compiler --
4+
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+
The guide itself is of course open source as well, and the sources can
10+
be found at [the GitHub repository]. If you find any mistakes in the
11+
guide, please file an issue about it -- or, even better, open a PR
12+
with a correction!
13+
14+
[the GitHub repository]: https://github.com/rust-lang-nursery/rustc-guide/

src/chap-040-compiler-conventions.md

-1
This file was deleted.

src/chap-090-ty.md

-1
This file was deleted.

src/chap-110-trait-resolution.md

-1
This file was deleted.

src/high-level-overview.md

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# High-level overview of the compiler source
2+
3+
## Crate structure
4+
5+
The main Rust repository consists of a `src` directory, under which
6+
there live many crates. These crates contain the sources for the
7+
standard library and the compiler. This document, of course, focuses
8+
on the latter.
9+
10+
Rustc consists of a number of crates, including `syntax`,
11+
`rustc`, `rustc_back`, `rustc_trans`, `rustc_driver`, and
12+
many more. The source for each crate can be found in a directory
13+
like `src/libXXX`, where `XXX` is the crate name.
14+
15+
(NB. The names and divisions of these crates are not set in
16+
stone and may change over time -- for the time being, we tend towards
17+
a finer-grained division to help with compilation time, though as
18+
incremental improves that may change.)
19+
20+
The dependency structure of these crates is roughly a diamond:
21+
22+
```
23+
rustc_driver
24+
/ | \
25+
/ | \
26+
/ | \
27+
/ v \
28+
rustc_trans rustc_borrowck ... rustc_metadata
29+
\ | /
30+
\ | /
31+
\ | /
32+
\ v /
33+
rustc
34+
|
35+
v
36+
syntax
37+
/ \
38+
/ \
39+
syntax_pos syntax_ext
40+
```
41+
42+
The `rustc_driver` crate, at the top of this lattice, is effectively
43+
the "main" function for the rust compiler. It doesn't have much "real
44+
code", but instead ties together all of the code defined in the other
45+
crates and defines the overall flow of execution. (As we transition
46+
more and more to the [query model](ty/maps/README.md), however, the
47+
"flow" of compilation is becoming less centrally defined.)
48+
49+
At the other extreme, the `rustc` crate defines the common and
50+
pervasive data structures that all the rest of the compiler uses
51+
(e.g., how to represent types, traits, and the program itself). It
52+
also contains some amount of the compiler itself, although that is
53+
relatively limited.
54+
55+
Finally, all the crates in the bulge in the middle define the bulk of
56+
the compiler -- they all depend on `rustc`, so that they can make use
57+
of the various types defined there, and they export public routines
58+
that `rustc_driver` will invoke as needed (more and more, what these
59+
crates export are "query definitions", but those are covered later
60+
on).
61+
62+
Below `rustc` lie various crates that make up the parser and error
63+
reporting mechanism. For historical reasons, these crates do not have
64+
the `rustc_` prefix, but they are really just as much an internal part
65+
of the compiler and not intended to be stable (though they do wind up
66+
getting used by some crates in the wild; a practice we hope to
67+
gradually phase out).
68+
69+
Each crate has a `README.md` file that describes, at a high-level,
70+
what it contains, and tries to give some kind of explanation (some
71+
better than others).
72+
73+
## The main stages of compilation
74+
75+
The Rust compiler is in a bit of transition right now. It used to be a
76+
purely "pass-based" compiler, where we ran a number of passes over the
77+
entire program, and each did a particular check of transformation. We
78+
are gradually replacing this pass-based code with an alternative setup
79+
based on on-demand **queries**. In the query-model, we work backwards,
80+
executing a *query* that expresses our ultimate goal (e.g., "compile
81+
this crate"). This query in turn may make other queries (e.g., "get me
82+
a list of all modules in the crate"). Those queries make other queries
83+
that ultimately bottom out in the base operations, like parsing the
84+
input, running the type-checker, and so forth. This on-demand model
85+
permits us to do exciting things like only do the minimal amount of
86+
work needed to type-check a single function. It also helps with
87+
incremental compilation. (For details on defining queries, check out
88+
`src/librustc/ty/maps/README.md`.)
89+
90+
Regardless of the general setup, the basic operations that the
91+
compiler must perform are the same. The only thing that changes is
92+
whether these operations are invoked front-to-back, or on demand. In
93+
order to compile a Rust crate, these are the general steps that we
94+
take:
95+
96+
1. **Parsing input**
97+
- this processes the `.rs` files and produces the AST ("abstract syntax tree")
98+
- the AST is defined in `syntax/ast.rs`. It is intended to match the lexical
99+
syntax of the Rust language quite closely.
100+
2. **Name resolution, macro expansion, and configuration**
101+
- once parsing is complete, we process the AST recursively, resolving paths
102+
and expanding macros. This same process also processes `#[cfg]` nodes, and hence
103+
may strip things out of the AST as well.
104+
3. **Lowering to HIR**
105+
- Once name resolution completes, we convert the AST into the HIR,
106+
or "high-level IR". The HIR is defined in `src/librustc/hir/`; that module also includes
107+
the lowering code.
108+
- The HIR is a lightly desugared variant of the AST. It is more processed than the
109+
AST and more suitable for the analyses that follow. It is **not** required to match
110+
the syntax of the Rust language.
111+
- As a simple example, in the **AST**, we preserve the parentheses
112+
that the user wrote, so `((1 + 2) + 3)` and `1 + 2 + 3` parse
113+
into distinct trees, even though they are equivalent. In the
114+
HIR, however, parentheses nodes are removed, and those two
115+
expressions are represented in the same way.
116+
3. **Type-checking and subsequent analyses**
117+
- An important step in processing the HIR is to perform type
118+
checking. This process assigns types to every HIR expression,
119+
for example, and also is responsible for resolving some
120+
"type-dependent" paths, such as field accesses (`x.f` -- we
121+
can't know what field `f` is being accessed until we know the
122+
type of `x`) and associated type references (`T::Item` -- we
123+
can't know what type `Item` is until we know what `T` is).
124+
- Type checking creates "side-tables" (`TypeckTables`) that include
125+
the types of expressions, the way to resolve methods, and so forth.
126+
- After type-checking, we can do other analyses, such as privacy checking.
127+
4. **Lowering to MIR and post-processing**
128+
- Once type-checking is done, we can lower the HIR into MIR ("middle IR"), which
129+
is a **very** desugared version of Rust, well suited to the borrowck but also
130+
certain high-level optimizations.
131+
5. **Translation to LLVM and LLVM optimizations**
132+
- From MIR, we can produce LLVM IR.
133+
- LLVM then runs its various optimizations, which produces a number of `.o` files
134+
(one for each "codegen unit").
135+
6. **Linking**
136+
- Finally, those `.o` files are linked together.
137+
138+
139+
140+
141+
The first thing you may wonder if
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)