2 // RMAC - Renamed Macro Assembler for all Atari computers
3 // SYMBOL.C - Symbol Handling
4 // Copyright (C) 199x Landon Dyer, 2011-2022 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
18 #define NBUCKETS 256 // Number of hash buckets (power of 2)
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.
29 // Tags for marking symbol spaces:
35 static uint8_t tdb_text[8] = {
36 'a', 't', 'd', '!', 'b', SPACE, SPACE, SPACE
39 // Internal function prototypes
40 static uint16_t WriteLODSection(int, uint16_t);
43 // Initialize symbol table
45 void InitSymbolTable(void)
47 for(int i=0; i<NBUCKETS; i++) // Initialise symbol hash table
48 symbolTable[i] = NULL;
50 curenv = 1; // Init local symbol enviroment
51 sorder = NULL; // Init symbol-reference list
53 sdecl = NULL; // Init symbol-decl list
59 // Hash the ASCII name and enviroment number
61 int HashSymbol(uint8_t * name, int envno)
63 int sum = envno, k = 0;
73 return sum & (NBUCKETS - 1);
77 // Make a new symbol of type 'type' in enviroment 'envno'
79 SYM * NewSymbol(uint8_t * name, int type, int envno)
81 // Allocate the symbol
82 SYM * symbol = malloc(sizeof(SYM));
86 printf("NewSymbol: MALLOC ERROR (symbol=\"%s\")\n", name);
91 symbol->sname = strdup(name);
92 symbol->stype = (uint8_t)type;
93 symbol->senv = (uint16_t)envno;
94 // We don't set this as DEFINED, as it could be a forward reference!
96 // We don't set RISCSYM here as not every symbol first seen in a RISC
97 // section is a RISC symbol!
100 symbol->sorder = NULL;
101 symbol->uid = currentUID++;
103 // Record filename the symbol is defined (for now only used by macro error reporting)
104 symbol->cfileno = cfileno;
106 // Install symbol in the symbol table
107 int hash = HashSymbol(name, envno);
108 symbol->snext = symbolTable[hash];
109 symbolTable[hash] = symbol;
111 // Append symbol to the symbol-order list
113 sorder = symbol; // Add first symbol
115 sordtail->sorder = symbol; // Or append to tail of list
122 // Look up the symbol name by its UID and return the pointer to the name.
123 // If it's not found, return NULL.
125 uint8_t * GetSymbolNameByUID(uint32_t uid)
127 //problem is with string lookup, that's why we're writing this
128 //so once this is written, we can put the uid in the token stream
130 // A much better approach to the symbol order list would be to make an
131 // array--that way you can do away with the UIDs and all the rest, and
132 // simply do an array lookup based on position. But meh, let's do this for
133 // now until we can rewrite things so they make sense.
134 SYM * symbol = sorder;
136 for(; symbol; symbol=symbol->sorder)
138 if (symbol->uid == uid)
139 return symbol->sname;
146 // Lookup the symbol 'name', of the specified type, with the specified
149 SYM * lookup(uint8_t * name, int type, int envno)
151 SYM * symbol = symbolTable[HashSymbol(name, envno)];
153 // Do linear-search for symbol in bucket
154 while (symbol != NULL)
156 if (symbol->stype == type // Type, envno and name must match
157 && symbol->senv == envno
158 && *name == *symbol->sname // Fast check for first character
159 && !strcmp(name, symbol->sname)) // More expensive check
162 symbol = symbol->snext;
165 // Return NULL or matching symbol
170 // Put symbol on "order-of-declaration" list of symbols
172 void AddToSymbolDeclarationList(SYM * symbol)
174 // Don't add if already on list, or it's an equated register/CC
175 if ((symbol->sattr & SDECLLIST)
176 || (symbol->sattre & (EQUATEDREG | UNDEF_EQUR | EQUATEDCC | UNDEF_CC)))
179 // Mark as "on .sdecl list"
180 symbol->sattr |= SDECLLIST;
183 sdecl = symbol; // First on decl-list
185 sdecltail->sdecl = symbol; // Add to end of list
187 // Fix up list's tail
188 symbol->sdecl = NULL;
193 // Make all referenced, undefined symbols global
195 void ForceUndefinedSymbolsGlobal(void)
199 DEBUG printf("~ForceUndefinedSymbolsGlobal()\n");
201 // Scan through all symbols; if a symbol is REFERENCED but not DEFINED,
202 // then make it global.
203 for(sy=sorder; sy!=NULL; sy=sy->sorder)
205 if (sy->stype == LABEL && sy->senv == 0
206 && ((sy->sattr & (REFERENCED | DEFINED)) == REFERENCED))
212 // Assign numbers to symbols that are to be exported or imported. The symbol
213 // number is put in 'senv'. Returns the number of symbols that will be in the
216 // N.B.: This is usually called twice; first time with NULL parameters and the
217 // second time with real ones. The first one is typically done to get a
218 // count of the # of symbols in the symbol table, and the second is to
219 // actually create it.
221 uint32_t AssignSymbolNos(uint8_t * buf, uint8_t *(* construct)())
225 // Done only on first pass...
228 // Append all symbols not appearing on the .sdecl list to the end of
230 for(SYM * sy=sorder; sy!=NULL; sy=sy->sorder)
231 AddToSymbolDeclarationList(sy);
234 // Run through all symbols (now on the .sdecl list) and assign numbers to
235 // them. We also pick which symbols should be global or not here.
236 for(SYM * sy=sdecl; sy!=NULL; sy=sy->sdecl)
239 if (sy->stype != LABEL)
242 // Nuke equated register/CC symbols from orbit:
243 if (sy->sattre & (EQUATEDREG | UNDEF_EQUR | EQUATEDCC | UNDEF_CC))
246 // Export or import external references, and export COMMON blocks.
247 // N.B.: This says to mark the symbol as global if either 1) the symbol
248 // is global AND the symbol is defined OR referenced, or 2) this
249 // symbol is a common symbol.
250 if (((sy->sattr & GLOBAL) && (sy->sattr & (DEFINED | REFERENCED)))
251 || (sy->sattr & COMMON))
256 buf = construct(buf, sy, 1);
258 // Export vanilla labels (but don't make them global). An exception is
259 // made for equates, which are not exported unless they are referenced.
260 // ^^^ The above just might be bullshit. ^^^
261 // N.B.: This says if the symbol is either defined OR referenced (but
262 // because of the above we know it *won't* be GLOBAL). And
263 // lsym_flag is always set true in Process() in rmac.c.
264 else if (lsym_flag && (sy->sattr & (DEFINED | REFERENCED)))
269 buf = construct(buf, sy, 0);
277 // Custom version of AssignSymbolNos for ELF .o files.
278 // The order that the symbols should be dumped is different.
279 // (globals must be explicitly at the end of the table)
281 // N.B.: It should be possible to merge this with AssignSymbolNos, as there's
282 // nothing really ELF specific in here, other than the "globals go at the
283 // end of the queue" thing, which doesn't break the others. :-P
284 uint32_t AssignSymbolNosELF(uint8_t * buf, uint8_t *(* construct)())
288 // Append all symbols not appearing on the .sdecl list to the end of
290 for(SYM * sy=sorder; sy!=NULL; sy=sy->sorder)
291 AddToSymbolDeclarationList(sy);
293 // Run through all symbols (now on the .sdecl list) and assign numbers to
294 // them. We also pick which symbols should be global or not here.
295 for(SYM * sy=sdecl; sy!=NULL; sy=sy->sdecl)
297 // Export vanilla labels (but don't make them global). An exception is
298 // made for equates, which are not exported unless they are referenced.
299 if (sy->stype == LABEL && lsym_flag
300 && (sy->sattr & (DEFINED | REFERENCED)) != 0
301 && (*sy->sname != '.')
302 && (sy->sattr & GLOBAL) == 0
303 && (sy->sattre & (EQUATEDREG | UNDEF_EQUR | EQUATEDCC | UNDEF_CC)) == 0)
308 buf = construct(buf, sy, 0);
312 firstglobal = scount;
314 // For ELF object mode run through all symbols in reference order
315 // and export all global-referenced labels. Not sure if this is
316 // required but it's here nonetheless
318 for(SYM * sy=sdecl; sy!=NULL; sy=sy->sdecl)
320 if ((sy->stype == LABEL)
321 && (sy->sattre & (EQUATEDREG | UNDEF_EQUR | EQUATEDCC | UNDEF_CC)) == 0
322 && ((sy->sattr & (GLOBAL | DEFINED)) == (GLOBAL | DEFINED)
323 || (sy->sattr & (GLOBAL | REFERENCED)) == (GLOBAL | REFERENCED))
324 || (sy->sattr & COMMON))
329 buf = construct(buf, sy, 1);
331 else if ((sy->sattr == (GLOBAL | REFERENCED)) && (buf != NULL) && (sy->sattre & (EQUATEDREG | UNDEF_EQUR | EQUATEDCC | UNDEF_CC)) == 0)
333 buf = construct(buf, sy, 0); // <-- this creates a NON-global symbol...
342 // Helper function for dsp_lod_symbols
344 static uint16_t WriteLODSection(int section, uint16_t symbolCount)
346 for(SYM * sy=sdecl; sy!=NULL; sy=sy->sdecl)
348 // Export vanilla labels (but don't make them global). An exception is
349 // made for equates, which are not exported unless they are referenced.
350 if (sy->stype == LABEL && lsym_flag
351 && (sy->sattr & (DEFINED | REFERENCED)) != 0
352 && (*sy->sname != '.')
353 && (sy->sattr & GLOBAL) == 0
354 && (sy->sattr & (section)))
356 sy->senv = symbolCount++;
357 D_printf("%-19s I %.6" PRIX64 "\n", sy->sname, sy->svalue);
365 // Dump LOD style symbols into the passed in buffer
367 void DumpLODSymbols(void)
369 D_printf("_SYMBOL P\n");
370 uint16_t count = WriteLODSection(M56001P, 0);
372 D_printf("_SYMBOL X\n");
373 count = WriteLODSection(M56001X, count);
375 D_printf("_SYMBOL Y\n");
376 count = WriteLODSection(M56001Y, count);
378 D_printf("_SYMBOL L\n");
379 count = WriteLODSection(M56001L, count);
381 // TODO: I've seen _SYMBOL N in there but no idea what symbols it needs...
382 //D_printf("_SYMBOL N\n");
383 //WriteLODSection(M56001?, count);
387 // Convert string to uppercase
389 void ToUppercase(uint8_t * s)
393 if (*s >= 'a' && *s <= 'z')
399 // Generate symbol table for listing file
416 int colhei = pagelen - 5;
418 // Allocate storage for list headers and partition all labels. Throw away
419 // macros and macro arguments.
420 SYM ** sy = (SYM **)malloc(128 * sizeof(SYM **));
425 for(i=0; i<NBUCKETS; i++)
427 for(p=symbolTable[i]; p!=NULL; p=k)
434 if ((p->stype != LABEL) || (p->sattre & UNDEF_EQUR))
437 for(q=sy[j]; q!=NULL; q=q->snext)
439 if (strcmp(p->sname, q->sname) < 0)
447 // Insert at front of list
453 // Insert in middle or append to list
460 // Link all symbols onto one list again
465 if ((r = sy[i]) != NULL)
472 while (q->snext != NULL)
481 strcpy(subttl, "Symbol Table");
489 for(j=0; j<colhei; j++)
498 for(i=0; i<colhei; i++)
502 if (colptr[0] == NULL)
507 if ((q = colptr[j]) == NULL)
510 colptr[j] = q->snext;
515 // x external reference
517 // space nothing special
523 else if ((w & (DEFINED | GLOBAL)) == GLOBAL)
528 c1 = tdb_text[w & TDB];
531 strcpy(ln2, "external");
534 sprintf(ln2, "%016lX", q->svalue);
538 sprintf(ln1, " %16s %s %c%c%c", q->sname, ln2, (ww & EQUATEDREG) ? 'e' : SPACE, c1, c);