diff --git a/include/asm/symbol.hpp b/include/asm/symbol.hpp index 15bdb477..5d2c7325 100644 --- a/include/asm/symbol.hpp +++ b/include/asm/symbol.hpp @@ -4,7 +4,6 @@ #define RGBDS_ASM_SYMBOL_HPP #include -#include #include #include #include @@ -100,7 +99,7 @@ bool sym_IsPurgedScoped(std::string const &symName); void sym_Init(time_t now); // Functions to save and restore the current symbol scope. -std::optional const &sym_GetCurrentSymbolScope(); -void sym_SetCurrentSymbolScope(std::optional const &newScope); +Symbol const *sym_GetCurrentSymbolScope(); +void sym_SetCurrentSymbolScope(Symbol const *newScope); #endif // RGBDS_ASM_SYMBOL_HPP diff --git a/src/asm/section.cpp b/src/asm/section.cpp index edf9b7d9..38780f1a 100644 --- a/src/asm/section.cpp +++ b/src/asm/section.cpp @@ -31,7 +31,7 @@ struct UnionStackEntry { struct SectionStackEntry { Section *section; Section *loadSection; - std::optional scope; // Section's symbol scope + Symbol const *scope; uint32_t offset; int32_t loadOffset; std::stack unionStack; @@ -44,7 +44,7 @@ std::unordered_map sectionMap; // Indexes into `sectionList uint32_t curOffset; // Offset into the current section (see sect_GetSymbolOffset) Section *currentSection = nullptr; static Section *currentLoadSection = nullptr; -std::optional currentLoadScope = std::nullopt; +static Symbol const *currentLoadScope = nullptr; int32_t loadOffset; // Offset into the LOAD section's parent (see sect_GetOutputOffset) // A quick check to see if we have an initialized section @@ -395,7 +395,7 @@ static void changeSection() { if (!currentUnionStack.empty()) fatalerror("Cannot change the section within a UNION\n"); - sym_SetCurrentSymbolScope(std::nullopt); + sym_SetCurrentSymbolScope(nullptr); } bool Section::isSizeKnown() const { @@ -944,7 +944,7 @@ void sect_PushSection() { // Reset the section scope currentSection = nullptr; currentLoadSection = nullptr; - sym_SetCurrentSymbolScope(std::nullopt); + sym_SetCurrentSymbolScope(nullptr); std::swap(currentUnionStack, sectionStack.front().unionStack); } @@ -979,5 +979,5 @@ void sect_EndSection() { // Reset the section scope currentSection = nullptr; - sym_SetCurrentSymbolScope(std::nullopt); + sym_SetCurrentSymbolScope(nullptr); } diff --git a/src/asm/symbol.cpp b/src/asm/symbol.cpp index 7dfb00a6..14ae47b7 100644 --- a/src/asm/symbol.cpp +++ b/src/asm/symbol.cpp @@ -22,7 +22,7 @@ using namespace std::literals; std::unordered_map symbols; std::unordered_set purgedSymbols; -static std::optional labelScope = std::nullopt; // Current section's label scope +static Symbol const *labelScope = nullptr; // Current section's label scope static Symbol *PCSymbol; static Symbol *_NARGSymbol; static Symbol *_RSSymbol; @@ -137,7 +137,7 @@ Symbol *sym_FindScopedSymbol(std::string const &symName) { ); // If auto-scoped local label, expand the name if (dotPos == 0 && labelScope) - return sym_FindExactSymbol(*labelScope + symName); + return sym_FindExactSymbol(labelScope->name + symName); } return sym_FindExactSymbol(symName); } @@ -177,9 +177,9 @@ void sym_Purge(std::string const &symName) { warning(WARNING_PURGE_1, "Purging an exported symbol \"%s\"\n", symName.c_str()); else if (sym->isLabel()) warning(WARNING_PURGE_2, "Purging a label \"%s\"\n", symName.c_str()); - // Do not keep a reference to the label's name after purging it - if (sym->name == labelScope) - labelScope = std::nullopt; + // Do not keep a reference to the label after purging it + if (labelScope == sym) + labelScope = nullptr; purgedSymbols.emplace(sym->name); symbols.erase(sym->name); } @@ -196,7 +196,7 @@ bool sym_IsPurgedScoped(std::string const &symName) { return false; // If auto-scoped local label, expand the name if (dotPos == 0 && labelScope) - return sym_IsPurgedExact(*labelScope + symName); + return sym_IsPurgedExact(labelScope->name + symName); } return sym_IsPurgedExact(symName); } @@ -246,11 +246,11 @@ uint32_t sym_GetConstantValue(std::string const &symName) { return 0; } -std::optional const &sym_GetCurrentSymbolScope() { +Symbol const *sym_GetCurrentSymbolScope() { return labelScope; } -void sym_SetCurrentSymbolScope(std::optional const &newScope) { +void sym_SetCurrentSymbolScope(Symbol const *newScope) { labelScope = newScope; } @@ -422,7 +422,7 @@ static Symbol *addLabel(std::string const &symName) { // Add a local (`.name` or `Parent.name`) relocatable symbol Symbol *sym_AddLocalLabel(std::string const &symName) { // Assuming no dots in `labelScope` if defined - assume(!labelScope.has_value() || labelScope->find('.') == std::string::npos); + assume(!labelScope || labelScope->name.find('.') == std::string::npos); size_t dotPos = symName.find('.'); @@ -437,11 +437,11 @@ Symbol *sym_AddLocalLabel(std::string const &symName) { fatalerror("'%s' is a nonsensical reference to a nested local label\n", symName.c_str()); if (dotPos == 0) { - if (!labelScope.has_value()) { + if (!labelScope) { error("Unqualified local label '%s' in main scope\n", symName.c_str()); return nullptr; } - return addLabel(*labelScope + symName); + return addLabel(labelScope->name + symName); } return addLabel(symName); } @@ -452,7 +452,7 @@ Symbol *sym_AddLabel(std::string const &symName) { // Set the symbol as the new scope if (sym) - labelScope = sym->name; + labelScope = sym; return sym; } @@ -543,11 +543,9 @@ Symbol *sym_Ref(std::string const &symName) { if (!sym) { if (symName.starts_with('.')) { - if (!labelScope.has_value()) + if (!labelScope) fatalerror("Local label reference '%s' in main scope\n", symName.c_str()); - std::string fullName = *labelScope + symName; - - sym = &createSymbol(fullName); + sym = &createSymbol(labelScope->name + symName); } else { sym = &createSymbol(symName); }