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 GlobalId, InterpResult, PointerArithmetic,
11 InterpCx, Machine, OpTy, ImmTy, PlaceTy, MPlaceTy, StackPopCleanup, FnVal,
14 impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
15 pub(super) fn eval_terminator(
17 terminator: &mir::Terminator<'tcx>,
18 ) -> InterpResult<'tcx> {
19 use rustc::mir::TerminatorKind::*;
20 match terminator.kind {
22 self.frame().return_place.map(|r| self.dump_place(*r));
23 self.pop_stack_frame(/* unwinding */ false)?
26 Goto { target } => self.go_to_block(target),
34 let discr = self.read_immediate(self.eval_operand(discr, None)?)?;
35 trace!("SwitchInt({:?})", *discr);
37 // Branch to the `otherwise` case by default, if no match is found.
38 let mut target_block = targets[targets.len() - 1];
40 for (index, &const_int) in values.iter().enumerate() {
41 // Compare using binary_op, to also support pointer values
42 let res = self.overflowing_binary_op(mir::BinOp::Eq,
44 ImmTy::from_uint(const_int, discr.layout),
47 target_block = targets[index];
52 self.go_to_block(target_block);
62 let func = self.eval_operand(func, None)?;
63 let (fn_val, abi) = match func.layout.ty.kind {
65 let caller_abi = sig.abi();
66 let fn_ptr = self.read_scalar(func)?.not_undef()?;
67 let fn_val = self.memory.get_fn(fn_ptr)?;
70 ty::FnDef(def_id, substs) => {
71 let sig = func.layout.ty.fn_sig(*self.tcx);
72 (FnVal::Instance(self.resolve(def_id, substs)?), sig.abi())
75 bug!("invalid callee of type {:?}", func.layout.ty)
78 let args = self.eval_operands(args)?;
79 let ret = match destination {
80 Some((dest, ret)) => Some((self.eval_place(dest)?, *ret)),
85 terminator.source_info.span,
98 // FIXME(CTFE): forbid drop in const eval
99 let place = self.eval_place(location)?;
100 let ty = place.layout.ty;
101 trace!("TerminatorKind::drop: {:?}, type {}", location, ty);
103 let instance = Instance::resolve_drop_in_place(*self.tcx, ty);
107 terminator.source_info.span,
120 let cond_val = self.read_immediate(self.eval_operand(cond, None)?)?
121 .to_scalar()?.to_bool()?;
122 if expected == cond_val {
123 self.go_to_block(target);
125 // Compute error message
126 use rustc::mir::interpret::PanicInfo::*;
127 return Err(match msg {
128 BoundsCheck { ref len, ref index } => {
130 .read_immediate(self.eval_operand(len, None)?)
131 .expect("can't eval len")
133 .to_bits(self.memory.pointer_size())? as u64;
135 .read_immediate(self.eval_operand(index, None)?)
136 .expect("can't eval index")
138 .to_bits(self.memory.pointer_size())? as u64;
139 err_panic!(BoundsCheck { len, index })
141 Overflow(op) => err_panic!(Overflow(*op)),
142 OverflowNeg => err_panic!(OverflowNeg),
143 DivisionByZero => err_panic!(DivisionByZero),
144 RemainderByZero => err_panic!(RemainderByZero),
145 GeneratorResumedAfterReturn => err_panic!(GeneratorResumedAfterReturn),
146 GeneratorResumedAfterPanic => err_panic!(GeneratorResumedAfterPanic),
147 Panic { .. } => bug!("`Panic` variant cannot occur in MIR"),
154 // When we encounter Resume, we've finished unwinding
155 // cleanup for the current stack frame. We pop it in order
156 // to continue unwinding the next frame
158 trace!("unwinding: resuming from cleanup");
159 // By definition, a Resume terminator means
160 // that we're unwinding
161 self.pop_stack_frame(/* unwinding */ true)?;
167 DropAndReplace { .. } |
168 Abort => unimplemented!("{:#?}", terminator.kind),
169 FalseEdges { .. } => bug!("should have been eliminated by\
170 `simplify_branches` mir pass"),
171 FalseUnwind { .. } => bug!("should have been eliminated by\
172 `simplify_branches` mir pass"),
173 Unreachable => throw_ub!(Unreachable),
179 fn check_argument_compat(
181 caller: TyLayout<'tcx>,
182 callee: TyLayout<'tcx>,
184 if caller.ty == callee.ty {
189 // Don't risk anything
193 match (&caller.abi, &callee.abi) {
194 // Different valid ranges are okay (once we enforce validity,
195 // that will take care to make it UB to leave the range, just
196 // like for transmute).
197 (layout::Abi::Scalar(ref caller), layout::Abi::Scalar(ref callee)) =>
198 caller.value == callee.value,
199 (layout::Abi::ScalarPair(ref caller1, ref caller2),
200 layout::Abi::ScalarPair(ref callee1, ref callee2)) =>
201 caller1.value == callee1.value && caller2.value == callee2.value,
207 /// Pass a single argument, checking the types for compatibility.
211 caller_arg: &mut impl Iterator<Item=OpTy<'tcx, M::PointerTag>>,
212 callee_arg: PlaceTy<'tcx, M::PointerTag>,
213 ) -> InterpResult<'tcx> {
214 if rust_abi && callee_arg.layout.is_zst() {
216 trace!("Skipping callee ZST");
219 let caller_arg = caller_arg.next()
220 .ok_or_else(|| err_unsup!(FunctionArgCountMismatch)) ?;
222 debug_assert!(!caller_arg.layout.is_zst(), "ZSTs must have been already filtered out");
225 if !Self::check_argument_compat(rust_abi, caller_arg.layout, callee_arg.layout) {
226 throw_unsup!(FunctionArgMismatch(caller_arg.layout.ty, callee_arg.layout.ty))
228 // We allow some transmutes here
229 self.copy_op_transmute(caller_arg, callee_arg)
232 /// Call this function -- pushing the stack frame and initializing the arguments.
235 fn_val: FnVal<'tcx, M::ExtraFnVal>,
238 args: &[OpTy<'tcx, M::PointerTag>],
239 ret: Option<(PlaceTy<'tcx, M::PointerTag>, mir::BasicBlock)>,
240 unwind: Option<mir::BasicBlock>
241 ) -> InterpResult<'tcx> {
242 trace!("eval_fn_call: {:#?}", fn_val);
244 let instance = match fn_val {
245 FnVal::Instance(instance) => instance,
246 FnVal::Other(extra) => {
247 return M::call_extra_fn(self, extra, args, ret, unwind);
254 let instance_ty = instance.ty(*self.tcx);
255 match instance_ty.kind {
257 instance_ty.fn_sig(*self.tcx).abi(),
258 ty::Closure(..) => Abi::RustCall,
259 ty::Generator(..) => Abi::Rust,
260 _ => bug!("unexpected callee ty: {:?}", instance_ty),
263 let normalize_abi = |abi| match abi {
264 Abi::Rust | Abi::RustCall | Abi::RustIntrinsic | Abi::PlatformIntrinsic =>
265 // These are all the same ABI, really.
270 if normalize_abi(caller_abi) != normalize_abi(callee_abi) {
271 throw_unsup!(FunctionAbiMismatch(caller_abi, callee_abi))
276 ty::InstanceDef::Intrinsic(..) => {
277 assert!(caller_abi == Abi::RustIntrinsic || caller_abi == Abi::PlatformIntrinsic);
278 return M::call_intrinsic(self, span, instance, args, ret, unwind);
280 ty::InstanceDef::VtableShim(..) |
281 ty::InstanceDef::ReifyShim(..) |
282 ty::InstanceDef::ClosureOnceShim { .. } |
283 ty::InstanceDef::FnPtrShim(..) |
284 ty::InstanceDef::DropGlue(..) |
285 ty::InstanceDef::CloneShim(..) |
286 ty::InstanceDef::Item(_) => {
287 // We need MIR for this fn
288 let body = match M::find_mir_or_eval_fn(self, instance, args, ret, unwind)? {
290 None => return Ok(()),
293 self.push_stack_frame(
298 StackPopCleanup::Goto { ret: ret.map(|p| p.1), unwind }
301 // We want to pop this frame again in case there was an error, to put
302 // the blame in the right location. Until the 2018 edition is used in
303 // the compiler, we have to do this with an immediately invoked function.
306 "caller ABI: {:?}, args: {:#?}",
309 .map(|arg| (arg.layout.ty, format!("{:?}", **arg)))
313 "spread_arg: {:?}, locals: {:#?}",
317 (local, self.layout_of_local(self.frame(), local, None).unwrap().ty)
322 // Figure out how to pass which arguments.
323 // The Rust ABI is special: ZST get skipped.
324 let rust_abi = match caller_abi {
325 Abi::Rust | Abi::RustCall => true,
328 // We have two iterators: Where the arguments come from,
329 // and where they go to.
331 // For where they come from: If the ABI is RustCall, we untuple the
332 // last incoming argument. These two iterators do not have the same type,
333 // so to keep the code paths uniform we accept an allocation
334 // (for RustCall ABI only).
335 let caller_args : Cow<'_, [OpTy<'tcx, M::PointerTag>]> =
336 if caller_abi == Abi::RustCall && !args.is_empty() {
338 let (&untuple_arg, args) = args.split_last().unwrap();
339 trace!("eval_fn_call: Will pass last argument by untupling");
340 Cow::from(args.iter().map(|&a| Ok(a))
341 .chain((0..untuple_arg.layout.fields.count()).into_iter()
342 .map(|i| self.operand_field(untuple_arg, i as u64))
344 .collect::<InterpResult<'_, Vec<OpTy<'tcx, M::PointerTag>>>>()?)
350 let mut caller_iter = caller_args.iter()
351 .filter(|op| !rust_abi || !op.layout.is_zst())
354 // Now we have to spread them out across the callee's locals,
355 // taking into account the `spread_arg`. If we could write
356 // this is a single iterator (that handles `spread_arg`), then
357 // `pass_argument` would be the loop body. It takes care to
358 // not advance `caller_iter` for ZSTs.
359 let mut locals_iter = body.args_iter();
360 while let Some(local) = locals_iter.next() {
361 let dest = self.eval_place(
362 &mir::Place::from(local)
364 if Some(local) == body.spread_arg {
366 for i in 0..dest.layout.fields.count() {
367 let dest = self.place_field(dest, i as u64)?;
368 self.pass_argument(rust_abi, &mut caller_iter, dest)?;
372 self.pass_argument(rust_abi, &mut caller_iter, dest)?;
375 // Now we should have no more caller args
376 if caller_iter.next().is_some() {
377 trace!("Caller has passed too many args");
378 throw_unsup!(FunctionArgCountMismatch)
380 // Don't forget to check the return type!
381 if let Some((caller_ret, _)) = ret {
382 let callee_ret = self.eval_place(
383 &mir::Place::return_place()
385 if !Self::check_argument_compat(
391 FunctionRetMismatch(caller_ret.layout.ty, callee_ret.layout.ty)
395 let local = mir::RETURN_PLACE;
396 let callee_layout = self.layout_of_local(self.frame(), local, None)?;
397 if !callee_layout.abi.is_uninhabited() {
398 throw_unsup!(FunctionRetMismatch(
399 self.tcx.types.never, callee_layout.ty
413 // cannot use the shim here, because that will only result in infinite recursion
414 ty::InstanceDef::Virtual(_, idx) => {
415 let mut args = args.to_vec();
416 // We have to implement all "object safe receivers". Currently we
417 // support built-in pointers (&, &mut, Box) as well as unsized-self. We do
418 // not yet support custom self types.
419 // Also see librustc_codegen_llvm/abi.rs and librustc_codegen_llvm/mir/block.rs.
420 let receiver_place = match args[0].layout.ty.builtin_deref(true) {
423 self.deref_operand(args[0])?
427 args[0].assert_mem_place()
430 // Find and consult vtable
431 let vtable = receiver_place.vtable();
432 let drop_fn = self.get_vtable_slot(vtable, idx)?;
434 // `*mut receiver_place.layout.ty` is almost the layout that we
435 // want for args[0]: We have to project to field 0 because we want
437 assert!(receiver_place.layout.is_unsized());
438 let receiver_ptr_ty = self.tcx.mk_mut_ptr(receiver_place.layout.ty);
439 let this_receiver_ptr = self.layout_of(receiver_ptr_ty)?.field(self, 0)?;
440 // Adjust receiver argument.
441 args[0] = OpTy::from(ImmTy {
442 layout: this_receiver_ptr,
443 imm: receiver_place.ptr.into()
445 trace!("Patched self operand to {:#?}", args[0]);
446 // recurse with concrete function
447 self.eval_fn_call(drop_fn, span, caller_abi, &args, ret, unwind)
452 /// Evaluate a const function where all arguments (if any) are zero-sized types.
453 /// The evaluation is memoized thanks to the query system.
454 pub (crate) fn eval_const_fn_call(
457 ret: Option<(PlaceTy<'tcx, M::PointerTag>, mir::BasicBlock)>,
458 ) -> InterpResult<'tcx> {
459 trace!("eval_const_fn_call: {:?}", gid);
461 let place = self.const_eval_raw(gid)?;
462 let dest = ret.ok_or_else(|| err_ub!(Unreachable))?.0;
464 self.copy_op(place.into(), dest)?;
466 self.return_to_block(ret.map(|r| r.1))?;
467 self.dump_place(*dest);
473 place: PlaceTy<'tcx, M::PointerTag>,
474 instance: ty::Instance<'tcx>,
476 target: mir::BasicBlock,
477 unwind: Option<mir::BasicBlock>
478 ) -> InterpResult<'tcx> {
479 trace!("drop_in_place: {:?},\n {:?}, {:?}", *place, place.layout.ty, instance);
480 // We take the address of the object. This may well be unaligned, which is fine
481 // for us here. However, unaligned accesses will probably make the actual drop
482 // implementation fail -- a problem shared by rustc.
483 let place = self.force_allocation(place)?;
485 let (instance, place) = match place.layout.ty.kind {
487 // Dropping a trait object.
488 self.unpack_dyn_trait(place)?
490 _ => (instance, place),
495 layout: self.layout_of(self.tcx.mk_mut_ptr(place.layout.ty))?,
498 let ty = self.tcx.mk_unit(); // return type is ()
499 let dest = MPlaceTy::dangling(self.layout_of(ty)?, self);
502 FnVal::Instance(instance),
506 Some((dest.into(), target)),