From 97feba68984a3e00117f3dfb1e3463d3979f8a8b Mon Sep 17 00:00:00 2001 From: Ryszard Rozak Date: Tue, 4 Jul 2023 15:11:15 +0200 Subject: [PATCH] Support locator methods with "with" on assoc arrays (#4335) --- include/verilated_types.h | 51 +++++++++++++++++++++++++++++++++ src/V3Width.cpp | 4 ++- test_regress/t/t_assoc_method.v | 22 ++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/include/verilated_types.h b/include/verilated_types.h index 90560bad5..5d5c41db2 100644 --- a/include/verilated_types.h +++ b/include/verilated_types.h @@ -830,6 +830,22 @@ public: } return out; } + template + VlQueue unique(Func with_func) const { + VlQueue out; + T_Key default_key; + using WithType = decltype(with_func(m_map.begin()->first, m_map.begin()->second)); + std::set saw; + for (const auto& i : m_map) { + const auto i_mapped = with_func(default_key, i.second); + const auto it = saw.find(i_mapped); + if (it == saw.end()) { + saw.insert(it, i_mapped); + out.push_back(i.second); + } + } + return out; + } VlQueue unique_index() const { VlQueue out; std::set saw; @@ -843,6 +859,21 @@ public: return out; } template + VlQueue unique_index(Func with_func) const { + VlQueue out; + using WithType = decltype(with_func(m_map.begin()->first, m_map.begin()->second)); + std::set saw; + for (const auto& i : m_map) { + const auto i_mapped = with_func(i.first, i.second); + auto it = saw.find(i_mapped); + if (it == saw.end()) { + saw.insert(it, i_mapped); + out.push_back(i.first); + } + } + return out; + } + template VlQueue find(Func with_func) const { VlQueue out; for (const auto& i : m_map) @@ -903,6 +934,16 @@ public: }); return VlQueue::cons(it->second); } + template + VlQueue min(Func with_func) const { + if (m_map.empty()) return VlQueue(); + const auto it = std::min_element( + m_map.begin(), m_map.end(), + [&with_func](const std::pair& a, const std::pair& b) { + return with_func(a.first, a.second) < with_func(b.first, b.second); + }); + return VlQueue::cons(it->second); + } VlQueue max() const { if (m_map.empty()) return VlQueue(); const auto it = std::max_element( @@ -912,6 +953,16 @@ public: }); return VlQueue::cons(it->second); } + template + VlQueue max(Func with_func) const { + if (m_map.empty()) return VlQueue(); + const auto it = std::max_element( + m_map.begin(), m_map.end(), + [&with_func](const std::pair& a, const std::pair& b) { + return with_func(a.first, a.second) < with_func(b.first, b.second); + }); + return VlQueue::cons(it->second); + } T_Value r_sum() const { T_Value out(0); // Type must have assignment operator diff --git a/src/V3Width.cpp b/src/V3Width.cpp index 68b6126d5..368f2ef29 100644 --- a/src/V3Width.cpp +++ b/src/V3Width.cpp @@ -3219,10 +3219,12 @@ private: if (!nodep->firstAbovep()) newp->dtypeSetVoid(); } else if (nodep->name() == "min" || nodep->name() == "max" || nodep->name() == "unique" || nodep->name() == "unique_index") { + AstWith* const withp = methodWithArgument( + nodep, false, true, nullptr, nodep->findUInt32DType(), adtypep->subDTypep()); methodOkArguments(nodep, 0, 0); methodCallLValueRecurse(nodep, nodep->fromp(), VAccess::READ); newp = new AstCMethodHard{nodep->fileline(), nodep->fromp()->unlinkFrBack(), - nodep->name()}; + nodep->name(), withp}; if (nodep->name() == "unique_index") { newp->dtypep(queueDTypeIndexedBy(adtypep->keyDTypep())); } else { diff --git a/test_regress/t/t_assoc_method.v b/test_regress/t/t_assoc_method.v index 0bb9f707c..5d923470f 100644 --- a/test_regress/t/t_assoc_method.v +++ b/test_regress/t/t_assoc_method.v @@ -9,11 +9,14 @@ `define checks(gotv,expv) do if ((gotv) !== (expv)) begin $write("%%Error: %s:%0d: got='%s' exp='%s'\n", `__FILE__,`__LINE__, (gotv), (expv)); `stop; end while(0); module t (/*AUTOARG*/); + typedef struct { int x, y; } point; initial begin int q[int]; int qe[int]; // Empty int qv[$]; // Value returns int qi[$]; // Index returns + point points_q[int]; + point points_qv[$]; int i; string v; @@ -37,6 +40,16 @@ module t (/*AUTOARG*/); qi = qe.unique_index; v = $sformatf("%p", qi); `checks(v, "'{}"); + points_q[0] = point'{1, 2}; + points_q[1] = point'{2, 4}; + points_q[5] = point'{1, 4}; + + points_qv = points_q.unique(p) with (p.x); + `checkh(points_qv.size, 2); + qi = points_q.unique_index(p) with (p.x + p.y); + qi.sort; + v = $sformatf("%p", qi); `checks(v, "'{'h0, 'h1, 'h5} "); + // These require an with clause or are illegal // TODO add a lint check that with clause is provided qv = q.find with (item == 2); @@ -74,13 +87,22 @@ module t (/*AUTOARG*/); qv = q.min; v = $sformatf("%p", qv); `checks(v, "'{'h1} "); + points_qv = points_q.min(p) with (p.x + p.y); + if (points_qv[0].x != 1 || points_qv[0].y != 2) $stop; + qv = q.max; v = $sformatf("%p", qv); `checks(v, "'{'h4} "); + points_qv = points_q.max(p) with (p.x + p.y); + if (points_qv[0].x != 2 || points_qv[0].y != 4) $stop; qv = qe.min; v = $sformatf("%p", qv); `checks(v, "'{}"); + qv = qe.min(x) with (x + 1); + v = $sformatf("%p", qv); `checks(v, "'{}"); qv = qe.max; v = $sformatf("%p", qv); `checks(v, "'{}"); + qv = qe.max(x) with (x + 1); + v = $sformatf("%p", qv); `checks(v, "'{}"); // Reduction methods