changeset 487:7fbf3171ca15

Add symbol table dump in assembly format Add --symbol-dump[=FILE] which will dump the global symbol table in assembly source format to stdout or to the FILE named by FILE.
author William Astle <lost@l-w.ca>
date Fri, 03 May 2019 19:44:02 -0600
parents e545196bf14f
children 94bbdb2890b7
files Makefile lwasm/lwasm.h lwasm/main.c lwasm/symdump.c
diffstat 4 files changed, 162 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Wed Apr 24 19:54:51 2019 -0600
+++ b/Makefile	Fri May 03 19:44:02 2019 -0600
@@ -56,7 +56,7 @@
 	insn_inh.c insn_logicmem.c insn_rel.c insn_rlist.c insn_rtor.c insn_tfm.c \
 	instab.c list.c lwasm.c macro.c main.c os9.c output.c pass1.c pass2.c \
 	pass3.c pass4.c pass5.c pass6.c pass7.c pragma.c pseudo.c section.c \
-	struct.c symbol.c unicorns.c
+	struct.c symbol.c symdump.c unicorns.c
 lwasm_srcs := $(addprefix lwasm/,$(lwasm_srcs))
 
 lwasm_objs := $(lwasm_srcs:.c=.o)
--- a/lwasm/lwasm.h	Wed Apr 24 19:54:51 2019 -0600
+++ b/lwasm/lwasm.h	Fri May 03 19:44:02 2019 -0600
@@ -76,6 +76,7 @@
 	FLAG_MAP = 0x0020,
 	FLAG_SYMBOLS_NOLOCALS = 0x0040,
 	FLAG_NOOUT = 0x80,
+	FLAG_SYMDUMP = 0x100,
 	FLAG_NONE = 0
 };
 
@@ -416,6 +417,7 @@
 	exportlist_t *exportlist;			// list of exported symbols
 	importlist_t *importlist;			// list of imported symbols
 	char *list_file;					// name of file to list to
+	char *symbol_dump_file;				// name of file to dump symbol table to
 	int tabwidth;						// tab width in list file
 	char *map_file;						// name of map file
 	char *output_file;					// output file name	
--- a/lwasm/main.c	Wed Apr 24 19:54:51 2019 -0600
+++ b/lwasm/main.c	Fri May 03 19:44:02 2019 -0600
@@ -49,6 +49,7 @@
 	{ "list-nofiles", 0x104, 0,			0,							"Omit file names in list output"},
 	{ "symbols",	's',	0,			lw_cmdline_opt_optional,	"Generate symbol list in listing, no effect without --list"},
 	{ "symbols-nolocals", 0x103,	0,	lw_cmdline_opt_optional,	"Same as --symbols but with local labels ignored"},
+	{ "symbol-dump", 0x106, "FILE",		lw_cmdline_opt_optional,	"Dump global symbol table in assembly format" },
 	{ "tabs",		't',	"WIDTH",	0,							"Set tab spacing in listing (0=don't expand tabs)" },
 	{ "map",		'm',	"FILE",		lw_cmdline_opt_optional,	"Generate map [to FILE]"},
 	{ "decb",		'b',	0,			0,							"Generate DECB .bin format output, equivalent of --format=decb"},
@@ -111,6 +112,16 @@
 		as -> flags |= FLAG_NOOUT;
 		break;
 
+	case 0x106:
+		if (as -> symbol_dump_file)
+			lw_free(as -> symbol_dump_file);
+		if (!arg)
+			as -> symbol_dump_file = lw_strdup("-");
+		else
+			as -> symbol_dump_file = lw_strdup(arg);
+		as -> flags |= FLAG_SYMDUMP;
+		break;
+
 	case 'd':
 #ifdef LWASM_NODEBUG
 		fprintf(stderr, "This binary has been built without debugging message support\n");
@@ -274,6 +285,7 @@
 void do_pass6(asmstate_t *as);
 void do_pass7(asmstate_t *as);
 void do_output(asmstate_t *as);
+void do_symdump(asmstate_t *as);
 void do_list(asmstate_t *as);
 void do_map(asmstate_t *as);
 lw_expr_t lwasm_evaluate_special(int t, void *ptr, void *priv);
@@ -390,6 +402,7 @@
 		debug_message(&asmstate, 50, "Invoking unicorns");
 		lwasm_do_unicorns(&asmstate);
 	}
+	do_symdump(&asmstate);
 	do_list(&asmstate);
 	do_map(&asmstate);
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwasm/symdump.c	Fri May 03 19:44:02 2019 -0600
@@ -0,0 +1,146 @@
+/*
+symdump.c
+
+Copyright © 2019 William Astle
+
+This file is part of LWTOOLS.
+
+LWTOOLS is free software: you can redistribute it and/or modify it under the
+terms of the GNU General Public License as published by the Free Software
+Foundation, either version 3 of the License, or (at your option) any later
+version.
+
+This program is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+more details.
+
+You should have received a copy of the GNU General Public License along with
+this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <lw_alloc.h>
+#include <lw_expr.h>
+#include <lw_string.h>
+
+#include "lwasm.h"
+
+struct listinfo
+{
+	sectiontab_t *sect;
+	asmstate_t *as;
+	int complex;
+};
+
+int dump_symbols_test(lw_expr_t e, void *p)
+{
+	struct listinfo *li = p;
+	
+	if (li -> complex)
+		return 0;
+	
+	if (lw_expr_istype(e, lw_expr_type_special))
+	{
+		if (lw_expr_specint(e) == lwasm_expr_secbase)
+		{
+			if (li -> sect)
+			{
+				li -> complex = 1;
+			}
+			else
+			{
+				li -> sect = lw_expr_specptr(e);
+			}
+		}
+	}
+	return 0;
+}
+
+void dump_symbols_aux(asmstate_t *as, FILE *of, struct symtabe *se)
+{
+	struct symtabe *s;
+	lw_expr_t te;
+	struct listinfo li;
+
+	li.as = as;
+	
+	if (!se)
+		return;
+	
+	dump_symbols_aux(as, of, se -> left);
+	
+	for (s = se; s; s = s -> nextver)
+	{	
+		if (s -> flags & symbol_flag_nolist)
+			continue;
+
+		if (s -> context >= 0)
+			continue;
+
+		lwasm_reduce_expr(as, s -> value);
+
+		fprintf(of, "%s ", s -> symbol);
+		if (s -> flags & symbol_flag_set)
+			fputs("SET", of);
+		else
+			fputs("EQU", of);
+		te = lw_expr_copy(s -> value);
+		li.complex = 0;
+		li.sect = NULL;
+		lw_expr_testterms(te, dump_symbols_test, &li);
+		if (li.sect)
+		{
+			as -> exportcheck = 1;
+			as -> csect = li.sect;
+			lwasm_reduce_expr(as, te);
+			as -> exportcheck = 0;
+		}
+		
+		if (lw_expr_istype(te, lw_expr_type_int))
+		{
+			fprintf(of, " %04X\n", lw_expr_intval(te));
+		}
+		else
+		{
+			fprintf(of, " 0 ; <<incomplete>>\n");
+		}
+		lw_expr_destroy(te);
+	}
+	
+	dump_symbols_aux(as, of, se -> right);
+}
+
+void do_symdump(asmstate_t *as)
+{
+	FILE *of;
+	
+	if (!(as -> flags & FLAG_SYMDUMP))
+	{
+		return;
+	}
+	else
+	{		
+		if (as -> symbol_dump_file)
+		{
+			if (strcmp(as -> symbol_dump_file, "-") == 0)
+			{
+				of = stdout;
+			}
+			else
+				of = fopen(as -> symbol_dump_file, "w");
+		}
+		else
+			of = stdout;
+
+		if (!of)
+		{
+			fprintf(stderr, "Cannot open list file; list not generated\n");
+			return;
+		}
+	}
+	dump_symbols_aux(as, of, as -> symtab.head);
+}