2020-05-29 00:08:15 +02:00
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Replace increments/decrements with new variables
|
|
|
|
|
//
|
|
|
|
|
// Code available from: https://verilator.org
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2025-01-01 14:30:25 +01:00
|
|
|
// Copyright 2003-2025 by Wilson Snyder. This program is free software; you
|
2020-05-29 00:08:15 +02: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 22:17:08 +01:00
|
|
|
// prepost_expr_visit
|
2020-05-29 00:08:15 +02: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.
|
2025-01-09 01:37:20 +01:00
|
|
|
//
|
2022-12-23 22:17:08 +01:00
|
|
|
// prepost_stmt_visit
|
2020-05-29 00:08:15 +02: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.
|
|
|
|
|
//
|
2025-01-09 01:37:20 +01:00
|
|
|
// prepost_stmt_sel_visit
|
|
|
|
|
// For e.g. 'array[something_with_side_eff]++', common in UVM etc
|
|
|
|
|
// PREADD/PRESUB/POSTADD/POSTSUB
|
|
|
|
|
// Create temporary with array index.
|
|
|
|
|
// Increment/decrement using index of the temporary.
|
|
|
|
|
//
|
2020-05-29 00:08:15 +02:00
|
|
|
//*************************************************************************
|
|
|
|
|
|
2023-10-18 04:50:27 +02:00
|
|
|
#include "V3PchAstNoMT.h" // VL_MT_DISABLED_CODE_UNIT
|
|
|
|
|
|
2023-10-18 12:37:46 +02:00
|
|
|
#include "V3LinkInc.h"
|
|
|
|
|
|
2022-09-18 21:53:42 +02:00
|
|
|
VL_DEFINE_DEBUG_FUNCTIONS;
|
|
|
|
|
|
2020-05-29 00:08:15 +02:00
|
|
|
//######################################################################
|
|
|
|
|
|
2022-01-02 19:56:40 +01:00
|
|
|
class LinkIncVisitor final : public VNVisitor {
|
2020-05-29 00:08:15 +02:00
|
|
|
// STATE
|
2022-10-19 02:04:09 +02:00
|
|
|
AstNodeFTask* m_ftaskp = nullptr; // Function or task we're inside
|
2023-08-19 13:03:21 +02:00
|
|
|
AstNodeModule* m_modp = nullptr; // Module we're inside
|
2020-08-16 15:55:36 +02:00
|
|
|
int m_modIncrementsNum = 0; // Var name counter
|
|
|
|
|
AstNode* m_insStmtp = nullptr; // Where to insert statement
|
|
|
|
|
bool m_unsupportedHere = false; // Used to detect where it's not supported yet
|
2020-05-29 00:08:15 +02:00
|
|
|
|
2022-03-11 13:34:11 +01:00
|
|
|
// METHODS
|
2023-08-19 13:03:21 +02:00
|
|
|
void insertOnTop(AstNode* newp) {
|
|
|
|
|
// Add the thing directly under the current TFunc/Module
|
|
|
|
|
AstNode* stmtsp = nullptr;
|
|
|
|
|
if (m_ftaskp) {
|
|
|
|
|
stmtsp = m_ftaskp->stmtsp();
|
|
|
|
|
} else if (m_modp) {
|
|
|
|
|
stmtsp = m_modp->stmtsp();
|
|
|
|
|
}
|
2025-09-20 23:40:50 +02:00
|
|
|
UASSERT_OBJ(stmtsp, newp, "Variable not under FTASK/MODULE");
|
2023-08-19 13:03:21 +02:00
|
|
|
newp->addNext(stmtsp->unlinkFrBackWithNext());
|
|
|
|
|
if (m_ftaskp) {
|
|
|
|
|
m_ftaskp->addStmtsp(newp);
|
|
|
|
|
} else if (m_modp) {
|
|
|
|
|
m_modp->addStmtsp(newp);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-23 14:50:39 +02:00
|
|
|
void insertBeforeStmt(AstNode* nodep, AstNode* newp) {
|
2020-05-29 00:08:15 +02:00
|
|
|
// Return node that must be visited, if any
|
2025-08-02 19:44:40 +02:00
|
|
|
UINFOTREE(9, newp, "", "newstmt");
|
2025-07-23 14:50:39 +02:00
|
|
|
UASSERT_OBJ(m_insStmtp, nodep, "Expression not underneath a statement");
|
|
|
|
|
m_insStmtp->addHereThisAsNext(newp);
|
2020-05-29 00:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VISITORS
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNodeModule* nodep) override {
|
2025-09-29 16:25:25 +02:00
|
|
|
if (nodep->dead()) return;
|
2023-08-19 13:03:21 +02:00
|
|
|
VL_RESTORER(m_modp);
|
2022-03-11 13:34:11 +01:00
|
|
|
VL_RESTORER(m_modIncrementsNum);
|
2023-08-19 13:03:21 +02:00
|
|
|
m_modp = nodep;
|
2020-05-29 00:08:15 +02:00
|
|
|
m_modIncrementsNum = 0;
|
|
|
|
|
iterateChildren(nodep);
|
|
|
|
|
}
|
2022-10-19 02:04:09 +02:00
|
|
|
void visit(AstNodeFTask* nodep) override {
|
|
|
|
|
VL_RESTORER(m_ftaskp);
|
|
|
|
|
m_ftaskp = nodep;
|
|
|
|
|
iterateChildren(nodep);
|
|
|
|
|
}
|
2025-09-15 16:50:31 +02:00
|
|
|
void visit(AstNodeCoverOrAssert* nodep) override {
|
|
|
|
|
VL_RESTORER(m_insStmtp);
|
|
|
|
|
m_insStmtp = nodep;
|
|
|
|
|
iterateAndNextNull(nodep->propp());
|
|
|
|
|
m_insStmtp = nullptr;
|
|
|
|
|
// Note: no iterating over sentreep here as they will be ignored anyway
|
|
|
|
|
if (AstAssert* const assertp = VN_CAST(nodep, Assert)) {
|
|
|
|
|
iterateAndNextNull(assertp->failsp());
|
|
|
|
|
} else if (AstAssertIntrinsic* const intrinsicp = VN_CAST(nodep, AssertIntrinsic)) {
|
|
|
|
|
iterateAndNextNull(intrinsicp->failsp());
|
|
|
|
|
}
|
|
|
|
|
iterateAndNextNull(nodep->passsp());
|
|
|
|
|
}
|
2025-09-29 16:25:25 +02:00
|
|
|
void visit(AstLoop* nodep) override {
|
|
|
|
|
UASSERT_OBJ(!nodep->contsp(), nodep, "'contsp' only used before LinkJump");
|
2020-08-15 16:12:55 +02:00
|
|
|
m_insStmtp = nullptr; // First thing should be new statement
|
2022-09-15 20:43:56 +02:00
|
|
|
iterateAndNextNull(nodep->stmtsp());
|
2020-08-15 16:12:55 +02:00
|
|
|
m_insStmtp = nullptr; // Next thing should be new statement
|
2020-05-29 00:08:15 +02:00
|
|
|
}
|
2023-11-12 02:20:37 +01:00
|
|
|
void visit(AstNodeForeach* nodep) override {
|
2021-12-11 21:06:33 +01: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 12:22:11 +02:00
|
|
|
void visit(AstJumpBlock* nodep) override {
|
2021-12-11 21:06:33 +01: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 12:22:11 +02:00
|
|
|
void visit(AstNodeIf* nodep) override {
|
2020-06-03 18:50:24 +02:00
|
|
|
m_insStmtp = nodep;
|
|
|
|
|
iterateAndNextNull(nodep->condp());
|
2020-08-15 16:12:55 +02:00
|
|
|
m_insStmtp = nullptr;
|
2022-09-15 20:43:56 +02:00
|
|
|
iterateAndNextNull(nodep->thensp());
|
2020-06-03 18:50:24 +02:00
|
|
|
iterateAndNextNull(nodep->elsesp());
|
2020-08-15 16:12:55 +02:00
|
|
|
m_insStmtp = nullptr;
|
2020-06-03 18:50:24 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstCaseItem* nodep) override {
|
2022-03-12 17:24:32 +01:00
|
|
|
{
|
|
|
|
|
VL_RESTORER(m_unsupportedHere);
|
|
|
|
|
m_unsupportedHere = true;
|
|
|
|
|
iterateAndNextNull(nodep->condsp());
|
|
|
|
|
}
|
|
|
|
|
m_insStmtp = nullptr; // Next thing should be new statement
|
2022-09-15 20:43:56 +02:00
|
|
|
iterateAndNextNull(nodep->stmtsp());
|
2022-03-12 17:24:32 +01:00
|
|
|
}
|
2022-09-16 17:15:10 +02: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 14:26:32 +02:00
|
|
|
m_insStmtp = nodep;
|
|
|
|
|
iterateAndNextNull(nodep->lhsp());
|
|
|
|
|
m_insStmtp = nullptr;
|
|
|
|
|
iterateAndNextNull(nodep->stmtsp());
|
|
|
|
|
m_insStmtp = nullptr;
|
|
|
|
|
}
|
2022-09-16 17:15:10 +02: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 14:26:32 +02:00
|
|
|
m_insStmtp = nullptr;
|
|
|
|
|
iterateAndNextNull(nodep->stmtsp());
|
|
|
|
|
m_insStmtp = nullptr;
|
|
|
|
|
}
|
2022-09-16 17:15:10 +02: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 14:26:32 +02:00
|
|
|
m_insStmtp = nodep;
|
|
|
|
|
iterateAndNextNull(nodep->condp());
|
|
|
|
|
m_insStmtp = nullptr;
|
2022-09-15 20:43:56 +02: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 14:26:32 +02:00
|
|
|
m_insStmtp = nullptr;
|
|
|
|
|
}
|
2025-09-29 16:25:25 +02:00
|
|
|
void visit(AstBegin* nodep) override {
|
|
|
|
|
m_insStmtp = nullptr; // Pretend not a statement TODO: parse ++/-- as ExprStmt
|
|
|
|
|
iterateChildren(nodep);
|
|
|
|
|
m_insStmtp = nullptr; // Next thing should be new statement
|
|
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNodeStmt* nodep) override {
|
2020-05-29 00:08:15 +02:00
|
|
|
m_insStmtp = nodep;
|
|
|
|
|
iterateChildren(nodep);
|
2020-08-15 16:12:55 +02:00
|
|
|
m_insStmtp = nullptr; // Next thing should be new statement
|
2020-05-29 00:08:15 +02:00
|
|
|
}
|
|
|
|
|
void unsupported_visit(AstNode* nodep) {
|
2022-03-11 13:34:11 +01:00
|
|
|
VL_RESTORER(m_unsupportedHere);
|
2020-05-29 00:08:15 +02:00
|
|
|
m_unsupportedHere = true;
|
2025-05-23 02:29:32 +02:00
|
|
|
UINFO(9, "Marking unsupported " << nodep);
|
2020-05-29 00:08:15 +02:00
|
|
|
iterateChildren(nodep);
|
|
|
|
|
}
|
2022-09-16 12:22:11 +02: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); }
|
2025-08-16 00:49:06 +02:00
|
|
|
void visit(AstCond* nodep) override { unsupported_visit(nodep); }
|
2022-11-01 23:53:47 +01:00
|
|
|
void visit(AstPropSpec* nodep) override { unsupported_visit(nodep); }
|
2020-05-29 00:08:15 +02:00
|
|
|
void prepost_visit(AstNodeTriop* nodep) {
|
|
|
|
|
// Check if we are underneath a statement
|
2025-01-09 01:37:20 +01:00
|
|
|
AstSelBit* const selbitp = VN_CAST(nodep->thsp(), SelBit);
|
|
|
|
|
if (!m_insStmtp && selbitp && VN_IS(selbitp->fromp(), NodeVarRef)
|
|
|
|
|
&& !selbitp->bitp()->isPure()) {
|
|
|
|
|
prepost_stmt_sel_visit(nodep);
|
2022-12-23 22:17:08 +01:00
|
|
|
} else {
|
2025-01-09 01:37:20 +01:00
|
|
|
// Purity check was deferred at creation in verilog.y, check now
|
|
|
|
|
nodep->thsp()->purityCheck();
|
|
|
|
|
if (!m_insStmtp) {
|
|
|
|
|
prepost_stmt_visit(nodep);
|
|
|
|
|
} else {
|
|
|
|
|
prepost_expr_visit(nodep);
|
|
|
|
|
}
|
2020-05-29 00:08:15 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-01-09 01:37:20 +01:00
|
|
|
void prepost_stmt_sel_visit(AstNodeTriop* nodep) {
|
|
|
|
|
// Special case array[something]++, see comments at file top
|
2025-08-02 19:44:40 +02:00
|
|
|
// UINFOTREE(9, nodep, "", "pp-stmt-sel-in");
|
2025-01-09 01:37:20 +01:00
|
|
|
iterateChildren(nodep);
|
|
|
|
|
AstConst* const constp = VN_AS(nodep->lhsp(), Const);
|
|
|
|
|
UASSERT_OBJ(nodep, constp, "Expecting CONST");
|
|
|
|
|
AstConst* const newconstp = constp->cloneTree(true);
|
|
|
|
|
|
|
|
|
|
AstSelBit* const rdSelbitp = VN_CAST(nodep->rhsp(), SelBit);
|
|
|
|
|
AstNodeExpr* const rdFromp = rdSelbitp->fromp()->unlinkFrBack();
|
|
|
|
|
AstNodeExpr* const rdBitp = rdSelbitp->bitp()->unlinkFrBack();
|
|
|
|
|
AstSelBit* const wrSelbitp = VN_CAST(nodep->thsp(), SelBit);
|
|
|
|
|
AstNodeExpr* const wrFromp = wrSelbitp->fromp()->unlinkFrBack();
|
|
|
|
|
|
|
|
|
|
// Prepare a temporary variable
|
|
|
|
|
FileLine* const fl = nodep->fileline();
|
|
|
|
|
const string name = "__VincIndex"s + cvtToStr(++m_modIncrementsNum);
|
|
|
|
|
AstVar* const varp = new AstVar{
|
|
|
|
|
fl, VVarType::BLOCKTEMP, name, VFlagChildDType{},
|
|
|
|
|
new AstRefDType{fl, AstRefDType::FlagTypeOfExpr{}, rdBitp->cloneTree(true)}};
|
|
|
|
|
if (m_ftaskp) varp->funcLocal(true);
|
|
|
|
|
|
|
|
|
|
// Declare the variable
|
|
|
|
|
insertOnTop(varp);
|
|
|
|
|
|
|
|
|
|
// Define what operation will we be doing
|
|
|
|
|
AstAssign* const varAssignp
|
|
|
|
|
= new AstAssign{fl, new AstVarRef{fl, varp, VAccess::WRITE}, rdBitp};
|
|
|
|
|
AstNode* const newp = varAssignp;
|
|
|
|
|
|
|
|
|
|
AstNodeExpr* const valuep
|
|
|
|
|
= new AstSelBit{fl, rdFromp, new AstVarRef{fl, varp, VAccess::READ}};
|
|
|
|
|
AstNodeExpr* const storeTop
|
|
|
|
|
= new AstSelBit{fl, wrFromp, new AstVarRef{fl, varp, VAccess::READ}};
|
|
|
|
|
|
|
|
|
|
AstAssign* assignp;
|
|
|
|
|
if (VN_IS(nodep, PreSub) || VN_IS(nodep, PostSub)) {
|
|
|
|
|
assignp = new AstAssign{nodep->fileline(), storeTop,
|
|
|
|
|
new AstSub{nodep->fileline(), valuep, newconstp}};
|
|
|
|
|
} else {
|
|
|
|
|
assignp = new AstAssign{nodep->fileline(), storeTop,
|
|
|
|
|
new AstAdd{nodep->fileline(), valuep, newconstp}};
|
|
|
|
|
}
|
|
|
|
|
newp->addNext(assignp);
|
|
|
|
|
|
|
|
|
|
// if (debug() >= 9) newp->dumpTreeAndNext("-pp-stmt-sel-new: ");
|
|
|
|
|
nodep->replaceWith(newp);
|
|
|
|
|
VL_DO_DANGLING(nodep->deleteTree(), nodep);
|
|
|
|
|
}
|
2022-12-23 22:17:08 +01:00
|
|
|
void prepost_stmt_visit(AstNodeTriop* nodep) {
|
2020-05-29 00:08:15 +02:00
|
|
|
iterateChildren(nodep);
|
2021-11-13 19:50:44 +01:00
|
|
|
AstConst* const constp = VN_AS(nodep->lhsp(), Const);
|
2020-05-29 00:08:15 +02:00
|
|
|
UASSERT_OBJ(nodep, constp, "Expecting CONST");
|
2021-11-13 19:50:44 +01:00
|
|
|
AstConst* const newconstp = constp->cloneTree(true);
|
2020-05-29 00:08:15 +02:00
|
|
|
|
2022-12-23 22:17:08 +01:00
|
|
|
AstNodeExpr* const storeTop = nodep->thsp()->unlinkFrBack();
|
|
|
|
|
AstNodeExpr* const valuep = nodep->rhsp()->unlinkFrBack();
|
2020-05-29 00:08:15 +02:00
|
|
|
|
|
|
|
|
AstAssign* assignp;
|
|
|
|
|
if (VN_IS(nodep, PreSub) || VN_IS(nodep, PostSub)) {
|
2022-12-23 22:17:08 +01:00
|
|
|
assignp = new AstAssign{nodep->fileline(), storeTop,
|
2022-11-20 21:06:49 +01:00
|
|
|
new AstSub{nodep->fileline(), valuep, newconstp}};
|
2020-05-29 00:08:15 +02:00
|
|
|
} else {
|
2022-12-23 22:17:08 +01:00
|
|
|
assignp = new AstAssign{nodep->fileline(), storeTop,
|
2022-11-20 21:06:49 +01:00
|
|
|
new AstAdd{nodep->fileline(), valuep, newconstp}};
|
2020-05-29 00:08:15 +02:00
|
|
|
}
|
|
|
|
|
nodep->replaceWith(assignp);
|
|
|
|
|
VL_DO_DANGLING(nodep->deleteTree(), nodep);
|
|
|
|
|
}
|
2022-12-23 22:17:08 +01:00
|
|
|
void prepost_expr_visit(AstNodeTriop* nodep) {
|
2020-05-29 00:08:15 +02:00
|
|
|
iterateChildren(nodep);
|
2023-04-08 02:57:17 +02:00
|
|
|
if (m_unsupportedHere) {
|
2020-06-10 01:20:16 +02:00
|
|
|
nodep->v3warn(E_UNSUPPORTED, "Unsupported: Incrementation in this context.");
|
2020-05-29 00:08:15 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2023-04-08 02:57:17 +02:00
|
|
|
AstNodeExpr* const readp = nodep->rhsp();
|
2023-09-02 01:49:57 +02:00
|
|
|
AstNodeExpr* const writep = nodep->thsp()->unlinkFrBack();
|
2020-05-29 00:08:15 +02:00
|
|
|
|
2021-11-13 19:50:44 +01:00
|
|
|
AstConst* const constp = VN_AS(nodep->lhsp(), Const);
|
2020-05-29 00:08:15 +02:00
|
|
|
UASSERT_OBJ(nodep, constp, "Expecting CONST");
|
2021-11-13 19:50:44 +01:00
|
|
|
AstConst* const newconstp = constp->cloneTree(true);
|
2020-05-29 00:08:15 +02:00
|
|
|
|
|
|
|
|
// Prepare a temporary variable
|
2023-08-19 13:03:21 +02:00
|
|
|
FileLine* const fl = nodep->fileline();
|
2024-07-14 17:39:45 +02:00
|
|
|
const string name = "__Vincrement"s + cvtToStr(++m_modIncrementsNum);
|
2023-04-08 02:57:17 +02:00
|
|
|
AstVar* const varp = new AstVar{
|
|
|
|
|
fl, VVarType::BLOCKTEMP, name, VFlagChildDType{},
|
|
|
|
|
new AstRefDType{fl, AstRefDType::FlagTypeOfExpr{}, readp->cloneTree(true)}};
|
2022-10-19 02:04:09 +02:00
|
|
|
if (m_ftaskp) varp->funcLocal(true);
|
2020-05-29 00:08:15 +02:00
|
|
|
|
|
|
|
|
// Declare the variable
|
2023-08-19 13:03:21 +02:00
|
|
|
insertOnTop(varp);
|
2020-05-29 00:08:15 +02:00
|
|
|
|
|
|
|
|
// Define what operation will we be doing
|
2022-11-13 21:33:11 +01:00
|
|
|
AstNodeExpr* operp;
|
2020-05-29 00:08:15 +02:00
|
|
|
if (VN_IS(nodep, PostSub) || VN_IS(nodep, PreSub)) {
|
2023-09-17 04:50:54 +02:00
|
|
|
operp = new AstSub{fl, readp->cloneTreePure(true), newconstp};
|
2020-05-29 00:08:15 +02:00
|
|
|
} else {
|
2023-09-17 04:50:54 +02:00
|
|
|
operp = new AstAdd{fl, readp->cloneTreePure(true), newconstp};
|
2020-05-29 00:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (VN_IS(nodep, PreAdd) || VN_IS(nodep, PreSub)) {
|
|
|
|
|
// PreAdd/PreSub operations
|
|
|
|
|
// Immediately after declaration - increment it by one
|
2023-08-19 13:03:21 +02:00
|
|
|
AstAssign* const assignp
|
|
|
|
|
= new AstAssign{fl, new AstVarRef{fl, varp, VAccess::WRITE}, operp};
|
2020-05-29 00:08:15 +02:00
|
|
|
// Immediately after incrementing - assign it to the original variable
|
2025-07-23 14:50:39 +02:00
|
|
|
assignp->addNext(new AstAssign{fl, writep, new AstVarRef{fl, varp, VAccess::READ}});
|
|
|
|
|
insertBeforeStmt(nodep, assignp);
|
2020-05-29 00:08:15 +02:00
|
|
|
} else {
|
|
|
|
|
// PostAdd/PostSub operations
|
2023-08-19 13:03:21 +02:00
|
|
|
// Assign the original variable to the temporary one
|
|
|
|
|
AstAssign* const assignp = new AstAssign{fl, new AstVarRef{fl, varp, VAccess::WRITE},
|
2023-09-17 04:50:54 +02:00
|
|
|
readp->cloneTreePure(true)};
|
2020-05-29 00:08:15 +02:00
|
|
|
// Increment the original variable by one
|
2025-07-23 14:50:39 +02:00
|
|
|
assignp->addNext(new AstAssign{fl, writep, operp});
|
|
|
|
|
insertBeforeStmt(nodep, assignp);
|
2020-05-29 00:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Replace the node with the temporary
|
2023-04-08 02:57:17 +02:00
|
|
|
nodep->replaceWith(new AstVarRef{readp->fileline(), varp, VAccess::READ});
|
2020-05-29 00:08:15 +02:00
|
|
|
VL_DO_DANGLING(nodep->deleteTree(), nodep);
|
|
|
|
|
}
|
2022-09-16 12:22:11 +02: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-29 00:08:15 +02:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// CONSTRUCTORS
|
2020-08-16 15:55:36 +02:00
|
|
|
explicit LinkIncVisitor(AstNetlist* nodep) { iterate(nodep); }
|
2022-09-16 12:22:11 +02:00
|
|
|
~LinkIncVisitor() override = default;
|
2020-05-29 00:08:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Task class functions
|
|
|
|
|
|
|
|
|
|
void V3LinkInc::linkIncrements(AstNetlist* nodep) {
|
2025-05-23 02:29:32 +02:00
|
|
|
UINFO(2, __FUNCTION__ << ":");
|
2021-11-26 16:52:36 +01:00
|
|
|
{ LinkIncVisitor{nodep}; } // Destruct before checking
|
2024-01-09 16:35:13 +01:00
|
|
|
V3Global::dumpCheckGlobalTree("linkinc", 0, dumpTreeEitherLevel() >= 3);
|
2020-05-29 00:08:15 +02:00
|
|
|
}
|