OpenASIP  2.0
NetlistPort.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 NetlistPort.cc
26  *
27  * Implementation of NetlistPort class.
28  *
29  * @author Lasse Laasonen 2005 (lasse.laasonen-no.spam-tut.fi)
30  * @author Otto Esko 2010 (otto.esko-no.spam-tut.fi)
31  * @author Henry Linjamäki 2015 (henry.linjamaki-no.spam-tut.fi)
32  * @note rating: red
33  */
34 
35 #include <cctype>
36 
37 #include "NetlistPort.hh"
38 
39 #include "BaseNetlistBlock.hh"
40 #include "NetlistBlock.hh"
41 #include "Conversion.hh"
42 #include "NetlistTools.hh"
43 
44 namespace ProGe {
45 
46 /**
47  * Constructor. Creates a netlist port with a defined bit width.
48  *
49  * Creates a port that has a known bit width. Adds the port automatically to
50  * the parent block. If a formula for calculating the bit width is given, it
51  * should match the actual bit width (an integer number). No check is
52  * performed, however, to make sure that the formula is compatible with the
53  * actual width. In case of mismatch, the error can be detected only after
54  * generation (for example, in a logic synthesis tool).
55  *
56  * @param name Name of the port.
57  * @param widthFormula Formula for calculating the width.
58  * @param realWidth Actual width of the port.
59  * @param dataType Type of the data.
60  * @param direction Direction of the port.
61  * @param parent The parent netlist block.
62  * @exception OutOfRange If the actual width is not positive ( <0 ).
63  */
65  const std::string& name,
66  const std::string& widthFormula,
67  int realWidth,
68  DataType dataType,
69  Direction direction,
70  BaseNetlistBlock& parent,
71  Signal signal)
72  : name_(name),
73  widthFormula_(widthFormula),
74  realWidth_(realWidth),
75  dataType_(dataType),
76  direction_(direction),
77  parentBlock_(&parent),
78  hasStaticValue_(false),
79  staticValue_(StaticSignal::GND),
80  signal_(signal) {
81 
82  // TODO: there might still be possible regressions from changing realWidth
83  // check to "< 0" from "< 1"
84  // RF (and IU?) opcode ports may have width 0
85  if (realWidth_ < 0 ) {
86  TCEString msg = "Port ";
87  msg << name << " has a negative width.";
88  throw OutOfRange(__FILE__, __LINE__, __func__, msg);
89  }
90 
91  parent.addPort(this);
92 }
93 
94 /**
95  * Constructor. Creates a netlist port with a defined bit width,
96  * and derive formula from the integer value
97  *
98  * @param name Name of the port.
99  * @param realWidth Actual width of the port.
100  * @param dataType Type of the data.
101  * @param direction Direction of the port.
102  * @param parent The parent netlist block.
103  * @exception OutOfRange If the actual width is not positive ( <0 ).
104  */
106  const std::string& name,
107  int realWidth,
108  DataType dataType,
109  Direction direction,
110  BaseNetlistBlock& parent,
111  Signal signal)
112  : name_(name),
113  widthFormula_(Conversion::toString(realWidth)),
114  realWidth_(realWidth),
115  dataType_(dataType),
116  direction_(direction),
117  parentBlock_(&parent),
118  hasStaticValue_(false),
119  staticValue_(StaticSignal::GND),
120  signal_(signal) {
121 
122  // TODO: there might still be possible regressions from changing realWidth
123  // check to "< 0" from "< 1"
124  // RF (and IU?) opcode ports may have width 0
125  if (realWidth_ < 0 ) {
126  TCEString msg = "Port ";
127  msg << name << " has a negative width.";
128  throw OutOfRange(__FILE__, __LINE__, __func__, msg);
129  }
130 
131  parent.addPort(this);
132 }
133 
134 /**
135  * Constructor. Creates a new netlist port.
136  *
137  * Creates a port that has its bit width defined symbolically, by an
138  * expression (formula). Adds the port automatically to the parent block.
139  *
140  * @param name Name of the port.
141  * @param widthFormula Formula for calculating the width.
142  * @param dataType Type of the data.
143  * @param direction Direction of the port.
144  * @param parent The parent netlist block.
145  */
147  const std::string& name,
148  const std::string& widthFormula,
149  DataType dataType,
150  Direction direction,
151  BaseNetlistBlock& parent,
152  Signal signal)
153  : name_(name),
154  widthFormula_(widthFormula),
155  realWidth_(-1),
156  dataType_(dataType),
157  direction_(direction),
158  parentBlock_(&parent),
159  hasStaticValue_(false),
160  staticValue_(StaticSignal::GND),
161  signal_(signal) {
162 
163  parent.addPort(this);
164 }
165 
166 /**
167  * Copy constructor. Copies everything except parent block reference since one
168  * NetlistBlock may not have ports with identical names.
169  */
170 NetlistPort::NetlistPort(const NetlistPort& other, bool asMirrored)
171  : name_(other.name_),
172  widthFormula_(other.widthFormula_),
173  realWidth_(other.realWidth_),
174  dataType_(other.dataType_),
175  direction_(other.direction_),
176  parentBlock_(nullptr),
177  hasStaticValue_(other.hasStaticValue_),
178  staticValue_(other.staticValue_),
179  signal_(other.signal_) {
180 
181  if (asMirrored) {
183  }
184 }
185 
187  const std::string& name,
188  const std::string& widthFormula,
189  DataType dataType,
190  Direction direction,
191  Signal signal)
192  : name_(name),
193  widthFormula_(widthFormula),
194  realWidth_(-1),
195  dataType_(dataType),
196  direction_(direction),
197  parentBlock_(nullptr),
198  hasStaticValue_(false),
199  staticValue_(StaticSignal::GND),
200  signal_(signal) {
201 }
202 
203 bool
204 NetlistPort::resolveRealWidth(int& width) const {
205 
206  std::string formula = widthFormula();
207  // check if it is a parameter
208  for (size_t i = 0; i < parentBlock().netlist().parameterCount(); i++) {
209  Parameter param = parentBlock().netlist().parameter(i);
210  if (param.name() == formula) {
211  width = Conversion::toInt(param.value());
212  return true;
213  }
214  }
215 
216  // check if formula is a plain number
217  bool success = false;
218  try {
219  width = Conversion::toInt(formula);
220  success = true;
221  } catch (Exception& e) {
222  success = false;
223  }
224  return success;
225 }
226 
227 /**
228  * DEPRECATED
229  */
232  BaseNetlistBlock& newParent,
233  std::string newName) const {
234  if (newName == "")
235  newName = this->name();
236 
237  if (realWidthAvailable()) {
238  return new NetlistPort(
239  newName, widthFormula(), realWidth(), dataType(), direction(),
240  newParent, assignedSignal());
241  } else {
242  int width = 0;
243  if (resolveRealWidth(width)) {
244  return new NetlistPort(
245  newName, widthFormula(), width, dataType(), direction(),
246  newParent, assignedSignal());
247  } else {
248  return new NetlistPort(
249  newName, widthFormula(), dataType(), direction(), newParent,
250  assignedSignal());
251  }
252  }
253  return NULL;
254 }
255 
256 
258 NetlistPort::clone(bool asMirrored) const {
259  NetlistPort* newPort = new NetlistPort(*this, asMirrored);
260  assert(newPort->assignedSignal().type() == this->assignedSignal().type());
261  return newPort;
262 }
263 
264 
265 /**
266  * Destructor.
267  *
268  * Removes itself from the parent netlist block.
269  */
271  if (hasParentBlock()) {
272  parentBlock_->removePort(this);
273  }
274 }
275 
276 
277 /**
278  * Returns the name of the port.
279  *
280  * @return The name of the port.
281  */
282 std::string
284  return name_;
285 }
286 
287 
288 /**
289  * Sets new name of the port.
290  *
291  * @return The name of the port.
292  */
293 void
294 NetlistPort::rename(const std::string& newname) {
295  if (hasParentBlock()) {
296  if(parentBlock_->port(newname, false) == nullptr ||
297  parentBlock_->port(newname, false) == this) {
298  name_ = newname;
299  } else {
300  THROW_EXCEPTION(ObjectAlreadyExists, "Port to be renamed ("
301  + name() +") to " + newname +
302  " is not unique within the block.");
303  }
304  } else {
305  name_ = newname;
306  }
307 }
308 
309 
310 /**
311  * Returns the formula that defines the width of the port.
312  *
313  * @return The formula.
314  */
315 std::string
317  return widthFormula_;
318 }
319 
320 /**
321  * Changes port's width formula.
322  */
323 void
324 NetlistPort::setWidthFormula(const std::string& newFormula) {
325  widthFormula_ = newFormula;
326 }
327 
328 /**
329  * Tells whether the actual bit width of the port is known.
330  *
331  * @return True if the width is known, otherwise false.
332  */
333 bool
335  // TODO: there might still be possible regressions from changing realWidth
336  // check to allow zero.
337  return realWidth_ >= 0;
338 }
339 
340 
341 /**
342  * Returns the actual bit width of the port.
343  *
344  * @return The actual bit width.
345  * @exception NotAvailable If the actual width is not known.
346  */
347 int
349  if (!realWidthAvailable()) {
350  throw NotAvailable(__FILE__, __LINE__, __func__, "Port " + name()
351  + " doesn't have actual bit width.");
352  }
353  return realWidth_;
354 }
355 
356 /**
357  * Returns the data type of the port.
358  *
359  * @return The data type of the port.
360  */
361 DataType
363  return dataType_;
364 }
365 
366 
367 /**
368  * Returns the direction of the port.
369  *
370  * @return The direction of the port.
371  */
372 Direction
374  return direction_;
375 }
376 
377 /**
378  * Sets direction of the port.
379  */
380 void
383 }
384 
385 /**
386  * Returns true if ports is attached to some netlist block. Otherwise,
387  * returns false.
388  */
389 bool
391  return parentBlock_ != NULL;
392 }
393 
394 /**
395  * Returns the parent netlist block.
396  *
397  * @return The parent netlist block.
398  */
399 const BaseNetlistBlock&
401  return *parentBlock_;
402 }
403 
406  return *parentBlock_;
407 }
408 
409 void
411 
412  hasStaticValue_ = true;
413  staticValue_ = value;
414 }
415 
416 void
418 
419  hasStaticValue_ = false;
420 }
421 
422 bool
424  return hasStaticValue_;
425 }
426 
427 
430  return staticValue_;
431 }
432 
433 /**
434  * Set parent block of this port.
435  *
436  * @param newParent The new parent. Can be NULL too.
437  */
438 void
440  parentBlock_ = parent;
441 }
442 
443 /**
444  * Assign signal to signify usage of the port.
445  */
446 void
448  signal_ = signal;
449 }
450 
451 /**
452  * Return signal assigned to the port.
453  */
454 Signal
456  return signal_;
457 }
458 
460  const std::string& name,
461  const std::string& widthFormula,
462  int realWidth,
463  DataType dataType,
464  BaseNetlistBlock& parent,
465  Signal signal)
466  : NetlistPort(name, widthFormula, realWidth,
467  dataType, OUT, parent, signal) {
468 }
469 
471  const std::string& name,
472  const std::string& widthFormula,
473  DataType dataType,
474  BaseNetlistBlock& parent,
475  Signal signal)
476  : NetlistPort(name, widthFormula, dataType, OUT, parent, signal) {
477 }
478 
480  const std::string& name,
481  const std::string& widthFormula,
482  DataType dataType,
483  Signal signal)
484  : NetlistPort(name, widthFormula, dataType, OUT, signal) {
485 }
486 
488  const std::string& name,
489  BaseNetlistBlock& parent,
490  Signal signal)
491  : NetlistPort(name, "1", 1, BIT, OUT, parent, signal) {
492 }
493 
495  const std::string& name,
496  Signal signal)
497  : NetlistPort(name, "1", BIT, OUT, signal) {
498 }
499 
501  const std::string& name,
502  const std::string& widthFormula,
503  int realWidth,
504  DataType dataType,
505  BaseNetlistBlock& parent,
506  Signal signal)
507  : NetlistPort(name, widthFormula, realWidth,
508  dataType, IN, parent, signal) {
509 }
510 
512  const std::string& name,
513  const std::string& widthFormula,
514  DataType dataType,
515  BaseNetlistBlock& parent,
516  Signal signal)
517  : NetlistPort(name, widthFormula, dataType, IN, parent, signal) {
518 }
519 
521  const std::string& name,
522  const std::string& widthFormula,
523  DataType dataType,
524  Signal signal)
525  : NetlistPort(name, widthFormula, dataType, IN, signal) {
526 }
527 
529  const std::string& name,
530  BaseNetlistBlock& parent,
531  Signal signal)
532  : NetlistPort(name, "1", 1, BIT, IN, parent, signal) {
533 }
534 
536  const std::string& name,
537  Signal signal)
538  : NetlistPort(name, "1", BIT, IN, signal) {
539 }
540 
541 } // namespace ProGe
ProGe::NetlistPort::hasStaticValue_
bool hasStaticValue_
Indicates if port is connected to vcc or gnd.
Definition: NetlistPort.hh:150
ProGe::NetlistPort::dataType_
DataType dataType_
Data type of the port.
Definition: NetlistPort.hh:144
ProGe::NetlistPort::widthFormula_
std::string widthFormula_
Formula for the width of the port.
Definition: NetlistPort.hh:140
ProGe::BaseNetlistBlock::addPort
NetlistPort * addPort(NetlistPort *port)
Definition: BaseNetlistBlock.cc:467
ProGe::BaseNetlistBlock
Definition: BaseNetlistBlock.hh:59
ProGe::NetlistPort::NetlistPort
NetlistPort(const std::string &name, const std::string &widthFormula, int realWidth, DataType dataType, Direction direction, BaseNetlistBlock &parent, Signal signal=Signal())
Definition: NetlistPort.cc:64
ProGe::NetlistPort::parentBlock
const BaseNetlistBlock & parentBlock() const
Definition: NetlistPort.cc:400
ProGe::OutBitPort::OutBitPort
OutBitPort(const std::string &name, BaseNetlistBlock &parent, Signal signal=Signal())
Definition: NetlistPort.cc:487
ProGe::BaseNetlistBlock::removePort
void removePort(NetlistPort *port)
Definition: BaseNetlistBlock.cc:494
OutOfRange
Definition: Exception.hh:320
ProGe::NetlistPort::widthFormula
std::string widthFormula() const
Definition: NetlistPort.cc:316
ProGe::NetlistPort::setDirection
void setDirection(Direction direction)
Definition: NetlistPort.cc:381
ProGe::NetlistPort::direction
Direction direction() const
Definition: NetlistPort.cc:373
ProGe::NetlistPort::unsetStatic
void unsetStatic() const
Definition: NetlistPort.cc:417
ProGe::InBitPort::InBitPort
InBitPort(const std::string &name, BaseNetlistBlock &parent, Signal signal=Signal())
Definition: NetlistPort.cc:528
Conversion
Definition: Conversion.hh:52
ProGe::NetlistPort::setToStatic
void setToStatic(StaticSignal value) const
Definition: NetlistPort.cc:410
ProGe::NetlistPort::direction_
Direction direction_
Direction of the port.
Definition: NetlistPort.hh:146
ProGe::NetlistPort::assignSignal
void assignSignal(Signal signal)
Definition: NetlistPort.cc:447
ProGe::InPort::InPort
InPort(const std::string &name, const std::string &widthFormula, int realWidth, DataType dataType, BaseNetlistBlock &parent, Signal signal=Signal())
Definition: NetlistPort.cc:500
ProGe::BaseNetlistBlock::netlist
virtual const Netlist & netlist() const
Definition: BaseNetlistBlock.cc:348
ProGe::OutPort::OutPort
OutPort(const std::string &name, const std::string &widthFormula, int realWidth, DataType dataType, BaseNetlistBlock &parent, Signal signal=Signal())
Definition: NetlistPort.cc:459
NotAvailable
Definition: Exception.hh:728
ProGe::NetlistPort::staticValue
StaticSignal staticValue() const
Definition: NetlistPort.cc:429
ProGe::NetlistPort::~NetlistPort
virtual ~NetlistPort()
Definition: NetlistPort.cc:270
assert
#define assert(condition)
Definition: Application.hh:86
ProGe::NetlistPort::signal_
Signal signal_
Assigned port usage.
Definition: NetlistPort.hh:154
ProGe::NetlistPort::realWidthAvailable
bool realWidthAvailable() const
Definition: NetlistPort.cc:334
ProGe::Netlist::parameterCount
size_t parameterCount() const
Definition: Netlist.cc:422
ProGe::NetlistPort::copyTo
NetlistPort * copyTo(BaseNetlistBlock &newParent, std::string newName="") const
Definition: NetlistPort.cc:231
ProGe::StaticSignal
Definition: NetlistPort.hh:47
NetlistTools.hh
THROW_EXCEPTION
#define THROW_EXCEPTION(exceptionType, message)
Exception wrapper macro that automatically includes file name, line number and function name where th...
Definition: Exception.hh:39
Conversion.hh
ProGe::Parameter
Definition: Parameter.hh:62
NetlistPort.hh
ProGe::Parameter::name
const TCEString & name() const
Definition: Parameter.cc:133
__func__
#define __func__
Definition: Application.hh:67
NetlistBlock.hh
ProGe::NetlistPort::hasParentBlock
bool hasParentBlock() const
Definition: NetlistPort.cc:390
ProGe::Parameter::value
const TCEString & value() const
Definition: Parameter.cc:143
ProGe::BIT
@ BIT
One bit.
Definition: ProGeTypes.hh:47
Exception
Definition: Exception.hh:54
ProGe::NetlistPort::name
std::string name() const
Definition: NetlistPort.cc:283
ProGe::NetlistPort::realWidth_
int realWidth_
Real width of the port.
Definition: NetlistPort.hh:142
ProGe::NetlistPort::clone
virtual NetlistPort * clone(bool asMirrored=false) const
Definition: NetlistPort.cc:258
ProGe::OUT
@ OUT
Output port.
Definition: ProGeTypes.hh:54
ProGe::NetlistPort::dataType
DataType dataType() const
Definition: NetlistPort.cc:362
ProGe::NetlistPort::name_
std::string name_
Name of the port.
Definition: NetlistPort.hh:138
ProGe::NetlistPort::realWidth
int realWidth() const
Definition: NetlistPort.cc:348
ProGe::NetlistPort::setParent
void setParent(BaseNetlistBlock *parent)
Definition: NetlistPort.cc:439
ProGe::NetlistPort::setWidthFormula
void setWidthFormula(const std::string &newFormula)
Definition: NetlistPort.cc:324
ProGe::NetlistPort::resolveRealWidth
bool resolveRealWidth(int &width) const
Definition: NetlistPort.cc:204
ProGe::Signal::type
SignalType type() const
Definition: Signal.cc:55
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
ProGe
Definition: FUGen.hh:54
BaseNetlistBlock.hh
ObjectAlreadyExists
Definition: Exception.hh:1002
ProGe::DataType
DataType
Data types of hardware ports.
Definition: ProGeTypes.hh:46
ProGe::NetlistPort::hasStaticValue
bool hasStaticValue() const
Definition: NetlistPort.cc:423
ProGe::NetlistTools::mirror
static Direction mirror(Direction direction)
Definition: NetlistTools.cc:202
TCEString
Definition: TCEString.hh:53
ProGe::Netlist::parameter
Parameter parameter(size_t index) const
Definition: Netlist.cc:434
ProGe::NetlistPort
Definition: NetlistPort.hh:70
ProGe::NetlistPort::assignedSignal
Signal assignedSignal() const
Definition: NetlistPort.cc:455
Conversion::toInt
static int toInt(const T &source)
ProGe::NetlistPort::parentBlock_
BaseNetlistBlock * parentBlock_
The parent netlist block.
Definition: NetlistPort.hh:148
ProGe::NetlistPort::rename
void rename(const std::string &newname)
Definition: NetlistPort.cc:294
ProGe::Signal
Definition: Signal.hh:46
ProGe::Direction
Direction
Direction of the port.
Definition: ProGeTypes.hh:52
ProGe::IN
@ IN
Input port.
Definition: ProGeTypes.hh:53
ProGe::NetlistPort::staticValue_
StaticSignal staticValue_
Static signal value.
Definition: NetlistPort.hh:152
ProGe::BaseNetlistBlock::port
virtual const NetlistPort & port(size_t index) const
Definition: BaseNetlistBlock.cc:253