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