Skip to content

Commit b4eff78

Browse files
committed
Pass command-line arguments to JITed function
Cherry-picked from f1f35405e15ca1b77425514b04b96b2749231899 by @milkey-mouse
1 parent b7f2a72 commit b4eff78

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

example/mini_core.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ impl Mul for u8 {
104104
}
105105
}
106106

107+
impl Mul for usize {
108+
type Output = Self;
109+
110+
fn mul(self, rhs: Self) -> Self::Output {
111+
self * rhs
112+
}
113+
}
114+
107115
#[lang = "add"]
108116
pub trait Add<RHS = Self> {
109117
type Output;
@@ -208,6 +216,15 @@ impl PartialEq for usize {
208216
}
209217
}
210218

219+
impl PartialEq for isize {
220+
fn eq(&self, other: &isize) -> bool {
221+
(*self) == (*other)
222+
}
223+
fn ne(&self, other: &isize) -> bool {
224+
(*self) != (*other)
225+
}
226+
}
227+
211228
impl PartialEq for char {
212229
fn eq(&self, other: &char) -> bool {
213230
(*self) == (*other)

example/mini_core_hello_world.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,15 @@ enum Ordering {
7575
#[lang = "start"]
7676
fn start<T: Termination + 'static>(
7777
main: fn() -> T,
78-
_argc: isize,
79-
_argv: *const *const u8,
78+
argc: isize,
79+
argv: *const *const u8,
8080
) -> isize {
81+
if argc == 3 {
82+
unsafe { puts(*argv); }
83+
unsafe { puts(*((argv as usize + intrinsics::size_of::<*const u8>()) as *const *const u8)); }
84+
unsafe { puts(*((argv as usize + 2 * intrinsics::size_of::<*const u8>()) as *const *const u8)); }
85+
}
86+
8187
main().report();
8288
0
8389
}

src/lib.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ extern crate syntax;
1616
use std::any::Any;
1717
use std::fs::File;
1818
use std::sync::mpsc;
19+
use std::os::raw::{c_char, c_int};
20+
use std::ffi::CString;
1921

2022
use rustc::dep_graph::DepGraph;
2123
use rustc::middle::cstore::MetadataLoader;
@@ -241,19 +243,26 @@ impl CodegenBackend for CraneliftCodegenBackend {
241243
jit_module.finalize_definitions();
242244

243245
tcx.sess.abort_if_errors();
244-
println!("Compiled everything");
245-
println!("Rustc codegen cranelift will JIT run the executable, because the SHOULD_RUN env var is set");
246246

247247
let finalized_main: *const u8 = jit_module.get_finalized_function(main_func_id);
248-
println!("🎉 Finalized everything");
249248

250-
let f: extern "C" fn(isize, *const *const u8) -> isize =
249+
println!("Rustc codegen cranelift will JIT run the executable, because the SHOULD_RUN env var is set");
250+
251+
let f: extern "C" fn(c_int, *const *const c_char) -> c_int =
251252
unsafe { ::std::mem::transmute(finalized_main) };
252-
let res = f(0, 0 as *const _);
253-
tcx.sess.warn(&format!("🚀 main returned {}", res));
253+
254+
let args = ::std::env::var("JIT_ARGS").unwrap_or_else(|_|String::new());
255+
let args = args
256+
.split(" ")
257+
.chain(Some(&*tcx.crate_name(LOCAL_CRATE).as_str().to_string()))
258+
.map(|arg| CString::new(arg).unwrap()).collect::<Vec<_>>();
259+
let argv = args.iter().map(|arg| arg.as_ptr()).collect::<Vec<_>>();
260+
// TODO: Rust doesn't care, but POSIX argv has a NULL sentinel at the end
261+
262+
let ret = f(args.len() as c_int, argv.as_ptr());
254263

255264
jit_module.finish();
256-
::std::process::exit(0);
265+
std::process::exit(ret);
257266
} else {
258267
let new_module = |name: String| {
259268
let module: Module<FaerieBackend> = Module::new(

test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ echo "[BUILD] example"
1111
$RUSTC example/example.rs --crate-type lib
1212

1313
echo "[JIT] mini_core_hello_world"
14-
SHOULD_RUN=1 $RUSTC --crate-type bin example/mini_core_hello_world.rs --cfg jit
14+
SHOULD_RUN=1 JIT_ARGS="abc bcd" $RUSTC --crate-type bin example/mini_core_hello_world.rs --cfg jit
1515

1616
echo "[AOT] mini_core_hello_world"
1717
$RUSTC example/mini_core_hello_world.rs --crate-name mini_core_hello_world --crate-type bin
18-
sh -c ./target/out/mini_core_hello_world
18+
./target/out/mini_core_hello_world abc bcd
1919

2020
echo "[BUILD] sysroot"
2121
time ./build_sysroot/build_sysroot.sh

0 commit comments

Comments
 (0)