OpenASIP 2.2
Loading...
Searching...
No Matches
NetlistBlock.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 NetlistBlock.cc
26 *
27 * Implementation of NetlistBlock 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 <string>
36#include <iostream>
37
38#include "NetlistBlock.hh"
39#include "NetlistPort.hh"
40#include "NetlistPortGroup.hh"
41#include "Netlist.hh"
42#include "Parameter.hh"
43#include "NetlistWriter.hh"
44#include "VHDLNetlistWriter.hh"
46#include "FileSystem.hh"
47
48#include "SequenceTools.hh"
49#include "ContainerTools.hh"
50#include "MapTools.hh"
51#include "Application.hh"
52#include "Conversion.hh"
53
54using std::string;
55
56namespace ProGe {
57
58/**
59 * Constructor. Creates a netlist with no ports.
60 *
61 * The created is empty. Only its name, the parent netlist and the
62 * name of the instance module are defined.
63 *
64 * @param moduleName Name of the module.
65 * @param instanceName Name of the instance of the module.
66 * @param netlist The netlist which the belongs to.
67 */
69 const std::string& moduleName, const std::string& instanceName,
70 BaseNetlistBlock* parent)
71 : BaseNetlistBlock(moduleName, instanceName, parent) {}
72
73/**
74 * The destructor.
75 *
76 * Deletes all the ports.
77 */
80
81/**
82 * Sets the given parameter for the .
83 *
84 * @param name Name of the parameter.
85 * @param type Type of the parameter.
86 * @param value Value of the parameter.
87 */
88void
90 const std::string& name,
91 const std::string& type,
92 const std::string& value) {
93 setParameter(Parameter(name, type, value));
94}
95
97NetlistBlock::port(const std::string& portName, bool partialMatch) {
98 return BaseNetlistBlock::findPort(portName, false, partialMatch);
99}
100
101/**
102 * Returns a sub by the given index.
103 *
104 * @param index The index.
105 * @return The sub .
106 * @exception OutOfRange If the given index is negative or not smaller than
107 * the number of sub blocks.
108 */
111 if (index >= subBlockCount()) {
112 throw OutOfRange(__FILE__, __LINE__, __func__);
113 }
114
115 // todo check for and throw wrong subblass
116 return *dynamic_cast<NetlistBlock*>(&BaseNetlistBlock::subBlock(index));
117}
118
119/**
120 * Returns the parent .
121 *
122 * @return The parent .
123 * @exception InstanceNotFound If the does not have a parent .
124 */
125const NetlistBlock&
127 if (!hasParentBlock()) {
128 const string procName = "NetlistBlock::parentBlock";
129 throw InstanceNotFound(__FILE__, __LINE__, procName);
130 }
131
132 // todo: fix parentblock may not be NetlistBlock.
133 return *dynamic_cast<const NetlistBlock*>(
135}
136
137/**
138 * Returns the parent .
139 *
140 * @return The parent .
141 * @exception InstanceNotFound If the does not have a parent .
142 */
145 if (!hasParentBlock()) {
146 const string procName = "NetlistBlock::parentBlock";
147 throw InstanceNotFound(__FILE__, __LINE__, procName);
148 }
149
150 return *dynamic_cast<NetlistBlock*>(&BaseNetlistBlock::parentBlock());
151}
152
153/**
154 * Copies the block without its children or internal connections - leaving
155 * only the outer portion of the block.
156 *
157 * @param instanceName New instance name for the copy
158 * @return Pointer to the netlist copy
159 */
161NetlistBlock::shallowCopy(const std::string& instanceName) const {
162 NetlistBlock* block =
163 new NetlistBlock(this->moduleName(), instanceName, NULL);
164
165 for (size_t i = 0; i < this->parameterCount(); i++) {
166 block->setParameter(this->parameter(i));
167 }
168 for (size_t i = 0; i < netlist().parameterCount(); i++) {
169 block->netlist().setParameter(this->netlist().parameter(i));
170 }
171 // Copy ports while preserving their insertion order.
172 std::map<std::string, NetlistPort*> copiedPorts;
173 for (size_t i = 0; i < portCount(); i++) {
174 NetlistPort* copiedPort = nullptr;
175 copiedPort = BaseNetlistBlock::port(i).copyTo(*block);
176 copiedPorts.insert({{copiedPort->name(), copiedPort}});
177 }
178 for (size_t i = 0; i < portGroupCount(); i++) {
179 const NetlistPortGroup* portGrp = &portGroup(i);
180 // Cloning to copy original class type, but clear ports as they are
181 // already created and added to the new block.
182 NetlistPortGroup* newGroup = portGrp->clone();
183 newGroup->clear();
184 for (auto port : *portGrp) {
185 newGroup->addPort(*copiedPorts.at(port->name()));
186 }
187 block->addPortGroup(newGroup);
188 }
189
190 for (size_t i = 0; i < packageCount(); i++) {
191 block->addPackage(this->package(i));
192 }
193
194 return block;
195}
196
197/**
198 * Writes self if non-empty and calls write() function of each sub block.
199 */
200void
201NetlistBlock::write(const Path& targetBaseDir, HDL targetLang) const {
202 if (netlist().hasConnections()) {
203 NetlistWriter* writer;
204 std::string topLevelDir = "";
205 if (targetLang == ProGe::VHDL) {
206 writer = new VHDLNetlistWriter(*this);
207 topLevelDir = targetBaseDir.string() +
209 } else if (targetLang == ProGe::Verilog) {
210 writer = new VerilogNetlistWriter(*this);
211 topLevelDir = targetBaseDir.string() +
213 } else {
214 assert(false && "Unsupported HDL.");
215 }
216
217 if (!FileSystem::fileExists(topLevelDir)) {
218 bool directoryCreated = FileSystem::createDirectory(topLevelDir);
219 if (!directoryCreated) {
220 std::string errorMsg =
221 "Unable to create directory " + topLevelDir + ".";
222 throw IOException(__FILE__, __LINE__, __func__, errorMsg);
223 }
224 }
225
226 writer->write(topLevelDir);
227 delete writer;
228 }
229
230 BaseNetlistBlock::write(targetBaseDir, targetLang);
231}
232
233} // namespace ProGe
#define __func__
#define assert(condition)
static bool createDirectory(const std::string &path)
static const std::string DIRECTORY_SEPARATOR
static bool fileExists(const std::string fileName)
virtual const BaseNetlistBlock & subBlock(size_t index) const
virtual size_t parameterCount() const
virtual size_t portGroupCount() const
virtual const NetlistPortGroup & portGroup(size_t index) const
virtual void write(const Path &targetBaseDir, HDL targetLang=VHDL) const override
virtual const NetlistPort & port(size_t index) const
virtual bool hasParentBlock() const
virtual const BaseNetlistBlock & parentBlock() const
const std::string & moduleName() const
const std::string name() const
NetlistPort * findPort(const std::string &portName, bool recursiveSearch=false, bool partialMatch=true) const
NetlistBlock * shallowCopy(const std::string &instanceName) const
virtual size_t subBlockCount() const
virtual size_t packageCount() const
virtual const Parameter & parameter(const std::string &name) const
NetlistBlock(const std::string &moduleName, const std::string &instanceName, BaseNetlistBlock *parent=NULL)
virtual const std::string & package(size_t idx) const
void setParameter(const std::string &name, const std::string &type, const std::string &value)
virtual void write(const Path &targetBaseDir, HDL targetLang=VHDL) const override
virtual NetlistPort * port(const std::string &portName, bool partialMatch=true)
NetlistBlock & subBlock(size_t index) override
virtual size_t portCount() const
void addPackage(const std::string &packageName)
virtual const NetlistBlock & parentBlock() const override
void addPortGroup(NetlistPortGroup *portGroup)
virtual const Netlist & netlist() const
void addPort(NetlistPort &port)
virtual NetlistPortGroup * clone(bool asMirrored=false) const
NetlistPort * copyTo(BaseNetlistBlock &newParent, std::string newName="") const
std::string name() const
virtual void write(const std::string &dstDirectory)=0
size_t parameterCount() const
Definition Netlist.cc:422
void setParameter(const std::string &name, const std::string &type, const std::string &value)
Definition Netlist.cc:362
Parameter parameter(size_t index) const
Definition Netlist.cc:434
Definition FUGen.hh:54
HDL
HDLs supported by ProGe.
Definition ProGeTypes.hh:40
@ Verilog
Verilog.
Definition ProGeTypes.hh:42
@ VHDL
VHDL.
Definition ProGeTypes.hh:41