Skip to content

Commit 498f3b5

Browse files
NlightNFotisEnrico Steffinlongo
and
Enrico Steffinlongo
committed
Add an Array sort in the new SMT backend, along with unit tests for the sort and its printing.
Co-authored-by: Fotis Koutoulakis <[email protected]> Co-authored-by: Enrico Steffinlongo <[email protected]>
1 parent e78eacf commit 498f3b5

File tree

7 files changed

+122
-0
lines changed

7 files changed

+122
-0
lines changed

src/solvers/smt2_incremental/convert_expr_to_smt.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ static smt_sortt convert_type_to_smt_sort(const bitvector_typet &type)
8989
return smt_bit_vector_sortt{type.get_width()};
9090
}
9191

92+
static smt_sortt convert_type_to_smt_sort(const array_typet &type)
93+
{
94+
UNIMPLEMENTED_FEATURE(
95+
"Conversion of array type to smt sort " + type.pretty());
96+
}
97+
9298
smt_sortt convert_type_to_smt_sort(const typet &type)
9399
{
94100
if(const auto bool_type = type_try_dynamic_cast<bool_typet>(type))
@@ -99,6 +105,10 @@ smt_sortt convert_type_to_smt_sort(const typet &type)
99105
{
100106
return convert_type_to_smt_sort(*bitvector_type);
101107
}
108+
if(const auto array_type = type_try_dynamic_cast<array_typet>(type))
109+
{
110+
return convert_type_to_smt_sort(*array_type);
111+
}
102112
UNIMPLEMENTED_FEATURE("Generation of SMT formula for type: " + type.pretty());
103113
}
104114

@@ -213,6 +223,13 @@ struct sort_based_cast_to_bit_vector_convertert final
213223
"Generation of SMT formula for type cast to bit vector from type: " +
214224
from_type.pretty());
215225
}
226+
227+
void visit(const smt_array_sortt &) override
228+
{
229+
UNIMPLEMENTED_FEATURE(
230+
"Generation of SMT formula for type cast to bit vector from type: " +
231+
from_type.pretty());
232+
}
216233
};
217234

218235
static smt_termt convert_bit_vector_cast(
@@ -294,6 +311,12 @@ struct sort_based_literal_convertert : public smt_sort_const_downcast_visitort
294311
const auto value = bvrep2integer(member_input.get_value(), width, false);
295312
result = smt_bit_vector_constant_termt{value, bit_vector_sort};
296313
}
314+
315+
void visit(const smt_array_sortt &array_sort) override
316+
{
317+
UNIMPLEMENTED_FEATURE(
318+
"Conversion of array SMT literal " + array_sort.pretty());
319+
}
297320
};
298321

299322
static smt_termt convert_expr_to_smt(const constant_exprt &constant_literal)

src/solvers/smt2_incremental/smt_sorts.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,29 @@ std::size_t smt_bit_vector_sortt::bit_width() const
6262
return get_size_t(ID_width);
6363
}
6464

65+
smt_array_sortt::smt_array_sortt(
66+
const smt_sortt &index_sort,
67+
const smt_sortt &element_sort)
68+
: smt_sortt{ID_smt_array_sort}
69+
{
70+
add(index_sort_label, index_sort);
71+
add(element_sort_label, element_sort);
72+
}
73+
74+
const smt_sortt &smt_array_sortt::get_index_sort() const
75+
{
76+
const irept &index_info = find(index_sort_label);
77+
const smt_sortt &index_sort = static_cast<const smt_sortt &>(index_info);
78+
return index_sort;
79+
}
80+
81+
const smt_sortt &smt_array_sortt::get_element_sort() const
82+
{
83+
const irept &element_info = find(element_sort_label);
84+
const smt_sortt &element_sort = static_cast<const smt_sortt &>(element_info);
85+
return element_sort;
86+
}
87+
6588
template <typename visitort>
6689
void accept(const smt_sortt &sort, const irep_idt &id, visitort &&visitor)
6790
{

src/solvers/smt2_incremental/smt_sorts.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
/// * The type of sort checks required to implement `smt_sortt::accept`.
99
SORT_ID(bool)
1010
SORT_ID(bit_vector)
11+
SORT_ID(array)

src/solvers/smt2_incremental/smt_sorts.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ class smt_bit_vector_sortt final : public smt_sortt
8787
std::size_t bit_width() const;
8888
};
8989

90+
class smt_array_sortt final : public smt_sortt
91+
{
92+
private:
93+
const std::string index_sort_label{"index_sort"};
94+
const std::string element_sort_label{"element_sort"};
95+
96+
public:
97+
explicit smt_array_sortt(
98+
const smt_sortt &index_sort,
99+
const smt_sortt &element_sort);
100+
const smt_sortt &get_index_sort() const;
101+
const smt_sortt &get_element_sort() const;
102+
};
103+
90104
class smt_sort_const_downcast_visitort
91105
{
92106
public:

src/solvers/smt2_incremental/smt_to_smt2_string.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ class smt_sort_output_visitort : public smt_sort_const_downcast_visitort
7676
{
7777
os << "(_ BitVec " << bit_vec.bit_width() << ")";
7878
}
79+
80+
void visit(const smt_array_sortt &array) override
81+
{
82+
os << "(Array " << smt_to_smt2_string(array.get_index_sort()) << " "
83+
<< smt_to_smt2_string(array.get_element_sort()) << ")";
84+
}
7985
};
8086

8187
std::ostream &operator<<(std::ostream &os, const smt_sortt &sort)

unit/solvers/smt2_incremental/smt_sorts.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ TEST_CASE("Visiting smt_bool_sortt.", "[core][smt2_incremental]")
3232
public:
3333
bool bool_visited = false;
3434
bool bit_vec_visited = false;
35+
bool array_visited = false;
3536

3637
void visit(const smt_bool_sortt &) override
3738
{
@@ -42,10 +43,16 @@ TEST_CASE("Visiting smt_bool_sortt.", "[core][smt2_incremental]")
4243
{
4344
bit_vec_visited = true;
4445
}
46+
47+
void visit(const smt_array_sortt &) override
48+
{
49+
array_visited = true;
50+
}
4551
} visitor;
4652
smt_bool_sortt{}.accept(visitor);
4753
REQUIRE(visitor.bool_visited);
4854
REQUIRE_FALSE(visitor.bit_vec_visited);
55+
REQUIRE_FALSE(visitor.array_visited);
4956
}
5057

5158
TEST_CASE("Visiting smt_bit_vec_sortt.", "[core][smt2_incremental]")
@@ -55,6 +62,7 @@ TEST_CASE("Visiting smt_bit_vec_sortt.", "[core][smt2_incremental]")
5562
public:
5663
bool bool_visited = false;
5764
bool bit_vec_visited = false;
65+
bool array_visited = false;
5866

5967
void visit(const smt_bool_sortt &) override
6068
{
@@ -66,10 +74,49 @@ TEST_CASE("Visiting smt_bit_vec_sortt.", "[core][smt2_incremental]")
6674
bit_vec_visited = true;
6775
REQUIRE(bit_vec.bit_width() == 8);
6876
}
77+
78+
void visit(const smt_array_sortt &) override
79+
{
80+
array_visited = true;
81+
}
6982
} visitor;
7083
smt_bit_vector_sortt{8}.accept(visitor);
7184
REQUIRE_FALSE(visitor.bool_visited);
7285
REQUIRE(visitor.bit_vec_visited);
86+
REQUIRE_FALSE(visitor.array_visited);
87+
}
88+
89+
TEST_CASE("Visiting smt_array_sort.", "[core][smt2_incremental]")
90+
{
91+
class : public smt_sort_const_downcast_visitort
92+
{
93+
public:
94+
bool bool_visited = false;
95+
bool bit_vec_visited = false;
96+
bool array_visited = false;
97+
98+
void visit(const smt_bool_sortt &) override
99+
{
100+
bool_visited = true;
101+
}
102+
103+
void visit(const smt_bit_vector_sortt &bit_vec) override
104+
{
105+
bit_vec_visited = true;
106+
REQUIRE(bit_vec.bit_width() == 8);
107+
}
108+
109+
void visit(const smt_array_sortt &) override
110+
{
111+
array_visited = true;
112+
}
113+
} visitor;
114+
115+
smt_array_sortt{smt_bool_sortt{}, smt_bool_sortt{}}.accept(visitor);
116+
117+
REQUIRE_FALSE(visitor.bool_visited);
118+
REQUIRE_FALSE(visitor.bit_vec_visited);
119+
REQUIRE(visitor.array_visited);
73120
}
74121

75122
TEST_CASE("smt_sortt equality", "[core][smt2_incremental]")
@@ -83,4 +130,9 @@ TEST_CASE("smt_sortt equality", "[core][smt2_incremental]")
83130
CHECK(bit_vector8 != bool_sort1);
84131
const smt_bit_vector_sortt bit_vector16{16};
85132
CHECK(bit_vector8 != bit_vector16);
133+
const smt_array_sortt array_sort{smt_bool_sortt{}, smt_bool_sortt{}};
134+
CHECK(array_sort != bool_sort1);
135+
CHECK(array_sort != bit_vector8);
136+
const smt_array_sortt array_sort_bv{bit_vector16, bit_vector16};
137+
CHECK(array_sort_bv != array_sort);
86138
}

unit/solvers/smt2_incremental/smt_to_smt2_string.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ TEST_CASE("Test smt_sortt to string conversion", "[core][smt2_incremental]")
2121
{
2222
CHECK(smt_to_smt2_string(smt_bool_sortt{}) == "Bool");
2323
CHECK(smt_to_smt2_string(smt_bit_vector_sortt{16}) == "(_ BitVec 16)");
24+
CHECK(
25+
smt_to_smt2_string(smt_array_sortt{smt_bool_sortt{}, smt_bool_sortt{}}) ==
26+
"(Array Bool Bool)");
2427
}
2528

2629
TEST_CASE(

0 commit comments

Comments
 (0)