Store a pointer to the scoped label, not just its name

This commit is contained in:
Rangi42
2024-09-08 22:35:42 -04:00
committed by Eldred Habert
parent 1dcc000572
commit 155e7287db
3 changed files with 21 additions and 24 deletions

View File

@@ -4,7 +4,6 @@
#define RGBDS_ASM_SYMBOL_HPP #define RGBDS_ASM_SYMBOL_HPP
#include <memory> #include <memory>
#include <optional>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <string> #include <string>
@@ -100,7 +99,7 @@ bool sym_IsPurgedScoped(std::string const &symName);
void sym_Init(time_t now); void sym_Init(time_t now);
// Functions to save and restore the current symbol scope. // Functions to save and restore the current symbol scope.
std::optional<std::string> const &sym_GetCurrentSymbolScope(); Symbol const *sym_GetCurrentSymbolScope();
void sym_SetCurrentSymbolScope(std::optional<std::string> const &newScope); void sym_SetCurrentSymbolScope(Symbol const *newScope);
#endif // RGBDS_ASM_SYMBOL_HPP #endif // RGBDS_ASM_SYMBOL_HPP

View File

@@ -31,7 +31,7 @@ struct UnionStackEntry {
struct SectionStackEntry { struct SectionStackEntry {
Section *section; Section *section;
Section *loadSection; Section *loadSection;
std::optional<std::string> scope; // Section's symbol scope Symbol const *scope;
uint32_t offset; uint32_t offset;
int32_t loadOffset; int32_t loadOffset;
std::stack<UnionStackEntry> unionStack; std::stack<UnionStackEntry> unionStack;
@@ -44,7 +44,7 @@ std::unordered_map<std::string, size_t> sectionMap; // Indexes into `sectionList
uint32_t curOffset; // Offset into the current section (see sect_GetSymbolOffset) uint32_t curOffset; // Offset into the current section (see sect_GetSymbolOffset)
Section *currentSection = nullptr; Section *currentSection = nullptr;
static Section *currentLoadSection = nullptr; static Section *currentLoadSection = nullptr;
std::optional<std::string> currentLoadScope = std::nullopt; static Symbol const *currentLoadScope = nullptr;
int32_t loadOffset; // Offset into the LOAD section's parent (see sect_GetOutputOffset) int32_t loadOffset; // Offset into the LOAD section's parent (see sect_GetOutputOffset)
// A quick check to see if we have an initialized section // A quick check to see if we have an initialized section
@@ -395,7 +395,7 @@ static void changeSection() {
if (!currentUnionStack.empty()) if (!currentUnionStack.empty())
fatalerror("Cannot change the section within a UNION\n"); fatalerror("Cannot change the section within a UNION\n");
sym_SetCurrentSymbolScope(std::nullopt); sym_SetCurrentSymbolScope(nullptr);
} }
bool Section::isSizeKnown() const { bool Section::isSizeKnown() const {
@@ -944,7 +944,7 @@ void sect_PushSection() {
// Reset the section scope // Reset the section scope
currentSection = nullptr; currentSection = nullptr;
currentLoadSection = nullptr; currentLoadSection = nullptr;
sym_SetCurrentSymbolScope(std::nullopt); sym_SetCurrentSymbolScope(nullptr);
std::swap(currentUnionStack, sectionStack.front().unionStack); std::swap(currentUnionStack, sectionStack.front().unionStack);
} }
@@ -979,5 +979,5 @@ void sect_EndSection() {
// Reset the section scope // Reset the section scope
currentSection = nullptr; currentSection = nullptr;
sym_SetCurrentSymbolScope(std::nullopt); sym_SetCurrentSymbolScope(nullptr);
} }

View File

@@ -22,7 +22,7 @@ using namespace std::literals;
std::unordered_map<std::string, Symbol> symbols; std::unordered_map<std::string, Symbol> symbols;
std::unordered_set<std::string> purgedSymbols; std::unordered_set<std::string> purgedSymbols;
static std::optional<std::string> labelScope = std::nullopt; // Current section's label scope static Symbol const *labelScope = nullptr; // Current section's label scope
static Symbol *PCSymbol; static Symbol *PCSymbol;
static Symbol *_NARGSymbol; static Symbol *_NARGSymbol;
static Symbol *_RSSymbol; static Symbol *_RSSymbol;
@@ -137,7 +137,7 @@ Symbol *sym_FindScopedSymbol(std::string const &symName) {
); );
// If auto-scoped local label, expand the name // If auto-scoped local label, expand the name
if (dotPos == 0 && labelScope) if (dotPos == 0 && labelScope)
return sym_FindExactSymbol(*labelScope + symName); return sym_FindExactSymbol(labelScope->name + symName);
} }
return sym_FindExactSymbol(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()); warning(WARNING_PURGE_1, "Purging an exported symbol \"%s\"\n", symName.c_str());
else if (sym->isLabel()) else if (sym->isLabel())
warning(WARNING_PURGE_2, "Purging a label \"%s\"\n", symName.c_str()); 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 // Do not keep a reference to the label after purging it
if (sym->name == labelScope) if (labelScope == sym)
labelScope = std::nullopt; labelScope = nullptr;
purgedSymbols.emplace(sym->name); purgedSymbols.emplace(sym->name);
symbols.erase(sym->name); symbols.erase(sym->name);
} }
@@ -196,7 +196,7 @@ bool sym_IsPurgedScoped(std::string const &symName) {
return false; return false;
// If auto-scoped local label, expand the name // If auto-scoped local label, expand the name
if (dotPos == 0 && labelScope) if (dotPos == 0 && labelScope)
return sym_IsPurgedExact(*labelScope + symName); return sym_IsPurgedExact(labelScope->name + symName);
} }
return sym_IsPurgedExact(symName); return sym_IsPurgedExact(symName);
} }
@@ -246,11 +246,11 @@ uint32_t sym_GetConstantValue(std::string const &symName) {
return 0; return 0;
} }
std::optional<std::string> const &sym_GetCurrentSymbolScope() { Symbol const *sym_GetCurrentSymbolScope() {
return labelScope; return labelScope;
} }
void sym_SetCurrentSymbolScope(std::optional<std::string> const &newScope) { void sym_SetCurrentSymbolScope(Symbol const *newScope) {
labelScope = newScope; labelScope = newScope;
} }
@@ -422,7 +422,7 @@ static Symbol *addLabel(std::string const &symName) {
// Add a local (`.name` or `Parent.name`) relocatable symbol // Add a local (`.name` or `Parent.name`) relocatable symbol
Symbol *sym_AddLocalLabel(std::string const &symName) { Symbol *sym_AddLocalLabel(std::string const &symName) {
// Assuming no dots in `labelScope` if defined // 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('.'); 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()); fatalerror("'%s' is a nonsensical reference to a nested local label\n", symName.c_str());
if (dotPos == 0) { if (dotPos == 0) {
if (!labelScope.has_value()) { if (!labelScope) {
error("Unqualified local label '%s' in main scope\n", symName.c_str()); error("Unqualified local label '%s' in main scope\n", symName.c_str());
return nullptr; return nullptr;
} }
return addLabel(*labelScope + symName); return addLabel(labelScope->name + symName);
} }
return addLabel(symName); return addLabel(symName);
} }
@@ -452,7 +452,7 @@ Symbol *sym_AddLabel(std::string const &symName) {
// Set the symbol as the new scope // Set the symbol as the new scope
if (sym) if (sym)
labelScope = sym->name; labelScope = sym;
return sym; return sym;
} }
@@ -543,11 +543,9 @@ Symbol *sym_Ref(std::string const &symName) {
if (!sym) { if (!sym) {
if (symName.starts_with('.')) { if (symName.starts_with('.')) {
if (!labelScope.has_value()) if (!labelScope)
fatalerror("Local label reference '%s' in main scope\n", symName.c_str()); fatalerror("Local label reference '%s' in main scope\n", symName.c_str());
std::string fullName = *labelScope + symName; sym = &createSymbol(labelScope->name + symName);
sym = &createSymbol(fullName);
} else { } else {
sym = &createSymbol(symName); sym = &createSymbol(symName);
} }