2018-08-25 19:22:00 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2018-08-27 11:34:35 +00:00
|
|
|
use rustc::{mir, ty};
|
2019-05-23 17:45:22 +00:00
|
|
|
use rustc::ty::Instance;
|
2018-08-27 11:34:35 +00:00
|
|
|
use rustc::ty::layout::{self, TyLayout, LayoutOf};
|
2018-08-18 10:14:03 +00:00
|
|
|
use syntax::source_map::Span;
|
2018-04-25 16:30:39 +00:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2016-06-23 07:03:58 +00:00
|
|
|
|
2018-08-24 12:40:55 +00:00
|
|
|
use super::{
|
2019-08-17 10:06:05 +00:00
|
|
|
InterpResult, PointerArithmetic,
|
2019-07-24 18:20:55 +00:00
|
|
|
InterpCx, Machine, OpTy, ImmTy, PlaceTy, MPlaceTy, StackPopCleanup, FnVal,
|
2018-08-24 12:40:55 +00:00
|
|
|
};
|
2016-06-23 07:03:58 +00:00
|
|
|
|
2019-06-27 09:36:01 +00:00
|
|
|
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
2018-08-23 17:04:33 +00:00
|
|
|
#[inline]
|
2019-06-07 16:56:27 +00:00
|
|
|
pub fn goto_block(&mut self, target: Option<mir::BasicBlock>) -> InterpResult<'tcx> {
|
2018-08-23 17:04:33 +00:00
|
|
|
if let Some(target) = target {
|
2019-10-20 16:51:25 +00:00
|
|
|
self.frame_mut().block = Some(target);
|
2018-08-23 17:04:33 +00:00
|
|
|
self.frame_mut().stmt = 0;
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2019-07-30 14:48:50 +00:00
|
|
|
throw_ub!(Unreachable)
|
2018-08-23 17:04:33 +00:00
|
|
|
}
|
2016-07-06 15:55:05 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 07:03:58 +00:00
|
|
|
pub(super) fn eval_terminator(
|
|
|
|
&mut self,
|
|
|
|
terminator: &mir::Terminator<'tcx>,
|
2019-06-07 16:56:27 +00:00
|
|
|
) -> InterpResult<'tcx> {
|
2017-12-12 16:14:49 +00:00
|
|
|
use rustc::mir::TerminatorKind::*;
|
2016-06-23 07:03:58 +00:00
|
|
|
match terminator.kind {
|
2016-11-27 03:13:22 +00:00
|
|
|
Return => {
|
2018-10-09 19:05:53 +00:00
|
|
|
self.frame().return_place.map(|r| self.dump_place(*r));
|
2019-04-17 01:04:54 +00:00
|
|
|
self.pop_stack_frame(/* unwinding */ false)?
|
2016-11-27 03:13:22 +00:00
|
|
|
}
|
2016-06-23 07:03:58 +00:00
|
|
|
|
2018-08-23 17:04:33 +00:00
|
|
|
Goto { target } => self.goto_block(Some(target))?,
|
2016-06-23 07:03:58 +00:00
|
|
|
|
2017-08-10 15:48:38 +00:00
|
|
|
SwitchInt {
|
|
|
|
ref discr,
|
|
|
|
ref values,
|
|
|
|
ref targets,
|
|
|
|
..
|
|
|
|
} => {
|
2018-10-26 10:33:26 +00:00
|
|
|
let discr = self.read_immediate(self.eval_operand(discr, None)?)?;
|
2018-08-15 18:18:40 +00:00
|
|
|
trace!("SwitchInt({:?})", *discr);
|
2016-06-23 07:03:58 +00:00
|
|
|
|
|
|
|
// Branch to the `otherwise` case by default, if no match is found.
|
|
|
|
let mut target_block = targets[targets.len() - 1];
|
|
|
|
|
2018-01-16 08:24:38 +00:00
|
|
|
for (index, &const_int) in values.iter().enumerate() {
|
2018-08-27 11:34:35 +00:00
|
|
|
// Compare using binary_op, to also support pointer values
|
2019-08-10 17:40:56 +00:00
|
|
|
let res = self.overflowing_binary_op(mir::BinOp::Eq,
|
2019-02-08 13:00:52 +00:00
|
|
|
discr,
|
2019-08-17 10:06:05 +00:00
|
|
|
ImmTy::from_uint(const_int, discr.layout),
|
2019-08-10 17:40:56 +00:00
|
|
|
)?.0;
|
2018-08-13 14:14:22 +00:00
|
|
|
if res.to_bool()? {
|
2016-06-23 07:03:58 +00:00
|
|
|
target_block = targets[index];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 17:04:33 +00:00
|
|
|
self.goto_block(Some(target_block))?;
|
2016-06-23 07:03:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 15:48:38 +00:00
|
|
|
Call {
|
|
|
|
ref func,
|
|
|
|
ref args,
|
|
|
|
ref destination,
|
2019-04-17 01:04:54 +00:00
|
|
|
ref cleanup,
|
2017-08-10 15:48:38 +00:00
|
|
|
..
|
|
|
|
} => {
|
2018-08-23 17:04:33 +00:00
|
|
|
let (dest, ret) = match *destination {
|
|
|
|
Some((ref lv, target)) => (Some(self.eval_place(lv)?), Some(target)),
|
|
|
|
None => (None, None),
|
2016-07-06 15:55:05 +00:00
|
|
|
};
|
2016-06-23 07:03:58 +00:00
|
|
|
|
2018-08-20 13:21:04 +00:00
|
|
|
let func = self.eval_operand(func, None)?;
|
2019-09-16 18:08:35 +00:00
|
|
|
let (fn_val, abi) = match func.layout.ty.kind {
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::FnPtr(sig) => {
|
2018-08-27 11:34:35 +00:00
|
|
|
let caller_abi = sig.abi();
|
2019-07-01 09:16:18 +00:00
|
|
|
let fn_ptr = self.read_scalar(func)?.not_undef()?;
|
2019-06-30 11:51:18 +00:00
|
|
|
let fn_val = self.memory.get_fn(fn_ptr)?;
|
|
|
|
(fn_val, caller_abi)
|
2017-08-10 15:48:38 +00:00
|
|
|
}
|
2018-08-24 12:40:55 +00:00
|
|
|
ty::FnDef(def_id, substs) => {
|
|
|
|
let sig = func.layout.ty.fn_sig(*self.tcx);
|
2019-06-30 11:51:18 +00:00
|
|
|
(FnVal::Instance(self.resolve(def_id, substs)?), sig.abi())
|
2018-08-24 12:40:55 +00:00
|
|
|
},
|
2016-11-27 06:58:01 +00:00
|
|
|
_ => {
|
2019-08-02 21:41:24 +00:00
|
|
|
bug!("invalid callee of type {:?}", func.layout.ty)
|
2016-11-27 06:58:01 +00:00
|
|
|
}
|
2017-02-28 11:35:00 +00:00
|
|
|
};
|
2018-08-13 14:14:22 +00:00
|
|
|
let args = self.eval_operands(args)?;
|
2017-08-10 15:48:38 +00:00
|
|
|
self.eval_fn_call(
|
2019-06-30 11:51:18 +00:00
|
|
|
fn_val,
|
2018-08-27 11:34:35 +00:00
|
|
|
terminator.source_info.span,
|
|
|
|
abi,
|
2018-08-13 14:14:22 +00:00
|
|
|
&args[..],
|
2018-08-23 17:04:33 +00:00
|
|
|
dest,
|
|
|
|
ret,
|
2019-04-17 01:04:54 +00:00
|
|
|
*cleanup
|
2017-08-10 15:48:38 +00:00
|
|
|
)?;
|
2016-06-23 07:03:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 15:48:38 +00:00
|
|
|
Drop {
|
|
|
|
ref location,
|
|
|
|
target,
|
2019-04-17 01:04:54 +00:00
|
|
|
unwind,
|
2017-08-10 15:48:38 +00:00
|
|
|
} => {
|
2017-07-25 09:32:48 +00:00
|
|
|
// FIXME(CTFE): forbid drop in const eval
|
2017-12-06 08:25:29 +00:00
|
|
|
let place = self.eval_place(location)?;
|
2018-08-13 14:14:22 +00:00
|
|
|
let ty = place.layout.ty;
|
2017-08-24 14:04:50 +00:00
|
|
|
trace!("TerminatorKind::drop: {:?}, type {}", location, ty);
|
2017-03-22 12:13:52 +00:00
|
|
|
|
2019-05-23 17:45:22 +00:00
|
|
|
let instance = Instance::resolve_drop_in_place(*self.tcx, ty);
|
2018-08-15 22:18:09 +00:00
|
|
|
self.drop_in_place(
|
2017-12-06 08:25:29 +00:00
|
|
|
place,
|
2017-08-10 15:48:38 +00:00
|
|
|
instance,
|
|
|
|
terminator.source_info.span,
|
2017-08-24 14:04:50 +00:00
|
|
|
target,
|
2019-04-17 01:04:54 +00:00
|
|
|
unwind
|
2017-08-10 15:48:38 +00:00
|
|
|
)?;
|
2017-03-22 12:13:52 +00:00
|
|
|
}
|
2016-06-23 07:03:58 +00:00
|
|
|
|
2017-08-10 15:48:38 +00:00
|
|
|
Assert {
|
|
|
|
ref cond,
|
|
|
|
expected,
|
|
|
|
ref msg,
|
|
|
|
target,
|
|
|
|
..
|
|
|
|
} => {
|
2018-10-26 10:33:26 +00:00
|
|
|
let cond_val = self.read_immediate(self.eval_operand(cond, None)?)?
|
2018-08-23 17:27:14 +00:00
|
|
|
.to_scalar()?.to_bool()?;
|
2016-09-19 08:19:31 +00:00
|
|
|
if expected == cond_val {
|
2018-08-23 17:04:33 +00:00
|
|
|
self.goto_block(Some(target))?;
|
2016-06-23 07:03:58 +00:00
|
|
|
} else {
|
2018-08-27 11:34:35 +00:00
|
|
|
// Compute error message
|
2019-07-29 07:58:55 +00:00
|
|
|
use rustc::mir::interpret::PanicInfo::*;
|
2019-07-31 07:18:54 +00:00
|
|
|
return Err(match msg {
|
2019-07-24 08:24:55 +00:00
|
|
|
BoundsCheck { ref len, ref index } => {
|
2019-07-31 07:18:54 +00:00
|
|
|
let len = self
|
|
|
|
.read_immediate(self.eval_operand(len, None)?)
|
|
|
|
.expect("can't eval len")
|
|
|
|
.to_scalar()?
|
2019-10-11 20:33:55 +00:00
|
|
|
.to_bits(self.memory.pointer_size())? as u64;
|
2019-07-31 07:18:54 +00:00
|
|
|
let index = self
|
|
|
|
.read_immediate(self.eval_operand(index, None)?)
|
|
|
|
.expect("can't eval index")
|
|
|
|
.to_scalar()?
|
2019-10-11 20:33:55 +00:00
|
|
|
.to_bits(self.memory.pointer_size())? as u64;
|
2019-07-31 07:18:54 +00:00
|
|
|
err_panic!(BoundsCheck { len, index })
|
2017-08-10 15:48:38 +00:00
|
|
|
}
|
2019-07-31 07:18:54 +00:00
|
|
|
Overflow(op) => err_panic!(Overflow(*op)),
|
|
|
|
OverflowNeg => err_panic!(OverflowNeg),
|
|
|
|
DivisionByZero => err_panic!(DivisionByZero),
|
|
|
|
RemainderByZero => err_panic!(RemainderByZero),
|
|
|
|
GeneratorResumedAfterReturn => err_panic!(GeneratorResumedAfterReturn),
|
|
|
|
GeneratorResumedAfterPanic => err_panic!(GeneratorResumedAfterPanic),
|
|
|
|
Panic { .. } => bug!("`Panic` variant cannot occur in MIR"),
|
|
|
|
}
|
|
|
|
.into());
|
2016-06-23 07:03:58 +00:00
|
|
|
}
|
2017-08-10 15:48:38 +00:00
|
|
|
}
|
2016-06-23 07:03:58 +00:00
|
|
|
|
2019-04-17 01:04:54 +00:00
|
|
|
|
|
|
|
// When we encounter Resume, we've finished unwinding
|
|
|
|
// cleanup for the current stack frame. We pop it in order
|
|
|
|
// to continue unwinding the next frame
|
|
|
|
Resume => {
|
|
|
|
trace!("unwinding: resuming from cleanup");
|
2019-09-19 17:47:59 +00:00
|
|
|
// By definition, a Resume terminator means
|
2019-04-17 01:04:54 +00:00
|
|
|
// that we're unwinding
|
|
|
|
self.pop_stack_frame(/* unwinding */ true)?;
|
|
|
|
return Ok(())
|
|
|
|
},
|
|
|
|
|
2018-08-27 11:34:35 +00:00
|
|
|
Yield { .. } |
|
|
|
|
GeneratorDrop |
|
|
|
|
DropAndReplace { .. } |
|
|
|
|
Abort => unimplemented!("{:#?}", terminator.kind),
|
2018-08-22 19:58:39 +00:00
|
|
|
FalseEdges { .. } => bug!("should have been eliminated by\
|
|
|
|
`simplify_branches` mir pass"),
|
|
|
|
FalseUnwind { .. } => bug!("should have been eliminated by\
|
|
|
|
`simplify_branches` mir pass"),
|
2019-07-30 14:48:50 +00:00
|
|
|
Unreachable => throw_ub!(Unreachable),
|
2016-06-23 07:03:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-08-27 11:34:35 +00:00
|
|
|
fn check_argument_compat(
|
2018-11-22 16:38:59 +00:00
|
|
|
rust_abi: bool,
|
2018-08-27 11:34:35 +00:00
|
|
|
caller: TyLayout<'tcx>,
|
|
|
|
callee: TyLayout<'tcx>,
|
|
|
|
) -> bool {
|
|
|
|
if caller.ty == callee.ty {
|
|
|
|
// No question
|
|
|
|
return true;
|
2017-05-27 03:02:51 +00:00
|
|
|
}
|
2018-11-22 16:38:59 +00:00
|
|
|
if !rust_abi {
|
|
|
|
// Don't risk anything
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-27 11:34:35 +00:00
|
|
|
// Compare layout
|
|
|
|
match (&caller.abi, &callee.abi) {
|
2018-11-22 16:38:59 +00:00
|
|
|
// Different valid ranges are okay (once we enforce validity,
|
|
|
|
// that will take care to make it UB to leave the range, just
|
|
|
|
// like for transmute).
|
2018-08-27 11:34:35 +00:00
|
|
|
(layout::Abi::Scalar(ref caller), layout::Abi::Scalar(ref callee)) =>
|
|
|
|
caller.value == callee.value,
|
2018-11-22 12:43:05 +00:00
|
|
|
(layout::Abi::ScalarPair(ref caller1, ref caller2),
|
|
|
|
layout::Abi::ScalarPair(ref callee1, ref callee2)) =>
|
|
|
|
caller1.value == callee1.value && caller2.value == callee2.value,
|
2018-08-27 11:34:35 +00:00
|
|
|
// Be conservative
|
|
|
|
_ => false
|
2017-05-27 03:02:51 +00:00
|
|
|
}
|
2018-08-27 11:34:35 +00:00
|
|
|
}
|
2017-05-27 03:02:51 +00:00
|
|
|
|
2018-08-27 11:34:35 +00:00
|
|
|
/// Pass a single argument, checking the types for compatibility.
|
|
|
|
fn pass_argument(
|
|
|
|
&mut self,
|
2018-11-22 16:38:59 +00:00
|
|
|
rust_abi: bool,
|
2018-09-21 21:32:59 +00:00
|
|
|
caller_arg: &mut impl Iterator<Item=OpTy<'tcx, M::PointerTag>>,
|
|
|
|
callee_arg: PlaceTy<'tcx, M::PointerTag>,
|
2019-06-07 16:56:27 +00:00
|
|
|
) -> InterpResult<'tcx> {
|
2018-11-22 16:38:59 +00:00
|
|
|
if rust_abi && callee_arg.layout.is_zst() {
|
2018-08-27 11:34:35 +00:00
|
|
|
// Nothing to do.
|
|
|
|
trace!("Skipping callee ZST");
|
|
|
|
return Ok(());
|
2017-05-27 03:02:51 +00:00
|
|
|
}
|
2018-08-27 11:34:35 +00:00
|
|
|
let caller_arg = caller_arg.next()
|
2019-07-30 10:38:32 +00:00
|
|
|
.ok_or_else(|| err_unsup!(FunctionArgCountMismatch)) ?;
|
2018-11-22 16:38:59 +00:00
|
|
|
if rust_abi {
|
2018-08-27 11:34:35 +00:00
|
|
|
debug_assert!(!caller_arg.layout.is_zst(), "ZSTs must have been already filtered out");
|
|
|
|
}
|
|
|
|
// Now, check
|
2018-11-22 16:38:59 +00:00
|
|
|
if !Self::check_argument_compat(rust_abi, caller_arg.layout, callee_arg.layout) {
|
2019-07-30 14:48:50 +00:00
|
|
|
throw_unsup!(FunctionArgMismatch(caller_arg.layout.ty, callee_arg.layout.ty))
|
2018-08-27 11:34:35 +00:00
|
|
|
}
|
2018-10-09 15:06:57 +00:00
|
|
|
// We allow some transmutes here
|
|
|
|
self.copy_op_transmute(caller_arg, callee_arg)
|
2017-05-27 03:02:51 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 12:40:55 +00:00
|
|
|
/// Call this function -- pushing the stack frame and initializing the arguments.
|
2016-06-23 07:03:58 +00:00
|
|
|
fn eval_fn_call(
|
|
|
|
&mut self,
|
2019-06-30 11:51:18 +00:00
|
|
|
fn_val: FnVal<'tcx, M::ExtraFnVal>,
|
2018-08-27 11:34:35 +00:00
|
|
|
span: Span,
|
|
|
|
caller_abi: Abi,
|
2018-09-21 21:32:59 +00:00
|
|
|
args: &[OpTy<'tcx, M::PointerTag>],
|
|
|
|
dest: Option<PlaceTy<'tcx, M::PointerTag>>,
|
2018-08-23 17:04:33 +00:00
|
|
|
ret: Option<mir::BasicBlock>,
|
2019-04-17 01:04:54 +00:00
|
|
|
unwind: Option<mir::BasicBlock>
|
2019-06-07 16:56:27 +00:00
|
|
|
) -> InterpResult<'tcx> {
|
2019-06-30 11:51:18 +00:00
|
|
|
trace!("eval_fn_call: {:#?}", fn_val);
|
|
|
|
|
2019-06-30 13:06:13 +00:00
|
|
|
let instance = match fn_val {
|
|
|
|
FnVal::Instance(instance) => instance,
|
|
|
|
FnVal::Other(extra) => {
|
|
|
|
return M::call_extra_fn(self, extra, args, dest, ret);
|
|
|
|
}
|
|
|
|
};
|
2018-08-24 12:40:55 +00:00
|
|
|
|
2017-03-22 12:13:52 +00:00
|
|
|
match instance.def {
|
|
|
|
ty::InstanceDef::Intrinsic(..) => {
|
2019-11-16 10:44:32 +00:00
|
|
|
if caller_abi != Abi::RustIntrinsic && caller_abi != Abi::PlatformIntrinsic {
|
|
|
|
throw_ub_format!("Rust intrinsic called with an ABI other than \
|
|
|
|
`RustIntrinsic` and `PlatformIntrinsic`.");
|
|
|
|
}
|
|
|
|
|
2019-10-30 01:03:30 +00:00
|
|
|
let old_stack = self.cur_frame();
|
2019-11-05 05:22:49 +00:00
|
|
|
let old_bb = self.frame().block;
|
2019-11-05 05:06:05 +00:00
|
|
|
M::call_intrinsic(self, span, instance, args, dest, ret, unwind)?;
|
2018-08-23 17:04:33 +00:00
|
|
|
// No stack frame gets pushed, the main loop will just act as if the
|
|
|
|
// call completed.
|
2019-10-30 01:03:30 +00:00
|
|
|
if ret.is_some() {
|
|
|
|
self.goto_block(ret)?;
|
|
|
|
} else {
|
|
|
|
// If this intrinsic call doesn't have a ret block,
|
|
|
|
// then the intrinsic implementation should have
|
|
|
|
// changed the stack frame (otherwise, we'll end
|
|
|
|
// up trying to execute this intrinsic call again)
|
2019-11-05 05:22:49 +00:00
|
|
|
debug_assert!(self.cur_frame() != old_stack || self.frame().block != old_bb);
|
2019-10-30 01:03:30 +00:00
|
|
|
}
|
2019-04-17 01:04:54 +00:00
|
|
|
if let Some(dest) = dest {
|
|
|
|
self.dump_place(*dest)
|
|
|
|
}
|
2017-03-22 13:19:29 +00:00
|
|
|
Ok(())
|
2017-08-10 15:48:38 +00:00
|
|
|
}
|
2018-09-10 13:54:48 +00:00
|
|
|
ty::InstanceDef::VtableShim(..) |
|
2019-10-04 02:10:34 +00:00
|
|
|
ty::InstanceDef::ReifyShim(..) |
|
2018-08-24 12:40:55 +00:00
|
|
|
ty::InstanceDef::ClosureOnceShim { .. } |
|
2017-08-23 15:46:36 +00:00
|
|
|
ty::InstanceDef::FnPtrShim(..) |
|
|
|
|
ty::InstanceDef::DropGlue(..) |
|
2017-08-23 15:24:38 +00:00
|
|
|
ty::InstanceDef::CloneShim(..) |
|
2017-03-22 12:13:52 +00:00
|
|
|
ty::InstanceDef::Item(_) => {
|
2018-08-27 11:34:35 +00:00
|
|
|
// ABI check
|
|
|
|
{
|
|
|
|
let callee_abi = {
|
|
|
|
let instance_ty = instance.ty(*self.tcx);
|
2019-09-16 18:08:35 +00:00
|
|
|
match instance_ty.kind {
|
2018-08-27 11:34:35 +00:00
|
|
|
ty::FnDef(..) =>
|
|
|
|
instance_ty.fn_sig(*self.tcx).abi(),
|
|
|
|
ty::Closure(..) => Abi::RustCall,
|
|
|
|
ty::Generator(..) => Abi::Rust,
|
|
|
|
_ => bug!("unexpected callee ty: {:?}", instance_ty),
|
|
|
|
}
|
|
|
|
};
|
2019-07-14 09:58:12 +00:00
|
|
|
let normalize_abi = |abi| match abi {
|
|
|
|
Abi::Rust | Abi::RustCall | Abi::RustIntrinsic | Abi::PlatformIntrinsic =>
|
|
|
|
// These are all the same ABI, really.
|
|
|
|
Abi::Rust,
|
|
|
|
abi =>
|
|
|
|
abi,
|
|
|
|
};
|
2018-08-27 11:34:35 +00:00
|
|
|
if normalize_abi(caller_abi) != normalize_abi(callee_abi) {
|
2019-07-30 14:48:50 +00:00
|
|
|
throw_unsup!(FunctionAbiMismatch(caller_abi, callee_abi))
|
2018-08-27 11:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need MIR for this fn
|
2019-04-17 01:04:54 +00:00
|
|
|
let body = match M::find_fn(self, instance, args, dest, ret, unwind)? {
|
2019-06-03 22:26:48 +00:00
|
|
|
Some(body) => body,
|
2018-08-23 17:04:33 +00:00
|
|
|
None => return Ok(()),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.push_stack_frame(
|
|
|
|
instance,
|
|
|
|
span,
|
2019-06-03 22:26:48 +00:00
|
|
|
body,
|
2018-10-09 19:05:53 +00:00
|
|
|
dest,
|
2019-04-17 01:04:54 +00:00
|
|
|
StackPopCleanup::Goto { ret, unwind }
|
2018-08-23 17:04:33 +00:00
|
|
|
)?;
|
2017-03-23 12:36:13 +00:00
|
|
|
|
2018-08-27 11:34:35 +00:00
|
|
|
// We want to pop this frame again in case there was an error, to put
|
|
|
|
// the blame in the right location. Until the 2018 edition is used in
|
|
|
|
// the compiler, we have to do this with an immediately invoked function.
|
|
|
|
let res = (||{
|
|
|
|
trace!(
|
|
|
|
"caller ABI: {:?}, args: {:#?}",
|
|
|
|
caller_abi,
|
|
|
|
args.iter()
|
|
|
|
.map(|arg| (arg.layout.ty, format!("{:?}", **arg)))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
);
|
|
|
|
trace!(
|
|
|
|
"spread_arg: {:?}, locals: {:#?}",
|
2019-06-03 22:26:48 +00:00
|
|
|
body.spread_arg,
|
|
|
|
body.args_iter()
|
2018-08-27 11:34:35 +00:00
|
|
|
.map(|local|
|
2019-01-30 13:55:31 +00:00
|
|
|
(local, self.layout_of_local(self.frame(), local, None).unwrap().ty)
|
2018-08-25 19:22:00 +00:00
|
|
|
)
|
2018-08-27 11:34:35 +00:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Figure out how to pass which arguments.
|
2019-04-06 21:58:59 +00:00
|
|
|
// The Rust ABI is special: ZST get skipped.
|
2018-11-22 16:38:59 +00:00
|
|
|
let rust_abi = match caller_abi {
|
2018-08-27 11:34:35 +00:00
|
|
|
Abi::Rust | Abi::RustCall => true,
|
|
|
|
_ => false
|
2018-08-25 19:22:00 +00:00
|
|
|
};
|
2019-04-06 21:58:59 +00:00
|
|
|
// We have two iterators: Where the arguments come from,
|
|
|
|
// and where they go to.
|
2018-08-24 12:40:55 +00:00
|
|
|
|
2018-08-27 11:34:35 +00:00
|
|
|
// For where they come from: If the ABI is RustCall, we untuple the
|
|
|
|
// last incoming argument. These two iterators do not have the same type,
|
|
|
|
// so to keep the code paths uniform we accept an allocation
|
|
|
|
// (for RustCall ABI only).
|
2019-02-07 21:28:15 +00:00
|
|
|
let caller_args : Cow<'_, [OpTy<'tcx, M::PointerTag>]> =
|
2018-08-27 11:34:35 +00:00
|
|
|
if caller_abi == Abi::RustCall && !args.is_empty() {
|
|
|
|
// Untuple
|
|
|
|
let (&untuple_arg, args) = args.split_last().unwrap();
|
|
|
|
trace!("eval_fn_call: Will pass last argument by untupling");
|
|
|
|
Cow::from(args.iter().map(|&a| Ok(a))
|
|
|
|
.chain((0..untuple_arg.layout.fields.count()).into_iter()
|
|
|
|
.map(|i| self.operand_field(untuple_arg, i as u64))
|
|
|
|
)
|
2019-06-07 16:56:27 +00:00
|
|
|
.collect::<InterpResult<'_, Vec<OpTy<'tcx, M::PointerTag>>>>()?)
|
2018-08-27 11:34:35 +00:00
|
|
|
} else {
|
|
|
|
// Plain arg passing
|
|
|
|
Cow::from(args)
|
|
|
|
};
|
|
|
|
// Skip ZSTs
|
|
|
|
let mut caller_iter = caller_args.iter()
|
2018-11-22 16:38:59 +00:00
|
|
|
.filter(|op| !rust_abi || !op.layout.is_zst())
|
2018-08-27 11:34:35 +00:00
|
|
|
.map(|op| *op);
|
|
|
|
|
|
|
|
// Now we have to spread them out across the callee's locals,
|
|
|
|
// taking into account the `spread_arg`. If we could write
|
|
|
|
// this is a single iterator (that handles `spread_arg`), then
|
|
|
|
// `pass_argument` would be the loop body. It takes care to
|
|
|
|
// not advance `caller_iter` for ZSTs.
|
2019-06-03 22:26:48 +00:00
|
|
|
let mut locals_iter = body.args_iter();
|
2018-08-27 11:34:35 +00:00
|
|
|
while let Some(local) = locals_iter.next() {
|
2019-02-22 04:24:03 +00:00
|
|
|
let dest = self.eval_place(
|
2019-06-24 15:46:09 +00:00
|
|
|
&mir::Place::from(local)
|
2019-02-22 04:24:03 +00:00
|
|
|
)?;
|
2019-06-03 22:26:48 +00:00
|
|
|
if Some(local) == body.spread_arg {
|
2018-08-27 11:34:35 +00:00
|
|
|
// Must be a tuple
|
|
|
|
for i in 0..dest.layout.fields.count() {
|
|
|
|
let dest = self.place_field(dest, i as u64)?;
|
2018-11-22 16:38:59 +00:00
|
|
|
self.pass_argument(rust_abi, &mut caller_iter, dest)?;
|
2018-08-27 11:34:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Normal argument
|
2018-11-22 16:38:59 +00:00
|
|
|
self.pass_argument(rust_abi, &mut caller_iter, dest)?;
|
2018-08-24 12:40:55 +00:00
|
|
|
}
|
2017-03-23 12:36:13 +00:00
|
|
|
}
|
2018-08-27 11:34:35 +00:00
|
|
|
// Now we should have no more caller args
|
|
|
|
if caller_iter.next().is_some() {
|
2019-04-06 21:58:59 +00:00
|
|
|
trace!("Caller has passed too many args");
|
2019-07-30 14:48:50 +00:00
|
|
|
throw_unsup!(FunctionArgCountMismatch)
|
2018-08-27 11:34:35 +00:00
|
|
|
}
|
2018-10-02 19:16:35 +00:00
|
|
|
// Don't forget to check the return type!
|
|
|
|
if let Some(caller_ret) = dest {
|
2019-02-22 04:24:03 +00:00
|
|
|
let callee_ret = self.eval_place(
|
2019-07-29 22:07:28 +00:00
|
|
|
&mir::Place::return_place()
|
2019-02-22 04:24:03 +00:00
|
|
|
)?;
|
2018-11-22 16:38:59 +00:00
|
|
|
if !Self::check_argument_compat(
|
|
|
|
rust_abi,
|
|
|
|
caller_ret.layout,
|
|
|
|
callee_ret.layout,
|
|
|
|
) {
|
2019-07-30 14:48:50 +00:00
|
|
|
throw_unsup!(
|
2019-07-27 12:19:12 +00:00
|
|
|
FunctionRetMismatch(caller_ret.layout.ty, callee_ret.layout.ty)
|
2019-07-30 14:48:50 +00:00
|
|
|
)
|
2018-10-02 19:16:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-06-25 02:39:23 +00:00
|
|
|
let local = mir::RETURN_PLACE;
|
2019-08-10 11:08:47 +00:00
|
|
|
let callee_layout = self.layout_of_local(self.frame(), local, None)?;
|
|
|
|
if !callee_layout.abi.is_uninhabited() {
|
|
|
|
throw_unsup!(FunctionRetMismatch(
|
|
|
|
self.tcx.types.never, callee_layout.ty
|
|
|
|
))
|
2018-10-09 16:16:27 +00:00
|
|
|
}
|
2018-10-02 19:16:35 +00:00
|
|
|
}
|
2018-08-27 11:34:35 +00:00
|
|
|
Ok(())
|
|
|
|
})();
|
|
|
|
match res {
|
|
|
|
Err(err) => {
|
|
|
|
self.stack.pop();
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
Ok(v) => Ok(v)
|
2017-03-23 12:36:13 +00:00
|
|
|
}
|
2017-08-10 15:48:38 +00:00
|
|
|
}
|
2017-08-23 15:46:36 +00:00
|
|
|
// cannot use the shim here, because that will only result in infinite recursion
|
2017-03-23 13:24:02 +00:00
|
|
|
ty::InstanceDef::Virtual(_, idx) => {
|
2019-04-07 17:54:43 +00:00
|
|
|
let mut args = args.to_vec();
|
|
|
|
// We have to implement all "object safe receivers". Currently we
|
|
|
|
// support built-in pointers (&, &mut, Box) as well as unsized-self. We do
|
|
|
|
// not yet support custom self types.
|
|
|
|
// Also see librustc_codegen_llvm/abi.rs and librustc_codegen_llvm/mir/block.rs.
|
|
|
|
let receiver_place = match args[0].layout.ty.builtin_deref(true) {
|
|
|
|
Some(_) => {
|
|
|
|
// Built-in pointer.
|
|
|
|
self.deref_operand(args[0])?
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// Unsized self.
|
2019-07-06 10:46:08 +00:00
|
|
|
args[0].assert_mem_place()
|
2019-04-07 17:54:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// Find and consult vtable
|
2019-06-23 12:26:36 +00:00
|
|
|
let vtable = receiver_place.vtable();
|
2019-04-17 01:04:54 +00:00
|
|
|
let drop_fn = self.get_vtable_slot(vtable, idx)?;
|
2018-08-16 08:30:56 +00:00
|
|
|
|
2019-04-07 17:54:43 +00:00
|
|
|
// `*mut receiver_place.layout.ty` is almost the layout that we
|
|
|
|
// want for args[0]: We have to project to field 0 because we want
|
|
|
|
// a thin pointer.
|
|
|
|
assert!(receiver_place.layout.is_unsized());
|
|
|
|
let receiver_ptr_ty = self.tcx.mk_mut_ptr(receiver_place.layout.ty);
|
|
|
|
let this_receiver_ptr = self.layout_of(receiver_ptr_ty)?.field(self, 0)?;
|
|
|
|
// Adjust receiver argument.
|
|
|
|
args[0] = OpTy::from(ImmTy {
|
|
|
|
layout: this_receiver_ptr,
|
2019-07-24 18:20:55 +00:00
|
|
|
imm: receiver_place.ptr.into()
|
2019-02-08 11:20:55 +00:00
|
|
|
});
|
2018-08-16 08:30:56 +00:00
|
|
|
trace!("Patched self operand to {:#?}", args[0]);
|
2017-03-23 13:57:11 +00:00
|
|
|
// recurse with concrete function
|
2019-04-17 01:04:54 +00:00
|
|
|
self.eval_fn_call(drop_fn, span, caller_abi, &args, dest, ret, unwind)
|
2017-08-10 15:48:38 +00:00
|
|
|
}
|
2017-02-28 11:35:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-24 12:44:30 +00:00
|
|
|
|
|
|
|
fn drop_in_place(
|
|
|
|
&mut self,
|
2018-09-21 21:32:59 +00:00
|
|
|
place: PlaceTy<'tcx, M::PointerTag>,
|
2018-08-24 12:44:30 +00:00
|
|
|
instance: ty::Instance<'tcx>,
|
|
|
|
span: Span,
|
|
|
|
target: mir::BasicBlock,
|
2019-04-17 01:04:54 +00:00
|
|
|
unwind: Option<mir::BasicBlock>
|
2019-06-07 16:56:27 +00:00
|
|
|
) -> InterpResult<'tcx> {
|
2018-08-24 12:44:30 +00:00
|
|
|
trace!("drop_in_place: {:?},\n {:?}, {:?}", *place, place.layout.ty, instance);
|
|
|
|
// We take the address of the object. This may well be unaligned, which is fine
|
|
|
|
// for us here. However, unaligned accesses will probably make the actual drop
|
|
|
|
// implementation fail -- a problem shared by rustc.
|
|
|
|
let place = self.force_allocation(place)?;
|
|
|
|
|
2019-09-16 18:08:35 +00:00
|
|
|
let (instance, place) = match place.layout.ty.kind {
|
2018-08-24 12:44:30 +00:00
|
|
|
ty::Dynamic(..) => {
|
|
|
|
// Dropping a trait object.
|
2018-08-25 12:36:24 +00:00
|
|
|
self.unpack_dyn_trait(place)?
|
2018-08-24 12:44:30 +00:00
|
|
|
}
|
|
|
|
_ => (instance, place),
|
|
|
|
};
|
|
|
|
|
2019-02-08 11:20:55 +00:00
|
|
|
let arg = ImmTy {
|
|
|
|
imm: place.to_ref(),
|
2018-08-24 12:44:30 +00:00
|
|
|
layout: self.layout_of(self.tcx.mk_mut_ptr(place.layout.ty))?,
|
|
|
|
};
|
|
|
|
|
2018-09-10 02:07:13 +00:00
|
|
|
let ty = self.tcx.mk_unit(); // return type is ()
|
2018-11-03 20:57:53 +00:00
|
|
|
let dest = MPlaceTy::dangling(self.layout_of(ty)?, self);
|
2018-08-24 12:44:30 +00:00
|
|
|
|
|
|
|
self.eval_fn_call(
|
2019-06-30 11:51:18 +00:00
|
|
|
FnVal::Instance(instance),
|
2018-08-27 11:34:35 +00:00
|
|
|
span,
|
|
|
|
Abi::Rust,
|
2019-02-08 11:20:55 +00:00
|
|
|
&[arg.into()],
|
2018-10-09 19:05:53 +00:00
|
|
|
Some(dest.into()),
|
2018-08-24 12:44:30 +00:00
|
|
|
Some(target),
|
2019-04-17 01:04:54 +00:00
|
|
|
unwind
|
2018-08-24 12:44:30 +00:00
|
|
|
)
|
|
|
|
}
|
2016-09-20 10:52:01 +00:00
|
|
|
}
|