]> git.sur5r.net Git - cc65/blobdiff - src/ca65/symentry.c
Finished implemenation of commands to delete macros. Added the new commands to
[cc65] / src / ca65 / symentry.c
index 1f0420956568b5ede9b53f87f527c0424549dcac..763820731c954d9d1d785877882256a1dd8df706 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-2003 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,25 +73,33 @@ 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;
+
     /* Allocate memory */
     SymEntry* S = xmalloc (sizeof (SymEntry));
 
     /* Initialize the entry */
-    S->Left      = 0;
-    S->Right     = 0;
-    S->Locals    = 0;
-    S->SymTab    = 0;
-    S->Pos       = CurPos;
-    S->Flags     = Flags;
+    S->Left              = 0;
+    S->Right             = 0;
+    S->Locals            = 0;
+    S->Sym.Tab    = 0;
+    S->LineInfos  = EmptyCollection;
+    GetFullLineInfo (&S->LineInfos, 1);
+    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->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;
@@ -102,7 +111,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,
@@ -121,10 +130,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) {
@@ -174,19 +183,69 @@ void SymTransferExprRefs (SymEntry* From, SymEntry* To)
 
 
 
+static void SymReplaceExprRefs (SymEntry* S)
+/* Replace the references to this symbol by a copy of the symbol expression */
+{
+    unsigned I;
+    long     Val;
+
+    /* Check if the expression is const and get its value */
+    int IsConst = IsConstExpr (S->Expr, &Val);
+    CHECK (IsConst);
+
+    /* Loop over all references */
+    for (I = 0; I < CollCount (&S->ExprRefs); ++I) {
+
+        /* Get the expression node */
+        ExprNode* E = CollAtUnchecked (&S->ExprRefs, I);
+
+        /* Safety */
+        CHECK (E->Op == EXPR_SYMBOL && E->V.Sym == S);
+
+        /* We cannot touch the root node, since there are pointers to it.
+         * Replace it by a literal node.
+         */
+        E->Op = EXPR_LITERAL;
+        E->V.IVal = Val;
+    }
+
+    /* Remove all symbol references from the symbol */
+    CollDeleteAll (&S->ExprRefs);
+}
+
+
+
 void SymDef (SymEntry* S, ExprNode* Expr, unsigned char AddrSize, unsigned Flags)
 /* Define a new symbol */
 {
     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 `%m%p' cannot be an export or global symbol", GetSymName (S));
+        return;
+    }
     if (S->Flags & SF_DEFINED) {
-               /* Multiple definition */
-               Error ("Symbol `%s' is already defined", GetSymName (S));
-               S->Flags |= SF_MULTDEF;
-               return;
+               /* Multiple definition. In case of a variable, this is legal. */
+        if ((S->Flags & SF_VAR) == 0) {
+            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 `%m%p' is already different kind", GetSymName (S));
+                return;
+            }
+            /* Delete the current symbol expression, since it will get
+             * replaced
+             */
+            FreeExpr (S->Expr);
+            S->Expr = 0;
+        }
     }
 
     /* Map a default address size to a real value */
@@ -202,6 +261,15 @@ void SymDef (SymEntry* S, ExprNode* Expr, unsigned char AddrSize, unsigned Flags
     /* Set the symbol value */
     S->Expr = Expr;
 
+    /* In case of a variable symbol, walk over all expressions containing
+     * this symbol and replace the (sub-)expression by the literal value of
+     * the tree. Be sure to replace the expression node in place, since there
+     * may be pointers to it.
+     */
+    if (Flags & SF_VAR) {
+        SymReplaceExprRefs (S);
+    }
+
     /* If the symbol is marked as global, export it. Address size is checked
      * below.
      */
@@ -220,9 +288,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));
+            LIWarning (&S->LineInfos, 1, "Symbol `%m%p' is %s but exported %s",
+                       GetSymName (S), AddrSizeToStr (S->AddrSize),
+                       AddrSizeToStr (S->ExportSize));
         }
     }
 
@@ -238,13 +306,13 @@ 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;
     }
 
@@ -260,16 +328,16 @@ 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));
        }
     }
 
@@ -286,16 +354,21 @@ 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 `%m%p' cannot be exported", GetSymName (S));
+        return;
+    }
 
     /* If the symbol was marked as global before, remove the global flag and
      * proceed, but check the address size.
      */
     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;
     }
@@ -305,7 +378,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;
@@ -319,7 +392,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));
         }
@@ -336,6 +409,12 @@ void SymGlobal (SymEntry* S, unsigned char AddrSize, unsigned Flags)
  * either imported or exported.
  */
 {
+    if (S->Flags & SF_VAR) {
+        /* Variable symbols cannot be exported or imported */
+        Error ("Var symbol `%m%p' cannot be made global", GetSymName (S));
+        return;
+    }
+
     /* If the symbol is already marked as import, the address size must match.
      * Apart from that, ignore the global declaration.
      */
@@ -345,7 +424,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;
     }
@@ -357,12 +436,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;
@@ -374,7 +453,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;
     }
@@ -392,7 +471,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));
         }
@@ -430,9 +509,14 @@ 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 `%m%p' cannot be exported", GetSymName (S));
+        return;
+    }
 
     /* If the symbol was already marked as an export or global, check if
      * this was done specifiying the same address size. In case of a global
@@ -440,7 +524,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;
     }
@@ -454,7 +538,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));
         }
     }
 
@@ -463,7 +547,7 @@ 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;
@@ -474,7 +558,57 @@ void SymConDes (SymEntry* S, unsigned char AddrSize, unsigned Type, unsigned Pri
 
 
 
-int SymIsConst (SymEntry* S, long* Val)
+void SymGuessedAddrSize (SymEntry* Sym, unsigned char AddrSize)
+/* Mark the address size of the given symbol as guessed. The address size
+ * passed as argument is the one NOT used, because the actual address size
+ * wasn't known. Example: Zero page addressing was not used because symbol
+ * is undefined, and absolute addressing was available.
+ */
+{
+    /* We must have a valid address size passed */
+    PRECONDITION (AddrSize != ADDR_SIZE_DEFAULT);
+
+    /* We do not support all address sizes currently */
+    if (AddrSize > sizeof (Sym->GuessedUse) / sizeof (Sym->GuessedUse[0])) {
+        return;
+    }
+
+    /* We can only remember one such occurance */
+    if (Sym->GuessedUse[AddrSize-1]) {
+        return;
+    }
+
+    /* Ok, remember the file position */
+    Sym->GuessedUse[AddrSize-1] = xdup (&CurTok.Pos, sizeof (CurTok.Pos));
+}
+
+
+
+void SymExportFromGlobal (SymEntry* S)
+/* Called at the end of assembly. Converts a global symbol that is defined
+ * into an export.
+ */
+{
+    /* Remove the global flag and make the symbol an export */
+    S->Flags &= ~SF_GLOBAL;
+    S->Flags |= SF_EXPORT;
+}
+
+
+
+void SymImportFromGlobal (SymEntry* S)
+/* Called at the end of assembly. Converts a global symbol that is undefined
+ * into an import.
+ */
+{
+    /* Remove the global flag and make it an import */
+    S->Flags &= ~SF_GLOBAL;
+    S->Flags |= SF_IMPORT;
+}
+
+
+
+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.
  */
@@ -490,7 +624,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;
+    }
 }
 
 
@@ -526,11 +671,39 @@ 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 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.
+ */
+{
+    /* 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;
+
+    /* Return the result */
+    return Flags;
+}
+
+
+
+const FilePos* GetSymPos (const SymEntry* S)
+/* Return the position of first occurence in the source for the given symbol */
 {
-    PRECONDITION (S != 0 && (S->Flags & SF_INDEXED) != 0);
-    return S->Index;
+    /* The actual source entry is in slot zero */
+    return &((const LineInfo*) CollConstAt (&S->LineInfos, 0))->Pos;
 }