Use std::deque (iterable) for section stack

This commit is contained in:
Rangi42
2024-02-21 10:28:51 -05:00
committed by Sylvie
parent feb342804b
commit a310b659cd
4 changed files with 13 additions and 29 deletions

View File

@@ -3,6 +3,7 @@
#ifndef RGBDS_ASM_OUTPUT_H #ifndef RGBDS_ASM_OUTPUT_H
#define RGBDS_ASM_OUTPUT_H #define RGBDS_ASM_OUTPUT_H
#include <deque>
#include <stdint.h> #include <stdint.h>
#include "linkdefs.hpp" #include "linkdefs.hpp"
@@ -11,7 +12,7 @@ struct Expression;
struct FileStackNode; struct FileStackNode;
extern const char *objectName; extern const char *objectName;
extern struct Section *sectionList; extern std::deque<struct Section *> sectionList;
void out_RegisterNode(struct FileStackNode *node); void out_RegisterNode(struct FileStackNode *node);
void out_ReplaceNode(struct FileStackNode *node); void out_ReplaceNode(struct FileStackNode *node);

View File

@@ -23,7 +23,6 @@ struct Section {
uint32_t bank; uint32_t bank;
uint8_t align; // Exactly as specified in `ALIGN[]` uint8_t align; // Exactly as specified in `ALIGN[]`
uint16_t alignOfs; uint16_t alignOfs;
struct Section *next;
struct Patch *patches; struct Patch *patches;
uint8_t *data; uint8_t *data;
}; };

View File

@@ -2,7 +2,9 @@
// Outputs an objectfile // Outputs an objectfile
#include <algorithm>
#include <assert.h> #include <assert.h>
#include <deque>
#include <errno.h> #include <errno.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
@@ -44,7 +46,7 @@ struct Assertion {
const char *objectName; const char *objectName;
struct Section *sectionList; std::deque<struct Section *> sectionList;
// Linked list of symbols to put in the object file // Linked list of symbols to put in the object file
static struct Symbol *objectSymbols = NULL; static struct Symbol *objectSymbols = NULL;
@@ -55,17 +57,6 @@ static struct Assertion *assertions = NULL;
static struct FileStackNode *fileStackNodes = 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 // Count the number of patches used in this object
static uint32_t countPatches(struct Section const *sect) 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 // Return a section's ID
static uint32_t getsectid(struct Section const *sect) static uint32_t getsectid(struct Section const *sect)
{ {
struct Section const *sec = sectionList; auto search = std::find(RANGE(sectionList), sect);
uint32_t ID = 0;
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) static uint32_t getSectIDIfAny(struct Section const *sect)
@@ -497,7 +483,7 @@ void out_WriteObject(void)
putlong(RGBDS_OBJECT_REV, f); putlong(RGBDS_OBJECT_REV, f);
putlong(nbSymbols, f); putlong(nbSymbols, f);
putlong(countSections(), f); putlong(sectionList.size(), f);
putlong(getNbFileStackNodes(), f); putlong(getNbFileStackNodes(), f);
for (struct FileStackNode const *node = fileStackNodes; node; node = node->next) { 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) for (struct Symbol const *sym = objectSymbols; sym; sym = sym->next)
writesymbol(sym, f); writesymbol(sym, f);
for (struct Section *sect = sectionList; sect; sect = sect->next) { for (struct Section *sect : sectionList) {
writesection(sect, f); writesection(sect, f);
freesection(sect); freesection(sect);
} }

View File

@@ -108,7 +108,7 @@ attr_(warn_unused_result) static bool reserveSpace(uint32_t delta_size)
struct Section *sect_FindSectionByName(char const *name) 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) if (strcmp(name, sect->name) == 0)
return sect; return sect;
} }
@@ -283,7 +283,6 @@ static struct Section *createSection(char const *name, enum SectionType type,
sect->bank = bank; sect->bank = bank;
sect->align = alignment; sect->align = alignment;
sect->alignOfs = alignOffset; sect->alignOfs = alignOffset;
sect->next = NULL;
sect->patches = NULL; sect->patches = NULL;
// It is only needed to allocate memory for ROM sections. // 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 { } else {
sect = createSection(name, type, org, bank, alignment, alignOffset, mod); sect = createSection(name, type, org, bank, alignment, alignOffset, mod);
// Add the new section to the list (order doesn't matter) // Add the new section to the list (order doesn't matter)
sect->next = sectionList; sectionList.push_front(sect);
sectionList = sect;
} }
return sect; return sect;