OpenASIP  2.0
FUImplementation.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file FUImplementation.cc
26  *
27  * Implementation of FUImplementation class.
28  *
29  * @author Lasse Laasonen 2005 (lasse.laasonen-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include <string>
34 #include <algorithm>
35 
36 #include "FUImplementation.hh"
37 #include "FUEntry.hh"
38 #include "FUExternalPort.hh"
39 #include "FUPortImplementation.hh"
41 
42 #include "SequenceTools.hh"
43 #include "MapTools.hh"
44 #include "ContainerTools.hh"
45 #include "StringTools.hh"
46 #include "MathTools.hh"
47 
48 using std::string;
49 
50 namespace HDB {
51 
52 /**
53  * The constructor.
54  *
55  * @param name Name of the module.
56  * @param opcodePort Name of the opcode port.
57  * @param clkPort Name of the clock port.
58  * @param rstPort Name of the reset port.
59  * @param glockPort Name of the global lock port.
60  * @param glockReqPort Name of the global lock request port.
61  */
63  const std::string& name,
64  const std::string& opcodePort,
65  const std::string& clkPort,
66  const std::string& rstPort,
67  const std::string& glockPort,
68  const std::string& glockReqPort) :
69  HWBlockImplementation(name, clkPort, rstPort, glockPort),
70  opcodePort_(opcodePort), glockReqPort_(glockReqPort) {
71 }
72 
73 
74 /**
75  * Copy constructor.
76  *
77  * @param original FUImplementation to copy.
78  */
80  HWBlockImplementation(original) {
81 
82  opcodePort_ = original.opcodePort_;
83  glockReqPort_ = original.glockReqPort_;
84  opcodes_ = original.opcodes_;
85  parameters_ = original.parameters_;
86 
87  // Deep copy architecture ports.
88  for (int i = 0; i < original.architecturePortCount(); i++) {
89 
90  FUPortImplementation* portCopy =
91  new FUPortImplementation(original.architecturePort(i));
92 
93  addArchitecturePort(portCopy);
94  }
95 
96  // Deep copy external ports.
97  for (int i = 0; i < original.externalPortCount(); i++) {
98 
99  FUExternalPort* portCopy =
100  new FUExternalPort(original.externalPort(i));
101 
102  addExternalPort(portCopy);
103  }
104 }
105 
106 
107 
108 /**
109  * The destructor.
110  *
111  * Deletes all the ports.
112  */
116 }
117 
118 /**
119  * Sets the name of the opcode port.
120  *
121  * @param name Name of the port.
122  */
123 void
124 FUImplementation::setOpcodePort(const std::string& name) {
125  opcodePort_ = name;
126 }
127 
128 
129 /**
130  * Returns the name of the opcode port.
131  *
132  * @return The name of the opcode port.
133  */
134 std::string
136  return opcodePort_;
137 }
138 
139 
140 /**
141  * Sets the name of the global lock request port.
142  *
143  * @param name Name of the port.
144  */
145 void
146 FUImplementation::setGlockReqPort(const std::string& name) {
147  glockReqPort_ = name;
148 }
149 
150 
151 /**
152  * Returns the name of the global lock request port.
153  *
154  * @return The name of the global lock request port.
155  */
156 std::string
158  return glockReqPort_;
159 }
160 
161 
162 /**
163  * Sets operation code for the given operation.
164  *
165  * @param operation Name of the operation.
166  * @param opcode The operation code.
167  */
168 void
169 FUImplementation::setOpcode(const std::string& operation, int opcode) {
170  unsetOpcode(operation);
171  string opName = StringTools::stringToUpper(operation);
172  opcodes_.insert(std::pair<std::string, int>(opName, opcode));
173 }
174 
175 
176 /**
177  * Unsets the opcode of the given operation.
178  *
179  * @param operation Name of the operation.
180  */
181 void
182 FUImplementation::unsetOpcode(const std::string& operation) {
183  string opName = StringTools::stringToUpper(operation);
184  opcodes_.erase(opName);
185 }
186 
187 
188 /**
189  * Returns the number of opcodes in the FU.
190  *
191  * @return The number of opcodes.
192  */
193 int
195  return opcodes_.size();
196 }
197 
198 
199 /**
200  * By the given index, returns an operation that has an opcode.
201  *
202  * @param index The index.
203  * @exception OutOfRange If the given index is negative or not smaller than
204  * the number of opcodes.
205  */
206 std::string
208  if (index < 0 || index >= opcodeCount()) {
209  throw OutOfRange(__FILE__, __LINE__, __func__);
210  }
211 
212  OpcodeMap::const_iterator iter = opcodes_.begin();
213  for (int i = 0; i < index; i++) {
214  iter++;
215  }
216 
217  return iter->first;
218 }
219 
220 /**
221  * Tells whether there is an opcode defined for the given operation.
222  *
223  * @param operation Name of the operation.
224  * @return True if there is an opcode, otherwise false.
225  */
226 bool
227 FUImplementation::hasOpcode(const std::string& operation) const {
228  string opName = StringTools::stringToUpper(operation);
229  return MapTools::containsKey(opcodes_, opName);
230 }
231 
232 
233 /**
234  * Returns the opcode of the given operation.
235  *
236  * @param operation The operation.
237  * @return The operation code.
238  * @exception KeyNotFound If there is no opcode defined for the given
239  * operation.
240  */
241 int
242 FUImplementation::opcode(const std::string& operation) const {
243  string opName = StringTools::stringToUpper(operation);
244  return MapTools::valueForKey<int>(opcodes_, opName);
245 }
246 
247 /**
248  * Returns the maximum bitwidth of an opcode in this FU implementation.
249  *
250  * The bitwidth is not stored explicitly in HDB, this method is provided
251  * only for convenience.
252  *
253  * @return The maximum bitwidth needed by an opcode in this FU.
254  *
255  */
256 int
258  int maxFound = 0;
259  for (OpcodeMap::const_iterator i = opcodes_.begin(); i != opcodes_.end();
260  ++i) {
261  maxFound = std::max(MathTools::requiredBits((*i).second), maxFound);
262  }
263  return maxFound;
264 }
265 
266 
267 
268 /**
269  * Adds the given architectural port.
270  *
271  * @param port The port to be added.
272  */
273 void
275  ports_.push_back(port);
276 }
277 
278 
279 /**
280  * Deletes the given architectural port.
281  *
282  * @param port The port to delete.
283  * @exception InstanceNotFound If the given port is not in this FU
284  * implementation.
285  */
286 void
288  bool removed = ContainerTools::deleteValueIfExists(ports_, port);
289  if (!removed) {
290  throw InstanceNotFound(__FILE__, __LINE__, __func__);
291  }
292 }
293 
294 /**
295  * Adds the given external port.
296  *
297  * @param port The port to be added.
298  */
299 void
301  externalPorts_.push_back(port);
302 }
303 
304 
305 /**
306  * Deletes the given external port.
307  *
308  * @param port The port to delete.
309  * @exception InstanceNotFound If the given port is not in this FU
310  * implementation.
311  */
312 void
315  if (!removed) {
316  throw InstanceNotFound(__FILE__, __LINE__, __func__);
317  }
318 }
319 
320 /**
321  * Returns the number of architectural ports.
322  *
323  * @return The number of ports.
324  */
325 int
327  return ports_.size();
328 }
329 
330 
331 /**
332  * Returns the number of external ports.
333  *
334  * @return The number of external ports.
335  */
336 int
338  return externalPorts_.size();
339 }
340 
341 
342 /**
343  * Returns the architectural port implementation at the given position.
344  *
345  * @param index The position index.
346  * @return The port implementation.
347  * @exception OutOfRange If the given index is negative or not smaller the
348  * number of architectural ports.
349  */
352  if (index < 0 || index >= architecturePortCount()) {
353  const string procName = "FUImplementation::architecturePort";
354  throw OutOfRange(__FILE__, __LINE__, procName);
355  }
356 
357  return *ports_[index];
358 }
359 
360 /**
361  * Returns the implementation of a port for the given port architecture
362  * name.
363  *
364  * @param architectureName Name of the port in architecture table.
365  * @return The port implementation.
366  * @exception InstanceNotFound If no port with the given architecture name
367  * is found.
368  */
371  const std::string& architectureName) const {
372  for (int i = 0; i < architecturePortCount(); ++i) {
373  if (ports_[i]->architecturePort() == architectureName)
374  return *ports_[i];
375  }
376  throw InstanceNotFound(
377  __FILE__, __LINE__, __func__,
378  "No port implementation with the given architecture name found.");
379 }
380 
381 /**
382  * Returns the external port at the given position.
383  *
384  * @param index The position index.
385  * @return The port.
386  * @exception OutOfRange If the given index is negative or not smaller the
387  * number of external ports.
388  */
391  if (index < 0 || index >= externalPortCount()) {
392  const string procName = "FUImplementation::externalPort";
393  throw OutOfRange(__FILE__, __LINE__, procName);
394  }
395 
396  return *externalPorts_[index];
397 }
398 
399 /**
400  * Adds the given parameter for the implementation.
401  *
402  * @param name Name of the parameter.
403  * @param type Type of the parameter.
404  * @param value Value of the parameter.
405  * @exception IllegalParameters If the FU implementation contains a
406  * parameter with the same name already.
407  */
408 void
410  const std::string& name, const std::string& type,
411  const std::string& value) {
412  if (hasParameter(name)) {
413  throw IllegalParameters(__FILE__, __LINE__, __func__);
414  } else {
415  Parameter param = {name, type, value};
416  parameters_.push_back(param);
417  }
418 }
419 
420 /**
421  * Removes the parameter of the given name.
422  *
423  * @param name Name of the parameter.
424  */
425 void
426 FUImplementation::removeParameter(const std::string& name) {
427  for (ParameterTable::iterator iter = parameters_.begin();
428  iter != parameters_.end(); iter++) {
429  if (iter->name == name) {
430  parameters_.erase(iter);
431  return;
432  }
433  }
434 }
435 
436 
437 /**
438  * Returns the number of parameters.
439  *
440  * @return The number of parameters.
441  */
442 int
444  return parameters_.size();
445 }
446 
447 
448 /**
449  * Returns a parameter by the given index.
450  *
451  * @param index The index.
452  * @return The parameter.
453  * @exception OutOfRange If the index is negative or not smaller than the
454  * number of parameters.
455  */
457 FUImplementation::parameter(int index) const {
458  if (index < 0 || index >= parameterCount()) {
459  throw OutOfRange(__FILE__, __LINE__, __func__);
460  }
461 
462  return parameters_[index];
463 }
464 
465 /**
466  * Tells whether the implementation has the given parameter.
467  *
468  * @param name Name of the parameter.
469  * @return True if the implementation has the parameter, otherwise false.
470  */
471 bool
472 FUImplementation::hasParameter(const std::string& name) const {
473  for (ParameterTable::const_iterator iter = parameters_.begin();
474  iter != parameters_.end(); iter++) {
475  if (iter->name == name) {
476  return true;
477  }
478  }
479  return false;
480 }
481 }
HDB::FUImplementation::parameters_
ParameterTable parameters_
Contains the parameters.
Definition: FUImplementation.hh:130
HDB::FUExternalPort
Definition: FUExternalPort.hh:52
HDB::FUImplementation::addArchitecturePort
void addArchitecturePort(FUPortImplementation *port)
Definition: FUImplementation.cc:274
HDB::FUImplementation::opcodes_
OpcodeMap opcodes_
Contains the operation codes.
Definition: FUImplementation.hh:128
HDB::FUImplementation::hasOpcode
bool hasOpcode(const std::string &operation) const
Definition: FUImplementation.cc:227
HDB::FUImplementation::opcodeCount
int opcodeCount() const
Definition: FUImplementation.cc:194
HDB::FUImplementation::glockReqPort_
std::string glockReqPort_
Name of the global lock request port.
Definition: FUImplementation.hh:121
HDB
Definition: CostDatabase.hh:49
HDB::FUPortImplementation
Definition: FUPortImplementation.hh:46
OutOfRange
Definition: Exception.hh:320
MapTools.hh
HDB::FUImplementation::addExternalPort
void addExternalPort(FUExternalPort *port)
Definition: FUImplementation.cc:300
SequenceTools.hh
HDB::FUImplementation::externalPort
FUExternalPort & externalPort(int index) const
Definition: FUImplementation.cc:390
FUPortImplementation.hh
HDB::FUImplementation::opcodePort
std::string opcodePort() const
Definition: FUImplementation.cc:135
HDB::FUImplementation::opcodePort_
std::string opcodePort_
Name of the opcode port.
Definition: FUImplementation.hh:119
HDB::FUImplementation::glockReqPort
std::string glockReqPort() const
Definition: FUImplementation.cc:157
StringTools::stringToUpper
static std::string stringToUpper(const std::string &source)
Definition: StringTools.cc:143
StringTools.hh
HDB::FUImplementation::parameterCount
int parameterCount() const
Definition: FUImplementation.cc:443
SequenceTools::deleteAllItems
static void deleteAllItems(SequenceType &aSequence)
IllegalParameters
Definition: Exception.hh:113
HDB::FUImplementation::setGlockReqPort
void setGlockReqPort(const std::string &name)
Definition: FUImplementation.cc:146
FUEntry.hh
__func__
#define __func__
Definition: Application.hh:67
MathTools::requiredBits
static int requiredBits(unsigned long int number)
HDB::FUImplementation::externalPorts_
ExternalPortTable externalPorts_
Contains the external ports.
Definition: FUImplementation.hh:126
HDB::FUImplementation::addParameter
void addParameter(const std::string &name, const std::string &type, const std::string &value)
Definition: FUImplementation.cc:409
HDB::FUImplementation::opcodeOperation
std::string opcodeOperation(int index) const
Definition: FUImplementation.cc:207
HDB::FUImplementation::FUImplementation
FUImplementation(const std::string &name, const std::string &opcodePort, const std::string &clkPort, const std::string &rstPort, const std::string &glockPort, const std::string &glockReqPort)
Definition: FUImplementation.cc:62
HDB::HWBlockImplementation
Definition: HWBlockImplementation.hh:49
FUImplementation.hh
HDB::FUImplementation::setOpcode
void setOpcode(const std::string &operation, int opcode)
Definition: FUImplementation.cc:169
HDB::FUImplementation::deleteExternalPort
void deleteExternalPort(FUExternalPort *port)
Definition: FUImplementation.cc:313
HDB::FUImplementation::setOpcodePort
void setOpcodePort(const std::string &name)
Definition: FUImplementation.cc:124
HDB::FUImplementation
Definition: FUImplementation.hh:53
HDB::FUImplementation::opcode
int opcode(const std::string &operation) const
Definition: FUImplementation.cc:242
HDB::FUImplementation::deleteArchitecturePort
void deleteArchitecturePort(FUPortImplementation *port)
Definition: FUImplementation.cc:287
MapTools::containsKey
static bool containsKey(const MapType &aMap, const KeyType &aKey)
FUExternalPort.hh
BlockImplementationFile.hh
HDB::FUImplementation::parameter
Parameter parameter(int index) const
Definition: FUImplementation.cc:457
HDB::FUImplementation::unsetOpcode
void unsetOpcode(const std::string &operation)
Definition: FUImplementation.cc:182
HDB::FUImplementation::architecturePortCount
int architecturePortCount() const
Definition: FUImplementation.cc:326
HDB::FUImplementation::~FUImplementation
virtual ~FUImplementation()
Definition: FUImplementation.cc:113
HDB::Parameter
Definition: HDBTypes.hh:46
HDB::FUImplementation::maxOpcodeWidth
int maxOpcodeWidth() const
Definition: FUImplementation.cc:257
MathTools.hh
HDB::FUImplementation::portImplementationByArchitectureName
FUPortImplementation & portImplementationByArchitectureName(const std::string &architectureName) const
Definition: FUImplementation.cc:370
HDB::FUImplementation::architecturePort
FUPortImplementation & architecturePort(int index) const
Definition: FUImplementation.cc:351
HDB::FUImplementation::externalPortCount
int externalPortCount() const
Definition: FUImplementation.cc:337
InstanceNotFound
Definition: Exception.hh:304
ContainerTools::deleteValueIfExists
static bool deleteValueIfExists(ContainerType &aContainer, const ElementType &aKey)
HDB::FUImplementation::hasParameter
bool hasParameter(const std::string &name) const
Definition: FUImplementation.cc:472
ContainerTools.hh
HDB::FUImplementation::removeParameter
void removeParameter(const std::string &name)
Definition: FUImplementation.cc:426
HDB::FUImplementation::ports_
PortTable ports_
Contains the architectural ports.
Definition: FUImplementation.hh:124