From 67c707739de46824072e8cfb8fbdb0216a5d2d69 Mon Sep 17 00:00:00 2001 From: Sylvie <35663410+Rangi42@users.noreply.github.com> Date: Wed, 13 Mar 2024 14:06:20 -0400 Subject: [PATCH] Allow multiple identical exported numeric constants (#1341) This still doesn't allow identical exported label constants. That can be addressed when or if it's requested for a real use case. Symbols only store one source filename + line number, so this arbitrarily keeps the last read symbol as the reported one. --- src/link/symbol.cpp | 10 +++++++--- test/link/same-consts/a.asm | 2 ++ test/link/same-consts/b.asm | 2 ++ test/link/same-consts/out.err | 0 test/link/test.sh | 9 +++++++++ 5 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 test/link/same-consts/a.asm create mode 100644 test/link/same-consts/b.asm create mode 100644 test/link/same-consts/out.err diff --git a/src/link/symbol.cpp b/src/link/symbol.cpp index a7f320b6..184f5077 100644 --- a/src/link/symbol.cpp +++ b/src/link/symbol.cpp @@ -26,8 +26,12 @@ Label const &Symbol::label() const { } void sym_AddSymbol(Symbol &symbol) { - // Check if the symbol already exists - if (Symbol *other = sym_GetSymbol(symbol.name); other) { + Symbol *other = sym_GetSymbol(symbol.name); + int32_t const *symValue = std::get_if(&symbol.data); + int32_t const *otherValue = other ? std::get_if(&other->data) : nullptr; + + // Check if the symbol already exists with a different value + if (other && !(symValue && otherValue && *symValue == *otherValue)) { fprintf(stderr, "error: \"%s\" both in %s from ", symbol.name.c_str(), symbol.objFileName); symbol.src->dump(symbol.lineNo); fprintf(stderr, " and in %s from ", other->objFileName); @@ -36,7 +40,7 @@ void sym_AddSymbol(Symbol &symbol) { exit(1); } - // If not, add it + // If not, add it (potentially replacing the previous same-value symbol) symbols[symbol.name] = &symbol; } diff --git a/test/link/same-consts/a.asm b/test/link/same-consts/a.asm new file mode 100644 index 00000000..8cace25a --- /dev/null +++ b/test/link/same-consts/a.asm @@ -0,0 +1,2 @@ +def constant equ 42 +export constant diff --git a/test/link/same-consts/b.asm b/test/link/same-consts/b.asm new file mode 100644 index 00000000..8cace25a --- /dev/null +++ b/test/link/same-consts/b.asm @@ -0,0 +1,2 @@ +def constant equ 42 +export constant diff --git a/test/link/same-consts/out.err b/test/link/same-consts/out.err new file mode 100644 index 00000000..e69de29b diff --git a/test/link/test.sh b/test/link/test.sh index 75bb9d01..357e92b1 100755 --- a/test/link/test.sh +++ b/test/link/test.sh @@ -190,6 +190,15 @@ tryDiff "$test"/out.err "$outtemp" tryCmp "$test"/out.gb "$gbtemp" evaluateTest +test="same-consts" +startTest +"$RGBASM" -o "$otemp" "$test"/a.asm +"$RGBASM" -o "$gbtemp2" "$test"/b.asm +continueTest +rgblinkQuiet -o "$gbtemp" "$gbtemp2" "$otemp" 2>"$outtemp" +tryDiff "$test"/out.err "$outtemp" +evaluateTest + test="scramble-romx" startTest "$RGBASM" -o "$otemp" "$test"/a.asm