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
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
20 // Tags for marking symbol spaces
26 static char tdb_text[8] = {
27 'a', 't', 'd', '!', 'b', SPACE, SPACE, SPACE
31 // --- Initialize Symbol Table ---------------------------------------------------------------------
37 for(i = 0; i < NBUCKETS; ++i) // Initialise symbol hash table
40 curenv = 1; // Init local symbol enviroment
41 sorder = NULL; // Init symbol-reference list
43 sdecl = NULL; // Init symbol-decl list
48 // --- Allocate and Return Pointer to a Copy of a String -------------------------------------------
51 char *nstring(char *str) {
55 for(i = 0; str[i]; ++i)
66 // --- Hash the Print Name and Enviroment Number ---------------------------------------------------
68 int syhash(char *name, int envno) {
69 int sum, k; // Hash calculation
72 for(sum = envno; *name; ++name) {
79 return(sum & (NBUCKETS - 1));
83 // --- Make a new symbol of type `type' in enviroment `envno' --------------------------------------
86 SYM *newsym(char *name, int type, int envno) {
87 int hash; // Symbol hash value
88 SYM *sy; // Pointer to symbol
91 // Allocate the symbol
92 sy = (SYM *)amem((long)(sizeof(SYM)));
94 printf("SYMALLOC ERROR (%s)\n", name);
98 sy->sname = nstring(name);
100 // Fill-in the symbol
101 sy->stype = (BYTE)type;
102 sy->senv = (WORD)envno;
104 if(rgpu || rdsp) sy->sattre = RISCSYM;
108 // Install symbol in symbol table
109 hash = syhash(name, envno);
110 sy->snext = sytab[hash];
113 // Append symbol to symbol-order list
115 sorder = sy; // Add first symbol
117 sordtail->sorder = sy; // Or append to tail of list
122 return(sy); // Return pointer to symbol
126 // --- Lookup the symbol `name', of the specified type, with the specified enviroment level --------
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
134 // Pick a hash-bucket (SAME algorithm as syhash())
137 for(sum = envno; *s;) {
143 sy = sytab[sum & (NBUCKETS-1)];
145 // Do linear-search for symbol in bucket
147 if(sy->stype == type && // Type, envno and name must match
149 *name == *sy->sname && // Fast check for first character
150 !strcmp(name, sy->sname))
155 return(sy); // Return NULL or matching symbol
159 // --- Put symbol on "order-of-declaration" list of symbols ----------------------------------------
162 void sym_decl(SYM *sym) {
163 if(sym->sattr & SDECLLIST) return; // Already on list
164 sym->sattr |= SDECLLIST; // Mark "already on list"
167 sdecl = sym; // First on decl-list
169 sdecltail->sdecl = sym; // Add to end of list
171 sym->sdecl = NULL; // Fix up list's tail
176 // --- Make all referenced, undefined symbols global -----------------------------------------------
182 DEBUG printf("~syg_fix()\n");
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))
195 // --- Convert string to uppercase -----------------------------------------------------------------
198 int uc_string(char *s) {
200 if(*s >= 'a' && *s <= 'z')
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 // -------------------------------------------------------------------------------------------------
212 int sy_assign(char *buf, char *(*constr)()) {
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) {
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"
227 if(sdecl == NULL) sdecl = sy; // First on decl-list
228 else sdecltail->sdecl = sy; // Add to end of list
230 sy->sdecl = NULL; // Fix up list's tail
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) {
238 if(sy->sattre & UNDEF_EQUR) continue; // Don't want undefined on our list
239 if(sy->sattre & UNDEF_CC) continue;
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);
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);
263 // --- Generate symbol table for listing file ------------------------------------------------------
284 colhei = pagelen - 5;
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;
291 for(i = 0; i < NBUCKETS; ++i)
292 for(p = sytab[i]; p != NULL; p = k) {
296 if(p->stype != LABEL) continue; // Ignore non-labels
297 if(p->sattre & UNDEF_EQUR) continue;
299 for(q = sy[j]; q != NULL; q = q->snext)
300 if(strcmp(p->sname, q->sname) < 0)
304 if(r == NULL) { // Insert at front of list
307 } else { // Insert in middle or append to list
313 // Link all symbols onto one list again
315 for(i = 0; i < 128; ++i)
316 if((r = sy[i]) != NULL) {
321 while(q->snext != NULL)
329 strcpy(subttl, "Symbol Table");
332 for (i = 0; i < 4; ++i) {
334 for(j = 0; j < colhei; ++j)
340 for(i = 0; i < colhei; ++i) {
342 if(colptr[0] == NULL)
345 for(j = 0; j < 4; ++j) {
346 if((q = colptr[j]) == NULL)
348 colptr[j] = q->snext;
353 // x external reference
355 // space nothing special
359 if(w & COMMON) c = 'c';
360 else if((w & (DEFINED|GLOBAL)) == GLOBAL) c = 'x';
361 else if(w & GLOBAL) c = 'g';
363 c1 = tdb_text[w & TDB];
364 if(c == 'x') strcpy(ln2, "external");
366 sprintf(ln2, "%08lx", q->svalue);
370 sprintf(ln1, " %16s %s %c%c%c", q->sname, ln2, (ww & EQUATEDREG) ? 'e' : SPACE, c1, c);