mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
Output exported numeric constants to sym file (#1439)
This commit is contained in:
@@ -68,7 +68,7 @@ struct Symbol {
|
||||
uint32_t getConstantValue() const;
|
||||
};
|
||||
|
||||
void sym_ForEach(void (*func)(Symbol &));
|
||||
void sym_ForEach(void (*callback)(Symbol &));
|
||||
|
||||
void sym_SetExportAll(bool set);
|
||||
Symbol *sym_AddLocalLabel(std::string const &symName);
|
||||
|
||||
@@ -38,6 +38,8 @@ struct Symbol {
|
||||
Label const &label() const;
|
||||
};
|
||||
|
||||
void sym_ForEach(void (*callback)(Symbol &));
|
||||
|
||||
void sym_AddSymbol(Symbol &symbol);
|
||||
|
||||
/*
|
||||
|
||||
@@ -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
|
||||
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
|
||||
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.
|
||||
.It Fl O Ar overlay_file , Fl \-overlay Ar overlay_file
|
||||
If specified, sections will be overlaid "on top" of the ROM image
|
||||
|
||||
@@ -558,6 +558,25 @@ static void writeSym() {
|
||||
for (uint32_t bank = 0; bank < sections[type].size(); 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.
|
||||
|
||||
@@ -22,6 +22,11 @@ Label const &Symbol::label() const {
|
||||
return std::get<Label>(data);
|
||||
}
|
||||
|
||||
void sym_ForEach(void (*callback)(Symbol &)) {
|
||||
for (auto &it : symbols)
|
||||
callback(*it.second);
|
||||
}
|
||||
|
||||
void sym_AddSymbol(Symbol &symbol) {
|
||||
Symbol *other = sym_GetSymbol(symbol.name);
|
||||
auto *symValue = std::get_if<int32_t>(&symbol.data);
|
||||
|
||||
Reference in New Issue
Block a user