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: Add C++ casts across expression size changes
|
|
|
|
|
//
|
2019-11-08 04:33:59 +01:00
|
|
|
// Code available from: https://verilator.org
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2026-01-01 13:22:09 +01:00
|
|
|
// Copyright 2004-2026 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
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
// V3Cast's Transformations:
|
2008-06-10 03:25:10 +02:00
|
|
|
//
|
2006-08-26 13:35:28 +02:00
|
|
|
// Each module:
|
2022-10-12 11:19:21 +02:00
|
|
|
// For each expression, if above expression requires 32 bits,
|
2019-05-19 22:13:13 +02:00
|
|
|
// and this isn't, cast to 32 bits.
|
2022-10-12 11:19:21 +02:00
|
|
|
// Likewise for 64 bit expressions.
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
|
|
|
|
// C++ rules:
|
2019-05-19 22:13:13 +02:00
|
|
|
// Integral promotions allow conversion to larger int. Unsigned is only
|
|
|
|
|
// used if a int would not fit the value.
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
2019-05-19 22:13:13 +02:00
|
|
|
// Bools converts to int, not unsigned.
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
2019-05-19 22:13:13 +02:00
|
|
|
// Most operations return unsigned if either operand is unsigned.
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
2019-05-19 22:13:13 +02:00
|
|
|
// Unsignedness can be lost on results of the below operations, as they
|
|
|
|
|
// may need the sign bit for proper operation:
|
|
|
|
|
// /, %, /=, %=, <, <=, >, or >=
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
2019-05-19 22:13:13 +02:00
|
|
|
// Signed values are always sign extended on promotion or right shift,
|
|
|
|
|
// even if assigning to a unsigned.
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
2019-10-05 02:17:11 +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 "V3Cast.h"
|
|
|
|
|
|
2022-09-18 21:53:42 +02:00
|
|
|
VL_DEFINE_DEBUG_FUNCTIONS;
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
//######################################################################
|
|
|
|
|
// Cast state, as a visitor of each AstNode
|
|
|
|
|
|
2022-01-02 19:56:40 +01:00
|
|
|
class CastVisitor final : public VNVisitor {
|
2006-08-26 13:35:28 +02:00
|
|
|
// NODE STATE
|
|
|
|
|
// Entire netlist:
|
2023-10-07 03:33:31 +02:00
|
|
|
// AstNode::user1() // bool. Indicates node is of known size
|
2022-01-02 19:56:40 +01:00
|
|
|
const VNUser1InUse m_inuser1;
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
// STATE
|
|
|
|
|
|
|
|
|
|
// METHODS
|
2009-01-21 22:56:50 +01:00
|
|
|
|
2022-11-13 21:33:11 +01:00
|
|
|
void insertCast(AstNodeExpr* nodep, int needsize) { // We'll insert ABOVE passed node
|
2022-01-02 16:32:35 +01:00
|
|
|
VNRelinker relinkHandle;
|
2019-05-19 22:13:13 +02:00
|
|
|
nodep->unlinkFrBack(&relinkHandle);
|
|
|
|
|
//
|
2021-07-12 00:42:01 +02:00
|
|
|
AstCCast* const castp
|
|
|
|
|
= new AstCCast{nodep->fileline(), nodep, needsize, nodep->widthMin()};
|
2025-05-23 02:29:32 +02:00
|
|
|
UINFO(4, " MadeCast " << static_cast<void*>(castp) << " for " << nodep);
|
2019-05-19 22:13:13 +02:00
|
|
|
relinkHandle.relink(castp);
|
2025-08-02 19:44:40 +02:00
|
|
|
// UINFOTREE(9, castp, "", "castins");
|
2019-05-19 22:13:13 +02:00
|
|
|
//
|
2020-01-25 02:10:44 +01:00
|
|
|
ensureLower32Cast(castp);
|
2019-05-19 22:13:13 +02:00
|
|
|
nodep->user1(1); // Now must be of known size
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2025-08-19 23:02:10 +02:00
|
|
|
static int castSize(const AstNode* nodep) {
|
2020-04-15 13:58:34 +02:00
|
|
|
if (nodep->isQuad()) {
|
|
|
|
|
return VL_QUADSIZE;
|
|
|
|
|
} else if (nodep->width() <= 8) {
|
|
|
|
|
return 8;
|
|
|
|
|
} else if (nodep->width() <= 16) {
|
|
|
|
|
return 16;
|
|
|
|
|
} else {
|
|
|
|
|
return VL_IDATASIZE;
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2022-11-13 21:33:11 +01:00
|
|
|
void ensureCast(AstNodeExpr* nodep) {
|
2020-04-15 13:58:34 +02:00
|
|
|
if (castSize(nodep->backp()) != castSize(nodep) || !nodep->user1()) {
|
2024-03-16 13:49:36 +01:00
|
|
|
if (!nodep->isNull()) insertCast(nodep, castSize(nodep->backp()));
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2025-08-20 19:21:24 +02:00
|
|
|
// cppcheck-suppress constParameterPointer // lhsp might be changed
|
2020-01-25 02:10:44 +01:00
|
|
|
void ensureLower32Cast(AstCCast* nodep) {
|
2019-05-19 22:13:13 +02:00
|
|
|
// If we have uint64 = CAST(uint64(x)) then the upcasting
|
|
|
|
|
// really needs to be CAST(uint64(CAST(uint32(x))).
|
|
|
|
|
// Otherwise a (uint64)(a>b) would return wrong value, as
|
2019-09-09 13:50:21 +02:00
|
|
|
// less than has nondeterministic signedness.
|
2020-04-15 13:58:34 +02:00
|
|
|
if (nodep->isQuad() && !nodep->lhsp()->isQuad() && !VN_IS(nodep->lhsp(), CCast)) {
|
2019-12-09 03:36:38 +01:00
|
|
|
insertCast(nodep->lhsp(), VL_IDATASIZE);
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2022-11-13 21:33:11 +01:00
|
|
|
void ensureNullChecked(AstNodeExpr* nodep) {
|
2020-04-05 15:30:23 +02:00
|
|
|
// TODO optimize to track null checked values and avoid where possible
|
|
|
|
|
if (!VN_IS(nodep->backp(), NullCheck)) {
|
2022-01-02 16:32:35 +01:00
|
|
|
VNRelinker relinkHandle;
|
2020-04-05 15:30:23 +02:00
|
|
|
nodep->unlinkFrBack(&relinkHandle);
|
2022-11-13 21:33:11 +01:00
|
|
|
relinkHandle.relink(new AstNullCheck{nodep->fileline(), nodep});
|
2020-04-05 15:30:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
// VISITORS
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNodeUniop* nodep) override {
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2019-05-19 22:13:13 +02:00
|
|
|
nodep->user1(nodep->lhsp()->user1());
|
2020-01-25 02:10:44 +01:00
|
|
|
if (nodep->sizeMattersLhs()) ensureCast(nodep->lhsp());
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNodeBiop* nodep) override {
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2020-04-15 13:58:34 +02:00
|
|
|
nodep->user1(nodep->lhsp()->user1() | nodep->rhsp()->user1());
|
2020-01-25 02:10:44 +01:00
|
|
|
if (nodep->sizeMattersLhs()) ensureCast(nodep->lhsp());
|
|
|
|
|
if (nodep->sizeMattersRhs()) ensureCast(nodep->rhsp());
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2025-08-16 00:49:06 +02:00
|
|
|
void visit(AstCond* nodep) override {
|
2023-05-30 14:59:00 +02:00
|
|
|
// All class types are castable to each other. If they are of different types,
|
|
|
|
|
// a compilation error will be thrown, so an explicit cast is required. Types were
|
|
|
|
|
// already checked by V3Width and dtypep of a condition operator is a type of their
|
2023-11-11 05:25:53 +01:00
|
|
|
// common base class, so both classes can be safely casted.
|
2023-05-30 14:59:00 +02:00
|
|
|
const AstClassRefDType* const thenClassDtypep
|
2024-03-16 14:12:10 +01:00
|
|
|
= VN_CAST(nodep->thenp()->dtypep()->skipRefp(), ClassRefDType);
|
2023-05-30 14:59:00 +02:00
|
|
|
const AstClassRefDType* const elseClassDtypep
|
2024-03-16 14:12:10 +01:00
|
|
|
= VN_CAST(nodep->elsep()->dtypep()->skipRefp(), ClassRefDType);
|
2023-05-30 14:59:00 +02:00
|
|
|
const bool castRequired = thenClassDtypep && elseClassDtypep
|
|
|
|
|
&& (thenClassDtypep->classp() != elseClassDtypep->classp());
|
|
|
|
|
if (castRequired) {
|
|
|
|
|
const AstClass* const commonBaseClassp
|
2024-03-16 14:12:10 +01:00
|
|
|
= VN_AS(nodep->dtypep()->skipRefp(), ClassRefDType)->classp();
|
2023-05-30 14:59:00 +02:00
|
|
|
if (thenClassDtypep->classp() != commonBaseClassp) {
|
|
|
|
|
AstNodeExpr* thenp = nodep->thenp()->unlinkFrBack();
|
|
|
|
|
nodep->thenp(new AstCCast{thenp->fileline(), thenp, nodep});
|
|
|
|
|
}
|
|
|
|
|
if (elseClassDtypep->classp() != commonBaseClassp) {
|
|
|
|
|
AstNodeExpr* elsep = nodep->elsep()->unlinkFrBack();
|
|
|
|
|
nodep->elsep(new AstCCast{elsep->fileline(), elsep, nodep});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
visit(static_cast<AstNodeTriop*>(nodep));
|
|
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNodeTriop* nodep) override {
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2020-04-15 13:58:34 +02:00
|
|
|
nodep->user1(nodep->lhsp()->user1() | nodep->rhsp()->user1() | nodep->thsp()->user1());
|
2020-01-25 02:10:44 +01:00
|
|
|
if (nodep->sizeMattersLhs()) ensureCast(nodep->lhsp());
|
|
|
|
|
if (nodep->sizeMattersRhs()) ensureCast(nodep->rhsp());
|
|
|
|
|
if (nodep->sizeMattersThs()) ensureCast(nodep->thsp());
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNodeQuadop* nodep) override {
|
2020-05-10 20:27:22 +02:00
|
|
|
iterateChildren(nodep);
|
|
|
|
|
nodep->user1(nodep->lhsp()->user1() | nodep->rhsp()->user1() | nodep->thsp()->user1()
|
|
|
|
|
| nodep->fhsp()->user1());
|
|
|
|
|
if (nodep->sizeMattersLhs()) ensureCast(nodep->lhsp());
|
|
|
|
|
if (nodep->sizeMattersRhs()) ensureCast(nodep->rhsp());
|
|
|
|
|
if (nodep->sizeMattersThs()) ensureCast(nodep->thsp());
|
|
|
|
|
if (nodep->sizeMattersFhs()) ensureCast(nodep->fhsp());
|
|
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstCCast* nodep) override {
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2020-01-25 02:10:44 +01:00
|
|
|
ensureLower32Cast(nodep);
|
2019-05-19 22:13:13 +02:00
|
|
|
nodep->user1(1);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2023-10-18 23:51:25 +02:00
|
|
|
void visit(AstConsPackMember* nodep) override {
|
|
|
|
|
iterateChildren(nodep);
|
|
|
|
|
if (VN_IS(nodep->rhsp()->dtypep()->skipRefp(), BasicDType)) ensureCast(nodep->rhsp());
|
|
|
|
|
nodep->user1(1);
|
|
|
|
|
}
|
2023-10-07 03:33:31 +02:00
|
|
|
void visit(AstExprStmt* nodep) override {
|
|
|
|
|
iterateChildren(nodep);
|
|
|
|
|
nodep->user1(1);
|
|
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNegate* nodep) override {
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2019-05-19 22:13:13 +02:00
|
|
|
nodep->user1(nodep->lhsp()->user1());
|
2025-07-28 08:20:57 +02:00
|
|
|
if (nodep->lhsp()->widthMin() == 1 && !nodep->lhsp()->isWide()) {
|
2019-05-19 22:13:13 +02:00
|
|
|
// We want to avoid a GCC "converting of negative value" warning
|
|
|
|
|
// from our expansion of
|
|
|
|
|
// out = {32{a<b}} => out = - (a<b)
|
|
|
|
|
insertCast(nodep->lhsp(), castSize(nodep));
|
|
|
|
|
} else {
|
2025-07-28 08:20:57 +02:00
|
|
|
if (nodep->sizeMattersLhs()) ensureCast(nodep->lhsp());
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstVarRef* nodep) override {
|
2021-11-13 16:42:26 +01:00
|
|
|
const AstNode* const backp = nodep->backp();
|
2022-10-12 11:19:21 +02:00
|
|
|
if (nodep->access().isReadOnly() && VN_IS(backp, NodeExpr) && !VN_IS(backp, CCast)
|
|
|
|
|
&& !VN_IS(backp, NodeCCall) && !VN_IS(backp, CMethodHard) && !VN_IS(backp, SFormatF)
|
2022-12-21 01:22:42 +01:00
|
|
|
&& !VN_IS(backp, ArraySel) && !VN_IS(backp, StructSel) && !VN_IS(backp, RedXor)
|
2025-10-31 19:29:11 +01:00
|
|
|
&& (nodep->varp()->basicp() && !nodep->varp()->basicp()->isForkSync()
|
2023-10-26 16:38:47 +02:00
|
|
|
&& !nodep->varp()->basicp()->isProcessRef() && !nodep->varp()->basicp()->isEvent())
|
2022-05-15 17:03:32 +02:00
|
|
|
&& backp->width() && castSize(nodep) != castSize(nodep->varp())) {
|
2019-05-19 22:13:13 +02:00
|
|
|
// Cast vars to IData first, else below has upper bits wrongly set
|
|
|
|
|
// CData x=3; out = (QData)(x<<30);
|
2018-08-25 15:52:45 +02:00
|
|
|
insertCast(nodep, castSize(nodep));
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
|
|
|
|
nodep->user1(1);
|
2007-10-31 20:22:26 +01:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstConst* nodep) override {
|
2019-09-09 13:50:21 +02:00
|
|
|
// Constants are of unknown size if smaller than 33 bits, because
|
2019-05-19 22:13:13 +02:00
|
|
|
// we're too lazy to wrap every constant in the universe in
|
|
|
|
|
// ((IData)#).
|
2024-03-16 13:49:36 +01:00
|
|
|
nodep->user1(nodep->isQuad() || nodep->isWide() || nodep->isNull());
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-05 15:30:23 +02:00
|
|
|
// Null dereference protection
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNullCheck* nodep) override {
|
2020-04-05 15:30:23 +02:00
|
|
|
iterateChildren(nodep);
|
|
|
|
|
nodep->user1(nodep->lhsp()->user1());
|
|
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstCMethodCall* nodep) override {
|
2020-04-05 15:30:23 +02:00
|
|
|
iterateChildren(nodep);
|
|
|
|
|
ensureNullChecked(nodep->fromp());
|
2024-02-25 14:19:53 +01:00
|
|
|
nodep->user1(true);
|
|
|
|
|
}
|
|
|
|
|
void visit(AstCMethodHard* nodep) override {
|
|
|
|
|
iterateChildren(nodep);
|
|
|
|
|
nodep->user1(true);
|
2020-04-05 15:30:23 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstMemberSel* nodep) override {
|
2020-04-05 15:30:23 +02:00
|
|
|
iterateChildren(nodep);
|
|
|
|
|
ensureNullChecked(nodep->fromp());
|
2025-07-28 08:20:57 +02:00
|
|
|
nodep->user1(true);
|
|
|
|
|
}
|
|
|
|
|
void visit(AstStructSel* nodep) override {
|
|
|
|
|
iterateChildren(nodep);
|
|
|
|
|
nodep->user1(true);
|
2020-04-05 15:30:23 +02:00
|
|
|
}
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
// NOPs
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstVar*) override {}
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
//--------------------
|
2022-09-16 12:22:11 +02:00
|
|
|
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
|
2020-04-15 13:58:34 +02:00
|
|
|
explicit CastVisitor(AstNetlist* nodep) { iterate(nodep); }
|
2022-09-16 12:22:11 +02:00
|
|
|
~CastVisitor() override = default;
|
2006-08-26 13:35:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Cast class functions
|
|
|
|
|
|
|
|
|
|
void V3Cast::castAll(AstNetlist* nodep) {
|
2025-05-23 02:29:32 +02:00
|
|
|
UINFO(2, __FUNCTION__ << ":");
|
2021-11-26 16:52:36 +01:00
|
|
|
{ CastVisitor{nodep}; } // Destruct before checking
|
2024-01-09 16:35:13 +01:00
|
|
|
V3Global::dumpCheckGlobalTree("cast", 0, dumpTreeEitherLevel() >= 3);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|