]> Shamusworld >> Repos - rmac/blob - symbol.c
Updated printf commands that were complaining about long-words as necessary.
[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 static SYM *sytab[NBUCKETS];                                // User symbol-table header
15 int curenv;                                                 // Current enviroment number
16 SYM *sorder;                                                // * -> Symbols, in order of reference
17 SYM *sordtail;                                              // * -> Last symbol in sorder list
18 SYM *sdecl;                                                 // * -> Symbols, in order of declaration
19 SYM *sdecltail;                                             // * -> Last symbol in sdecl list
20
21 // Tags for marking symbol spaces
22 // a = absolute
23 // t = text
24 // d = data
25 // ! = "impossible!"
26 // b = BSS
27 static char tdb_text[8] = {
28    'a', 't', 'd', '!', 'b', SPACE, SPACE, SPACE
29 };
30
31 //
32 // --- Initialize Symbol Table ---------------------------------------------------------------------
33 //
34
35 void init_sym(void) {
36    int i;                                                   // Iterator
37
38    for(i = 0; i < NBUCKETS; ++i)                            // Initialise symbol hash table
39       sytab[i] = NULL;
40
41    curenv = 1;                                              // Init local symbol enviroment
42    sorder = NULL;                                           // Init symbol-reference list
43    sordtail = NULL;
44    sdecl = NULL;                                            // Init symbol-decl list
45    sdecltail = NULL;
46 }
47
48 //
49 // --- Allocate and Return Pointer to a Copy of a String -------------------------------------------
50 //
51
52 char *nstring(char *str) {
53    long i;
54    char *s, *d;
55
56    for(i = 0; str[i]; ++i)
57       ;
58    s = d = amem(i + 1);
59    while(*str)
60       *d++ = *str++;
61    *d++ = '\0';
62    
63    return(s);
64 }
65
66 //
67 // --- Hash the Print Name and Enviroment Number ---------------------------------------------------
68 //
69 int syhash(char *name, int envno) {
70    int sum, k;                                              // Hash calculation
71
72    k = 0;
73    for(sum = envno; *name; ++name) {
74       if(k++ == 1)
75          sum += *name << 2;
76       else
77          sum += *name;
78    }
79
80    return(sum & (NBUCKETS - 1));
81 }
82
83 //
84 // --- Make a new symbol of type `type' in enviroment `envno' --------------------------------------
85 //
86
87 SYM *newsym(char *name, int type, int envno) {
88    int hash;                                                // Symbol hash value
89    SYM *sy;                                                 // Pointer to symbol
90
91    
92    // Allocate the symbol
93    sy = (SYM *)amem((long)(sizeof(SYM)));
94    if(sy == NULL) {
95       printf("SYMALLOC ERROR (%s)\n", name);
96       return(NULL);
97    }
98
99    sy->sname = nstring(name);
100
101    // Fill-in the symbol
102    sy->stype  = (BYTE)type;
103    sy->senv   = (WORD)envno;
104    sy->sattr  = 0;
105    if(rgpu || rdsp) sy->sattre = RISCSYM;
106    else sy->sattre = 0;
107    sy->svalue = 0;
108
109    // Install symbol in symbol table
110    hash = syhash(name, envno);
111    sy->snext = sytab[hash];
112    sytab[hash] = sy;
113
114    // Append symbol to symbol-order list
115    if(sorder == NULL)
116       sorder = sy;                                          // Add first symbol 
117    else
118       sordtail->sorder = sy;                                // Or append to tail of list
119
120    sy->sorder = NULL;
121    sordtail = sy;
122
123    return(sy);                                              // Return pointer to symbol
124 }
125
126 //
127 // --- Lookup the symbol `name', of the specified type, with the specified enviroment level --------
128 //
129
130 SYM *lookup(char *name, int type, int envno) {
131    SYM *sy;                                                 // Symbol record pointer
132    int k, sum;                                              // Hash bucket calculation
133    char *s;                                                 // String pointer
134
135    // Pick a hash-bucket (SAME algorithm as syhash())
136    k = 0;
137    s = name;
138    for(sum = envno; *s;) {
139       if(k++ == 1)
140          sum += *s++ << 2;
141       else sum += *s++;
142    }
143
144    sy = sytab[sum & (NBUCKETS-1)];
145
146    // Do linear-search for symbol in bucket
147    while(sy != NULL) {
148       if(sy->stype == type       &&                         // Type, envno and name must match
149          sy->senv  == envno      &&
150                         *name     == *sy->sname &&                         // Fast check for first character
151                         !strcmp(name, sy->sname))
152          break;
153       else sy = sy->snext;
154    }
155
156    return(sy);                                              // Return NULL or matching symbol
157 }
158
159 //
160 // --- Put symbol on "order-of-declaration" list of symbols ----------------------------------------
161 //
162
163 void sym_decl(SYM *sym) {
164    if(sym->sattr & SDECLLIST) return;                       // Already on list
165    sym->sattr |= SDECLLIST;                                 // Mark "already on list"
166
167    if(sdecl == NULL)
168       sdecl = sym;                                          // First on decl-list
169    else 
170       sdecltail->sdecl = sym;                               // Add to end of list
171
172    sym->sdecl = NULL;                                       // Fix up list's tail
173    sdecltail = sym;
174 }
175
176 //
177 // --- Make all referenced, undefined symbols global -----------------------------------------------
178 //
179
180 int syg_fix(void) {
181    SYM *sy;
182
183    DEBUG printf("~syg_fix()\n");
184
185    // Scan through all symbols;
186    // If a symbol is REFERENCED but not DEFINED, then make it global.
187    for(sy = sorder; sy != NULL; sy = sy->sorder)
188       if(sy->stype == LABEL && sy->senv == 0 &&
189                         ((sy->sattr & (REFERENCED|DEFINED)) == REFERENCED))
190          sy->sattr |= GLOBAL;
191
192    return(0);
193 }
194
195 //
196 // --- Convert string to uppercase -----------------------------------------------------------------
197 //
198
199 int uc_string(char *s) {
200    for(; *s; ++s)
201       if(*s >= 'a' && *s <= 'z')
202          *s -= 32;
203    return(0);
204 }
205
206 //
207 // -------------------------------------------------------------------------------------------------
208 // Assign numbers to symbols that are to be exported or imported.  The symbol number is put in 
209 // `.senv'. Return the number of symbols that will be in the symbol table.
210 // -------------------------------------------------------------------------------------------------
211 //
212
213 int sy_assign(char *buf, char *(*constr)()) {
214    SYM *sy;
215    int scount;
216    //int i;
217
218    scount = 0;
219
220    if(buf == NULL)
221       // Append all symbols not appearing on the .sdecl list to the end of the .sdecl list
222       for(sy = sorder; sy != NULL; sy = sy->sorder) {
223
224          // Essentially the same as 'sym_decl()' above:
225          if(sy->sattr & SDECLLIST) continue;                // Already on list 
226          sy->sattr |= SDECLLIST;                            // Mark "on the list"
227
228          if(sdecl == NULL) sdecl = sy;                      // First on decl-list 
229          else sdecltail->sdecl = sy;                        // Add to end of list
230
231          sy->sdecl = NULL;                                  // Fix up list's tail
232          sdecltail = sy;
233       }
234
235    // Run through all symbols (now on the .sdecl list) and assign numbers to them.  We also pick 
236    // which symbols should be global or not here.
237    for(sy = sdecl; sy != NULL; sy = sy->sdecl) {
238
239       if(sy->sattre & UNDEF_EQUR) continue;                 // Don't want undefined on our list
240       if(sy->sattre & UNDEF_CC) continue;                   
241       
242       // Export or import external references, and export COMMON blocks.
243       if((sy->stype == LABEL) &&
244          ((sy->sattr & (GLOBAL|DEFINED)) == (GLOBAL|DEFINED) ||
245          (sy->sattr & (GLOBAL|REFERENCED)) == (GLOBAL|REFERENCED)) ||
246          (sy->sattr & COMMON)) {
247          sy->senv = (WORD)scount++;
248          if(buf != NULL) buf = (*constr)(buf, sy, 1);
249       } else
250          // Export vanilla labels (but don't make them global). An exception is made for equates, 
251          // which are not exported unless they are referenced.
252          if(sy->stype == LABEL && lsym_flag &&
253             (sy->sattr & (DEFINED|REFERENCED)) != 0 &&
254             (!as68_flag || *sy->sname != 'L') ) {
255             sy->senv = (WORD)scount++;
256             if(buf != NULL) buf = (*constr)(buf, sy, 0);
257          }
258    }
259
260    return(scount);
261 }
262
263 //
264 // --- Generate symbol table for listing file ------------------------------------------------------
265 //
266
267 int symtable(void) {
268    int i;
269    int j;
270    SYM *q = NULL;
271    SYM *p;
272    SYM *r;
273    SYM *k;
274    SYM **sy;
275    SYM *colptr[4];
276    char ln[150];
277    char ln1[150];
278    char ln2[20];
279    char c, c1;
280    WORD w;
281    int ww;
282    int colhei;
283    extern int pagelen;
284
285    colhei = pagelen - 5;
286
287    // Allocate storage for list headers and partition all labels.  
288    // Throw away macros and macro arguments.
289    sy = (SYM **)amem((LONG)(128 * sizeof(LONG)));
290    for(i = 0; i < 128; ++i) sy[i] = NULL;
291
292    for(i = 0; i < NBUCKETS; ++i)
293       for(p = sytab[i]; p != NULL; p = k) {
294          k = p->snext;
295          j = *p->sname;
296          r = NULL;
297          if(p->stype != LABEL) continue;                    // Ignore non-labels
298          if(p->sattre & UNDEF_EQUR) continue;
299
300          for(q = sy[j]; q != NULL; q = q->snext)
301             if(strcmp(p->sname, q->sname) < 0)
302                break;
303             else r = q;
304
305          if(r == NULL) {                                    // Insert at front of list
306             p->snext = sy[j];
307             sy[j] = p;
308          } else {                                           // Insert in middle or append to list
309             p->snext = r->snext;
310             r->snext = p;
311          }
312       }
313
314    // Link all symbols onto one list again
315    p = NULL;
316    for(i = 0; i < 128; ++i)
317       if((r = sy[i]) != NULL) {
318          if(p == NULL)
319             q = r;
320          else q->snext = r;
321
322          while(q->snext != NULL)
323             q = q->snext;
324
325          if(p == NULL)
326             p = r;
327       }
328
329    eject();
330    strcpy(subttl, "Symbol Table");
331
332    while(p != NULL) {
333       for (i = 0; i < 4; ++i) {
334          colptr[i] = p;
335          for(j = 0; j < colhei; ++j)
336             if(p == NULL)
337                break;
338             else p = p->snext;
339       }
340
341       for(i = 0; i < colhei; ++i) {
342          *ln = EOS;
343          if(colptr[0] == NULL)
344             break;
345
346          for(j = 0; j < 4; ++j) {
347             if((q = colptr[j]) == NULL)
348                break;
349             colptr[j] = q->snext;
350             w = q->sattr;
351             ww = q->sattre;
352             // Pick a tag:
353             // c        common
354             // x        external reference
355             // g        global (export)
356             // space    nothing special
357             c1 = SPACE;
358             c = SPACE;
359
360             if(w & COMMON) c = 'c';
361             else if((w & (DEFINED|GLOBAL)) == GLOBAL) c = 'x';
362             else if(w & GLOBAL) c = 'g';
363
364             c1 = tdb_text[w & TDB];
365             if(c == 'x') strcpy(ln2, "external");
366             else {
367                sprintf(ln2, "%08ux", q->svalue);
368                uc_string(ln2);
369             }
370
371             sprintf(ln1, "  %16s %s %c%c%c", q->sname, ln2, (ww & EQUATEDREG) ? 'e' : SPACE, c1, c);
372
373             strcat(ln, ln1);
374          }
375          ship_ln(ln);
376       }
377       eject();
378    }
379
380    return(0);
381 }