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