From b0f898cec87a2bd772d7811c462b1e2abf35ca47 Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Sun, 1 Dec 2024 11:35:00 -0500 Subject: [PATCH] Internals: Determine needing verilated_std without symbol table --- include/verilated_std.sv | 3 +++ src/Makefile_obj.in | 4 +++- src/V3ParseImp.cpp | 30 ++++++++++++++++++++++-------- src/V3ParseImp.h | 1 + test_regress/t/t_dump_json.out | 16 ++++++++-------- test_regress/t/t_std_identifier.py | 2 +- 6 files changed, 38 insertions(+), 18 deletions(-) diff --git a/include/verilated_std.sv b/include/verilated_std.sv index 8a1e2541c..dfe8b9ab6 100644 --- a/include/verilated_std.sv +++ b/include/verilated_std.sv @@ -21,6 +21,9 @@ /// It is only for internal use. /// //************************************************************************* +// +// The following keywords from this file are hardcoded for detection in the parser: +// "mailbox", "process", "randomize", "semaphore", "std" // verilator lint_off DECLFILENAME // verilator lint_off TIMESCALEMOD diff --git a/src/Makefile_obj.in b/src/Makefile_obj.in index adfcb2215..291bb88c9 100644 --- a/src/Makefile_obj.in +++ b/src/Makefile_obj.in @@ -142,6 +142,8 @@ BISONPRE = $(srcdir)/bisonpre FLEXFIX = $(srcdir)/flexfix VLCOVGEN = $(srcdir)/vlcovgen +# BISON_DEBUG = -Wcounterexamples + ###################################################################### # CCACHE flags (via environment as no command line option available) CCACHE_SLOPPINESS ?= pch_defines,time_macros @@ -417,7 +419,7 @@ V3ParseBison.h: V3ParseBison.c # Have only one output file in this rule to prevent parallel make issues V3ParseBison.c: verilog.y $(BISONPRE) @echo "If you get errors from verilog.y below, try upgrading bison to version 1.875 or newer." - $(PYTHON3) $(BISONPRE) --yacc ${YACC} -d -v -o V3ParseBison.c $< + $(PYTHON3) $(BISONPRE) --yacc ${YACC} -d -v -o V3ParseBison.c $(BISON_DEBUG) $< V3Lexer_pregen.yy.cpp: verilog.l V3ParseBison.h $(HEADERS) ${LEX} --version diff --git a/src/V3ParseImp.cpp b/src/V3ParseImp.cpp index 0b98a6c8f..451e08982 100644 --- a/src/V3ParseImp.cpp +++ b/src/V3ParseImp.cpp @@ -70,6 +70,25 @@ V3ParseImp::~V3ParseImp() { //###################################################################### // Parser utility methods +void V3ParseImp::importIfInStd(FileLine* fileline, const string& id) { + // Keywords that auto-import to require use of verilated_std.vh. + // OK if overly sensitive; will over-import and keep std:: around + // longer than migt otherwise. + if (v3Global.usesStdPackage()) return; // Run once then short-circuit + const bool identifierImportsStd = (id == "mailbox" || id == "process" || id == "randomize" + || id == "semaphore" || id == "std"); + if (!identifierImportsStd) return; + // Ignore Std:: used inside verilated_std.vh itself + if (fileline->filename() == V3Options::getStdPackagePath()) return; + if (AstPackage* const stdpkgp + = v3Global.rootp()->stdPackagep()) { // else e.g. --no-std-package + UINFO(9, "import and keep std:: for " << fileline << "\n"); + AstPackageImport* const impp = new AstPackageImport{stdpkgp->fileline(), stdpkgp, "*"}; + unitPackage(stdpkgp->fileline())->addStmtsp(impp); + v3Global.setUsesStdPackage(); + } +} + void V3ParseImp::lexPpline(const char* textp) { // Handle lexer `line directive // FileLine* const prevFl = lexFileline(); @@ -647,6 +666,9 @@ void V3ParseImp::tokenPipelineSym() { // Note above sometimes converts yGLOBAL to a yaID__LEX tokenPipeline(); // sets yylval int token = yylval.token; + if (token == yaID__LEX || token == yaID__CC || token == yaID__aTYPE) { + importIfInStd(yylval.fl, *(yylval.strp)); + } if (token == yaID__LEX || token == yaID__CC) { const VSymEnt* foundp; if (const VSymEnt* const look_underp = V3ParseImp::parsep()->symp()->nextId()) { @@ -669,12 +691,6 @@ void V3ParseImp::tokenPipelineSym() { VSymEnt* const stdsymp = stdpkgp->user4u().toSymEnt(); foundp = stdsymp->findIdFallback(*(yylval.strp)); } - if (foundp && !v3Global.usesStdPackage()) { - AstPackageImport* const impp - = new AstPackageImport{stdpkgp->fileline(), stdpkgp, "*"}; - unitPackage(stdpkgp->fileline())->addStmtsp(impp); - v3Global.setUsesStdPackage(); - } } if (foundp) { AstNode* const scp = foundp->nodep(); @@ -692,8 +708,6 @@ void V3ParseImp::tokenPipelineSym() { } else { token = yaID__ETC; } - } else if (!m_afterColonColon && *(yylval.strp) == "std") { - v3Global.setUsesStdPackage(); } } else { // Not found yylval.scp = nullptr; diff --git a/src/V3ParseImp.h b/src/V3ParseImp.h index 82e8e4fcb..1afbf98fe 100644 --- a/src/V3ParseImp.h +++ b/src/V3ParseImp.h @@ -309,6 +309,7 @@ private: void preprocDumps(std::ostream& os); void lexFile(const string& modname) VL_MT_DISABLED; void yylexReadTok() VL_MT_DISABLED; + void importIfInStd(FileLine* fileline, const string& id); void tokenPull() VL_MT_DISABLED; void tokenPipeline() VL_MT_DISABLED; // Internal; called from tokenToBison int tokenPipelineId(int token) VL_MT_DISABLED; diff --git a/test_regress/t/t_dump_json.out b/test_regress/t/t_dump_json.out index ac99ae1cd..602123ff9 100644 --- a/test_regress/t/t_dump_json.out +++ b/test_regress/t/t_dump_json.out @@ -519,14 +519,14 @@ "miscsp": [ {"type":"TYPETABLE","name":"","addr":"(C)","loc":"a,0:0,0:0","constraintRefp":"UNLINKED","emptyQueuep":"UNLINKED","queueIndexp":"UNLINKED","streamp":"UNLINKED","voidp":"(HI)", "typesp": [ - {"type":"BASICDTYPE","name":"integer","addr":"(II)","loc":"d,31:27,31:28","dtypep":"(II)","keyword":"integer","range":"31:0","generic":true,"rangep": []}, - {"type":"BASICDTYPE","name":"logic","addr":"(L)","loc":"d,33:32,33:33","dtypep":"(L)","keyword":"logic","range":"31:0","generic":true,"rangep": []}, - {"type":"BASICDTYPE","name":"logic","addr":"(UE)","loc":"d,50:22,50:24","dtypep":"(UE)","keyword":"logic","generic":true,"rangep": []}, - {"type":"VOIDDTYPE","name":"","addr":"(HI)","loc":"d,51:21,51:30","dtypep":"(HI)","generic":false}, - {"type":"BASICDTYPE","name":"logic","addr":"(QD)","loc":"d,125:22,125:23","dtypep":"(QD)","keyword":"logic","range":"31:0","generic":true,"rangep": []}, - {"type":"BASICDTYPE","name":"logic","addr":"(JI)","loc":"d,127:22,127:23","dtypep":"(JI)","keyword":"logic","range":"31:0","generic":true,"rangep": []}, - {"type":"BASICDTYPE","name":"logic","addr":"(KI)","loc":"d,162:17,162:56","dtypep":"(KI)","keyword":"logic","range":"295:0","generic":true,"rangep": []}, - {"type":"BASICDTYPE","name":"string","addr":"(BG)","loc":"d,162:10,162:16","dtypep":"(BG)","keyword":"string","generic":true,"rangep": []}, + {"type":"BASICDTYPE","name":"integer","addr":"(II)","loc":"d,34:27,34:28","dtypep":"(II)","keyword":"integer","range":"31:0","generic":true,"rangep": []}, + {"type":"BASICDTYPE","name":"logic","addr":"(L)","loc":"d,36:32,36:33","dtypep":"(L)","keyword":"logic","range":"31:0","generic":true,"rangep": []}, + {"type":"BASICDTYPE","name":"logic","addr":"(UE)","loc":"d,53:22,53:24","dtypep":"(UE)","keyword":"logic","generic":true,"rangep": []}, + {"type":"VOIDDTYPE","name":"","addr":"(HI)","loc":"d,54:21,54:30","dtypep":"(HI)","generic":false}, + {"type":"BASICDTYPE","name":"logic","addr":"(QD)","loc":"d,128:22,128:23","dtypep":"(QD)","keyword":"logic","range":"31:0","generic":true,"rangep": []}, + {"type":"BASICDTYPE","name":"logic","addr":"(JI)","loc":"d,130:22,130:23","dtypep":"(JI)","keyword":"logic","range":"31:0","generic":true,"rangep": []}, + {"type":"BASICDTYPE","name":"logic","addr":"(KI)","loc":"d,165:17,165:56","dtypep":"(KI)","keyword":"logic","range":"295:0","generic":true,"rangep": []}, + {"type":"BASICDTYPE","name":"string","addr":"(BG)","loc":"d,165:10,165:16","dtypep":"(BG)","keyword":"string","generic":true,"rangep": []}, {"type":"BASICDTYPE","name":"logic","addr":"(Q)","loc":"e,14:9,14:11","dtypep":"(Q)","keyword":"logic","range":"31:0","generic":true,"rangep": []}, {"type":"BASICDTYPE","name":"logic","addr":"(BB)","loc":"e,18:10,18:12","dtypep":"(BB)","keyword":"logic","range":"31:0","generic":true,"rangep": []}, {"type":"BASICDTYPE","name":"logic","addr":"(MC)","loc":"e,33:26,33:31","dtypep":"(MC)","keyword":"logic","range":"31:0","generic":true,"rangep": []}, diff --git a/test_regress/t/t_std_identifier.py b/test_regress/t/t_std_identifier.py index 6938ef706..c312a43ce 100755 --- a/test_regress/t/t_std_identifier.py +++ b/test_regress/t/t_std_identifier.py @@ -11,6 +11,6 @@ import vltest_bootstrap test.scenarios('linter') -test.lint(verilator_flags2=["-DTEST_DECLARE_STD"]) +test.lint(verilator_flags2=["-DTEST_DECLARE_STD"], fails=test.vlt_all) # Issue #4705 due to :: test.passes()