From 9140180c85120ac81a8d7b65ae42d5a666b420b1 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Thu, 22 Feb 2024 15:04:52 -0500 Subject: [PATCH] Use automatic allocation for sections --- include/asm/output.hpp | 2 +- src/asm/output.cpp | 60 ++++++++++++++++++++---------------------- src/asm/section.cpp | 48 ++++++++++++++++----------------- 3 files changed, 52 insertions(+), 58 deletions(-) diff --git a/include/asm/output.hpp b/include/asm/output.hpp index c548c906..63e598c8 100644 --- a/include/asm/output.hpp +++ b/include/asm/output.hpp @@ -12,7 +12,7 @@ struct Expression; struct FileStackNode; extern const char *objectName; -extern std::deque sectionList; +extern std::deque sectionList; void out_RegisterNode(struct FileStackNode *node); void out_ReplaceNode(struct FileStackNode *node); diff --git a/src/asm/output.cpp b/src/asm/output.cpp index 71738e34..61a43b13 100644 --- a/src/asm/output.cpp +++ b/src/asm/output.cpp @@ -46,7 +46,7 @@ struct Assertion { const char *objectName; -std::deque sectionList; +std::deque sectionList; // List of symbols to put in the object file static std::vector objectSymbols; @@ -102,20 +102,18 @@ This is code intended to replace a node, which is pretty useless until ref count #endif } -// Return a section's ID -static uint32_t getsectid(struct Section const *sect) +// Return a section's ID, or -1 if the section is not in the list +static uint32_t getSectIDIfAny(struct Section *sect) { - auto search = std::find(RANGE(sectionList), sect); + if (!sect) + return (uint32_t)-1; - if (search == sectionList.end()) - fatalerror("Unknown section '%s'\n", sect->name); + for (auto it = sectionList.begin(); it != sectionList.end(); it++) { + if (&*it == sect) + return it - sectionList.begin(); + } - return search - sectionList.begin(); -} - -static uint32_t getSectIDIfAny(struct Section const *sect) -{ - return sect ? getsectid(sect) : (uint32_t)-1; + fatalerror("Unknown section '%s'\n", sect->name); } // Write a patch to a file @@ -133,39 +131,39 @@ static void writepatch(struct Patch const *patch, FILE *f) } // Write a section to a file -static void writesection(struct Section const *sect, FILE *f) +static void writesection(struct Section const §, FILE *f) { - putstring(sect->name, f); + putstring(sect.name, f); - putlong(sect->size, f); + putlong(sect.size, f); - bool isUnion = sect->modifier == SECTION_UNION; - bool isFragment = sect->modifier == SECTION_FRAGMENT; + bool isUnion = sect.modifier == SECTION_UNION; + bool isFragment = sect.modifier == SECTION_FRAGMENT; - putc(sect->type | isUnion << 7 | isFragment << 6, f); + putc(sect.type | isUnion << 7 | isFragment << 6, f); - putlong(sect->org, f); - putlong(sect->bank, f); - putc(sect->align, f); - putlong(sect->alignOfs, f); + putlong(sect.org, f); + putlong(sect.bank, f); + putc(sect.align, f); + putlong(sect.alignOfs, f); - if (sect_HasData(sect->type)) { - fwrite(sect->data, 1, sect->size, f); - putlong(sect->patches->size(), f); + if (sect_HasData(sect.type)) { + fwrite(sect.data, 1, sect.size, f); + putlong(sect.patches->size(), f); - for (struct Patch const *patch : *sect->patches) + for (struct Patch const *patch : *sect.patches) writepatch(patch, f); } } -static void freesection(struct Section const *sect) +static void freesection(struct Section const §) { - if (sect_HasData(sect->type)) { - for (struct Patch *patch : *sect->patches) { + if (sect_HasData(sect.type)) { + for (struct Patch *patch : *sect.patches) { free(patch->rpn); free(patch); } - delete sect->patches; + delete sect.patches; } } @@ -439,7 +437,7 @@ void out_WriteObject(void) for (struct Symbol const *sym : objectSymbols) writesymbol(sym, f); - for (struct Section *sect : sectionList) { + for (struct Section § : sectionList) { writesection(sect, f); freesection(sect); } diff --git a/src/asm/section.cpp b/src/asm/section.cpp index cd6bd19a..70d03167 100644 --- a/src/asm/section.cpp +++ b/src/asm/section.cpp @@ -110,9 +110,9 @@ attr_(warn_unused_result) static bool reserveSpace(uint32_t delta_size) struct Section *sect_FindSectionByName(char const *name) { - for (struct Section *sect : sectionList) { - if (strcmp(name, sect->name) == 0) - return sect; + for (struct Section § : sectionList) { + if (strcmp(name, sect.name) == 0) + return § } return NULL; } @@ -267,38 +267,36 @@ static struct Section *createSection(char const *name, enum SectionType type, uint32_t org, uint32_t bank, uint8_t alignment, uint16_t alignOffset, enum SectionModifier mod) { - struct Section *sect = (struct Section *)malloc(sizeof(*sect)); + // Add the new section to the list (order doesn't matter) + struct Section § = sectionList.emplace_front(); - if (sect == NULL) - fatalerror("Not enough memory for section: %s\n", strerror(errno)); - - sect->name = strdup(name); - if (sect->name == NULL) + sect.name = strdup(name); + if (sect.name == NULL) fatalerror("Not enough memory for section name: %s\n", strerror(errno)); - sect->type = type; - sect->modifier = mod; - sect->src = fstk_GetFileStack(); - sect->fileLine = lexer_GetLineNo(); - sect->size = 0; - sect->org = org; - sect->bank = bank; - sect->align = alignment; - sect->alignOfs = alignOffset; - sect->patches = new(std::nothrow) std::deque(); - if (sect->patches == NULL) + sect.type = type; + sect.modifier = mod; + sect.src = fstk_GetFileStack(); + sect.fileLine = lexer_GetLineNo(); + sect.size = 0; + sect.org = org; + sect.bank = bank; + sect.align = alignment; + sect.alignOfs = alignOffset; + sect.patches = new(std::nothrow) std::deque(); + if (sect.patches == NULL) fatalerror("Not enough memory for section patches: %s\n", strerror(errno)); // It is only needed to allocate memory for ROM sections. if (sect_HasData(type)) { - sect->data = (uint8_t *)malloc(sectionTypeInfo[type].size); - if (sect->data == NULL) + sect.data = (uint8_t *)malloc(sectionTypeInfo[type].size); + if (sect.data == NULL) fatalerror("Not enough memory for section: %s\n", strerror(errno)); } else { - sect->data = NULL; + sect.data = NULL; } - return sect; + return § } // Find a section by name and type. If it doesn't exist, create it. @@ -371,8 +369,6 @@ static struct Section *getSection(char const *name, enum SectionType type, uint3 mergeSections(sect, type, org, bank, alignment, alignOffset, mod); } else { sect = createSection(name, type, org, bank, alignment, alignOffset, mod); - // Add the new section to the list (order doesn't matter) - sectionList.push_front(sect); } return sect;