Skip to content

Commit 06117c0

Browse files
authored
Merge pull request #16 from FTRobbin/haobin_bitops
Adding `Bitand` and `Neg` to `rs2bril` and `brillvm`
2 parents e117fbe + f132020 commit 06117c0

File tree

8 files changed

+83
-10
lines changed

8 files changed

+83
-10
lines changed

bril-rs/brillvm/src/llvm.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,34 @@ fn build_instruction<'a, 'b>(
293293
.build_store(heap.get(dest).ptr, context.f64_type().const_float(*f))
294294
.unwrap();
295295
}
296+
Instruction::Value {
297+
args,
298+
dest,
299+
funcs: _,
300+
labels: _,
301+
op: ValueOps::Bitand,
302+
op_type: _,
303+
} => {
304+
let ret_name = fresh.fresh_var();
305+
build_op(
306+
context,
307+
builder,
308+
heap,
309+
fresh,
310+
|v| {
311+
builder
312+
.build_and::<IntValue>(
313+
v[0].try_into().unwrap(),
314+
v[1].try_into().unwrap(),
315+
&ret_name,
316+
)
317+
.unwrap()
318+
.into()
319+
},
320+
args,
321+
dest,
322+
);
323+
}
296324
Instruction::Value {
297325
args,
298326
dest,
@@ -550,6 +578,30 @@ fn build_instruction<'a, 'b>(
550578
dest,
551579
);
552580
}
581+
Instruction::Value {
582+
args,
583+
dest,
584+
funcs: _,
585+
labels: _,
586+
op: ValueOps::Neg,
587+
op_type: _,
588+
} => {
589+
let ret_name = fresh.fresh_var();
590+
build_op(
591+
context,
592+
builder,
593+
heap,
594+
fresh,
595+
|v| {
596+
builder
597+
.build_int_neg::<IntValue>(v[0].try_into().unwrap(), &ret_name)
598+
.unwrap()
599+
.into()
600+
},
601+
args,
602+
dest,
603+
);
604+
}
553605
Instruction::Value {
554606
args,
555607
dest,

bril-rs/rs2bril/src/lib.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,7 @@ fn from_expr_to_bril(expr: Expr, state: &mut State) -> (Option<String>, Vec<Code
686686
let mut place_expression = None;
687687

688688
let (value_op, op_type) = match (op, state.get_type_for_ident(arg1.as_ref().unwrap())) {
689+
(BinOp::BitAnd(_), Type::Int) => (ValueOps::Bitand, Type::Int),
689690
(BinOp::Add(_), Type::Int) => (ValueOps::Add, Type::Int),
690691
(BinOp::Add(_), Type::Float) => (ValueOps::Fadd, Type::Float),
691692
(BinOp::Sub(_), Type::Int) => (ValueOps::Sub, Type::Int),
@@ -1157,16 +1158,8 @@ fn from_expr_to_bril(expr: Expr, state: &mut State) -> (Option<String>, Vec<Code
11571158
(
11581159
match ty {
11591160
Type::Int => {
1160-
let tmp = state.fresh_var(ty.clone());
1161-
code.push(Code::Instruction(Instruction::Constant {
1162-
op: ConstOps::Const,
1163-
dest: tmp.clone(),
1164-
const_type: ty.clone(),
1165-
value: Literal::Int(-1),
1166-
pos: None,
1167-
}));
1168-
args.push(tmp);
1169-
ValueOps::Mul
1161+
//Only integer negation for now
1162+
ValueOps::Neg
11701163
}
11711164
Type::Float => {
11721165
let tmp = state.fresh_var(ty.clone());

test/rs/bitop_tests.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
3
2+
-3

test/rs/bitop_tests.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//# Testing Bitand and (integer) Neg
2+
//# ARGS: 7 19
3+
fn main(n:i64, m:i64) {
4+
let res : i64 = n & m;
5+
println!("{:?}", res);
6+
res = -res;
7+
println!("{:?}", res);
8+
}

test/rs/lowbit.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4

test/rs/lowbit.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//# Compute the lowest 1 bit of an integer
2+
//# ARGS: 21324
3+
fn main(n:i64) {
4+
let lb : i64 = n & (-n);
5+
println!("{:?}", lb);
6+
}

test/rs/lowbit_naive.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4

test/rs/lowbit_naive.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//# Compute the lowest 1 bit of an integer in the naive way
2+
//# ARGS: 21324
3+
fn main(n:i64) {
4+
let lb : i64 = 1;
5+
while (n == n / 2 * 2) {
6+
n = n / 2;
7+
lb = lb * 2;
8+
}
9+
println!("{:?}", lb);
10+
}

0 commit comments

Comments
 (0)