2012-04-13 03:08:20 +02:00
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2007-04-19 20:20:16 +02:00
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Prevent very deep expressions
|
|
|
|
|
//
|
2019-11-08 04:33:59 +01:00
|
|
|
// Code available from: https://verilator.org
|
2007-04-19 20:20:16 +02:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2025-01-01 14:30:25 +01:00
|
|
|
// Copyright 2003-2025 by Wilson Snyder. This program is free software; you
|
2020-03-21 16:24:24 +01:00
|
|
|
// can 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.
|
2020-03-21 16:24:24 +01:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
|
2007-04-19 20:20:16 +02:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
// V3DepthBlock's Transformations:
|
2008-06-10 03:25:10 +02:00
|
|
|
//
|
2007-04-19 20:20:16 +02:00
|
|
|
// Each module:
|
2019-05-19 22:13:13 +02:00
|
|
|
// For each deep block, create cfunc including that block.
|
2007-04-19 20:20:16 +02:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
2019-10-05 02:17:11 +02:00
|
|
|
|
2023-10-18 12:37:46 +02:00
|
|
|
#include "V3PchAstNoMT.h" // VL_MT_DISABLED_CODE_UNIT
|
|
|
|
|
|
2007-04-19 20:20:16 +02:00
|
|
|
#include "V3DepthBlock.h"
|
2022-08-05 11:56:57 +02:00
|
|
|
|
2007-04-19 20:20:16 +02:00
|
|
|
#include "V3EmitCBase.h"
|
2018-10-14 19:43:24 +02:00
|
|
|
|
2022-09-18 21:53:42 +02:00
|
|
|
VL_DEFINE_DEBUG_FUNCTIONS;
|
|
|
|
|
|
2007-04-19 20:20:16 +02:00
|
|
|
//######################################################################
|
|
|
|
|
|
2022-01-02 19:56:40 +01:00
|
|
|
class DepthBlockVisitor final : public VNVisitor {
|
2007-04-19 20:20:16 +02:00
|
|
|
// NODE STATE
|
|
|
|
|
|
2025-04-27 20:17:24 +02:00
|
|
|
// STATE - for current visit position (use VL_RESTORER)
|
2021-11-26 23:55:36 +01:00
|
|
|
const AstNodeModule* m_modp = nullptr; // Current module
|
|
|
|
|
const AstCFunc* m_cfuncp = nullptr; // Current function
|
2020-08-16 15:55:36 +02:00
|
|
|
int m_depth = 0; // How deep in an expression
|
|
|
|
|
int m_deepNum = 0; // How many functions made
|
2007-04-19 20:20:16 +02:00
|
|
|
|
|
|
|
|
// METHODS
|
2009-01-21 22:56:50 +01:00
|
|
|
|
2022-10-12 11:19:21 +02:00
|
|
|
AstCFunc* createDeepFunc(AstNodeStmt* nodep) {
|
2022-01-02 16:32:35 +01:00
|
|
|
VNRelinker relinkHandle;
|
2019-05-19 22:13:13 +02:00
|
|
|
nodep->unlinkFrBack(&relinkHandle);
|
2021-06-16 18:44:06 +02:00
|
|
|
// Create sub function
|
|
|
|
|
AstScope* const scopep = m_cfuncp->scopep();
|
|
|
|
|
const string name = m_cfuncp->name() + "__deep" + cvtToStr(++m_deepNum);
|
2022-11-19 20:45:33 +01:00
|
|
|
AstCFunc* const funcp = new AstCFunc{nodep->fileline(), name, scopep};
|
2020-10-31 13:59:35 +01:00
|
|
|
funcp->slow(m_cfuncp->slow());
|
2021-06-13 15:33:11 +02:00
|
|
|
funcp->isStatic(m_cfuncp->isStatic());
|
|
|
|
|
funcp->isLoose(m_cfuncp->isLoose());
|
2019-05-19 22:13:13 +02:00
|
|
|
funcp->addStmtsp(nodep);
|
2022-09-15 20:43:56 +02:00
|
|
|
scopep->addBlocksp(funcp);
|
2021-06-16 18:44:06 +02:00
|
|
|
// Call sub function at the point where the body was removed from
|
2022-11-19 20:45:33 +01:00
|
|
|
AstCCall* const callp = new AstCCall{nodep->fileline(), funcp};
|
2022-10-12 11:19:21 +02:00
|
|
|
callp->dtypeSetVoid();
|
2021-06-13 15:33:11 +02:00
|
|
|
if (VN_IS(m_modp, Class)) {
|
2025-08-26 04:05:40 +02:00
|
|
|
funcp->argTypes(EmitCUtil::symClassVar());
|
2021-06-13 15:33:11 +02:00
|
|
|
callp->argTypes("vlSymsp");
|
|
|
|
|
}
|
2025-05-23 02:29:32 +02:00
|
|
|
UINFO(6, " New " << callp);
|
2022-10-12 11:19:21 +02:00
|
|
|
relinkHandle.relink(callp->makeStmt());
|
2021-06-16 18:44:06 +02:00
|
|
|
// Done
|
2019-05-19 22:13:13 +02:00
|
|
|
return funcp;
|
2007-04-19 20:20:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VISITORS
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNodeModule* nodep) override {
|
2025-05-23 02:29:32 +02:00
|
|
|
UINFO(4, " MOD " << nodep);
|
2020-08-25 03:10:43 +02:00
|
|
|
VL_RESTORER(m_modp);
|
2024-11-10 16:51:48 +01:00
|
|
|
m_modp = nodep;
|
|
|
|
|
m_deepNum = 0;
|
|
|
|
|
iterateChildren(nodep);
|
2007-04-19 20:20:16 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstCFunc* nodep) override {
|
2019-05-19 22:13:13 +02:00
|
|
|
// We recurse into this.
|
2020-08-25 03:10:43 +02:00
|
|
|
VL_RESTORER(m_depth);
|
2020-10-31 13:59:35 +01:00
|
|
|
VL_RESTORER(m_cfuncp);
|
2024-11-10 16:51:48 +01:00
|
|
|
m_depth = 0;
|
|
|
|
|
m_cfuncp = nodep;
|
|
|
|
|
iterateChildren(nodep);
|
2007-04-19 20:20:16 +02:00
|
|
|
}
|
2022-10-12 11:19:21 +02:00
|
|
|
void visit(AstStmtExpr* nodep) override {} // Stop recursion after introducing new function
|
2023-03-17 02:40:01 +01:00
|
|
|
void visit(AstJumpBlock*) override {} // Stop recursion as can't break up across a jump
|
2022-10-12 11:19:21 +02:00
|
|
|
void visit(AstNodeStmt* nodep) override {
|
2025-04-27 20:17:24 +02:00
|
|
|
++m_depth;
|
2022-10-12 11:19:21 +02:00
|
|
|
if (m_depth > v3Global.opt.compLimitBlocks()) { // Already done
|
2025-05-23 02:29:32 +02:00
|
|
|
UINFO(4, "DeepBlocks " << m_depth << " " << nodep);
|
2021-11-26 23:55:36 +01:00
|
|
|
const AstNode* const backp = nodep->backp(); // Only for debug
|
2025-08-02 19:44:40 +02:00
|
|
|
UINFOTREE(9, backp, "", "pre ");
|
2021-11-26 23:55:36 +01:00
|
|
|
AstCFunc* const funcp = createDeepFunc(nodep);
|
2018-05-11 02:55:37 +02:00
|
|
|
iterate(funcp);
|
2025-08-02 19:44:40 +02:00
|
|
|
UINFOTREE(9, backp, "", "post");
|
|
|
|
|
UINFOTREE(9, funcp, "", "func");
|
2019-05-19 22:13:13 +02:00
|
|
|
} else {
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
2025-04-27 20:17:24 +02:00
|
|
|
--m_depth;
|
2007-04-19 20:20:16 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-12 11:19:21 +02:00
|
|
|
void visit(AstNodeExpr*) override {} // Accelerate
|
2007-04-19 20:20:16 +02:00
|
|
|
//--------------------
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstVar*) override {} // Don't hit varrefs under vars
|
|
|
|
|
void visit(AstNode* nodep) override { iterateChildren(nodep); }
|
2007-04-19 20:20:16 +02:00
|
|
|
|
|
|
|
|
public:
|
2019-09-12 13:22:22 +02:00
|
|
|
// CONSTRUCTORS
|
2020-08-16 15:55:36 +02:00
|
|
|
explicit DepthBlockVisitor(AstNetlist* nodep) { iterate(nodep); }
|
2022-09-16 12:22:11 +02:00
|
|
|
~DepthBlockVisitor() override = default;
|
2007-04-19 20:20:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// DepthBlock class functions
|
|
|
|
|
|
|
|
|
|
void V3DepthBlock::depthBlockAll(AstNetlist* nodep) {
|
2025-05-23 02:29:32 +02:00
|
|
|
UINFO(2, __FUNCTION__ << ":");
|
2021-11-26 16:52:36 +01:00
|
|
|
{ DepthBlockVisitor{nodep}; } // Destruct before checking
|
2024-01-09 16:35:13 +01:00
|
|
|
V3Global::dumpCheckGlobalTree("deepblock", 0, dumpTreeEitherLevel() >= 3);
|
2007-04-19 20:20:16 +02:00
|
|
|
}
|