]> git.sur5r.net Git - cc65/blobdiff - src/cc65/symtab.c
Stack adjustment code optimizations.
[cc65] / src / cc65 / symtab.c
index f0b5fefe20037259c1183b64964f7cc1148813ee..8db6a741c8c1ad547c89e5a96e1225d78557fc4f 100644 (file)
@@ -57,6 +57,8 @@
 #include "symentry.h"
 #include "typecmp.h"
 #include "symtab.h"
+#include "function.h"
+#include "input.h"
 
 
 
@@ -90,7 +92,7 @@ static SymTable*        SymTab          = 0;
 static SymTable*        TagTab0         = 0;
 static SymTable*        TagTab          = 0;
 static SymTable*        LabelTab        = 0;
-
+static SymTable*        SPAdjustTab     = 0;
 
 
 /*****************************************************************************/
@@ -159,8 +161,8 @@ static void CheckSymTable (SymTable* Tab)
         if (!SymIsTypeDef (Entry)) {
 
             /* Check if the symbol is one with storage, and it if it was
-             * defined but not used.
-             */
+            ** defined but not used.
+            */
             if (((Flags & SC_AUTO) || (Flags & SC_STATIC)) && (Flags & SC_EXTERN) == 0) {
                 if (SymIsDef (Entry) && !SymIsRef (Entry) &&
                     !SymHasAttr (Entry, atUnused)) {
@@ -223,6 +225,8 @@ void EnterGlobalLevel (void)
 
     /* Create and assign the tag table */
     TagTab0 = TagTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
+
+    SPAdjustTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
 }
 
 
@@ -268,7 +272,9 @@ void EnterFunctionLevel (void)
     TagTab  = S;
 
     /* Create and assign a new label table */
-    LabelTab = NewSymTable (SYMTAB_SIZE_LABEL);
+    S = NewSymTable (SYMTAB_SIZE_LABEL);
+    S->PrevTab = LabelTab;
+    LabelTab = S;
 }
 
 
@@ -286,6 +292,7 @@ void RememberFunctionLevel (struct FuncDesc* F)
     /* Don't delete the tables */
     SymTab = SymTab->PrevTab;
     TagTab = TagTab->PrevTab;
+    LabelTab = LabelTab->PrevTab;
 }
 
 
@@ -375,9 +382,9 @@ void EnterStructLevel (void)
     SymTable* S;
 
     /* Get a new symbol table and make it current. Note: Structs and enums
-     * nested in struct scope are NOT local to the struct but visible in the
-     * outside scope. So we will NOT create a new struct or enum table.
-     */
+    ** nested in struct scope are NOT local to the struct but visible in the
+    ** outside scope. So we will NOT create a new struct or enum table.
+    */
     S = NewSymTable (SYMTAB_SIZE_BLOCK);
     S->PrevTab  = SymTab;
     SymTab      = S;
@@ -400,7 +407,7 @@ void LeaveStructLevel (void)
 
 
 
-static SymEntry* FindSymInTable (const SymTable* T, const char* Name, unsigned Hash)
+SymEntry* FindSymInTable (const SymTable* T, const char* Name, unsigned Hash)
 /* Search for an entry in one table */
 {
     /* Get the start of the hash chain */
@@ -497,8 +504,8 @@ SymEntry* FindStructField (const Type* T, const char* Name)
         CHECK (Struct != 0);
 
         /* Now search in the struct symbol table. Beware: The table may not
-         * exist.
-         */
+        ** exist.
+        */
         if (Struct->V.S.SymTab) {
             Field = FindSymInTable (Struct->V.S.SymTab, Name, HashStr (Name));
         }
@@ -655,10 +662,45 @@ SymEntry* AddConstSym (const char* Name, const Type* T, unsigned Flags, long Val
 }
 
 
+DefOrRef* AddDefOrRef (SymEntry* E, unsigned Flags)
+/* Add definition or reference to the SymEntry and preserve its attributes */
+{
+    DefOrRef *DOR;
+
+    DOR = xmalloc (sizeof (DefOrRef));
+    CollAppend (E->V.L.DefsOrRefs, DOR);
+    DOR->Line = GetCurrentLine ();
+    DOR->LocalsBlockId = (long)CollLast (&CurrentFunc->LocalsBlockStack);
+    DOR->Flags = Flags;
+    DOR->StackPtr = StackPtr;
+    DOR->Depth = CollCount (&CurrentFunc->LocalsBlockStack);
+    DOR->LateSP_Label = GetLocalLabel ();
+
+    return DOR;
+}
+
+unsigned short FindSPAdjustment (const char* Name)
+{
+    SymEntry* Entry = FindSymInTable (SPAdjustTab, Name, HashStr (Name));
+
+    if (Entry) {
+            printf("L: %s sa: %d\n", Name, Entry->V.G.SPAdjustment);
+            return Entry->V.G.SPAdjustment;
+    }
+
+    Fatal("ICE: No label entry found");
+
+    return 0;
+}
 
 SymEntry* AddLabelSym (const char* Name, unsigned Flags)
 /* Add a goto label to the label table */
 {
+    unsigned i;
+    DefOrRef *DOR, *NewDOR;
+    /* We juggle it so much that a shortcut will help with clarity */
+    Collection *AIC = &CurrentFunc->LocalsBlockStack;
+
     /* Do we have an entry with this name already? */
     SymEntry* Entry = FindSymInTable (LabelTab, Name, HashStr (Name));
     if (Entry) {
@@ -667,6 +709,63 @@ SymEntry* AddLabelSym (const char* Name, unsigned Flags)
             /* Trying to define the label more than once */
             Error ("Label `%s' is defined more than once", Name);
         }
+
+        NewDOR = AddDefOrRef (Entry, Flags);
+
+        /* Walk through all occurrences of the label so far and evaluate
+        ** their relationship with the one passed to the function.
+        */
+        for (i = 0; i < CollCount (Entry->V.L.DefsOrRefs); i++) {
+            DOR = CollAt (Entry->V.L.DefsOrRefs, i);
+
+            if ((DOR->Flags & SC_DEF) && (Flags & SC_REF) && (Flags & SC_GOTO)) {
+                /* We're processing a goto and here is its destination label.
+                ** This means the difference between SP values is already known,
+                ** so we simply emit the SP adjustment code.
+                */
+                if (StackPtr != DOR->StackPtr) {
+                    g_space (StackPtr - DOR->StackPtr);
+                }
+
+                /* Are we jumping into a block with initalization of an object that
+                ** has automatic storage duration? Let's emit a warning.
+                */
+                if ((long)CollLast (AIC) != DOR->LocalsBlockId &&
+                    (CollCount (AIC) < DOR->Depth ||
+                    (long)CollAt (AIC, DOR->Depth - 1) != DOR->LocalsBlockId)) {
+                    Warning ("Goto at line %d to label %s jumps into a block with "
+                    "initialization of an object that has automatic storage duration",
+                    GetCurrentLine (), Name);
+                }
+            }
+
+
+            if ((DOR->Flags & SC_REF) && (DOR->Flags & SC_GOTO) && (Flags & SC_DEF)) {
+                /* We're processing a label, let's update all gotos encountered
+                ** so far
+                */
+                SymEntry *E;
+                g_userodata();
+                g_defdatalabel (DOR->LateSP_Label);
+                g_defdata (CF_CONST | CF_INT, StackPtr - DOR->StackPtr, 0);
+                E = NewSymEntry (LocalLabelName(DOR->LateSP_Label), SC_SPADJUSTMENT);
+                E->V.G.SPAdjustment = StackPtr - DOR->StackPtr;
+                AddSymEntry (SPAdjustTab, E);
+
+
+                /* Are we jumping into a block with initalization of an object that
+                ** has automatic storage duration? Let's emit a warning.
+                */
+                if ((long)CollLast (AIC) != DOR->LocalsBlockId &&
+                    (CollCount (AIC) >= DOR->Depth ||
+                    (long)CollLast (AIC) >= (long)DOR->Line))
+                    Warning ("Goto at line %d to label %s jumps into a block with "
+                    "initialization of an object that has automatic storage duration",
+                    DOR->Line, Name);
+             }
+
+        }
+
         Entry->Flags |= Flags;
 
     } else {
@@ -675,16 +774,25 @@ SymEntry* AddLabelSym (const char* Name, unsigned Flags)
         Entry = NewSymEntry (Name, SC_LABEL | Flags);
 
         /* Set a new label number */
-        Entry->V.Label = GetLocalLabel ();
+        Entry->V.L.Label = GetLocalLabel ();
+
+        /* Create Collection for label definition and references */
+        Entry->V.L.DefsOrRefs = NewCollection ();
+        NewDOR = AddDefOrRef (Entry, Flags);
 
         /* Generate the assembler name of the label */
-        Entry->AsmName = xstrdup (LocalLabelName (Entry->V.Label));
+        Entry->AsmName = xstrdup (LocalLabelName (Entry->V.L.Label));
 
         /* Add the entry to the label table */
         AddSymEntry (LabelTab, Entry);
 
     }
 
+    /* We are processing a goto, but the label has not yet been defined */
+    if (!SymIsDef (Entry) && (Flags & SC_REF) && (Flags & SC_GOTO)) {
+        g_lateadjustSP (NewDOR->LateSP_Label);
+    }
+
     /* Return the entry */
     return Entry;
 }
@@ -714,12 +822,12 @@ SymEntry* AddLocalSym (const char* Name, const Type* T, unsigned Flags, int Offs
             Entry->V.R.RegOffs  = Offs;
             Entry->V.R.SaveOffs = StackPtr;
         } else if ((Flags & SC_EXTERN) == SC_EXTERN) {
-            Entry->V.Label = Offs;
+            Entry->V.L.Label = Offs;
             SymSetAsmName (Entry);
         } else if ((Flags & SC_STATIC) == SC_STATIC) {
             /* Generate the assembler name from the label number */
-            Entry->V.Label = Offs;
-            Entry->AsmName = xstrdup (LocalLabelName (Entry->V.Label));
+            Entry->V.L.Label = Offs;
+            Entry->AsmName = xstrdup (LocalLabelName (Entry->V.L.Label));
         } else if ((Flags & SC_STRUCTFIELD) == SC_STRUCTFIELD) {
             Entry->V.Offs = Offs;
         } else {
@@ -749,9 +857,15 @@ SymEntry* AddGlobalSym (const char* Name, const Type* T, unsigned Flags)
     /* Do we have an entry with this name already? */
     SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
     if (Entry) {
-
         Type* EType;
 
+        /* If the existing symbol is an enumerated constant,
+        ** then avoid a compiler crash.  See GitHub issue #728.
+        */
+        if (Entry->Flags & SC_ENUM) {
+            Fatal ("Can't redeclare enum constant `%s' as global variable", Name);
+        }
+
         /* We have a symbol with this name already */
         if (Entry->Flags & SC_TYPE) {
             Error ("Multiple definition for `%s'", Name);
@@ -762,9 +876,9 @@ SymEntry* AddGlobalSym (const char* Name, const Type* T, unsigned Flags)
         EType = Entry->Type;
 
         /* If we are handling arrays, the old entry or the new entry may be an
-         * incomplete declaration. Accept this, and if the exsting entry is
-         * incomplete, complete it.
-         */
+        ** incomplete declaration. Accept this, and if the exsting entry is
+        ** incomplete, complete it.
+        */
         if (IsTypeArray (T) && IsTypeArray (EType)) {
 
             /* Get the array sizes */
@@ -792,17 +906,17 @@ SymEntry* AddGlobalSym (const char* Name, const Type* T, unsigned Flags)
             }
 
             /* In case of a function, use the new type descriptor, since it
-             * contains pointers to the new symbol tables that are needed if
-             * an actual function definition follows. Be sure not to use the
-             * new descriptor if it contains a function declaration with an
-             * empty parameter list.
-             */
+            ** contains pointers to the new symbol tables that are needed if
+            ** an actual function definition follows. Be sure not to use the
+            ** new descriptor if it contains a function declaration with an
+            ** empty parameter list.
+            */
             if (IsFunc) {
                 /* Get the function descriptor from the new type */
                 FuncDesc* F = GetFuncDesc (T);
                 /* Use this new function descriptor if it doesn't contain
-                 * an empty parameter list.
-                 */
+                ** an empty parameter list.
+                */
                 if ((F->Flags & FD_EMPTY) == 0) {
                     Entry->V.F.Func = F;
                     SetFuncDesc (EType, F);
@@ -810,6 +924,25 @@ SymEntry* AddGlobalSym (const char* Name, const Type* T, unsigned Flags)
             }
         }
 
+        /* If a static declaration follows a non-static declaration, then
+        ** warn about the conflict.  (It will compile a public declaration.)
+        */
+        if ((Flags & SC_EXTERN) == 0 && (Entry->Flags & SC_EXTERN) != 0) {
+            Warning ("static declaration follows non-static declaration of `%s'.", Name);
+        }
+
+        /* An extern declaration must not change the current linkage. */
+        if (IsFunc || (Flags & (SC_EXTERN | SC_STORAGE)) == SC_EXTERN) {
+            Flags &= ~SC_EXTERN;
+        }
+
+        /* If a public declaration follows a static declaration, then
+        ** warn about the conflict.  (It will compile a public declaration.)
+        */
+        if ((Flags & SC_EXTERN) != 0 && (Entry->Flags & SC_EXTERN) == 0) {
+            Warning ("public declaration follows static declaration of `%s'.", Name);
+        }
+
         /* Add the new flags */
         Entry->Flags |= Flags;
 
@@ -822,8 +955,8 @@ SymEntry* AddGlobalSym (const char* Name, const Type* T, unsigned Flags)
         Entry->Type = TypeDup (T);
 
         /* If this is a function, set the function descriptor and clear
-         * additional fields.
-         */
+        ** additional fields.
+        */
         if (IsFunc) {
             Entry->V.F.Func = GetFuncDesc (Entry->Type);
             Entry->V.F.Seg  = 0;
@@ -956,8 +1089,8 @@ void EmitDebugInfo (void)
     /* Output info for locals if enabled */
     if (DebugInfo) {
         /* For cosmetic reasons in the output file, we will insert two tabs
-         * on global level and just one on local level.
-         */
+        ** on global level and just one on local level.
+        */
         if (LexicalLevel == LEX_LEVEL_GLOBAL) {
             Head = "\t.dbg\t\tsym";
         } else {