From a37866ee92f2aa8aeeae8d6e317406b8e9bdb16a Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Thu, 26 Nov 2020 11:06:59 -0500 Subject: [PATCH] Support 'super'. --- Changes | 2 ++ src/V3AstNodes.cpp | 3 +++ src/V3LinkDot.cpp | 34 +++++++++++++++++++++--- test_regress/t/t_class_extends_this.out | 25 ----------------- test_regress/t/t_class_extends_this.pl | 4 +-- test_regress/t/t_class_extends_this.v | 21 ++++++++++----- test_regress/t/t_class_super_bad.out | 4 +++ test_regress/t/t_class_super_bad.pl | 19 +++++++++++++ test_regress/t/t_class_super_bad.v | 19 +++++++++++++ test_regress/t/t_class_super_bad2.out | 4 +++ test_regress/t/t_class_super_bad2.pl | 19 +++++++++++++ test_regress/t/t_class_super_bad2.v | 12 +++++++++ test_regress/t/t_class_uses_this_bad.out | 2 +- test_regress/t/t_interface2.v | 4 +++ 14 files changed, 133 insertions(+), 39 deletions(-) delete mode 100644 test_regress/t/t_class_extends_this.out create mode 100644 test_regress/t/t_class_super_bad.out create mode 100755 test_regress/t/t_class_super_bad.pl create mode 100644 test_regress/t/t_class_super_bad.v create mode 100644 test_regress/t/t_class_super_bad2.out create mode 100755 test_regress/t/t_class_super_bad2.pl create mode 100644 test_regress/t/t_class_super_bad2.v diff --git a/Changes b/Changes index b0f62fc5a..023477ec9 100644 --- a/Changes +++ b/Changes @@ -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). diff --git a/src/V3AstNodes.cpp b/src/V3AstNodes.cpp index dac9ca69e..9978297e5 100644 --- a/src/V3AstNodes.cpp +++ b/src/V3AstNodes.cpp @@ -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(); } diff --git a/src/V3LinkDot.cpp b/src/V3LinkDot.cpp index 45c949149..1c05ae13c 100644 --- a/src/V3LinkDot.cpp +++ b/src/V3LinkDot.cpp @@ -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 diff --git a/test_regress/t/t_class_extends_this.out b/test_regress/t/t_class_extends_this.out deleted file mode 100644 index 87bd383a9..000000000 --- a/test_regress/t/t_class_extends_this.out +++ /dev/null @@ -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 diff --git a/test_regress/t/t_class_extends_this.pl b/test_regress/t/t_class_extends_this.pl index 2ad4a887d..aabcde63e 100755 --- a/test_regress/t/t_class_extends_this.pl +++ b/test_regress/t/t_class_extends_this.pl @@ -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; diff --git a/test_regress/t/t_class_extends_this.v b/test_regress/t/t_class_extends_this.v index 4f72270e9..fb185267d 100644 --- a/test_regress/t/t_class_extends_this.v +++ b/test_regress/t/t_class_extends_this.v @@ -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 diff --git a/test_regress/t/t_class_super_bad.out b/test_regress/t/t_class_super_bad.out new file mode 100644 index 000000000..9927708ff --- /dev/null +++ b/test_regress/t/t_class_super_bad.out @@ -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 diff --git a/test_regress/t/t_class_super_bad.pl b/test_regress/t/t_class_super_bad.pl new file mode 100755 index 000000000..45af9e7a6 --- /dev/null +++ b/test_regress/t/t_class_super_bad.pl @@ -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; diff --git a/test_regress/t/t_class_super_bad.v b/test_regress/t/t_class_super_bad.v new file mode 100644 index 000000000..20a229969 --- /dev/null +++ b/test_regress/t/t_class_super_bad.v @@ -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 diff --git a/test_regress/t/t_class_super_bad2.out b/test_regress/t/t_class_super_bad2.out new file mode 100644 index 000000000..b3b195c17 --- /dev/null +++ b/test_regress/t/t_class_super_bad2.out @@ -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 diff --git a/test_regress/t/t_class_super_bad2.pl b/test_regress/t/t_class_super_bad2.pl new file mode 100755 index 000000000..45af9e7a6 --- /dev/null +++ b/test_regress/t/t_class_super_bad2.pl @@ -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; diff --git a/test_regress/t/t_class_super_bad2.v b/test_regress/t/t_class_super_bad2.v new file mode 100644 index 000000000..ba31d657b --- /dev/null +++ b/test_regress/t/t_class_super_bad2.v @@ -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 diff --git a/test_regress/t/t_class_uses_this_bad.out b/test_regress/t/t_class_uses_this_bad.out index 05075f46a..0b2fd0a3c 100644 --- a/test_regress/t/t_class_uses_this_bad.out +++ b/test_regress/t/t_class_uses_this_bad.out @@ -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 diff --git a/test_regress/t/t_interface2.v b/test_regress/t/t_interface2.v index caa6c1591..799e7ec13 100644 --- a/test_regress/t/t_interface2.v +++ b/test_regress/t/t_interface2.v @@ -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