Skip to content

Commit 690d10b

Browse files
Merge pull request diffblue#1956 from romainbrenguier/refactor/prop_conv_straightforward2
Partial solver cleanup (straightforward changes part 2)
2 parents e8d26ae + d35c2ac commit 690d10b

File tree

7 files changed

+54
-135
lines changed

7 files changed

+54
-135
lines changed

src/solvers/flattening/boolbv.cpp

+30-70
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
253253
return convert_bitvector(expr.op0());
254254
}
255255
else if(expr.id()==ID_abs)
256-
return convert_abs(expr);
256+
return convert_abs(to_abs_expr(expr));
257257
else if(expr.id() == ID_bswap)
258258
return convert_bswap(to_bswap_expr(expr));
259259
else if(expr.id()==ID_byte_extract_little_endian ||
@@ -433,13 +433,7 @@ bvt boolbvt::convert_function_application(
433433

434434
literalt boolbvt::convert_rest(const exprt &expr)
435435
{
436-
if(expr.type().id()!=ID_bool)
437-
{
438-
error() << "boolbvt::convert_rest got non-boolean operand: "
439-
<< expr.pretty() << eom;
440-
throw 0;
441-
}
442-
436+
PRECONDITION(expr.type().id() == ID_bool);
443437
const exprt::operandst &operands=expr.operands();
444438

445439
if(expr.id()==ID_typecast)
@@ -451,9 +445,7 @@ literalt boolbvt::convert_rest(const exprt &expr)
451445
return convert_verilog_case_equality(to_binary_relation_expr(expr));
452446
else if(expr.id()==ID_notequal)
453447
{
454-
if(expr.operands().size()!=2)
455-
throw "notequal expects two operands";
456-
448+
DATA_INVARIANT(expr.operands().size() == 2, "notequal expects two operands");
457449
return !convert_equality(equal_exprt(expr.op0(), expr.op1()));
458450
}
459451
else if(expr.id()==ID_ieee_float_equal ||
@@ -480,57 +472,37 @@ literalt boolbvt::convert_rest(const exprt &expr)
480472
else if(expr.id()==ID_index)
481473
{
482474
bvt bv=convert_index(to_index_expr(expr));
483-
484-
if(bv.size()!=1)
485-
throw "convert_index returned non-bool bitvector";
486-
475+
CHECK_RETURN(bv.size() == 1);
487476
return bv[0];
488477
}
489478
else if(expr.id()==ID_member)
490479
{
491480
bvt bv=convert_member(to_member_expr(expr));
492-
493-
if(bv.size()!=1)
494-
throw "convert_member returned non-bool bitvector";
495-
481+
CHECK_RETURN(bv.size() == 1);
496482
return bv[0];
497483
}
498484
else if(expr.id()==ID_case)
499485
{
500486
bvt bv=convert_case(expr);
501-
502-
if(bv.size()!=1)
503-
throw "convert_case returned non-bool bitvector";
504-
487+
CHECK_RETURN(bv.size() == 1);
505488
return bv[0];
506489
}
507490
else if(expr.id()==ID_cond)
508491
{
509492
bvt bv=convert_cond(expr);
510-
511-
if(bv.size()!=1)
512-
throw "convert_cond returned non-bool bitvector";
513-
493+
CHECK_RETURN(bv.size() == 1);
514494
return bv[0];
515495
}
516496
else if(expr.id()==ID_sign)
517497
{
518-
if(expr.operands().size()!=1)
519-
throw "sign expects one operand";
520-
498+
DATA_INVARIANT(expr.operands().size() == 1, "sign expects one operand");
521499
const bvt &bv=convert_bv(operands[0]);
522-
523-
if(bv.empty())
524-
throw "sign operator takes one non-empty operand";
525-
526-
if(operands[0].type().id()==ID_signedbv)
500+
CHECK_RETURN(!bv.empty());
501+
const irep_idt type_id = operands[0].type().id();
502+
if(type_id == ID_signedbv || type_id == ID_fixedbv || type_id == ID_floatbv)
527503
return bv[bv.size()-1];
528-
else if(operands[0].type().id()==ID_unsignedbv)
504+
if(type_id == ID_unsignedbv)
529505
return const_literal(false);
530-
else if(operands[0].type().id()==ID_fixedbv)
531-
return bv[bv.size()-1];
532-
else if(operands[0].type().id()==ID_floatbv)
533-
return bv[bv.size()-1];
534506
}
535507
else if(expr.id()==ID_reduction_or || expr.id()==ID_reduction_and ||
536508
expr.id()==ID_reduction_nor || expr.id()==ID_reduction_nand ||
@@ -542,64 +514,53 @@ literalt boolbvt::convert_rest(const exprt &expr)
542514
return convert_overflow(expr);
543515
else if(expr.id()==ID_isnan)
544516
{
545-
if(expr.operands().size()!=1)
546-
throw "isnan expects one operand";
547-
517+
DATA_INVARIANT(expr.operands().size() == 1, "isnan expects one operand");
548518
const bvt &bv=convert_bv(operands[0]);
549519

550520
if(expr.op0().type().id()==ID_floatbv)
551521
{
552522
float_utilst float_utils(prop, to_floatbv_type(expr.op0().type()));
553523
return float_utils.is_NaN(bv);
554524
}
555-
else if(expr.op0().type().id()==ID_fixedbv)
525+
else if(expr.op0().type().id() == ID_fixedbv)
556526
return const_literal(false);
557527
}
558528
else if(expr.id()==ID_isfinite)
559529
{
560-
if(expr.operands().size()!=1)
561-
throw "isfinite expects one operand";
562-
530+
DATA_INVARIANT(expr.operands().size() == 1, "isfinite expects one operand");
563531
const bvt &bv=convert_bv(operands[0]);
564-
565532
if(expr.op0().type().id()==ID_floatbv)
566533
{
567534
float_utilst float_utils(prop, to_floatbv_type(expr.op0().type()));
568535
return prop.land(
569536
!float_utils.is_infinity(bv),
570537
!float_utils.is_NaN(bv));
571538
}
572-
else if(expr.op0().type().id()==ID_fixedbv)
539+
else if(expr.op0().type().id() == ID_fixedbv)
573540
return const_literal(true);
574541
}
575542
else if(expr.id()==ID_isinf)
576543
{
577-
if(expr.operands().size()!=1)
578-
throw "isinf expects one operand";
579-
544+
DATA_INVARIANT(expr.operands().size() == 1, "isinf expects one operand");
580545
const bvt &bv=convert_bv(operands[0]);
581-
582546
if(expr.op0().type().id()==ID_floatbv)
583547
{
584548
float_utilst float_utils(prop, to_floatbv_type(expr.op0().type()));
585549
return float_utils.is_infinity(bv);
586550
}
587-
else if(expr.op0().type().id()==ID_fixedbv)
551+
else if(expr.op0().type().id() == ID_fixedbv)
588552
return const_literal(false);
589553
}
590554
else if(expr.id()==ID_isnormal)
591555
{
592-
if(expr.operands().size()!=1)
593-
throw "isnormal expects one operand";
594-
595-
const bvt &bv=convert_bv(operands[0]);
596-
556+
DATA_INVARIANT(expr.operands().size() == 1, "isnormal expects one operand");
597557
if(expr.op0().type().id()==ID_floatbv)
598558
{
559+
const bvt &bv = convert_bv(operands[0]);
599560
float_utilst float_utils(prop, to_floatbv_type(expr.op0().type()));
600561
return float_utils.is_normal(bv);
601562
}
602-
else if(expr.op0().type().id()==ID_fixedbv)
563+
else if(expr.op0().type().id() == ID_fixedbv)
603564
return const_literal(true);
604565
}
605566

@@ -699,17 +660,16 @@ void boolbvt::print_assignment(std::ostream &out) const
699660
out << pair.first << "=" << pair.second.get_value(prop) << '\n';
700661
}
701662

702-
void boolbvt::build_offset_map(const struct_typet &src, offset_mapt &dest)
663+
boolbvt::offset_mapt boolbvt::build_offset_map(const struct_typet &src)
703664
{
704-
const struct_typet::componentst &components=
705-
src.components();
706-
707-
dest.resize(components.size());
708-
std::size_t offset=0;
709-
for(std::size_t i=0; i<components.size(); i++)
665+
const struct_typet::componentst &components = src.components();
666+
offset_mapt dest;
667+
dest.reserve(components.size());
668+
std::size_t offset = 0;
669+
for(const auto &comp : components)
710670
{
711-
std::size_t comp_width=boolbv_width(components[i].type());
712-
dest[i]=offset;
713-
offset+=comp_width;
671+
dest.push_back(offset);
672+
offset += boolbv_width(comp.type());
714673
}
674+
return dest;
715675
}

src/solvers/flattening/boolbv.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class boolbvt:public arrayst
163163
virtual bvt convert_shift(const binary_exprt &expr);
164164
virtual bvt convert_bitwise(const exprt &expr);
165165
virtual bvt convert_unary_minus(const unary_exprt &expr);
166-
virtual bvt convert_abs(const exprt &expr);
166+
virtual bvt convert_abs(const abs_exprt &expr);
167167
virtual bvt convert_concatenation(const exprt &expr);
168168
virtual bvt convert_replication(const replication_exprt &expr);
169169
virtual bvt convert_bv_literals(const exprt &expr);
@@ -250,7 +250,7 @@ class boolbvt:public arrayst
250250
void post_process_quantifiers();
251251

252252
typedef std::vector<std::size_t> offset_mapt;
253-
void build_offset_map(const struct_typet &src, offset_mapt &dest);
253+
offset_mapt build_offset_map(const struct_typet &src);
254254

255255
// strings
256256
numbering<irep_idt> string_numbering;

src/solvers/flattening/boolbv_abs.cpp

+5-12
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,19 @@ Author: Daniel Kroening, [email protected]
1414

1515
#include <solvers/floatbv/float_utils.h>
1616

17-
bvt boolbvt::convert_abs(const exprt &expr)
17+
bvt boolbvt::convert_abs(const abs_exprt &expr)
1818
{
19-
std::size_t width=boolbv_width(expr.type());
19+
const std::size_t width = boolbv_width(expr.type());
2020

2121
if(width==0)
2222
return conversion_failed(expr);
2323

24-
const exprt::operandst &operands=expr.operands();
24+
const bvt &op_bv=convert_bv(expr.op());
2525

26-
if(operands.size()!=1)
27-
throw "abs takes one operand";
28-
29-
const exprt &op0=expr.op0();
30-
31-
const bvt &op_bv=convert_bv(op0);
32-
33-
if(op0.type()!=expr.type())
26+
if(expr.op().type()!=expr.type())
3427
return conversion_failed(expr);
3528

36-
bvtypet bvtype=get_bvtype(expr.type());
29+
const bvtypet bvtype = get_bvtype(expr.type());
3730

3831
if(bvtype==bvtypet::IS_FIXED ||
3932
bvtype==bvtypet::IS_SIGNED ||

src/solvers/flattening/boolbv_bitwise.cpp

+6-15
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,16 @@ Author: Daniel Kroening, [email protected]
1111

1212
bvt boolbvt::convert_bitwise(const exprt &expr)
1313
{
14-
std::size_t width=boolbv_width(expr.type());
15-
14+
const std::size_t width = boolbv_width(expr.type());
1615
if(width==0)
1716
return conversion_failed(expr);
1817

1918
if(expr.id()==ID_bitnot)
2019
{
21-
if(expr.operands().size()!=1)
22-
throw "bitnot takes one operand";
23-
20+
DATA_INVARIANT(expr.operands().size() == 1, "bitnot takes one operand");
2421
const exprt &op0=expr.op0();
25-
2622
const bvt &op_bv=convert_bv(op0);
27-
28-
if(op_bv.size()!=width)
29-
throw "convert_bitwise: unexpected operand width";
30-
23+
CHECK_RETURN(op_bv.size() == width);
3124
return bv_utils.inverted(op_bv);
3225
}
3326
else if(expr.id()==ID_bitand || expr.id()==ID_bitor ||
@@ -41,9 +34,7 @@ bvt boolbvt::convert_bitwise(const exprt &expr)
4134
forall_operands(it, expr)
4235
{
4336
const bvt &op=convert_bv(*it);
44-
45-
if(op.size()!=width)
46-
throw "convert_bitwise: unexpected operand width";
37+
CHECK_RETURN(op.size() == width);
4738

4839
if(it==expr.operands().begin())
4940
bv=op;
@@ -64,13 +55,13 @@ bvt boolbvt::convert_bitwise(const exprt &expr)
6455
else if(expr.id()==ID_bitxnor)
6556
bv[i]=prop.lequal(bv[i], op[i]);
6657
else
67-
throw "unexpected operand";
58+
UNIMPLEMENTED;
6859
}
6960
}
7061
}
7162

7263
return bv;
7364
}
7465

75-
throw "unexpected bitwise operand";
66+
UNIMPLEMENTED;
7667
}

src/solvers/flattening/boolbv_byte_extract.cpp

+9-26
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,28 @@ Author: Daniel Kroening, [email protected]
2323

2424
bvt map_bv(const bv_endianness_mapt &map, const bvt &src)
2525
{
26-
assert(map.number_of_bits()==src.size());
27-
26+
PRECONDITION(map.number_of_bits() == src.size());
2827
bvt result;
29-
result.resize(src.size(), const_literal(false));
28+
result.reserve(src.size());
3029

3130
for(std::size_t i=0; i<src.size(); i++)
3231
{
33-
size_t mapped_index=map.map_bit(i);
34-
assert(mapped_index<src.size());
35-
result[i]=src[mapped_index];
32+
const size_t mapped_index = map.map_bit(i);
33+
CHECK_RETURN(mapped_index < src.size());
34+
result.push_back(src[mapped_index]);
3635
}
3736

3837
return result;
3938
}
4039

4140
bvt boolbvt::convert_byte_extract(const byte_extract_exprt &expr)
4241
{
43-
if(expr.operands().size()!=2)
44-
throw "byte_extract takes two operands";
45-
4642
// if we extract from an unbounded array, call the flattening code
4743
if(is_unbounded_array(expr.op().type()))
4844
{
4945
try
5046
{
51-
exprt tmp = flatten_byte_extract(expr, ns);
52-
return convert_bv(tmp);
47+
return convert_bv(flatten_byte_extract(expr, ns));
5348
}
5449
catch(const flatten_byte_extract_exceptiont &byte_extract_flatten_exception)
5550
{
@@ -58,7 +53,7 @@ bvt boolbvt::convert_byte_extract(const byte_extract_exprt &expr)
5853
}
5954
}
6055

61-
std::size_t width=boolbv_width(expr.type());
56+
const std::size_t width = boolbv_width(expr.type());
6257

6358
// special treatment for bit-fields and big-endian:
6459
// we need byte granularity
@@ -105,22 +100,10 @@ bvt boolbvt::convert_byte_extract(const byte_extract_exprt &expr)
105100

106101
const exprt &op=expr.op();
107102
const exprt &offset=expr.offset();
108-
109-
bool little_endian;
110-
111-
if(expr.id()==ID_byte_extract_little_endian)
112-
little_endian=true;
113-
else if(expr.id()==ID_byte_extract_big_endian)
114-
little_endian=false;
115-
else
116-
{
117-
little_endian=false;
118-
assert(false);
119-
}
103+
const bool little_endian = expr.id() == ID_byte_extract_little_endian;
120104

121105
// first do op0
122-
123-
bv_endianness_mapt op_map(op.type(), little_endian, ns, boolbv_width);
106+
const bv_endianness_mapt op_map(op.type(), little_endian, ns, boolbv_width);
124107
const bvt op_bv=map_bv(op_map, convert_bv(op));
125108

126109
// do result

src/solvers/flattening/boolbv_index.cpp

-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ Author: Daniel Kroening, [email protected]
1717

1818
bvt boolbvt::convert_index(const index_exprt &expr)
1919
{
20-
if(expr.id()!=ID_index)
21-
throw "expected index expression";
22-
23-
if(expr.operands().size()!=2)
24-
throw "index takes two operands";
25-
2620
const exprt &array=expr.array();
2721
const exprt &index=expr.index();
2822

0 commit comments

Comments
 (0)