2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // SYMBOL.C - Symbol Handling
4 // Copyright (C) 199x Landon Dyer, 2011-2017 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
17 #define NBUCKETS 256 // Number of hash buckets (power of 2)
19 static SYM * symbolTable[NBUCKETS]; // User symbol-table header
20 int curenv; // Current enviroment number
21 static SYM * sorder; // * -> Symbols, in order of reference
22 static SYM * sordtail; // * -> Last symbol in sorder list
23 static SYM * sdecl; // * -> Symbols, in order of declaration
24 static SYM * sdecltail; // * -> Last symbol in sdecl list
25 static uint32_t currentUID; // Symbol UID tracking (done by NewSymbol())
26 uint32_t firstglobal; // Index of the first global symbol in an ELF object.
28 // Tags for marking symbol spaces:
34 static uint8_t tdb_text[8] = {
35 'a', 't', 'd', '!', 'b', SPACE, SPACE, SPACE
40 // Initialize symbol table
42 void InitSymbolTable(void)
44 for(int i=0; i<NBUCKETS; i++) // Initialise symbol hash table
45 symbolTable[i] = NULL;
47 curenv = 1; // Init local symbol enviroment
48 sorder = NULL; // Init symbol-reference list
50 sdecl = NULL; // Init symbol-decl list
57 // Hash the ASCII name and enviroment number
59 int HashSymbol(uint8_t * name, int envno)
61 int sum = envno, k = 0;
71 return sum & (NBUCKETS - 1);
76 // Make a new symbol of type 'type' in enviroment 'envno'
78 SYM * NewSymbol(uint8_t * name, int type, int envno)
80 // Allocate the symbol
81 SYM * symbol = malloc(sizeof(SYM));
85 printf("NewSymbol: MALLOC ERROR (symbol=\"%s\")\n", name);
90 symbol->sname = strdup(name);
91 symbol->stype = (uint8_t)type;
92 symbol->senv = (uint16_t)envno;
93 // We don't set this as DEFINED, as it could be a forward reference!
95 // We don't set RISCSYM here as not every symbol first seen in a RISC
96 // section is a RISC symbol!
99 symbol->sorder = NULL;
100 symbol->uid = currentUID++;
102 // Install symbol in the symbol table
103 int hash = HashSymbol(name, envno);
104 symbol->snext = symbolTable[hash];
105 symbolTable[hash] = symbol;
107 // Append symbol to the symbol-order list
109 sorder = symbol; // Add first symbol
111 sordtail->sorder = symbol; // Or append to tail of list
119 // Look up the symbol name by its UID and return the pointer to the name.
120 // If it's not found, return NULL.
122 uint8_t * GetSymbolNameByUID(uint32_t uid)
124 //problem is with string lookup, that's why we're writing this
125 //so once this is written, we can put the uid in the token stream
127 // A much better approach to the symbol order list would be to make an
128 // array--that way you can do away with the UIDs and all the rest, and
129 // simply do an array lookup based on position. But meh, let's do this for
130 // now until we can rewrite things so they make sense.
131 SYM * symbol = sorder;
133 for(; symbol; symbol=symbol->sorder)
135 if (symbol->uid == uid)
136 return symbol->sname;
144 // Lookup the symbol 'name', of the specified type, with the specified
147 SYM * lookup(uint8_t * name, int type, int envno)
149 SYM * symbol = symbolTable[HashSymbol(name, envno)];
151 // Do linear-search for symbol in bucket
152 while (symbol != NULL)
154 if (symbol->stype == type // Type, envno and name must match
155 && symbol->senv == envno
156 && *name == *symbol->sname // Fast check for first character
157 && !strcmp(name, symbol->sname)) // More expensive check
160 symbol = symbol->snext;
163 // Return NULL or matching symbol
169 // Put symbol on "order-of-declaration" list of symbols
171 void AddToSymbolDeclarationList(SYM * symbol)
173 // Don't add if already on list, or it's an equated register/CC
174 if ((symbol->sattr & SDECLLIST)
175 || (symbol->sattre & (EQUATEDREG | UNDEF_EQUR | EQUATEDCC | UNDEF_CC)))
178 // Mark as "on .sdecl list"
179 symbol->sattr |= SDECLLIST;
182 sdecl = symbol; // First on decl-list
184 sdecltail->sdecl = symbol; // Add to end of list
186 // Fix up list's tail
187 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))
213 // Assign numbers to symbols that are to be exported or imported. The symbol
214 // number is put in 'senv'. Returns the number of symbols that will be in the
217 // N.B.: This is usually called twice; first time with NULL parameters and the
218 // second time with real ones. The first one is typically done to get a
219 // count of the # of symbols in the symbol table, and the second is to
220 // actually create it.
222 uint32_t sy_assign(uint8_t * buf, uint8_t *(* construct)())
226 // Done only on first pass...
229 // Append all symbols not appearing on the .sdecl list to the end of
231 for(SYM * sy=sorder; sy!=NULL; sy=sy->sorder)
232 AddToSymbolDeclarationList(sy);
235 // Run through all symbols (now on the .sdecl list) and assign numbers to
236 // them. We also pick which symbols should be global or not here.
237 for(SYM * sy=sdecl; sy!=NULL; sy=sy->sdecl)
239 // Export or import external references, and export COMMON blocks.
240 if ((sy->stype == LABEL)
241 && ((sy->sattr & (GLOBAL | DEFINED)) == (GLOBAL | DEFINED)
242 || (sy->sattr & (GLOBAL | REFERENCED)) == (GLOBAL | REFERENCED))
243 || (sy->sattr & COMMON))
248 buf = construct(buf, sy, 1);
250 // Export vanilla labels (but don't make them global). An exception is
251 // made for equates, which are not exported unless they are referenced.
252 else if (sy->stype == LABEL && lsym_flag
253 && (sy->sattr & (DEFINED | REFERENCED)) != 0
254 && (!as68_flag || *sy->sname != 'L'))
259 buf = construct(buf, sy, 0);
268 // Custom version of sy_assign for ELF .o files.
269 // The order that the symbols should be dumped is different.
270 // (globals must be explicitly at the end of the table)
272 // N.B.: It should be possible to merge this with sy_assign, as there's nothing
273 // really ELF specific in here, other than the "globals go at the end of
274 // the queue" thing, which doesn't break the others. :-P
275 uint32_t sy_assign_ELF(uint8_t * buf, uint8_t *(* construct)())
279 // if (construct == (uint8_t *(*)())constr_elfsymtab)
282 // Append all symbols not appearing on the .sdecl list to the end of
284 for(SYM * sy=sorder; sy!=NULL; sy=sy->sorder)
285 AddToSymbolDeclarationList(sy);
288 // Run through all symbols (now on the .sdecl list) and assign numbers to
289 // them. We also pick which symbols should be global or not here.
290 for(SYM * sy=sdecl; sy!=NULL; sy=sy->sdecl)
292 // Export vanilla labels (but don't make them global). An exception is
293 // made for equates, which are not exported unless they are referenced.
294 if (sy->stype == LABEL && lsym_flag
295 && (sy->sattr & (DEFINED | REFERENCED)) != 0
296 && (*sy->sname != '.')
297 && (sy->sattr & GLOBAL) == 0)
302 buf = construct(buf, sy, 0);
306 firstglobal = scount;
308 // For ELF object mode run through all symbols in reference order
309 // and export all global-referenced labels. Not sure if this is
310 // required but it's here nonetheless
312 for(SYM * sy=sdecl; sy!=NULL; sy=sy->sdecl)
314 if ((sy->stype == LABEL)
315 && ((sy->sattr & (GLOBAL | DEFINED)) == (GLOBAL | DEFINED)
316 || (sy->sattr & (GLOBAL | REFERENCED)) == (GLOBAL | REFERENCED))
317 || (sy->sattr & COMMON))
322 buf = construct(buf, sy, 1);
324 else if ((sy->sattr == (GLOBAL | REFERENCED)) && (buf != NULL))
326 buf = construct(buf, sy, 0);
336 // Convert string to uppercase
338 void ToUppercase(uint8_t * s)
342 if (*s >= 'a' && *s <= 'z')
349 // Generate symbol table for listing file
366 int colhei = pagelen - 5;
368 // Allocate storage for list headers and partition all labels. Throw away
369 // macros and macro arguments.
370 SYM ** sy = (SYM **)malloc(128 * sizeof(SYM **));
375 for(i=0; i<NBUCKETS; i++)
377 for(p=symbolTable[i]; p!=NULL; p=k)
384 if ((p->stype != LABEL) || (p->sattre & UNDEF_EQUR))
387 for(q=sy[j]; q!=NULL; q=q->snext)
389 if (strcmp(p->sname, q->sname) < 0)
397 // Insert at front of list
403 // Insert in middle or append to list
410 // Link all symbols onto one list again
415 if ((r = sy[i]) != NULL)
422 while (q->snext != NULL)
431 strcpy(subttl, "Symbol Table");
439 for(j=0; j<colhei; j++)
448 for(i=0; i<colhei; i++)
452 if (colptr[0] == NULL)
457 if ((q = colptr[j]) == NULL)
460 colptr[j] = q->snext;
465 // x external reference
467 // space nothing special
473 else if ((w & (DEFINED | GLOBAL)) == GLOBAL)
478 c1 = tdb_text[w & TDB];
481 strcpy(ln2, "external");
484 sprintf(ln2, "%016lX", q->svalue);
488 sprintf(ln1, " %16s %s %c%c%c", q->sname, ln2, (ww & EQUATEDREG) ? 'e' : SPACE, c1, c);