]> git.sur5r.net Git - cc65/blobdiff - src/ca65/symtab.c
More lineinfo usage.
[cc65] / src / ca65 / symtab.c
index 615e1086a36289216a432b5ee448098096ccb1ce..661c05f6d9dd54d0402fdbe4070b39fe8aed46d7 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2008 Ullrich von Bassewitz                                       */
-/*               Roemerstrasse 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       */
 #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 */
 
 
 
@@ -124,6 +125,18 @@ static SymTable* NewSymTable (SymTable* Parent, const StrBuf* Name)
                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;
@@ -315,6 +328,7 @@ SymEntry* SymFindLocal (SymEntry* Parent, const StrBuf* 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) {
@@ -354,6 +368,7 @@ SymEntry* SymFind (SymTable* Scope, const StrBuf* 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) {
@@ -361,7 +376,6 @@ SymEntry* SymFind (SymTable* Scope, const StrBuf* Name, int AllocNew)
         } else {
             S->Right = N;
         }
-        N->SymTab = Scope;
         ++Scope->TableEntries;
         return N;
 
@@ -444,14 +458,16 @@ 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 `%m%p' is %s but exported %s",
+                    LIWarning (&S->LineInfos, 1,
+                               "Symbol `%m%p' is %s but exported %s",
                               GetSymName (Sym),
                               AddrSizeToStr (Sym->AddrSize),
                               AddrSizeToStr (S->ExportSize));
@@ -466,10 +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 `%m%p' 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));
                 }
             }
         }
@@ -485,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 `%m%p' was never defined",
-                    GetSymName (S));
+           LIError (&S->LineInfos, 
+                     "Exported symbol `%m%p' was never defined",
+                     GetSymName (S));
        } else {
            if (AutoImport) {
                /* Mark as import, will be indexed later */
@@ -495,7 +513,9 @@ static void SymCheckUndefined (SymEntry* S)
                 S->AddrSize = CodeAddrSize;
            } else {
                /* Error */
-               PError (&S->Pos, "Symbol `%m%p' is undefined", GetSymName (S));
+               LIError (&S->LineInfos, 
+                         "Symbol `%m%p' is undefined", 
+                         GetSymName (S));
            }
        }
     }
@@ -553,9 +573,9 @@ void SymCheck (void)
            if ((S->Flags & SF_DEFINED) != 0 && (S->Flags & SF_REFERENCED) == 0) {
                 const StrBuf* Name = GetStrBuf (S->Name);
                 if (SB_At (Name, 0) != '.') {           /* Ignore internals */
-                    PWarning (&S->Pos, 2,
-                              "Symbol `%m%p' is defined but never used",
-                              GetSymName (S));
+                    LIWarning (&S->LineInfos, 2,
+                               "Symbol `%m%p' is defined but never used",
+                               GetSymName (S));
                 }
            }
 
@@ -563,21 +583,18 @@ void SymCheck (void)
            if (S->Flags & SF_IMPORT) {
                if ((S->Flags & (SF_REFERENCED | SF_FORCED)) == SF_NONE) {
                    /* Imported symbol is not referenced */
-                   PWarning (&S->Pos, 2,
-                              "Symbol `%m%p' is imported but never used",
-                              GetSymName (S));
+                   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++;
                }
            }
 
-            /* Assign an index to all exports */
+            /* 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,
@@ -594,11 +611,11 @@ void SymCheck (void)
                         S->ExportSize = S->AddrSize;
                     } else if (S->AddrSize > S->ExportSize) {
                         /* We're exporting a symbol smaller than it actually is */
-                        PWarning (&S->Pos, 1,
-                                  "Symbol `%m%p' 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));
                     }
                 }
                 ED_Done (&ED);
@@ -678,7 +695,7 @@ void WriteImports (void)
 
             ObjWrite8 (S->AddrSize);
                    ObjWriteVar (S->Name);
-           ObjWritePos (&S->Pos);
+           WriteLineInfo (&S->LineInfos);
        }
        S = S->List;
     }
@@ -706,38 +723,36 @@ 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 {
@@ -745,8 +760,8 @@ void WriteExports (void)
                WriteExpr (S->Expr);
             }
 
-           /* Write the source file position */
-           ObjWritePos (&S->Pos);
+           /* Write the line infos */
+           WriteLineInfo (&S->LineInfos);
        }
        S = S->List;
     }
@@ -769,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;
        }
@@ -787,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);
@@ -803,7 +816,7 @@ 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 {
@@ -811,8 +824,8 @@ void WriteDbgSyms (void)
                    WriteExpr (S->Expr);
                }
 
-               /* Write the source file position */
-               ObjWritePos (&S->Pos);
+               /* Write the line infos */
+               WriteLineInfo (&S->LineInfos);
            }
            S = S->List;
        }
@@ -836,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 */
@@ -845,5 +858,3 @@ void WriteScopes (void)
 
 
 
-
-