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