From 12b7cf3cd44a2e953ad58050c9565ad5c7ea544a Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Sun, 6 Sep 2020 18:50:19 +0200 Subject: [PATCH] Move `curOffset` into section code Improves organization --- include/asm/main.h | 2 -- include/asm/section.h | 2 ++ src/asm/asmy.y | 8 ++++---- src/asm/main.c | 1 - src/asm/output.c | 2 +- src/asm/section.c | 16 +++++++++++++++- src/asm/symbol.c | 4 ++-- 7 files changed, 24 insertions(+), 11 deletions(-) diff --git a/include/asm/main.h b/include/asm/main.h index 1ef1aa1e..874ad5a6 100644 --- a/include/asm/main.h +++ b/include/asm/main.h @@ -26,8 +26,6 @@ extern uint32_t ulNewMacroSize; extern int32_t nGBGfxID; extern int32_t nBinaryID; -extern uint32_t curOffset; /* Offset into the current section */ - extern struct sOptions DefaultOptions; extern struct sOptions CurrentOptions; extern bool haltnop; diff --git a/include/asm/section.h b/include/asm/section.h index 41bb4425..3819d92f 100644 --- a/include/asm/section.h +++ b/include/asm/section.h @@ -45,6 +45,8 @@ void out_SetLoadSection(char const *name, uint32_t secttype, uint32_t org, void out_EndLoadSection(void); struct Section *sect_GetSymbolSection(void); +uint32_t sect_GetSymbolOffset(void); +void sect_SetSymbolOffset(uint32_t ofs); uint32_t sect_GetOutputOffset(void); void sect_AlignPC(uint8_t alignment, uint16_t offset); diff --git a/src/asm/asmy.y b/src/asm/asmy.y index 89f50fe8..8eec6d4e 100644 --- a/src/asm/asmy.y +++ b/src/asm/asmy.y @@ -394,19 +394,19 @@ static void startUnion(void) if (nUnionDepth > MAXUNIONS) fatalerror("Too many nested UNIONs"); - unionStart[unionIndex] = curOffset; + unionStart[unionIndex] = sect_GetOutputOffset(); unionSize[unionIndex] = 0; } static void updateUnion(void) { uint32_t unionIndex = nUnionDepth - 1; - uint32_t size = curOffset - unionStart[unionIndex]; + uint32_t size = sect_GetOutputOffset() - unionStart[unionIndex]; if (size > unionSize[unionIndex]) unionSize[unionIndex] = size; - curOffset = unionStart[unionIndex]; + sect_SetSymbolOffset(unionStart[unionIndex]); } static size_t strlenUTF8(const char *s) @@ -993,7 +993,7 @@ endu : T_POP_ENDU { updateUnion(); nUnionDepth--; - curOffset = unionStart[nUnionDepth] + unionSize[nUnionDepth]; + sect_SetSymbolOffset(unionStart[nUnionDepth] + unionSize[nUnionDepth]); } ; diff --git a/src/asm/main.c b/src/asm/main.c index 08ab85a3..9534a98b 100644 --- a/src/asm/main.c +++ b/src/asm/main.c @@ -45,7 +45,6 @@ bool skipElif; uint32_t unionStart[128], unionSize[128]; int32_t nLineNo; -uint32_t curOffset; #if defined(YYDEBUG) && YYDEBUG extern int yydebug; diff --git a/src/asm/output.c b/src/asm/output.c index f89845b2..768e1d59 100644 --- a/src/asm/output.c +++ b/src/asm/output.c @@ -322,7 +322,7 @@ static struct Patch *allocpatch(uint32_t type, struct Expression const *expr, fstk_DumpToStr(pPatch->tzFilename, sizeof(pPatch->tzFilename)); pPatch->nOffset = ofs; pPatch->pcSection = sect_GetSymbolSection(); - pPatch->pcOffset = curOffset; + pPatch->pcOffset = sect_GetSymbolOffset(); /* If the expression's value is known, output a constant RPN expression directly */ if (expr->isKnown) { diff --git a/src/asm/section.c b/src/asm/section.c index f26e1c11..bf857a77 100644 --- a/src/asm/section.c +++ b/src/asm/section.c @@ -24,8 +24,9 @@ struct SectionStackEntry { }; struct SectionStackEntry *pSectionStack; +uint32_t curOffset; /* Offset into the current section (see sect_GetSymbolOffset) */ static struct Section *currentLoadSection = NULL; -uint32_t loadOffset = 0; /* The offset of the LOAD section within its parent */ +uint32_t loadOffset; /* The offset of the LOAD section within its parent */ /* * A quick check to see if we have an initialized section @@ -358,6 +359,19 @@ struct Section *sect_GetSymbolSection(void) return currentLoadSection ? currentLoadSection : pCurrentSection; } +/* + * The offset into the section above + */ +uint32_t sect_GetSymbolOffset(void) +{ + return curOffset; +} + +void sect_SetSymbolOffset(uint32_t ofs) +{ + curOffset = ofs; +} + uint32_t sect_GetOutputOffset(void) { return curOffset + loadOffset; diff --git a/src/asm/symbol.c b/src/asm/symbol.c index 4887b364..fcd72ba5 100644 --- a/src/asm/symbol.c +++ b/src/asm/symbol.c @@ -89,7 +89,7 @@ static int32_t CallbackPC(void) { struct Section const *section = sect_GetSymbolSection(); - return section ? section->nOrg + curOffset : 0; + return section ? section->nOrg + sect_GetSymbolOffset() : 0; } /* @@ -414,7 +414,7 @@ struct Symbol *sym_AddReloc(char const *symName) sym->type = SYM_LABEL; sym->callback = NULL; - sym->value = curOffset; + sym->value = sect_GetSymbolOffset(); if (exportall) sym->isExported = true;