HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
cpp_converter.cpp
Go to the documentation of this file.
2 
6 
7 #include <algorithm>
8 
9 namespace hal
10 {
11  namespace z3_utils
12  {
13  std::string Cpp_Converter::build_operand(const std::string& operand) const
14  {
15  if (operand.find("?") != std::string::npos)
16  {
17  return operand.substr(1);
18  }
19  if (operand.find("sub_") != std::string::npos)
20  {
21  return operand;
22  }
23  if (utils::starts_with(operand, std::string("var_")))
24  {
25  return "values[" + operand.substr(4) + "]";
26  }
27 
28  log_error("z3_utils", "Unkown operand format for {}!", operand);
29  exit(0);
30  return "NOT IMPLEMENTED REACHED";
31  }
32 
33  std::string Cpp_Converter::build_operation(const Converter::Operation& operation, const std::vector<std::string>& operands) const
34  {
35  std::string op;
36 
37  switch (operation)
38  {
39  case Converter::bvand:
40  for (const auto& o : operands)
41  {
42  op += (build_operand(o) + " & ");
43  }
44  op.erase(op.size() - 3);
45  break;
46  case Converter::bvnot:
47  if (operands.size() > 1)
48  {
49  log_error("z3_utils", "unhandled amount of operands for operation");
50  return "NOT IMPLEMENTED REACHED";
51  }
52  op = "!" + build_operand(operands.front());
53  break;
54  case Converter::bvor:
55  for (const auto& o : operands)
56  {
57  op += (build_operand(o) + " | ");
58  }
59  op.erase(op.size() - 3);
60  break;
61  case Converter::bvxor:
62  for (const auto& o : operands)
63  {
64  op += (build_operand(o) + " ^ ");
65  }
66  op.erase(op.size() - 3);
67  break;
68  default:
69  log_error("z3_utils", "unkown operation for operation building ({})", operation_to_string(operation));
70  return "NOT IMPLEMENTED REACHED";
71  }
72 
73  op = '(' + op + ')';
74  return op;
75  }
76 
77  std::string Cpp_Converter::generate_assignment(const std::string& l) const
78  {
79  // stores variable the is being declared
80  std::string lhs = extract_lhs(l);
81 
82  auto sub_expressions_start = l.find(lhs) + lhs.size() + 1; // hardcoded + 1 for the space between (just for my ocd)
83  auto sub_expressions_size = l.size() - sub_expressions_start - 2; // hardcoded -2 for the last two closing brackest (just for my ocd)
84  auto sub_expressions_string = l.substr(sub_expressions_start, sub_expressions_size);
85 
86  auto sub_expressions = extract_sub_exrepssions(sub_expressions_string);
87  auto translated_sub_expressions = translate_sub_expressions(sub_expressions);
88 
89  auto rhs = merge_sub_expressions(translated_sub_expressions);
90 
91  return "bool " + lhs + " = " + rhs + ";\n";
92  }
93 
94  std::string Cpp_Converter::generate_initialization(const std::vector<std::string>& input_vars) const
95  {
96  std::string init = "";
97  for (const auto& var : input_vars)
98  {
99  if (!utils::starts_with(var, std::string("var_")))
100  {
101  log_error("z3_utils", "Wrong variable format for {}! Expecting 'var_<INDEX>'.", var);
102  return "WRONG VAR FORMAT";
103  }
104 
105  init = init + var.substr(4) + ",\n";
106  }
107 
108  return init;
109  }
110 
111  std::string Cpp_Converter::construct_function(const std::string& assignments, const std::string& initalization, const std::vector<std::string>& input_vars) const
112  {
113  std::string func = m_function_corpus;
114 
115  // extract last return val
116  const auto last_assigment_pos = assignments.rfind("bool") + 5; // length of "bool" + one space
117  const auto last_line = assignments.substr(last_assigment_pos);
118 
119  const auto return_var = extract_lhs(last_line);
120 
121  // func = replace_all(func, "<INIT>", initalization);
122  func = replace_all(func, "<ASSIGNMENTS>", assignments);
123  func = replace_all(func, "<RETURN>", return_var);
124  // func = replace_all(func, "<INPUT_SIZE>", std::to_string(input_vars.size() + 1));
125 
126  return func;
127  }
128  } // namespace z3_utils
129 } // namespace hal
std::string replace_all(const std::string &str, const std::string &from, const std::string &to) const
Definition: converter.cpp:23
std::map< std::string, std::string > extract_sub_exrepssions(const std::string &l) const
Definition: converter.cpp:147
std::string extract_lhs(const std::string &l) const
Definition: converter.cpp:75
std::map< std::string, std::string > translate_sub_expressions(const std::map< std::string, std::string > &se) const
Definition: converter.cpp:256
std::string merge_sub_expressions(const std::map< std::string, std::string > &translated_sub_expressions) const
Definition: converter.cpp:271
std::string operation_to_string(const Operation &op) const
Definition: converter.cpp:43
#define log_error(channel,...)
Definition: log.h:78
bool starts_with(const T &s, const T &start)
Definition: utils.h:169
Definition: defines.h:45