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
16 #define NBUCKETS 256 // Number of hash buckets (power of 2)
18 static SYM * symbolTable[NBUCKETS]; // User symbol-table header
19 int curenv; // Current enviroment number
20 static SYM * sorder; // * -> Symbols, in order of reference
21 static SYM * sordtail; // * -> Last symbol in sorder list
22 static SYM * sdecl; // * -> Symbols, in order of declaration
23 static SYM * sdecltail; // * -> Last symbol in sdecl list
24 static uint32_t currentUID; // Symbol UID tracking (done by NewSymbol())
26 // Tags for marking symbol spaces
32 static char tdb_text[8] = {
33 'a', 't', 'd', '!', 'b', SPACE, SPACE, SPACE
38 // Initialize Symbol Table
40 void InitSymbolTable(void)
44 for(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 Print Name and Enviroment Number
59 int HashSymbol(char * name, int envno)
61 int sum, k = 0; // Hash calculation
63 for(sum=envno; *name; name++)
71 return sum & (NBUCKETS - 1);
76 // Make a new symbol of type `type' in enviroment `envno'
78 SYM * NewSymbol(char * 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 = (BYTE)type;
92 symbol->senv = (WORD)envno;
94 symbol->sattre = (rgpu || rdsp ? RISCSYM : 0);
96 symbol->sorder = NULL;
97 symbol->uid = currentUID++;
99 // Install symbol in symbol table
100 int hash = HashSymbol(name, envno);
101 symbol->snext = symbolTable[hash];
102 symbolTable[hash] = symbol;
104 // Append symbol to symbol-order list
106 sorder = symbol; // Add first symbol
108 sordtail->sorder = symbol; // Or append to tail of list
116 // Look up the symbol name by its UID and return the pointer to the name.
117 // If it's not found, return NULL.
119 char * GetSymbolNameByUID(uint32_t uid)
121 //problem is with string lookup, that's why we're writing this
122 //so once this is written, we can put the uid in the token stream
124 // A much better approach to the symbol order list would be to make an
125 // array--that way you can do away with the UIDs and all the rest, and
126 // simply do an array lookup based on position. But meh, let's do this for
127 // now until we can rewrite things so they make sense.
128 SYM * symbol = sorder;
130 for(; symbol; symbol=symbol->sorder)
132 if (symbol->uid == uid)
133 return symbol->sname;
141 // Lookup the symbol `name', of the specified type, with the specified
144 SYM * lookup(char * name, int type, int envno)
146 SYM * symbol = symbolTable[HashSymbol(name, envno)];
148 // Do linear-search for symbol in bucket
149 while (symbol != NULL)
151 if (symbol->stype == type // Type, envno and name must match
152 && symbol->senv == envno
153 && *name == *symbol->sname // Fast check for first character
154 && !strcmp(name, symbol->sname)) // More expensive check
157 symbol = symbol->snext;
160 // Return NULL or matching symbol
166 // Put symbol on "order-of-declaration" list of symbols
168 void sym_decl(SYM * symbol)
170 if (symbol->sattr & SDECLLIST)
171 return; // Already on list
173 symbol->sattr |= SDECLLIST; // Mark "already on list"
176 sdecl = symbol; // First on decl-list
178 sdecltail->sdecl = symbol; // Add to end of list
180 symbol->sdecl = NULL; // Fix up list's tail
186 // Make all referenced, undefined symbols global
192 DEBUG printf("~syg_fix()\n");
194 // Scan through all symbols;
195 // If a symbol is REFERENCED but not DEFINED, then make it global.
196 for(sy=sorder; sy!=NULL; sy=sy->sorder)
198 if (sy->stype == LABEL && sy->senv == 0
199 && ((sy->sattr & (REFERENCED | DEFINED)) == REFERENCED))
208 // Convert string to uppercase
210 int uc_string(char * s)
214 if (*s >= 'a' && *s <= 'z')
223 // Assign numbers to symbols that are to be exported or imported. The symbol
224 // number is put in `.senv'. Return the number of symbols that will be in the
227 int sy_assign(char * buf, char *(* constr)())
237 // Append all symbols not appearing on the .sdecl list to the end of
239 for(sy=sorder; sy!=NULL; sy=sy->sorder)
241 // Essentially the same as 'sym_decl()' above:
242 if (sy->sattr & SDECLLIST)
243 continue; // Already on list
245 sy->sattr |= SDECLLIST; // Mark "on the list"
248 sdecl = sy; // First on decl-list
250 sdecltail->sdecl = sy; // Add to end of list
252 sy->sdecl = NULL; // Fix up list's tail
257 // Run through all symbols (now on the .sdecl list) and assign numbers to
258 // them. We also pick which symbols should be global or not here.
259 for(sy=sdecl; sy!=NULL; sy=sy->sdecl)
261 if (sy->sattre & UNDEF_EQUR)
262 continue; // Don't want undefined on our list
264 if (sy->sattre & UNDEF_CC)
267 // Export or import external references, and export COMMON blocks.
268 if ((sy->stype == LABEL)
269 && ((sy->sattr & (GLOBAL | DEFINED)) == (GLOBAL | DEFINED)
270 || (sy->sattr & (GLOBAL | REFERENCED)) == (GLOBAL | REFERENCED))
271 || (sy->sattr & COMMON))
273 sy->senv = (WORD)scount++;
276 buf = (*constr)(buf, sy, 1);
278 // Export vanilla labels (but don't make them global). An exception is
279 // made for equates, which are not exported unless they are referenced.
280 else if (sy->stype == LABEL && lsym_flag
281 && (sy->sattr & (DEFINED | REFERENCED)) != 0
282 && (!as68_flag || *sy->sname != 'L'))
284 sy->senv = (WORD)scount++;
285 if (buf != NULL) buf = (*constr)(buf, sy, 0);
294 // Generate symbol table for listing file
315 colhei = pagelen - 5;
317 // Allocate storage for list headers and partition all labels.
318 // Throw away macros and macro arguments.
319 // sy = (SYM **)amem((LONG)(128 * sizeof(LONG)));
320 sy = (SYM **)malloc(128 * sizeof(LONG));
325 for(i=0; i<NBUCKETS; ++i)
327 for(p=symbolTable[i]; p!=NULL; p=k)
333 if (p->stype != LABEL)
334 continue; // Ignore non-labels
336 if (p->sattre & UNDEF_EQUR)
339 for(q=sy[j]; q!=NULL; q=q->snext)
341 if (strcmp(p->sname, q->sname) < 0)
348 { // Insert at front of list
353 { // Insert in middle or append to list
360 // Link all symbols onto one list again
365 if ((r = sy[i]) != NULL)
372 while (q->snext != NULL)
381 strcpy(subttl, "Symbol Table");
389 for(j=0; j<colhei; ++j)
398 for(i=0; i<colhei; ++i)
402 if (colptr[0] == NULL)
407 if ((q = colptr[j]) == NULL)
410 colptr[j] = q->snext;
415 // x external reference
417 // space nothing special
423 else if ((w & (DEFINED|GLOBAL)) == GLOBAL)
428 c1 = tdb_text[w & TDB];
431 strcpy(ln2, "external");
434 sprintf(ln2, "%08X", q->svalue);
438 sprintf(ln1, " %16s %s %c%c%c", q->sname, ln2, (ww & EQUATEDREG) ? 'e' : SPACE, c1, c);