Skip to content

Commit e94bdbb

Browse files
committed
Store pointer encoding configuration in boolbv_widtht
Rather than handling the object/offset bit width in multiple places, make boolbv_widtht the only place to provide this information.
1 parent 23c11e8 commit e94bdbb

File tree

4 files changed

+73
-18
lines changed

4 files changed

+73
-18
lines changed

src/solvers/flattening/boolbv_width.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ Author: Daniel Kroening, [email protected]
1111
#include <algorithm>
1212

1313
#include <util/arith_tools.h>
14+
#include <util/config.h>
1415
#include <util/exception_utils.h>
1516
#include <util/invariant.h>
17+
#include <util/namespace.h>
1618
#include <util/std_types.h>
1719

1820
boolbv_widtht::boolbv_widtht(const namespacet &_ns):ns(_ns)
@@ -23,6 +25,24 @@ boolbv_widtht::~boolbv_widtht()
2325
{
2426
}
2527

28+
std::size_t boolbv_widtht::get_object_width(const pointer_typet &type) const
29+
{
30+
return config.bv_encoding.object_bits;
31+
}
32+
33+
std::size_t boolbv_widtht::get_offset_width(const pointer_typet &type) const
34+
{
35+
const std::size_t pointer_width = type.get_width();
36+
const std::size_t object_width = get_object_width(type);
37+
PRECONDITION(pointer_width >= object_width);
38+
return pointer_width - object_width;
39+
}
40+
41+
std::size_t boolbv_widtht::get_address_width(const pointer_typet &type) const
42+
{
43+
return type.get_width();
44+
}
45+
2646
const boolbv_widtht::entryt &boolbv_widtht::get_entry(const typet &type) const
2747
{
2848
// check cache first
@@ -182,7 +202,11 @@ const boolbv_widtht::entryt &boolbv_widtht::get_entry(const typet &type) const
182202
CHECK_RETURN(entry.total_width > 0);
183203
}
184204
else if(type_id==ID_pointer)
185-
entry.total_width = type_checked_cast<pointer_typet>(type).get_width();
205+
{
206+
entry.total_width =
207+
get_address_width(type_checked_cast<pointer_typet>(type));
208+
DATA_INVARIANT(entry.total_width != 0, "pointer must have width");
209+
}
186210
else if(type_id==ID_struct_tag)
187211
entry=get_entry(ns.follow_tag(to_struct_tag_type(type)));
188212
else if(type_id==ID_union_tag)

src/solvers/flattening/boolbv_width.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Author: Daniel Kroening, [email protected]
1111
#define CPROVER_SOLVERS_FLATTENING_BOOLBV_WIDTH_H
1212

1313
#include <util/std_types.h>
14-
#include <util/namespace.h>
14+
15+
class namespacet;
1516

1617
class boolbv_widtht
1718
{
@@ -33,6 +34,10 @@ class boolbv_widtht
3334
const struct_typet &type,
3435
const irep_idt &member) const;
3536

37+
std::size_t get_object_width(const pointer_typet &type) const;
38+
std::size_t get_offset_width(const pointer_typet &type) const;
39+
std::size_t get_address_width(const pointer_typet &type) const;
40+
3641
protected:
3742
const namespacet &ns;
3843

src/solvers/flattening/bv_pointers.cpp

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ Author: Daniel Kroening, [email protected]
1111
#include <util/arith_tools.h>
1212
#include <util/bitvector_expr.h>
1313
#include <util/c_types.h>
14-
#include <util/config.h>
1514
#include <util/exception_utils.h>
1615
#include <util/pointer_expr.h>
1716
#include <util/pointer_offset_size.h>
@@ -34,6 +33,10 @@ literalt bv_pointerst::convert_rest(const exprt &expr)
3433
bvt invalid_bv;
3534
encode(pointer_logic.get_invalid_object(), invalid_bv);
3635

36+
const pointer_typet &type = to_pointer_type(operands[0].type());
37+
const std::size_t object_bits = boolbv_width.get_object_width(type);
38+
const std::size_t offset_bits = boolbv_width.get_offset_width(type);
39+
3740
bvt equal_invalid_bv;
3841
equal_invalid_bv.resize(object_bits);
3942

@@ -89,10 +92,6 @@ bv_pointerst::bv_pointerst(
8992
: boolbvt(_ns, _prop, message_handler, get_array_constraints),
9093
pointer_logic(_ns)
9194
{
92-
object_bits=config.bv_encoding.object_bits;
93-
std::size_t pointer_width = boolbv_width(pointer_type(empty_typet()));
94-
offset_bits=pointer_width-object_bits;
95-
bits=pointer_width;
9695
}
9796

9897
bool bv_pointerst::convert_address_of_rec(
@@ -121,6 +120,8 @@ bool bv_pointerst::convert_address_of_rec(
121120
const exprt &index=index_expr.index();
122121
const typet &array_type = array.type();
123122

123+
const std::size_t bits = boolbv_width(pointer_type(empty_typet()));
124+
124125
// recursive call
125126
if(array_type.id()==ID_pointer)
126127
{
@@ -155,6 +156,7 @@ bool bv_pointerst::convert_address_of_rec(
155156
if(convert_address_of_rec(byte_extract_expr.op(), bv))
156157
return true;
157158

159+
const std::size_t bits = boolbv_width(pointer_type(empty_typet()));
158160
CHECK_RETURN(bv.size()==bits);
159161

160162
offset_arithmetic(bv, 1, byte_extract_expr.offset());
@@ -223,8 +225,7 @@ bvt bv_pointerst::convert_pointer_type(const exprt &expr)
223225
{
224226
PRECONDITION(expr.type().id() == ID_pointer);
225227

226-
// make sure the config hasn't been changed
227-
PRECONDITION(bits==boolbv_width(expr.type()));
228+
const std::size_t bits = boolbv_width(expr.type());
228229

229230
if(expr.id()==ID_symbol)
230231
{
@@ -504,18 +505,19 @@ bvt bv_pointerst::convert_bitvector(const exprt &expr)
504505
expr.id() == ID_pointer_offset &&
505506
to_unary_expr(expr).op().type().id() == ID_pointer)
506507
{
507-
bvt op0 = convert_bv(to_unary_expr(expr).op());
508+
const exprt &op0 = to_unary_expr(expr).op();
509+
bvt op0_bv = convert_bv(op0);
508510

509511
std::size_t width=boolbv_width(expr.type());
510512

511513
if(width==0)
512514
return conversion_failed(expr);
513515

514516
// we need to strip off the object part
515-
op0.resize(offset_bits);
517+
op0_bv.resize(boolbv_width.get_offset_width(to_pointer_type(op0.type())));
516518

517519
// we do a sign extension to permit negative offsets
518-
return bv_utils.sign_extension(op0, width);
520+
return bv_utils.sign_extension(op0_bv, width);
519521
}
520522
else if(
521523
expr.id() == ID_object_size &&
@@ -542,7 +544,8 @@ bvt bv_pointerst::convert_bitvector(const exprt &expr)
542544
expr.id() == ID_pointer_object &&
543545
to_unary_expr(expr).op().type().id() == ID_pointer)
544546
{
545-
bvt op0 = convert_bv(to_unary_expr(expr).op());
547+
const exprt &op0 = to_unary_expr(expr).op();
548+
bvt op0_bv = convert_bv(op0);
546549

547550
std::size_t width=boolbv_width(expr.type());
548551

@@ -551,9 +554,12 @@ bvt bv_pointerst::convert_bitvector(const exprt &expr)
551554

552555
// erase offset bits
553556

554-
op0.erase(op0.begin()+0, op0.begin()+offset_bits);
557+
op0_bv.erase(
558+
op0_bv.begin() + 0,
559+
op0_bv.begin() +
560+
boolbv_width.get_offset_width(to_pointer_type(op0.type())));
555561

556-
return bv_utils.zero_extension(op0, width);
562+
return bv_utils.zero_extension(op0_bv, width);
557563
}
558564
else if(
559565
expr.id() == ID_typecast &&
@@ -586,6 +592,9 @@ exprt bv_pointerst::bv_get_rec(
586592

587593
std::string value_addr, value_offset, value;
588594

595+
const std::size_t bits = boolbv_width(to_pointer_type(type));
596+
const std::size_t offset_bits =
597+
boolbv_width.get_offset_width(to_pointer_type(type));
589598
for(std::size_t i=0; i<bits; i++)
590599
{
591600
char ch=0;
@@ -631,7 +640,11 @@ exprt bv_pointerst::bv_get_rec(
631640

632641
void bv_pointerst::encode(std::size_t addr, bvt &bv)
633642
{
634-
bv.resize(bits);
643+
const pointer_typet type = pointer_type(empty_typet());
644+
const std::size_t offset_bits = boolbv_width.get_offset_width(type);
645+
const std::size_t object_bits = boolbv_width.get_object_width(type);
646+
647+
bv.resize(boolbv_width(type));
635648

636649
// set offset to zero
637650
for(std::size_t i=0; i<offset_bits; i++)
@@ -644,6 +657,9 @@ void bv_pointerst::encode(std::size_t addr, bvt &bv)
644657

645658
void bv_pointerst::offset_arithmetic(bvt &bv, const mp_integer &x)
646659
{
660+
const pointer_typet type = pointer_type(empty_typet());
661+
const std::size_t offset_bits = boolbv_width.get_offset_width(type);
662+
647663
bvt bv1=bv;
648664
bv1.resize(offset_bits); // strip down
649665

@@ -667,6 +683,8 @@ void bv_pointerst::offset_arithmetic(
667683
index.type().id()==ID_signedbv?bv_utilst::representationt::SIGNED:
668684
bv_utilst::representationt::UNSIGNED;
669685

686+
const pointer_typet type = pointer_type(empty_typet());
687+
const std::size_t offset_bits = boolbv_width.get_offset_width(type);
670688
bv_index=bv_utils.extension(bv_index, offset_bits, rep);
671689

672690
offset_arithmetic(bv, factor, bv_index);
@@ -692,6 +710,8 @@ void bv_pointerst::offset_arithmetic(
692710
bvt bv_tmp=bv_utils.add(bv, bv_index);
693711

694712
// copy lower parts of result
713+
const pointer_typet type = pointer_type(empty_typet());
714+
const std::size_t offset_bits = boolbv_width.get_offset_width(type);
695715
for(std::size_t i=0; i<offset_bits; i++)
696716
bv[i]=bv_tmp[i];
697717
}
@@ -700,6 +720,8 @@ void bv_pointerst::add_addr(const exprt &expr, bvt &bv)
700720
{
701721
std::size_t a=pointer_logic.add_object(expr);
702722

723+
const pointer_typet type = pointer_type(expr.type());
724+
const std::size_t object_bits = boolbv_width.get_object_width(type);
703725
const std::size_t max_objects=std::size_t(1)<<object_bits;
704726

705727
if(a==max_objects)
@@ -717,6 +739,9 @@ void bv_pointerst::do_postponed(
717739
{
718740
if(postponed.expr.id() == ID_is_dynamic_object)
719741
{
742+
const std::size_t offset_bits = boolbv_width.get_offset_width(
743+
to_pointer_type(to_unary_expr(postponed.expr).op().type()));
744+
720745
const auto &objects = pointer_logic.objects;
721746
std::size_t number=0;
722747

@@ -749,6 +774,9 @@ void bv_pointerst::do_postponed(
749774
}
750775
else if(postponed.expr.id()==ID_object_size)
751776
{
777+
const std::size_t offset_bits = boolbv_width.get_offset_width(
778+
to_pointer_type(to_unary_expr(postponed.expr).op().type()));
779+
752780
const auto &objects = pointer_logic.objects;
753781
std::size_t number=0;
754782

src/solvers/flattening/bv_pointers.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ class bv_pointerst:public boolbvt
3131
// NOLINTNEXTLINE(readability/identifiers)
3232
typedef boolbvt SUB;
3333

34-
unsigned object_bits, offset_bits, bits;
35-
3634
void encode(std::size_t object, bvt &bv);
3735

3836
virtual bvt convert_pointer_type(const exprt &expr);

0 commit comments

Comments
 (0)