yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SubCircuit::Graph Class Reference

#include <subcircuit.h>

Data Structures

struct  BitRef
 
struct  Edge
 
struct  Node
 
struct  Port
 
struct  PortBit
 

Public Member Functions

 Graph ()
 
 Graph (const Graph &other, const std::vector< std::string > &otherNodes)
 
void createNode (std::string nodeId, std::string typeId, void *userData=NULL, bool shared=false)
 
void createPort (std::string nodeId, std::string portId, int width=1, int minWidth=-1)
 
void createConnection (std::string fromNodeId, std::string fromPortId, int fromBit, std::string toNodeId, std::string toPortId, int toBit, int width=1)
 
void createConnection (std::string fromNodeId, std::string fromPortId, std::string toNodeId, std::string toPortId)
 
void createConstant (std::string toNodeId, std::string toPortId, int toBit, int constValue)
 
void createConstant (std::string toNodeId, std::string toPortId, int constValue)
 
void markExtern (std::string nodeId, std::string portId, int bit=-1)
 
void markAllExtern ()
 
void print ()
 

Protected Attributes

bool allExtern
 
std::map< std::string, int > nodeMap
 
std::vector< Nodenodes
 
std::vector< Edgeedges
 

Friends

class SolverWorker
 

Detailed Description

Definition at line 33 of file subcircuit.h.

Constructor & Destructor Documentation

SubCircuit::Graph::Graph ( )
inline

Definition at line 76 of file subcircuit.h.

76 : allExtern(false) { };
SubCircuit::Graph::Graph ( const Graph other,
const std::vector< std::string > &  otherNodes 
)

Definition at line 60 of file subcircuit.cc.

61 {
62  allExtern = other.allExtern;
63 
64  std::map<int, int> other2this;
65  for (int i = 0; i < int(otherNodes.size()); i++) {
66  assert(other.nodeMap.count(otherNodes[i]) > 0);
67  other2this[other.nodeMap.at(otherNodes[i])] = i;
68  nodeMap[otherNodes[i]] = i;
69  }
70 
71  std::map<int, int> edges2this;
72  for (auto &i1 : other2this)
73  for (auto &i2 : other.nodes[i1.first].ports)
74  for (auto &i3 : i2.bits)
75  if (edges2this.count(i3.edgeIdx) == 0) {
76  int next_idx = edges2this.size();
77  edges2this[i3.edgeIdx] = next_idx;
78  }
79 
80  edges.resize(edges2this.size());
81  for (auto &it : edges2this) {
82  for (auto &bit : other.edges[it.first].portBits)
83  if (other2this.count(bit.nodeIdx) > 0)
84  edges[it.second].portBits.insert(BitRef(other2this[bit.nodeIdx], bit.portIdx, bit.bitIdx));
85  edges[it.second].constValue = other.edges[it.first].constValue;
86  edges[it.second].isExtern = other.edges[it.first].isExtern;
87  }
88 
89  nodes.resize(other2this.size());
90  for (auto &it : other2this) {
91  nodes[it.second] = other.nodes[it.first];
92  for (auto &i2 : nodes[it.second].ports)
93  for (auto &i3 : i2.bits)
94  i3.edgeIdx = edges2this.at(i3.edgeIdx);
95  }
96 }
std::vector< Node > nodes
Definition: subcircuit.h:72
std::vector< Edge > edges
Definition: subcircuit.h:73
std::map< std::string, int > nodeMap
Definition: subcircuit.h:71

Member Function Documentation

void SubCircuit::Graph::createConnection ( std::string  fromNodeId,
std::string  fromPortId,
int  fromBit,
std::string  toNodeId,
std::string  toPortId,
int  toBit,
int  width = 1 
)

Definition at line 144 of file subcircuit.cc.

145 {
146  assert(nodeMap.count(fromNodeId) != 0);
147  assert(nodeMap.count(toNodeId) != 0);
148 
149  int fromNodeIdx = nodeMap[fromNodeId];
150  Node &fromNode = nodes[fromNodeIdx];
151 
152  int toNodeIdx = nodeMap[toNodeId];
153  Node &toNode = nodes[toNodeIdx];
154 
155  assert(fromNode.portMap.count(fromPortId) != 0);
156  assert(toNode.portMap.count(toPortId) != 0);
157 
158  int fromPortIdx = fromNode.portMap[fromPortId];
159  Port &fromPort = fromNode.ports[fromPortIdx];
160 
161  int toPortIdx = toNode.portMap[toPortId];
162  Port &toPort = toNode.ports[toPortIdx];
163 
164  if (width < 0) {
165  assert(fromBit == 0 && toBit == 0);
166  assert(fromPort.bits.size() == toPort.bits.size());
167  width = fromPort.bits.size();
168  }
169 
170  assert(fromBit >= 0 && toBit >= 0);
171  for (int i = 0; i < width; i++)
172  {
173  assert(fromBit + i < int(fromPort.bits.size()));
174  assert(toBit + i < int(toPort.bits.size()));
175 
176  int fromEdgeIdx = fromPort.bits[fromBit + i].edgeIdx;
177  int toEdgeIdx = toPort.bits[toBit + i].edgeIdx;
178 
179  if (fromEdgeIdx == toEdgeIdx)
180  continue;
181 
182  // merge toEdge into fromEdge
183  if (edges[toEdgeIdx].isExtern)
184  edges[fromEdgeIdx].isExtern = true;
185  if (edges[toEdgeIdx].constValue) {
186  assert(edges[fromEdgeIdx].constValue == 0);
187  edges[fromEdgeIdx].constValue = edges[toEdgeIdx].constValue;
188  }
189  for (const auto &ref : edges[toEdgeIdx].portBits) {
190  edges[fromEdgeIdx].portBits.insert(ref);
191  nodes[ref.nodeIdx].ports[ref.portIdx].bits[ref.bitIdx].edgeIdx = fromEdgeIdx;
192  }
193 
194  // remove toEdge (move last edge over toEdge if needed)
195  if (toEdgeIdx+1 != int(edges.size())) {
196  edges[toEdgeIdx] = edges.back();
197  for (const auto &ref : edges[toEdgeIdx].portBits)
198  nodes[ref.nodeIdx].ports[ref.portIdx].bits[ref.bitIdx].edgeIdx = toEdgeIdx;
199  }
200  edges.pop_back();
201  }
202 }
std::vector< Node > nodes
Definition: subcircuit.h:72
std::vector< Edge > edges
Definition: subcircuit.h:73
std::map< std::string, int > nodeMap
Definition: subcircuit.h:71

+ Here is the caller graph for this function:

void SubCircuit::Graph::createConnection ( std::string  fromNodeId,
std::string  fromPortId,
std::string  toNodeId,
std::string  toPortId 
)

Definition at line 204 of file subcircuit.cc.

205 {
206  createConnection(fromNodeId, fromPortId, 0, toNodeId, toPortId, 0, -1);
207 }
void createConnection(std::string fromNodeId, std::string fromPortId, int fromBit, std::string toNodeId, std::string toPortId, int toBit, int width=1)
Definition: subcircuit.cc:144
void SubCircuit::Graph::createConstant ( std::string  toNodeId,
std::string  toPortId,
int  toBit,
int  constValue 
)

Definition at line 209 of file subcircuit.cc.

210 {
211  assert(nodeMap.count(toNodeId) != 0);
212  int toNodeIdx = nodeMap[toNodeId];
213  Node &toNode = nodes[toNodeIdx];
214 
215  assert(toNode.portMap.count(toPortId) != 0);
216  int toPortIdx = toNode.portMap[toPortId];
217  Port &toPort = toNode.ports[toPortIdx];
218 
219  assert(toBit >= 0 && toBit < int(toPort.bits.size()));
220  int toEdgeIdx = toPort.bits[toBit].edgeIdx;
221 
222  assert(edges[toEdgeIdx].constValue == 0);
223  edges[toEdgeIdx].constValue = constValue;
224 }
std::vector< Node > nodes
Definition: subcircuit.h:72
std::vector< Edge > edges
Definition: subcircuit.h:73
std::map< std::string, int > nodeMap
Definition: subcircuit.h:71

+ Here is the caller graph for this function:

void SubCircuit::Graph::createConstant ( std::string  toNodeId,
std::string  toPortId,
int  constValue 
)

Definition at line 226 of file subcircuit.cc.

227 {
228  assert(nodeMap.count(toNodeId) != 0);
229  int toNodeIdx = nodeMap[toNodeId];
230  Node &toNode = nodes[toNodeIdx];
231 
232  assert(toNode.portMap.count(toPortId) != 0);
233  int toPortIdx = toNode.portMap[toPortId];
234  Port &toPort = toNode.ports[toPortIdx];
235 
236  for (int i = 0; i < int(toPort.bits.size()); i++) {
237  int toEdgeIdx = toPort.bits[i].edgeIdx;
238  assert(edges[toEdgeIdx].constValue == 0);
239  edges[toEdgeIdx].constValue = constValue % 2 ? '1' : '0';
240  constValue = constValue >> 1;
241  }
242 }
std::vector< Node > nodes
Definition: subcircuit.h:72
std::vector< Edge > edges
Definition: subcircuit.h:73
std::map< std::string, int > nodeMap
Definition: subcircuit.h:71
void SubCircuit::Graph::createNode ( std::string  nodeId,
std::string  typeId,
void *  userData = NULL,
bool  shared = false 
)

Definition at line 107 of file subcircuit.cc.

108 {
109  assert(nodeMap.count(nodeId) == 0);
110  nodeMap[nodeId] = nodes.size();
111  nodes.push_back(Node());
112 
113  Node &newNode = nodes.back();
114  newNode.nodeId = nodeId;
115  newNode.typeId = typeId;
116  newNode.userData = userData;
117  newNode.shared = shared;
118 }
std::vector< Node > nodes
Definition: subcircuit.h:72
std::map< std::string, int > nodeMap
Definition: subcircuit.h:71

+ Here is the caller graph for this function:

void SubCircuit::Graph::createPort ( std::string  nodeId,
std::string  portId,
int  width = 1,
int  minWidth = -1 
)

Definition at line 120 of file subcircuit.cc.

121 {
122  assert(nodeMap.count(nodeId) != 0);
123  int nodeIdx = nodeMap[nodeId];
124  Node &node = nodes[nodeIdx];
125 
126  assert(node.portMap.count(portId) == 0);
127 
128  int portIdx = node.ports.size();
129  node.portMap[portId] = portIdx;
130  node.ports.push_back(Port());
131  Port &port = node.ports.back();
132 
133  port.portId = portId;
134  port.minWidth = minWidth < 0 ? width : minWidth;
135  port.bits.insert(port.bits.end(), width, PortBit());
136 
137  for (int i = 0; i < width; i++) {
138  port.bits[i].edgeIdx = edges.size();
139  edges.push_back(Edge());
140  edges.back().portBits.insert(BitRef(nodeIdx, portIdx, i));
141  }
142 }
std::vector< Node > nodes
Definition: subcircuit.h:72
std::vector< Edge > edges
Definition: subcircuit.h:73
std::map< std::string, int > nodeMap
Definition: subcircuit.h:71

+ Here is the caller graph for this function:

void SubCircuit::Graph::markAllExtern ( )

Definition at line 261 of file subcircuit.cc.

262 {
263  allExtern = true;
264 }

+ Here is the caller graph for this function:

void SubCircuit::Graph::markExtern ( std::string  nodeId,
std::string  portId,
int  bit = -1 
)

Definition at line 244 of file subcircuit.cc.

245 {
246  assert(nodeMap.count(nodeId) != 0);
247  Node &node = nodes[nodeMap[nodeId]];
248 
249  assert(node.portMap.count(portId) != 0);
250  Port &port = node.ports[node.portMap[portId]];
251 
252  if (bit < 0) {
253  for (const auto portBit : port.bits)
254  edges[portBit.edgeIdx].isExtern = true;
255  } else {
256  assert(bit < int(port.bits.size()));
257  edges[port.bits[bit].edgeIdx].isExtern = true;
258  }
259 }
std::vector< Node > nodes
Definition: subcircuit.h:72
std::vector< Edge > edges
Definition: subcircuit.h:73
std::map< std::string, int > nodeMap
Definition: subcircuit.h:71

+ Here is the caller graph for this function:

void SubCircuit::Graph::print ( )

Definition at line 266 of file subcircuit.cc.

267 {
268  for (int i = 0; i < int(nodes.size()); i++) {
269  const Node &node = nodes[i];
270  my_printf("NODE %d: %s (%s)\n", i, node.nodeId.c_str(), node.typeId.c_str());
271  for (int j = 0; j < int(node.ports.size()); j++) {
272  const Port &port = node.ports[j];
273  my_printf(" PORT %d: %s (%d/%d)\n", j, port.portId.c_str(), port.minWidth, int(port.bits.size()));
274  for (int k = 0; k < int(port.bits.size()); k++) {
275  int edgeIdx = port.bits[k].edgeIdx;
276  my_printf(" BIT %d (%d):", k, edgeIdx);
277  for (const auto &ref : edges[edgeIdx].portBits)
278  my_printf(" %d.%d.%d", ref.nodeIdx, ref.portIdx, ref.bitIdx);
279  if (edges[edgeIdx].isExtern)
280  my_printf(" [extern]");
281  my_printf("\n");
282  }
283  }
284  }
285 }
std::vector< Node > nodes
Definition: subcircuit.h:72
std::vector< Edge > edges
Definition: subcircuit.h:73
#define my_printf
Definition: subcircuit.cc:32

+ Here is the caller graph for this function:

Friends And Related Function Documentation

friend class SolverWorker
friend

Definition at line 89 of file subcircuit.h.

Field Documentation

bool SubCircuit::Graph::allExtern
protected

Definition at line 70 of file subcircuit.h.

std::vector<Edge> SubCircuit::Graph::edges
protected

Definition at line 73 of file subcircuit.h.

std::map<std::string, int> SubCircuit::Graph::nodeMap
protected

Definition at line 71 of file subcircuit.h.

std::vector<Node> SubCircuit::Graph::nodes
protected

Definition at line 72 of file subcircuit.h.


The documentation for this class was generated from the following files: