comparison lwasm/symdump.c @ 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
children 1c3220ed87a8
comparison
equal deleted inserted replaced
486:e545196bf14f 487:7fbf3171ca15
1 /*
2 symdump.c
3
4 Copyright © 2019 William Astle
5
6 This file is part of LWTOOLS.
7
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation, either version 3 of the License, or (at your option) any later
11 version.
12
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 more details.
17
18 You should have received a copy of the GNU General Public License along with
19 this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <lw_alloc.h>
27 #include <lw_expr.h>
28 #include <lw_string.h>
29
30 #include "lwasm.h"
31
32 struct listinfo
33 {
34 sectiontab_t *sect;
35 asmstate_t *as;
36 int complex;
37 };
38
39 int dump_symbols_test(lw_expr_t e, void *p)
40 {
41 struct listinfo *li = p;
42
43 if (li -> complex)
44 return 0;
45
46 if (lw_expr_istype(e, lw_expr_type_special))
47 {
48 if (lw_expr_specint(e) == lwasm_expr_secbase)
49 {
50 if (li -> sect)
51 {
52 li -> complex = 1;
53 }
54 else
55 {
56 li -> sect = lw_expr_specptr(e);
57 }
58 }
59 }
60 return 0;
61 }
62
63 void dump_symbols_aux(asmstate_t *as, FILE *of, struct symtabe *se)
64 {
65 struct symtabe *s;
66 lw_expr_t te;
67 struct listinfo li;
68
69 li.as = as;
70
71 if (!se)
72 return;
73
74 dump_symbols_aux(as, of, se -> left);
75
76 for (s = se; s; s = s -> nextver)
77 {
78 if (s -> flags & symbol_flag_nolist)
79 continue;
80
81 if (s -> context >= 0)
82 continue;
83
84 lwasm_reduce_expr(as, s -> value);
85
86 fprintf(of, "%s ", s -> symbol);
87 if (s -> flags & symbol_flag_set)
88 fputs("SET", of);
89 else
90 fputs("EQU", of);
91 te = lw_expr_copy(s -> value);
92 li.complex = 0;
93 li.sect = NULL;
94 lw_expr_testterms(te, dump_symbols_test, &li);
95 if (li.sect)
96 {
97 as -> exportcheck = 1;
98 as -> csect = li.sect;
99 lwasm_reduce_expr(as, te);
100 as -> exportcheck = 0;
101 }
102
103 if (lw_expr_istype(te, lw_expr_type_int))
104 {
105 fprintf(of, " %04X\n", lw_expr_intval(te));
106 }
107 else
108 {
109 fprintf(of, " 0 ; <<incomplete>>\n");
110 }
111 lw_expr_destroy(te);
112 }
113
114 dump_symbols_aux(as, of, se -> right);
115 }
116
117 void do_symdump(asmstate_t *as)
118 {
119 FILE *of;
120
121 if (!(as -> flags & FLAG_SYMDUMP))
122 {
123 return;
124 }
125 else
126 {
127 if (as -> symbol_dump_file)
128 {
129 if (strcmp(as -> symbol_dump_file, "-") == 0)
130 {
131 of = stdout;
132 }
133 else
134 of = fopen(as -> symbol_dump_file, "w");
135 }
136 else
137 of = stdout;
138
139 if (!of)
140 {
141 fprintf(stderr, "Cannot open list file; list not generated\n");
142 return;
143 }
144 }
145 dump_symbols_aux(as, of, as -> symtab.head);
146 }