2012-04-13 01:08:20 +00:00
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2007-04-19 18:20:16 +00:00
|
|
|
//*************************************************************************
|
|
|
|
// DESCRIPTION: Verilator: Prevent very deep expressions
|
|
|
|
//
|
2019-11-08 03:33:59 +00:00
|
|
|
// Code available from: https://verilator.org
|
2007-04-19 18:20:16 +00:00
|
|
|
//
|
|
|
|
//*************************************************************************
|
|
|
|
//
|
2020-03-21 15:24:24 +00:00
|
|
|
// Copyright 2003-2020 by Wilson Snyder. This program is free software; you
|
|
|
|
// can redistribute it and/or modify it under the terms of either the GNU
|
2009-05-04 21:07:57 +00:00
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
// Version 2.0.
|
2020-03-21 15:24:24 +00:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
|
2007-04-19 18:20:16 +00:00
|
|
|
//
|
|
|
|
//*************************************************************************
|
|
|
|
// V3DepthBlock's Transformations:
|
2008-06-10 01:25:10 +00:00
|
|
|
//
|
2007-04-19 18:20:16 +00:00
|
|
|
// Each module:
|
2019-05-19 20:13:13 +00:00
|
|
|
// For each deep block, create cfunc including that block.
|
2007-04-19 18:20:16 +00:00
|
|
|
//
|
|
|
|
//*************************************************************************
|
2019-10-05 00:17:11 +00:00
|
|
|
|
2007-04-19 18:20:16 +00:00
|
|
|
#include "config_build.h"
|
|
|
|
#include "verilatedos.h"
|
|
|
|
|
|
|
|
#include "V3Global.h"
|
|
|
|
#include "V3DepthBlock.h"
|
|
|
|
#include "V3Ast.h"
|
|
|
|
#include "V3EmitCBase.h"
|
|
|
|
|
2018-10-14 17:43:24 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2007-04-19 18:20:16 +00:00
|
|
|
//######################################################################
|
|
|
|
|
|
|
|
class DepthBlockVisitor : public AstNVisitor {
|
|
|
|
private:
|
|
|
|
// NODE STATE
|
|
|
|
|
|
|
|
// STATE
|
2020-08-16 13:55:36 +00:00
|
|
|
AstNodeModule* m_modp = nullptr; // Current module
|
2020-10-31 12:59:35 +00:00
|
|
|
AstCFunc* m_cfuncp = nullptr; // Current function
|
2020-08-16 13:55:36 +00:00
|
|
|
int m_depth = 0; // How deep in an expression
|
|
|
|
int m_deepNum = 0; // How many functions made
|
2007-04-19 18:20:16 +00:00
|
|
|
|
|
|
|
// METHODS
|
2018-05-14 10:50:47 +00:00
|
|
|
VL_DEBUG_FUNC; // Declare debug()
|
2009-01-21 21:56:50 +00:00
|
|
|
|
2007-04-19 18:20:16 +00:00
|
|
|
AstCFunc* createDeepFunc(AstNode* nodep) {
|
2019-05-19 20:13:13 +00:00
|
|
|
AstNRelinker relinkHandle;
|
|
|
|
nodep->unlinkFrBack(&relinkHandle);
|
|
|
|
// Create function
|
2020-10-31 12:59:35 +00:00
|
|
|
string name = m_cfuncp->name() + "__deep" + cvtToStr(++m_deepNum);
|
2020-08-15 14:12:55 +00:00
|
|
|
AstCFunc* funcp = new AstCFunc(nodep->fileline(), name, nullptr);
|
2019-05-19 20:13:13 +00:00
|
|
|
funcp->argTypes(EmitCBaseVisitor::symClassVar());
|
|
|
|
funcp->symProlog(true);
|
2020-10-31 12:59:35 +00:00
|
|
|
funcp->slow(m_cfuncp->slow());
|
2019-05-19 20:13:13 +00:00
|
|
|
funcp->addStmtsp(nodep);
|
|
|
|
m_modp->addStmtp(funcp);
|
|
|
|
// Call it at the point where the body was removed from
|
|
|
|
AstCCall* callp = new AstCCall(nodep->fileline(), funcp);
|
|
|
|
callp->argTypes("vlSymsp");
|
2020-04-14 02:51:35 +00:00
|
|
|
UINFO(6, " New " << callp << endl);
|
2019-05-19 20:13:13 +00:00
|
|
|
//
|
|
|
|
relinkHandle.relink(callp);
|
|
|
|
return funcp;
|
2007-04-19 18:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// VISITORS
|
2020-08-15 14:03:34 +00:00
|
|
|
virtual void visit(AstNodeModule* nodep) override {
|
2020-04-14 02:51:35 +00:00
|
|
|
UINFO(4, " MOD " << nodep << endl);
|
2020-08-25 01:10:43 +00:00
|
|
|
VL_RESTORER(m_modp);
|
2020-01-20 18:27:27 +00:00
|
|
|
{
|
|
|
|
m_modp = nodep;
|
|
|
|
m_deepNum = 0;
|
|
|
|
iterateChildren(nodep);
|
|
|
|
}
|
2007-04-19 18:20:16 +00:00
|
|
|
}
|
2020-08-15 14:03:34 +00:00
|
|
|
virtual void visit(AstCFunc* nodep) override {
|
2019-05-19 20:13:13 +00:00
|
|
|
// We recurse into this.
|
2020-08-25 01:10:43 +00:00
|
|
|
VL_RESTORER(m_depth);
|
2020-10-31 12:59:35 +00:00
|
|
|
VL_RESTORER(m_cfuncp);
|
2019-05-19 20:13:13 +00:00
|
|
|
{
|
|
|
|
m_depth = 0;
|
2020-10-31 12:59:35 +00:00
|
|
|
m_cfuncp = nodep;
|
2018-05-11 00:55:37 +00:00
|
|
|
iterateChildren(nodep);
|
2019-05-19 20:13:13 +00:00
|
|
|
}
|
2007-04-19 18:20:16 +00:00
|
|
|
}
|
|
|
|
void visitStmt(AstNodeStmt* nodep) {
|
2019-05-19 20:13:13 +00:00
|
|
|
m_depth++;
|
|
|
|
if (m_depth > v3Global.opt.compLimitBlocks()
|
2020-03-07 17:52:11 +00:00
|
|
|
&& !VN_IS(nodep, NodeCCall)) { // Already done
|
2020-04-14 02:51:35 +00:00
|
|
|
UINFO(4, "DeepBlocks " << m_depth << " " << nodep << endl);
|
2019-05-19 20:13:13 +00:00
|
|
|
AstNode* backp = nodep->backp(); // Only for debug
|
2020-04-14 02:51:35 +00:00
|
|
|
if (debug() >= 9) backp->dumpTree(cout, "- pre : ");
|
2019-05-19 20:13:13 +00:00
|
|
|
AstCFunc* funcp = createDeepFunc(nodep);
|
2018-05-11 00:55:37 +00:00
|
|
|
iterate(funcp);
|
2020-04-14 02:51:35 +00:00
|
|
|
if (debug() >= 9) backp->dumpTree(cout, "- post: ");
|
|
|
|
if (debug() >= 9) funcp->dumpTree(cout, "- func: ");
|
2019-05-19 20:13:13 +00:00
|
|
|
} else {
|
2018-05-11 00:55:37 +00:00
|
|
|
iterateChildren(nodep);
|
2019-05-19 20:13:13 +00:00
|
|
|
}
|
|
|
|
m_depth--;
|
2007-04-19 18:20:16 +00:00
|
|
|
}
|
2020-08-15 14:03:34 +00:00
|
|
|
virtual void visit(AstNodeStmt* nodep) override {
|
2019-01-06 22:38:27 +00:00
|
|
|
if (!nodep->isStatement()) {
|
|
|
|
iterateChildren(nodep);
|
|
|
|
} else {
|
|
|
|
visitStmt(nodep);
|
|
|
|
}
|
2007-04-19 18:20:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 14:03:34 +00:00
|
|
|
virtual void visit(AstNodeMath*) override {} // Accelerate
|
2007-04-19 18:20:16 +00:00
|
|
|
//--------------------
|
2020-08-15 14:03:34 +00:00
|
|
|
virtual void visit(AstVar*) override {} // Don't hit varrefs under vars
|
|
|
|
virtual void visit(AstNode* nodep) override { iterateChildren(nodep); }
|
2007-04-19 18:20:16 +00:00
|
|
|
|
|
|
|
public:
|
2019-09-12 11:22:22 +00:00
|
|
|
// CONSTRUCTORS
|
2020-08-16 13:55:36 +00:00
|
|
|
explicit DepthBlockVisitor(AstNetlist* nodep) { iterate(nodep); }
|
2020-08-15 15:44:10 +00:00
|
|
|
virtual ~DepthBlockVisitor() override {}
|
2007-04-19 18:20:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
// DepthBlock class functions
|
|
|
|
|
|
|
|
void V3DepthBlock::depthBlockAll(AstNetlist* nodep) {
|
2020-04-14 02:51:35 +00:00
|
|
|
UINFO(2, __FUNCTION__ << ": " << endl);
|
|
|
|
{ DepthBlockVisitor visitor(nodep); } // Destruct before checking
|
2017-09-18 02:52:57 +00:00
|
|
|
V3Global::dumpCheckGlobalTree("deepblock", 0, v3Global.opt.dumpTreeLevel(__FILE__) >= 3);
|
2007-04-19 18:20:16 +00:00
|
|
|
}
|