Optimize constant string arguments before Emit, and cleanup V3EmitV

This commit is contained in:
Wilson Snyder 2025-10-12 20:09:03 -04:00
parent eae1e5bdf2
commit 01157d414b
8 changed files with 36 additions and 65 deletions

View File

@ -4872,7 +4872,7 @@ public:
}
ASTGEN_MEMBERS_AstCvtPackString;
void numberOperate(V3Number& out, const V3Number& lhs) override { out.opAssign(lhs); }
string emitVerilog() override { return "%f$_CAST(%l)"; }
string emitVerilog() override { V3ERROR_NA_RETURN(""); }
string emitC() override { V3ERROR_NA_RETURN(""); }
bool cleanOut() const override { return true; }
bool cleanLhs() const override { return true; }

View File

@ -864,7 +864,7 @@ public:
switch (nodep->ctlType()) {
case VDumpCtlType::FILE:
putns(nodep, "vlSymsp->_vm_contextp__->dumpfile(");
emitCvtPackStr(nodep->exprp());
iterateConst(nodep->exprp());
puts(");\n");
break;
case VDumpCtlType::VARS:
@ -927,7 +927,7 @@ public:
puts("(");
puts(cvtToStr(nodep->outp()->widthMin()));
puts(", ");
emitCvtPackStr(nodep->searchp());
iterateConst(nodep->searchp());
puts(", ");
putbs("");
iterateAndNextConstNull(nodep->outp());
@ -935,7 +935,7 @@ public:
}
void visit(AstTestPlusArgs* nodep) override {
putns(nodep, "VL_TESTPLUSARGS_I(");
emitCvtPackStr(nodep->searchp());
iterateConst(nodep->searchp());
puts(")");
}
void visit(AstFError* nodep) override {
@ -965,16 +965,14 @@ public:
}
void visit(AstFOpen* nodep) override {
putns(nodep, "VL_FOPEN_NN(");
emitCvtPackStr(nodep->filenamep());
iterateConst(nodep->filenamep());
putbs(", ");
if (nodep->modep()->width() > 4 * 8)
nodep->modep()->v3error("$fopen mode should be <= 4 characters");
emitCvtPackStr(nodep->modep());
iterateConst(nodep->modep());
puts(");\n");
}
void visit(AstFOpenMcd* nodep) override {
putns(nodep, "VL_FOPEN_MCD_N(");
emitCvtPackStr(nodep->filenamep());
iterateConst(nodep->filenamep());
puts(");\n");
}
void visit(AstNodeReadWriteMem* nodep) override {
@ -1004,7 +1002,7 @@ public:
}
}
putbs(", ");
emitCvtPackStr(nodep->filenamep());
iterateConst(nodep->filenamep());
putbs(", ");
{
const bool need_ptr = !VN_IS(nodep->memp()->dtypep(), AssocArrayDType);
@ -1297,7 +1295,7 @@ public:
puts(", ");
if (nodep->suffixp()) {
puts("true, ");
emitCvtPackStr(nodep->suffixp());
iterateAndNextConstNull(nodep->suffixp());
} else {
puts("false, \"\"");
}

View File

@ -283,6 +283,16 @@ class EmitVBaseVisitorConst VL_NOT_FINAL : public VNVisitorConst {
void visit(AstCoverInc*) override {} // N/A
void visit(AstCoverToggle*) override {} // N/A
void visit(AstCvtPackString* nodep) override {
putfs(nodep, "");
if (AstConst* const lhsConstp = VN_CAST(nodep->lhsp(), Const)) {
putsQuoted(lhsConstp->num().toString());
} else {
puts("string'(");
iterateAndNextConstNull(nodep->lhsp());
puts(")");
}
}
void visit(AstTestPlusArgs* nodep) override {
putfs(nodep, nodep->verilogKwd());
putbs("(");
@ -292,7 +302,9 @@ class EmitVBaseVisitorConst VL_NOT_FINAL : public VNVisitorConst {
void visit(AstValuePlusArgs* nodep) override {
putfs(nodep, nodep->verilogKwd());
putbs("(");
iterateChildrenConst(nodep);
iterateConstNull(nodep->searchp());
putbs(", ");
iterateConstNull(nodep->outp());
puts(")");
}
void visitNodeDisplay(AstNode* nodep, AstNode* fileOrStrgp, const string& text,

View File

@ -5893,17 +5893,16 @@ class WidthVisitor final : public VNVisitor {
}
void visit(AstDumpCtl* nodep) override {
assertAtStatement(nodep);
// Just let all arguments seek their natural sizes
userIterateChildren(nodep, WidthVP{SELF, BOTH}.p());
if (nodep->exprp()) iterateCheckString(nodep, "LHS", nodep->exprp(), BOTH);
}
void visit(AstFOpen* nodep) override {
// Although a system function in IEEE, here a statement which sets the file pointer (MCD)
userIterateAndNext(nodep->filenamep(), WidthVP{SELF, BOTH}.p());
userIterateAndNext(nodep->modep(), WidthVP{SELF, BOTH}.p());
iterateCheckString(nodep, "filename", nodep->filenamep(), BOTH);
iterateCheckString(nodep, "mode", nodep->modep(), BOTH);
nodep->dtypeSetLogicUnsized(32, 1, VSigning::SIGNED); // Spec says integer return
}
void visit(AstFOpenMcd* nodep) override {
userIterateAndNext(nodep->filenamep(), WidthVP{SELF, BOTH}.p());
iterateCheckString(nodep, "filename", nodep->filenamep(), BOTH);
nodep->dtypeSetLogicUnsized(32, 1, VSigning::SIGNED); // Spec says integer return
}
void visit(AstFClose* nodep) override {
@ -6005,7 +6004,7 @@ class WidthVisitor final : public VNVisitor {
}
void visit(AstNodeReadWriteMem* nodep) override {
assertAtStatement(nodep);
userIterateAndNext(nodep->filenamep(), WidthVP{SELF, BOTH}.p());
iterateCheckString(nodep, "filename", nodep->filenamep(), BOTH);
userIterateAndNext(nodep->memp(), WidthVP{SELF, BOTH}.p());
const AstNodeDType* subp = nullptr;
if (const AstAssocArrayDType* adtypep
@ -6037,13 +6036,13 @@ class WidthVisitor final : public VNVisitor {
}
void visit(AstTestPlusArgs* nodep) override {
if (m_vup->prelim()) {
userIterateAndNext(nodep->searchp(), WidthVP{SELF, BOTH}.p());
iterateCheckString(nodep, "LHS", nodep->searchp(), BOTH);
nodep->dtypeChgWidthSigned(32, 1, VSigning::SIGNED); // Spec says integer return
}
}
void visit(AstValuePlusArgs* nodep) override {
if (m_vup->prelim()) {
userIterateAndNext(nodep->searchp(), WidthVP{SELF, BOTH}.p());
iterateCheckString(nodep, "LHS", nodep->searchp(), BOTH);
userIterateAndNext(nodep->outp(), WidthVP{SELF, BOTH}.p());
nodep->dtypeChgWidthSigned(32, 1, VSigning::SIGNED); // Spec says integer return
}

View File

@ -40,7 +40,7 @@ module Vt_debug_emitv_t;
Iface the_ifaces[3:0] ();
initial begin
begin
if ($test$plusargs(40'h48454c4c4f)) begin
if ($test$plusargs("HELLO")) begin
$display("Hello argument found.");
end
if ((Pkg::FOO == 'sh0)) begin
@ -49,7 +49,7 @@ module Vt_debug_emitv_t;
if ((ZERO == 'sh0)) begin
$write("");
end
if ($value$plusargs(56'h544553543d2564i1)) begin
if ($value$plusargs("TEST=%d", i1)) begin
$display("value was %~", i1);
end
else begin
@ -185,10 +185,10 @@ module Vt_debug_emitv_t;
: $c('sh1e)));
$c(;);
$display("%d", $c(0));
fd = $fopen(72'h2f6465762f6e756c6c);
fd = $fopen("/dev/null");
;
$fclose(fd);
fd = $fopen(72'h2f6465762f6e756c6c, 8'h72);
fd = $fopen("/dev/null", "r");
;
$fgetc(fd);
$fflush(fd);
@ -196,9 +196,9 @@ module Vt_debug_emitv_t;
;
$fdisplay(32'h69203d20, "%~", sum);
$fwrite(fd, "hello");
$readmemh(fd, array);
$readmemh(fd, array, 'sh0);
$readmemh(fd, array, 'sh0, 'sh0);
$readmemh(string'(fd), array);
$readmemh(string'(fd), array, 'sh0);
$readmemh(string'(fd), array, 'sh0, 'sh0);
sum = 'sh0;
begin : unnamedblk3
int signed i;

View File

@ -1,5 +0,0 @@
%Error: t/t_sys_fopen_bad.v:14:34: $fopen mode should be <= 4 characters
14 | file = $fopen("/dev/null", "somereallywidebad");
| ^~~~~~~~~~~~~~~~~~~
... See the manual at https://verilator.org/verilator_doc.html?v=latest for more assistance.
%Error: Exiting due to

View File

@ -1,16 +0,0 @@
#!/usr/bin/env python3
# DESCRIPTION: Verilator: Verilog Test driver/expect definition
#
# Copyright 2024 by Wilson Snyder. This program is free software; you
# can 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.
# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
import vltest_bootstrap
test.scenarios('vlt')
test.lint(fails=True, expect_filename=test.golden_filename)
test.passes()

View File

@ -1,17 +0,0 @@
// DESCRIPTION: Verilator: Verilog Test module
//
// Copyright 2022 by Wilson Snyder. This program is free software; you can
// 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.
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
module t ();
integer file;
initial begin
file = $fopen("/dev/null", "somereallywidebad");
end
endmodule