Pass command-line arguments to JITed function

Cherry-picked from f1f35405e15ca1b77425514b04b96b2749231899 by
@milkey-mouse
This commit is contained in:
bjorn3 2019-02-18 18:53:18 +01:00
parent b7f2a722c9
commit b4eff78a9e
4 changed files with 43 additions and 11 deletions

View File

@ -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<RHS = Self> {
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)

View 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
}

View File

@ -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::<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(

View File

@ -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