HAL
netlist_graph.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 <igraph/igraph.h>
34 #include <set>
35 #include <unordered_map>
36 
42 namespace hal
43 {
44  class Netlist;
45  class Gate;
46  class Net;
47 
48  namespace graph_algorithm
49  {
57  {
58  public:
63  enum class Direction
64  {
68  NONE,
69 
73  IN,
74 
78  OUT,
79 
83  ALL
84  };
85 
93  NetlistGraph(Netlist* nl, igraph_t&& graph, std::unordered_map<u32, Gate*>&& nodes_to_gates);
94 
98  ~NetlistGraph();
99 
111  static Result<std::unique_ptr<NetlistGraph>> from_netlist(Netlist* nl, bool create_dummy_vertices = false, const std::function<bool(const Net*)>& filter = nullptr);
112 
122  static Result<std::unique_ptr<NetlistGraph>> from_netlist_no_edges(Netlist* nl, const std::vector<Gate*>& gates = {});
123 
130 
136  Netlist* get_netlist() const;
137 
143  igraph_t* get_graph() const;
144 
153  Result<std::vector<Gate*>> get_gates_from_vertices(const std::vector<u32>& vertices) const;
154 
163  Result<std::vector<Gate*>> get_gates_from_vertices(const std::set<u32>& vertices) const;
164 
173  Result<std::vector<Gate*>> get_gates_from_vertices_igraph(const igraph_vector_int_t* vertices) const;
174 
181  Result<std::set<Gate*>> get_gates_set_from_vertices(const std::vector<u32>& vertices) const;
182 
189  Result<std::set<Gate*>> get_gates_set_from_vertices(const std::set<u32>& vertices) const;
190 
197  Result<std::set<Gate*>> get_gates_set_from_vertices_igraph(const igraph_vector_int_t* vertices) const;
198 
205  Result<Gate*> get_gate_from_vertex(const u32 vertex) const;
206 
213  Result<std::vector<u32>> get_vertices_from_gates(const std::vector<Gate*>& gates) const;
214 
221  Result<std::vector<u32>> get_vertices_from_gates(const std::set<Gate*>& gates) const;
222 
229  Result<igraph_vector_int_t> get_vertices_from_gates_igraph(const std::vector<Gate*>& gates) const;
230 
237  Result<igraph_vector_int_t> get_vertices_from_gates_igraph(const std::set<Gate*>& gates) const;
238 
246 
253  u32 get_num_vertices(bool only_connected = false) const;
254 
260  u32 get_num_edges() const;
261 
268  Result<std::vector<u32>> get_vertices(bool only_connected = false) const;
269 
276 
283 
292  Result<std::monostate> add_edges(const std::vector<std::pair<Gate*, Gate*>>& edges);
293 
302  Result<std::monostate> add_edges(const std::vector<std::pair<u32, u32>>& edges);
303 
312  Result<std::monostate> add_edges(const std::map<Gate*, std::set<Gate*>>& edges);
313 
320  Result<std::monostate> delete_edges(const std::vector<std::pair<Gate*, Gate*>>& edges);
321 
328  Result<std::monostate> delete_edges(const std::vector<std::pair<u32, u32>>& edges);
329 
333  void print() const;
334 
335  private:
336  NetlistGraph() = delete;
337 
343  NetlistGraph(Netlist* nl);
344 
348  Netlist* m_nl;
349 
353  igraph_t m_graph;
354 
358  igraph_t* m_graph_ptr;
359 
363  std::unordered_map<u32, Gate*> m_nodes_to_gates;
364 
368  std::unordered_map<Gate*, u32> m_gates_to_nodes;
369  };
370  } // namespace graph_algorithm
371 
372  template<>
373  std::map<graph_algorithm::NetlistGraph::Direction, std::string> EnumStrings<graph_algorithm::NetlistGraph::Direction>::data;
374 } // namespace hal
Definition: gate.h:58
Definition: net.h:58
A directed graph corresponding to a netlist.
Definition: netlist_graph.h:57
Netlist * get_netlist() const
Get the netlist associated with the netlist graph.
u32 get_num_vertices(bool only_connected=false) const
Get the number of vertices in the netlist graph.
Result< std::vector< Gate * > > get_gates_from_vertices(const std::vector< u32 > &vertices) const
Get the gates corresponding to the specified vertices.
static Result< std::unique_ptr< NetlistGraph > > from_netlist(Netlist *nl, bool create_dummy_vertices=false, const std::function< bool(const Net *)> &filter=nullptr)
Create a directed graph from a netlist.
Result< Gate * > get_gate_from_vertex(const u32 vertex) const
Get the gate corresponding to the specified vertex.
Result< std::unique_ptr< NetlistGraph > > copy() const
Create a deep copy of the netlist graph.
Result< std::set< Gate * > > get_gates_set_from_vertices(const std::vector< u32 > &vertices) const
Get the gates corresponding to the specified vertices.
~NetlistGraph()
Default destructor for NetlistGraph.
Result< std::set< Gate * > > get_gates_set_from_vertices_igraph(const igraph_vector_int_t *vertices) const
Get the gates corresponding to the specified vertices.
Direction
The direction of exploration within the graph.
Definition: netlist_graph.h:64
@ ALL
Explore in both directions, i.e., treat the graph as undirected.
@ NONE
No direction, invalid default setting.
@ IN
Explore through the inputs of the current node, i.e., traverse backwards.
@ OUT
Explore through the outputs of the current node, i.e., traverse forwards.
Result< u32 > get_vertex_from_gate(Gate *g) const
Get the vertex corresponding to the specified gate.
Result< igraph_vector_int_t > get_vertices_from_gates_igraph(const std::vector< Gate * > &gates) const
Get the vertices corresponding to the specified gates.
Result< std::vector< u32 > > get_vertices_from_gates(const std::vector< Gate * > &gates) const
Get the vertices corresponding to the specified gates.
Result< std::monostate > add_edges(const std::vector< std::pair< Gate *, Gate * >> &edges)
Add edges between the specified pairs of source and destination gates to the netlist graph.
void print() const
Print the edge list of the graph to stdout.
Result< std::vector< Gate * > > get_gates_from_vertices_igraph(const igraph_vector_int_t *vertices) const
Get the gates corresponding to the specified vertices.
Result< std::vector< std::pair< u32, u32 > > > get_edges() const
Get the edges between vertices in the netlist graph.
u32 get_num_edges() const
Get the number of edges in the netlist graph.
Result< std::vector< std::pair< Gate *, Gate * > > > get_edges_in_netlist() const
Get the edges between gates in the netlist corresponding to the netlist graph.
Result< std::monostate > delete_edges(const std::vector< std::pair< Gate *, Gate * >> &edges)
Delete edges between the specified pairs of source and destination gates from the netlist graph.
Result< std::vector< u32 > > get_vertices(bool only_connected=false) const
Get the vertices in the netlist graph.
static Result< std::unique_ptr< NetlistGraph > > from_netlist_no_edges(Netlist *nl, const std::vector< Gate * > &gates={})
Create an empty directed graph from a netlist.
igraph_t * get_graph() const
Get the graph object of the netlist graph.
quint32 u32