CBMC
Loading...
Searching...
No Matches
bitvector_types.h
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Pre-defined bitvector types
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
11
12#ifndef CPROVER_UTIL_BITVECTOR_TYPES_H
13#define CPROVER_UTIL_BITVECTOR_TYPES_H
14
15#include "expr_cast.h" // IWYU pragma: keep
16#include "mp_arith.h"
17#include "type.h"
18
19class constant_exprt;
20
26class bitvector_typet : public typet
27{
28public:
29 explicit bitvector_typet(const irep_idt &_id) : typet(_id)
30 {
31 }
32
33 bitvector_typet(const irep_idt &_id, std::size_t width) : typet(_id)
34 {
36 }
37
39 {
40 width(_width);
41 }
42
43 std::size_t get_width() const
44 {
45 return get_size_t(ID_width);
46 }
47
48 std::size_t width() const;
49
50 void set_width(std::size_t width)
51 {
53 }
54
55 void width(const mp_integer &);
56
57 static void check(
58 const typet &type,
60 {
62 vm, !type.get(ID_width).empty(), "bitvector type must have width");
63 }
64
65 // helpers to create common constants
68};
69
73template <>
74inline bool can_cast_type<bitvector_typet>(const typet &type)
75{
76 return type.id() == ID_signedbv || type.id() == ID_unsignedbv ||
77 type.id() == ID_fixedbv || type.id() == ID_floatbv ||
78 type.id() == ID_verilog_signedbv ||
79 type.id() == ID_verilog_unsignedbv || type.id() == ID_bv ||
80 type.id() == ID_pointer || type.id() == ID_c_bit_field ||
81 type.id() == ID_c_bool;
82}
83
86inline bool is_signed_or_unsigned_bitvector(const typet &type)
87{
88 return type.id() == ID_signedbv || type.id() == ID_unsignedbv;
89}
90
99inline const bitvector_typet &to_bitvector_type(const typet &type)
100{
102
103 return static_cast<const bitvector_typet &>(type);
104}
105
108{
110
111 return static_cast<bitvector_typet &>(type);
112}
113
116{
117public:
118 explicit bv_typet(std::size_t width) : bitvector_typet(ID_bv)
119 {
121 }
122
123 static void check(
124 const typet &type,
126 {
128 vm, !type.get(ID_width).empty(), "bitvector type must have width");
129 }
130};
131
135template <>
136inline bool can_cast_type<bv_typet>(const typet &type)
137{
138 return type.id() == ID_bv;
139}
140
149inline const bv_typet &to_bv_type(const typet &type)
150{
152 bv_typet::check(type);
153 return static_cast<const bv_typet &>(type);
154}
155
158{
160 bv_typet::check(type);
161 return static_cast<bv_typet &>(type);
162}
163
166{
167public:
168 integer_bitvector_typet(const irep_idt &id, std::size_t width)
170 {
171 }
172
177
179 mp_integer smallest() const;
181 mp_integer largest() const;
182
183 // If we ever need any of the following three methods in \ref fixedbv_typet or
184 // \ref floatbv_typet, we might want to move them to a new class
185 // numeric_bitvector_typet, which would be between integer_bitvector_typet and
186 // bitvector_typet in the hierarchy.
187
194};
195
199template <>
201{
202 return type.id() == ID_signedbv || type.id() == ID_unsignedbv;
203}
204
213inline const integer_bitvector_typet &
215{
217
218 return static_cast<const integer_bitvector_typet &>(type);
219}
220
228
231{
232public:
233 explicit unsignedbv_typet(std::size_t width)
235 {
236 }
237
242
243 static void check(
244 const typet &type,
246 {
247 bitvector_typet::check(type, vm);
248 }
249};
250
254template <>
256{
257 return type.id() == ID_unsignedbv;
258}
259
268inline const unsignedbv_typet &to_unsignedbv_type(const typet &type)
269{
272 return static_cast<const unsignedbv_typet &>(type);
273}
274
277{
280 return static_cast<unsignedbv_typet &>(type);
281}
282
285{
286public:
287 explicit signedbv_typet(std::size_t width)
289 {
290 }
291
296
297 static void check(
298 const typet &type,
300 {
302 vm, !type.get(ID_width).empty(), "signed bitvector type must have width");
303 }
304};
305
309template <>
310inline bool can_cast_type<signedbv_typet>(const typet &type)
311{
312 return type.id() == ID_signedbv;
313}
314
323inline const signedbv_typet &to_signedbv_type(const typet &type)
324{
327 return static_cast<const signedbv_typet &>(type);
328}
329
332{
335 return static_cast<signedbv_typet &>(type);
336}
337
343{
344public:
348
349 std::size_t get_fraction_bits() const
350 {
351 return get_width() - get_integer_bits();
352 }
353
354 std::size_t get_integer_bits() const;
355
356 void set_integer_bits(std::size_t b)
357 {
359 }
360
361 static void check(
362 const typet &type,
364 {
366 vm, !type.get(ID_width).empty(), "fixed bitvector type must have width");
367 }
368};
369
373template <>
374inline bool can_cast_type<fixedbv_typet>(const typet &type)
375{
376 return type.id() == ID_fixedbv;
377}
378
387inline const fixedbv_typet &to_fixedbv_type(const typet &type)
388{
391 return static_cast<const fixedbv_typet &>(type);
392}
393
396{
399 return static_cast<fixedbv_typet &>(type);
400}
401
404{
405public:
409
410 std::size_t get_e() const
411 {
412 // subtract one for sign bit
413 return get_width() - get_f() - 1;
414 }
415
416 std::size_t get_f() const;
417
418 void set_f(std::size_t b)
419 {
420 set_size_t(ID_f, b);
421 }
422
423 static void check(
424 const typet &type,
426 {
428 vm, !type.get(ID_width).empty(), "float bitvector type must have width");
429 }
430};
431
435template <>
436inline bool can_cast_type<floatbv_typet>(const typet &type)
437{
438 return type.id() == ID_floatbv;
439}
440
449inline const floatbv_typet &to_floatbv_type(const typet &type)
450{
453 return static_cast<const floatbv_typet &>(type);
454}
455
458{
461 return static_cast<floatbv_typet &>(type);
462}
463
464#endif // CPROVER_UTIL_BITVECTOR_TYPES_H
const bv_typet & to_bv_type(const typet &type)
Cast a typet to a bv_typet.
bool is_signed_or_unsigned_bitvector(const typet &type)
This method tests, if the given typet is a signed or unsigned bitvector.
bool can_cast_type< signedbv_typet >(const typet &type)
Check whether a reference to a typet is a signedbv_typet.
const fixedbv_typet & to_fixedbv_type(const typet &type)
Cast a typet to a fixedbv_typet.
bool can_cast_type< integer_bitvector_typet >(const typet &type)
Check whether a reference to a typet is an integer_bitvector_typet.
bool can_cast_type< bv_typet >(const typet &type)
Check whether a reference to a typet is a bv_typet.
const bitvector_typet & to_bitvector_type(const typet &type)
Cast a typet to a bitvector_typet.
bool can_cast_type< fixedbv_typet >(const typet &type)
Check whether a reference to a typet is a fixedbv_typet.
bool can_cast_type< bitvector_typet >(const typet &type)
Check whether a reference to a typet is a bitvector_typet.
bool can_cast_type< unsignedbv_typet >(const typet &type)
Check whether a reference to a typet is a unsignedbv_typet.
bool can_cast_type< floatbv_typet >(const typet &type)
Check whether a reference to a typet is a floatbv_typet.
const integer_bitvector_typet & to_integer_bitvector_type(const typet &type)
Cast a typet to an integer_bitvector_typet.
const floatbv_typet & to_floatbv_type(const typet &type)
Cast a typet to a floatbv_typet.
const unsignedbv_typet & to_unsignedbv_type(const typet &type)
Cast a typet to an unsignedbv_typet.
const signedbv_typet & to_signedbv_type(const typet &type)
Cast a typet to a signedbv_typet.
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:566
Base class of fixed-width bit-vector types.
constant_exprt all_zeros_expr() const
constant_exprt all_ones_expr() const
void set_width(std::size_t width)
std::size_t get_width() const
std::size_t width() const
static void check(const typet &type, const validation_modet vm=validation_modet::INVARIANT)
bitvector_typet(const irep_idt &_id, std::size_t width)
bitvector_typet(const irep_idt &_id, mp_integer _width)
bitvector_typet(const irep_idt &_id)
Fixed-width bit-vector without numerical interpretation.
bv_typet(std::size_t width)
static void check(const typet &type, const validation_modet vm=validation_modet::INVARIANT)
A constant literal expression.
Definition std_expr.h:2997
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:38
Fixed-width bit-vector with signed fixed-point interpretation.
void set_integer_bits(std::size_t b)
std::size_t get_fraction_bits() const
std::size_t get_integer_bits() const
static void check(const typet &type, const validation_modet vm=validation_modet::INVARIANT)
Fixed-width bit-vector with IEEE floating-point interpretation.
void set_f(std::size_t b)
std::size_t get_f() const
std::size_t get_e() const
static void check(const typet &type, const validation_modet vm=validation_modet::INVARIANT)
Fixed-width bit-vector representing a signed or unsigned integer.
integer_bitvector_typet(const irep_idt &id, std::size_t width)
constant_exprt largest_expr() const
Return an expression representing the largest value of this type.
integer_bitvector_typet(const irep_idt &id, const mp_integer &width)
mp_integer largest() const
Return the largest value that can be represented using this type.
constant_exprt zero_expr() const
Return an expression representing the zero value of this type.
constant_exprt smallest_expr() const
Return an expression representing the smallest value of this type.
mp_integer smallest() const
Return the smallest value that can be represented using this type.
std::size_t get_size_t(const irep_idt &name) const
Definition irep.cpp:67
const irep_idt & get(const irep_idt &name) const
Definition irep.cpp:44
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
Fixed-width bit-vector with two's complement interpretation.
static void check(const typet &type, const validation_modet vm=validation_modet::INVARIANT)
signedbv_typet(const mp_integer &width)
signedbv_typet(std::size_t width)
The type of an expression, extends irept.
Definition type.h:29
Fixed-width bit-vector with unsigned binary interpretation.
unsignedbv_typet(const mp_integer &width)
unsignedbv_typet(std::size_t width)
static void check(const typet &type, const validation_modet vm=validation_modet::INVARIANT)
Templated functions to cast to specific exprt-derived classes.
BigInt mp_integer
Definition smt_terms.h:17
#define PRECONDITION(CONDITION)
Definition invariant.h:463
Defines typet, type_with_subtypet and type_with_subtypest.
#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