changeset 10:127e5b1e01c0

Removed use of asprintf() and added Makefile options for cross compiling; also a bugfix with sizeof{} in lwasm
author lost@l-w.ca
date Sat, 22 Jan 2011 10:37:53 -0700
parents 6eed14cccac9
children a0317b794b7b
files Makefile lwasm/input.c lwasm/pseudo.c lwasm/struct.c lwlink/lwlink.c
diffstat 5 files changed, 90 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sat Jan 22 10:04:32 2011 -0700
+++ b/Makefile	Sat Jan 22 10:37:53 2011 -0700
@@ -1,8 +1,29 @@
-CPPFLAGS += -I lwlib -D_GNU_SOURCE -DPACKAGE_STRING='"lwtools 4.0-pre"' -DPACKAGE_BUGREPORT='"lost@l-w.ca"'
+# define anything system specific here
+#
+# set these variables if needed
+# PROGSUFFIX: suffix added to binaries
+# BUILDTPREFIX: prefix added to build utilities (cc, etc.) for xcompile
+# can also set them when invoking "make"
+#PROGSUFFIX := .exe
+#BUILDTPREFIX=i586-mingw32msvc-
 
+# C compiler
+CC := $(BUILDTPREFIX)cc
+
+# ar
+AR := $(BUILDTPREFIX)ar
+
+# ranlib
+RANLIB := $(BUILDTPREFIX)ranlib
+
+CPPFLAGS += -I lwlib -DPACKAGE_STRING='"lwtools 4.0-pre"'
 LDFLAGS += -L$(PWD)/lwlib -llw
 
-MAIN_TARGETS := lwasm/lwasm lwlink/lwlink lwar/lwar lwlink/lwobjdump
+
+MAIN_TARGETS := lwasm/lwasm$(PROGSUFFIX) \
+	lwlink/lwlink$(PROGSUFFIX) \
+	lwar/lwar$(PROGSUFFIX) \
+	lwlink/lwobjdump$(PROGSUFFIX)
 
 .PHONY: all
 all: $(MAIN_TARGETS)
@@ -23,22 +44,22 @@
 lwlib_deps := $(lwlib_srcs:.c=.d)
 lwobjdump_deps := $(lwobjdump_srcs:.c=.d)
 
-.PHONY: lwlink lwasm lwar lwobjdump
-lwlink: lwlink/lwlink
-lwasm: lwasm/lwasm
-lwar: lwar/lwar
-lwobjdump: lwlink/lwobjdump
+.PHONY: lwlink lwasm lwar lwobjdump$(PROGSUFFIX)
+lwlink: lwlink/lwlink$(PROGSUFFIX)
+lwasm: lwasm/lwasm$(PROGSUFFIX)
+lwar: lwar/lwar$(PROGSUFFIX)
+lwobjdump: lwlink/lwobjdump$(PROGSUFFIX)
 
-lwasm/lwasm: $(lwasm_objs) lwlib lwasm/rules.make
+lwasm/lwasm$(PROGSUFFIX): $(lwasm_objs) lwlib lwasm/rules.make
 	$(CC) -o $@ $(lwasm_objs) $(LDFLAGS)
 
-lwlink/lwlink: $(lwlink_objs) lwlib lwlink/rules.make
+lwlink/lwlink$(PROGSUFFIX): $(lwlink_objs) lwlib lwlink/rules.make
 	$(CC) -o $@ $(lwlink_objs) $(LDFLAGS)
 
-lwlink/lwobjdump: $(lwobjdump_objs) lwlib lwlink/rules.make
+lwlink/lwobjdump$(PROGSUFFIX): $(lwobjdump_objs) lwlib lwlink/rules.make
 	$(CC) -o $@ $(lwobjdump_objs) $(LDFLAGS)
 
-lwar/lwar: $(lwar_objs) lwlib lwar/rules.make
+lwar/lwar$(PROGSUFFIX): $(lwar_objs) lwlib lwar/rules.make
 	$(CC) -o $@ $(lwar_objs) $(LDFLAGS)
 
 test: test.c lwlib
@@ -49,6 +70,7 @@
 
 lwlib/liblw.a: $(lwlib_objs) lwlib/rules.make
 	$(AR) rc $@ $(lwlib_objs)
+	$(RANLIB) $@
 
 %.d: %.c
 #	@echo "Building dependencies for $@"
@@ -67,7 +89,7 @@
 .PHONY: clean
 clean:
 	rm -f $(lwasm_deps) $(lwlink_deps) $(lwar_deps) $(lwlib_deps) $(lwobjdump_deps)
-	rm -f lwlib/liblw.a lwasm/lwasm lwlink/lwlink lwlink/lwobjdump lwar/lwar
+	rm -f lwlib/liblw.a lwasm/lwasm$(PROGSUFFIX) lwlink/lwlink$(PROGSUFFIX) lwlink/lwobjdump$(PROGSUFFIX) lwar/lwar$(PROGSUFFIX)
 	rm -f $(lwasm_objs) $(lwlink_objs) $(lwar_objs) $(lwlib_objs) $(lwobjdump_objs)
 	rm -f $(extra_clean)
 
--- a/lwasm/input.c	Sat Jan 22 10:04:32 2011 -0700
+++ b/lwasm/input.c	Sat Jan 22 10:37:53 2011 -0700
@@ -58,6 +58,16 @@
 	char *filespec;
 };
 
+static char *make_filename(char *p, char *f)
+{
+	int l;
+	char *r;
+	l = strlen(p) + strlen(f) + 1;
+	r = lw_alloc(l + 1);
+	sprintf(r, "%s/%s", p, f);
+	return r;
+}
+
 #define IS	((struct input_stack *)(as -> input_data))
 
 void input_init(asmstate_t *as)
@@ -179,7 +189,7 @@
 		
 		/* relative path, check relative to "current file" directory */
 		p = lw_stack_top(as -> file_dir);
-		(void)(0 == asprintf(&p2, "%s/%s", p, s));
+		p2 = make_filename(p, s);
 		debug_message(as, 1, "Open: (cd) %s\n", p2);
 		IS -> data = fopen(p2, "rb");
 		if (IS -> data)
@@ -195,7 +205,7 @@
 		lw_stringlist_reset(as -> include_list);
 		while ((p = lw_stringlist_current(as -> include_list)))
 		{
-			(void)(0 == asprintf(&p2, "%s/%s", p, s));
+			p2 = make_filename(p, s);
 		debug_message(as, 1, "Open (sp): %s\n", p2);
 			IS -> data = fopen(p2, "rb");
 			if (IS -> data)
@@ -247,7 +257,7 @@
 
 	/* relative path, check relative to "current file" directory */
 	p = lw_stack_top(as -> file_dir);
-	(void)(0 == asprintf(&p2, "%s/%s", p, s));
+	p2 = make_filename(p, s);
 	debug_message(as, 2, "Open file (st cd) %s", p2);
 	fp = fopen(p2, "rb");
 	if (fp)
@@ -261,7 +271,7 @@
 	lw_stringlist_reset(as -> include_list);
 	while ((p = lw_stringlist_current(as -> include_list)))
 	{
-		(void)(0 == asprintf(&p2, "%s/%s", p, s));
+		p2 = make_filename(p, s);
 		debug_message(as, 2, "Open file (st ip) %s", p2);
 		fp = fopen(p2, "rb");
 		if (fp)
--- a/lwasm/pseudo.c	Sat Jan 22 10:04:32 2011 -0700
+++ b/lwasm/pseudo.c	Sat Jan 22 10:37:53 2011 -0700
@@ -21,6 +21,7 @@
 
 #include <stdio.h>
 #include <ctype.h>
+#include <string.h>
 
 #include <lw_alloc.h>
 
@@ -1034,6 +1035,7 @@
 	char *fn, *p2;
 	char *p3;
 	int delim = 0;
+	int len;
 	
 	if (!**p)
 	{
@@ -1059,7 +1061,9 @@
 	if (delim && **p)
 		(*p)++;
 	
-	(void)(0 == asprintf(&p3, "include:%s", fn));
+	len = strlen(fn) + 8;
+	p3 = lw_alloc(len + 1);
+	sprintf(p3, "include:%s", fn);
 	input_open(as, p3);
 	lw_free(p3);
 
--- a/lwasm/struct.c	Sat Jan 22 10:04:32 2011 -0700
+++ b/lwasm/struct.c	Sat Jan 22 10:37:53 2011 -0700
@@ -76,13 +76,22 @@
 {
 	char *symname = NULL;
 	lw_expr_t te1, te2;
+	int len;
 	
 	while (e)
 	{
 		if (e -> name)
-			(void)(0 == asprintf(&symname, "%s.%s", prefix, e -> name));
+		{
+			len = strlen(prefix) + strlen(e -> name) + 1;
+			symname = lw_alloc(len + 1);
+			sprintf(symname, "%s.%s", prefix, e -> name);
+		}
 		else
-			(void)(0 == asprintf(&symname, "%s.____%d", prefix, *coff));
+		{
+			len = strlen(prefix) + 30;
+			symname = lw_alloc(len + 1);
+			sprintf(symname, "%s.____%d", prefix, *coff);
+		}
 		
 		// register the symbol
 		te1 = lw_expr_build(lw_expr_type_int, *coff);
@@ -94,7 +103,9 @@
 		if (e -> substruct)
 		{
 			char *t;
-			(void)(0 == asprintf(&t, "sizeof{%s}", symname));
+			len = strlen(symname) + 8;
+			t = lw_alloc(len + 1);
+			sprintf(t, "sizeof{%s}", symname);
 			te1 = lw_expr_build(lw_expr_type_int, e -> substruct -> size);
 			register_symbol(as, l, t, te1, symbol_flag_nocheck);
 			lw_expr_destroy(te1);
@@ -115,15 +126,18 @@
 	char *t;
 	int coff = 0;
 	lw_expr_t te;
-	
+	int len;
+		
 	if (as -> instruct == 0)
 	{
 		lwasm_register_warning(as, l, "endstruct without struct");
 		skip_operand(p);
 		return;
 	}
-	
-	(void)(0 == asprintf(&t, "sizeof{%s}", as -> cstruct -> name));
+
+	len = strlen(as -> cstruct -> name) + 8;
+	t = lw_alloc(len + 1);
+	sprintf(t, "sizeof{%s}", as -> cstruct -> name);
 	te = lw_expr_build(lw_expr_type_int, as -> cstruct -> size);
 	register_symbol(as, l, t, te, symbol_flag_nocheck);
 	lw_expr_destroy(te);
@@ -171,7 +185,8 @@
 	char *t;
 	lw_expr_t te;
 	int addr = 0;
-
+	int len;
+	
 	debug_message(as, 200, "Checking for structure expansion: %s", opc);
 
 	for (s = as -> structs; s; s = s -> next)
@@ -194,16 +209,28 @@
 	l -> len = s -> size;
 
 	if (as -> instruct)
-		(void)(0 == asprintf(&t, "sizeof(%s.%s}", as -> cstruct -> name, l -> sym));
+	{
+		len = strlen(as -> cstruct -> name) + strlen(l -> sym) + 9;
+		t = lw_alloc(len + 1);
+		sprintf(t, "sizeof{%s.%s}", as -> cstruct -> name, l -> sym);
+	}
 	else
-		(void)(0 == asprintf(&t, "sizeof{%s}", l -> sym));
+	{
+		len = strlen(l -> sym) + 8;
+		t = lw_alloc(len + 1);
+		sprintf(t, "sizeof{%s}", l -> sym);
+	}
 	te = lw_expr_build(lw_expr_type_int, s -> size);
 	register_symbol(as, l, t, te, symbol_flag_nocheck);
 	lw_expr_destroy(te);
 	lw_free(t);
 	
 	if (as -> instruct)
-		(void)(0 == asprintf(&t, "%s.%s", as -> cstruct -> name, l -> sym));
+	{
+		len = strlen(as -> cstruct -> name) + strlen(l -> sym) + 1;
+		t = lw_alloc(len + 1);
+		sprintf(t, "%s.%s", as -> cstruct -> name, l -> sym);
+	}
 	else
 		t = lw_strdup(l -> sym);
 	pseudo_endstruct_aux(as, l, s -> fields, t, &addr);
--- a/lwlink/lwlink.c	Sat Jan 22 10:04:32 2011 -0700
+++ b/lwlink/lwlink.c	Sat Jan 22 10:37:53 2011 -0700
@@ -23,7 +23,6 @@
 
 #define __lwlink_c_seen__
 
-#include <argp.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>