systemc-clang 2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
Automata.cpp
Go to the documentation of this file.
1#include "Automata.h"
2
3using namespace systemc_clang;
4
6
7Node::Node() : _id(-1) {}
8
9Node::Node(int i) : _id(i) {}
10
11int Node::getId() { return _id; }
12
14 _succs.insert(Node::connectPairType(s->getId(), s));
15 s->addPredecessor(this);
16}
17
19 _preds.insert(Node::connectPairType(p->getId(), p));
20}
21
22vector<int> Node::getSuccessors(int fromId) {
23 vector<int> tmpSuccs;
24
25 for (Node::connectMapType::iterator it = _succs.begin(), eit = _succs.end();
26 it != eit; it++) {
27 // if(it->first == fromId) {
28 tmpSuccs.push_back(it->first);
29 // }
30 }
31 return tmpSuccs;
32}
33
34vector<int> Node::getPredecessors(int toId) {
35 vector<int> tmpPreds;
36
37 for (Node::connectMapType::iterator it = _preds.begin(), eit = _preds.end();
38 it != eit; it++) {
39 // if(it->first == fromId) {
40 tmpPreds.push_back(it->first);
41 // }
42 }
43 return tmpPreds;
44}
45
46void Node::dump(raw_ostream &os, int tabn) {
47 os << "Node " << getId() << "\n";
48 os << " preds: ";
49 for (Node::connectMapType::iterator it = _preds.begin(), eit = _preds.end();
50 it != eit; it++) {
51 os << it->first << " ";
52 }
53 os << "\n succs: ";
54 for (Node::connectMapType::iterator it = _succs.begin(), eit = _succs.end();
55 it != eit; it++) {
56 os << it->first << " ";
57 }
58 os << "\n";
59}
60
63Edge::Edge(Node *f, Node *t) : _id(-1), _from(f), _to(t) {}
64
65Edge::Edge(Node *f, Node *t, int i) : _id(i), _from(f), _to(t) {}
66
68 _timeAdvanceVector.push_back(timePair);
69}
70
71int Edge::getId() { return _id; }
72
73void Edge::dump(raw_ostream &os, int tabn) {
74 os << "Edge (" << _from->getId() << "," << _to->getId() << ")\n";
75}
76
77int Edge::getToId() { return _to->getId(); }
78
79int Edge::getFromId() { return _from->getId(); }
80
84
87
88Graph::Graph() : _nNodes(0), _nEdges(0) {}
89
91 Node *n = new Node(_nNodes);
92
94 _nodeVector.push_back(n);
95 _nodeIDVector.push_back(_nNodes);
96 ++_nNodes;
97 return n;
98}
99
101 Node *n = new Node(id);
102
103 _nodeMap.insert(Graph::nodePairType(id, n));
104 _nodeVector.push_back(n);
105 _nodeIDVector.push_back(id);
106 ++_nNodes;
107 return n;
108}
109
111 Edge *e = new Edge(f, t, _nEdges);
112
114
115 // Update preds and succs in nodes.
116 f->addSuccessor(t);
117 ++_nEdges;
118
119 // Insert in adjacency list.
120 _adjList.insert(
122 return e;
123}
124
125Edge *Graph::addEdge(int fID, int tID) {
126 Node *f, *t;
127
128 if (_nodeMap.find(fID) != _nodeMap.end()) {
129 nodeMapType::iterator nodeFound = _nodeMap.find(fID);
130 f = nodeFound->second;
131 } else {
132 f = new Node(fID);
133 _nodeMap.insert(nodePairType(fID, f));
134 _nodeVector.push_back(f);
135 _nodeIDVector.push_back(fID);
136 _nNodes++;
137 }
138 if (_nodeMap.find(tID) != _nodeMap.end()) {
139 nodeMapType::iterator nodeFound = _nodeMap.find(tID);
140 t = nodeFound->second;
141 } else {
142 t = new Node(tID);
143 _nodeMap.insert(nodePairType(tID, t));
144 _nodeVector.push_back(t);
145 _nodeIDVector.push_back(tID);
146 _nNodes++;
147 }
148 Edge *e = new Edge(f, t, _nEdges);
149
151
152 // Update preds and succs in nodes.
153 f->addSuccessor(t);
154 ++_nEdges;
155
156 // Insert in adjacency list.
157 _adjList.insert(
159 return e;
160}
161
163 for (nodeMapType::iterator it = _nodeMap.begin(), eit = _nodeMap.end();
164 it != eit; it++) {
165 if (n == it->second) {
166 return it->first;
167 }
168 }
169 return -1;
170}
171
173 for (edgeMapType::iterator it = _edgeMap.begin(), eit = _edgeMap.end();
174 it != eit; it++) {
175 if (it->second == e) {
176 return it->first;
177 }
178 }
179 return -1;
180}
181
183 if (_adjList.find(twoNodePairType(f->getId(), t->getId())) !=
184 _adjList.end()) {
185 adjMapType::iterator edgeFound =
186 _adjList.find(twoNodePairType(f->getId(), t->getId()));
187 return getEdgeID(edgeFound->second);
188 }
189 return -1;
190}
191
192int Graph::getEdgeID(int fID, int tID) {
193 if (_adjList.find(twoNodePairType(fID, tID)) != _adjList.end()) {
194 adjMapType::iterator edgeFound = _adjList.find(twoNodePairType(fID, tID));
195 return getEdgeID(edgeFound->second);
196 }
197 return -1;
198}
199
200Edge *Graph::getEdge(int f, int t) {
201 Graph::adjMapType::iterator fit = _adjList.find(Graph::twoNodePairType(f, t));
202 if (fit == _adjList.end()) {
203 return NULL;
204 }
205 return fit->second;
206}
207
209 Graph::adjMapType::iterator fit =
210 _adjList.find(Graph::twoNodePairType(f->getId(), t->getId()));
211 if (fit == _adjList.end()) {
212 return NULL;
213 }
214
215 return fit->second;
216}
217
218Node *Graph::getNode(int nodeID) {
219 if (_nodeMap.find(nodeID) != _nodeMap.end()) {
220 nodeMapType::iterator nodeFound = _nodeMap.find(nodeID);
221 return nodeFound->second;
222 }
223 return NULL;
224}
225
226vector<Edge *> Graph::getEdgesFromSource(int sourceID) {
227 vector<Edge *> edges;
228 for (adjMapType::iterator it = _adjList.begin(), eit = _adjList.end();
229 it != eit; it++) {
230 twoNodePairType nodePair = it->first;
231
232 if (nodePair.first == sourceID) {
233 edges.push_back(it->second);
234 }
235 }
236 return edges;
237}
238
239vector<Edge *> Graph::getEdgesFromDest(int destID) {
240 vector<Edge *> edges;
241 for (adjMapType::iterator it = _adjList.begin(), eit = _adjList.end();
242 it != eit; it++) {
243 twoNodePairType nodePair = it->first;
244
245 if (nodePair.second == destID) {
246 edges.push_back(it->second);
247 }
248 }
249 return edges;
250}
251
252vector<Edge *> Graph::getEdgesFromSource(Node *sourceNode) {
253 for (nodeMapType::iterator it = _nodeMap.begin(), eit = _nodeMap.end();
254 it != eit; it++) {
255 if (it->second == sourceNode) {
256 getEdgesFromSource(it->first);
257 }
258 }
259}
260
261vector<Edge *> Graph::getEdgesFromDest(Node *destNode) {
262 for (nodeMapType::iterator it = _nodeMap.begin(), eit = _nodeMap.end();
263 it != eit; it++) {
264 if (it->second == destNode) {
265 getEdgesFromDest(it->first);
266 }
267 }
268}
269
270void Graph::dump(raw_ostream &os, int tabn) {
271 // Print all nodes.
272 os << "Node map: " << _nNodes << "\n";
273 for (Graph::nodeMapType::iterator it = _nodeMap.begin(), eit = _nodeMap.end();
274 it != eit; it++) {
275 os << it->first << " " << it->second << " ";
276 it->second->dump(os, tabn++);
277 }
278
279 // Print all Edges.
280 os << "Edge map: " << _nEdges << "\n";
281 for (Graph::edgeMapType::iterator it = _edgeMap.begin(), eit = _edgeMap.end();
282 it != eit; it++) {
283 os << it->first << " " << it->second << " ";
284 // it->second->dump(os, tabn++);
285 }
286
287 os << "Adjacency list: " << _adjList.size() << "\n";
288 for (Graph::adjMapType::iterator it = _adjList.begin(), eit = _adjList.end();
289 it != eit; it++) {
290 Graph::twoNodePairType p = it->first;
291 Edge *e = it->second;
292
293 os << "Edge (" << p.first << "," << p.second << ") \n";
294 os << "TimeAdvance : \n";
295 Edge::timeAdvanceVectorType timeAdvanceVector = e->getTimeAdvanceVector();
296 os << "\n Size of timeAdvanceVector : " << timeAdvanceVector.size();
297 for (unsigned int i = 0; i < timeAdvanceVector.size(); i++) {
298 Edge::timePairType timePair = timeAdvanceVector.at(i);
299 os << " " << timePair.first << " " << timePair.second << "\n";
300 }
301 // e->dump(os,tabn++);
302 }
303}
304
305void Graph::dumpSauto(raw_ostream &os, int tabn) {
306 LangOptions LO;
307
308 LO.CPlusPlus = true;
309 PrintingPolicy Policy(LO);
310
311 for (Graph::adjMapType::iterator it = _adjList.begin(), eit = _adjList.end();
312 it != eit; it++) {
313 Graph::twoNodePairType p = it->first;
314 Edge *e = it->second;
315
316 os << " Edge (" << p.first << ", " << p.second << ")\n";
317
318 /*
319 os<<" Transition code \n";
320 vector<CFGBlock*> codeBlocks = e->getPreStmtBlks();
321 for (int i = 0; i<codeBlocks.size(); i++) {
322 CFGBlock* currBlock = codeBlocks.at(i);
323 for(CFGBlock::iterator it = currBlock->begin(), eit = currBlock->end();
324 it != eit;
325 it++) {
326 if(Optional <CFGStmt> cfgStmt = it->getAs<CFGStmt>()) {
327 const Stmt* stmt = cfgStmt->getStmt();
328 stmt->printPretty(llvm::errs(), 0, Policy, 0);
329 os<<"\n";
330 }
331 }
332 }
333 */
334 }
335}
336
337/*
338void Graph::dumpSauto(raw_ostream& os, int tabn) {
339
340 LangOptions LO;
341 LO.CPlusPlus = true;
342 PrintingPolicy Policy(LO);
343 unsigned int state = 0;
344 bool duplicateFound;
345for (Graph::adjMapType::iterator it = _adjList.begin(), eit = _adjList.end();
346 it != eit;
347 it++) {
348 Graph::twoNodePairType p = it->first;
349 duplicateFound = false;
350 //start and final states
351 os <<"\n q_s : "<<p.first<< " "<<" q_f : "<<p.second;
352
353 for (unsigned int i = 0; i<_nodeIds.size();i++) {
354 if(p.first == _nodeIds.at(i)){
355 duplicateFound = true;
356 break;
357 }
358 }
359 if(duplicateFound == false) {
360 _nodeIds.push_back(p.first);
361 }
362 Edge *e = it->second;
363 vector<CFGBlock*> pre = e->getPreStmtBlks();
364
365 os <<"\n B_pre :";
366 for (unsigned int i = 0; i<pre.size();i++) {
367 os<<pre.at(i)->getBlockID()<<" ";
368 }
369 vector<CFGBlock*> guard = e->getGuardBlks();
370 os <<"\n Guard :";
371 for (unsigned int i = 0; i<guard.size();i++) {
372 os<<guard.at(i)->getBlockID()<<" ";
373 Stmt* condition = guard.at(i)->getTerminator();
374
375 }
376 vector<CFGBlock*> post = e->getPostStmtBlks();
377 os <<"\n B_post :";
378 for (unsigned int i = 0; i<post.size();i++) {
379 os<<post.at(i)->getBlockID()<<" ";
380 }
381
382 os <<"\n **************************";
383 }
384}
385*/
386
388
390
392
394
396
398 // Responsible for cleaning everything up.
399 for (Graph::nodeMapType::iterator it = _nodeMap.begin(), eit = _nodeMap.end();
400 it != eit; it++) {
401 delete it->second;
402
403 // os << it->first << " " << it->second;
404 // it->second->dump(os, tabn++);
405 }
406 _nodeMap.clear();
407
408 // Print all Edges.
409 for (Graph::edgeMapType::iterator it = _edgeMap.begin(), eit = _edgeMap.end();
410 it != eit; it++) {
411 delete it->second;
412 }
413 _edgeMap.clear();
414 _adjList.clear();
415}
Edge(Node *, Node *)
Edge class.
Definition Automata.cpp:63
timeAdvanceVectorType _timeAdvanceVector
Definition Automata.h:61
vector< timePairType > timeAdvanceVectorType
Definition Automata.h:45
void dump(raw_ostream &, int)
Definition Automata.cpp:73
void updateSuspensionTime(timePairType)
Definition Automata.cpp:67
timeAdvanceVectorType getTimeAdvanceVector()
Definition Automata.cpp:81
pair< unsigned int, unsigned int > timePairType
Definition Automata.h:44
void dump(raw_ostream &, int tabn=0)
Definition Automata.cpp:270
int getNodeID(Node *)
Definition Automata.cpp:162
edgeVector returnEdgeVector()
Definition Automata.cpp:395
vector< Edge * > getEdgesFromSource(int)
Definition Automata.cpp:226
Edge * addEdge(Node *, Node *)
Definition Automata.cpp:110
int getEdgeID(Edge *)
Definition Automata.cpp:172
edgeVector _edgeVector
Definition Automata.h:131
pair< int, Node * > nodePairType
Definition Automata.h:75
vector< Node * > nodeVector
Definition Automata.h:71
void dumpSauto(raw_ostream &, int tabn=0)
Definition Automata.cpp:305
edgeIDVector _edgeIDVector
Definition Automata.h:129
map< twoNodePairType, Edge * > adjMapType
Definition Automata.h:83
vector< int > nodeIDVector
Definition Automata.h:68
vector< Edge * > edgeVector
Definition Automata.h:72
pair< twoNodePairType, Edge * > adjPairType
Definition Automata.h:82
Node * getNode(int)
Definition Automata.cpp:218
Edge * getEdge(Node *, Node *)
Definition Automata.cpp:208
pair< int, int > twoNodePairType
Definition Automata.h:80
edgeMapType _edgeMap
Definition Automata.h:124
adjMapType returnAdjList()
Definition Automata.cpp:387
adjMapType _adjList
Definition Automata.h:122
vector< Edge * > getEdgesFromDest(int)
Definition Automata.cpp:239
edgeIDVector returnEdgeIDs()
Definition Automata.cpp:391
nodeVector returnNodeVector()
Definition Automata.cpp:393
vector< int > edgeIDVector
Definition Automata.h:69
pair< int, Edge * > edgePairType
Definition Automata.h:78
Graph()
Graph class.
Definition Automata.cpp:88
nodeVector _nodeVector
Definition Automata.h:130
nodeIDVector returnNodeIDs()
Definition Automata.cpp:389
nodeIDVector _nodeIDVector
Definition Automata.h:128
nodeMapType _nodeMap
Definition Automata.h:123
void addPredecessor(Node *)
Definition Automata.cpp:18
pair< int, Node * > connectPairType
Definition Automata.h:21
connectMapType _preds
Definition Automata.h:38
connectMapType _succs
Definition Automata.h:39
vector< int > getSuccessors(int)
Definition Automata.cpp:22
void dump(raw_ostream &, int)
Definition Automata.cpp:46
vector< int > getPredecessors(int)
Definition Automata.cpp:34
void addSuccessor(Node *)
Definition Automata.cpp:13
Node()
Node class.
Definition Automata.cpp:7