2017-12-12 17:14:49 +01:00
|
|
|
use rustc::mir;
|
|
|
|
use rustc::ty::{self, Ty};
|
2018-08-13 16:14:22 +02:00
|
|
|
use rustc::ty::layout::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-13 16:14:22 +02:00
|
|
|
use rustc::mir::interpret::{EvalResult, Scalar};
|
|
|
|
use super::{EvalContext, Machine, Value, OpTy, PlaceTy, ValTy, Operand};
|
2017-07-21 13:39:06 +02:00
|
|
|
|
2017-03-23 13:36:13 +01:00
|
|
|
use rustc_data_structures::indexed_vec::Idx;
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2017-03-22 18:31:41 +01:00
|
|
|
mod drop;
|
2016-09-20 16:05:30 +02:00
|
|
|
|
2018-01-16 09:31:48 +01:00
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
|
2017-07-21 17:25:30 +02:00
|
|
|
pub fn goto_block(&mut self, target: mir::BasicBlock) {
|
2016-07-06 17:55:05 +02:00
|
|
|
self.frame_mut().block = target;
|
|
|
|
self.frame_mut().stmt = 0;
|
|
|
|
}
|
|
|
|
|
2016-06-23 01:03:58 -06:00
|
|
|
pub(super) fn eval_terminator(
|
|
|
|
&mut self,
|
|
|
|
terminator: &mir::Terminator<'tcx>,
|
2017-02-04 13:09:10 -08:00
|
|
|
) -> EvalResult<'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-08-13 16:14:22 +02:00
|
|
|
self.dump_place(self.frame().return_place);
|
2016-11-26 19:13:22 -08:00
|
|
|
self.pop_stack_frame()?
|
|
|
|
}
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2016-07-06 17:55:05 +02:00
|
|
|
Goto { target } => self.goto_block(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-08-20 15:21:04 +02:00
|
|
|
let discr_val = self.eval_operand(discr, None)?;
|
2018-08-13 16:14:22 +02:00
|
|
|
let discr = self.read_value(discr_val)?;
|
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-07-15 11:35:18 +02:00
|
|
|
// Compare using binary_op
|
2018-08-13 16:14:22 +02:00
|
|
|
let const_int = Scalar::Bits { bits: const_int, size: discr.layout.size.bytes() as u8 };
|
|
|
|
let (res, _) = self.binary_op(mir::BinOp::Eq,
|
|
|
|
discr,
|
|
|
|
ValTy { value: Value::Scalar(const_int.into()), layout: 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 17:55:05 +02:00
|
|
|
self.goto_block(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,
|
|
|
|
..
|
|
|
|
} => {
|
2016-07-06 17:55:05 +02:00
|
|
|
let destination = match *destination {
|
2017-12-06 09:25:29 +01:00
|
|
|
Some((ref lv, target)) => Some((self.eval_place(lv)?, target)),
|
2016-07-06 17:55:05 +02:00
|
|
|
None => None,
|
|
|
|
};
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2018-08-20 15:21:04 +02:00
|
|
|
let func = self.eval_operand(func, None)?;
|
2018-08-13 16:14:22 +02:00
|
|
|
let (fn_def, sig) = match func.layout.ty.sty {
|
2017-03-22 13:13:52 +01:00
|
|
|
ty::TyFnPtr(sig) => {
|
2018-08-13 16:14:22 +02:00
|
|
|
let fn_ptr = self.read_scalar(func)?.to_ptr()?;
|
2017-06-21 21:45:51 -07:00
|
|
|
let instance = self.memory.get_fn(fn_ptr)?;
|
2018-02-06 18:33:59 +01:00
|
|
|
let instance_ty = instance.ty(*self.tcx);
|
2017-03-23 17:57:40 +01:00
|
|
|
match instance_ty.sty {
|
2017-06-28 21:24:17 -04:00
|
|
|
ty::TyFnDef(..) => {
|
2018-02-06 18:33:59 +01:00
|
|
|
let real_sig = instance_ty.fn_sig(*self.tcx);
|
2018-03-03 08:23:28 -05:00
|
|
|
let sig = self.tcx.normalize_erasing_late_bound_regions(
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
&sig,
|
|
|
|
);
|
|
|
|
let real_sig = self.tcx.normalize_erasing_late_bound_regions(
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
&real_sig,
|
|
|
|
);
|
2017-05-26 20:02:51 -07:00
|
|
|
if !self.check_sig_compat(sig, real_sig)? {
|
2017-08-02 16:59:01 +02:00
|
|
|
return err!(FunctionPointerTyMismatch(real_sig, sig));
|
2017-03-23 15:17:02 +01:00
|
|
|
}
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2017-03-23 15:17:02 +01:00
|
|
|
ref other => bug!("instance def ty: {:?}", other),
|
|
|
|
}
|
|
|
|
(instance, sig)
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
|
|
|
ty::TyFnDef(def_id, substs) => (
|
2017-12-06 09:25:29 +01:00
|
|
|
self.resolve(def_id, substs)?,
|
2018-08-13 16:14:22 +02:00
|
|
|
func.layout.ty.fn_sig(*self.tcx),
|
2017-08-10 08:48:38 -07: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)?;
|
2018-03-03 08:23:28 -05:00
|
|
|
let sig = self.tcx.normalize_erasing_late_bound_regions(
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
&sig,
|
|
|
|
);
|
2017-08-10 08:48:38 -07:00
|
|
|
self.eval_fn_call(
|
|
|
|
fn_def,
|
|
|
|
destination,
|
2018-08-13 16:14:22 +02:00
|
|
|
&args[..],
|
2017-08-10 08:48:38 -07:00
|
|
|
terminator.source_info.span,
|
|
|
|
sig,
|
|
|
|
)?;
|
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
|
|
|
|
2018-02-06 18:33:59 +01:00
|
|
|
let instance = ::monomorphize::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-08-20 20:08:24 +02:00
|
|
|
let cond_val = self.eval_operand_and_read_value(cond, None)?.to_scalar()?.to_bool()?;
|
2016-09-19 02:19:31 -06:00
|
|
|
if expected == cond_val {
|
2016-07-06 17:55:05 +02:00
|
|
|
self.goto_block(target);
|
2016-06-23 01:03:58 -06:00
|
|
|
} else {
|
2018-04-27 15:21:31 +02:00
|
|
|
use rustc::mir::interpret::EvalErrorKind::*;
|
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-08-20 20:08:24 +02:00
|
|
|
let len = self.eval_operand_and_read_value(len, None)
|
|
|
|
.expect("can't eval len").to_scalar()?
|
2018-05-22 10:28:46 +02:00
|
|
|
.to_bits(self.memory().pointer_size())? as u64;
|
2018-08-20 20:08:24 +02:00
|
|
|
let index = self.eval_operand_and_read_value(index, None)
|
|
|
|
.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
|
|
|
|
2017-08-30 11:13:01 +02:00
|
|
|
Yield { .. } => unimplemented!("{:#?}", terminator.kind),
|
|
|
|
GeneratorDrop => unimplemented!(),
|
2016-06-23 01:03:58 -06:00
|
|
|
DropAndReplace { .. } => unimplemented!(),
|
|
|
|
Resume => unimplemented!(),
|
2017-12-19 01:17:16 +01:00
|
|
|
Abort => unimplemented!(),
|
2017-12-06 09:25:29 +01:00
|
|
|
FalseEdges { .. } => bug!("should have been eliminated by `simplify_branches` mir pass"),
|
2018-01-25 01:45:45 -05:00
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
2017-05-30 10:24:37 -07:00
|
|
|
/// Decides whether it is okay to call the method with signature `real_sig` using signature `sig`.
|
|
|
|
/// FIXME: This should take into account the platform-dependent ABI description.
|
2017-05-26 20:02:51 -07:00
|
|
|
fn check_sig_compat(
|
|
|
|
&mut self,
|
|
|
|
sig: ty::FnSig<'tcx>,
|
|
|
|
real_sig: ty::FnSig<'tcx>,
|
|
|
|
) -> EvalResult<'tcx, bool> {
|
2017-12-06 13:55:46 +02:00
|
|
|
fn check_ty_compat<'tcx>(ty: Ty<'tcx>, real_ty: Ty<'tcx>) -> bool {
|
2017-08-10 08:48:38 -07:00
|
|
|
if ty == real_ty {
|
|
|
|
return true;
|
|
|
|
} // This is actually a fast pointer comparison
|
2017-05-26 20:02:51 -07:00
|
|
|
return match (&ty.sty, &real_ty.sty) {
|
|
|
|
// Permit changing the pointer type of raw pointers and references as well as
|
|
|
|
// mutability of raw pointers.
|
|
|
|
// TODO: Should not be allowed when fat pointers are involved.
|
2017-12-06 13:55:46 +02:00
|
|
|
(&ty::TyRawPtr(_), &ty::TyRawPtr(_)) => true,
|
2018-05-02 15:21:05 +02:00
|
|
|
(&ty::TyRef(_, _, _), &ty::TyRef(_, _, _)) => {
|
2017-08-10 08:48:38 -07:00
|
|
|
ty.is_mutable_pointer() == real_ty.is_mutable_pointer()
|
|
|
|
}
|
2017-05-26 20:02:51 -07:00
|
|
|
// rule out everything else
|
2017-08-10 08:48:38 -07:00
|
|
|
_ => false,
|
|
|
|
};
|
2017-05-26 20:02:51 -07:00
|
|
|
}
|
|
|
|
|
2017-08-10 08:48:38 -07:00
|
|
|
if sig.abi == real_sig.abi && sig.variadic == real_sig.variadic &&
|
2017-05-26 20:02:51 -07:00
|
|
|
sig.inputs_and_output.len() == real_sig.inputs_and_output.len() &&
|
2017-08-10 08:48:38 -07:00
|
|
|
sig.inputs_and_output
|
|
|
|
.iter()
|
|
|
|
.zip(real_sig.inputs_and_output)
|
|
|
|
.all(|(ty, real_ty)| check_ty_compat(ty, real_ty))
|
|
|
|
{
|
2017-05-26 20:02:51 -07:00
|
|
|
// Definitely good.
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if sig.variadic || real_sig.variadic {
|
|
|
|
// We're not touching this
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to allow what comes up when a non-capturing closure is cast to a fn().
|
|
|
|
match (sig.abi, real_sig.abi) {
|
|
|
|
(Abi::Rust, Abi::RustCall) // check the ABIs. This makes the test here non-symmetric.
|
|
|
|
if check_ty_compat(sig.output(), real_sig.output()) && real_sig.inputs_and_output.len() == 3 => {
|
|
|
|
// First argument of real_sig must be a ZST
|
|
|
|
let fst_ty = real_sig.inputs_and_output[0];
|
2017-12-06 13:50:31 +02:00
|
|
|
if self.layout_of(fst_ty)?.is_zst() {
|
2017-05-26 20:02:51 -07:00
|
|
|
// Second argument must be a tuple matching the argument list of sig
|
|
|
|
let snd_ty = real_sig.inputs_and_output[1];
|
|
|
|
match snd_ty.sty {
|
2018-01-21 13:33:21 +08:00
|
|
|
ty::TyTuple(tys) if sig.inputs().len() == tys.len() =>
|
2017-05-26 20:02:51 -07:00
|
|
|
if sig.inputs().iter().zip(tys).all(|(ty, real_ty)| check_ty_compat(ty, real_ty)) {
|
|
|
|
return Ok(true)
|
|
|
|
},
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Nope, this doesn't work.
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
|
2016-06-23 01:03:58 -06:00
|
|
|
fn eval_fn_call(
|
|
|
|
&mut self,
|
2017-03-21 13:53:55 +01:00
|
|
|
instance: ty::Instance<'tcx>,
|
2018-08-13 16:14:22 +02:00
|
|
|
destination: Option<(PlaceTy<'tcx>, mir::BasicBlock)>,
|
|
|
|
args: &[OpTy<'tcx>],
|
2016-06-23 01:03:58 -06:00
|
|
|
span: Span,
|
2017-03-22 14:19:29 +01:00
|
|
|
sig: ty::FnSig<'tcx>,
|
2017-02-04 13:09:10 -08:00
|
|
|
) -> EvalResult<'tcx> {
|
2017-03-22 13:13:52 +01:00
|
|
|
trace!("eval_fn_call: {:#?}", instance);
|
2018-08-15 22:39:39 +02:00
|
|
|
if let Some((place, _)) = destination {
|
|
|
|
assert_eq!(place.layout.ty, sig.output());
|
|
|
|
}
|
2017-03-22 13:13:52 +01:00
|
|
|
match instance.def {
|
|
|
|
ty::InstanceDef::Intrinsic(..) => {
|
2017-01-12 09:59:00 +01:00
|
|
|
let (ret, target) = match destination {
|
2017-03-22 14:19:29 +01:00
|
|
|
Some(dest) => dest,
|
2017-08-02 16:59:01 +02:00
|
|
|
_ => return err!(Unreachable),
|
2017-01-12 09:59:00 +01:00
|
|
|
};
|
2018-08-13 16:14:22 +02:00
|
|
|
M::call_intrinsic(self, instance, args, ret, target)?;
|
|
|
|
self.dump_place(*ret);
|
2017-03-22 14:19:29 +01:00
|
|
|
Ok(())
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2017-08-23 17:46:36 +02:00
|
|
|
// FIXME: figure out why we can't just go through the shim
|
2017-08-10 08:48:38 -07:00
|
|
|
ty::InstanceDef::ClosureOnceShim { .. } => {
|
2018-08-13 16:14:22 +02:00
|
|
|
if M::eval_fn_call(self, instance, destination, args, span)? {
|
2017-03-23 17:36:10 +01:00
|
|
|
return Ok(());
|
|
|
|
}
|
2017-03-23 13:36:13 +01:00
|
|
|
let mut arg_locals = self.frame().mir.args_iter();
|
|
|
|
match sig.abi {
|
|
|
|
// closure as closure once
|
|
|
|
Abi::RustCall => {
|
2018-08-13 16:14:22 +02:00
|
|
|
for (arg_local, &op) in arg_locals.zip(args) {
|
2017-12-06 09:25:29 +01:00
|
|
|
let dest = self.eval_place(&mir::Place::Local(arg_local))?;
|
2018-08-13 16:14:22 +02:00
|
|
|
self.copy_op(op, dest)?;
|
2017-03-23 13:36:13 +01:00
|
|
|
}
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2017-03-23 13:36:13 +01:00
|
|
|
// non capture closure as fn ptr
|
|
|
|
// need to inject zst ptr for closure object (aka do nothing)
|
|
|
|
// and need to pack arguments
|
|
|
|
Abi::Rust => {
|
2017-08-10 08:48:38 -07:00
|
|
|
trace!(
|
2018-08-15 20:18:40 +02:00
|
|
|
"args: {:#?}",
|
|
|
|
self.frame().mir.args_iter().zip(args.iter())
|
|
|
|
.map(|(local, arg)| (local, **arg, arg.layout.ty)).collect::<Vec<_>>()
|
2017-08-10 08:48:38 -07:00
|
|
|
);
|
2017-03-23 13:36:13 +01:00
|
|
|
let local = arg_locals.nth(1).unwrap();
|
2018-08-13 16:14:22 +02:00
|
|
|
for (i, &op) in args.into_iter().enumerate() {
|
2017-12-06 09:25:29 +01:00
|
|
|
let dest = self.eval_place(&mir::Place::Local(local).field(
|
2017-08-10 08:48:38 -07:00
|
|
|
mir::Field::new(i),
|
2018-08-13 16:14:22 +02:00
|
|
|
op.layout.ty,
|
2017-08-10 08:48:38 -07:00
|
|
|
))?;
|
2018-08-13 16:14:22 +02:00
|
|
|
self.copy_op(op, dest)?;
|
2017-03-23 13:36:13 +01:00
|
|
|
}
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2017-03-23 13:36:13 +01:00
|
|
|
_ => bug!("bad ABI for ClosureOnceShim: {:?}", sig.abi),
|
|
|
|
}
|
|
|
|
Ok(())
|
2017-03-22 13:13:52 +01:00
|
|
|
}
|
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(_) => {
|
2017-05-25 22:38:07 -07:00
|
|
|
// Push the stack frame, and potentially be entirely done if the call got hooked
|
2018-08-13 16:14:22 +02:00
|
|
|
if M::eval_fn_call(self, instance, destination, args, span)? {
|
|
|
|
// TODO: Can we make it return the frame to push, instead
|
|
|
|
// of the hook doing half of the work and us doing the argument
|
|
|
|
// initialization?
|
2017-03-23 17:36:10 +01:00
|
|
|
return Ok(());
|
|
|
|
}
|
2017-03-23 13:36:13 +01:00
|
|
|
|
2017-05-25 22:38:07 -07:00
|
|
|
// Pass the arguments
|
2017-03-23 13:36:13 +01:00
|
|
|
let mut arg_locals = self.frame().mir.args_iter();
|
2017-03-23 16:09:36 +01:00
|
|
|
trace!("ABI: {:?}", sig.abi);
|
2017-08-10 08:48:38 -07:00
|
|
|
trace!(
|
2018-08-15 20:18:40 +02:00
|
|
|
"args: {:#?}",
|
|
|
|
self.frame().mir.args_iter().zip(args.iter())
|
|
|
|
.map(|(local, arg)| (local, **arg, arg.layout.ty)).collect::<Vec<_>>()
|
2017-08-10 08:48:38 -07:00
|
|
|
);
|
2017-03-23 13:36:13 +01:00
|
|
|
match sig.abi {
|
|
|
|
Abi::RustCall => {
|
|
|
|
assert_eq!(args.len(), 2);
|
|
|
|
|
2017-08-10 08:48:38 -07:00
|
|
|
{
|
|
|
|
// write first argument
|
2017-03-23 13:36:13 +01:00
|
|
|
let first_local = arg_locals.next().unwrap();
|
2017-12-06 09:25:29 +01:00
|
|
|
let dest = self.eval_place(&mir::Place::Local(first_local))?;
|
2018-08-13 16:14:22 +02:00
|
|
|
self.copy_op(args[0], dest)?;
|
2017-03-23 13:36:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// unpack and write all other args
|
2018-08-13 16:14:22 +02:00
|
|
|
let layout = args[1].layout;
|
|
|
|
if let ty::TyTuple(_) = layout.ty.sty {
|
2018-06-02 23:34:25 +02:00
|
|
|
if layout.is_zst() {
|
|
|
|
// Nothing to do, no need to unpack zsts
|
|
|
|
return Ok(());
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
if self.frame().mir.args_iter().count() == layout.fields.count() + 1 {
|
2018-06-02 23:34:25 +02:00
|
|
|
for (i, arg_local) in arg_locals.enumerate() {
|
2018-08-13 16:14:22 +02:00
|
|
|
let arg = self.operand_field(args[1], i as u64)?;
|
2018-06-04 14:50:29 +02:00
|
|
|
let dest = self.eval_place(&mir::Place::Local(arg_local))?;
|
2018-08-13 16:14:22 +02:00
|
|
|
self.copy_op(arg, dest)?;
|
2017-03-23 13:36:13 +01:00
|
|
|
}
|
2017-03-23 15:07:33 +01:00
|
|
|
} else {
|
2017-03-23 16:09:36 +01:00
|
|
|
trace!("manual impl of rust-call ABI");
|
2017-03-23 15:07:33 +01:00
|
|
|
// called a manual impl of a rust-call function
|
2017-12-06 09:25:29 +01:00
|
|
|
let dest = self.eval_place(
|
|
|
|
&mir::Place::Local(arg_locals.next().unwrap()),
|
2017-08-10 08:48:38 -07:00
|
|
|
)?;
|
2018-08-13 16:14:22 +02:00
|
|
|
self.copy_op(args[1], dest)?;
|
2017-03-23 13:36:13 +01:00
|
|
|
}
|
|
|
|
} else {
|
2017-08-10 08:48:38 -07:00
|
|
|
bug!(
|
2018-08-13 16:14:22 +02:00
|
|
|
"rust-call ABI tuple argument was {:#?}",
|
2017-08-10 08:48:38 -07:00
|
|
|
layout
|
|
|
|
);
|
2017-03-23 13:36:13 +01:00
|
|
|
}
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2017-05-30 10:24:37 -07:00
|
|
|
_ => {
|
2018-08-13 16:14:22 +02:00
|
|
|
for (arg_local, &op) in arg_locals.zip(args) {
|
2017-12-06 09:25:29 +01:00
|
|
|
let dest = self.eval_place(&mir::Place::Local(arg_local))?;
|
2018-08-13 16:14:22 +02:00
|
|
|
self.copy_op(op, dest)?;
|
2017-05-30 10:24:37 -07:00
|
|
|
}
|
2017-03-23 13:36:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
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) => {
|
|
|
|
let ptr_size = self.memory.pointer_size();
|
2017-12-17 08:47:22 +02:00
|
|
|
let ptr_align = self.tcx.data_layout.pointer_align;
|
2018-08-13 16:14:22 +02:00
|
|
|
let (ptr, vtable) = self.read_value(args[0])?.to_scalar_dyn_trait()?;
|
2018-02-22 17:29:39 +01:00
|
|
|
let fn_ptr = self.memory.read_ptr_sized(
|
2017-12-17 08:47:22 +02:00
|
|
|
vtable.offset(ptr_size * (idx as u64 + 3), &self)?,
|
|
|
|
ptr_align
|
2018-08-13 16:14:22 +02:00
|
|
|
)?.to_ptr()?;
|
2017-08-25 16:20:13 +02:00
|
|
|
let instance = self.memory.get_fn(fn_ptr)?;
|
2018-08-16 10:30:56 +02:00
|
|
|
|
|
|
|
// We have to patch the self argument, in particular get the layout
|
|
|
|
// expected by the actual function. Cannot just use "field 0" due to
|
|
|
|
// Box<self>.
|
2017-08-24 14:41:49 +02:00
|
|
|
let mut args = args.to_vec();
|
2018-08-16 10:30:56 +02:00
|
|
|
let pointee = args[0].layout.ty.builtin_deref(true).unwrap().ty;
|
|
|
|
let fake_fat_ptr_ty = self.tcx.mk_mut_ptr(pointee);
|
|
|
|
args[0].layout = self.layout_of(fake_fat_ptr_ty)?.field(&self, 0)?;
|
|
|
|
args[0].op = Operand::Immediate(Value::Scalar(ptr.into())); // strip vtable
|
|
|
|
trace!("Patched self operand to {:#?}", args[0]);
|
2017-03-23 14:57:11 +01:00
|
|
|
// recurse with concrete function
|
2017-08-24 14:41:49 +02:00
|
|
|
self.eval_fn_call(instance, destination, &args, span, sig)
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2017-02-28 12:35:00 +01:00
|
|
|
}
|
|
|
|
}
|
2016-09-20 12:52:01 +02:00
|
|
|
}
|