19 ss << std::setw(total_length) << std::setfill(
'0') << i;
25 std::string r_str = str;
30 if (found_index == std::string::npos)
35 r_str = r_str.replace(found_index, from.size(),
to);
62 log_error(
"z3_utils",
"Tried to convert unkown operation to string.");
63 op_str =
"NOT IMPLEMENTED REACHED";
72 return l.find(
"let") != std::string::npos;
85 std::map<u32, u32> pairs;
89 auto found_index = l.find_first_of(
'(',
start_index);
90 if (found_index == std::string::npos)
97 for (
u32 i = found_index + 1; i < l.size(); i++)
103 else if (l.at(i) ==
')')
110 pairs.insert({found_index, i});
116 log_debug(
"z3_utils",
"Trying to find paranthesis_pairs.");
133 if (found_index == std::string::npos)
152 std::map<std::string, std::string> sub_expressions;
154 for (
const auto& [start, end] : parentheses)
156 auto sub_expression = l.substr(start, end - start + 1);
158 sub_expressions.insert({sub_name, sub_expression});
163 std::set<std::string> to_erase;
164 for (
const auto& [dom_name, dom_expr] : sub_expressions)
166 for (
const auto& [sub_name, sub_expr] : sub_expressions)
168 if (dom_expr == sub_expr && dom_name != sub_name && to_erase.find(dom_name) == to_erase.end())
170 to_erase.insert(sub_name);
174 for (
const auto& n : to_erase)
176 sub_expressions.erase(n);
180 std::queue<std::string> q;
181 for (
const auto& [n, e] : sub_expressions)
189 auto f_name = q.front();
190 auto f_expr = sub_expressions.at(f_name);
195 for (
auto [n, e] : sub_expressions)
202 sub_expressions.at(n) =
replace_all(e, f_expr, f_name);
212 log_debug(
"z3_utils",
"Queue: {} at front {}.", q.size(), q.front());
213 log_debug(
"z3_utils",
"Sub: {}", sub_expressions.at(q.front()));
217 return sub_expressions;
225 if (se.find(op_str) != std::string::npos)
231 log_error(
"z3_utils",
"Unkown operation in subexpression {}", se);
238 std::string shortend_string = se.substr(1);
239 shortend_string.pop_back();
241 std::vector<std::string> operands;
243 std::istringstream iss(shortend_string);
245 while (getline(iss, tmp,
' '))
247 operands.push_back(tmp);
251 operands.erase(operands.begin());
258 std::map<std::string, std::string> translated_sub_expressions;
260 for (
const auto& [n, s] : se)
265 translated_sub_expressions.insert({n,
build_operation(operation, operands)});
268 return translated_sub_expressions;
273 std::string assignment = translated_sub_expressions.at(
"sub_00000");
276 std::map<std::string, std::string> merged_sub_expressions = translated_sub_expressions;
277 for (
const auto& [n, e] : merged_sub_expressions)
279 for (
auto [m, f] : merged_sub_expressions)
281 merged_sub_expressions.at(m) =
replace_all(f, n, e);
286 for (
const auto& [n, e] : merged_sub_expressions)
296 std::string assignments =
"";
309 std::istringstream iss(smt);
310 for (std::string line; std::getline(iss, line);)
315 assignments = assignments + assignment;
320 log_debug(
"z3_utils",
"found {} assignments.", assignments.size());
322 if (assignments.empty())
325 auto dummy_assignment =
"(let ((?x1 (bvnot (bvnot " + e.to_string() +
")))))";
std::string replace_all(const std::string &str, const std::string &from, const std::string &to) const
bool does_line_contain_assignment(const std::string &l) const
std::map< std::string, std::string > extract_sub_exrepssions(const std::string &l) const
bool contains_one_operation(const std::string &l) const
std::string integer_with_leading_zeros(const u32 i, const u32 total_length) const
std::string extract_lhs(const std::string &l) const
std::map< std::string, std::string > translate_sub_expressions(const std::map< std::string, std::string > &se) const
std::vector< std::string > extract_operands(const std::string &se) const
std::string convert_z3_expr_to_func(const z3::expr &e) const
std::string merge_sub_expressions(const std::map< std::string, std::string > &translated_sub_expressions) const
std::map< u32, u32 > extract_paranthesis_pairs(const std::string &l) const
virtual std::string construct_function(const std::string &assignments, const std::string &initalization, const std::vector< std::string > &input_vars) const =0
std::string operation_to_string(const Operation &op) const
Operation extract_operation(const std::string &se) const
virtual std::string build_operation(const Operation &operation, const std::vector< std::string > &operands) const =0
virtual std::string generate_assignment(const std::string &l) const =0
virtual std::string generate_initialization(const std::vector< std::string > &input_vars) const =0
std::vector< Operation > m_operations
#define log_error(channel,...)
#define log_debug(channel,...)
std::vector< T > to_vector(const Container< T, Args... > &container)
std::set< std::string > get_variable_names(const z3::expr &e)
Extracts all variable names from a z3 expression.
std::string to_smt2(const z3::expr &e)