shithub: rgbds

Download patch

ref: 42b3a173566a70c665066bf9e1f8d2568b9b176c
parent: 4ce4fdec711df0c0f3af7ab955140161165716be
parent: 4e1d79081c3aba476daf38ba42180281752a8e3b
author: Eldred Habert <eldredhabert0@gmail.com>
date: Tue Oct 13 11:48:16 EDT 2020

Merge pull request #602 from NieDzejkob/shiftstorm

Report only one error when invalid shift has argument

--- a/include/asm/macro.h
+++ b/include/asm/macro.h
@@ -29,7 +29,7 @@
 char const *macro_GetUniqueIDStr(void);
 void macro_SetUniqueID(uint32_t id);
 uint32_t macro_UseNewUniqueID(void);
-void macro_ShiftCurrentArgs(void);
+void macro_ShiftCurrentArgs(int32_t count);
 uint32_t macro_NbArgs(void);
 
 #endif
--- a/src/asm/macro.c
+++ b/src/asm/macro.c
@@ -128,12 +128,17 @@
 	return maxUniqueID;
 }
 
-void macro_ShiftCurrentArgs(void)
+void macro_ShiftCurrentArgs(int32_t count)
 {
-	if (!macroArgs)
+	if (!macroArgs) {
 		error("Cannot shift macro arguments outside of a macro\n");
-	else if (macroArgs->shift != macroArgs->nbArgs)
-		macroArgs->shift++;
+	} else if (count < 0) {
+		error("Cannot shift arguments by negative amount %" PRId32 "\n", count);
+	} else if (macroArgs->shift < macroArgs->nbArgs) {
+		macroArgs->shift += count;
+		if (macroArgs->shift > macroArgs->nbArgs)
+			macroArgs->shift = macroArgs->nbArgs;
+	}
 }
 
 uint32_t macro_NbArgs(void)
--- a/src/asm/parser.y
+++ b/src/asm/parser.y
@@ -579,13 +579,8 @@
 		}
 ;
 
-shift		: T_POP_SHIFT		{ macro_ShiftCurrentArgs(); }
-		| T_POP_SHIFT uconst
-		{
-			int32_t i = $2;
-			while (i--)
-				macro_ShiftCurrentArgs();
-		}
+shift		: T_POP_SHIFT		{ macro_ShiftCurrentArgs(1); }
+		| T_POP_SHIFT const	{ macro_ShiftCurrentArgs($2); }
 ;
 
 load		: T_POP_LOAD string ',' sectiontype sectorg sectattrs {
--- /dev/null
+++ b/test/asm/shift-negative.asm
@@ -1,0 +1,4 @@
+m: MACRO
+	shift -3
+ENDM
+	m
--- /dev/null
+++ b/test/asm/shift-negative.err
@@ -1,0 +1,3 @@
+ERROR: shift-negative.asm(4) -> shift-negative.asm::m(2):
+    Cannot shift arguments by negative amount -3
+error: Assembly aborted (1 errors)!
--- a/test/asm/shift-outside-macro.asm
+++ b/test/asm/shift-outside-macro.asm
@@ -1,1 +1,2 @@
 shift
+shift 3
--- a/test/asm/shift-outside-macro.err
+++ b/test/asm/shift-outside-macro.err
@@ -1,3 +1,5 @@
 ERROR: shift-outside-macro.asm(1):
     Cannot shift macro arguments outside of a macro
-error: Assembly aborted (1 errors)!
+ERROR: shift-outside-macro.asm(2):
+    Cannot shift macro arguments outside of a macro
+error: Assembly aborted (2 errors)!