Skip to content

Commit b36f3e8

Browse files
committed
Add option to pass arguments to JIT compiled program
1 parent 7d29c4f commit b36f3e8

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

src/main.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -604,15 +604,17 @@ fn driver() -> Result<(), Box<dyn std::error::Error>> {
604604
}
605605
},
606606
"jit" => {
607-
let mut in_file: Option<&str> = None;
608-
let mut linked: Vec<&str> = vec![];
607+
let mut in_file: Option<String> = None;
608+
let mut linked: Vec<String> = vec![];
609609
let mut link_dirs: Vec<String> = vec![];
610610
let mut continue_if_err = false;
611611
let mut no_default_link = false;
612-
let mut headers: Vec<&str> = vec![];
613-
let mut profile: Option<&str> = None;
612+
let mut headers: Vec<String> = vec![];
613+
let mut profile: Option<String> = None;
614+
let mut args = Vec::<String>::new();
614615
{
615-
let mut it = args.iter().skip(2).skip_while(|x| x.is_empty());
616+
let mut it = std::env::args().skip_while(|x| x.is_empty());
617+
args.push(it.by_ref().take(2).map(|x| format!("{x} ")).collect::<String>());
616618
while let Some(arg) = it.next() {
617619
if arg.is_empty() {continue;}
618620
if arg.as_bytes()[0] == b'-' {
@@ -621,10 +623,11 @@ fn driver() -> Result<(), Box<dyn std::error::Error>> {
621623
error!("respecification of input file");
622624
exit(1)
623625
}
624-
in_file = Some("-");
626+
in_file = Some("-".to_string());
625627
}
626628
else if arg.as_bytes()[1] == b'-' {
627629
match &arg[2..] {
630+
"" => args.extend(it.by_ref()),
628631
"continue" => {
629632
if continue_if_err {
630633
warning!("reuse of --continue flag");
@@ -651,7 +654,7 @@ fn driver() -> Result<(), Box<dyn std::error::Error>> {
651654
warning!("respecification of optimization profile");
652655
}
653656
if let Some(x) = it.next() {
654-
profile = Some(x.as_str());
657+
profile = Some(x);
655658
}
656659
else {
657660
error!("expected profile after -p flag");
@@ -666,7 +669,7 @@ fn driver() -> Result<(), Box<dyn std::error::Error>> {
666669
},
667670
'l' => {
668671
if let Some(x) = it.next() {
669-
linked.push(x.as_str());
672+
linked.push(x);
670673
}
671674
else {
672675
error!("expected library after -l flag");
@@ -675,7 +678,7 @@ fn driver() -> Result<(), Box<dyn std::error::Error>> {
675678
},
676679
'L' => {
677680
if let Some(x) = it.next() {
678-
link_dirs.push(x.clone());
681+
link_dirs.push(x);
679682
}
680683
else {
681684
error!("expected directory after -L flag");
@@ -684,7 +687,7 @@ fn driver() -> Result<(), Box<dyn std::error::Error>> {
684687
},
685688
'h' => {
686689
if let Some(x) = it.next() {
687-
headers.push(x.as_str());
690+
headers.push(x);
688691
}
689692
else {
690693
error!("expected header file after -h flag");
@@ -704,7 +707,7 @@ fn driver() -> Result<(), Box<dyn std::error::Error>> {
704707
error!("respecification of input file");
705708
exit(1)
706709
}
707-
in_file = Some(arg.as_str());
710+
in_file = Some(arg);
708711
}
709712
}
710713
}
@@ -713,11 +716,16 @@ fn driver() -> Result<(), Box<dyn std::error::Error>> {
713716
if let Ok(home) = std::env::var("HOME") {link_dirs.extend_from_slice(&[format!("{home}/.cobalt/packages"), format!("{home}/.local/lib/cobalt"), "/usr/local/lib/cobalt/packages".to_string(), "/usr/lib/cobalt/packages".to_string(), "/lib/cobalt/packages".to_string(), "/usr/local/lib".to_string(), "/usr/lib".to_string(), "/lib".to_string()]);}
714717
else {link_dirs.extend(["/usr/local/lib/cobalt/packages", "/usr/lib/cobalt/packages", "/lib/cobalt/packages", "/usr/local/lib", "/usr/lib", "/lib"].into_iter().map(String::from));}
715718
}
716-
let (in_file, code) = if let Some(f) = in_file {(f, std::fs::read_to_string(f)?)}
717-
else {
718-
let mut s = String::new();
719-
std::io::stdin().read_to_string(&mut s)?;
720-
("<stdin>", s)
719+
let (in_file, code) = match in_file.as_deref().unwrap_or("-") {
720+
"-" => {
721+
let mut s = String::new();
722+
std::io::stdin().read_to_string(&mut s)?;
723+
("<stdin>", s)
724+
},
725+
f => {
726+
args[0].push_str(f);
727+
(f, std::fs::read_to_string(f)?)
728+
}
721729
};
722730
let ink_ctx = inkwell::context::Context::create();
723731
let mut ctx = cobalt::context::CompCtx::new(&ink_ctx, in_file);
@@ -762,7 +770,7 @@ fn driver() -> Result<(), Box<dyn std::error::Error>> {
762770
}
763771
if fail || overall_fail {exit(101)}
764772
let pm = inkwell::passes::PassManager::create(());
765-
opt::load_profile(profile, &pm);
773+
opt::load_profile(profile.as_deref(), &pm);
766774
pm.run_on(&ctx.module);
767775
let ee = ctx.module.create_jit_execution_engine(inkwell::OptimizationLevel::None).expect("Couldn't create execution engine!");
768776
for (lib, _) in libs {
@@ -781,9 +789,8 @@ fn driver() -> Result<(), Box<dyn std::error::Error>> {
781789
exit(255)
782790
}
783791
};
784-
let this = format!("{} {in_file}", std::env::args().next().unwrap_or("<no exe?>".to_string()));
785792
ee.run_static_constructors();
786-
let ec = ee.run_function_as_main(main_fn, &[&this]);
793+
let ec = ee.run_function_as_main(main_fn, &args.iter().map(String::as_str).collect::<Vec<_>>());
787794
ee.run_static_destructors();
788795
exit(ec);
789796
}

0 commit comments

Comments
 (0)