]> git.lizzy.rs Git - rust.git/blobdiff - src/common.rs
Rollup merge of #81618 - bjorn3:sync_cg_clif-2021-02-01, r=bjorn3
[rust.git] / src / common.rs
index d1375c73a4b21e31438d332a6999327f41829c95..fbee84e09f7a6e40625dc9728b3eb4dc73e6d20c 100644 (file)
@@ -1,4 +1,5 @@
 use rustc_index::vec::IndexVec;
+use rustc_target::abi::call::FnAbi;
 use rustc_target::abi::{Integer, Primitive};
 use rustc_target::spec::{HasTargetSpec, Target};
 
@@ -131,7 +132,7 @@ pub(crate) fn has_ptr_meta<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
 }
 
 pub(crate) fn codegen_icmp_imm(
-    fx: &mut FunctionCx<'_, '_, impl Backend>,
+    fx: &mut FunctionCx<'_, '_, impl Module>,
     intcc: IntCC,
     lhs: Value,
     rhs: i128,
@@ -233,7 +234,7 @@ pub(crate) fn type_min_max_value(
             let min_msb = bcx.ins().iconst(types::I64, (min >> 64) as u64 as i64);
             let min = bcx.ins().iconcat(min_lsb, min_msb);
 
-            let max = i128::MIN as u128;
+            let max = i128::MAX as u128;
             let max_lsb = bcx.ins().iconst(types::I64, max as u64 as i64);
             let max_msb = bcx.ins().iconst(types::I64, (max >> 64) as u64 as i64);
             let max = bcx.ins().iconcat(max_lsb, max_msb);
@@ -287,13 +288,14 @@ pub(crate) fn type_sign(ty: Ty<'_>) -> bool {
     }
 }
 
-pub(crate) struct FunctionCx<'clif, 'tcx, B: Backend + 'static> {
-    pub(crate) cx: &'clif mut crate::CodegenCx<'tcx, B>,
+pub(crate) struct FunctionCx<'clif, 'tcx, M: Module> {
+    pub(crate) cx: &'clif mut crate::CodegenCx<'tcx, M>,
     pub(crate) tcx: TyCtxt<'tcx>,
     pub(crate) pointer_type: Type, // Cached from module
 
     pub(crate) instance: Instance<'tcx>,
     pub(crate) mir: &'tcx Body<'tcx>,
+    pub(crate) fn_abi: Option<FnAbi<'tcx, Ty<'tcx>>>,
 
     pub(crate) bcx: FunctionBuilder<'clif>,
     pub(crate) block_map: IndexVec<BasicBlock, Block>,
@@ -314,60 +316,49 @@ pub(crate) struct FunctionCx<'clif, 'tcx, B: Backend + 'static> {
     pub(crate) inline_asm_index: u32,
 }
 
-impl<'tcx, B: Backend> LayoutOf for FunctionCx<'_, 'tcx, B> {
+impl<'tcx, M: Module> LayoutOf for FunctionCx<'_, 'tcx, M> {
     type Ty = Ty<'tcx>;
     type TyAndLayout = TyAndLayout<'tcx>;
 
     fn layout_of(&self, ty: Ty<'tcx>) -> TyAndLayout<'tcx> {
-        assert!(!ty.still_further_specializable());
-        self.tcx
-            .layout_of(ParamEnv::reveal_all().and(&ty))
-            .unwrap_or_else(|e| {
-                if let layout::LayoutError::SizeOverflow(_) = e {
-                    self.tcx.sess.fatal(&e.to_string())
-                } else {
-                    bug!("failed to get layout for `{}`: {}", ty, e)
-                }
-            })
+        RevealAllLayoutCx(self.tcx).layout_of(ty)
     }
 }
 
-impl<'tcx, B: Backend + 'static> layout::HasTyCtxt<'tcx> for FunctionCx<'_, 'tcx, B> {
+impl<'tcx, M: Module> layout::HasTyCtxt<'tcx> for FunctionCx<'_, 'tcx, M> {
     fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
         self.tcx
     }
 }
 
-impl<'tcx, B: Backend + 'static> rustc_target::abi::HasDataLayout for FunctionCx<'_, 'tcx, B> {
+impl<'tcx, M: Module> rustc_target::abi::HasDataLayout for FunctionCx<'_, 'tcx, M> {
     fn data_layout(&self) -> &rustc_target::abi::TargetDataLayout {
         &self.tcx.data_layout
     }
 }
 
-impl<'tcx, B: Backend + 'static> layout::HasParamEnv<'tcx> for FunctionCx<'_, 'tcx, B> {
+impl<'tcx, M: Module> layout::HasParamEnv<'tcx> for FunctionCx<'_, 'tcx, M> {
     fn param_env(&self) -> ParamEnv<'tcx> {
         ParamEnv::reveal_all()
     }
 }
 
-impl<'tcx, B: Backend + 'static> HasTargetSpec for FunctionCx<'_, 'tcx, B> {
+impl<'tcx, M: Module> HasTargetSpec for FunctionCx<'_, 'tcx, M> {
     fn target_spec(&self) -> &Target {
-        &self.tcx.sess.target.target
+        &self.tcx.sess.target
     }
 }
 
-impl<'tcx, B: Backend + 'static> FunctionCx<'_, 'tcx, B> {
-    pub(crate) fn monomorphize<T>(&self, value: &T) -> T
+impl<'tcx, M: Module> FunctionCx<'_, 'tcx, M> {
+    pub(crate) fn monomorphize<T>(&self, value: T) -> T
     where
         T: TypeFoldable<'tcx> + Copy,
     {
-        if let Some(substs) = self.instance.substs_for_mir_body() {
-            self.tcx
-                .subst_and_normalize_erasing_regions(substs, ty::ParamEnv::reveal_all(), value)
-        } else {
-            self.tcx
-                .normalize_erasing_regions(ty::ParamEnv::reveal_all(), *value)
-        }
+        self.instance.subst_mir_and_normalize_erasing_regions(
+            self.tcx,
+            ty::ParamEnv::reveal_all(),
+            value,
+        )
     }
 
     pub(crate) fn clif_type(&self, ty: Ty<'tcx>) -> Option<Type> {
@@ -406,7 +397,7 @@ pub(crate) fn get_caller_location(&mut self, span: Span) -> CValue<'tcx> {
             caller.line as u32,
             caller.col_display as u32 + 1,
         ));
-        crate::constant::trans_const_value(self, const_loc, self.tcx.caller_location_ty())
+        crate::constant::codegen_const_value(self, const_loc, self.tcx.caller_location_ty())
     }
 
     pub(crate) fn triple(&self) -> &target_lexicon::Triple {
@@ -430,7 +421,6 @@ pub(crate) fn anonymous_str(&mut self, prefix: &str, msg: &str) -> Value {
                 Linkage::Local,
                 false,
                 false,
-                None,
             )
             .unwrap();
 
@@ -445,3 +435,47 @@ pub(crate) fn anonymous_str(&mut self, prefix: &str, msg: &str) -> Value {
         self.bcx.ins().global_value(self.pointer_type, local_msg_id)
     }
 }
+
+pub(crate) struct RevealAllLayoutCx<'tcx>(pub(crate) TyCtxt<'tcx>);
+
+impl<'tcx> LayoutOf for RevealAllLayoutCx<'tcx> {
+    type Ty = Ty<'tcx>;
+    type TyAndLayout = TyAndLayout<'tcx>;
+
+    fn layout_of(&self, ty: Ty<'tcx>) -> TyAndLayout<'tcx> {
+        assert!(!ty.still_further_specializable());
+        self.0
+            .layout_of(ParamEnv::reveal_all().and(&ty))
+            .unwrap_or_else(|e| {
+                if let layout::LayoutError::SizeOverflow(_) = e {
+                    self.0.sess.fatal(&e.to_string())
+                } else {
+                    bug!("failed to get layout for `{}`: {}", ty, e)
+                }
+            })
+    }
+}
+
+impl<'tcx> layout::HasTyCtxt<'tcx> for RevealAllLayoutCx<'tcx> {
+    fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
+        self.0
+    }
+}
+
+impl<'tcx> rustc_target::abi::HasDataLayout for RevealAllLayoutCx<'tcx> {
+    fn data_layout(&self) -> &rustc_target::abi::TargetDataLayout {
+        &self.0.data_layout
+    }
+}
+
+impl<'tcx> layout::HasParamEnv<'tcx> for RevealAllLayoutCx<'tcx> {
+    fn param_env(&self) -> ParamEnv<'tcx> {
+        ParamEnv::reveal_all()
+    }
+}
+
+impl<'tcx> HasTargetSpec for RevealAllLayoutCx<'tcx> {
+    fn target_spec(&self) -> &Target {
+        &self.0.sess.target
+    }
+}