HAL  v4.5.0-257-ge24279b4e
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
gate.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"
34 
35 #include <functional>
36 #include <map>
37 #include <memory>
38 #include <set>
39 #include <unordered_map>
40 #include <unordered_set>
41 #include <vector>
42 
43 namespace hal
44 {
45  /* forward declaration */
46  class Netlist;
47  class Net;
48  class Module;
49  class Grouping;
50  class Endpoint;
51  class NetlistInternalManager;
52 
59  {
60  public:
68  bool operator==(const Gate& other) const;
69 
77  bool operator!=(const Gate& other) const;
78 
84  ssize_t get_hash() const;
85 
91  u32 get_id() const;
92 
98  Netlist* get_netlist() const;
99 
105  const std::string& get_name() const;
106 
112  void set_name(const std::string& name);
113 
119  GateType* get_type() const;
120 
126  bool has_location() const;
127 
134  void set_location_x(i32 x);
135 
142  i32 get_location_x() const;
143 
150  void set_location_y(i32 y);
151 
158  i32 get_location_y() const;
159 
166  void set_location(const std::pair<i32, i32>& location);
167 
175  std::pair<i32, i32> get_location() const;
176 
182  Module* get_module() const;
183 
193  std::vector<Module*> get_modules(const std::function<bool(Module*)>& filter = nullptr, bool recursive = true) const;
194 
201  Grouping* get_grouping() const;
202 
211  BooleanFunction get_boolean_function(const std::string& name) const;
212 
226  Result<BooleanFunction> get_boolean_function(const std::string& name, bool inlined, bool substitute_nets) const;
227 
236  BooleanFunction get_boolean_function(const GatePin* pin = nullptr) const;
237 
251  Result<BooleanFunction> get_boolean_function(const GatePin* pin, bool inlined, bool substitute_nets) const;
252 
261  bool has_boolean_function(const std::string& name) const;
262 
271  bool has_boolean_function(const GatePin* pin) const;
272 
280  std::unordered_map<std::string, BooleanFunction> get_boolean_functions(bool only_custom_functions = false) const;
281 
295  Result<std::unordered_map<std::string, BooleanFunction>> get_boolean_functions(bool only_custom_functions, bool inlined, bool substitute_nets) const;
296 
303  bool add_boolean_function(const std::string& name, const BooleanFunction& func);
304 
310  bool mark_vcc_gate();
311 
317  bool mark_gnd_gate();
318 
324  bool unmark_vcc_gate();
325 
331  bool unmark_gnd_gate();
332 
338  bool is_vcc_gate() const;
339 
345  bool is_gnd_gate() const;
346 
347  /*
348  * pin specific functions
349  */
350 
356  const std::vector<Net*>& get_fan_in_nets() const;
357 
365  std::vector<Net*> get_fan_in_nets(const std::function<bool(Net*)>& filter) const;
366 
373  Net* get_fan_in_net(const std::string& pin_name) const;
374 
381  Net* get_fan_in_net(const GatePin* pin) const;
382 
389  bool is_fan_in_net(const Net* net) const;
390 
396  const std::vector<Endpoint*>& get_fan_in_endpoints() const;
397 
405  std::vector<Endpoint*> get_fan_in_endpoints(const std::function<bool(Endpoint*)>& filter) const;
406 
413  Endpoint* get_fan_in_endpoint(const std::string& pin_name) const;
414 
421  Endpoint* get_fan_in_endpoint(const GatePin* pin) const;
422 
429  Endpoint* get_fan_in_endpoint(const Net* net) const;
430 
436  const std::vector<Net*>& get_fan_out_nets() const;
437 
445  std::vector<Net*> get_fan_out_nets(const std::function<bool(Net*)>& filter) const;
446 
453  Net* get_fan_out_net(const std::string& pin_name) const;
454 
461  Net* get_fan_out_net(const GatePin* pin) const;
462 
469  bool is_fan_out_net(const Net* net) const;
470 
476  const std::vector<Endpoint*>& get_fan_out_endpoints() const;
477 
485  std::vector<Endpoint*> get_fan_out_endpoints(const std::function<bool(Endpoint*)>& filter) const;
486 
493  Endpoint* get_fan_out_endpoint(const std::string& pin_name) const;
494 
501  Endpoint* get_fan_out_endpoint(const GatePin* pin) const;
502 
509  Endpoint* get_fan_out_endpoint(const Net* net) const;
510 
518  std::vector<Gate*> get_unique_predecessors(const std::function<bool(const GatePin* pin, Endpoint* ep)>& filter = nullptr) const;
519 
527  std::vector<Endpoint*> get_predecessors(const std::function<bool(const GatePin* pin, Endpoint* ep)>& filter = nullptr) const;
528 
536  Endpoint* get_predecessor(const std::string& pin_name) const;
537 
545  Endpoint* get_predecessor(const GatePin* pin) const;
546 
554  std::vector<Gate*> get_unique_successors(const std::function<bool(const GatePin* pin, Endpoint* ep)>& filter = nullptr) const;
555 
563  std::vector<Endpoint*> get_successors(const std::function<bool(const GatePin* pin, Endpoint* ep)>& filter = nullptr) const;
564 
572  Endpoint* get_successor(const std::string& pin_name) const;
573 
581  Endpoint* get_successor(const GatePin* pin) const;
582 
583  private:
585  Gate(NetlistInternalManager* mgr, EventHandler* event_handler, u32 id, GateType* gt, const std::string& name, i32 x, i32 y);
586 
587  Gate(const Gate&) = delete;
588  Gate(Gate&&) = delete;
589  Gate& operator=(const Gate&) = delete;
590  Gate& operator=(Gate&&) = delete;
591 
592  BooleanFunction get_lut_function(const GatePin* pin) const;
593 
594  Result<BooleanFunction> get_inlined_boolean_function(BooleanFunction bf, std::unordered_set<std::string>& on_stack) const;
595 
596  /* pointer to corresponding netlist parent */
597  NetlistInternalManager* m_internal_manager;
598 
599  /* id of the gate */
600  u32 m_id;
601 
602  /* name of the gate */
603  std::string m_name;
604 
605  /* type of the gate */
606  GateType* m_type;
607 
608  /* location */
609  i32 m_x = -1;
610  i32 m_y = -1;
611 
612  /* owning module */
613  Module* m_module;
614 
615  /* grouping */
616  Grouping* m_grouping = nullptr;
617 
618  /* connected nets */
619  std::vector<Endpoint*> m_in_endpoints;
620  std::vector<Endpoint*> m_out_endpoints;
621  std::vector<Net*> m_in_nets;
622  std::vector<Net*> m_out_nets;
623 
624  /* dedicated functions */
625  std::unordered_map<std::string, BooleanFunction> m_functions;
626 
627  EventHandler* m_event_handler;
628  };
629 } // namespace hal
#define NETLIST_API
Definition: arch_linux.h:30
Definition: gate.h:59
Definition: net.h:58
uint32_t u32
Definition: defines.h:41
int32_t i32
Definition: defines.h:36
Definition: defines.h:45
Net * net
std::string name