]> git.lizzy.rs Git - rust.git/blobdiff - src/abi/mod.rs
Use the new cranelift-module interface
[rust.git] / src / abi / mod.rs
index 1d1aec3f91de48c3d09f5ef15b3f874c2bc8d049..801691228431770e98453154e9d80a110a793653 100644 (file)
@@ -1,10 +1,12 @@
+//! 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_target::spec::abi::Abi;
 use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
+use rustc_target::spec::abi::Abi;
 
 use cranelift_codegen::ir::{AbiParam, ArgumentPurpose};
 
 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 {
+    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 {
+            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),
@@ -64,13 +67,13 @@ pub(crate) fn fn_sig_for_fn_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx
             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.require_lang_item(rustc_hir::LangItem::PinTypeLangItem, None);
+            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.require_lang_item(rustc_hir::LangItem::GeneratorStateLangItem, None);
+                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()]);
@@ -102,12 +105,20 @@ fn clif_sig_from_fn_sig<'tcx>(
         abi => abi,
     };
     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::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"),
             };
@@ -116,7 +127,11 @@ fn clif_sig_from_fn_sig<'tcx>(
             (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),
     };
 
@@ -163,10 +178,7 @@ fn clif_sig_from_fn_sig<'tcx>(
         tcx.layout_of(ParamEnv::reveal_all().and(output)).unwrap(),
     ) {
         PassMode::NoPass => (inputs.collect(), vec![]),
-        PassMode::ByVal(ret_ty) => (
-            inputs.collect(),
-            vec![AbiParam::new(ret_ty)],
-        ),
+        PassMode::ByVal(ret_ty) => (inputs.collect(), vec![AbiParam::new(ret_ty)]),
         PassMode::ByValPair(ret_ty_a, ret_ty_b) => (
             inputs.collect(),
             vec![AbiParam::new(ret_ty_a), AbiParam::new(ret_ty_b)],
@@ -202,19 +214,31 @@ pub(crate) fn get_function_name_and_sig<'tcx>(
     support_vararg: bool,
 ) -> (String, Signature) {
     assert!(!inst.substs.needs_infer());
-    let fn_sig =
-        tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), &fn_sig_for_fn_abi(tcx, inst));
+    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 {
-        tcx.sess.span_fatal(tcx.def_span(inst.def_id()), "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, tcx.def_span(inst.def_id()), false, inst.def.requires_caller_location(tcx));
+    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(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);
@@ -223,12 +247,13 @@ pub(crate) 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(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
+            .cx
+            .module
             .declare_func_in_func(func_id, &mut self.bcx.func);
 
         #[cfg(debug_assertions)]
@@ -250,11 +275,13 @@ pub(crate) fn lib_call(
             call_conv: CallConv::triple_default(self.triple()),
         };
         let func_id = self
-            .cx.module
+            .cx
+            .module
             .declare_function(&name, Linkage::Import, &sig)
             .unwrap();
         let func_ref = self
-            .cx.module
+            .cx
+            .module
             .declare_func_in_func(func_id, &mut self.bcx.func);
         let call_inst = self.bcx.ins().call(func_ref, args);
         #[cfg(debug_assertions)]
@@ -282,7 +309,7 @@ pub(crate) 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()]
@@ -300,8 +327,9 @@ pub(crate) 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: TyAndLayout<'tcx>,
     is_ssa: bool,
@@ -319,13 +347,11 @@ 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(crate) fn codegen_fn_prelude<'tcx>(
-    fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
+    fx: &mut FunctionCx<'_, 'tcx, impl Module>,
     start_block: Block,
 ) {
     let ssa_analyzed = crate::analyze::analyze(fx);
@@ -333,7 +359,8 @@ pub(crate) fn codegen_fn_prelude<'tcx>(
     #[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> {
@@ -354,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),
                 };
@@ -376,7 +403,9 @@ enum ArgKind<'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);
@@ -414,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;
                     }
                 }
@@ -423,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) => {
@@ -449,7 +478,8 @@ enum ArgKind<'tcx> {
 
         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
@@ -458,7 +488,7 @@ enum ArgKind<'tcx> {
 }
 
 pub(crate) fn codegen_terminator_call<'tcx>(
-    fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
+    fx: &mut FunctionCx<'_, 'tcx, impl Module>,
     span: Span,
     current_block: Block,
     func: &Operand<'tcx>,
@@ -473,7 +503,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
     let destination = destination.map(|(place, bb)| (trans_place(fx, place), bb));
 
     // Handle special calls like instrinsics and empty drop glue.
-    let instance = if let ty::FnDef(def_id, substs) = fn_ty.kind {
+    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()
@@ -502,17 +532,20 @@ pub(crate) fn codegen_terminator_call<'tcx>(
                 fx.bcx.ins().jump(ret_block, &[]);
                 return;
             }
-            _ => Some(instance)
+            _ => 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);
+    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);
     }
@@ -523,10 +556,8 @@ pub(crate) fn codegen_terminator_call<'tcx>(
         let self_arg = trans_operand(fx, &args[0]);
         let pack_arg = trans_operand(fx, &args[1]);
 
-        let tupled_arguments = match pack_arg.layout().ty.kind {
-            ty::Tuple(ref tupled_arguments) => {
-                tupled_arguments
-            }
+        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"),
         };
 
@@ -582,8 +613,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
                 let nop_inst = fx.bcx.ins().nop();
                 fx.add_comment(nop_inst, "indirect call");
             }
-            let func = trans_operand(fx, func)
-                .load_scalar(fx);
+            let func = trans_operand(fx, func).load_scalar(fx);
             (
                 Some(func),
                 args.get(0)
@@ -608,7 +638,10 @@ pub(crate) fn codegen_terminator_call<'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());
@@ -637,7 +670,10 @@ pub(crate) fn codegen_terminator_call<'tcx>(
     // FIXME find a cleaner way to support varargs
     if fn_sig.c_variadic {
         if fn_sig.abi != Abi::C {
-            fx.tcx.sess.span_fatal(span, &format!("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
@@ -646,7 +682,9 @@ pub(crate) fn codegen_terminator_call<'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
-                    fx.tcx.sess.span_fatal(span, &format!("Non int ty {:?} for variadic call", ty));
+                    fx.tcx
+                        .sess
+                        .span_fatal(span, &format!("Non int ty {:?} for variadic call", ty));
                 }
                 AbiParam::new(ty)
             })
@@ -663,7 +701,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
 }
 
 pub(crate) fn codegen_drop<'tcx>(
-    fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
+    fx: &mut FunctionCx<'_, 'tcx, impl Module>,
     span: Span,
     drop_place: CPlace<'tcx>,
 ) {
@@ -680,7 +718,7 @@ pub(crate) fn codegen_drop<'tcx>(
         );
         assert_eq!(fn_sig.output(), fx.tcx.mk_unit());
 
-        match ty.kind {
+        match ty.kind() {
             ty::Dynamic(..) => {
                 let (ptr, vtable) = drop_place.to_ptr_maybe_unsized();
                 let ptr = ptr.get_addr(fx);
@@ -700,13 +738,16 @@ pub(crate) fn codegen_drop<'tcx>(
             _ => {
                 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,
-                    TypeAndMut {
-                        ty,
-                        mutbl: crate::rustc_hir::Mutability::Mut,
-                    },
-                )));
+                let arg_value = drop_place.place_ref(
+                    fx,
+                    fx.layout_of(fx.tcx.mk_ref(
+                        &ty::RegionKind::ReErased,
+                        TypeAndMut {
+                            ty,
+                            mutbl: crate::rustc_hir::Mutability::Mut,
+                        },
+                    )),
+                );
                 let arg_value = adjust_arg_for_abi(fx, arg_value);
 
                 let mut call_args: Vec<Value> = arg_value.into_iter().collect::<Vec<_>>();