Skip to content

Verilog: $isunknown #1149

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -3,6 +3,7 @@
* Verilog: `elsif preprocessor directive
* Verilog: fix for named generate blocks
* Verilog: $onehot and $onehot0 are now elaboration-time constant
* Verilog: $isunknown
* SystemVerilog: fix for #-# and #=# for empty matches
* SystemVerilog: fix for |-> and |=> for empty matches
* LTL/SVA to Buechi with --buechi
Expand Down
7 changes: 7 additions & 0 deletions regression/verilog/system-functions/isunknown1.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CORE
isunknown1.sv
--module main
^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
13 changes: 13 additions & 0 deletions regression/verilog/system-functions/isunknown1.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module main;

p0: assert final ($isunknown(123)==0);
p1: assert final ($isunknown('z)==1);
p2: assert final ($isunknown('x)==1);
p3: assert final ($isunknown('b01x0)==1);
p4: assert final ($isunknown(3'bzzz)==1);

// $isunknown yields an elaboration-time constant
parameter Q1 = $isunknown(3'b10z);
parameter P1 = $isunknown(3'b101);

endmodule
49 changes: 49 additions & 0 deletions src/verilog/verilog_typecheck_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Author: Daniel Kroening, [email protected]
#include <util/std_expr.h>
#include <util/string2int.h>

#include "aval_bval_encoding.h"
#include "convert_literals.h"
#include "expr2verilog.h"
#include "verilog_bits.h"
Expand Down Expand Up @@ -488,6 +489,31 @@ exprt verilog_typecheck_exprt::convert_expr_function_call(

/*******************************************************************\

Function: verilog_typecheck_exprt::isunknown

Inputs:

Outputs:

Purpose:

\*******************************************************************/

constant_exprt verilog_typecheck_exprt::isunknown(const constant_exprt &expr)
{
// constant folding for $isunknown
auto bval = ::bval(expr);
auto bval_simplified = simplify_expr(bval, ns);
CHECK_RETURN(bval_simplified.is_constant());
auto all_zeros = to_bv_type(bval_simplified.type()).all_zeros_expr();
if(bval_simplified == all_zeros)
return false_exprt{};
else
return true_exprt{};
}

/*******************************************************************\

Function: verilog_typecheck_exprt::bits

Inputs:
Expand Down Expand Up @@ -979,6 +1005,18 @@ exprt verilog_typecheck_exprt::convert_system_function(

return std::move(expr);
}
else if(identifier == "$isunknown")
{
if(arguments.size() != 1)
{
throw errort().with_location(expr.source_location())
<< "$isunknown takes one argument";
}

expr.type() = bool_typet();

return std::move(expr);
}
else if(identifier == "$past")
{
if(arguments.size() == 0 || arguments.size() >= 4)
Expand Down Expand Up @@ -1784,6 +1822,17 @@ exprt verilog_typecheck_exprt::elaborate_constant_system_function_call(
DATA_INVARIANT(arguments.size() == 1, "$typename takes one argument");
return typename_string(arguments[0]);
}
else if(identifier == "$isunknown")
{
DATA_INVARIANT(arguments.size() == 1, "$isunknown takes one argument");

auto op = elaborate_constant_expression(arguments[0]);

if(!op.is_constant())
return std::move(expr); // give up
else
return isunknown(to_constant_expr(op));
}
else
return std::move(expr); // don't know it, won't elaborate
}
Expand Down
1 change: 1 addition & 0 deletions src/verilog/verilog_typecheck_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ class verilog_typecheck_exprt:public verilog_typecheck_baset
exprt bits(const exprt &);
std::optional<mp_integer> bits_rec(const typet &) const;
constant_exprt countones(const constant_exprt &);
constant_exprt isunknown(const constant_exprt &);
constant_exprt left(const exprt &);
constant_exprt right(const exprt &);
constant_exprt low(const exprt &);
Expand Down
Loading