changeset 219:afd50d6b4113

Add --preprocess option Add --preprocess option which expands macros, resolves conditions, and resolves include files if possible. If an include file is not available, it the include directive is retained.
author William Astle <lost@l-w.ca>
date Sun, 10 Jun 2012 18:24:31 -0600
parents b0c9df865b25
children 3604d0ef06c6
files lwasm/input.c lwasm/lwasm.h lwasm/macro.c lwasm/main.c lwasm/pass1.c lwasm/pseudo.c
diffstat 6 files changed, 46 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/lwasm/input.c	Sun Jun 10 14:55:27 2012 -0600
+++ b/lwasm/input.c	Sun Jun 10 18:24:31 2012 -0600
@@ -41,7 +41,7 @@
 Data type for storing input buffers
 */
 
-#define IGNOREERROR (errno == ENOENT && (as -> flags & FLAG_DEPENDNOERR))
+#define IGNOREERROR (errno == ENOENT && (as -> preprocess || as -> flags & FLAG_DEPENDNOERR))
 
 enum input_types_e
 {
@@ -195,6 +195,10 @@
 			{
 				lw_error("Cannot open file '%s': %s\n", s, strerror(errno));
 			}
+			else
+			{
+				as -> fileerr = 1;
+			}
 			input_pushpath(as, s);
 			return;
 		}
@@ -233,6 +237,7 @@
 		if (IGNOREERROR)
 		{
 			input_pushpath(as, s);
+			as -> fileerr = 1;
 			return;
 		}
 		lw_error("Cannot open include file '%s': %s\n", s, strerror(errno));
--- a/lwasm/lwasm.h	Sun Jun 10 14:55:27 2012 -0600
+++ b/lwasm/lwasm.h	Sun Jun 10 18:24:31 2012 -0600
@@ -189,7 +189,7 @@
 
 	int noexpand_start;					// start of a no-expand block
 	int noexpand_end;					// end of a no-expand block
-	
+	int hideline;						// set if we're going to hide this line on output	
 };
 
 enum
@@ -304,6 +304,8 @@
 	lw_expr_t savedaddr;				// old address counter before struct started	
 	int exportcheck;					// set if we need to collapse out the section base to 0
 	int passno;							// set to the current pass number
+	int preprocess;						// set if we are prepocessing
+	int fileerr;						// flags error opening file
 };
 
 #ifndef ___symbol_c_seen___
--- a/lwasm/macro.c	Sun Jun 10 14:55:27 2012 -0600
+++ b/lwasm/macro.c	Sun Jun 10 18:24:31 2012 -0600
@@ -39,7 +39,7 @@
 	char tc;
 		
 	l -> len = 0;
-	
+	l -> hideline = 1;
 	if (as -> skipcond)
 	{
 		as -> skipmacro = 1;
@@ -90,6 +90,7 @@
 
 PARSEFUNC(pseudo_parse_endm)
 {
+	l -> hideline = 1;
 	l -> len = 0;
 
 	if (as -> skipcond)
@@ -332,5 +333,6 @@
 	}
 
 	// indicate a macro was expanded
+	l -> hideline = 1;
 	return 0;	
 }
--- a/lwasm/main.c	Sun Jun 10 14:55:27 2012 -0600
+++ b/lwasm/main.c	Sun Jun 10 18:24:31 2012 -0600
@@ -55,6 +55,7 @@
 	{ "6309",		'3',	0,			0,							"Set assembler to 6309 mode (default)" },
 	{ "includedir",	'I',	"PATH",		0,							"Add entry to include path" },
 	{ "define", 'D', "SYM[=VAL]", 0, "Automatically define SYM to be VAL (or 1)"},
+	{ "preprocess",	'P',	0,			0,							"Preprocess macros and conditionals and output revised source to stdout" },
 	{ 0 }
 };
 
@@ -172,6 +173,10 @@
 		as -> target = TARGET_6309;
 		break;
 
+	case 'P':
+		as -> preprocess = 1;
+		break;
+		
 	case lw_cmdline_key_end:
 		break;
 	
@@ -265,7 +270,12 @@
 		(passlist[passnum].fn)(&asmstate);
 		debug_message(&asmstate, 50, "After pass %d (%s)\n", passnum, passlist[passnum].passname);
 		dump_state(&asmstate);
-		
+
+		if (asmstate.preprocess)
+		{
+			/* we're done if we were preprocessing */
+			exit(0);
+		}
 		if (asmstate.errorcount > 0)
 		{
 			if (asmstate.flags & FLAG_DEPEND)
--- a/lwasm/pass1.c	Sun Jun 10 14:55:27 2012 -0600
+++ b/lwasm/pass1.c	Sun Jun 10 18:24:31 2012 -0600
@@ -413,7 +413,9 @@
 			}
 			debug_message(as, 40, "Line address: %s", lw_expr_print(cl -> addr));
 		}
-		
+		if (as -> skipcond || as -> inmacro || cl -> ltext[0] == 1)
+			cl -> hideline = 1;
+			
 	nextline:
 		if (sym)
 			lw_free(sym);
@@ -421,6 +423,11 @@
 		
 		lw_free(line);
 		
+		if (cl -> hideline == 0)
+		{
+			printf("%s\n", cl -> ltext);
+		}
+		
 		// if we've hit the "end" bit, finish out
 		if (as -> endseen)
 			return;
--- a/lwasm/pseudo.c	Sun Jun 10 14:55:27 2012 -0600
+++ b/lwasm/pseudo.c	Sun Jun 10 18:24:31 2012 -0600
@@ -1017,6 +1017,7 @@
 PARSEFUNC(pseudo_parse_ifp1)
 {
 	l -> len = 0;
+	l -> hideline = 1;
 	
 	if (as -> skipcond && !(as -> skipmacro))
 	{
@@ -1032,6 +1033,7 @@
 PARSEFUNC(pseudo_parse_ifp2)
 {
 	l -> len = 0;
+	l -> hideline = 1;
 	
 	if (as -> skipcond && !(as -> skipmacro))
 	{
@@ -1048,6 +1050,7 @@
 	lw_expr_t e;
 	
 	l -> len = 0;
+	l -> hideline = 1;
 	
 	if (as -> skipcond && !(as -> skipmacro))
 	{
@@ -1071,6 +1074,7 @@
 	lw_expr_t e;
 	
 	l -> len = 0;
+	l -> hideline = 1;
 	
 	if (as -> skipcond && !(as -> skipmacro))
 	{
@@ -1095,6 +1099,7 @@
 	lw_expr_t e;
 	
 	l -> len = 0;
+	l -> hideline = 1;
 	
 	if (as -> skipcond && !(as -> skipmacro))
 	{
@@ -1118,6 +1123,7 @@
 	lw_expr_t e;
 	
 	l -> len = 0;
+	l -> hideline = 1;
 	
 	if (as -> skipcond && !(as -> skipmacro))
 	{
@@ -1141,6 +1147,7 @@
 	lw_expr_t e;
 	
 	l -> len = 0;
+	l -> hideline = 1;
 	
 	if (as -> skipcond && !(as -> skipmacro))
 	{
@@ -1163,6 +1170,7 @@
 {
 	lw_expr_t e;
 
+	l -> hideline = 1;
 	l -> len = 0;
 	
 	if (as -> skipcond && !(as -> skipmacro))
@@ -1184,6 +1192,7 @@
 
 PARSEFUNC(pseudo_parse_endc)
 {
+	l -> hideline = 1;
 	l -> len = 0;
 	skip_operand(p);
 	if (as -> skipcond && !(as -> skipmacro))
@@ -1197,6 +1206,7 @@
 PARSEFUNC(pseudo_parse_else)
 {
 	l -> len = 0;
+	l -> hideline = 1;
 	skip_operand(p);
 	
 	if (as -> skipmacro)
@@ -1222,6 +1232,7 @@
 	struct symtabe *s;
 	
 	l -> len = 0;
+	l -> hideline = 1;
 	
 	if (as -> skipcond && !(as -> skipmacro))
 	{
@@ -1261,6 +1272,7 @@
 	struct symtabe *s;
 	
 	l -> len = 0;
+	l -> hideline = 1;
 	
 	if (as -> skipcond && !(as -> skipmacro))
 	{
@@ -1409,9 +1421,12 @@
 	len = strlen(fn) + 8;
 	p3 = lw_alloc(len + 1);
 	sprintf(p3, "include:%s", fn);
+	as -> fileerr = 0;
 	input_open(as, p3);
 	lw_free(p3);
 
+	if (as -> fileerr == 0)
+		l -> hideline = 1;
 	l -> len = 0;
 	lw_free(fn);
 }