changeset 225:058f18119025

Fixed filename parsing bug in include directive and added includebin directive
author lost
date Thu, 11 Jun 2009 23:29:15 +0000
parents 2e5a32d56e60
children c8787fad0f9f
files ChangeLog lwasm/instab.c lwasm/pseudo.c
diffstat 3 files changed, 80 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu Jun 11 23:12:31 2009 +0000
+++ b/ChangeLog	Thu Jun 11 23:29:15 2009 +0000
@@ -14,6 +14,9 @@
 Version 2.5
 
 [!] Fixed error in the fix for invalid operands included in 2.4 [LWASM]
+[b] Fixed bug with "include" directive operand parsing [LWASM]
+[+] Added includebin directive to include the literal contents of a binary
+    file at the current assembly address. [LWASM]
 
 Version 2.4
 
--- a/lwasm/instab.c	Thu Jun 11 23:12:31 2009 +0000
+++ b/lwasm/instab.c	Thu Jun 11 23:29:15 2009 +0000
@@ -80,6 +80,7 @@
 extern OPFUNC(pseudo_ifdef);
 extern OPFUNC(pseudo_ifndef);
 extern OPFUNC(pseudo_noop);
+extern OPFUNC(pseudo_includebin);
 
 instab_t instab[] =
 {
@@ -351,6 +352,7 @@
 
 	{ "end", 	{ -1, -1, -1, -1 }, pseudo_end },
 
+	{ "includebin", { -1, -1, -1, -1}, pseudo_includebin },
 	{ "include", { -1, -1, -1, -1 }, pseudo_include },
 	
 	{ "align", { -1, -1, -1, -1 },	pseudo_align },
--- a/lwasm/pseudo.c	Thu Jun 11 23:12:31 2009 +0000
+++ b/lwasm/pseudo.c	Thu Jun 11 23:29:15 2009 +0000
@@ -21,6 +21,8 @@
 This file implements the various pseudo operations.
 */
 #include <config.h>
+#include <errno.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "lwasm.h"
@@ -117,6 +119,10 @@
 	memcpy(fn, *p, v1);
 	fn[v1] = '\0';
 
+	(*p) += v1;
+	if (**p == '"')
+		(*p)++;
+
 	// end local label context on include	
 	as -> context = lwasm_next_context(as);
 	if (lwasm_read_file(as, fn) < 0)
@@ -126,6 +132,75 @@
 	lwasm_free(fn);
 }
 
+/*
+The operand for includebin is a string optionally enclosed in "
+*/
+OPFUNC(pseudo_includebin)
+{
+	int v1;
+	char *fn;
+	FILE *f;
+	
+	// only include files on pass 1
+	while (**p && isspace(**p))
+		(*p)++;
+
+	if (!**p)
+	{
+		register_error(as, l, 1, "Bad file name");
+		return;
+	}
+
+	if (**p == '"')
+	{
+		// search for ending "
+		(*p)++;
+		for (v1 = 0; *((*p)+v1) && *((*p)+v1) != '"'; v1++)
+			/* do nothing */ ;
+		if (*((*p)+v1) != '"')
+		{
+			register_error(as, l, 1, "Bad file name");
+			return;
+		}
+	}
+	else
+	{
+		// search for a space type character
+		for (v1 = 0; *((*p)+v1) && !isspace(*((*p)+v1)); v1++)
+			;
+	}
+
+	fn = lwasm_alloc(v1 + 1);
+	memcpy(fn, *p, v1);
+	fn[v1] = '\0';
+
+	(*p) += v1;
+	if (**p == '"')
+		(*p)++;
+
+	// open the file
+	f = fopen(fn, "rb");
+	if (!f)
+	{
+		register_error(as, l, 1, "Cannot open file: %s", strerror(errno));
+		register_error(as, l, 2, "Cannot open file: %s", strerror(errno));
+	}
+	
+	// don't need fn any more
+	lwasm_free(fn);
+	
+	// read the contents of the file and "emit()" it
+	while (!feof(f) && !ferror(f))
+	{
+		v1 = fgetc(f);
+		if (v1 == EOF)
+			break;
+		lwasm_emit(as, l, v1);
+	}
+	// close file
+	fclose(f);
+}
+
 OPFUNC(pseudo_rmb)
 {
 	int r, v;