Fixed subtle bug in expr().
[rmac] / symbol.c
1 //
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
7 //
8
9 #include "symbol.h"
10 #include "listing.h"
11 #include "procln.h"
12 #include "error.h"
13
14
15 // Macros
16 #define NBUCKETS 256                                    // Number of hash buckets (power of 2)
17
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())
25
26 // Tags for marking symbol spaces
27 // a = absolute
28 // t = text
29 // d = data
30 // ! = "impossible!"
31 // b = BSS
32 static char tdb_text[8] = {
33    'a', 't', 'd', '!', 'b', SPACE, SPACE, SPACE
34 };
35
36
37 //
38 // Initialize Symbol Table
39 //
40 void InitSymbolTable(void)
41 {
42         int i;                                                                  // Iterator
43
44         for(i=0; i<NBUCKETS; i++)                               // Initialise symbol hash table
45                 symbolTable[i] = NULL;
46
47         curenv = 1;                                                             // Init local symbol enviroment
48         sorder = NULL;                                                  // Init symbol-reference list
49         sordtail = NULL;
50         sdecl = NULL;                                                   // Init symbol-decl list
51         sdecltail = NULL;
52         currentUID = 0;
53 }
54
55
56 //
57 // Hash the Print Name and Enviroment Number
58 //
59 int HashSymbol(char * name, int envno)
60 {
61         int sum, k = 0;                                                 // Hash calculation
62
63         for(sum=envno; *name; name++)
64         {
65                 if (k++ == 1)
66                         sum += *name << 2;
67                 else
68                         sum += *name;
69         }
70
71         return sum & (NBUCKETS - 1);
72 }
73
74
75 //
76 // Make a new symbol of type `type' in enviroment `envno'
77 //
78 SYM * NewSymbol(char * name, int type, int envno)
79 {
80         // Allocate the symbol
81         SYM * symbol = malloc(sizeof(SYM));
82
83         if (symbol == NULL)
84         {
85                 printf("NewSymbol: MALLOC ERROR (symbol=\"%s\")\n", name);
86                 return NULL;
87         }
88
89         // Fill-in the symbol
90         symbol->sname  = strdup(name);
91         symbol->stype  = (BYTE)type;
92         symbol->senv   = (WORD)envno;
93         symbol->sattr  = 0;
94         symbol->sattre = (rgpu || rdsp ? RISCSYM : 0);
95         symbol->svalue = 0;
96         symbol->sorder = NULL;
97         symbol->uid    = currentUID++;
98
99         // Install symbol in symbol table
100         int hash = HashSymbol(name, envno);
101         symbol->snext = symbolTable[hash];
102         symbolTable[hash] = symbol;
103
104         // Append symbol to symbol-order list
105         if (sorder == NULL)
106                 sorder = symbol;                                        // Add first symbol 
107         else
108                 sordtail->sorder = symbol;                      // Or append to tail of list
109
110         sordtail = symbol;
111         return symbol;
112 }
113
114
115 //
116 // Look up the symbol name by its UID and return the pointer to the name.
117 // If it's not found, return NULL.
118 //
119 char * GetSymbolNameByUID(uint32_t uid)
120 {
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
123
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;
129
130         for(; symbol; symbol=symbol->sorder)
131         {
132                 if (symbol->uid == uid)
133                         return symbol->sname;
134         }
135
136         return NULL;
137 }
138
139
140 //
141 // Lookup the symbol `name', of the specified type, with the specified
142 // enviroment level
143 //
144 SYM * lookup(char * name, int type, int envno)
145 {
146         SYM * symbol = symbolTable[HashSymbol(name, envno)];
147
148         // Do linear-search for symbol in bucket
149         while (symbol != NULL)
150         {
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
155                         break;
156
157                 symbol = symbol->snext;
158         }
159
160         // Return NULL or matching symbol
161         return symbol;
162 }
163
164
165 //
166 // Put symbol on "order-of-declaration" list of symbols
167 //
168 void sym_decl(SYM * symbol)
169 {
170         if (symbol->sattr & SDECLLIST)
171                 return;                                                         // Already on list
172
173         symbol->sattr |= SDECLLIST;                             // Mark "already on list"
174
175         if (sdecl == NULL)
176                 sdecl = symbol;                                         // First on decl-list
177         else 
178                 sdecltail->sdecl = symbol;                      // Add to end of list
179
180         symbol->sdecl = NULL;                                   // Fix up list's tail
181         sdecltail = symbol;
182 }
183
184
185 //
186 // Make all referenced, undefined symbols global
187 //
188 int syg_fix(void)
189 {
190         SYM * sy;
191
192         DEBUG printf("~syg_fix()\n");
193
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)
197         {
198                 if (sy->stype == LABEL && sy->senv == 0
199                         && ((sy->sattr & (REFERENCED | DEFINED)) == REFERENCED))
200                         sy->sattr |= GLOBAL;
201         }
202
203         return 0;
204 }
205
206
207 //
208 // Convert string to uppercase
209 //
210 int uc_string(char * s)
211 {
212         for(; *s; s++)
213         {
214                 if (*s >= 'a' && *s <= 'z')
215                         *s -= 32;
216         }
217
218         return 0;
219 }
220
221
222 //
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
225 // symbol table.
226 //
227 int sy_assign(char * buf, char *(* constr)())
228 {
229         SYM * sy;
230         int scount;
231         //int i;
232
233         scount = 0;
234
235         if (buf == NULL)
236         {
237                 // Append all symbols not appearing on the .sdecl list to the end of
238                 // the .sdecl list
239                 for(sy=sorder; sy!=NULL; sy=sy->sorder)
240                 {
241                         // Essentially the same as 'sym_decl()' above:
242                         if (sy->sattr & SDECLLIST)
243                                 continue;                // Already on list 
244
245                         sy->sattr |= SDECLLIST;                            // Mark "on the list"
246
247                         if (sdecl == NULL)
248                                 sdecl = sy;                      // First on decl-list 
249                         else
250                                 sdecltail->sdecl = sy;                        // Add to end of list
251
252                         sy->sdecl = NULL;                                  // Fix up list's tail
253                         sdecltail = sy;
254                 }
255         }
256
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)
260         {
261                 if (sy->sattre & UNDEF_EQUR)
262                         continue;                 // Don't want undefined on our list
263
264                 if (sy->sattre & UNDEF_CC)
265                         continue;                   
266                 
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))
272                 {
273                         sy->senv = (WORD)scount++;
274
275                         if (buf != NULL)
276                                 buf = (*constr)(buf, sy, 1);
277                 }
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'))
283                 {
284                         sy->senv = (WORD)scount++;
285                         if (buf != NULL) buf = (*constr)(buf, sy, 0);
286                 }
287         }
288
289         return scount;
290 }
291
292
293 //
294 // Generate symbol table for listing file
295 //
296 int symtable(void)
297 {
298         int i;
299         int j;
300         SYM * q = NULL;
301         SYM * p;
302         SYM * r;
303         SYM * k;
304         SYM ** sy;
305         SYM * colptr[4];
306         char ln[150];
307         char ln1[150];
308         char ln2[20];
309         char c, c1;
310         WORD w;
311         int ww;
312         int colhei;
313         extern int pagelen;
314
315         colhei = pagelen - 5;
316
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));
321
322         for(i=0; i<128; ++i)
323                 sy[i] = NULL;
324
325         for(i=0; i<NBUCKETS; ++i)
326         {
327                 for(p=symbolTable[i]; p!=NULL; p=k)
328                 {
329                         k = p->snext;
330                         j = *p->sname;
331                         r = NULL;
332
333                         if (p->stype != LABEL)
334                                 continue;                   // Ignore non-labels
335
336                         if (p->sattre & UNDEF_EQUR)
337                                 continue;
338
339                         for(q=sy[j]; q!=NULL; q=q->snext)
340                         {
341                                 if (strcmp(p->sname, q->sname) < 0)
342                                         break;
343                                 else
344                                         r = q;
345                         }
346
347                         if (r == NULL)
348                         {                               // Insert at front of list
349                                 p->snext = sy[j];
350                                 sy[j] = p;
351                         }
352                         else
353                         {                               // Insert in middle or append to list
354                                 p->snext = r->snext;
355                                 r->snext = p;
356                         }
357                 }
358         }
359
360         // Link all symbols onto one list again
361         p = NULL;
362
363         for(i=0; i<128; ++i)
364         {
365                 if ((r = sy[i]) != NULL)
366                 {
367                         if (p == NULL)
368                                 q = r;
369                         else
370                                 q->snext = r;
371
372                         while (q->snext != NULL)
373                                 q = q->snext;
374
375                         if (p == NULL)
376                                 p = r;
377                 }
378         }
379
380         eject();
381         strcpy(subttl, "Symbol Table");
382
383         while (p != NULL)
384         {
385                 for(i=0; i<4; ++i)
386                 {
387                         colptr[i] = p;
388
389                         for(j=0; j<colhei; ++j)
390                         {
391                                 if (p == NULL)
392                                         break;
393                                 else
394                                         p = p->snext;
395                         }
396                 }
397
398                 for(i=0; i<colhei; ++i)
399                 {
400                         *ln = EOS;
401
402                         if (colptr[0] == NULL)
403                                 break;
404
405                         for(j=0; j<4; ++j)
406                         {
407                                 if ((q = colptr[j]) == NULL)
408                                         break;
409
410                                 colptr[j] = q->snext;
411                                 w = q->sattr;
412                                 ww = q->sattre;
413                                 // Pick a tag:
414                                 // c    common
415                                 // x    external reference
416                                 // g    global (export)
417                                 // space        nothing special
418                                 c1 = SPACE;
419                                 c = SPACE;
420
421                                 if (w & COMMON)
422                                         c = 'c';
423                                 else if ((w & (DEFINED|GLOBAL)) == GLOBAL)
424                                         c = 'x';
425                                 else if (w & GLOBAL)
426                                         c = 'g';
427
428                                 c1 = tdb_text[w & TDB];
429
430                                 if (c == 'x')
431                                         strcpy(ln2, "external");
432                                 else
433                                 {
434                                         sprintf(ln2, "%08X", q->svalue);
435                                         uc_string(ln2);
436                                 }
437
438                                 sprintf(ln1, "  %16s %s %c%c%c", q->sname, ln2, (ww & EQUATEDREG) ? 'e' : SPACE, c1, c);
439                                 strcat(ln, ln1);
440                         }
441
442                         ship_ln(ln);
443                 }
444
445                 eject();
446         }
447
448         return 0;
449 }