Support 'super'.

This commit is contained in:
Wilson Snyder 2020-11-26 11:06:59 -05:00
parent 1a92a44c7d
commit a37866ee92
14 changed files with 133 additions and 39 deletions

View File

@ -7,6 +7,8 @@ The contributors that suggested a given feature are shown in []. Thanks!
*** Support $random and $urandom seeds.
*** Support 'super'.
*** Support 'with item.index'.
*** Check for proper 'local' and 'protected' (#2228).

View File

@ -1180,6 +1180,9 @@ void AstClass::dump(std::ostream& str) const {
}
AstClass* AstClassExtends::classp() const {
AstClassRefDType* refp = VN_CAST(dtypep(), ClassRefDType);
if (VL_UNLIKELY(!refp)) { // LinkDot uses this for 'super.'
refp = VN_CAST(childDTypep(), ClassRefDType);
}
UASSERT_OBJ(refp, this, "class extends non-ref");
return refp->classp();
}

View File

@ -2027,10 +2027,34 @@ private:
do {
classSymp = classSymp->parentp();
} while (classSymp && !VN_IS(classSymp->nodep(), Class));
m_ds.m_dotSymp = classSymp;
if (!classSymp) {
nodep->v3error("'this' used outside class");
nodep->v3error("'this' used outside class (IEEE 1800-2017 8.11)");
m_ds.m_dotErr = true;
} else {
m_ds.m_dotSymp = classSymp;
UINFO(8, " this. " << m_ds.ascii() << endl);
}
} else if (VN_IS(nodep->lhsp(), ParseRef) && nodep->lhsp()->name() == "super") {
VSymEnt* classSymp = m_ds.m_dotSymp;
do {
classSymp = classSymp->parentp();
} while (classSymp && !VN_IS(classSymp->nodep(), Class));
if (!classSymp) {
nodep->v3error("'super' used outside class (IEEE 1800-2017 8.15)");
m_ds.m_dotErr = true;
} else {
auto classp = VN_CAST(classSymp->nodep(), Class);
if (!classp->extendsp()) {
nodep->v3error("'super' used on non-extended class (IEEE 1800-2017 8.15)");
m_ds.m_dotErr = true;
} else {
auto cextp = VN_CAST(classp->extendsp(), ClassExtends);
UASSERT_OBJ(cextp, nodep, "Bad super extends link");
auto classp = cextp->classp();
UASSERT_OBJ(classp, nodep, "Bad superclass");
m_ds.m_dotSymp = m_statep->getNodeSym(classp);
UINFO(8, " super. " << m_ds.ascii() << endl);
}
}
} else if (VN_IS(nodep->lhsp(), ClassOrPackageRef)) {
// m_ds.m_dotText communicates the cell prefix between stages
@ -2504,6 +2528,7 @@ private:
virtual void visit(AstNodeFTaskRef* nodep) override {
if (nodep->user3SetOnce()) return;
UINFO(8, " " << nodep << endl);
UINFO(8, " " << m_ds.ascii() << endl);
if (m_ds.m_dotp && m_ds.m_dotPos == DP_PACKAGE) {
UASSERT_OBJ(VN_IS(m_ds.m_dotp->lhsp(), ClassOrPackageRef), m_ds.m_dotp->lhsp(),
"Bad package link");
@ -2558,12 +2583,15 @@ private:
} else {
string baddot;
VSymEnt* okSymp = nullptr;
VSymEnt* dotSymp = m_curSymp; // Start search at module, as a variable
VSymEnt* dotSymp = nodep->dotted().empty()
? m_ds.m_dotSymp // Non-'super.' dotted reference
: m_curSymp; // Start search at dotted point
// of same name under a subtask isn't a relevant hit however a
// function under a begin/end is. So we want begins, but not
// the function
if (nodep->classOrPackagep()) { // Look only in specified package
dotSymp = m_statep->getNodeSym(nodep->classOrPackagep());
UINFO(8, " Override classOrPackage " << dotSymp << endl);
} else {
if (nodep->inlinedDots() != "") { // Correct for current scope
// Dotted lookup is always relative to module, as maybe

View File

@ -1,25 +0,0 @@
%Error-UNSUPPORTED: t/t_class_extends_this.v:22:11: Unsupported: super
22 | if (super.value != 1) $stop;
| ^~~~~
%Error: t/t_class_extends_this.v:22:11: Can't find definition of scope/variable: 'super'
22 | if (super.value != 1) $stop;
| ^~~~~
%Error-UNSUPPORTED: t/t_class_extends_this.v:23:7: Unsupported: super
23 | super.test();
| ^~~~~
%Error: t/t_class_extends_this.v:23:7: Can't find definition of scope/variable: 'super'
23 | super.test();
| ^~~~~
%Error-UNSUPPORTED: t/t_class_extends_this.v:24:7: Unsupported: super
24 | super.value = 10;
| ^~~~~
%Error: t/t_class_extends_this.v:24:7: Can't find definition of scope/variable: 'super'
24 | super.value = 10;
| ^~~~~
%Error-UNSUPPORTED: t/t_class_extends_this.v:26:11: Unsupported: super
26 | if (super.value != 10) $stop;
| ^~~~~
%Error: t/t_class_extends_this.v:26:11: Can't find definition of scope/variable: 'super'
26 | if (super.value != 10) $stop;
| ^~~~~
%Error: Exiting due to

View File

@ -11,13 +11,11 @@ if (!$::Driver) { use FindBin; exec("$FindBin::Bin/bootstrap.pl", @ARGV, $0); di
scenarios(simulator => 1);
compile(
fails => $Self->{vlt_all},
expect_filename => $Self->{golden_filename},
);
execute(
check_finished => 1,
) if !$Self->{vlt_all};
);
ok(1);
1;

View File

@ -11,20 +11,27 @@ class Base;
function void test;
if (value != 1) $stop;
if (this.value != 1) $stop;
value = 2;
if (value != 2) $stop;
this.value = 3;
if (value != 3) $stop;
endfunction
endclass
class Cls extends Base;
int value = 2;
int value = 20;
function void test;
if (value != 2) $stop;
if (this.value != 2) $stop;
if (value != 20) $stop;
if (this.value != 20) $stop;
if (super.value != 1) $stop;
super.test();
super.value = 10;
this.value = 20;
if (super.value != 10) $stop;
if (value != 20) $stop;;
if (this.value != 20) $stop;
super.value = 9;
this.value = 29;
if (super.value != 9) $stop;
if (value != 29) $stop;;
endfunction
endclass

View File

@ -0,0 +1,4 @@
%Error: t/t_class_super_bad.v:15:12: 'super' used outside class (IEEE 1800-2017 8.15)
15 | super.addr = 2;
| ^
%Error: Exiting due to

View File

@ -0,0 +1,19 @@
#!/usr/bin/env perl
if (!$::Driver) { use FindBin; exec("$FindBin::Bin/bootstrap.pl", @ARGV, $0); die; }
# DESCRIPTION: Verilator: Verilog Test driver/expect definition
#
# Copyright 2020 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
scenarios(linter => 1);
compile(
fails => 1,
expect_filename => $Self->{golden_filename},
);
ok(1);
1;

View File

@ -0,0 +1,19 @@
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain, for
// any use, without warranty, 2020 Rafal Kapuscik
// SPDX-License-Identifier: CC0-1.0
//
module t(/*AUTOARG*/
// Inputs
clk
);
input clk;
bit [3:0] addr;
initial begin
super.addr = 2;
$write("*-* All Finished *-*\n");
$finish;
end
endmodule

View File

@ -0,0 +1,4 @@
%Error: t/t_class_super_bad2.v:10:12: 'super' used on non-extended class (IEEE 1800-2017 8.15)
10 | super.i = 1;
| ^
%Error: Exiting due to

View File

@ -0,0 +1,19 @@
#!/usr/bin/env perl
if (!$::Driver) { use FindBin; exec("$FindBin::Bin/bootstrap.pl", @ARGV, $0); die; }
# DESCRIPTION: Verilator: Verilog Test driver/expect definition
#
# Copyright 2020 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
scenarios(linter => 1);
compile(
fails => 1,
expect_filename => $Self->{golden_filename},
);
ok(1);
1;

View File

@ -0,0 +1,12 @@
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain, for
// any use, without warranty, 2020 Rafal Kapuscik
// SPDX-License-Identifier: CC0-1.0
//
class Cls;
task t;
super.i = 1; // Bad - no extends
endtask
endclass

View File

@ -1,4 +1,4 @@
%Error: t/t_class_uses_this_bad.v:15:12: 'this' used outside class
%Error: t/t_class_uses_this_bad.v:15:12: 'this' used outside class (IEEE 1800-2017 8.11)
15 | this.addr = 2;
| ^
%Error: Exiting due to

View File

@ -104,6 +104,10 @@ module counter_nansi(clkm, c_data, i_value);
endmodule : counter_nansi
`endif
// Test uses Verilator --top-module, which means this isn't in the hierarchy
// Other simulators will see it, and is illegal to have unconnected interface
`ifdef VERILATOR
module modunused (ifunused ifinunused);
ifunused ifunused();
endmodule
`endif