OpenASIP 2.2
Loading...
Searching...
No Matches
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
44namespace 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 */
170NetlistPort::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
203bool
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,
251 }
252 }
253 return NULL;
254}
255
256
258NetlistPort::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()) {
273 }
274}
275
276
277/**
278 * Returns the name of the port.
279 *
280 * @return The name of the port.
281 */
282std::string
284 return name_;
285}
286
287
288/**
289 * Sets new name of the port.
290 *
291 * @return The name of the port.
292 */
293void
294NetlistPort::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 */
315std::string
317 return widthFormula_;
318}
319
320/**
321 * Changes port's width formula.
322 */
323void
324NetlistPort::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 */
333bool
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 */
347int
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 */
363 return dataType_;
364}
365
366
367/**
368 * Returns the direction of the port.
369 *
370 * @return The direction of the port.
371 */
374 return direction_;
375}
376
377/**
378 * Sets direction of the port.
379 */
380void
384
385/**
386 * Returns true if ports is attached to some netlist block. Otherwise,
387 * returns false.
388 */
389bool
391 return parentBlock_ != NULL;
392}
393
394/**
395 * Returns the parent netlist block.
396 *
397 * @return The parent netlist block.
398 */
399const BaseNetlistBlock&
401 return *parentBlock_;
402}
403
408
409void
411
412 hasStaticValue_ = true;
413 staticValue_ = value;
414}
415
416void
418
419 hasStaticValue_ = false;
420}
421
422bool
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 */
438void
442
443/**
444 * Assign signal to signify usage of the port.
445 */
446void
448 signal_ = signal;
449}
450
451/**
452 * Return signal assigned to the port.
453 */
454Signal
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
#define __func__
#define assert(condition)
#define THROW_EXCEPTION(exceptionType, message)
Exception wrapper macro that automatically includes file name, line number and function name where th...
Definition Exception.hh:39
find Finds info of the inner loops in the false
static int toInt(const T &source)
NetlistPort * addPort(NetlistPort *port)
virtual const NetlistPort & port(size_t index) const
virtual const Netlist & netlist() const
void removePort(NetlistPort *port)
InBitPort(const std::string &name, BaseNetlistBlock &parent, Signal signal=Signal())
InPort(const std::string &name, const std::string &widthFormula, int realWidth, DataType dataType, BaseNetlistBlock &parent, Signal signal=Signal())
virtual NetlistPort * clone(bool asMirrored=false) const
void setToStatic(StaticSignal value) const
NetlistPort * copyTo(BaseNetlistBlock &newParent, std::string newName="") const
void setParent(BaseNetlistBlock *parent)
DataType dataType_
Data type of the port.
bool resolveRealWidth(int &width) const
BaseNetlistBlock * parentBlock_
The parent netlist block.
void setWidthFormula(const std::string &newFormula)
bool hasStaticValue() const
bool realWidthAvailable() const
std::string widthFormula() const
Signal assignedSignal() const
bool hasParentBlock() const
NetlistPort(const std::string &name, const std::string &widthFormula, int realWidth, DataType dataType, Direction direction, BaseNetlistBlock &parent, Signal signal=Signal())
DataType dataType() const
std::string widthFormula_
Formula for the width of the port.
void setDirection(Direction direction)
int realWidth_
Real width of the port.
bool hasStaticValue_
Indicates if port is connected to vcc or gnd.
StaticSignal staticValue_
Static signal value.
void unsetStatic() const
Direction direction_
Direction of the port.
const BaseNetlistBlock & parentBlock() const
Signal signal_
Assigned port usage.
Direction direction() const
std::string name_
Name of the port.
std::string name() const
StaticSignal staticValue() const
int realWidth() const
void rename(const std::string &newname)
void assignSignal(Signal signal)
static Direction mirror(Direction direction)
size_t parameterCount() const
Definition Netlist.cc:422
Parameter parameter(size_t index) const
Definition Netlist.cc:434
OutBitPort(const std::string &name, BaseNetlistBlock &parent, Signal signal=Signal())
OutPort(const std::string &name, const std::string &widthFormula, int realWidth, DataType dataType, BaseNetlistBlock &parent, Signal signal=Signal())
const TCEString & value() const
Definition Parameter.cc:143
const TCEString & name() const
Definition Parameter.cc:133
SignalType type() const
Definition Signal.cc:55
Definition FUGen.hh:54
DataType
Data types of hardware ports.
Definition ProGeTypes.hh:46
@ BIT
One bit.
Definition ProGeTypes.hh:47
Direction
Direction of the port.
Definition ProGeTypes.hh:52
@ OUT
Output port.
Definition ProGeTypes.hh:54
@ IN
Input port.
Definition ProGeTypes.hh:53