2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // SYMBOL.C - Symbol Handling
4 // Copyright (C) 199x Landon Dyer, 2011-2012 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);
263 // For ELF object mode run through all symbols in reference order
264 // and export all global-referenced labels. Not sure if this is
265 // required but it's here nonetheless
266 /* why?? when you have sy_assign_ELF ???
267 if (obj_format == ELF)
269 for(sy=sdecl; sy!=NULL; sy=sy->sorder)
271 if ((sy->sattr == (GLOBAL | REFERENCED)) && (buf != NULL))
273 buf = (*construct)(buf, sy, 0);
284 // Custom version of sy_assign for ELF .o files.
285 // The order that the symbols should be dumped is different.
286 // (globals must be explicitly at the end of the table)
288 // N.B.: It should be possible to merge this with sy_assign, as there's nothing
289 // really ELF specific in here, other than the "globals go at the end of
290 // the queue" thing, which doesn't break the others. :-P
291 uint32_t sy_assign_ELF(uint8_t * buf, uint8_t *(* construct)())
295 // if (construct == (uint8_t *(*)())constr_elfsymtab)
298 // Append all symbols not appearing on the .sdecl list to the end of
300 for(SYM * sy=sorder; sy!=NULL; sy=sy->sorder)
301 AddToSymbolDeclarationList(sy);
304 // Run through all symbols (now on the .sdecl list) and assign numbers to
305 // them. We also pick which symbols should be global or not here.
306 for(SYM * sy=sdecl; sy!=NULL; sy=sy->sdecl)
308 // Export or import external references, and export COMMON blocks.
309 //if ((sy->stype == LABEL)
310 // && ((sy->sattr & (GLOBAL | DEFINED)) == (GLOBAL | DEFINED)
311 // || (sy->sattr & (GLOBAL | REFERENCED)) == (GLOBAL | REFERENCED))
312 // || (sy->sattr & COMMON))
314 // sy->senv = (WORD)scount++;
317 // buf = (*construct)(buf, sy, 1);
319 // Export vanilla labels (but don't make them global). An exception is
320 // made for equates, which are not exported unless they are referenced.
321 if (sy->stype == LABEL && lsym_flag
322 && (sy->sattr & (DEFINED | REFERENCED)) != 0
323 && (*sy->sname != '.')
324 && (sy->sattr & GLOBAL) == 0)
325 //if (sy->stype == 0)
327 // if ((sy->sattr & (DEFINED | REFERENCED)) != 0)
328 // if ((!as68_flag || *sy->sname != 'L'))
333 buf = construct(buf, sy, 0);
337 firstglobal = scount;
339 // For ELF object mode run through all symbols in reference order
340 // and export all global-referenced labels. Not sure if this is
341 // required but it's here nonetheless
343 //for(sy=sdecl; sy!=NULL; sy=sy->sorder)
344 for(SYM * sy=sdecl; sy!=NULL; sy=sy->sdecl)
346 if ((sy->stype == LABEL)
347 && ((sy->sattr & (GLOBAL | DEFINED)) == (GLOBAL | DEFINED)
348 || (sy->sattr & (GLOBAL | REFERENCED)) == (GLOBAL | REFERENCED))
349 || (sy->sattr & COMMON))
354 buf = construct(buf, sy, 1);
356 else if ((sy->sattr == (GLOBAL | REFERENCED)) && (buf != NULL))
358 buf = construct(buf, sy, 0);
368 // Convert string to uppercase
370 void ToUppercase(uint8_t * s)
374 if (*s >= 'a' && *s <= 'z')
381 // Generate symbol table for listing file
399 int colhei = pagelen - 5;
401 // Allocate storage for list headers and partition all labels. Throw away
402 // macros and macro arguments.
403 SYM ** sy = (SYM **)malloc(128 * sizeof(uint32_t));
408 for(i=0; i<NBUCKETS; i++)
410 for(p=symbolTable[i]; p!=NULL; p=k)
417 if ((p->stype != LABEL) || (p->sattre & UNDEF_EQUR))
420 for(q=sy[j]; q!=NULL; q=q->snext)
422 if (strcmp(p->sname, q->sname) < 0)
430 // Insert at front of list
436 // Insert in middle or append to list
443 // Link all symbols onto one list again
448 if ((r = sy[i]) != NULL)
455 while (q->snext != NULL)
464 strcpy(subttl, "Symbol Table");
472 for(j=0; j<colhei; j++)
481 for(i=0; i<colhei; i++)
485 if (colptr[0] == NULL)
490 if ((q = colptr[j]) == NULL)
493 colptr[j] = q->snext;
498 // x external reference
500 // space nothing special
506 else if ((w & (DEFINED | GLOBAL)) == GLOBAL)
511 c1 = tdb_text[w & TDB];
514 strcpy(ln2, "external");
517 sprintf(ln2, "%08X", q->svalue);
521 sprintf(ln1, " %16s %s %c%c%c", q->sname, ln2, (ww & EQUATEDREG) ? 'e' : SPACE, c1, c);