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

aval/bval encoding for bitwise operators #929

Merged
merged 1 commit into from
Jan 16, 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
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
Loading