Skip to content

Commit 2064e37

Browse files
committed
introduce literal_vector_exprt
This introduces an expression, analogously to literal_exprt, which holds a vector of literals. This enables an implementation of boolbvt::handle, which returns the literal_vector_exprt for the given bit-vector typed expression.
1 parent 8ff5619 commit 2064e37

File tree

4 files changed

+96
-2
lines changed

4 files changed

+96
-2
lines changed

src/solvers/flattening/boolbv.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ Author: Daniel Kroening, [email protected]
88

99
#include "boolbv.h"
1010

11-
#include <algorithm>
12-
1311
#include <util/arith_tools.h>
1412
#include <util/bitvector_expr.h>
1513
#include <util/bitvector_types.h>
@@ -24,6 +22,10 @@ Author: Daniel Kroening, [email protected]
2422

2523
#include <solvers/floatbv/float_utils.h>
2624

25+
#include "literal_vector_expr.h"
26+
27+
#include <algorithm>
28+
2729
endianness_mapt boolbvt::endianness_map(const typet &type) const
2830
{
2931
const bool little_endian =
@@ -78,6 +80,15 @@ const bvt &boolbvt::convert_bv(
7880
return cache_entry;
7981
}
8082

83+
exprt boolbvt::handle(const exprt &expr)
84+
{
85+
if(expr.type().id() == ID_bool)
86+
return prop_conv_solvert::handle(expr);
87+
auto bv = convert_bv(expr);
88+
set_frozen(bv); // for incremental usage
89+
return literal_vector_exprt{bv, expr.type()};
90+
}
91+
8192
/// Print that the expression of x has failed conversion,
8293
/// then return a vector of x's width.
8394
bvt boolbvt::conversion_failed(const exprt &expr)
@@ -242,6 +253,8 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
242253
}
243254
else if(expr.id() == ID_find_first_set)
244255
return convert_bv(simplify_expr(to_find_first_set_expr(expr).lower(), ns));
256+
else if(expr.id() == ID_literal_vector)
257+
return to_literal_vector_expr(expr).bv();
245258

246259
return conversion_failed(expr);
247260
}

src/solvers/flattening/boolbv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class boolbvt:public arrayst
6969
exprt get(const exprt &expr) const override;
7070
void set_to(const exprt &expr, bool value) override;
7171
void print_assignment(std::ostream &out) const override;
72+
exprt handle(const exprt &) override;
7273

7374
void clear_cache() override
7475
{
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*******************************************************************\
2+
3+
Module:
4+
5+
Author: Daniel Kroening, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_SOLVERS_FLATTENING_LITERAL_VECTOR_EXPR_H
10+
#define CPROVER_SOLVERS_FLATTENING_LITERAL_VECTOR_EXPR_H
11+
12+
#include <util/std_expr.h>
13+
#include <util/string2int.h>
14+
15+
#include <solvers/prop/literal.h>
16+
17+
class literal_vector_exprt : public nullary_exprt
18+
{
19+
public:
20+
literal_vector_exprt(const bvt &__bv, typet __type)
21+
: nullary_exprt(ID_literal_vector, std::move(__type))
22+
{
23+
bv(__bv);
24+
}
25+
26+
bvt bv() const
27+
{
28+
auto &sub = find(ID_literals).get_sub();
29+
bvt result;
30+
result.reserve(sub.size());
31+
for(auto &literal_irep : sub)
32+
{
33+
literalt l;
34+
l.set(literalt::var_not(
35+
unsafe_string2signedlonglong(literal_irep.id_string())));
36+
result.push_back(l);
37+
}
38+
return result;
39+
}
40+
41+
void bv(const bvt &bv)
42+
{
43+
auto &sub = add(ID_literals).get_sub();
44+
sub.clear();
45+
sub.reserve(bv.size());
46+
for(auto &literal : bv)
47+
sub.emplace_back(irept{std::to_string(literal.get())});
48+
}
49+
};
50+
51+
template <>
52+
inline bool can_cast_expr<literal_vector_exprt>(const exprt &base)
53+
{
54+
return base.id() == ID_literal_vector;
55+
}
56+
57+
/// Cast a generic exprt to a \ref literal_vector_exprt. This is an unchecked
58+
/// conversion. \a expr must be known to be \ref literal_vector_exprt.
59+
/// \param expr: Source expression
60+
/// \return Object of type \ref literal_vector_exprt
61+
/// \ingroup gr_std_expr
62+
inline const literal_vector_exprt &to_literal_vector_expr(const exprt &expr)
63+
{
64+
PRECONDITION(expr.id() == ID_literal_vector);
65+
literal_vector_exprt::check(expr);
66+
return static_cast<const literal_vector_exprt &>(expr);
67+
}
68+
69+
/// \copydoc to_literal_expr(const exprt &)
70+
/// \ingroup gr_std_expr
71+
inline literal_vector_exprt &to_literal_vector_expr(exprt &expr)
72+
{
73+
PRECONDITION(expr.id() == ID_literal_vector);
74+
literal_vector_exprt::check(expr);
75+
return static_cast<literal_vector_exprt &>(expr);
76+
}
77+
78+
#endif // CPROVER_SOLVERS_FLATTENING_LITERAL_VECTOR_EXPR_H

src/util/irep_ids.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,8 @@ IREP_ID_ONE(is_static_assert)
407407
IREP_ID_ONE(is_virtual)
408408
IREP_ID_TWO(C_is_virtual, #is_virtual)
409409
IREP_ID_ONE(literal)
410+
IREP_ID_ONE(literal_vector)
411+
IREP_ID_ONE(literals)
410412
IREP_ID_ONE(member_initializers)
411413
IREP_ID_ONE(member_initializer)
412414
IREP_ID_ONE(method_qualifier)

0 commit comments

Comments
 (0)