2006-08-26 11:35:28 +00:00
|
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Prevent very deep expressions
|
|
|
|
|
//
|
2008-04-25 12:14:27 +00:00
|
|
|
|
// Code available from: http://www.veripool.org/verilator
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
|
|
|
|
// AUTHORS: Wilson Snyder with Paul Wasson, Duane Gabli
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2009-01-02 16:47:39 +00:00
|
|
|
|
// Copyright 2003-2009 by Wilson Snyder. This program is free software; you can
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// redistribute it and/or modify it under the terms of either the GNU
|
|
|
|
|
// General Public License or the Perl Artistic License.
|
|
|
|
|
//
|
|
|
|
|
// Verilator is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
// V3Depth's Transformations:
|
2008-06-10 01:25:10 +00:00
|
|
|
|
//
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// Each module:
|
|
|
|
|
// For each wide OP, assign a temporary variable.
|
|
|
|
|
// For each deep expression, assign expression to temporary.
|
2006-08-30 22:00:55 +00:00
|
|
|
|
// Each CFunc:
|
|
|
|
|
// Any statements that need "this" are marked non-static
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
2006-12-18 19:20:45 +00:00
|
|
|
|
#include "config_build.h"
|
|
|
|
|
#include "verilatedos.h"
|
2008-06-30 17:11:25 +00:00
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdarg>
|
2006-08-26 11:35:28 +00:00
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
#include "V3Global.h"
|
|
|
|
|
#include "V3Depth.h"
|
|
|
|
|
#include "V3Ast.h"
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
|
|
|
|
|
class DepthVisitor : public AstNVisitor {
|
|
|
|
|
private:
|
|
|
|
|
// NODE STATE
|
|
|
|
|
|
|
|
|
|
// STATE
|
|
|
|
|
AstModule* m_modp; // Current module
|
|
|
|
|
AstCFunc* m_funcp; // Current block
|
|
|
|
|
AstNode* m_stmtp; // Current statement
|
|
|
|
|
int m_depth; // How deep in an expression
|
|
|
|
|
int m_maxdepth; // Maximum depth in an expression
|
|
|
|
|
|
|
|
|
|
// METHODS
|
2009-01-21 21:56:50 +00:00
|
|
|
|
static int debug() {
|
|
|
|
|
static int level = -1;
|
|
|
|
|
if (VL_UNLIKELY(level < 0)) level = v3Global.opt.debugSrcLevel(__FILE__);
|
|
|
|
|
return level;
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
void createDeepTemp(AstNode* nodep) {
|
|
|
|
|
UINFO(6," Deep "<<nodep<<endl);
|
|
|
|
|
//if (debug()>=9) nodep->dumpTree(cout,"deep:");
|
|
|
|
|
|
2008-11-18 02:02:10 +00:00
|
|
|
|
string newvarname = ((string)"__Vdeeptemp"+cvtToStr(m_modp->varNumGetInc()));
|
2006-08-26 11:35:28 +00:00
|
|
|
|
AstVar* varp = new AstVar (nodep->fileline(), AstVarType::STMTTEMP, newvarname,
|
|
|
|
|
// Width, not widthMin, as we may be in middle of BITSEL expression which
|
|
|
|
|
// though it's one bit wide, needs the mask in the upper bits.
|
|
|
|
|
// (Someday we'll have a valid bitmask instead of widths....)
|
|
|
|
|
new AstRange(nodep->fileline(), nodep->width()-1, 0));
|
2006-08-30 22:00:55 +00:00
|
|
|
|
if (!m_funcp) nodep->v3fatalSrc("Deep expression not under a function");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_funcp->addInitsp(varp);
|
|
|
|
|
// Replace node tree with reference to var
|
|
|
|
|
AstVarRef* newp = new AstVarRef (nodep->fileline(), varp, false);
|
|
|
|
|
nodep->replaceWith(newp);
|
|
|
|
|
// Put assignment before the referencing statement
|
|
|
|
|
AstAssign* assp = new AstAssign (nodep->fileline(),
|
|
|
|
|
new AstVarRef(nodep->fileline(), varp, true),
|
|
|
|
|
nodep);
|
|
|
|
|
AstNRelinker linker2;
|
|
|
|
|
m_stmtp->unlinkFrBack(&linker2);
|
|
|
|
|
assp->addNext(m_stmtp);
|
|
|
|
|
linker2.relink(assp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VISITORS
|
|
|
|
|
virtual void visit(AstModule* nodep, AstNUser*) {
|
|
|
|
|
UINFO(4," MOD "<<nodep<<endl);
|
|
|
|
|
m_modp = nodep;
|
|
|
|
|
m_funcp = NULL;
|
|
|
|
|
nodep->iterateChildren(*this);
|
2008-11-12 20:32:09 +00:00
|
|
|
|
m_modp = NULL;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstCFunc* nodep, AstNUser*) {
|
|
|
|
|
m_funcp = nodep;
|
|
|
|
|
m_depth = 0;
|
|
|
|
|
m_maxdepth = 0;
|
|
|
|
|
nodep->iterateChildren(*this);
|
2006-08-30 22:00:55 +00:00
|
|
|
|
m_funcp = NULL;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
2006-08-30 22:00:55 +00:00
|
|
|
|
void visitStmt(AstNodeStmt* nodep) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_depth = 0;
|
|
|
|
|
m_maxdepth = 0;
|
|
|
|
|
m_stmtp = nodep;
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
m_stmtp = NULL;
|
|
|
|
|
}
|
2006-08-30 22:00:55 +00:00
|
|
|
|
virtual void visit(AstNodeStmt* nodep, AstNUser*) {
|
|
|
|
|
visitStmt(nodep);
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// Operators
|
|
|
|
|
virtual void visit(AstNodeTermop* nodep, AstNUser*) {
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstNodeMath* nodep, AstNUser*) {
|
2007-04-19 18:20:16 +00:00
|
|
|
|
// We have some operator defines that use 2 parens, so += 2.
|
|
|
|
|
m_depth += 2;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (m_depth>m_maxdepth) m_maxdepth=m_depth;
|
2008-06-10 01:25:10 +00:00
|
|
|
|
nodep->iterateChildren(*this);
|
2007-04-19 18:20:16 +00:00
|
|
|
|
m_depth -= 2;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
2007-04-19 18:20:16 +00:00
|
|
|
|
if (m_stmtp
|
|
|
|
|
&& (v3Global.opt.compLimitParens() >= 1) // Else compiler doesn't need it
|
|
|
|
|
&& (m_maxdepth-m_depth) > v3Global.opt.compLimitParens()
|
2006-08-26 11:35:28 +00:00
|
|
|
|
&& !nodep->backp()->castNodeStmt() // Not much point if we're about to use it
|
|
|
|
|
) {
|
|
|
|
|
m_maxdepth = m_depth;
|
|
|
|
|
createDeepTemp(nodep);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-30 22:00:55 +00:00
|
|
|
|
//--------------------
|
|
|
|
|
// Marking of non-static functions (because they might need "this")
|
|
|
|
|
void needNonStaticFunc(AstNode* nodep) {
|
|
|
|
|
if (!m_funcp) nodep->v3fatalSrc("Non-static accessor not under a function");
|
|
|
|
|
if (m_funcp->isStatic()) {
|
|
|
|
|
UINFO(5,"Mark non-public due to "<<nodep<<endl);
|
|
|
|
|
m_funcp->isStatic(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstUCFunc* nodep, AstNUser*) {
|
|
|
|
|
needNonStaticFunc(nodep);
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstUCStmt* nodep, AstNUser*) {
|
|
|
|
|
needNonStaticFunc(nodep);
|
|
|
|
|
visitStmt(nodep);
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//--------------------
|
|
|
|
|
// Default: Just iterate
|
|
|
|
|
virtual void visit(AstVar* nodep, AstNUser*) {} // Don't hit varrefs under vars
|
|
|
|
|
virtual void visit(AstNode* nodep, AstNUser*) {
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// CONSTUCTORS
|
|
|
|
|
DepthVisitor(AstNode* nodep) {
|
|
|
|
|
m_modp=NULL;
|
|
|
|
|
m_funcp=NULL;
|
|
|
|
|
m_stmtp=NULL;
|
|
|
|
|
m_depth=0;
|
|
|
|
|
m_maxdepth=0;
|
|
|
|
|
//
|
|
|
|
|
nodep->accept(*this);
|
|
|
|
|
}
|
|
|
|
|
virtual ~DepthVisitor() {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Depth class functions
|
|
|
|
|
|
|
|
|
|
void V3Depth::depthAll(AstNetlist* nodep) {
|
|
|
|
|
UINFO(2,__FUNCTION__<<": "<<endl);
|
|
|
|
|
DepthVisitor visitor (nodep);
|
|
|
|
|
}
|