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
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
21 // Tags for marking symbol spaces
27 static char tdb_text[8] = {
28 'a', 't', 'd', '!', 'b', SPACE, SPACE, SPACE
32 // --- Initialize Symbol Table ---------------------------------------------------------------------
38 for(i = 0; i < NBUCKETS; ++i) // Initialise symbol hash table
41 curenv = 1; // Init local symbol enviroment
42 sorder = NULL; // Init symbol-reference list
44 sdecl = NULL; // Init symbol-decl list
49 // --- Allocate and Return Pointer to a Copy of a String -------------------------------------------
52 char *nstring(char *str) {
56 for(i = 0; str[i]; ++i)
67 // --- Hash the Print Name and Enviroment Number ---------------------------------------------------
69 int syhash(char *name, int envno) {
70 int sum, k; // Hash calculation
73 for(sum = envno; *name; ++name) {
80 return(sum & (NBUCKETS - 1));
84 // --- Make a new symbol of type `type' in enviroment `envno' --------------------------------------
87 SYM *newsym(char *name, int type, int envno) {
88 int hash; // Symbol hash value
89 SYM *sy; // Pointer to symbol
92 // Allocate the symbol
93 sy = (SYM *)amem((long)(sizeof(SYM)));
95 printf("SYMALLOC ERROR (%s)\n", name);
99 sy->sname = nstring(name);
101 // Fill-in the symbol
102 sy->stype = (BYTE)type;
103 sy->senv = (WORD)envno;
105 if(rgpu || rdsp) sy->sattre = RISCSYM;
109 // Install symbol in symbol table
110 hash = syhash(name, envno);
111 sy->snext = sytab[hash];
114 // Append symbol to symbol-order list
116 sorder = sy; // Add first symbol
118 sordtail->sorder = sy; // Or append to tail of list
123 return(sy); // Return pointer to symbol
127 // --- Lookup the symbol `name', of the specified type, with the specified enviroment level --------
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
135 // Pick a hash-bucket (SAME algorithm as syhash())
138 for(sum = envno; *s;) {
144 sy = sytab[sum & (NBUCKETS-1)];
146 // Do linear-search for symbol in bucket
148 if(sy->stype == type && // Type, envno and name must match
150 *name == *sy->sname && // Fast check for first character
151 !strcmp(name, sy->sname))
156 return(sy); // Return NULL or matching symbol
160 // --- Put symbol on "order-of-declaration" list of symbols ----------------------------------------
163 void sym_decl(SYM *sym) {
164 if(sym->sattr & SDECLLIST) return; // Already on list
165 sym->sattr |= SDECLLIST; // Mark "already on list"
168 sdecl = sym; // First on decl-list
170 sdecltail->sdecl = sym; // Add to end of list
172 sym->sdecl = NULL; // Fix up list's tail
177 // --- Make all referenced, undefined symbols global -----------------------------------------------
183 DEBUG printf("~syg_fix()\n");
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))
196 // --- Convert string to uppercase -----------------------------------------------------------------
199 int uc_string(char *s) {
201 if(*s >= 'a' && *s <= 'z')
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 // -------------------------------------------------------------------------------------------------
213 int sy_assign(char *buf, char *(*constr)()) {
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) {
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"
228 if(sdecl == NULL) sdecl = sy; // First on decl-list
229 else sdecltail->sdecl = sy; // Add to end of list
231 sy->sdecl = NULL; // Fix up list's tail
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) {
239 if(sy->sattre & UNDEF_EQUR) continue; // Don't want undefined on our list
240 if(sy->sattre & UNDEF_CC) continue;
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);
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);
264 // --- Generate symbol table for listing file ------------------------------------------------------
285 colhei = pagelen - 5;
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;
292 for(i = 0; i < NBUCKETS; ++i)
293 for(p = sytab[i]; p != NULL; p = k) {
297 if(p->stype != LABEL) continue; // Ignore non-labels
298 if(p->sattre & UNDEF_EQUR) continue;
300 for(q = sy[j]; q != NULL; q = q->snext)
301 if(strcmp(p->sname, q->sname) < 0)
305 if(r == NULL) { // Insert at front of list
308 } else { // Insert in middle or append to list
314 // Link all symbols onto one list again
316 for(i = 0; i < 128; ++i)
317 if((r = sy[i]) != NULL) {
322 while(q->snext != NULL)
330 strcpy(subttl, "Symbol Table");
333 for (i = 0; i < 4; ++i) {
335 for(j = 0; j < colhei; ++j)
341 for(i = 0; i < colhei; ++i) {
343 if(colptr[0] == NULL)
346 for(j = 0; j < 4; ++j) {
347 if((q = colptr[j]) == NULL)
349 colptr[j] = q->snext;
354 // x external reference
356 // space nothing special
360 if(w & COMMON) c = 'c';
361 else if((w & (DEFINED|GLOBAL)) == GLOBAL) c = 'x';
362 else if(w & GLOBAL) c = 'g';
364 c1 = tdb_text[w & TDB];
365 if(c == 'x') strcpy(ln2, "external");
367 sprintf(ln2, "%08lx", q->svalue);
371 sprintf(ln1, " %16s %s %c%c%c", q->sname, ln2, (ww & EQUATEDREG) ? 'e' : SPACE, c1, c);