]> Shamusworld >> Repos - rmac/blobdiff - symbol.c
Add NewDebugSymbol(): stabs symbol factory
[rmac] / symbol.c
index 0a50eeb6c34c77ca39db4371e50fa171fcae7c24..490d4f077fb388ae5c1508fb4454a505846b7f72 100644 (file)
--- a/symbol.c
+++ b/symbol.c
@@ -88,7 +88,7 @@ SYM * NewSymbol(uint8_t * name, int type, int envno)
        }
 
        // Fill-in the symbol
-       symbol->sname  = strdup(name);
+       symbol->sname  = name ? strdup(name) : NULL;
        symbol->stype  = (uint8_t)type;
        symbol->senv   = (uint16_t)envno;
        // We don't set this as DEFINED, as it could be a forward reference!
@@ -99,14 +99,22 @@ SYM * NewSymbol(uint8_t * name, int type, int envno)
        symbol->svalue = 0;
        symbol->sorder = NULL;
        symbol->uid    = currentUID++;
+       // We don't set st_type, st_desc, or st_other here because they are only
+       // used by stabs debug symbols, which are always initialized by
+       // NewDebugSymbol(), which always sets these fields. Hence, initializing
+       // them here would be redundant.
 
-       // Record filename the symbol is defined (for now only used by macro error reporting)
+       // Record filename the symbol is defined (Used by macro error reporting and some debug symbols)
        symbol->cfileno = cfileno;
 
-       // Install symbol in the symbol table
-       int hash = HashSymbol(name, envno);
-       symbol->snext = symbolTable[hash];
-       symbolTable[hash] = symbol;
+       // Don't hash debug symbols: they are never looked up and may have no name.
+       if (type != DBGSYM)
+       {
+               // Install symbol in the symbol table
+               int hash = HashSymbol(name, envno);
+               symbol->snext = symbolTable[hash];
+               symbolTable[hash] = symbol;
+       }
 
        // Append symbol to the symbol-order list
        if (sorder == NULL)
@@ -235,7 +243,16 @@ uint32_t AssignSymbolNos(uint8_t * buf, uint8_t *(* construct)())
        // them. We also pick which symbols should be global or not here.
        for(SYM * sy=sdecl; sy!=NULL; sy=sy->sdecl)
        {
-               // Skip non-labels
+               // 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;
 
@@ -547,3 +564,19 @@ int symtable(void)
 
        return 0;
 }
+
+SYM * NewDebugSymbol(uint8_t * str, uint8_t type, uint8_t other, uint16_t desc)
+{
+       SYM * symbol = NewSymbol(str, DBGSYM, 0);
+
+       if (NULL == symbol)
+               return NULL;
+
+       AddToSymbolDeclarationList(symbol);
+
+       symbol->st_type = type;
+       symbol->st_other = other;
+       symbol->st_desc = desc;
+
+       return symbol;
+}