Debian Codegraph @

src/symbol.c

raw symbols SCIP
1 // ACME - a crossassembler for producing 6502/65c02/65816/65ce02 code.
2 // Copyright (C) 1998-2025 Marco Baye
3 // Have a look at "acme.c" for further info
4 //
5 // symbol stuff
6 //
7 // 22 Nov 2007 "warn on indented labels" is now a CLI switch
8 // 25 Sep 2011 Fixed bug in !sl (colons in filename could be interpreted as EOS)
9 // 23 Nov 2014 Added label output in VICE format
10 #include "symbol.h"
11 #include <stdio.h>
12 #include <string.h> // for memcpy()
13 #include "alu.h"
14 #include "dynabuf.h"
15 #include "global.h"
16 #include "input.h"
17 #include "output.h"
18 #include "platform.h"
19 #include "section.h"
20 #include "tree.h"
21 #include "typesystem.h"
22
23
24 // variables
25 struct rwnode *symbols_forest[256] = { NULL }; // because of 8-bit hash - must be (at least partially) pre-defined so array will be zeroed!
26
27
28 // Dump symbol value and flags to dump file
29 static void dump_one_symbol(struct rwnode *node, FILE *fd)
30 {
31 struct symbol *symbol = node->body;
32
33 // if symbol is neither int nor float, skip
34 if (symbol->object.type != &type_number)
35 return;
36
37 // CAUTION: if more types are added, check for NULL before using type pointer!
38
39 // output name
40 if (config.warn_on_type_mismatch
41 && symbol->object.u.number.addr_refs == 1)
42 fprintf(fd, "!addr");
43 fprintf(fd, "\t%s", node->id_string);
44 switch (symbol->object.u.number.flags & NUMBER_FORCEBITS) {
45 case NUMBER_FORCES_16:
46 fprintf(fd, "+2\t= ");
47 break;
48 case NUMBER_FORCES_16 | NUMBER_FORCES_24:
49 /*FALLTHROUGH*/
50 case NUMBER_FORCES_24:
51 fprintf(fd, "+3\t= ");
52 break;
53 default:
54 fprintf(fd, "\t= ");
55 }
56 if (symbol->object.u.number.ntype == NUMTYPE_UNDEFINED)
57 fprintf(fd, " ?"); // TODO - maybe write "UNDEFINED" instead? then the file could at least be parsed without errors
58 else if (symbol->object.u.number.ntype == NUMTYPE_INT)
59 fprintf(fd, "$%x", (unsigned) symbol->object.u.number.val.intval);
60 else if (symbol->object.u.number.ntype == NUMTYPE_FLOAT)
61 fprintf(fd, "%.30f", symbol->object.u.number.val.fpval); //FIXME %g
62 else
63 BUG("IllegalNumberType4", symbol->object.u.number.ntype);
64 if (symbol->object.u.number.flags & NUMBER_EVER_UNDEFINED)
65 fprintf(fd, "\t; ?"); // TODO - write "forward" instead?
66 if (!symbol->has_been_read)
67 fprintf(fd, "\t; unused");
68 fprintf(fd, "\n");
69 }
70
71
72 // output symbols in VICE format (example: "al C:09ae .nmi1")
73 static void dump_vice_address(struct rwnode *node, FILE *fd)
74 {
75 struct symbol *symbol = node->body;
76
77 // dump address symbols even if they are not used
78 if ((symbol->object.type == &type_number)
79 && (symbol->object.u.number.ntype == NUMTYPE_INT)
80 && (symbol->object.u.number.addr_refs == 1))
81 fprintf(fd, "al C:%04x .%s\n", (unsigned) symbol->object.u.number.val.intval, node->id_string);
82 }
83 static void dump_vice_usednonaddress(struct rwnode *node, FILE *fd)
84 {
85 struct symbol *symbol = node->body;
86
87 // dump non-addresses that are used
88 if (symbol->has_been_read
89 && (symbol->object.type == &type_number)
90 && (symbol->object.u.number.ntype == NUMTYPE_INT)
91 && (symbol->object.u.number.addr_refs != 1))
92 fprintf(fd, "al C:%04x .%s\n", (unsigned) symbol->object.u.number.val.intval, node->id_string);
93 }
94 static void dump_vice_unusednonaddress(struct rwnode *node, FILE *fd)
95 {
96 struct symbol *symbol = node->body;
97
98 // dump non-addresses that are unused
99 if (!symbol->has_been_read
100 && (symbol->object.type == &type_number)
101 && (symbol->object.u.number.ntype == NUMTYPE_INT)
102 && (symbol->object.u.number.addr_refs != 1))
103 fprintf(fd, "al C:%04x .%s\n", (unsigned) symbol->object.u.number.val.intval, node->id_string);
104 }
105
106
107 // search for symbol. if it does not exist, create with NULL object (CAUTION!).
108 // the symbol name must be held in GlobalDynaBuf.
109 struct symbol *symbol_find(scope_t scope)
110 {
111 struct rwnode *node;
112 struct symbol *symbol;
113 boolean node_created;
114
115 node_created = tree_hard_scan(&node, symbols_forest, scope, TRUE);
116 // if node has just been created, create symbol as well
117 if (node_created) {
118 // create new symbol structure
119 symbol = safe_malloc(sizeof(*symbol));
120 node->body = symbol;
121 // finish empty symbol item
122 symbol->object.type = NULL; // no object yet (CAUTION!)
123 symbol->has_been_read = FALSE;
124 symbol->has_been_reported = FALSE;
125 symbol->pseudopc = NULL;
126 // we must set "definition" fields to dummy data, because the object
127 // has been created, but not necessarily set to a defined value:
128 symbol->definition.plat_filename = NULL;
129 symbol->definition.line_number = 0;
130 symbol->pass_number = pass.number;
131 } else {
132 symbol = node->body;
133 }
134 return symbol; // now symbol->object.type can be tested to see if this was freshly created.
135 // CAUTION: this only works if caller always sets a type pointer after checking! if NULL is kept, the struct still looks new later on...
136 }
137
138
139 // assign object to symbol. the function acts upon the symbol's flag bits and
140 // produces an error if needed.
141 // using "power" bits, caller can state which changes are ok.
142 // called by:
143 // implicit label definitions (including anons, backward anons have POWER_CHANGE_VALUE)
144 // explicit symbol assignments
145 // explicit symbol assignments via "!set" (has all powers)
146 // loop counter var init via "!for" (has POWER_CHANGE_VALUE and POWER_CHANGE_NUMTYPE)
147 // CAUTION: actual incrementing of counter is then done directly without calls here!
148 void symbol_set_object(struct symbol *symbol, struct object *new_value, bits powers)
149 {
150 boolean redefined;
151
152 if (symbol->object.type == NULL) {
153 // symbol has no object assigned to it yet
154 symbol->object = *new_value; // copy whole struct including type
155 // as long as the symbol has not been read, the force bits can
156 // be changed, so the caller still has a chance to do that.
157 } else {
158 // symbol already has an object
159
160 // compare types
161 // if too different, needs power (or complains)
162 if (symbol->object.type != new_value->type) {
163 if (!(powers & POWER_CHANGE_OBJTYPE)) {
164 throw_redef_error(exception_symbol_defined, &symbol->definition, "Previous definition.");
165 /* FIXME - this goes wrong if a forward reference is assumed to be an int and
166 later defined to be a string! the call above will not show the previous
167 definition, because there isn't one.
168 improve error output in these cases!
169 */
170 }
171 // CAUTION: if line above triggers, we still go ahead and change type!
172 // this is to keep "!for" working, where the counter var is accessed.
173 symbol->object = *new_value; // copy whole struct including type
174 // clear flag so caller can adjust force bits:
175 symbol->has_been_read = FALSE; // it's basically a new symbol now
176 } else {
177 // symbol and new value have compatible types, so call handler:
178 redefined = symbol->object.type->assign(&symbol->object, new_value, !!(powers & POWER_CHANGE_VALUE));
179 if (redefined) {
180 // do we accept re-definitions without "!set"?
181 if (config.dialect >= V0_98__PATHS_AND_SYMBOLCHANGE) {
182 // since version 0.98 new passes can assign new values:
183 if (symbol->pass_number != pass.number) {
184 ++pass.counters.symbolchanges;
185 } else {
186 throw_redef_error(exception_symbol_defined, &symbol->definition, "Previous definition.");
187 }
188 } else {
189 // older versions complained about _all_ re-definitions:
190 throw_redef_error(exception_symbol_defined, &symbol->definition, "Previous definition.");
191 }
192 }
193 }
194 }
195 // if symbol is an address, give it a pseudopc context:
196 if ((symbol->object.type == &type_number)
197 && (symbol->object.u.number.addr_refs == 1)) {
198 symbol->pseudopc = pseudopc_get_context();
199 }
200 // remember current location and pass number for "symbol twice" errors in future:
201 input_get_location(&symbol->definition);
202 symbol->pass_number = pass.number;
203 }
204
205
206 // set force bit of symbol. trying to change to a different one will raise error.
207 void symbol_set_force_bit(struct symbol *symbol, bits force_bit)
208 {
209 if (!force_bit)
210 BUG("ForceBitZero", 0);
211 if (symbol->object.type == NULL)
212 BUG("NullTypeObject", 0);
213
214 if (symbol->object.type != &type_number) {
215 throw_error("Force bits can only be given to numbers.");
216 return;
217 }
218
219 // if change is ok, change
220 if (!symbol->has_been_read) {
221 symbol->object.u.number.flags &= ~NUMBER_FORCEBITS;
222 symbol->object.u.number.flags |= force_bit;
223 return; // and be done with it
224 }
225
226 // it's too late to change, so check if the wanted bit is actually different
227 if ((symbol->object.u.number.flags & NUMBER_FORCEBITS) != force_bit)
228 throw_error("Too late for postfix.");
229 }
230
231
232 // create and return symbol for "-D" command line switch (with NULL type object, CAUTION!).
233 // name must be held in GlobalDynaBuf
234 extern struct symbol *symbol_for_cli_def(void)
235 {
236 struct symbol *symbol;
237
238 symbol = symbol_find(SCOPE_GLOBAL);
239 symbol->definition.plat_filename = "\"-D SYMBOL=VALUE\"";
240 symbol->definition.line_number = 1;
241 return symbol;
242 }
243 // set symbol to integer value, no questions asked (for "-D" switch)
244 // FIXME - remove and call int_create_byte instead?
245 void symbol_define_int(struct symbol *symbol, intval_t value)
246 {
247 symbol->object.type = &type_number;
248 symbol->object.u.number.ntype = NUMTYPE_INT;
249 symbol->object.u.number.flags = 0;
250 symbol->object.u.number.val.intval = value;
251 symbol->object.u.number.addr_refs = 0;
252 }
253 // set symbol to string value, no questions asked (for "-D" switch)
254 // string value must be held in GlobalDynaBuf
255 void symbol_define_string(struct symbol *symbol)
256 {
257 string_prepare_string(&symbol->object, GlobalDynaBuf->size);
258 memcpy(symbol->object.u.string->payload, GLOBALDYNABUF_CURRENT, GlobalDynaBuf->size);
259 }
260
261
262 // dump global symbols to file
263 void symbols_list(FILE *fd)
264 {
265 tree_dump_forest(symbols_forest, SCOPE_GLOBAL, dump_one_symbol, fd);
266 }
267
268
269 void symbols_vicelabels(FILE *fd)
270 {
271 // FIXME - if type checking is enabled, maybe only output addresses?
272 // the order of dumped labels is important because VICE will prefer later defined labels
273 // dump unused labels
274 tree_dump_forest(symbols_forest, SCOPE_GLOBAL, dump_vice_unusednonaddress, fd);
275 fputc('\n', fd);
276 // dump other used labels
277 tree_dump_forest(symbols_forest, SCOPE_GLOBAL, dump_vice_usednonaddress, fd);
278 fputc('\n', fd);
279 // dump address symbols
280 tree_dump_forest(symbols_forest, SCOPE_GLOBAL, dump_vice_address, fd);
281 // TODO - add trace points and watch points with load/store/exec args!
282 }
283
284
285 // fix name of anonymous forward label (held in DynaBuf, NOT TERMINATED!) so it
286 // references the *next* anonymous forward label definition. The tricky bit is,
287 // each name length would need its own counter. But hey, ACME's real quick in
288 // finding symbols, so I'll just abuse the symbol system to store those counters.
289 // example:
290 // forward anon name is "+++"
291 // we look up that symbol's value in the current local scope -> $12
292 // we attach hex digits to name -> "+++21"
293 // that's the name of the symbol that _actually_ contains the address
294 // caller sets "increment" to TRUE for writing, FALSE for reading
295 void symbol_fix_forward_anon_name(boolean increment)
296 {
297 struct symbol *counter_symbol;
298 unsigned long number;
299
300 // terminate name, find "counter" symbol and read value
301 dynabuf_append(GlobalDynaBuf, '\0');
302 counter_symbol = symbol_find(section_now->local_scope);
303 if (counter_symbol->object.type == NULL) {
304 // finish freshly created symbol item
305 counter_symbol->object.type = &type_number;
306 counter_symbol->object.u.number.ntype = NUMTYPE_INT;
307 counter_symbol->object.u.number.flags = 0;
308 counter_symbol->object.u.number.addr_refs = 0;
309 counter_symbol->object.u.number.val.intval = 0;
310 } else if (counter_symbol->object.type != &type_number) {
311 // sanity check: it must be a number!
312 BUG("ForwardAnonCounterNotInt", 0);
313 }
314 // make sure it gets reset to zero in each new pass
315 if (counter_symbol->pass_number != pass.number) {
316 counter_symbol->pass_number = pass.number;
317 counter_symbol->object.u.number.val.intval = 0;
318 }
319 // now use value
320 number = (unsigned long) counter_symbol->object.u.number.val.intval;
321 if (increment)
322 counter_symbol->object.u.number.val.intval++;
323 // now append to the name to make it unique
324 GlobalDynaBuf->size--; // forget terminator, we want to append
325 do {
326 DYNABUF_APPEND(GlobalDynaBuf, 'a' + (number & 15));
327 number >>= 4;
328 } while (number);
329 dynabuf_append(GlobalDynaBuf, '\0');
330 }