From a4fe274c250bd5a9375636c8aa840abdf8cea8f0 Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Tue, 4 Feb 2020 01:52:18 +0100 Subject: [PATCH] Unify all section declarations --- include/asm/section.h | 12 ++++--- src/asm/asmy.y | 81 +++++++++++++++++++------------------------ src/asm/globlex.c | 2 +- src/asm/lexer.c | 1 + src/asm/section.c | 38 ++++++++------------ 5 files changed, 60 insertions(+), 74 deletions(-) diff --git a/include/asm/section.h b/include/asm/section.h index d598a9e1..ef998783 100644 --- a/include/asm/section.h +++ b/include/asm/section.h @@ -17,12 +17,14 @@ struct Section { 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); diff --git a/src/asm/asmy.y b/src/asm/asmy.y index d982d31b..23342ac1 100644 --- a/src/asm/asmy.y +++ b/src/asm/asmy.y @@ -37,20 +37,6 @@ char *tzNewMacro; 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 @@ static void strsubUTF8(char *dest, const char *src, uint32_t pos, uint32_t len) char tzString[MAXSTRLEN + 1]; struct Expression sVal; int32_t nConstValue; + struct SectionSpec sectSpec; } %type relocconst @@ -521,6 +508,9 @@ static void strsubUTF8(char *dest, const char *src, uint32_t pos, uint32_t len) %type string +%type sectorg +%type sectattrs + %token T_NUMBER %token T_STRING @@ -1384,38 +1374,9 @@ string : T_STRING } ; -section : T_POP_SECTION string comma sectiontype +section : T_POP_SECTION string comma sectiontype sectorg sectattrs { - out_NewSection($2, $4); - } - | 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); + out_NewSection($2, $4, $5, &$6); } ; @@ -1449,6 +1410,36 @@ sectiontype : T_SECT_WRAM0 { $$ = 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; + } +; + cpu_command : z80_adc | z80_add diff --git a/src/asm/globlex.c b/src/asm/globlex.c index 44881a96..c092188d 100644 --- 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" diff --git a/src/asm/lexer.c b/src/asm/lexer.c index 23e00e0c..69f3be1e 100644 --- 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" diff --git a/src/asm/section.c b/src/asm/section.c index a0f047a8..0ae711d5 100644 --- a/src/asm/section.c +++ b/src/asm/section.c @@ -156,31 +156,23 @@ static void setCurrentSection(struct Section *pSect) /* * 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)); } /*