shithub: rgbds

Download patch

ref: a4fe274c250bd5a9375636c8aa840abdf8cea8f0
parent: 34597ce6a0420505f27041e75ac138c7cc62523d
author: ISSOtm <eldredhabert0@gmail.com>
date: Mon Feb 3 20:52:18 EST 2020

Unify all section declarations

--- a/include/asm/section.h
+++ b/include/asm/section.h
@@ -17,12 +17,14 @@
 	uint8_t *tData;
 };
 
+struct SectionSpec {
+	int32_t bank;
+	int32_t alignment;
+};
+
 struct Section *out_FindSectionByName(const char *pzName);
-void out_NewSection(char const *pzName, uint32_t secttype);
-void out_NewAbsSection(char const *pzName, uint32_t secttype, int32_t org,
-		       int32_t bank);
-void out_NewAlignedSection(char const *pzName, uint32_t secttype,
-			   int32_t alignment, int32_t bank);
+void out_NewSection(char const *pzName, uint32_t secttype, int32_t org,
+		    struct SectionSpec const *attributes);
 
 void out_AbsByte(int32_t b);
 void out_AbsByteGroup(char const *s, int32_t length);
--- a/src/asm/asmy.y
+++ b/src/asm/asmy.y
@@ -37,20 +37,6 @@
 uint32_t ulNewMacroSize;
 int32_t nPCOffset;
 
-static void bankrangecheck(char *name, uint32_t secttype, int32_t org,
-			   int32_t bank)
-{
-	if (secttype != SECTTYPE_ROMX && secttype != SECTTYPE_VRAM
-	 && secttype != SECTTYPE_SRAM && secttype != SECTTYPE_WRAMX)
-		yyerror("BANK only allowed for ROMX, WRAMX, SRAM, or VRAM sections");
-	else if (bank < bankranges[secttype][0] || bank > bankranges[secttype][1])
-		yyerror("%s bank value $%x out of range ($%x to $%x)",
-		        typeNames[secttype], bank, bankranges[secttype][0],
-		        bankranges[secttype][1]);
-
-	out_NewAbsSection(name, secttype, org, bank);
-}
-
 size_t symvaluetostring(char *dest, size_t maxLength, char *symName,
 			const char *mode)
 {
@@ -509,6 +495,7 @@
 	char tzString[MAXSTRLEN + 1];
 	struct Expression sVal;
 	int32_t nConstValue;
+	struct SectionSpec sectSpec;
 }
 
 %type	<sVal>		relocconst
@@ -521,6 +508,9 @@
 
 %type	<tzString>	string
 
+%type	<nConstValue>	sectorg
+%type	<sectSpec>	sectattrs
+
 %token	<nConstValue>	T_NUMBER
 %token	<tzString>	T_STRING
 
@@ -1384,39 +1374,10 @@
 		}
 ;
 
-section		: T_POP_SECTION string comma sectiontype
+section		: T_POP_SECTION string comma sectiontype sectorg sectattrs
 		{
-			out_NewSection($2, $4);
+			out_NewSection($2, $4, $5, &$6);
 		}
-		| T_POP_SECTION string comma sectiontype '[' uconst ']'
-		{
-			if (($6 >= 0) && ($6 < 0x10000))
-				out_NewAbsSection($2, $4, $6, -1);
-			else
-				yyerror("Address $%x not 16-bit", $6);
-		}
-		| T_POP_SECTION string comma sectiontype comma T_OP_ALIGN '[' uconst ']'
-		{
-			out_NewAlignedSection($2, $4, $8, -1);
-		}
-		| T_POP_SECTION string comma sectiontype comma T_OP_BANK '[' uconst ']'
-		{
-			bankrangecheck($2, $4, -1, $8);
-		}
-		| T_POP_SECTION string comma sectiontype '[' uconst ']' comma T_OP_BANK '[' uconst ']'
-		{
-			if (($6 < 0) || ($6 > 0x10000))
-				yyerror("Address $%x not 16-bit", $6);
-			bankrangecheck($2, $4, $6, $11);
-		}
-		| T_POP_SECTION string comma sectiontype comma T_OP_ALIGN '[' uconst ']' comma T_OP_BANK '[' uconst ']'
-		{
-			out_NewAlignedSection($2, $4, $8, $13);
-		}
-		| T_POP_SECTION string comma sectiontype comma T_OP_BANK '[' uconst ']' comma T_OP_ALIGN '[' uconst ']'
-		{
-			out_NewAlignedSection($2, $4, $13, $8);
-		}
 ;
 
 sectiontype	: T_SECT_WRAM0	{ $$ = SECTTYPE_WRAM0; }
@@ -1446,6 +1407,36 @@
 		{
 			warning(WARNING_OBSOLETE, "BSS section name is deprecated, use WRAM0 instead.");
 			$$ = SECTTYPE_WRAM0;
+		}
+;
+
+sectorg		: { $$ = -1; }
+		| '[' uconst ']'
+		{
+			if ($2 < 0 || $2 >= 0x10000)
+				yyerror("Address $%x is not 16-bit", $2);
+			else
+				$$ = $2;
+		}
+;
+
+sectattrs	:
+		{
+			$$.alignment = 0;
+			$$.bank = -1;
+		}
+		| sectattrs comma T_OP_ALIGN '[' uconst ']'
+		{
+			if ($5 < 0 || $5 > 16)
+				yyerror("Alignment must be between 0 and 16 bits, not %u",
+					$5);
+			else
+				$$.alignment = $5;
+		}
+		| sectattrs comma T_OP_BANK '[' uconst ']'
+		{
+			/* We cannot check the validity of this now */
+			$$.bank = $5;
 		}
 ;
 
--- a/src/asm/globlex.c
+++ b/src/asm/globlex.c
@@ -17,7 +17,7 @@
 #include "asm/lexer.h"
 #include "asm/main.h"
 #include "asm/rpn.h"
-#include "asm/symbol.h"
+#include "asm/section.h"
 #include "asm/symbol.h"
 #include "asm/warning.h"
 
--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -19,6 +19,7 @@
 #include "asm/lexer.h"
 #include "asm/main.h"
 #include "asm/rpn.h"
+#include "asm/section.h"
 #include "asm/warning.h"
 
 #include "extern/err.h"
--- a/src/asm/section.c
+++ b/src/asm/section.c
@@ -156,31 +156,23 @@
 /*
  * Set the current section by name and type
  */
-void out_NewSection(char const *pzName, uint32_t secttype)
+void out_NewSection(char const *pzName, uint32_t secttype, int32_t org,
+		    struct SectionSpec const *attributes)
 {
-	setCurrentSection(findSection(pzName, secttype, -1, -1, 1));
-}
+	if (attributes->bank != -1) {
+		if (secttype != SECTTYPE_ROMX && secttype != SECTTYPE_VRAM
+		 && secttype != SECTTYPE_SRAM && secttype != SECTTYPE_WRAMX)
+			yyerror("BANK only allowed for ROMX, WRAMX, SRAM, or VRAM sections");
+		else if (attributes->bank < bankranges[secttype][0]
+		      || attributes->bank > bankranges[secttype][1])
+			yyerror("%s bank value $%x out of range ($%x to $%x)",
+				typeNames[secttype], attributes->bank,
+				bankranges[secttype][0],
+				bankranges[secttype][1]);
+	}
 
-/*
- * Set the current section by name and type
- */
-void out_NewAbsSection(char const *pzName, uint32_t secttype, int32_t org,
-		       int32_t bank)
-{
-	setCurrentSection(findSection(pzName, secttype, org, bank, 1));
-}
-
-/*
- * Set the current section by name and type, using a given byte alignment
- */
-void out_NewAlignedSection(char const *pzName, uint32_t secttype,
-			   int32_t alignment, int32_t bank)
-{
-	if (alignment < 0 || alignment > 16)
-		yyerror("Alignment must be between 0-16 bits.");
-
-	setCurrentSection(findSection(pzName, secttype, -1, bank,
-				      1 << alignment));
+	setCurrentSection(findSection(pzName, secttype, org, attributes->bank,
+				      1 << attributes->alignment));
 }
 
 /*