@@ -11,15 +11,17 @@ list of items:
1111 * Drop code (the ` Drop::drop ` function is not called directly)
1212 * Drop implementations of types without an explicit ` Drop ` implementation
1313
14- The lowering is triggered by calling the [ ` mir_built ` ] query. The MIR builder does
15- not actually use the HIR but operates on the [ THIR] instead, processing THIR
16- expressions recursively.
14+ The lowering is triggered by calling the [ ` mir_built ` ] query.
15+ The MIR builder does not actually use the HIR,
16+ but operates on the [ THIR] instead,
17+ processing THIR expressions recursively.
1718
1819The lowering creates local variables for every argument as specified in the signature.
1920Next, it creates local variables for every binding specified (e.g. ` (a, b): (i32, String) ` )
20- produces 3 bindings, one for the argument, and two for the bindings. Next, it generates
21- field accesses that read the fields from the argument and writes the value to the binding
22- variable.
21+ produces 3 bindings, one for the argument, and two for the bindings.
22+ Next,
23+ it generates field accesses that read the fields from the argument,
24+ and writes the value to the binding variable.
2325
2426With this initialization out of the way, the lowering triggers a recursive call
2527to a function that generates the MIR for the body (a ` Block ` expression) and
@@ -52,7 +54,8 @@ fn generate_more_mir(&mut self, block: BasicBlock) -> BlockAnd<ResultType> {
5254```
5355
5456When you invoke these functions, it is common to have a local variable ` block `
55- that is effectively a "cursor". It represents the point at which we are adding new MIR.
57+ that is effectively a "cursor".
58+ It represents the point at which we are adding new MIR.
5659When you invoke ` generate_more_mir ` , you want to update this cursor.
5760You can do this manually, but it's tedious:
5861
@@ -89,18 +92,21 @@ representations:
8992
9093We start out with lowering the function body to an ` Rvalue ` so we can create an
9194assignment to ` RETURN_PLACE ` , This ` Rvalue ` lowering will in turn trigger lowering to
92- ` Operand ` for its arguments (if any). ` Operand ` lowering either produces a ` const `
93- operand, or moves/copies out of a ` Place ` , thus triggering a ` Place ` lowering. An
94- expression being lowered to a ` Place ` can in turn trigger a temporary to be created
95- if the expression being lowered contains operations. This is where the snake bites its
95+ ` Operand ` for its arguments (if any).
96+ ` Operand ` lowering either produces a ` const ` operand,
97+ or moves/copies out of a ` Place ` , thus triggering a ` Place ` lowering.
98+ An expression being lowered to a ` Place ` can in turn trigger a temporary to be created
99+ if the expression being lowered contains operations.
100+ This is where the snake bites its
96101own tail and we need to trigger an ` Rvalue ` lowering for the expression to be written
97102into the local.
98103
99104## Operator lowering
100105
101106Operators on builtin types are not lowered to function calls (which would end up being
102107infinite recursion calls, because the trait impls just contain the operation itself
103- again). Instead there are ` Rvalue ` s for binary and unary operators and index operations.
108+ again).
109+ Instead there are ` Rvalue ` s for binary and unary operators and index operations.
104110These ` Rvalue ` s later get codegened to llvm primitive operations or llvm intrinsics.
105111
106112Operators on all other types get lowered to a function call to their ` impl ` of the
@@ -118,7 +124,8 @@ In [MIR] there is no difference between method calls and function calls anymore.
118124## Conditions
119125
120126` if ` conditions and ` match ` statements for ` enum ` s with variants that have no fields are
121- lowered to ` TerminatorKind::SwitchInt ` . Each possible value (so ` 0 ` and ` 1 ` for ` if `
127+ lowered to ` TerminatorKind::SwitchInt ` .
128+ Each possible value (so ` 0 ` and ` 1 ` for ` if `
122129conditions) has a corresponding ` BasicBlock ` to which the code continues.
123130The argument being branched on is (again) an ` Operand ` representing the value of
124131the if condition.
@@ -127,14 +134,14 @@ the if condition.
127134
128135` match ` statements for ` enum ` s with variants that have fields are lowered to
129136` TerminatorKind::SwitchInt ` , too, but the ` Operand ` refers to a ` Place ` where the
130- discriminant of the value can be found. This often involves reading the discriminant
131- to a new temporary variable.
137+ discriminant of the value can be found.
138+ This often involves reading the discriminant to a new temporary variable.
132139
133140## Aggregate construction
134141
135142Aggregate values of any kind (e.g. structs or tuples) are built via ` Rvalue::Aggregate ` .
136- All fields are
137- lowered to ` Operator ` s. This is essentially equivalent to one assignment
143+ All fields are lowered to ` Operator ` s.
144+ This is essentially equivalent to one assignment
138145statement per aggregate field plus an assignment to the discriminant in the
139146case of ` enum ` s.
140147
0 commit comments