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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
@@ -1,6 +1,7 @@
# EBMC 5.7

* Verilog: `elsif preprocessor directive
* Verilog: $isunknown
* LTL/SVA to Buechi with --buechi

# EBMC 5.6
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
28 changes: 28 additions & 0 deletions src/verilog/verilog_simplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]

#include <ebmc/ebmc_error.h>

#include "aval_bval_encoding.h"
#include "verilog_expr.h"
#include "verilog_types.h"

Expand All @@ -33,6 +34,18 @@ countones(const constant_exprt &expr, const namespacet &ns)
return to_constant_expr(simplified);
}

/// constant folding for $isunknown
static constant_exprt
isunknown(const constant_exprt &expr, const namespacet &ns)
{
auto bval = ::bval(expr);
CHECK_RETURN(bval.is_constant());
if(numeric_cast_v<mp_integer>(to_constant_expr(bval)) == 0)
return false_exprt{};
else
return true_exprt{};
}

static exprt verilog_simplifier_rec(exprt expr, const namespacet &ns)
{
// Remember the Verilog type.
Expand Down Expand Up @@ -129,6 +142,21 @@ 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_function_call)
{
auto &call = to_function_call_expr(expr);
if(call.function().id() == ID_symbol)
{
auto identifier = to_symbol_expr(call.function()).get_identifier();
if(identifier == "$isunknown")
{
DATA_INVARIANT(
call.arguments().size() == 1, "$isunknown gets one argument");
if(call.arguments()[0].is_constant())
expr = isunknown(to_constant_expr(call.arguments()[0]), ns);
}
}
}

// We fall back to the simplifier to approximate
// the standard's definition of 'constant expression'.
Expand Down
12 changes: 12 additions & 0 deletions src/verilog/verilog_typecheck_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,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
Loading