2018-08-25 21:22:00 +02:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2018-08-27 13:34:35 +02:00
|
|
|
use rustc::{mir, ty};
|
2019-05-23 12:45:22 -05:00
|
|
|
use rustc::ty::Instance;
|
2018-08-27 13:34:35 +02:00
|
|
|
use rustc::ty::layout::{self, TyLayout, LayoutOf};
|
2018-08-18 12:14:03 +02:00
|
|
|
use syntax::source_map::Span;
|
2018-04-25 19:30:39 +03:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2018-08-24 14:40:55 +02:00
|
|
|
use super::{
|
2019-06-30 13:51:18 +02:00
|
|
|
InterpResult, PointerArithmetic, InterpError, Scalar,
|
|
|
|
InterpCx, Machine, Immediate, OpTy, ImmTy, PlaceTy, MPlaceTy, StackPopCleanup, FnVal,
|
2018-08-24 14:40:55 +02:00
|
|
|
};
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2019-06-27 11:36:01 +02:00
|
|
|
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
2018-08-23 19:04:33 +02:00
|
|
|
#[inline]
|
2019-06-07 18:56:27 +02:00
|
|
|
pub fn goto_block(&mut self, target: Option<mir::BasicBlock>) -> InterpResult<'tcx> {
|
2018-08-23 19:04:33 +02:00
|
|
|
if let Some(target) = target {
|
|
|
|
self.frame_mut().block = target;
|
|
|
|
self.frame_mut().stmt = 0;
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
err!(Unreachable)
|
|
|
|
}
|
2016-07-06 17:55:05 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 01:03:58 -06:00
|
|
|
pub(super) fn eval_terminator(
|
|
|
|
&mut self,
|
|
|
|
terminator: &mir::Terminator<'tcx>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2017-12-12 17:14:49 +01:00
|
|
|
use rustc::mir::TerminatorKind::*;
|
2016-06-23 01:03:58 -06:00
|
|
|
match terminator.kind {
|
2016-11-26 19:13:22 -08:00
|
|
|
Return => {
|
2018-10-09 21:05:53 +02:00
|
|
|
self.frame().return_place.map(|r| self.dump_place(*r));
|
2016-11-26 19:13:22 -08:00
|
|
|
self.pop_stack_frame()?
|
|
|
|
}
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2018-08-23 19:04:33 +02:00
|
|
|
Goto { target } => self.goto_block(Some(target))?,
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2017-08-10 08:48:38 -07:00
|
|
|
SwitchInt {
|
|
|
|
ref discr,
|
|
|
|
ref values,
|
|
|
|
ref targets,
|
|
|
|
..
|
|
|
|
} => {
|
2018-10-26 12:33:26 +02:00
|
|
|
let discr = self.read_immediate(self.eval_operand(discr, None)?)?;
|
2018-08-15 20:18:40 +02:00
|
|
|
trace!("SwitchInt({:?})", *discr);
|
2016-06-23 01:03:58 -06:00
|
|
|
|
|
|
|
// Branch to the `otherwise` case by default, if no match is found.
|
|
|
|
let mut target_block = targets[targets.len() - 1];
|
|
|
|
|
2018-01-16 09:24:38 +01:00
|
|
|
for (index, &const_int) in values.iter().enumerate() {
|
2018-08-27 13:34:35 +02:00
|
|
|
// Compare using binary_op, to also support pointer values
|
2018-08-26 20:42:52 +02:00
|
|
|
let const_int = Scalar::from_uint(const_int, discr.layout.size);
|
2018-08-13 16:14:22 +02:00
|
|
|
let (res, _) = self.binary_op(mir::BinOp::Eq,
|
2019-02-08 14:00:52 +01:00
|
|
|
discr,
|
|
|
|
ImmTy::from_scalar(const_int, discr.layout),
|
2018-07-15 11:35:18 +02:00
|
|
|
)?;
|
2018-08-13 16:14:22 +02:00
|
|
|
if res.to_bool()? {
|
2016-06-23 01:03:58 -06:00
|
|
|
target_block = targets[index];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:04:33 +02:00
|
|
|
self.goto_block(Some(target_block))?;
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
2017-08-10 08:48:38 -07:00
|
|
|
Call {
|
|
|
|
ref func,
|
|
|
|
ref args,
|
|
|
|
ref destination,
|
|
|
|
..
|
|
|
|
} => {
|
2018-08-23 19:04:33 +02:00
|
|
|
let (dest, ret) = match *destination {
|
|
|
|
Some((ref lv, target)) => (Some(self.eval_place(lv)?), Some(target)),
|
|
|
|
None => (None, None),
|
2016-07-06 17:55:05 +02:00
|
|
|
};
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2018-08-20 15:21:04 +02:00
|
|
|
let func = self.eval_operand(func, None)?;
|
2019-06-30 13:51:18 +02:00
|
|
|
let (fn_val, abi) = match func.layout.ty.sty {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::FnPtr(sig) => {
|
2018-08-27 13:34:35 +02:00
|
|
|
let caller_abi = sig.abi();
|
2019-06-12 13:08:09 -05:00
|
|
|
let fn_ptr = self.force_ptr(self.read_scalar(func)?.not_undef()?)?;
|
2019-06-30 13:51:18 +02:00
|
|
|
let fn_val = self.memory.get_fn(fn_ptr)?;
|
|
|
|
(fn_val, caller_abi)
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2018-08-24 14:40:55 +02:00
|
|
|
ty::FnDef(def_id, substs) => {
|
|
|
|
let sig = func.layout.ty.fn_sig(*self.tcx);
|
2019-06-30 13:51:18 +02:00
|
|
|
(FnVal::Instance(self.resolve(def_id, substs)?), sig.abi())
|
2018-08-24 14:40:55 +02:00
|
|
|
},
|
2016-11-26 22:58:01 -08:00
|
|
|
_ => {
|
2018-08-13 16:14:22 +02:00
|
|
|
let msg = format!("can't handle callee of type {:?}", func.layout.ty);
|
2017-08-02 16:59:01 +02:00
|
|
|
return err!(Unimplemented(msg));
|
2016-11-26 22:58:01 -08:00
|
|
|
}
|
2017-02-28 12:35:00 +01:00
|
|
|
};
|
2018-08-13 16:14:22 +02:00
|
|
|
let args = self.eval_operands(args)?;
|
2017-08-10 08:48:38 -07:00
|
|
|
self.eval_fn_call(
|
2019-06-30 13:51:18 +02:00
|
|
|
fn_val,
|
2018-08-27 13:34:35 +02:00
|
|
|
terminator.source_info.span,
|
|
|
|
abi,
|
2018-08-13 16:14:22 +02:00
|
|
|
&args[..],
|
2018-08-23 19:04:33 +02:00
|
|
|
dest,
|
|
|
|
ret,
|
2017-08-10 08:48:38 -07:00
|
|
|
)?;
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
2017-08-10 08:48:38 -07:00
|
|
|
Drop {
|
|
|
|
ref location,
|
|
|
|
target,
|
|
|
|
..
|
|
|
|
} => {
|
2017-07-25 11:32:48 +02:00
|
|
|
// FIXME(CTFE): forbid drop in const eval
|
2017-12-06 09:25:29 +01:00
|
|
|
let place = self.eval_place(location)?;
|
2018-08-13 16:14:22 +02:00
|
|
|
let ty = place.layout.ty;
|
2017-08-24 16:04:50 +02:00
|
|
|
trace!("TerminatorKind::drop: {:?}, type {}", location, ty);
|
2017-03-22 13:13:52 +01:00
|
|
|
|
2019-05-23 12:45:22 -05:00
|
|
|
let instance = Instance::resolve_drop_in_place(*self.tcx, ty);
|
2018-08-16 00:18:09 +02:00
|
|
|
self.drop_in_place(
|
2017-12-06 09:25:29 +01:00
|
|
|
place,
|
2017-08-10 08:48:38 -07:00
|
|
|
instance,
|
|
|
|
terminator.source_info.span,
|
2017-08-24 16:04:50 +02:00
|
|
|
target,
|
2017-08-10 08:48:38 -07:00
|
|
|
)?;
|
2017-03-22 13:13:52 +01:00
|
|
|
}
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2017-08-10 08:48:38 -07:00
|
|
|
Assert {
|
|
|
|
ref cond,
|
|
|
|
expected,
|
|
|
|
ref msg,
|
|
|
|
target,
|
|
|
|
..
|
|
|
|
} => {
|
2018-10-26 12:33:26 +02:00
|
|
|
let cond_val = self.read_immediate(self.eval_operand(cond, None)?)?
|
2018-08-23 19:27:14 +02:00
|
|
|
.to_scalar()?.to_bool()?;
|
2016-09-19 02:19:31 -06:00
|
|
|
if expected == cond_val {
|
2018-08-23 19:04:33 +02:00
|
|
|
self.goto_block(Some(target))?;
|
2016-06-23 01:03:58 -06:00
|
|
|
} else {
|
2018-08-27 13:34:35 +02:00
|
|
|
// Compute error message
|
2019-04-02 01:02:18 +09:00
|
|
|
use rustc::mir::interpret::InterpError::*;
|
2016-06-23 01:03:58 -06:00
|
|
|
return match *msg {
|
2017-08-30 11:13:01 +02:00
|
|
|
BoundsCheck { ref len, ref index } => {
|
2018-10-26 12:33:26 +02:00
|
|
|
let len = self.read_immediate(self.eval_operand(len, None)?)
|
2018-08-20 20:08:24 +02:00
|
|
|
.expect("can't eval len").to_scalar()?
|
2018-05-22 10:28:46 +02:00
|
|
|
.to_bits(self.memory().pointer_size())? as u64;
|
2018-10-26 12:33:26 +02:00
|
|
|
let index = self.read_immediate(self.eval_operand(index, None)?)
|
2018-08-20 20:08:24 +02:00
|
|
|
.expect("can't eval index").to_scalar()?
|
2018-05-22 10:28:46 +02:00
|
|
|
.to_bits(self.memory().pointer_size())? as u64;
|
2018-04-27 15:21:31 +02:00
|
|
|
err!(BoundsCheck { len, index })
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2018-04-27 15:21:31 +02:00
|
|
|
Overflow(op) => Err(Overflow(op).into()),
|
|
|
|
OverflowNeg => Err(OverflowNeg.into()),
|
2018-04-28 13:35:35 +02:00
|
|
|
DivisionByZero => Err(DivisionByZero.into()),
|
|
|
|
RemainderByZero => Err(RemainderByZero.into()),
|
2017-08-30 11:13:01 +02:00
|
|
|
GeneratorResumedAfterReturn |
|
|
|
|
GeneratorResumedAfterPanic => unimplemented!(),
|
2018-04-27 15:21:31 +02:00
|
|
|
_ => bug!(),
|
2017-08-10 08:48:38 -07:00
|
|
|
};
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2018-08-27 13:34:35 +02:00
|
|
|
Yield { .. } |
|
|
|
|
GeneratorDrop |
|
|
|
|
DropAndReplace { .. } |
|
|
|
|
Resume |
|
|
|
|
Abort => unimplemented!("{:#?}", terminator.kind),
|
2018-08-22 16:58:39 -03:00
|
|
|
FalseEdges { .. } => bug!("should have been eliminated by\
|
|
|
|
`simplify_branches` mir pass"),
|
|
|
|
FalseUnwind { .. } => bug!("should have been eliminated by\
|
|
|
|
`simplify_branches` mir pass"),
|
2017-08-02 16:59:01 +02:00
|
|
|
Unreachable => return err!(Unreachable),
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-08-27 13:34:35 +02:00
|
|
|
fn check_argument_compat(
|
2018-11-22 17:38:59 +01:00
|
|
|
rust_abi: bool,
|
2018-08-27 13:34:35 +02:00
|
|
|
caller: TyLayout<'tcx>,
|
|
|
|
callee: TyLayout<'tcx>,
|
|
|
|
) -> bool {
|
|
|
|
if caller.ty == callee.ty {
|
|
|
|
// No question
|
|
|
|
return true;
|
2017-05-26 20:02:51 -07:00
|
|
|
}
|
2018-11-22 17:38:59 +01:00
|
|
|
if !rust_abi {
|
|
|
|
// Don't risk anything
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-27 13:34:35 +02:00
|
|
|
// Compare layout
|
|
|
|
match (&caller.abi, &callee.abi) {
|
2018-11-22 17:38:59 +01: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 13:34:35 +02:00
|
|
|
(layout::Abi::Scalar(ref caller), layout::Abi::Scalar(ref callee)) =>
|
|
|
|
caller.value == callee.value,
|
2018-11-22 13:43:05 +01: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 13:34:35 +02:00
|
|
|
// Be conservative
|
|
|
|
_ => false
|
2017-05-26 20:02:51 -07:00
|
|
|
}
|
2018-08-27 13:34:35 +02:00
|
|
|
}
|
2017-05-26 20:02:51 -07:00
|
|
|
|
2018-08-27 13:34:35 +02:00
|
|
|
/// Pass a single argument, checking the types for compatibility.
|
|
|
|
fn pass_argument(
|
|
|
|
&mut self,
|
2018-11-22 17:38:59 +01:00
|
|
|
rust_abi: bool,
|
2018-09-21 23:32:59 +02:00
|
|
|
caller_arg: &mut impl Iterator<Item=OpTy<'tcx, M::PointerTag>>,
|
|
|
|
callee_arg: PlaceTy<'tcx, M::PointerTag>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2018-11-22 17:38:59 +01:00
|
|
|
if rust_abi && callee_arg.layout.is_zst() {
|
2018-08-27 13:34:35 +02:00
|
|
|
// Nothing to do.
|
|
|
|
trace!("Skipping callee ZST");
|
|
|
|
return Ok(());
|
2017-05-26 20:02:51 -07:00
|
|
|
}
|
2018-08-27 13:34:35 +02:00
|
|
|
let caller_arg = caller_arg.next()
|
2019-04-02 01:02:18 +09:00
|
|
|
.ok_or_else(|| InterpError::FunctionArgCountMismatch)?;
|
2018-11-22 17:38:59 +01:00
|
|
|
if rust_abi {
|
2018-08-27 13:34:35 +02:00
|
|
|
debug_assert!(!caller_arg.layout.is_zst(), "ZSTs must have been already filtered out");
|
|
|
|
}
|
|
|
|
// Now, check
|
2018-11-22 17:38:59 +01:00
|
|
|
if !Self::check_argument_compat(rust_abi, caller_arg.layout, callee_arg.layout) {
|
2018-08-27 13:34:35 +02:00
|
|
|
return err!(FunctionArgMismatch(caller_arg.layout.ty, callee_arg.layout.ty));
|
|
|
|
}
|
2018-10-09 17:06:57 +02:00
|
|
|
// We allow some transmutes here
|
|
|
|
self.copy_op_transmute(caller_arg, callee_arg)
|
2017-05-26 20:02:51 -07:00
|
|
|
}
|
|
|
|
|
2018-08-24 14:40:55 +02:00
|
|
|
/// Call this function -- pushing the stack frame and initializing the arguments.
|
2016-06-23 01:03:58 -06:00
|
|
|
fn eval_fn_call(
|
|
|
|
&mut self,
|
2019-06-30 13:51:18 +02:00
|
|
|
fn_val: FnVal<'tcx, M::ExtraFnVal>,
|
2018-08-27 13:34:35 +02:00
|
|
|
span: Span,
|
|
|
|
caller_abi: Abi,
|
2018-09-21 23:32:59 +02:00
|
|
|
args: &[OpTy<'tcx, M::PointerTag>],
|
|
|
|
dest: Option<PlaceTy<'tcx, M::PointerTag>>,
|
2018-08-23 19:04:33 +02:00
|
|
|
ret: Option<mir::BasicBlock>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2019-06-30 13:51:18 +02:00
|
|
|
trace!("eval_fn_call: {:#?}", fn_val);
|
|
|
|
|
|
|
|
let instance = fn_val.as_instance()?;
|
2018-08-24 14:40:55 +02:00
|
|
|
|
2017-03-22 13:13:52 +01:00
|
|
|
match instance.def {
|
|
|
|
ty::InstanceDef::Intrinsic(..) => {
|
2018-08-27 13:34:35 +02:00
|
|
|
if caller_abi != Abi::RustIntrinsic {
|
|
|
|
return err!(FunctionAbiMismatch(caller_abi, Abi::RustIntrinsic));
|
|
|
|
}
|
2018-08-23 19:04:33 +02:00
|
|
|
// The intrinsic itself cannot diverge, so if we got here without a return
|
2018-11-27 02:59:49 +00:00
|
|
|
// place... (can happen e.g., for transmute returning `!`)
|
2018-08-23 19:04:33 +02:00
|
|
|
let dest = match dest {
|
2017-03-22 14:19:29 +01:00
|
|
|
Some(dest) => dest,
|
2018-08-23 19:04:33 +02:00
|
|
|
None => return err!(Unreachable)
|
2017-01-12 09:59:00 +01:00
|
|
|
};
|
2018-08-23 19:04:33 +02:00
|
|
|
M::call_intrinsic(self, instance, args, dest)?;
|
|
|
|
// No stack frame gets pushed, the main loop will just act as if the
|
|
|
|
// call completed.
|
|
|
|
self.goto_block(ret)?;
|
|
|
|
self.dump_place(*dest);
|
2017-03-22 14:19:29 +01:00
|
|
|
Ok(())
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2018-09-10 22:54:48 +09:00
|
|
|
ty::InstanceDef::VtableShim(..) |
|
2018-08-24 14:40:55 +02:00
|
|
|
ty::InstanceDef::ClosureOnceShim { .. } |
|
2017-08-23 17:46:36 +02:00
|
|
|
ty::InstanceDef::FnPtrShim(..) |
|
|
|
|
ty::InstanceDef::DropGlue(..) |
|
2017-08-23 17:24:38 +02:00
|
|
|
ty::InstanceDef::CloneShim(..) |
|
2017-03-22 13:13:52 +01:00
|
|
|
ty::InstanceDef::Item(_) => {
|
2018-08-27 13:34:35 +02:00
|
|
|
// ABI check
|
|
|
|
{
|
|
|
|
let callee_abi = {
|
|
|
|
let instance_ty = instance.ty(*self.tcx);
|
|
|
|
match instance_ty.sty {
|
|
|
|
ty::FnDef(..) =>
|
|
|
|
instance_ty.fn_sig(*self.tcx).abi(),
|
|
|
|
ty::Closure(..) => Abi::RustCall,
|
|
|
|
ty::Generator(..) => Abi::Rust,
|
|
|
|
_ => bug!("unexpected callee ty: {:?}", instance_ty),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Rust and RustCall are compatible
|
|
|
|
let normalize_abi = |abi| if abi == Abi::RustCall { Abi::Rust } else { abi };
|
|
|
|
if normalize_abi(caller_abi) != normalize_abi(callee_abi) {
|
|
|
|
return err!(FunctionAbiMismatch(caller_abi, callee_abi));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need MIR for this fn
|
2019-06-03 18:26:48 -04:00
|
|
|
let body = match M::find_fn(self, instance, args, dest, ret)? {
|
|
|
|
Some(body) => body,
|
2018-08-23 19:04:33 +02:00
|
|
|
None => return Ok(()),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.push_stack_frame(
|
|
|
|
instance,
|
|
|
|
span,
|
2019-06-03 18:26:48 -04:00
|
|
|
body,
|
2018-10-09 21:05:53 +02:00
|
|
|
dest,
|
2018-08-23 19:04:33 +02:00
|
|
|
StackPopCleanup::Goto(ret),
|
|
|
|
)?;
|
2017-03-23 13:36:13 +01:00
|
|
|
|
2018-08-27 13:34:35 +02: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 18:26:48 -04:00
|
|
|
body.spread_arg,
|
|
|
|
body.args_iter()
|
2018-08-27 13:34:35 +02:00
|
|
|
.map(|local|
|
2019-01-30 14:55:31 +01:00
|
|
|
(local, self.layout_of_local(self.frame(), local, None).unwrap().ty)
|
2018-08-25 21:22:00 +02:00
|
|
|
)
|
2018-08-27 13:34:35 +02:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Figure out how to pass which arguments.
|
2019-04-06 23:58:59 +02:00
|
|
|
// The Rust ABI is special: ZST get skipped.
|
2018-11-22 17:38:59 +01:00
|
|
|
let rust_abi = match caller_abi {
|
2018-08-27 13:34:35 +02:00
|
|
|
Abi::Rust | Abi::RustCall => true,
|
|
|
|
_ => false
|
2018-08-25 21:22:00 +02:00
|
|
|
};
|
2019-04-06 23:58:59 +02:00
|
|
|
// We have two iterators: Where the arguments come from,
|
|
|
|
// and where they go to.
|
2018-08-24 14:40:55 +02:00
|
|
|
|
2018-08-27 13:34:35 +02: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-08 06:28:15 +09:00
|
|
|
let caller_args : Cow<'_, [OpTy<'tcx, M::PointerTag>]> =
|
2018-08-27 13:34:35 +02: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 18:56:27 +02:00
|
|
|
.collect::<InterpResult<'_, Vec<OpTy<'tcx, M::PointerTag>>>>()?)
|
2018-08-27 13:34:35 +02:00
|
|
|
} else {
|
|
|
|
// Plain arg passing
|
|
|
|
Cow::from(args)
|
|
|
|
};
|
|
|
|
// Skip ZSTs
|
|
|
|
let mut caller_iter = caller_args.iter()
|
2018-11-22 17:38:59 +01:00
|
|
|
.filter(|op| !rust_abi || !op.layout.is_zst())
|
2018-08-27 13:34:35 +02: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 18:26:48 -04:00
|
|
|
let mut locals_iter = body.args_iter();
|
2018-08-27 13:34:35 +02:00
|
|
|
while let Some(local) = locals_iter.next() {
|
2019-02-22 05:24:03 +01:00
|
|
|
let dest = self.eval_place(
|
2019-06-24 17:46:09 +02:00
|
|
|
&mir::Place::from(local)
|
2019-02-22 05:24:03 +01:00
|
|
|
)?;
|
2019-06-03 18:26:48 -04:00
|
|
|
if Some(local) == body.spread_arg {
|
2018-08-27 13:34:35 +02: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 17:38:59 +01:00
|
|
|
self.pass_argument(rust_abi, &mut caller_iter, dest)?;
|
2018-08-27 13:34:35 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Normal argument
|
2018-11-22 17:38:59 +01:00
|
|
|
self.pass_argument(rust_abi, &mut caller_iter, dest)?;
|
2018-08-24 14:40:55 +02:00
|
|
|
}
|
2017-03-23 13:36:13 +01:00
|
|
|
}
|
2018-08-27 13:34:35 +02:00
|
|
|
// Now we should have no more caller args
|
|
|
|
if caller_iter.next().is_some() {
|
2019-04-06 23:58:59 +02:00
|
|
|
trace!("Caller has passed too many args");
|
2018-08-27 13:34:35 +02:00
|
|
|
return err!(FunctionArgCountMismatch);
|
|
|
|
}
|
2018-10-02 21:16:35 +02:00
|
|
|
// Don't forget to check the return type!
|
|
|
|
if let Some(caller_ret) = dest {
|
2019-02-22 05:24:03 +01:00
|
|
|
let callee_ret = self.eval_place(
|
|
|
|
&mir::Place::RETURN_PLACE
|
|
|
|
)?;
|
2018-11-22 17:38:59 +01:00
|
|
|
if !Self::check_argument_compat(
|
|
|
|
rust_abi,
|
|
|
|
caller_ret.layout,
|
|
|
|
callee_ret.layout,
|
|
|
|
) {
|
2018-10-02 21:16:35 +02:00
|
|
|
return err!(FunctionRetMismatch(
|
|
|
|
caller_ret.layout.ty, callee_ret.layout.ty
|
|
|
|
));
|
|
|
|
}
|
|
|
|
} else {
|
2019-06-25 03:39:23 +01:00
|
|
|
let local = mir::RETURN_PLACE;
|
|
|
|
let ty = self.frame().body.local_decls[local].ty;
|
|
|
|
if !self.tcx.is_ty_uninhabited_from_any_module(ty) {
|
|
|
|
return err!(FunctionRetMismatch(self.tcx.types.never, ty));
|
2018-10-09 18:16:27 +02:00
|
|
|
}
|
2018-10-02 21:16:35 +02:00
|
|
|
}
|
2018-08-27 13:34:35 +02:00
|
|
|
Ok(())
|
|
|
|
})();
|
|
|
|
match res {
|
|
|
|
Err(err) => {
|
|
|
|
self.stack.pop();
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
Ok(v) => Ok(v)
|
2017-03-23 13:36:13 +01:00
|
|
|
}
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2017-08-23 17:46:36 +02:00
|
|
|
// cannot use the shim here, because that will only result in infinite recursion
|
2017-03-23 14:24:02 +01:00
|
|
|
ty::InstanceDef::Virtual(_, idx) => {
|
2019-04-07 19:54:43 +02:00
|
|
|
let mut args = args.to_vec();
|
2018-08-26 20:42:52 +02:00
|
|
|
let ptr_size = self.pointer_size();
|
2019-04-07 19:54:43 +02:00
|
|
|
// 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.
|
|
|
|
args[0].to_mem_place()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Find and consult vtable
|
2019-06-23 14:26:36 +02:00
|
|
|
let vtable = receiver_place.vtable();
|
|
|
|
let vtable_slot = vtable.ptr_offset(ptr_size * (idx as u64 + 3), self)?;
|
|
|
|
let vtable_slot = self.memory.check_ptr_access(
|
|
|
|
vtable_slot,
|
|
|
|
ptr_size,
|
|
|
|
self.tcx.data_layout.pointer_align.abi,
|
|
|
|
)?.expect("cannot be a ZST");
|
|
|
|
let fn_ptr = self.memory.get(vtable_slot.alloc_id)?
|
|
|
|
.read_ptr_sized(self, vtable_slot)?.to_ptr()?;
|
2019-06-30 13:51:18 +02:00
|
|
|
let drop_fn = self.memory.get_fn(fn_ptr)?;
|
2018-08-16 10:30:56 +02:00
|
|
|
|
2019-04-07 19:54:43 +02: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,
|
|
|
|
imm: Immediate::Scalar(receiver_place.ptr.into())
|
2019-02-08 12:20:55 +01:00
|
|
|
});
|
2018-08-16 10:30:56 +02:00
|
|
|
trace!("Patched self operand to {:#?}", args[0]);
|
2017-03-23 14:57:11 +01:00
|
|
|
// recurse with concrete function
|
2019-06-30 13:51:18 +02:00
|
|
|
self.eval_fn_call(drop_fn, span, caller_abi, &args, dest, ret)
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2017-02-28 12:35:00 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-24 14:44:30 +02:00
|
|
|
|
|
|
|
fn drop_in_place(
|
|
|
|
&mut self,
|
2018-09-21 23:32:59 +02:00
|
|
|
place: PlaceTy<'tcx, M::PointerTag>,
|
2018-08-24 14:44:30 +02:00
|
|
|
instance: ty::Instance<'tcx>,
|
|
|
|
span: Span,
|
|
|
|
target: mir::BasicBlock,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2018-08-24 14:44:30 +02: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)?;
|
|
|
|
|
|
|
|
let (instance, place) = match place.layout.ty.sty {
|
|
|
|
ty::Dynamic(..) => {
|
|
|
|
// Dropping a trait object.
|
2018-08-25 14:36:24 +02:00
|
|
|
self.unpack_dyn_trait(place)?
|
2018-08-24 14:44:30 +02:00
|
|
|
}
|
|
|
|
_ => (instance, place),
|
|
|
|
};
|
|
|
|
|
2019-02-08 12:20:55 +01:00
|
|
|
let arg = ImmTy {
|
|
|
|
imm: place.to_ref(),
|
2018-08-24 14:44:30 +02:00
|
|
|
layout: self.layout_of(self.tcx.mk_mut_ptr(place.layout.ty))?,
|
|
|
|
};
|
|
|
|
|
2018-09-10 11:07:13 +09:00
|
|
|
let ty = self.tcx.mk_unit(); // return type is ()
|
2018-11-03 22:57:53 +02:00
|
|
|
let dest = MPlaceTy::dangling(self.layout_of(ty)?, self);
|
2018-08-24 14:44:30 +02:00
|
|
|
|
|
|
|
self.eval_fn_call(
|
2019-06-30 13:51:18 +02:00
|
|
|
FnVal::Instance(instance),
|
2018-08-27 13:34:35 +02:00
|
|
|
span,
|
|
|
|
Abi::Rust,
|
2019-02-08 12:20:55 +01:00
|
|
|
&[arg.into()],
|
2018-10-09 21:05:53 +02:00
|
|
|
Some(dest.into()),
|
2018-08-24 14:44:30 +02:00
|
|
|
Some(target),
|
|
|
|
)
|
|
|
|
}
|
2016-09-20 12:52:01 +02:00
|
|
|
}
|