1
+ <!--
1
2
# Appendix B: Background topics
3
+ -->
2
4
5
+ # 附录 B:背景话题
6
+
7
+ <!--
3
8
This section covers a numbers of common compiler terms that arise in
4
9
this guide. We try to give the general definition while providing some
5
10
Rust-specific context.
11
+ -->
12
+
13
+ 本节涵盖了本指南中出现的常见编译器术语。我们会在某些特定于 Rust
14
+ 的上下文中给出这些术语的一般定义。
6
15
7
16
<a name =" cfg " ></a >
8
17
18
+ <!--
9
19
## What is a control-flow graph?
20
+ -->
21
+
22
+ ## 什么是控制流图?
10
23
24
+ <!--
11
25
A control-flow graph is a common term from compilers. If you've ever
12
26
used a flow-chart, then the concept of a control-flow graph will be
13
27
pretty familiar to you. It's a representation of your program that
14
28
exposes the underlying control flow in a very clear way.
29
+ -->
15
30
31
+ 控制流图(control-flow graph)是编译器中常见的术语。如果你曾使用过流程图,
32
+ 那么控制流图的概念对你来说会很熟悉。它是程序的一种表示方式,
33
+ 能够以非常清晰的方式展现底层控制流。
34
+
35
+ <!--
16
36
A control-flow graph is structured as a set of **basic blocks**
17
37
connected by edges. The key idea of a basic block is that it is a set
18
38
of statements that execute "together" – that is, whenever you branch
19
39
to a basic block, you start at the first statement and then execute
20
40
all the remainder. Only at the end of the block is there the
21
41
possibility of branching to more than one place (in MIR, we call that
22
42
final statement the **terminator**):
43
+ -->
44
+
45
+ 控制流图由以边相连的一组** 基本块(basic block)** 构成。基本块的主要概念是
46
+ 一组「一起」执行的语句,也就是说,只要你的分支跳到了基本块,
47
+ 它就会从头到尾依次执行所有的语句。只有到基本块的最后才有可能分支到更多地方
48
+ (在 MIR 中,我们将最后一条语句称作** 终止句(terminator)** ):
23
49
24
50
``` mir
25
51
bb0 : {
@@ -31,8 +57,12 @@ bb0: {
31
57
}
32
58
```
33
59
60
+ <!--
34
61
Many expressions that you are used to in Rust compile down to multiple
35
62
basic blocks. For example, consider an if statement:
63
+ -->
64
+
65
+ 你在 Rust 中使用的很多表达式都会编译成多个基本块。例如,考虑以下 if 语句:
36
66
37
67
``` rust,ignore
38
68
a = 1;
@@ -44,7 +74,11 @@ if some_variable {
44
74
d = 1;
45
75
```
46
76
77
+ <!--
47
78
This would compile into four basic blocks:
79
+ -->
80
+
81
+ 它会被编译成四个基本块
48
82
49
83
``` mir
50
84
BB0 : {
@@ -68,39 +102,76 @@ BB3: {
68
102
}
69
103
```
70
104
105
+ <!--
71
106
When using a control-flow graph, a loop simply appears as a cycle in
72
107
the graph, and the `break` keyword translates into a path out of that
73
108
cycle.
109
+ -->
110
+
111
+ 在使用控制流图时,循环会简单地作为一个图中的环路出现,而 ` break `
112
+ 关键字则会被翻译成跳出此环路的一条路径。
74
113
75
114
<a name =" dataflow " ></a >
76
115
116
+ <!--
77
117
## What is a dataflow analysis?
118
+ -->
78
119
120
+ ## 什么是数据流分析?
121
+
122
+ <!--
79
123
[*Static Program Analysis*](https://cs.au.dk/~amoeller/spa/) by Anders Møller
80
124
and Michael I. Schwartzbach is an incredible resource!
125
+ -->
126
+
127
+ [ ** 静态程序分析(Static Program Analysis)** ] ( https://cs.au.dk/~amoeller/spa/ ) ,
128
+ 作者 Anders Møller 和 Michael I. Schwartzbach,它是一个绝佳的资源。
81
129
82
130
* to be written*
83
131
84
132
<a name =" quantified " ></a >
85
133
134
+ <!--
86
135
## What is "universally quantified"? What about "existentially quantified"?
136
+ -->
137
+
138
+ ## 什么是「全称量化」?「存在量化」呢?
87
139
88
140
* to be written*
89
141
90
142
<a name =" variance " ></a >
91
143
144
+ <!--
92
145
## What is co- and contra-variance?
146
+ -->
147
+
148
+ ## 什么是协变和逆变?
93
149
150
+ <!--
94
151
Check out the subtyping chapter from the
95
152
[Rust Nomicon](https://doc.rust-lang.org/nomicon/subtyping.html).
153
+ -->
154
+
155
+ 详见 [ Rust 秘典] ( https://doc.rust-lang.org/nomicon/subtyping.html )
156
+ 中的子定型(Subtyping)一章。
96
157
158
+ <!--
97
159
See the [variance](../variance.html) chapter of this guide for more info on how
98
160
the type checker handles variance.
161
+ -->
162
+
163
+ 关于类型检查器如何处理型变的更多信息见本指南的
164
+ [ 型变(variance)] ( ../variance.html ) 一章。
99
165
100
166
<a name =" free-vs-bound " ></a >
101
167
168
+ <!--
102
169
## What is a "free region" or a "free variable"? What about "bound region"?
170
+ -->
171
+
172
+ ## 什么是「自由生存域」和「自由变量」?「约束生存域」呢?
103
173
174
+ <!--
104
175
Let's describe the concepts of free vs bound in terms of program
105
176
variables, since that's the thing we're most familiar with.
106
177
@@ -115,14 +186,38 @@ variables, since that's the thing we're most familiar with.
115
186
refer to local variables that are defined *outside* of the
116
187
expression. We say that those variables **appear free** in the
117
188
expression (i.e., they are **free**, not **bound** (tied up)).
189
+ -->
118
190
191
+ 我们来描述一下程序变量的自由和约束的概念,因为它们是我们最熟悉的概念。
192
+
193
+ - 考虑此表达式,它创建了一个闭包:` |a, b| a + b ` 。在这里,` a + b ` 中的 ` a `
194
+ 和 ` b ` 指代该闭包被调用会时传入的参数。我们称 ` a ` 和 ` b ` 在该闭包中是
195
+ ** 被约束(bound)** 的,而闭包签名 ` |a, b| ` 是名字 ` a ` 和 ` b ` 的** 约束位(binder)**
196
+ (因为对 ` a ` 或 ` b ` 的任何引用都是指代它引入的变量)。
197
+
198
+ - 考虑此表达式 ` a + b ` 。在该表达式中,` a ` 和 ` b ` 均指代定义在该表达式** 之外**
199
+ 的局部变量。我们称这些变量在该表达式中** 自由出现(appear free)**
200
+ (即它们是** 自由(free)** 的,而非** 被约束(bound)** 的(被束缚的))。
201
+
202
+ <!--
119
203
So there you have it: a variable "appears free" in some
120
204
expression/statement/whatever if it refers to something defined
121
205
outside of that expressions/statement/whatever. Equivalently, we can
122
206
then refer to the "free variables" of an expression – which is just
123
207
the set of variables that "appear free".
208
+ -->
124
209
210
+ 所以现在你理解了:在某些「表达式、语句、还是别的什么」中的变量,
211
+ 如果指代的是定义在该「表达式、语句、还是别的什么」之外的东西,那么它们就是
212
+ 「自由出现」的。我们可以等价地称之为表达式中的「自由变量」,
213
+ 毕竟它们就是一组「自由出现」的变量而已。
214
+
215
+ <!--
125
216
So what does this have to do with regions? Well, we can apply the
126
217
analogous concept to type and regions. For example, in the type `&'a
127
218
u32`, `'a` appears free. But in the type `for<'a> fn(&'a u32)`, it
128
219
does not.
220
+ -->
221
+
222
+ 那么,它们与生存域(region)有什么关系呢?我们可以将类似的概念应用到类型和生存域上来。
223
+ 例如,在类型 ` &'a u32 ` 中,` 'a ` 是自由出现的。但在类型 ` for<'a> fn(&'a u32) ` 中则不是。
0 commit comments