-SYM *newsym(char *name, int type, int envno) {
- int hash; // Symbol hash value
- SYM *sy; // Pointer to symbol
-
-
- // Allocate the symbol
- sy = (SYM *)amem((long)(sizeof(SYM)));
- if(sy == NULL) {
- printf("SYMALLOC ERROR (%s)\n", name);
- return(NULL);
- }
-
- sy->sname = nstring(name);
-
- // Fill-in the symbol
- sy->stype = (BYTE)type;
- sy->senv = (WORD)envno;
- sy->sattr = 0;
- if(rgpu || rdsp) sy->sattre = RISCSYM;
- else sy->sattre = 0;
- sy->svalue = 0;
-
- // Install symbol in symbol table
- hash = syhash(name, envno);
- sy->snext = sytab[hash];
- sytab[hash] = sy;
-
- // Append symbol to symbol-order list
- if(sorder == NULL)
- sorder = sy; // Add first symbol
- else
- sordtail->sorder = sy; // Or append to tail of list
-
- sy->sorder = NULL;
- sordtail = sy;
-
- return(sy); // Return pointer to symbol
+//
+// Assign numbers to symbols that are to be exported or imported. The symbol
+// number is put in 'senv'. Returns the number of symbols that will be in the
+// symbol table.
+//
+// N.B.: This is usually called twice; first time with NULL parameters and the
+// second time with real ones. The first one is typically done to get a
+// count of the # of symbols in the symbol table, and the second is to
+// actually create it.
+//
+uint32_t AssignSymbolNos(uint8_t * buf, uint8_t *(* construct)())
+{
+ uint16_t scount = 0;
+
+ // Done only on first pass...
+ if (buf == NULL)
+ {
+ // Append all symbols not appearing on the .sdecl list to the end of
+ // the .sdecl list
+ for(SYM * sy=sorder; sy!=NULL; sy=sy->sorder)
+ AddToSymbolDeclarationList(sy);
+ }
+
+ // Run through all symbols (now on the .sdecl list) and assign numbers to
+ // them. We also pick which symbols should be global or not here.
+ for(SYM * sy=sdecl; sy!=NULL; sy=sy->sdecl)
+ {
+ // Always export debug symbols. Don't force them global.
+ if (DBGSYM == sy->stype) {
+ sy->senv = scount++;
+
+ if (buf != NULL)
+ buf = construct(buf, sy, 0);
+ continue;
+ }
+
+ // Skip non-labels.
+ if (sy->stype != LABEL)
+ continue;
+
+ // Nuke equated register/CC symbols from orbit:
+ if (sy->sattre & (EQUATEDREG | UNDEF_EQUR | EQUATEDCC | UNDEF_CC))
+ continue;
+
+ // Export or import external references, and export COMMON blocks.
+ // N.B.: This says to mark the symbol as global if either 1) the symbol
+ // is global AND the symbol is defined OR referenced, or 2) this
+ // symbol is a common symbol.
+ if (((sy->sattr & GLOBAL) && (sy->sattr & (DEFINED | REFERENCED)))
+ || (sy->sattr & COMMON))
+ {
+ sy->senv = scount++;
+
+ if (buf != NULL)
+ buf = construct(buf, sy, 1);
+ }
+ // Export vanilla labels (but don't make them global). An exception is
+ // made for equates, which are not exported unless they are referenced.
+ // ^^^ The above just might be bullshit. ^^^
+ // N.B.: This says if the symbol is either defined OR referenced (but
+ // because of the above we know it *won't* be GLOBAL). And
+ // lsym_flag is always set true in Process() in rmac.c.
+ else if (lsym_flag && (sy->sattr & (DEFINED | REFERENCED)))
+ {
+ sy->senv = scount++;
+
+ if (buf != NULL)
+ buf = construct(buf, sy, 0);
+ }
+ }
+
+ return scount;