Skip to content

Commit

Permalink
Group label scopes together with std::pair
Browse files Browse the repository at this point in the history
  • Loading branch information
Rangi42 committed Sep 15, 2024
1 parent f6196ae commit aaa0fac
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 37 deletions.
8 changes: 4 additions & 4 deletions include/asm/symbol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>
#include <string_view>
#include <time.h>
#include <utility>
#include <variant>

#include "asm/lexer.hpp"
Expand Down Expand Up @@ -99,9 +100,8 @@ bool sym_IsPurgedScoped(std::string const &symName);
void sym_Init(time_t now);

// Functions to save and restore the current label scopes.
Symbol const *sym_GetCurrentGlobalScope();
Symbol const *sym_GetCurrentLocalScope();
void sym_SetCurrentGlobalScope(Symbol const *newScope);
void sym_SetCurrentLocalScope(Symbol const *newScope);
std::pair<Symbol const *, Symbol const *> sym_GetCurrentLabelScopes();
void sym_SetCurrentLabelScopes(std::pair<Symbol const *, Symbol const *> newScopes);
void sym_ResetCurrentLabelScopes();

#endif // RGBDS_ASM_SYMBOL_HPP
28 changes: 10 additions & 18 deletions src/asm/section.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <utility>

#include "helpers.hpp"

Expand All @@ -31,8 +32,7 @@ struct UnionStackEntry {
struct SectionStackEntry {
Section *section;
Section *loadSection;
Symbol const *globalScope;
Symbol const *localScope;
std::pair<Symbol const *, Symbol const *> labelScopes;
uint32_t offset;
int32_t loadOffset;
std::stack<UnionStackEntry> unionStack;
Expand All @@ -45,8 +45,7 @@ std::unordered_map<std::string, size_t> sectionMap; // Indexes into `sectionList
uint32_t curOffset; // Offset into the current section (see sect_GetSymbolOffset)
Section *currentSection = nullptr;
static Section *currentLoadSection = nullptr;
static Symbol const *currentLoadGlobalScope = nullptr;
static Symbol const *currentLoadLocalScope = nullptr;
static std::pair<Symbol const *, Symbol const *> currentLoadLabelScopes = {nullptr, 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
Expand Down Expand Up @@ -397,8 +396,7 @@ static void changeSection() {
if (!currentUnionStack.empty())
fatalerror("Cannot change the section within a UNION\n");

sym_SetCurrentGlobalScope(nullptr);
sym_SetCurrentLocalScope(nullptr);
sym_ResetCurrentLabelScopes();
}

bool Section::isSizeKnown() const {
Expand Down Expand Up @@ -476,8 +474,7 @@ void sect_SetLoadSection(

Section *sect = getSection(name, type, org, attrs, mod);

currentLoadGlobalScope = sym_GetCurrentGlobalScope();
currentLoadLocalScope = sym_GetCurrentLocalScope();
currentLoadLabelScopes = sym_GetCurrentLabelScopes();
changeSection();
loadOffset = curOffset - (mod == SECTION_UNION ? 0 : sect->size);
curOffset -= loadOffset;
Expand All @@ -494,8 +491,7 @@ void sect_EndLoadSection() {
curOffset += loadOffset;
loadOffset = 0;
currentLoadSection = nullptr;
sym_SetCurrentGlobalScope(currentLoadGlobalScope);
sym_SetCurrentLocalScope(currentLoadLocalScope);
sym_SetCurrentLabelScopes(currentLoadLabelScopes);
}

Section *sect_GetSymbolSection() {
Expand Down Expand Up @@ -940,8 +936,7 @@ void sect_PushSection() {
sectionStack.push_front({
.section = currentSection,
.loadSection = currentLoadSection,
.globalScope = sym_GetCurrentGlobalScope(),
.localScope = sym_GetCurrentLocalScope(),
.labelScopes = sym_GetCurrentLabelScopes(),
.offset = curOffset,
.loadOffset = loadOffset,
.unionStack = {},
Expand All @@ -950,8 +945,7 @@ void sect_PushSection() {
// Reset the section scope
currentSection = nullptr;
currentLoadSection = nullptr;
sym_SetCurrentGlobalScope(nullptr);
sym_SetCurrentLocalScope(nullptr);
sym_ResetCurrentLabelScopes();
std::swap(currentUnionStack, sectionStack.front().unionStack);
}

Expand All @@ -968,8 +962,7 @@ void sect_PopSection() {
changeSection();
currentSection = entry.section;
currentLoadSection = entry.loadSection;
sym_SetCurrentGlobalScope(entry.globalScope);
sym_SetCurrentLocalScope(entry.localScope);
sym_SetCurrentLabelScopes(entry.labelScopes);
curOffset = entry.offset;
loadOffset = entry.loadOffset;
std::swap(currentUnionStack, entry.unionStack);
Expand All @@ -987,6 +980,5 @@ void sect_EndSection() {

// Reset the section scope
currentSection = nullptr;
sym_SetCurrentGlobalScope(nullptr);
sym_SetCurrentLocalScope(nullptr);
sym_ResetCurrentLabelScopes();
}
27 changes: 12 additions & 15 deletions src/asm/symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,26 +320,23 @@ uint32_t sym_GetConstantValue(std::string const &symName) {
return 0;
}

Symbol const *sym_GetCurrentGlobalScope() {
return globalScope;
std::pair<Symbol const *, Symbol const *> sym_GetCurrentLabelScopes() {
return {globalScope, localScope};
}

void sym_SetCurrentGlobalScope(Symbol const *newScope) {
// `newScope` should be global if it's defined
assume(!newScope || newScope->name.find('.') == std::string::npos);
void sym_SetCurrentLabelScopes(std::pair<Symbol const *, Symbol const *> newScopes) {
globalScope = std::get<0>(newScopes);
localScope = std::get<1>(newScopes);

globalScope = newScope;
}

Symbol const *sym_GetCurrentLocalScope() {
return localScope;
// `globalScope` should be global if it's defined
assume(!globalScope || globalScope->name.find('.') == std::string::npos);
// `localScope` should be qualified local if it's defined
assume(!localScope || localScope->name.find('.') != std::string::npos);
}

void sym_SetCurrentLocalScope(Symbol const *newScope) {
// `newScope` should be qualified local if it's defined
assume(!newScope || newScope->name.find('.') != std::string::npos);

localScope = newScope;
void sym_ResetCurrentLabelScopes() {
globalScope = nullptr;
localScope = nullptr;
}

static Symbol *createNonrelocSymbol(std::string const &symName, bool numeric) {
Expand Down

0 comments on commit aaa0fac

Please sign in to comment.