Skip to content

Commit 57e49f1

Browse files
committed
sembr src/mir/construction.md
1 parent 0fd4f09 commit 57e49f1

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

src/mir/construction.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ 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
14+
The lowering is triggered by calling the [`mir_built`] query.
15+
The MIR builder does
1516
not actually use the HIR but operates on the [THIR] instead, processing THIR
1617
expressions recursively.
1718

1819
The lowering creates local variables for every argument as specified in the signature.
1920
Next, 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+
produces 3 bindings, one for the argument, and two for the bindings.
22+
Next, it generates
2123
field accesses that read the fields from the argument and writes the value to the binding
2224
variable.
2325

@@ -52,7 +54,8 @@ fn generate_more_mir(&mut self, block: BasicBlock) -> BlockAnd<ResultType> {
5254
```
5355

5456
When 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.
5659
When you invoke `generate_more_mir`, you want to update this cursor.
5760
You can do this manually, but it's tedious:
5861

@@ -89,18 +92,22 @@ representations:
8992

9093
We start out with lowering the function body to an `Rvalue` so we can create an
9194
assignment 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
95+
`Operand` for its arguments (if any).
96+
`Operand` lowering either produces a `const`
97+
operand, or moves/copies out of a `Place`, thus triggering a `Place` lowering.
98+
An
9499
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
100+
if the expression being lowered contains operations.
101+
This is where the snake bites its
96102
own tail and we need to trigger an `Rvalue` lowering for the expression to be written
97103
into the local.
98104

99105
## Operator lowering
100106

101107
Operators on builtin types are not lowered to function calls (which would end up being
102108
infinite 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.
109+
again).
110+
Instead there are `Rvalue`s for binary and unary operators and index operations.
104111
These `Rvalue`s later get codegened to llvm primitive operations or llvm intrinsics.
105112

106113
Operators on all other types get lowered to a function call to their `impl` of the
@@ -118,7 +125,8 @@ In [MIR] there is no difference between method calls and function calls anymore.
118125
## Conditions
119126

120127
`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`
128+
lowered to `TerminatorKind::SwitchInt`.
129+
Each possible value (so `0` and `1` for `if`
122130
conditions) has a corresponding `BasicBlock` to which the code continues.
123131
The argument being branched on is (again) an `Operand` representing the value of
124132
the if condition.
@@ -127,14 +135,14 @@ the if condition.
127135

128136
`match` statements for `enum`s with variants that have fields are lowered to
129137
`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.
138+
discriminant of the value can be found.
139+
This often involves reading the discriminant to a new temporary variable.
132140

133141
## Aggregate construction
134142

135143
Aggregate 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
144+
All fields are lowered to `Operator`s.
145+
This is essentially equivalent to one assignment
138146
statement per aggregate field plus an assignment to the discriminant in the
139147
case of `enum`s.
140148

0 commit comments

Comments
 (0)