11# Miri
22
33Miri (** 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 .
55
6- If you start out with a constant
6+ If you start out with a constant:
77
88``` rust
99const FOO : usize = 1 << 12 ;
@@ -12,7 +12,7 @@ const FOO: usize = 1 << 12;
1212rustc doesn't actually invoke anything until the constant is either used or
1313placed into metadata.
1414
15- Once you have a use-site like
15+ Once you have a use-site like:
1616
1717``` rust,ignore
1818type Foo = [u8; FOO - 42];
@@ -35,17 +35,17 @@ Invoking `tcx.const_eval(param_env.and(gid))` will now trigger the creation of
3535the MIR of the array length expression. The MIR will look something like this:
3636
3737``` mir
38- const Foo::{{initializer}} : usize = {
39- let mut _0 : usize; // return pointer
38+ Foo::{{constant}}#0 : usize = {
39+ let mut _0 : usize;
4040 let mut _1 : (usize, bool);
4141
4242 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;
4545 }
4646
4747 bb1 : {
48- _0 = (_1.0 : usize);
48+ _0 = move (_1.0 : usize);
4949 return;
5050 }
5151}
@@ -55,16 +55,16 @@ Before the evaluation, a virtual memory location (in this case essentially a
5555` vec![u8; 4] ` or ` vec![u8; 8] ` ) is created for storing the evaluation result.
5656
5757At 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
5959a mouthful: [ ` Operand ` ] can represent either data stored somewhere in the
6060[ interpreter memory] ( #memory ) (` Operand::Indirect ` ), or (as an optimization)
6161immediate data stored in-line. And [ ` Immediate ` ] can either be a single
6262(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)
6464initialized.
6565
6666When 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
6868here. If the evaluation of FOO is successful, ` 42 ` will be subtracted from its
6969value ` 4096 ` and the result stored in ` _1 ` as
7070`Operand::Immediate(Immediate::ScalarPair(Scalar::Raw { data: 4054, .. },
@@ -200,8 +200,8 @@ division on pointer values.
200200
201201## Interpretation
202202
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
205205[ librustc_mir/const_eval.rs] ( https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/const_eval/index.html )
206206that allow accessing the fields of a ` ConstValue ` (` ByRef ` or otherwise). You should
207207never 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
217217and contains all the local
218218variables memory (` None ` at the start of evaluation). Each frame refers to the
219219evaluation 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
221221entirely new and independent stack frame.
222222
223223The 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
229229) until it either returns an error or has no further statements to execute. Each
230230statement will now initialize or modify the locals or the virtual memory
231231referred 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