Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library-izing brilift #312

Merged
merged 19 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion brilift/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
bril-rs = { path = "../bril-rs", features = ["float", "memory"] }
bril-rs = { path = "../bril-rs", features = ["memory", "float", "ssa", "speculate", "position", "import", "char"] }
cranelift-codegen = { version = "0.103.0", features = ["all-arch"] }
cranelift-frontend = "0.103.0"
cranelift-object = "0.103.0"
Expand Down
73 changes: 73 additions & 0 deletions brilift/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
mod rt;
pub mod translator;

use crate::translator::{find_func, Translator};
use bril_rs as bril;
use bril_rs::Program;
use cranelift_jit::JITModule;
use cranelift_object::ObjectModule;

/// Compile a program ahead of time to an object file.
///
/// * `program` - the Bril program to compile
/// * `target` - the target triple, or None to target the host
/// * `output` - the filename where we should write the object file
/// * `opt_level` - a Cranelift optimization level
/// * `dump_ir` - optionally emit the Cranelift IR to stdout
pub fn compile(
program: &Program,
target: Option<String>,
output: &str,
opt_level: &str,
dump_ir: bool,
) {
// Compile.
let mut trans = Translator::<ObjectModule>::new(target, opt_level);
trans.compile_prog(program, dump_ir);

// Add a C-style `main` wrapper.
let main = find_func(&program.functions, "main");
trans.add_c_main(&main.args, dump_ir);

// Write object file.
trans.emit(output);
}

/// Just-in-time compile and execute a Bril program.
///
/// * `program` - the Bril program to compile
/// * `args` - the arguments to pass to the `@main` function
/// * `dump_ir` - optionally emit the Cranelift IR to stdout
pub fn jit_run(program: &Program, args: Vec<String>, dump_ir: bool) {
// Compile.
let mut trans = Translator::<JITModule>::new();
trans.compile_prog(program, dump_ir);

// Add a JIT wrapper for `main`.
let main = find_func(&program.functions, "main");
let entry_id = trans.add_mem_wrapper("main", &main.args, dump_ir);

// Parse CLI arguments.
if main.args.len() != args.len() {
panic!(
"@main expects {} arguments; got {}",
main.args.len(),
args.len()
);
}
let main_args: Vec<bril::Literal> = main
.args
.iter()
.zip(args)
.map(|(arg, val_str)| match arg.arg_type {
bril::Type::Int => bril::Literal::Int(val_str.parse().unwrap()),
bril::Type::Bool => bril::Literal::Bool(val_str == "true"),
bril::Type::Float => bril::Literal::Float(val_str.parse().unwrap()),
bril::Type::Char => bril::Literal::Char(val_str.parse().unwrap()),
bril::Type::Pointer(_) => unimplemented!("pointers not supported as main args"),
})
.collect();

// Invoke the main function.
unsafe { trans.run(entry_id, &main_args) };
}
Loading
Loading