HAL
module.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"
39 
40 #include <functional>
41 #include <list>
42 #include <map>
43 #include <memory>
44 #include <string>
45 #include <tuple>
46 #include <type_traits>
47 #include <unordered_map>
48 #include <unordered_set>
49 
50 namespace hal
51 {
53  class Netlist;
54  class NetlistInternalManager;
55  class Net;
56  class Grouping;
57  class Gate;
58 
66  {
67  public:
75  bool operator==(const Module& other) const;
76 
84  bool operator!=(const Module& other) const;
85 
91  ssize_t get_hash() const;
92 
98  u32 get_id() const;
99 
105  std::string get_name() const;
106 
112  void set_name(const std::string& name);
113 
119  std::string get_type() const;
120 
126  void set_type(const std::string& type);
127 
133  Grouping* get_grouping() const;
134 
140  int get_submodule_depth() const;
141 
148  Module* get_parent_module() const;
149 
159  std::vector<Module*> get_parent_modules(const std::function<bool(Module*)>& filter = nullptr, bool recursive = true) const;
160 
168  bool set_parent_module(Module* new_parent);
169 
177  bool is_parent_module_of(const Module* module, bool recursive = false) const;
178 
188  std::vector<Module*> get_submodules(const std::function<bool(Module*)>& filter = nullptr, bool recursive = false) const;
189 
197  bool is_submodule_of(const Module* module, bool recursive = false) const;
198 
207  bool contains_module(const Module* other, bool recursive = false) const;
208 
214  bool is_top_module() const;
215 
221  Netlist* get_netlist() const;
222 
223  /*
224  * ################################################################
225  * net functions
226  * ################################################################
227  */
228 
234  void update_nets();
235 
244  bool contains_net(Net* net, bool recursive = false) const;
245 
251  const std::unordered_set<Net*>& get_nets() const;
252 
262  std::unordered_set<Net*> get_nets(const std::function<bool(Net*)>& filter, bool recursive = false) const;
263 
269  const std::unordered_set<Net*>& get_input_nets() const;
270 
276  const std::unordered_set<Net*>& get_output_nets() const;
277 
283  const std::unordered_set<Net*>& get_internal_nets() const;
284 
291  bool is_input_net(Net* net) const;
292 
299  bool is_output_net(Net* net) const;
300 
307  bool is_internal_net(Net* net) const;
308 
309  /*
310  * ################################################################
311  * pin functions
312  * ################################################################
313  */
314 
321  u32 get_unique_pin_id();
322 
329  u32 get_unique_pin_group_id();
330 
346  Result<ModulePin*> create_pin(const u32 id, const std::string& name, Net* net, PinType type = PinType::none, bool create_group = true, bool force_name = false);
347 
363  Result<ModulePin*> create_pin(const std::string& name, Net* net, PinType type = PinType::none, bool create_group = true, bool force_name = false);
364 
372  std::vector<ModulePin*> get_pins(const std::function<bool(ModulePin*)>& filter = nullptr) const;
373 
381  std::vector<std::string> get_pin_names(const std::function<bool(ModulePin*)>& filter = nullptr) const;
382 
388  std::vector<ModulePin*> get_input_pins() const;
389 
395  std::vector<std::string> get_input_pin_names() const;
396 
402  std::vector<ModulePin*> get_output_pins() const;
403 
409  std::vector<std::string> get_output_pin_names() const;
410 
418  std::vector<PinGroup<ModulePin>*> get_pin_groups(const std::function<bool(PinGroup<ModulePin>*)>& filter = nullptr) const;
419 
426  ModulePin* get_pin_by_id(const u32 id) const;
427 
434  ModulePin* get_pin_by_name(const std::string& name) const;
435 
442  ModulePin* get_pin_by_net(Net* net) const;
443 
450  PinGroup<ModulePin>* get_pin_group_by_id(const u32 id) const;
451 
458  PinGroup<ModulePin>* get_pin_group_by_name(const std::string& name) const;
459 
468  bool set_pin_name(ModulePin* pin, const std::string& new_name, bool force_name = false);
469 
477  bool set_pin_type(ModulePin* pin, PinType new_type);
478 
493  Result<PinGroup<ModulePin>*> create_pin_group(const u32 id,
494  const std::string& name,
495  const std::vector<ModulePin*> pins = {},
498  bool ascending = true,
499  u32 start_index = 0,
500  bool delete_empty_groups = true,
501  bool force_name = false);
502 
517  Result<PinGroup<ModulePin>*> create_pin_group(const std::string& name,
518  const std::vector<ModulePin*> pins = {},
521  bool ascending = true,
522  u32 start_index = 0,
523  bool delete_empty_groups = true,
524  bool force_name = false);
525 
532  bool delete_pin_group(PinGroup<ModulePin>* pin_group);
533 
542  bool move_pin_group(PinGroup<ModulePin>* pin_group, u32 new_index);
543 
552  bool set_pin_group_name(PinGroup<ModulePin>* pin_group, const std::string& new_name, bool force_name = false);
553 
561  bool set_pin_group_type(PinGroup<ModulePin>* pin_group, PinType new_type);
562 
570  bool set_pin_group_direction(PinGroup<ModulePin>* pin_group, PinDirection new_direction);
571 
580  bool assign_pin_to_group(PinGroup<ModulePin>* pin_group, ModulePin* pin, bool delete_empty_groups = true);
581 
591  bool move_pin_within_group(PinGroup<ModulePin>* pin_group, ModulePin* pin, u32 new_index);
592 
602  bool remove_pin_from_group(PinGroup<ModulePin>* pin_group, ModulePin* pin, bool delete_empty_groups = true);
603 
604  /*
605  * ################################################################
606  * gate functions
607  * ################################################################
608  */
609 
617  bool assign_gate(Gate* gate);
618 
626  bool assign_gates(const std::vector<Gate*>& gates);
627 
635  bool remove_gate(Gate* gate);
636 
644  bool remove_gates(const std::vector<Gate*>& gates);
645 
654  bool contains_gate(Gate* gate, bool recursive = false) const;
655 
664  Gate* get_gate_by_id(const u32 id, bool recursive = false) const;
665 
671  const std::vector<Gate*>& get_gates() const;
672 
682  std::vector<Gate*> get_gates(const std::function<bool(Gate*)>& filter, bool recursive = false) const;
683 
688  EventHandler* get_event_handler() const;
689 
690  private:
692  Module(NetlistInternalManager* internal_manager, EventHandler* event_handler, u32 id, Module* parent, const std::string& name);
693 
694  Module(const Module&) = delete; //disable copy-constructor
695  Module& operator=(const Module&) = delete; //disable copy-assignment
696 
697  struct NetConnectivity
698  {
699  bool has_internal_source;
700  bool has_internal_destination;
701  bool has_external_source;
702  bool has_external_destination;
703  };
704 
705  std::string m_name;
706  std::string m_type;
707 
708  NetlistInternalManager* m_internal_manager;
709  u32 m_id;
710 
711  /* grouping */
712  Grouping* m_grouping = nullptr;
713 
714  Module* m_parent;
715  std::unordered_map<u32, Module*> m_submodules_map;
716  std::vector<Module*> m_submodules;
717 
718  // pins
719  u32 m_next_pin_id;
720  std::set<u32> m_used_pin_ids;
721  std::set<u32> m_free_pin_ids;
722  u32 m_next_pin_group_id;
723  std::set<u32> m_used_pin_group_ids;
724  std::set<u32> m_free_pin_group_ids;
725 
726  u32 m_next_input_index = 0;
727  u32 m_next_inout_index = 0;
728  u32 m_next_output_index = 0;
729 
730  std::vector<std::unique_ptr<ModulePin>> m_pins;
731  std::unordered_map<u32, ModulePin*> m_pins_map;
732  std::unordered_map<std::string, ModulePin*> m_pin_names_map;
733  std::vector<std::unique_ptr<PinGroup<ModulePin>>> m_pin_groups;
734  std::unordered_map<u32, PinGroup<ModulePin>*> m_pin_groups_map;
735  std::unordered_map<std::string, PinGroup<ModulePin>*> m_pin_group_names_map;
736  std::list<PinGroup<ModulePin>*> m_pin_groups_ordered;
737 
738  /* stores gates sorted by id */
739  std::unordered_map<u32, Gate*> m_gates_map;
740  std::vector<Gate*> m_gates;
741 
742  std::unordered_set<Net*> m_nets;
743  std::unordered_set<Net*> m_input_nets;
744  std::unordered_set<Net*> m_output_nets;
745  std::unordered_set<Net*> m_internal_nets;
746 
747  EventHandler* m_event_handler;
748 
749  NetConnectivity check_net_endpoints(const Net* net) const;
750  Result<std::monostate> check_net(Net* net, bool recursive = false);
751  bool assign_pin_net(const u32 pin_id, Net* net, PinDirection direction);
752  bool remove_pin_net(Net* net);
753  Result<ModulePin*> create_pin_internal(const u32 id, const std::string& name, Net* net, PinDirection direction, PinType type, bool force_name);
754  bool delete_pin_internal(ModulePin* pin);
755  Result<PinGroup<ModulePin>*> create_pin_group_internal(const u32 id, const std::string& name, PinDirection direction, PinType type, bool ascending, u32 start_index, bool force_name);
756  bool delete_pin_group_internal(PinGroup<ModulePin>* pin_group);
757  };
758 } // namespace hal
#define NETLIST_API
Definition: arch_linux.h:30
Definition: gate.h:58
Definition: net.h:58
const Module * module(const Gate *g, const NodeBoxes &boxes)
PinDirection
Definition: pin_direction.h:36
PinType
Definition: pin_type.h:36
quint32 u32
PinType type
std::vector< PinInformation > pins
Net * net
u32 start_index
bool ascending
PinDirection direction
std::string name