HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
gui_def.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"
29 #include "hal_core/utilities/log.h"
30 #include <QDebug>
31 #include <QPoint>
32 #include <QHash>
33 #include <QSet>
34 #include <QMetaType>
35 
36 namespace hal
37 {
44  enum class ItemType
45  {
46  None, Module, Gate, Net
47  };
48 
60  class Node
61  {
62  public:
64 
65  Node(u32 i=0, NodeType t=None) : mId(i), mType(t) {;}
66 
71  NodeType type() const { return mType; }
72 
77  u32 id() const { return mId; }
78 
83  bool isNull() const { return mType == None; }
84 
89  bool isGate() const { return mType == Gate; }
90 
95  bool isModule() const { return mType == Module; }
96 
102  bool operator<(const Node& rhs) const
103  {
104  if (mType < rhs.mType)
105  return true;
106  if (mType > rhs.mType)
107  return false;
108  return mId < rhs.mId;
109  }
110 
116  bool operator==(const Node& rhs) const
117  {
118  return mType == rhs.mType && mId == rhs.mId;
119  }
120 
126  bool operator!=(const Node& rhs) const
127  {
128  return !(*this == rhs);
129  }
130 
131  private:
132 
133  u32 mId;
134  NodeType mType;
135  };
136 
137 
138  uint qHash(const Node &n);
139 
143  class GridPlacement : public QHash<Node,QPoint>
144  {
145  public:
148 
149  void setGatePosition(u32 gateId, std::pair<int,int>p, bool swap = false) {
150  std::pair<int,int>* posPtr = gatePosition(gateId);
151  if (!posPtr)
152  {
153  log_warning("gui", "Gate id {} cannot be moved, not found in current placement", gateId);
154  return;
155  }
156  QPoint pos = QPoint(posPtr->first, posPtr->second); //position of current gate to move
157  hal::Node nd = key(QPoint(p.first, p.second)); //find the node in the destination
158 
159  if(!nd.isNull() && !swap) //if the destination placement is not available
160  log_warning("gui", "Target position is already occupied");
161  else if (!nd.isNull() && swap)
162  {
163  operator[](hal::Node(gateId,hal::Node::Gate)) = QPoint(p.first,p.second);//set the position of the first node to the destination
164  if(nd.isGate())
165  operator[](hal::Node(nd.id(), hal::Node::Gate)) = pos;//set the position of the destination node to the position of the first node
166  else operator[](hal::Node(nd.id(), hal::Node::Module)) = pos;
167  }
168  else
169  operator[](hal::Node(gateId,hal::Node::Gate)) = QPoint(p.first,p.second);
170  }
171 
172  void setModulePosition(u32 moduleId, std::pair<int,int>p, bool swap = false){
173  std::pair<int,int>* posPtr = modulePosition(moduleId);
174  if (!posPtr)
175  {
176  log_warning("gui", "Module id {} cannot be moved, not found in current placement", moduleId);
177  return;
178  }
179  QPoint pos = QPoint(posPtr->first, posPtr->second); //position of current module to move
180  hal::Node nd = key(QPoint(p.first, p.second));
181 
182  if(!nd.isNull() && !swap)
183  log_warning("gui", "Target position is already occupied");
184  else if (!nd.isNull() && swap)
185  {
186  operator[](hal::Node(moduleId,hal::Node::Module)) = QPoint(p.first,p.second);
187  if(nd.isGate())
188  operator[](hal::Node(nd.id(), hal::Node::Gate)) = pos;
189  else operator[](hal::Node(nd.id(), hal::Node::Module)) = pos;
190  }
191  else
192  operator[](hal::Node(moduleId,hal::Node::Module)) = QPoint(p.first,p.second);};
193 
194  std::pair<int,int>* gatePosition(u32 gateId) const
195  {
196  auto it = constFind(hal::Node(gateId,hal::Node::Gate));
197  return (it == constEnd() ? nullptr : new std::pair<int,int>(it->x(),it->y()));
198  }
199 
200  std::pair<int,int>* modulePosition(u32 moduleId) const
201  {
202  auto it = constFind(hal::Node(moduleId,hal::Node::Module));
203  return (it == constEnd() ? nullptr : new std::pair(it->x(),it->y()));
204  }
205  };
206 
214  {
215  public:
221 
228  : mMode(mod), mPreferredOrigin(orign) {;}
229 
234  PlacementHint(const GridPlacement& gridPlc)
235  : mMode(GridPosition), mPreferredOrigin(Node()), mGridPos(gridPlc) {;}
236 
241  PlacementModeType mode() const { return mMode; }
242 
249  Node preferredOrigin() const { return mPreferredOrigin; }
250 
256  bool operator<(const PlacementHint& rhs) const
257  {
258  if (mMode < rhs.mMode)
259  return true;
260  if (mMode > rhs.mMode)
261  return false;
262  return mPreferredOrigin < rhs.mPreferredOrigin;
263  }
264 
270  bool operator==(const PlacementHint& rhs) const
271  {
272  return mMode == rhs.mMode && mPreferredOrigin == rhs.mPreferredOrigin;
273  }
274 
281  void addGridPosition(const Node& nd, const QPoint& p)
282  {
283  mGridPos.insert(nd,p);
284  }
285 
291  const QHash<Node,QPoint>& gridPosition() const { return mGridPos; }
292  private:
293  PlacementModeType mMode;
294  Node mPreferredOrigin;
295  GridPlacement mGridPos;
296  };
297 
302  public:
313  PlacementEntry(const PlacementHint& plc, const QSet<u32>& mods, const QSet<u32>& gats)
314  : mPlacementHint(plc), mModules(mods), mGates(gats) {;}
315  };
316 
317 }
318 // must stand globally at the end
319 Q_DECLARE_METATYPE(hal::Node);
Definition: gate.h:58
std::pair< int, int > * gatePosition(u32 gateId) const
Definition: gui_def.h:194
std::pair< int, int > * modulePosition(u32 moduleId) const
Definition: gui_def.h:200
void setGatePosition(u32 gateId, std::pair< int, int >p, bool swap=false)
Definition: gui_def.h:149
GridPlacement(const QHash< hal::Node, QPoint > &data)
Definition: gui_def.h:147
void setModulePosition(u32 moduleId, std::pair< int, int >p, bool swap=false)
Definition: gui_def.h:172
Definition: net.h:58
The Node class object represents a module or a gate.
Definition: gui_def.h:61
bool operator<(const Node &rhs) const
operator < to provide an ordering scheme for maps and ordered lists
Definition: gui_def.h:102
bool isModule() const
isModule test wheter node is a module
Definition: gui_def.h:95
NodeType type() const
type getter for type information
Definition: gui_def.h:71
bool isGate() const
isGate test whether node is a gate
Definition: gui_def.h:89
@ Module
Definition: gui_def.h:63
@ Gate
Definition: gui_def.h:63
@ None
Definition: gui_def.h:63
bool operator==(const Node &rhs) const
operator == to test whether two nodes are equal
Definition: gui_def.h:116
Node(u32 i=0, NodeType t=None)
Definition: gui_def.h:65
bool isNull() const
isNull test for null-Node object typically returned from functions
Definition: gui_def.h:83
u32 id() const
id getter for ID information
Definition: gui_def.h:77
bool operator!=(const Node &rhs) const
operator != to test whether two nodes are not equal
Definition: gui_def.h:126
Container class to store a PlacementHint togerther with a set of modules and gates.
Definition: gui_def.h:301
PlacementHint mPlacementHint
Definition: gui_def.h:303
QSet< u32 > mGates
Definition: gui_def.h:305
PlacementEntry(const PlacementHint &plc, const QSet< u32 > &mods, const QSet< u32 > &gats)
Definition: gui_def.h:313
QSet< u32 > mModules
Definition: gui_def.h:304
The PlacementHint class object provides hints for the layouter how new box objects are placed on a vi...
Definition: gui_def.h:214
bool operator<(const PlacementHint &rhs) const
operator < provide an order structure for placement hints
Definition: gui_def.h:256
void addGridPosition(const Node &nd, const QPoint &p)
Definition: gui_def.h:281
bool operator==(const PlacementHint &rhs) const
operator == test whether two placement hints are equal
Definition: gui_def.h:270
Node preferredOrigin() const
preferredOrigin getter for placement origin if any.
Definition: gui_def.h:249
PlacementHint(const GridPlacement &gridPlc)
PlacementHint constructor for grid placement.
Definition: gui_def.h:234
PlacementHint(PlacementModeType mod=Standard, const Node &orign=Node())
PlacementHint standard constructor.
Definition: gui_def.h:227
PlacementModeType mode() const
mode getter for placement mode type
Definition: gui_def.h:241
PlacementModeType
The PlacementModeType enum either most compact arrangement (Standard) or to the left or right of give...
Definition: gui_def.h:220
const QHash< Node, QPoint > & gridPosition() const
Definition: gui_def.h:291
uint32_t u32
Definition: defines.h:41
ItemType
The ItemType enum provides the enum type to classify graphic items into Modules, Gates or Nets....
Definition: gui_def.h:45
#define log_warning(channel,...)
Definition: log.h:76
Definition: defines.h:45
uint qHash(const LaneIndex &ri)
QHash::const_iterator constEnd() const const
QHash::const_iterator constFind(const Key &key) const const
QHash::iterator insert(const Key &key, const T &value)
const Key key(const T &value) const const
T & operator[](const Key &key)
void swap(QHash< K, V > &other)