]> git.lizzy.rs Git - rust.git/commitdiff
Support simplejit and faerie at the same time
authorbjorn3 <bjorn3@users.noreply.github.com>
Tue, 14 Aug 2018 18:58:24 +0000 (20:58 +0200)
committerbjorn3 <bjorn3@users.noreply.github.com>
Tue, 14 Aug 2018 18:58:24 +0000 (20:58 +0200)
examples/mini_core_hello_world.rs
src/base.rs
src/common.rs
src/lib.rs

index 3ac2e8966b3679b24d2dda079d55c2066bcfa73f..014d7aaa40c4b5e798312cca8d9f89b0e8116009 100644 (file)
@@ -26,6 +26,8 @@ fn start(_main: *const u8, i: isize, _: *const *const u8) -> isize {
         puts(ptr);
     }
 
+    //panic(&("panic msg", "abc.rs", 0, 43));
+
     unsafe {
         NUM = 6 * 7 + 1 + (1u8 == 1u8) as u8; // 44
         *NUM_REF as isize
index 4c91db448c7bd82e7888bc4f7c20d1363a56caef..3944d474d26a3f36b2ef6caa1be22592be72b5a1 100644 (file)
@@ -1,10 +1,8 @@
 use crate::prelude::*;
 
-pub fn trans_mono_item<'a, 'tcx: 'a>(
-    cx: &mut CodegenCx<'a, 'tcx, CurrentBackend>,
-    mono_item: MonoItem<'tcx>,
-) {
+pub fn trans_mono_item<'a, 'tcx: 'a>(cx: &mut CodegenCx<'a, 'tcx>, mono_item: MonoItem<'tcx>) {
     let tcx = cx.tcx;
+    let context = &mut cx.context;
 
     match mono_item {
         MonoItem::Fn(inst) => match inst {
@@ -20,8 +18,10 @@ pub fn trans_mono_item<'a, 'tcx: 'a>(
                     String::from_utf8_lossy(&mir.into_inner())
                 ));
 
-                let func_id = trans_fn(cx.tcx, cx.module, &mut cx.constants, &mut cx.context, inst);
-                cx.defined_functions.push(func_id);
+                let res = each_module!(cx, |(ccx, m)| trans_fn(tcx, *m, ccx, context, inst));
+                if let Some(func_id) = res.jit {
+                    cx.defined_functions.push(func_id);
+                };
             }
             Instance {
                 def: InstanceDef::DropGlue(_, _),
@@ -30,7 +30,9 @@ pub fn trans_mono_item<'a, 'tcx: 'a>(
             inst => unimpl!("Unimplemented instance {:?}", inst),
         },
         MonoItem::Static(def_id) => {
-            crate::constant::codegen_static(&mut cx.constants, def_id);
+            each_module!(cx, |(ccx, _m)| {
+                crate::constant::codegen_static(ccx, def_id);
+            });
         }
         MonoItem::GlobalAsm(node_id) => cx
             .tcx
@@ -41,7 +43,7 @@ pub fn trans_mono_item<'a, 'tcx: 'a>(
 
 fn trans_fn<'a, 'tcx: 'a>(
     tcx: TyCtxt<'a, 'tcx, 'tcx>,
-    module: &mut Module<SimpleJITBackend>,
+    module: &mut Module<impl Backend>,
     constants: &mut crate::constant::ConstantCx,
     context: &mut Context,
     instance: Instance<'tcx>,
index e0c66218ba2645cbbe7df70386b99cee7f039a9f..7ed91a30d2f75d4015f622dd666fc3a8557d887b 100644 (file)
@@ -6,8 +6,6 @@
 
 use crate::prelude::*;
 
-pub type CurrentBackend = ::cranelift_simplejit::SimpleJITBackend;
-
 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
 pub struct Variable(pub Local);
 
index 9e6e9acf4c0c4be5ef76e74745ceb4eb99fa723c..b2f87a2171d672e89b50fe6256ffdaab8402a152 100644 (file)
@@ -47,6 +47,15 @@ macro_rules! unimpl {
     };
 }
 
+macro_rules! each_module {
+    ($cx:expr, |$p:pat| $res:expr) => {
+        ModuleTup {
+            jit: $cx.jit.as_mut().map(|$p| $res),
+            faerie: $cx.faerie.as_mut().map(|$p| $res),
+        }
+    };
+}
+
 mod abi;
 mod analyze;
 mod base;
@@ -88,7 +97,7 @@ mod prelude {
     pub use crate::common::Variable;
     pub use crate::common::*;
 
-    pub use crate::CodegenCx;
+    pub use crate::{CodegenCx, ModuleTup};
 
     pub fn should_codegen(tcx: TyCtxt) -> bool {
         ::std::env::var("SHOULD_CODEGEN").is_ok()
@@ -96,18 +105,24 @@ pub fn should_codegen(tcx: TyCtxt) -> bool {
     }
 }
 
+use crate::constant::ConstantCx;
 use crate::prelude::*;
 
-pub struct CodegenCx<'a, 'tcx: 'a, B: Backend + 'a> {
+pub struct CodegenCx<'a, 'tcx: 'a> {
     pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
-    pub module: &'a mut Module<B>,
-    pub constants: crate::constant::ConstantCx,
+    pub jit: Option<(ConstantCx, &'a mut Module<SimpleJITBackend>)>,
+    pub faerie: Option<(ConstantCx, &'a mut Module<FaerieBackend>)>,
     pub defined_functions: Vec<FuncId>,
 
     // Cache
     pub context: Context,
 }
 
+pub struct ModuleTup<T> {
+    jit: Option<T>,
+    faerie: Option<T>,
+}
+
 struct CraneliftMetadataLoader;
 
 impl MetadataLoader for CraneliftMetadataLoader {
@@ -265,8 +280,8 @@ fn codegen_crate<'a, 'tcx>(
             use std::io::Write;
             let mut cx = CodegenCx {
                 tcx,
-                module: &mut jit_module,
-                constants: Default::default(),
+                jit: Some((ConstantCx::default(), &mut jit_module)),
+                faerie: Some((ConstantCx::default(), &mut faerie_module)),
                 defined_functions: Vec::new(),
 
                 context: Context::new(),
@@ -299,7 +314,18 @@ fn codegen_crate<'a, 'tcx>(
                 }
             }
 
-            cx.constants.finalize(tcx, &mut cx.module);
+            match cx {
+                CodegenCx {
+                    tcx,
+                    jit,
+                    faerie,
+                    defined_functions: _,
+                    context: _,
+                } => {
+                    jit.map(|jit| jit.0.finalize(tcx, jit.1));
+                    faerie.map(|faerie| faerie.0.finalize(tcx, faerie.1));
+                }
+            }
 
             let after = ::std::time::Instant::now();
             println!("time: {:?}", after - before);
@@ -307,6 +333,8 @@ fn codegen_crate<'a, 'tcx>(
             cx.defined_functions
         };
 
+        tcx.sess.abort_if_errors();
+
         tcx.sess.warn("Compiled everything");
 
         // TODO: this doesn't work most of the time
@@ -334,9 +362,8 @@ fn codegen_crate<'a, 'tcx>(
 
             jit_module.finish();
         } else if should_codegen(tcx) {
-            for func_id in defined_functions {
-                jit_module.finalize_function(func_id);
-            }
+            jit_module.finalize_all();
+            faerie_module.finalize_all();
 
             tcx.sess.warn("Finalized everything");
         }