@@ -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
constant_exprt new_op (expr.type ());
747
755
std::string new_value;
@@ -849,32 +857,40 @@ bool simplify_exprt::simplify_bitwise(exprt &expr)
849
857
850
858
bool simplify_exprt::simplify_extractbit (exprt &expr)
851
859
{
852
- const typet &op0_type=expr.op0 ().type ();
853
-
854
- if (!is_bitvector_type (op0_type))
855
- return true ;
860
+ PRECONDITION (expr.id () == ID_extractbit);
861
+ const auto &extractbit_expr = to_extractbit_expr (expr);
856
862
857
- std:: size_t width= to_bitvector_type (op0_type). get_width ();
863
+ const typet &src_type = extractbit_expr. src (). type ();
858
864
859
- assert (expr.operands ().size ()==2 );
865
+ if (!is_bitvector_type (src_type))
866
+ return true ;
860
867
861
- mp_integer i ;
868
+ const std:: size_t src_bit_width = to_bitvector_type (src_type). get_width () ;
862
869
863
- if (to_integer (expr.op1 (), i))
870
+ const auto index_converted_to_int =
871
+ numeric_cast<mp_integer>(extractbit_expr.index ());
872
+ if (!index_converted_to_int.has_value ())
873
+ {
864
874
return true ;
865
-
866
- if (!expr.op0 ().is_constant ())
875
+ }
876
+ const mp_integer index_as_int = index_converted_to_int.value ();
877
+ if (!extractbit_expr.src ().is_constant ())
867
878
return true ;
868
879
869
- if (i< 0 || i>=width )
880
+ if (index_as_int < 0 || index_as_int >= src_bit_width )
870
881
return true ;
871
882
872
- const irep_idt &value=expr.op0 ().get (ID_value);
883
+ const irep_idt &src_value =
884
+ to_constant_expr (extractbit_expr.src ()).get_value ();
885
+
886
+ std::string src_value_as_string = id2string (src_value);
873
887
874
- if (value .size ()!=width )
888
+ if (src_value_as_string .size () != src_bit_width )
875
889
return true ;
876
890
877
- bool bit=(id2string (value)[width-integer2size_t (i)-1 ]==' 1' );
891
+ const bool bit =
892
+ (src_value_as_string
893
+ [src_bit_width - numeric_cast_v<std::size_t >(index_as_int) - 1 ] == ' 1' );
878
894
879
895
expr.make_bool (bit);
880
896
@@ -1582,7 +1598,9 @@ bool simplify_exprt::simplify_inequality_not_constant(exprt &expr)
1582
1598
1583
1599
// now we only have >=, =
1584
1600
1585
- assert (expr.id ()==ID_ge || expr.id ()==ID_equal);
1601
+ INVARIANT (
1602
+ expr.id () == ID_ge || expr.id () == ID_equal,
1603
+ " we previously converted all other cases to >= or ==" );
1586
1604
1587
1605
// syntactically equal?
1588
1606
@@ -1664,7 +1682,7 @@ bool simplify_exprt::simplify_inequality_not_constant(exprt &expr)
1664
1682
bool simplify_exprt::simplify_inequality_constant (exprt &expr)
1665
1683
{
1666
1684
// the constant is always on the RHS
1667
- assert (expr.op1 ().is_constant ());
1685
+ PRECONDITION (expr.op1 ().is_constant ());
1668
1686
1669
1687
if (expr.op0 ().id ()==ID_if && expr.op0 ().operands ().size ()==3 )
1670
1688
{
@@ -1768,7 +1786,7 @@ bool simplify_exprt::simplify_inequality_constant(exprt &expr)
1768
1786
{
1769
1787
constant+=i;
1770
1788
*it=from_integer (0 , it->type ());
1771
- assert (it->is_not_nil ());
1789
+ CHECK_RETURN (it->is_not_nil ());
1772
1790
changed=true ;
1773
1791
}
1774
1792
}
@@ -1908,9 +1926,7 @@ bool simplify_exprt::simplify_inequality_constant(exprt &expr)
1908
1926
}
1909
1927
else if (expr.id ()==ID_gt)
1910
1928
{
1911
- mp_integer i;
1912
- if (to_integer (expr.op1 (), i))
1913
- throw " bit-vector constant unexpectedly non-integer" ;
1929
+ mp_integer i = numeric_cast_v<mp_integer>(expr.op1 ());
1914
1930
1915
1931
if (i==max)
1916
1932
{
@@ -1934,9 +1950,7 @@ bool simplify_exprt::simplify_inequality_constant(exprt &expr)
1934
1950
}
1935
1951
else if (expr.id ()==ID_le)
1936
1952
{
1937
- mp_integer i;
1938
- if (to_integer (expr.op1 (), i))
1939
- throw " bit-vector constant unexpectedly non-integer" ;
1953
+ mp_integer i = numeric_cast_v<mp_integer>(expr.op1 ());
1940
1954
1941
1955
if (i==max)
1942
1956
{
0 commit comments