changeset 104:2ba8f9ef1417

Added ifdef/ifndef conditionals
author lost
date Sun, 25 Jan 2009 04:02:50 +0000
parents 26c058fa0bc1
children faf43dc805c9
files src/instab.c src/pseudo.c
diffstat 2 files changed, 114 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/instab.c	Fri Jan 23 05:48:55 2009 +0000
+++ b/src/instab.c	Sun Jan 25 04:02:50 2009 +0000
@@ -77,6 +77,8 @@
 extern OPFUNC(pseudo_starpragma);
 extern OPFUNC(pseudo_extern);
 extern OPFUNC(pseudo_export);
+extern OPFUNC(pseudo_ifdef);
+extern OPFUNC(pseudo_ifndef);
 
 instab_t instab[] =
 {
@@ -363,6 +365,8 @@
 	{ "ifle",	{ -1, -1, -1, -1}, 	pseudo_ifle, 1 },
 	{ "endc",	{ -1, -1, -1, -1}, 	pseudo_endc, 1 },
 	{ "else",	{ -1, -1, -1, -1}, 	pseudo_else, 1 },
+	{ "ifdef",	{ -1, -1, -1, -1},	pseudo_ifdef, 1},
+	{ "ifndef",	{ -1, -1, -1, -1},	pseudo_ifndef, 1},
 
 	{ "macro",	{ -1, -1, -1, -1}, 	pseudo_macro,	1,	0,	1 },
 	{ "endm",	{ -1, -1, -1, -1},	pseudo_endm,	1,	1,	1 },
--- a/src/pseudo.c	Fri Jan 23 05:48:55 2009 +0000
+++ b/src/pseudo.c	Sun Jan 25 04:02:50 2009 +0000
@@ -551,6 +551,116 @@
 	}
 }
 
+OPFUNC(pseudo_ifdef)
+{
+	lwasm_symbol_ent_t *se;
+	char *sym;
+	char *p2;
+	
+	if (as -> skipcond && !(as -> skipmacro))
+	{
+		as -> skipcount++;
+		return;
+	}
+
+	if (as -> passnum != 1)
+	{
+		if (!(l -> fsize))
+		{
+			as -> skipcond = 1;
+			as -> skipcount = 1;
+		}
+		return;
+	}
+
+	if (!**p)
+	{
+		register_error(as, l, 1, "Need symbol name");
+		return;
+	}
+	
+	for (p2 = *p; *p2 && !isspace(*p2); p2++)
+		/* do nothing */ ;
+	
+	sym = lwasm_alloc(p2 - *p + 1);
+	memcpy(sym, *p, p2 - *p);
+	sym[p2 - *p] = '\0';
+	
+	*p = p2;
+
+	se = lwasm_find_symbol(as, sym, l -> context);
+	if (!se)
+		se = lwasm_find_symbol(as, sym, -1);
+	
+	lwasm_free(sym);
+	
+	if (!se)
+	{
+		as -> skipcond = 1;
+		as -> skipcount = 1;
+		l -> fsize = 0;
+	}
+	else
+	{
+		l -> fsize = 1;
+	}
+}
+
+OPFUNC(pseudo_ifndef)
+{
+	lwasm_symbol_ent_t *se;
+	char *sym;
+	char *p2;
+	
+	if (as -> skipcond && !(as -> skipmacro))
+	{
+		as -> skipcount++;
+		return;
+	}
+
+	if (as -> passnum != 1)
+	{
+		if (l -> fsize)
+		{
+			as -> skipcond = 1;
+			as -> skipcount = 1;
+		}
+		return;
+	}
+
+	if (!**p)
+	{
+		register_error(as, l, 1, "Need symbol name");
+		return;
+	}
+	
+	for (p2 = *p; *p2 && !isspace(*p2); p2++)
+		/* do nothing */ ;
+	
+	sym = lwasm_alloc(p2 - *p + 1);
+	memcpy(sym, *p, p2 - *p);
+	sym[p2 - *p] = '\0';
+	
+	*p = p2;
+
+	se = lwasm_find_symbol(as, sym, l -> context);
+	if (!se)
+		se = lwasm_find_symbol(as, sym, -1);
+	
+	lwasm_free(sym);
+	
+	if (se)
+	{
+		as -> skipcond = 1;
+		as -> skipcount = 1;
+		l -> fsize = 0;
+	}
+	else
+	{
+		l -> fsize = 1;
+	}
+}
+
 OPFUNC(pseudo_ifeq)
 {
 	int v1;