diff --git a/include/asm/fstack.hpp b/include/asm/fstack.hpp index 8b22a243..aa8eadaa 100644 --- a/include/asm/fstack.hpp +++ b/include/asm/fstack.hpp @@ -32,8 +32,8 @@ struct FileStackNode { // Meaningless at the root level, but gets written to the object file anyway, so init it uint32_t lineNo = 0; - // Set only if referenced: ID within the object file, `UINT32_MAX` if not output yet - uint32_t ID = UINT32_MAX; + uint32_t ID = UINT32_MAX; // ID of the node in the object file (`UINT32_MAX` if none) + bool isReferenced() const { return ID != UINT32_MAX; } // REPT iteration counts since last named node, in reverse depth order std::vector &iters() { return std::get>(data); } diff --git a/include/asm/symbol.hpp b/include/asm/symbol.hpp index 886a80e4..c94f96fa 100644 --- a/include/asm/symbol.hpp +++ b/include/asm/symbol.hpp @@ -45,7 +45,8 @@ struct Symbol { > data; - uint32_t ID; // ID of the symbol in the object file (`UINT32_MAX` if none) + uint32_t ID; // ID of the symbol in the object file (`UINT32_MAX` if none) + bool isReferenced() const { return ID != UINT32_MAX; } uint32_t defIndex; // Ordering of the symbol in the state file bool isDefined() const { return type != SYM_REF; } diff --git a/src/asm/output.cpp b/src/asm/output.cpp index b9041645..2af5651f 100644 --- a/src/asm/output.cpp +++ b/src/asm/output.cpp @@ -65,14 +65,14 @@ static void putString(std::string const &s, FILE *file) { void out_RegisterNode(std::shared_ptr node) { // If node is not already registered, register it (and parents), and give it a unique ID - for (; node && node->ID == UINT32_MAX; node = node->parent) { + for (; node && !node->isReferenced(); node = node->parent) { node->ID = fileStackNodes.size(); fileStackNodes.push_front(node); } } static void writeSection(Section const §, FILE *file) { - assume(sect.src->ID != UINT32_MAX); + assume(sect.src->isReferenced()); putString(sect.name, file); @@ -96,7 +96,7 @@ static void writeSection(Section const §, FILE *file) { putLong(sect.patches.size(), file); for (Patch const &patch : sect.patches) { - assume(patch.src->ID != UINT32_MAX); + assume(patch.src->isReferenced()); putLong(patch.src->ID, file); putLong(patch.lineNo, file); @@ -111,13 +111,14 @@ static void writeSection(Section const §, FILE *file) { } static void writeSymbol(Symbol const &sym, FILE *file) { - assume(sym.ID != UINT32_MAX); + assume(!sym.isBuiltin); // All builtin symbols are designed to be per-invocation, not unique. + assume(sym.isReferenced()); // Expect that its `ID` has been set. putString(sym.name.str(), file); if (!sym.isDefined()) { putc(SYMTYPE_IMPORT, file); } else { - assume(sym.src->ID != UINT32_MAX); + assume(sym.src->isReferenced()); Section *symSection = sym.getSection(); @@ -131,7 +132,7 @@ static void writeSymbol(Symbol const &sym, FILE *file) { void out_RegisterSymbol(Symbol &sym) { // Check for `sym.src`, to skip any built-in symbol from rgbasm - if (sym.src && sym.ID == UINT32_MAX && !sym_IsPC(&sym)) { + if (sym.src && !sym.isReferenced() && !sym_IsPC(&sym)) { sym.ID = objectSymbols.size(); // Set the symbol's ID within the object file objectSymbols.push_back(&sym); out_RegisterNode(sym.src); @@ -178,7 +179,7 @@ void out_CreateAssert( } static void writeAssert(Assertion const &assert, FILE *file) { - assume(assert.src->ID != UINT32_MAX); + assume(assert.src->isReferenced()); putLong(assert.src->ID, file); putLong(assert.lineNo, file); diff --git a/src/asm/symbol.cpp b/src/asm/symbol.cpp index b21c9d62..673e5623 100644 --- a/src/asm/symbol.cpp +++ b/src/asm/symbol.cpp @@ -164,7 +164,7 @@ static void updateSymbolFilename(Symbol &sym) { sym.fileLine = sym.src ? lexer_GetLineNo() : 0; // If the old node was registered, ensure the new one is too - if (oldSrc && oldSrc->ID != UINT32_MAX) { + if (oldSrc && oldSrc->isReferenced()) { out_RegisterNode(sym.src); } } @@ -348,7 +348,7 @@ void sym_Purge(InternedStr symName) { } } else if (sym->isBuiltin) { error("Built-in symbol `%s` cannot be purged", symName.c_str()); - } else if (sym->ID != UINT32_MAX) { + } else if (sym->isReferenced()) { error("Symbol `%s` is referenced and thus cannot be purged", symName.c_str()); } else { if (sym->isExported) {