]> git.lizzy.rs Git - rust.git/blobdiff - src/abi/mod.rs
Use the new cranelift-module interface
[rust.git] / src / abi / mod.rs
index 0f62efea632de0ac275472390e62fd7e1e03739a..801691228431770e98453154e9d80a110a793653 100644 (file)
@@ -1,25 +1,41 @@
+//! Handling of everything related to the calling convention. Also fills `fx.local_map`.
+
 #[cfg(debug_assertions)]
 mod comments;
 mod pass_mode;
 mod returning;
 
+use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
 use rustc_target::spec::abi::Abi;
 
-use cranelift_codegen::ir::AbiParam;
+use cranelift_codegen::ir::{AbiParam, ArgumentPurpose};
 
 use self::pass_mode::*;
 use crate::prelude::*;
 
-pub use self::returning::{can_return_to_ssa_var, codegen_return};
+pub(crate) use self::returning::{can_return_to_ssa_var, codegen_return};
+
+// Copied from https://github.com/rust-lang/rust/blob/f52c72948aa1dd718cc1f168d21c91c584c0a662/src/librustc_middle/ty/layout.rs#L2301
+#[rustfmt::skip]
+pub(crate) fn fn_sig_for_fn_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> ty::PolyFnSig<'tcx> {
+    use rustc_middle::ty::subst::Subst;
+
+    // FIXME(davidtwco,eddyb): A `ParamEnv` should be passed through to this function.
+    let ty = instance.ty(tcx, ty::ParamEnv::reveal_all());
+    match *ty.kind() {
+        ty::FnDef(..) => {
+            // HACK(davidtwco,eddyb): This is a workaround for polymorphization considering
+            // parameters unused if they show up in the signature, but not in the `mir::Body`
+            // (i.e. due to being inside a projection that got normalized, see
+            // `src/test/ui/polymorphization/normalized_sig_types.rs`), and codegen not keeping
+            // track of a polymorphization `ParamEnv` to allow normalizing later.
+            let mut sig = match *ty.kind() {
+                ty::FnDef(def_id, substs) => tcx
+                    .normalize_erasing_regions(tcx.param_env(def_id), tcx.fn_sig(def_id))
+                    .subst(tcx, substs),
+                _ => unreachable!(),
+            };
 
-// Copied from https://github.com/rust-lang/rust/blob/c2f4c57296f0d929618baed0b0d6eb594abf01eb/src/librustc/ty/layout.rs#L2349
-pub fn fn_sig_for_fn_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> ty::PolyFnSig<'tcx> {
-    let ty = instance.monomorphic_ty(tcx);
-    match ty.kind {
-        ty::FnDef(..) |
-        // Shims currently have type FnPtr. Not sure this should remain.
-        ty::FnPtr(_) => {
-            let mut sig = ty.fn_sig(tcx);
             if let ty::InstanceDef::VtableShim(..) = instance.def {
                 // Modify `fn(self, ...)` to `fn(self: *mut Self, ...)`.
                 sig = sig.map_bound(|mut sig| {
@@ -32,46 +48,47 @@ pub fn fn_sig_for_fn_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> t
             sig
         }
         ty::Closure(def_id, substs) => {
-            let sig = substs.as_closure().sig(def_id, tcx);
+            let sig = substs.as_closure().sig();
 
             let env_ty = tcx.closure_env_ty(def_id, substs).unwrap();
-            sig.map_bound(|sig| tcx.mk_fn_sig(
-                std::iter::once(*env_ty.skip_binder()).chain(sig.inputs().iter().cloned()),
-                sig.output(),
-                sig.c_variadic,
-                sig.unsafety,
-                sig.abi
-            ))
+            sig.map_bound(|sig| {
+                tcx.mk_fn_sig(
+                    std::iter::once(env_ty.skip_binder()).chain(sig.inputs().iter().cloned()),
+                    sig.output(),
+                    sig.c_variadic,
+                    sig.unsafety,
+                    sig.abi,
+                )
+            })
         }
-        ty::Generator(def_id, substs, _) => {
-            let sig = substs.as_generator().poly_sig(def_id, tcx);
+        ty::Generator(_, substs, _) => {
+            let sig = substs.as_generator().poly_sig();
 
             let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
             let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty);
 
-            let pin_did = tcx.lang_items().pin_type().unwrap();
+            let pin_did = tcx.require_lang_item(rustc_hir::LangItem::Pin, None);
             let pin_adt_ref = tcx.adt_def(pin_did);
             let pin_substs = tcx.intern_substs(&[env_ty.into()]);
             let env_ty = tcx.mk_adt(pin_adt_ref, pin_substs);
 
             sig.map_bound(|sig| {
-                let state_did = tcx.lang_items().gen_state().unwrap();
+                let state_did = tcx.require_lang_item(rustc_hir::LangItem::GeneratorState, None);
                 let state_adt_ref = tcx.adt_def(state_did);
-                let state_substs = tcx.intern_substs(&[
-                    sig.yield_ty.into(),
-                    sig.return_ty.into(),
-                ]);
+                let state_substs =
+                    tcx.intern_substs(&[sig.yield_ty.into(), sig.return_ty.into()]);
                 let ret_ty = tcx.mk_adt(state_adt_ref, state_substs);
 
-                tcx.mk_fn_sig(std::iter::once(env_ty),
-                    ret_ty,
+                tcx.mk_fn_sig(
+                    [env_ty, sig.resume_ty].iter(),
+                    &ret_ty,
                     false,
                     rustc_hir::Unsafety::Normal,
-                    rustc_target::spec::abi::Abi::Rust
+                    rustc_target::spec::abi::Abi::Rust,
                 )
             })
         }
-        _ => bug!("unexpected type {:?} in Instance::fn_sig", ty)
+        _ => bug!("unexpected type {:?} in Instance::fn_sig", ty),
     }
 }
 
@@ -79,34 +96,42 @@ fn clif_sig_from_fn_sig<'tcx>(
     tcx: TyCtxt<'tcx>,
     triple: &target_lexicon::Triple,
     sig: FnSig<'tcx>,
+    span: Span,
     is_vtable_fn: bool,
     requires_caller_location: bool,
 ) -> Signature {
     let abi = match sig.abi {
-        Abi::System => {
-            if tcx.sess.target.target.options.is_like_windows {
-                unimplemented!()
-            } else {
-                Abi::C
-            }
-        }
+        Abi::System => Abi::C,
         abi => abi,
     };
-    let (call_conv, inputs, output): (CallConv, Vec<Ty>, Ty) = match abi {
-        Abi::Rust => (CallConv::triple_default(triple), sig.inputs().to_vec(), sig.output()),
-        Abi::C => (CallConv::triple_default(triple), sig.inputs().to_vec(), sig.output()),
+    let (call_conv, inputs, output): (CallConv, Vec<Ty<'tcx>>, Ty<'tcx>) = match abi {
+        Abi::Rust => (
+            CallConv::triple_default(triple),
+            sig.inputs().to_vec(),
+            sig.output(),
+        ),
+        Abi::C | Abi::Unadjusted => (
+            CallConv::triple_default(triple),
+            sig.inputs().to_vec(),
+            sig.output(),
+        ),
+        Abi::SysV64 => (CallConv::SystemV, sig.inputs().to_vec(), sig.output()),
         Abi::RustCall => {
             assert_eq!(sig.inputs().len(), 2);
-            let extra_args = match sig.inputs().last().unwrap().kind {
+            let extra_args = match sig.inputs().last().unwrap().kind() {
                 ty::Tuple(ref tupled_arguments) => tupled_arguments,
                 _ => bug!("argument to function with \"rust-call\" ABI is not a tuple"),
             };
-            let mut inputs: Vec<Ty> = vec![sig.inputs()[0]];
+            let mut inputs: Vec<Ty<'tcx>> = vec![sig.inputs()[0]];
             inputs.extend(extra_args.types());
             (CallConv::triple_default(triple), inputs, sig.output())
         }
         Abi::System => unreachable!(),
-        Abi::RustIntrinsic => (CallConv::triple_default(triple), sig.inputs().to_vec(), sig.output()),
+        Abi::RustIntrinsic => (
+            CallConv::triple_default(triple),
+            sig.inputs().to_vec(),
+            sig.output(),
+        ),
         _ => unimplemented!("unsupported abi {:?}", sig.abi),
     };
 
@@ -122,7 +147,29 @@ fn clif_sig_from_fn_sig<'tcx>(
                     .layout_of(ParamEnv::reveal_all().and(tcx.mk_mut_ptr(tcx.mk_unit())))
                     .unwrap();
             }
-            get_pass_mode(tcx, layout).get_param_ty(tcx).into_iter()
+            let pass_mode = get_pass_mode(tcx, layout);
+            if abi != Abi::Rust && abi != Abi::RustCall && abi != Abi::RustIntrinsic {
+                match pass_mode {
+                    PassMode::NoPass | PassMode::ByVal(_) => {}
+                    PassMode::ByRef { size: Some(size) } => {
+                        let purpose = ArgumentPurpose::StructArgument(u32::try_from(size.bytes()).expect("struct too big to pass on stack"));
+                        return EmptySinglePair::Single(AbiParam::special(pointer_ty(tcx), purpose)).into_iter();
+                    }
+                    PassMode::ByValPair(_, _) | PassMode::ByRef { size: None } => {
+                        tcx.sess.span_warn(
+                            span,
+                            &format!(
+                                "Argument of type `{:?}` with pass mode `{:?}` is not yet supported \
+                                for non-rust abi `{}`. Calling this function may result in a crash.",
+                                layout.ty,
+                                pass_mode,
+                                abi,
+                            ),
+                        );
+                    }
+                }
+            }
+            pass_mode.get_param_ty(tcx).map(AbiParam::new).into_iter()
         })
         .flatten();
 
@@ -130,26 +177,23 @@ fn clif_sig_from_fn_sig<'tcx>(
         tcx,
         tcx.layout_of(ParamEnv::reveal_all().and(output)).unwrap(),
     ) {
-        PassMode::NoPass => (inputs.map(AbiParam::new).collect(), vec![]),
-        PassMode::ByVal(ret_ty) => (
-            inputs.map(AbiParam::new).collect(),
-            vec![AbiParam::new(ret_ty)],
-        ),
+        PassMode::NoPass => (inputs.collect(), vec![]),
+        PassMode::ByVal(ret_ty) => (inputs.collect(), vec![AbiParam::new(ret_ty)]),
         PassMode::ByValPair(ret_ty_a, ret_ty_b) => (
-            inputs.map(AbiParam::new).collect(),
+            inputs.collect(),
             vec![AbiParam::new(ret_ty_a), AbiParam::new(ret_ty_b)],
         ),
-        PassMode::ByRef { sized: true } => {
+        PassMode::ByRef { size: Some(_) } => {
             (
                 Some(pointer_ty(tcx)) // First param is place to put return val
                     .into_iter()
+                    .map(|ty| AbiParam::special(ty, ArgumentPurpose::StructReturn))
                     .chain(inputs)
-                    .map(AbiParam::new)
                     .collect(),
                 vec![],
             )
         }
-        PassMode::ByRef { sized: false } => todo!(),
+        PassMode::ByRef { size: None } => todo!(),
     };
 
     if requires_caller_location {
@@ -163,26 +207,38 @@ fn clif_sig_from_fn_sig<'tcx>(
     }
 }
 
-pub fn get_function_name_and_sig<'tcx>(
+pub(crate) fn get_function_name_and_sig<'tcx>(
     tcx: TyCtxt<'tcx>,
     triple: &target_lexicon::Triple,
     inst: Instance<'tcx>,
     support_vararg: bool,
 ) -> (String, Signature) {
-    assert!(!inst.substs.needs_infer() && !inst.substs.has_param_types());
-    let fn_sig =
-        tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), &fn_sig_for_fn_abi(tcx, inst));
+    assert!(!inst.substs.needs_infer());
+    let fn_sig = tcx.normalize_erasing_late_bound_regions(
+        ParamEnv::reveal_all(),
+        &fn_sig_for_fn_abi(tcx, inst),
+    );
     if fn_sig.c_variadic && !support_vararg {
-        unimpl!("Variadic function definitions are not yet supported");
+        tcx.sess.span_fatal(
+            tcx.def_span(inst.def_id()),
+            "Variadic function definitions are not yet supported",
+        );
     }
-    let sig = clif_sig_from_fn_sig(tcx, triple, fn_sig, false, inst.def.requires_caller_location(tcx));
-    (tcx.symbol_name(inst).name.as_str().to_string(), sig)
+    let sig = clif_sig_from_fn_sig(
+        tcx,
+        triple,
+        fn_sig,
+        tcx.def_span(inst.def_id()),
+        false,
+        inst.def.requires_caller_location(tcx),
+    );
+    (tcx.symbol_name(inst).name.to_string(), sig)
 }
 
 /// Instance must be monomorphized
-pub fn import_function<'tcx>(
+pub(crate) fn import_function<'tcx>(
     tcx: TyCtxt<'tcx>,
-    module: &mut Module<impl Backend>,
+    module: &mut impl Module,
     inst: Instance<'tcx>,
 ) -> FuncId {
     let (name, sig) = get_function_name_and_sig(tcx, module.isa().triple(), inst, true);
@@ -191,21 +247,22 @@ pub fn import_function<'tcx>(
         .unwrap()
 }
 
-impl<'tcx, B: Backend + 'static> FunctionCx<'_, 'tcx, B> {
+impl<'tcx, M: Module> FunctionCx<'_, 'tcx, M> {
     /// Instance must be monomorphized
-    pub fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef {
-        let func_id = import_function(self.tcx, self.module, inst);
+    pub(crate) fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef {
+        let func_id = import_function(self.tcx, &mut self.cx.module, inst);
         let func_ref = self
+            .cx
             .module
             .declare_func_in_func(func_id, &mut self.bcx.func);
 
         #[cfg(debug_assertions)]
-        self.add_entity_comment(func_ref, format!("{:?}", inst));
+        self.add_comment(func_ref, format!("{:?}", inst));
 
         func_ref
     }
 
-    fn lib_call(
+    pub(crate) fn lib_call(
         &mut self,
         name: &str,
         input_tys: Vec<types::Type>,
@@ -218,10 +275,12 @@ fn lib_call(
             call_conv: CallConv::triple_default(self.triple()),
         };
         let func_id = self
+            .cx
             .module
             .declare_function(&name, Linkage::Import, &sig)
             .unwrap();
         let func_ref = self
+            .cx
             .module
             .declare_func_in_func(func_id, &mut self.bcx.func);
         let call_inst = self.bcx.ins().call(func_ref, args);
@@ -234,7 +293,7 @@ fn lib_call(
         results
     }
 
-    pub fn easy_call(
+    pub(crate) fn easy_call(
         &mut self,
         name: &str,
         args: &[CValue<'tcx>],
@@ -250,7 +309,7 @@ pub fn easy_call(
             })
             .unzip();
         let return_layout = self.layout_of(return_ty);
-        let return_tys = if let ty::Tuple(tup) = return_ty.kind {
+        let return_tys = if let ty::Tuple(tup) = return_ty.kind() {
             tup.types().map(|ty| self.clif_type(ty).unwrap()).collect()
         } else {
             vec![self.clif_type(return_ty).unwrap()]
@@ -258,7 +317,7 @@ pub fn easy_call(
         let ret_vals = self.lib_call(name, input_tys, return_tys, &args);
         match *ret_vals {
             [] => CValue::by_ref(
-                Pointer::const_addr(self, self.pointer_type.bytes() as i64),
+                Pointer::const_addr(self, i64::from(self.pointer_type.bytes())),
                 return_layout,
             ),
             [val] => CValue::by_val(val, return_layout),
@@ -268,14 +327,19 @@ pub fn easy_call(
     }
 }
 
-fn local_place<'tcx>(
-    fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
+/// Make a [`CPlace`] capable of holding value of the specified type.
+fn make_local_place<'tcx>(
+    fx: &mut FunctionCx<'_, 'tcx, impl Module>,
     local: Local,
-    layout: TyLayout<'tcx>,
+    layout: TyAndLayout<'tcx>,
     is_ssa: bool,
 ) -> CPlace<'tcx> {
     let place = if is_ssa {
-        CPlace::new_var(fx, local, layout)
+        if let rustc_target::abi::Abi::ScalarPair(_, _) = layout.abi {
+            CPlace::new_var_pair(fx, local, layout)
+        } else {
+            CPlace::new_var(fx, local, layout)
+        }
     } else {
         CPlace::new_stack_slot(fx, layout)
     };
@@ -283,18 +347,20 @@ fn local_place<'tcx>(
     #[cfg(debug_assertions)]
     self::comments::add_local_place_comments(fx, place, local);
 
-    let prev_place = fx.local_map.insert(local, place);
-    debug_assert!(prev_place.is_none());
-    fx.local_map[&local]
+    place
 }
 
-pub fn codegen_fn_prelude(fx: &mut FunctionCx<'_, '_, impl Backend>, start_block: Block, should_codegen_locals: bool) {
+pub(crate) fn codegen_fn_prelude<'tcx>(
+    fx: &mut FunctionCx<'_, 'tcx, impl Module>,
+    start_block: Block,
+) {
     let ssa_analyzed = crate::analyze::analyze(fx);
 
     #[cfg(debug_assertions)]
     self::comments::add_args_header_comment(fx);
 
-    self::returning::codegen_return_param(fx, &ssa_analyzed, start_block);
+    let ret_place = self::returning::codegen_return_param(fx, &ssa_analyzed, start_block);
+    assert_eq!(fx.local_map.push(ret_place), RETURN_PLACE);
 
     // None means pass_mode == NoPass
     enum ArgKind<'tcx> {
@@ -315,7 +381,7 @@ enum ArgKind<'tcx> {
                 // to reconstruct it into a tuple local variable, from multiple
                 // individual function arguments.
 
-                let tupled_arg_tys = match arg_ty.kind {
+                let tupled_arg_tys = match arg_ty.kind() {
                     ty::Tuple(ref tys) => tys,
                     _ => bug!("spread argument isn't a tuple?! but {:?}", arg_ty),
                 };
@@ -332,12 +398,14 @@ enum ArgKind<'tcx> {
                 (local, ArgKind::Normal(param), arg_ty)
             }
         })
-        .collect::<Vec<(Local, ArgKind, Ty)>>();
+        .collect::<Vec<(Local, ArgKind<'tcx>, Ty<'tcx>)>>();
 
     assert!(fx.caller_location.is_none());
     if fx.instance.def.requires_caller_location(fx.tcx) {
         // Store caller location for `#[track_caller]`.
-        fx.caller_location = Some(cvalue_for_param(fx, start_block, None, None, fx.tcx.caller_location_ty()).unwrap());
+        fx.caller_location = Some(
+            cvalue_for_param(fx, start_block, None, None, fx.tcx.caller_location_ty()).unwrap(),
+        );
     }
 
     fx.bcx.switch_to_block(start_block);
@@ -359,9 +427,8 @@ enum ArgKind<'tcx> {
                     let local_decl = &fx.mir.local_decls[local];
                     //                       v this ! is important
                     let internally_mutable = !val.layout().ty.is_freeze(
-                        fx.tcx,
+                        fx.tcx.at(local_decl.source_info.span),
                         ParamEnv::reveal_all(),
-                        local_decl.source_info.span,
                     );
                     if local_decl.mutability == mir::Mutability::Not && !internally_mutable {
                         // We wont mutate this argument, so it is fine to borrow the backing storage
@@ -376,8 +443,7 @@ enum ArgKind<'tcx> {
                         #[cfg(debug_assertions)]
                         self::comments::add_local_place_comments(fx, place, local);
 
-                        let prev_place = fx.local_map.insert(local, place);
-                        debug_assert!(prev_place.is_none());
+                        assert_eq!(fx.local_map.push(place), local);
                         continue;
                     }
                 }
@@ -385,7 +451,8 @@ enum ArgKind<'tcx> {
             _ => {}
         }
 
-        let place = local_place(fx, local, layout, is_ssa);
+        let place = make_local_place(fx, local, layout, is_ssa);
+        assert_eq!(fx.local_map.push(place), local);
 
         match arg_kind {
             ArgKind::Normal(param) => {
@@ -405,17 +472,14 @@ enum ArgKind<'tcx> {
         }
     }
 
-    // HACK should_codegen_locals required for the ``implement `<Box<F> as FnOnce>::call_once`
-    // without `alloca``` hack in `base::trans_fn`.
-    if should_codegen_locals {
-        for local in fx.mir.vars_and_temps_iter() {
-            let ty = fx.monomorphize(&fx.mir.local_decls[local].ty);
-            let layout = fx.layout_of(ty);
+    for local in fx.mir.vars_and_temps_iter() {
+        let ty = fx.monomorphize(&fx.mir.local_decls[local].ty);
+        let layout = fx.layout_of(ty);
 
-            let is_ssa = ssa_analyzed[local] == crate::analyze::SsaKind::Ssa;
+        let is_ssa = ssa_analyzed[local] == crate::analyze::SsaKind::Ssa;
 
-            local_place(fx, local, layout, is_ssa);
-        }
+        let place = make_local_place(fx, local, layout, is_ssa);
+        assert_eq!(fx.local_map.push(place), local);
     }
 
     fx.bcx
@@ -423,30 +487,32 @@ enum ArgKind<'tcx> {
         .jump(*fx.block_map.get(START_BLOCK).unwrap(), &[]);
 }
 
-pub fn codegen_terminator_call<'tcx>(
-    fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
+pub(crate) fn codegen_terminator_call<'tcx>(
+    fx: &mut FunctionCx<'_, 'tcx, impl Module>,
     span: Span,
+    current_block: Block,
     func: &Operand<'tcx>,
     args: &[Operand<'tcx>],
-    destination: &Option<(Place<'tcx>, BasicBlock)>,
+    destination: Option<(Place<'tcx>, BasicBlock)>,
 ) {
     let fn_ty = fx.monomorphize(&func.ty(fx.mir, fx.tcx));
-    let sig = fx
+    let fn_sig = fx
         .tcx
         .normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), &fn_ty.fn_sig(fx.tcx));
 
-    let destination = destination
-        .as_ref()
-        .map(|&(ref place, bb)| (trans_place(fx, place), bb));
+    let destination = destination.map(|(place, bb)| (trans_place(fx, place), bb));
 
-    if let ty::FnDef(def_id, substs) = fn_ty.kind {
-        let instance =
-            ty::Instance::resolve(fx.tcx, ty::ParamEnv::reveal_all(), def_id, substs).unwrap();
+    // Handle special calls like instrinsics and empty drop glue.
+    let instance = if let ty::FnDef(def_id, substs) = *fn_ty.kind() {
+        let instance = ty::Instance::resolve(fx.tcx, ty::ParamEnv::reveal_all(), def_id, substs)
+            .unwrap()
+            .unwrap()
+            .polymorphize(fx.tcx);
 
-        if fx.tcx.symbol_name(instance).name.as_str().starts_with("llvm.") {
-            crate::intrinsics::llvm::codegen_llvm_intrinsic_call(
+        if fx.tcx.symbol_name(instance).name.starts_with("llvm.") {
+            crate::intrinsics::codegen_llvm_intrinsic_call(
                 fx,
-                &fx.tcx.symbol_name(instance).name.as_str(),
+                &fx.tcx.symbol_name(instance).name,
                 substs,
                 args,
                 destination,
@@ -466,24 +532,39 @@ pub fn codegen_terminator_call<'tcx>(
                 fx.bcx.ins().jump(ret_block, &[]);
                 return;
             }
-            _ => {}
+            _ => Some(instance),
         }
+    } else {
+        None
+    };
+
+    let is_cold = instance
+        .map(|inst| {
+            fx.tcx
+                .codegen_fn_attrs(inst.def_id())
+                .flags
+                .contains(CodegenFnAttrFlags::COLD)
+        })
+        .unwrap_or(false);
+    if is_cold {
+        fx.cold_blocks.insert(current_block);
     }
 
     // Unpack arguments tuple for closures
-    let args = if sig.abi == Abi::RustCall {
+    let args = if fn_sig.abi == Abi::RustCall {
         assert_eq!(args.len(), 2, "rust-call abi requires two arguments");
         let self_arg = trans_operand(fx, &args[0]);
         let pack_arg = trans_operand(fx, &args[1]);
-        let mut args = Vec::new();
-        args.push(self_arg);
-        match pack_arg.layout().ty.kind {
-            ty::Tuple(ref tupled_arguments) => {
-                for (i, _) in tupled_arguments.iter().enumerate() {
-                    args.push(pack_arg.value_field(fx, mir::Field::new(i)));
-                }
-            }
+
+        let tupled_arguments = match pack_arg.layout().ty.kind() {
+            ty::Tuple(ref tupled_arguments) => tupled_arguments,
             _ => bug!("argument to function with \"rust-call\" ABI is not a tuple"),
+        };
+
+        let mut args = Vec::with_capacity(1 + tupled_arguments.len());
+        args.push(self_arg);
+        for i in 0..tupled_arguments.len() {
+            args.push(pack_arg.value_field(fx, mir::Field::new(i)));
         }
         args
     } else {
@@ -492,43 +573,6 @@ pub fn codegen_terminator_call<'tcx>(
             .collect::<Vec<_>>()
     };
 
-    codegen_call_inner(
-        fx,
-        span,
-        Some(func),
-        fn_ty,
-        args,
-        destination.map(|(place, _)| place),
-    );
-
-    if let Some((_, dest)) = destination {
-        let ret_block = fx.get_block(dest);
-        fx.bcx.ins().jump(ret_block, &[]);
-    } else {
-        trap_unreachable(fx, "[corruption] Diverging function returned");
-    }
-}
-
-fn codegen_call_inner<'tcx>(
-    fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
-    span: Span,
-    func: Option<&Operand<'tcx>>,
-    fn_ty: Ty<'tcx>,
-    args: Vec<CValue<'tcx>>,
-    ret_place: Option<CPlace<'tcx>>,
-) {
-    // FIXME mark the current block as cold when calling a `#[cold]` function.
-    let fn_sig = fx
-        .tcx
-        .normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), &fn_ty.fn_sig(fx.tcx));
-
-    let instance = match fn_ty.kind {
-        ty::FnDef(def_id, substs) => {
-            Some(Instance::resolve(fx.tcx, ParamEnv::reveal_all(), def_id, substs).unwrap())
-        }
-        _ => None,
-    };
-
     //   | indirect call target
     //   |         | the first argument to be passed
     //   v         v          v virtual calls are special cased below
@@ -569,8 +613,7 @@ fn codegen_call_inner<'tcx>(
                 let nop_inst = fx.bcx.ins().nop();
                 fx.add_comment(nop_inst, "indirect call");
             }
-            let func = trans_operand(fx, func.expect("indirect call without func Operand"))
-                .load_scalar(fx);
+            let func = trans_operand(fx, func).load_scalar(fx);
             (
                 Some(func),
                 args.get(0)
@@ -581,6 +624,7 @@ fn codegen_call_inner<'tcx>(
         }
     };
 
+    let ret_place = destination.map(|(place, _)| place);
     let (call_inst, call_args) =
         self::returning::codegen_with_call_return_arg(fx, fn_sig, ret_place, |fx, return_ptr| {
             let mut call_args: Vec<Value> = return_ptr
@@ -594,7 +638,10 @@ fn codegen_call_inner<'tcx>(
                 )
                 .collect::<Vec<_>>();
 
-            if instance.map(|inst| inst.def.requires_caller_location(fx.tcx)).unwrap_or(false) {
+            if instance
+                .map(|inst| inst.def.requires_caller_location(fx.tcx))
+                .unwrap_or(false)
+            {
                 // Pass the caller location for `#[track_caller]`.
                 let caller_location = fx.get_caller_location(span);
                 call_args.extend(adjust_arg_for_abi(fx, caller_location).into_iter());
@@ -605,6 +652,7 @@ fn codegen_call_inner<'tcx>(
                     fx.tcx,
                     fx.triple(),
                     fn_sig,
+                    span,
                     is_virtual_call,
                     false, // calls through function pointers never pass the caller location
                 );
@@ -622,7 +670,10 @@ fn codegen_call_inner<'tcx>(
     // FIXME find a cleaner way to support varargs
     if fn_sig.c_variadic {
         if fn_sig.abi != Abi::C {
-            unimpl!("Variadic call for non-C abi {:?}", fn_sig.abi);
+            fx.tcx.sess.span_fatal(
+                span,
+                &format!("Variadic call for non-C abi {:?}", fn_sig.abi),
+            );
         }
         let sig_ref = fx.bcx.func.dfg.call_signature(call_inst).unwrap();
         let abi_params = call_args
@@ -631,44 +682,53 @@ fn codegen_call_inner<'tcx>(
                 let ty = fx.bcx.func.dfg.value_type(arg);
                 if !ty.is_int() {
                     // FIXME set %al to upperbound on float args once floats are supported
-                    unimpl!("Non int ty {:?} for variadic call", ty);
+                    fx.tcx
+                        .sess
+                        .span_fatal(span, &format!("Non int ty {:?} for variadic call", ty));
                 }
                 AbiParam::new(ty)
             })
             .collect::<Vec<AbiParam>>();
         fx.bcx.func.dfg.signatures[sig_ref].params = abi_params;
     }
+
+    if let Some((_, dest)) = destination {
+        let ret_block = fx.get_block(dest);
+        fx.bcx.ins().jump(ret_block, &[]);
+    } else {
+        trap_unreachable(fx, "[corruption] Diverging function returned");
+    }
 }
 
-pub fn codegen_drop<'tcx>(
-    fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
+pub(crate) fn codegen_drop<'tcx>(
+    fx: &mut FunctionCx<'_, 'tcx, impl Module>,
     span: Span,
     drop_place: CPlace<'tcx>,
 ) {
     let ty = drop_place.layout().ty;
-    let drop_fn = Instance::resolve_drop_in_place(fx.tcx, ty);
+    let drop_fn = Instance::resolve_drop_in_place(fx.tcx, ty).polymorphize(fx.tcx);
 
     if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
         // we don't actually need to drop anything
     } else {
-        let drop_fn_ty = drop_fn.monomorphic_ty(fx.tcx);
-        match ty.kind {
+        let drop_fn_ty = drop_fn.ty(fx.tcx, ParamEnv::reveal_all());
+        let fn_sig = fx.tcx.normalize_erasing_late_bound_regions(
+            ParamEnv::reveal_all(),
+            &drop_fn_ty.fn_sig(fx.tcx),
+        );
+        assert_eq!(fn_sig.output(), fx.tcx.mk_unit());
+
+        match ty.kind() {
             ty::Dynamic(..) => {
-                let (ptr, vtable) = drop_place.to_ptr_maybe_unsized(fx);
+                let (ptr, vtable) = drop_place.to_ptr_maybe_unsized();
                 let ptr = ptr.get_addr(fx);
                 let drop_fn = crate::vtable::drop_fn_of_obj(fx, vtable.unwrap());
 
-                let fn_sig = fx.tcx.normalize_erasing_late_bound_regions(
-                    ParamEnv::reveal_all(),
-                    &drop_fn_ty.fn_sig(fx.tcx),
-                );
-
-                assert_eq!(fn_sig.output(), fx.tcx.mk_unit());
-
                 let sig = clif_sig_from_fn_sig(
                     fx.tcx,
                     fx.triple(),
                     fn_sig,
+                    span,
                     true,
                     false, // `drop_in_place` is never `#[track_caller]`
                 );
@@ -676,7 +736,9 @@ pub fn codegen_drop<'tcx>(
                 fx.bcx.ins().call_indirect(sig, drop_fn, &[ptr]);
             }
             _ => {
-                let arg_place = CPlace::new_stack_slot(
+                assert!(!matches!(drop_fn.def, InstanceDef::Virtual(_, _)));
+
+                let arg_value = drop_place.place_ref(
                     fx,
                     fx.layout_of(fx.tcx.mk_ref(
                         &ty::RegionKind::ReErased,
@@ -686,9 +748,18 @@ pub fn codegen_drop<'tcx>(
                         },
                     )),
                 );
-                drop_place.write_place_ref(fx, arg_place);
-                let arg_value = arg_place.to_cvalue(fx);
-                codegen_call_inner(fx, span, None, drop_fn_ty, vec![arg_value], None);
+                let arg_value = adjust_arg_for_abi(fx, arg_value);
+
+                let mut call_args: Vec<Value> = arg_value.into_iter().collect::<Vec<_>>();
+
+                if drop_fn.def.requires_caller_location(fx.tcx) {
+                    // Pass the caller location for `#[track_caller]`.
+                    let caller_location = fx.get_caller_location(span);
+                    call_args.extend(adjust_arg_for_abi(fx, caller_location).into_iter());
+                }
+
+                let func_ref = fx.get_function_ref(drop_fn);
+                fx.bcx.ins().call(func_ref, &call_args);
             }
         }
     }