2012-04-13 01:08:20 +00:00
|
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Netlist (top level) functions
|
|
|
|
|
//
|
2008-04-25 12:14:27 +00:00
|
|
|
|
// Code available from: http://www.veripool.org/verilator
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2019-01-04 00:17:22 +00:00
|
|
|
|
// Copyright 2003-2019 by Wilson Snyder. This program is free software; you can
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// redistribute it and/or modify it under the terms of either the GNU
|
2009-05-04 21:07:57 +00:00
|
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
|
// Version 2.0.
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
|
|
|
|
// Verilator is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
2009-10-31 14:08:38 +00:00
|
|
|
|
// Overview of files involved in parsing
|
2019-05-19 20:13:13 +00:00
|
|
|
|
// V3Parse.h External consumer interface to V3ParseImp
|
|
|
|
|
// V3ParseImp Internals to parser, common to across flex & bison
|
|
|
|
|
// V3ParseGrammar Wrapper that includes V3ParseBison
|
|
|
|
|
// V3ParseBison Bison output
|
|
|
|
|
// V3ParseLex Wrapper that includes lex output
|
|
|
|
|
// V3Lexer.yy.cpp Flex output
|
2009-10-31 14:08:38 +00:00
|
|
|
|
//*************************************************************************
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
2006-12-18 19:20:45 +00:00
|
|
|
|
#include "config_build.h"
|
|
|
|
|
#include "verilatedos.h"
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
|
#include "V3Error.h"
|
|
|
|
|
#include "V3Global.h"
|
2015-02-27 01:40:45 +00:00
|
|
|
|
#include "V3Os.h"
|
2006-08-26 11:35:28 +00:00
|
|
|
|
#include "V3Ast.h"
|
|
|
|
|
#include "V3File.h"
|
2009-10-31 14:08:38 +00:00
|
|
|
|
#include "V3ParseImp.h"
|
2006-08-26 11:35:28 +00:00
|
|
|
|
#include "V3PreShell.h"
|
|
|
|
|
|
2018-10-14 17:43:24 +00:00
|
|
|
|
#include <cstdarg>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//======================================================================
|
|
|
|
|
// Globals
|
|
|
|
|
|
2019-05-19 20:13:13 +00:00
|
|
|
|
V3ParseImp* V3ParseImp::s_parsep = NULL;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
2019-05-19 20:13:13 +00:00
|
|
|
|
int V3ParseSym::s_anonNum = 0;
|
2009-10-31 14:14:04 +00:00
|
|
|
|
|
2019-06-12 01:03:03 +00:00
|
|
|
|
extern void yyerror(const char*);
|
|
|
|
|
extern void yyerrorf(const char* format, ...);
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//######################################################################
|
2019-06-12 01:03:03 +00:00
|
|
|
|
// Parser constructor
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
2009-10-31 14:08:38 +00:00
|
|
|
|
V3ParseImp::~V3ParseImp() {
|
2018-02-02 02:24:41 +00:00
|
|
|
|
for (std::deque<string*>::iterator it = m_stringps.begin(); it != m_stringps.end(); ++it) {
|
2019-05-19 20:13:13 +00:00
|
|
|
|
delete (*it);
|
2007-11-02 11:23:03 +00:00
|
|
|
|
}
|
|
|
|
|
m_stringps.clear();
|
2018-02-02 02:24:41 +00:00
|
|
|
|
for (std::deque<V3Number*>::iterator it = m_numberps.begin(); it != m_numberps.end(); ++it) {
|
2019-05-19 20:13:13 +00:00
|
|
|
|
delete (*it);
|
2007-11-02 11:23:03 +00:00
|
|
|
|
}
|
|
|
|
|
m_numberps.clear();
|
2009-10-31 14:08:38 +00:00
|
|
|
|
lexDestroy();
|
2007-11-02 11:23:03 +00:00
|
|
|
|
parserClear();
|
2009-10-31 14:14:04 +00:00
|
|
|
|
|
|
|
|
|
if (debug()>=9) { UINFO(0,"~V3ParseImp\n"); symp()->dump(cout, "-vpi: "); }
|
2007-11-02 11:23:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-12 01:03:03 +00:00
|
|
|
|
//######################################################################
|
|
|
|
|
// Parser utility methods
|
|
|
|
|
|
|
|
|
|
void V3ParseImp::ppline(const char* textp) {
|
|
|
|
|
// Handle `line directive
|
|
|
|
|
int enterExit;
|
|
|
|
|
fileline()->lineDirective(textp, enterExit/*ref*/);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void V3ParseImp::verilatorCmtLintSave() {
|
|
|
|
|
m_lintState.push_back(*parsep()->fileline());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void V3ParseImp::verilatorCmtLintRestore() {
|
|
|
|
|
if (m_lintState.empty()) {
|
|
|
|
|
yyerrorf("/*verilator lint_restore*/ without matching save.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
parsep()->fileline()->warnStateFrom(m_lintState.back());
|
|
|
|
|
m_lintState.pop_back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void V3ParseImp::verilatorCmtLint(const char* textp, bool warnOff) {
|
|
|
|
|
const char* sp = textp;
|
|
|
|
|
while (*sp && !isspace(*sp)) sp++;
|
|
|
|
|
while (*sp && isspace(*sp)) sp++;
|
|
|
|
|
while (*sp && !isspace(*sp)) sp++;
|
|
|
|
|
while (*sp && isspace(*sp)) sp++;
|
|
|
|
|
string msg = sp;
|
|
|
|
|
string::size_type pos;
|
|
|
|
|
if ((pos = msg.find('*')) != string::npos) { msg.erase(pos); }
|
|
|
|
|
if (!(parsep()->fileline()->warnOff(msg, warnOff))) {
|
|
|
|
|
if (!parsep()->optFuture(msg)) {
|
|
|
|
|
yyerrorf("Unknown verilator lint message code: %s, in %s", msg.c_str(), textp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void V3ParseImp::verilatorCmtBad(const char* textp) {
|
|
|
|
|
string cmtparse = textp;
|
|
|
|
|
if (cmtparse.substr(0, strlen("/*verilator")) == "/*verilator") {
|
|
|
|
|
cmtparse.replace(0, strlen("/*verilator"), "");
|
|
|
|
|
}
|
|
|
|
|
while (isspace(cmtparse[0])) {
|
|
|
|
|
cmtparse.replace(0, 1, "");
|
|
|
|
|
}
|
|
|
|
|
string cmtname;
|
|
|
|
|
for (int i = 0; isalnum(cmtparse[i]); i++) {
|
|
|
|
|
cmtname += cmtparse[i];
|
|
|
|
|
}
|
|
|
|
|
if (!parsep()->optFuture(cmtname)) {
|
|
|
|
|
yyerrorf("Unknown verilator comment: %s", textp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void V3ParseImp::tag(const char* text) {
|
|
|
|
|
if (m_tagNodep) {
|
|
|
|
|
string tmp = text + strlen("/*verilator tag ");
|
|
|
|
|
string::size_type pos;
|
|
|
|
|
if ((pos = tmp.rfind("*/")) != string::npos) { tmp.erase(pos); }
|
|
|
|
|
m_tagNodep->tag(tmp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double V3ParseImp::parseDouble(const char* textp, size_t length, bool* successp) {
|
|
|
|
|
char* strgp = new char[length+1];
|
|
|
|
|
char* dp = strgp;
|
|
|
|
|
if (successp) *successp = true;
|
|
|
|
|
for (const char* sp = textp; sp < (textp+length); ++sp) {
|
|
|
|
|
if (*sp != '_') *dp++ = *sp;
|
|
|
|
|
}
|
|
|
|
|
*dp++ = '\0';
|
|
|
|
|
char* endp = strgp;
|
|
|
|
|
double d = strtod(strgp, &endp);
|
|
|
|
|
size_t parsed_len = endp-strgp;
|
|
|
|
|
if (parsed_len != strlen(strgp)) {
|
|
|
|
|
if (successp) *successp = false;
|
|
|
|
|
else yyerrorf("Syntax error parsing real: %s", strgp);
|
|
|
|
|
}
|
|
|
|
|
delete[] strgp;
|
|
|
|
|
return d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Parser tokenization
|
|
|
|
|
|
2010-04-07 00:20:44 +00:00
|
|
|
|
size_t V3ParseImp::ppInputToLex(char* buf, size_t max_size) {
|
|
|
|
|
size_t got = 0;
|
2019-05-19 20:13:13 +00:00
|
|
|
|
while (got < max_size // Haven't got enough
|
|
|
|
|
&& !m_ppBuffers.empty()) { // And something buffered
|
|
|
|
|
string front = m_ppBuffers.front(); m_ppBuffers.pop_front();
|
|
|
|
|
size_t len = front.length();
|
|
|
|
|
if (len > (max_size-got)) { // Front string too big
|
|
|
|
|
string remainder = front.substr(max_size-got);
|
|
|
|
|
front = front.substr(0, max_size-got);
|
|
|
|
|
m_ppBuffers.push_front(remainder); // Put back remainder for next time
|
|
|
|
|
len = (max_size-got);
|
|
|
|
|
}
|
|
|
|
|
strncpy(buf+got, front.c_str(), len);
|
|
|
|
|
got += len;
|
2008-01-31 14:49:27 +00:00
|
|
|
|
}
|
|
|
|
|
if (debug()>=9) {
|
2019-05-19 20:13:13 +00:00
|
|
|
|
string out = string(buf, got);
|
|
|
|
|
cout<<" inputToLex got="<<got<<" '"<<out<<"'"<<endl;
|
2008-01-31 14:49:27 +00:00
|
|
|
|
}
|
|
|
|
|
// Note returns 0 at EOF
|
|
|
|
|
return got;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 23:45:06 +00:00
|
|
|
|
void V3ParseImp::preprocDumps(std::ostream& os) {
|
|
|
|
|
if (v3Global.opt.dumpDefines()) {
|
|
|
|
|
V3PreShell::dumpDefines(os);
|
|
|
|
|
} else {
|
|
|
|
|
bool noblanks = v3Global.opt.preprocOnly() && v3Global.opt.preprocNoLine();
|
|
|
|
|
for (std::deque<string>::iterator it = m_ppBuffers.begin(); it!=m_ppBuffers.end(); ++it) {
|
|
|
|
|
if (noblanks) {
|
|
|
|
|
bool blank = true;
|
|
|
|
|
for (string::iterator its = it->begin(); its != it->end(); ++its) {
|
2019-05-19 20:13:13 +00:00
|
|
|
|
if (!isspace(*its) && *its!='\n') { blank = false; break; }
|
2018-10-25 23:45:06 +00:00
|
|
|
|
}
|
|
|
|
|
if (blank) continue;
|
|
|
|
|
}
|
|
|
|
|
os << *it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-28 00:56:38 +00:00
|
|
|
|
void V3ParseImp::parseFile(FileLine* fileline, const string& modfilename, bool inLibrary,
|
2019-05-19 20:13:13 +00:00
|
|
|
|
const string& errmsg) { // "" for no error, make fake node
|
2015-02-27 01:40:45 +00:00
|
|
|
|
string modname = V3Os::filenameNonExt(modfilename);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
|
UINFO(2,__FUNCTION__<<": "<<modname<<(inLibrary?" [LIB]":"")<<endl);
|
|
|
|
|
m_fileline = new FileLine(fileline);
|
|
|
|
|
m_inLibrary = inLibrary;
|
|
|
|
|
|
2008-01-31 14:49:27 +00:00
|
|
|
|
// Preprocess into m_ppBuffer
|
2011-10-28 00:56:38 +00:00
|
|
|
|
bool ok = V3PreShell::preproc(fileline, modfilename, m_filterp, this, errmsg);
|
|
|
|
|
if (!ok) {
|
2019-05-19 20:13:13 +00:00
|
|
|
|
if (errmsg != "") return; // Threw error already
|
|
|
|
|
// Create fake node for later error reporting
|
|
|
|
|
AstNodeModule* nodep = new AstNotFoundModule(fileline, modname);
|
|
|
|
|
v3Global.rootp()->addModulep(nodep);
|
|
|
|
|
return;
|
2011-10-28 00:56:38 +00:00
|
|
|
|
}
|
2008-01-31 14:49:27 +00:00
|
|
|
|
|
|
|
|
|
if (v3Global.opt.preprocOnly() || v3Global.opt.keepTempFiles()) {
|
2019-05-19 20:13:13 +00:00
|
|
|
|
// Create output file with all the preprocessor output we buffered up
|
|
|
|
|
string vppfilename = v3Global.opt.makeDir()+"/"+v3Global.opt.prefix()+"_"+modname+".vpp";
|
2018-02-02 02:24:41 +00:00
|
|
|
|
std::ofstream* ofp = NULL;
|
|
|
|
|
std::ostream* osp;
|
2019-05-19 20:13:13 +00:00
|
|
|
|
if (v3Global.opt.preprocOnly()) {
|
|
|
|
|
osp = &cout;
|
|
|
|
|
} else {
|
|
|
|
|
osp = ofp = V3File::new_ofstream(vppfilename);
|
|
|
|
|
}
|
|
|
|
|
if (osp->fail()) {
|
|
|
|
|
fileline->v3error("Cannot write preprocessor output: "+vppfilename);
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
2018-10-25 23:45:06 +00:00
|
|
|
|
preprocDumps(*osp);
|
2019-05-19 20:13:13 +00:00
|
|
|
|
if (ofp) {
|
|
|
|
|
ofp->close();
|
|
|
|
|
delete ofp; VL_DANGLING(ofp);
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
2006-10-04 18:46:42 +00:00
|
|
|
|
|
2008-01-31 14:49:27 +00:00
|
|
|
|
// Parse it
|
|
|
|
|
if (!v3Global.opt.preprocOnly()) {
|
2018-08-25 13:52:45 +00:00
|
|
|
|
lexFile(modfilename);
|
2017-10-10 01:08:50 +00:00
|
|
|
|
} else {
|
2019-05-19 20:13:13 +00:00
|
|
|
|
m_ppBuffers.clear();
|
2006-10-04 18:46:42 +00:00
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-10-31 14:08:38 +00:00
|
|
|
|
void V3ParseImp::lexFile(const string& modname) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// Prepare for lexing
|
2008-01-31 14:49:27 +00:00
|
|
|
|
UINFO(3,"Lexing "<<modname<<endl);
|
2009-10-31 14:08:38 +00:00
|
|
|
|
s_parsep = this;
|
2019-05-19 20:13:13 +00:00
|
|
|
|
fileline()->warnResetDefault(); // Reenable warnings on each file
|
|
|
|
|
lexDestroy(); // Restart from clean slate.
|
2018-10-14 22:39:33 +00:00
|
|
|
|
lexNew();
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
|
// Lex it
|
2009-10-31 14:08:38 +00:00
|
|
|
|
if (bisonParse()) v3fatal("Cannot continue\n");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//======================================================================
|
2009-10-31 14:08:38 +00:00
|
|
|
|
// V3Parse functions
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
2012-08-16 01:28:30 +00:00
|
|
|
|
V3Parse::V3Parse(AstNetlist* rootp, V3InFilter* filterp, V3ParseSym* symp) {
|
2018-08-25 13:52:45 +00:00
|
|
|
|
m_impp = new V3ParseImp(rootp, filterp, symp);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
2009-10-31 14:08:38 +00:00
|
|
|
|
V3Parse::~V3Parse() {
|
|
|
|
|
delete m_impp; m_impp = NULL;
|
2008-07-22 18:27:34 +00:00
|
|
|
|
}
|
2011-10-28 00:56:38 +00:00
|
|
|
|
void V3Parse::parseFile(FileLine* fileline, const string& modname, bool inLibrary,
|
2019-05-19 20:13:13 +00:00
|
|
|
|
const string& errmsg) {
|
2011-10-28 00:56:38 +00:00
|
|
|
|
m_impp->parseFile(fileline, modname, inLibrary, errmsg);
|
2009-10-31 14:08:38 +00:00
|
|
|
|
}
|
|
|
|
|
void V3Parse::ppPushText(V3ParseImp* impp, const string& text) {
|
2017-10-10 01:08:50 +00:00
|
|
|
|
if (text != "") impp->ppPushText(text);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|