Skip to content
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 CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Verilog: `elsif preprocessor directive
* Verilog: fix for named generate blocks
* Verilog: $onehot and $onehot0 are now elaboration-time constant
* LTL/SVA to Buechi with --buechi

# EBMC 5.6
Expand Down
13 changes: 13 additions & 0 deletions regression/verilog/system-functions/onehot1.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CORE
onehot1.sv
--module main
^\[main\.pA0\] always \$onehot\(8\) == 1: PROVED .*$
^\[main\.pA1\] always \$onehot\(10\) == 0: PROVED .*$
^\[main\.pA2\] always \$onehot\(247\) == 0: PROVED .*$
^\[main\.pB0\] always \$onehot0\(8\) == 0: PROVED .*$
^\[main\.pB1\] always \$onehot0\(10\) == 0: PROVED .*$
^\[main\.pB2\] always \$onehot0\(247\) == 1: PROVED .*$
^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
15 changes: 15 additions & 0 deletions regression/verilog/system-functions/onehot1.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module main;

pA0: assert final ($onehot(8'b00001000)==1);
pA1: assert final ($onehot(8'b00001010)==0);
pA2: assert final ($onehot(8'b11110111)==0);

pB0: assert final ($onehot0(8'b00001000)==0);
pB1: assert final ($onehot0(8'b00001010)==0);
pB2: assert final ($onehot0(8'b11110111)==1);

// $onehot and $onehot0 yield elaboration-time constants
parameter Q1 = $onehot(3'b101);
parameter P1 = $onehot0(3'b101);

endmodule
32 changes: 32 additions & 0 deletions src/verilog/verilog_simplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ countones(const constant_exprt &expr, const namespacet &ns)
return to_constant_expr(simplified);
}

static constant_exprt onehot(const constant_exprt &expr, const namespacet &ns)
{
if(numeric_cast_v<mp_integer>(countones(expr, ns)) == 1)
return true_exprt();
else
return false_exprt();
}

static constant_exprt onehot0(const constant_exprt &expr, const namespacet &ns)
{
if(
numeric_cast_v<mp_integer>(countones(expr, ns)) ==
to_bitvector_type(expr.type()).get_width() - 1)
{
return true_exprt();
}
else
return false_exprt();
}

static exprt verilog_simplifier_rec(exprt expr, const namespacet &ns)
{
// Remember the Verilog type.
Expand Down Expand Up @@ -129,6 +149,18 @@ static exprt verilog_simplifier_rec(exprt expr, const namespacet &ns)
ops.push_back(replication.op());
expr = concatenation_exprt{ops, expr.type()};
}
else if(expr.id() == ID_onehot)
{
auto &op = to_onehot_expr(expr).op();
if(op.is_constant())
expr = onehot(to_constant_expr(op), ns);
}
else if(expr.id() == ID_onehot0)
{
auto &op = to_onehot0_expr(expr).op();
if(op.is_constant())
expr = onehot0(to_constant_expr(op), ns);
}

// We fall back to the simplifier to approximate
// the standard's definition of 'constant expression'.
Expand Down
Loading