Skip to content

Commit

Permalink
shl and shr
Browse files Browse the repository at this point in the history
  • Loading branch information
ajpal committed May 27, 2024
1 parent b5e3c59 commit 40ec0e5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 38 deletions.
2 changes: 2 additions & 0 deletions bril-rs/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ impl TryFrom<AbstractInstruction> for Instruction {
"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 Down
6 changes: 6 additions & 0 deletions bril-rs/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,10 @@ pub enum ValueOps {
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 @@ -515,6 +519,8 @@ impl Display for ValueOps {
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 Down
6 changes: 5 additions & 1 deletion 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,7 +478,9 @@ 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 => {
Expand Down
52 changes: 16 additions & 36 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 @@ -194,24 +202,13 @@ fn type_check_instruction<'a>(
update_env(env, dest, op_type)
}
Instruction::Value {
op: ValueOps::Smax | ValueOps::Smin,
dest,
op_type,
args,
funcs,
labels,
pos: _,
} => {
check_num_args(2, args)?;
check_num_funcs(0, funcs)?;
check_num_labels(0, labels)?;
check_asmt_type(&Type::Int, get_type(env, 0, args)?)?;
check_asmt_type(&Type::Int, get_type(env, 1, args)?)?;
check_asmt_type(&Type::Int, op_type)?;
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 Expand Up @@ -244,23 +241,6 @@ fn type_check_instruction<'a>(
check_asmt_type(&Type::Bool, op_type)?;
update_env(env, dest, op_type)
}
Instruction::Value {
op: ValueOps::Fmax | ValueOps::Fmin,
dest,
op_type,
args,
funcs,
labels,
pos: _,
} => {
check_num_args(2, args)?;
check_num_funcs(0, funcs)?;
check_num_labels(0, labels)?;
check_asmt_type(&Type::Float, get_type(env, 0, args)?)?;
check_asmt_type(&Type::Float, get_type(env, 1, args)?)?;
check_asmt_type(&Type::Float, op_type)?;
update_env(env, dest, op_type)
}
Instruction::Value {
op: ValueOps::Ceq | ValueOps::Cge | ValueOps::Clt | ValueOps::Cgt | ValueOps::Cle,
args,
Expand Down
14 changes: 13 additions & 1 deletion brilirs/src/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ fn execute_value_op<T: std::io::Write>(
use bril_rs::ValueOps::{
Add, Alloc, And, Call, Ceq, Cge, Cgt, Char2int, Cle, Clt, Div, Eq, Fadd, Fdiv, Feq, Fge, Fgt,
Fle, Flt, Fmax, Fmin, Fmul, Fsub, Ge, Gt, Id, Int2char, Le, Load, Lt, Mul, Not, Or, Phi,
PtrAdd, Select, Smax, Smin, Sub,
PtrAdd, Select, Shl, Shr, Smax, Smin, Sub,
};
match op {
Add => {
Expand Down Expand Up @@ -414,6 +414,18 @@ fn execute_value_op<T: std::io::Write>(
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

0 comments on commit 40ec0e5

Please sign in to comment.