1
1
# Miri
2
2
3
3
Miri (** MIR** ** I** nterpreter) is a virtual machine for executing MIR without
4
- compiling to machine code. It is usually invoked via ` tcx.const_eval ` .
4
+ compiling to machine code. It is usually invoked via ` tcx.const_eval_* ` functions .
5
5
6
- If you start out with a constant
6
+ If you start out with a constant:
7
7
8
8
``` rust
9
9
const FOO : usize = 1 << 12 ;
@@ -12,7 +12,7 @@ const FOO: usize = 1 << 12;
12
12
rustc doesn't actually invoke anything until the constant is either used or
13
13
placed into metadata.
14
14
15
- Once you have a use-site like
15
+ Once you have a use-site like:
16
16
17
17
``` rust,ignore
18
18
type Foo = [u8; FOO - 42];
@@ -35,17 +35,17 @@ Invoking `tcx.const_eval(param_env.and(gid))` will now trigger the creation of
35
35
the MIR of the array length expression. The MIR will look something like this:
36
36
37
37
``` mir
38
- const Foo::{{initializer}} : usize = {
39
- let mut _0 : usize; // return pointer
38
+ Foo::{{constant}}#0 : usize = {
39
+ let mut _0 : usize;
40
40
let mut _1 : (usize, bool);
41
41
42
42
bb0 : {
43
- _1 = CheckedSub(const Unevaluated( FOO, Slice([])) , const 42usize);
44
- assert(!(_1.1 : bool), "attempt to subtract with overflow") -> bb1;
43
+ _1 = CheckedSub(const FOO, const 42usize);
44
+ assert(!move (_1.1 : bool), "attempt to subtract with overflow") -> bb1;
45
45
}
46
46
47
47
bb1 : {
48
- _0 = (_1.0 : usize);
48
+ _0 = move (_1.0 : usize);
49
49
return;
50
50
}
51
51
}
@@ -55,16 +55,16 @@ Before the evaluation, a virtual memory location (in this case essentially a
55
55
` vec![u8; 4] ` or ` vec![u8; 8] ` ) is created for storing the evaluation result.
56
56
57
57
At the start of the evaluation, ` _0 ` and ` _1 ` are
58
- ` Operand::Immediate(Immediate::Scalar(ScalarMaybeUndef::Undef)) ` . This is quite
58
+ ` Operand::Immediate(Immediate::Scalar(ScalarMaybeUndef::Undef)) ` . This is quite
59
59
a mouthful: [ ` Operand ` ] can represent either data stored somewhere in the
60
60
[ interpreter memory] ( #memory ) (` Operand::Indirect ` ), or (as an optimization)
61
61
immediate data stored in-line. And [ ` Immediate ` ] can either be a single
62
62
(potentially uninitialized) [ scalar value] [ `Scalar` ] (integer or thin pointer),
63
- or a pair of two of them. In our case, the single scalar value is * not* (yet)
63
+ or a pair of two of them. In our case, the single scalar value is * not* (yet)
64
64
initialized.
65
65
66
66
When the initialization of ` _1 ` is invoked, the value of the ` FOO ` constant is
67
- required, and triggers another call to ` tcx.const_eval ` , which will not be shown
67
+ required, and triggers another call to ` tcx.const_eval_* ` , which will not be shown
68
68
here. If the evaluation of FOO is successful, ` 42 ` will be subtracted from its
69
69
value ` 4096 ` and the result stored in ` _1 ` as
70
70
`Operand::Immediate(Immediate::ScalarPair(Scalar::Raw { data: 4054, .. },
@@ -200,8 +200,8 @@ division on pointer values.
200
200
201
201
## Interpretation
202
202
203
- Although the main entry point to constant evaluation is the ` tcx.const_eval `
204
- query , there are additional functions in
203
+ Although the main entry point to constant evaluation is the ` tcx.const_eval_* `
204
+ functions , there are additional functions in
205
205
[ librustc_mir/const_eval.rs] ( https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/const_eval/index.html )
206
206
that allow accessing the fields of a ` ConstValue ` (` ByRef ` or otherwise). You should
207
207
never have to access an ` Allocation ` directly except for translating it to the
@@ -217,7 +217,7 @@ A stack frame is defined by the `Frame` type in
217
217
and contains all the local
218
218
variables memory (` None ` at the start of evaluation). Each frame refers to the
219
219
evaluation of either the root constant or subsequent calls to ` const fn ` . The
220
- evaluation of another constant simply calls ` tcx.const_eval ` , which produces an
220
+ evaluation of another constant simply calls ` tcx.const_eval_* ` , which produce an
221
221
entirely new and independent stack frame.
222
222
223
223
The frames are just a ` Vec<Frame> ` , there's no way to actually refer to a
@@ -229,4 +229,4 @@ Miri now calls the `step` method (in
229
229
) until it either returns an error or has no further statements to execute. Each
230
230
statement will now initialize or modify the locals or the virtual memory
231
231
referred to by a local. This might require evaluating other constants or
232
- statics, which just recursively invokes ` tcx.const_eval ` .
232
+ statics, which just recursively invokes ` tcx.const_eval_* ` .
0 commit comments