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 abs #12

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions bril-rs/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ impl TryFrom<AbstractInstruction> for Instruction {
#[cfg(feature = "position")]
pos: pos.clone(),
op: match op.as_ref() {
"abs" => ValueOps::Abs,
"add" => ValueOps::Add,
"mul" => ValueOps::Mul,
"div" => ValueOps::Div,
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 @@ -393,6 +393,8 @@ impl Display for EffectOps {
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[serde(rename_all = "lowercase")]
pub enum ValueOps {
/// Absolute Values
Abs,
/// <https://capra.cs.cornell.edu/bril/lang/core.html#arithmetic>
Add,
/// <https://capra.cs.cornell.edu/bril/lang/core.html#arithmetic>
Expand Down Expand Up @@ -502,6 +504,7 @@ pub enum ValueOps {
impl Display for ValueOps {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::Abs => write!(f, "abs"),
Self::Add => write!(f, "add"),
Self::Sub => write!(f, "sub"),
Self::Mul => write!(f, "mul"),
Expand Down
6 changes: 6 additions & 0 deletions brilift/src/translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ impl CompileEnv<'_> {
/// Translate Bril opcodes that have CLIF equivalents.
fn translate_op(op: bril::ValueOps) -> ir::Opcode {
match op {
bril::ValueOps::Abs => ir::Opcode::Iabs,
bril::ValueOps::Add => ir::Opcode::Iadd,
bril::ValueOps::Sub => ir::Opcode::Isub,
bril::ValueOps::Mul => ir::Opcode::Imul,
Expand Down Expand Up @@ -473,6 +474,11 @@ impl CompileEnv<'_> {
op_type,
..
} => match op {
bril::ValueOps::Abs => {
let x = builder.use_var(self.vars[&args[0]]);
let res = builder.ins().iabs(x);
builder.def_var(self.vars[dest], res);
}
bril::ValueOps::Add
| bril::ValueOps::Sub
| bril::ValueOps::Mul
Expand Down
16 changes: 16 additions & 0 deletions brilirs/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ fn type_check_instruction<'a>(
}
update_env(env, dest, const_type)
}
Instruction::Value {
op: ValueOps::Abs,
dest,
op_type,
args,
funcs,
labels,
pos: _,
} => {
check_num_args(1, 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, op_type)?;
update_env(env, dest, op_type)
}
Instruction::Value {
op:
ValueOps::Add
Expand Down
8 changes: 6 additions & 2 deletions brilirs/src/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,15 @@ fn execute_value_op<T: std::io::Write>(
last_label: Option<&String>,
) -> 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, Fmax, Fmin, Fmul, Fsub, Ge, Gt, Id, Int2char, Le, Load, Lt, Mul, Not, Or, Phi,
Abs, 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, Shl, Shr, Smax, Smin, Sub,
};
match op {
Abs => {
let arg0 = get_arg::<i64>(&state.env, 0, args);
state.env.set(dest, Value::Int(arg0.abs()));
}
Add => {
let arg0 = get_arg::<i64>(&state.env, 0, args);
let arg1 = get_arg::<i64>(&state.env, 1, args);
Expand Down
Loading