Skip to content

Commit 1926470

Browse files
committed
Add SMT bit vector theory concatenation
1 parent e831f43 commit 1926470

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/solvers/smt2_incremental/smt_bit_vector_theory.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,34 @@
44

55
#include <util/invariant.h>
66

7+
const char *smt_bit_vector_theoryt::concatt::identifier()
8+
{
9+
return "concat";
10+
}
11+
12+
smt_sortt smt_bit_vector_theoryt::concatt::return_sort(
13+
const smt_termt &lhs,
14+
const smt_termt &rhs)
15+
{
16+
const auto get_width = [](const smt_termt &term) {
17+
return term.get_sort().cast<smt_bit_vector_sortt>()->bit_width();
18+
};
19+
return smt_bit_vector_sortt{get_width(lhs) + get_width(rhs)};
20+
}
21+
22+
void smt_bit_vector_theoryt::concatt::validate(
23+
const smt_termt &lhs,
24+
const smt_termt &rhs)
25+
{
26+
const auto lhs_sort = lhs.get_sort().cast<smt_bit_vector_sortt>();
27+
INVARIANT(lhs_sort, "Left operand must have bitvector sort.");
28+
const auto rhs_sort = rhs.get_sort().cast<smt_bit_vector_sortt>();
29+
INVARIANT(rhs_sort, "Right operand must have bitvector sort.");
30+
}
31+
32+
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::concatt>
33+
smt_bit_vector_theoryt::concat{};
34+
735
const char *smt_bit_vector_theoryt::extractt::identifier()
836
{
937
return "extract";

src/solvers/smt2_incremental/smt_bit_vector_theory.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
class smt_bit_vector_theoryt
99
{
1010
public:
11+
struct concatt final
12+
{
13+
static const char *identifier();
14+
static smt_sortt return_sort(const smt_termt &lhs, const smt_termt &rhs);
15+
static void validate(const smt_termt &lhs, const smt_termt &rhs);
16+
};
17+
static const smt_function_application_termt::factoryt<concatt> concat;
18+
1119
struct extractt final
1220
{
1321
std::size_t i;

unit/solvers/smt2_incremental/smt_bit_vector_theory.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,32 @@
77

88
#include <util/mp_arith.h>
99

10+
TEST_CASE("SMT bit vector concatenation", "[core][smt2_incremental]")
11+
{
12+
const smt_bit_vector_constant_termt a_valid{42, 8}, b_valid{42, 16};
13+
SECTION("Valid operands")
14+
{
15+
const auto concat = smt_bit_vector_theoryt::concat(a_valid, b_valid);
16+
const auto expected_return_sort = smt_bit_vector_sortt{24};
17+
REQUIRE(
18+
concat.function_identifier() ==
19+
smt_identifier_termt("concat", expected_return_sort));
20+
REQUIRE(concat.get_sort() == expected_return_sort);
21+
REQUIRE(concat.arguments().size() == 2);
22+
REQUIRE(concat.arguments()[0].get() == a_valid);
23+
REQUIRE(concat.arguments()[1].get() == b_valid);
24+
}
25+
SECTION("Invalid operands")
26+
{
27+
const smt_bool_literal_termt false_term{false};
28+
const smt_bool_literal_termt true_term{true};
29+
cbmc_invariants_should_throwt invariants_throw;
30+
CHECK_THROWS(smt_bit_vector_theoryt::concat(a_valid, false_term));
31+
CHECK_THROWS(smt_bit_vector_theoryt::concat(false_term, a_valid));
32+
CHECK_THROWS(smt_bit_vector_theoryt::concat(false_term, true_term));
33+
}
34+
}
35+
1036
TEST_CASE("SMT bit vector extract", "[core][smt2_incremental]")
1137
{
1238
const smt_bit_vector_constant_termt operand{42, 8};

0 commit comments

Comments
 (0)