shithub: rgbds

Download patch

ref: fe65e07cb6812424869bf069ea1e9b0f721a6e15
parent: bb12806da1b3e504afa76ca0c44a00482f8c48a8
author: Antonio Niño Díaz <[email protected]>
date: Sat Jul 1 10:29:31 EDT 2017

Fail when using negative constants if not allowed

Some commands, such as `DS`, `BANK[n]`, etc, don't allow the use of
negative constants, but there wasn't any check to prohibit the code from
trying to do so.

This patch adds the `uconst` type to the parser to use when a constant
is expected, but it mustn't be negative.

Signed-off-by: Antonio Niño Díaz <[email protected]>

--- a/src/asm/asmy.y
+++ b/src/asm/asmy.y
@@ -439,6 +439,7 @@
 
 %type	<sVal>	relocconst
 %type	<nConstValue>	const
+%type	<nConstValue>	uconst
 %type	<nConstValue>	const_3bit
 %type	<sVal>	const_8bit
 %type	<sVal>	const_16bit
@@ -692,7 +693,7 @@
 					{ sym_ShiftCurrentMacroArgs(); }
 ;
 
-rept			:	T_POP_REPT const
+rept			:	T_POP_REPT uconst
 					{
 						copyrept();
 						fstk_RunRept( $2 );
@@ -710,7 +711,7 @@
 					{ sym_AddString( $1, $3 ); }
 ;
 
-rsset			:	T_POP_RSSET const
+rsset			:	T_POP_RSSET uconst
 					{ sym_AddSet( "_RS", $2 ); }
 ;
 
@@ -718,7 +719,7 @@
 					{ sym_AddSet( "_RS", 0 ); }
 ;
 
-rl				:	T_LABEL T_POP_RL const
+rl				:	T_LABEL T_POP_RL uconst
 					{
 						sym_AddEqu( $1, sym_GetConstantValue("_RS") );
 						sym_AddSet( "_RS", sym_GetConstantValue("_RS")+4*$3 );
@@ -725,7 +726,7 @@
 					}
 ;
 
-rw				:	T_LABEL T_POP_RW const
+rw				:	T_LABEL T_POP_RW uconst
 					{
 						sym_AddEqu( $1, sym_GetConstantValue("_RS") );
 						sym_AddSet( "_RS", sym_GetConstantValue("_RS")+2*$3 );
@@ -732,7 +733,7 @@
 					}
 ;
 
-rb				:	T_LABEL T_POP_RB const
+rb				:	T_LABEL T_POP_RB uconst
 					{
 						sym_AddEqu( $1, sym_GetConstantValue("_RS") );
 						sym_AddSet( "_RS", sym_GetConstantValue("_RS")+$3 );
@@ -739,7 +740,7 @@
 					}
 ;
 
-ds				:	T_POP_DS const
+ds				:	T_POP_DS uconst
 					{ out_Skip( $2 ); }
 ;
 
@@ -821,7 +822,7 @@
 
 incbin			:	T_POP_INCBIN string
 					{ out_BinaryFile( $2 ); }
-				|	T_POP_INCBIN string ',' const ',' const
+				|	T_POP_INCBIN string ',' uconst ',' uconst
 					{
 						out_BinaryFileSlice( $2, $4, $6 );
 					}
@@ -1042,6 +1043,14 @@
 						{ $$ = $2; }
 ;
 
+uconst			:	const
+					{
+						if($1 < 0)
+							fatalerror("Constant mustn't be negative: %d", $1);
+						$$=$1;
+					}
+;
+
 const			:	T_ID							{ $$ = sym_GetConstantValue($1); }
 				|	T_NUMBER 						{ $$ = $1; }
 				|	string						{ $$ = str2int($1); }
@@ -1115,7 +1124,7 @@
 
 string			:	T_STRING
 					{ strcpy($$,$1); }
-				|	T_OP_STRSUB '(' string ',' const ',' const ')'
+				|	T_OP_STRSUB '(' string ',' uconst ',' uconst ')'
 					{ strncpy($$,$3+$5-1,$7); $$[$7]=0; }
 				|	T_OP_STRCAT '(' string ',' string ')'
 					{ strcpy($$,$3); strcat($$,$5); }
@@ -1129,7 +1138,7 @@
 		{
 			out_NewSection($2,$4);
 		}
-	|	T_POP_SECTION string ',' sectiontype '[' const ']'
+	|	T_POP_SECTION string ',' sectiontype '[' uconst ']'
 		{
 			if( $6>=0 && $6<0x10000 )
 				out_NewAbsSection($2,$4,$6,-1);
@@ -1136,15 +1145,15 @@
 			else
 				yyerror("Address $%x not 16-bit", $6);
 		}
-	|	T_POP_SECTION string ',' sectiontype ',' T_OP_ALIGN '[' const ']'
+	|	T_POP_SECTION string ',' sectiontype ',' T_OP_ALIGN '[' uconst ']'
 		{
 			out_NewAlignedSection($2, $4, $8, -1);
 		}
-	|	T_POP_SECTION string ',' sectiontype ',' T_OP_BANK '[' const ']'
+	|	T_POP_SECTION string ',' sectiontype ',' T_OP_BANK '[' uconst ']'
 		{
 			bankrangecheck($2, $4, -1, $8);
 		}
-	|	T_POP_SECTION string ',' sectiontype '[' const ']' ',' T_OP_BANK '[' const ']'
+	|	T_POP_SECTION string ',' sectiontype '[' uconst ']' ',' T_OP_BANK '[' uconst ']'
 		{
 			if ($6 < 0 || $6 > 0x10000) {
 				yyerror("Address $%x not 16-bit", $6);
@@ -1151,11 +1160,11 @@
 			}
 			bankrangecheck($2, $4, $6, $11);
 		}
-	|	T_POP_SECTION string ',' sectiontype ',' T_OP_ALIGN '[' const ']' ',' T_OP_BANK '[' const ']'
+	|	T_POP_SECTION string ',' sectiontype ',' T_OP_ALIGN '[' uconst ']' ',' T_OP_BANK '[' uconst ']'
 		{
 			out_NewAlignedSection($2, $4, $8, $13);
 		}
-	|	T_POP_SECTION string ',' sectiontype ',' T_OP_BANK '[' const ']' ',' T_OP_ALIGN '[' const ']'
+	|	T_POP_SECTION string ',' sectiontype ',' T_OP_BANK '[' uconst ']' ',' T_OP_ALIGN '[' uconst ']'
 		{
 			out_NewAlignedSection($2, $4, $13, $8);
 		}