changeset 44:2330b88f9600

Added simple output listing
author lost
date Sun, 04 Jan 2009 06:52:18 +0000
parents b33eca135258
children be459d69f481
files src/expr.c src/list.c src/lwasm.h src/parse.c src/pass1.c src/pass2.c
diffstat 6 files changed, 94 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/expr.c	Sun Jan 04 06:16:22 2009 +0000
+++ b/src/expr.c	Sun Jan 04 06:52:18 2009 +0000
@@ -58,7 +58,7 @@
 {
 	if (t)
 	{
-		if (t -> symbol)
+		if (t -> term_type == LWASM_TERM_SYM)
 			lwasm_free(t -> symbol);
 		lwasm_free(t);
 	}
--- a/src/list.c	Sun Jan 04 06:16:22 2009 +0000
+++ b/src/list.c	Sun Jan 04 06:52:18 2009 +0000
@@ -40,12 +40,79 @@
 			{
 				fprintf(stderr, "ERROR: %s\n", e -> mess);
 			}
-			fprintf(stderr, "%s\n", l -> text);
+			fprintf(stderr, "%s:%d: %s\n", l -> filename, l -> lineno, l -> text);
 		}
 	}
 }
 
 void lwasm_list(asmstate_t *as)
 {
+	FILE *lf;
+	lwasm_line_t *l;
+	int c;
+	char *p;
+	
+	if (as -> listfile[0] == '-' && as -> listfile[1] == '\0')
+		lf = stdout;
+	else
+	{
+		lf = fopen(as -> listfile, "o");
+		if (!lf)
+		{
+			fprintf(stderr, "Unable to open list file. No listing will be generated!\n");
+			goto showerr;
+		}
+	}
+
+	for (l = as -> lineshead; l; l = l -> next)
+	{
+		fprintf(lf, "%04X ", l -> codeaddr);
+		
+		if (l -> codelen > 0)
+		{
+			for (c = 0; c < l -> codelen && c < 5; c++)
+			{
+				fprintf(lf, "%02X", l -> bytes[c]);
+			}
+		}
+		while (c < 5)
+		{
+			fprintf(lf, "  ");
+			c++;
+		}
+		fprintf(lf, " %20.20s:%05d ", l -> filename, l -> lineno);
+		
+		// print line here
+		for (c = 0, p = l -> text; *p; c++, p++)
+		{
+			if (*p == '\t')
+			{
+				int c2;
+				c2 = 8 - (c % 8);
+				while (c2--) fputc(' ', lf);
+			}
+			else
+				fputc(*p, lf);
+		}
+		fputc('\n', lf);
+		
+		if (l -> codelen > 5)
+		{
+			fprintf(lf, "     ");
+			for (c = 5; c < l -> codelen; c++)
+			{
+				if (!(c % 5) && c != 5)
+				{
+					fprintf(lf, "\n     ");
+				}
+				fprintf(lf, "%02X", l -> bytes[c]);
+			}
+			fputc('\n', lf);
+		}
+	}
+	if (lf != stdout)
+		fclose(lf);
+
+showerr:
 	lwasm_show_errors(as);
 }
--- a/src/lwasm.h	Sun Jan 04 06:16:22 2009 +0000
+++ b/src/lwasm.h	Sun Jan 04 06:52:18 2009 +0000
@@ -53,6 +53,7 @@
 	unsigned char *bytes;	// actual bytes emitted
 	int codelen;		// number of bytes emitted
 	int codesize;		// the size of the code buffer
+	int codeaddr;		// address the code goes at
 };
 
 // for keeping track of symbols
--- a/src/parse.c	Sun Jan 04 06:16:22 2009 +0000
+++ b/src/parse.c	Sun Jan 04 06:52:18 2009 +0000
@@ -37,7 +37,7 @@
 	char *p, *p2;
 	char *opc;
 	int opnum;
-	char *sym;
+	char *sym = NULL;
 	
 	p = l -> text;
 	
@@ -47,6 +47,9 @@
 		return 0;
 	}
 	
+	// for output generation later
+	l -> codeaddr = as -> addr;
+	
 	if (!isspace(*p) && *p != '*' && *p != ';')
 	{
 		// we have a symbol specified here
@@ -59,12 +62,17 @@
 		sym[p2 - p] = '\0';
 		memcpy(sym, p, p2 - p);
 		
-		l -> sym = sym;
-		// have a symbol; now determine if it is valid and register it
-		// at the current address of the line
-		debug_message(1, "Registering symbol '%s' at %04X", sym, as -> addr);
-		if (lwasm_register_symbol(as, l, sym, as -> addr) < 0)
-			l -> sym = NULL;
+		p = p2;
+		
+		if (as -> passnum == 1)
+		{
+			l -> sym = sym;
+			// have a symbol; now determine if it is valid and register it
+			// at the current address of the line
+			debug_message(1, "Registering symbol '%s' at %04X", sym, as -> addr);
+			if (lwasm_register_symbol(as, l, sym, as -> addr) < 0)
+				l -> sym = NULL;
+		}
 	}
 	else
 	{
@@ -84,7 +92,8 @@
 	// if comment or end of line, return
 	if (!*p || *p == '*' || *p == ';')
 	{
-		lwasm_free(l -> sym);
+		if (sym)
+			lwasm_free(l -> sym);
 		return 0;
 	}
 	
@@ -115,7 +124,8 @@
 	{
 		// invalid operation code, throw error
 		register_error(as, l, 1, "Invalid operation code '%s'", opc);
-		lwasm_free(l -> sym);
+		if (sym)
+			lwasm_free(l -> sym);
 		lwasm_free(opc);
 		return -1;
 	}
@@ -124,5 +134,6 @@
 	(instab[opnum].fn)(as, l, &p2, opnum);
 	
 	lwasm_free(opc);
-	lwasm_free(sym);
+	if (sym)
+		lwasm_free(sym);
 }
--- a/src/pass1.c	Sun Jan 04 06:16:22 2009 +0000
+++ b/src/pass1.c	Sun Jan 04 06:52:18 2009 +0000
@@ -141,8 +141,7 @@
 			nl -> codesize = 0;
 			if (as -> linestail)
 				as -> linestail -> next = nl;
-			else
-				as -> linestail = nl;
+			as -> linestail = nl;
 			if (!(as -> lineshead))
 				as -> lineshead = nl;
 			lwasm_parse_line(as, nl);
@@ -160,6 +159,7 @@
 	as -> passnum = 1;
 	as -> addr = 0;
 	
+	debug_message(1, "Entering pass 1");
 	if (lwasm_read_file(as, as -> infile) < 0)
 	{
 		fprintf(stderr, "Error reading input file '%s'", as -> infile);
--- a/src/pass2.c	Sun Jan 04 06:16:22 2009 +0000
+++ b/src/pass2.c	Sun Jan 04 06:52:18 2009 +0000
@@ -32,6 +32,7 @@
 {
 	lwasm_line_t *l;
 
+	debug_message(1, "Entering pass 2");
 	as -> passnum = 2;
 	as -> addr = 0;