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
|
|
|
|
|
//
|
2019-11-08 04:33:59 +01:00
|
|
|
// Code available from: https://verilator.org
|
2006-08-26 13:35:28 +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
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
// V3Depth's Transformations:
|
2008-06-10 03:25:10 +02:00
|
|
|
//
|
2006-08-26 13:35:28 +02:00
|
|
|
// Each module:
|
2019-05-19 22:13:13 +02:00
|
|
|
// 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:
|
2019-05-19 22:13:13 +02:00
|
|
|
// Any statements that need "this" are marked non-static
|
2006-08-26 13:35:28 +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
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
#include "V3Depth.h"
|
2022-08-05 11:56:57 +02:00
|
|
|
|
2021-08-11 15:30:00 +02:00
|
|
|
#include "V3UniqueNames.h"
|
2006-08-26 13:35:28 +02:00
|
|
|
|
2022-09-18 21:53:42 +02:00
|
|
|
VL_DEFINE_DEBUG_FUNCTIONS;
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
//######################################################################
|
|
|
|
|
|
2022-01-02 19:56:40 +01:00
|
|
|
class DepthVisitor final : public VNVisitor {
|
2006-08-26 13:35:28 +02:00
|
|
|
// NODE STATE
|
|
|
|
|
|
2025-04-27 20:17:24 +02:00
|
|
|
// STATE - for current visit position (use VL_RESTORER)
|
2020-10-31 13:59:35 +01:00
|
|
|
AstCFunc* m_cfuncp = nullptr; // Current block
|
2022-03-06 02:17:36 +01:00
|
|
|
AstMTaskBody* m_mtaskbodyp = nullptr; // Current mtaskbody
|
2020-08-16 15:55:36 +02: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 15:30:00 +02:00
|
|
|
V3UniqueNames m_tempNames; // For generating unique temporary variable names
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
// METHODS
|
2009-01-21 22:56:50 +01:00
|
|
|
|
2022-11-13 21:33:11 +01:00
|
|
|
void createDeepTemp(AstNodeExpr* nodep) {
|
2025-05-23 02:29:32 +02:00
|
|
|
UINFO(6, " Deep " << nodep);
|
2025-08-02 19:44:40 +02:00
|
|
|
// UINFOTREE(9, nodep, "", "deep");
|
2022-01-02 19:56:40 +01:00
|
|
|
AstVar* const varp = new AstVar{nodep->fileline(), VVarType::STMTTEMP,
|
2021-08-11 15:30:00 +02:00
|
|
|
m_tempNames.get(nodep), nodep->dtypep()};
|
2022-03-06 02:17:36 +01: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 22:13:13 +02:00
|
|
|
// Replace node tree with reference to var
|
2021-07-12 00:42:01 +02:00
|
|
|
AstVarRef* const newp = new AstVarRef{nodep->fileline(), varp, VAccess::READ};
|
2019-05-19 22:13:13 +02:00
|
|
|
nodep->replaceWith(newp);
|
|
|
|
|
// Put assignment before the referencing statement
|
2021-07-12 00:42:01 +02:00
|
|
|
AstAssign* const assp = new AstAssign{
|
|
|
|
|
nodep->fileline(), new AstVarRef{nodep->fileline(), varp, VAccess::WRITE}, nodep};
|
2022-07-13 14:55:40 +02:00
|
|
|
m_stmtp->addHereThisAsNext(assp);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VISITORS
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstCFunc* nodep) override {
|
2020-10-31 13:59:35 +01:00
|
|
|
VL_RESTORER(m_cfuncp);
|
2022-03-06 02:17:36 +01:00
|
|
|
VL_RESTORER(m_mtaskbodyp);
|
2025-04-27 20:17:24 +02:00
|
|
|
VL_RESTORER(m_depth);
|
|
|
|
|
VL_RESTORER(m_maxdepth);
|
2024-11-10 16:51:48 +01:00
|
|
|
m_cfuncp = nodep;
|
|
|
|
|
m_mtaskbodyp = nullptr;
|
|
|
|
|
m_depth = 0;
|
|
|
|
|
m_maxdepth = 0;
|
|
|
|
|
m_tempNames.reset();
|
|
|
|
|
iterateChildren(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstMTaskBody* nodep) override {
|
2022-03-06 02:17:36 +01:00
|
|
|
VL_RESTORER(m_cfuncp);
|
|
|
|
|
VL_RESTORER(m_mtaskbodyp);
|
2025-04-27 20:17:24 +02:00
|
|
|
VL_RESTORER(m_depth);
|
|
|
|
|
VL_RESTORER(m_maxdepth);
|
2024-11-10 16:51:48 +01:00
|
|
|
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);
|
2022-03-06 02:17:36 +01:00
|
|
|
}
|
2006-08-31 00:00:55 +02:00
|
|
|
void visitStmt(AstNodeStmt* nodep) {
|
2020-10-31 13:59:35 +01:00
|
|
|
VL_RESTORER(m_stmtp);
|
2025-04-27 20:17:24 +02:00
|
|
|
VL_RESTORER(m_depth);
|
|
|
|
|
VL_RESTORER(m_maxdepth);
|
2024-11-10 16:51:48 +01:00
|
|
|
m_stmtp = nodep;
|
|
|
|
|
m_depth = 0;
|
|
|
|
|
m_maxdepth = 0;
|
|
|
|
|
iterateChildren(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2022-10-12 11:19:21 +02:00
|
|
|
void visit(AstNodeStmt* nodep) override { visitStmt(nodep); }
|
2006-08-26 13:35:28 +02:00
|
|
|
// Operators
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNodeTermop* nodep) override {}
|
2022-10-12 11:19:21 +02:00
|
|
|
void visit(AstNodeExpr* nodep) override {
|
2019-05-19 22:13:13 +02:00
|
|
|
// We have some operator defines that use 2 parens, so += 2.
|
2020-12-06 19:49:44 +01:00
|
|
|
{
|
|
|
|
|
VL_RESTORER(m_depth);
|
|
|
|
|
m_depth += 2;
|
|
|
|
|
if (m_depth > m_maxdepth) m_maxdepth = m_depth;
|
|
|
|
|
iterateChildren(nodep);
|
|
|
|
|
}
|
2020-04-14 04:51:35 +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
|
2020-04-14 04:51:35 +02:00
|
|
|
) {
|
2019-05-19 22:13:13 +02:00
|
|
|
m_maxdepth = m_depth;
|
|
|
|
|
createDeepTemp(nodep);
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
2006-08-31 00:00:55 +02:00
|
|
|
//--------------------
|
|
|
|
|
// Marking of non-static functions (because they might need "this")
|
2019-09-09 13:50:21 +02:00
|
|
|
// (Here instead of new visitor after V3Descope just to avoid another visitor)
|
2006-08-31 00:00:55 +02:00
|
|
|
void needNonStaticFunc(AstNode* nodep) {
|
2020-10-31 13:59:35 +01:00
|
|
|
UASSERT_OBJ(m_cfuncp, nodep, "Non-static accessor not under a function");
|
2021-06-16 14:52:37 +02:00
|
|
|
if (m_cfuncp->isStatic()) {
|
2025-05-23 02:29:32 +02:00
|
|
|
UINFO(5, "Mark non-public due to " << nodep);
|
2020-10-31 13:59:35 +01:00
|
|
|
m_cfuncp->isStatic(false);
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
2006-08-31 00:00:55 +02:00
|
|
|
}
|
Internals: Refactor text based Ast constructs (#6280) (#6571)
Remove the large variety of ways raw "text" is represented in the Ast.
Particularly, the only thing that represents a string to be emitted in
the output is AstText.
There are 5 AstNodes that can contain AstText, and V3Emit will throw an
error if an AstText is encountered anywhere else:
- AstCStmt: Internally generated procedural statements involving raw
text.
- AstCStmtUser: This is the old AstUCStmt, renamed so it sorts next to
AstCStmt, as it's largely equivalent. We should never create this
internally unless used to represent user input. It is used for $c,
statements in the input, and for some 'systemc_* blocks.
- AstCExpr: Internally generaged expression involving raw text.
- AstCExprUser: This is the old AstUCFunc, renamed so it sorts next to
AstCExpr. It is largely equivalent, but also has more optimizations
disabled. This should never be created internally, it is only used for
$c expressions in the input.
- AstTextBlock: Use by V3ProtectLib only, to generate the hierarchical
wrappers.
Text "tracking" for indentation is always on for AstCStmt, AstCExpr, and
AstTextBlock, as these are always generated by us, and should always be
well formed.
Tracking is always off for AstCStmtUser and AstCExprUser, as these
contain arbitrary user input that might not be safe to parse for
indentation.
Remove subsequently redundant AstNodeSimpleText and AstNodeText types.
This patch also fixes incorrect indentation in emitted waveform tracing
functions, and makes the output more readable for hier block SV stubs.
With that, all raw text nodes are handled as a proper AstNodeStmt or
AstNodeExpr as required for #6280.
2025-10-21 13:41:29 +02:00
|
|
|
void visit(AstCExprUser* nodep) override {
|
2019-05-19 22:13:13 +02:00
|
|
|
needNonStaticFunc(nodep);
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2006-08-31 00:00:55 +02:00
|
|
|
}
|
Internals: Refactor text based Ast constructs (#6280) (#6571)
Remove the large variety of ways raw "text" is represented in the Ast.
Particularly, the only thing that represents a string to be emitted in
the output is AstText.
There are 5 AstNodes that can contain AstText, and V3Emit will throw an
error if an AstText is encountered anywhere else:
- AstCStmt: Internally generated procedural statements involving raw
text.
- AstCStmtUser: This is the old AstUCStmt, renamed so it sorts next to
AstCStmt, as it's largely equivalent. We should never create this
internally unless used to represent user input. It is used for $c,
statements in the input, and for some 'systemc_* blocks.
- AstCExpr: Internally generaged expression involving raw text.
- AstCExprUser: This is the old AstUCFunc, renamed so it sorts next to
AstCExpr. It is largely equivalent, but also has more optimizations
disabled. This should never be created internally, it is only used for
$c expressions in the input.
- AstTextBlock: Use by V3ProtectLib only, to generate the hierarchical
wrappers.
Text "tracking" for indentation is always on for AstCStmt, AstCExpr, and
AstTextBlock, as these are always generated by us, and should always be
well formed.
Tracking is always off for AstCStmtUser and AstCExprUser, as these
contain arbitrary user input that might not be safe to parse for
indentation.
Remove subsequently redundant AstNodeSimpleText and AstNodeText types.
This patch also fixes incorrect indentation in emitted waveform tracing
functions, and makes the output more readable for hier block SV stubs.
With that, all raw text nodes are handled as a proper AstNodeStmt or
AstNodeExpr as required for #6280.
2025-10-21 13:41:29 +02:00
|
|
|
void visit(AstCStmtUser* nodep) override {
|
2019-05-19 22:13:13 +02:00
|
|
|
needNonStaticFunc(nodep);
|
|
|
|
|
visitStmt(nodep);
|
2006-08-31 00:00:55 +02:00
|
|
|
}
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
//--------------------
|
|
|
|
|
// Default: Just iterate
|
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); }
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
public:
|
2019-09-12 13:22:22 +02:00
|
|
|
// CONSTRUCTORS
|
2021-08-11 15:30:00 +02:00
|
|
|
explicit DepthVisitor(AstNetlist* nodep)
|
|
|
|
|
: m_tempNames{"__Vdeeptemp"} {
|
|
|
|
|
iterate(nodep);
|
|
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
~DepthVisitor() override = default;
|
2006-08-26 13:35:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Depth class functions
|
|
|
|
|
|
|
|
|
|
void V3Depth::depthAll(AstNetlist* nodep) {
|
2025-05-23 02:29:32 +02:00
|
|
|
UINFO(2, __FUNCTION__ << ":");
|
2021-11-26 16:52:36 +01:00
|
|
|
{ DepthVisitor{nodep}; } // Destruct before checking
|
2024-01-09 16:35:13 +01:00
|
|
|
V3Global::dumpCheckGlobalTree("depth", 0, dumpTreeEitherLevel() >= 6);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|