OpenASIP  2.0
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"
45 #include "VerilogNetlistWriter.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 
54 using std::string;
55 
56 namespace 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  */
79 }
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  */
88 void
90  const std::string& name,
91  const std::string& type,
92  const std::string& value) {
93  setParameter(Parameter(name, type, value));
94 }
95 
97 NetlistBlock::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  */
110 NetlistBlock::subBlock(size_t index) {
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  */
125 const 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  */
160 NetlistBlock*
161 NetlistBlock::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  */
200 void
201 NetlistBlock::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
ProGe::BaseNetlistBlock
Definition: BaseNetlistBlock.hh:59
Netlist.hh
ProGe::BaseNetlistBlock::name
const std::string name() const
Definition: BaseNetlistBlock.cc:145
ProGe::NetlistBlock::netlist
virtual const Netlist & netlist() const
Definition: BaseNetlistBlock.cc:348
ProGe::NetlistPortGroup::clone
virtual NetlistPortGroup * clone(bool asMirrored=false) const
Definition: NetlistPortGroup.cc:169
ProGe::NetlistBlock::write
virtual void write(const Path &targetBaseDir, HDL targetLang=VHDL) const override
Definition: NetlistBlock.cc:201
Path
Definition: FileSystem.hh:197
FileSystem.hh
FileSystem::createDirectory
static bool createDirectory(const std::string &path)
Definition: FileSystem.cc:400
ProGe::NetlistBlock
Definition: NetlistBlock.hh:61
ProGe::Verilog
@ Verilog
Verilog.
Definition: ProGeTypes.hh:42
ProGe::NetlistPortGroup::clear
void clear()
Definition: NetlistPortGroup.cc:128
ProGe::BaseNetlistBlock::parameterCount
virtual size_t parameterCount() const
Definition: BaseNetlistBlock.cc:231
OutOfRange
Definition: Exception.hh:320
MapTools.hh
ProGe::VHDLNetlistWriter
Definition: VHDLNetlistWriter.hh:52
SequenceTools.hh
ProGe::NetlistBlock::setParameter
void setParameter(const std::string &name, const std::string &type, const std::string &value)
Definition: NetlistBlock.cc:89
ProGe::NetlistWriter
Definition: NetlistWriter.hh:47
ProGe::BaseNetlistBlock::package
virtual const std::string & package(size_t idx) const
Definition: BaseNetlistBlock.cc:699
ProGe::NetlistBlock::parentBlock
virtual const NetlistBlock & parentBlock() const override
Definition: NetlistBlock.cc:126
ProGe::BaseNetlistBlock::Parameter
friend class Parameter
Definition: BaseNetlistBlock.hh:63
ProGe::BaseNetlistBlock::netlist
virtual const Netlist & netlist() const
Definition: BaseNetlistBlock.cc:348
ProGe::BaseNetlistBlock::parentBlock
virtual const BaseNetlistBlock & parentBlock() const
Definition: BaseNetlistBlock.cc:358
ProGe::NetlistBlock::addPackage
void addPackage(const std::string &packageName)
Definition: BaseNetlistBlock.cc:687
ProGe::BaseNetlistBlock::portGroupCount
virtual size_t portGroupCount() const
Definition: BaseNetlistBlock.cc:327
NetlistPortGroup.hh
ProGe::BaseNetlistBlock::hasParentBlock
virtual bool hasParentBlock() const
Definition: BaseNetlistBlock.cc:353
assert
#define assert(condition)
Definition: Application.hh:86
NetlistWriter.hh
ProGe::NetlistPortGroup
Definition: NetlistPortGroup.hh:53
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::VHDL
@ VHDL
VHDL.
Definition: ProGeTypes.hh:41
ProGe::BaseNetlistBlock::parameter
virtual const Parameter & parameter(const std::string &name) const
Definition: BaseNetlistBlock.cc:198
Conversion.hh
NetlistPort.hh
ProGe::BaseNetlistBlock::portCount
virtual size_t portCount() const
Definition: BaseNetlistBlock.cc:248
Application.hh
ProGe::BaseNetlistBlock::packageCount
virtual size_t packageCount() const
Definition: BaseNetlistBlock.cc:694
__func__
#define __func__
Definition: Application.hh:67
ProGe::NetlistBlock::~NetlistBlock
virtual ~NetlistBlock()
Definition: NetlistBlock.cc:78
NetlistBlock.hh
ProGe::NetlistPortGroup::addPort
void addPort(NetlistPort &port)
Definition: NetlistPortGroup.cc:93
ProGe::BaseNetlistBlock::findPort
NetlistPort * findPort(const std::string &portName, bool recursiveSearch=false, bool partialMatch=true) const
Definition: BaseNetlistBlock.cc:574
VHDLNetlistWriter.hh
ProGe::BaseNetlistBlock::portGroup
virtual const NetlistPortGroup & portGroup(size_t index) const
Definition: BaseNetlistBlock.cc:332
ProGe::NetlistBlock::shallowCopy
NetlistBlock * shallowCopy(const std::string &instanceName) const
Definition: NetlistBlock.cc:161
ProGe::NetlistPort::name
std::string name() const
Definition: NetlistPort.cc:283
VerilogNetlistWriter.hh
FileSystem::DIRECTORY_SEPARATOR
static const std::string DIRECTORY_SEPARATOR
Definition: FileSystem.hh:189
ProGe::NetlistWriter::write
virtual void write(const std::string &dstDirectory)=0
ProGe
Definition: FUGen.hh:54
FileSystem::fileExists
static bool fileExists(const std::string fileName)
ProGe::Netlist::parameter
Parameter parameter(size_t index) const
Definition: Netlist.cc:434
ProGe::NetlistBlock::addPortGroup
void addPortGroup(NetlistPortGroup *portGroup)
Definition: BaseNetlistBlock.cc:508
ProGe::HDL
HDL
HDLs supported by ProGe.
Definition: ProGeTypes.hh:40
ProGe::NetlistPort
Definition: NetlistPort.hh:70
ProGe::BaseNetlistBlock::moduleName
const std::string & moduleName() const
Definition: BaseNetlistBlock.cc:140
ProGe::BaseNetlistBlock::subBlock
virtual const BaseNetlistBlock & subBlock(size_t index) const
Definition: BaseNetlistBlock.cc:155
ProGe::Netlist::setParameter
void setParameter(const std::string &name, const std::string &type, const std::string &value)
Definition: Netlist.cc:362
IOException
Definition: Exception.hh:130
ProGe::VerilogNetlistWriter
Definition: VerilogNetlistWriter.hh:52
ProGe::NetlistBlock::subBlock
NetlistBlock & subBlock(size_t index) override
Definition: NetlistBlock.cc:110
ProGe::NetlistBlock::NetlistBlock
NetlistBlock(const std::string &moduleName, const std::string &instanceName, BaseNetlistBlock *parent=NULL)
Definition: NetlistBlock.cc:68
ProGe::NetlistBlock::port
virtual NetlistPort * port(const std::string &portName, bool partialMatch=true)
Definition: NetlistBlock.cc:97
Parameter.hh
InstanceNotFound
Definition: Exception.hh:304
ProGe::BaseNetlistBlock::subBlockCount
virtual size_t subBlockCount() const
Definition: BaseNetlistBlock.cc:150
ProGe::BaseNetlistBlock::port
virtual const NetlistPort & port(size_t index) const
Definition: BaseNetlistBlock.cc:253
ProGe::BaseNetlistBlock::write
virtual void write(const Path &targetBaseDir, HDL targetLang=VHDL) const override
Definition: BaseNetlistBlock.cc:614
ContainerTools.hh