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

Adding Bitand and Neg to rs2bril and brillvm #16

Merged
merged 2 commits into from
Jan 24, 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
52 changes: 52 additions & 0 deletions bril-rs/brillvm/src/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,34 @@ fn build_instruction<'a, 'b>(
.build_store(heap.get(dest).ptr, context.f64_type().const_float(*f))
.unwrap();
}
Instruction::Value {
args,
dest,
funcs: _,
labels: _,
op: ValueOps::Bitand,
op_type: _,
} => {
let ret_name = fresh.fresh_var();
build_op(
context,
builder,
heap,
fresh,
|v| {
builder
.build_and::<IntValue>(
v[0].try_into().unwrap(),
v[1].try_into().unwrap(),
&ret_name,
)
.unwrap()
.into()
},
args,
dest,
);
}
Instruction::Value {
args,
dest,
Expand Down Expand Up @@ -550,6 +578,30 @@ fn build_instruction<'a, 'b>(
dest,
);
}
Instruction::Value {
args,
dest,
funcs: _,
labels: _,
op: ValueOps::Neg,
op_type: _,
} => {
let ret_name = fresh.fresh_var();
build_op(
context,
builder,
heap,
fresh,
|v| {
builder
.build_int_neg::<IntValue>(v[0].try_into().unwrap(), &ret_name)
.unwrap()
.into()
},
args,
dest,
);
}
Instruction::Value {
args,
dest,
Expand Down
13 changes: 3 additions & 10 deletions bril-rs/rs2bril/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ fn from_expr_to_bril(expr: Expr, state: &mut State) -> (Option<String>, Vec<Code
let mut place_expression = None;

let (value_op, op_type) = match (op, state.get_type_for_ident(arg1.as_ref().unwrap())) {
(BinOp::BitAnd(_), Type::Int) => (ValueOps::Bitand, Type::Int),
(BinOp::Add(_), Type::Int) => (ValueOps::Add, Type::Int),
(BinOp::Add(_), Type::Float) => (ValueOps::Fadd, Type::Float),
(BinOp::Sub(_), Type::Int) => (ValueOps::Sub, Type::Int),
Expand Down Expand Up @@ -1157,16 +1158,8 @@ fn from_expr_to_bril(expr: Expr, state: &mut State) -> (Option<String>, Vec<Code
(
match ty {
Type::Int => {
let tmp = state.fresh_var(ty.clone());
code.push(Code::Instruction(Instruction::Constant {
op: ConstOps::Const,
dest: tmp.clone(),
const_type: ty.clone(),
value: Literal::Int(-1),
pos: None,
}));
args.push(tmp);
ValueOps::Mul
//Only integer negation for now
ValueOps::Neg
}
Type::Float => {
let tmp = state.fresh_var(ty.clone());
Expand Down
2 changes: 2 additions & 0 deletions test/rs/bitop_tests.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
3
-3
8 changes: 8 additions & 0 deletions test/rs/bitop_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//# Testing Bitand and (integer) Neg
//# ARGS: 7 19
fn main(n:i64, m:i64) {
let res : i64 = n & m;
println!("{:?}", res);
res = -res;
println!("{:?}", res);
}
1 change: 1 addition & 0 deletions test/rs/lowbit.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4
6 changes: 6 additions & 0 deletions test/rs/lowbit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//# Compute the lowest 1 bit of an integer
//# ARGS: 21324
fn main(n:i64) {
let lb : i64 = n & (-n);
println!("{:?}", lb);
}
1 change: 1 addition & 0 deletions test/rs/lowbit_naive.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4
10 changes: 10 additions & 0 deletions test/rs/lowbit_naive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//# Compute the lowest 1 bit of an integer in the naive way
//# ARGS: 21324
fn main(n:i64) {
let lb : i64 = 1;
while (n == n / 2 * 2) {
n = n / 2;
lb = lb * 2;
}
println!("{:?}", lb);
}
Loading