HAL  v4.5.0-124-g47ab54673
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
module.cpp
Go to the documentation of this file.
2 
3 namespace hal
4 {
6  {
7  py::class_<Module, DataContainer, RawPtrWrapper<Module>> py_module(m, "Module", R"(
8  A module is a container for gates and their associated nets that enables hierarchization within the netlist.
9  Each gate can only be in one module at a time. Nets are only loosely associated with modules.
10  )");
11 
12  py_module.def(py::self == py::self, R"(
13  Check whether two modules are equal.
14  Does not check for parent module.
15 
16  :returns: ``True`` if both modules are equal, ``False`` otherwise.
17  :rtype: bool
18  )");
19 
20  py_module.def(py::self != py::self, R"(
21  Check whether two modules are unequal.
22  Does not check for parent module.
23 
24  :returns: ``True`` if both modules are unequal, ``False`` otherwise.
25  :rtype: bool
26  )");
27 
28  py_module.def("__hash__", &Module::get_hash, R"(
29  Python requires hash for set and dict container.
30 
31  :returns: The hash.
32  :rtype: Py_hash_t
33  )");
34 
35  py_module.def_property_readonly("id", &Module::get_id, R"(
36  The unique ID of the module.
37 
38  :type: int
39  )");
40 
41  py_module.def("get_id", &Module::get_id, R"(
42  Get the unique ID of the module.
43 
44  :returns: The unique id.
45  :rtype: int
46  )");
47 
48  py_module.def_property("name", &Module::get_name, &Module::set_name, R"(
49  The name of the module.
50 
51  :type: str
52  )");
53 
54  py_module.def("get_name", &Module::get_name, R"(
55  Get the name of the module.
56 
57  :returns: The name.
58  :rtype: str
59  )");
60 
61  py_module.def("set_name", &Module::set_name, py::arg("name"), R"(
62  Set the name of the module.
63 
64  :param str name: The new name.
65  )");
66 
67  py_module.def_property("type", &Module::get_type, &Module::set_type, R"(
68  The type of the module.
69 
70  :type: str
71  )");
72 
73  py_module.def("get_type", &Module::get_type, R"(
74  Get the type of the module.
75 
76  :returns: The type.
77  :rtype: str
78  )");
79 
80  py_module.def("set_type", &Module::set_type, py::arg("type"), R"(
81  Set the type of the module.
82 
83  :param str type: The new type.
84  )");
85 
86  py_module.def("get_grouping", &Module::get_grouping, borrowed(), R"(
87  Get the grouping in which this module is contained.
88 
89  :returns: The grouping.
90  :rtype: hal_py.Grouping
91  )");
92 
93  py_module.def_property_readonly("submodule_depth", &Module::get_submodule_depth, R"(
94  The depth of the module within the module hierarchie (0 = top module, 1 = direct child of top module, ...).
95 
96  :type: int
97  )");
98 
99  py_module.def("get_submodule_depth", &Module::get_submodule_depth, R"(
100  Get the depth of the module within the module hierarchie (0 = top module, 1 = direct child of top module, ...).
101 
102  :returns: The depth within the module hierarchie.
103  :rtype: int
104  )");
105 
106  py_module.def_property("parent_module", &Module::get_parent_module, &Module::set_parent_module, borrowed(), R"(
107  The parent module of this module.
108  Is set to ``None`` for the top module, but cannot be set to ``None`` by the user.
109 
110  :type: hal_py.Module or None
111  )");
112 
113  py_module.def("get_parent_module", &Module::get_parent_module, borrowed(), R"(
114  Get the parent module of this module.
115  For the top module, ``None`` is returned.
116 
117  :returns: The parent module.
118  :rtype: hal_py.Module or None
119  )");
120 
121  py_module.def_property_readonly("parent_modules", py::cpp_function([](Module* mod) { return mod->get_parent_modules(); }, py::is_method(py_module), borrowed()), R"(
122  The parent modules of this module.
123 
124  :type: list[hal_py.Module]
125  )");
126 
127  py_module.def("get_parent_modules", &Module::get_parent_modules, py::arg("filter") = nullptr, py::arg("recursive") = true, borrowed(), R"(
128  Get all parents of this module.
129  If ``recursive`` is set to ``True``, all indirect parents are also included.
130  The optional filter is evaluated on every candidate such that the result only contains those matching the specified condition.
131 
132  :param lambda filter: An optional filter.
133  :param bool recursive: Set ``True`` to include indirect parents as well, ``False`` otherwise.
134  :returns: A list of parent modules.
135  :rtype: list[hal_py.Module]
136  )");
137 
138  py_module.def("set_parent_module", &Module::set_parent_module, py::arg("new_parent"), R"(
139  Set a new parent for this module.
140  If the new parent is a submodule of this module, the new parent is added as a direct submodule to the old parent first.
141 
142  :param hal_py.Module new_parent: The new parent module.
143  :returns: ``True`` if the parent was changed, ``False`` otherwise.
144  :rtype: bool
145  )");
146 
147  py_module.def("is_parent_module_of", &Module::is_parent_module_of, py::arg("module"), py::arg("recursive") = false, R"(
148  Check if the module is a parent of the specified module.
149 
150  :param hal_py.Module module: The module.
151  :param bool recursive: Set ``True`` to check recursively, ``False`` otherwise.
152  :returns: ``True`` if the module is a parent of the specified module, ``False`` otherwise.
153  :rtype: bool
154  )");
155 
156  py_module.def_property_readonly("submodules", py::cpp_function([](Module* mod) { return mod->get_submodules(); }, py::is_method(py_module), borrowed()), R"(
157  A list of all direct submodules of this module.
158 
159  :type: list[hal_py.Module]
160  )");
161 
162  py_module.def("get_submodules", &Module::get_submodules, py::arg("filter") = nullptr, py::arg("recursive") = false, borrowed(), R"(
163  Get all direct submodules of this module.
164  If ``recursive`` is set to ``True``, all indirect submodules are also included.
165  The optional filter is evaluated on every candidate such that the result only contains those matching the specified condition.
166 
167  :param lambda filter: An optional filter.
168  :param bool recursive: Set ``True`` to include indirect submodules as well, ``False`` otherwise.
169  :returns: A list of submodules.
170  :rtype: list[hal_py.Module]
171  )");
172 
173  py_module.def("is_submodule_of", &Module::is_submodule_of, py::arg("module"), py::arg("recursive") = false, R"(
174  Check if the module is a submodule of the specified module.
175 
176  :param hal_py.Module module: The module.
177  :param bool recursive: Set ``True`` to check recursively, ``False`` otherwise.
178  :returns: ``True`` if the module is a submodule of the specified module, ``False`` otherwise.
179  :rtype: bool
180  )");
181 
182  py_module.def("contains_module", &Module::contains_module, py::arg("other"), py::arg("recursive") = false, R"(
183  Checks whether another module is a submodule of this module.
184  If recursive is set to ``True``, all indirect submodules are also included.
185 
186  :param hal_py.Module other: Other module to check for.
187  :param bool recursive: Set ``True`` to include indirect submodules as well, ``False`` otherwise.
188  :returns: ``True`` if the other module is a submodule, ``False`` otherwise.
189  :rtype: bool
190  )");
191 
192  py_module.def_property_readonly("top_module", &Module::is_top_module, R"(
193  ``True`` only if the module is the top module of the netlist.
194 
195  :type: bool
196  )");
197 
198  py_module.def("is_top_module", &Module::is_top_module, R"(
199  Returns ``True`` only if the module is the top module of the netlist.
200 
201  :returns: ``True`` if the module is the top module, ``False`` otherwise.
202  :rtype: bool
203  )");
204 
205  py_module.def_property_readonly("netlist", [](Module* module) { return RawPtrWrapper<Netlist>(module->get_netlist()); }, R"(
206  The netlist this module is associated with.
207 
208  :type: hal_py.Netlist
209  )");
210 
211  py_module.def("get_netlist", [](Module* module) { return RawPtrWrapper<Netlist>(module->get_netlist()); }, R"(
212  Get the netlist this module is associated with.
213 
214  :returns: The netlist.
215  :rtype: hal_py.Netlist
216  )");
217 
218  py_module.def("update_nets", &Module::update_nets, R"(
219  Iterates over all nets connected to at least one gate of the module to update the nets, internal nets, input nets, and output nets of the module.
220  Has no effect on module pins.
221 
222  WARNING: can only be used when automatic net checks have been disabled using hal_py.Netlist.enable_automatic_net_checks.
223  )");
224 
225  py_module.def("contains_net", &Module::contains_net, py::arg("net"), py::arg("recursive") = false, R"(
226  Check whether a net is contained in the module.
227  If ``recursive`` is set to ``True``, nets in submodules are considered as well.
228 
229  :param hal_py.Net net: The net to check for.
230  :param bool recursive: ``True`` to also consider nets in submodules, ``False`` otherwise.
231  :returns: ``True`` if the net is contained in the module, ``False`` otherwise.
232  :rtype: bool
233  )");
234 
235  py_module.def_property_readonly("nets", py::cpp_function(py::overload_cast<>(&Module::get_nets, py::const_), py::is_method(py_module), borrowed()), R"(
236  An unordered set of all nets that have at least one source or one destination within the module.
237 
238  :type: set[hal_py.Net]
239  )");
240 
241  py_module.def("get_nets", py::overload_cast<>(&Module::get_nets, py::const_), borrowed(), R"(
242  Get all nets that have at least one source or one destination within the module.
243 
244  :returns: An unordered set of nets.
245  :rtype: set[hal_py.Net]
246  )");
247 
248  py_module.def("get_nets", py::overload_cast<const std::function<bool(Net*)>&, bool>(&Module::get_nets, py::const_), py::arg("filter"), py::arg("recursive") = false, borrowed(), R"(
249  Get all nets that have at least one source or one destination within the module.
250  The filter is evaluated on every candidate such that the result only contains those matching the specified condition.
251  If ``recursive`` is ``True``, nets in submodules are considered as well.
252 
253  :param lambda filter: Filter function to be evaluated on each net.
254  :param bool recursive: ``True`` to also consider nets in submodules, ``False`` otherwise.
255  :returns: An unordered set of nets.
256  :rtype: set[hal_py.Net]
257  )");
258 
259  py_module.def_property_readonly("input_nets", py::cpp_function(&Module::get_input_nets, py::is_method(py_module), borrowed()), R"(
260  A set of all nets that are either a global input to the netlist or have at least one source outside of the module.
261 
262  :type: set[hal_py.Net]
263  )");
264 
265  py_module.def("get_input_nets", &Module::get_input_nets, borrowed(), R"(
266  Get all nets that are either a global input to the netlist or have at least one source outside of the module.
267 
268  :returns: A set of input nets.
269  :rtype: set[hal_py.Net]
270  )");
271 
272  py_module.def_property_readonly("output_nets", py::cpp_function(&Module::get_output_nets, py::is_method(py_module), borrowed()), R"(
273  A set of all nets that are either a global output to the netlist or have at least one destination outside of the module.
274 
275  :type: set[hal_py.Net]
276  )");
277 
278  py_module.def("get_output_nets", &Module::get_output_nets, borrowed(), R"(
279  Get all nets that are either a global output to the netlist or have at least one destination outside of the module.
280 
281  :returns: A set of output nets.
282  :rtype: set[hal_py.Net]
283  )");
284 
285  py_module.def_property_readonly("internal_nets", py::cpp_function(&Module::get_internal_nets, py::is_method(py_module), borrowed()), R"(
286  A set of all nets that have at least one source and one destination within the module, including its submodules. The result may contain nets that are also regarded as input or output nets.
287 
288  :type: set[hal_py.Net]
289  )");
290 
291  py_module.def("get_internal_nets", &Module::get_internal_nets, borrowed(), R"(
292  Get all nets that have at least one source and one destination within the module, including its submodules. The result may contain nets that are also regarded as input or output nets.
293 
294  :returns: A set of internal nets.
295  :rtype: set[hal_py.Net]
296  )");
297 
298  py_module.def("is_input_net", &Module::is_input_net, py::arg("net"), R"(
299  Check whether the given net is an input of the module, i.e., whether the net is a global input to the netlist or has at least one source outside of the module.
300 
301  :param hal_py.Net net: The net.
302  :returns: ``True`` if the net is an input net, ``False`` otherwise.
303  :rtype: bool
304  )");
305 
306  py_module.def("is_output_net", &Module::is_output_net, py::arg("net"), R"(
307  Check whether the given net is an output of the module, i.e., whether the net is a global output to the netlist or has at least one destination outside of the module.
308 
309  :param hal_py.Net net: The net.
310  :returns: ``True`` if the net is an output net, ``False`` otherwise.
311  :rtype: bool
312  )");
313 
314  py_module.def("is_internal_net", &Module::is_internal_net, py::arg("net"), R"(
315  Check whether the given net is an internal net of the module, i.e. whether the net has at least one source and one destination within the module.
316 
317  :param hal_py.Net net: The net.
318  :returns: ``True`` if the net is an internal net, ``False`` otherwise.
319  :rtype: bool
320  )");
321 
322  py_module.def("assign_gate", &Module::assign_gate, py::arg("gate"), R"(
323  Assign a gate to the module.
324  The gate is removed from its previous module in the process.
325 
326  :param hal_py.Gate gate: The gate to assign.
327  :returns: ``True`` on success, ``False`` otherwise.
328  :rtype: bool
329  )");
330 
331  py_module.def("assign_gates", &Module::assign_gates, py::arg("gates"), R"(
332  Assign a list of gates to the module.
333  The gates are removed from their previous module in the process.
334 
335  :param list[hal_py.Gate] gates: The gates to assign.
336  :returns: ``True`` on success, ``False`` otherwise.
337  :rtype: bool
338  )");
339 
340  py_module.def("remove_gate", &Module::remove_gate, py::arg("gate"), R"(
341  Remove a gate from the module.
342  Automatically moves the gate to the top module of the netlist.
343 
344  :param hal_py.Gate gate: The gate to remove.
345  :returns: ``True`` on success, ``False`` otherwise.
346  :rtype: bool
347  )");
348 
349  py_module.def("remove_gates", &Module::remove_gates, py::arg("gates"), R"(
350  Remove a list of gates from the module.
351  Automatically moves the gates to the top module of the netlist.
352 
353  :param list[hal_py.Gate] gates: The gates to remove.
354  :returns: ``True`` on success, ``False`` otherwise.
355  :rtype: bool
356  )");
357 
358  py_module.def("contains_gate", &Module::contains_gate, py::arg("gate"), py::arg("recursive") = false, R"(
359  Check whether a gate is contained in the module.
360  If ``recursive`` is ``True``, gates in submodules are considered as well.
361 
362  :param hal_py.Gate gate: The gate to check for.
363  :param bool recursive: ``True`` to also consider gates in submodules, ``False`` otherwise.
364  :returns: ``True`` if the gate is contained in the module, ``False`` otherwise.
365  :rtype: bool
366  )");
367 
368  py_module.def("get_gate_by_id", &Module::get_gate_by_id, py::arg("id"), py::arg("recursive") = false, borrowed(), R"(
369  Get a gate specified by the given ID.
370  If ``recursive`` is ``True``, gates in submodules are considered as well.
371 
372  :param int id: The unique ID of the gate.
373  :param bool recursive: ``True`` to also consider gates in submodules, ``False`` otherwise.
374  :returns: The gate if found, ``None`` otherwise.
375  :rtype: hal_py.Gate or None
376  )");
377 
378  py_module.def_property_readonly("gates", py::cpp_function(py::overload_cast<>(&Module::get_gates, py::const_), py::is_method(py_module), borrowed()), R"(
379  The list of all gates contained within the module.
380 
381  :type: list[hal_py.Gate]
382  )");
383 
384  py_module.def("get_gates", py::overload_cast<>(&Module::get_gates, py::const_), borrowed(), R"(
385  Get all gates contained within the module.
386 
387  :returns: A list of gates.
388  :rtype: list[hal_py.Gate]
389  )");
390 
391  py_module.def("get_gates", py::overload_cast<const std::function<bool(Gate*)>&, bool>(&Module::get_gates, py::const_), py::arg("filter") = nullptr, py::arg("recursive") = false, borrowed(), R"(
392  Get all gates contained within the module.
393  The filter is evaluated on every candidate such that the result only contains those matching the specified condition.
394  If ``recursive`` is ``True``, gates in submodules are considered as well.
395 
396  :param lambda filter: Filter function to be evaluated on each gate.
397  :param bool recursive: ``True`` to also consider gates in submodules, ``False`` otherwise. Defaults to ``False``.
398  :returns: A list of gates.
399  :rtype: list[hal_py.Gate]
400  )");
401 
402  py_module.def("get_unique_pin_id", &Module::get_unique_pin_id, R"(
403  Get a spare pin ID.
404  The value of 0 is reserved and represents an invalid ID.
405 
406  :returns: The pin ID.
407  :rtype: int
408  )");
409 
410  py_module.def("get_unique_pin_group_id", &Module::get_unique_pin_group_id, R"(
411  Get a spare pin group ID.
412  The value of 0 is reserved and represents an invalid ID.
413 
414  :returns: The pin group ID.
415  :rtype: int
416  )");
417 
418  py_module.def(
419  "create_pin",
420  [](Module& self, const u32 id, const std::string& name, Net* net, PinType type = PinType::none, bool create_group = true, bool force_name = false) -> ModulePin* {
421  auto res = self.create_pin(id, name, net, type, create_group, force_name);
422  if (res.is_ok())
423  {
424  return res.get();
425  }
426  else
427  {
428  log_error("python_context", "error encountered while creating pin:\n{}", res.get_error().get());
429  return nullptr;
430  }
431  },
432  py::arg("id"),
433  py::arg("name"),
434  py::arg("net"),
435  py::arg("type") = PinType::none,
436  py::arg("create_group") = true,
437  py::arg("force_name") = false,
438  borrowed(), R"(
439  Manually create a module pin and assign it to a net.
440  Checks whether the given direction matches the actual properties of the net, i.e., checks whether the net actually is an input and/or output to the module.
441  Hence, make sure to update the module nets beforehand using ``hal_py.Module.update_nets``.
442  If ``create_group`` is set to ``False``, the pin will not be added to a pin group.
443 
444  WARNING: can only be used when automatic net checks have been disabled using ``hal_py.Netlist.enable_automatic_net_checks``.
445 
446  :param int id: The ID of the pin.
447  :param str name: The name of the pin.
448  :param hal_py.Net net: The net that the pin is being assigned to.
449  :param hal_py.PinType type: The type of the pin. Defaults to ``hal_py.PinType.none``.
450  :param bool create_group: Set ``True`` to automatically create a pin group and assign the pin, ``False`` otherwise. Defaults to ``True``.
451  :param bool force_name: Set ``True`` to enforce the name, ``False`` otherwise. If a pin with the same name already exists, that existing pin will be renamed. Defaults to ``False``.
452  :returns: The module pin on success, ``None`` otherwise.
453  :rtype: hal_py.ModulePin or None
454  )");
455 
456  py_module.def(
457  "create_pin",
458  [](Module& self, const std::string& name, Net* net, PinType type = PinType::none, bool create_group = true, bool force_name = false) -> ModulePin* {
459  auto res = self.create_pin(name, net, type, create_group, force_name);
460  if (res.is_ok())
461  {
462  return res.get();
463  }
464  else
465  {
466  log_error("python_context", "error encountered while creating pin:\n{}", res.get_error().get());
467  return nullptr;
468  }
469  },
470  py::arg("name"),
471  py::arg("net"),
472  py::arg("type") = PinType::none,
473  py::arg("create_group") = true,
474  py::arg("force_name") = false,
475  borrowed(), R"(
476  Manually create a module pin and assign it to a net.
477  The ID of the pin is set automatically.
478  Checks whether the given direction matches the actual properties of the net, i.e., checks whether the net actually is an input and/or output to the module.
479  Hence, make sure to update the module nets beforehand using ``hal_py.Module.update_nets``.
480  If ``create_group`` is set to ``False``, the pin will not be added to a pin group.
481 
482  WARNING: can only be used when automatic net checks have been disabled using ``hal_py.Netlist.enable_automatic_net_checks``.
483 
484  :param str name: The name of the pin.
485  :param hal_py.Net net: The net that the pin is being assigned to.
486  :param hal_py.PinType type: The type of the pin. Defaults to ``hal_py.PinType.none``.
487  :param bool create_group: Set ``True`` to automatically create a pin group and assign the pin, ``False`` otherwise. Defaults to ``True``.
488  :param bool force_name: Set ``True`` to enforce the name, ``False`` otherwise. If a pin with the same name already exists, that existing pin will be renamed. Defaults to ``False``.
489  :returns: The module pin on success, ``None`` otherwise.
490  :rtype: hal_py.ModulePin or None
491  )");
492 
493  py_module.def_property_readonly(
494  "pins", py::cpp_function([](const Module& self) { return self.get_pins(); }, py::is_method(py_module), borrowed()), R"(
495  The (ordered) pins of the module.
496 
497  :type: list[hal_py.ModulePin]
498  )");
499 
500  py_module.def("get_pins", &Module::get_pins, py::arg("filter") = nullptr, borrowed(), R"(
501  Get an ordered list of all pins of the module.
502  The optional filter is evaluated on every candidate such that the result only contains those matching the specified condition.
503 
504  :param lambda filter: An optional filter.
505  :returns: An ordered list of pins.
506  :rtype: list[hal_py.ModulePin]
507  )");
508 
509  py_module.def_property_readonly(
510  "pin_names",
511  [](const Module& self) -> std::vector<std::string> { return self.get_pin_names(); },
512  R"(
513  An ordered list of the names of all pins of the module.
514 
515  :type: list[str]
516  )");
517 
518  py_module.def("get_pin_names", &Module::get_pin_names, py::arg("filter") = nullptr, R"(
519  Get an ordered list of the names of all pins of the module.
520  The optional filter is evaluated on every candidate such that the result only contains those matching the specified condition.
521 
522  :returns: A list of input pin names of the module.
523  :param lambda filter: An optional filter.
524  :returns: An ordered list of pin names.
525  :rtype: list[str]
526  )");
527 
528  py_module.def_property_readonly("input_pins", py::cpp_function(&Module::get_input_pins, py::is_method(py_module), borrowed()), R"(
529  An ordered list of all input pins of the module (including inout pins).
530 
531  :type: list[hal_py.ModulePin]
532  )");
533 
534  py_module.def("get_input_pins", &Module::get_input_pins, borrowed(), R"(
535  Get an ordered list of all input pins of the module (including inout pins).
536 
537  :returns: An ordered list of input pins.
538  :rtype: list[hal_py.ModulePin]
539  )");
540 
541  py_module.def_property_readonly("input_pin_names", &Module::get_input_pin_names, R"(
542  An ordered list of the names of all input pins of the module (including inout pins).
543 
544  :type: list[str]
545  )");
546 
547  py_module.def("get_input_pin_names", &Module::get_input_pin_names, R"(
548  Get an ordered list of the names of all input pins of the module (including inout pins).
549 
550  :returns: An ordered list of input pin names.
551  :rtype: list[str]
552  )");
553 
554  py_module.def_property_readonly("output_pins", py::cpp_function(&Module::get_output_pins, py::is_method(py_module), borrowed()), R"(
555  An ordered list of all output pins of the module (including inout pins).
556 
557  :type: list[hal_py.ModulePin]
558  )");
559 
560  py_module.def("get_output_pins", &Module::get_output_pins, borrowed(), R"(
561  Get an ordered list of all output pins of the module (including inout pins).
562 
563  :returns: An ordered list of output pins.
564  :rtype: list[hal_py.ModulePin]
565  )");
566 
567  py_module.def_property_readonly("output_pin_names", &Module::get_output_pin_names, R"(
568  An ordered list of the names of all output pins of the module (including inout pins).
569 
570  :type: list[str]
571  )");
572 
573  py_module.def("get_output_pin_names", &Module::get_output_pin_names, R"(
574  Get an ordered list of the names of all output pins of the module (including inout pins).
575 
576  :returns: An ordered list of output pin names.
577  :rtype: list[str]
578  )");
579 
580  py_module.def_property_readonly(
581  "pin_groups", py::cpp_function([](const Module& self) { return self.get_pin_groups(); }, py::is_method(py_module), borrowed()), R"(
582  All pin_groups of the module.
583 
584  :type: list[hal_py.ModulePinGroup]
585  )");
586 
587  py_module.def("get_pin_groups", &Module::get_pin_groups, py::arg("filter") = nullptr, borrowed(), R"(
588  Get all pin groups of the module.
589  The optional filter is evaluated on every candidate such that the result only contains those matching the specified condition.
590 
591  :param lambda filter: An optional filter.
592  :returns: A list of pin groups.
593  :rtype: list[hal_py.ModulePinGroup]
594  )");
595 
596  py_module.def("get_pin_by_id", &Module::get_pin_by_id, py::arg("id"), borrowed(), R"(
597  Get the pin corresponding to the given ID.
598 
599  :param int id: The ID of the pin.
600  :returns: The pin on success, ``None`` otherwise.
601  :rtype: hal_py.ModulePin or None
602  )");
603 
604  py_module.def("get_pin_by_name", &Module::get_pin_by_name, py::arg("name"), borrowed(), R"(
605  Get the pin corresponding to the given name.
606 
607  :param str name: The name of the pin.
608  :returns: The pin on success, ``None`` otherwise.
609  :rtype: hal_py.ModulePin or None
610  )");
611 
612  py_module.def("get_pin_by_net", &Module::get_pin_by_net, py::arg("net"), borrowed(), R"(
613  Get the pin that passes through the specified net.
614 
615  :param hal_py.Net net: The net.
616  :returns: The pin on success, ``None`` otherwise.
617  :rtype: hal_py.ModulePin or None
618  )");
619 
620  py_module.def("get_pin_group_by_id", &Module::get_pin_group_by_id, py::arg("id"), borrowed(), R"(
621  Get the pin group corresponding to the given ID.
622 
623  :param int id: The ID of the pin group.
624  :returns: The pin group on success, ``None`` otherwise.
625  :rtype: hal_py.ModulePinGroup or None
626  )");
627 
628  py_module.def("get_pin_group_by_name", &Module::get_pin_group_by_name, py::arg("name"), borrowed(), R"(
629  Get the pin group corresponding to the given name.
630 
631  :param str name: The name of the pin group.
632  :returns: The pin group on success, ``None`` otherwise.
633  :rtype: hal_py.ModulePinGroup or None
634  )");
635 
636  py_module.def("set_pin_name", &Module::set_pin_name, py::arg("pin"), py::arg("new_name"), py::arg("force_name") = false, R"(
637  Set the name of the given pin.
638 
639  :param hal_py.ModulePin pin: The pin.
640  :param str new_name: The name to be assigned to the pin.
641  :param bool force_name: Set ``True`` to enforce the name, ``False`` otherwise. If a pin with the same name already exists, that existing pin will be renamed. Defaults to ``False``.
642  :returns: ``True`` on success, ``False`` otherwise.
643  :rtype: bool
644  )");
645 
646  py_module.def("set_pin_group_name", &Module::set_pin_group_name, py::arg("pin_group"), py::arg("new_name"), py::arg("force_name") = false, R"(
647  Set the name of the given pin group.
648 
649  :param hal_py.ModulePinGroup pin_group: The pin group.
650  :param str new_name: The name to be assigned to the pin group.
651  :param bool force_name: Set ``True`` to enforce the name, ``False`` otherwise. If a pin group with the same name already exists, the existing pin group will be renamed. Defaults to ``False``.
652  :returns: ``True`` on success, ``False`` otherwise.
653  :rtype: bool
654  )");
655 
656  py_module.def("set_pin_type", &Module::set_pin_type, py::arg("pin"), py::arg("new_type"), R"(
657  Set the type of the given pin.
658 
659  :param hal_py.ModulePin pin: The pin.
660  :param hal_py.PinType new_type: The type to be assigned to the pin.
661  :returns: ``True`` on success, ``False`` otherwise.
662  :rtype: bool
663  )");
664 
665  py_module.def("set_pin_group_type", &Module::set_pin_group_type, py::arg("pin_group"), py::arg("new_type"), R"(
666  Set the type of the given pin group.
667 
668  :param hal_py.ModulePinGroup pin_group: The pin group.
669  :param hal_py.PinType new_type: The type to be assigned to the pin group.
670  :returns: ``True`` on success, ``False`` otherwise.
671  :rtype: bool
672  )");
673 
674  py_module.def("set_pin_group_direction", &Module::set_pin_group_direction, py::arg("pin_group"), py::arg("new_direction"), R"(
675  Set the direction of the given pin group.
676 
677  :param hal_py.ModulePinGroup pin_group: The pin group.
678  :param hal_py.PinDirection new_direction: The direction to be assigned to the pin group.
679  :returns: ``True`` on success, ``False`` otherwise.
680  :rtype: bool
681  )");
682 
683  py_module.def(
684  "create_pin_group",
685  [](Module& self,
686  const u32 id,
687  const std::string& name,
688  const std::vector<ModulePin*> pins = {},
691  bool ascending = false,
692  u32 start_index = UINT_MAX, // placeholder for default depending on ascending <-> descending
693  bool delete_empty_groups = true,
694  bool force_name = false) -> PinGroup<ModulePin>* {
695  if (start_index == UINT_MAX)
696  {
697  if (ascending || pins.empty())
698  start_index = 0;
699  else
700  start_index = pins.size() - 1;
701  }
702  auto res = self.create_pin_group(id, name, pins, direction, type, ascending, start_index, delete_empty_groups, force_name);
703  if (res.is_ok())
704  {
705  return res.get();
706  }
707  else
708  {
709  log_error("python_context", "error encountered while creating pin group:\n{}", res.get_error().get());
710  return nullptr;
711  }
712  },
713  py::arg("id"),
714  py::arg("name"),
715  py::arg("pins") = std::vector<ModulePin*>(),
716  py::arg("direction") = PinDirection::none,
717  py::arg("type") = PinType::none,
718  py::arg("ascending") = false,
719  py::arg("start_index") = UINT_MAX,
720  py::arg("delete_empty_groups") = true,
721  py::arg("force_name") = false,
722  borrowed(), R"(
723  Create a new pin group with the given name.
724  All pins to be added to the pin group must have the same direction and type.
725 
726  :param int id: The ID of the pin group.
727  :param str name: The name of the pin group.
728  :param list[hal_py.ModulePin] pins: The pins to be assigned to the pin group. Defaults to an empty list.
729  :param hal_py.PinDirection direction: The direction of the pin group, if any. Defaults to ``hal_py.PinDirection.none``.
730  :param hal_py.PinType type: The type of the pin group, if any. Defaults to ``hal_py.PinType.none``.
731  :param bool ascending: Set ``True`` for ascending pin order (from 0 to n-1), ``False`` otherwise (from n-1 to 0). Defaults to ``True``.
732  :param int start_index: The start index of the pin group. Defaults to ``0``.
733  :param bool delete_empty_groups: Set ``True`` to delete groups that are empty after the pins have been assigned to the new group, ``False`` to keep empty groups. Defaults to ``True``.
734  :param bool force_name: Set ``True`` to enforce the name, ``False`` otherwise. If a pin group with the same name already exists, the existing pin group will be renamed. Defaults to ``False``.
735  :returns: The pin group on success, ``None`` otherwise.
736  :rtype: hal_py.ModulePinGroup or None
737  )");
738 
739  py_module.def(
740  "create_pin_group",
741  [](Module& self,
742  const std::string& name,
743  const std::vector<ModulePin*> pins = {},
746  bool ascending = false,
747  u32 start_index = UINT_MAX, // placeholder for default depending on ascending <-> descending
748  bool delete_empty_groups = true,
749  bool force_name = false) -> PinGroup<ModulePin>* {
750  if (start_index == UINT_MAX)
751  {
752  if (ascending || pins.empty())
753  start_index = 0;
754  else
755  start_index = pins.size() - 1;
756  }
757  auto res = self.create_pin_group(name, pins, direction, type, ascending, start_index, delete_empty_groups, force_name);
758  if (res.is_ok())
759  {
760  return res.get();
761  }
762  else
763  {
764  log_error("python_context", "error encountered while creating pin group:\n{}", res.get_error().get());
765  return nullptr;
766  }
767  },
768  py::arg("name"),
769  py::arg("pins") = std::vector<ModulePin*>(),
770  py::arg("direction") = PinDirection::none,
771  py::arg("type") = PinType::none,
772  py::arg("ascending") = false,
773  py::arg("start_index") = UINT_MAX,
774  py::arg("delete_empty_groups") = true,
775  py::arg("force_name") = false,
776  borrowed(), R"(
777  Create a new pin group with the given name.
778  All pins to be added to the pin group must have the same direction and type.
779 
780  :param str name: The name of the pin group.
781  :param list[hal_py.ModulePin] pins: The pins to be assigned to the pin group. Defaults to an empty list.
782  :param hal_py.PinDirection direction: The direction of the pin group, if any. Defaults to ``hal_py.PinDirection.none``.
783  :param hal_py.PinType type: The type of the pin group, if any. Defaults to ``hal_py.PinType.none``.
784  :param bool ascending: Set ``True`` for ascending pin order (from 0 to n-1), ``False`` otherwise (from n-1 to 0). Defaults to ``True``.
785  :param int start_index: The start index of the pin group. Defaults to ``0``.
786  :param bool delete_empty_groups: Set ``True``` to delete groups that are empty after the pins have been assigned to the new group, ```False``` to keep empty groups. Defaults to ```True```.
787  :param bool force_name: Set ``True`` to enforce the name, ``False`` otherwise. If a pin group with the same name already exists, the existing pin group will be renamed. Defaults to ``False``.
788  :returns: The pin group on success, ``None`` otherwise.
789  :rtype: hal_py.ModulePinGroup or None
790  )");
791 
792  py_module.def(
793  "delete_pin_group",
794  [](Module& self, PinGroup<ModulePin>* pin_group) {
795  if (self.delete_pin_group(pin_group))
796  {
797  return true;
798  }
799  else
800  {
801  log_error("python_context", "error encountered while deleting pin group.");
802  return false;
803  }
804  },
805  py::arg("pin_group"),
806  R"(
807  Delete the given pin group.
808 
809  :param hal_py.ModulePinGroup pin_group: The pin group to be deleted.
810  :returns: ``True`` on success, ``False`` otherwise.
811  :rtype: bool
812  )");
813 
814  py_module.def(
815  "move_pin_group",
816  [](Module& self, PinGroup<ModulePin>* pin_group, u32 new_index) {
817  if (self.move_pin_group(pin_group, new_index))
818  {
819  return true;
820  }
821  else
822  {
823  log_error("python_context", "error encountered while moving pin group.");
824  return false;
825  }
826  },
827  py::arg("pin_group"),
828  py::arg("new_index"),
829  R"(
830  Move a pin group to another index within the module.
831  The indices of some other pin groups will be incremented or decremented to make room for the moved pin group to be inserted at the desired position.
832 
833  :param hal_py.ModulePinGroup pin_group: The pin group to be moved.
834  :param int new_index: The index to which the pin group is moved.
835  :returns: ``True`` on success, ``False`` otherwise.
836  :rtype: bool
837  )");
838 
839  py_module.def(
840  "assign_pin_to_group",
841  [](Module& self, PinGroup<ModulePin>* pin_group, ModulePin* pin, bool delete_empty_groups = true) {
842  if (self.assign_pin_to_group(pin_group, pin, delete_empty_groups))
843  {
844  return true;
845  }
846  else
847  {
848  log_error("python_context", "error encountered while assigning pin to pin group.");
849  return false;
850  }
851  },
852  py::arg("pin_group"),
853  py::arg("pin"),
854  py::arg("delete_empty_groups") = true,
855  R"(
856  Assign a pin to a pin group.
857 
858  :param hal_py.ModulePinGroup pin_group: The new pin group.
859  :param hal_py.ModulePin pin: The pin to be added.
860  :param bool delete_empty_groups: Set ``True`` to delete groups that are empty after the pin has been assigned to the new group, ``False`` to keep empty groups. Defaults to ``True``.
861  :returns: ``True`` on success, ``False`` otherwise.
862  :rtype: bool
863  )");
864 
865  py_module.def(
866  "move_pin_within_group",
867  [](Module& self, PinGroup<ModulePin>* pin_group, ModulePin* pin, u32 new_index) {
868  if (self.move_pin_within_group(pin_group, pin, new_index))
869  {
870  return true;
871  }
872  else
873  {
874  log_error("python_context", "error encountered while moving pin within pin group.");
875  return false;
876  }
877  },
878  py::arg("pin_group"),
879  py::arg("pin"),
880  py::arg("new_index"),
881  R"(
882  Move a pin to another index within the given pin group.
883  The indices of some other pins within the group will be incremented or decremented to make room for the moved pin to be inserted at the desired position.
884 
885  :param hal_py.ModulePinGroup pin_group: The pin group.
886  :param hal_py.ModulePin pin: The pin to be moved.
887  :param int new_index: The index to which the pin is moved.
888  :returns: ``True`` on success, ``False`` otherwise.
889  :rtype: bool
890  )");
891 
892  py_module.def(
893  "remove_pin_from_group",
894  [](Module& self, PinGroup<ModulePin>* pin_group, ModulePin* pin, bool delete_empty_groups = true) {
895  if (self.remove_pin_from_group(pin_group, pin, delete_empty_groups))
896  {
897  return true;
898  }
899  else
900  {
901  log_error("python_context", "error encountered while removing pin from pin group.");
902  return false;
903  }
904  },
905  py::arg("pin_group"),
906  py::arg("pin"),
907  py::arg("delete_empty_groups") = true,
908  R"(
909  Remove a pin from a pin group.
910  The pin will be moved to a new group that goes by the pin's name.
911 
912  :param hal_py.ModulePinGroup pin_group: The old pin group.
913  :param hal_py.ModulePin pin: The pin to be removed.
914  :param bool delete_empty_groups: Set ``True`` to delete the group of it is empty after the pin has been removed, ``False`` to keep the empty group. Defaults to ``True``.
915  :returns: ``True`` on success, ``False`` otherwise.
916  :rtype: bool
917  )");
918  }
919 } // namespace hal
Definition: gate.h:58
bool is_input_net(Net *net) const
Definition: module.cpp:557
void set_name(const std::string &name)
Definition: module.cpp:92
const std::unordered_set< Net * > & get_nets() const
Definition: module.cpp:505
void update_nets()
Definition: module.cpp:436
std::vector< std::string > get_pin_names(const std::function< bool(ModulePin *)> &filter=nullptr) const
Definition: module.cpp:901
PinGroup< ModulePin > * get_pin_group_by_name(const std::string &name) const
Definition: module.cpp:1053
Module * get_parent_module() const
Definition: module.cpp:125
bool is_parent_module_of(const Module *module, bool recursive=false) const
Definition: module.cpp:240
std::vector< ModulePin * > get_output_pins() const
Definition: module.cpp:948
bool set_parent_module(Module *new_parent)
Definition: module.cpp:170
bool remove_gate(Gate *gate)
Definition: module.cpp:334
std::vector< ModulePin * > get_pins(const std::function< bool(ModulePin *)> &filter=nullptr) const
Definition: module.cpp:873
std::vector< ModulePin * > get_input_pins() const
Definition: module.cpp:932
bool assign_gates(const std::vector< Gate * > &gates)
Definition: module.cpp:329
bool contains_net(Net *net, bool recursive=false) const
Definition: module.cpp:485
Gate * get_gate_by_id(const u32 id, bool recursive=false) const
Definition: module.cpp:372
const std::unordered_set< Net * > & get_internal_nets() const
Definition: module.cpp:552
ModulePin * get_pin_by_name(const std::string &name) const
Definition: module.cpp:1002
int get_submodule_depth() const
Definition: module.cpp:159
bool is_top_module() const
Definition: module.cpp:314
bool remove_gates(const std::vector< Gate * > &gates)
Definition: module.cpp:339
ModulePin * get_pin_by_id(const u32 id) const
Definition: module.cpp:985
bool set_pin_group_type(PinGroup< ModulePin > *pin_group, PinType new_type)
Definition: module.cpp:1203
bool set_pin_type(ModulePin *pin, PinType new_type)
Definition: module.cpp:1124
bool assign_gate(Gate *gate)
Definition: module.cpp:324
bool is_output_net(Net *net) const
Definition: module.cpp:567
const std::vector< Gate * > & get_gates() const
Definition: module.cpp:393
bool set_pin_name(ModulePin *pin, const std::string &new_name, bool force_name=false)
Definition: module.cpp:1070
std::string get_name() const
Definition: module.cpp:87
Grouping * get_grouping() const
Definition: module.cpp:120
const std::unordered_set< Net * > & get_input_nets() const
Definition: module.cpp:542
std::vector< std::string > get_input_pin_names() const
Definition: module.cpp:940
bool set_pin_group_direction(PinGroup< ModulePin > *pin_group, PinDirection new_direction)
Definition: module.cpp:1226
void set_type(const std::string &type)
Definition: module.cpp:111
u32 get_unique_pin_id()
Definition: module.cpp:763
bool contains_module(const Module *other, bool recursive=false) const
Definition: module.cpp:309
std::vector< Module * > get_parent_modules(const std::function< bool(Module *)> &filter=nullptr, bool recursive=true) const
Definition: module.cpp:130
Netlist * get_netlist() const
Definition: module.cpp:319
ModulePin * get_pin_by_net(Net *net) const
Definition: module.cpp:1019
bool set_pin_group_name(PinGroup< ModulePin > *pin_group, const std::string &new_name, bool force_name=false)
Definition: module.cpp:1147
std::vector< PinGroup< ModulePin > * > get_pin_groups(const std::function< bool(PinGroup< ModulePin > *)> &filter=nullptr) const
Definition: module.cpp:964
PinGroup< ModulePin > * get_pin_group_by_id(const u32 id) const
Definition: module.cpp:1036
ssize_t get_hash() const
Definition: module.cpp:77
std::vector< Module * > get_submodules(const std::function< bool(Module *)> &filter=nullptr, bool recursive=false) const
Definition: module.cpp:261
std::vector< std::string > get_output_pin_names() const
Definition: module.cpp:956
bool contains_gate(Gate *gate, bool recursive=false) const
Definition: module.cpp:352
std::string get_type() const
Definition: module.cpp:106
bool is_internal_net(Net *net) const
Definition: module.cpp:577
bool is_submodule_of(const Module *module, bool recursive=false) const
Definition: module.cpp:291
const std::unordered_set< Net * > & get_output_nets() const
Definition: module.cpp:547
u32 get_id() const
Definition: module.cpp:82
u32 get_unique_pin_group_id()
Definition: module.cpp:776
Definition: net.h:58
uint32_t u32
Definition: defines.h:41
std::unique_ptr< T, py::nodelete > RawPtrWrapper
void module_init(py::module &m)
Definition: module.cpp:5
#define log_error(channel,...)
Definition: log.h:78
const Module * module(const Gate *g, const NodeBoxes &boxes)
Definition: defines.h:45
PinDirection
Definition: pin_direction.h:36
PinType
Definition: pin_type.h:36
PinType type
std::vector< PinInformation > pins
Net * net
u32 start_index
bool ascending
PinDirection direction
std::string name