torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VerilogImporterVisitor.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Header for the VerilogImporterVisitor class.
18 
19 #ifndef TORC_GENERIC_VERILOG_VERILOGIMPORTERVISITOR_HPP
20 #define TORC_GENERIC_VERILOG_VERILOGIMPORTERVISITOR_HPP
21 
22 #include "torc/generic/Root.hpp"
24 #include "torc/generic/Net.hpp"
26 #include "torc/externals/verilator/src/V3Global.h"
27 #include "torc/externals/verilator/src/V3Ast.h"
28 #include <boost/smart_ptr.hpp>
29 #include <boost/filesystem.hpp>
30 #include <string>
31 #include <vector>
32 
33 namespace torc {
34 namespace generic {
35 
36 namespace generic { class VerilogImporterUnitTest; }
37 
38  /// \brief AST visitor to convert structural Verilog into a generic design.
39  class VerilogImporterVisitor : public AstNVisitor {
40  protected:
41  // friends
42  /// \brief The unit test class has access to our internals.
44  // typedefs
45  /// \brief Imported type name.
47  /// \brief A map from string to view shared pointer.
48  typedef map<string, ViewSharedPtr> StringToViewMap;
49  /// \brief A map from string to net shared pointer.
50  typedef map<string, NetSharedPtr> StringToNetMap;
51  /// \brief A map of cell shared pointers to booleans.
52  typedef map<CellSharedPtr, bool> CellToBoolMap;
53  /// \brief A vector of net shared pointers.
54  typedef vector<NetSharedPtr> NetSharedPtrVector;
55  /// \brief A vector of element indices.
56  typedef vector<size_t> IndexVector;
57  // constants
58  /// \brief Undefined index constant.
59  static const int32_t cUndefined = INT_MIN; // numeric_limits<boost::int32_t>::min();
60  // members
61  /// \brief The object factory shared pointer.
63  /// \brief The root shared pointer.
65  /// \brief The inferred black-box library shared pointer.
67  /// \brief The cell library shared pointer.
69  /// \brief The current library shared pointer.
71  /// \brief The current cell shared pointer.
73  /// \brief The current view shared pointer.
75  /// \brief The current instance shared pointer.
77  /// \brief The current net shared pointer;
79  /// \brief The current net vector.
81  /// \brief The current constant string.
83  /// \brief The current constant signed integer.
85  /// \brief The current constant number object.
86  V3Number mCurrentConstNum;
87  /// \brief The current text string.
88  string mCurrentText;
89  /// \brief The current inversion flag.
91  /// \brief The current concatenation flag.
93  /// \brief The current range.
94  int32_t mCurrentRange[2];
95  /// \brief The current array indices.
97  /// \brief A map from module name to master view pointer.
99  /// \brief A unique index to avoid name collisions.
100  uint32_t mCurrentIndex;
101  /// \brief A map of net bit names to net shared pointers.
103  /// \brief A map of cell shared pointers to instantiation settings.
105  /// \brief A flag to indicate whether we should currently allow library cells
107  // statics
108  static FileLine sNullFileLine;
109  // functions
110  virtual void visit(AstNetlist* nodePtr, AstNUser* userPtr);
111  virtual void visit(AstModule* nodePtr, AstNUser* userPtr);
112  virtual void visit(AstCell* nodePtr, AstNUser* userPtr);
113  virtual void visit(AstPin* nodePtr, AstNUser* userPtr);
114  virtual void visit(AstPort* nodePtr, AstNUser* userPtr);
115  virtual void visit(AstSelBit* nodePtr, AstNUser* userPtr);
116  virtual void visit(AstSelExtract* nodePtr, AstNUser* userPtr);
117  virtual void visit(AstRange* nodePtr, AstNUser* userPtr);
118  virtual void visit(AstDefParam* nodePtr, AstNUser* userPtr);
119  virtual void visit(AstVar* nodePtr, AstNUser* userPtr);
120  virtual void visit(AstAssignW* nodePtr, AstNUser* userPtr);
121  virtual void visit(AstParseRef* nodePtr, AstNUser* userPtr);
122  virtual void visit(AstText* nodePtr, AstNUser* userPtr);
123  virtual void visit(AstConst* nodePtr, AstNUser* userPtr);
124  virtual void visit(AstNot* nodePtr, AstNUser* userPtr);
125  virtual void visit(AstNotFoundModule* nodePtr, AstNUser* userPtr);
126  virtual void visit(AstBasicDType* nodePtr, AstNUser* userPtr);
127  virtual void visit(AstConcat* nodePtr, AstNUser* userPtr);
128  virtual void visit(AstNode* nodePtr, AstNUser* userPtr);
129  /// \brief Find the specified view, or optionally create it.
130  ViewSharedPtr findMasterView(std::string inMasterName, std::string inOriginalName,
131  bool inCreate) {
132  // keep track of what we have or haven't found
133  ViewSharedPtr masterViewPtr;
134  // search our map for the module of interest
135  StringToViewMap::iterator e = mMasterNameToView.end();
136  StringToViewMap::iterator p = mMasterNameToView.find(inMasterName);
137  if(p != e) masterViewPtr = p->second;
138  // if we didn't find a master, and the caller requested it, create it now
139  if(!masterViewPtr && inCreate) {
140  // create and add the inferred cell
141  CellSharedPtr cellPtr = mObjectFactoryPtr->newCellPtr(inMasterName,
143  if(inMasterName != inOriginalName && !inOriginalName.empty())
144  cellPtr->setOriginalName(inOriginalName);
145  // create and add the inferred view
146  masterViewPtr = mObjectFactoryPtr->newViewPtr(
148  // map the master name to this view
149  mMasterNameToView[inMasterName] = masterViewPtr;
150  }
151  // return the view that we found or created
152  return masterViewPtr;
153  }
154  /// \brief Create top-level designs for all Verilog modules that are never instantiated.
155  void createDesigns(void);
156  public:
157  // constructors
158  /// \brief Public constructor.
159  /// \param inRootPtr A torc::generic Root shared pointer to populate.
161  : mObjectFactoryPtr(inObjectFactoryPtr), mRootPtr(inRootPtr),
163  // initialize what remains
165  mCurrentNotFlag = false;
166  mCurrentConcatFlag = false;
168  mCurrentIndex = 1000000;
169  // create a new library for inferred black boxes
172  // create a new library for imported library cells
175  // create a new library for use
176  mCurrentLibraryPtr = mObjectFactoryPtr->newLibraryPtr(
178  }
179  };
180 
181 } // namespace generic
182 } // namespace torc
183 
184 #endif // TORC_GENERIC_VERILOG_VERILOGIMPORTERVISITOR_HPP
V3Number mCurrentConstNum
The current constant number object.
NetSharedPtr mCurrentNetPtr
The current net shared pointer;.
static string getImportedVerilogLibraryName(void)
Returns the imported Verilog library name.
boost::shared_ptr< Instance > InstanceSharedPtr
static string getImportedCellLibraryName(void)
Returns the imported cell library name.
InstanceSharedPtr mCurrentInstancePtr
The current instance shared pointer.
ViewSharedPtr findMasterView(std::string inMasterName, std::string inOriginalName, bool inCreate)
Find the specified view, or optionally create it.
VerilogImporterVisitor(ObjectFactorySharedPtr inObjectFactoryPtr, RootSharedPtr inRootPtr)
Public constructor.
map< string, NetSharedPtr > StringToNetMap
A map from string to net shared pointer.
bool mCurrentNotFlag
The current inversion flag.
LibrarySharedPtr mCurrentLibraryPtr
The current library shared pointer.
ViewSharedPtr mCurrentViewPtr
The current view shared pointer.
std::string string
Imported type name.
void createDesigns(void)
Create top-level designs for all Verilog modules that are never instantiated.
static const int32_t cUndefined
Undefined index constant.
boost::shared_ptr< ObjectFactory > ObjectFactorySharedPtr
NetSharedPtrVector mCurrentNetPtrVector
The current net vector.
std::string string
static FileLine sNullFileLine
Initial file line information.
string mCurrentConstStr
The current constant string.
bool mImportLibraryCells
A flag to indicate whether we should currently allow library cells.
boost::shared_ptr< Net > NetSharedPtr
boost::shared_ptr< Library > LibrarySharedPtr
bool mCurrentConcatFlag
The current concatenation flag.
int32_t mCurrentRange[2]
The current range.
CellToBoolMap mCellInstantiationFlag
A map of cell shared pointers to instantiation settings.
friend class torc::generic::generic::VerilogImporterUnitTest
The unit test class has access to our internals.
LibrarySharedPtr mInferredBlackBoxesLibraryPtr
The inferred black-box library shared pointer.
RootSharedPtr mRootPtr
The root shared pointer.
StringToNetMap mVectorBitNameToNet
A map of net bit names to net shared pointers.
IndexVector mCurrentIndices
The current array indices.
string mCurrentText
The current text string.
map< string, ViewSharedPtr > StringToViewMap
A map from string to view shared pointer.
int32_t mCurrentConstInt
The current constant signed integer.
map< CellSharedPtr, bool > CellToBoolMap
A map of cell shared pointers to booleans.
AST visitor to convert structural Verilog into a generic design.
virtual void visit(AstNetlist *nodePtr, AstNUser *userPtr)
Visit the top-level netlist.
uint32_t mCurrentIndex
A unique index to avoid name collisions.
vector< size_t > IndexVector
A vector of element indices.
LibrarySharedPtr mImportedCellLibraryPtr
The cell library shared pointer.
boost::shared_ptr< View > ViewSharedPtr
boost::shared_ptr< Cell > CellSharedPtr
Header for the VerilogNames class.
ObjectFactorySharedPtr mObjectFactoryPtr
The object factory shared pointer.
StringToViewMap mMasterNameToView
A map from module name to master view pointer.
static string getImportedVerilogViewName(void)
Returns the imported Verilog view name.
vector< NetSharedPtr > NetSharedPtrVector
A vector of net shared pointers.
CellSharedPtr mCurrentCellPtr
The current cell shared pointer.
boost::shared_ptr< Root > RootSharedPtr
static string getInferredBlackBoxesLibraryName(void)
Returns the inferred black box library name.