diff src/list.c @ 44:2330b88f9600

Added simple output listing
author lost
date Sun, 04 Jan 2009 06:52:18 +0000
parents 39d750ee8d34
children be459d69f481
line wrap: on
line diff
--- 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);
 }