torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VerilogImporter.cpp
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 Source for the VerilogImporter class.
18 
21 #include "torc/externals/verilator/src/V3Ast.h"
22 #include "torc/externals/verilator/src/V3File.h"
23 #include "torc/externals/verilator/src/V3Graph.h"
24 #include "torc/externals/verilator/src/V3LinkCells.h"
25 #include "torc/externals/verilator/src/V3Parse.h"
26 #include "torc/externals/verilator/src/V3PreShell.h"
27 #include <fstream>
28 #include <sstream>
29 #include <boost/foreach.hpp>
30 #include <boost/tokenizer.hpp>
31 
32 // debug
33 #include "torc/Generic.hpp"
34 
35 /// \brief Global variables for Verilator.
36 /// \note The name v3Global is expected by Verilator code and therefore cannot be changed.
37 V3Global v3Global;
38 
39 /// \brief Create a netlist for Verilator to use.
40 AstNetlist* V3Global::makeNetlist() {
41  AstNetlist* newp = new AstNetlist();
42  return newp;
43 }
44 
45 /// \brief Check the AST for consistency.
46 void V3Global::checkTree() {
47  rootp()->checkTree();
48 }
49 
50 /// \brief Delete the AST tree.
51 void V3Global::clear() {
52  if (m_rootp) m_rootp->deleteTree(); m_rootp=NULL;
53 }
54 
55 /// \brief Read all input files and libraries.
56 void V3Global::readFiles() {
57  // set up the parser
58  V3InFilter filter(v3Global.opt.pipeFilter());
59  V3Parse parser(v3Global.rootp(), &filter);
60 
61  // read the top module
62  for(V3StringList::const_iterator it = v3Global.opt.vFiles().begin();
63  it != v3Global.opt.vFiles().end(); ++it) {
64  string filename = *it;
65  parser.parseFile(new FileLine("COMMAND_LINE",0), filename, false,
66  "Cannot find file containing module: ");
67  }
68 
69  // read any libraries
70  // "To be compatible with other simulators, this needs to be done after the top file is read"
71  for(V3StringSet::const_iterator it = v3Global.opt.libraryFiles().begin();
72  it != v3Global.opt.libraryFiles().end(); ++it) {
73  string filename = *it;
74  parser.parseFile(new FileLine("COMMAND_LINE",0), filename, true,
75  "Cannot find file containing library module: ");
76  }
77  // dump the AST parse tree if the user has requested it with --dump-tree
78  v3Global.rootp()->dumpTreeFile(v3Global.debugFilename("parse.tree"));
79  // fail on errors
80  V3Error::abortIfErrors();
81 
82  // link instances to modules
83  if(!v3Global.opt.preprocOnly()) V3LinkCells::link(v3Global.rootp(), &filter);
84 }
85 
86 namespace torc {
87 namespace generic {
88 
89  using namespace std;
90  using namespace torc;
91 
93  // assume success unless we encounter an error
94  mSuccess = true;
95  }
96 
98  const std::string& inArguments) {
99 
100  int argc = 0;
101  char* env[] = {
102  0
103  };
104  std::string arguments =
105  "./VerilogImporter "
106  + inFilename.string()
107  + " --cc "
108  + "-Itorc/externals/verilator/include "
109  // + "--dump-tree "
110  ;
111  // combine the local arguments and the user supplied arguments
112  std::string input = arguments + " " + inArguments;
113  // tokenize just so we can count arguments (so much uglier than it should be ...)
114  boost::char_separator<char> sep(" ");
115  boost::tokenizer< boost::char_separator<char> > tokens(input, sep);
116  typedef boost::token_iterator_generator< boost::char_separator<char> >::type Iterator;
117  Iterator p = boost::make_token_iterator<string>(input.begin(), input.end(), sep);
118  Iterator e = boost::make_token_iterator<string>(input.end(), input.end(), sep);
119  while(p != e) { *p++; argc++; }
120  // declare an array of sufficient size
121  char* argv[argc];
122  // put all of the arguments into the array
123  argc = 0;
124  BOOST_FOREACH(const string& t, tokens) { argv[argc++] = strdup(t.c_str()); }
125  /// \todo Release the arguments in argv after use with free()
126 
127  // General initialization
128  ios::sync_with_stdio();
129 
130  // enable the following if we ever allow command line -D definitions
131  V3PreShell::boot(env);
132 
133  // parse command line options
134  v3Global.opt.bin(argv[0]);
135  string argString = V3Options::argString(argc-1, argv+1);
136  v3Global.opt.parseOpts(new FileLine("COMMAND_LINE",0), argc-1, argv+1);
137  if(!v3Global.opt.outFormatOk() && !v3Global.opt.preprocOnly() && !v3Global.opt.lintOnly()
138  && !v3Global.opt.cdc()) {
139  v3fatal("verilator: Need --cc, --sc, --sp, --cdc, --lint-only or --E option");
140  }
141  // release memory allocated for the command line arguments
142  while(argc) { free(argv[--argc]); }
143 
144  // extract environment variables
145  V3Options::getenvSYSTEMC();
146  V3Options::getenvSYSTEMC_ARCH();
147  V3Options::getenvSYSTEMC_INCLUDE();
148  V3Options::getenvSYSTEMC_LIBDIR();
149  V3Options::getenvSYSTEMPERL();
150  V3Options::getenvSYSTEMPERL_INCLUDE();
151 
152  V3Error::abortIfErrors();
153 
154  V3File::addSrcDepend(v3Global.opt.bin());
155 
156  // Internal tests (after option parsing as need debug() setting)
157  V3Graph::test();
158 
159  // Read first filename
160  v3Global.readFiles();
161 
162  // restore C++ and C io to their default unsyncronized state
163  ios::sync_with_stdio(false);
164 
165  // create a Generic object factory and root
167  mRootPtr = objectFactoryPtr->newRootPtr("WORK");
168 
169  // visit the abstract syntax tree and extract the generic netlist representation
170  torc::generic::VerilogImporterVisitor verilogImporterVisitor(objectFactoryPtr, mRootPtr);
171  v3Global.rootp()->accept(verilogImporterVisitor);
172 
173  // export the EDIF design
174  string outFileName = "output.edf";
175  fstream edifExport(outFileName.c_str(), ios_base::out);
176  torc::generic::EdifExporter exporter(edifExport);
177  exporter(mRootPtr);
178 
179  return mSuccess;
180  }
181 
182 } // namespace generic
183 } // namespace torc
Main torc::generic namespace header.
VerilogImporter(void)
Construct the parser importer context.
bool operator()(const boost::filesystem::path &inFilename, const std::string &inArguments=std::string())
Import Verilog from a file.
Header for the VerilogImporter class.
boost::shared_ptr< ObjectFactory > ObjectFactorySharedPtr
std::string string
Header for the VerilogImporterVisitor class.
boost::filesystem::path path
AST visitor to convert structural Verilog into a generic design.
V3Global v3Global
Global variables for Verilator.