]> git.lizzy.rs Git - rust.git/commitdiff
Pass command-line arguments to JITed function
authorbjorn3 <bjorn3@users.noreply.github.com>
Mon, 18 Feb 2019 17:53:18 +0000 (18:53 +0100)
committerbjorn3 <bjorn3@users.noreply.github.com>
Mon, 18 Feb 2019 17:53:18 +0000 (18:53 +0100)
Cherry-picked from f1f35405e15ca1b77425514b04b96b2749231899 by
@milkey-mouse

example/mini_core.rs
example/mini_core_hello_world.rs
src/lib.rs
test.sh

index 66314ab3e5d3e82863877a2c3bb424ece889fde8..b7277f5ee9f489867bedf15f32be363c47ccbd5a 100644 (file)
@@ -104,6 +104,14 @@ fn mul(self, rhs: Self) -> Self::Output {
     }
 }
 
+impl Mul for usize {
+    type Output = Self;
+
+    fn mul(self, rhs: Self) -> Self::Output {
+        self * rhs
+    }
+}
+
 #[lang = "add"]
 pub trait Add<RHS = Self> {
     type Output;
@@ -208,6 +216,15 @@ fn ne(&self, other: &usize) -> bool {
     }
 }
 
+impl PartialEq for isize {
+    fn eq(&self, other: &isize) -> bool {
+        (*self) == (*other)
+    }
+    fn ne(&self, other: &isize) -> bool {
+        (*self) != (*other)
+    }
+}
+
 impl PartialEq for char {
     fn eq(&self, other: &char) -> bool {
         (*self) == (*other)
index 871aebddee9c79c77f884e03965a2683662dc4d4..5ab7575252b077f424e82d9242daa519cebc9109 100644 (file)
@@ -75,9 +75,15 @@ enum Ordering {
 #[lang = "start"]
 fn start<T: Termination + 'static>(
     main: fn() -> T,
-    _argc: isize,
-    _argv: *const *const u8,
+    argc: isize,
+    argv: *const *const u8,
 ) -> isize {
+    if argc == 3 {
+        unsafe { puts(*argv); }
+        unsafe { puts(*((argv as usize + intrinsics::size_of::<*const u8>()) as *const *const u8)); }
+        unsafe { puts(*((argv as usize + 2 * intrinsics::size_of::<*const u8>()) as *const *const u8)); }
+    }
+
     main().report();
     0
 }
index 5863f730bcace6432b299eb4fa8866833c8abc21..ead24921652d3d7686c41ae8aa9ad78a63d8d6ec 100644 (file)
@@ -16,6 +16,8 @@
 use std::any::Any;
 use std::fs::File;
 use std::sync::mpsc;
+use std::os::raw::{c_char, c_int};
+use std::ffi::CString;
 
 use rustc::dep_graph::DepGraph;
 use rustc::middle::cstore::MetadataLoader;
@@ -241,19 +243,26 @@ fn codegen_crate<'a, 'tcx>(
             jit_module.finalize_definitions();
 
             tcx.sess.abort_if_errors();
-            println!("Compiled everything");
-            println!("Rustc codegen cranelift will JIT run the executable, because the SHOULD_RUN env var is set");
 
             let finalized_main: *const u8 = jit_module.get_finalized_function(main_func_id);
-            println!("🎉 Finalized everything");
 
-            let f: extern "C" fn(isize, *const *const u8) -> isize =
+            println!("Rustc codegen cranelift will JIT run the executable, because the SHOULD_RUN env var is set");
+
+            let f: extern "C" fn(c_int, *const *const c_char) -> c_int =
                 unsafe { ::std::mem::transmute(finalized_main) };
-            let res = f(0, 0 as *const _);
-            tcx.sess.warn(&format!("🚀 main returned {}", res));
+
+            let args = ::std::env::var("JIT_ARGS").unwrap_or_else(|_|String::new());
+            let args = args
+                .split(" ")
+                .chain(Some(&*tcx.crate_name(LOCAL_CRATE).as_str().to_string()))
+                .map(|arg| CString::new(arg).unwrap()).collect::<Vec<_>>();
+            let argv = args.iter().map(|arg| arg.as_ptr()).collect::<Vec<_>>();
+            // TODO: Rust doesn't care, but POSIX argv has a NULL sentinel at the end
+
+            let ret = f(args.len() as c_int, argv.as_ptr());
 
             jit_module.finish();
-            ::std::process::exit(0);
+            std::process::exit(ret);
         } else {
             let new_module = |name: String| {
                 let module: Module<FaerieBackend> = Module::new(
diff --git a/test.sh b/test.sh
index 8cc5b2d186ecc2e801d716b066fd0ce594ca52d6..0ea592778181a7888973b9396523e4a5086ebc0a 100755 (executable)
--- a/test.sh
+++ b/test.sh
@@ -11,11 +11,11 @@ echo "[BUILD] example"
 $RUSTC example/example.rs --crate-type lib
 
 echo "[JIT] mini_core_hello_world"
-SHOULD_RUN=1 $RUSTC --crate-type bin example/mini_core_hello_world.rs --cfg jit
+SHOULD_RUN=1 JIT_ARGS="abc bcd" $RUSTC --crate-type bin example/mini_core_hello_world.rs --cfg jit
 
 echo "[AOT] mini_core_hello_world"
 $RUSTC example/mini_core_hello_world.rs --crate-name mini_core_hello_world --crate-type bin
-sh -c ./target/out/mini_core_hello_world
+./target/out/mini_core_hello_world abc bcd
 
 echo "[BUILD] sysroot"
 time ./build_sysroot/build_sysroot.sh