]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_const_eval/src/interpret/step.rs
Rollup merge of #89869 - kpreid:from-doc, r=yaahc
[rust.git] / compiler / rustc_const_eval / src / interpret / step.rs
1 //! This module contains the `InterpCx` methods for executing a single step of the interpreter.
2 //!
3 //! The main entry point is the `step` method.
4
5 use rustc_middle::mir;
6 use rustc_middle::mir::interpret::{InterpResult, Scalar};
7 use rustc_middle::ty::layout::LayoutOf;
8
9 use super::{InterpCx, Machine};
10
11 /// Classify whether an operator is "left-homogeneous", i.e., the LHS has the
12 /// same type as the result.
13 #[inline]
14 fn binop_left_homogeneous(op: mir::BinOp) -> bool {
15     use rustc_middle::mir::BinOp::*;
16     match op {
17         Add | Sub | Mul | Div | Rem | BitXor | BitAnd | BitOr | Offset | Shl | Shr => true,
18         Eq | Ne | Lt | Le | Gt | Ge => false,
19     }
20 }
21 /// Classify whether an operator is "right-homogeneous", i.e., the RHS has the
22 /// same type as the LHS.
23 #[inline]
24 fn binop_right_homogeneous(op: mir::BinOp) -> bool {
25     use rustc_middle::mir::BinOp::*;
26     match op {
27         Add | Sub | Mul | Div | Rem | BitXor | BitAnd | BitOr | Eq | Ne | Lt | Le | Gt | Ge => true,
28         Offset | Shl | Shr => false,
29     }
30 }
31
32 impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
33     pub fn run(&mut self) -> InterpResult<'tcx> {
34         while self.step()? {}
35         Ok(())
36     }
37
38     /// Returns `true` as long as there are more things to do.
39     ///
40     /// This is used by [priroda](https://github.com/oli-obk/priroda)
41     ///
42     /// This is marked `#inline(always)` to work around adverserial codegen when `opt-level = 3`
43     #[inline(always)]
44     pub fn step(&mut self) -> InterpResult<'tcx, bool> {
45         if self.stack().is_empty() {
46             return Ok(false);
47         }
48
49         let loc = match self.frame().loc {
50             Ok(loc) => loc,
51             Err(_) => {
52                 // We are unwinding and this fn has no cleanup code.
53                 // Just go on unwinding.
54                 trace!("unwinding: skipping frame");
55                 self.pop_stack_frame(/* unwinding */ true)?;
56                 return Ok(true);
57             }
58         };
59         let basic_block = &self.body().basic_blocks()[loc.block];
60
61         let old_frames = self.frame_idx();
62
63         if let Some(stmt) = basic_block.statements.get(loc.statement_index) {
64             assert_eq!(old_frames, self.frame_idx());
65             self.statement(stmt)?;
66             return Ok(true);
67         }
68
69         M::before_terminator(self)?;
70
71         let terminator = basic_block.terminator();
72         assert_eq!(old_frames, self.frame_idx());
73         self.terminator(terminator)?;
74         Ok(true)
75     }
76
77     /// Runs the interpretation logic for the given `mir::Statement` at the current frame and
78     /// statement counter. This also moves the statement counter forward.
79     pub fn statement(&mut self, stmt: &mir::Statement<'tcx>) -> InterpResult<'tcx> {
80         info!("{:?}", stmt);
81
82         use rustc_middle::mir::StatementKind::*;
83
84         // Some statements (e.g., box) push new stack frames.
85         // We have to record the stack frame number *before* executing the statement.
86         let frame_idx = self.frame_idx();
87
88         match &stmt.kind {
89             Assign(box (place, rvalue)) => self.eval_rvalue_into_place(rvalue, *place)?,
90
91             SetDiscriminant { place, variant_index } => {
92                 let dest = self.eval_place(**place)?;
93                 self.write_discriminant(*variant_index, &dest)?;
94             }
95
96             // Mark locals as alive
97             StorageLive(local) => {
98                 self.storage_live(*local)?;
99             }
100
101             // Mark locals as dead
102             StorageDead(local) => {
103                 self.storage_dead(*local)?;
104             }
105
106             // No dynamic semantics attached to `FakeRead`; MIR
107             // interpreter is solely intended for borrowck'ed code.
108             FakeRead(..) => {}
109
110             // Stacked Borrows.
111             Retag(kind, place) => {
112                 let dest = self.eval_place(**place)?;
113                 M::retag(self, *kind, &dest)?;
114             }
115
116             // Call CopyNonOverlapping
117             CopyNonOverlapping(box rustc_middle::mir::CopyNonOverlapping { src, dst, count }) => {
118                 let src = self.eval_operand(src, None)?;
119                 let dst = self.eval_operand(dst, None)?;
120                 let count = self.eval_operand(count, None)?;
121                 self.copy_intrinsic(&src, &dst, &count, /* nonoverlapping */ true)?;
122             }
123
124             // Statements we do not track.
125             AscribeUserType(..) => {}
126
127             // Currently, Miri discards Coverage statements. Coverage statements are only injected
128             // via an optional compile time MIR pass and have no side effects. Since Coverage
129             // statements don't exist at the source level, it is safe for Miri to ignore them, even
130             // for undefined behavior (UB) checks.
131             //
132             // A coverage counter inside a const expression (for example, a counter injected in a
133             // const function) is discarded when the const is evaluated at compile time. Whether
134             // this should change, and/or how to implement a const eval counter, is a subject of the
135             // following issue:
136             //
137             // FIXME(#73156): Handle source code coverage in const eval
138             Coverage(..) => {}
139
140             // Defined to do nothing. These are added by optimization passes, to avoid changing the
141             // size of MIR constantly.
142             Nop => {}
143         }
144
145         self.stack_mut()[frame_idx].loc.as_mut().unwrap().statement_index += 1;
146         Ok(())
147     }
148
149     /// Evaluate an assignment statement.
150     ///
151     /// There is no separate `eval_rvalue` function. Instead, the code for handling each rvalue
152     /// type writes its results directly into the memory specified by the place.
153     pub fn eval_rvalue_into_place(
154         &mut self,
155         rvalue: &mir::Rvalue<'tcx>,
156         place: mir::Place<'tcx>,
157     ) -> InterpResult<'tcx> {
158         let dest = self.eval_place(place)?;
159
160         use rustc_middle::mir::Rvalue::*;
161         match *rvalue {
162             ThreadLocalRef(did) => {
163                 let ptr = M::thread_local_static_base_pointer(self, did)?;
164                 self.write_pointer(ptr, &dest)?;
165             }
166
167             Use(ref operand) => {
168                 // Avoid recomputing the layout
169                 let op = self.eval_operand(operand, Some(dest.layout))?;
170                 self.copy_op(&op, &dest)?;
171             }
172
173             BinaryOp(bin_op, box (ref left, ref right)) => {
174                 let layout = binop_left_homogeneous(bin_op).then_some(dest.layout);
175                 let left = self.read_immediate(&self.eval_operand(left, layout)?)?;
176                 let layout = binop_right_homogeneous(bin_op).then_some(left.layout);
177                 let right = self.read_immediate(&self.eval_operand(right, layout)?)?;
178                 self.binop_ignore_overflow(bin_op, &left, &right, &dest)?;
179             }
180
181             CheckedBinaryOp(bin_op, box (ref left, ref right)) => {
182                 // Due to the extra boolean in the result, we can never reuse the `dest.layout`.
183                 let left = self.read_immediate(&self.eval_operand(left, None)?)?;
184                 let layout = binop_right_homogeneous(bin_op).then_some(left.layout);
185                 let right = self.read_immediate(&self.eval_operand(right, layout)?)?;
186                 self.binop_with_overflow(bin_op, &left, &right, &dest)?;
187             }
188
189             UnaryOp(un_op, ref operand) => {
190                 // The operand always has the same type as the result.
191                 let val = self.read_immediate(&self.eval_operand(operand, Some(dest.layout))?)?;
192                 let val = self.unary_op(un_op, &val)?;
193                 assert_eq!(val.layout, dest.layout, "layout mismatch for result of {:?}", un_op);
194                 self.write_immediate(*val, &dest)?;
195             }
196
197             Aggregate(ref kind, ref operands) => {
198                 // active_field_index is for union initialization.
199                 let (dest, active_field_index) = match **kind {
200                     mir::AggregateKind::Adt(adt_did, variant_index, _, _, active_field_index) => {
201                         self.write_discriminant(variant_index, &dest)?;
202                         if self.tcx.adt_def(adt_did).is_enum() {
203                             assert!(active_field_index.is_none());
204                             (self.place_downcast(&dest, variant_index)?, None)
205                         } else {
206                             if active_field_index.is_some() {
207                                 assert_eq!(operands.len(), 1);
208                             }
209                             (dest, active_field_index)
210                         }
211                     }
212                     _ => (dest, None),
213                 };
214
215                 for (i, operand) in operands.iter().enumerate() {
216                     let op = self.eval_operand(operand, None)?;
217                     let field_index = active_field_index.unwrap_or(i);
218                     let field_dest = self.place_field(&dest, field_index)?;
219                     self.copy_op(&op, &field_dest)?;
220                 }
221             }
222
223             Repeat(ref operand, _) => {
224                 let src = self.eval_operand(operand, None)?;
225                 assert!(!src.layout.is_unsized());
226                 let dest = self.force_allocation(&dest)?;
227                 let length = dest.len(self)?;
228
229                 if length == 0 {
230                     // Nothing to copy... but let's still make sure that `dest` as a place is valid.
231                     self.get_alloc_mut(&dest)?;
232                 } else {
233                     // Write the src to the first element.
234                     let first = self.mplace_field(&dest, 0)?;
235                     self.copy_op(&src, &first.into())?;
236
237                     // This is performance-sensitive code for big static/const arrays! So we
238                     // avoid writing each operand individually and instead just make many copies
239                     // of the first element.
240                     let elem_size = first.layout.size;
241                     let first_ptr = first.ptr;
242                     let rest_ptr = first_ptr.offset(elem_size, self)?;
243                     // For the alignment of `rest_ptr`, we crucially do *not* use `first.align` as
244                     // that place might be more aligned than its type mandates (a `u8` array could
245                     // be 4-aligned if it sits at the right spot in a struct). Instead we use
246                     // `first.layout.align`, i.e., the alignment given by the type.
247                     self.memory.copy_repeatedly(
248                         first_ptr,
249                         first.align,
250                         rest_ptr,
251                         first.layout.align.abi,
252                         elem_size,
253                         length - 1,
254                         /*nonoverlapping:*/ true,
255                     )?;
256                 }
257             }
258
259             Len(place) => {
260                 let src = self.eval_place(place)?;
261                 let mplace = self.force_allocation(&src)?;
262                 let len = mplace.len(self)?;
263                 self.write_scalar(Scalar::from_machine_usize(len, self), &dest)?;
264             }
265
266             AddressOf(_, place) | Ref(_, _, place) => {
267                 let src = self.eval_place(place)?;
268                 let place = self.force_allocation(&src)?;
269                 self.write_immediate(place.to_ref(self), &dest)?;
270             }
271
272             NullaryOp(null_op, ty) => {
273                 let ty = self.subst_from_current_frame_and_normalize_erasing_regions(ty)?;
274                 let layout = self.layout_of(ty)?;
275                 if layout.is_unsized() {
276                     // FIXME: This should be a span_bug (#80742)
277                     self.tcx.sess.delay_span_bug(
278                         self.frame().current_span(),
279                         &format!("Nullary MIR operator called for unsized type {}", ty),
280                     );
281                     throw_inval!(SizeOfUnsizedType(ty));
282                 }
283                 let val = match null_op {
284                     mir::NullOp::SizeOf => layout.size.bytes(),
285                     mir::NullOp::AlignOf => layout.align.abi.bytes(),
286                 };
287                 self.write_scalar(Scalar::from_machine_usize(val, self), &dest)?;
288             }
289
290             ShallowInitBox(ref operand, _) => {
291                 let src = self.eval_operand(operand, None)?;
292                 let v = self.read_immediate(&src)?;
293                 self.write_immediate(*v, &dest)?;
294             }
295
296             Cast(cast_kind, ref operand, cast_ty) => {
297                 let src = self.eval_operand(operand, None)?;
298                 let cast_ty =
299                     self.subst_from_current_frame_and_normalize_erasing_regions(cast_ty)?;
300                 self.cast(&src, cast_kind, cast_ty, &dest)?;
301             }
302
303             Discriminant(place) => {
304                 let op = self.eval_place_to_op(place, None)?;
305                 let discr_val = self.read_discriminant(&op)?.0;
306                 self.write_scalar(discr_val, &dest)?;
307             }
308         }
309
310         trace!("{:?}", self.dump_place(*dest));
311
312         Ok(())
313     }
314
315     fn terminator(&mut self, terminator: &mir::Terminator<'tcx>) -> InterpResult<'tcx> {
316         info!("{:?}", terminator.kind);
317
318         self.eval_terminator(terminator)?;
319         if !self.stack().is_empty() {
320             if let Ok(loc) = self.frame().loc {
321                 info!("// executing {:?}", loc.block);
322             }
323         }
324         Ok(())
325     }
326 }