]> git.sur5r.net Git - cc65/blobdiff - src/ca65/symtab.c
More lineinfo usage.
[cc65] / src / ca65 / symtab.c
index 32c549e7a89d594d21ba11fdae1d5776eaa622ec..661c05f6d9dd54d0402fdbe4070b39fe8aed46d7 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (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       */
@@ -52,6 +52,7 @@
 #include "segment.h"
 #include "sizeof.h"
 #include "spool.h"
+#include "studyexpr.h"
 #include "symtab.h"
 
 
 /* Combined symbol entry flags used within this module */
 #define SF_UNDEFMASK   (SF_REFERENCED | SF_DEFINED | SF_IMPORT)
 #define SF_UNDEFVAL    (SF_REFERENCED)
-#define SF_DBGINFOMASK         (SF_UNUSED | SF_DEFINED | SF_EXPORT | SF_IMPORT)
+#define SF_DBGINFOMASK         (SF_UNUSED | SF_DEFINED | SF_IMPORT)
 #define SF_DBGINFOVAL  (SF_DEFINED)
 
 /* Symbol tables */
-SymTable*              CurrentScope = 0;       /* Pointer to current symbol table */
-SymTable*      RootScope    = 0;       /* Root symbol table */
+SymTable*                  CurrentScope = 0;   /* Pointer to current symbol table */
+SymTable*          RootScope    = 0;   /* Root symbol table */
+static SymTable*    LastScope    = 0;   /* Pointer to last scope in list */
 
 /* Symbol table variables */
-static unsigned ImportCount = 0;        /* Counter for import symbols */
-static unsigned ExportCount = 0;        /* Counter for export symbols */
+static unsigned     ImportCount = 0;    /* Counter for import symbols */
+static unsigned     ExportCount = 0;    /* Counter for export symbols */
 
 
 
@@ -96,7 +98,7 @@ static unsigned ScopeTableSize (unsigned Level)
 
 
 
-static SymTable* NewSymTable (SymTable* Parent, const char* Name)
+static SymTable* NewSymTable (SymTable* Parent, const StrBuf* Name)
 /* Allocate a symbol table on the heap and return it */
 {
     /* Determine the lexical level and the number of table slots */
@@ -118,11 +120,23 @@ static SymTable* NewSymTable (SymTable* Parent, const char* Name)
     S->TableSlots   = Slots;
     S->TableEntries = 0;
     S->Parent       = Parent;
-    S->Name         = GetStringId (Name);
+    S->Name         = GetStrBufId (Name);
     while (Slots--) {
                S->Table[Slots] = 0;
     }
 
+    /* Insert the symbol table into the list of all symbol tables and maintain
+     * a unqiue id for each scope.
+     */
+    S->Next = LastScope;
+    if (RootScope == 0) {
+        S->Id = 0;
+        RootScope = S;
+    } else {
+        S->Id = LastScope->Id + 1;
+    }
+    LastScope = S;
+
     /* Insert the symbol table into the child tree of the parent */
     if (Parent) {
         SymTable* T = Parent->Childs;
@@ -132,7 +146,7 @@ static SymTable* NewSymTable (SymTable* Parent, const char* Name)
         } else {
             while (1) {
                 /* Choose next entry */
-                int Cmp = strcmp (Name, GetString (T->Name));
+                int Cmp = SB_Compare (Name, GetStrBuf (T->Name));
                 if (Cmp < 0) {
                     if (T->Left) {
                         T = T->Left;
@@ -149,7 +163,7 @@ static SymTable* NewSymTable (SymTable* Parent, const char* Name)
                     }
                 } else {
                     /* Duplicate scope name */
-                    Internal ("Duplicate scope name: `%s'", Name);
+                    Internal ("Duplicate scope name: `%m%p'", Name);
                 }
             }
         }
@@ -162,12 +176,12 @@ static SymTable* NewSymTable (SymTable* Parent, const char* Name)
 
 
 /*****************************************************************************/
-/*                                          Code                                    */
+/*                                          Code                                    */
 /*****************************************************************************/
 
 
 
-void SymEnterLevel (const char* ScopeName, unsigned char Type, unsigned char AddrSize)
+void SymEnterLevel (const StrBuf* ScopeName, unsigned char Type, unsigned char AddrSize)
 /* Enter a new lexical level */
 {
     /* Map a default address size to something real */
@@ -186,7 +200,7 @@ void SymEnterLevel (const char* ScopeName, unsigned char Type, unsigned char Add
 
         /* Check if the scope has been defined before */
         if (CurrentScope->Flags & ST_DEFINED) {
-            Error ("Duplicate scope `%s'", ScopeName);
+            Error ("Duplicate scope `%m%p'", ScopeName);
         }
 
     } else {
@@ -234,12 +248,12 @@ void SymLeaveLevel (void)
 
 
 
-SymTable* SymFindScope (SymTable* Parent, const char* Name, int AllocNew)
+SymTable* SymFindScope (SymTable* Parent, const StrBuf* Name, int AllocNew)
 /* Find a scope in the given enclosing scope */
 {
     SymTable** T = &Parent->Childs;
     while (*T) {
-        int Cmp = strcmp (Name, GetString ((*T)->Name));
+        int Cmp = SB_Compare (Name, GetStrBuf ((*T)->Name));
         if (Cmp < 0) {
             T = &(*T)->Left;
         } else if (Cmp > 0) {
@@ -261,7 +275,7 @@ SymTable* SymFindScope (SymTable* Parent, const char* Name, int AllocNew)
 
 
 
-SymTable* SymFindAnyScope (SymTable* Parent, const char* Name)
+SymTable* SymFindAnyScope (SymTable* Parent, const StrBuf* Name)
 /* Find a scope in the given or any of its parent scopes. The function will
  * never create a new symbol, since this can only be done in one specific
  * scope.
@@ -282,7 +296,7 @@ SymTable* SymFindAnyScope (SymTable* Parent, const char* Name)
 
 
 
-SymEntry* SymFindLocal (SymEntry* Parent, const char* Name, int AllocNew)
+SymEntry* SymFindLocal (SymEntry* Parent, const StrBuf* Name, int AllocNew)
 /* Find a cheap local symbol. If AllocNew is given and the entry is not
  * found, create a new one. Return the entry found, or the new entry created,
  * or - in case AllocNew is zero - return 0.
@@ -314,6 +328,7 @@ SymEntry* SymFindLocal (SymEntry* Parent, const char* Name, int AllocNew)
 
         /* Otherwise create a new entry, insert and return it */
         SymEntry* N = NewSymEntry (Name, SF_LOCAL);
+        N->Sym.Entry = Parent;
         if (S == 0) {
             Parent->Locals = N;
         } else if (Cmp < 0) {
@@ -330,7 +345,7 @@ SymEntry* SymFindLocal (SymEntry* Parent, const char* Name, int AllocNew)
 
 
 
-SymEntry* SymFind (SymTable* Scope, const char* Name, int AllocNew)
+SymEntry* SymFind (SymTable* Scope, const StrBuf* Name, int AllocNew)
 /* Find a new symbol table entry in the given table. If AllocNew is given and
  * the entry is not found, create a new one. Return the entry found, or the
  * new entry created, or - in case AllocNew is zero - return 0.
@@ -339,7 +354,7 @@ SymEntry* SymFind (SymTable* Scope, const char* Name, int AllocNew)
     SymEntry* S;
 
     /* Global symbol: Get the hash value for the name */
-    unsigned Hash = HashStr (Name) % Scope->TableSlots;
+    unsigned Hash = HashBuf (Name) % Scope->TableSlots;
 
     /* Search for the entry */
     int Cmp = SymSearchTree (Scope->Table[Hash], Name, &S);
@@ -353,6 +368,7 @@ SymEntry* SymFind (SymTable* Scope, const char* Name, int AllocNew)
 
         /* Otherwise create a new entry, insert and return it */
         SymEntry* N = NewSymEntry (Name, SF_NONE);
+        N->Sym.Tab = Scope;
         if (S == 0) {
             Scope->Table[Hash] = N;
         } else if (Cmp < 0) {
@@ -360,7 +376,6 @@ SymEntry* SymFind (SymTable* Scope, const char* Name, int AllocNew)
         } else {
             S->Right = N;
         }
-        N->SymTab = Scope;
         ++Scope->TableEntries;
         return N;
 
@@ -372,7 +387,7 @@ SymEntry* SymFind (SymTable* Scope, const char* Name, int AllocNew)
 
 
 
-SymEntry* SymFindAny (SymTable* Scope, const char* Name)
+SymEntry* SymFindAny (SymTable* Scope, const StrBuf* Name)
 /* Find a symbol in the given or any of its parent scopes. The function will
  * never create a new symbol, since this can only be done in one specific
  * scope.
@@ -384,15 +399,16 @@ SymEntry* SymFindAny (SymTable* Scope, const char* Name)
        Sym = SymFind (Scope, Name, SYM_FIND_EXISTING);
                if (Sym) {
            /* Found, return it */
-           return Sym;
-       } else {
-           /* Not found, search in the parent scope, if we have one */
-           Scope = Scope->Parent;
+           break;
        }
+
+        /* Not found, search in the parent scope, if we have one */
+       Scope = Scope->Parent;
+
     } while (Sym == 0 && Scope != 0);
 
-    /* Not found */
-    return 0;
+    /* Return the result */
+    return Sym;
 }
 
 
@@ -420,26 +436,17 @@ static void SymCheckUndefined (SymEntry* S)
      *     AutoImport flag is not set, it's an error.
      */
     SymEntry* Sym = 0;
-    if (S->SymTab) {
-       /* It's a global symbol, get the higher level table */
-       SymTable* Tab = S->SymTab->Parent;
-       while (Tab) {
-           Sym = SymFindAny (Tab, GetString (S->Name));
-           if (Sym) {
-               if (Sym->Flags & (SF_DEFINED | SF_IMPORT)) {
-                   /* We've found a symbol in a higher level that is
-                    * either defined in the source, or an import.
-                    */
-                    break;
-               } else {
-                   /* The symbol found is undefined itself. Look further */
-                   Tab = Sym->SymTab->Parent;
-               }
-           } else {
-               /* No symbol found */
-               break;
-           }
-       }
+    SymTable* Tab = GetSymParentScope (S);
+    while (Tab) {
+        Sym = SymFind (Tab, GetStrBuf (S->Name), SYM_FIND_EXISTING);
+        if (Sym && (Sym->Flags & (SF_DEFINED | SF_IMPORT)) != 0) {
+            /* We've found a symbol in a higher level that is
+             * either defined in the source, or an import.
+             */
+             break;
+        }
+        /* No matching symbol found in this level. Look further */
+        Tab = Tab->Parent;
     }
 
     if (Sym) {
@@ -451,15 +458,18 @@ static void SymCheckUndefined (SymEntry* S)
         if (S->Flags & SF_EXPORT) {
            if (Sym->Flags & SF_IMPORT) {
                /* The symbol is already marked as import */
-               PError (&S->Pos, "Symbol `%s' is already an import",
-                        GetString (Sym->Name));
+                       LIError (&S->LineInfos,
+                         "Symbol `%s' is already an import",
+                         GetString (Sym->Name));
            }
             if (Sym->Flags & SF_EXPORT) {
                 /* The symbol is already marked as an export. */
                 if (Sym->AddrSize > S->ExportSize) {
                     /* We're exporting a symbol smaller than it actually is */
-                    PWarning (&S->Pos, 1, "Symbol `%s' is %s but exported %s",
-                              GetSymName (Sym), AddrSizeToStr (Sym->AddrSize),
+                    LIWarning (&S->LineInfos, 1,
+                               "Symbol `%m%p' is %s but exported %s",
+                              GetSymName (Sym),
+                              AddrSizeToStr (Sym->AddrSize),
                               AddrSizeToStr (S->ExportSize));
                 }
             } else {
@@ -472,9 +482,11 @@ static void SymCheckUndefined (SymEntry* S)
                 }
                 if (Sym->AddrSize > Sym->ExportSize) {
                     /* We're exporting a symbol smaller than it actually is */
-                    PWarning (&S->Pos, 1, "Symbol `%s' is %s but exported %s",
-                              GetSymName (Sym), AddrSizeToStr (Sym->AddrSize),
-                              AddrSizeToStr (Sym->ExportSize));
+                    LIWarning (&S->LineInfos, 1,
+                               "Symbol `%m%p' is %s but exported %s",
+                               GetSymName (Sym),
+                               AddrSizeToStr (Sym->AddrSize),
+                               AddrSizeToStr (Sym->ExportSize));
                 }
             }
         }
@@ -490,8 +502,9 @@ static void SymCheckUndefined (SymEntry* S)
        /* The symbol is definitely undefined */
        if (S->Flags & SF_EXPORT) {
            /* We will not auto-import an export */
-           PError (&S->Pos, "Exported symbol `%s' was never defined",
-                    GetString (S->Name));
+           LIError (&S->LineInfos, 
+                     "Exported symbol `%m%p' was never defined",
+                     GetSymName (S));
        } else {
            if (AutoImport) {
                /* Mark as import, will be indexed later */
@@ -500,7 +513,9 @@ static void SymCheckUndefined (SymEntry* S)
                 S->AddrSize = CodeAddrSize;
            } else {
                /* Error */
-               PError (&S->Pos, "Symbol `%s' is undefined", GetString (S->Name));
+               LIError (&S->LineInfos, 
+                         "Symbol `%m%p' is undefined", 
+                         GetSymName (S));
            }
        }
     }
@@ -527,11 +542,10 @@ void SymCheck (void)
         * already defined, otherwise mark it as import.
         */
        if (S->Flags & SF_GLOBAL) {
-           S->Flags &= ~SF_GLOBAL;
            if (S->Flags & SF_DEFINED) {
-               S->Flags |= SF_EXPORT;
+               SymExportFromGlobal (S);
            } else {
-               S->Flags |= SF_IMPORT;
+               SymImportFromGlobal (S);
            }
        }
 
@@ -545,37 +559,86 @@ void SymCheck (void)
        S = S->List;
     }
 
-    /* Second pass: Walk again through the symbols. Ignore undefined's, since
-     * we handled them in the last pass, and ignore unused symbols, since
-     * we handled them in the last pass, too.
+    /* Second pass: Walk again through the symbols. Count exports and imports
+     * and set address sizes where this has not happened before. Ignore
+     * undefined's, since we handled them in the last pass, and ignore unused
+     * symbols, since we handled them in the last pass, too.
      */
     S = SymList;
     while (S) {
        if ((S->Flags & SF_UNUSED) == 0 &&
            (S->Flags & SF_UNDEFMASK) != SF_UNDEFVAL) {
+
+            /* Check for defined symbols that were never referenced */
            if ((S->Flags & SF_DEFINED) != 0 && (S->Flags & SF_REFERENCED) == 0) {
-               /* Symbol was defined but never referenced */
-               PWarning (&S->Pos, 2,
-                          "Symbol `%s' is defined but never used",
-                          GetString (S->Name));
+                const StrBuf* Name = GetStrBuf (S->Name);
+                if (SB_At (Name, 0) != '.') {           /* Ignore internals */
+                    LIWarning (&S->LineInfos, 2,
+                               "Symbol `%m%p' is defined but never used",
+                               GetSymName (S));
+                }
            }
+
+            /* Assign an index to all imports */
            if (S->Flags & SF_IMPORT) {
                if ((S->Flags & (SF_REFERENCED | SF_FORCED)) == SF_NONE) {
                    /* Imported symbol is not referenced */
-                   PWarning (&S->Pos, 2,
-                              "Symbol `%s' is imported but never used",
-                              GetString (S->Name));
+                   LIWarning (&S->LineInfos, 2,
+                               "Symbol `%m%p' is imported but never used",
+                               GetSymName (S));
                } else {
-                   /* Give the import an index, count imports */
-                   S->Index = ImportCount++;
-                   S->Flags |= SF_INDEXED;
+                   /* Give the import an id, count imports */
+                   S->ImportId = ImportCount++;
                }
            }
+
+            /* Count exports */
            if (S->Flags & SF_EXPORT) {
-               /* Give the export an index, count exports */
-               S->Index = ExportCount++;
-               S->Flags |= SF_INDEXED;
+               ++ExportCount;
            }
+
+            /* If the symbol is defined but has an unknown address size,
+             * recalculate it.
+             */
+            if (SymHasExpr (S) && S->AddrSize == ADDR_SIZE_DEFAULT) {
+                ExprDesc ED;
+                ED_Init (&ED);
+                StudyExpr (S->Expr, &ED);
+                S->AddrSize = ED.AddrSize;
+                if (SymIsExport (S)) {
+                    if (S->ExportSize == ADDR_SIZE_DEFAULT) {
+                        /* Use the real export size */
+                        S->ExportSize = S->AddrSize;
+                    } else if (S->AddrSize > S->ExportSize) {
+                        /* We're exporting a symbol smaller than it actually is */
+                        LIWarning (&S->LineInfos, 1,
+                                   "Symbol `%m%p' is %s but exported %s",
+                                   GetSymName (S),
+                                   AddrSizeToStr (S->AddrSize),
+                                   AddrSizeToStr (S->ExportSize));
+                    }
+                }
+                ED_Done (&ED);
+            }
+
+            /* If the address size of the symbol was guessed, check the guess
+             * against the actual address size and print a warning if the two
+             * differ.
+             */
+            if (S->AddrSize != ADDR_SIZE_DEFAULT) {
+                /* Do we have data for this address size? */
+                if (S->AddrSize <= sizeof (S->GuessedUse) / sizeof (S->GuessedUse[0])) {
+                    /* Get the file position where the symbol was used */
+                    const FilePos* P = S->GuessedUse[S->AddrSize - 1];
+                    if (P) {
+                        PWarning (P, 0,
+                                  "Didn't use %s addressing for `%m%p'",
+                                  AddrSizeToStr (S->AddrSize),
+                                  GetSymName (S));
+                    }
+                }
+            }
+
        }
 
        /* Next symbol */
@@ -594,8 +657,8 @@ void SymDump (FILE* F)
        /* Ignore unused symbols */
        if ((S->Flags & SF_UNUSED) != 0) {
            fprintf (F,
-                    "%-24s %s %s %s %s %s\n",
-                    GetString (S->Name),
+                    "%m%-24p %s %s %s %s %s\n",
+                    GetSymName (S),
                     (S->Flags & SF_DEFINED)? "DEF" : "---",
                     (S->Flags & SF_REFERENCED)? "REF" : "---",
                     (S->Flags & SF_IMPORT)? "IMP" : "---",
@@ -632,7 +695,7 @@ void WriteImports (void)
 
             ObjWrite8 (S->AddrSize);
                    ObjWriteVar (S->Name);
-           ObjWritePos (&S->Pos);
+           WriteLineInfo (&S->LineInfos);
        }
        S = S->List;
     }
@@ -660,47 +723,45 @@ void WriteExports (void)
     while (S) {
                if ((S->Flags & (SF_UNUSED | SF_EXPORT)) == SF_EXPORT) {
 
+           /* Get the expression bits and the value */
             long ConstVal;
-
-           /* Get the expression bits */
-            unsigned char ExprMask = SymIsConst (S, &ConstVal)? EXP_CONST : EXP_EXPR;
-            ExprMask |= (S->Flags & SF_LABEL)? EXP_LABEL : EXP_EQUATE;
+            unsigned ExprMask = GetSymInfoFlags (S, &ConstVal);
 
            /* Count the number of ConDes types */
            for (Type = 0; Type < CD_TYPE_COUNT; ++Type) {
-               if (S->ConDesPrio[Type] != CD_PRIO_NONE) {
-                   INC_EXP_CONDES_COUNT (ExprMask);
-               }
+               if (S->ConDesPrio[Type] != CD_PRIO_NONE) {
+                   SYM_INC_CONDES_COUNT (ExprMask);
+               }
            }
 
            /* Write the type and the export size */
-           ObjWrite8 (ExprMask);
+           ObjWriteVar (ExprMask);
             ObjWrite8 (S->ExportSize);
 
            /* Write any ConDes declarations */
-           if (GET_EXP_CONDES_COUNT (ExprMask) > 0) {
-               for (Type = 0; Type < CD_TYPE_COUNT; ++Type) {
-                   unsigned char Prio = S->ConDesPrio[Type];
-                   if (Prio != CD_PRIO_NONE) {
-                       ObjWrite8 (CD_BUILD (Type, Prio));
-                   }
-               }
+           if (SYM_GET_CONDES_COUNT (ExprMask) > 0) {
+               for (Type = 0; Type < CD_TYPE_COUNT; ++Type) {
+                   unsigned char Prio = S->ConDesPrio[Type];
+                   if (Prio != CD_PRIO_NONE) {
+                       ObjWrite8 (CD_BUILD (Type, Prio));
+                   }
+               }
            }
 
            /* Write the name */
                    ObjWriteVar (S->Name);
 
            /* Write the value */
-           if ((ExprMask & EXP_MASK_VAL) == EXP_CONST) {
+           if (SYM_IS_CONST (ExprMask)) {
                /* Constant value */
                ObjWrite32 (ConstVal);
            } else {
                /* Expression involved */
-               WriteExpr (S->V.Expr);
+               WriteExpr (S->Expr);
             }
 
-           /* Write the source file position */
-           ObjWritePos (&S->Pos);
+           /* Write the line infos */
+           WriteLineInfo (&S->LineInfos);
        }
        S = S->List;
     }
@@ -723,12 +784,12 @@ void WriteDbgSyms (void)
     /* Check if debug info is requested */
     if (DbgSyms) {
 
-       /* Walk through the list and count the symbols */
+       /* Walk through the list, give each symbol an id and count them */
        Count = 0;
        S = SymList;
        while (S) {
            if ((S->Flags & SF_DBGINFOMASK) == SF_DBGINFOVAL) {
-               ++Count;
+                S->DebugSymId = Count++;
            }
            S = S->List;
        }
@@ -741,14 +802,12 @@ void WriteDbgSyms (void)
        while (S) {
            if ((S->Flags & SF_DBGINFOMASK) == SF_DBGINFOVAL) {
 
+                /* Get the expression bits and the value */
                 long ConstVal;
-
-               /* Get the expression bits */
-                unsigned char ExprMask = (SymIsConst (S, &ConstVal))? EXP_CONST : EXP_EXPR;
-                ExprMask |= (S->Flags & SF_LABEL)? EXP_LABEL : EXP_EQUATE;
+                unsigned ExprMask = GetSymInfoFlags (S, &ConstVal);
 
                /* Write the type */
-               ObjWrite8 (ExprMask);
+               ObjWriteVar (ExprMask);
 
                 /* Write the address size */
                 ObjWrite8 (S->AddrSize);
@@ -757,16 +816,16 @@ void WriteDbgSyms (void)
                        ObjWriteVar (S->Name);
 
                /* Write the value */
-               if ((ExprMask & EXP_MASK_VAL) == EXP_CONST) {
+               if (SYM_IS_CONST (ExprMask)) {
                    /* Constant value */
                    ObjWrite32 (ConstVal);
                } else {
                    /* Expression involved */
-                   WriteExpr (S->V.Expr);
+                   WriteExpr (S->Expr);
                }
 
-               /* Write the source file position */
-               ObjWritePos (&S->Pos);
+               /* Write the line infos */
+               WriteLineInfo (&S->LineInfos);
            }
            S = S->List;
        }
@@ -790,7 +849,7 @@ void WriteScopes (void)
     /* Tell the object file module that we're about to start the scopes */
     ObjStartScopes ();
 
-    /* For now ...*/
+    /* No debug info requested */
     ObjWriteVar (0);
 
     /* Done writing the scopes */