changeset 210:5d969517db74

Added condundefzero pragma Added pragma condundefzero to allow the assembler to treat symbols that are undefined in a conditional expression as if their value had been set to zero.
author William Astle <lost@l-w.ca>
date Sat, 09 Jun 2012 16:25:19 -0600
parents 52d9dd71f555
children 6f2e18f1fe67
files docs/manual.docbook.sgml lwasm/lwasm.c lwasm/lwasm.h lwasm/pragma.c
diffstat 4 files changed, 37 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/docs/manual.docbook.sgml	Sat Jun 09 16:03:36 2012 -0600
+++ b/docs/manual.docbook.sgml	Sat Jun 09 16:25:19 2012 -0600
@@ -1673,6 +1673,25 @@
 all cases when using the object file assembly target.  It is intended for
 use only when the assembler will be producing the final binary.</para>
 
+<varlistentry>
+<term>condundefzero</term>
+<listitem>
+
+<para>This pragma will cause the assembler to change the way it handles
+symbols in conditional expressions.  Ordinarily, any symbol that is not
+defined prior to the conditional will throw an undefined symbol error.  With
+this pragma in effect, symbols that are not yet defined at the point the
+conditional is encountered will be treated as zero.</para>
+
+<para>This is not the default because it encourages poor code design. One
+should use the "IFDEF" or "IFNDEF" conditionals to test for the presence of
+a symbol.</para>
+
+<para>It is important to note that if a symbol is defined but it does not
+yet evaluate to a constant value at the point where the conditional appears,
+the assembler will still complain about a non constant condition.</para>
+
+
 </listitem>
 </varlistentry>
 
--- a/lwasm/lwasm.c	Sat Jun 09 16:03:36 2012 -0600
+++ b/lwasm/lwasm.c	Sat Jun 09 16:25:19 2012 -0600
@@ -58,6 +58,12 @@
 		return e;
 	}
 	
+	if (as -> undefzero)
+	{
+		e = lw_expr_build(lw_expr_type_int, 0);
+		return e;
+	}
+	
 	// undefined here is undefied unless output is object
 	if (as -> output_format != OUTPUT_OBJ)
 		goto nomatch;
@@ -896,6 +902,14 @@
 		return NULL;
 	}
 
+	/* handle condundefzero */
+	if (CURPRAGMA(as -> cl, PRAGMA_CONDUNDEFZERO))
+	{
+		as -> undefzero = 1;
+		lwasm_reduce_expr(as, e);
+		as -> undefzero = 0;
+	}
+
 	/* we need to simplify the expression here */
 	debug_message(as, 250, "Doing interim reductions");
 	lwasm_interim_reduce(as);
--- a/lwasm/lwasm.h	Sat Jun 09 16:03:36 2012 -0600
+++ b/lwasm/lwasm.h	Sat Jun 09 16:25:19 2012 -0600
@@ -84,7 +84,8 @@
 	PRAGMA_NOLIST = 0x0080,				// don't show line in listing
 	PRAGMA_AUTOBRANCHLENGTH = 0x0100,	// automatically select proper length for relative branches
 	PRAGMA_EXPORT = 0x0200,				// export symbols by default, unless local
-	PRAGMA_SYMBOLNOCASE = 0x400			// symbols defined under this pragma are matched case insensitively
+	PRAGMA_SYMBOLNOCASE = 0x400,		// symbols defined under this pragma are matched case insensitively
+	PRAGMA_CONDUNDEFZERO = 0x800		// treat undefined symbols as zero in conditionals during pass 1
 };
 
 
@@ -269,6 +270,7 @@
 	int endseen;						// have we seen an "end" pseudo?
 	int execaddr;						// address from "end"
 	int inmod;							// inside an os9 module?
+	int undefzero;						// used for handling "condundefzero"
 	unsigned char crc[3];				// crc accumulator
 	int badsymerr;						// throw error on undef sym if set
 
--- a/lwasm/pragma.c	Sat Jun 09 16:03:36 2012 -0600
+++ b/lwasm/pragma.c	Sat Jun 09 16:25:19 2012 -0600
@@ -59,6 +59,7 @@
 	{ "export", "noexport", PRAGMA_EXPORT },
 	{ "symbolnocase", "nosymbolnocase", PRAGMA_SYMBOLNOCASE },
 	{ "nosymbolcase", "symbolcase", PRAGMA_SYMBOLNOCASE },
+	{ "condundefzero", "nocondundefzero", PRAGMA_CONDUNDEFZERO },
 	{ 0, 0, 0}
 };