OpenASIP  2.0
NetlistTools.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2015 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 NetlistTools.cc
26  *
27  * Implementation of NetlistTools class.
28  *
29  * Created on: 5.5.2015
30  * @author: Henry Linjamäki (henry.linjamaki-no.spam-tut.fi)
31  * @note rating: red
32  */
33 
34 #include "NetlistTools.hh"
35 
36 #include <cstddef>
37 #include <set>
38 #include <algorithm>
39 #include <iterator>
40 
41 #include "BaseNetlistBlock.hh"
42 #include "NetlistPort.hh"
43 #include "NetlistPortGroup.hh"
44 
45 namespace ProGe {
46 
47 /**
48  * Finds common parent block of given netlist blocks and maximum distance to
49  * the common parent.
50  *
51  * Distance is calculated how many times parent block pointer is needed to
52  * followed to get to the common parent block.
53  *
54  * @return Returns found common parent and maximum distance to the parent as
55  * pair. The first member is the common parent and the second is the
56  * distance. If common parent has not found return (NULL, 0).
57  */
58 std::pair<const BaseNetlistBlock*, size_t>
60  const BaseNetlistBlock& b1, const BaseNetlistBlock& b2) {
61  // Todo check trivial cases: b1 id parent of b2 or vice versa.
62 
63  std::set<const BaseNetlistBlock*> parentChain;
64 
65  const BaseNetlistBlock* block = &b1;
66  do {
67  parentChain.insert(block);
68  } while ((block = parent(block)));
69 
70  const BaseNetlistBlock* found = NULL;
71  block = &b2;
72  do {
73  if (parentChain.count(block)) {
74  found = block;
75  break;
76  }
77  } while ((block = parent(block)));
78 
79  if (found == NULL) {
80  return std::make_pair(found, 0);
81  }
82 
83  size_t distance = 0;
84  block = &b1;
85  size_t hops = 0;
86  while (block != found) {
87  hops++;
88  block = parent(block);
89  }
90  distance = hops;
91  block = &b2;
92  hops = 0;
93  while (block != found) {
94  hops++;
95  block = parent(block);
96  }
97  distance = std::max(distance, hops);
98 
99  return std::make_pair(found, distance);
100 }
101 
102 /**
103  * Renames ports by adding prefix into them.
104  */
105 void
107  NetlistPortGroup& portGroup, const std::string& prefix) {
108  for (NetlistPort* port : portGroup) {
109  addPrefixToPortName(*port, prefix);
110  }
111 }
112 
113 /**
114  * Renames port by adding prefix into it.
115  */
116 void
118  NetlistPort& port, const std::string& prefix) {
119  port.rename(prefix + port.name());
120 }
121 
122 /**
123  * Renames ports in netlist port group by given rules.
124  *
125  * returns number of ports left renamed.
126  */
127 size_t
129  NetlistPortGroup& portGroup,
130  std::map<SignalType, const std::string>&& renameRules) {
131  size_t renamedCount = 0;
132  for (NetlistPort* port : portGroup) {
133  SignalType type = port->assignedSignal().type();
134  if (renameRules.count(type)) {
135  port->rename(renameRules.at(type));
136  renamedCount++;
137  }
138  }
139  return portGroup.portCount() - renamedCount;
140 }
141 
142 /**
143  * Returns a string that is unique within the given netlist block.
144  *
145  * That is, the string as instance name does not clash with any of the
146  * immediate sub block of the given netlist block.
147  *
148  * @param within The netlist block.
149  * @param basename The string used as base for the instance name.
150  * @return The unique instance name. May be the given base name if it itself
151  * is unique within the block. Otherwise the the returned string is
152  * base name postfixed with a running number.
153  */
154 std::string
156  const BaseNetlistBlock& within, const std::string& basename) {
157  std::string name(basename);
158  while (within.hasSubBlock(name)) {
159  int postFixNumber = -1;
160  std::string::iterator it;
161  std::string::reverse_iterator rit;
162  for (rit = name.rbegin(); rit != name.rend(); rit++) {
163  std::locale loc;
164  if (!std::isdigit(*rit, loc)) {
165  it = rit.base(); // Points to first number digit or end().
166  break;
167  }
168  }
169  if (it != name.end()) {
170  postFixNumber = Conversion::toInt(std::string(it, name.end()));
171  }
172  postFixNumber++;
173  name.replace(it, name.end(), Conversion::toString(postFixNumber));
174  }
175  return name;
176 }
177 
178 /**
179  * Returns parent of the given block or NULL if the block does not have one.
180  */
181 const BaseNetlistBlock*
183  return parent(&block);
184 }
185 
186 /**
187  * Returns parent of the given block or NULL if the block does not have one.
188  */
189 const BaseNetlistBlock*
191  if (!block->hasParentBlock()) {
192  return NULL;
193  } else {
194  return &block->parentBlock();
195  }
196 }
197 
198 /**
199  * Returns given direction mirrored.
200  */
201 Direction
203  switch (direction) {
204  case IN:
205  return OUT;
206  break;
207  case OUT:
208  return IN;
209  break;
210  case BIDIR:
211  return BIDIR;
212  break;
213  default:
214  assert(false && "Unregocnized direction.");
215  return IN;
216  }
217 }
218 
219 /**
220  * Mirrors ports direction.
221  */
224  port->setDirection(mirror(port->direction()));
225  return port;
226 }
227 
228 } /* namespace ProGe */
ProGe::BaseNetlistBlock
Definition: BaseNetlistBlock.hh:59
ProGe::BaseNetlistBlock::hasSubBlock
virtual bool hasSubBlock(const std::string &instanceName) const
Definition: BaseNetlistBlock.cc:160
ProGe::NetlistPort::setDirection
void setDirection(Direction direction)
Definition: NetlistPort.cc:381
ProGe::NetlistPort::direction
Direction direction() const
Definition: NetlistPort.cc:373
ProGe::BIDIR
@ BIDIR
Bidirectional port.
Definition: ProGeTypes.hh:55
ProGe::NetlistTools::parent
static const BaseNetlistBlock * parent(const BaseNetlistBlock &block)
Definition: NetlistTools.cc:182
ProGe::BaseNetlistBlock::parentBlock
virtual const BaseNetlistBlock & parentBlock() const
Definition: BaseNetlistBlock.cc:358
ProGe::NetlistTools::renamePorts
static size_t renamePorts(NetlistPortGroup &portGroup, std::map< SignalType, const std::string > &&renameRules)
Definition: NetlistTools.cc:128
Conversion::toString
static std::string toString(const T &source)
NetlistPortGroup.hh
ProGe::BaseNetlistBlock::hasParentBlock
virtual bool hasParentBlock() const
Definition: BaseNetlistBlock.cc:353
assert
#define assert(condition)
Definition: Application.hh:86
ProGe::NetlistPortGroup
Definition: NetlistPortGroup.hh:53
ProGe::NetlistTools::commonParent
static std::pair< const BaseNetlistBlock *, size_t > commonParent(const BaseNetlistBlock &b1, const BaseNetlistBlock &b2)
Definition: NetlistTools.cc:59
NetlistTools.hh
NetlistPort.hh
ProGe::NetlistTools::addPrefixToPortName
static void addPrefixToPortName(NetlistPort &port, const std::string &prefix)
Definition: NetlistTools.cc:117
ProGe::NetlistPort::name
std::string name() const
Definition: NetlistPort.cc:283
ProGe::OUT
@ OUT
Output port.
Definition: ProGeTypes.hh:54
ProGe::SignalType
SignalType
Definition: SignalTypes.hh:42
ProGe
Definition: FUGen.hh:54
BaseNetlistBlock.hh
ProGe::NetlistTools::mirror
static Direction mirror(Direction direction)
Definition: NetlistTools.cc:202
ProGe::NetlistTools::addPrefixToPortNames
static void addPrefixToPortNames(NetlistPortGroup &portGroup, const std::string &prefix)
Definition: NetlistTools.cc:106
ProGe::NetlistPort
Definition: NetlistPort.hh:70
Conversion::toInt
static int toInt(const T &source)
ProGe::NetlistPort::rename
void rename(const std::string &newname)
Definition: NetlistPort.cc:294
ProGe::Direction
Direction
Direction of the port.
Definition: ProGeTypes.hh:52
ProGe::IN
@ IN
Input port.
Definition: ProGeTypes.hh:53
ProGe::NetlistTools::getUniqueInstanceName
static std::string getUniqueInstanceName(const BaseNetlistBlock &within, const std::string &basename)
Definition: NetlistTools.cc:155