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

Add Select #1

Merged
merged 6 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
18 changes: 9 additions & 9 deletions bril-rs/rs2bril/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl State {
}

fn starting_new_function(&mut self, name: &String) {
self.ident_type_map = self.func_context_map.get(name).unwrap().0.clone();
self.ident_type_map.clone_from(&self.func_context_map.get(name).unwrap().0);
}

fn add_type_for_ident(&mut self, ident: String, ty: Type) {
Expand Down Expand Up @@ -570,35 +570,35 @@ fn from_expr_to_bril(expr: Expr, state: &mut State) -> (Option<String>, Vec<Code
// So we need to set a specific destination
// https://doc.rust-lang.org/reference/expressions.html#place-expressions-and-value-expressions
(BinOp::AddAssign(_), Type::Int) => {
place_expression = arg1.clone();
place_expression.clone_from(&arg1);
(ValueOps::Add, Type::Int)
}
(BinOp::AddAssign(_), Type::Float) => {
place_expression = arg1.clone();
place_expression.clone_from(&arg1);
(ValueOps::Fadd, Type::Float)
}
(BinOp::SubAssign(_), Type::Int) => {
place_expression = arg1.clone();
place_expression.clone_from(&arg1);
(ValueOps::Sub, Type::Int)
}
(BinOp::SubAssign(_), Type::Float) => {
place_expression = arg1.clone();
place_expression.clone_from(&arg1);
(ValueOps::Fsub, Type::Float)
}
(BinOp::MulAssign(_), Type::Int) => {
place_expression = arg1.clone();
place_expression.clone_from(&arg1);
(ValueOps::Mul, Type::Int)
}
(BinOp::MulAssign(_), Type::Float) => {
place_expression = arg1.clone();
place_expression.clone_from(&arg1);
(ValueOps::Fmul, Type::Float)
}
(BinOp::DivAssign(_), Type::Int) => {
place_expression = arg1.clone();
place_expression.clone_from(&arg1);
(ValueOps::Div, Type::Int)
}
(BinOp::DivAssign(_), Type::Float) => {
place_expression = arg1.clone();
place_expression.clone_from(&arg1);
(ValueOps::Fdiv, Type::Float)
}
(_, _) => unimplemented!("{op:?}"),
Expand Down
1 change: 1 addition & 0 deletions bril-rs/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ impl TryFrom<AbstractInstruction> for Instruction {
"or" => ValueOps::Or,
"call" => ValueOps::Call,
"id" => ValueOps::Id,
"select" => ValueOps::Select,
"sub" => ValueOps::Sub,
#[cfg(feature = "ssa")]
"phi" => ValueOps::Phi,
Expand Down
3 changes: 3 additions & 0 deletions bril-rs/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@ pub enum ValueOps {
Call,
/// <https://capra.cs.cornell.edu/bril/lang/core.html#miscellaneous>
Id,
/// Select
Select,
/// <https://capra.cs.cornell.edu/bril/lang/ssa.html#operations>
#[cfg(feature = "ssa")]
Phi,
Expand Down Expand Up @@ -500,6 +502,7 @@ impl Display for ValueOps {
Self::Or => write!(f, "or"),
Self::Call => write!(f, "call"),
Self::Id => write!(f, "id"),
Self::Select => write!(f, "select"),
#[cfg(feature = "ssa")]
Self::Phi => write!(f, "phi"),
#[cfg(feature = "float")]
Expand Down
7 changes: 7 additions & 0 deletions brilift/src/translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,13 @@ impl CompileEnv<'_> {
| bril::ValueOps::Or => {
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);
}
bril::ValueOps::Lt
| bril::ValueOps::Le
| bril::ValueOps::Eq
Expand Down
17 changes: 17 additions & 0 deletions brilirs/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,23 @@ fn type_check_instruction<'a>(
check_asmt_type(op_type, get_type(env, 0, args)?)?;
update_env(env, dest, op_type)
}
Instruction::Value {
op: ValueOps::Select,
dest,
op_type,
args,
funcs,
labels,
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)?)?;
update_env(env, dest, op_type)
}
Instruction::Value {
op: ValueOps::Fadd | ValueOps::Fsub | ValueOps::Fmul | ValueOps::Fdiv,
dest,
Expand Down
9 changes: 8 additions & 1 deletion brilirs/src/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ 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,
Fle, Flt, Fmul, Fsub, Ge, Gt, Id, Int2char, Le, Load, Lt, Mul, Not, Or, Phi, PtrAdd, Sub, Select
};
match op {
Add => {
Expand Down Expand Up @@ -394,6 +394,13 @@ fn execute_value_op<T: std::io::Write>(
let src = get_arg::<Value>(&state.env, 0, args);
state.env.set(dest, src);
}
Select => {
let arg0 = get_arg::<bool>(&state.env, 0, args);
let arg1 = get_arg::<Value>(&state.env, 1, args);
let arg2 = get_arg::<Value>(&state.env, 2, args);
let res = if arg0 { arg1 } else { arg2 };
state.env.set(dest, res);
}
Fadd => {
let arg0 = get_arg::<f64>(&state.env, 0, args);
let arg1 = get_arg::<f64>(&state.env, 1, args);
Expand Down
Loading