Bitstream Interpretation Library (BIL)  0.1
XDLParserImp0.cpp
Go to the documentation of this file.
1 
6 #include <utility>
9 
10 using namespace bil;
11 using namespace bil::xdlparser_detail;
12 
13 
14 XDLParserImp::XDLParserImp():
15  m_headerParsed(false),
16  m_quoteBuffer(0),
17  m_tileMap(),
18  m_wirePIPMaps(),
19  m_pinMaps(),
20  m_primitiveTypesMap(),
21  m_instanceMap(),
22  m_netSet()
23 {
24  m_quoteBuffer = new char[QUOTE_BUFFER_SIZE+1];
25 }
26 
27 
29 {
30  delete[] m_quoteBuffer;
31  m_quoteBuffer = 0;
32 }
33 
34 
35 void XDLParserImp::setupSyntax()
36 {
37  m_tok->resetSyntax();
38  m_tok->commentChar('#');
39  m_tok->whitespaceChar(' ');
40  m_tok->whitespaceChar('\t');
41  m_tok->whitespaceChar('\n');
42  m_tok->whitespaceChar('\r');
43  m_tok->wordChars('a', 'z');
44  m_tok->wordChars('A', 'Z');
45  m_tok->wordChars('0', '9');
46  m_tok->wordChar('_');
47  m_tok->wordChar('-');
48  m_tok->wordChar('.');
49 }
50 
51 
52 void XDLParserImp::fillDeviceLookups()
53 {
54  // clear all maps
55  clearTemporaryData();
56 
57  // fill tile map
58  const Tiles& tiles = m_device->tiles();
59  size_t count = tiles.size();
60  for (size_t i = 0; i < count; ++i)
61  {
62  const Tile& tile = tiles[i];
63  m_tileMap.insert(std::make_pair(tile.name(), i));
64  }
65 
66  // fill wire and pip maps
67  const TileTypes& tileTypes = m_device->tileTypes();
68  count = tileTypes.size();
69  m_wirePIPMaps.resize(count);
70  for (size_t i = 0; i < count; ++i)
71  {
72  // get current tile type and its maps
73  TileTypeEx& tileTypeEx = m_wirePIPMaps[i];
74  const TileType& tileType = tileTypes[i];
75 
76  // fill wire map
77  nameIndexMap_t& wireMap = tileTypeEx.wireMap;
78  const Wires& wires = tileType.wires();
79  size_t wireCount = wires.size();
80  for (size_t j = 0; j < wireCount; ++j)
81  {
82  const Wire& wire = wires[j];
83  wireMap.insert(std::make_pair(wire.name(), j));
84  }
85 
86  // fill PIP map
87  pipIndexMap_t& pipMap = tileTypeEx.pipMap;
88  const PIPs& pips = tileType.pips();
89  size_t pipCount = pips.size();
90  for (size_t j = 0; j < pipCount; ++j)
91  {
92  const PIP& pip = pips[j];
93  pipMap.insert(std::make_pair(pip, j));
94  }
95  }
96 
97  // fill primitive type map and primitive pin maps
98  const PrimitiveTypes& primitiveTypes = m_device->primitiveTypes();
99  count = primitiveTypes.size();
100  m_pinMaps.resize(count);
101  for (size_t i = 0; i < count; ++i)
102  {
103  // get primitive type and insert it into map
104  const PrimitiveType& primitiveType = primitiveTypes[i];
105  m_primitiveTypesMap.insert(std::make_pair(primitiveType.name(), i));
106 
107  // get pin map of current primitive type
108  nameIndexMap_t& pinMap = m_pinMaps[i];
109  // get pins of current primitive type and add them into map
110  const Pins& pins = primitiveType.pins();
111  size_t pinCount = pins.size();
112  for (size_t j = 0; j < pinCount; ++j)
113  {
114  const Pin& pin = pins[j];
115  pinMap.insert(std::make_pair(pin.externalName(), j));
116  }
117  }
118 }
119 
120 
121 void XDLParserImp::clearAll()
122 {
123  // header must be parsed again
124  m_headerParsed = false;
125  // reset all pointers
126  m_tok = 0;
127  m_device = 0;
128  m_tiles = 0;
129  m_design = 0;
130  m_instances = 0;
131  m_instance = 0;
132  m_nets = 0;
133  m_net = 0;
134  // clear data
135  clearTemporaryData();
136 }
137 
138 
139 void XDLParserImp::clearTemporaryData()
140 {
141  // clear maps
142  m_tileMap.clear();
143  m_wirePIPMaps.clear();
144  m_pinMaps.clear();
145  m_primitiveTypesMap.clear();
146  m_instanceMap.clear();
147  m_netSet.clear();
148 }
149 
150 
151 void XDLParserImp::addInstance(const char* instanceName)
152 {
153  // check, if given instance name is not already in use
154  nameIndexMap_t::iterator lb = m_instanceMap.lower_bound(instanceName);
155  if ((m_instanceMap.end() != lb) && (instanceName == lb->first)) throw Exception();
156 
157  // create new instance and set its name
158  size_t count = m_instances->size();
159  m_instances->push_back(Instance());
160  m_instance = &((*m_instances)[count]);
161  (m_instance->name()).assign(instanceName);
162 
163  // insert tile name and index into map
164  m_instanceMap.insert(lb, std::make_pair(instanceName, count));
165 }
166 
167 
168 void XDLParserImp::addNet(const char* netName)
169 {
170  // check, if given net name is not already in use
171  nameSet_t::iterator lb = m_netSet.lower_bound(netName);
172  if ((m_netSet.end() != lb) && (netName == *lb)) throw Exception();
173 
174  // create new instance and set its name
175  size_t count = m_nets->size();
176  m_nets->push_back(Net());
177  m_net = &((*m_nets)[count]);
178  (m_net->name()).assign(netName);
179 
180  // insert net into set
181  m_netSet.insert(lb, netName);
182 }
183 
184 
185 size_t XDLParserImp::findTile(const char* tileName) const
186 {
187  // search for tile name
188  nameIndexMap_t::const_iterator it = m_tileMap.find(tileName);
189  if (m_tileMap.end() == it) throw Exception();
190  // return its index
191  return it->second;
192 }
193 
194 
195 size_t XDLParserImp::findWire(const char* wireName, const nameIndexMap_t& wireMap) const
196 {
197  // search for wire name
198  nameIndexMap_t::const_iterator it = wireMap.find(wireName);
199  if (wireMap.end() == it) throw Exception();
200  // return its index
201  return it->second;
202 }
203 
204 
205 size_t XDLParserImp::findPIP(const PIP& pip, const pipIndexMap_t& pipMap) const
206 {
207  // search for PIP
208  pipIndexMap_t::const_iterator it = pipMap.find(pip);
209  if (pipMap.end() == it) throw Exception();
210  // return its index
211  return it->second;
212 }
213 
214 
215 size_t XDLParserImp::findPrimitiveSite(const char* siteName, size_t tileIndex) const
216 {
217  // get primitive sites of given tile
218  const Tile& tile = m_tiles->at(tileIndex);
219  const PrimitiveSites& sites = tile.primitiveSites();
220  // search for site of given name
221  size_t count = sites.size();
222  for (size_t i = 0; i < count; ++i)
223  if (0 == ((sites[i]).name()).compare(siteName))
224  return i;
225  // not found
226  throw Exception();
227 }
228 
229 
230 size_t XDLParserImp::findPrimitiveType(const char* typeName) const
231 {
232  // search for primitive type
233  nameIndexMap_t::const_iterator it = m_primitiveTypesMap.find(typeName);
234  if (m_primitiveTypesMap.end() == it) throw Exception();
235  // return its index
236  return it->second;
237 }
238 
239 
240 size_t XDLParserImp::findInstance(const char* instanceName) const
241 {
242  // search for instance name
243  nameIndexMap_t::const_iterator it = m_instanceMap.find(instanceName);
244  if (m_instanceMap.end() == it) throw Exception();
245  // return its index
246  return it->second;
247 }
248 
249 
250 size_t XDLParserImp::findPin(const char* pinName, size_t instanceIndex) const
251 {
252  // get pin map of instance's primitive type
253  const Instance& instance = m_instances->at(instanceIndex);
254  size_t primitiveTypeIndex = instance.primitiveTypeIndex();
255  const nameIndexMap_t& pinMap = m_pinMaps.at(primitiveTypeIndex);
256  // search in map
257  nameIndexMap_t::const_iterator it = pinMap.find(pinName);
258  if (pinMap.end() == it) throw Exception();
259  // return its index
260  return it->second;
261 }