2020-05-28 22:08:15 +00:00
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
|
|
|
//*************************************************************************
|
|
|
|
// DESCRIPTION: Verilator: Replace increments/decrements with new variables
|
|
|
|
//
|
|
|
|
// Code available from: https://verilator.org
|
|
|
|
//
|
|
|
|
//*************************************************************************
|
|
|
|
//
|
2023-01-01 15:18:39 +00:00
|
|
|
// Copyright 2003-2023 by Wilson Snyder. This program is free software; you
|
2020-05-28 22:08:15 +00:00
|
|
|
// can redistribute it and/or modify it under the terms of either the GNU
|
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
// Version 2.0.
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
|
|
|
|
//
|
|
|
|
//*************************************************************************
|
|
|
|
// V3LinkInc's Transformations:
|
|
|
|
//
|
2022-12-23 21:17:08 +00:00
|
|
|
// prepost_expr_visit
|
2020-05-28 22:08:15 +00:00
|
|
|
// PREADD/PRESUB
|
|
|
|
// Create a temporary __VIncrementX variable, assign the value of
|
|
|
|
// the current variable value to it, substitute the current
|
|
|
|
// variable with the temporary one in the statement.
|
|
|
|
// Increment/decrement the original variable with by the given
|
|
|
|
// value.
|
|
|
|
// POSTADD/POSTSUB
|
|
|
|
// Increment/decrement the current variable by the given value.
|
|
|
|
// Create a temporary __VIncrementX variable, assign the value of
|
|
|
|
// of the current variable (after the operation) to it. Substitute
|
|
|
|
// The original variable with the temporary one in the statement.
|
2022-12-23 21:17:08 +00:00
|
|
|
// prepost_stmt_visit
|
2020-05-28 22:08:15 +00:00
|
|
|
// PREADD/PRESUB/POSTADD/POSTSUB
|
|
|
|
// Increment/decrement the current variable by the given value.
|
|
|
|
// The order (pre/post) doesn't matter outside statements thus
|
|
|
|
// the pre/post operations are treated equally and there is no
|
|
|
|
// need for a temporary variable.
|
|
|
|
//
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
|
|
#include "config_build.h"
|
|
|
|
#include "verilatedos.h"
|
|
|
|
|
|
|
|
#include "V3LinkInc.h"
|
2022-08-05 09:56:57 +00:00
|
|
|
|
2020-05-28 22:08:15 +00:00
|
|
|
#include "V3Ast.h"
|
2022-08-05 09:56:57 +00:00
|
|
|
#include "V3Global.h"
|
2020-05-28 22:08:15 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2022-09-18 19:53:42 +00:00
|
|
|
VL_DEFINE_DEBUG_FUNCTIONS;
|
|
|
|
|
2020-05-28 22:08:15 +00:00
|
|
|
//######################################################################
|
|
|
|
|
2022-01-02 18:56:40 +00:00
|
|
|
class LinkIncVisitor final : public VNVisitor {
|
2020-05-28 22:08:15 +00:00
|
|
|
private:
|
|
|
|
// TYPES
|
2020-08-16 16:05:35 +00:00
|
|
|
enum InsertMode : uint8_t {
|
2020-05-28 22:08:15 +00:00
|
|
|
IM_BEFORE, // Pointing at statement ref is in, insert before this
|
|
|
|
IM_AFTER, // Pointing at last inserted stmt, insert after
|
|
|
|
IM_WHILE_PRECOND // Pointing to for loop, add to body end
|
|
|
|
};
|
|
|
|
|
|
|
|
// STATE
|
2022-10-19 00:04:09 +00:00
|
|
|
AstNodeFTask* m_ftaskp = nullptr; // Function or task we're inside
|
2020-08-16 13:55:36 +00:00
|
|
|
int m_modIncrementsNum = 0; // Var name counter
|
|
|
|
InsertMode m_insMode = IM_BEFORE; // How to insert
|
|
|
|
AstNode* m_insStmtp = nullptr; // Where to insert statement
|
|
|
|
bool m_unsupportedHere = false; // Used to detect where it's not supported yet
|
2020-05-28 22:08:15 +00:00
|
|
|
|
2022-03-11 12:34:11 +00:00
|
|
|
// METHODS
|
2020-05-28 22:08:15 +00:00
|
|
|
void insertBeforeStmt(AstNode* nodep, AstNode* newp) {
|
|
|
|
// Return node that must be visited, if any
|
|
|
|
// See also AstNode::addBeforeStmt; this predates that function
|
2022-11-27 13:31:22 +00:00
|
|
|
if (debug() >= 9) newp->dumpTree("- newstmt: ");
|
2020-05-28 22:08:15 +00:00
|
|
|
UASSERT_OBJ(m_insStmtp, nodep, "Function not underneath a statement");
|
|
|
|
if (m_insMode == IM_BEFORE) {
|
|
|
|
// Add the whole thing before insertAt
|
2022-11-27 13:31:22 +00:00
|
|
|
if (debug() >= 9) newp->dumpTree("- newfunc: ");
|
2020-05-28 22:08:15 +00:00
|
|
|
m_insStmtp->addHereThisAsNext(newp);
|
|
|
|
} else if (m_insMode == IM_AFTER) {
|
|
|
|
m_insStmtp->addNextHere(newp);
|
|
|
|
} else if (m_insMode == IM_WHILE_PRECOND) {
|
2021-11-13 18:50:44 +00:00
|
|
|
AstWhile* const whilep = VN_AS(m_insStmtp, While);
|
2020-05-28 22:08:15 +00:00
|
|
|
UASSERT_OBJ(whilep, nodep, "Insert should be under WHILE");
|
|
|
|
whilep->addPrecondsp(newp);
|
|
|
|
} else {
|
|
|
|
nodep->v3fatalSrc("Unknown InsertMode");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// VISITORS
|
2022-09-16 10:22:11 +00:00
|
|
|
void visit(AstNodeModule* nodep) override {
|
2022-03-11 12:34:11 +00:00
|
|
|
VL_RESTORER(m_modIncrementsNum);
|
2020-05-28 22:08:15 +00:00
|
|
|
m_modIncrementsNum = 0;
|
|
|
|
iterateChildren(nodep);
|
|
|
|
}
|
2022-10-19 00:04:09 +00:00
|
|
|
void visit(AstNodeFTask* nodep) override {
|
|
|
|
VL_RESTORER(m_ftaskp);
|
|
|
|
m_ftaskp = nodep;
|
|
|
|
iterateChildren(nodep);
|
|
|
|
}
|
2022-09-16 10:22:11 +00:00
|
|
|
void visit(AstWhile* nodep) override {
|
2020-05-28 22:08:15 +00:00
|
|
|
// Special, as statements need to be put in different places
|
|
|
|
// Preconditions insert first just before themselves (the normal
|
|
|
|
// rule for other statement types)
|
2020-08-15 14:12:55 +00:00
|
|
|
m_insStmtp = nullptr; // First thing should be new statement
|
2020-05-28 22:08:15 +00:00
|
|
|
iterateAndNextNull(nodep->precondsp());
|
|
|
|
// Conditions insert first at end of precondsp.
|
|
|
|
m_insMode = IM_WHILE_PRECOND;
|
|
|
|
m_insStmtp = nodep;
|
|
|
|
iterateAndNextNull(nodep->condp());
|
|
|
|
// Body insert just before themselves
|
2020-08-15 14:12:55 +00:00
|
|
|
m_insStmtp = nullptr; // First thing should be new statement
|
2022-09-15 18:43:56 +00:00
|
|
|
iterateAndNextNull(nodep->stmtsp());
|
2020-05-28 22:08:15 +00:00
|
|
|
iterateAndNextNull(nodep->incsp());
|
|
|
|
// Done the loop
|
2020-08-15 14:12:55 +00:00
|
|
|
m_insStmtp = nullptr; // Next thing should be new statement
|
2020-05-28 22:08:15 +00:00
|
|
|
}
|
2022-09-16 10:22:11 +00:00
|
|
|
void visit(AstForeach* nodep) override {
|
2021-12-11 20:06:33 +00:00
|
|
|
// Special, as statements need to be put in different places
|
|
|
|
// Body insert just before themselves
|
|
|
|
m_insStmtp = nullptr; // First thing should be new statement
|
|
|
|
iterateChildren(nodep);
|
|
|
|
// Done the loop
|
|
|
|
m_insStmtp = nullptr; // Next thing should be new statement
|
|
|
|
}
|
2022-09-16 10:22:11 +00:00
|
|
|
void visit(AstJumpBlock* nodep) override {
|
2021-12-11 20:06:33 +00:00
|
|
|
// Special, as statements need to be put in different places
|
|
|
|
// Body insert just before themselves
|
|
|
|
m_insStmtp = nullptr; // First thing should be new statement
|
|
|
|
iterateChildren(nodep);
|
|
|
|
// Done the loop
|
|
|
|
m_insStmtp = nullptr; // Next thing should be new statement
|
|
|
|
}
|
2022-09-16 10:22:11 +00:00
|
|
|
void visit(AstNodeIf* nodep) override {
|
2020-06-03 16:50:24 +00:00
|
|
|
m_insStmtp = nodep;
|
|
|
|
iterateAndNextNull(nodep->condp());
|
2020-08-15 14:12:55 +00:00
|
|
|
m_insStmtp = nullptr;
|
2022-09-15 18:43:56 +00:00
|
|
|
iterateAndNextNull(nodep->thensp());
|
2020-06-03 16:50:24 +00:00
|
|
|
iterateAndNextNull(nodep->elsesp());
|
2020-08-15 14:12:55 +00:00
|
|
|
m_insStmtp = nullptr;
|
2020-06-03 16:50:24 +00:00
|
|
|
}
|
2022-09-16 10:22:11 +00:00
|
|
|
void visit(AstCaseItem* nodep) override {
|
2022-03-12 16:24:32 +00:00
|
|
|
m_insMode = IM_BEFORE;
|
|
|
|
{
|
|
|
|
VL_RESTORER(m_unsupportedHere);
|
|
|
|
m_unsupportedHere = true;
|
|
|
|
iterateAndNextNull(nodep->condsp());
|
|
|
|
}
|
|
|
|
m_insStmtp = nullptr; // Next thing should be new statement
|
2022-09-15 18:43:56 +00:00
|
|
|
iterateAndNextNull(nodep->stmtsp());
|
2022-03-12 16:24:32 +00:00
|
|
|
}
|
2022-09-16 10:22:11 +00:00
|
|
|
void visit(AstNodeFor* nodep) override { // LCOV_EXCL_LINE
|
2020-05-28 22:08:15 +00:00
|
|
|
nodep->v3fatalSrc(
|
|
|
|
"For statements should have been converted to while statements in V3Begin.cpp");
|
|
|
|
}
|
2022-09-16 15:15:10 +00:00
|
|
|
void visit(AstDelay* nodep) override {
|
Timing support (#3363)
Adds timing support to Verilator. It makes it possible to use delays,
event controls within processes (not just at the start), wait
statements, and forks.
Building a design with those constructs requires a compiler that
supports C++20 coroutines (GCC 10, Clang 5).
The basic idea is to have processes and tasks with delays/event controls
implemented as C++20 coroutines. This allows us to suspend and resume
them at any time.
There are five main runtime classes responsible for managing suspended
coroutines:
* `VlCoroutineHandle`, a wrapper over C++20's `std::coroutine_handle`
with move semantics and automatic cleanup.
* `VlDelayScheduler`, for coroutines suspended by delays. It resumes
them at a proper simulation time.
* `VlTriggerScheduler`, for coroutines suspended by event controls. It
resumes them if its corresponding trigger was set.
* `VlForkSync`, used for syncing `fork..join` and `fork..join_any`
blocks.
* `VlCoroutine`, the return type of all verilated coroutines. It allows
for suspending a stack of coroutines (normally, C++ coroutines are
stackless).
There is a new visitor in `V3Timing.cpp` which:
* scales delays according to the timescale,
* simplifies intra-assignment timing controls and net delays into
regular timing controls and assignments,
* simplifies wait statements into loops with event controls,
* marks processes and tasks with timing controls in them as
suspendable,
* creates delay, trigger scheduler, and fork sync variables,
* transforms timing controls and fork joins into C++ awaits
There are new functions in `V3SchedTiming.cpp` (used by `V3Sched.cpp`)
that integrate static scheduling with timing. This involves providing
external domains for variables, so that the necessary combinational
logic gets triggered after coroutine resumption, as well as statements
that need to be injected into the design eval function to perform this
resumption at the correct time.
There is also a function that transforms forked processes into separate
functions.
See the comments in `verilated_timing.h`, `verilated_timing.cpp`,
`V3Timing.cpp`, and `V3SchedTiming.cpp`, as well as the internals
documentation for more details.
Signed-off-by: Krzysztof Bieganski <kbieganski@antmicro.com>
2022-08-22 12:26:32 +00:00
|
|
|
m_insStmtp = nodep;
|
|
|
|
iterateAndNextNull(nodep->lhsp());
|
|
|
|
m_insStmtp = nullptr;
|
|
|
|
iterateAndNextNull(nodep->stmtsp());
|
|
|
|
m_insStmtp = nullptr;
|
|
|
|
}
|
2022-09-16 15:15:10 +00:00
|
|
|
void visit(AstEventControl* nodep) override {
|
Timing support (#3363)
Adds timing support to Verilator. It makes it possible to use delays,
event controls within processes (not just at the start), wait
statements, and forks.
Building a design with those constructs requires a compiler that
supports C++20 coroutines (GCC 10, Clang 5).
The basic idea is to have processes and tasks with delays/event controls
implemented as C++20 coroutines. This allows us to suspend and resume
them at any time.
There are five main runtime classes responsible for managing suspended
coroutines:
* `VlCoroutineHandle`, a wrapper over C++20's `std::coroutine_handle`
with move semantics and automatic cleanup.
* `VlDelayScheduler`, for coroutines suspended by delays. It resumes
them at a proper simulation time.
* `VlTriggerScheduler`, for coroutines suspended by event controls. It
resumes them if its corresponding trigger was set.
* `VlForkSync`, used for syncing `fork..join` and `fork..join_any`
blocks.
* `VlCoroutine`, the return type of all verilated coroutines. It allows
for suspending a stack of coroutines (normally, C++ coroutines are
stackless).
There is a new visitor in `V3Timing.cpp` which:
* scales delays according to the timescale,
* simplifies intra-assignment timing controls and net delays into
regular timing controls and assignments,
* simplifies wait statements into loops with event controls,
* marks processes and tasks with timing controls in them as
suspendable,
* creates delay, trigger scheduler, and fork sync variables,
* transforms timing controls and fork joins into C++ awaits
There are new functions in `V3SchedTiming.cpp` (used by `V3Sched.cpp`)
that integrate static scheduling with timing. This involves providing
external domains for variables, so that the necessary combinational
logic gets triggered after coroutine resumption, as well as statements
that need to be injected into the design eval function to perform this
resumption at the correct time.
There is also a function that transforms forked processes into separate
functions.
See the comments in `verilated_timing.h`, `verilated_timing.cpp`,
`V3Timing.cpp`, and `V3SchedTiming.cpp`, as well as the internals
documentation for more details.
Signed-off-by: Krzysztof Bieganski <kbieganski@antmicro.com>
2022-08-22 12:26:32 +00:00
|
|
|
m_insStmtp = nullptr;
|
|
|
|
iterateAndNextNull(nodep->stmtsp());
|
|
|
|
m_insStmtp = nullptr;
|
|
|
|
}
|
2022-09-16 15:15:10 +00:00
|
|
|
void visit(AstWait* nodep) override {
|
Timing support (#3363)
Adds timing support to Verilator. It makes it possible to use delays,
event controls within processes (not just at the start), wait
statements, and forks.
Building a design with those constructs requires a compiler that
supports C++20 coroutines (GCC 10, Clang 5).
The basic idea is to have processes and tasks with delays/event controls
implemented as C++20 coroutines. This allows us to suspend and resume
them at any time.
There are five main runtime classes responsible for managing suspended
coroutines:
* `VlCoroutineHandle`, a wrapper over C++20's `std::coroutine_handle`
with move semantics and automatic cleanup.
* `VlDelayScheduler`, for coroutines suspended by delays. It resumes
them at a proper simulation time.
* `VlTriggerScheduler`, for coroutines suspended by event controls. It
resumes them if its corresponding trigger was set.
* `VlForkSync`, used for syncing `fork..join` and `fork..join_any`
blocks.
* `VlCoroutine`, the return type of all verilated coroutines. It allows
for suspending a stack of coroutines (normally, C++ coroutines are
stackless).
There is a new visitor in `V3Timing.cpp` which:
* scales delays according to the timescale,
* simplifies intra-assignment timing controls and net delays into
regular timing controls and assignments,
* simplifies wait statements into loops with event controls,
* marks processes and tasks with timing controls in them as
suspendable,
* creates delay, trigger scheduler, and fork sync variables,
* transforms timing controls and fork joins into C++ awaits
There are new functions in `V3SchedTiming.cpp` (used by `V3Sched.cpp`)
that integrate static scheduling with timing. This involves providing
external domains for variables, so that the necessary combinational
logic gets triggered after coroutine resumption, as well as statements
that need to be injected into the design eval function to perform this
resumption at the correct time.
There is also a function that transforms forked processes into separate
functions.
See the comments in `verilated_timing.h`, `verilated_timing.cpp`,
`V3Timing.cpp`, and `V3SchedTiming.cpp`, as well as the internals
documentation for more details.
Signed-off-by: Krzysztof Bieganski <kbieganski@antmicro.com>
2022-08-22 12:26:32 +00:00
|
|
|
m_insStmtp = nodep;
|
|
|
|
iterateAndNextNull(nodep->condp());
|
|
|
|
m_insStmtp = nullptr;
|
2022-09-15 18:43:56 +00:00
|
|
|
iterateAndNextNull(nodep->stmtsp());
|
Timing support (#3363)
Adds timing support to Verilator. It makes it possible to use delays,
event controls within processes (not just at the start), wait
statements, and forks.
Building a design with those constructs requires a compiler that
supports C++20 coroutines (GCC 10, Clang 5).
The basic idea is to have processes and tasks with delays/event controls
implemented as C++20 coroutines. This allows us to suspend and resume
them at any time.
There are five main runtime classes responsible for managing suspended
coroutines:
* `VlCoroutineHandle`, a wrapper over C++20's `std::coroutine_handle`
with move semantics and automatic cleanup.
* `VlDelayScheduler`, for coroutines suspended by delays. It resumes
them at a proper simulation time.
* `VlTriggerScheduler`, for coroutines suspended by event controls. It
resumes them if its corresponding trigger was set.
* `VlForkSync`, used for syncing `fork..join` and `fork..join_any`
blocks.
* `VlCoroutine`, the return type of all verilated coroutines. It allows
for suspending a stack of coroutines (normally, C++ coroutines are
stackless).
There is a new visitor in `V3Timing.cpp` which:
* scales delays according to the timescale,
* simplifies intra-assignment timing controls and net delays into
regular timing controls and assignments,
* simplifies wait statements into loops with event controls,
* marks processes and tasks with timing controls in them as
suspendable,
* creates delay, trigger scheduler, and fork sync variables,
* transforms timing controls and fork joins into C++ awaits
There are new functions in `V3SchedTiming.cpp` (used by `V3Sched.cpp`)
that integrate static scheduling with timing. This involves providing
external domains for variables, so that the necessary combinational
logic gets triggered after coroutine resumption, as well as statements
that need to be injected into the design eval function to perform this
resumption at the correct time.
There is also a function that transforms forked processes into separate
functions.
See the comments in `verilated_timing.h`, `verilated_timing.cpp`,
`V3Timing.cpp`, and `V3SchedTiming.cpp`, as well as the internals
documentation for more details.
Signed-off-by: Krzysztof Bieganski <kbieganski@antmicro.com>
2022-08-22 12:26:32 +00:00
|
|
|
m_insStmtp = nullptr;
|
|
|
|
}
|
2022-09-16 10:22:11 +00:00
|
|
|
void visit(AstNodeStmt* nodep) override {
|
2020-05-28 22:08:15 +00:00
|
|
|
m_insMode = IM_BEFORE;
|
|
|
|
m_insStmtp = nodep;
|
|
|
|
iterateChildren(nodep);
|
2020-08-15 14:12:55 +00:00
|
|
|
m_insStmtp = nullptr; // Next thing should be new statement
|
2020-05-28 22:08:15 +00:00
|
|
|
}
|
|
|
|
void unsupported_visit(AstNode* nodep) {
|
2022-03-11 12:34:11 +00:00
|
|
|
VL_RESTORER(m_unsupportedHere);
|
2020-05-28 22:08:15 +00:00
|
|
|
m_unsupportedHere = true;
|
|
|
|
UINFO(9, "Marking unsupported " << nodep << endl);
|
|
|
|
iterateChildren(nodep);
|
|
|
|
}
|
2022-09-16 10:22:11 +00:00
|
|
|
void visit(AstLogAnd* nodep) override { unsupported_visit(nodep); }
|
|
|
|
void visit(AstLogOr* nodep) override { unsupported_visit(nodep); }
|
|
|
|
void visit(AstLogEq* nodep) override { unsupported_visit(nodep); }
|
|
|
|
void visit(AstLogIf* nodep) override { unsupported_visit(nodep); }
|
|
|
|
void visit(AstNodeCond* nodep) override { unsupported_visit(nodep); }
|
2022-11-01 22:53:47 +00:00
|
|
|
void visit(AstPropSpec* nodep) override { unsupported_visit(nodep); }
|
2020-05-28 22:08:15 +00:00
|
|
|
void prepost_visit(AstNodeTriop* nodep) {
|
|
|
|
// Check if we are underneath a statement
|
|
|
|
if (!m_insStmtp) {
|
|
|
|
prepost_stmt_visit(nodep);
|
2022-12-23 21:17:08 +00:00
|
|
|
} else {
|
|
|
|
prepost_expr_visit(nodep);
|
2020-05-28 22:08:15 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-23 21:17:08 +00:00
|
|
|
void prepost_stmt_visit(AstNodeTriop* nodep) {
|
2020-05-28 22:08:15 +00:00
|
|
|
iterateChildren(nodep);
|
|
|
|
|
2023-02-28 20:21:58 +00:00
|
|
|
// Currently we can't reference the target, so we just copy the AST both for read and
|
|
|
|
// write, but doing so would double any side-effects, so as a safety measure all
|
|
|
|
// statements which could have side-effects are banned at the moment.
|
|
|
|
if (!nodep->rhsp()->isTreePureRecurse()) {
|
|
|
|
nodep->rhsp()->v3warn(E_UNSUPPORTED,
|
|
|
|
"Unsupported: Inc/Dec of expression with side-effects");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-13 18:50:44 +00:00
|
|
|
AstConst* const constp = VN_AS(nodep->lhsp(), Const);
|
2020-05-28 22:08:15 +00:00
|
|
|
UASSERT_OBJ(nodep, constp, "Expecting CONST");
|
2021-11-13 18:50:44 +00:00
|
|
|
AstConst* const newconstp = constp->cloneTree(true);
|
2020-05-28 22:08:15 +00:00
|
|
|
|
2022-12-23 21:17:08 +00:00
|
|
|
AstNodeExpr* const storeTop = nodep->thsp()->unlinkFrBack();
|
|
|
|
AstNodeExpr* const valuep = nodep->rhsp()->unlinkFrBack();
|
2020-05-28 22:08:15 +00:00
|
|
|
|
|
|
|
AstAssign* assignp;
|
|
|
|
if (VN_IS(nodep, PreSub) || VN_IS(nodep, PostSub)) {
|
2022-12-23 21:17:08 +00:00
|
|
|
assignp = new AstAssign{nodep->fileline(), storeTop,
|
2022-11-20 20:06:49 +00:00
|
|
|
new AstSub{nodep->fileline(), valuep, newconstp}};
|
2020-05-28 22:08:15 +00:00
|
|
|
} else {
|
2022-12-23 21:17:08 +00:00
|
|
|
assignp = new AstAssign{nodep->fileline(), storeTop,
|
2022-11-20 20:06:49 +00:00
|
|
|
new AstAdd{nodep->fileline(), valuep, newconstp}};
|
2020-05-28 22:08:15 +00:00
|
|
|
}
|
|
|
|
nodep->replaceWith(assignp);
|
|
|
|
VL_DO_DANGLING(nodep->deleteTree(), nodep);
|
|
|
|
}
|
2022-12-23 21:17:08 +00:00
|
|
|
void prepost_expr_visit(AstNodeTriop* nodep) {
|
2020-05-28 22:08:15 +00:00
|
|
|
iterateChildren(nodep);
|
|
|
|
|
2023-02-28 20:21:58 +00:00
|
|
|
// Currently we can't reference the target, so we just copy the AST both for read and
|
|
|
|
// write, but doing so would double any side-effects, so as a safety measure all
|
|
|
|
// statements which could have side-effects are banned at the moment.
|
|
|
|
if (!nodep->rhsp()->isTreePureRecurse()) {
|
|
|
|
nodep->rhsp()->v3warn(E_UNSUPPORTED,
|
|
|
|
"Unsupported: Inc/Dec of expression with side-effects");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-13 18:50:44 +00:00
|
|
|
const AstNodeVarRef* varrefp = nullptr;
|
2020-05-28 22:08:15 +00:00
|
|
|
if (m_unsupportedHere || !(varrefp = VN_CAST(nodep->rhsp(), VarRef))) {
|
2020-06-09 23:20:16 +00:00
|
|
|
nodep->v3warn(E_UNSUPPORTED, "Unsupported: Incrementation in this context.");
|
2020-05-28 22:08:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-13 18:50:44 +00:00
|
|
|
AstConst* const constp = VN_AS(nodep->lhsp(), Const);
|
2020-05-28 22:08:15 +00:00
|
|
|
UASSERT_OBJ(nodep, constp, "Expecting CONST");
|
2021-11-13 18:50:44 +00:00
|
|
|
const AstNode* const backp = nodep->backp();
|
|
|
|
AstConst* const newconstp = constp->cloneTree(true);
|
2020-05-28 22:08:15 +00:00
|
|
|
|
|
|
|
// Prepare a temporary variable
|
2021-11-13 18:50:44 +00:00
|
|
|
FileLine* const fl = backp->fileline();
|
2021-06-20 22:32:57 +00:00
|
|
|
const string name = string("__Vincrement") + cvtToStr(++m_modIncrementsNum);
|
2022-11-20 20:06:49 +00:00
|
|
|
AstVar* const varp = new AstVar{fl, VVarType::BLOCKTEMP, name, VFlagChildDType{},
|
|
|
|
varrefp->varp()->subDTypep()->cloneTree(true)};
|
2022-10-19 00:04:09 +00:00
|
|
|
if (m_ftaskp) varp->funcLocal(true);
|
2020-05-28 22:08:15 +00:00
|
|
|
|
|
|
|
// Declare the variable
|
|
|
|
insertBeforeStmt(nodep, varp);
|
|
|
|
|
|
|
|
// Define what operation will we be doing
|
2022-11-13 20:33:11 +00:00
|
|
|
AstNodeExpr* operp;
|
2020-05-28 22:08:15 +00:00
|
|
|
if (VN_IS(nodep, PostSub) || VN_IS(nodep, PreSub)) {
|
2022-11-20 20:06:49 +00:00
|
|
|
operp = new AstSub{fl, new AstVarRef{fl, varrefp->varp(), VAccess::READ}, newconstp};
|
2020-05-28 22:08:15 +00:00
|
|
|
} else {
|
2022-11-20 20:06:49 +00:00
|
|
|
operp = new AstAdd{fl, new AstVarRef{fl, varrefp->varp(), VAccess::READ}, newconstp};
|
2020-05-28 22:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (VN_IS(nodep, PreAdd) || VN_IS(nodep, PreSub)) {
|
|
|
|
// PreAdd/PreSub operations
|
|
|
|
// Immediately after declaration - increment it by one
|
2022-11-17 18:26:45 +00:00
|
|
|
varp->addNextHere(new AstAssign{fl, new AstVarRef{fl, varrefp->varp(), VAccess::WRITE},
|
|
|
|
new AstVarRef{fl, varp, VAccess::READ}});
|
2020-05-28 22:08:15 +00:00
|
|
|
// Immediately after incrementing - assign it to the original variable
|
2022-11-17 18:26:45 +00:00
|
|
|
varp->addNextHere(new AstAssign{fl, new AstVarRef{fl, varp, VAccess::WRITE}, operp});
|
2020-05-28 22:08:15 +00:00
|
|
|
} else {
|
|
|
|
// PostAdd/PostSub operations
|
|
|
|
// assign the original variable to the temporary one
|
2022-11-17 18:26:45 +00:00
|
|
|
varp->addNextHere(
|
|
|
|
new AstAssign{fl, new AstVarRef{fl, varrefp->varp(), VAccess::WRITE}, operp});
|
2020-05-28 22:08:15 +00:00
|
|
|
// Increment the original variable by one
|
2022-11-17 18:26:45 +00:00
|
|
|
varp->addNextHere(new AstAssign{fl, new AstVarRef{fl, varp, VAccess::WRITE},
|
|
|
|
new AstVarRef{fl, varrefp->varp(), VAccess::READ}});
|
2020-05-28 22:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the node with the temporary
|
2022-10-10 11:58:05 +00:00
|
|
|
nodep->replaceWith(new AstVarRef{varrefp->fileline(), varp, VAccess::READ});
|
2020-05-28 22:08:15 +00:00
|
|
|
VL_DO_DANGLING(nodep->deleteTree(), nodep);
|
|
|
|
}
|
2022-09-16 10:22:11 +00:00
|
|
|
void visit(AstPreAdd* nodep) override { prepost_visit(nodep); }
|
|
|
|
void visit(AstPostAdd* nodep) override { prepost_visit(nodep); }
|
|
|
|
void visit(AstPreSub* nodep) override { prepost_visit(nodep); }
|
|
|
|
void visit(AstPostSub* nodep) override { prepost_visit(nodep); }
|
|
|
|
void visit(AstGenFor* nodep) override { iterateChildren(nodep); }
|
|
|
|
void visit(AstNode* nodep) override { iterateChildren(nodep); }
|
2020-05-28 22:08:15 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
// CONSTRUCTORS
|
2020-08-16 13:55:36 +00:00
|
|
|
explicit LinkIncVisitor(AstNetlist* nodep) { iterate(nodep); }
|
2022-09-16 10:22:11 +00:00
|
|
|
~LinkIncVisitor() override = default;
|
2020-05-28 22:08:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
// Task class functions
|
|
|
|
|
|
|
|
void V3LinkInc::linkIncrements(AstNetlist* nodep) {
|
|
|
|
UINFO(2, __FUNCTION__ << ": " << endl);
|
2021-11-26 15:52:36 +00:00
|
|
|
{ LinkIncVisitor{nodep}; } // Destruct before checking
|
2022-09-18 19:53:42 +00:00
|
|
|
V3Global::dumpCheckGlobalTree("linkinc", 0, dumpTree() >= 3);
|
2020-05-28 22:08:15 +00:00
|
|
|
}
|