2012-04-12 21:08:20 -04:00
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2006-08-26 11:35:28 +00:00
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Prevent very deep expressions
|
|
|
|
|
//
|
2019-11-07 22:33:59 -05:00
|
|
|
// Code available from: https://verilator.org
|
2006-08-26 11:35:28 +00:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2022-01-01 08:26:40 -05:00
|
|
|
// Copyright 2003-2022 by Wilson Snyder. This program is free software; you
|
2020-03-21 11:24:24 -04:00
|
|
|
// can redistribute it and/or modify it under the terms of either the GNU
|
2009-05-04 17:07:57 -04:00
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
|
// Version 2.0.
|
2020-03-21 11:24:24 -04:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
|
2006-08-26 11:35:28 +00:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
// V3Depth's Transformations:
|
2008-06-09 21:25:10 -04:00
|
|
|
//
|
2006-08-26 11:35:28 +00:00
|
|
|
// Each module:
|
2019-05-19 16:13:13 -04:00
|
|
|
// 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:
|
2019-05-19 16:13:13 -04:00
|
|
|
// Any statements that need "this" are marked non-static
|
2006-08-26 11:35:28 +00:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
2019-10-04 20:17:11 -04:00
|
|
|
|
2006-12-18 19:20:45 +00:00
|
|
|
#include "config_build.h"
|
|
|
|
|
#include "verilatedos.h"
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
#include "V3Depth.h"
|
2022-08-05 10:56:57 +01:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
#include "V3Ast.h"
|
2022-08-05 10:56:57 +01:00
|
|
|
#include "V3Global.h"
|
2021-08-11 14:30:00 +01:00
|
|
|
#include "V3UniqueNames.h"
|
2006-08-26 11:35:28 +00:00
|
|
|
|
2018-10-14 13:43:24 -04:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
2022-09-18 20:53:42 +01:00
|
|
|
VL_DEFINE_DEBUG_FUNCTIONS;
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
//######################################################################
|
|
|
|
|
|
2022-01-02 13:56:40 -05:00
|
|
|
class DepthVisitor final : public VNVisitor {
|
2006-08-26 11:35:28 +00:00
|
|
|
private:
|
|
|
|
|
// NODE STATE
|
|
|
|
|
|
|
|
|
|
// STATE
|
2020-10-31 08:59:35 -04:00
|
|
|
AstCFunc* m_cfuncp = nullptr; // Current block
|
2022-03-05 20:17:36 -05:00
|
|
|
AstMTaskBody* m_mtaskbodyp = nullptr; // Current mtaskbody
|
2020-08-16 09:55:36 -04:00
|
|
|
AstNode* m_stmtp = nullptr; // Current statement
|
|
|
|
|
int m_depth = 0; // How deep in an expression
|
|
|
|
|
int m_maxdepth = 0; // Maximum depth in an expression
|
2021-08-11 14:30:00 +01:00
|
|
|
V3UniqueNames m_tempNames; // For generating unique temporary variable names
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
// METHODS
|
2009-01-21 16:56:50 -05:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
void createDeepTemp(AstNode* nodep) {
|
2020-04-13 22:51:35 -04:00
|
|
|
UINFO(6, " Deep " << nodep << endl);
|
2020-04-15 07:58:34 -04:00
|
|
|
// if (debug() >= 9) nodep->dumpTree(cout, "deep:");
|
2022-01-02 13:56:40 -05:00
|
|
|
AstVar* const varp = new AstVar{nodep->fileline(), VVarType::STMTTEMP,
|
2021-08-11 14:30:00 +01:00
|
|
|
m_tempNames.get(nodep), nodep->dtypep()};
|
2022-03-05 20:17:36 -05:00
|
|
|
if (m_cfuncp) {
|
|
|
|
|
m_cfuncp->addInitsp(varp);
|
|
|
|
|
} else if (m_mtaskbodyp) {
|
|
|
|
|
m_mtaskbodyp->addStmtsFirstp(varp);
|
|
|
|
|
} else {
|
|
|
|
|
nodep->v3fatalSrc("Deep expression not under a function");
|
|
|
|
|
}
|
2019-05-19 16:13:13 -04:00
|
|
|
// Replace node tree with reference to var
|
2021-07-11 18:42:01 -04:00
|
|
|
AstVarRef* const newp = new AstVarRef{nodep->fileline(), varp, VAccess::READ};
|
2019-05-19 16:13:13 -04:00
|
|
|
nodep->replaceWith(newp);
|
|
|
|
|
// Put assignment before the referencing statement
|
2021-07-11 18:42:01 -04:00
|
|
|
AstAssign* const assp = new AstAssign{
|
|
|
|
|
nodep->fileline(), new AstVarRef{nodep->fileline(), varp, VAccess::WRITE}, nodep};
|
2022-07-13 13:55:40 +01:00
|
|
|
m_stmtp->addHereThisAsNext(assp);
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VISITORS
|
2022-09-16 11:22:11 +01:00
|
|
|
void visit(AstCFunc* nodep) override {
|
2020-10-31 08:59:35 -04:00
|
|
|
VL_RESTORER(m_cfuncp);
|
2022-03-05 20:17:36 -05:00
|
|
|
VL_RESTORER(m_mtaskbodyp);
|
2020-10-31 08:59:35 -04:00
|
|
|
{
|
|
|
|
|
m_cfuncp = nodep;
|
2022-03-05 20:17:36 -05:00
|
|
|
m_mtaskbodyp = nullptr;
|
2020-10-31 08:59:35 -04:00
|
|
|
m_depth = 0;
|
|
|
|
|
m_maxdepth = 0;
|
2021-08-11 14:30:00 +01:00
|
|
|
m_tempNames.reset();
|
2020-10-31 08:59:35 -04:00
|
|
|
iterateChildren(nodep);
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2022-09-16 11:22:11 +01:00
|
|
|
void visit(AstMTaskBody* nodep) override {
|
2022-03-05 20:17:36 -05:00
|
|
|
VL_RESTORER(m_cfuncp);
|
|
|
|
|
VL_RESTORER(m_mtaskbodyp);
|
|
|
|
|
{
|
|
|
|
|
m_cfuncp = nullptr;
|
|
|
|
|
m_mtaskbodyp = nodep;
|
|
|
|
|
m_depth = 0;
|
|
|
|
|
m_maxdepth = 0;
|
|
|
|
|
// We don't reset the names, as must share across tasks
|
|
|
|
|
iterateChildren(nodep);
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-08-30 22:00:55 +00:00
|
|
|
void visitStmt(AstNodeStmt* nodep) {
|
2020-10-31 08:59:35 -04:00
|
|
|
VL_RESTORER(m_stmtp);
|
|
|
|
|
{
|
|
|
|
|
m_stmtp = nodep;
|
|
|
|
|
m_depth = 0;
|
|
|
|
|
m_maxdepth = 0;
|
|
|
|
|
iterateChildren(nodep);
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2022-09-16 11:22:11 +01:00
|
|
|
void visit(AstNodeStmt* nodep) override {
|
2019-01-06 17:38:27 -05:00
|
|
|
if (!nodep->isStatement()) {
|
|
|
|
|
iterateChildren(nodep);
|
|
|
|
|
} else {
|
|
|
|
|
visitStmt(nodep);
|
|
|
|
|
}
|
2006-08-30 22:00:55 +00:00
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
// Operators
|
2022-09-16 11:22:11 +01:00
|
|
|
void visit(AstNodeTermop* nodep) override {}
|
|
|
|
|
void visit(AstNodeMath* nodep) override {
|
2019-05-19 16:13:13 -04:00
|
|
|
// We have some operator defines that use 2 parens, so += 2.
|
2020-12-06 13:49:44 -05:00
|
|
|
{
|
|
|
|
|
VL_RESTORER(m_depth);
|
|
|
|
|
m_depth += 2;
|
|
|
|
|
if (m_depth > m_maxdepth) m_maxdepth = m_depth;
|
|
|
|
|
iterateChildren(nodep);
|
|
|
|
|
}
|
2020-04-13 22:51:35 -04:00
|
|
|
if (m_stmtp && (v3Global.opt.compLimitParens() >= 1) // Else compiler doesn't need it
|
|
|
|
|
&& (m_maxdepth - m_depth) > v3Global.opt.compLimitParens()
|
2018-02-01 21:32:58 -05:00
|
|
|
&& !VN_IS(nodep->backp(), NodeStmt) // Not much point if we're about to use it
|
2020-04-13 22:51:35 -04:00
|
|
|
) {
|
2019-05-19 16:13:13 -04:00
|
|
|
m_maxdepth = m_depth;
|
|
|
|
|
createDeepTemp(nodep);
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
2006-08-30 22:00:55 +00:00
|
|
|
//--------------------
|
|
|
|
|
// Marking of non-static functions (because they might need "this")
|
2019-09-09 07:50:21 -04:00
|
|
|
// (Here instead of new visitor after V3Descope just to avoid another visitor)
|
2006-08-30 22:00:55 +00:00
|
|
|
void needNonStaticFunc(AstNode* nodep) {
|
2020-10-31 08:59:35 -04:00
|
|
|
UASSERT_OBJ(m_cfuncp, nodep, "Non-static accessor not under a function");
|
2021-06-16 13:52:37 +01:00
|
|
|
if (m_cfuncp->isStatic()) {
|
2020-04-13 22:51:35 -04:00
|
|
|
UINFO(5, "Mark non-public due to " << nodep << endl);
|
2020-10-31 08:59:35 -04:00
|
|
|
m_cfuncp->isStatic(false);
|
2019-05-19 16:13:13 -04:00
|
|
|
}
|
2006-08-30 22:00:55 +00:00
|
|
|
}
|
2022-09-16 11:22:11 +01:00
|
|
|
void visit(AstUCFunc* nodep) override {
|
2019-05-19 16:13:13 -04:00
|
|
|
needNonStaticFunc(nodep);
|
2018-05-10 20:55:37 -04:00
|
|
|
iterateChildren(nodep);
|
2006-08-30 22:00:55 +00:00
|
|
|
}
|
2022-09-16 11:22:11 +01:00
|
|
|
void visit(AstUCStmt* nodep) override {
|
2019-05-19 16:13:13 -04:00
|
|
|
needNonStaticFunc(nodep);
|
|
|
|
|
visitStmt(nodep);
|
2006-08-30 22:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
//--------------------
|
|
|
|
|
// Default: Just iterate
|
2022-09-16 11:22:11 +01:00
|
|
|
void visit(AstVar*) override {} // Don't hit varrefs under vars
|
|
|
|
|
void visit(AstNode* nodep) override { iterateChildren(nodep); }
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
public:
|
2019-09-12 07:22:22 -04:00
|
|
|
// CONSTRUCTORS
|
2021-08-11 14:30:00 +01:00
|
|
|
explicit DepthVisitor(AstNetlist* nodep)
|
|
|
|
|
: m_tempNames{"__Vdeeptemp"} {
|
|
|
|
|
iterate(nodep);
|
|
|
|
|
}
|
2022-09-16 11:22:11 +01:00
|
|
|
~DepthVisitor() override = default;
|
2006-08-26 11:35:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Depth class functions
|
|
|
|
|
|
|
|
|
|
void V3Depth::depthAll(AstNetlist* nodep) {
|
2020-04-13 22:51:35 -04:00
|
|
|
UINFO(2, __FUNCTION__ << ": " << endl);
|
2021-11-26 10:52:36 -05:00
|
|
|
{ DepthVisitor{nodep}; } // Destruct before checking
|
2022-09-18 20:53:42 +01:00
|
|
|
V3Global::dumpCheckGlobalTree("depth", 0, dumpTree() >= 6);
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|