CBMC
Loading...
Searching...
No Matches
bitvector_expr.h
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: API to expression classes for bitvectors
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
9#ifndef CPROVER_UTIL_BITVECTOR_EXPR_H
10#define CPROVER_UTIL_BITVECTOR_EXPR_H
11
14
15#include "bitvector_types.h"
16#include "std_expr.h"
17
20{
21public:
23 : unary_exprt(ID_bswap, std::move(_op), std::move(_type))
24 {
26 }
27
33
34 std::size_t get_bits_per_byte() const
35 {
37 }
38
43};
44
45template <>
46inline bool can_cast_expr<bswap_exprt>(const exprt &base)
47{
48 return base.id() == ID_bswap;
49}
50
51inline void validate_expr(const bswap_exprt &value)
52{
53 validate_operands(value, 1, "bswap must have one operand");
55 value.op().type() == value.type(), "bswap type must match operand type");
56}
57
64inline const bswap_exprt &to_bswap_expr(const exprt &expr)
65{
66 PRECONDITION(expr.id() == ID_bswap);
67 const bswap_exprt &ret = static_cast<const bswap_exprt &>(expr);
69 return ret;
70}
71
74{
75 PRECONDITION(expr.id() == ID_bswap);
76 bswap_exprt &ret = static_cast<bswap_exprt &>(expr);
78 return ret;
79}
80
83{
84public:
86 {
87 }
88};
89
90template <>
91inline bool can_cast_expr<bitnot_exprt>(const exprt &base)
92{
93 return base.id() == ID_bitnot;
94}
95
96inline void validate_expr(const bitnot_exprt &value)
97{
98 validate_operands(value, 1, "Bit-wise not must have one operand");
99}
100
107inline const bitnot_exprt &to_bitnot_expr(const exprt &expr)
108{
109 PRECONDITION(expr.id() == ID_bitnot);
110 const bitnot_exprt &ret = static_cast<const bitnot_exprt &>(expr);
112 return ret;
113}
114
117{
118 PRECONDITION(expr.id() == ID_bitnot);
119 bitnot_exprt &ret = static_cast<bitnot_exprt &>(expr);
121 return ret;
122}
123
128{
129public:
132 {
133 }
134
139
141 : multi_ary_exprt(ID_bitor, std::move(_operands), std::move(_type))
142 {
143 }
144};
145
146template <>
147inline bool can_cast_expr<bitor_exprt>(const exprt &base)
148{
149 return base.id() == ID_bitor;
150}
151
158inline const bitor_exprt &to_bitor_expr(const exprt &expr)
159{
160 PRECONDITION(expr.id() == ID_bitor);
161 return static_cast<const bitor_exprt &>(expr);
162}
163
166{
167 PRECONDITION(expr.id() == ID_bitor);
168 return static_cast<bitor_exprt &>(expr);
169}
170
179{
180public:
182 : multi_ary_exprt(std::move(_op0), ID_bitnor, std::move(_op1))
183 {
184 }
185
190
192 : multi_ary_exprt(ID_bitnor, std::move(_operands), std::move(_type))
193 {
194 }
195};
196
197template <>
198inline bool can_cast_expr<bitnor_exprt>(const exprt &base)
199{
200 return base.id() == ID_bitnor;
201}
202
209inline const bitnor_exprt &to_bitnor_expr(const exprt &expr)
210{
211 PRECONDITION(expr.id() == ID_bitnor);
212 return static_cast<const bitnor_exprt &>(expr);
213}
214
217{
218 PRECONDITION(expr.id() == ID_bitnor);
219 return static_cast<bitnor_exprt &>(expr);
220}
221
226{
227public:
229 : multi_ary_exprt(std::move(_op0), ID_bitxor, std::move(_op1))
230 {
231 }
232
237
239 : multi_ary_exprt(ID_bitxor, std::move(_operands), std::move(_type))
240 {
241 }
242};
243
244template <>
245inline bool can_cast_expr<bitxor_exprt>(const exprt &base)
246{
247 return base.id() == ID_bitxor;
248}
249
256inline const bitxor_exprt &to_bitxor_expr(const exprt &expr)
257{
258 PRECONDITION(expr.id() == ID_bitxor);
259 return static_cast<const bitxor_exprt &>(expr);
260}
261
264{
265 PRECONDITION(expr.id() == ID_bitxor);
266 return static_cast<bitxor_exprt &>(expr);
267}
268
277{
278public:
283
288
290 : multi_ary_exprt(ID_bitxnor, std::move(_operands), std::move(_type))
291 {
292 }
293};
294
295template <>
296inline bool can_cast_expr<bitxnor_exprt>(const exprt &base)
297{
298 return base.id() == ID_bitxnor;
299}
300
307inline const bitxnor_exprt &to_bitxnor_expr(const exprt &expr)
308{
309 PRECONDITION(expr.id() == ID_bitxnor);
311 return static_cast<const bitxnor_exprt &>(expr);
312}
313
316{
317 PRECONDITION(expr.id() == ID_bitxnor);
319 return static_cast<bitxnor_exprt &>(expr);
320}
321
326{
327public:
330 {
331 }
332
337
339 : multi_ary_exprt(ID_bitand, std::move(_operands), std::move(_type))
340 {
341 }
342};
343
344template <>
345inline bool can_cast_expr<bitand_exprt>(const exprt &base)
346{
347 return base.id() == ID_bitand;
348}
349
356inline const bitand_exprt &to_bitand_expr(const exprt &expr)
357{
358 PRECONDITION(expr.id() == ID_bitand);
359 return static_cast<const bitand_exprt &>(expr);
360}
361
364{
365 PRECONDITION(expr.id() == ID_bitand);
366 return static_cast<bitand_exprt &>(expr);
367}
368
377{
378public:
383
388
390 : multi_ary_exprt(ID_bitnand, std::move(_operands), std::move(_type))
391 {
392 }
393};
394
395template <>
396inline bool can_cast_expr<bitnand_exprt>(const exprt &base)
397{
398 return base.id() == ID_bitnand;
399}
400
407inline const bitnand_exprt &to_bitnand_expr(const exprt &expr)
408{
409 PRECONDITION(expr.id() == ID_bitnand);
410 return static_cast<const bitnand_exprt &>(expr);
411}
412
415{
416 PRECONDITION(expr.id() == ID_bitnand);
417 return static_cast<bitnand_exprt &>(expr);
418}
419
422{
423public:
425 : binary_exprt(std::move(_src), _id, std::move(_distance))
426 {
427 }
428
429 shift_exprt(exprt _src, const irep_idt &_id, const std::size_t _distance);
430
432 {
433 return op0();
434 }
435
436 const exprt &op() const
437 {
438 return op0();
439 }
440
442 {
443 return op1();
444 }
445
446 const exprt &distance() const
447 {
448 return op1();
449 }
450};
451
452template <>
453inline bool can_cast_expr<shift_exprt>(const exprt &base)
454{
455 return base.id() == ID_shl || base.id() == ID_ashr || base.id() == ID_lshr ||
456 base.id() == ID_ror || base.id() == ID_rol;
457}
458
459inline void validate_expr(const shift_exprt &value)
460{
461 validate_operands(value, 2, "Shifts must have two operands");
462}
463
470inline const shift_exprt &to_shift_expr(const exprt &expr)
471{
472 const shift_exprt &ret = static_cast<const shift_exprt &>(expr);
474 return ret;
475}
476
479{
480 shift_exprt &ret = static_cast<shift_exprt &>(expr);
482 return ret;
483}
484
486class shl_exprt : public shift_exprt
487{
488public:
490 : shift_exprt(std::move(_src), ID_shl, std::move(_distance))
491 {
492 }
493
494 shl_exprt(exprt _src, const std::size_t _distance)
496 {
497 }
498};
499
500template <>
501inline bool can_cast_expr<shl_exprt>(const exprt &base)
502{
503 return base.id() == ID_shl;
504}
505
512inline const shl_exprt &to_shl_expr(const exprt &expr)
513{
514 PRECONDITION(expr.id() == ID_shl);
515 const shl_exprt &ret = static_cast<const shl_exprt &>(expr);
517 return ret;
518}
519
522{
523 PRECONDITION(expr.id() == ID_shl);
524 shl_exprt &ret = static_cast<shl_exprt &>(expr);
526 return ret;
527}
528
531{
532public:
537
538 ashr_exprt(exprt _src, const std::size_t _distance)
540 {
541 }
542};
543
544template <>
545inline bool can_cast_expr<ashr_exprt>(const exprt &base)
546{
547 return base.id() == ID_ashr;
548}
549
552{
553public:
558
559 lshr_exprt(exprt _src, const std::size_t _distance)
560 : shift_exprt(std::move(_src), ID_lshr, std::move(_distance))
561 {
562 }
563};
564
565template <>
566inline bool can_cast_expr<lshr_exprt>(const exprt &base)
567{
568 return base.id() == ID_lshr;
569}
570
573{
574public:
580
581 extractbit_exprt(exprt _src, const std::size_t _index);
582
584 {
585 return op0();
586 }
587
589 {
590 return op1();
591 }
592
593 const exprt &src() const
594 {
595 return op0();
596 }
597
598 const exprt &index() const
599 {
600 return op1();
601 }
602};
603
604template <>
606{
607 return base.id() == ID_extractbit;
608}
609
610inline void validate_expr(const extractbit_exprt &value)
611{
612 validate_operands(value, 2, "Extract bit must have two operands");
613}
614
621inline const extractbit_exprt &to_extractbit_expr(const exprt &expr)
622{
623 PRECONDITION(expr.id() == ID_extractbit);
624 const extractbit_exprt &ret = static_cast<const extractbit_exprt &>(expr);
626 return ret;
627}
628
631{
632 PRECONDITION(expr.id() == ID_extractbit);
633 extractbit_exprt &ret = static_cast<extractbit_exprt &>(expr);
635 return ret;
636}
637
640{
641public:
650 std::move(_type),
651 {std::move(_src), std::move(_index)})
652 {
653 }
654
655 extractbits_exprt(exprt _src, const std::size_t _index, typet _type);
656
658 {
659 return op0();
660 }
661
663 {
664 return op1();
665 }
666
667 const exprt &src() const
668 {
669 return op0();
670 }
671
672 const exprt &index() const
673 {
674 return op1();
675 }
676};
677
678template <>
680{
681 return base.id() == ID_extractbits;
682}
683
684inline void validate_expr(const extractbits_exprt &value)
685{
686 validate_operands(value, 2, "Extractbits must have two operands");
687}
688
696{
697 PRECONDITION(expr.id() == ID_extractbits);
698 const extractbits_exprt &ret = static_cast<const extractbits_exprt &>(expr);
700 return ret;
701}
702
705{
706 PRECONDITION(expr.id() == ID_extractbits);
707 extractbits_exprt &ret = static_cast<extractbits_exprt &>(expr);
709 return ret;
710}
711
714{
715public:
723 _src.type(),
724 {_src, std::move(_index), std::move(_new_value)})
725 {
726 PRECONDITION(new_value().type().id() == ID_bool);
727 }
728
729 update_bit_exprt(exprt _src, const std::size_t _index, exprt _new_value);
730
732 {
733 return op0();
734 }
735
737 {
738 return op1();
739 }
740
742 {
743 return op2();
744 }
745
746 const exprt &src() const
747 {
748 return op0();
749 }
750
751 const exprt &index() const
752 {
753 return op1();
754 }
755
756 const exprt &new_value() const
757 {
758 return op2();
759 }
760
761 static void check(
762 const exprt &expr,
764 {
765 validate_operands(expr, 3, "update_bit must have three operands");
766 }
767
769 exprt lower() const;
770};
771
772template <>
774{
775 return base.id() == ID_update_bit;
776}
777
784inline const update_bit_exprt &to_update_bit_expr(const exprt &expr)
785{
786 PRECONDITION(expr.id() == ID_update_bit);
788 return static_cast<const update_bit_exprt &>(expr);
789}
790
793{
794 PRECONDITION(expr.id() == ID_update_bit);
796 return static_cast<update_bit_exprt &>(expr);
797}
798
801{
802public:
810 _src.type(),
811 {_src, std::move(_index), std::move(_new_value)})
812 {
813 }
814
816
818 {
819 return op0();
820 }
821
823 {
824 return op1();
825 }
826
828 {
829 return op2();
830 }
831
832 const exprt &src() const
833 {
834 return op0();
835 }
836
837 const exprt &index() const
838 {
839 return op1();
840 }
841
842 const exprt &new_value() const
843 {
844 return op2();
845 }
846
847 static void check(
848 const exprt &expr,
850 {
851 validate_operands(expr, 3, "update_bits must have three operands");
852 }
853
855 exprt lower() const;
856};
857
858template <>
860{
861 return base.id() == ID_update_bits;
862}
863
871{
872 PRECONDITION(expr.id() == ID_update_bits);
874 return static_cast<const update_bits_exprt &>(expr);
875}
876
879{
880 PRECONDITION(expr.id() == ID_update_bits);
882 return static_cast<update_bits_exprt &>(expr);
883}
884
887{
888public:
890 : binary_exprt(
891 std::move(_times),
893 std::move(_src),
894 std::move(_type))
895 {
896 }
897
899 {
900 return static_cast<constant_exprt &>(op0());
901 }
902
903 const constant_exprt &times() const
904 {
905 return static_cast<const constant_exprt &>(op0());
906 }
907
909 {
910 return op1();
911 }
912
913 const exprt &op() const
914 {
915 return op1();
916 }
917};
918
919template <>
921{
922 return base.id() == ID_replication;
923}
924
925inline void validate_expr(const replication_exprt &value)
926{
927 validate_operands(value, 2, "Bit-wise replication must have two operands");
928}
929
937{
938 PRECONDITION(expr.id() == ID_replication);
939 const replication_exprt &ret = static_cast<const replication_exprt &>(expr);
941 return ret;
942}
943
946{
947 PRECONDITION(expr.id() == ID_replication);
948 replication_exprt &ret = static_cast<replication_exprt &>(expr);
950 return ret;
951}
952
959{
960public:
965
969 {std::move(_op0), std::move(_op1)},
970 std::move(_type))
971 {
972 }
973};
974
975template <>
977{
978 return base.id() == ID_concatenation;
979}
980
988{
990 return static_cast<const concatenation_exprt &>(expr);
991}
992
995{
997 return static_cast<concatenation_exprt &>(expr);
998}
999
1002{
1003public:
1005 : unary_exprt(ID_popcount, std::move(_op), std::move(_type))
1006 {
1007 }
1008
1009 explicit popcount_exprt(const exprt &_op)
1011 {
1012 }
1013
1016 exprt lower() const;
1017};
1018
1019template <>
1020inline bool can_cast_expr<popcount_exprt>(const exprt &base)
1021{
1022 return base.id() == ID_popcount;
1023}
1024
1025inline void validate_expr(const popcount_exprt &value)
1026{
1027 validate_operands(value, 1, "popcount must have one operand");
1028}
1029
1036inline const popcount_exprt &to_popcount_expr(const exprt &expr)
1037{
1038 PRECONDITION(expr.id() == ID_popcount);
1039 const popcount_exprt &ret = static_cast<const popcount_exprt &>(expr);
1041 return ret;
1042}
1043
1046{
1047 PRECONDITION(expr.id() == ID_popcount);
1048 popcount_exprt &ret = static_cast<popcount_exprt &>(expr);
1050 return ret;
1051}
1052
1056{
1057public:
1059 : binary_predicate_exprt(std::move(_lhs), make_id(kind), std::move(_rhs))
1060 {
1061 INVARIANT(
1062 valid_id(id()),
1063 "The kind used to construct binary_overflow_exprt should be in the set "
1064 "of expected valid kinds.");
1065 }
1066
1067 static void check(
1068 const exprt &expr,
1070 {
1071 binary_exprt::check(expr, vm);
1072
1073 if(expr.id() != ID_overflow_shl)
1074 {
1076 DATA_CHECK(
1077 vm,
1078 binary_expr.lhs().type() == binary_expr.rhs().type(),
1079 "operand types must match");
1080 }
1081 }
1082
1083 static void validate(
1084 const exprt &expr,
1085 const namespacet &,
1087 {
1088 check(expr, vm);
1089 }
1090
1092 static bool valid_id(const irep_idt &id)
1093 {
1094 return id == ID_overflow_plus || id == ID_overflow_mult ||
1095 id == ID_overflow_minus || id == ID_overflow_shl;
1096 }
1097
1098private:
1099 static irep_idt make_id(const irep_idt &kind)
1100 {
1101 if(valid_id(kind))
1102 return kind;
1103 else
1104 return "overflow-" + id2string(kind);
1105 }
1106};
1107
1108template <>
1110{
1111 return binary_overflow_exprt::valid_id(base.id());
1112}
1113
1114inline void validate_expr(const binary_overflow_exprt &value)
1115{
1117 value, 2, "binary overflow expression must have two operands");
1118}
1119
1127{
1129 expr.id() == ID_overflow_plus || expr.id() == ID_overflow_mult ||
1130 expr.id() == ID_overflow_minus || expr.id() == ID_overflow_shl);
1131 const binary_overflow_exprt &ret =
1132 static_cast<const binary_overflow_exprt &>(expr);
1134 return ret;
1135}
1136
1139{
1141 expr.id() == ID_overflow_plus || expr.id() == ID_overflow_mult ||
1142 expr.id() == ID_overflow_minus || expr.id() == ID_overflow_shl);
1143 binary_overflow_exprt &ret = static_cast<binary_overflow_exprt &>(expr);
1145 return ret;
1146}
1147
1149{
1150public:
1155
1158 exprt lower() const;
1159};
1160
1161template <>
1163{
1164 return base.id() == ID_overflow_plus;
1165}
1166
1168{
1169public:
1174
1177 exprt lower() const;
1178};
1179
1180template <>
1182{
1183 return base.id() == ID_overflow_minus;
1184}
1185
1187{
1188public:
1193
1196 exprt lower() const;
1197};
1198
1199template <>
1201{
1202 return base.id() == ID_overflow_mult;
1203}
1204
1206{
1207public:
1212};
1213
1214template <>
1216{
1217 return base.id() == ID_overflow_shl;
1218}
1219
1223{
1224public:
1226 : unary_predicate_exprt("overflow-" + id2string(kind), std::move(_op))
1227 {
1228 }
1229
1230 static void check(
1231 const exprt &expr,
1233 {
1234 unary_exprt::check(expr, vm);
1235 }
1236
1237 static void validate(
1238 const exprt &expr,
1239 const namespacet &,
1241 {
1242 check(expr, vm);
1243 }
1244};
1245
1246template <>
1248{
1249 return base.id() == ID_overflow_unary_minus;
1250}
1251
1252inline void validate_expr(const unary_overflow_exprt &value)
1253{
1255 value, 1, "unary overflow expression must have one operand");
1256}
1257
1261{
1262public:
1267
1268 static void check(
1269 const exprt &expr,
1271 {
1272 unary_exprt::check(expr, vm);
1273 }
1274
1275 static void validate(
1276 const exprt &expr,
1277 const namespacet &,
1279 {
1280 check(expr, vm);
1281 }
1282};
1283
1284template <>
1286{
1287 return base.id() == ID_overflow_unary_minus;
1288}
1289
1291{
1293 value, 1, "unary minus overflow expression must have one operand");
1294}
1295
1303{
1305 const unary_overflow_exprt &ret =
1306 static_cast<const unary_overflow_exprt &>(expr);
1308 return ret;
1309}
1310
1313{
1315 unary_overflow_exprt &ret = static_cast<unary_overflow_exprt &>(expr);
1317 return ret;
1318}
1319
1326{
1327public:
1333
1336 {
1337 }
1338
1339 bool zero_permitted() const
1340 {
1341 return !get_bool(ID_C_bounds_check);
1342 }
1343
1344 void zero_permitted(bool value)
1345 {
1346 set(ID_C_bounds_check, !value);
1347 }
1348
1349 static void check(
1350 const exprt &expr,
1352 {
1353 DATA_CHECK(
1354 vm,
1355 expr.operands().size() == 1,
1356 "unary expression must have a single operand");
1357 DATA_CHECK(
1358 vm,
1360 "operand must be of bitvector type");
1361 }
1362
1363 static void validate(
1364 const exprt &expr,
1365 const namespacet &,
1367 {
1368 check(expr, vm);
1369 }
1370
1373 exprt lower() const;
1374};
1375
1376template <>
1378{
1379 return base.id() == ID_count_leading_zeros;
1380}
1381
1383{
1384 validate_operands(value, 1, "count_leading_zeros must have one operand");
1385}
1386
1393inline const count_leading_zeros_exprt &
1395{
1398 static_cast<const count_leading_zeros_exprt &>(expr);
1400 return ret;
1401}
1402
1412
1419{
1420public:
1426
1431
1432 bool zero_permitted() const
1433 {
1434 return !get_bool(ID_C_bounds_check);
1435 }
1436
1437 void zero_permitted(bool value)
1438 {
1439 set(ID_C_bounds_check, !value);
1440 }
1441
1442 static void check(
1443 const exprt &expr,
1445 {
1446 DATA_CHECK(
1447 vm,
1448 expr.operands().size() == 1,
1449 "unary expression must have a single operand");
1450 DATA_CHECK(
1451 vm,
1453 "operand must be of bitvector type");
1454 }
1455
1456 static void validate(
1457 const exprt &expr,
1458 const namespacet &,
1460 {
1461 check(expr, vm);
1462 }
1463
1466 exprt lower() const;
1467};
1468
1469template <>
1471{
1472 return base.id() == ID_count_trailing_zeros;
1473}
1474
1476{
1477 validate_operands(value, 1, "count_trailing_zeros must have one operand");
1478}
1479
1486inline const count_trailing_zeros_exprt &
1488{
1491 static_cast<const count_trailing_zeros_exprt &>(expr);
1493 return ret;
1494}
1495
1505
1508{
1509public:
1511 : unary_exprt(ID_bitreverse, std::move(op))
1512 {
1513 }
1514
1517 exprt lower() const;
1518};
1519
1520template <>
1522{
1523 return base.id() == ID_bitreverse;
1524}
1525
1526inline void validate_expr(const bitreverse_exprt &value)
1527{
1528 validate_operands(value, 1, "Bit-wise reverse must have one operand");
1529}
1530
1538{
1539 PRECONDITION(expr.id() == ID_bitreverse);
1540 const bitreverse_exprt &ret = static_cast<const bitreverse_exprt &>(expr);
1542 return ret;
1543}
1544
1547{
1548 PRECONDITION(expr.id() == ID_bitreverse);
1549 bitreverse_exprt &ret = static_cast<bitreverse_exprt &>(expr);
1551 return ret;
1552}
1553
1556{
1557public:
1562
1564 : binary_exprt(
1565 std::move(_lhs),
1567 std::move(_rhs),
1568 std::move(_type))
1569 {
1570 }
1571};
1572
1573template <>
1575{
1576 return base.id() == ID_saturating_plus;
1577}
1578
1579inline void validate_expr(const saturating_plus_exprt &value)
1580{
1581 validate_operands(value, 2, "saturating plus must have two operands");
1582}
1583
1591{
1593 const saturating_plus_exprt &ret =
1594 static_cast<const saturating_plus_exprt &>(expr);
1596 return ret;
1597}
1598
1601{
1603 saturating_plus_exprt &ret = static_cast<saturating_plus_exprt &>(expr);
1605 return ret;
1606}
1607
1610{
1611public:
1616};
1617
1618template <>
1620{
1621 return base.id() == ID_saturating_minus;
1622}
1623
1624inline void validate_expr(const saturating_minus_exprt &value)
1625{
1626 validate_operands(value, 2, "saturating minus must have two operands");
1627}
1628
1636{
1639 static_cast<const saturating_minus_exprt &>(expr);
1641 return ret;
1642}
1643
1646{
1648 saturating_minus_exprt &ret = static_cast<saturating_minus_exprt &>(expr);
1650 return ret;
1651}
1652
1656{
1657public:
1660 make_id(kind),
1662 {{ID_value, _lhs.type()},
1663 {"overflow-" + id2string(kind), bool_typet{}}}},
1664 {_lhs, std::move(_rhs)})
1665 {
1666 INVARIANT(
1667 valid_id(id()),
1668 "The kind used to construct overflow_result_exprt should be in the set "
1669 "of expected valid kinds.");
1670 }
1671
1674 make_id(kind),
1676 {{ID_value, _op.type()},
1677 {"overflow-" + id2string(kind), bool_typet{}}}},
1678 {_op})
1679 {
1680 INVARIANT(
1681 valid_id(id()),
1682 "The kind used to construct overflow_result_exprt should be in the set "
1683 "of expected valid kinds.");
1684 }
1685
1686 // make op0 and op1 public
1687 using exprt::op0;
1688 using exprt::op1;
1689
1690 const exprt &op2() const = delete;
1691 exprt &op2() = delete;
1692 const exprt &op3() const = delete;
1693 exprt &op3() = delete;
1694
1695 static void check(
1696 const exprt &expr,
1698 {
1700 binary_exprt::check(expr, vm);
1701
1702 if(
1704 expr.id() != ID_overflow_result_shl)
1705 {
1707 DATA_CHECK(
1708 vm,
1709 binary_expr.lhs().type() == binary_expr.rhs().type(),
1710 "operand types must match");
1711 }
1712 }
1713
1714 static void validate(
1715 const exprt &expr,
1716 const namespacet &,
1718 {
1719 check(expr, vm);
1720 }
1721
1723 static bool valid_id(const irep_idt &id)
1724 {
1725 return id == ID_overflow_result_plus || id == ID_overflow_result_mult ||
1728 }
1729
1730private:
1731 static irep_idt make_id(const irep_idt &kind)
1732 {
1733 return "overflow_result-" + id2string(kind);
1734 }
1735};
1736
1737template <>
1739{
1740 return overflow_result_exprt::valid_id(base.id());
1741}
1742
1743inline void validate_expr(const overflow_result_exprt &value)
1744{
1745 if(value.id() == ID_overflow_result_unary_minus)
1746 {
1748 value, 1, "unary overflow expression must have two operands");
1749 }
1750 else
1751 {
1753 value, 2, "binary overflow expression must have two operands");
1754 }
1755}
1756
1764{
1766 const overflow_result_exprt &ret =
1767 static_cast<const overflow_result_exprt &>(expr);
1769 return ret;
1770}
1771
1780
1784{
1785public:
1787 : unary_exprt(ID_find_first_set, std::move(_op), std::move(_type))
1788 {
1789 }
1790
1793 {
1794 }
1795
1796 static void check(
1797 const exprt &expr,
1799 {
1800 DATA_CHECK(
1801 vm,
1802 expr.operands().size() == 1,
1803 "unary expression must have a single operand");
1804 DATA_CHECK(
1805 vm,
1807 "operand must be of bitvector type");
1808 }
1809
1810 static void validate(
1811 const exprt &expr,
1812 const namespacet &,
1814 {
1815 check(expr, vm);
1816 }
1817
1820 exprt lower() const;
1821};
1822
1823template <>
1825{
1826 return base.id() == ID_find_first_set;
1827}
1828
1829inline void validate_expr(const find_first_set_exprt &value)
1830{
1831 validate_operands(value, 1, "find_first_set must have one operand");
1832}
1833
1841{
1843 const find_first_set_exprt &ret =
1844 static_cast<const find_first_set_exprt &>(expr);
1846 return ret;
1847}
1848
1851{
1853 find_first_set_exprt &ret = static_cast<find_first_set_exprt &>(expr);
1855 return ret;
1856}
1857
1864{
1865public:
1867 : unary_exprt(ID_zero_extend, std::move(_op), std::move(_type))
1868 {
1869 }
1870
1871 // a lowering to extraction or concatenation
1872 exprt lower() const;
1873};
1874
1875template <>
1877{
1878 return base.id() == ID_zero_extend;
1879}
1880
1888{
1889 PRECONDITION(expr.id() == ID_zero_extend);
1891 return static_cast<const zero_extend_exprt &>(expr);
1892}
1893
1896{
1897 PRECONDITION(expr.id() == ID_zero_extend);
1899 return static_cast<zero_extend_exprt &>(expr);
1900}
1901
1905{
1906public:
1909 {
1910 }
1911
1913 exprt lower() const;
1914};
1915
1922inline const onehot_exprt &to_onehot_expr(const exprt &expr)
1923{
1924 PRECONDITION(expr.id() == ID_onehot);
1925 onehot_exprt::check(expr);
1926 return static_cast<const onehot_exprt &>(expr);
1927}
1928
1931{
1932 PRECONDITION(expr.id() == ID_onehot);
1933 onehot_exprt::check(expr);
1934 return static_cast<onehot_exprt &>(expr);
1935}
1936
1940{
1941public:
1944 {
1945 }
1946
1948 exprt lower() const;
1949};
1950
1957inline const onehot0_exprt &to_onehot0_expr(const exprt &expr)
1958{
1959 PRECONDITION(expr.id() == ID_onehot0);
1961 return static_cast<const onehot0_exprt &>(expr);
1962}
1963
1966{
1967 PRECONDITION(expr.id() == ID_onehot0);
1969 return static_cast<onehot0_exprt &>(expr);
1970}
1971
1972#endif // CPROVER_UTIL_BITVECTOR_EXPR_H
bool can_cast_expr< count_trailing_zeros_exprt >(const exprt &base)
const bitxnor_exprt & to_bitxnor_expr(const exprt &expr)
Cast an exprt to a bitxnor_exprt.
const onehot0_exprt & to_onehot0_expr(const exprt &expr)
Cast an exprt to a onehot0_exprt.
const saturating_minus_exprt & to_saturating_minus_expr(const exprt &expr)
Cast an exprt to a saturating_minus_exprt.
const replication_exprt & to_replication_expr(const exprt &expr)
Cast an exprt to a replication_exprt.
const saturating_plus_exprt & to_saturating_plus_expr(const exprt &expr)
Cast an exprt to a saturating_plus_exprt.
bool can_cast_expr< mult_overflow_exprt >(const exprt &base)
bool can_cast_expr< replication_exprt >(const exprt &base)
bool can_cast_expr< bswap_exprt >(const exprt &base)
const shift_exprt & to_shift_expr(const exprt &expr)
Cast an exprt to a shift_exprt.
const bitnand_exprt & to_bitnand_expr(const exprt &expr)
Cast an exprt to a bitnand_exprt.
bool can_cast_expr< find_first_set_exprt >(const exprt &base)
bool can_cast_expr< saturating_plus_exprt >(const exprt &base)
const shl_exprt & to_shl_expr(const exprt &expr)
Cast an exprt to a shl_exprt.
bool can_cast_expr< bitreverse_exprt >(const exprt &base)
bool can_cast_expr< count_leading_zeros_exprt >(const exprt &base)
const popcount_exprt & to_popcount_expr(const exprt &expr)
Cast an exprt to a popcount_exprt.
const update_bits_exprt & to_update_bits_expr(const exprt &expr)
Cast an exprt to an update_bits_exprt.
bool can_cast_expr< bitnand_exprt >(const exprt &base)
bool can_cast_expr< shift_exprt >(const exprt &base)
bool can_cast_expr< bitand_exprt >(const exprt &base)
bool can_cast_expr< update_bits_exprt >(const exprt &base)
bool can_cast_expr< saturating_minus_exprt >(const exprt &base)
const extractbits_exprt & to_extractbits_expr(const exprt &expr)
Cast an exprt to an extractbits_exprt.
bool can_cast_expr< popcount_exprt >(const exprt &base)
const onehot_exprt & to_onehot_expr(const exprt &expr)
Cast an exprt to a onehot_exprt.
bool can_cast_expr< extractbits_exprt >(const exprt &base)
bool can_cast_expr< bitxor_exprt >(const exprt &base)
const binary_overflow_exprt & to_binary_overflow_expr(const exprt &expr)
Cast an exprt to a binary_overflow_exprt.
bool can_cast_expr< bitor_exprt >(const exprt &base)
bool can_cast_expr< update_bit_exprt >(const exprt &base)
bool can_cast_expr< minus_overflow_exprt >(const exprt &base)
void validate_expr(const bswap_exprt &value)
const find_first_set_exprt & to_find_first_set_expr(const exprt &expr)
Cast an exprt to a find_first_set_exprt.
bool can_cast_expr< ashr_exprt >(const exprt &base)
bool can_cast_expr< zero_extend_exprt >(const exprt &base)
bool can_cast_expr< shl_overflow_exprt >(const exprt &base)
bool can_cast_expr< bitxnor_exprt >(const exprt &base)
const bitor_exprt & to_bitor_expr(const exprt &expr)
Cast an exprt to a bitor_exprt.
const bitand_exprt & to_bitand_expr(const exprt &expr)
Cast an exprt to a bitand_exprt.
bool can_cast_expr< overflow_result_exprt >(const exprt &base)
const update_bit_exprt & to_update_bit_expr(const exprt &expr)
Cast an exprt to an update_bit_exprt.
const bitnot_exprt & to_bitnot_expr(const exprt &expr)
Cast an exprt to a bitnot_exprt.
const unary_overflow_exprt & to_unary_overflow_expr(const exprt &expr)
Cast an exprt to a unary_overflow_exprt.
const bitnor_exprt & to_bitnor_expr(const exprt &expr)
Cast an exprt to a bitnor_exprt.
bool can_cast_expr< concatenation_exprt >(const exprt &base)
bool can_cast_expr< unary_overflow_exprt >(const exprt &base)
const overflow_result_exprt & to_overflow_result_expr(const exprt &expr)
Cast an exprt to a overflow_result_exprt.
bool can_cast_expr< lshr_exprt >(const exprt &base)
bool can_cast_expr< shl_exprt >(const exprt &base)
const bitxor_exprt & to_bitxor_expr(const exprt &expr)
Cast an exprt to a bitxor_exprt.
const bswap_exprt & to_bswap_expr(const exprt &expr)
Cast an exprt to a bswap_exprt.
bool can_cast_expr< plus_overflow_exprt >(const exprt &base)
const count_leading_zeros_exprt & to_count_leading_zeros_expr(const exprt &expr)
Cast an exprt to a count_leading_zeros_exprt.
bool can_cast_expr< bitnot_exprt >(const exprt &base)
bool can_cast_expr< binary_overflow_exprt >(const exprt &base)
const bitreverse_exprt & to_bitreverse_expr(const exprt &expr)
Cast an exprt to a bitreverse_exprt.
const extractbit_exprt & to_extractbit_expr(const exprt &expr)
Cast an exprt to an extractbit_exprt.
bool can_cast_expr< unary_minus_overflow_exprt >(const exprt &base)
bool can_cast_expr< extractbit_exprt >(const exprt &base)
const concatenation_exprt & to_concatenation_expr(const exprt &expr)
Cast an exprt to a concatenation_exprt.
bool can_cast_expr< bitnor_exprt >(const exprt &base)
const zero_extend_exprt & to_zero_extend_expr(const exprt &expr)
Cast an exprt to a zero_extend_exprt.
const count_trailing_zeros_exprt & to_count_trailing_zeros_expr(const exprt &expr)
Cast an exprt to a count_trailing_zeros_exprt.
Pre-defined bitvector types.
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:566
Arithmetic right shift.
ashr_exprt(exprt _src, exprt _distance)
ashr_exprt(exprt _src, const std::size_t _distance)
A base class for binary expressions.
Definition std_expr.h:639
static void check(const exprt &expr, const validation_modet vm=validation_modet::INVARIANT)
Definition std_expr.h:651
exprt & op0()
Definition expr.h:134
exprt & op1()
Definition expr.h:137
A Boolean expression returning true, iff operation kind would result in an overflow when applied to o...
static void check(const exprt &expr, const validation_modet vm=validation_modet::INVARIANT)
static void validate(const exprt &expr, const namespacet &, const validation_modet vm=validation_modet::INVARIANT)
static bool valid_id(const irep_idt &id)
Returns true iff id is a valid identifier of a binary_overflow_exprt.
static irep_idt make_id(const irep_idt &kind)
binary_overflow_exprt(exprt _lhs, const irep_idt &kind, exprt _rhs)
A base class for expressions that are predicates, i.e., Boolean-typed, and that take exactly two argu...
Definition std_expr.h:727
Bit-wise AND Any number of operands that is greater or equal one.
bitand_exprt(exprt::operandst _operands, typet _type)
bitand_exprt(exprt::operandst _operands)
bitand_exprt(const exprt &_op0, exprt _op1)
Bit-wise NAND.
bitnand_exprt(exprt::operandst _operands)
bitnand_exprt(exprt _op0, exprt _op1)
bitnand_exprt(exprt::operandst _operands, typet _type)
Bit-wise NOR.
bitnor_exprt(exprt::operandst _operands, typet _type)
bitnor_exprt(exprt::operandst _operands)
bitnor_exprt(exprt _op0, exprt _op1)
Bit-wise negation of bit-vectors.
bitnot_exprt(exprt op)
Bit-wise OR Any number of operands that is greater or equal one.
bitor_exprt(exprt::operandst _operands)
bitor_exprt(exprt::operandst _operands, typet _type)
bitor_exprt(const exprt &_op0, exprt _op1)
Reverse the order of bits in a bit-vector.
bitreverse_exprt(exprt op)
exprt lower() const
Lower a bitreverse_exprt to arithmetic and logic expressions.
Bit-wise XNOR.
bitxnor_exprt(exprt::operandst _operands)
bitxnor_exprt(exprt::operandst _operands, typet _type)
bitxnor_exprt(exprt _op0, exprt _op1)
Bit-wise XOR Any number of operands that is greater or equal one.
bitxor_exprt(exprt::operandst _operands, typet _type)
bitxor_exprt(exprt::operandst _operands)
bitxor_exprt(exprt _op0, exprt _op1)
The Boolean type.
Definition std_types.h:36
The byte swap expression.
bswap_exprt(exprt _op, std::size_t bits_per_byte)
std::size_t get_bits_per_byte() const
bswap_exprt(exprt _op, std::size_t bits_per_byte, typet _type)
void set_bits_per_byte(std::size_t bits_per_byte)
Concatenation of bit-vector operands.
concatenation_exprt(operandst _operands, typet _type)
concatenation_exprt(exprt _op0, exprt _op1, typet _type)
A constant literal expression.
Definition std_expr.h:2997
The count leading zeros (counting the number of zero bits starting from the most-significant bit) exp...
static void validate(const exprt &expr, const namespacet &, const validation_modet vm=validation_modet::INVARIANT)
void zero_permitted(bool value)
exprt lower() const
Lower a count_leading_zeros_exprt to arithmetic and logic expressions.
count_leading_zeros_exprt(const exprt &_op)
static void check(const exprt &expr, const validation_modet vm=validation_modet::INVARIANT)
count_leading_zeros_exprt(exprt _op, bool _zero_permitted, typet _type)
The count trailing zeros (counting the number of zero bits starting from the least-significant bit) e...
count_trailing_zeros_exprt(const exprt &_op)
count_trailing_zeros_exprt(exprt _op, bool _zero_permitted, typet _type)
static void check(const exprt &expr, const validation_modet vm=validation_modet::INVARIANT)
static void validate(const exprt &expr, const namespacet &, const validation_modet vm=validation_modet::INVARIANT)
void zero_permitted(bool value)
exprt lower() const
Lower a count_trailing_zeros_exprt to arithmetic and logic expressions.
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:38
Base class for all expressions.
Definition expr.h:350
exprt & op0()
Definition expr.h:134
exprt & op1()
Definition expr.h:137
exprt & op2()
Definition expr.h:140
Base class for all expressions.
Definition expr.h:57
std::vector< exprt > operandst
Definition expr.h:59
exprt & op0()
Definition expr.h:134
exprt & op1()
Definition expr.h:137
typet & type()
Return the type of the expression.
Definition expr.h:85
operandst & operands()
Definition expr.h:95
static void check(const exprt &, const validation_modet=validation_modet::INVARIANT)
Check that the expression is well-formed (shallow checks only, i.e., subexpressions and its type are ...
Definition expr.h:260
Extracts a single bit of a bit-vector operand.
extractbit_exprt(exprt _src, exprt _index)
Extract the _index-th least significant bit from _src.
const exprt & index() const
const exprt & src() const
Extracts a sub-range of a bit-vector operand.
const exprt & index() const
extractbits_exprt(exprt _src, exprt _index, typet _type)
Extract the bits [_index .
const exprt & src() const
Returns one plus the index of the least-significant one bit, or zero if the operand is zero.
find_first_set_exprt(exprt _op, typet _type)
static void validate(const exprt &expr, const namespacet &, const validation_modet vm=validation_modet::INVARIANT)
exprt lower() const
Lower a find_first_set_exprt to arithmetic and logic expressions.
find_first_set_exprt(const exprt &_op)
static void check(const exprt &expr, const validation_modet vm=validation_modet::INVARIANT)
bool get_bool(const irep_idt &name) const
Definition irep.cpp:57
std::size_t get_size_t(const irep_idt &name) const
Definition irep.cpp:67
void set(const irep_idt &name, const irep_idt &value)
Definition irep.h:412
void set_size_t(const irep_idt &name, const std::size_t value)
Definition irep.cpp:82
const irep_idt & id() const
Definition irep.h:388
Logical right shift.
lshr_exprt(exprt _src, const std::size_t _distance)
lshr_exprt(exprt _src, exprt _distance)
minus_overflow_exprt(exprt _lhs, exprt _rhs)
exprt lower() const
Lower a minus_overflow_exprt to arithmetic and logic expressions.
mult_overflow_exprt(exprt _lhs, exprt _rhs)
exprt lower() const
Lower a mult_overflow_exprt to arithmetic and logic expressions.
A base class for multi-ary expressions Associativity is not specified.
Definition std_expr.h:898
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition namespace.h:91
A Boolean expression returning true iff the given operand consists of exactly one '0' and '1' otherwi...
onehot0_exprt(exprt _op)
exprt lower() const
lowering to extractbit
A Boolean expression returning true iff the given operand consists of exactly one '1' and '0' otherwi...
onehot_exprt(exprt _op)
exprt lower() const
lowering to extractbit
An expression returning both the result of the arithmetic operation under wrap-around semantics as we...
exprt & op3()=delete
overflow_result_exprt(exprt _lhs, const irep_idt &kind, exprt _rhs)
overflow_result_exprt(exprt _op, const irep_idt &kind)
static void validate(const exprt &expr, const namespacet &, const validation_modet vm=validation_modet::INVARIANT)
exprt & op2()=delete
static bool valid_id(const irep_idt &id)
Returns true iff id is a valid identifier of an overflow_exprt.
const exprt & op2() const =delete
static irep_idt make_id(const irep_idt &kind)
static void check(const exprt &expr, const validation_modet vm=validation_modet::INVARIANT)
const exprt & op3() const =delete
plus_overflow_exprt(exprt _lhs, exprt _rhs)
exprt lower() const
Lower a plus_overflow_exprt to arithmetic and logic expressions.
The popcount (counting the number of bits set to 1) expression.
exprt lower() const
Lower a popcount_exprt to arithmetic and logic expressions.
popcount_exprt(exprt _op, typet _type)
popcount_exprt(const exprt &_op)
Bit-vector replication.
const exprt & op() const
const constant_exprt & times() const
constant_exprt & times()
replication_exprt(constant_exprt _times, exprt _src, typet _type)
Saturating subtraction expression.
saturating_minus_exprt(exprt _lhs, exprt _rhs)
The saturating plus expression.
saturating_plus_exprt(exprt _lhs, exprt _rhs, typet _type)
saturating_plus_exprt(exprt _lhs, exprt _rhs)
A base class for shift and rotate operators.
exprt & distance()
shift_exprt(exprt _src, const irep_idt &_id, exprt _distance)
const exprt & distance() const
const exprt & op() const
Left shift.
shl_exprt(exprt _src, const std::size_t _distance)
shl_exprt(exprt _src, exprt _distance)
shl_overflow_exprt(exprt _lhs, exprt _rhs)
Structure type, corresponds to C style structs.
Definition std_types.h:231
The type of an expression, extends irept.
Definition type.h:29
Generic base class for unary expressions.
Definition std_expr.h:354
static void check(const exprt &expr, const validation_modet vm=validation_modet::INVARIANT)
Definition std_expr.h:366
const exprt & op() const
Definition std_expr.h:384
A Boolean expression returning true, iff negation would result in an overflow when applied to the (si...
static void validate(const exprt &expr, const namespacet &, const validation_modet vm=validation_modet::INVARIANT)
static void check(const exprt &expr, const validation_modet vm=validation_modet::INVARIANT)
A Boolean expression returning true, iff operation kind would result in an overflow when applied to t...
static void validate(const exprt &expr, const namespacet &, const validation_modet vm=validation_modet::INVARIANT)
unary_overflow_exprt(const irep_idt &kind, exprt _op)
static void check(const exprt &expr, const validation_modet vm=validation_modet::INVARIANT)
A base class for expressions that are predicates, i.e., Boolean-typed, and that take exactly one argu...
Definition std_expr.h:564
Replaces a sub-range of a bit-vector operand.
const exprt & src() const
const exprt & index() const
update_bit_exprt(exprt _src, exprt _index, exprt _new_value)
Replaces the bit [_index] from _src to produce a result of the same type as _src.
exprt lower() const
A lowering to masking, shifting, or.
const exprt & new_value() const
static void check(const exprt &expr, const validation_modet vm=validation_modet::INVARIANT)
Replaces a sub-range of a bit-vector operand.
const exprt & index() const
static void check(const exprt &expr, const validation_modet vm=validation_modet::INVARIANT)
update_bits_exprt(exprt _src, exprt _index, exprt _new_value)
Replace the bits [_index .
const exprt & new_value() const
exprt lower() const
A lowering to masking, shifting, or.
const exprt & src() const
update_bits_exprt(exprt _src, const std::size_t _index, exprt _new_value)
zero extension The operand is converted to the given type by either a) truncating if the new type is ...
zero_extend_exprt(exprt _op, typet _type)
void validate_operands(const exprt &value, exprt::operandst::size_type number, const char *message, bool allow_more=false)
Definition expr_cast.h:250
const std::string & id2string(const irep_idt &d)
Definition irep.h:44
STL namespace.
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition invariant.h:534
#define PRECONDITION(CONDITION)
Definition invariant.h:463
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition invariant.h:423
API to expression classes.
const binary_exprt & to_binary_expr(const exprt &expr)
Cast an exprt to a binary_exprt.
Definition std_expr.h:711
const unary_exprt & to_unary_expr(const exprt &expr)
Cast an exprt to a unary_exprt.
Definition std_expr.h:414
#define DATA_CHECK(vm, condition, message)
This macro takes a condition which denotes a well-formedness criterion on goto programs,...
Definition validate.h:22
validation_modet