HAL
netlist.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"
31 
32 #include <functional>
33 #include <memory>
34 #include <string>
35 #include <tuple>
36 #include <type_traits>
37 #include <unordered_map>
38 #include <unordered_set>
39 
40 namespace hal
41 {
43  class NetlistInternalManager;
44  class Net;
45  class Gate;
46  class Module;
47  class Grouping;
48  class Endpoint;
49 
56  {
57  friend class NetlistInternalManager;
58 
59  public:
66  explicit Netlist(const GateLibrary* gate_library);
67 
68  ~Netlist();
69 
77  bool operator==(const Netlist& other) const;
78 
86  bool operator!=(const Netlist& other) const;
87 
94  u32 get_id() const;
95 
101  void set_id(const u32 id);
102 
108  std::filesystem::path get_input_filename() const;
109 
115  void set_input_filename(const std::filesystem::path& path);
116 
122  const std::string& get_design_name() const;
123 
129  void set_design_name(const std::string& name);
130 
136  const std::string& get_device_name() const;
137 
143  void set_device_name(const std::string& name);
144 
150  const GateLibrary* get_gate_library() const;
151 
157  Result<std::unique_ptr<Netlist>> copy() const;
158 
159  /*
160  * ################################################################
161  * gate functions
162  * ################################################################
163  */
164 
171  u32 get_unique_gate_id();
172 
183  Gate* create_gate(const u32 gate_id, GateType* gate_type, const std::string& name = "", i32 x = -1, i32 y = -1);
184 
195  Gate* create_gate(GateType* gate_type, const std::string& name = "", i32 x = -1, i32 y = -1);
196 
203  bool delete_gate(Gate* gate);
204 
211  bool is_gate_in_netlist(const Gate* gate) const;
212 
219  Gate* get_gate_by_id(const u32 gate_id) const;
220 
226  const std::vector<Gate*>& get_gates() const;
227 
235  std::vector<Gate*> get_gates(const std::function<bool(const Gate*)>& filter) const;
236 
243  bool mark_vcc_gate(Gate* gate);
244 
251  bool mark_gnd_gate(Gate* gate);
252 
259  bool unmark_vcc_gate(Gate* gate);
260 
267  bool unmark_gnd_gate(Gate* gate);
268 
275  bool is_vcc_gate(const Gate* gate) const;
276 
283  bool is_gnd_gate(const Gate* gate) const;
284 
290  const std::vector<Gate*>& get_vcc_gates() const;
291 
297  const std::vector<Gate*>& get_gnd_gates() const;
298 
299  /*
300  * ################################################################
301  * net functions
302  * ################################################################
303  */
304 
311  u32 get_unique_net_id();
312 
320  Net* create_net(const u32 net_id, const std::string& name);
321 
329  Net* create_net(const std::string& name);
330 
337  bool delete_net(Net* net);
338 
345  bool is_net_in_netlist(const Net* net) const;
346 
353  Net* get_net_by_id(u32 net_id) const;
354 
360  const std::vector<Net*>& get_nets() const;
361 
369  std::vector<Net*> get_nets(const std::function<bool(const Net*)>& filter) const;
370 
377  bool mark_global_input_net(Net* net);
378 
385  bool mark_global_output_net(Net* net);
386 
393  bool unmark_global_input_net(Net* net);
394 
401  bool unmark_global_output_net(Net* net);
402 
409  bool is_global_input_net(const Net* net) const;
410 
417  bool is_global_output_net(const Net* net) const;
418 
424  const std::vector<Net*>& get_global_input_nets() const;
425 
431  const std::vector<Net*>& get_global_output_nets() const;
432 
438  std::vector<Net*> get_gnd_nets() const;
439 
445  std::vector<Net*> get_vcc_nets() const;
446 
453  void enable_automatic_net_checks(bool enable_checks = true);
454 
455  /*
456  * ################################################################
457  * module functions
458  * ################################################################
459  */
460 
467  u32 get_unique_module_id();
468 
478  Module* create_module(const u32 module_id, const std::string& name, Module* parent, const std::vector<Gate*>& gates = {});
479 
489  Module* create_module(const std::string& name, Module* parent, const std::vector<Gate*>& gates = {});
490 
498  bool delete_module(Module* module);
499 
506  bool is_module_in_netlist(const Module* module) const;
507 
514  Module* get_module_by_id(u32 module_id) const;
515 
521  const std::vector<Module*>& get_modules() const;
522 
530  std::vector<Module*> get_modules(const std::function<bool(const Module*)>& filter) const;
531 
537  Module* get_top_module() const;
538 
539  /*
540  * ################################################################
541  * grouping functions
542  * ################################################################
543  */
544 
551  u32 get_unique_grouping_id();
552 
560  Grouping* create_grouping(const u32 grouping_id, const std::string& name = "");
561 
569  Grouping* create_grouping(const std::string& name = "");
570 
577  bool delete_grouping(Grouping* grouping);
578 
585  bool is_grouping_in_netlist(const Grouping* grouping) const;
586 
593  Grouping* get_grouping_by_id(u32 grouping_id) const;
594 
600  const std::vector<Grouping*>& get_groupings() const;
601 
609  std::vector<Grouping*> get_groupings(const std::function<bool(const Grouping*)>& filter) const;
610 
611  /*
612  * ################################################################
613  * getter/setter for ID tracking
614  * ################################################################
615  */
616 
622  u32 get_next_gate_id() const;
623 
629  void set_next_gate_id(const u32 id);
630 
636  std::set<u32> get_used_gate_ids() const;
637 
643  void set_used_gate_ids(const std::set<u32> ids);
644 
650  std::set<u32> get_free_gate_ids() const;
651 
657  void set_free_gate_ids(const std::set<u32> ids);
658 
664  u32 get_next_net_id() const;
665 
671  void set_next_net_id(const u32 id);
672 
678  std::set<u32> get_used_net_ids() const;
679 
685  void set_used_net_ids(const std::set<u32> ids);
686 
692  std::set<u32> get_free_net_ids() const;
693 
699  void set_free_net_ids(const std::set<u32> ids);
700 
706  u32 get_next_module_id() const;
707 
713  void set_next_module_id(const u32 id);
714 
720  std::set<u32> get_used_module_ids() const;
721 
727  void set_used_module_ids(const std::set<u32> ids);
728 
734  std::set<u32> get_free_module_ids() const;
735 
741  void set_free_module_ids(const std::set<u32> ids);
742 
748  u32 get_next_grouping_id() const;
749 
755  void set_next_grouping_id(const u32 id);
756 
762  std::set<u32> get_used_grouping_ids() const;
763 
769  void set_used_grouping_ids(const std::set<u32> ids);
770 
776  std::set<u32> get_free_grouping_ids() const;
777 
783  void set_free_grouping_ids(const std::set<u32> ids);
784 
790  EventHandler* get_event_handler() const;
791 
792  /*
793  * ################################################################
794  * utility functions
795  * ################################################################
796  */
797 
802  void clear_caches();
803 
812  bool load_gate_locations_from_data(const std::string& data_category = "", const std::pair<std::string, std::string>& data_identifiers = std::pair<std::string, std::string>());
813 
814  private:
815  /* stores the gate library */
816  const GateLibrary* m_gate_library;
817 
818  /* stores the netlist id */
819  u32 m_netlist_id;
820 
821  /* stores the name of the input file */
822  std::filesystem::path m_file_name;
823 
824  /* stores the name of the design */
825  std::string m_design_name;
826 
827  /* stores the name of the device */
828  std::string m_device_name;
829 
830  /* stores the pointer to the netlist internal manager */
831  NetlistInternalManager* m_manager;
832 
833  /* the event handler associated with the netlist */
834  std::unique_ptr<EventHandler> m_event_handler;
835 
836  /* stores the auto generated ids for fast next id */
837  u32 m_next_gate_id;
838  std::set<u32> m_used_gate_ids;
839  std::set<u32> m_free_gate_ids;
840  u32 m_next_net_id;
841  std::set<u32> m_used_net_ids;
842  std::set<u32> m_free_net_ids;
843  u32 m_next_module_id;
844  std::set<u32> m_used_module_ids;
845  std::set<u32> m_free_module_ids;
846  u32 m_next_grouping_id;
847  std::set<u32> m_used_grouping_ids;
848  std::set<u32> m_free_grouping_ids;
849 
850  /* stores the modules */
851  Module* m_top_module;
852  std::unordered_map<u32, std::unique_ptr<Module>> m_modules_map;
853  std::unordered_set<const Module*> m_modules_set;
854  std::vector<Module*> m_modules;
855 
856  /* stores the nets */
857  std::unordered_map<u32, std::unique_ptr<Net>> m_nets_map;
858  std::unordered_set<const Net*> m_nets_set;
859  std::vector<Net*> m_nets;
860 
861  /* stores the gates */
862  std::unordered_map<u32, std::unique_ptr<Gate>> m_gates_map;
863  std::unordered_set<const Gate*> m_gates_set;
864  std::vector<Gate*> m_gates;
865 
866  /* stores the groupings */
867  std::unordered_map<u32, std::unique_ptr<Grouping>> m_groupings_map;
868  std::unordered_set<const Grouping*> m_groupings_set;
869  std::vector<Grouping*> m_groupings;
870 
871  /* stores the set of global gates and nets */
872  std::vector<Net*> m_global_input_nets;
873  std::vector<Net*> m_global_output_nets;
874  std::vector<Gate*> m_gnd_gates;
875  std::vector<Gate*> m_vcc_gates;
876  };
877 } // namespace hal
#define NETLIST_API
Definition: arch_linux.h:30
Definition: gate.h:58
Definition: net.h:58
int32_t i32
Definition: defines.h:36
const Module * module(const Gate *g, const NodeBoxes &boxes)
GateLibrary * get_gate_library(const std::string &file_path)
quint32 u32
Net * net
std::string name