Internals: Swap lhs/rhs $cast args.

This commit is contained in:
Wilson Snyder 2020-12-05 17:11:00 -05:00
parent 8582aed66a
commit 9c2785b49b
4 changed files with 21 additions and 15 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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);

View File

@ -3667,7 +3667,7 @@ system_t_call<nodep>: // 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($<fl>1, $1); }
@ -3677,7 +3677,7 @@ system_f_call<nodep>: // IEEE: system_tf_call (as func)
yaD_PLI systemDpiArgsE { $$ = new AstFuncRef($<fl>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<nodep>: // 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