]> Shamusworld >> Repos - rmac/blob - symbol.c
Fix for bug #174 - when a macro contains an error, actually print the filename it...
[rmac] / symbol.c
1 //
2 // RMAC - Renamed Macro Assembler for all Atari computers
3 // SYMBOL.C - Symbol Handling
4 // Copyright (C) 199x Landon Dyer, 2011-2021 Reboot and Friends
5 // RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
6 // Source utilised with the kind permission of Landon Dyer
7 //
8
9 #include "symbol.h"
10 #include "dsp56k.h"
11 #include "error.h"
12 #include "listing.h"
13 #include "object.h"
14 #include "procln.h"
15
16
17 // Macros
18 #define NBUCKETS 256                            // Number of hash buckets (power of 2)
19
20 static SYM * symbolTable[NBUCKETS];     // User symbol-table header
21 int curenv;                                                     // Current enviroment number
22 static SYM * sorder;                            // * -> Symbols, in order of reference
23 static SYM * sordtail;                          // * -> Last symbol in sorder list
24 static SYM * sdecl;                                     // * -> Symbols, in order of declaration
25 static SYM * sdecltail;                         // * -> Last symbol in sdecl list
26 static uint32_t currentUID;                     // Symbol UID tracking (done by NewSymbol())
27 uint32_t firstglobal; // Index of the first global symbol in an ELF object.
28
29 // Tags for marking symbol spaces:
30 // a = absolute
31 // t = text
32 // d = data
33 // ! = "impossible!"
34 // b = BSS
35 static uint8_t tdb_text[8] = {
36    'a', 't', 'd', '!', 'b', SPACE, SPACE, SPACE
37 };
38
39 // Internal function prototypes
40 static uint16_t WriteLODSection(int, uint16_t);
41
42
43 //
44 // Initialize symbol table
45 //
46 void InitSymbolTable(void)
47 {
48         for(int i=0; i<NBUCKETS; i++)                   // Initialise symbol hash table
49                 symbolTable[i] = NULL;
50
51         curenv = 1;                                                             // Init local symbol enviroment
52         sorder = NULL;                                                  // Init symbol-reference list
53         sordtail = NULL;
54         sdecl = NULL;                                                   // Init symbol-decl list
55         sdecltail = NULL;
56         currentUID = 0;
57 }
58
59
60 //
61 // Hash the ASCII name and enviroment number
62 //
63 int HashSymbol(uint8_t * name, int envno)
64 {
65         int sum = envno, k = 0;
66
67         for(; *name; name++)
68         {
69                 if (k++ == 1)
70                         sum += *name << 2;
71                 else
72                         sum += *name;
73         }
74
75         return sum & (NBUCKETS - 1);
76 }
77
78
79 //
80 // Make a new symbol of type 'type' in enviroment 'envno'
81 //
82 SYM * NewSymbol(uint8_t * name, int type, int envno)
83 {
84         // Allocate the symbol
85         SYM * symbol = malloc(sizeof(SYM));
86
87         if (symbol == NULL)
88         {
89                 printf("NewSymbol: MALLOC ERROR (symbol=\"%s\")\n", name);
90                 return NULL;
91         }
92
93         // Fill-in the symbol
94         symbol->sname  = strdup(name);
95         symbol->stype  = (uint8_t)type;
96         symbol->senv   = (uint16_t)envno;
97         // We don't set this as DEFINED, as it could be a forward reference!
98         symbol->sattr  = 0;
99         // We don't set RISCSYM here as not every symbol first seen in a RISC
100         // section is a RISC symbol!
101         symbol->sattre = 0;
102         symbol->svalue = 0;
103         symbol->sorder = NULL;
104         symbol->uid    = currentUID++;
105
106         // Record filename the symbol is defined (for now only used by macro error reporting)
107         symbol->cfileno = cfileno;
108
109         // Install symbol in the symbol table
110         int hash = HashSymbol(name, envno);
111         symbol->snext = symbolTable[hash];
112         symbolTable[hash] = symbol;
113
114         // Append symbol to the symbol-order list
115         if (sorder == NULL)
116                 sorder = symbol;                                        // Add first symbol
117         else
118                 sordtail->sorder = symbol;                      // Or append to tail of list
119
120         sordtail = symbol;
121         return symbol;
122 }
123
124
125 //
126 // Look up the symbol name by its UID and return the pointer to the name.
127 // If it's not found, return NULL.
128 //
129 uint8_t * GetSymbolNameByUID(uint32_t uid)
130 {
131         //problem is with string lookup, that's why we're writing this
132         //so once this is written, we can put the uid in the token stream
133
134         // A much better approach to the symbol order list would be to make an
135         // array--that way you can do away with the UIDs and all the rest, and
136         // simply do an array lookup based on position. But meh, let's do this for
137         // now until we can rewrite things so they make sense.
138         SYM * symbol = sorder;
139
140         for(; symbol; symbol=symbol->sorder)
141         {
142                 if (symbol->uid == uid)
143                         return symbol->sname;
144         }
145
146         return NULL;
147 }
148
149
150 //
151 // Lookup the symbol 'name', of the specified type, with the specified
152 // enviroment level
153 //
154 SYM * lookup(uint8_t * name, int type, int envno)
155 {
156         SYM * symbol = symbolTable[HashSymbol(name, envno)];
157
158         // Do linear-search for symbol in bucket
159         while (symbol != NULL)
160         {
161                 if (symbol->stype == type                       // Type, envno and name must match
162                         && symbol->senv  == envno
163                         && *name == *symbol->sname              // Fast check for first character
164                         && !strcmp(name, symbol->sname))        // More expensive check
165                         break;
166
167                 symbol = symbol->snext;
168         }
169
170         // Return NULL or matching symbol
171         return symbol;
172 }
173
174
175 //
176 // Put symbol on "order-of-declaration" list of symbols
177 //
178 void AddToSymbolDeclarationList(SYM * symbol)
179 {
180         // Don't add if already on list, or it's an equated register/CC
181         if ((symbol->sattr & SDECLLIST)
182                 || (symbol->sattre & (EQUATEDREG | UNDEF_EQUR | EQUATEDCC | UNDEF_CC)))
183                 return;
184
185         // Mark as "on .sdecl list"
186         symbol->sattr |= SDECLLIST;
187
188         if (sdecl == NULL)
189                 sdecl = symbol;                         // First on decl-list
190         else
191                 sdecltail->sdecl = symbol;      // Add to end of list
192
193         // Fix up list's tail
194         symbol->sdecl = NULL;
195         sdecltail = symbol;
196 }
197
198
199 //
200 // Make all referenced, undefined symbols global
201 //
202 void ForceUndefinedSymbolsGlobal(void)
203 {
204         SYM * sy;
205
206         DEBUG printf("~ForceUndefinedSymbolsGlobal()\n");
207
208         // Scan through all symbols; if a symbol is REFERENCED but not DEFINED,
209         // then make it global.
210         for(sy=sorder; sy!=NULL; sy=sy->sorder)
211         {
212                 if (sy->stype == LABEL && sy->senv == 0
213                         && ((sy->sattr & (REFERENCED | DEFINED)) == REFERENCED))
214                         sy->sattr |= GLOBAL;
215         }
216 }
217
218
219 //
220 // Assign numbers to symbols that are to be exported or imported. The symbol
221 // number is put in 'senv'. Returns the number of symbols that will be in the
222 // symbol table.
223 //
224 // N.B.: This is usually called twice; first time with NULL parameters and the
225 //       second time with real ones. The first one is typically done to get a
226 //       count of the # of symbols in the symbol table, and the second is to
227 //       actually create it.
228 //
229 uint32_t sy_assign(uint8_t * buf, uint8_t *(* construct)())
230 {
231         uint16_t scount = 0;
232
233         // Done only on first pass...
234         if (buf == NULL)
235         {
236                 // Append all symbols not appearing on the .sdecl list to the end of
237                 // the .sdecl list
238                 for(SYM * sy=sorder; sy!=NULL; sy=sy->sorder)
239                         AddToSymbolDeclarationList(sy);
240         }
241
242         // Run through all symbols (now on the .sdecl list) and assign numbers to
243         // them. We also pick which symbols should be global or not here.
244         for(SYM * sy=sdecl; sy!=NULL; sy=sy->sdecl)
245         {
246                 // Export or import external references, and export COMMON blocks.
247                 if ((sy->stype == LABEL)
248                         && ((sy->sattr & (GLOBAL | DEFINED)) == (GLOBAL | DEFINED)
249                         || (sy->sattr & (GLOBAL | REFERENCED)) == (GLOBAL | REFERENCED))
250                         || (sy->sattr & COMMON))
251                 {
252                         sy->senv = scount++;
253
254                         if (buf != NULL)
255                                 buf = construct(buf, sy, 1);
256                 }
257                 // Export vanilla labels (but don't make them global). An exception is
258                 // made for equates, which are not exported unless they are referenced.
259                 else if (sy->stype == LABEL && lsym_flag
260                         && (sy->sattr & (DEFINED | REFERENCED)) != 0
261                         && (!as68_flag || *sy->sname != 'L'))
262                 {
263                         sy->senv = scount++;
264
265                         if (buf != NULL)
266                                 buf = construct(buf, sy, 0);
267                 }
268         }
269
270         return scount;
271 }
272
273
274 //
275 // Custom version of sy_assign for ELF .o files.
276 // The order that the symbols should be dumped is different.
277 // (globals must be explicitly at the end of the table)
278 //
279 // N.B.: It should be possible to merge this with sy_assign, as there's nothing
280 //       really ELF specific in here, other than the "globals go at the end of
281 //       the queue" thing, which doesn't break the others. :-P
282 uint32_t sy_assign_ELF(uint8_t * buf, uint8_t *(* construct)())
283 {
284         uint16_t scount = 0;
285
286 //      if (construct == (uint8_t *(*)())constr_elfsymtab)
287 //      if (buf == NULL)
288         {
289                 // Append all symbols not appearing on the .sdecl list to the end of
290                 // the .sdecl list
291                 for(SYM * sy=sorder; sy!=NULL; sy=sy->sorder)
292                         AddToSymbolDeclarationList(sy);
293         }
294
295         // Run through all symbols (now on the .sdecl list) and assign numbers to
296         // them. We also pick which symbols should be global or not here.
297         for(SYM * sy=sdecl; sy!=NULL; sy=sy->sdecl)
298         {
299                 // Export vanilla labels (but don't make them global). An exception is
300                 // made for equates, which are not exported unless they are referenced.
301                 if (sy->stype == LABEL && lsym_flag
302                         && (sy->sattr & (DEFINED | REFERENCED)) != 0
303                         && (*sy->sname != '.')
304                         && (sy->sattr & GLOBAL) == 0)
305                 {
306                         sy->senv = scount++;
307
308                         if (buf != NULL)
309                                 buf = construct(buf, sy, 0);
310                 }
311         }
312
313         firstglobal = scount;
314
315         // For ELF object mode run through all symbols in reference order
316         // and export all global-referenced labels. Not sure if this is
317         // required but it's here nonetheless
318
319         for(SYM * sy=sdecl; sy!=NULL; sy=sy->sdecl)
320         {
321                 if ((sy->stype == LABEL)
322                         && ((sy->sattr & (GLOBAL | DEFINED)) == (GLOBAL | DEFINED)
323                         || (sy->sattr & (GLOBAL | REFERENCED)) == (GLOBAL | REFERENCED))
324                         || (sy->sattr & COMMON))
325                 {
326                         sy->senv = scount++;
327
328                         if (buf != NULL)
329                                 buf = construct(buf, sy, 1);
330                 }
331                 else if ((sy->sattr == (GLOBAL | REFERENCED)) &&  (buf != NULL))
332                 {
333                         buf = construct(buf, sy, 0);
334                         scount++;
335                 }
336         }
337
338         return scount;
339 }
340
341
342 //
343 // Helper function for dsp_lod_symbols
344 //
345 static uint16_t WriteLODSection(int section, uint16_t symbolCount)
346 {
347         for(SYM * sy=sdecl; sy!=NULL; sy=sy->sdecl)
348         {
349                 // Export vanilla labels (but don't make them global). An exception is
350                 // made for equates, which are not exported unless they are referenced.
351                 if (sy->stype == LABEL && lsym_flag
352                         && (sy->sattr & (DEFINED | REFERENCED)) != 0
353                         && (*sy->sname != '.')
354                         && (sy->sattr & GLOBAL) == 0
355                         && (sy->sattr & (section)))
356                 {
357                         sy->senv = symbolCount++;
358                         D_printf("%-19s   I %.6" PRIX64 "\n", sy->sname, sy->svalue);
359                 }
360         }
361
362         return symbolCount;
363 }
364
365
366 //
367 // Dump LOD style symbols into the passed in buffer
368 //
369 void DumpLODSymbols(void)
370 {
371         D_printf("_SYMBOL P\n");
372         uint16_t count = WriteLODSection(M56001P, 0);
373
374         D_printf("_SYMBOL X\n");
375         count = WriteLODSection(M56001X, count);
376
377         D_printf("_SYMBOL Y\n");
378         count = WriteLODSection(M56001Y, count);
379
380         D_printf("_SYMBOL L\n");
381         count = WriteLODSection(M56001L, count);
382
383         // TODO: I've seen _SYMBOL N in there but no idea what symbols it needs...
384         //D_printf("_SYMBOL N\n");
385         //WriteLODSection(M56001?, count);
386 }
387
388
389 //
390 // Convert string to uppercase
391 //
392 void ToUppercase(uint8_t * s)
393 {
394         for(; *s; s++)
395         {
396                 if (*s >= 'a' && *s <= 'z')
397                         *s -= 0x20;
398         }
399 }
400
401
402 //
403 // Generate symbol table for listing file
404 //
405 int symtable(void)
406 {
407         int i;
408         int j;
409         SYM * q = NULL;
410         SYM * p;
411         SYM * r;
412         SYM * k;
413         SYM * colptr[4];
414         char ln[1024];
415         char ln1[1024];
416         char ln2[20];
417         char c, c1;
418         WORD w;
419         int ww;
420         int colhei = pagelen - 5;
421
422         // Allocate storage for list headers and partition all labels. Throw away
423         // macros and macro arguments.
424         SYM ** sy = (SYM **)malloc(128 * sizeof(SYM **));
425
426         for(i=0; i<128; i++)
427                 sy[i] = NULL;
428
429         for(i=0; i<NBUCKETS; i++)
430         {
431                 for(p=symbolTable[i]; p!=NULL; p=k)
432                 {
433                         k = p->snext;
434                         j = *p->sname;
435                         r = NULL;
436
437                         // Ignore non-labels
438                         if ((p->stype != LABEL) || (p->sattre & UNDEF_EQUR))
439                                 continue;
440
441                         for(q=sy[j]; q!=NULL; q=q->snext)
442                         {
443                                 if (strcmp(p->sname, q->sname) < 0)
444                                         break;
445
446                                 r = q;
447                         }
448
449                         if (r == NULL)
450                         {
451                                 // Insert at front of list
452                                 p->snext = sy[j];
453                                 sy[j] = p;
454                         }
455                         else
456                         {
457                                 // Insert in middle or append to list
458                                 p->snext = r->snext;
459                                 r->snext = p;
460                         }
461                 }
462         }
463
464         // Link all symbols onto one list again
465         p = NULL;
466
467         for(i=0; i<128; ++i)
468         {
469                 if ((r = sy[i]) != NULL)
470                 {
471                         if (p == NULL)
472                                 q = r;
473                         else
474                                 q->snext = r;
475
476                         while (q->snext != NULL)
477                                 q = q->snext;
478
479                         if (p == NULL)
480                                 p = r;
481                 }
482         }
483
484         eject();
485         strcpy(subttl, "Symbol Table");
486
487         while (p != NULL)
488         {
489                 for(i=0; i<4; i++)
490                 {
491                         colptr[i] = p;
492
493                         for(j=0; j<colhei; j++)
494                         {
495                                 if (p == NULL)
496                                         break;
497                                 else
498                                         p = p->snext;
499                         }
500                 }
501
502                 for(i=0; i<colhei; i++)
503                 {
504                         *ln = EOS;
505
506                         if (colptr[0] == NULL)
507                                 break;
508
509                         for(j=0; j<4; j++)
510                         {
511                                 if ((q = colptr[j]) == NULL)
512                                         break;
513
514                                 colptr[j] = q->snext;
515                                 w = q->sattr;
516                                 ww = q->sattre;
517                                 // Pick a tag:
518                                 // c    common
519                                 // x    external reference
520                                 // g    global (export)
521                                 // space        nothing special
522                                 c1 = SPACE;
523                                 c = SPACE;
524
525                                 if (w & COMMON)
526                                         c = 'c';
527                                 else if ((w & (DEFINED | GLOBAL)) == GLOBAL)
528                                         c = 'x';
529                                 else if (w & GLOBAL)
530                                         c = 'g';
531
532                                 c1 = tdb_text[w & TDB];
533
534                                 if (c == 'x')
535                                         strcpy(ln2, "external");
536                                 else
537                                 {
538                                         sprintf(ln2, "%016lX", q->svalue);
539                                         ToUppercase(ln2);
540                                 }
541
542                                 sprintf(ln1, "  %16s %s %c%c%c", q->sname, ln2, (ww & EQUATEDREG) ? 'e' : SPACE, c1, c);
543                                 strcat(ln, ln1);
544                         }
545
546                         ship_ln(ln);
547                 }
548
549                 eject();
550         }
551
552         return 0;
553 }
554