Internals: Refector to create single V3Number::emitC. Fix some double emit issues (#6528).

This commit is contained in:
Wilson Snyder
2025-10-04 07:23:13 -04:00
committed by GitHub
parent a03d327e96
commit f09c30df35
6 changed files with 148 additions and 175 deletions
+1 -50
View File
@@ -29,7 +29,6 @@
class EmitCConstInit VL_NOT_FINAL : public EmitCBaseVisitorConst {
// MEMBERS
uint32_t m_unpackedWord = 0;
bool m_inUnpacked = false;
// METHODS
uint32_t tabModulus(const AstNodeDType* dtypep) {
@@ -44,9 +43,7 @@ class EmitCConstInit VL_NOT_FINAL : public EmitCBaseVisitorConst {
protected:
// VISITORS
void visit(AstInitArray* nodep) override {
VL_RESTORER(m_inUnpacked);
VL_RESTORER(m_unpackedWord);
m_inUnpacked = true;
if (VN_IS(nodep->dtypep()->skipRefp(), AssocArrayDType)) {
// Note the double {{ initializer. The first { starts the initializer of the
// VlAssocArray, and the second starts the initializer of m_storage within the
@@ -98,55 +95,9 @@ protected:
} // LCOV_EXCL_STOP
void visit(AstConst* nodep) override {
// TODO merge with EmitCFunc::emitConstant
const V3Number& num = nodep->num();
UASSERT_OBJ(!num.isFourState(), nodep, "4-state value in constant pool");
const AstNodeDType* const dtypep = nodep->dtypep();
if (num.isNull()) {
putns(nodep, "VlNull{}");
} else if (num.isString()) {
// Note: putsQuoted does not track indentation, so we use this instead
putns(nodep, "\"");
puts(num.toString());
puts("\"");
} else if (dtypep->isWide()) {
const uint32_t size = dtypep->widthWords();
// Note the double {{ initializer. The first { starts the initializer of the VlWide,
// and the second starts the initializer of m_storage within the VlWide.
putns(nodep, "{");
ofp()->putsNoTracking("{");
if (m_inUnpacked) puts(" // VlWide " + cvtToStr(m_unpackedWord));
puts("\n");
for (uint32_t n = 0; n < size; ++n) {
if (n) puts((n % 4) ? ", " : ",\n");
ofp()->printf("0x%08" PRIx32, num.edataWord(n));
}
puts("\n");
puts("}");
ofp()->putsNoTracking("}");
} else if (dtypep->isDouble()) {
const double dnum = num.toDouble();
const char* const fmt
= !m_inUnpacked && (static_cast<int>(dnum) == dnum && -1000 < dnum && dnum < 1000)
? "%3.1f" // Force decimal point
: "%.17e"; // %e always yields a float literal
putns(nodep, "");
ofp()->printf(fmt, dnum);
} else if (dtypep->isQuad()) {
const uint64_t qnum = static_cast<uint64_t>(num.toUQuad());
const char* const fmt
= !m_inUnpacked && (qnum < 10) ? ("%" PRIx64 "ULL") : ("0x%016" PRIx64 "ULL");
putns(nodep, "");
ofp()->printf(fmt, qnum);
} else {
const uint32_t unum = num.toUInt();
const char* const fmt = !m_inUnpacked && (unum < 10) ? ("%" PRIu32 "U")
: (dtypep->widthMin() > 16) ? ("0x%08" PRIx32 "U")
: (dtypep->widthMin() > 8) ? ("0x%04" PRIx32 "U")
: ("0x%02" PRIx32 "U");
putns(nodep, "");
ofp()->printf(fmt, unum);
}
putns(nodep, num.emitC());
}
// Default