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 11 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
56 changes: 56 additions & 0 deletions brilift/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
mod rt;
pub mod translator;

use crate::translator::{find_func, Translator};
use bril_rs::Program;
use cranelift_object::ObjectModule;
use std::str::FromStr;

pub enum OptLevel {
None,
Speed,
SpeedAndSize,
}

impl OptLevel {
pub fn to_str(self) -> &'static str {
match self {
OptLevel::None => "none",
OptLevel::Speed => "speed",
OptLevel::SpeedAndSize => "speed_and_size",
}
}
}

impl FromStr for OptLevel {
type Err = String;
fn from_str(s: &str) -> Result<OptLevel, String> {
match s {
"none" => Ok(OptLevel::None),
"speed" => Ok(OptLevel::Speed),
"speed_and_size" => Ok(OptLevel::SpeedAndSize),
_ => Err(format!("unknown optimization level {s}")),
}
}
}

pub struct CompileArgs<'a> {
pub program: &'a Program,
pub target: Option<String>,
pub output: &'a str,
pub opt_level: OptLevel,
pub dump_ir: bool,
}

pub fn compile(args: CompileArgs) {
// Compile.
let mut trans = Translator::<ObjectModule>::new(args.target, args.opt_level.to_str());
trans.compile_prog(args.program, args.dump_ir);

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

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