@@ -43,7 +43,10 @@ bool simplify_exprt::simplify_bswap(bswap_exprt &expr)
43
43
mp_integer new_value=0 ;
44
44
for (std::size_t bit = 0 ; bit < width; bit += bits_per_byte)
45
45
{
46
- assert (!bytes.empty ());
46
+ INVARIANT (
47
+ !bytes.empty (),
48
+ " bytes is not empty because we just pushed just as many elements on "
49
+ " top of it as we are popping now" );
47
50
new_value+=bytes.back ()<<bit;
48
51
bytes.pop_back ();
49
52
}
@@ -180,7 +183,7 @@ bool simplify_exprt::simplify_mult(exprt &expr)
180
183
exprt::operandst::iterator constant;
181
184
182
185
// true if we have found a constant
183
- bool found = false ;
186
+ bool constant_found = false ;
184
187
185
188
typet c_sizeof_type=nil_typet ();
186
189
@@ -212,7 +215,7 @@ bool simplify_exprt::simplify_mult(exprt &expr)
212
215
c_sizeof_type=
213
216
static_cast <const typet &>(it->find (ID_C_c_sizeof_type));
214
217
215
- if (found )
218
+ if (constant_found )
216
219
{
217
220
// update the constant factor
218
221
if (!mul_expr (to_constant_expr (*constant), to_constant_expr (*it)))
@@ -222,7 +225,7 @@ bool simplify_exprt::simplify_mult(exprt &expr)
222
225
{
223
226
// set it as the constant factor if this is the first
224
227
constant=it;
225
- found= true ;
228
+ constant_found = true ;
226
229
}
227
230
}
228
231
@@ -238,7 +241,10 @@ bool simplify_exprt::simplify_mult(exprt &expr)
238
241
239
242
if (c_sizeof_type.is_not_nil ())
240
243
{
241
- assert (found);
244
+ INVARIANT (
245
+ constant_found,
246
+ " c_sizeof_type is only set to a non-nil value "
247
+ " if a constant has been found" );
242
248
constant->set (ID_C_c_sizeof_type, c_sizeof_type);
243
249
}
244
250
@@ -252,7 +258,7 @@ bool simplify_exprt::simplify_mult(exprt &expr)
252
258
else
253
259
{
254
260
// if the constant is a one and there are other factors
255
- if (found && constant->is_one ())
261
+ if (constant_found && constant->is_one ())
256
262
{
257
263
// just delete it
258
264
operands.erase (constant);
@@ -440,6 +446,8 @@ bool simplify_exprt::simplify_mod(exprt &expr)
440
446
441
447
bool simplify_exprt::simplify_plus (exprt &expr)
442
448
{
449
+ PRECONDITION (expr.id () == ID_plus);
450
+
443
451
if (!is_number (expr.type ()) &&
444
452
expr.type ().id ()!=ID_pointer)
445
453
return true ;
@@ -448,8 +456,6 @@ bool simplify_exprt::simplify_plus(exprt &expr)
448
456
449
457
exprt::operandst &operands=expr.operands ();
450
458
451
- assert (expr.id ()==ID_plus);
452
-
453
459
// floating-point addition is _NOT_ associative; thus,
454
460
// there is special case for float
455
461
@@ -523,7 +529,7 @@ bool simplify_exprt::simplify_plus(exprt &expr)
523
529
to_constant_expr (*it)))
524
530
{
525
531
*it=from_integer (0 , it->type ());
526
- assert (it->is_not_nil ());
532
+ CHECK_RETURN (it->is_not_nil ());
527
533
result=false ;
528
534
}
529
535
}
@@ -556,7 +562,7 @@ bool simplify_exprt::simplify_plus(exprt &expr)
556
562
{
557
563
*(itm->second )=from_integer (0 , expr.type ());
558
564
*it=from_integer (0 , expr.type ());
559
- assert (it->is_not_nil ());
565
+ CHECK_RETURN (it->is_not_nil ());
560
566
expr_map.erase (itm);
561
567
result=false ;
562
568
}
@@ -583,7 +589,7 @@ bool simplify_exprt::simplify_plus(exprt &expr)
583
589
if (operands.empty ())
584
590
{
585
591
expr=from_integer (0 , expr.type ());
586
- assert (expr.is_not_nil ());
592
+ CHECK_RETURN (expr.is_not_nil ());
587
593
return false ;
588
594
}
589
595
else if (operands.size ()==1 )
@@ -598,14 +604,14 @@ bool simplify_exprt::simplify_plus(exprt &expr)
598
604
599
605
bool simplify_exprt::simplify_minus (exprt &expr)
600
606
{
607
+ PRECONDITION (expr.id () == ID_minus);
608
+
601
609
if (!is_number (expr.type ()) &&
602
610
expr.type ().id ()!=ID_pointer)
603
611
return true ;
604
612
605
613
exprt::operandst &operands=expr.operands ();
606
614
607
- assert (expr.id ()==ID_minus);
608
-
609
615
if (operands.size ()!=2 )
610
616
return true ;
611
617
@@ -646,7 +652,7 @@ bool simplify_exprt::simplify_minus(exprt &expr)
646
652
if (operands[0 ]==operands[1 ])
647
653
{
648
654
exprt zero=from_integer (0 , expr.type ());
649
- assert (zero.is_not_nil ());
655
+ CHECK_RETURN (zero.is_not_nil ());
650
656
expr=zero;
651
657
return false ;
652
658
}
@@ -741,7 +747,9 @@ bool simplify_exprt::simplify_bitwise(exprt &expr)
741
747
if (expr.op1 ().type ()!=expr.type ())
742
748
break ;
743
749
744
- assert (a_str.size ()==b_str.size ());
750
+ INVARIANT (
751
+ a_str.size () == b_str.size (),
752
+ " bitvectors of the same type have the same size" );
745
753
746
754
std::string new_value;
747
755
new_value.resize (width);
@@ -847,32 +855,40 @@ bool simplify_exprt::simplify_bitwise(exprt &expr)
847
855
848
856
bool simplify_exprt::simplify_extractbit (exprt &expr)
849
857
{
850
- const typet &op0_type=expr.op0 ().type ();
851
-
852
- if (!is_bitvector_type (op0_type))
853
- return true ;
858
+ PRECONDITION (expr.id () == ID_extractbit);
859
+ const auto &extractbit_expr = to_extractbit_expr (expr);
854
860
855
- std:: size_t width= to_bitvector_type (op0_type). get_width ();
861
+ const typet &src_type = extractbit_expr. src (). type ();
856
862
857
- assert (expr.operands ().size ()==2 );
863
+ if (!is_bitvector_type (src_type))
864
+ return true ;
858
865
859
- mp_integer i ;
866
+ const std:: size_t src_bit_width = to_bitvector_type (src_type). get_width () ;
860
867
861
- if (to_integer (expr.op1 (), i))
868
+ const auto index_converted_to_int =
869
+ numeric_cast<mp_integer>(extractbit_expr.index ());
870
+ if (!index_converted_to_int.has_value ())
871
+ {
862
872
return true ;
863
-
864
- if (!expr.op0 ().is_constant ())
873
+ }
874
+ const mp_integer index_as_int = index_converted_to_int.value ();
875
+ if (!extractbit_expr.src ().is_constant ())
865
876
return true ;
866
877
867
- if (i< 0 || i>=width )
878
+ if (index_as_int < 0 || index_as_int >= src_bit_width )
868
879
return true ;
869
880
870
- const irep_idt &value=expr.op0 ().get (ID_value);
881
+ const irep_idt &src_value =
882
+ to_constant_expr (extractbit_expr.src ()).get_value ();
883
+
884
+ std::string src_value_as_string = id2string (src_value);
871
885
872
- if (value .size ()!=width )
886
+ if (src_value_as_string .size () != src_bit_width )
873
887
return true ;
874
888
875
- bool bit=(id2string (value)[width-integer2size_t (i)-1 ]==' 1' );
889
+ const bool bit =
890
+ (src_value_as_string
891
+ [src_bit_width - numeric_cast_v<std::size_t >(index_as_int) - 1 ] == ' 1' );
876
892
877
893
expr.make_bool (bit);
878
894
@@ -1580,7 +1596,9 @@ bool simplify_exprt::simplify_inequality_not_constant(exprt &expr)
1580
1596
1581
1597
// now we only have >=, =
1582
1598
1583
- assert (expr.id ()==ID_ge || expr.id ()==ID_equal);
1599
+ INVARIANT (
1600
+ expr.id () == ID_ge || expr.id () == ID_equal,
1601
+ " we previously converted all other cases to >= or ==" );
1584
1602
1585
1603
// syntactically equal?
1586
1604
@@ -1662,7 +1680,7 @@ bool simplify_exprt::simplify_inequality_not_constant(exprt &expr)
1662
1680
bool simplify_exprt::simplify_inequality_constant (exprt &expr)
1663
1681
{
1664
1682
// the constant is always on the RHS
1665
- assert (expr.op1 ().is_constant ());
1683
+ PRECONDITION (expr.op1 ().is_constant ());
1666
1684
1667
1685
if (expr.op0 ().id ()==ID_if && expr.op0 ().operands ().size ()==3 )
1668
1686
{
@@ -1766,7 +1784,7 @@ bool simplify_exprt::simplify_inequality_constant(exprt &expr)
1766
1784
{
1767
1785
constant+=i;
1768
1786
*it=from_integer (0 , it->type ());
1769
- assert (it->is_not_nil ());
1787
+ CHECK_RETURN (it->is_not_nil ());
1770
1788
changed=true ;
1771
1789
}
1772
1790
}
@@ -1906,9 +1924,7 @@ bool simplify_exprt::simplify_inequality_constant(exprt &expr)
1906
1924
}
1907
1925
else if (expr.id ()==ID_gt)
1908
1926
{
1909
- mp_integer i;
1910
- if (to_integer (expr.op1 (), i))
1911
- throw " bit-vector constant unexpectedly non-integer" ;
1927
+ mp_integer i = numeric_cast_v<mp_integer>(expr.op1 ());
1912
1928
1913
1929
if (i==max)
1914
1930
{
@@ -1932,9 +1948,7 @@ bool simplify_exprt::simplify_inequality_constant(exprt &expr)
1932
1948
}
1933
1949
else if (expr.id ()==ID_le)
1934
1950
{
1935
- mp_integer i;
1936
- if (to_integer (expr.op1 (), i))
1937
- throw " bit-vector constant unexpectedly non-integer" ;
1951
+ mp_integer i = numeric_cast_v<mp_integer>(expr.op1 ());
1938
1952
1939
1953
if (i==max)
1940
1954
{
0 commit comments