Don't output anonymous labels in map files (#1674)

This commit is contained in:
Rangi
2025-05-01 13:19:25 -04:00
committed by GitHub
parent e45b9625ca
commit 56f7222230
2 changed files with 23 additions and 8 deletions

View File

@@ -259,8 +259,13 @@ static void writeROM() {
} }
} }
// Checks whether this character is legal as the first character of a symbol's name in a sym file // Checks whether a symbol is legal for a sym file or map file.
static bool canStartSymName(char c) { // Eliminates anonymous labels, which start with a '!'.
static bool isLegalSymbol(Symbol const &sym) {
if (sym.name.empty()) {
return false;
}
char c = sym.name[0];
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_'; return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_';
} }
@@ -271,7 +276,7 @@ static bool isLegalForSymName(char c) {
} }
// Prints a symbol's name to a file, assuming that the first character is legal. // Prints a symbol's name to a file, assuming that the first character is legal.
// Illegal characters are UTF-8-decoded (errors are replaced by U+FFFD) and emitted as `\u`/`\U`. // Illegal characters are UTF-8-decoded (errors are replaced by U+FFFD) and emitted as '\u'/'\U'.
static void printSymName(std::string const &name, FILE *file) { static void printSymName(std::string const &name, FILE *file) {
for (char const *ptr = name.c_str(); *ptr != '\0';) { for (char const *ptr = name.c_str(); *ptr != '\0';) {
char c = *ptr; char c = *ptr;
@@ -368,7 +373,7 @@ static void writeSymBank(SortedSections const &bankSections, SectionType type, u
forEachSortedSection(sect, { forEachSortedSection(sect, {
for (Symbol const *sym : sect->symbols) { for (Symbol const *sym : sect->symbols) {
// Don't output symbols that begin with an illegal character // Don't output symbols that begin with an illegal character
if (!sym->name.empty() && canStartSymName(sym->name[0])) { if (isLegalSymbol(*sym)) {
symList.push_back({ symList.push_back({
.sym = sym, .sym = sym,
.addr = static_cast<uint16_t>(sym->label().offset + sect->org), .addr = static_cast<uint16_t>(sym->label().offset + sect->org),
@@ -471,11 +476,14 @@ static void writeMapBank(SortedSections const &sectList, SectionType type, uint3
// Also print symbols in the following "pieces" // Also print symbols in the following "pieces"
for (uint16_t org = sect->org; sect; sect = sect->nextu.get()) { for (uint16_t org = sect->org; sect; sect = sect->nextu.get()) {
for (Symbol *sym : sect->symbols) { for (Symbol *sym : sect->symbols) {
// Don't output symbols that begin with an illegal character
if (isLegalSymbol(*sym)) {
// Space matches "\tSECTION: $xxxx ..." // Space matches "\tSECTION: $xxxx ..."
fprintf(mapFile, "\t $%04" PRIx32 " = ", sym->label().offset + org); fprintf(mapFile, "\t $%04" PRIx32 " = ", sym->label().offset + org);
printSymName(sym->name, mapFile); printSymName(sym->name, mapFile);
putc('\n', mapFile); putc('\n', mapFile);
} }
}
if (sect->nextu) { if (sect->nextu) {
// Announce the following "piece" // Announce the following "piece"

View File

@@ -1,29 +1,36 @@
SECTION "rom0", ROM0 SECTION "rom0", ROM0
Label0:: ds 1 Label0:: ds 1
.local:: .local::
:
SECTION "romx", ROMX SECTION "romx", ROMX
Label1:: ds 2 Label1:: ds 2
.local:: .local::
:
SECTION "vram", VRAM SECTION "vram", VRAM
vLabel:: ds 3 vLabel:: ds 3
.local:: .local::
:
SECTION "sram", SRAM SECTION "sram", SRAM
sLabel:: ds 4 sLabel:: ds 4
.local:: .local::
:
SECTION "wram0", WRAM0 SECTION "wram0", WRAM0
wLabel0:: ds 5 wLabel0:: ds 5
.local:: .local::
:
SECTION "wramx", WRAMX SECTION "wramx", WRAMX
wLabel1:: ds 6 wLabel1:: ds 6
.local:: .local::
:
SECTION "hram", HRAM SECTION "hram", HRAM
hLabel:: ds 7 hLabel:: ds 7
.local:: .local::
:
SECTION "\n\r\t\"\\", ROM0[1] SECTION "\n\r\t\"\\", ROM0[1]