2012-04-13 03:08:20 +02:00
|
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2006-08-26 13:35:28 +02:00
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
// DESCRIPTION: Verilator: Prevent very deep expressions
|
|
|
|
|
|
//
|
2008-04-25 14:14:27 +02:00
|
|
|
|
// Code available from: http://www.veripool.org/verilator
|
2006-08-26 13:35:28 +02:00
|
|
|
|
//
|
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
//
|
2019-01-04 01:17:22 +01:00
|
|
|
|
// Copyright 2003-2019 by Wilson Snyder. This program is free software; you can
|
2006-08-26 13:35:28 +02:00
|
|
|
|
// redistribute it and/or modify it under the terms of either the GNU
|
2009-05-04 23:07:57 +02:00
|
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
|
|
// Version 2.0.
|
2006-08-26 13:35:28 +02:00
|
|
|
|
//
|
|
|
|
|
|
// 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 03:25:10 +02:00
|
|
|
|
//
|
2006-08-26 13:35:28 +02:00
|
|
|
|
// Each module:
|
|
|
|
|
|
// For each wide OP, assign a temporary variable.
|
|
|
|
|
|
// For each deep expression, assign expression to temporary.
|
2006-08-31 00:00:55 +02:00
|
|
|
|
// Each CFunc:
|
|
|
|
|
|
// Any statements that need "this" are marked non-static
|
2006-08-26 13:35:28 +02:00
|
|
|
|
//
|
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
|
2006-12-18 20:20:45 +01:00
|
|
|
|
#include "config_build.h"
|
|
|
|
|
|
#include "verilatedos.h"
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
|
|
#include "V3Global.h"
|
|
|
|
|
|
#include "V3Depth.h"
|
|
|
|
|
|
#include "V3Ast.h"
|
|
|
|
|
|
|
2018-10-14 19:43:24 +02:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
#include <cstdarg>
|
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
|
//######################################################################
|
|
|
|
|
|
|
|
|
|
|
|
class DepthVisitor : public AstNVisitor {
|
|
|
|
|
|
private:
|
|
|
|
|
|
// NODE STATE
|
|
|
|
|
|
|
|
|
|
|
|
// STATE
|
2009-11-07 12:20:20 +01:00
|
|
|
|
AstNodeModule* m_modp; // Current module
|
2006-08-26 13:35:28 +02:00
|
|
|
|
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
|
2018-05-14 12:50:47 +02:00
|
|
|
|
VL_DEBUG_FUNC; // Declare debug()
|
2009-01-21 22:56:50 +01:00
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
|
void createDeepTemp(AstNode* nodep) {
|
|
|
|
|
|
UINFO(6," Deep "<<nodep<<endl);
|
|
|
|
|
|
//if (debug()>=9) nodep->dumpTree(cout,"deep:");
|
|
|
|
|
|
|
2018-10-14 04:02:39 +02:00
|
|
|
|
string newvarname = (string("__Vdeeptemp")+cvtToStr(m_modp->varNumGetInc()));
|
2018-08-25 15:52:45 +02: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....)
|
|
|
|
|
|
// See t_func_crc for an example test that requires this
|
|
|
|
|
|
VFlagLogicPacked(), nodep->width());
|
2006-08-31 00:00:55 +02:00
|
|
|
|
if (!m_funcp) nodep->v3fatalSrc("Deep expression not under a function");
|
2006-08-26 13:35:28 +02:00
|
|
|
|
m_funcp->addInitsp(varp);
|
|
|
|
|
|
// Replace node tree with reference to var
|
2018-08-25 15:52:45 +02:00
|
|
|
|
AstVarRef* newp = new AstVarRef(nodep->fileline(), varp, false);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
nodep->replaceWith(newp);
|
|
|
|
|
|
// Put assignment before the referencing statement
|
2018-08-25 15:52:45 +02:00
|
|
|
|
AstAssign* assp = new AstAssign(nodep->fileline(),
|
|
|
|
|
|
new AstVarRef(nodep->fileline(), varp, true),
|
|
|
|
|
|
nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
AstNRelinker linker2;
|
|
|
|
|
|
m_stmtp->unlinkFrBack(&linker2);
|
|
|
|
|
|
assp->addNext(m_stmtp);
|
|
|
|
|
|
linker2.relink(assp);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// VISITORS
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstNodeModule* nodep) {
|
2006-08-26 13:35:28 +02:00
|
|
|
|
UINFO(4," MOD "<<nodep<<endl);
|
|
|
|
|
|
m_modp = nodep;
|
|
|
|
|
|
m_funcp = NULL;
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterateChildren(nodep);
|
2008-11-12 21:32:09 +01:00
|
|
|
|
m_modp = NULL;
|
2006-08-26 13:35:28 +02:00
|
|
|
|
}
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstCFunc* nodep) {
|
2006-08-26 13:35:28 +02:00
|
|
|
|
m_funcp = nodep;
|
|
|
|
|
|
m_depth = 0;
|
|
|
|
|
|
m_maxdepth = 0;
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterateChildren(nodep);
|
2006-08-31 00:00:55 +02:00
|
|
|
|
m_funcp = NULL;
|
2006-08-26 13:35:28 +02:00
|
|
|
|
}
|
2006-08-31 00:00:55 +02:00
|
|
|
|
void visitStmt(AstNodeStmt* nodep) {
|
2006-08-26 13:35:28 +02:00
|
|
|
|
m_depth = 0;
|
|
|
|
|
|
m_maxdepth = 0;
|
|
|
|
|
|
m_stmtp = nodep;
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterateChildren(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
m_stmtp = NULL;
|
|
|
|
|
|
}
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstNodeStmt* nodep) {
|
2019-01-06 23:38:27 +01:00
|
|
|
|
if (!nodep->isStatement()) {
|
|
|
|
|
|
iterateChildren(nodep);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
visitStmt(nodep);
|
|
|
|
|
|
}
|
2006-08-31 00:00:55 +02:00
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
|
// Operators
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstNodeTermop* nodep) {
|
2006-08-26 13:35:28 +02:00
|
|
|
|
}
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstNodeMath* nodep) {
|
2007-04-19 20:20:16 +02:00
|
|
|
|
// We have some operator defines that use 2 parens, so += 2.
|
|
|
|
|
|
m_depth += 2;
|
2006-08-26 13:35:28 +02:00
|
|
|
|
if (m_depth>m_maxdepth) m_maxdepth=m_depth;
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterateChildren(nodep);
|
2007-04-19 20:20:16 +02:00
|
|
|
|
m_depth -= 2;
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
2007-04-19 20:20:16 +02:00
|
|
|
|
if (m_stmtp
|
|
|
|
|
|
&& (v3Global.opt.compLimitParens() >= 1) // Else compiler doesn't need it
|
|
|
|
|
|
&& (m_maxdepth-m_depth) > v3Global.opt.compLimitParens()
|
2018-02-02 03:32:58 +01:00
|
|
|
|
&& !VN_IS(nodep->backp(), NodeStmt) // Not much point if we're about to use it
|
2006-08-26 13:35:28 +02:00
|
|
|
|
) {
|
|
|
|
|
|
m_maxdepth = m_depth;
|
|
|
|
|
|
createDeepTemp(nodep);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2006-08-31 00:00:55 +02:00
|
|
|
|
//--------------------
|
|
|
|
|
|
// Marking of non-static functions (because they might need "this")
|
2019-01-16 06:38:42 +01:00
|
|
|
|
// (Here instead of new vistor after V3Descope just to avoid another visitor)
|
2006-08-31 00:00:55 +02:00
|
|
|
|
void needNonStaticFunc(AstNode* nodep) {
|
2019-01-16 06:38:42 +01:00
|
|
|
|
if (!m_funcp) nodep->v3fatalSrc("Non-static accessor not under a function");
|
|
|
|
|
|
if (m_funcp->isStatic().trueU()) {
|
2006-08-31 00:00:55 +02:00
|
|
|
|
UINFO(5,"Mark non-public due to "<<nodep<<endl);
|
|
|
|
|
|
m_funcp->isStatic(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstUCFunc* nodep) {
|
2006-08-31 00:00:55 +02:00
|
|
|
|
needNonStaticFunc(nodep);
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterateChildren(nodep);
|
2006-08-31 00:00:55 +02:00
|
|
|
|
}
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstUCStmt* nodep) {
|
2006-08-31 00:00:55 +02:00
|
|
|
|
needNonStaticFunc(nodep);
|
|
|
|
|
|
visitStmt(nodep);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
|
//--------------------
|
|
|
|
|
|
// Default: Just iterate
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstVar* nodep) {} // Don't hit varrefs under vars
|
|
|
|
|
|
virtual void visit(AstNode* nodep) {
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterateChildren(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
// CONSTUCTORS
|
2015-10-04 04:33:06 +02:00
|
|
|
|
explicit DepthVisitor(AstNetlist* nodep) {
|
2006-08-26 13:35:28 +02:00
|
|
|
|
m_modp=NULL;
|
|
|
|
|
|
m_funcp=NULL;
|
|
|
|
|
|
m_stmtp=NULL;
|
|
|
|
|
|
m_depth=0;
|
|
|
|
|
|
m_maxdepth=0;
|
|
|
|
|
|
//
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterate(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
virtual ~DepthVisitor() {}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
|
// Depth class functions
|
|
|
|
|
|
|
|
|
|
|
|
void V3Depth::depthAll(AstNetlist* nodep) {
|
|
|
|
|
|
UINFO(2,__FUNCTION__<<": "<<endl);
|
2018-03-10 18:57:50 +01:00
|
|
|
|
{
|
|
|
|
|
|
DepthVisitor visitor (nodep);
|
|
|
|
|
|
} // Destruct before checking
|
2017-09-18 04:52:57 +02:00
|
|
|
|
V3Global::dumpCheckGlobalTree("depth", 0, v3Global.opt.dumpTreeLevel(__FILE__) >= 6);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
}
|