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
15 #define NBUCKETS 256 // Number of hash buckets (power of 2)
17 static SYM * symbolTable[NBUCKETS]; // User symbol-table header
18 int curenv; // Current enviroment number
19 static SYM * sorder; // * -> Symbols, in order of reference
20 static SYM * sordtail; // * -> Last symbol in sorder list
21 static SYM * sdecl; // * -> Symbols, in order of declaration
22 static SYM * sdecltail; // * -> Last symbol in sdecl list
23 static uint32_t currentUID; // Symbol UID tracking (done by NewSymbol())
25 // Tags for marking symbol spaces
31 static char tdb_text[8] = {
32 'a', 't', 'd', '!', 'b', SPACE, SPACE, SPACE
37 // Initialize Symbol Table
39 void InitSymbolTable(void)
43 for(i=0; i<NBUCKETS; i++) // Initialise symbol hash table
44 symbolTable[i] = NULL;
46 curenv = 1; // Init local symbol enviroment
47 sorder = NULL; // Init symbol-reference list
49 sdecl = NULL; // Init symbol-decl list
56 // Hash the Print Name and Enviroment Number
58 int HashSymbol(char * name, int envno)
60 int sum, k = 0; // Hash calculation
62 for(sum=envno; *name; name++)
70 return sum & (NBUCKETS - 1);
75 // Make a new symbol of type `type' in enviroment `envno'
77 SYM * NewSymbol(char * name, int type, int envno)
79 // Allocate the symbol
80 SYM * symbol = malloc(sizeof(SYM));
84 printf("NewSymbol: MALLOC ERROR (symbol=\"%s\")\n", name);
89 symbol->sname = strdup(name);
90 symbol->stype = (BYTE)type;
91 symbol->senv = (WORD)envno;
93 symbol->sattre = (rgpu || rdsp ? RISCSYM : 0);
95 symbol->sorder = NULL;
96 symbol->uid = currentUID++;
98 // Install symbol in symbol table
99 int hash = HashSymbol(name, envno);
100 symbol->snext = symbolTable[hash];
101 symbolTable[hash] = symbol;
103 // Append symbol to symbol-order list
105 sorder = symbol; // Add first symbol
107 sordtail->sorder = symbol; // Or append to tail of list
115 // Look up the symbol name by its UID and return the pointer to the name.
116 // If it's not found, return NULL.
118 char * GetSymbolNameByUID(uint32_t uid)
120 //problem is with string lookup, that's why we're writing this
121 //so once this is written, we can put the uid in the token stream
123 // A much better approach to the symbol order list would be to make an
124 // array--that way you can do away with the UIDs and all the rest, and
125 // simply do an array lookup based on position. But meh, let's do this for
126 // now until we can rewrite things so they make sense.
127 SYM * symbol = sorder;
129 for(; symbol; symbol=symbol->sorder)
131 if (symbol->uid == uid)
132 return symbol->sname;
140 // Lookup the symbol `name', of the specified type, with the specified
143 SYM * lookup(char * name, int type, int envno)
145 SYM * symbol = symbolTable[HashSymbol(name, envno)];
147 // Do linear-search for symbol in bucket
148 while (symbol != NULL)
150 if (symbol->stype == type // Type, envno and name must match
151 && symbol->senv == envno
152 && *name == *symbol->sname // Fast check for first character
153 && !strcmp(name, symbol->sname))
156 symbol = symbol->snext;
159 return symbol; // Return NULL or matching symbol
164 // Put symbol on "order-of-declaration" list of symbols
166 void sym_decl(SYM * symbol)
168 if (symbol->sattr & SDECLLIST)
169 return; // Already on list
171 symbol->sattr |= SDECLLIST; // Mark "already on list"
174 sdecl = symbol; // First on decl-list
176 sdecltail->sdecl = symbol; // Add to end of list
178 symbol->sdecl = NULL; // Fix up list's tail
184 // Make all referenced, undefined symbols global
190 DEBUG printf("~syg_fix()\n");
192 // Scan through all symbols;
193 // If a symbol is REFERENCED but not DEFINED, then make it global.
194 for(sy=sorder; sy!=NULL; sy=sy->sorder)
196 if (sy->stype == LABEL && sy->senv == 0
197 && ((sy->sattr & (REFERENCED | DEFINED)) == REFERENCED))
206 // Convert string to uppercase
208 int uc_string(char * s)
212 if (*s >= 'a' && *s <= 'z')
221 // Assign numbers to symbols that are to be exported or imported. The symbol
222 // number is put in `.senv'. Return the number of symbols that will be in the
225 int sy_assign(char * buf, char *(* constr)())
235 // Append all symbols not appearing on the .sdecl list to the end of
237 for(sy=sorder; sy!=NULL; sy=sy->sorder)
239 // Essentially the same as 'sym_decl()' above:
240 if (sy->sattr & SDECLLIST)
241 continue; // Already on list
243 sy->sattr |= SDECLLIST; // Mark "on the list"
246 sdecl = sy; // First on decl-list
248 sdecltail->sdecl = sy; // Add to end of list
250 sy->sdecl = NULL; // Fix up list's tail
255 // Run through all symbols (now on the .sdecl list) and assign numbers to
256 // them. We also pick which symbols should be global or not here.
257 for(sy=sdecl; sy!=NULL; sy=sy->sdecl)
259 if (sy->sattre & UNDEF_EQUR)
260 continue; // Don't want undefined on our list
262 if (sy->sattre & UNDEF_CC)
265 // Export or import external references, and export COMMON blocks.
266 if ((sy->stype == LABEL)
267 && ((sy->sattr & (GLOBAL | DEFINED)) == (GLOBAL | DEFINED)
268 || (sy->sattr & (GLOBAL | REFERENCED)) == (GLOBAL | REFERENCED))
269 || (sy->sattr & COMMON))
271 sy->senv = (WORD)scount++;
274 buf = (*constr)(buf, sy, 1);
276 // Export vanilla labels (but don't make them global). An exception is
277 // made for equates, which are not exported unless they are referenced.
278 else if (sy->stype == LABEL && lsym_flag
279 && (sy->sattr & (DEFINED | REFERENCED)) != 0
280 && (!as68_flag || *sy->sname != 'L'))
282 sy->senv = (WORD)scount++;
283 if (buf != NULL) buf = (*constr)(buf, sy, 0);
292 // Generate symbol table for listing file
313 colhei = pagelen - 5;
315 // Allocate storage for list headers and partition all labels.
316 // Throw away macros and macro arguments.
317 // sy = (SYM **)amem((LONG)(128 * sizeof(LONG)));
318 sy = (SYM **)malloc(128 * sizeof(LONG));
323 for(i=0; i<NBUCKETS; ++i)
325 for(p=symbolTable[i]; p!=NULL; p=k)
331 if (p->stype != LABEL)
332 continue; // Ignore non-labels
334 if (p->sattre & UNDEF_EQUR)
337 for(q=sy[j]; q!=NULL; q=q->snext)
339 if (strcmp(p->sname, q->sname) < 0)
346 { // Insert at front of list
351 { // Insert in middle or append to list
358 // Link all symbols onto one list again
363 if ((r = sy[i]) != NULL)
370 while (q->snext != NULL)
379 strcpy(subttl, "Symbol Table");
387 for(j=0; j<colhei; ++j)
396 for(i=0; i<colhei; ++i)
400 if (colptr[0] == NULL)
405 if ((q = colptr[j]) == NULL)
408 colptr[j] = q->snext;
413 // x external reference
415 // space nothing special
421 else if ((w & (DEFINED|GLOBAL)) == GLOBAL)
426 c1 = tdb_text[w & TDB];
429 strcpy(ln2, "external");
432 sprintf(ln2, "%08X", q->svalue);
436 sprintf(ln1, " %16s %s %c%c%c", q->sname, ln2, (ww & EQUATEDREG) ? 'e' : SPACE, c1, c);