From a310b659cd5c34b6bba224a706e43e5150afe078 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Wed, 21 Feb 2024 10:28:51 -0500 Subject: [PATCH] Use `std::deque` (iterable) for section stack --- include/asm/output.hpp | 3 ++- include/asm/section.hpp | 1 - src/asm/output.cpp | 32 +++++++++----------------------- src/asm/section.cpp | 6 ++---- 4 files changed, 13 insertions(+), 29 deletions(-) diff --git a/include/asm/output.hpp b/include/asm/output.hpp index 323c6969..bfe0793f 100644 --- a/include/asm/output.hpp +++ b/include/asm/output.hpp @@ -3,6 +3,7 @@ #ifndef RGBDS_ASM_OUTPUT_H #define RGBDS_ASM_OUTPUT_H +#include #include #include "linkdefs.hpp" @@ -11,7 +12,7 @@ struct Expression; struct FileStackNode; extern const char *objectName; -extern struct Section *sectionList; +extern std::deque sectionList; void out_RegisterNode(struct FileStackNode *node); void out_ReplaceNode(struct FileStackNode *node); diff --git a/include/asm/section.hpp b/include/asm/section.hpp index 8bf7d5ad..b841e194 100644 --- a/include/asm/section.hpp +++ b/include/asm/section.hpp @@ -23,7 +23,6 @@ struct Section { uint32_t bank; uint8_t align; // Exactly as specified in `ALIGN[]` uint16_t alignOfs; - struct Section *next; struct Patch *patches; uint8_t *data; }; diff --git a/src/asm/output.cpp b/src/asm/output.cpp index 9865e5a9..76c5f228 100644 --- a/src/asm/output.cpp +++ b/src/asm/output.cpp @@ -2,7 +2,9 @@ // Outputs an objectfile +#include #include +#include #include #include #include @@ -44,7 +46,7 @@ struct Assertion { const char *objectName; -struct Section *sectionList; +std::deque sectionList; // Linked list of symbols to put in the object file static struct Symbol *objectSymbols = NULL; @@ -55,17 +57,6 @@ static struct Assertion *assertions = NULL; static struct FileStackNode *fileStackNodes = NULL; -// Count the number of sections used in this object -static uint32_t countSections(void) -{ - uint32_t count = 0; - - for (struct Section const *sect = sectionList; sect; sect = sect->next) - count++; - - return count; -} - // Count the number of patches used in this object static uint32_t countPatches(struct Section const *sect) { @@ -154,17 +145,12 @@ This is code intended to replace a node, which is pretty useless until ref count // Return a section's ID static uint32_t getsectid(struct Section const *sect) { - struct Section const *sec = sectionList; - uint32_t ID = 0; + auto search = std::find(RANGE(sectionList), sect); - while (sec) { - if (sec == sect) - return ID; - ID++; - sec = sec->next; - } + if (search == sectionList.end()) + fatalerror("Unknown section '%s'\n", sect->name); - fatalerror("Unknown section '%s'\n", sect->name); + return search - sectionList.begin(); } static uint32_t getSectIDIfAny(struct Section const *sect) @@ -497,7 +483,7 @@ void out_WriteObject(void) putlong(RGBDS_OBJECT_REV, f); putlong(nbSymbols, f); - putlong(countSections(), f); + putlong(sectionList.size(), f); putlong(getNbFileStackNodes(), f); for (struct FileStackNode const *node = fileStackNodes; node; node = node->next) { @@ -511,7 +497,7 @@ void out_WriteObject(void) for (struct Symbol const *sym = objectSymbols; sym; sym = sym->next) writesymbol(sym, f); - for (struct Section *sect = sectionList; sect; sect = sect->next) { + for (struct Section *sect : sectionList) { writesection(sect, f); freesection(sect); } diff --git a/src/asm/section.cpp b/src/asm/section.cpp index df9171e1..6858021d 100644 --- a/src/asm/section.cpp +++ b/src/asm/section.cpp @@ -108,7 +108,7 @@ attr_(warn_unused_result) static bool reserveSpace(uint32_t delta_size) struct Section *sect_FindSectionByName(char const *name) { - for (struct Section *sect = sectionList; sect; sect = sect->next) { + for (struct Section *sect : sectionList) { if (strcmp(name, sect->name) == 0) return sect; } @@ -283,7 +283,6 @@ static struct Section *createSection(char const *name, enum SectionType type, sect->bank = bank; sect->align = alignment; sect->alignOfs = alignOffset; - sect->next = NULL; sect->patches = NULL; // It is only needed to allocate memory for ROM sections. @@ -369,8 +368,7 @@ static struct Section *getSection(char const *name, enum SectionType type, uint3 } else { sect = createSection(name, type, org, bank, alignment, alignOffset, mod); // Add the new section to the list (order doesn't matter) - sect->next = sectionList; - sectionList = sect; + sectionList.push_front(sect); } return sect;