HAL
gate_type.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 
34 
35 #include <map>
36 #include <set>
37 #include <string>
38 #include <unordered_map>
39 #include <unordered_set>
40 
41 namespace hal
42 {
43  class GateLibrary;
44 
45 
52  {
53  public:
59  ssize_t get_hash() const;
60 
68  std::vector<GateTypeComponent*> get_components(const std::function<bool(const GateTypeComponent*)>& filter = nullptr) const;
69 
77  GateTypeComponent* get_component(const std::function<bool(const GateTypeComponent*)>& filter = nullptr) const;
78 
88  template<typename T>
89  T* get_component_as(const std::function<bool(const GateTypeComponent*)>& filter = nullptr) const
90  {
91  GateTypeComponent* component = this->get_component(filter);
92  if (component != nullptr)
93  {
94  return component->convert_to<T>();
95  }
96 
97  return nullptr;
98  }
99 
106  bool has_component_of_type(const GateTypeComponent::ComponentType type) const;
107 
113  u32 get_id() const;
114 
120  const std::string& get_name() const;
121 
127  void assign_property(const GateTypeProperty property);
128 
134  std::set<GateTypeProperty> get_properties() const;
135 
142  std::vector<GateTypeProperty> get_property_list() const;
143 
150  bool has_property(GateTypeProperty property) const;
151 
157  GateLibrary* get_gate_library() const;
158 
164  std::string to_string() const;
165 
173  friend std::ostream& operator<<(std::ostream& os, const GateType& gate_type);
174 
181  bool operator==(const GateType& other) const;
182 
189  bool operator!=(const GateType& other) const;
190 
197  u32 get_unique_pin_id();
198 
205  u32 get_unique_pin_group_id();
206 
217  Result<GatePin*> create_pin(const u32 id, const std::string& name, PinDirection direction, PinType type = PinType::none, bool create_group = true);
218 
229  Result<GatePin*> create_pin(const std::string& name, PinDirection direction, PinType type = PinType::none, bool create_group = true);
230 
238  std::vector<GatePin*> get_pins(const std::function<bool(GatePin*)>& filter = nullptr) const;
239 
247  std::vector<std::string> get_pin_names(const std::function<bool(GatePin*)>& filter = nullptr) const;
248 
254  std::vector<GatePin*> get_input_pins() const;
255 
261  std::vector<std::string> get_input_pin_names() const;
262 
268  std::vector<GatePin*> get_output_pins() const;
269 
275  std::vector<std::string> get_output_pin_names() const;
276 
283  GatePin* get_pin_by_id(const u32 id) const;
284 
291  GatePin* get_pin_by_name(const std::string& name) const;
292 
306  Result<PinGroup<GatePin>*> create_pin_group(const u32 id,
307  const std::string& name,
308  const std::vector<GatePin*> pins = {},
311  bool ascending = true,
312  i32 start_index = 0,
313  bool delete_empty_groups = true);
314 
328  Result<PinGroup<GatePin>*> create_pin_group(const std::string& name,
329  const std::vector<GatePin*> pins = {},
332  bool ascending = true,
333  i32 start_index = 0,
334  bool delete_empty_groups = true);
335 
343  std::vector<PinGroup<GatePin>*> get_pin_groups(const std::function<bool(PinGroup<GatePin>*)>& filter = nullptr) const;
344 
351  PinGroup<GatePin>* get_pin_group_by_id(const u32 id) const;
352 
359  PinGroup<GatePin>* get_pin_group_by_name(const std::string& name) const;
360 
367  bool delete_pin_group(PinGroup<GatePin>* pin_group);
368 
377  Result<std::monostate> assign_pin_to_group(PinGroup<GatePin>* pin_group, GatePin* pin, bool delete_empty_groups = true);
378 
386  bool set_pin_group_name(PinGroup<GatePin>* pin_group, const std::string& new_name);
387 
395  bool set_pin_group_type(PinGroup<GatePin>* pin_group, PinType new_type);
396 
404  bool set_pin_group_direction(PinGroup<GatePin>* pin_group, PinDirection new_direction);
405 
412  void add_boolean_function(const std::string& name, const BooleanFunction& function);
413 
419  void add_boolean_functions(const std::unordered_map<std::string, BooleanFunction>& functions);
420 
426  const std::unordered_map<std::string, BooleanFunction>& get_boolean_functions() const;
427 
435  BooleanFunction get_boolean_function(const std::string& name) const;
436 
444  BooleanFunction get_boolean_function(const GatePin* pin = nullptr) const;
445 
446  private:
447  friend class GateLibrary;
448 
449  GateLibrary* m_gate_library;
450  u32 m_id;
451  std::string m_name;
452  std::set<GateTypeProperty> m_properties;
453  std::unique_ptr<GateTypeComponent> m_component;
454 
455  // pins
456  u32 m_next_pin_id;
457  std::set<u32> m_used_pin_ids;
458  std::set<u32> m_free_pin_ids;
459  u32 m_next_pin_group_id;
460  std::set<u32> m_used_pin_group_ids;
461  std::set<u32> m_free_pin_group_ids;
462 
463  std::vector<std::unique_ptr<GatePin>> m_pins;
464  std::unordered_map<u32, GatePin*> m_pins_map;
465  std::unordered_map<std::string, GatePin*> m_pin_names_map;
466  std::vector<std::unique_ptr<PinGroup<GatePin>>> m_pin_groups;
467  std::unordered_map<u32, PinGroup<GatePin>*> m_pin_groups_map;
468  std::unordered_map<std::string, PinGroup<GatePin>*> m_pin_group_names_map;
469  std::list<PinGroup<GatePin>*> m_pin_groups_ordered;
470 
471  // Boolean functions
472  std::unordered_map<std::string, BooleanFunction> m_functions;
473 
474  GateType(GateLibrary* gate_library, u32 id, const std::string& name, std::set<GateTypeProperty> properties, std::unique_ptr<GateTypeComponent> component = nullptr);
475 
476  GateType(const GateType&) = delete;
477  GateType& operator=(const GateType&) = delete;
478 
479  Result<PinGroup<GatePin>*> create_pin_group_internal(const u32 id, const std::string& name, PinDirection direction, PinType type, bool ascending, u32 start_index);
480  bool delete_pin_group_internal(PinGroup<GatePin>* pin_group);
481  };
482 } // namespace hal
#define NETLIST_API
Definition: arch_linux.h:30
T * get_component_as(const std::function< bool(const GateTypeComponent *)> &filter=nullptr) const
Definition: gate_type.h:89
int32_t i32
Definition: defines.h:36
GateLibrary * get_gate_library(const std::string &file_path)
PinDirection
Definition: pin_direction.h:36
std::ostream & operator<<(std::ostream &os, BooleanFunction::Value v)
PinType
Definition: pin_type.h:36
quint32 u32
PinType type
std::vector< PinInformation > pins
u32 start_index
bool ascending
PinDirection direction
std::string name