changeset 67:d5fe306f1ab1

Fixed numerous bugs in macro handling
author lost
date Mon, 05 Jan 2009 05:40:33 +0000
parents aa9d9fedfdf4
children cef25b0088e6
files src/expr.h src/instab.c src/lwasm.h src/macro.c src/pass1.c
diffstat 5 files changed, 19 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/expr.h	Mon Jan 05 05:40:16 2009 +0000
+++ b/src/expr.h	Mon Jan 05 05:40:33 2009 +0000
@@ -107,10 +107,10 @@
 #define lwasm_expr_is_simple(s) ((s) -> head == (s) -> tail)
 
 // is the expression constant?
-#define lwasm_expr_is_constant(s) (lwasm_expr_is_simple(s) && (s) -> head -> term -> term_type == LWASM_TERM_INT)
+#define lwasm_expr_is_constant(s) (lwasm_expr_is_simple(s) && (!((s) -> head) || (s) -> head -> term -> term_type == LWASM_TERM_INT))
 
 // get the constant value of an expression or 0 if not constant
-#define lwasm_expr_get_value(s) (lwasm_expr_is_constant(s) ? (s) -> head -> term -> value : 0)
+#define lwasm_expr_get_value(s) (lwasm_expr_is_constant(s) ? ((s) -> head ? (s) -> head -> term -> value : 0) : 0)
 
 #undef __expr_E__
 
--- a/src/instab.c	Mon Jan 05 05:40:16 2009 +0000
+++ b/src/instab.c	Mon Jan 05 05:40:33 2009 +0000
@@ -353,8 +353,8 @@
 	{ "endc",	{ -1, -1, -1, -1}, 	pseudo_endc, 1 },
 	{ "else",	{ -1, -1, -1, -1}, 	pseudo_else, 1 },
 
-	{ "macro",	{ -1, -1, -1, -1}, 	pseudo_macro,	1,	0 },
-	{ "endm",	{ -1, -1, -1, -1},	pseudo_endm,	1,	1 },	
+	{ "macro",	{ -1, -1, -1, -1}, 	pseudo_macro,	1,	0,	1 },
+	{ "endm",	{ -1, -1, -1, -1},	pseudo_endm,	1,	1,	1 },
 
 	{ "setdp", 	{ -1, -1, -1, -1},	pseudo_setdp },
 	{ "set",	{ -1, -1, -1, -1},	pseudo_set,	0,	0,	1 },
--- a/src/lwasm.h	Mon Jan 05 05:40:16 2009 +0000
+++ b/src/lwasm.h	Mon Jan 05 05:40:33 2009 +0000
@@ -68,6 +68,7 @@
 	int nocodelen;		// for "RMB" type instructions
 	int addrset;		// set if this instruction sets the assembly address
 	int symaddr;		// set if this instruction sets a symbol addr with EQU or the like
+	int badop;			// bad operation - ignore it
 };
 
 // for keeping track of symbols
--- a/src/macro.c	Mon Jan 05 05:40:16 2009 +0000
+++ b/src/macro.c	Mon Jan 05 05:40:33 2009 +0000
@@ -146,7 +146,7 @@
 	macrotab_t *m;
 
 	char **args = NULL;		// macro arguments
-	int nargs;				// number of arguments
+	int nargs = 0;			// number of arguments
 
 	char *p2, *p3;
 	
@@ -183,6 +183,7 @@
 					if (p2[1])
 						p2++;
 				}
+				p2++;
 			}
 			
 			// have arg here
@@ -238,6 +239,7 @@
 			nl -> nocodelen = 0;
 			nl -> addrset = 0;
 			nl -> symaddr = -1;
+			nl -> badop = 0;
 			if (as -> linestail)
 				as -> linestail -> next = nl;
 			as -> linestail = nl;
@@ -256,13 +258,13 @@
 					n = *p2 - '0';
 					if (n == 0)
 					{
-						for (p3 = m -> name; p3; p3++)
+						for (p3 = m -> name; *p3; p3++)
 							macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
 						continue;
 					}
 					if (n < 1 || n > nargs)
 						continue;
-					for (p3 = args[n]; p3; p3++)
+					for (p3 = args[n - 1]; *p3; p3++)
 						macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
 					continue;
 				}
@@ -283,13 +285,13 @@
 					 
 					if (n == 0)
 					{
-						for (p3 = m -> name; p3; p3++)
+						for (p3 = m -> name; *p3; p3++)
 							macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
 						continue;
 					}
 					if (n < 1 || n > nargs)
 						continue;
-					for (p3 = args[n]; p3; p3++)
+					for (p3 = args[n - 1]; *p3; p3++)
 						macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
 					continue;
 				}
@@ -312,11 +314,14 @@
 	as -> context = oldcontext;
 
 	// clean up
-	while (nargs)
+	if (args)
 	{
-		lwasm_free(args[--nargs]);
+		while (nargs)
+		{
+			lwasm_free(args[--nargs]);
+		}
+		lwasm_free(args);
 	}
-	lwasm_free(args);
 
 	// indicate a macro was expanded
 	return 0;	
--- a/src/pass1.c	Mon Jan 05 05:40:16 2009 +0000
+++ b/src/pass1.c	Mon Jan 05 05:40:33 2009 +0000
@@ -142,6 +142,7 @@
 			nl -> nocodelen = 0;
 			nl -> addrset = 0;
 			nl -> symaddr = -1;
+			nl -> badop = 0;
 			if (as -> linestail)
 				as -> linestail -> next = nl;
 			as -> linestail = nl;