-int syg_fix(void) {
- SYM *sy;
-
- DEBUG printf("~syg_fix()\n");
-
- // Scan through all symbols;
- // If a symbol is REFERENCED but not DEFINED, then make it global.
- for(sy = sorder; sy != NULL; sy = sy->sorder)
- if(sy->stype == LABEL && sy->senv == 0 &&
- ((sy->sattr & (REFERENCED|DEFINED)) == REFERENCED))
- sy->sattr |= GLOBAL;
-
- return(0);
+//
+// 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)
+ {
+ // 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;