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