Skip to content

Commit

Permalink
Improve compile API, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Fischman committed Mar 16, 2024
1 parent e3faef8 commit c9ed95e
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 62 deletions.
49 changes: 43 additions & 6 deletions brilift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,53 @@ pub mod translator;
use crate::translator::{find_func, Translator};
use bril_rs::Program;
use cranelift_object::ObjectModule;
use std::str::FromStr;

pub fn compile(program: &Program, target: Option<String>, opt_level: &str, output: &str) {
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(target, opt_level);
trans.compile_prog(&program, false);
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(&program.functions, "main");
trans.add_c_main(&main.args, false);
let main = find_func(&args.program.functions, "main");
trans.add_c_main(&main.args, args.dump_ir);

// Write object file.
trans.emit(output);
trans.emit(args.output);
}
62 changes: 49 additions & 13 deletions brilift/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
use argh::FromArgs;
use bril_rs as bril;
use brilift::translator::{find_func, Args, Translator};
use brilift::translator::{find_func, Translator};
use brilift::{compile, CompileArgs, OptLevel};
use cranelift_jit::JITModule;
use cranelift_object::ObjectModule;

#[derive(FromArgs)]
#[argh(description = "Bril compiler")]
struct RunArgs {
#[argh(switch, short = 'j', description = "JIT and run (doesn't work)")]
jit: bool,

#[argh(option, short = 't', description = "target triple")]
target: Option<String>,

#[argh(
option,
short = 'o',
description = "output object file",
default = "String::from(\"bril.o\")"
)]
output: String,

#[argh(switch, short = 'd', description = "dump CLIF IR")]
dump_ir: bool,

#[argh(switch, short = 'v', description = "verbose logging")]
verbose: bool,

#[argh(
option,
short = 'O',
description = "optimization level (none, speed, or speed_and_size)",
default = "String::from(\"none\")"
)]
opt_level: String,

#[argh(
positional,
description = "arguments for @main function (JIT mode only)"
)]
args: Vec<String>,
}

fn main() {
let args: Args = argh::from_env();
let args: RunArgs = argh::from_env();

// Set up logging.
simplelog::TermLogger::init(
Expand Down Expand Up @@ -55,15 +94,12 @@ fn main() {
// Invoke the main function.
unsafe { trans.run(entry_id, &main_args) };
} else {
// Compile.
let mut trans = Translator::<ObjectModule>::new(args.target, &args.opt_level);
trans.compile_prog(&prog, args.dump_ir);

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

// Write object file.
trans.emit(&args.output);
compile(CompileArgs {
program: &prog,
target: args.target.clone(),
output: &args.output,
opt_level: args.opt_level.parse::<OptLevel>().unwrap(),
dump_ir: args.dump_ir,
});
}
}
55 changes: 12 additions & 43 deletions brilift/src/translator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::rt;
use argh::FromArgs;
use bril_rs as bril;
use core::mem;
use cranelift_codegen::entity::EntityRef;
Expand Down Expand Up @@ -159,7 +158,7 @@ impl RTSetupFunc {
],
returns: vec![ir::AbiParam::new(ir::types::R32)],
call_conv,
}
},
}
}

Expand Down Expand Up @@ -476,8 +475,10 @@ impl CompileEnv<'_> {
bril::EffectOps::Free => {
let ptr_arg = builder.use_var(self.vars[&args[0]]);
builder.ins().call(self.rt_refs[RTFunc::Free], &[ptr_arg]);
},
bril::EffectOps::Speculate | bril::EffectOps::Commit | bril::EffectOps::Guard => unimplemented!(),
}
bril::EffectOps::Speculate | bril::EffectOps::Commit | bril::EffectOps::Guard => {
unimplemented!()
}
},
bril::Instruction::Value {
args,
Expand Down Expand Up @@ -574,7 +575,13 @@ impl CompileEnv<'_> {
builder.def_var(self.vars[dest], res);
}
bril::ValueOps::Phi => unimplemented!(),
bril::ValueOps::Ceq | bril::ValueOps::Clt | bril::ValueOps::Cgt | bril::ValueOps::Cle | bril::ValueOps::Cge | bril::ValueOps::Char2int | bril::ValueOps::Int2char => todo!(),
bril::ValueOps::Ceq
| bril::ValueOps::Clt
| bril::ValueOps::Cgt
| bril::ValueOps::Cle
| bril::ValueOps::Cge
| bril::ValueOps::Char2int
| bril::ValueOps::Int2char => todo!(),
},
}
}
Expand Down Expand Up @@ -1061,44 +1068,6 @@ impl Translator<JITModule> {
}
}

#[derive(FromArgs)]
#[argh(description = "Bril compiler")]
pub struct Args {
#[argh(switch, short = 'j', description = "JIT and run (doesn't work)")]
pub jit: bool,

#[argh(option, short = 't', description = "target triple")]
pub target: Option<String>,

#[argh(
option,
short = 'o',
description = "output object file",
default = "String::from(\"bril.o\")"
)]
pub output: String,

#[argh(switch, short = 'd', description = "dump CLIF IR")]
pub dump_ir: bool,

#[argh(switch, short = 'v', description = "verbose logging")]
pub verbose: bool,

#[argh(
option,
short = 'O',
description = "optimization level (none, speed, or speed_and_size)",
default = "String::from(\"none\")"
)]
pub opt_level: String,

#[argh(
positional,
description = "arguments for @main function (JIT mode only)"
)]
pub args: Vec<String>,
}

pub fn find_func<'a>(funcs: &'a [bril::Function], name: &str) -> &'a bril::Function {
funcs.iter().find(|f| f.name == name).unwrap()
}

0 comments on commit c9ed95e

Please sign in to comment.