Skip to content

Commit b794916

Browse files
committed
Verilog: aval/bval lowering for casts to Bool
This implements casing four-valued ava/bval encoded cast to Bool. In particular, values containing x or z cast to false, not true.
1 parent 01c39cf commit b794916

File tree

7 files changed

+34
-8
lines changed

7 files changed

+34
-8
lines changed

regression/verilog/SVA/sequence5.desc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
KNOWNBUG
1+
CORE broken-smt-backend
22
sequence5.sv
33
--bound 0
44
^\[main\.p0\] 1: PROVED up to bound 0$
55
^\[main\.p1\] 0: REFUTED$
6-
^\[main\.p2\] 1'bx: PROVED up to bound 0$
7-
^\[main\.p3\] 1'bz: PROVED up to bound 0$
6+
^\[main\.p2\] 1'bx: REFUTED$
7+
^\[main\.p3\] 1'bz: REFUTED$
88
^EXIT=10$
99
^SIGNAL=0$
1010
--
1111
^warning: ignoring
1212
--
13-
x and z are recognized as 'true', but 'true' is defined as a "nonzero known
14-
value" (1800-2017 12.4).

regression/verilog/SVA/sva_and1.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ sva_and1.sv
33
--bound 0
44
^\[main\.p0\] always \(1 and 1\): PROVED up to bound 0$
55
^\[main\.p1\] always \(1 and 0\): REFUTED$
6-
^\[main\.p2\] always \(1 and 32'b0000000000000000000000000000000x\): PROVED up to bound 0$
6+
^\[main\.p2\] always \(1 and 32'b0000000000000000000000000000000x\): REFUTED$
77
^EXIT=10$
88
^SIGNAL=0$
99
--

regression/verilog/SVA/sva_iff1.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ sva_iff1.sv
33
--bound 0
44
^\[main\.p0\] always \(1 iff 1\): PROVED up to bound 0$
55
^\[main\.p1\] always \(1 iff 0\): REFUTED$
6-
^\[main\.p2\] always \(1 iff 32'b0000000000000000000000000000000x\): PROVED up to bound 0$
6+
^\[main\.p2\] always \(1 iff 32'b0000000000000000000000000000000x\): REFUTED$
77
^EXIT=10$
88
^SIGNAL=0$
99
--

regression/verilog/SVA/sva_implies1.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ sva_implies1.sv
33
--bound 0
44
^\[main\.p0\] always \(1 implies 1\): PROVED up to bound 0$
55
^\[main\.p1\] always \(1 implies 0\): REFUTED$
6-
^\[main\.p2\] always \(1 implies 32'b0000000000000000000000000000000x\): PROVED up to bound 0$
6+
^\[main\.p2\] always \(1 implies 32'b0000000000000000000000000000000x\): REFUTED$
77
^EXIT=10$
88
^SIGNAL=0$
99
--

src/verilog/aval_bval_encoding.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ bool is_four_valued(const typet &type)
2121
return type.id() == ID_verilog_unsignedbv || type.id() == ID_verilog_signedbv;
2222
}
2323

24+
bool is_four_valued(const exprt &expr)
25+
{
26+
return is_four_valued(expr.type());
27+
}
28+
2429
bv_typet aval_bval_type(std::size_t width, irep_idt source_type)
2530
{
2631
PRECONDITION(!source_type.empty());
@@ -333,3 +338,15 @@ exprt aval_bval(const power_exprt &expr)
333338
auto x = make_x(expr.type());
334339
return if_exprt{has_xz, x, aval_bval_conversion(power_expr, x.type())};
335340
}
341+
342+
exprt aval_bval(const typecast_exprt &expr)
343+
{
344+
// 'true' is defined as a "nonzero known value" (1800-2017 12.4).
345+
PRECONDITION(is_aval_bval(expr.op()));
346+
PRECONDITION(expr.type().id() == ID_bool);
347+
348+
auto op_has_xz = ::has_xz(expr.op());
349+
auto op_aval = aval(expr.op());
350+
auto op_aval_zero = to_bv_type(op_aval.type()).all_zeros_expr();
351+
return and_exprt{not_exprt{op_has_xz}, notequal_exprt{op_aval, op_aval_zero}};
352+
}

src/verilog/aval_bval_encoding.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ Author: Daniel Kroening, [email protected]
2424
// 1 1 | Z
2525

2626
bool is_four_valued(const typet &);
27+
bool is_four_valued(const exprt &);
2728
bool is_aval_bval(const typet &);
29+
bool is_aval_bval(const exprt &);
2830
std::size_t aval_bval_width(const typet &);
2931
typet aval_bval_underlying(const typet &);
3032

@@ -50,5 +52,7 @@ exprt aval_bval(const verilog_wildcard_equality_exprt &);
5052
exprt aval_bval(const verilog_wildcard_inequality_exprt &);
5153
/// lowering for **
5254
exprt aval_bval(const power_exprt &);
55+
/// lowering for typecasts
56+
exprt aval_bval(const typecast_exprt &);
5357

5458
#endif

src/verilog/verilog_synthesis.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,13 @@ exprt verilog_synthesist::synth_expr(exprt expr, symbol_statet symbol_state)
304304
}
305305
else if(expr.id()==ID_typecast)
306306
{
307+
// When casting a four-valued scalar to bool,
308+
// 'true' is defined as a "nonzero known value" (1800-2017 12.4).
309+
if(is_aval_bval(to_typecast_expr(expr).op()) && expr.type().id() == ID_bool)
310+
{
311+
expr = aval_bval(to_typecast_expr(expr));
312+
}
313+
else
307314
{
308315
auto &op = to_typecast_expr(expr).op();
309316

0 commit comments

Comments
 (0)