shithub: rgbds

Download patch

ref: 12b7cf3cd44a2e953ad58050c9565ad5c7ea544a
parent: 0d7914bff72af17f5bb638896161649e85290288
author: ISSOtm <eldredhabert0@gmail.com>
date: Sun Sep 6 14:50:19 EDT 2020

Move `curOffset` into section code

Improves organization

--- a/include/asm/main.h
+++ b/include/asm/main.h
@@ -26,8 +26,6 @@
 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;
--- a/include/asm/section.h
+++ b/include/asm/section.h
@@ -45,6 +45,8 @@
 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);
 
--- a/src/asm/asmy.y
+++ b/src/asm/asmy.y
@@ -394,7 +394,7 @@
 	if (nUnionDepth > MAXUNIONS)
 		fatalerror("Too many nested UNIONs");
 
-	unionStart[unionIndex] = curOffset;
+	unionStart[unionIndex] = sect_GetOutputOffset();
 	unionSize[unionIndex] = 0;
 }
 
@@ -401,12 +401,12 @@
 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 @@
 			updateUnion();
 
 			nUnionDepth--;
-			curOffset = unionStart[nUnionDepth] + unionSize[nUnionDepth];
+			sect_SetSymbolOffset(unionStart[nUnionDepth] + unionSize[nUnionDepth]);
 		}
 ;
 
--- a/src/asm/main.c
+++ b/src/asm/main.c
@@ -45,7 +45,6 @@
 uint32_t unionStart[128], unionSize[128];
 
 int32_t nLineNo;
-uint32_t curOffset;
 
 #if defined(YYDEBUG) && YYDEBUG
 extern int yydebug;
--- a/src/asm/output.c
+++ b/src/asm/output.c
@@ -322,7 +322,7 @@
 	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) {
--- a/src/asm/section.c
+++ b/src/asm/section.c
@@ -24,8 +24,9 @@
 };
 
 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
@@ -356,6 +357,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)
--- a/src/asm/symbol.c
+++ b/src/asm/symbol.c
@@ -89,7 +89,7 @@
 {
 	struct Section const *section = sect_GetSymbolSection();
 
-	return section ? section->nOrg + curOffset : 0;
+	return section ? section->nOrg + sect_GetSymbolOffset() : 0;
 }
 
 /*
@@ -414,7 +414,7 @@
 
 	sym->type = SYM_LABEL;
 	sym->callback = NULL;
-	sym->value = curOffset;
+	sym->value = sect_GetSymbolOffset();
 
 	if (exportall)
 		sym->isExported = true;