Skip to content

Commit

Permalink
Merge pull request #929 from diffblue/aval-bval-bitwise
Browse files Browse the repository at this point in the history
aval/bval encoding for bitwise operators
  • Loading branch information
tautschnig authored Jan 16, 2025
2 parents bc7a3df + b40d0a8 commit 4f09a7e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
7 changes: 7 additions & 0 deletions regression/verilog/expressions/bitwise1.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CORE
bitwise1.sv
--bound 0
^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
16 changes: 16 additions & 0 deletions regression/verilog/expressions/bitwise1.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module main;

assert final ((4'b0001 & 4'b1001) === 4'b0001);
assert final ((4'b1001 & 4'bx001) === 4'bx001);
assert final ((4'b1001 & 4'bz001) === 4'bx001);
assert final ((4'b0001 | 4'b1001) === 4'b1001);
assert final ((4'b0001 | 4'bx001) === 4'bx001);
assert final ((4'b0001 | 4'bz001) === 4'bx001);
assert final ((4'b0001 ^ 4'b1001) === 4'b1000);
assert final ((4'b0001 ^ 4'bx001) === 4'bx000);
assert final ((4'b0001 ^ 4'bz001) === 4'bx000);
assert final ((4'b0001 ~^ 4'b1001) === 4'b0111);
assert final ((4'b0001 ~^ 4'bx001) === 4'bx111);
assert final ((4'b0001 ~^ 4'bz001) === 4'bx111);

endmodule
33 changes: 33 additions & 0 deletions src/verilog/aval_bval_encoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,39 @@ exprt aval_bval(const bitnot_exprt &expr)
return combine_aval_bval(aval, op_bval, lower_to_aval_bval(expr.type()));
}

exprt aval_bval_bitwise(const multi_ary_exprt &expr)
{
auto &type = expr.type();
PRECONDITION(is_four_valued(type));
PRECONDITION(!expr.operands().empty());

for(auto &op : expr.operands())
PRECONDITION(is_aval_bval(op));

// x/z is done bit-wise.
// Any bit involving x/z is x.

// bval
exprt::operandst bval_disjuncts;

for(auto &op : expr.operands())
bval_disjuncts.push_back(bval(op));

auto bval = bitor_exprt{bval_disjuncts, bval_disjuncts.front().type()};

// aval
exprt::operandst aval_conjuncts;

for(auto &op : expr.operands())
aval_conjuncts.push_back(aval(op));

exprt aval = bitand_exprt{
multi_ary_exprt{expr.id(), aval_conjuncts, aval_conjuncts.front().type()},
bitnot_exprt{bval}}; // 0/1 case

return combine_aval_bval(aval, bval, lower_to_aval_bval(expr.type()));
}

exprt aval_bval(const power_exprt &expr)
{
PRECONDITION(is_four_valued(expr.type()));
Expand Down
2 changes: 2 additions & 0 deletions src/verilog/aval_bval_encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ exprt aval_bval(const verilog_logical_inequality_exprt &);
exprt aval_bval(const not_exprt &);
/// lowering for ~
exprt aval_bval(const bitnot_exprt &);
/// lowering for &, |, ^, ^~
exprt aval_bval_bitwise(const multi_ary_exprt &);
/// lowering for ==?
exprt aval_bval(const verilog_wildcard_equality_exprt &);
/// lowering for !=?
Expand Down
12 changes: 12 additions & 0 deletions src/verilog/verilog_lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,18 @@ exprt verilog_lowering(exprt expr)
else
return expr; // leave as is
}
else if(
expr.id() == ID_bitand || expr.id() == ID_bitor || expr.id() == ID_bitxor ||
expr.id() == ID_bitxnor)
{
auto &multi_ary_expr = to_multi_ary_expr(expr);

// encode into aval/bval
if(is_four_valued(expr.type()))
return aval_bval_bitwise(multi_ary_expr);
else
return expr; // leave as is
}
else if(expr.id() == ID_verilog_iff)
{
auto &iff = to_verilog_iff_expr(expr);
Expand Down

0 comments on commit 4f09a7e

Please sign in to comment.