339
|
1 /*
|
|
2 symbol.c
|
|
3 Copyright © 2009 William Astle
|
|
4
|
|
5 This file is part of LWASM.
|
|
6
|
|
7 LWASM is free software: you can redistribute it and/or modify it under the
|
|
8 terms of the GNU General Public License as published by the Free Software
|
|
9 Foundation, either version 3 of the License, or (at your option) any later
|
|
10 version.
|
|
11
|
|
12 This program is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
15 more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License along with
|
|
18 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
19 */
|
|
20
|
|
21 /*
|
|
22 for handling the symbol table
|
|
23 */
|
|
24
|
|
25 #define __symbol_c_seen__
|
|
26 #include <config.h>
|
|
27 #include <string.h>
|
|
28
|
|
29 #include "lwasm.h"
|
|
30 #include "util.h"
|
|
31 #include "expr.h"
|
|
32
|
|
33 /*
|
|
34 Note that this function may accept symbols that the expression evaluator doesn't
|
|
35 recognize because the expression evaluator must avoid all ambiguity in order
|
|
36 to achieve predictable results. The checks here are simply a fuzz check.
|
|
37 */
|
|
38
|
|
39 /*
|
|
40 NOTE: complex symbols always take their value from slot 0 on the expression placeholders
|
|
41 for a line!
|
|
42 */
|
|
43 int lwasm_register_symbol(asmstate_t *as, lwasm_line_t *l, char *sym, int val, int flags)
|
|
44 {
|
|
45 lwasm_symbol_ent_t *se, *se2;
|
|
46 char *p;
|
|
47
|
|
48 int scontext = -1;
|
|
49
|
|
50 // if the symbol is constant, fall back to simple registration!
|
|
51 if (flags & SYMBOL_COMPLEX)
|
|
52 {
|
|
53 if (l -> exprs[0] == NULL)
|
|
54 {
|
|
55 val = l -> exprvals[0];
|
|
56 flags &= ~SYMBOL_COMPLEX;
|
|
57 }
|
|
58 }
|
|
59
|
|
60 // first check if the symbol is valid
|
|
61 // the following characters are allowed in a symbol:
|
|
62 // [a-zA-Z0-9._$?@] and any byte value larger than 0x7F
|
|
63 // although symbols should be restricted to the 7 bit range
|
|
64 // symbols must start with [a-zA-Z._]
|
|
65 if (!strchr(sym, '$'))
|
|
66 {
|
|
67 if (!strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._@?", *sym))
|
|
68 {
|
|
69 register_error(as, l, 1, "Bad symbol: %s", sym);
|
|
70 return -1;
|
|
71 }
|
|
72
|
|
73 if (*sym == '@' && isdigit(sym[1]))
|
|
74 {
|
|
75 register_error(as, l, 1, "Bad symbol: %s", sym);
|
|
76 return -1;
|
|
77 }
|
|
78 }
|
|
79
|
|
80 for (p = sym; *p; p++)
|
|
81 {
|
|
82 if (!strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._$?@0123456789", *sym))
|
|
83 {
|
|
84 register_error(as, l, 1, "Bad symbol: %s", sym);
|
|
85 return -1;
|
|
86 }
|
|
87 // flag local symbols while we're at it...
|
|
88 if (*p == '?' || *p == '@' || (*p == '$' && !(as -> pragmas & PRAGMA_DOLLARNOTLOCAL)))
|
|
89 scontext = as -> context;
|
|
90 }
|
|
91
|
|
92 debug_message(3, "lwasm_register_symbol(): registering '%s' (%d) at %04X; flags=%d", sym, scontext, val, flags);
|
|
93
|
|
94 // now look it for to see if it is a duplicate
|
|
95 se = lwasm_find_symbol(as, sym, scontext);
|
|
96 if (se)
|
|
97 {
|
|
98 if (flags & SYMBOL_FORCE && as -> passnum != 2)
|
|
99 {
|
|
100 register_error(as, l, 1, "Multiply defined symbol: %s", sym);
|
|
101 return -1;
|
|
102 }
|
|
103 if (!(flags & SYMBOL_SET) || (flags & SYMBOL_SET && !(se -> flags & SYMBOL_SET)))
|
|
104 {
|
|
105 register_error(as, l, 1, "Mulitply defined symbol: %s", sym);
|
|
106 return -1;
|
|
107 }
|
|
108 }
|
|
109 if (se)
|
|
110 {
|
|
111 se -> value = val;
|
|
112 if (flags & SYMBOL_COMPLEX)
|
|
113 {
|
|
114 se -> expr = l -> exprs[0];
|
|
115 }
|
|
116 else
|
|
117 {
|
|
118 se -> expr = NULL;
|
|
119 }
|
|
120 return;
|
|
121 }
|
|
122
|
|
123 // if not a duplicate, register it with the value
|
|
124 se = lwasm_alloc(sizeof(lwasm_symbol_ent_t));
|
|
125 if (as -> symhead)
|
|
126 {
|
|
127 se -> prev = NULL;
|
|
128 se -> next = as -> symhead;
|
|
129 as -> symhead -> prev = se;
|
|
130 as -> symhead = se;
|
|
131 }
|
|
132 else
|
|
133 {
|
|
134 se -> next = NULL;
|
|
135 se -> prev = NULL;
|
|
136 as -> symhead = se;
|
|
137 as -> symtail = se;
|
|
138 }
|
|
139 se -> value = val;
|
|
140 if (flags & SYMBOL_COMPLEX)
|
|
141 se -> expr = l -> exprs[0];
|
|
142 se -> sym = lwasm_strdup(sym);
|
|
143 se -> context = scontext;
|
|
144
|
|
145 if (!(flags & SYMBOL_EXTERN) && ((flags & SYMBOL_COMPLEX) || !(flags & SYMBOL_GLOBAL)))
|
|
146 se -> sect = as -> csect;
|
|
147 else
|
|
148 se -> sect = NULL;
|
|
149
|
|
150 se -> expr = NULL;
|
|
151 se -> flags = flags;
|
|
152 se -> externalname = NULL;
|
|
153
|
|
154 return 0;
|
|
155 }
|
|
156
|
|
157 lwasm_symbol_ent_t *lwasm_find_symbol(asmstate_t *as, char *sym, int scontext)
|
|
158 {
|
|
159 lwasm_symbol_ent_t *se;
|
|
160 static int st = 0;
|
|
161
|
|
162 for (se = as -> symhead; se; se = se -> next)
|
|
163 {
|
|
164 if (scontext == se -> context && !strcmp(sym, se -> sym))
|
|
165 {
|
|
166 return se;
|
|
167 }
|
|
168 }
|
|
169 if (as -> passnum == 2 && st == 0 && scontext == -1 && as -> outformat == OUTPUT_OBJ && as -> pragmas & PRAGMA_UNDEFEXTERN)
|
|
170 {
|
|
171 // we want undefined symbols to be considered external
|
|
172 // we didn't find it on a lookup so register it as external
|
|
173 // but we only do so when looking up in global context
|
|
174 st = 1;
|
|
175 if (lwasm_register_symbol(as, NULL, sym, 0, SYMBOL_EXTERN))
|
|
176 {
|
|
177 st = 0;
|
|
178 return NULL;
|
|
179 }
|
|
180 st = 0;
|
|
181
|
|
182 // find the newly registered symbol and return it
|
|
183 for (se = as -> symhead; se; se = se -> next)
|
|
184 {
|
|
185 if (scontext == se -> context && !strcmp(sym, se -> sym))
|
|
186 {
|
|
187 return se;
|
|
188 }
|
|
189 }
|
|
190 }
|
|
191
|
|
192 return NULL;
|
|
193 }
|
|
194
|
|
195 // reset the value of a symbol - should not be used normally
|
|
196 // it is intended for use by such operations as EQU
|
|
197 // returns -1 if the symbol is not registered
|
|
198 int lwasm_set_symbol(asmstate_t *as, char *sym, int scontext, int val)
|
|
199 {
|
|
200 lwasm_symbol_ent_t *se;
|
|
201
|
|
202 se = lwasm_find_symbol(as, sym, scontext);
|
|
203 if (!se)
|
|
204 return -1;
|
|
205
|
|
206 se -> value = val;
|
|
207 return 0;
|
|
208 }
|
|
209
|
|
210 void lwasm_list_symbols(asmstate_t *as, FILE *lf)
|
|
211 {
|
|
212 lwasm_symbol_ent_t *se;
|
|
213
|
|
214 for (se = as -> symhead; se; se = se -> next)
|
|
215 {
|
|
216 if (se -> expr)
|
|
217 {
|
|
218 fprintf(lf, "<incompl>");
|
|
219 }
|
|
220 else if (se -> value > 0xffff || se -> value < -0x8000)
|
|
221 {
|
|
222 fprintf(lf, "%08X ", se -> value);
|
|
223 }
|
|
224 else
|
|
225 {
|
|
226 fprintf(lf, " %04X ", se -> value);
|
|
227 }
|
|
228 if (se -> context < 0)
|
|
229 fputc('G', lf);
|
|
230 else
|
|
231 fputc('L', lf);
|
|
232
|
|
233 if (se -> flags & SYMBOL_SET)
|
|
234 fputc('S', lf);
|
|
235 else if (se -> flags & SYMBOL_EXTERN)
|
|
236 fputc('E', lf);
|
|
237 else
|
|
238 fputc(' ', lf);
|
|
239
|
|
240 fprintf(lf, " %s", se -> sym);
|
|
241
|
|
242 if (se -> context >= 0)
|
|
243 fprintf(lf, " (%d)", se -> context);
|
|
244
|
|
245 if (se -> sect)
|
|
246 {
|
|
247 fprintf(lf, " [%s]", se -> sect -> name);
|
|
248 }
|
|
249
|
|
250 fputc('\n', lf);
|
|
251 }
|
|
252 }
|