HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
converter.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
28 #include "hal_core/defines.h"
29 #include "z3++.h"
30 
31 #include <map>
32 #include <vector>
33 
34 namespace hal
35 {
36  namespace z3_utils
37  {
41  class Converter
42  {
43  public:
47  enum Operation
48  {
51  bvor,
53  NONE,
54  };
55 
63  std::string convert_z3_expr_to_func(const z3::expr& e) const;
64 
65  protected:
66  // VIRTUAL METHODS
67 
74  virtual std::string build_operand(const std::string& operand) const = 0;
75 
83  virtual std::string build_operation(const Operation& operation, const std::vector<std::string>& operands) const = 0;
84 
91  virtual std::string generate_assignment(const std::string& l) const = 0;
92 
99  virtual std::string generate_initialization(const std::vector<std::string>& input_vars) const = 0;
100 
109  virtual std::string construct_function(const std::string& assignments, const std::string& initalization, const std::vector<std::string>& input_vars) const = 0;
110 
111  // UTILS
112 
120  std::string integer_with_leading_zeros(const u32 i, const u32 total_length) const;
121 
130  std::string replace_all(const std::string& str, const std::string& from, const std::string& to) const;
131 
138  std::string operation_to_string(const Operation& op) const;
139 
140  // GENERAL METHODS
141 
148  bool does_line_contain_assignment(const std::string& l) const;
149 
156  std::string extract_lhs(const std::string& l) const;
157 
164  std::map<u32, u32> extract_paranthesis_pairs(const std::string& l) const;
165 
172  bool contains_one_operation(const std::string& l) const;
173 
180  std::map<std::string, std::string> extract_sub_exrepssions(const std::string& l) const;
181 
188  Operation extract_operation(const std::string& se) const;
189 
196  std::vector<std::string> extract_operands(const std::string& se) const;
197 
204  std::map<std::string, std::string> translate_sub_expressions(const std::map<std::string, std::string>& se) const;
205 
212  std::string merge_sub_expressions(const std::map<std::string, std::string>& translated_sub_expressions) const;
213 
221  std::string generate_function(const std::vector<std::string>& assignments, std::vector<std::string>& input_vars) const;
222 
225  };
226  } //namespace z3_utils
227 } // namespace hal
std::string replace_all(const std::string &str, const std::string &from, const std::string &to) const
Definition: converter.cpp:23
bool does_line_contain_assignment(const std::string &l) const
Definition: converter.cpp:70
std::map< std::string, std::string > extract_sub_exrepssions(const std::string &l) const
Definition: converter.cpp:147
bool contains_one_operation(const std::string &l) const
Definition: converter.cpp:122
std::string integer_with_leading_zeros(const u32 i, const u32 total_length) const
Definition: converter.cpp:16
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::vector< std::string > extract_operands(const std::string &se) const
Definition: converter.cpp:235
std::string convert_z3_expr_to_func(const z3::expr &e) const
Definition: converter.cpp:294
std::string merge_sub_expressions(const std::map< std::string, std::string > &translated_sub_expressions) const
Definition: converter.cpp:271
std::map< u32, u32 > extract_paranthesis_pairs(const std::string &l) const
Definition: converter.cpp:83
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
Definition: converter.cpp:43
virtual std::string build_operand(const std::string &operand) const =0
Operation extract_operation(const std::string &se) const
Definition: converter.cpp:220
std::string generate_function(const std::vector< std::string > &assignments, std::vector< std::string > &input_vars) 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
Definition: converter.h:224
uint32_t u32
Definition: defines.h:41
S to(const T &str)
Definition: defines.h:45