Ignore comments when doing if removal

git-svn-id: file://localhost/svn/verilator/trunk/verilator@758 77ca24e4-aefa-0310-84f0-b9a241c72d87
This commit is contained in:
Wilson Snyder 2006-08-27 15:20:55 +00:00
parent 5105a0f7f5
commit 7758877166

View File

@ -138,6 +138,13 @@ private:
);
}
AstNode* afterComment(AstNode* nodep) {
// Ignore comments, such as to determine if a AstIf is empty.
// nodep may be null, if so return null.
while (nodep && nodep->castComment()) { nodep = nodep->nextp(); }
return nodep;
}
// Extraction checks
bool warnSelect(AstSel* nodep) {
AstNode* basefromp = AstArraySel::baseFromp(nodep->fromp());
@ -190,7 +197,7 @@ private:
AstNodeIf* lowerIfp = nodep->ifsp()->castNodeIf();
if (!lowerIfp || lowerIfp->nextp()) return false;
if (nodep->type() != lowerIfp->type()) return false;
if (lowerIfp->elsesp()) return false;
if (afterComment(lowerIfp->elsesp())) return false;
return true;
}
@ -839,17 +846,18 @@ private:
}
nodep->deleteTree(); nodep=NULL;
}
else if (!nodep->ifsp() && !nodep->elsesp()) {
else if (!afterComment(nodep->ifsp()) && !afterComment(nodep->elsesp())) {
// Empty block, remove it
// Note if we support more C++ then there might be side effects in the condition itself
nodep->unlinkFrBack()->deleteTree(); nodep=NULL;
}
else if (!nodep->ifsp()) {
else if (!afterComment(nodep->ifsp())) {
UINFO(4,"IF({x}) NULL {...} => IF(NOT{x}}: "<<nodep<<endl);
AstNode* condp = nodep->condp();
AstNode* elsesp = nodep->elsesp();
condp->unlinkFrBackWithNext();
elsesp->unlinkFrBackWithNext();
if (nodep->ifsp()) { nodep->ifsp()->unlinkFrBackWithNext()->deleteTree(); } // Must have been comment
nodep->condp(new AstLogNot(condp->fileline(), condp)); // LogNot, as C++ optimization also possible
nodep->addIfsp(elsesp);
}