From 37b615f070a2859b71e1a6a4f12a398c9a6dd467 Mon Sep 17 00:00:00 2001 From: YamaArashi Date: Sat, 25 Jul 2015 21:20:44 -0700 Subject: [PATCH 1/6] Fix bug with macro args in symbol names If a macro arg came in the middle of a symbol or at the end, e.g. "SYM\1", it would say that the symbol was not defined. This was because it wasn't looking up the macro arg's value correctly. --- src/asm/globlex.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/asm/globlex.c b/src/asm/globlex.c index e2866661..cbf6235f 100644 --- a/src/asm/globlex.c +++ b/src/asm/globlex.c @@ -161,7 +161,7 @@ ParseSymbol(char *src, ULONG size) if (*src == '@') marg = sym_FindMacroArg(-1); else if (*src >= '0' && *src <= '9') - marg = sym_FindMacroArg(*src); + marg = sym_FindMacroArg(*src - '0'); else { fatalerror("Malformed ID"); return (0); From 81675bc4c71faaa00f037018c8eee4c1cf66183c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christophe=20Sta=C3=AFesse?= Date: Sun, 26 Jul 2015 01:50:56 -0600 Subject: [PATCH 2/6] Fix yacc conflict (asmfile/lastline/lines/line rules) --- src/asm/asmy.y | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/asm/asmy.y b/src/asm/asmy.y index 3e9b3837..ee181254 100644 --- a/src/asm/asmy.y +++ b/src/asm/asmy.y @@ -497,22 +497,16 @@ void if_skip_to_endc( void ) %% -asmfile : lines lastline; - -lastline : /* empty */ - | line { - nLineNo += 1; - nTotalLines += 1; - }; +asmfile : lines; +/* Note: The lexer add '\n' at the end of the input */ lines : /* empty */ | lines line '\n' { nLineNo += 1; nTotalLines += 1; }; -line : /* empty */ - | label +line : label | label cpu_command | label macro | label simple_pseudoop From 49809f6caf1a7c7c3d80cfe231010437606a2b91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christophe=20Sta=C3=AFesse?= Date: Sun, 26 Jul 2015 01:57:30 -0600 Subject: [PATCH 3/6] Fix segfault in createpatch() when symbol is an inexistant local label or bank Fixed as follows: if the symbol doesn't exist, don't add it to the relocation table. The functions calling createpatch will nevertheless increment PC correctly. Test case: SECTION "CODE", CODE glob: jp .loc ; from test/asm/banknoexist.asm: SECTION "sec", ROM0 db BANK(noexist) See also issue #68 --- src/asm/output.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/asm/output.c b/src/asm/output.c index 0bf461da..ebe2a01f 100644 --- a/src/asm/output.c +++ b/src/asm/output.c @@ -386,7 +386,10 @@ createpatch(ULONG type, struct Expression * expr) rpnexpr[rpnptr++] = value >> 16; rpnexpr[rpnptr++] = value >> 24; } else { - symptr = addsymbol(sym_FindSymbol(tzSym)); + struct sSymbol *sym; + if ((sym = sym_FindSymbol(tzSym)) == NULL) + break; + symptr = addsymbol(sym); rpnexpr[rpnptr++] = RPN_SYM; rpnexpr[rpnptr++] = symptr & 0xFF; rpnexpr[rpnptr++] = symptr >> 8; @@ -394,15 +397,19 @@ createpatch(ULONG type, struct Expression * expr) rpnexpr[rpnptr++] = symptr >> 24; } break; - case RPN_BANK: + case RPN_BANK: { + struct sSymbol *sym; symptr = 0; while ((tzSym[symptr++] = rpn_PopByte(expr)) != 0); - symptr = addsymbol(sym_FindSymbol(tzSym)); + if ((sym = sym_FindSymbol(tzSym)) == NULL) + break; + symptr = addsymbol(sym); rpnexpr[rpnptr++] = RPN_BANK; rpnexpr[rpnptr++] = symptr & 0xFF; rpnexpr[rpnptr++] = symptr >> 8; rpnexpr[rpnptr++] = symptr >> 16; rpnexpr[rpnptr++] = symptr >> 24; + } break; default: rpnexpr[rpnptr++] = rpndata; From 6c10ca62ad899fe5f5f0f687d7ec7b3b0650601f Mon Sep 17 00:00:00 2001 From: "Anthony J. Bentley" Date: Sun, 26 Jul 2015 02:08:39 -0600 Subject: [PATCH 4/6] Don't silently truncate banks greater than 255 to 8 bits. --- include/link/symbol.h | 2 +- src/link/symbol.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/link/symbol.h b/include/link/symbol.h index 3be6e43c..29fdfdcf 100644 --- a/include/link/symbol.h +++ b/include/link/symbol.h @@ -4,7 +4,7 @@ #include "types.h" void sym_Init(void); -void sym_CreateSymbol(char *tzName, SLONG nValue, SBYTE nBank); +void sym_CreateSymbol(char *tzName, SLONG nValue, SLONG nBank); SLONG sym_GetValue(char *tzName); SLONG sym_GetBank(char *tzName); diff --git a/src/link/symbol.c b/src/link/symbol.c index b0040022..36cbd7d5 100644 --- a/src/link/symbol.c +++ b/src/link/symbol.c @@ -76,7 +76,7 @@ sym_GetBank(char *tzName) } void -sym_CreateSymbol(char *tzName, SLONG nValue, SBYTE nBank) +sym_CreateSymbol(char *tzName, SLONG nValue, SLONG nBank) { if (strcmp(tzName, "@") == 0) return; From 31294d6d9dd5f4cdb79caa07832ff4a494f74072 Mon Sep 17 00:00:00 2001 From: "Anthony J. Bentley" Date: Mon, 5 Oct 2015 16:56:37 -0600 Subject: [PATCH 5/6] rgbasm: refactor bank range checking, and bump the SRAM limit. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This doesn’t affect rgblink, which still needs work. --- src/asm/asmy.y | 113 ++++++++++++++++++++----------------------------- 1 file changed, 47 insertions(+), 66 deletions(-) diff --git a/src/asm/asmy.y b/src/asm/asmy.y index ee181254..46d4554d 100644 --- a/src/asm/asmy.y +++ b/src/asm/asmy.y @@ -20,6 +20,49 @@ char *tzNewMacro; ULONG ulNewMacroSize; +void +bankrangecheck(char *name, ULONG secttype, SLONG org, SLONG bank) +{ + SLONG minbank, maxbank; + char *stype; + switch (secttype) { + case SECT_ROMX: + stype = "ROMX"; + minbank = 1; + maxbank = 0x1ff; + break; + case SECT_SRAM: + stype = "SRAM"; + minbank = 0; + maxbank = 0x1ff; + break; + case SECT_WRAMX: + stype = "WRAMX"; + minbank = 1; + maxbank = 7; + break; + case SECT_VRAM: + stype = "VRAM"; + minbank = 0; + maxbank = 1; + break; + default: + yyerror("BANK only allowed for " + "ROMX, WRAMX, SRAM, or VRAM sections"); + } + + if (bank < minbank || bank > maxbank) { + yyerror("%s bank value $%x out of range ($%x to $%x)", + stype, bank, minbank, maxbank); + } + + if (secttype == SECT_WRAMX) { + bank -= minbank; + } + + out_NewAbsSection(name, secttype, org, bank); +} + size_t symvaluetostring(char *dest, size_t maxLength, char *sym) { size_t length; @@ -1054,76 +1097,14 @@ section: } | T_POP_SECTION string ',' sectiontype ',' T_OP_BANK '[' const ']' { - if( $4==SECT_ROMX ) { - if( $8>=1 && $8<=0x1ff ) - out_NewAbsSection($2,$4,-1,$8); - else - yyerror("ROM bank value $%x out of range (1 to $1ff)", $8); - } else if ($4 == SECT_SRAM) { - if ($8 >= 0 && $8 <= 3) { - out_NewAbsSection($2, $4, -1, $8); - } else { - yyerror("SRAM bank value $%x out of range (0 to 3)", $8); - } - } else if ($4 == SECT_WRAMX) { - if ($8 >= 1 && $8 <= 7) { - out_NewAbsSection($2, $4, -1, $8 - 1); - } else { - yyerror("WRAMX bank value $%x out of range (1 to 7)", $8); - } - } else if ($4 == SECT_VRAM) { - if ($8 >= 0 && $8 <= 1) { - out_NewAbsSection($2, $4, -1, $8); - } else { - yyerror("VRAM bank value $%x out of range (0 to 1)", $8); - } - } else { - yyerror("BANK only allowed for ROMX, WRAMX, SRAM, or VRAM sections"); - } + bankrangecheck($2, $4, -1, $8); } | T_POP_SECTION string ',' sectiontype '[' const ']' ',' T_OP_BANK '[' const ']' { - if( $4==SECT_ROMX ) { - if( $6>=0 && $6<0x10000 ) { - if( $11>=1 && $11<=0x1ff ) - out_NewAbsSection($2,$4,$6,$11); - else - yyerror("ROM bank value $%x out of range (1 to $1ff)", $11); - } else - yyerror("Address $%x not 16-bit", $6); - } else if ($4 == SECT_SRAM) { - if ($6 >= 0 && $6 < 0x10000) { - if ($11 >= 0 && $11 <= 3) { - out_NewAbsSection($2, $4, $6, $11); - } else { - yyerror("SRAM bank value $%x out of range (0 to 3)", $11); - } - } else { - yyerror("Address $%x not 16-bit", $6); - } - } else if ($4 == SECT_WRAMX) { - if ($6 >= 0 && $6 < 0x10000) { - if ($11 >= 1 && $11 <= 7) { - out_NewAbsSection($2, $4, $6, $11 - 1); - } else { - yyerror("WRAMX bank value $%x out of range (1 to 7)", $11); - } - } else { - yyerror("Address $%x not 16-bit", $6); - } - } else if ($4 == SECT_VRAM) { - if ($6 >= 0 && $6 < 0x10000) { - if ($11 >= 0 && $11 <= 1) { - out_NewAbsSection($2,$4,$6,$11); - } else { - yyerror("VRAM bank value $%x out of range (0 to 1)", $11); - } - } else { - yyerror("Address $%x not 16-bit", $6); - } - } else { - yyerror("BANK only allowed for ROMX, WRAMX, SRAM, or VRAM sections"); + if ($6 < 0 || $6 > 0x10000) { + yyerror("Address $%x not 16-bit", $6); } + bankrangecheck($2, $4, $6, $11); } ; From 2ea2e47231bb98437dfa00908a65ca3052275f42 Mon Sep 17 00:00:00 2001 From: "Anthony J. Bentley" Date: Mon, 5 Oct 2015 23:58:00 -0600 Subject: [PATCH 6/6] Avoid a pointless strcpy(). --- include/link/main.h | 2 +- src/link/main.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/link/main.h b/include/link/main.h index 3915f49b..218ca6d0 100644 --- a/include/link/main.h +++ b/include/link/main.h @@ -4,6 +4,6 @@ #include "types.h" extern SLONG fillchar; -extern char smartlinkstartsymbol[256]; +extern char *smartlinkstartsymbol; #endif diff --git a/src/link/main.c b/src/link/main.c index 954d0234..8eda44a8 100644 --- a/src/link/main.c +++ b/src/link/main.c @@ -22,7 +22,7 @@ enum eBlockType { SLONG options = 0; SLONG fillchar = 0; -char smartlinkstartsymbol[256]; +char *smartlinkstartsymbol; char *progname; @@ -79,7 +79,7 @@ main(int argc, char *argv[]) break; case 's': options |= OPT_SMART_C_LINK; - strcpy(smartlinkstartsymbol, optarg); + smartlinkstartsymbol = optarg; break; case 't': options |= OPT_SMALL;