Skip to content

Commit 5973f10

Browse files
authored
Merge pull request #4352 from antlechner/antonia/bv-min-max-refactor
Refactor code related to bitvector type min/max values
2 parents 2b2a62b + f313fdf commit 5973f10

File tree

5 files changed

+75
-74
lines changed

5 files changed

+75
-74
lines changed

jbmc/src/java_bytecode/java_object_factory.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -351,19 +351,6 @@ class recursion_set_entryt
351351
}
352352
};
353353

354-
/// Get max value for an integer type
355-
/// \param type:
356-
/// Type to find maximum value for
357-
/// \return Maximum integer value
358-
static mp_integer max_value(const typet &type)
359-
{
360-
if(type.id() == ID_signedbv)
361-
return to_signedbv_type(type).largest();
362-
else if(type.id() == ID_unsignedbv)
363-
return to_unsignedbv_type(type).largest();
364-
UNREACHABLE;
365-
}
366-
367354
/// Initialise length and data fields for a nondeterministic String structure.
368355
///
369356
/// If the structure is not a nondeterministic structure, the call results in
@@ -458,7 +445,9 @@ void initialize_nondet_string_fields(
458445
code.add(code_assumet(binary_relation_exprt(length_expr, ID_ge, min_length)));
459446

460447
// assume (length_expr <= max_input_length)
461-
if(max_nondet_string_length <= max_value(length_expr.type()))
448+
if(
449+
max_nondet_string_length <=
450+
to_integer_bitvector_type(length_expr.type()).largest())
462451
{
463452
exprt max_length =
464453
from_integer(max_nondet_string_length, length_expr.type());

src/ansi-c/ansi_c_entry_point.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -377,18 +377,8 @@ bool generate_ansi_c_start_function(
377377
}
378378

379379
// assume envp_size is INTMAX-1
380-
mp_integer max;
381-
382-
if(envp_size_symbol.type.id()==ID_signedbv)
383-
{
384-
max=to_signedbv_type(envp_size_symbol.type).largest();
385-
}
386-
else if(envp_size_symbol.type.id()==ID_unsignedbv)
387-
{
388-
max=to_unsignedbv_type(envp_size_symbol.type).largest();
389-
}
390-
else
391-
UNREACHABLE;
380+
const mp_integer max =
381+
to_integer_bitvector_type(envp_size_symbol.type).largest();
392382

393383
exprt max_minus_one=from_integer(max-1, envp_size_symbol.type);
394384

src/util/simplify_expr_int.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ Author: Daniel Kroening, [email protected]
1111
#include <cassert>
1212

1313
#include "arith_tools.h"
14-
#include "bv_arithmetic.h"
1514
#include "byte_operators.h"
1615
#include "config.h"
1716
#include "expr_util.h"
@@ -1879,8 +1878,7 @@ bool simplify_exprt::simplify_inequality_constant(exprt &expr)
18791878
if(expr.op0().type().id()==ID_unsignedbv ||
18801879
expr.op0().type().id()==ID_signedbv)
18811880
{
1882-
bv_spect spec(expr.op0().type());
1883-
mp_integer max(spec.max_value());
1881+
mp_integer max(to_integer_bitvector_type(expr.op0().type()).largest());
18841882

18851883
if(expr.id()==ID_notequal)
18861884
{

src/util/std_types.cpp

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Author: Daniel Kroening, [email protected]
1313
#include "std_types.h"
1414

1515
#include "arith_tools.h"
16+
#include "bv_arithmetic.h"
1617
#include "namespace.h"
1718
#include "std_expr.h"
1819
#include "string2int.h"
@@ -161,52 +162,27 @@ mp_integer range_typet::get_to() const
161162
return string2integer(get_string(ID_to));
162163
}
163164

164-
mp_integer signedbv_typet::smallest() const
165+
mp_integer integer_bitvector_typet::smallest() const
165166
{
166-
return -power(2, get_width()-1);
167+
return bv_spect(*this).min_value();
167168
}
168169

169-
mp_integer signedbv_typet::largest() const
170+
mp_integer integer_bitvector_typet::largest() const
170171
{
171-
return power(2, get_width()-1)-1;
172+
return bv_spect(*this).max_value();
172173
}
173174

174-
constant_exprt signedbv_typet::zero_expr() const
175+
constant_exprt integer_bitvector_typet::zero_expr() const
175176
{
176177
return from_integer(0, *this);
177178
}
178179

179-
constant_exprt signedbv_typet::smallest_expr() const
180+
constant_exprt integer_bitvector_typet::smallest_expr() const
180181
{
181182
return from_integer(smallest(), *this);
182183
}
183184

184-
constant_exprt signedbv_typet::largest_expr() const
185-
{
186-
return from_integer(largest(), *this);
187-
}
188-
189-
mp_integer unsignedbv_typet::smallest() const
190-
{
191-
return 0;
192-
}
193-
194-
mp_integer unsignedbv_typet::largest() const
195-
{
196-
return power(2, get_width())-1;
197-
}
198-
199-
constant_exprt unsignedbv_typet::zero_expr() const
200-
{
201-
return from_integer(0, *this);
202-
}
203-
204-
constant_exprt unsignedbv_typet::smallest_expr() const
205-
{
206-
return from_integer(smallest(), *this);
207-
}
208-
209-
constant_exprt unsignedbv_typet::largest_expr() const
185+
constant_exprt integer_bitvector_typet::largest_expr() const
210186
{
211187
return from_integer(largest(), *this);
212188
}

src/util/std_types.h

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,20 +1167,74 @@ inline bv_typet &to_bv_type(typet &type)
11671167
return static_cast<bv_typet &>(type);
11681168
}
11691169

1170-
/// Fixed-width bit-vector with unsigned binary interpretation
1171-
class unsignedbv_typet:public bitvector_typet
1170+
/// Fixed-width bit-vector representing a signed or unsigned integer
1171+
class integer_bitvector_typet : public bitvector_typet
11721172
{
11731173
public:
1174-
explicit unsignedbv_typet(std::size_t width):
1175-
bitvector_typet(ID_unsignedbv, width)
1174+
integer_bitvector_typet(const irep_idt &id, std::size_t width)
1175+
: bitvector_typet(id, width)
11761176
{
11771177
}
11781178

1179+
/// Return the smallest value that can be represented using this type.
11791180
mp_integer smallest() const;
1181+
/// Return the largest value that can be represented using this type.
11801182
mp_integer largest() const;
1183+
1184+
// If we ever need any of the following three methods in \ref fixedbv_typet or
1185+
// \ref floatbv_typet, we might want to move them to a new class
1186+
// numeric_bitvector_typet, which would be between integer_bitvector_typet and
1187+
// bitvector_typet in the hierarchy.
1188+
1189+
/// Return an expression representing the smallest value of this type.
11811190
constant_exprt smallest_expr() const;
1191+
/// Return an expression representing the zero value of this type.
11821192
constant_exprt zero_expr() const;
1193+
/// Return an expression representing the largest value of this type.
11831194
constant_exprt largest_expr() const;
1195+
};
1196+
1197+
/// Check whether a reference to a typet is an \ref integer_bitvector_typet.
1198+
/// \param type: Source type.
1199+
/// \return True if \p type is an \ref integer_bitvector_typet.
1200+
template <>
1201+
inline bool can_cast_type<integer_bitvector_typet>(const typet &type)
1202+
{
1203+
return type.id() == ID_signedbv || type.id() == ID_unsignedbv;
1204+
}
1205+
1206+
/// \brief Cast a typet to an \ref integer_bitvector_typet
1207+
///
1208+
/// This is an unchecked conversion. \a type must be known to be \ref
1209+
/// integer_bitvector_typet. Will fail with a precondition violation if type
1210+
/// doesn't match.
1211+
///
1212+
/// \param type: Source type.
1213+
/// \return Object of type \ref integer_bitvector_typet.
1214+
inline const integer_bitvector_typet &
1215+
to_integer_bitvector_type(const typet &type)
1216+
{
1217+
PRECONDITION(can_cast_type<integer_bitvector_typet>(type));
1218+
1219+
return static_cast<const integer_bitvector_typet &>(type);
1220+
}
1221+
1222+
/// \copydoc to_integer_bitvector_type(const typet &type)
1223+
inline integer_bitvector_typet &to_integer_bitvector_type(typet &type)
1224+
{
1225+
PRECONDITION(can_cast_type<integer_bitvector_typet>(type));
1226+
1227+
return static_cast<integer_bitvector_typet &>(type);
1228+
}
1229+
1230+
/// Fixed-width bit-vector with unsigned binary interpretation
1231+
class unsignedbv_typet : public integer_bitvector_typet
1232+
{
1233+
public:
1234+
explicit unsignedbv_typet(std::size_t width)
1235+
: integer_bitvector_typet(ID_unsignedbv, width)
1236+
{
1237+
}
11841238

11851239
static void check(
11861240
const typet &type,
@@ -1223,20 +1277,14 @@ inline unsignedbv_typet &to_unsignedbv_type(typet &type)
12231277
}
12241278

12251279
/// Fixed-width bit-vector with two's complement interpretation
1226-
class signedbv_typet:public bitvector_typet
1280+
class signedbv_typet : public integer_bitvector_typet
12271281
{
12281282
public:
1229-
explicit signedbv_typet(std::size_t width):
1230-
bitvector_typet(ID_signedbv, width)
1283+
explicit signedbv_typet(std::size_t width)
1284+
: integer_bitvector_typet(ID_signedbv, width)
12311285
{
12321286
}
12331287

1234-
mp_integer smallest() const;
1235-
mp_integer largest() const;
1236-
constant_exprt smallest_expr() const;
1237-
constant_exprt zero_expr() const;
1238-
constant_exprt largest_expr() const;
1239-
12401288
static void check(
12411289
const typet &type,
12421290
const validation_modet vm = validation_modet::INVARIANT)

0 commit comments

Comments
 (0)