Skip to content

Commit 6377b10

Browse files
committed
Verilog: default aval/bval lowering
This introduces a default lowering for four-valued expressions. If any operand has x/z, then the result is 'x'. Otherwise, the result is the expression applied to the aval of the operands.
1 parent a1830bb commit 6377b10

File tree

6 files changed

+57
-14
lines changed

6 files changed

+57
-14
lines changed
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
KNOWNBUG
1+
CORE
22
minus1.sv
33

44
^EXIT=0$
55
^SIGNAL=0$
66
--
77
^warning: ignoring
88
--
9-
The result is wrong.
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
KNOWNBUG
1+
CORE
22
mult1.sv
33

44
^EXIT=0$
55
^SIGNAL=0$
66
--
77
^warning: ignoring
88
--
9-
The result is wrong.
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
KNOWNBUG
1+
CORE
22
plus1.sv
33

44
^EXIT=0$
55
^SIGNAL=0$
66
--
77
^warning: ignoring
88
--
9-
The result is wrong.

src/verilog/aval_bval_encoding.cpp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ bv_typet aval_bval_type(std::size_t width, irep_idt source_type)
3636

3737
bv_typet lower_to_aval_bval(const typet &src)
3838
{
39-
PRECONDITION(
40-
src.id() == ID_verilog_unsignedbv || src.id() == ID_verilog_signedbv);
39+
PRECONDITION(is_four_valued(src));
4140
return aval_bval_type(to_bitvector_type(src).get_width(), src.id());
4241
}
4342

@@ -75,9 +74,7 @@ typet aval_bval_underlying(const typet &src)
7574

7675
constant_exprt lower_to_aval_bval(const constant_exprt &src)
7776
{
78-
PRECONDITION(
79-
src.type().id() == ID_verilog_signedbv ||
80-
src.type().id() == ID_verilog_unsignedbv);
77+
PRECONDITION(is_four_valued(src.type()));
8178

8279
auto new_type = lower_to_aval_bval(src.type());
8380
auto width = aval_bval_width(new_type);
@@ -256,7 +253,7 @@ exprt has_xz(const exprt &expr)
256253
return false_exprt{}; // it's two-valued
257254
}
258255

259-
/// return 'x', one bit, in aval_bval encoding
256+
/// return 'x'
260257
exprt make_x(const typet &type)
261258
{
262259
PRECONDITION(is_four_valued(type));
@@ -470,3 +467,33 @@ exprt aval_bval(const shift_exprt &expr)
470467
auto x = make_x(expr.type());
471468
return if_exprt{distance_has_xz, x, combined};
472469
}
470+
471+
exprt default_aval_bval_lowering(const exprt &expr)
472+
{
473+
auto &type = expr.type();
474+
475+
PRECONDITION(is_four_valued(type));
476+
477+
exprt::operandst disjuncts;
478+
for(auto &op : expr.operands())
479+
disjuncts.push_back(has_xz(op));
480+
481+
auto has_xz = disjunction(disjuncts);
482+
483+
exprt two_valued_expr = expr; // copy
484+
485+
for(auto &op : two_valued_expr.operands())
486+
op = aval_underlying(op); // replace by aval
487+
488+
if(type.id() == ID_verilog_unsignedbv)
489+
two_valued_expr.type() = unsignedbv_typet{to_bitvector_type(type).width()};
490+
else if(type.id() == ID_verilog_signedbv)
491+
two_valued_expr.type() = signedbv_typet{to_bitvector_type(type).width()};
492+
else
493+
PRECONDITION(false);
494+
495+
return if_exprt{
496+
has_xz,
497+
make_x(type),
498+
aval_bval_conversion(two_valued_expr, lower_to_aval_bval(type))};
499+
}

src/verilog/aval_bval_encoding.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@ exprt aval_bval(const typecast_exprt &);
6666
/// lowering for shifts
6767
exprt aval_bval(const shift_exprt &);
6868

69+
/// If any operand has x/z, then the result is 'x'.
70+
/// Otherwise, the result is the expression applied to the aval
71+
/// of the operands.
72+
exprt default_aval_bval_lowering(const exprt &);
73+
6974
#endif

src/verilog/verilog_lowering.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,9 +614,23 @@ exprt verilog_lowering(exprt expr)
614614
{
615615
// turn into floatbv
616616
expr.type() = verilog_lowering(expr.type());
617+
return expr;
617618
}
618-
619-
return expr;
619+
else if(is_four_valued(expr))
620+
{
621+
return default_aval_bval_lowering(expr);
622+
}
623+
else
624+
return expr;
625+
}
626+
else if(
627+
expr.id() == ID_plus || expr.id() == ID_minus || expr.id() == ID_mult ||
628+
expr.id() == ID_div || expr.id() == ID_mod)
629+
{
630+
if(is_four_valued(expr))
631+
return default_aval_bval_lowering(expr);
632+
else
633+
return expr;
620634
}
621635
else if(expr.id() == ID_lshr || expr.id() == ID_ashr || expr.id() == ID_shl)
622636
{

0 commit comments

Comments
 (0)