Skip to content

Commit

Permalink
Merge pull request #6 from uwplse/ajpal-ops2
Browse files Browse the repository at this point in the history
Add more ops to bril
  • Loading branch information
ajpal authored May 29, 2024
2 parents f461e22 + 40ec0e5 commit bc0061e
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 9 deletions.
8 changes: 8 additions & 0 deletions bril-rs/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,11 @@ impl TryFrom<AbstractInstruction> for Instruction {
"call" => ValueOps::Call,
"id" => ValueOps::Id,
"select" => ValueOps::Select,
"smax" => ValueOps::Smax,
"smin" => ValueOps::Smin,
"sub" => ValueOps::Sub,
"shl" => ValueOps::Shl,
"shr" => ValueOps::Shr,
#[cfg(feature = "ssa")]
"phi" => ValueOps::Phi,
#[cfg(feature = "float")]
Expand All @@ -246,6 +250,10 @@ impl TryFrom<AbstractInstruction> for Instruction {
"fle" => ValueOps::Fle,
#[cfg(feature = "float")]
"fge" => ValueOps::Fge,
#[cfg(feature = "float")]
"fmax" => ValueOps::Fmax,
#[cfg(feature = "float")]
"fmin" => ValueOps::Fmin,
#[cfg(feature = "char")]
"ceq" => ValueOps::Ceq,
#[cfg(feature = "char")]
Expand Down
22 changes: 22 additions & 0 deletions bril-rs/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,14 @@ pub enum ValueOps {
Id,
/// Select
Select,
/// Signed max
Smax,
/// Signed min
Smin,
/// Shift left
Shl,
/// Shift right
Shr,
/// <https://capra.cs.cornell.edu/bril/lang/ssa.html#operations>
#[cfg(feature = "ssa")]
Phi,
Expand Down Expand Up @@ -453,6 +461,12 @@ pub enum ValueOps {
/// <https://capra.cs.cornell.edu/bril/lang/float.html#operations>
#[cfg(feature = "float")]
Fge,
/// Float max
#[cfg(feature = "float")]
Fmax,
/// Float min
#[cfg(feature = "float")]
Fmin,
/// <https://capra.cs.cornell.edu/bril/lang/char.html#operations>
#[cfg(feature = "char")]
Ceq,
Expand Down Expand Up @@ -503,6 +517,10 @@ impl Display for ValueOps {
Self::Call => write!(f, "call"),
Self::Id => write!(f, "id"),
Self::Select => write!(f, "select"),
Self::Smax => write!(f, "smax"),
Self::Smin => write!(f, "smin"),
Self::Shl => write!(f, "shl"),
Self::Shr => write!(f, "shr"),
#[cfg(feature = "ssa")]
Self::Phi => write!(f, "phi"),
#[cfg(feature = "float")]
Expand All @@ -523,6 +541,10 @@ impl Display for ValueOps {
Self::Fle => write!(f, "fle"),
#[cfg(feature = "float")]
Self::Fge => write!(f, "fge"),
#[cfg(feature = "float")]
Self::Fmax => write!(f, "fmax"),
#[cfg(feature = "float")]
Self::Fmin => write!(f, "fmin"),
#[cfg(feature = "char")]
Self::Ceq => write!(f, "ceq"),
#[cfg(feature = "char")]
Expand Down
38 changes: 36 additions & 2 deletions brilift/src/translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ impl CompileEnv<'_> {
bril::ValueOps::Fsub => ir::Opcode::Fsub,
bril::ValueOps::Fmul => ir::Opcode::Fmul,
bril::ValueOps::Fdiv => ir::Opcode::Fdiv,
bril::ValueOps::Shl => ir::Opcode::Ishl,
bril::ValueOps::Shr => ir::Opcode::Ushr,
_ => panic!("not a translatable opcode: {op}"),
}
}
Expand Down Expand Up @@ -476,15 +478,31 @@ impl CompileEnv<'_> {
| bril::ValueOps::Mul
| bril::ValueOps::Div
| bril::ValueOps::And
| bril::ValueOps::Or => {
| bril::ValueOps::Or
| bril::ValueOps::Shl
| bril::ValueOps::Shr => {
self.gen_binary(builder, args, dest, op_type, Self::translate_op(*op));
}
bril::ValueOps::Select => {
let cond = builder.use_var(self.vars[&args[0]]);
let thn = builder.use_var(self.vars[&args[1]]);
let els = builder.use_var(self.vars[&args[2]]);
let res = builder.ins().select(cond, thn, els);
builder.def_var(self.vars[dest], res);
builder.def_var(self.vars[dest], res);
}
bril::ValueOps::Smax => {
let a = builder.use_var(self.vars[&args[0]]);
let b = builder.use_var(self.vars[&args[1]]);
let cmp = builder.ins().icmp(IntCC::SignedGreaterThan, a, b);
let res = builder.ins().select(cmp, a, b);
builder.def_var(self.vars[dest], res);
}
bril::ValueOps::Smin => {
let a = builder.use_var(self.vars[&args[0]]);
let b = builder.use_var(self.vars[&args[1]]);
let cmp = builder.ins().icmp(IntCC::SignedLessThan, a, b);
let res = builder.ins().select(cmp, a, b);
builder.def_var(self.vars[dest], res);
}
bril::ValueOps::Lt
| bril::ValueOps::Le
Expand Down Expand Up @@ -532,6 +550,22 @@ impl CompileEnv<'_> {
self.gen_fcmp(builder, args, dest, Self::translate_floatcc(*op))
}

bril::ValueOps::Fmax => {
let a = builder.use_var(self.vars[&args[0]]);
let b = builder.use_var(self.vars[&args[1]]);
let cmp = builder.ins().fcmp(FloatCC::GreaterThan, a, b);
let res = builder.ins().select(cmp, a, b);
builder.def_var(self.vars[dest], res);
}

bril::ValueOps::Fmin => {
let a = builder.use_var(self.vars[&args[0]]);
let b = builder.use_var(self.vars[&args[1]]);
let cmp = builder.ins().fcmp(FloatCC::LessThan, a, b);
let res = builder.ins().select(cmp, a, b);
builder.def_var(self.vars[dest], res);
}

// Memory extension.
bril::ValueOps::Alloc => {
// The number of elements to allocate comes from the program.
Expand Down
2 changes: 1 addition & 1 deletion brilirs/src/basic_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl BBProgram {
.collect::<Result<Vec<BBFunction>, InterpError>>()?;

let bb = Self {
index_of_main: func_map.get(&"main".to_string()).copied(),
index_of_main: func_map.get("main").copied(),
func_index,
};
if func_map.len() == num_funcs {
Expand Down
24 changes: 19 additions & 5 deletions brilirs/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,15 @@ fn type_check_instruction<'a>(
update_env(env, dest, const_type)
}
Instruction::Value {
op: ValueOps::Add | ValueOps::Sub | ValueOps::Mul | ValueOps::Div,
op:
ValueOps::Add
| ValueOps::Sub
| ValueOps::Mul
| ValueOps::Div
| ValueOps::Smax
| ValueOps::Smin
| ValueOps::Shl
| ValueOps::Shr,
dest,
op_type,
args,
Expand Down Expand Up @@ -183,18 +191,24 @@ fn type_check_instruction<'a>(
args,
funcs,
labels,
pos: _
pos: _,
} => {
check_num_args(3, args)?;
check_num_funcs(0, funcs)?;
check_num_labels(0, labels)?;
check_asmt_type(&Type::Bool, get_type(env, 0, args)?)?;
check_asmt_type(&op_type, get_type(env, 1, args)?)?;
check_asmt_type(&op_type, get_type(env, 2, args)?)?;
check_asmt_type(op_type, get_type(env, 1, args)?)?;
check_asmt_type(op_type, get_type(env, 2, args)?)?;
update_env(env, dest, op_type)
}
Instruction::Value {
op: ValueOps::Fadd | ValueOps::Fsub | ValueOps::Fmul | ValueOps::Fdiv,
op:
ValueOps::Fadd
| ValueOps::Fsub
| ValueOps::Fmul
| ValueOps::Fdiv
| ValueOps::Fmax
| ValueOps::Fmin,
dest,
op_type,
args,
Expand Down
39 changes: 38 additions & 1 deletion brilirs/src/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ fn execute_value_op<T: std::io::Write>(
) -> Result<(), InterpError> {
use bril_rs::ValueOps::{
Add, Alloc, And, Call, Ceq, Cge, Cgt, Char2int, Cle, Clt, Div, Eq, Fadd, Fdiv, Feq, Fge, Fgt,
Fle, Flt, Fmul, Fsub, Ge, Gt, Id, Int2char, Le, Load, Lt, Mul, Not, Or, Phi, PtrAdd, Sub, Select
Fle, Flt, Fmax, Fmin, Fmul, Fsub, Ge, Gt, Id, Int2char, Le, Load, Lt, Mul, Not, Or, Phi,
PtrAdd, Select, Shl, Shr, Smax, Smin, Sub,
};
match op {
Add => {
Expand Down Expand Up @@ -401,6 +402,30 @@ fn execute_value_op<T: std::io::Write>(
let res = if arg0 { arg1 } else { arg2 };
state.env.set(dest, res);
}
Smax => {
let arg0 = get_arg::<i64>(&state.env, 0, args);
let arg1 = get_arg::<i64>(&state.env, 1, args);
let res = if arg0 > arg1 { arg0 } else { arg1 };
state.env.set(dest, Value::Int(res));
}
Smin => {
let arg0 = get_arg::<i64>(&state.env, 0, args);
let arg1 = get_arg::<i64>(&state.env, 1, args);
let res = if arg0 < arg1 { arg0 } else { arg1 };
state.env.set(dest, Value::Int(res));
}
Shl => {
let arg0 = get_arg::<i64>(&state.env, 0, args);
let arg1 = get_arg::<i64>(&state.env, 1, args);
let res = arg0 << arg1;
state.env.set(dest, Value::Int(res));
}
Shr => {
let arg0 = get_arg::<i64>(&state.env, 0, args);
let arg1 = get_arg::<i64>(&state.env, 1, args);
let res = arg0 >> arg1;
state.env.set(dest, Value::Int(res));
}
Fadd => {
let arg0 = get_arg::<f64>(&state.env, 0, args);
let arg1 = get_arg::<f64>(&state.env, 1, args);
Expand Down Expand Up @@ -446,6 +471,18 @@ fn execute_value_op<T: std::io::Write>(
let arg1 = get_arg::<f64>(&state.env, 1, args);
state.env.set(dest, Value::Bool(arg0 >= arg1));
}
Fmax => {
let arg0 = get_arg::<f64>(&state.env, 0, args);
let arg1 = get_arg::<f64>(&state.env, 1, args);
let res = if arg0 > arg1 { arg0 } else { arg1 };
state.env.set(dest, Value::Float(res));
}
Fmin => {
let arg0 = get_arg::<f64>(&state.env, 0, args);
let arg1 = get_arg::<f64>(&state.env, 1, args);
let res = if arg0 < arg1 { arg0 } else { arg1 };
state.env.set(dest, Value::Float(res));
}
Ceq => {
let arg0 = get_arg::<char>(&state.env, 0, args);
let arg1 = get_arg::<char>(&state.env, 1, args);
Expand Down

0 comments on commit bc0061e

Please sign in to comment.