HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
python_bindings.cpp
Go to the documentation of this file.
2 
5 #include "pybind11/operators.h"
6 #include "pybind11/pybind11.h"
7 #include "pybind11/stl.h"
8 #include "pybind11/stl_bind.h"
9 
10 namespace py = pybind11;
11 
12 namespace hal
13 {
14 
15  // the name in PYBIND11_MODULE/PYBIND11_PLUGIN *MUST* match the filename of the output library (without extension),
16  // otherwise you will get "ImportError: dynamic module does not define module export function" when importing the module
17 
18 #ifdef PYBIND11_MODULE
19  PYBIND11_MODULE(bitorder_propagation, m)
20  {
21  m.doc() = "Tool to automatically propagate known bit orders to module pin groups of unknown bit order.";
22 #else
23  PYBIND11_PLUGIN(bitorder_propagation)
24  {
25  py::module m("bitorder_propagation", "Tool to automatically propagate known bit orders to module pin groups of unknown bit order.");
26 #endif // ifdef PYBIND11_MODULE
27 
28  py::class_<BitorderPropagationPlugin, RawPtrWrapper<BitorderPropagationPlugin>, BasePluginInterface> py_bitorder_propagation_plugin(
29  m, "BitorderPropagationPlugin", R"(This class provides an interface to integrate the bit-order propagation as a plugin within the HAL framework.)");
30 
31  py_bitorder_propagation_plugin.def_property_readonly("name", &BitorderPropagationPlugin::get_name, R"(
32  The name of the plugin.
33 
34  :type: str
35  )");
36 
37  py_bitorder_propagation_plugin.def("get_name", &BitorderPropagationPlugin::get_name, R"(
38  Get the name of the plugin.
39 
40  :returns: The name of the plugin.
41  :rtype: str
42  )");
43 
44  py_bitorder_propagation_plugin.def_property_readonly("version", &BitorderPropagationPlugin::get_version, R"(
45  The version of the plugin.
46 
47  :type: str
48  )");
49 
50  py_bitorder_propagation_plugin.def("get_version", &BitorderPropagationPlugin::get_version, R"(
51  Get the version of the plugin.
52 
53  :returns: The version of the plugin.
54  :rtype: str
55  )");
56 
57  py_bitorder_propagation_plugin.def_property_readonly("description", &BitorderPropagationPlugin::get_description, R"(
58  The description of the plugin.
59 
60  :type: str
61  )");
62 
63  py_bitorder_propagation_plugin.def("get_description", &BitorderPropagationPlugin::get_description, R"(
64  Get the description of the plugin.
65 
66  :returns: The description of the plugin.
67  :rtype: str
68  )");
69 
70  m.def(
71  "propagate_module_pingroup_bitorder",
72  [](const std::map<std::pair<Module*, PinGroup<ModulePin>*>, std::map<Net*, u32>>& src,
73  const std::set<std::pair<Module*, PinGroup<ModulePin>*>>& dst,
74  const bool enforce_continuous_bitorders = true) -> std::optional<std::map<std::pair<Module*, PinGroup<ModulePin>*>, std::map<Net*, u32>>> {
75  const auto res = bitorder_propagation::propagate_module_pingroup_bitorder(src, dst, enforce_continuous_bitorders);
76  if (res.is_ok())
77  {
78  return res.get();
79  }
80  else
81  {
82  log_error("python_context", "{}", res.get_error().get());
83  return std::nullopt;
84  }
85  },
86  py::arg("src"),
87  py::arg("dst"),
88  py::arg("enforce_continuous_bitorders") = true,
89  R"(
90  Propagate known bit-order information from the given module pin groups to module pin groups of unknown bit order.
91  The known bit-order information is taken from the map from net to index given for each pair of module and pin group in ``src``.
92  After propagation, the algorithm tries to reconstruct valid bit orders from the propagated information.
93 
94  :param dict[tuple(hal_py.Module,hal_py.ModulePinGroup),dict[hal_py.Net,int]] src: The known indices for the nets belonging to the given module pin groups.
95  :param set[tuple(hal_py.Module,hal_py.ModulePinGroup)] dst: The pairs of module ID and pin group name with unknown bit order.
96  :param bool enforce_continuous_bitorders: Set ``True`` to only allow for continuous bit orders, ``^`` to also allow bit orders that are not continuous. Defaults to ``True``.
97  :returns: A dict containing all known bit orders (including new and already known ones) on success, ``None`` otherwise.
98  :rtype: dict[tuple(hal_py.Module,hal_py.ModulePinGroup),dict[hal_py.Net,int]] or None
99  )");
100 
101  m.def(
102  "reorder_module_pin_groups",
103  [](const std::map<std::pair<Module*, PinGroup<ModulePin>*>, std::map<Net*, u32>>& ordered_module_pin_groups) -> bool {
104  const auto res = bitorder_propagation::reorder_module_pin_groups(ordered_module_pin_groups);
105  if (res.is_ok())
106  {
107  return true;
108  }
109  else
110  {
111  log_error("python_context", "{}", res.get_error().get());
112  return false;
113  }
114  },
115  py::arg("ordered_module_pin_groups"),
116  R"(
117  Reorder and rename the pins of the pin groups according to the provided bit-order information.
118 
119  :param dict[tuple(hal_py.Module,hal_py.ModulePinGroup),dict[hal_py.Net,int]] ordered_module_pin_groups: A mapping from pairs of modules and their pin groups to known bit-order information given as a mapping from nets to their index.
120  :returns: ``True`` on success, ``False`` otherwise.
121  :rtype: bool
122  )");
123 
124  m.def(
125  "propagate_bitorder",
126  [](Netlist* nl, const std::pair<u32, std::string>& src, const std::pair<u32, std::string>& dst) -> std::optional<std::map<std::pair<Module*, PinGroup<ModulePin>*>, std::map<Net*, u32>>> {
127  const auto res = bitorder_propagation::propagate_bitorder(nl, src, dst);
128  if (res.is_ok())
129  {
130  return res.get();
131  }
132  else
133  {
134  log_error("python_context", "{}", res.get_error().get());
135  return std::nullopt;
136  }
137  },
138  py::arg("nl"),
139  py::arg("src"),
140  py::arg("dst"),
141  R"(
142  Propagate known bit-order information from one module pin group to another module pin group of unknown bit order.
143  The known bit-order information is taken from the order of pins in the pin group of ``src``.
144  After propagation, the algorithm tries to reconstruct a valid bit order from the propagated information.
145  The valid bit order is then annotated to the module pin group, i.e., the pins of the respective pin group are renamed and reordered.
146 
147  :param hal_py.netlist nl: The netlist containing the module.
148  :param tuple(int,str) src: The pair of module ID and pin group name with known bit order.
149  :param tuple(int,str) dst: The pair of module ID and pin group name with unknown bit order.
150  :returns: A dict containing all known bit orders (including new and already known ones) on success, ``None`` otherwise.
151  :rtype: dict[tuple(hal_py.Module,hal_py.ModulePinGroup),dict[hal_py.Net,int]] or None
152  )");
153 
154  m.def(
155  "propagate_bitorder",
156  [](const std::pair<Module*, PinGroup<ModulePin>*>& src,
157  const std::pair<Module*, PinGroup<ModulePin>*>& dst) -> std::optional<std::map<std::pair<Module*, PinGroup<ModulePin>*>, std::map<Net*, u32>>> {
158  const auto res = bitorder_propagation::propagate_bitorder(src, dst);
159  if (res.is_ok())
160  {
161  return res.get();
162  }
163  else
164  {
165  log_error("python_context", "{}", res.get_error().get());
166  return std::nullopt;
167  }
168  },
169  py::arg("src"),
170  py::arg("dst"),
171  R"(
172  Propagate known bit-order information from one module pin group to another module pin group of unknown bit order.
173  The known bit-order information is taken from the order of pins in the pin group of ``src``.
174  After propagation, the algorithm tries to reconstruct a valid bit order from the propagated information.
175  The valid bit order is then annotated to the module pin group, i.e., the pins of the respective pin group are renamed and reordered.
176 
177  :param tuple(hal_py.Module,hal_py.ModulePinGroup) src: The pair of module and pin group with known bit order.
178  :param tuple(hal_py.Module,hal_py.ModulePinGroup) dst: The pair of module and pin group with unknown bit order.
179  :returns: A dict containing all known bit orders (including new and already known ones) on success, ``None`` otherwise.
180  :rtype: dict[tuple(hal_py.Module,hal_py.ModulePinGroup),dict[hal_py.Net,int]] or None
181  )");
182 
183  m.def(
184  "propagate_bitorder",
185  [](Netlist* nl,
186  const std::vector<std::pair<u32, std::string>>& src,
187  const std::vector<std::pair<u32, std::string>>& dst) -> std::optional<std::map<std::pair<Module*, PinGroup<ModulePin>*>, std::map<Net*, u32>>> {
188  const auto res = bitorder_propagation::propagate_bitorder(nl, src, dst);
189  if (res.is_ok())
190  {
191  return res.get();
192  }
193  else
194  {
195  log_error("python_context", "{}", res.get_error().get());
196  return std::nullopt;
197  }
198  },
199  py::arg("nl"),
200  py::arg("src"),
201  py::arg("dst"),
202  R"(
203  Propagate known bit-order information from the given module pin groups to module pin groups of unknown bit order.
204  The known bit-order information is taken from the order of pins in the pin groups of ``src``.
205  After propagation, the algorithm tries to reconstruct valid bit orders from the propagated information.
206  The valid bit orders are then annotated to the module pin groups, i.e., the pins of the respective pin groups are renamed and reordered.
207 
208  :param hal_py.netlist nl: The netlist containing the modules.
209  :param list[tuple(int,str)] src: The pairs of module ID and pin group name with known bit order.
210  :param list[tuple(int,str)] dst: The pairs of module ID and pin group name with unknown bit order.
211  :returns: A dict containing all known bit orders (including new and already known ones) on success, ``None`` otherwise.
212  :rtype: dict[tuple(hal_py.Module,hal_py.ModulePinGroup),dict[hal_py.Net,int]] or None
213  )");
214 
215  m.def(
216  "propagate_bitorder",
217  [](const std::vector<std::pair<Module*, PinGroup<ModulePin>*>>& src,
218  const std::vector<std::pair<Module*, PinGroup<ModulePin>*>>& dst) -> std::optional<std::map<std::pair<Module*, PinGroup<ModulePin>*>, std::map<Net*, u32>>> {
219  const auto res = bitorder_propagation::propagate_bitorder(src, dst);
220  if (res.is_ok())
221  {
222  return res.get();
223  }
224  else
225  {
226  log_error("python_context", "{}", res.get_error().get());
227  return std::nullopt;
228  }
229  },
230  py::arg("src"),
231  py::arg("dst"),
232  R"(
233  Propagate known bit-order information from the given module pin groups to module pin groups of unknown bit order.
234  The known bit-order information is taken from the order of pins in the pin groups of ``src``.
235  After propagation, the algorithm tries to reconstruct valid bit orders from the propagated information.
236  The valid bit orders are then annotated to the module pin groups, i.e., the pins of the respective pin groups are renamed and reordered.
237 
238  :param list[tuple(hal_py.Module,hal_py.ModulePinGroup)] src: The pairs of module and pin group with known bit order.
239  :param list[tuple(hal_py.Module,hal_py.ModulePinGroup)] dst: The pairs of module and pin group with unknown bit order.
240  :returns: A dict containing all known bit orders (including new and already known ones) on success, ``None`` otherwise.
241  :rtype: dict[tuple(hal_py.Module,hal_py.ModulePinGroup),dict[hal_py.Net,int]] or None
242  )");
243 
244  m.def(
245  "export_bitorder_propagation_information",
246  [](const std::map<std::pair<Module*, PinGroup<ModulePin>*>, std::map<Net*, u32>>& src,
247  const std::set<std::pair<Module*, PinGroup<ModulePin>*>>& dst,
248  const std::string& export_filepath) -> std::optional<std::map<std::pair<Module*, PinGroup<ModulePin>*>, u32>> {
249  const auto res = bitorder_propagation::export_bitorder_propagation_information(src, dst, export_filepath);
250  if (res.is_ok())
251  {
252  return res.get();
253  }
254  else
255  {
256  log_error("python_context", "{}", res.get_error().get());
257  return std::nullopt;
258  }
259  },
260  py::arg("src"),
261  py::arg("dst"),
262  py::arg("export_filepath"),
263  R"(
264  Export collected bitorder information like word composition, known bitorder and connectivity in ``.json`` format to solve with external tools.
265 
266  :param dict[tuple(hal_py.Module,hal_py.ModulePinGroup),dict[hal_py.Net,int]] src: The known indices for the nets belonging to the given module pin groups.
267  :param set[tuple(hal_py.Module,hal_py.ModulePinGroup)] dst: The pairs of module ID and pin group name with unknown bit order.
268  :param str export_filepath: The filepath where the ``.json`` file should be written to.
269  :returns: The mapping from each mdoule/pingroup pair to its index on success, ``None`` otherwise.
270  :rtype: dict[tuple(hal_py.Module, hal_py.ModulePinGroup), int] or None
271  )");
272 
273  m.def(
274  "export_bitorder_propagation_information",
275  [](const std::vector<std::pair<Module*, PinGroup<ModulePin>*>>& src,
276  const std::vector<std::pair<Module*, PinGroup<ModulePin>*>>& dst,
277  const std::string& export_filepath) -> std::optional<std::map<std::pair<Module*, PinGroup<ModulePin>*>, u32>> {
278  const auto res = bitorder_propagation::export_bitorder_propagation_information(src, dst, export_filepath);
279  if (res.is_ok())
280  {
281  return res.get();
282  }
283  else
284  {
285  log_error("python_context", "{}", res.get_error().get());
286  return std::nullopt;
287  }
288  },
289  py::arg("src"),
290  py::arg("dst"),
291  py::arg("export_filepath"),
292  R"(
293  Export collected bitorder information like word composition, known bitorder and connectivity in ``.json`` format to solve with external tools.
294 
295  :param tuple(hal_py.Module,hal_py.ModulePinGroup) src: The pair of module and pin group with known bit order.
296  :param tuple(hal_py.Module,hal_py.ModulePinGroup) dst: The pair of module and pin group with unknown bit order.
297  :param str export_filepath: The filepath where the ``.json`` file should be written to.
298  :returns: The mapping from each mdoule/pingroup pair to its index on success, ``None`` otherwise.
299  :rtype: dict[tuple(hal_py.Module, hal_py.ModulePinGroup), int] or None
300  )");
301 
302 #ifndef PYBIND11_MODULE
303  return m.ptr();
304 #endif // PYBIND11_MODULE
305  }
306 } // namespace hal
This file contains functions for bit-order propagation from pin groups of known bit order to pin grou...
std::string get_description() const override
Get a short description of the plugin.
std::string get_version() const override
Get the version of the plugin.
std::string get_name() const override
Get the name of the plugin.
uint32_t u32
Definition: defines.h:41
#define log_error(channel,...)
Definition: log.h:78
const Module * module(const Gate *g, const NodeBoxes &boxes)
Result< std::map< std::pair< Module *, PinGroup< ModulePin > * >, std::map< Net *, u32 > > > propagate_bitorder(Netlist *nl, const std::pair< u32, std::string > &src, const std::pair< u32, std::string > &dst)
Propagate known bit-order information from one module pin group to another module pin group of unknow...
Result< std::monostate > reorder_module_pin_groups(const std::map< std::pair< Module *, PinGroup< ModulePin > * >, std::map< Net *, u32 >> &ordered_module_pin_groups)
Reorder and rename the pins of the pin groups according to the provided bit-order information.
Result< std::map< std::pair< Module *, PinGroup< ModulePin > * >, u32 > > export_bitorder_propagation_information(const std::vector< std::pair< Module *, PinGroup< ModulePin > * >> &src, const std::vector< std::pair< Module *, PinGroup< ModulePin > * >> &dst, const std::string &export_filepath)
Export word composition, known bitorder and connectivity in .json format to solve with external tools...
Result< std::map< std::pair< Module *, PinGroup< ModulePin > * >, std::map< Net *, u32 > > > propagate_module_pingroup_bitorder(const std::map< std::pair< Module *, PinGroup< ModulePin > * >, std::map< Net *, u32 >> &src, const std::set< std::pair< Module *, PinGroup< ModulePin > * >> &dst, const bool enforce_continuous_bitorders=true)
Propagate known bit-order information from the given module pin groups to module pin groups of unknow...
Definition: defines.h:45
PYBIND11_PLUGIN(hal_py)
This file contains all functions related to the HAL plugin API.