From 646cb05faf028941dbac96abbac2b4567b613696 Mon Sep 17 00:00:00 2001 From: Rangi <35663410+Rangi42@users.noreply.github.com> Date: Fri, 25 Sep 2026 13:18:52 -0400 Subject: [PATCH] Fix `BANK(@)` discrepancy within `LOAD` blocks (#2149) RGBASM and RGBLINK disagreed on which section to consider `@` to be in; we have decided that the latter is correct. The `assume` change is known to be suspicious; for more discussion, see https://github.com/gbdev/rgbds/pull/2149#discussion_r4106902830 --- include/asm/section.hpp | 2 -- src/asm/output.cpp | 2 +- src/asm/rpn.cpp | 6 +++--- src/asm/section.cpp | 4 ---- test/asm/load-pc-bank.asm | 31 +++++++++++++++++++++++++++++++ test/asm/ram-code.out.bin | Bin 27 -> 27 bytes 6 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 test/asm/load-pc-bank.asm diff --git a/include/asm/section.hpp b/include/asm/section.hpp index d7f86fdc..e7a92ef9 100644 --- a/include/asm/section.hpp +++ b/include/asm/section.hpp @@ -5,7 +5,6 @@ #include #include -#include #include #include #include @@ -78,7 +77,6 @@ void sect_CheckLoadClosed(); Section *sect_GetSymbolSection(); uint32_t sect_GetSymbolOffset(); uint32_t sect_GetOutputOffset(); -std::optional sect_GetOutputBank(); Patch *sect_AddOutputPatch(); diff --git a/src/asm/output.cpp b/src/asm/output.cpp index 2c22cd09..f635c9e5 100644 --- a/src/asm/output.cpp +++ b/src/asm/output.cpp @@ -148,7 +148,7 @@ static void initPatch(Patch &patch, uint32_t type, Expression const &expr, uint3 void out_CreatePatch(uint32_t type, Expression const &expr, uint32_t ofs, uint32_t pcShift) { // Add the patch to the list - assume(sect_GetOutputBank().has_value()); + assume(sect_GetSymbolSection() != nullptr); Patch &patch = *sect_AddOutputPatch(); initPatch(patch, type, expr, ofs); diff --git a/src/asm/rpn.cpp b/src/asm/rpn.cpp index 301c8e35..63b6c34f 100644 --- a/src/asm/rpn.cpp +++ b/src/asm/rpn.cpp @@ -83,14 +83,14 @@ void Expression::makeBankSymbol(InternedStr symName) { assume(rpn.empty()); if (Symbol const *sym = sym_FindScopedSymbol(symName); sym_IsPC(sym)) { // The @ symbol is treated differently. - if (std::optional outputBank = sect_GetOutputBank(); !outputBank) { + if (Section *section = sect_GetSymbolSection(); !section) { error("PC has no bank outside of a section"); data = 1; - } else if (*outputBank == UINT32_MAX) { + } else if (section->bank == UINT32_MAX) { data = "Current section's bank is not known"; rpn.emplace_back(RPN_BANK_SELF); } else { - data = static_cast(*outputBank); + data = static_cast(section->bank); } } else if (sym && !sym->isLabel()) { error("`BANK` argument must be a label"); diff --git a/src/asm/section.cpp b/src/asm/section.cpp index 8c6bc027..5d784466 100644 --- a/src/asm/section.cpp +++ b/src/asm/section.cpp @@ -644,10 +644,6 @@ uint32_t sect_GetOutputOffset() { return curOffset + loadOffset; } -std::optional sect_GetOutputBank() { - return currentSection ? std::optional(currentSection->bank) : std::nullopt; -} - Patch *sect_AddOutputPatch() { return currentSection ? ¤tSection->patches.emplace_front() : nullptr; } diff --git a/test/asm/load-pc-bank.asm b/test/asm/load-pc-bank.asm new file mode 100644 index 00000000..062c00df --- /dev/null +++ b/test/asm/load-pc-bank.asm @@ -0,0 +1,31 @@ +SECTION "rom", ROMX, BANK[1] +xLabel: +assert @ == xLabel +static_assert BANK(@) == BANK(xLabel) +static_assert SECTION(@) === SECTION(xLabel) + ds $3000, $42 + +LOAD "ram", WRAMX, BANK[2] +wLabel:: +assert @ == wLabel +static_assert BANK(@) == BANK(wLabel) +static_assert SECTION(@) === SECTION(wLabel) + ds $1000, $42 + +ENDL + +SECTION "floating rom", ROMX +xLabel2: +assert @ == xLabel2 +assert BANK(@) == BANK(xLabel2) +static_assert SECTION(@) === SECTION(xLabel2) + ds $3000, $42 + +LOAD "floating ram", WRAMX +wLabel2:: +assert @ == wLabel2 +assert BANK(@) == BANK(wLabel2) +static_assert SECTION(@) === SECTION(wLabel2) + ds $1000, $42 + +ENDL diff --git a/test/asm/ram-code.out.bin b/test/asm/ram-code.out.bin index ba867a1021eb84887e147ba97126f5b2386592d8..b562f1e141d696d4c478b5d2a09254a52ed8c353 100644 GIT binary patch literal 27 jcmZQL%Xs1azP0xlB^V_BX=xp1yr9T)f#Cwj1%3tqqqPd6 literal 27 jcmZQL%Xs1azP0xlBp4+AX=xp1yr9T)f#Cwj1%3tqqoN9-