Output exported numeric constants to sym file (#1439)

This commit is contained in:
Sylvie
2024-08-06 10:58:35 -04:00
committed by GitHub
parent bb480b761c
commit fc8707886c
5 changed files with 30 additions and 2 deletions

View File

@@ -68,7 +68,7 @@ struct Symbol {
uint32_t getConstantValue() const; uint32_t getConstantValue() const;
}; };
void sym_ForEach(void (*func)(Symbol &)); void sym_ForEach(void (*callback)(Symbol &));
void sym_SetExportAll(bool set); void sym_SetExportAll(bool set);
Symbol *sym_AddLocalLabel(std::string const &symName); Symbol *sym_AddLocalLabel(std::string const &symName);

View File

@@ -38,6 +38,8 @@ struct Symbol {
Label const &label() const; Label const &label() const;
}; };
void sym_ForEach(void (*callback)(Symbol &));
void sym_AddSymbol(Symbol &symbol); void sym_AddSymbol(Symbol &symbol);
/* /*

View File

@@ -78,7 +78,9 @@ If specified, the map file will not list symbols, only sections.
.It Fl m Ar map_file , Fl \-map Ar map_file .It Fl m Ar map_file , Fl \-map Ar map_file
Write a map file to the given filename, listing how sections and symbols were assigned. Write a map file to the given filename, listing how sections and symbols were assigned.
.It Fl n Ar sym_file , Fl \-sym Ar sym_file .It Fl n Ar sym_file , Fl \-sym Ar sym_file
Write a symbol file to the given filename, listing the address of all exported symbols. Write a symbol file to the given filename, listing all visible labels and exported numeric constants.
Labels output their bank and address, numeric constants output their value, following
.Lk https://rgbds.gbdev.io/sym/ this specification .
Several external programs can use this information, for example to help debugging ROMs. Several external programs can use this information, for example to help debugging ROMs.
.It Fl O Ar overlay_file , Fl \-overlay Ar overlay_file .It Fl O Ar overlay_file , Fl \-overlay Ar overlay_file
If specified, sections will be overlaid "on top" of the ROM image If specified, sections will be overlaid "on top" of the ROM image

View File

@@ -558,6 +558,25 @@ static void writeSym() {
for (uint32_t bank = 0; bank < sections[type].size(); bank++) for (uint32_t bank = 0; bank < sections[type].size(); bank++)
writeSymBank(sections[type][bank], type, bank); writeSymBank(sections[type][bank], type, bank);
} }
// Output the exported numeric constants
static std::vector<Symbol *> constants; // `static` so `sym_ForEach` callback can see it
constants.clear();
sym_ForEach([](Symbol &sym) {
// Symbols are already limited to the exported ones
if (std::holds_alternative<int32_t>(sym.data))
constants.push_back(&sym);
});
// Numeric constants are ordered by value, then by name
std::sort(RANGE(constants), [](Symbol *sym1, Symbol *sym2) -> bool {
int32_t val1 = std::get<int32_t>(sym1->data), val2 = std::get<int32_t>(sym2->data);
return val1 != val2 ? val1 < val2 : sym1->name < sym2->name;
});
for (Symbol *sym : constants) {
int32_t val = std::get<int32_t>(sym->data);
int width = val < 0x100 ? 2 : val < 0x10000 ? 4 : 8;
fprintf(symFile, "%0*" PRIx32 " %s\n", width, val, sym->name.c_str());
}
} }
// Writes the map file, if applicable. // Writes the map file, if applicable.

View File

@@ -22,6 +22,11 @@ Label const &Symbol::label() const {
return std::get<Label>(data); return std::get<Label>(data);
} }
void sym_ForEach(void (*callback)(Symbol &)) {
for (auto &it : symbols)
callback(*it.second);
}
void sym_AddSymbol(Symbol &symbol) { void sym_AddSymbol(Symbol &symbol) {
Symbol *other = sym_GetSymbol(symbol.name); Symbol *other = sym_GetSymbol(symbol.name);
auto *symValue = std::get_if<int32_t>(&symbol.data); auto *symValue = std::get_if<int32_t>(&symbol.data);