diff --git a/example/mini_core.rs b/example/mini_core.rs index 66314ab3e5d..b7277f5ee9f 100644 --- a/example/mini_core.rs +++ b/example/mini_core.rs @@ -104,6 +104,14 @@ impl Mul for u8 { } } +impl Mul for usize { + type Output = Self; + + fn mul(self, rhs: Self) -> Self::Output { + self * rhs + } +} + #[lang = "add"] pub trait Add { type Output; @@ -208,6 +216,15 @@ impl PartialEq for usize { } } +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) diff --git a/example/mini_core_hello_world.rs b/example/mini_core_hello_world.rs index 871aebddee9..5ab7575252b 100644 --- a/example/mini_core_hello_world.rs +++ b/example/mini_core_hello_world.rs @@ -75,9 +75,15 @@ enum Ordering { #[lang = "start"] fn start( 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 } diff --git a/src/lib.rs b/src/lib.rs index 5863f730bca..ead24921652 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,8 @@ extern crate syntax; 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 @@ impl CodegenBackend for CraneliftCodegenBackend { 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::>(); + let argv = args.iter().map(|arg| arg.as_ptr()).collect::>(); + // 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 = Module::new( diff --git a/test.sh b/test.sh index 8cc5b2d186e..0ea59277818 100755 --- 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