2012-04-13 01:08:20 +00:00
|
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2009-10-31 14:08:38 +00:00
|
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Common header between parser and lex
|
|
|
|
|
//
|
|
|
|
|
// Code available from: http://www.veripool.org/verilator
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2017-01-15 17:09:59 +00:00
|
|
|
|
// Copyright 2009-2017 by Wilson Snyder. This program is free software; you can
|
2009-10-31 14:08:38 +00:00
|
|
|
|
// redistribute it and/or modify it under the terms of either the GNU
|
|
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
|
// Version 2.0.
|
|
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
|
|
|
|
#ifndef _V3PARSEIMP_H_
|
|
|
|
|
#define _V3PARSEIMP_H_ 1
|
|
|
|
|
#include "config_build.h"
|
|
|
|
|
#include "verilatedos.h"
|
|
|
|
|
#include "V3Error.h"
|
2014-11-22 16:48:39 +00:00
|
|
|
|
#include "V3FileLine.h"
|
2009-10-31 14:08:38 +00:00
|
|
|
|
#include "V3Global.h"
|
|
|
|
|
#include "V3Parse.h"
|
2012-08-16 01:14:20 +00:00
|
|
|
|
#include "V3ParseSym.h"
|
2009-10-31 14:08:38 +00:00
|
|
|
|
#include <deque>
|
|
|
|
|
|
|
|
|
|
class V3Lexer;
|
|
|
|
|
|
|
|
|
|
// IMPORTANT: Don't include this file other than in the bison and flex,
|
|
|
|
|
// as it's definitions will confuse other parsers
|
|
|
|
|
|
|
|
|
|
//======================================================================
|
|
|
|
|
// Types (between parser & lexer)
|
|
|
|
|
|
2010-12-26 02:58:28 +00:00
|
|
|
|
typedef enum { uniq_NONE, uniq_UNIQUE, uniq_UNIQUE0, uniq_PRIORITY } V3UniqState;
|
2009-10-31 14:08:38 +00:00
|
|
|
|
|
2009-12-03 11:55:29 +00:00
|
|
|
|
typedef enum { iprop_NONE, iprop_CONTEXT, iprop_PURE } V3ImportProperty;
|
|
|
|
|
|
2009-10-31 14:08:38 +00:00
|
|
|
|
//============================================================================
|
|
|
|
|
// We can't use bison's %union as we want to pass the fileline with all tokens
|
|
|
|
|
|
|
|
|
|
struct V3ParseBisonYYSType {
|
|
|
|
|
FileLine* fl;
|
2009-11-08 02:05:02 +00:00
|
|
|
|
AstNode* scp; // Symbol table scope for future lookups
|
2009-10-31 14:08:38 +00:00
|
|
|
|
union {
|
|
|
|
|
V3Number* nump;
|
|
|
|
|
string* strp;
|
|
|
|
|
int cint;
|
|
|
|
|
double cdouble;
|
2012-02-12 01:40:58 +00:00
|
|
|
|
bool cbool;
|
2009-10-31 14:08:38 +00:00
|
|
|
|
V3UniqState uniqstate;
|
2012-04-29 12:30:02 +00:00
|
|
|
|
VSignedState signstate;
|
2009-12-03 11:55:29 +00:00
|
|
|
|
V3ImportProperty iprop;
|
2010-01-21 11:11:30 +00:00
|
|
|
|
V3ErrorCode::en errcodeen;
|
2009-10-31 14:08:38 +00:00
|
|
|
|
|
|
|
|
|
AstNode* nodep;
|
|
|
|
|
|
2009-11-02 13:06:04 +00:00
|
|
|
|
AstBasicDType* bdtypep;
|
2009-10-31 14:08:38 +00:00
|
|
|
|
AstBegin* beginp;
|
|
|
|
|
AstCase* casep;
|
|
|
|
|
AstCaseItem* caseitemp;
|
2012-04-25 00:43:15 +00:00
|
|
|
|
AstCell* cellp;
|
2009-10-31 14:08:38 +00:00
|
|
|
|
AstConst* constp;
|
2012-07-29 14:16:20 +00:00
|
|
|
|
AstMemberDType* memberp;
|
2009-11-07 11:20:20 +00:00
|
|
|
|
AstNodeModule* modulep;
|
2012-07-29 14:16:20 +00:00
|
|
|
|
AstNodeClassDType* classp;
|
2009-11-05 14:57:23 +00:00
|
|
|
|
AstNodeDType* dtypep;
|
2009-11-03 03:50:31 +00:00
|
|
|
|
AstNodeFTask* ftaskp;
|
2009-11-08 02:05:02 +00:00
|
|
|
|
AstNodeFTaskRef* ftaskrefp;
|
2009-11-03 03:50:31 +00:00
|
|
|
|
AstNodeSenItem* senitemp;
|
2009-10-31 14:08:38 +00:00
|
|
|
|
AstNodeVarRef* varnodep;
|
2009-11-08 02:05:02 +00:00
|
|
|
|
AstPackage* packagep;
|
2012-12-31 22:05:13 +00:00
|
|
|
|
AstPackageRef* packagerefp;
|
2009-10-31 14:08:38 +00:00
|
|
|
|
AstParseRef* parserefp;
|
2012-08-12 19:15:21 +00:00
|
|
|
|
AstPatMember* patmemberp;
|
2013-02-14 01:52:38 +00:00
|
|
|
|
AstPattern* patternp;
|
2009-10-31 14:08:38 +00:00
|
|
|
|
AstPin* pinp;
|
|
|
|
|
AstRange* rangep;
|
|
|
|
|
AstSenTree* sentreep;
|
|
|
|
|
AstVar* varp;
|
2010-01-08 03:08:48 +00:00
|
|
|
|
AstVarRef* varrefp;
|
2009-10-31 14:08:38 +00:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define YYSTYPE V3ParseBisonYYSType
|
|
|
|
|
|
2009-10-31 14:14:04 +00:00
|
|
|
|
//######################################################################
|
2009-10-31 14:08:38 +00:00
|
|
|
|
|
|
|
|
|
class V3ParseImp {
|
|
|
|
|
// MEMBERS
|
2010-01-20 12:15:51 +00:00
|
|
|
|
AstNetlist* m_rootp; // Root of the design
|
|
|
|
|
V3InFilter* m_filterp; // Reading filter
|
2012-08-16 01:28:30 +00:00
|
|
|
|
V3ParseSym* m_symp; // Symbol table
|
|
|
|
|
|
2010-01-20 12:15:51 +00:00
|
|
|
|
V3Lexer* m_lexerp; // Current FlexLexer
|
2009-10-31 14:08:38 +00:00
|
|
|
|
static V3ParseImp* s_parsep; // Current THIS, bison() isn't class based
|
2010-01-20 12:15:51 +00:00
|
|
|
|
FileLine* m_fileline; // Filename/linenumber currently active
|
2009-10-31 14:08:38 +00:00
|
|
|
|
|
|
|
|
|
bool m_inCellDefine; // Inside a `celldefine
|
|
|
|
|
bool m_inLibrary; // Currently reading a library vs. regular file
|
|
|
|
|
int m_inBeginKwd; // Inside a `begin_keywords
|
|
|
|
|
int m_lastVerilogState; // Last LEX state in `begin_keywords
|
|
|
|
|
|
2014-03-11 23:07:58 +00:00
|
|
|
|
int m_prevLexToken; // previous parsed token (for lexer)
|
2010-01-23 00:08:20 +00:00
|
|
|
|
bool m_ahead; // aheadToken is valid
|
|
|
|
|
int m_aheadToken; // Token we read ahead
|
|
|
|
|
V3ParseBisonYYSType m_aheadVal; // aheadToken's value
|
|
|
|
|
|
2009-10-31 14:08:38 +00:00
|
|
|
|
deque<string*> m_stringps; // Created strings for later cleanup
|
|
|
|
|
deque<V3Number*> m_numberps; // Created numbers for later cleanup
|
|
|
|
|
deque<FileLine> m_lintState; // Current lint state for save/restore
|
|
|
|
|
deque<string> m_ppBuffers; // Preprocessor->lex buffer of characters to process
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// Note these are an exception to using the filename as the debug type
|
|
|
|
|
static int debugBison() {
|
|
|
|
|
static int level = -1;
|
|
|
|
|
if (VL_UNLIKELY(level < 0)) level = v3Global.opt.debugSrcLevel("bison");
|
|
|
|
|
return level;
|
|
|
|
|
}
|
|
|
|
|
static int debugFlex() {
|
|
|
|
|
static int level = -1;
|
|
|
|
|
if (VL_UNLIKELY(level < 0)) level = v3Global.opt.debugSrcLevel("flex");
|
|
|
|
|
return level;
|
|
|
|
|
}
|
|
|
|
|
static int debug() { return debugBison() ? debugFlex() : 0; }
|
|
|
|
|
|
|
|
|
|
// Functions called by lex rules:
|
|
|
|
|
int yylexThis();
|
|
|
|
|
static bool optFuture(const string& flag) { return v3Global.opt.isFuture(flag); }
|
|
|
|
|
|
|
|
|
|
void ppline (const char* text);
|
2010-07-09 00:31:41 +00:00
|
|
|
|
void linenoInc() { fileline()->linenoInc(); }
|
2009-10-31 14:08:38 +00:00
|
|
|
|
void verilatorCmtLint(const char* text, bool on);
|
|
|
|
|
void verilatorCmtLintSave();
|
|
|
|
|
void verilatorCmtLintRestore();
|
|
|
|
|
void verilatorCmtBad(const char* text);
|
2016-03-23 11:48:32 +00:00
|
|
|
|
static double parseDouble(const char* text, size_t length, bool* successp = NULL);
|
2009-10-31 14:08:38 +00:00
|
|
|
|
void pushBeginKeywords(int state) { m_inBeginKwd++; m_lastVerilogState=state; }
|
|
|
|
|
bool popBeginKeywords() { if (m_inBeginKwd) { m_inBeginKwd--; return true; } else return false; }
|
2011-08-05 01:58:45 +00:00
|
|
|
|
int lastVerilogState() const { return m_lastVerilogState; }
|
2009-10-31 14:08:38 +00:00
|
|
|
|
static const char* tokenName(int tok);
|
|
|
|
|
|
|
|
|
|
void ppPushText(const string& text) { m_ppBuffers.push_back(text); }
|
2010-04-07 00:20:44 +00:00
|
|
|
|
size_t ppInputToLex(char* buf, size_t max_size);
|
2009-10-31 14:08:38 +00:00
|
|
|
|
|
|
|
|
|
static V3ParseImp* parsep() { return s_parsep; }
|
|
|
|
|
|
|
|
|
|
// TODO: Many of these functions are the old interface; they'd be better as non-static
|
|
|
|
|
// and called as READP->newString(...) etc.
|
|
|
|
|
string* newString(const string& text) {
|
|
|
|
|
// Allocate a string, remembering it so we can reclaim storage at lex end
|
|
|
|
|
string* strp = new string (text);
|
|
|
|
|
m_stringps.push_back(strp);
|
|
|
|
|
return strp;
|
|
|
|
|
}
|
|
|
|
|
string* newString(const char* text) {
|
|
|
|
|
// Allocate a string, remembering it so we can reclaim storage at lex end
|
|
|
|
|
string* strp = new string (text);
|
|
|
|
|
m_stringps.push_back(strp);
|
|
|
|
|
return strp;
|
|
|
|
|
}
|
2010-04-07 00:20:44 +00:00
|
|
|
|
string* newString(const char* text, size_t length) {
|
2009-10-31 14:08:38 +00:00
|
|
|
|
string* strp = new string (text, length);
|
|
|
|
|
m_stringps.push_back(strp);
|
|
|
|
|
return strp;
|
|
|
|
|
}
|
|
|
|
|
V3Number* newNumber(FileLine* fl, const char* text) {
|
|
|
|
|
V3Number* nump = new V3Number (fl, text);
|
|
|
|
|
m_numberps.push_back(nump);
|
|
|
|
|
return nump;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return next token, for bison, since bison isn't class based, use a global THIS
|
2011-08-05 01:58:45 +00:00
|
|
|
|
FileLine* fileline() const { return m_fileline; }
|
|
|
|
|
AstNetlist* rootp() const { return m_rootp; }
|
2009-10-31 14:08:38 +00:00
|
|
|
|
FileLine* copyOrSameFileLine() { return fileline()->copyOrSameFileLine(); }
|
2011-08-05 01:58:45 +00:00
|
|
|
|
bool inCellDefine() const { return m_inCellDefine; }
|
2009-10-31 14:08:38 +00:00
|
|
|
|
void inCellDefine(bool flag) { m_inCellDefine = flag; }
|
2011-08-05 01:58:45 +00:00
|
|
|
|
bool inLibrary() const { return m_inLibrary; }
|
2009-10-31 14:08:38 +00:00
|
|
|
|
|
|
|
|
|
// Interactions with parser
|
|
|
|
|
int bisonParse();
|
|
|
|
|
|
|
|
|
|
// Interactions with lexer
|
|
|
|
|
void lexNew(int debug);
|
|
|
|
|
void lexDestroy();
|
2011-08-05 01:58:45 +00:00
|
|
|
|
void statePop(); // Parser -> lexer communication
|
|
|
|
|
static int stateVerilogRecent(); // Parser -> lexer communication
|
2014-03-11 23:07:58 +00:00
|
|
|
|
int prevLexToken() { return m_prevLexToken; } // Parser -> lexer communication
|
2010-04-07 00:20:44 +00:00
|
|
|
|
size_t flexPpInputToLex(char* buf, size_t max_size) { return ppInputToLex(buf,max_size); }
|
2009-10-31 14:08:38 +00:00
|
|
|
|
|
2009-10-31 14:14:04 +00:00
|
|
|
|
//==== Symbol tables
|
2012-08-16 01:28:30 +00:00
|
|
|
|
V3ParseSym* symp() { return m_symp; }
|
2009-10-31 14:14:04 +00:00
|
|
|
|
|
2009-10-31 14:08:38 +00:00
|
|
|
|
public:
|
|
|
|
|
// CREATORS
|
2012-08-16 01:28:30 +00:00
|
|
|
|
V3ParseImp(AstNetlist* rootp, V3InFilter* filterp, V3ParseSym* parserSymp)
|
|
|
|
|
: m_rootp(rootp), m_filterp(filterp), m_symp(parserSymp) {
|
2011-08-05 01:58:45 +00:00
|
|
|
|
m_fileline = NULL;
|
2012-08-16 01:28:30 +00:00
|
|
|
|
m_lexerp = NULL;
|
2009-10-31 14:08:38 +00:00
|
|
|
|
m_inCellDefine = false;
|
|
|
|
|
m_inLibrary = false;
|
|
|
|
|
m_inBeginKwd = 0;
|
|
|
|
|
m_lastVerilogState = stateVerilogRecent();
|
2014-03-11 23:07:58 +00:00
|
|
|
|
m_prevLexToken = 0;
|
2010-01-23 00:08:20 +00:00
|
|
|
|
m_ahead = false;
|
|
|
|
|
m_aheadToken = 0;
|
2015-02-10 02:05:27 +00:00
|
|
|
|
// m_aheadVal not used as m_ahead = false
|
2009-10-31 14:08:38 +00:00
|
|
|
|
}
|
|
|
|
|
~V3ParseImp();
|
|
|
|
|
void parserClear();
|
2014-03-11 23:07:58 +00:00
|
|
|
|
void unputString(const char* textp, size_t length);
|
2009-10-31 14:08:38 +00:00
|
|
|
|
|
|
|
|
|
// METHODS
|
|
|
|
|
// Preprocess and read the Verilog file specified into the netlist database
|
|
|
|
|
int lexToBison(); // Pass token to bison
|
|
|
|
|
|
2011-10-28 00:56:38 +00:00
|
|
|
|
void parseFile(FileLine* fileline, const string& modfilename, bool inLibrary,
|
|
|
|
|
const string& errmsg);
|
2009-10-31 14:08:38 +00:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void lexFile(const string& modname);
|
2014-03-11 23:07:58 +00:00
|
|
|
|
int yylexReadTok();
|
2009-10-31 14:08:38 +00:00
|
|
|
|
int lexToken(); // Internal; called from lexToBison
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif // Guard
|