]> git.lizzy.rs Git - rust.git/commitdiff
Introduce CodegenCx
authorbjorn3 <bjorn3@users.noreply.github.com>
Tue, 18 Dec 2018 17:28:02 +0000 (18:28 +0100)
committerbjorn3 <bjorn3@users.noreply.github.com>
Tue, 18 Dec 2018 17:28:02 +0000 (18:28 +0100)
src/base.rs
src/common.rs
src/lib.rs

index 9d5ab22a7c6b770d1c0444b532e9532fb1b73a6d..24a46ef0a988353d7812e2be8d4958fd762ddeeb 100644 (file)
@@ -9,13 +9,11 @@ fn drop(&mut self) {
     }
 }
 
-pub fn trans_mono_item<'a, 'tcx: 'a>(
-    tcx: TyCtxt<'a, 'tcx, 'tcx>,
-    module: &mut Module<impl Backend>,
-    caches: &mut Caches<'tcx>,
-    ccx: &mut crate::constant::ConstantCx,
+pub fn trans_mono_item<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
+    cx: &mut crate::CodegenCx<'a, 'clif, 'tcx, B>,
     mono_item: MonoItem<'tcx>,
 ) {
+    let tcx = cx.tcx;
     match mono_item {
         MonoItem::Fn(inst) => {
             let _inst_guard =
@@ -44,10 +42,10 @@ pub fn trans_mono_item<'a, 'tcx: 'a>(
                 }
             });
 
-            trans_fn(tcx, module, ccx, caches, inst);
+            trans_fn(cx, inst);
         }
         MonoItem::Static(def_id) => {
-            crate::constant::codegen_static(ccx, def_id);
+            crate::constant::codegen_static(&mut cx.ccx, def_id);
         }
         MonoItem::GlobalAsm(node_id) => tcx
             .sess
@@ -55,19 +53,18 @@ pub fn trans_mono_item<'a, 'tcx: 'a>(
     }
 }
 
-fn trans_fn<'a, 'tcx: 'a>(
-    tcx: TyCtxt<'a, 'tcx, 'tcx>,
-    module: &mut Module<impl Backend>,
-    constants: &mut crate::constant::ConstantCx,
-    caches: &mut Caches<'tcx>,
+fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
+    cx: &mut crate::CodegenCx<'a, 'clif, 'tcx, B>,
     instance: Instance<'tcx>,
 ) {
+    let tcx = cx.tcx;
+
     // Step 1. Get mir
     let mir = tcx.instance_mir(instance.def);
 
     // Step 2. Declare function
     let (name, sig) = get_function_name_and_sig(tcx, instance);
-    let func_id = module
+    let func_id = cx.module
         .declare_function(&name, Linkage::Export, &sig)
         .unwrap();
 
@@ -84,10 +81,10 @@ fn trans_fn<'a, 'tcx: 'a>(
     }
 
     // Step 5. Make FunctionCx
-    let pointer_type = module.target_config().pointer_type();
+    let pointer_type = cx.module.target_config().pointer_type();
     let mut fx = FunctionCx {
         tcx,
-        module,
+        module: cx.module,
         pointer_type,
 
         instance,
@@ -98,8 +95,8 @@ fn trans_fn<'a, 'tcx: 'a>(
         local_map: HashMap::new(),
 
         comments: HashMap::new(),
-        constants,
-        caches,
+        constants: &mut cx.ccx,
+        caches: &mut cx.caches,
 
         top_nop: None,
     };
@@ -132,11 +129,11 @@ fn trans_fn<'a, 'tcx: 'a>(
     verify_func(tcx, writer, &func);
 
     // Step 9. Define function
-    caches.context.func = func;
-    module
-        .define_function(func_id, &mut caches.context)
+    cx.caches.context.func = func;
+    cx.module
+        .define_function(func_id, &mut cx.caches.context)
         .unwrap();
-    caches.context.clear();
+    cx.caches.context.clear();
 }
 
 fn verify_func(tcx: TyCtxt, writer: crate::pretty_clif::CommentWriter, func: &Function) {
index 1890caaf830a5471bdc445fa73c7bd618d95db1d..29d6522f23dd9ec3038701c667c5348769503a34 100644 (file)
@@ -611,6 +611,7 @@ pub fn clif_intcast<'a, 'tcx: 'a>(
 }
 
 pub struct FunctionCx<'a, 'tcx: 'a, B: Backend> {
+    // FIXME use a reference to `CodegenCx` instead of `tcx`, `module` and `constants` and `caches`
     pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
     pub module: &'a mut Module<B>,
     pub pointer_type: Type, // Cached from module
index 3f6f2dabb3e165fc6d58553b96beaed48a1dc4cc..ec916449d57fb1be5a27e5b9a83a8a9d4fff5034 100644 (file)
@@ -97,7 +97,7 @@ mod prelude {
     pub use crate::common::*;
     pub use crate::trap::*;
     pub use crate::unimpl::{unimpl, with_unimpl_span};
-    pub use crate::Caches;
+    pub use crate::{Caches, CodegenCx};
 }
 
 pub struct Caches<'tcx> {
@@ -105,8 +105,8 @@ pub struct Caches<'tcx> {
     pub vtables: HashMap<(Ty<'tcx>, ty::PolyExistentialTraitRef<'tcx>), DataId>,
 }
 
-impl<'tcx> Caches<'tcx> {
-    fn new() -> Self {
+impl<'tcx> Default for Caches<'tcx> {
+    fn default() -> Self {
         Caches {
             context: Context::new(),
             vtables: HashMap::new(),
@@ -114,6 +114,28 @@ fn new() -> Self {
     }
 }
 
+pub struct CodegenCx<'a, 'clif, 'tcx, B: Backend + 'static> {
+    tcx: TyCtxt<'a, 'tcx, 'tcx>,
+    module: &'clif mut Module<B>,
+    ccx: ConstantCx,
+    caches: Caches<'tcx>,
+}
+
+impl<'a, 'clif, 'tcx, B: Backend + 'static> CodegenCx<'a, 'clif, 'tcx, B> {
+    fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>, module: &'clif mut Module<B>) -> Self {
+        CodegenCx {
+            tcx,
+            module,
+            ccx: ConstantCx::default(),
+            caches: Caches::default(),
+        }
+    }
+
+    fn finalize(self) {
+        self.ccx.finalize(self.tcx, self.module);
+    }
+}
+
 struct CraneliftCodegenBackend;
 
 impl CodegenBackend for CraneliftCodegenBackend {
@@ -369,17 +391,15 @@ fn codegen_mono_items<'a, 'tcx: 'a>(
     log: &mut Option<File>,
     mono_items: FxHashMap<MonoItem<'tcx>, (RLinkage, Visibility)>,
 ) {
+    let mut cx = CodegenCx::new(tcx, module);
     time("codegen mono items", move || {
-        let mut caches = Caches::new();
-        let mut ccx = ConstantCx::default();
-
         for (mono_item, (_linkage, _vis)) in mono_items {
             unimpl::try_unimpl(tcx, log, || {
-                base::trans_mono_item(tcx, module, &mut caches, &mut ccx, mono_item);
+                base::trans_mono_item(&mut cx, mono_item);
             });
         }
 
-        ccx.finalize(tcx, module);
+        cx.finalize();
     });
 }