4 use rustc::ty::Instance;
5 use rustc::ty::layout::{self, TyLayout, LayoutOf};
6 use syntax::source_map::Span;
7 use rustc_target::spec::abi::Abi;
10 InterpResult, PointerArithmetic,
11 InterpCx, Machine, OpTy, ImmTy, PlaceTy, MPlaceTy, StackPopCleanup, FnVal,
14 impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
16 pub fn goto_block(&mut self, target: Option<mir::BasicBlock>) -> InterpResult<'tcx> {
17 if let Some(target) = target {
18 self.frame_mut().block = target;
19 self.frame_mut().stmt = 0;
22 throw_ub!(Unreachable)
26 pub(super) fn eval_terminator(
28 terminator: &mir::Terminator<'tcx>,
29 ) -> InterpResult<'tcx> {
30 use rustc::mir::TerminatorKind::*;
31 match terminator.kind {
33 self.frame().return_place.map(|r| self.dump_place(*r));
34 self.pop_stack_frame(/* unwinding */ false)?
37 Goto { target } => self.goto_block(Some(target))?,
45 let discr = self.read_immediate(self.eval_operand(discr, None)?)?;
46 trace!("SwitchInt({:?})", *discr);
48 // Branch to the `otherwise` case by default, if no match is found.
49 let mut target_block = targets[targets.len() - 1];
51 for (index, &const_int) in values.iter().enumerate() {
52 // Compare using binary_op, to also support pointer values
53 let res = self.overflowing_binary_op(mir::BinOp::Eq,
55 ImmTy::from_uint(const_int, discr.layout),
58 target_block = targets[index];
63 self.goto_block(Some(target_block))?;
73 let (dest, ret) = match *destination {
74 Some((ref lv, target)) => (Some(self.eval_place(lv)?), Some(target)),
78 let func = self.eval_operand(func, None)?;
79 let (fn_val, abi) = match func.layout.ty.kind {
81 let caller_abi = sig.abi();
82 let fn_ptr = self.read_scalar(func)?.not_undef()?;
83 let fn_val = self.memory.get_fn(fn_ptr)?;
86 ty::FnDef(def_id, substs) => {
87 let sig = func.layout.ty.fn_sig(*self.tcx);
88 (FnVal::Instance(self.resolve(def_id, substs)?), sig.abi())
91 bug!("invalid callee of type {:?}", func.layout.ty)
94 let args = self.eval_operands(args)?;
97 terminator.source_info.span,
111 // FIXME(CTFE): forbid drop in const eval
112 let place = self.eval_place(location)?;
113 let ty = place.layout.ty;
114 trace!("TerminatorKind::drop: {:?}, type {}", location, ty);
116 let instance = Instance::resolve_drop_in_place(*self.tcx, ty);
120 terminator.source_info.span,
133 let cond_val = self.read_immediate(self.eval_operand(cond, None)?)?
134 .to_scalar()?.to_bool()?;
135 if expected == cond_val {
136 self.goto_block(Some(target))?;
138 // Compute error message
139 use rustc::mir::interpret::PanicInfo::*;
140 return Err(match msg {
141 BoundsCheck { ref len, ref index } => {
143 .read_immediate(self.eval_operand(len, None)?)
144 .expect("can't eval len")
146 .to_bits(self.memory.pointer_size())? as u64;
148 .read_immediate(self.eval_operand(index, None)?)
149 .expect("can't eval index")
151 .to_bits(self.memory.pointer_size())? as u64;
152 err_panic!(BoundsCheck { len, index })
154 Overflow(op) => err_panic!(Overflow(*op)),
155 OverflowNeg => err_panic!(OverflowNeg),
156 DivisionByZero => err_panic!(DivisionByZero),
157 RemainderByZero => err_panic!(RemainderByZero),
158 GeneratorResumedAfterReturn => err_panic!(GeneratorResumedAfterReturn),
159 GeneratorResumedAfterPanic => err_panic!(GeneratorResumedAfterPanic),
160 Panic { .. } => bug!("`Panic` variant cannot occur in MIR"),
167 // When we encounter Resume, we've finished unwinding
168 // cleanup for the current stack frame. We pop it in order
169 // to continue unwinding the next frame
171 trace!("unwinding: resuming from cleanup");
172 // By definition, a Resume terminator means
173 // that we're unwinding
174 self.pop_stack_frame(/* unwinding */ true)?;
180 DropAndReplace { .. } |
181 Abort => unimplemented!("{:#?}", terminator.kind),
182 FalseEdges { .. } => bug!("should have been eliminated by\
183 `simplify_branches` mir pass"),
184 FalseUnwind { .. } => bug!("should have been eliminated by\
185 `simplify_branches` mir pass"),
186 Unreachable => throw_ub!(Unreachable),
192 fn check_argument_compat(
194 caller: TyLayout<'tcx>,
195 callee: TyLayout<'tcx>,
197 if caller.ty == callee.ty {
202 // Don't risk anything
206 match (&caller.abi, &callee.abi) {
207 // Different valid ranges are okay (once we enforce validity,
208 // that will take care to make it UB to leave the range, just
209 // like for transmute).
210 (layout::Abi::Scalar(ref caller), layout::Abi::Scalar(ref callee)) =>
211 caller.value == callee.value,
212 (layout::Abi::ScalarPair(ref caller1, ref caller2),
213 layout::Abi::ScalarPair(ref callee1, ref callee2)) =>
214 caller1.value == callee1.value && caller2.value == callee2.value,
220 /// Pass a single argument, checking the types for compatibility.
224 caller_arg: &mut impl Iterator<Item=OpTy<'tcx, M::PointerTag>>,
225 callee_arg: PlaceTy<'tcx, M::PointerTag>,
226 ) -> InterpResult<'tcx> {
227 if rust_abi && callee_arg.layout.is_zst() {
229 trace!("Skipping callee ZST");
232 let caller_arg = caller_arg.next()
233 .ok_or_else(|| err_unsup!(FunctionArgCountMismatch)) ?;
235 debug_assert!(!caller_arg.layout.is_zst(), "ZSTs must have been already filtered out");
238 if !Self::check_argument_compat(rust_abi, caller_arg.layout, callee_arg.layout) {
239 throw_unsup!(FunctionArgMismatch(caller_arg.layout.ty, callee_arg.layout.ty))
241 // We allow some transmutes here
242 self.copy_op_transmute(caller_arg, callee_arg)
245 /// Call this function -- pushing the stack frame and initializing the arguments.
248 fn_val: FnVal<'tcx, M::ExtraFnVal>,
251 args: &[OpTy<'tcx, M::PointerTag>],
252 dest: Option<PlaceTy<'tcx, M::PointerTag>>,
253 ret: Option<mir::BasicBlock>,
254 unwind: Option<mir::BasicBlock>
255 ) -> InterpResult<'tcx> {
256 trace!("eval_fn_call: {:#?}", fn_val);
258 let instance = match fn_val {
259 FnVal::Instance(instance) => instance,
260 FnVal::Other(extra) => {
261 return M::call_extra_fn(self, extra, args, dest, ret);
266 ty::InstanceDef::Intrinsic(..) => {
268 if caller_abi != Abi::RustIntrinsic {
269 throw_unsup!(FunctionAbiMismatch(caller_abi, Abi::RustIntrinsic))
272 M::call_intrinsic(self, span, instance, args, dest)?;
273 // No stack frame gets pushed, the main loop will just act as if the
275 self.goto_block(ret)?;
276 if let Some(dest) = dest {
277 self.dump_place(*dest)
281 ty::InstanceDef::VtableShim(..) |
282 ty::InstanceDef::ReifyShim(..) |
283 ty::InstanceDef::ClosureOnceShim { .. } |
284 ty::InstanceDef::FnPtrShim(..) |
285 ty::InstanceDef::DropGlue(..) |
286 ty::InstanceDef::CloneShim(..) |
287 ty::InstanceDef::Item(_) => {
291 let instance_ty = instance.ty(*self.tcx);
292 match instance_ty.kind {
294 instance_ty.fn_sig(*self.tcx).abi(),
295 ty::Closure(..) => Abi::RustCall,
296 ty::Generator(..) => Abi::Rust,
297 _ => bug!("unexpected callee ty: {:?}", instance_ty),
300 let normalize_abi = |abi| match abi {
301 Abi::Rust | Abi::RustCall | Abi::RustIntrinsic | Abi::PlatformIntrinsic =>
302 // These are all the same ABI, really.
307 if normalize_abi(caller_abi) != normalize_abi(callee_abi) {
308 throw_unsup!(FunctionAbiMismatch(caller_abi, callee_abi))
312 // We need MIR for this fn
313 let body = match M::find_fn(self, instance, args, dest, ret, unwind)? {
315 None => return Ok(()),
318 self.push_stack_frame(
323 StackPopCleanup::Goto { ret, unwind }
326 // We want to pop this frame again in case there was an error, to put
327 // the blame in the right location. Until the 2018 edition is used in
328 // the compiler, we have to do this with an immediately invoked function.
331 "caller ABI: {:?}, args: {:#?}",
334 .map(|arg| (arg.layout.ty, format!("{:?}", **arg)))
338 "spread_arg: {:?}, locals: {:#?}",
342 (local, self.layout_of_local(self.frame(), local, None).unwrap().ty)
347 // Figure out how to pass which arguments.
348 // The Rust ABI is special: ZST get skipped.
349 let rust_abi = match caller_abi {
350 Abi::Rust | Abi::RustCall => true,
353 // We have two iterators: Where the arguments come from,
354 // and where they go to.
356 // For where they come from: If the ABI is RustCall, we untuple the
357 // last incoming argument. These two iterators do not have the same type,
358 // so to keep the code paths uniform we accept an allocation
359 // (for RustCall ABI only).
360 let caller_args : Cow<'_, [OpTy<'tcx, M::PointerTag>]> =
361 if caller_abi == Abi::RustCall && !args.is_empty() {
363 let (&untuple_arg, args) = args.split_last().unwrap();
364 trace!("eval_fn_call: Will pass last argument by untupling");
365 Cow::from(args.iter().map(|&a| Ok(a))
366 .chain((0..untuple_arg.layout.fields.count()).into_iter()
367 .map(|i| self.operand_field(untuple_arg, i as u64))
369 .collect::<InterpResult<'_, Vec<OpTy<'tcx, M::PointerTag>>>>()?)
375 let mut caller_iter = caller_args.iter()
376 .filter(|op| !rust_abi || !op.layout.is_zst())
379 // Now we have to spread them out across the callee's locals,
380 // taking into account the `spread_arg`. If we could write
381 // this is a single iterator (that handles `spread_arg`), then
382 // `pass_argument` would be the loop body. It takes care to
383 // not advance `caller_iter` for ZSTs.
384 let mut locals_iter = body.args_iter();
385 while let Some(local) = locals_iter.next() {
386 let dest = self.eval_place(
387 &mir::Place::from(local)
389 if Some(local) == body.spread_arg {
391 for i in 0..dest.layout.fields.count() {
392 let dest = self.place_field(dest, i as u64)?;
393 self.pass_argument(rust_abi, &mut caller_iter, dest)?;
397 self.pass_argument(rust_abi, &mut caller_iter, dest)?;
400 // Now we should have no more caller args
401 if caller_iter.next().is_some() {
402 trace!("Caller has passed too many args");
403 throw_unsup!(FunctionArgCountMismatch)
405 // Don't forget to check the return type!
406 if let Some(caller_ret) = dest {
407 let callee_ret = self.eval_place(
408 &mir::Place::return_place()
410 if !Self::check_argument_compat(
416 FunctionRetMismatch(caller_ret.layout.ty, callee_ret.layout.ty)
420 let local = mir::RETURN_PLACE;
421 let callee_layout = self.layout_of_local(self.frame(), local, None)?;
422 if !callee_layout.abi.is_uninhabited() {
423 throw_unsup!(FunctionRetMismatch(
424 self.tcx.types.never, callee_layout.ty
438 // cannot use the shim here, because that will only result in infinite recursion
439 ty::InstanceDef::Virtual(_, idx) => {
440 let mut args = args.to_vec();
441 // We have to implement all "object safe receivers". Currently we
442 // support built-in pointers (&, &mut, Box) as well as unsized-self. We do
443 // not yet support custom self types.
444 // Also see librustc_codegen_llvm/abi.rs and librustc_codegen_llvm/mir/block.rs.
445 let receiver_place = match args[0].layout.ty.builtin_deref(true) {
448 self.deref_operand(args[0])?
452 args[0].assert_mem_place()
455 // Find and consult vtable
456 let vtable = receiver_place.vtable();
457 let drop_fn = self.get_vtable_slot(vtable, idx)?;
459 // `*mut receiver_place.layout.ty` is almost the layout that we
460 // want for args[0]: We have to project to field 0 because we want
462 assert!(receiver_place.layout.is_unsized());
463 let receiver_ptr_ty = self.tcx.mk_mut_ptr(receiver_place.layout.ty);
464 let this_receiver_ptr = self.layout_of(receiver_ptr_ty)?.field(self, 0)?;
465 // Adjust receiver argument.
466 args[0] = OpTy::from(ImmTy {
467 layout: this_receiver_ptr,
468 imm: receiver_place.ptr.into()
470 trace!("Patched self operand to {:#?}", args[0]);
471 // recurse with concrete function
472 self.eval_fn_call(drop_fn, span, caller_abi, &args, dest, ret, unwind)
479 place: PlaceTy<'tcx, M::PointerTag>,
480 instance: ty::Instance<'tcx>,
482 target: mir::BasicBlock,
483 unwind: Option<mir::BasicBlock>
484 ) -> InterpResult<'tcx> {
485 trace!("drop_in_place: {:?},\n {:?}, {:?}", *place, place.layout.ty, instance);
486 // We take the address of the object. This may well be unaligned, which is fine
487 // for us here. However, unaligned accesses will probably make the actual drop
488 // implementation fail -- a problem shared by rustc.
489 let place = self.force_allocation(place)?;
491 let (instance, place) = match place.layout.ty.kind {
493 // Dropping a trait object.
494 self.unpack_dyn_trait(place)?
496 _ => (instance, place),
501 layout: self.layout_of(self.tcx.mk_mut_ptr(place.layout.ty))?,
504 let ty = self.tcx.mk_unit(); // return type is ()
505 let dest = MPlaceTy::dangling(self.layout_of(ty)?, self);
508 FnVal::Instance(instance),