]> AND Private Git Repository - blast.git/blob - Graph.cpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
moved vhdl gen. into block
[blast.git] / Graph.cpp
1 #include "Graph.h"
2 #include "GroupBlock.h"
3 #include "ReferenceBlock.h"
4 #include "FunctionalBlock.h"
5
6 Graph::Graph() {
7   topGroup = new GroupBlock(NULL);
8   topGroup->setName("top group");
9   groups.append(topGroup);
10 }
11
12 Graph::~Graph() {
13   
14   delete topGroup;
15 }
16
17 QList<AbstractInterface *> Graph::getOutsideInterfaces() {
18   return topGroup->getInterfaces();
19 }
20
21 GroupBlock* Graph::createChildGroupBlock(GroupBlock* parent) {
22   GroupBlock* b = new GroupBlock(parent);
23   groups.append(b);
24   return b;
25 }
26
27 void Graph::removeGroupBlock(GroupBlock *group) {
28   group->removeAllBlocks();
29   GroupBlock* parent = AB_TO_GRP(group->getParent());
30   parent->removeBlock(group);
31   groups.removeAll(group);
32 }
33
34 GroupBlock* Graph::getGroupBlockByName(QString name) {
35   foreach(GroupBlock* group, groups) {
36     if (group->getName() == name) return group;
37   }
38   return NULL;
39 }
40
41 FunctionalBlock* Graph::createFunctionalBlock(GroupBlock* group, ReferenceBlock* ref) {
42
43   FunctionalBlock* newBlock = new FunctionalBlock(group,ref);
44   newBlock->populate();
45   group->addBlock(newBlock);
46
47   return newBlock;
48 }
49
50 FunctionalBlock* Graph::duplicateFunctionalBlock(FunctionalBlock *block) {
51
52   ReferenceBlock* ref = block->getReference();
53   GroupBlock* group = AB_TO_GRP(block->getParent());
54
55   // adding to the graph
56   FunctionalBlock* newBlock = createFunctionalBlock(group,ref);
57   return newBlock;
58 }
59
60
61 bool Graph::removeFunctionalBlock(FunctionalBlock* block) {
62   GroupBlock* group = AB_TO_GRP(block->getParent());
63   group->removeBlock(block);
64   return true;
65 }
66
67 FunctionalBlock* Graph::getFunctionalBlockByName(QString name, GroupBlock* parent) {
68   FunctionalBlock* block = NULL;
69   if (parent != NULL) {
70     block = AB_TO_FUN(parent->getFunctionalBlockByName(name));
71   }
72   else {
73     foreach(GroupBlock* group, groups) {
74       block = AB_TO_FUN(group->getFunctionalBlockByName(name));
75       if (block != NULL) return block;
76     }
77   }
78   return block;
79 }
80
81 FunctionalBlock* Graph::createSourceBlock(ReferenceBlock* ref) {
82
83   FunctionalBlock* newBlock = new FunctionalBlock(NULL,ref);
84   newBlock->populate();
85   sources.append(newBlock);
86   return newBlock;
87 }
88
89 FunctionalBlock* Graph::duplicateSourceBlock(FunctionalBlock *block) {
90
91   ReferenceBlock* ref = block->getReference();  
92
93   // adding to the graph
94   FunctionalBlock* newBlock = createSourceBlock(ref);
95   return newBlock;
96 }
97
98 FunctionalBlock* Graph::getSourceBlockByName(QString name) {
99   foreach(FunctionalBlock* block, sources) {
100     if (block->getName() == name) return block;
101   }
102   return NULL;
103 }
104
105 bool Graph::removeSourceBlock(FunctionalBlock *block) {
106   sources.removeAll(block);
107   return true;
108 }
109
110 void Graph::createPatterns() throw(Exception) {
111   
112   foreach(AbstractBlock* block, sources) {
113     FunctionalBlock* funBlock = AB_TO_FUN(block);
114     try {
115       funBlock->createPatterns();
116     }
117     catch(Exception e) {
118       throw(e);      
119     }
120   }
121   
122   foreach(AbstractBlock* block, groups) {
123     GroupBlock* group = AB_TO_GRP(block);    
124     foreach(AbstractBlock* inBlock, group->getBlocks()) {
125       if (inBlock->isFunctionalBlock()) {
126         FunctionalBlock* funBlock = AB_TO_FUN(inBlock);
127         try {
128           funBlock->createPatterns();
129         }
130         catch(Exception e) {
131           throw(e);
132         }
133       }
134     }
135   }  
136 }
137
138 void Graph::resetPatternComputed() {
139   foreach(AbstractBlock* block, sources) {
140     block->setPatternComputed(false);
141     block->resetTraversalLevel();
142   }
143   foreach(AbstractBlock* block, groups) {
144     GroupBlock* group = AB_TO_GRP(block);
145     group->setPatternComputed(false);
146     block->resetTraversalLevel();
147     foreach(AbstractBlock* inBlock, group->getBlocks()) {
148       inBlock->setPatternComputed(false);
149       block->resetTraversalLevel();
150     }
151   }
152 }
153
154 void Graph::computeOutputPatterns(int nbExec) throw(Exception) {
155   
156   try {
157     createPatterns();
158   }
159   catch(Exception e) {
160     throw(e);    
161   }
162
163   resetPatternComputed();
164   // search for all block that are generators.
165   QList<FunctionalBlock*> generators;
166   generators.append(sources);
167   foreach(AbstractBlock* block, groups) {    
168     GroupBlock* group = AB_TO_GRP(block);    
169     foreach(AbstractBlock* inBlock, group->getBlocks()) {
170       FunctionalBlock* funBlock = AB_TO_FUN(inBlock);
171       if ((inBlock->isFunctionalBlock()) && (inBlock->isGeneratorBlock())) {
172         generators.append(funBlock);
173       }
174     }    
175   }
176   // search for maximum PP length
177   int maxPP = 0;
178   foreach(FunctionalBlock* block, generators) {    
179     if (block->getProductionPatternLength() > maxPP) maxPP = block->getProductionPatternLength();
180   }
181   // compute output for generators
182   int maxExecLen = maxPP*nbExec;
183   foreach(FunctionalBlock* block, generators) {    
184     int d = block->getProductionPatternLength();
185     block->computeOutputPattern((maxExecLen+d-1)/d);
186   }
187   // compute output for top group
188   try {
189     topGroup->computeOutputPattern();
190   }
191   catch(Exception e) {
192     throw(e);
193   }
194 }
195
196 void Graph::generateVHDL(const QString &path) throw(Exception) {
197   try {
198     topGroup->generateVHDL(path);
199   }
200   catch(Exception e) {
201     throw(e);
202   }
203 }