2019-06-27 11:36:01 +02:00
|
|
|
//! This module contains the `InterpCx` methods for executing a single step of the interpreter.
|
2016-06-23 00:02:47 -06:00
|
|
|
//!
|
|
|
|
//! The main entry point is the `step` method.
|
|
|
|
|
2018-11-06 11:04:10 +01:00
|
|
|
use rustc::mir;
|
2018-08-09 15:04:53 +02:00
|
|
|
use rustc::ty::layout::LayoutOf;
|
2019-06-07 18:56:27 +02:00
|
|
|
use rustc::mir::interpret::{InterpResult, Scalar, PointerArithmetic};
|
2016-12-07 20:30:37 -08:00
|
|
|
|
2019-06-27 11:36:01 +02:00
|
|
|
use super::{InterpCx, Machine};
|
2017-07-21 13:39:06 +02:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
/// Classify whether an operator is "left-homogeneous", i.e., the LHS has the
|
2018-08-20 20:08:24 +02:00
|
|
|
/// same type as the result.
|
|
|
|
#[inline]
|
|
|
|
fn binop_left_homogeneous(op: mir::BinOp) -> bool {
|
|
|
|
use rustc::mir::BinOp::*;
|
|
|
|
match op {
|
|
|
|
Add | Sub | Mul | Div | Rem | BitXor | BitAnd | BitOr |
|
|
|
|
Offset | Shl | Shr =>
|
|
|
|
true,
|
|
|
|
Eq | Ne | Lt | Le | Gt | Ge =>
|
|
|
|
false,
|
|
|
|
}
|
|
|
|
}
|
2018-11-27 02:59:49 +00:00
|
|
|
/// Classify whether an operator is "right-homogeneous", i.e., the RHS has the
|
2018-08-20 20:08:24 +02:00
|
|
|
/// same type as the LHS.
|
|
|
|
#[inline]
|
|
|
|
fn binop_right_homogeneous(op: mir::BinOp) -> bool {
|
|
|
|
use rustc::mir::BinOp::*;
|
|
|
|
match op {
|
|
|
|
Add | Sub | Mul | Div | Rem | BitXor | BitAnd | BitOr |
|
|
|
|
Eq | Ne | Lt | Le | Gt | Ge =>
|
|
|
|
true,
|
|
|
|
Offset | Shl | Shr =>
|
|
|
|
false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 11:36:01 +02:00
|
|
|
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
2019-06-07 18:56:27 +02:00
|
|
|
pub fn run(&mut self) -> InterpResult<'tcx> {
|
2018-08-23 19:04:33 +02:00
|
|
|
while self.step()? {}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `true` as long as there are more things to do.
|
2018-10-14 11:50:05 +02:00
|
|
|
///
|
|
|
|
/// This is used by [priroda](https://github.com/oli-obk/priroda)
|
2019-06-07 18:56:27 +02:00
|
|
|
pub fn step(&mut self) -> InterpResult<'tcx, bool> {
|
2018-06-22 12:36:54 -07:00
|
|
|
if self.stack.is_empty() {
|
2016-06-09 10:52:45 +02:00
|
|
|
return Ok(false);
|
2016-06-03 15:48:56 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 00:02:47 -06:00
|
|
|
let block = self.frame().block;
|
2016-09-06 16:04:51 +02:00
|
|
|
let stmt_id = self.frame().stmt;
|
2019-06-03 18:26:48 -04:00
|
|
|
let body = self.body();
|
|
|
|
let basic_block = &body.basic_blocks()[block];
|
2016-06-01 17:05:20 +02:00
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
let old_frames = self.cur_frame();
|
|
|
|
|
2016-09-13 13:03:42 +02:00
|
|
|
if let Some(stmt) = basic_block.statements.get(stmt_id) {
|
2018-01-16 09:31:48 +01:00
|
|
|
assert_eq!(old_frames, self.cur_frame());
|
|
|
|
self.statement(stmt)?;
|
2016-06-09 10:52:45 +02:00
|
|
|
return Ok(true);
|
2016-06-01 17:05:20 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 10:12:21 +02:00
|
|
|
M::before_terminator(self)?;
|
2018-02-19 12:00:15 +01:00
|
|
|
|
2016-06-03 15:48:56 +02:00
|
|
|
let terminator = basic_block.terminator();
|
2018-01-16 09:31:48 +01:00
|
|
|
assert_eq!(old_frames, self.cur_frame());
|
|
|
|
self.terminator(terminator)?;
|
2016-06-09 10:52:45 +02:00
|
|
|
Ok(true)
|
2016-06-01 17:05:20 +02:00
|
|
|
}
|
2016-06-23 00:02:47 -06:00
|
|
|
|
2019-06-07 18:56:27 +02:00
|
|
|
fn statement(&mut self, stmt: &mir::Statement<'tcx>) -> InterpResult<'tcx> {
|
2018-12-19 10:10:39 +01:00
|
|
|
info!("{:?}", stmt);
|
2016-08-27 01:44:46 -06:00
|
|
|
|
2017-12-12 17:14:49 +01:00
|
|
|
use rustc::mir::StatementKind::*;
|
2017-09-25 15:55:21 +02:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Some statements (e.g., box) push new stack frames.
|
2018-08-22 16:58:39 -03:00
|
|
|
// We have to record the stack frame number *before* executing the statement.
|
2017-09-25 15:55:21 +02:00
|
|
|
let frame_idx = self.cur_frame();
|
2018-02-06 18:33:59 +01:00
|
|
|
self.tcx.span = stmt.source_info.span;
|
2018-06-22 12:36:54 -07:00
|
|
|
self.memory.tcx.span = stmt.source_info.span;
|
2017-09-25 15:55:21 +02:00
|
|
|
|
2016-08-27 01:44:46 -06:00
|
|
|
match stmt.kind {
|
2017-12-06 09:25:29 +01:00
|
|
|
Assign(ref place, ref rvalue) => self.eval_rvalue_into_place(rvalue, place)?,
|
2016-12-18 23:31:23 -08:00
|
|
|
|
2017-08-10 08:48:38 -07:00
|
|
|
SetDiscriminant {
|
2017-12-06 09:25:29 +01:00
|
|
|
ref place,
|
2017-08-10 08:48:38 -07:00
|
|
|
variant_index,
|
|
|
|
} => {
|
2017-12-06 09:25:29 +01:00
|
|
|
let dest = self.eval_place(place)?;
|
2018-08-25 11:07:03 +02:00
|
|
|
self.write_discriminant_index(variant_index, dest)?;
|
2016-12-18 23:31:23 -08:00
|
|
|
}
|
2016-08-27 01:44:46 -06:00
|
|
|
|
2017-09-05 17:18:48 +02:00
|
|
|
// Mark locals as alive
|
|
|
|
StorageLive(local) => {
|
2018-07-24 18:28:53 +02:00
|
|
|
let old_val = self.storage_live(local)?;
|
2017-09-05 17:18:48 +02:00
|
|
|
self.deallocate_local(old_val)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark locals as dead
|
|
|
|
StorageDead(local) => {
|
2018-08-13 16:14:22 +02:00
|
|
|
let old_val = self.storage_dead(local);
|
2017-06-01 17:59:00 -07:00
|
|
|
self.deallocate_local(old_val)?;
|
2017-05-31 17:41:33 -07:00
|
|
|
}
|
2016-09-21 23:16:31 -06:00
|
|
|
|
2018-09-14 21:05:31 +02:00
|
|
|
// No dynamic semantics attached to `FakeRead`; MIR
|
2018-05-22 15:05:02 +02:00
|
|
|
// interpreter is solely intended for borrowck'ed code.
|
2018-09-14 21:05:31 +02:00
|
|
|
FakeRead(..) => {}
|
2018-05-04 12:04:33 +02:00
|
|
|
|
2018-11-06 11:04:10 +01:00
|
|
|
// Stacked Borrows.
|
2018-12-11 19:54:38 +01:00
|
|
|
Retag(kind, ref place) => {
|
2018-10-24 11:47:17 +02:00
|
|
|
let dest = self.eval_place(place)?;
|
2018-12-11 19:54:38 +01:00
|
|
|
M::retag(self, kind, dest)?;
|
2018-11-06 11:04:10 +01:00
|
|
|
}
|
2017-06-20 19:35:46 +09:00
|
|
|
|
2018-11-06 11:04:10 +01:00
|
|
|
// Statements we do not track.
|
2018-08-31 18:59:35 -04:00
|
|
|
AscribeUserType(..) => {}
|
2018-02-23 20:52:05 +00:00
|
|
|
|
2016-09-21 23:16:31 -06:00
|
|
|
// Defined to do nothing. These are added by optimization passes, to avoid changing the
|
|
|
|
// size of MIR constantly.
|
|
|
|
Nop => {}
|
2017-02-24 10:39:55 +01:00
|
|
|
|
2017-08-02 16:59:01 +02:00
|
|
|
InlineAsm { .. } => return err!(InlineAsm),
|
2016-08-27 01:44:46 -06:00
|
|
|
}
|
|
|
|
|
2018-06-22 12:36:54 -07:00
|
|
|
self.stack[frame_idx].stmt += 1;
|
2016-06-23 00:02:47 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-08-09 15:04:53 +02:00
|
|
|
/// Evaluate an assignment statement.
|
|
|
|
///
|
|
|
|
/// There is no separate `eval_rvalue` function. Instead, the code for handling each rvalue
|
|
|
|
/// type writes its results directly into the memory specified by the place.
|
|
|
|
fn eval_rvalue_into_place(
|
|
|
|
&mut self,
|
|
|
|
rvalue: &mir::Rvalue<'tcx>,
|
|
|
|
place: &mir::Place<'tcx>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2018-08-09 15:04:53 +02:00
|
|
|
let dest = self.eval_place(place)?;
|
|
|
|
|
|
|
|
use rustc::mir::Rvalue::*;
|
|
|
|
match *rvalue {
|
|
|
|
Use(ref operand) => {
|
2018-08-20 15:21:04 +02:00
|
|
|
// Avoid recomputing the layout
|
|
|
|
let op = self.eval_operand(operand, Some(dest.layout))?;
|
2018-08-13 16:14:22 +02:00
|
|
|
self.copy_op(op, dest)?;
|
2018-08-09 15:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BinaryOp(bin_op, ref left, ref right) => {
|
2018-08-20 20:08:24 +02:00
|
|
|
let layout = if binop_left_homogeneous(bin_op) { Some(dest.layout) } else { None };
|
2018-10-26 12:33:26 +02:00
|
|
|
let left = self.read_immediate(self.eval_operand(left, layout)?)?;
|
2018-08-20 20:08:24 +02:00
|
|
|
let layout = if binop_right_homogeneous(bin_op) { Some(left.layout) } else { None };
|
2018-10-26 12:33:26 +02:00
|
|
|
let right = self.read_immediate(self.eval_operand(right, layout)?)?;
|
2018-08-13 16:14:22 +02:00
|
|
|
self.binop_ignore_overflow(
|
2018-08-09 15:04:53 +02:00
|
|
|
bin_op,
|
|
|
|
left,
|
|
|
|
right,
|
|
|
|
dest,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckedBinaryOp(bin_op, ref left, ref right) => {
|
2018-08-20 20:08:24 +02:00
|
|
|
// Due to the extra boolean in the result, we can never reuse the `dest.layout`.
|
2018-10-26 12:33:26 +02:00
|
|
|
let left = self.read_immediate(self.eval_operand(left, None)?)?;
|
2018-08-20 20:08:24 +02:00
|
|
|
let layout = if binop_right_homogeneous(bin_op) { Some(left.layout) } else { None };
|
2018-10-26 12:33:26 +02:00
|
|
|
let right = self.read_immediate(self.eval_operand(right, layout)?)?;
|
2018-08-13 16:14:22 +02:00
|
|
|
self.binop_with_overflow(
|
2018-08-09 15:04:53 +02:00
|
|
|
bin_op,
|
|
|
|
left,
|
|
|
|
right,
|
|
|
|
dest,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnaryOp(un_op, ref operand) => {
|
2018-08-20 20:08:24 +02:00
|
|
|
// The operand always has the same type as the result.
|
2018-10-26 12:33:26 +02:00
|
|
|
let val = self.read_immediate(self.eval_operand(operand, Some(dest.layout))?)?;
|
2019-02-08 14:00:52 +01:00
|
|
|
let val = self.unary_op(un_op, val)?;
|
2018-08-13 16:14:22 +02:00
|
|
|
self.write_scalar(val, dest)?;
|
2018-08-09 15:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Aggregate(ref kind, ref operands) => {
|
|
|
|
let (dest, active_field_index) = match **kind {
|
2018-08-09 11:56:53 -04:00
|
|
|
mir::AggregateKind::Adt(adt_def, variant_index, _, _, active_field_index) => {
|
2018-08-25 11:07:03 +02:00
|
|
|
self.write_discriminant_index(variant_index, dest)?;
|
2018-08-09 15:04:53 +02:00
|
|
|
if adt_def.is_enum() {
|
|
|
|
(self.place_downcast(dest, variant_index)?, active_field_index)
|
|
|
|
} else {
|
|
|
|
(dest, active_field_index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (dest, None)
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i, operand) in operands.iter().enumerate() {
|
2018-08-20 15:21:04 +02:00
|
|
|
let op = self.eval_operand(operand, None)?;
|
2018-08-09 15:04:53 +02:00
|
|
|
// Ignore zero-sized fields.
|
2018-08-13 16:14:22 +02:00
|
|
|
if !op.layout.is_zst() {
|
2018-08-09 15:04:53 +02:00
|
|
|
let field_index = active_field_index.unwrap_or(i);
|
2018-08-13 16:14:22 +02:00
|
|
|
let field_dest = self.place_field(dest, field_index as u64)?;
|
|
|
|
self.copy_op(op, field_dest)?;
|
2018-08-09 15:04:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Repeat(ref operand, _) => {
|
2018-08-20 15:21:04 +02:00
|
|
|
let op = self.eval_operand(operand, None)?;
|
2018-08-13 16:14:22 +02:00
|
|
|
let dest = self.force_allocation(dest)?;
|
2018-11-03 22:57:53 +02:00
|
|
|
let length = dest.len(self)?;
|
2018-08-09 15:04:53 +02:00
|
|
|
|
|
|
|
if length > 0 {
|
2018-08-13 16:14:22 +02:00
|
|
|
// write the first
|
|
|
|
let first = self.mplace_field(dest, 0)?;
|
|
|
|
self.copy_op(op, first.into())?;
|
2018-08-09 15:04:53 +02:00
|
|
|
|
|
|
|
if length > 1 {
|
2018-08-13 16:14:22 +02:00
|
|
|
// copy the rest
|
|
|
|
let (dest, dest_align) = first.to_scalar_ptr_align();
|
2018-11-03 22:57:53 +02:00
|
|
|
let rest = dest.ptr_offset(first.layout.size, self)?;
|
2018-08-13 16:14:22 +02:00
|
|
|
self.memory.copy_repeatedly(
|
|
|
|
dest, dest_align, rest, dest_align, first.layout.size, length - 1, true
|
|
|
|
)?;
|
2018-08-09 15:04:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Len(ref place) => {
|
|
|
|
// FIXME(CTFE): don't allow computing the length of arrays in const eval
|
|
|
|
let src = self.eval_place(place)?;
|
2018-08-13 16:14:22 +02:00
|
|
|
let mplace = self.force_allocation(src)?;
|
2018-11-03 22:57:53 +02:00
|
|
|
let len = mplace.len(self)?;
|
2018-08-26 20:42:52 +02:00
|
|
|
let size = self.pointer_size();
|
2018-08-09 15:04:53 +02:00
|
|
|
self.write_scalar(
|
2018-08-26 20:42:52 +02:00
|
|
|
Scalar::from_uint(len, size),
|
2018-08-13 16:14:22 +02:00
|
|
|
dest,
|
2018-08-09 15:04:53 +02:00
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
2018-11-06 11:04:10 +01:00
|
|
|
Ref(_, _, ref place) => {
|
2018-08-09 15:04:53 +02:00
|
|
|
let src = self.eval_place(place)?;
|
2018-10-16 14:50:07 +02:00
|
|
|
let val = self.force_allocation(src)?;
|
2018-11-06 11:04:10 +01:00
|
|
|
self.write_immediate(val.to_ref(), dest)?;
|
2018-08-09 15:04:53 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:14:22 +02:00
|
|
|
NullaryOp(mir::NullOp::Box, _) => {
|
|
|
|
M::box_alloc(self, dest)?;
|
2018-08-09 15:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NullaryOp(mir::NullOp::SizeOf, ty) => {
|
2019-01-23 11:34:02 +01:00
|
|
|
let ty = self.monomorphize(ty)?;
|
2018-08-09 15:04:53 +02:00
|
|
|
let layout = self.layout_of(ty)?;
|
|
|
|
assert!(!layout.is_unsized(),
|
|
|
|
"SizeOf nullary MIR operator called for unsized type");
|
2018-08-26 20:42:52 +02:00
|
|
|
let size = self.pointer_size();
|
2018-08-09 15:04:53 +02:00
|
|
|
self.write_scalar(
|
2018-08-26 20:42:52 +02:00
|
|
|
Scalar::from_uint(layout.size.bytes(), size),
|
2018-08-13 16:14:22 +02:00
|
|
|
dest,
|
2018-08-09 15:04:53 +02:00
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
2018-12-29 21:10:00 +00:00
|
|
|
Cast(kind, ref operand, _) => {
|
2018-08-20 15:21:04 +02:00
|
|
|
let src = self.eval_operand(operand, None)?;
|
2018-08-13 16:14:22 +02:00
|
|
|
self.cast(src, kind, dest)?;
|
2018-08-09 15:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Discriminant(ref place) => {
|
2019-02-16 12:36:23 +01:00
|
|
|
let op = self.eval_place_to_op(place, None)?;
|
|
|
|
let discr_val = self.read_discriminant(op)?.0;
|
2018-08-26 20:42:52 +02:00
|
|
|
let size = dest.layout.size;
|
|
|
|
self.write_scalar(Scalar::from_uint(discr_val, size), dest)?;
|
2018-08-09 15:04:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:14:22 +02:00
|
|
|
self.dump_place(*dest);
|
2018-08-09 15:04:53 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-06-07 18:56:27 +02:00
|
|
|
fn terminator(&mut self, terminator: &mir::Terminator<'tcx>) -> InterpResult<'tcx> {
|
2018-12-19 10:10:39 +01:00
|
|
|
info!("{:?}", terminator.kind);
|
2018-02-06 18:33:59 +01:00
|
|
|
self.tcx.span = terminator.source_info.span;
|
2018-06-22 12:36:54 -07:00
|
|
|
self.memory.tcx.span = terminator.source_info.span;
|
2018-08-23 19:04:33 +02:00
|
|
|
|
|
|
|
let old_stack = self.cur_frame();
|
|
|
|
let old_bb = self.frame().block;
|
2016-06-23 00:02:47 -06:00
|
|
|
self.eval_terminator(terminator)?;
|
2018-06-22 12:36:54 -07:00
|
|
|
if !self.stack.is_empty() {
|
2018-08-23 19:04:33 +02:00
|
|
|
// This should change *something*
|
|
|
|
debug_assert!(self.cur_frame() != old_stack || self.frame().block != old_bb);
|
2018-12-19 10:10:39 +01:00
|
|
|
info!("// {:?}", self.frame().block);
|
2016-06-23 00:02:47 -06:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-06-01 17:05:20 +02:00
|
|
|
}
|