OpenASIP 2.2
Loading...
Searching...
No Matches
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
45namespace 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 */
58std::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 */
105void
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 */
116void
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 */
127size_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 */
154std::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 */
181const 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 */
189const BaseNetlistBlock*
191 if (!block->hasParentBlock()) {
192 return NULL;
193 } else {
194 return &block->parentBlock();
195 }
196}
197
198/**
199 * Returns given direction mirrored.
200 */
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 */
#define assert(condition)
static std::string toString(const T &source)
static int toInt(const T &source)
virtual bool hasSubBlock(const std::string &instanceName) const
virtual bool hasParentBlock() const
virtual const BaseNetlistBlock & parentBlock() const
void setDirection(Direction direction)
Direction direction() const
std::string name() const
void rename(const std::string &newname)
static void addPrefixToPortNames(NetlistPortGroup &portGroup, const std::string &prefix)
static const BaseNetlistBlock * parent(const BaseNetlistBlock &block)
static void addPrefixToPortName(NetlistPort &port, const std::string &prefix)
static std::string getUniqueInstanceName(const BaseNetlistBlock &within, const std::string &basename)
static size_t renamePorts(NetlistPortGroup &portGroup, std::map< SignalType, const std::string > &&renameRules)
static std::pair< const BaseNetlistBlock *, size_t > commonParent(const BaseNetlistBlock &b1, const BaseNetlistBlock &b2)
static Direction mirror(Direction direction)
Definition FUGen.hh:54
Direction
Direction of the port.
Definition ProGeTypes.hh:52
@ OUT
Output port.
Definition ProGeTypes.hh:54
@ IN
Input port.
Definition ProGeTypes.hh:53
@ BIDIR
Bidirectional port.
Definition ProGeTypes.hh:55