1 //! This module contains the `InterpretCx` methods for executing a single step of the interpreter.
3 //! The main entry point is the `step` method.
6 use rustc::ty::layout::LayoutOf;
7 use rustc::mir::interpret::{EvalResult, Scalar, PointerArithmetic};
9 use super::{InterpretCx, Machine};
11 /// Classify whether an operator is "left-homogeneous", i.e., the LHS has the
12 /// same type as the result.
14 fn binop_left_homogeneous(op: mir::BinOp) -> bool {
15 use rustc::mir::BinOp::*;
17 Add | Sub | Mul | Div | Rem | BitXor | BitAnd | BitOr |
20 Eq | Ne | Lt | Le | Gt | Ge =>
24 /// Classify whether an operator is "right-homogeneous", i.e., the RHS has the
25 /// same type as the LHS.
27 fn binop_right_homogeneous(op: mir::BinOp) -> bool {
28 use rustc::mir::BinOp::*;
30 Add | Sub | Mul | Div | Rem | BitXor | BitAnd | BitOr |
31 Eq | Ne | Lt | Le | Gt | Ge =>
38 impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M> {
39 pub fn run(&mut self) -> EvalResult<'tcx> {
44 /// Returns `true` as long as there are more things to do.
46 /// This is used by [priroda](https://github.com/oli-obk/priroda)
47 pub fn step(&mut self) -> EvalResult<'tcx, bool> {
48 if self.stack.is_empty() {
52 let block = self.frame().block;
53 let stmt_id = self.frame().stmt;
55 let basic_block = &mir.basic_blocks()[block];
57 let old_frames = self.cur_frame();
59 if let Some(stmt) = basic_block.statements.get(stmt_id) {
60 assert_eq!(old_frames, self.cur_frame());
61 self.statement(stmt)?;
65 M::before_terminator(self)?;
67 let terminator = basic_block.terminator();
68 assert_eq!(old_frames, self.cur_frame());
69 self.terminator(terminator)?;
73 fn statement(&mut self, stmt: &mir::Statement<'tcx>) -> EvalResult<'tcx> {
76 use rustc::mir::StatementKind::*;
78 // Some statements (e.g., box) push new stack frames.
79 // We have to record the stack frame number *before* executing the statement.
80 let frame_idx = self.cur_frame();
81 self.tcx.span = stmt.source_info.span;
82 self.memory.tcx.span = stmt.source_info.span;
85 Assign(ref place, ref rvalue) => self.eval_rvalue_into_place(rvalue, place)?,
91 let dest = self.eval_place(place)?;
92 self.write_discriminant_index(variant_index, dest)?;
95 // Mark locals as alive
96 StorageLive(local) => {
97 let old_val = self.storage_live(local)?;
98 self.deallocate_local(old_val)?;
101 // Mark locals as dead
102 StorageDead(local) => {
103 let old_val = self.storage_dead(local);
104 self.deallocate_local(old_val)?;
107 // No dynamic semantics attached to `FakeRead`; MIR
108 // interpreter is solely intended for borrowck'ed code.
112 Retag(kind, ref place) => {
113 let dest = self.eval_place(place)?;
114 M::retag(self, kind, dest)?;
117 // Statements we do not track.
118 AscribeUserType(..) => {}
120 // Defined to do nothing. These are added by optimization passes, to avoid changing the
121 // size of MIR constantly.
124 InlineAsm { .. } => return err!(InlineAsm),
127 self.stack[frame_idx].stmt += 1;
131 /// Evaluate an assignment statement.
133 /// There is no separate `eval_rvalue` function. Instead, the code for handling each rvalue
134 /// type writes its results directly into the memory specified by the place.
135 fn eval_rvalue_into_place(
137 rvalue: &mir::Rvalue<'tcx>,
138 place: &mir::Place<'tcx>,
139 ) -> EvalResult<'tcx> {
140 let dest = self.eval_place(place)?;
142 use rustc::mir::Rvalue::*;
144 Use(ref operand) => {
145 // Avoid recomputing the layout
146 let op = self.eval_operand(operand, Some(dest.layout))?;
147 self.copy_op(op, dest)?;
150 BinaryOp(bin_op, ref left, ref right) => {
151 let layout = if binop_left_homogeneous(bin_op) { Some(dest.layout) } else { None };
152 let left = self.read_immediate(self.eval_operand(left, layout)?)?;
153 let layout = if binop_right_homogeneous(bin_op) { Some(left.layout) } else { None };
154 let right = self.read_immediate(self.eval_operand(right, layout)?)?;
155 self.binop_ignore_overflow(
163 CheckedBinaryOp(bin_op, ref left, ref right) => {
164 // Due to the extra boolean in the result, we can never reuse the `dest.layout`.
165 let left = self.read_immediate(self.eval_operand(left, None)?)?;
166 let layout = if binop_right_homogeneous(bin_op) { Some(left.layout) } else { None };
167 let right = self.read_immediate(self.eval_operand(right, layout)?)?;
168 self.binop_with_overflow(
176 UnaryOp(un_op, ref operand) => {
177 // The operand always has the same type as the result.
178 let val = self.read_immediate(self.eval_operand(operand, Some(dest.layout))?)?;
179 let val = self.unary_op(un_op, val)?;
180 self.write_scalar(val, dest)?;
183 Aggregate(ref kind, ref operands) => {
184 let (dest, active_field_index) = match **kind {
185 mir::AggregateKind::Adt(adt_def, variant_index, _, _, active_field_index) => {
186 self.write_discriminant_index(variant_index, dest)?;
187 if adt_def.is_enum() {
188 (self.place_downcast(dest, variant_index)?, active_field_index)
190 (dest, active_field_index)
196 for (i, operand) in operands.iter().enumerate() {
197 let op = self.eval_operand(operand, None)?;
198 // Ignore zero-sized fields.
199 if !op.layout.is_zst() {
200 let field_index = active_field_index.unwrap_or(i);
201 let field_dest = self.place_field(dest, field_index as u64)?;
202 self.copy_op(op, field_dest)?;
207 Repeat(ref operand, _) => {
208 let op = self.eval_operand(operand, None)?;
209 let dest = self.force_allocation(dest)?;
210 let length = dest.len(self)?;
214 let first = self.mplace_field(dest, 0)?;
215 self.copy_op(op, first.into())?;
219 let (dest, dest_align) = first.to_scalar_ptr_align();
220 let rest = dest.ptr_offset(first.layout.size, self)?;
221 self.memory.copy_repeatedly(
222 dest, dest_align, rest, dest_align, first.layout.size, length - 1, true
229 // FIXME(CTFE): don't allow computing the length of arrays in const eval
230 let src = self.eval_place(place)?;
231 let mplace = self.force_allocation(src)?;
232 let len = mplace.len(self)?;
233 let size = self.pointer_size();
235 Scalar::from_uint(len, size),
240 Ref(_, _, ref place) => {
241 let src = self.eval_place(place)?;
242 let val = self.force_allocation(src)?;
243 self.write_immediate(val.to_ref(), dest)?;
246 NullaryOp(mir::NullOp::Box, _) => {
247 M::box_alloc(self, dest)?;
250 NullaryOp(mir::NullOp::SizeOf, ty) => {
251 let ty = self.monomorphize(ty)?;
252 let layout = self.layout_of(ty)?;
253 assert!(!layout.is_unsized(),
254 "SizeOf nullary MIR operator called for unsized type");
255 let size = self.pointer_size();
257 Scalar::from_uint(layout.size.bytes(), size),
262 Cast(kind, ref operand, cast_ty) => {
263 debug_assert_eq!(self.monomorphize(cast_ty)?, dest.layout.ty);
264 let src = self.eval_operand(operand, None)?;
265 self.cast(src, kind, dest)?;
268 Discriminant(ref place) => {
269 let op = self.eval_place_to_op(place, None)?;
270 let discr_val = self.read_discriminant(op)?.0;
271 let size = dest.layout.size;
272 self.write_scalar(Scalar::from_uint(discr_val, size), dest)?;
276 self.dump_place(*dest);
281 fn terminator(&mut self, terminator: &mir::Terminator<'tcx>) -> EvalResult<'tcx> {
282 info!("{:?}", terminator.kind);
283 self.tcx.span = terminator.source_info.span;
284 self.memory.tcx.span = terminator.source_info.span;
286 let old_stack = self.cur_frame();
287 let old_bb = self.frame().block;
288 self.eval_terminator(terminator)?;
289 if !self.stack.is_empty() {
290 // This should change *something*
291 debug_assert!(self.cur_frame() != old_stack || self.frame().block != old_bb);
292 info!("// {:?}", self.frame().block);