SMTStabilizer API
Public API documentation for SMTStabilizer
Loading...
Searching...
No Matches
sort.cpp
Go to the documentation of this file.
1/* -*- Source -*-
2 *
3 * The Sort Class
4 *
5 * Author: Fuqi Jia <jiafq@ios.ac.cn>
6 *
7 * Copyright (C) 2025 Fuqi Jia
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 */
27// Modified by Xiang Zhang, 2026
28// Additional changes licensed under the MIT License
29#include "sort.h"
30
31namespace stabilizer::parser {
32
33// Static constant sorts are now defined inline in the header file
34
35// SortManager implementation
37
39
41 // Add static sorts to the manager
42 sorts.push_back(NULL_SORT);
43 sorts.push_back(UNKNOWN_SORT);
44 sorts.push_back(BOOL_SORT);
45 sorts.push_back(INT_SORT);
46 sorts.push_back(REAL_SORT);
47 sorts.push_back(ALGEBRAIC_SORT);
48 sorts.push_back(TRANSCENDENTAL_SORT);
49 sorts.push_back(STR_SORT);
50 sorts.push_back(REG_SORT);
51 sorts.push_back(EXT_SORT);
52 sorts.push_back(NAT_SORT);
53 sorts.push_back(RAND_SORT);
54 sorts.push_back(INTOREAL_SORT);
55 sorts.push_back(FLOAT64_SORT);
56 sorts.push_back(FLOAT32_SORT);
57 sorts.push_back(FLOAT16_SORT);
58 sorts.push_back(ROUNDING_MODE_SORT);
59
60 // Add them to the hash buckets for deduplication
61 for (size_t i = 0; i < sorts.size(); ++i) {
62 size_t hash_val = sorts[i]->hash();
63 sort_buckets[hash_val].push_back({sorts[i], i});
64 }
65}
66
67std::shared_ptr<Sort> SortManager::getSort(const size_t index) const {
68 if (index >= sorts.size()) {
69 return nullptr;
70 }
71 return sorts[index];
72}
73
74size_t SortManager::getIndex(const std::shared_ptr<Sort> &sort) const {
75 for (size_t i = 0; i < sorts.size(); ++i) {
76 if (sorts[i] == sort) {
77 return i;
78 }
79 }
80 return SIZE_MAX; // Not found
81}
82
83size_t SortManager::size() const { return sorts.size(); }
84
86 sorts.clear();
87 sort_buckets.clear();
88}
89
90std::shared_ptr<Sort>
91SortManager::insertSortToBucket(const std::shared_ptr<Sort> &sort) {
92 size_t hash_val = sort->hash();
93
94 // Check if an equivalent sort already exists
95 auto bucket_it = sort_buckets.find(hash_val);
96 if (bucket_it != sort_buckets.end()) {
97 for (const auto &pair : bucket_it->second) {
98 if (*pair.first == *sort) {
99 return pair.first; // Return existing sort
100 }
101 }
102 }
103
104 // Add new sort
105 size_t new_index = sorts.size();
106 sorts.push_back(sort);
107 sort_buckets[hash_val].push_back({sort, new_index});
108
109 return sort;
110}
111
112// createSort methods
113std::shared_ptr<Sort>
114SortManager::createSort(SORT_KIND kind, std::string name, size_t arity, std::vector<std::shared_ptr<Sort>> children) {
115 auto sort = std::make_shared<Sort>(kind, name, arity, children);
116 return insertSortToBucket(sort);
117}
118
119std::shared_ptr<Sort> SortManager::createSort(SORT_KIND kind, std::string name, size_t arity) {
120 auto sort = std::make_shared<Sort>(kind, name, arity);
121 return insertSortToBucket(sort);
122}
123
124std::shared_ptr<Sort> SortManager::createSort(SORT_KIND kind,
125 std::string name) {
126 auto sort = std::make_shared<Sort>(kind, name);
127 return insertSortToBucket(sort);
128}
129
130std::shared_ptr<Sort> SortManager::createSort(SORT_KIND kind) {
131 auto sort = std::make_shared<Sort>(kind);
132 return insertSortToBucket(sort);
133}
134
135std::shared_ptr<Sort> SortManager::createSort(std::string name) {
136 auto sort = std::make_shared<Sort>(name);
137 return insertSortToBucket(sort);
138}
139
140std::shared_ptr<Sort> SortManager::createSort() {
141 auto sort = std::make_shared<Sort>();
142 return insertSortToBucket(sort);
143}
144
145// Specific sort creation methods
146std::shared_ptr<Sort> SortManager::createBVSort(size_t width) {
147 auto width_sort = std::make_shared<Sort>(SORT_KIND::SK_NULL, "Width", width);
148 auto sort =
149 std::make_shared<Sort>(SORT_KIND::SK_BV, "BV", 0, std::vector<std::shared_ptr<Sort>>{width_sort});
150 return insertSortToBucket(sort);
151}
152
153std::shared_ptr<Sort> SortManager::createFPSort(size_t exp, size_t sig) {
154 auto exp_sort = std::make_shared<Sort>(SORT_KIND::SK_NULL, "Exp", exp);
155 auto sig_sort = std::make_shared<Sort>(SORT_KIND::SK_NULL, "Sig", sig);
156 auto sort = std::make_shared<Sort>(
157 SORT_KIND::SK_FP, "FP", 0, std::vector<std::shared_ptr<Sort>>{exp_sort, sig_sort});
158 return insertSortToBucket(sort);
159}
160
161std::shared_ptr<Sort> SortManager::createArraySort(std::shared_ptr<Sort> index,
162 std::shared_ptr<Sort> elem) {
163 auto sort =
164 std::make_shared<Sort>(SORT_KIND::SK_ARRAY, "Array", 0, std::vector<std::shared_ptr<Sort>>{index, elem});
165 return insertSortToBucket(sort);
166}
167
168std::shared_ptr<Sort>
169SortManager::createTupleSort(const std::vector<std::shared_ptr<Sort>> &fields) {
170 auto sort = std::make_shared<Sort>(SORT_KIND::SK_TUPLE, "Tuple", 0, fields);
171 return insertSortToBucket(sort);
172}
173
174std::shared_ptr<Sort> SortManager::createSortDec(const std::string &name,
175 size_t arity) {
176 auto sort = std::make_shared<Sort>(SORT_KIND::SK_DEC, name, arity);
177 return insertSortToBucket(sort);
178}
179
180std::shared_ptr<Sort>
181SortManager::createSortDef(const std::string &name,
182 const std::vector<std::shared_ptr<Sort>> &params,
183 std::shared_ptr<Sort> out_sort) {
184 std::vector<std::shared_ptr<Sort>> children;
185 children.insert(children.end(), params.begin(), params.end());
186 children.push_back(out_sort);
187 auto sort = std::make_shared<Sort>(SORT_KIND::SK_DEF, name, children.size() - 1, children);
188 return insertSortToBucket(sort);
189}
190
191size_t SortManager::getBitWidth(const std::shared_ptr<Sort> &sort) {
192 return sort->getBitWidth();
193}
194
195size_t SortManager::getExponentWidth(const std::shared_ptr<Sort> &sort) {
196 return sort->getExponentWidth();
197}
198
199size_t SortManager::getSignificandWidth(const std::shared_ptr<Sort> &sort) {
200 return sort->getSignificandWidth();
201}
202
203std::shared_ptr<Sort>
204SortManager::getIndexSort(const std::shared_ptr<Sort> &sort) {
205 return sort->getIndexSort();
206}
207
208std::shared_ptr<Sort>
209SortManager::getElemSort(const std::shared_ptr<Sort> &sort) {
210 return sort->getElemSort();
211}
212
213std::shared_ptr<Sort>
214SortManager::getParamSort(const std::shared_ptr<Sort> &sort, size_t index) {
215 return sort->getParamSort(index);
216}
217
218std::vector<std::shared_ptr<Sort>>
219SortManager::getParams(const std::shared_ptr<Sort> &sort) {
220 return sort->getParams();
221}
222
223std::shared_ptr<Sort>
224SortManager::getOutSort(const std::shared_ptr<Sort> &sort) {
225 return sort->getOutSort();
226}
227} // namespace stabilizer::parser
static const std::shared_ptr< Sort > INTOREAL_SORT
Definition sort.h:408
std::shared_ptr< Sort > getElemSort(const std::shared_ptr< Sort > &sort)
Definition sort.cpp:209
std::shared_ptr< Sort > createBVSort(size_t width)
Definition sort.cpp:146
static const std::shared_ptr< Sort > ROUNDING_MODE_SORT
Definition sort.h:419
std::unordered_map< size_t, std::vector< std::pair< std::shared_ptr< Sort >, size_t > > > sort_buckets
Definition sort.h:317
std::shared_ptr< Sort > getOutSort(const std::shared_ptr< Sort > &sort)
Definition sort.cpp:224
static const std::shared_ptr< Sort > ALGEBRAIC_SORT
Definition sort.h:394
std::shared_ptr< Sort > insertSortToBucket(const std::shared_ptr< Sort > &sort)
Definition sort.cpp:91
static const std::shared_ptr< Sort > STR_SORT
Definition sort.h:398
static const std::shared_ptr< Sort > TRANSCENDENTAL_SORT
Definition sort.h:396
size_t getBitWidth(const std::shared_ptr< Sort > &sort)
Definition sort.cpp:191
std::shared_ptr< Sort > createArraySort(std::shared_ptr< Sort > index, std::shared_ptr< Sort > elem)
Definition sort.cpp:161
static const std::shared_ptr< Sort > INT_SORT
Definition sort.h:390
static const std::shared_ptr< Sort > FLOAT32_SORT
Definition sort.h:413
std::vector< std::shared_ptr< Sort > > sorts
Definition sort.h:314
std::shared_ptr< Sort > getParamSort(const std::shared_ptr< Sort > &sort, size_t index)
Definition sort.cpp:214
std::shared_ptr< Sort > createSort()
Definition sort.cpp:140
static const std::shared_ptr< Sort > FLOAT16_SORT
Definition sort.h:416
std::shared_ptr< Sort > getIndexSort(const std::shared_ptr< Sort > &sort)
Definition sort.cpp:204
std::shared_ptr< Sort > createSortDef(const std::string &name, const std::vector< std::shared_ptr< Sort > > &params, std::shared_ptr< Sort > out_sort)
Definition sort.cpp:181
static const std::shared_ptr< Sort > FLOAT64_SORT
Definition sort.h:410
std::shared_ptr< Sort > createSortDec(const std::string &name, size_t arity)
Definition sort.cpp:174
static const std::shared_ptr< Sort > RAND_SORT
Definition sort.h:406
size_t getIndex(const std::shared_ptr< Sort > &sort) const
Definition sort.cpp:74
std::shared_ptr< Sort > createFPSort(size_t exp, size_t sig)
Definition sort.cpp:153
std::shared_ptr< Sort > createTupleSort(const std::vector< std::shared_ptr< Sort > > &fields)
Definition sort.cpp:169
std::vector< std::shared_ptr< Sort > > getParams(const std::shared_ptr< Sort > &sort)
Definition sort.cpp:219
std::shared_ptr< Sort > getSort(const size_t index) const
Definition sort.cpp:67
static const std::shared_ptr< Sort > REG_SORT
Definition sort.h:400
static const std::shared_ptr< Sort > BOOL_SORT
Definition sort.h:388
static const std::shared_ptr< Sort > REAL_SORT
Definition sort.h:392
static const std::shared_ptr< Sort > UNKNOWN_SORT
Definition sort.h:386
static const std::shared_ptr< Sort > EXT_SORT
Definition sort.h:402
static const std::shared_ptr< Sort > NULL_SORT
Definition sort.h:384
size_t getSignificandWidth(const std::shared_ptr< Sort > &sort)
Definition sort.cpp:199
static const std::shared_ptr< Sort > NAT_SORT
Definition sort.h:404
size_t getExponentWidth(const std::shared_ptr< Sort > &sort)
Definition sort.cpp:195