From 9c2785b49be8e7e021467b8b200ce755ac1db323 Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Sat, 5 Dec 2020 17:11:00 -0500 Subject: [PATCH] Internals: Swap lhs/rhs $cast args. --- src/V3AstNodes.h | 8 +++++++- src/V3LinkLValue.cpp | 6 +++--- src/V3Width.cpp | 16 ++++++++-------- src/verilog.y | 6 +++--- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/V3AstNodes.h b/src/V3AstNodes.h index b04c2bd65..96f1531c7 100644 --- a/src/V3AstNodes.h +++ b/src/V3AstNodes.h @@ -6025,6 +6025,7 @@ public: virtual bool cleanLhs() const { return true; } virtual bool sizeMattersLhs() const { return false; } AstNode* lhsp() const { return op1p(); } + AstNode* fromp() const { return lhsp(); } void lhsp(AstNode* nodep) { setOp1p(nodep); } virtual AstNodeDType* getChildDTypep() const override { return childDTypep(); } AstNodeDType* childDTypep() const { return VN_CAST(op2p(), NodeDType); } @@ -6036,6 +6037,9 @@ class AstCastDynamic final : public AstNodeBiop { // Task usage of $cast is converted during parse to assert($cast(...)) // Parents: MATH // Children: MATH + // lhsp() is value (we are converting FROM) to match AstCCast etc, this + // is opposite of $cast's order, because the first access is to the + // value reading from. Suggest use fromp()/top() instead of lhsp/rhsp(). public: AstCastDynamic(FileLine* fl, AstNode* lhsp, AstNode* rhsp) : ASTGEN_SUPER(fl, lhsp, rhsp) {} @@ -6046,7 +6050,7 @@ public: virtual AstNode* cloneType(AstNode* lhsp, AstNode* rhsp) override { return new AstCastDynamic(this->fileline(), lhsp, rhsp); } - virtual string emitVerilog() override { return "%f$cast(%r, %l)"; } + virtual string emitVerilog() override { return "%f$cast(%l, %r)"; } // Non-existent filehandle returns EOF virtual string emitC() override { V3ERROR_NA_RETURN(""); } virtual bool cleanOut() const override { return true; } @@ -6056,6 +6060,8 @@ public: virtual bool sizeMattersRhs() const override { return false; } virtual int instrCount() const override { return widthInstrs() * 20; } virtual bool isPure() const override { return true; } + AstNode* fromp() const { return lhsp(); } + AstNode* top() const { return rhsp(); } }; class AstCastParse final : public AstNode { diff --git a/src/V3LinkLValue.cpp b/src/V3LinkLValue.cpp index b1e365f10..1497a9d21 100644 --- a/src/V3LinkLValue.cpp +++ b/src/V3LinkLValue.cpp @@ -79,10 +79,10 @@ private: virtual void visit(AstCastDynamic* nodep) override { VL_RESTORER(m_setRefLvalue); { - m_setRefLvalue = VAccess::WRITE; - iterateAndNextNull(nodep->lhsp()); m_setRefLvalue = VAccess::NOCHANGE; - iterateAndNextNull(nodep->rhsp()); + iterateAndNextNull(nodep->fromp()); + m_setRefLvalue = VAccess::WRITE; + iterateAndNextNull(nodep->top()); } } virtual void visit(AstFOpen* nodep) override { diff --git a/src/V3Width.cpp b/src/V3Width.cpp index 5fcac846d..75244a44d 100644 --- a/src/V3Width.cpp +++ b/src/V3Width.cpp @@ -1652,27 +1652,27 @@ private: virtual void visit(AstCast* nodep) override { nodep->dtypep(iterateEditMoveDTypep(nodep, nodep->subDTypep())); // if (debug()) nodep->dumpTree(cout, " CastPre: "); - userIterateAndNext(nodep->lhsp(), WidthVP(SELF, PRELIM).p()); + userIterateAndNext(nodep->fromp(), WidthVP(SELF, PRELIM).p()); // When more general casts are supported, the cast elimination will be done later. // For now, replace it ASAP, so widthing can propagate easily // The cast may change signing, but we don't know the sign yet. Make it so. - // Note we don't sign lhsp() that would make the algorithm O(n^2) if lots of casting. + // Note we don't sign fromp() that would make the algorithm O(n^2) if lots of casting. AstBasicDType* basicp = nodep->dtypep()->basicp(); UASSERT_OBJ(basicp, nodep, "Unimplemented: Casting non-simple data type"); if (m_vup->prelim()) { - userIterateAndNext(nodep->lhsp(), WidthVP(SELF, PRELIM).p()); + userIterateAndNext(nodep->fromp(), WidthVP(SELF, PRELIM).p()); // When implement more complicated types need to convert childDTypep to // dtypep() not as a child - if (!basicp->isDouble() && !nodep->lhsp()->isDouble()) { - // Note castSized might modify nodep->lhsp() + if (!basicp->isDouble() && !nodep->fromp()->isDouble()) { + // Note castSized might modify nodep->fromp() int width = nodep->dtypep()->width(); - castSized(nodep, nodep->lhsp(), width); + castSized(nodep, nodep->fromp(), width); } else { - iterateCheck(nodep, "value", nodep->lhsp(), SELF, FINAL, nodep->lhsp()->dtypep(), + iterateCheck(nodep, "value", nodep->fromp(), SELF, FINAL, nodep->fromp()->dtypep(), EXTEND_EXP, false); } - AstNode* newp = nodep->lhsp()->unlinkFrBack(); + AstNode* newp = nodep->fromp()->unlinkFrBack(); if (basicp->isDouble() && !newp->isDouble()) { if (newp->isSigned()) { newp = new AstISToRD(nodep->fileline(), newp); diff --git a/src/verilog.y b/src/verilog.y index 2bc8d565a..af9a0e07e 100644 --- a/src/verilog.y +++ b/src/verilog.y @@ -3667,7 +3667,7 @@ system_t_call: // IEEE: system_tf_call (as task) | yD_WRITEMEMH '(' expr ',' idClassSel ',' expr ',' expr ')' { $$ = new AstWriteMem($1, true, $3, $5, $7, $9); } // | yD_CAST '(' expr ',' expr ')' - { $$ = new AstAssert($1, new AstCastDynamic($1, $3, $5), nullptr, nullptr, true); } + { $$ = new AstAssert($1, new AstCastDynamic($1, $5, $3), nullptr, nullptr, true); } // // Any system function as a task | system_f_call_or_t { $$ = new AstSysFuncAsTask($1, $1); } @@ -3677,7 +3677,7 @@ system_f_call: // IEEE: system_tf_call (as func) yaD_PLI systemDpiArgsE { $$ = new AstFuncRef($1, *$1, $2); VN_CAST($$, FuncRef)->pli(true); } // | yD_C '(' cStrList ')' { $$ = (v3Global.opt.ignc() ? nullptr : new AstUCFunc($1,$3)); } - | yD_CAST '(' expr ',' expr ')' { $$ = new AstCastDynamic($1, $3, $5); } + | yD_CAST '(' expr ',' expr ')' { $$ = new AstCastDynamic($1, $5, $3); } | yD_SYSTEM '(' expr ')' { $$ = new AstSystemF($1,$3); } // | system_f_call_or_t { $$ = $1; } @@ -4264,7 +4264,7 @@ expr: // IEEE: part of expression/constant_expression/primary | yCONST__ETC yP_TICK '(' expr ')' { $$ = $4; } // Not linting const presently // // Spec only allows primary with addition of a type reference // // We'll be more general, and later assert LHS was a type. - | ~l~expr yP_TICK '(' expr ')' { $$ = new AstCastParse($2,$4,$1); } + | ~l~expr yP_TICK '(' expr ')' { $$ = new AstCastParse($2, $4, $1); } // // // IEEE: assignment_pattern_expression // // IEEE: streaming_concatenation