shithub: rgbds

Download patch

ref: a828f82414eb9b4ea828c32bf1bbfa43c875b32d
parent: 4d0d6664d7cf2dfa5040c1a7e198bb3d310de96d
author: aaaaaa123456789 <aaaaaa123456789@acidch.at>
date: Tue May 12 02:46:07 EDT 2020

Remove fixed-size array for macro arguments

--- a/include/asm/macro.h
+++ b/include/asm/macro.h
@@ -20,7 +20,7 @@
 
 struct MacroArgs *macro_GetCurrentArgs(void);
 struct MacroArgs *macro_NewArgs(void);
-void macro_AppendArg(struct MacroArgs *args, char *s);
+void macro_AppendArg(struct MacroArgs **args, char *s);
 void macro_UseNewArgs(struct MacroArgs *args);
 void macro_FreeArgs(struct MacroArgs *args);
 char const *macro_GetArg(uint32_t i);
--- a/src/asm/asmy.y
+++ b/src/asm/asmy.y
@@ -720,10 +720,10 @@
 		}
 		| T_STRING {
 			$$ = macro_NewArgs();
-			macro_AppendArg($$, strdup($1));
+			macro_AppendArg(&($$), strdup($1));
 		}
 		| macroargs ',' T_STRING {
-			macro_AppendArg($$, strdup($3));
+			macro_AppendArg(&($$), strdup($3));
 		}
 ;
 
--- a/src/asm/macro.c
+++ b/src/asm/macro.c
@@ -10,13 +10,12 @@
 #include "asm/warning.h"
 
 struct MacroArgs {
-	char *args[MAXMACROARGS];
 	unsigned int nbArgs;
 	unsigned int shift;
+	char *args[];
 };
 
-static struct MacroArgs defaultArgs = { .nbArgs = 0, .shift = 0 };
-static struct MacroArgs *macroArgs = &defaultArgs;
+static struct MacroArgs *macroArgs = NULL;
 static uint32_t uniqueID = -1;
 /*
  * The initialization is somewhat harmful, since it is never used, but it
@@ -40,12 +39,13 @@
 	return args;
 }
 
-void macro_AppendArg(struct MacroArgs *args, char *s)
+void macro_AppendArg(struct MacroArgs **args, char *s)
 {
-	if (args->nbArgs == MAXMACROARGS)
+	if ((**args).nbArgs == MAXMACROARGS)
 		yyerror("A maximum of " EXPAND_AND_STR(MAXMACROARGS)
 			" arguments is allowed");
-	args->args[args->nbArgs++] = s;
+	*args = realloc(*args, sizeof **args + sizeof (char *) * (1 + (**args).nbArgs));
+	(**args).args[(**args).nbArgs++] = s;
 }
 
 void macro_UseNewArgs(struct MacroArgs *args)