]> git.sur5r.net Git - cc65/blobdiff - src/ca65/macro.c
Finished implemenation of commands to delete macros. Added the new commands to
[cc65] / src / ca65 / macro.c
index b98925bc89f4faa749360765da1752d9e94d2465..a5c86e9ad9975455f6bc8b21a6059bbe874fbbac 100644 (file)
@@ -106,8 +106,10 @@ struct Macro {
     unsigned       TokCount;   /* Number of tokens for this macro */
     TokNode*       TokRoot;    /* Root of token list */
     TokNode*       TokLast;    /* Pointer to last token in list */
-    unsigned char   Style;     /* Macro style */
     StrBuf          Name;      /* Macro name, dynamically allocated */
+    unsigned        Expansions; /* Number of active macro expansions */
+    unsigned char   Style;     /* Macro style */
+    unsigned char   Incomplete; /* Macro is currently built */
 };
 
 /* Hash table functions */
@@ -121,9 +123,6 @@ static const HashFunctions HashFunc = {
 /* Macro hash table */
 static HashTable MacroTab = STATIC_HASHTABLE_INITIALIZER (117, &HashFunc);
 
-/* Global macro data */
-static Macro*          MacroRoot = 0;  /* List of all macros */
-
 /* Structs that holds data for a macro expansion */
 typedef struct MacExp MacExp;
 struct MacExp {
@@ -139,6 +138,9 @@ struct MacExp {
     unsigned    LISlot;         /* Slot for additional line infos */
 };
 
+/* Maximum number of nested macro expansions */
+#define MAX_MACEXPANSIONS       256U
+
 /* Number of active macro expansions */
 static unsigned MacExpansions = 0;
 
@@ -148,6 +150,9 @@ static int DoMacAbort = 0;
 /* Counter to create local names for symbols */
 static unsigned LocalName = 0;
 
+/* Define style macros disabled if != 0 */
+static unsigned DisableDefines = 0;
+
 
 
 /*****************************************************************************/
@@ -201,15 +206,39 @@ static IdDesc* NewIdDesc (const StrBuf* Id)
 /* Create a new IdDesc, initialize and return it */
 {
     /* Allocate memory */
-    IdDesc* I = xmalloc (sizeof (IdDesc));
+    IdDesc* ID = xmalloc (sizeof (IdDesc));
 
     /* Initialize the struct */
-    I->Next = 0;
-    SB_Init (&I->Id);
-    SB_Copy (&I->Id, Id);
+    ID->Next = 0;
+    SB_Init (&ID->Id);
+    SB_Copy (&ID->Id, Id);
 
     /* Return the new struct */
-    return I;
+    return ID;
+}
+
+
+
+static void FreeIdDesc (IdDesc* ID)
+/* Free an IdDesc */
+{
+    /* Free the name */
+    SB_Done (&ID->Id);
+
+    /* Free the structure itself */
+    xfree (ID);
+}
+
+
+
+static void FreeIdDescList (IdDesc* ID)
+/* Free a complete list of IdDesc structures */
+{
+    while (ID) {
+        IdDesc* This = ID;      
+        ID = ID->Next;
+        FreeIdDesc (This);
+    }
 }
 
 
@@ -229,13 +258,11 @@ static Macro* NewMacro (const StrBuf* Name, unsigned char Style)
     M->TokCount          = 0;
     M->TokRoot    = 0;
     M->TokLast    = 0;
-    M->Style     = Style;
     SB_Init (&M->Name);
     SB_Copy (&M->Name, Name);
-
-    /* Insert the macro into the global macro list */
-    M->List = MacroRoot;
-    MacroRoot = M;
+    M->Expansions = 0;
+    M->Style     = Style;
+    M->Incomplete = 1;
 
     /* Insert the macro into the hash table */
     HT_Insert (&MacroTab, &M->Node);
@@ -246,6 +273,32 @@ static Macro* NewMacro (const StrBuf* Name, unsigned char Style)
 
 
 
+static void FreeMacro (Macro* M)
+/* Free a macro entry which has already been removed from the macro table. */
+{
+    TokNode* T;
+
+    /* Free locals */
+    FreeIdDescList (M->Locals);
+
+    /* Free identifiers of parameters */
+    FreeIdDescList (M->Params);
+
+    /* Free the token list for the macro */
+    while ((T = M->TokRoot) != 0) {
+        M->TokRoot = T->Next;
+        FreeTokNode (T);
+    }
+
+    /* Free the macro name */
+    SB_Done (&M->Name);
+
+    /* Free the macro structure itself */
+    xfree (M);
+}
+
+
+
 static MacExp* NewMacExp (Macro* M)
 /* Create a new expansion structure for the given macro */
 {
@@ -263,11 +316,14 @@ static MacExp* NewMacExp (Macro* M)
     LocalName    += M->LocalCount;
     E->ParamCount = 0;
     E->Params     = xmalloc (M->ParamCount * sizeof (TokNode*));
-    E->ParamExp          = 0;
     for (I = 0; I < M->ParamCount; ++I) {
        E->Params[I] = 0;
     }
-    E->LISlot     = AllocLineInfoSlot (LI_TYPE_MACRO | MacExpansions);
+    E->ParamExp          = 0;
+    E->LISlot     = AllocLineInfoSlot (LI_TYPE_MACRO, MacExpansions);
+
+    /* Mark the macro as expanding */
+    ++M->Expansions;
 
     /* One macro expansion more */
     ++MacExpansions;
@@ -286,6 +342,9 @@ static void FreeMacExp (MacExp* E)
     /* One macro expansion less */
     --MacExpansions;
 
+    /* No longer expanding this macro */
+    --E->M->Expansions;
+
     /* Free the parameter lists */
     for (I = 0; I < E->ParamCount; ++I) {
         /* Free one parameter list */
@@ -316,18 +375,18 @@ static void MacSkipDef (unsigned Style)
 /* Skip a macro definition */
 {
     if (Style == MAC_STYLE_CLASSIC) {
-       /* Skip tokens until we reach the final .endmacro */
-       while (CurTok.Tok != TOK_ENDMACRO && CurTok.Tok != TOK_EOF) {
-           NextTok ();
-       }
-       if (CurTok.Tok != TOK_EOF) {
-           SkipUntilSep ();
-       } else {
-           Error ("`.ENDMACRO' expected");
-       }
+               /* Skip tokens until we reach the final .endmacro */
+               while (CurTok.Tok != TOK_ENDMACRO && CurTok.Tok != TOK_EOF) {
+                   NextTok ();
+               }
+               if (CurTok.Tok != TOK_EOF) {
+                   SkipUntilSep ();
+               } else {
+                   Error ("`.ENDMACRO' expected");
+               }
     } else {
-       /* Skip until end of line */
-       SkipUntilSep ();
+               /* Skip until end of line */
+               SkipUntilSep ();
     }
 }
 
@@ -446,7 +505,7 @@ void MacDef (unsigned Style)
                /* Done */
                break;
            }
-           /* May not have end of file in a macro definition */
+           /* May not have end of file in a macro definition */
            if (CurTok.Tok == TOK_EOF) {
                Error ("`.ENDMACRO' expected");
                goto Done;
@@ -480,7 +539,7 @@ void MacDef (unsigned Style)
                I->Next = M->Locals;
                M->Locals = I;
                ++M->LocalCount;
-               NextTok ();
+                       NextTok ();
 
                /* Check for end of list */
                if (CurTok.Tok != TOK_COMMA) {
@@ -533,6 +592,9 @@ void MacDef (unsigned Style)
        NextTok ();
     }
 
+    /* Reset the Incomplete flag now that parsing is done */
+    M->Incomplete = 0;
+
 Done:
     /* Switch out of raw token mode */
     LeaveRawTokenMode ();
@@ -540,6 +602,33 @@ Done:
 
 
 
+void MacUndef (const StrBuf* Name, unsigned char Style)
+/* Undefine the macro with the given name and style. A style mismatch is
+ * treated as if the macro didn't exist.
+ */
+{
+    /* Search for the macro */
+    Macro* M = HT_FindEntry (&MacroTab, Name);
+
+    /* Don't let the user kid with us */
+    if (M == 0 || M->Style != Style) {
+        Error ("No such macro: %m%p", Name);
+        return;
+    }
+    if (M->Expansions > 0) {
+        Error ("Cannot delete a macro that is currently expanded");
+        return;
+    }
+
+    /* Remove the macro from the macro table */
+    HT_RemoveEntry (&MacroTab, M);
+
+    /* Free the macro structure */
+    FreeMacro (M);
+}
+
+
+
 static int MacExpand (void* Data)
 /* If we're currently expanding a macro, set the the scanner token and
  * attribute to the next value and return true. If we are not expanding
@@ -670,29 +759,24 @@ MacEnd:
 
 
 
-static void StartExpClassic (Macro* M)
-/* Start expanding the classic macro M */
+static void StartExpClassic (MacExp* E)
+/* Start expanding a classic macro */
 {
-    MacExp*     E;
     token_t     Term;
 
-
     /* Skip the macro name */
     NextTok ();
 
-    /* Create a structure holding expansion data */
-    E = NewMacExp (M);
-
     /* Read the actual parameters */
     while (!TokIsSep (CurTok.Tok)) {
 
        TokNode* Last;
 
                /* Check for maximum parameter count */
-       if (E->ParamCount >= M->ParamCount) {
+       if (E->ParamCount >= E->M->ParamCount) {
                    ErrorSkip ("Too many macro parameters");
            break;
-       }
+       }
 
         /* The macro may optionally be enclosed in curly braces */
         Term = GetTokListTerm (TOK_COMMA);
@@ -719,7 +803,7 @@ static void StartExpClassic (Macro* M)
            } else {
                Last->Next = T;
            }
-           Last = T;
+                   Last = T;
 
            /* And skip it... */
            NextTok ();
@@ -741,8 +825,8 @@ static void StartExpClassic (Macro* M)
 
        /* Check for a comma */
        if (CurTok.Tok == TOK_COMMA) {
-           NextTok ();
-       } else {
+           NextTok ();
+       } else {
            break;
        }
     }
@@ -756,16 +840,13 @@ static void StartExpClassic (Macro* M)
 
 
 
-static void StartExpDefine (Macro* M)
+static void StartExpDefine (MacExp* E)
 /* Start expanding a DEFINE style macro */
 {
-    /* Create a structure holding expansion data */
-    MacExp* E = NewMacExp (M);
-
     /* A define style macro must be called with as many actual parameters
      * as there are formal ones. Get the parameter count.
      */
-    unsigned Count = M->ParamCount;
+    unsigned Count = E->M->ParamCount;
 
     /* Skip the current token */
     NextTok ();
@@ -802,13 +883,13 @@ static void StartExpDefine (Macro* M)
                    }
                    Last = T;
 
-           /* And skip it... */
-           NextTok ();
+           /* And skip it... */
+           NextTok ();
 
                } while (CurTok.Tok != Term && !TokIsSep (CurTok.Tok));
 
-       /* One parameter more */
-       ++E->ParamCount;
+       /* One parameter more */
+       ++E->ParamCount;
 
         /* If the macro argument was enclosed in curly braces, end-of-line
          * is an error. Skip the closing curly brace.
@@ -847,15 +928,34 @@ static void StartExpDefine (Macro* M)
 void MacExpandStart (void)
 /* Start expanding the macro in SVal */
 {
+    MacExp* E;
+
     /* Search for the macro */
     Macro* M = HT_FindEntry (&MacroTab, &CurTok.SVal);
-    CHECK (M != 0);
+    CHECK (M != 0 && (M->Style != MAC_STYLE_DEFINE || DisableDefines == 0));
+
+    /* We cannot expand an incomplete macro */
+    if (M->Incomplete) {
+        Error ("Cannot expand an incomplete macro");
+        return;
+    }
+
+    /* Don't allow too many nested macro expansions - otherwise it is possible
+     * to force an endless loop and assembler crash.
+     */
+    if (MacExpansions >= MAX_MACEXPANSIONS) {
+        Error ("Too many nested macro expansions");
+        return;
+    }
+
+    /* Create a structure holding expansion data */
+    E = NewMacExp (M);
 
     /* Call the apropriate subroutine */
     switch (M->Style) {
-       case MAC_STYLE_CLASSIC: StartExpClassic (M);    break;
-       case MAC_STYLE_DEFINE:  StartExpDefine (M);     break;
-       default:                Internal ("Invalid macro style: %d", M->Style);
+       case MAC_STYLE_CLASSIC: StartExpClassic (E);    break;
+       case MAC_STYLE_DEFINE:  StartExpDefine (E);     break;
+       default:                Internal ("Invalid macro style: %d", M->Style);
     }
 }
 
@@ -884,7 +984,15 @@ int IsMacro (const StrBuf* Name)
 int IsDefine (const StrBuf* Name)
 /* Return true if the given name is the name of a define style macro */
 {
-    Macro* M = HT_FindEntry (&MacroTab, Name);
+    Macro* M;
+
+    /* Never if disabled */
+    if (DisableDefines) {
+        return 0;
+    }
+
+    /* Check if we have such a macro */
+    M = HT_FindEntry (&MacroTab, Name);
     return (M != 0 && M->Style == MAC_STYLE_DEFINE);
 }
 
@@ -898,4 +1006,22 @@ int InMacExpansion (void)
 
 
 
+void DisableDefineStyleMacros (void)
+/* Disable define style macros until EnableDefineStyleMacros is called */
+{
+    ++DisableDefines;
+}
+
+
+
+void EnableDefineStyleMacros (void)
+/* Re-enable define style macros previously disabled with
+ * DisableDefineStyleMacros.
+ */
+{
+    PRECONDITION (DisableDefines > 0);
+    --DisableDefines;
+}
+
+