2012-04-13 01:08:20 +00:00
|
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Add temporaries, such as for delayed nodes
|
|
|
|
|
//
|
2008-04-25 12:14:27 +00:00
|
|
|
|
// Code available from: http://www.veripool.org/verilator
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2018-01-02 23:05:06 +00:00
|
|
|
|
// Copyright 2003-2018 by Wilson Snyder. This program is free software; you can
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// redistribute it and/or modify it under the terms of either the GNU
|
2009-05-04 21:07:57 +00:00
|
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
|
// Version 2.0.
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
|
|
|
|
// Verilator is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
// V3Delayed's Transformations:
|
2008-06-10 01:25:10 +00:00
|
|
|
|
//
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// Each module:
|
2008-06-10 01:25:10 +00:00
|
|
|
|
// Replace ASSIGNDLY var, exp
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// With ASSIGNDLY newvar, exp
|
|
|
|
|
// At top of block: VAR newvar
|
|
|
|
|
// At bottom of block: ASSIGNW var newvar
|
|
|
|
|
// Need _x_dly = x at top of active if "x" is not always set
|
|
|
|
|
// For now we'll say it's set if at top of block (not under IF, etc)
|
|
|
|
|
// Need x = _x_dly at bottom of active if "x" is never referenced on LHS
|
|
|
|
|
// in the active, and above rule applies too. (If so, use x on LHS, not _x_dly.)
|
|
|
|
|
//
|
|
|
|
|
// If a signal is set in multiple always blocks, we need a dly read & set with
|
|
|
|
|
// multiple clock sensitivities. We have 3 options:
|
|
|
|
|
// 1. When detected, make a new ACTIVE and move earlier created delayed assignment there
|
|
|
|
|
// 2. Form unique ACTIVE for every multiple clocked assignment
|
|
|
|
|
// 3. Predetect signals from multiple always blocks and do #2 on them
|
|
|
|
|
// Since all 3 require a top activation cleanup, we do #2 which is easiest.
|
|
|
|
|
//
|
|
|
|
|
// ASSIGNDLY (BITSEL(ARRAYSEL (VARREF(v), bits), selbits), rhs)
|
|
|
|
|
// -> VAR __Vdlyvset_x
|
|
|
|
|
// VAR __Vdlyvval_x
|
|
|
|
|
// VAR __Vdlyvdim_x
|
|
|
|
|
// VAR __Vdlyvlsb_x
|
|
|
|
|
// ASSIGNW (__Vdlyvset_x,0)
|
|
|
|
|
// ...
|
|
|
|
|
// ASSIGNW (VARREF(__Vdlyvval_x), rhs)
|
|
|
|
|
// ASSIGNW (__Vdlyvdim_x, dimension_number)
|
|
|
|
|
// ASSIGNW (__Vdlyvset_x, 1)
|
|
|
|
|
// ...
|
|
|
|
|
// ASSIGNW (BITSEL(ARRAYSEL(VARREF(x), __Vdlyvdim_x), __Vdlyvlsb_x), __Vdlyvval_x)
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
2006-12-18 19:20:45 +00:00
|
|
|
|
#include "config_build.h"
|
|
|
|
|
#include "verilatedos.h"
|
2008-06-30 17:11:25 +00:00
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdarg>
|
2006-08-26 11:35:28 +00:00
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <deque>
|
|
|
|
|
|
|
|
|
|
#include "V3Global.h"
|
|
|
|
|
#include "V3Delayed.h"
|
|
|
|
|
#include "V3Ast.h"
|
|
|
|
|
#include "V3Stats.h"
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Delayed state, as a visitor of each AstNode
|
|
|
|
|
|
|
|
|
|
class DelayedVisitor : public AstNVisitor {
|
|
|
|
|
private:
|
|
|
|
|
// NODE STATE
|
|
|
|
|
// Cleared each module:
|
2008-11-25 14:03:49 +00:00
|
|
|
|
// AstVarScope::user1p() -> AstVarScope*. Points to temp var created.
|
2012-01-26 13:10:50 +00:00
|
|
|
|
// AstVarScope::user2p() -> AstActive*. Points to activity block of signal (valid when AstVarScope::user1p is valid)
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// AstVarScope::user4p() -> AstAlwaysPost*. Post block for this variable
|
2010-02-01 23:55:32 +00:00
|
|
|
|
// AstVarScope::user5() -> VarUsage. Tracks delayed vs non-delayed usage
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// AstVar::user2() -> bool. Set true if already made warning
|
|
|
|
|
// AstVarRef::user2() -> bool. Set true if already processed
|
2012-01-26 13:10:50 +00:00
|
|
|
|
// AstAlwaysPost::user2() -> ActActive*. Points to activity block of signal (valid when AstAlwaysPost::user4p is valid)
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// AstAlwaysPost::user4() -> AstIf*. Last IF (__Vdlyvset__) created under this AlwaysPost
|
2012-01-26 13:10:50 +00:00
|
|
|
|
// Cleared each scope/active:
|
2008-11-25 12:57:02 +00:00
|
|
|
|
// AstAssignDly::user3() -> AstVarScope*. __Vdlyvset__ created for this assign
|
|
|
|
|
// AstAlwaysPost::user3() -> AstVarScope*. __Vdlyvset__ last referenced in IF
|
2008-11-25 14:03:49 +00:00
|
|
|
|
AstUser1InUse m_inuser1;
|
|
|
|
|
AstUser2InUse m_inuser2;
|
|
|
|
|
AstUser3InUse m_inuser3;
|
|
|
|
|
AstUser4InUse m_inuser4;
|
2010-02-01 23:55:32 +00:00
|
|
|
|
AstUser5InUse m_inuser5;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
|
enum VarUsage { VU_NONE=0, VU_DLY=1, VU_NONDLY=2 };
|
|
|
|
|
|
|
|
|
|
// STATE
|
|
|
|
|
AstActive* m_activep; // Current activate
|
|
|
|
|
AstCFunc* m_cfuncp; // Current public C Function
|
|
|
|
|
AstAssignDly* m_nextDlyp; // Next delayed assignment in a list of assignments
|
|
|
|
|
bool m_inDly; // True in delayed assignments
|
|
|
|
|
bool m_inLoop; // True in for loops
|
|
|
|
|
bool m_inInitial; // True in intial blocks
|
2018-02-02 02:24:41 +00:00
|
|
|
|
typedef std::map<std::pair<AstNodeModule*,string>,AstVar*> VarMap;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
VarMap m_modVarMap; // Table of new var names created under module
|
|
|
|
|
V3Double0 m_statSharedSet;// Statistic tracking
|
2017-10-04 01:26:42 +00:00
|
|
|
|
typedef std::map<AstVarScope*,int> ScopeVecMap;
|
|
|
|
|
ScopeVecMap m_scopeVecMap; // Next var number for each scope
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
|
// METHODS
|
2018-05-14 10:50:47 +00:00
|
|
|
|
VL_DEBUG_FUNC; // Declare debug()
|
2009-01-21 21:56:50 +00:00
|
|
|
|
|
2010-02-01 23:55:32 +00:00
|
|
|
|
void markVarUsage(AstVarScope* nodep, uint32_t flags) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//UINFO(4," MVU "<<flags<<" "<<nodep<<endl);
|
2010-02-01 23:55:32 +00:00
|
|
|
|
nodep->user5( nodep->user5() | flags );
|
|
|
|
|
if ((nodep->user5() & VU_DLY) && (nodep->user5() & VU_NONDLY)) {
|
2018-03-10 21:32:04 +00:00
|
|
|
|
nodep->v3warn(BLKANDNBLK,"Unsupported: Blocked and non-blocking assignments to same variable: "
|
|
|
|
|
<<nodep->varp()->prettyName());
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-14 22:59:24 +00:00
|
|
|
|
AstVarScope* createVarSc(AstVarScope* oldvarscp, const string& name,
|
|
|
|
|
int width/*0==fromoldvar*/, AstNodeDType* newdtypep) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// Because we've already scoped it, we may need to add both the AstVar and the AstVarScope
|
|
|
|
|
if (!oldvarscp->scopep()) oldvarscp->v3fatalSrc("Var unscoped");
|
|
|
|
|
AstVar* varp;
|
2009-11-07 11:20:20 +00:00
|
|
|
|
AstNodeModule* addmodp = oldvarscp->scopep()->modp();
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// We need a new AstVar, but only one for all scopes, to match the new AstVarScope
|
2013-05-18 23:45:40 +00:00
|
|
|
|
VarMap::iterator it = m_modVarMap.find(make_pair(addmodp,name));
|
|
|
|
|
if (it != m_modVarMap.end()) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// Created module's AstVar earlier under some other scope
|
2013-05-18 23:45:40 +00:00
|
|
|
|
varp = it->second;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
} else {
|
2014-03-15 00:22:06 +00:00
|
|
|
|
if (newdtypep) {
|
|
|
|
|
varp = new AstVar (oldvarscp->fileline(), AstVarType::BLOCKTEMP, name, newdtypep);
|
|
|
|
|
} else if (width==0) {
|
2009-11-02 13:06:04 +00:00
|
|
|
|
varp = new AstVar (oldvarscp->fileline(), AstVarType::BLOCKTEMP, name, oldvarscp->varp());
|
2012-04-29 13:42:17 +00:00
|
|
|
|
varp->dtypeFrom(oldvarscp);
|
2012-01-26 13:29:55 +00:00
|
|
|
|
} else { // Used for vset and dimensions, so can zero init
|
2012-03-31 15:10:34 +00:00
|
|
|
|
varp = new AstVar (oldvarscp->fileline(), AstVarType::BLOCKTEMP, name, VFlagBitPacked(), width);
|
2007-11-02 11:23:03 +00:00
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
addmodp->addStmtp(varp);
|
|
|
|
|
m_modVarMap.insert(make_pair(make_pair(addmodp, name), varp));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AstVarScope* varscp = new AstVarScope (oldvarscp->fileline(), oldvarscp->scopep(), varp);
|
|
|
|
|
oldvarscp->scopep()->addVarp(varscp);
|
|
|
|
|
return varscp;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-26 13:10:50 +00:00
|
|
|
|
AstActive* createActivePost(AstVarRef* varrefp) {
|
|
|
|
|
AstActive* newactp = new AstActive (varrefp->fileline(), "sequentdly",
|
|
|
|
|
m_activep->sensesp());
|
2018-06-12 02:05:45 +00:00
|
|
|
|
// Was addNext(), but addNextHere() avoids a linear search.
|
|
|
|
|
m_activep->addNextHere(newactp);
|
2012-01-26 13:10:50 +00:00
|
|
|
|
return newactp;
|
|
|
|
|
}
|
|
|
|
|
void checkActivePost(AstVarRef* varrefp, AstActive* oldactivep) {
|
|
|
|
|
// Check for MULTIDRIVEN, and if so make new sentree that joins old & new sentree
|
|
|
|
|
if (!oldactivep) varrefp->v3fatalSrc("<= old dly assignment not put under sensitivity block");
|
|
|
|
|
if (oldactivep->sensesp() != m_activep->sensesp()) {
|
|
|
|
|
if (!varrefp->varp()->fileline()->warnIsOff(V3ErrorCode::MULTIDRIVEN)
|
|
|
|
|
&& !varrefp->varp()->user2()) {
|
2018-03-10 21:32:04 +00:00
|
|
|
|
varrefp->varp()->v3warn(MULTIDRIVEN,"Signal has multiple driving blocks with different clocking: "
|
|
|
|
|
<<varrefp->varp()->prettyName()<<endl
|
2012-05-22 01:24:17 +00:00
|
|
|
|
<<varrefp->warnMore()<<"... Location of first driving block"<<endl
|
|
|
|
|
<<oldactivep->warnMore()<<"... Location of other driving block");
|
2012-01-26 13:10:50 +00:00
|
|
|
|
varrefp->varp()->user2(true);
|
|
|
|
|
}
|
|
|
|
|
UINFO(4,"AssignDupDlyVar: "<<varrefp<<endl);
|
|
|
|
|
UINFO(4," Act: "<<m_activep<<endl);
|
|
|
|
|
UINFO(4," Act: "<<oldactivep<<endl);
|
|
|
|
|
// Make a new sensitivity list, which is the combination of both blocks
|
|
|
|
|
AstNodeSenItem* sena = m_activep->sensesp()->sensesp()->cloneTree(true);
|
|
|
|
|
AstNodeSenItem* senb = oldactivep->sensesp()->sensesp()->cloneTree(true);
|
|
|
|
|
AstSenTree* treep = new AstSenTree(m_activep->fileline(), sena);
|
|
|
|
|
if (senb) treep->addSensesp(senb);
|
|
|
|
|
if (AstSenTree* storep = oldactivep->sensesStorep()) {
|
|
|
|
|
storep->unlinkFrBack();
|
|
|
|
|
pushDeletep(storep);
|
|
|
|
|
}
|
|
|
|
|
oldactivep->sensesStorep(treep);
|
|
|
|
|
oldactivep->sensesp(treep);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
AstNode* createDlyArray(AstAssignDly* nodep, AstNode* lhsp) {
|
|
|
|
|
// Create delayed assignment
|
|
|
|
|
// See top of this file for transformation
|
|
|
|
|
// Return the new LHS for the assignment, Null = unlink
|
|
|
|
|
// Find selects
|
|
|
|
|
AstNode* newlhsp = NULL; // NULL = unlink old assign
|
|
|
|
|
AstSel* bitselp = NULL;
|
|
|
|
|
AstArraySel* arrayselp = NULL;
|
2018-02-02 02:32:58 +00:00
|
|
|
|
if (VN_IS(lhsp, Sel)) {
|
|
|
|
|
bitselp = VN_CAST(lhsp, Sel);
|
|
|
|
|
arrayselp = VN_CAST(bitselp->fromp(), ArraySel);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
} else {
|
2018-02-02 02:32:58 +00:00
|
|
|
|
arrayselp = VN_CAST(lhsp, ArraySel);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
if (!arrayselp) nodep->v3fatalSrc("No arraysel under bitsel?");
|
2018-03-10 21:51:34 +00:00
|
|
|
|
if (VN_IS(arrayselp->dtypep()->skipRefp(), UnpackArrayDType)) {
|
2018-03-10 21:32:04 +00:00
|
|
|
|
nodep->v3fatalSrc("ArraySel with unpacked arrays should have been removed in V3Slice");
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
UINFO(4,"AssignDlyArray: "<<nodep<<endl);
|
|
|
|
|
//
|
|
|
|
|
//=== Dimensions: __Vdlyvdim__
|
2018-02-02 02:24:41 +00:00
|
|
|
|
std::deque<AstNode*> dimvalp; // Assignment value for each dimension of assignment
|
2006-08-26 11:35:28 +00:00
|
|
|
|
AstNode* dimselp=arrayselp;
|
2018-02-02 02:32:58 +00:00
|
|
|
|
for (; VN_IS(dimselp, ArraySel); dimselp=VN_CAST(dimselp, ArraySel)->fromp()) {
|
|
|
|
|
AstNode* valp = VN_CAST(dimselp, ArraySel)->bitp()->unlinkFrBack();
|
2006-08-26 11:35:28 +00:00
|
|
|
|
dimvalp.push_front(valp);
|
|
|
|
|
}
|
2018-02-02 02:32:58 +00:00
|
|
|
|
AstVarRef* varrefp = VN_CAST(dimselp, VarRef);
|
2017-04-29 00:09:27 +00:00
|
|
|
|
if (!varrefp) nodep->v3fatalSrc("No var underneath arraysels");
|
|
|
|
|
if (!varrefp->varScopep()) varrefp->v3fatalSrc("Var didn't get varscoped in V3Scope.cpp");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
varrefp->unlinkFrBack();
|
|
|
|
|
AstVar* oldvarp = varrefp->varp();
|
2017-10-04 01:26:42 +00:00
|
|
|
|
int modVecNum = m_scopeVecMap[varrefp->varScopep()]++;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
2018-02-02 02:24:41 +00:00
|
|
|
|
std::deque<AstNode*> dimreadps; // Read value for each dimension of assignment
|
2006-08-26 11:35:28 +00:00
|
|
|
|
for (unsigned dimension=0; dimension<dimvalp.size(); dimension++) {
|
|
|
|
|
AstNode* dimp = dimvalp[dimension];
|
2018-02-02 02:32:58 +00:00
|
|
|
|
if (VN_IS(dimp, Const)) { // bit = const, can just use it
|
2006-08-26 11:35:28 +00:00
|
|
|
|
dimreadps.push_front(dimp);
|
|
|
|
|
} else {
|
|
|
|
|
string bitvarname = (string("__Vdlyvdim")+cvtToStr(dimension)
|
2008-11-18 02:02:10 +00:00
|
|
|
|
+"__"+oldvarp->shortName()+"__v"+cvtToStr(modVecNum));
|
2014-03-15 00:22:06 +00:00
|
|
|
|
AstVarScope* bitvscp = createVarSc(varrefp->varScopep(), bitvarname, dimp->width(), NULL);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
AstAssign* bitassignp
|
|
|
|
|
= new AstAssign (nodep->fileline(),
|
|
|
|
|
new AstVarRef(nodep->fileline(), bitvscp, true),
|
|
|
|
|
dimp);
|
|
|
|
|
nodep->addNextHere(bitassignp);
|
|
|
|
|
dimreadps.push_front(new AstVarRef(nodep->fileline(), bitvscp, false));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
//=== Bitselect: __Vdlyvlsb__
|
|
|
|
|
AstNode* bitreadp=NULL; // Code to read Vdlyvlsb
|
|
|
|
|
if (bitselp) {
|
|
|
|
|
AstNode* lsbvaluep = bitselp->lsbp()->unlinkFrBack();
|
2018-02-02 02:32:58 +00:00
|
|
|
|
if (VN_IS(bitselp->fromp(), Const)) { // vlsb = constant, can just push constant into where we use it
|
2006-08-26 11:35:28 +00:00
|
|
|
|
bitreadp = lsbvaluep;
|
|
|
|
|
} else {
|
2008-11-18 02:02:10 +00:00
|
|
|
|
string bitvarname = (string("__Vdlyvlsb__")+oldvarp->shortName()+"__v"+cvtToStr(modVecNum));
|
2014-03-15 00:22:06 +00:00
|
|
|
|
AstVarScope* bitvscp = createVarSc(varrefp->varScopep(), bitvarname, lsbvaluep->width(), NULL);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
AstAssign* bitassignp = new AstAssign (nodep->fileline(),
|
|
|
|
|
new AstVarRef(nodep->fileline(), bitvscp, true),
|
|
|
|
|
lsbvaluep);
|
|
|
|
|
nodep->addNextHere(bitassignp);
|
|
|
|
|
bitreadp = new AstVarRef(nodep->fileline(), bitvscp, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
//=== Value: __Vdlyvval__
|
|
|
|
|
AstNode* valreadp; // Code to read Vdlyvval
|
2018-02-02 02:32:58 +00:00
|
|
|
|
if (VN_IS(nodep->rhsp(), Const)) { // vval = constant, can just push constant into where we use it
|
2006-08-26 11:35:28 +00:00
|
|
|
|
valreadp = nodep->rhsp()->unlinkFrBack();
|
|
|
|
|
} else {
|
2008-11-18 02:02:10 +00:00
|
|
|
|
string valvarname = (string("__Vdlyvval__")+oldvarp->shortName()+"__v"+cvtToStr(modVecNum));
|
2014-03-15 00:22:06 +00:00
|
|
|
|
AstVarScope* valvscp = createVarSc(varrefp->varScopep(), valvarname, 0, nodep->rhsp()->dtypep());
|
2006-08-26 11:35:28 +00:00
|
|
|
|
newlhsp = new AstVarRef(nodep->fileline(), valvscp, true);
|
|
|
|
|
valreadp = new AstVarRef(nodep->fileline(), valvscp, false);
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
//=== Setting/not setting boolean: __Vdlyvset__
|
|
|
|
|
AstVarScope* setvscp;
|
2012-01-26 13:10:50 +00:00
|
|
|
|
AstAssignPre* setinitp = NULL;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
2008-11-25 12:57:02 +00:00
|
|
|
|
if (nodep->user3p()) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// Simplistic optimization. If the previous statement in same scope was also a =>,
|
2008-11-25 12:57:02 +00:00
|
|
|
|
// then we told this nodep->user3 we can use its Vdlyvset rather than making a new one.
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// This is good for code like:
|
|
|
|
|
// for (i=0; i<5; i++) vector[i] <= something;
|
2018-02-02 02:32:58 +00:00
|
|
|
|
setvscp = VN_CAST(nodep->user3p(), VarScope);
|
2011-08-05 01:15:24 +00:00
|
|
|
|
++m_statSharedSet;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
} else { // Create new one
|
2008-11-18 02:02:10 +00:00
|
|
|
|
string setvarname = (string("__Vdlyvset__")+oldvarp->shortName()+"__v"+cvtToStr(modVecNum));
|
2014-03-15 00:22:06 +00:00
|
|
|
|
setvscp = createVarSc(varrefp->varScopep(), setvarname, 1, NULL);
|
2012-01-26 13:10:50 +00:00
|
|
|
|
setinitp = new AstAssignPre (nodep->fileline(),
|
|
|
|
|
new AstVarRef(nodep->fileline(), setvscp, true),
|
|
|
|
|
new AstConst(nodep->fileline(), 0));
|
2006-08-26 11:35:28 +00:00
|
|
|
|
AstAssign* setassignp
|
|
|
|
|
= new AstAssign (nodep->fileline(),
|
|
|
|
|
new AstVarRef(nodep->fileline(), setvscp, true),
|
|
|
|
|
new AstConst(nodep->fileline(),
|
|
|
|
|
V3Number(nodep->fileline(),1,true)));
|
|
|
|
|
nodep->addNextHere(setassignp);
|
|
|
|
|
}
|
|
|
|
|
if (m_nextDlyp) { // Tell next assigndly it can share the variable
|
2008-11-25 12:57:02 +00:00
|
|
|
|
m_nextDlyp->user3p(setvscp);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
// Create ALWAYSPOST for delayed variable
|
|
|
|
|
// We add all logic to the same block if it's for the same memory
|
2017-11-28 01:11:34 +00:00
|
|
|
|
// This ensures that multiple assignments to the same memory will result
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// in correctly ordered code - the last assignment must be last.
|
|
|
|
|
// It also has the nice side effect of assisting cache locality.
|
|
|
|
|
AstNode* selectsp = varrefp;
|
|
|
|
|
for (int dimension=int(dimreadps.size())-1; dimension>=0; --dimension) {
|
|
|
|
|
selectsp = new AstArraySel(nodep->fileline(), selectsp, dimreadps[dimension]);
|
|
|
|
|
}
|
|
|
|
|
if (bitselp) {
|
|
|
|
|
selectsp = new AstSel(nodep->fileline(), selectsp, bitreadp,
|
|
|
|
|
bitselp->widthp()->cloneTree(false));
|
|
|
|
|
}
|
|
|
|
|
// Build "IF (changeit) ...
|
|
|
|
|
UINFO(9," For "<<setvscp<<endl);
|
|
|
|
|
UINFO(9," & "<<varrefp<<endl);
|
2018-02-02 02:32:58 +00:00
|
|
|
|
AstAlwaysPost* finalp = VN_CAST(varrefp->varScopep()->user4p(), AlwaysPost);
|
2012-01-26 13:10:50 +00:00
|
|
|
|
if (finalp) {
|
2018-02-02 02:32:58 +00:00
|
|
|
|
AstActive* oldactivep = VN_CAST(finalp->user2p(), Active);
|
2012-01-26 13:10:50 +00:00
|
|
|
|
checkActivePost(varrefp, oldactivep);
|
|
|
|
|
if (setinitp) oldactivep->addStmtsp(setinitp);
|
|
|
|
|
} else { // first time we've dealt with this memory
|
2006-08-26 11:35:28 +00:00
|
|
|
|
finalp = new AstAlwaysPost(nodep->fileline(), NULL/*sens*/, NULL/*body*/);
|
2008-06-10 01:25:10 +00:00
|
|
|
|
UINFO(9," Created "<<finalp<<endl);
|
2012-01-26 13:10:50 +00:00
|
|
|
|
AstActive* newactp = createActivePost(varrefp);
|
|
|
|
|
newactp->addStmtsp(finalp);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
varrefp->varScopep()->user4p(finalp);
|
2012-01-26 13:10:50 +00:00
|
|
|
|
finalp->user2p(newactp);
|
|
|
|
|
if (setinitp) newactp->addStmtsp(setinitp);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
AstIf* postLogicp;
|
2016-11-27 14:40:12 +00:00
|
|
|
|
if (finalp->user3p() == setvscp) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// Optimize as above; if sharing Vdlyvset *ON SAME VARIABLE*,
|
|
|
|
|
// we can share the IF statement too
|
2018-02-02 02:32:58 +00:00
|
|
|
|
postLogicp = VN_CAST(finalp->user4p(), If);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (!postLogicp) nodep->v3fatalSrc("Delayed assignment misoptimized; prev var found w/o associated IF");
|
|
|
|
|
} else {
|
|
|
|
|
postLogicp = new AstIf (nodep->fileline(),
|
|
|
|
|
new AstVarRef(nodep->fileline(), setvscp, false),
|
|
|
|
|
NULL,
|
|
|
|
|
NULL);
|
2008-06-10 01:25:10 +00:00
|
|
|
|
UINFO(9," Created "<<postLogicp<<endl);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
finalp->addBodysp(postLogicp);
|
2008-11-25 12:57:02 +00:00
|
|
|
|
finalp->user3p(setvscp); // Remember IF's vset variable
|
2006-08-26 11:35:28 +00:00
|
|
|
|
finalp->user4p(postLogicp); // and the associated IF, as we may be able to reuse it
|
|
|
|
|
}
|
|
|
|
|
postLogicp->addIfsp(new AstAssign(nodep->fileline(), selectsp, valreadp));
|
|
|
|
|
return newlhsp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VISITORS
|
2016-11-27 13:11:38 +00:00
|
|
|
|
virtual void visit(AstNetlist* nodep) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//VV***** We reset all userp() on the netlist
|
|
|
|
|
m_modVarMap.clear();
|
2018-05-11 00:55:37 +00:00
|
|
|
|
iterateChildren(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
2016-11-27 13:11:38 +00:00
|
|
|
|
virtual void visit(AstScope* nodep) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
UINFO(4," MOD "<<nodep<<endl);
|
2008-11-25 12:57:02 +00:00
|
|
|
|
AstNode::user3ClearTree();
|
2018-05-11 00:55:37 +00:00
|
|
|
|
iterateChildren(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
2016-11-27 13:11:38 +00:00
|
|
|
|
virtual void visit(AstCFunc* nodep) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_cfuncp = nodep;
|
2018-05-11 00:55:37 +00:00
|
|
|
|
iterateChildren(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_cfuncp = NULL;
|
|
|
|
|
}
|
2016-11-27 13:11:38 +00:00
|
|
|
|
virtual void visit(AstActive* nodep) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_activep = nodep;
|
|
|
|
|
bool oldinit = m_inInitial;
|
|
|
|
|
m_inInitial = nodep->hasInitial();
|
2012-01-26 13:10:50 +00:00
|
|
|
|
AstNode::user3ClearTree(); // Two sets to same variable in different actives must use different vars.
|
2018-05-11 00:55:37 +00:00
|
|
|
|
iterateChildren(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_inInitial = oldinit;
|
|
|
|
|
}
|
2016-11-27 13:11:38 +00:00
|
|
|
|
virtual void visit(AstAssignDly* nodep) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_inDly = true;
|
2018-02-02 02:32:58 +00:00
|
|
|
|
m_nextDlyp = VN_CAST(nodep->nextp(), AssignDly); // Next assignment in same block, maybe NULL.
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (m_cfuncp) nodep->v3error("Unsupported: Delayed assignment inside public function/task");
|
2018-02-02 02:32:58 +00:00
|
|
|
|
if (VN_IS(nodep->lhsp(), ArraySel)
|
|
|
|
|
|| (VN_IS(nodep->lhsp(), Sel)
|
|
|
|
|
&& VN_IS(VN_CAST(nodep->lhsp(), Sel)->fromp(), ArraySel))) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
AstNode* lhsp = nodep->lhsp()->unlinkFrBack();
|
|
|
|
|
AstNode* newlhsp = createDlyArray(nodep, lhsp);
|
2017-10-02 03:23:02 +00:00
|
|
|
|
if (m_inLoop) nodep->v3warn(BLKLOOPINIT,"Unsupported: Delayed assignment to array inside for loops (non-delayed is ok - see docs)");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (newlhsp) {
|
|
|
|
|
nodep->lhsp(newlhsp);
|
|
|
|
|
} else {
|
2015-10-04 17:16:35 +00:00
|
|
|
|
nodep->unlinkFrBack()->deleteTree(); VL_DANGLING(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
2015-10-04 17:16:35 +00:00
|
|
|
|
lhsp->deleteTree(); VL_DANGLING(lhsp);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2018-05-11 00:55:37 +00:00
|
|
|
|
iterateChildren(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
m_inDly = false;
|
|
|
|
|
m_nextDlyp = NULL;
|
|
|
|
|
}
|
2012-01-26 13:10:50 +00:00
|
|
|
|
|
2016-11-27 13:11:38 +00:00
|
|
|
|
virtual void visit(AstVarRef* nodep) {
|
2011-01-19 02:19:12 +00:00
|
|
|
|
if (!nodep->user2Inc()) { // Not done yet
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (m_inDly && nodep->lvalue()) {
|
|
|
|
|
UINFO(4,"AssignDlyVar: "<<nodep<<endl);
|
2010-02-01 23:55:32 +00:00
|
|
|
|
markVarUsage(nodep->varScopep(), VU_DLY);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (!m_activep) nodep->v3fatalSrc("<= not under sensitivity block");
|
2018-03-10 21:32:04 +00:00
|
|
|
|
if (!m_activep->hasClocked()) {
|
|
|
|
|
nodep->v3error("Internal: Blocking <= assignment in non-clocked block, should have converted in V3Active");
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
AstVarScope* oldvscp = nodep->varScopep();
|
2017-04-29 00:09:27 +00:00
|
|
|
|
if (!oldvscp) nodep->v3fatalSrc("Var didn't get varscoped in V3Scope.cpp");
|
2018-02-02 02:32:58 +00:00
|
|
|
|
AstVarScope* dlyvscp = VN_CAST(oldvscp->user1p(), VarScope);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (dlyvscp) { // Multiple use of delayed variable
|
2018-02-02 02:32:58 +00:00
|
|
|
|
AstActive* oldactivep = VN_CAST(dlyvscp->user2p(), Active);
|
2012-01-26 13:10:50 +00:00
|
|
|
|
checkActivePost(nodep, oldactivep);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
if (!dlyvscp) { // First use of this delayed variable
|
|
|
|
|
string newvarname = (string("__Vdly__")+nodep->varp()->shortName());
|
2014-03-15 00:22:06 +00:00
|
|
|
|
dlyvscp = createVarSc(oldvscp, newvarname, 0, NULL);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
AstNodeAssign* prep
|
|
|
|
|
= new AstAssignPre (nodep->fileline(),
|
|
|
|
|
new AstVarRef(nodep->fileline(), dlyvscp, true),
|
|
|
|
|
new AstVarRef(nodep->fileline(), oldvscp, false));
|
|
|
|
|
AstNodeAssign* postp
|
|
|
|
|
= new AstAssignPost (nodep->fileline(),
|
|
|
|
|
new AstVarRef(nodep->fileline(), oldvscp, true),
|
|
|
|
|
new AstVarRef(nodep->fileline(), dlyvscp, false));
|
|
|
|
|
postp->lhsp()->user2(true); // Don't detect this assignment
|
2008-11-25 14:03:49 +00:00
|
|
|
|
oldvscp->user1p(dlyvscp); // So we can find it later
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// Make new ACTIVE with identical sensitivity tree
|
2012-01-26 13:10:50 +00:00
|
|
|
|
AstActive* newactp = createActivePost(nodep);
|
|
|
|
|
dlyvscp->user2p(newactp);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
newactp->addStmtsp(prep); // Add to FRONT of statements
|
|
|
|
|
newactp->addStmtsp(postp);
|
|
|
|
|
}
|
|
|
|
|
AstVarRef* newrefp = new AstVarRef(nodep->fileline(), dlyvscp, true);
|
|
|
|
|
newrefp->user2(true); // No reason to do it again
|
2015-10-04 17:16:35 +00:00
|
|
|
|
nodep->replaceWith(newrefp); nodep->deleteTree(); VL_DANGLING(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
else if (!m_inDly && nodep->lvalue()) {
|
|
|
|
|
//UINFO(9,"NBA "<<nodep<<endl);
|
|
|
|
|
if (!m_inInitial) {
|
2009-04-08 18:33:12 +00:00
|
|
|
|
UINFO(4,"AssignNDlyVar: "<<nodep<<endl);
|
2010-02-01 23:55:32 +00:00
|
|
|
|
markVarUsage(nodep->varScopep(), VU_NONDLY);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-27 13:11:38 +00:00
|
|
|
|
virtual void visit(AstNodeFor* nodep) {
|
2017-04-29 00:09:27 +00:00
|
|
|
|
nodep->v3fatalSrc("For statements should have been converted to while statements in V3Begin");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
2016-11-27 13:11:38 +00:00
|
|
|
|
virtual void visit(AstWhile* nodep) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
bool oldloop = m_inLoop;
|
|
|
|
|
m_inLoop = true;
|
2018-05-11 00:55:37 +00:00
|
|
|
|
iterateChildren(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_inLoop = oldloop;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------
|
|
|
|
|
// Default: Just iterate
|
2016-11-27 13:11:38 +00:00
|
|
|
|
virtual void visit(AstNode* nodep) {
|
2018-05-11 00:55:37 +00:00
|
|
|
|
iterateChildren(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// CONSTUCTORS
|
2015-10-04 02:33:06 +00:00
|
|
|
|
explicit DelayedVisitor(AstNetlist* nodep) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_inDly = false;
|
|
|
|
|
m_activep=NULL;
|
|
|
|
|
m_cfuncp=NULL;
|
|
|
|
|
m_nextDlyp=NULL;
|
|
|
|
|
m_inLoop = false;
|
|
|
|
|
m_inInitial = false;
|
|
|
|
|
|
2018-05-11 00:55:37 +00:00
|
|
|
|
iterate(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
virtual ~DelayedVisitor() {
|
|
|
|
|
V3Stats::addStat("Optimizations, Delayed shared-sets", m_statSharedSet);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Delayed class functions
|
|
|
|
|
|
|
|
|
|
void V3Delayed::delayedAll(AstNetlist* nodep) {
|
|
|
|
|
UINFO(2,__FUNCTION__<<": "<<endl);
|
2018-03-10 17:57:50 +00:00
|
|
|
|
{
|
|
|
|
|
DelayedVisitor visitor (nodep);
|
|
|
|
|
} // Destruct before checking
|
2017-09-18 02:52:57 +00:00
|
|
|
|
V3Global::dumpCheckGlobalTree("delayed", 0, v3Global.opt.dumpTreeLevel(__FILE__) >= 3);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|