]> Shamusworld >> Repos - rmac/blob - symbol.c
Fixed word reversed fixup problem.
[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 //we don't do this, it could be a forward reference!
95 //      symbol->sattr  = DEFINED;               // We just defined it...
96         // This is a bad assumption. Not every symbol 1st seen in a RISC section is
97         // a RISC symbol!
98 //      symbol->sattre = (rgpu || rdsp ? RISCSYM : 0);
99         symbol->sattre = 0;
100         symbol->svalue = 0;
101         symbol->sorder = NULL;
102         symbol->uid    = currentUID++;
103
104         // Install symbol in symbol table
105         int hash = HashSymbol(name, envno);
106         symbol->snext = symbolTable[hash];
107         symbolTable[hash] = symbol;
108
109         // Append symbol to symbol-order list
110         if (sorder == NULL)
111                 sorder = symbol;                                        // Add first symbol 
112         else
113                 sordtail->sorder = symbol;                      // Or append to tail of list
114
115         sordtail = symbol;
116         return symbol;
117 }
118
119
120 //
121 // Look up the symbol name by its UID and return the pointer to the name.
122 // If it's not found, return NULL.
123 //
124 char * GetSymbolNameByUID(uint32_t uid)
125 {
126         //problem is with string lookup, that's why we're writing this
127         //so once this is written, we can put the uid in the token stream
128
129         // A much better approach to the symbol order list would be to make an
130         // array--that way you can do away with the UIDs and all the rest, and
131         // simply do an array lookup based on position. But meh, let's do this for
132         // now until we can rewrite things so they make sense.
133         SYM * symbol = sorder;
134
135         for(; symbol; symbol=symbol->sorder)
136         {
137                 if (symbol->uid == uid)
138                         return symbol->sname;
139         }
140
141         return NULL;
142 }
143
144
145 //
146 // Lookup the symbol `name', of the specified type, with the specified
147 // enviroment level
148 //
149 SYM * lookup(char * name, int type, int envno)
150 {
151         SYM * symbol = symbolTable[HashSymbol(name, envno)];
152
153         // Do linear-search for symbol in bucket
154         while (symbol != NULL)
155         {
156                 if (symbol->stype == type                       // Type, envno and name must match
157                         && symbol->senv  == envno
158                         && *name == *symbol->sname              // Fast check for first character
159                         && !strcmp(name, symbol->sname))        // More expensive check
160                         break;
161
162                 symbol = symbol->snext;
163         }
164
165         // Return NULL or matching symbol
166         return symbol;
167 }
168
169
170 //
171 // Put symbol on "order-of-declaration" list of symbols
172 //
173 //void sym_decl(SYM * symbol)
174 void AddToSymbolOrderList(SYM * symbol)
175 {
176         if (symbol->sattr & SDECLLIST)
177                 return;                                                         // Already on list
178
179         symbol->sattr |= SDECLLIST;                             // Mark "already on list"
180
181         if (sdecl == NULL)
182                 sdecl = symbol;                                         // First on decl-list
183         else 
184                 sdecltail->sdecl = symbol;                      // Add to end of list
185
186         symbol->sdecl = NULL;                                   // Fix up list's tail
187         sdecltail = symbol;
188 }
189
190
191 //
192 // Make all referenced, undefined symbols global
193 //
194 int syg_fix(void)
195 {
196         SYM * sy;
197
198         DEBUG printf("~syg_fix()\n");
199
200         // Scan through all symbols;
201         // If a symbol is REFERENCED but not DEFINED, then make it global.
202         for(sy=sorder; sy!=NULL; sy=sy->sorder)
203         {
204                 if (sy->stype == LABEL && sy->senv == 0
205                         && ((sy->sattr & (REFERENCED | DEFINED)) == REFERENCED))
206                         sy->sattr |= GLOBAL;
207         }
208
209         return 0;
210 }
211
212
213 //
214 // Convert string to uppercase
215 //
216 int uc_string(char * s)
217 {
218         for(; *s; s++)
219         {
220                 if (*s >= 'a' && *s <= 'z')
221                         *s -= 32;
222         }
223
224         return 0;
225 }
226
227
228 //
229 // Assign numbers to symbols that are to be exported or imported. The symbol
230 // number is put in `.senv'. Return the number of symbols that will be in the
231 // symbol table.
232 //
233 int sy_assign(char * buf, char *(* constr)())
234 {
235         SYM * sy;
236         int scount;
237         //int i;
238
239         scount = 0;
240
241         if (buf == NULL)
242         {
243                 // Append all symbols not appearing on the .sdecl list to the end of
244                 // the .sdecl list
245                 for(sy=sorder; sy!=NULL; sy=sy->sorder)
246                 {
247                         // Essentially the same as 'sym_decl()' above:
248                         if (sy->sattr & SDECLLIST)
249                                 continue;                // Already on list 
250
251                         sy->sattr |= SDECLLIST;                            // Mark "on the list"
252
253                         if (sdecl == NULL)
254                                 sdecl = sy;                      // First on decl-list 
255                         else
256                                 sdecltail->sdecl = sy;                        // Add to end of list
257
258                         sy->sdecl = NULL;                                  // Fix up list's tail
259                         sdecltail = sy;
260                 }
261         }
262
263         // Run through all symbols (now on the .sdecl list) and assign numbers to
264         // them. We also pick which symbols should be global or not here.
265         for(sy=sdecl; sy!=NULL; sy=sy->sdecl)
266         {
267                 if (sy->sattre & UNDEF_EQUR)
268                         continue;                 // Don't want undefined on our list
269
270                 if (sy->sattre & UNDEF_CC)
271                         continue;                   
272                 
273                 // Export or import external references, and export COMMON blocks.
274                 if ((sy->stype == LABEL)
275                         && ((sy->sattr & (GLOBAL | DEFINED)) == (GLOBAL | DEFINED)
276                         || (sy->sattr & (GLOBAL | REFERENCED)) == (GLOBAL | REFERENCED))
277                         || (sy->sattr & COMMON))
278                 {
279                         sy->senv = (WORD)scount++;
280
281                         if (buf != NULL)
282                                 buf = (*constr)(buf, sy, 1);
283                 }
284                 // Export vanilla labels (but don't make them global). An exception is
285                 // made for equates, which are not exported unless they are referenced.
286                 else if (sy->stype == LABEL && lsym_flag
287                         && (sy->sattr & (DEFINED | REFERENCED)) != 0
288                         && (!as68_flag || *sy->sname != 'L'))
289                 {
290                         sy->senv = (WORD)scount++;
291                         if (buf != NULL) buf = (*constr)(buf, sy, 0);
292                 }
293         }
294
295         return scount;
296 }
297
298
299 //
300 // Generate symbol table for listing file
301 //
302 int symtable(void)
303 {
304         int i;
305         int j;
306         SYM * q = NULL;
307         SYM * p;
308         SYM * r;
309         SYM * k;
310         SYM ** sy;
311         SYM * colptr[4];
312         char ln[150];
313         char ln1[150];
314         char ln2[20];
315         char c, c1;
316         WORD w;
317         int ww;
318         int colhei;
319         extern int pagelen;
320
321         colhei = pagelen - 5;
322
323         // Allocate storage for list headers and partition all labels.  
324         // Throw away macros and macro arguments.
325 //      sy = (SYM **)amem((LONG)(128 * sizeof(LONG)));
326         sy = (SYM **)malloc(128 * sizeof(LONG));
327
328         for(i=0; i<128; ++i)
329                 sy[i] = NULL;
330
331         for(i=0; i<NBUCKETS; ++i)
332         {
333                 for(p=symbolTable[i]; p!=NULL; p=k)
334                 {
335                         k = p->snext;
336                         j = *p->sname;
337                         r = NULL;
338
339                         if (p->stype != LABEL)
340                                 continue;                   // Ignore non-labels
341
342                         if (p->sattre & UNDEF_EQUR)
343                                 continue;
344
345                         for(q=sy[j]; q!=NULL; q=q->snext)
346                         {
347                                 if (strcmp(p->sname, q->sname) < 0)
348                                         break;
349                                 else
350                                         r = q;
351                         }
352
353                         if (r == NULL)
354                         {                               // Insert at front of list
355                                 p->snext = sy[j];
356                                 sy[j] = p;
357                         }
358                         else
359                         {                               // Insert in middle or append to list
360                                 p->snext = r->snext;
361                                 r->snext = p;
362                         }
363                 }
364         }
365
366         // Link all symbols onto one list again
367         p = NULL;
368
369         for(i=0; i<128; ++i)
370         {
371                 if ((r = sy[i]) != NULL)
372                 {
373                         if (p == NULL)
374                                 q = r;
375                         else
376                                 q->snext = r;
377
378                         while (q->snext != NULL)
379                                 q = q->snext;
380
381                         if (p == NULL)
382                                 p = r;
383                 }
384         }
385
386         eject();
387         strcpy(subttl, "Symbol Table");
388
389         while (p != NULL)
390         {
391                 for(i=0; i<4; ++i)
392                 {
393                         colptr[i] = p;
394
395                         for(j=0; j<colhei; ++j)
396                         {
397                                 if (p == NULL)
398                                         break;
399                                 else
400                                         p = p->snext;
401                         }
402                 }
403
404                 for(i=0; i<colhei; ++i)
405                 {
406                         *ln = EOS;
407
408                         if (colptr[0] == NULL)
409                                 break;
410
411                         for(j=0; j<4; ++j)
412                         {
413                                 if ((q = colptr[j]) == NULL)
414                                         break;
415
416                                 colptr[j] = q->snext;
417                                 w = q->sattr;
418                                 ww = q->sattre;
419                                 // Pick a tag:
420                                 // c    common
421                                 // x    external reference
422                                 // g    global (export)
423                                 // space        nothing special
424                                 c1 = SPACE;
425                                 c = SPACE;
426
427                                 if (w & COMMON)
428                                         c = 'c';
429                                 else if ((w & (DEFINED|GLOBAL)) == GLOBAL)
430                                         c = 'x';
431                                 else if (w & GLOBAL)
432                                         c = 'g';
433
434                                 c1 = tdb_text[w & TDB];
435
436                                 if (c == 'x')
437                                         strcpy(ln2, "external");
438                                 else
439                                 {
440                                         sprintf(ln2, "%08X", q->svalue);
441                                         uc_string(ln2);
442                                 }
443
444                                 sprintf(ln1, "  %16s %s %c%c%c", q->sname, ln2, (ww & EQUATEDREG) ? 'e' : SPACE, c1, c);
445                                 strcat(ln, ln1);
446                         }
447
448                         ship_ln(ln);
449                 }
450
451                 eject();
452         }
453
454         return 0;
455 }