]> git.sur5r.net Git - cc65/blobdiff - src/ca65/symentry.c
Remember where each symbol was defined and where it was referenced. Write this
[cc65] / src / ca65 / symentry.c
index 077ddd0d35b1ff55a811d9b3f7ea5f2156e7d4bf..1b81f6a2b22d70292f11490290a74fb1c7841475 100644 (file)
@@ -2,14 +2,14 @@
 /*                                                                           */
 /*                               symentry.c                                 */
 /*                                                                           */
-/*         Symbol table entry forward for the ca65 macroassembler           */
+/*              Symbol table entry for the ca65 macroassembler               */
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2006 Ullrich von Bassewitz                                       */
-/*               Römerstraße 52                                              */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 1998-2011, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -37,6 +37,7 @@
 
 /* common */
 #include "addrsize.h"
+#include "symdefs.h"
 #include "xmalloc.h"
 
 /* ca65 */
@@ -72,7 +73,7 @@ SymEntry* SymLast = 0;
 
 
 
-SymEntry* NewSymEntry (const char* Name, unsigned Flags)
+SymEntry* NewSymEntry (const StrBuf* Name, unsigned Flags)
 /* Allocate a symbol table entry, initialize and return it */
 {
     unsigned I;
@@ -84,18 +85,22 @@ SymEntry* NewSymEntry (const char* Name, unsigned Flags)
     S->Left              = 0;
     S->Right             = 0;
     S->Locals            = 0;
-    S->SymTab            = 0;
-    S->Pos               = CurPos;
+    S->Sym.Tab    = 0;
+    S->DefLines   = EmptyCollection;
+    S->RefLines   = EmptyCollection;
     for (I = 0; I < sizeof (S->GuessedUse) / sizeof (S->GuessedUse[0]); ++I) {
         S->GuessedUse[I] = 0;
     }
     S->Flags             = Flags;
+    S->DebugSymId = ~0U;
+    S->ImportId   = ~0U;
+    S->ExportId   = ~0U;
     S->Expr       = 0;
     S->ExprRefs   = AUTO_COLLECTION_INITIALIZER;
     S->ExportSize = ADDR_SIZE_DEFAULT;
     S->AddrSize   = ADDR_SIZE_DEFAULT;
     memset (S->ConDesPrio, 0, sizeof (S->ConDesPrio));
-    S->Name       = GetStringId (Name);
+    S->Name       = GetStrBufId (Name);
 
     /* Insert it into the list of all entries */
     S->List = SymList;
@@ -107,7 +112,7 @@ SymEntry* NewSymEntry (const char* Name, unsigned Flags)
 
 
 
-int SymSearchTree (SymEntry* T, const char* Name, SymEntry** E)
+int SymSearchTree (SymEntry* T, const StrBuf* Name, SymEntry** E)
 /* Search in the given tree for a name. If we find the symbol, the function
  * will return 0 and put the entry pointer into E. If we did not find the
  * symbol, and the tree is empty, E is set to NULL. If the tree is not empty,
@@ -126,10 +131,10 @@ int SymSearchTree (SymEntry* T, const char* Name, SymEntry** E)
     while (1) {
 
         /* Get the symbol name */
-        const char* SymName = GetString (T->Name);
+        const StrBuf* SymName = GetStrBuf (T->Name);
 
        /* Choose next entry */
-        int Cmp = strcmp (Name, SymName);
+        int Cmp = SB_Compare (Name, SymName);
                if (Cmp < 0 && T->Left) {
            T = T->Left;
        } else if (Cmp > 0&& T->Right) {
@@ -144,15 +149,6 @@ int SymSearchTree (SymEntry* T, const char* Name, SymEntry** E)
 
 
 
-void SymRef (SymEntry* S)
-/* Mark the given symbol as referenced */
-{
-    /* Mark the symbol as referenced */
-    S->Flags |= SF_REFERENCED;
-}
-
-
-
 void SymTransferExprRefs (SymEntry* From, SymEntry* To)
 /* Transfer all expression references from one symbol to another. */
 {
@@ -202,7 +198,7 @@ static void SymReplaceExprRefs (SymEntry* S)
          * Replace it by a literal node.
          */
         E->Op = EXPR_LITERAL;
-        E->V.Val = Val;
+        E->V.IVal = Val;
     }
 
     /* Remove all symbol references from the symbol */
@@ -216,24 +212,24 @@ void SymDef (SymEntry* S, ExprNode* Expr, unsigned char AddrSize, unsigned Flags
 {
     if (S->Flags & SF_IMPORT) {
                /* Defined symbol is marked as imported external symbol */
-               Error ("Symbol `%s' is already an import", GetSymName (S));
+               Error ("Symbol `%m%p' is already an import", GetSymName (S));
                return;
     }
     if ((Flags & SF_VAR) != 0 && (S->Flags & (SF_EXPORT | SF_GLOBAL))) {
         /* Variable symbols cannot be exports or globals */
-        Error ("Var symbol `%s' cannot be an export or global symbol", GetSymName (S));
+        Error ("Var symbol `%m%p' cannot be an export or global symbol", GetSymName (S));
         return;
     }
     if (S->Flags & SF_DEFINED) {
                /* Multiple definition. In case of a variable, this is legal. */
         if ((S->Flags & SF_VAR) == 0) {
-            Error ("Symbol `%s' is already defined", GetSymName (S));
+            Error ("Symbol `%m%p' is already defined", GetSymName (S));
             S->Flags |= SF_MULTDEF;
             return;
         } else {
             /* Redefinition must also be a variable symbol */
             if ((Flags & SF_VAR) == 0) {
-                Error ("Symbol `%s' is already different kind", GetSymName (S));
+                Error ("Symbol `%m%p' is already different kind", GetSymName (S));
                 return;
             }
             /* Delete the current symbol expression, since it will get
@@ -271,12 +267,16 @@ void SymDef (SymEntry* S, ExprNode* Expr, unsigned char AddrSize, unsigned Flags
      */
     if (S->Flags & SF_GLOBAL) {
         S->Flags = (S->Flags & ~SF_GLOBAL) | SF_EXPORT;
+        ReleaseFullLineInfo (&S->DefLines);
     }
 
     /* Mark the symbol as defined and use the given address size */
     S->Flags |= (SF_DEFINED | Flags);
     S->AddrSize = AddrSize;
 
+    /* Remember the line info of the symbol definition */
+    GetFullLineInfo (&S->DefLines);
+
     /* If the symbol is exported, check the address sizes */
     if (S->Flags & SF_EXPORT) {
         if (S->ExportSize == ADDR_SIZE_DEFAULT) {
@@ -284,9 +284,9 @@ void SymDef (SymEntry* S, ExprNode* Expr, unsigned char AddrSize, unsigned Flags
             S->ExportSize = S->AddrSize;
         } else if (S->AddrSize > S->ExportSize) {
             /* We're exporting a symbol smaller than it actually is */
-            PWarning (GetSymPos (S), 1, "Symbol `%s' is %s but exported %s",
-                      GetSymName (S), AddrSizeToStr (S->AddrSize),
-                      AddrSizeToStr (S->ExportSize));
+            Warning (1, "Symbol `%m%p' is %s but exported %s",
+                     GetSymName (S), AddrSizeToStr (S->AddrSize),
+                     AddrSizeToStr (S->ExportSize));
         }
     }
 
@@ -298,17 +298,29 @@ void SymDef (SymEntry* S, ExprNode* Expr, unsigned char AddrSize, unsigned Flags
 
 
 
+void SymRef (SymEntry* S)
+/* Mark the given symbol as referenced */
+{
+    /* Mark the symbol as referenced */
+    S->Flags |= SF_REFERENCED;
+
+    /* Remember the current location */
+    CollAppend (&S->RefLines, GetAsmLineInfo ());
+}
+
+
+
 void SymImport (SymEntry* S, unsigned char AddrSize, unsigned Flags)
 /* Mark the given symbol as an imported symbol */
 {
     if (S->Flags & SF_DEFINED) {
-       Error ("Symbol `%s' is already defined", GetSymName (S));
+       Error ("Symbol `%m%p' is already defined", GetSymName (S));
        S->Flags |= SF_MULTDEF;
        return;
     }
     if (S->Flags & SF_EXPORT) {
        /* The symbol is already marked as exported symbol */
-       Error ("Cannot import exported symbol `%s'", GetSymName (S));
+       Error ("Cannot import exported symbol `%m%p'", GetSymName (S));
        return;
     }
 
@@ -324,22 +336,28 @@ void SymImport (SymEntry* S, unsigned char AddrSize, unsigned Flags)
      */
     if (S->Flags & SF_IMPORT) {
        if ((Flags & SF_FORCED) != (S->Flags & SF_FORCED)) {
-                   Error ("Redeclaration mismatch for symbol `%s'", GetSymName (S));
+                   Error ("Redeclaration mismatch for symbol `%m%p'", GetSymName (S));
        }
         if (AddrSize != S->AddrSize) {
-            Error ("Address size mismatch for symbol `%s'", GetSymName (S));
+            Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
         }
     }
     if (S->Flags & SF_GLOBAL) {
         S->Flags &= ~SF_GLOBAL;
         if (AddrSize != S->AddrSize) {
-            Error ("Address size mismatch for symbol `%s'", GetSymName (S));
+            Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
        }
     }
 
     /* Set the symbol data */
     S->Flags |= (SF_IMPORT | Flags);
     S->AddrSize = AddrSize;
+
+    /* Mark the position of the import as the position of the definition.
+     * Please note: In case of multiple .global or .import statements, the line
+     * infos add up.
+     */
+    GetFullLineInfo (&S->DefLines);
 }
 
 
@@ -350,12 +368,12 @@ void SymExport (SymEntry* S, unsigned char AddrSize, unsigned Flags)
     /* Check if it's ok to export the symbol */
     if (S->Flags & SF_IMPORT) {
        /* The symbol is already marked as imported external symbol */
-       Error ("Symbol `%s' is already an import", GetSymName (S));
+       Error ("Symbol `%m%p' is already an import", GetSymName (S));
        return;
     }
     if (S->Flags & SF_VAR) {
         /* Variable symbols cannot be exported */
-        Error ("Var symbol `%s' cannot be exported", GetSymName (S));
+        Error ("Var symbol `%m%p' cannot be exported", GetSymName (S));
         return;
     }
 
@@ -364,9 +382,14 @@ void SymExport (SymEntry* S, unsigned char AddrSize, unsigned Flags)
      */
     if (S->Flags & SF_GLOBAL) {
         if (AddrSize != S->ExportSize) {
-            Error ("Address size mismatch for symbol `%s'", GetSymName (S));
+            Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
         }
         S->Flags &= ~SF_GLOBAL;
+
+        /* .GLOBAL remembers line infos in case an .IMPORT follows. We have
+         * to remove these here.
+         */
+        ReleaseFullLineInfo (&S->DefLines);
     }
 
     /* If the symbol was already marked as an export, but wasn't defined
@@ -374,7 +397,7 @@ void SymExport (SymEntry* S, unsigned char AddrSize, unsigned Flags)
      */
     if ((S->Flags & (SF_EXPORT|SF_DEFINED)) == SF_EXPORT) {
         if (S->ExportSize != AddrSize) {
-            Error ("Address size mismatch for symbol `%s'", GetSymName (S));
+            Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
         }
     }
     S->ExportSize = AddrSize;
@@ -388,7 +411,7 @@ void SymExport (SymEntry* S, unsigned char AddrSize, unsigned Flags)
             S->ExportSize = S->AddrSize;
         } else if (S->AddrSize > S->ExportSize) {
             /* We're exporting a symbol smaller than it actually is */
-            Warning (1, "Symbol `%s' is %s but exported %s",
+            Warning (1, "Symbol `%m%p' is %s but exported %s",
                      GetSymName (S), AddrSizeToStr (S->AddrSize),
                      AddrSizeToStr (S->ExportSize));
         }
@@ -407,7 +430,7 @@ void SymGlobal (SymEntry* S, unsigned char AddrSize, unsigned Flags)
 {
     if (S->Flags & SF_VAR) {
         /* Variable symbols cannot be exported or imported */
-        Error ("Var symbol `%s' cannot be made global", GetSymName (S));
+        Error ("Var symbol `%m%p' cannot be made global", GetSymName (S));
         return;
     }
 
@@ -420,7 +443,7 @@ void SymGlobal (SymEntry* S, unsigned char AddrSize, unsigned Flags)
             AddrSize = GetCurrentSegAddrSize ();
         }
         if (AddrSize != S->AddrSize) {
-            Error ("Address size mismatch for symbol `%s'", GetSymName (S));
+            Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
         }
         return;
     }
@@ -432,12 +455,12 @@ void SymGlobal (SymEntry* S, unsigned char AddrSize, unsigned Flags)
         if ((S->Flags & SF_DEFINED) == 0) {
             /* Symbol is undefined */
             if (AddrSize != S->ExportSize) {
-                Error ("Address size mismatch for symbol `%s'", GetSymName (S));
+                Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
             }
         } else if (AddrSize != ADDR_SIZE_DEFAULT) {
             /* Symbol is defined and address size given */
             if (AddrSize != S->ExportSize) {
-                Error ("Address size mismatch for symbol `%s'", GetSymName (S));
+                Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
             }
         }
         return;
@@ -449,7 +472,7 @@ void SymGlobal (SymEntry* S, unsigned char AddrSize, unsigned Flags)
      */
     if (S->Flags & SF_GLOBAL) {
         if (AddrSize != S->ExportSize) {
-            Error ("Address size mismatch for symbol `%s'", GetSymName (S));
+            Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
         }
         return;
     }
@@ -467,7 +490,7 @@ void SymGlobal (SymEntry* S, unsigned char AddrSize, unsigned Flags)
             S->ExportSize = S->AddrSize;
         } else if (S->AddrSize > S->ExportSize) {
             /* We're exporting a symbol smaller than it actually is */
-            Warning (1, "Symbol `%s' is %s but exported %s",
+            Warning (1, "Symbol `%m%p' is %s but exported %s",
                      GetSymName (S), AddrSizeToStr (S->AddrSize),
                      AddrSizeToStr (S->ExportSize));
         }
@@ -484,6 +507,11 @@ void SymGlobal (SymEntry* S, unsigned char AddrSize, unsigned Flags)
         }
         S->ExportSize = AddrSize;
         S->Flags |= (SF_GLOBAL | Flags);
+
+        /* Remember the current location as location of definition in case
+         * an .IMPORT follows later.
+         */
+        GetFullLineInfo (&S->DefLines);
     }
 }
 
@@ -505,12 +533,12 @@ void SymConDes (SymEntry* S, unsigned char AddrSize, unsigned Type, unsigned Pri
     /* Check for errors */
     if (S->Flags & SF_IMPORT) {
                /* The symbol is already marked as imported external symbol */
-               Error ("Symbol `%s' is already an import", GetSymName (S));
+               Error ("Symbol `%m%p' is already an import", GetSymName (S));
                return;
     }
     if (S->Flags & SF_VAR) {
         /* Variable symbols cannot be exported or imported */
-        Error ("Var symbol `%s' cannot be exported", GetSymName (S));
+        Error ("Var symbol `%m%p' cannot be exported", GetSymName (S));
         return;
     }
 
@@ -520,7 +548,7 @@ void SymConDes (SymEntry* S, unsigned char AddrSize, unsigned Type, unsigned Pri
      */
     if (S->Flags & (SF_EXPORT | SF_GLOBAL)) {
         if (S->ExportSize != AddrSize) {
-            Error ("Address size mismatch for symbol `%s'", GetSymName (S));
+            Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
         }
         S->Flags &= ~SF_GLOBAL;
     }
@@ -534,7 +562,7 @@ void SymConDes (SymEntry* S, unsigned char AddrSize, unsigned Type, unsigned Pri
             /* Use the real size of the symbol */
             S->ExportSize = S->AddrSize;
         } else if (S->AddrSize != S->ExportSize) {
-            Error ("Address size mismatch for symbol `%s'", GetSymName (S));
+            Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
         }
     }
 
@@ -543,13 +571,18 @@ void SymConDes (SymEntry* S, unsigned char AddrSize, unsigned Type, unsigned Pri
      */
     if (S->ConDesPrio[Type] != CD_PRIO_NONE) {
        if (S->ConDesPrio[Type] != Prio) {
-           Error ("Redeclaration mismatch for symbol `%s'", GetSymName (S));
+           Error ("Redeclaration mismatch for symbol `%m%p'", GetSymName (S));
        }
     }
     S->ConDesPrio[Type] = Prio;
 
     /* Set the symbol data */
     S->Flags |= (SF_EXPORT | SF_REFERENCED);
+
+    /* In case we have no line info for the definition, record it now */
+    if (CollCount (&S->DefLines) == 0) {
+        GetFullLineInfo (&S->DefLines);
+    }
 }
 
 
@@ -575,7 +608,7 @@ void SymGuessedAddrSize (SymEntry* Sym, unsigned char AddrSize)
     }
 
     /* Ok, remember the file position */
-    Sym->GuessedUse[AddrSize-1] = xdup (&CurPos, sizeof (CurPos));
+    Sym->GuessedUse[AddrSize-1] = xdup (&CurTok.Pos, sizeof (CurTok.Pos));
 }
 
 
@@ -604,7 +637,7 @@ void SymImportFromGlobal (SymEntry* S)
 
 
 
-int SymIsConst (SymEntry* S, long* Val)
+int SymIsConst (const SymEntry* S, long* Val)
 /* Return true if the given symbol has a constant value. If Val is not NULL
  * and the symbol has a constant value, store it's value there.
  */
@@ -620,7 +653,18 @@ SymTable* GetSymParentScope (SymEntry* S)
  * NULL if the symbol is a cheap local, or defined on global level.
  */
 {
-    return (S->SymTab && S->SymTab->Parent)? S->SymTab->Parent : 0;
+    if ((S->Flags & SF_LOCAL) != 0) {
+        /* This is a cheap local symbol */
+        return 0;
+    } else if (S->Sym.Tab == 0) {
+        /* Symbol not in a table. This may happen if there have been errors
+         * before. Return NULL in this case to avoid further errors.
+         */
+        return 0;
+    } else {
+        /* This is a global symbol */
+        return S->Sym.Tab->Parent;
+    }
 }
 
 
@@ -656,11 +700,45 @@ long GetSymVal (SymEntry* S)
 
 
 
-unsigned GetSymIndex (const SymEntry* S)
-/* Return the symbol index for the given symbol */
+unsigned GetSymImportId (const SymEntry* S)
+/* Return the import id for the given symbol */
+{
+    PRECONDITION (S != 0 && (S->Flags & SF_IMPORT) && S->ImportId != ~0U);
+    return S->ImportId;
+}
+
+
+
+unsigned GetSymExportId (const SymEntry* S)
+/* Return the export id for the given symbol */
+{
+    PRECONDITION (S != 0 && (S->Flags & SF_EXPORT) && S->ExportId != ~0U);
+    return S->ExportId;
+}
+
+
+
+unsigned GetSymInfoFlags (const SymEntry* S, long* ConstVal)
+/* Return a set of flags used when writing symbol information into a file.
+ * If the SYM_CONST bit is set, ConstVal will contain the constant value
+ * of the symbol. The result does not include the condes count.
+ * See common/symdefs.h for more information.
+ */
 {
-    PRECONDITION (S != 0 && (S->Flags & SF_INDEXED) != 0);
-    return S->Index;
+    /* Setup info flags */
+    unsigned Flags = 0;
+    Flags |= SymIsConst (S, ConstVal)? SYM_CONST : SYM_EXPR;
+    Flags |= (S->Flags & SF_LABEL)? SYM_LABEL : SYM_EQUATE;
+    Flags |= (S->Flags & SF_LOCAL)? SYM_CHEAP_LOCAL : SYM_STD;
+    if (S->Flags & SF_EXPORT) {
+        Flags |= SYM_EXPORT;
+    }
+    if (S->Flags & SF_IMPORT) {
+        Flags |= SYM_IMPORT;
+    }
+
+    /* Return the result */
+    return Flags;
 }