mirror of
https://github.com/gbdev/rgbds.git
synced 2026-09-02 11:58:39 +00:00
Do not allow defining labels outside sections (#2013)
Creating a `Symbol` with `type = SYM_LABEL` but `section = nullptr` is inconsistent and dangerous. I was not able to cause any buggy behavior so far, but it's safer and reasonable to not create such a symbol in the first place. The main consequence is that `DEF(LabelOutsideSection)` will now evaluate as 0.
This commit is contained in:
+2
-2
@@ -227,9 +227,9 @@ static int32_t tryConstMask(Expression const &lhs, Expression const &rhs) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
assume(sym.isNumeric());
|
assume(sym.type == SYM_LABEL);
|
||||||
|
// We can now safely use `expr.value()` and `sym.getSection()`
|
||||||
|
|
||||||
// We can now safely use `expr.value()`
|
|
||||||
int32_t mask = expr.value();
|
int32_t mask = expr.value();
|
||||||
|
|
||||||
// The mask must not cover any unknown bits
|
// The mask must not cover any unknown bits
|
||||||
|
|||||||
+8
-5
@@ -537,6 +537,12 @@ Symbol *sym_AddVar(InternedStr symName, int32_t value) {
|
|||||||
static Symbol *addLabel(InternedStr symName) {
|
static Symbol *addLabel(InternedStr symName) {
|
||||||
assumeAlreadyExpanded(symName);
|
assumeAlreadyExpanded(symName);
|
||||||
|
|
||||||
|
Section *section = sect_GetSymbolSection();
|
||||||
|
if (!section) {
|
||||||
|
error("Cannot define label `%s` outside of a `SECTION`", symName.c_str());
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
Symbol *sym = sym_FindExactSymbol(symName);
|
Symbol *sym = sym_FindExactSymbol(symName);
|
||||||
|
|
||||||
if (!sym) {
|
if (!sym) {
|
||||||
@@ -547,6 +553,7 @@ static Symbol *addLabel(InternedStr symName) {
|
|||||||
} else {
|
} else {
|
||||||
updateSymbolFilename(*sym);
|
updateSymbolFilename(*sym);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the symbol already exists as a ref, just "take over" it
|
// If the symbol already exists as a ref, just "take over" it
|
||||||
sym->type = SYM_LABEL;
|
sym->type = SYM_LABEL;
|
||||||
sym->data = static_cast<int32_t>(sect_GetSymbolOffset());
|
sym->data = static_cast<int32_t>(sect_GetSymbolOffset());
|
||||||
@@ -554,11 +561,7 @@ static Symbol *addLabel(InternedStr symName) {
|
|||||||
if (options.exportAll && !symName.str().starts_with('!')) {
|
if (options.exportAll && !symName.str().starts_with('!')) {
|
||||||
sym->isExported = true;
|
sym->isExported = true;
|
||||||
}
|
}
|
||||||
sym->section = sect_GetSymbolSection();
|
sym->section = section;
|
||||||
|
|
||||||
if (sym && !sym->section) {
|
|
||||||
error("Label `%s` created outside of a `SECTION`", symName.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
return sym;
|
return sym;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
error: Label `!0` created outside of a `SECTION`
|
error: Cannot define label `!0` outside of a `SECTION`
|
||||||
at anon-label-bad.asm(2)
|
at anon-label-bad.asm(2)
|
||||||
error: Reference to anonymous label 2 before, when only 1 has been created so far
|
error: Reference to anonymous label 2 before, when only 1 has been created so far
|
||||||
at anon-label-bad.asm(6)
|
at anon-label-bad.asm(6)
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
error: Label `bad` created outside of a `SECTION`
|
error: Cannot define label `bad` outside of a `SECTION`
|
||||||
at label-outside-section.asm(1)
|
at label-outside-section.asm(1)
|
||||||
Assembly aborted with 1 error
|
Assembly aborted with 1 error
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
error: Label `foo` created outside of a `SECTION`
|
error: Cannot define label `foo` outside of a `SECTION`
|
||||||
at line-continuation-whitespace.asm(7)
|
at line-continuation-whitespace.asm(7)
|
||||||
Assembly aborted with 1 error
|
Assembly aborted with 1 error
|
||||||
|
|||||||
@@ -2,6 +2,6 @@ error: Invalid character 's' after line continuation
|
|||||||
at line-continuation.asm(3)
|
at line-continuation.asm(3)
|
||||||
warning: spam [-Wuser]
|
warning: spam [-Wuser]
|
||||||
at line-continuation.asm::spam(4) <- line-continuation.asm(6)
|
at line-continuation.asm::spam(4) <- line-continuation.asm(6)
|
||||||
error: Label `foo` created outside of a `SECTION`
|
error: Cannot define label `foo` outside of a `SECTION`
|
||||||
at line-continuation.asm(14)
|
at line-continuation.asm(14)
|
||||||
Assembly aborted with 2 errors
|
Assembly aborted with 2 errors
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
error: Label `foo` created outside of a `SECTION`
|
error: Cannot define label `foo` outside of a `SECTION`
|
||||||
at [email protected](1)
|
at [email protected](1)
|
||||||
error: `@` is not a macro
|
error: `@` is not a macro
|
||||||
at [email protected](1)
|
at [email protected](1)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
error: Label `old` created outside of a `SECTION`
|
error: Cannot define label `old` outside of a `SECTION`
|
||||||
at macro-syntax.asm(7)
|
at macro-syntax.asm(7)
|
||||||
error: syntax error, unexpected MACRO
|
error: syntax error, unexpected MACRO
|
||||||
at macro-syntax.asm(7)
|
at macro-syntax.asm(7)
|
||||||
@@ -6,6 +6,6 @@ error: `\1` cannot be used outside of a macro
|
|||||||
at macro-syntax.asm(8)
|
at macro-syntax.asm(8)
|
||||||
error: syntax error, unexpected ENDM
|
error: syntax error, unexpected ENDM
|
||||||
at macro-syntax.asm(9)
|
at macro-syntax.asm(9)
|
||||||
error: `old` is not a macro
|
error: Undefined macro `old`
|
||||||
at macro-syntax.asm(11)
|
at macro-syntax.asm(11)
|
||||||
Assembly aborted with 5 errors
|
Assembly aborted with 5 errors
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
error: Label `DisallowedContent` created outside of a `SECTION`
|
error: Cannot define label `DisallowedContent` outside of a `SECTION`
|
||||||
at pops-restore-no-section.asm(9)
|
at pops-restore-no-section.asm(9)
|
||||||
error: Cannot output data outside of a `SECTION`
|
error: Cannot output data outside of a `SECTION`
|
||||||
at pops-restore-no-section.asm(10)
|
at pops-restore-no-section.asm(10)
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
lab:
|
lab:
|
||||||
PRINTLN lab-lab
|
PRINTLN DEF(lab)
|
||||||
|
PRINTLN lab - lab
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
error: Label `lab` created outside of a `SECTION`
|
error: Cannot define label `lab` outside of a `SECTION`
|
||||||
at use-label-outside-section.asm(1)
|
at use-label-outside-section.asm(1)
|
||||||
error: Expected constant expression: `lab` is not constant at assembly time
|
error: Expected constant expression: undefined symbol `lab`
|
||||||
at use-label-outside-section.asm(2)
|
at use-label-outside-section.asm(3)
|
||||||
Assembly aborted with 2 errors
|
Assembly aborted with 2 errors
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
$0
|
$0
|
||||||
|
$0
|
||||||
|
|||||||
Reference in New Issue
Block a user