18 #endif //HAVE_CONFIG_H
42 using namespace torc::generic;
44 class FlatteningHelpers {
47 static FlatteningHelpers* instance();
67 inline void setSeparator(
const std::string& inSeparator);
74 FlatteningHelpers(
const FlatteningHelpers&);
75 FlatteningHelpers& operator =(
const FlatteningHelpers&);
84 FlatteningHelpers* FlatteningHelpers::instance() {
85 static FlatteningHelpers obj;
90 return mNetNamingFunction;
93 inline void FlatteningHelpers::setNetNamingFunction(
const NetNamingFunction& inNetNamingFunction) {
94 mNetNamingFunction = inNetNamingFunction;
98 return mNetRenamingFunction;
101 inline void FlatteningHelpers::setNetRenamingFunction(
103 mNetRenamingFunction = inNetRenamingFunction;
107 return mInstanceNamingFunction;
110 inline void FlatteningHelpers::setInstanceNamingFunction(
112 mInstanceNamingFunction = inInstanceNamingFunction;
116 return mInstanceRenamingFunction;
119 inline void FlatteningHelpers::setInstanceRenamingFunction(
121 mInstanceRenamingFunction = inInstanceRenamingFunction;
124 inline std::string FlatteningHelpers::getSeparator() {
128 inline void FlatteningHelpers::setSeparator(
const std::string& inSeparator) {
129 mSeparator = inSeparator;
132 inline void FlatteningHelpers::reset() {
140 FlatteningHelpers::FlatteningHelpers() : mNetNamingFunction(), mNetRenamingFunction(),
141 mInstanceNamingFunction(), mInstanceRenamingFunction(), mSeparator() {}
143 template <
typename _Po
inter>
struct NameModifier {
145 static NameModifier* instance() {
146 static NameModifier obj;
150 const std::vector<size_t>& inIndicesVector = std::vector<size_t>()) {
152 name = inInstName +
"_";
153 if(inIndicesVector.empty()) {
154 name += inNameable->getName();
156 std::ostringstream indices;
157 copy(inIndicesVector.begin(), inIndicesVector.end(),
158 std::ostream_iterator < size_t > (indices,
"_"));
159 name += indices.str();
160 name += inNameable->getName();
162 if(name.length() > 255) {
163 std::map<std::string, std::string>::iterator it = mNameMap.find(name);
164 if(it != mNameMap.end()) {
168 std::ostringstream sout;
174 mNameMap.insert(std::make_pair(name, numericName));
180 mNameMap(), mNumber(1) {
187 std::map<std::string, std::string> mNameMap;
194 log(
"Replicating portRef connections for %s to %s\n", inOrigNet->getName().c_str(),
195 outTargetNet->getName().c_str());
196 std::vector<PortReferenceSharedPtr> portRefs;
198 inOrigNet->getConnectedPortRefs(portRefs, !isBit);
199 for(std::vector<PortReferenceSharedPtr>::iterator ref = portRefs.begin(); ref != portRefs.end();
201 std::vector < std::string > nestedNames;
207 nestedNames.push_back(portRef->getName());
208 instance = portRef->getParent();
210 portRef = portRef->getParentCollection();
212 std::vector < size_t > indices;
215 instance = instance->getParentCollection();
217 std::vector < size_t > originalInstanceIndex;
221 std::string newInstName = FlatteningHelpers::instance()->getInstanceNamingFunction()(
222 inInstance->getName(), instance, originalInstanceIndex);
224 if(!indices.empty()) {
225 targetInst = targetInst->get(indices);
232 if(nestedNames.size() > 1) {
238 targetPortRef = targetPortRef->get(portRefIndices);
240 log(
"\tConnecting %s to net %s\n", targetPortRef->getName().c_str(),
241 outTargetNet->getName().c_str());
243 targetPortRef->connect(outTargetNet);
255 replicatePortRefConnections(inScalarNet.getSharedThis(), mTargetNet, mCurrentView,
265 replicatePortRefConnections(inVectorNet.getSharedThis(), mTargetNet, mCurrentView,
268 std::vector<NetSharedPtr> children;
269 inVectorNet.getCreatedChildren(children);
270 for(std::vector<NetSharedPtr>::iterator it = children.begin(); it != children.end(); ++it) {
272 PortRefConnectionReplicator()(childNet,
274 mCurrentView, mInstName);
285 replicatePortRefConnections(inVectorNetBit.getSharedThis(), mTargetNet, mCurrentView,
295 replicatePortRefConnections(inNetBundle.getSharedThis(), mTargetNet, mCurrentView,
298 std::vector< NetSharedPtr > children;
299 inNetBundle.getChildren(children);
300 std::vector< NetSharedPtr > targetChildren;
301 mTargetNet->getChildren(targetChildren);
302 std::vector< NetSharedPtr >::iterator target
303 = targetChildren.begin();
304 for( std::vector< NetSharedPtr >::iterator it
305 = children.begin(); it != children.end();
308 PortRefConnectionReplicator()( *it, *target,
309 mCurrentView, mInstName );
322 mTargetNet = outTargetNet;
323 mCurrentView = inCurrentView;
324 mOriginalInstance = inOriginalInstance;
325 inOrigNet->accept(*
this);
332 ~PortRefConnectionReplicator() throw () {
347 std::vector<PortSharedPtr> ports;
348 inOrigNet->getConnectedPorts(ports,
true);
352 targetNet = inTargetNet;
355 std::vector < size_t > indices;
360 FlatteningHelpers::instance()->getNetNamingFunction()(inInstName, inOrigNet,
363 FlatteningHelpers::instance()->getNetRenamingFunction()(inInstance, inOrigNet,
365 clonedNet->setOriginalName(renamedName);
366 inCurrentView->addNet(clonedNet);
367 outClonedNet = clonedNet;
368 targetNet = clonedNet;
370 PortRefConnectionReplicator()(inOrigNet, targetNet, inCurrentView, inInstance);
372 for(std::vector<PortSharedPtr>::iterator port = ports.begin(); port != ports.end();
374 std::vector < std::string > nestedNames;
379 nestedNames.push_back(portRef->getName());
381 portRef = portRef->getParentCollection();
385 if(nestedNames.size() > 1) {
389 origPortRef = origPortRef->get(
392 std::vector<NetSharedPtr> nets;
393 origPortRef->getConnectedNets(nets);
394 for(std::vector<NetSharedPtr>::iterator myNet = nets.begin(); myNet != nets.end();
397 PortRefConnectionReplicator()(inOrigNet, connectedNet, inCurrentView,
399 origPortRef->disconnect(connectedNet);
415 replicateNetConnections(inScalarNet.getSharedThis(), mCurrentView, mInstance, mFactory,
416 mInstName, mClonedNet, mTargetNet);
424 std::vector<NetSharedPtr> children;
425 inVectorNet.getCreatedChildren(children);
427 for(std::vector<NetSharedPtr>::iterator it = children.begin(); it != children.end();
429 NetConnectionReplicator()(*it, mCurrentView, mInstance, mFactory, mClonedNet,
430 targetVector, mInstName);
432 replicateNetConnections(inVectorNet.getSharedThis(), mCurrentView, mInstance, mFactory,
433 mInstName, mClonedNet, mTargetNet);
442 replicateNetConnections(inVectorNetBit.getSharedThis(), mCurrentView, mInstance, mFactory,
443 mInstName, mClonedNet, mTargetNet->get(inVectorNetBit.getIndices()));
446 std::vector<PortSharedPtr> ports;
447 inVectorNetBit.getConnectedPorts(ports);
452 mFactory->create(newScalarNet);
460 std::string newName = FlatteningHelpers::instance()->getNetNamingFunction()(mInstName,
462 Renamable::Name renamedName = FlatteningHelpers::instance()->getNetRenamingFunction()(
463 mInstance, origNet, indices);
464 newScalarNet->setName(newName);
465 newScalarNet->setOriginalName(renamedName);
466 mCurrentView->addNet(newScalarNet);
467 PortRefConnectionReplicator()(origNet, newScalarNet, mCurrentView, mInstance);
469 for(std::vector<PortSharedPtr>::iterator port = ports.begin(); port != ports.end();
471 std::vector < std::string > nestedNames;
476 nestedNames.push_back(portRef->getName());
478 portRef = portRef->getParentCollection();
482 if(nestedNames.size() > 1) {
492 std::vector<NetSharedPtr> nets;
493 origPortRef->getConnectedNets(nets);
494 for(std::vector<NetSharedPtr>::iterator myNet = nets.begin(); myNet != nets.end();
496 std::vector<PortSharedPtr> cPorts;
497 (*myNet)->getConnectedPorts(cPorts);
498 for(std::vector<PortSharedPtr>::iterator it = cPorts.begin();
499 it != cPorts.end(); ++it) {
500 (*it)->connect(newScalarNet);
502 std::vector<PortReferenceSharedPtr> cPortReferences;
503 (*myNet)->getConnectedPortRefs(cPortReferences);
504 for(std::vector<PortReferenceSharedPtr>::iterator it = cPortReferences.begin();
505 it != cPortReferences.end(); ++it) {
506 if(*it == origPortRef)
508 (*it)->connect(newScalarNet);
514 std::vector < size_t > portIdx =
IndexFinder<PortReference,
528 replicateNetConnections(inNetBundle.getSharedThis(), mCurrentView, mInstance, mFactory,
529 mInstName, mClonedNet, mTargetNet);
530 std::vector<NetSharedPtr> children;
531 inNetBundle.getChildren(children);
532 std::vector<NetSharedPtr> tChildren;
533 mClonedNet->getChildren(tChildren);
534 std::vector<NetSharedPtr>::iterator tNet = tChildren.begin();
535 for(std::vector<NetSharedPtr>::iterator it = children.begin(); it != children.end();
537 NetConnectionReplicator()(*it, mCurrentView, mInstance, mFactory, mClonedNet, *tNet,
551 mCurrentView = inCurrentView;
552 mInstance = inInstance;
553 mFactory = inFactory;
554 mInstName = (inInstName.empty()) ? inInstance->getName() : inInstName;
555 mTargetNet = inTargetNet;
556 inOrigNet->accept(*
this);
557 outClonedNet = mClonedNet;
564 ~NetConnectionReplicator() throw () {
582 log(
"Flattening instance with name %s\n", name.c_str());
584 log(
"Copying instantiations... ");
585 std::vector<InstanceSharedPtr> childInstances;
586 masterView->getInstances(childInstances);
587 if(childInstances.empty()) {
588 log(
"Leaf node.. cannot flatten\n");
591 std::vector<PortReferenceSharedPtr> portRefs;
592 inInstance->getPortReferences(portRefs);
594 std::vector < size_t > indices;
599 for(std::vector<InstanceSharedPtr>::iterator it = childInstances.begin();
600 it != childInstances.end(); ++it) {
603 instName = FlatteningHelpers::instance()->getInstanceNamingFunction()(name, inst, indices);
604 Renamable::Name renamedName = FlatteningHelpers::instance()->getInstanceRenamingFunction()(
605 inInstance, inst, indices);
606 log(
"Added Instance Name :: %s\n", instName.c_str());
607 inst->setName(instName);
608 inst->setOriginalName(renamedName);
609 inParentView->addInstance(inst);
610 outAddedInstances.push_back(inst);
619 std::string name = (!inName.empty()) ? inName : inInstance->getName();
621 std::vector<InstanceSharedPtr> childInstances;
622 masterView->getInstances(childInstances);
623 std::vector<PortReferenceSharedPtr> portRefs;
624 inInstance->getPortReferences(portRefs);
628 log(
"Copying internal nets... ");
629 std::vector<NetSharedPtr> allNets;
630 masterView->getNets(allNets);
631 for(std::vector<NetSharedPtr>::iterator it = allNets.begin(); it != allNets.end(); ++it) {
635 NetConnectionReplicator()(origNet, inParentView, inInstance, inFactory, clonedNet, dummy);
641 std::list<InstanceSharedPtr>& outAddedInstances)
throw (Error) {
647 if(!parentView || !parentView->findInstance(inInstance->getName())) {
651 switch(inInstance->getCompositionType()) {
654 bool added = addFlattenInstances(parentView, inInstance, inFactory, outAddedInstances);
656 flattenInstance(parentView, inInstance, inFactory);
657 parentView->removeInstance(inInstance->getName());
670 std::vector<InstanceSharedPtr> children;
671 inInstance->getChildren(children);
673 for(std::vector<InstanceSharedPtr>::iterator it = children.begin();
674 it != children.end(); ++it) {
675 added = addFlattenInstances(parentView, (*it), inFactory, outAddedInstances);
681 for(std::vector<InstanceSharedPtr>::iterator it = children.begin();
682 it != children.end(); ++it) {
683 flattenInstance(parentView, (*it), inFactory);
685 parentView->removeInstance(inInstance->getName());
708 const _Pointer& inNameable,
const std::vector<size_t>& inIndicesVector =
709 std::vector<size_t>()) {
710 return (*NameModifier<_Pointer>::instance())(inInstName, inNameable, inIndicesVector);
714 const _Pointer& inRenamable,
const std::vector<size_t>& inIndicesVector =
715 std::vector<size_t>()) {
716 std::string sep = FlatteningHelpers::instance()->getSeparator();
718 if(originalName.empty()) {
719 originalName = inRenamable->getName();
721 std::string parentInstanceName = inParentInstance->getOriginalName();
722 if(parentInstanceName.empty()) {
723 parentInstanceName = inParentInstance->getName();
725 if(!inIndicesVector.empty()) {
726 parentInstanceName +=
"[ ";
727 std::ostringstream sout;
728 copy(inIndicesVector.begin(), inIndicesVector.end(),
729 std::ostream_iterator < size_t > (sout,
" "));
730 parentInstanceName += sout.str();
731 parentInstanceName +=
"]";
733 std::ostringstream sout;
734 sout << ((parentInstanceName.find_first_of(sep)) ? sep :
"") << parentInstanceName << sep
744 FlatteningHelpers *pHelper = FlatteningHelpers::instance();
745 pHelper->setSeparator(inSeparator);
746 pHelper->setInstanceRenamingFunction(inInstanceRenameFunc);
747 pHelper->setNetRenamingFunction(inNetRenameFunc);
748 pHelper->setInstanceNamingFunction(inInstanceNameFunc);
749 pHelper->setNetNamingFunction(inNetNameFunc);
751 std::list<InstanceSharedPtr> newInstances;
752 if(!flatten_impl(inInstance, inFactory, newInstances)) {
755 while(!newInstances.empty()) {
756 std::list<InstanceSharedPtr>::iterator top = newInstances.begin();
758 newInstances.erase(top);
759 flatten_impl(inst, inFactory, newInstances);
762 std::list < InstanceSharedPtr > dummy;
763 flatten_impl(inInstance, inFactory, dummy);
773 if(!inView || !inFactory) {
776 std::vector<InstanceSharedPtr> outInstances;
777 inView->getInstances(outInstances);
778 std::vector<InstanceSharedPtr>::iterator instIt = outInstances.begin();
779 for(; instIt != outInstances.end(); instIt++) {
782 flatten(inst, inFactory, inRecursive, inSeparator, inInstanceRenameFunc,
783 inNetRenameFunc, inInstanceNameFunc, inNetNameFunc);
794 if(!inDesign || !inFactory) {
798 std::string libName = inDesign->getLibraryRefName();
818 std::vector<ViewSharedPtr> outViews;
819 cell->getViews(outViews);
820 std::vector<ViewSharedPtr>::iterator viewIt = outViews.begin();
821 for(; viewIt != outViews.end(); viewIt++) {
826 flatten(view, inFactory, inRecursive, inSeparator, inInstanceRenameFunc, inNetRenameFunc,
827 inInstanceNameFunc, inNetNameFunc);
832 const std::vector<size_t>& inIndicesVector);
835 const InstanceSharedPtr& inNameable,
const std::vector<size_t>& inIndicesVector);
838 const NetSharedPtr& inRenamable,
const std::vector<size_t>& inIndicesVector);
841 const InstanceSharedPtr& inRenamable,
const std::vector<size_t>& inIndicesVector);
An acyclic inoutVisitor implementation.
void log(const char *fmt,...)
Represents a bit of a net array.
boost::shared_ptr< Instance > InstanceSharedPtr
void findLeafConnectable(std::vector< std::string > &nestedNames, boost::shared_ptr< _Connectable > &conn)
Represents the usable instance of a port of a cell in another cell.
boost::shared_ptr< ScalarNet > ScalarNetSharedPtr
std::string getModifiedName(const std::string &inInstName, const _Pointer &inNameable, const std::vector< size_t > &inIndicesVector=std::vector< size_t >())
boost::shared_ptr< ObjectFactory > ObjectFactorySharedPtr
boost::shared_ptr< Design > DesignSharedPtr
boost::shared_ptr< _Tp > clone(const boost::shared_ptr< _Tp > &inPointer, const ObjectFactorySharedPtr &inFactory)
The Error object thrown by different methods of EdifOM.
boost::shared_ptr< Net > NetSharedPtr
boost::shared_ptr< Library > LibrarySharedPtr
boost::shared_ptr< PortReference > PortReferenceSharedPtr
Contains functions for flattening a design.
Represents a standalone net.
void saveContextData(const std::string &inName, const boost::any &inSource)
boost::function< std::string(const InstanceSharedPtr &inParentInstance, const NetSharedPtr &inNet, const std::vector< size_t > &inIndicesVector)> NetRenamingFunction
Function to create names of nets that are set in rename constructs.
boost::function< std::string(const InstanceSharedPtr &inParentInstance, const InstanceSharedPtr &inInstance, const std::vector< size_t > &inIndicesVector)> InstanceRenamingFunction
Function to create names of nets that are set in rename constructs.
Represents a reference to a bit of a port.
std::string getRenamedName(const InstanceSharedPtr &inParentInstance, const _Pointer &inRenamable, const std::vector< size_t > &inIndicesVector=std::vector< size_t >())
Represents a bundle of nets.
boost::shared_ptr< View > ViewSharedPtr
boost::shared_ptr< Cell > CellSharedPtr
boost::shared_ptr< Port > PortSharedPtr
boost::function< std::string(const std::string &inParentInstanceName, const NetSharedPtr &inNet, const std::vector< size_t > &inIndicesVector)> NetNamingFunction
Function to create net names during flattening.
void flatten(const InstanceSharedPtr &inInstance, const ObjectFactorySharedPtr &inFactory, bool inRecursive, const std::string &inSeparator, const InstanceRenamingFunction &inInstanceRenameFunc, const NetRenamingFunction &inNetRenameFunc, const InstanceNamingFunction &inInstanceNameFunc, const NetNamingFunction &inNetNameFunc)
boost::function< std::string(const std::string &inParentInstanceName, const InstanceSharedPtr &inInstance, const std::vector< size_t > &inIndicesVector)> InstanceNamingFunction
Function to create instance names during flattening.
boost::shared_ptr< Root > RootSharedPtr
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)