]> Shamusworld >> Repos - rmac/blobdiff - macro.c
Version bump for last commit. :-)
[rmac] / macro.c
diff --git a/macro.c b/macro.c
index 46443463317e265a30d1950241c7056c3e3aa645..8da9bcb540223b19b32f16c45ba5ff968d50f5e5 100644 (file)
--- a/macro.c
+++ b/macro.c
@@ -1,7 +1,7 @@
 //
-// RMAC - Reboot's Macro Assembler for all Atari computers
+// RMAC - Renamed Macro Assembler for all Atari computers
 // MACRO.C - Macro Definition and Invocation
-// Copyright (C) 199x Landon Dyer, 2011-2017 Reboot and Friends
+// Copyright (C) 199x Landon Dyer, 2011-2021 Reboot and Friends
 // RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
 // Source utilised with the kind permission of Landon Dyer
 //
@@ -22,11 +22,12 @@ int macnum;                                 // Unique number for macro definition
 
 static LONG macuniq;           // Unique-per-macro number
 static SYM * curmac;           // Macro currently being defined
-static uint32_t argno;                 // Formal argument count
+static uint32_t argno;         // Formal argument count
+LONG reptuniq;                         // Unique-per-rept number
 
 static LLIST * firstrpt;       // First .rept line
 static LLIST * nextrpt;                // Last .rept line
-static int rptlevel;           // .rept nesting level
+int rptlevel;                          // .rept nesting level
 
 // Function prototypes
 static int KWMatch(char *, char *);
@@ -40,6 +41,7 @@ void InitMacro(void)
 {
        macuniq = 0;
        macnum = 1;
+       reptuniq = 0;
 }
 
 
@@ -117,6 +119,7 @@ int defmac1(char * ln, int notEndFlag)
                        curmac->lineList = malloc(sizeof(LLIST));
                        curmac->lineList->next = NULL;
                        curmac->lineList->line = strdup(ln);
+                       curmac->lineList->lineno = curlineno;
                        curmac->last = curmac->lineList;
                }
                else
@@ -124,6 +127,7 @@ int defmac1(char * ln, int notEndFlag)
                        curmac->last->next = malloc(sizeof(LLIST));
                        curmac->last->next->next = NULL;
                        curmac->last->next->line = strdup(ln);
+                       curmac->lineList->lineno = curlineno;
                        curmac->last = curmac->last->next;
                }
 
@@ -153,10 +157,10 @@ int DefineMacro(void)
        // Setup entry in symbol table, make sure the macro isn't a duplicate
        // entry, and that it doesn't override any processor mnemonic or assembler
        // directive.
-       if (*tok.u32++ != SYMBOL)
+       if (*tok++ != SYMBOL)
                return error("missing symbol");
 
-       char * name = string[*tok.u32++];
+       char * name = string[*tok++];
 
        if (lookup(name, MACRO, 0) != NULL)
                return error("duplicate macro definition");
@@ -166,11 +170,11 @@ int DefineMacro(void)
        curmac->sattr = (WORD)(macnum++);
 
        // Parse and define formal arguments in symbol table
-       if (*tok.u32 != EOL)
+       if (*tok != EOL)
        {
                argno = 0;
                symlist(defmac2);
-               at_eol();
+               ErrorIfNotAtEOL();
        }
 
        // Suck in the macro definition; we're looking for an ENDM symbol on a line
@@ -224,6 +228,7 @@ WARNING("!!! Casting (char *) as LONG !!!")
                firstrpt = malloc(sizeof(LLIST));
                firstrpt->next = NULL;
                firstrpt->line = strdup(line);
+               firstrpt->lineno = curlineno;
                nextrpt = firstrpt;
        }
        else
@@ -231,6 +236,7 @@ WARNING("!!! Casting (char *) as LONG !!!")
                nextrpt->next = malloc(sizeof(LLIST));
                nextrpt->next->next = NULL;
                nextrpt->next->line = strdup(line);
+               nextrpt->next->lineno = curlineno;
                nextrpt = nextrpt->next;
        }
 #endif
@@ -307,19 +313,19 @@ static int LNCatch(int (* lnfunc)(), char * dirlist)
                char * p = NULL;
                int k = -1;
 
-               if (*tok.u32 == SYMBOL)
+               if (*tok == SYMBOL)
                {
                        // A string followed by a colon or double colon is a symbol and
                        // *not* a directive, see if we can find the directive after it
-                       if ((tok.u32[2] == ':' || tok.u32[2] == DCOLON))
+                       if ((tok[2] == ':' || tok[2] == DCOLON))
                        {
-                               if (tok.u32[3] == SYMBOL)
-                                       p = string[tok.u32[4]];
+                               if (tok[3] == SYMBOL)
+                                       p = string[tok[4]];
                        }
                        else
                        {
                                // Otherwise, just grab the directive
-                               p = string[tok.u32[1]];
+                               p = string[tok[1]];
                        }
                }
 
@@ -389,7 +395,7 @@ static int KWMatch(char * kw, char * kwlist)
 //
 int InvokeMacro(SYM * mac, WORD siz)
 {
-       DEBUG { printf("InvokeMacro: arguments="); DumpTokens(tok.u32); }
+       DEBUG { printf("InvokeMacro: arguments="); DumpTokens(tok); }
 
        INOBJ * inobj = a_inobj(SRC_IMACRO);    // Alloc and init IMACRO
        IMACRO * imacro = inobj->inobj.imacro;
@@ -397,46 +403,80 @@ int InvokeMacro(SYM * mac, WORD siz)
 
        // Chop up the arguments, if any (tok comes from token.c, which at this
        // point points at the macro argument token stream)
-       if (*tok.u32 != EOL)
+       if (*tok != EOL)
        {
                // Parse out the arguments and set them up correctly
                TOKEN * p = imacro->argument[nargs].token;
                int stringNum = 0;
+               int numTokens = 0;
 
-               while (*tok.u32 != EOL)
+               while (*tok != EOL)
                {
-                       if (*tok.u32 == ACONST)
+                       if (*tok == ACONST)
                        {
+                               // Sanity checking (it's numTokens + 1 because we need an EOL
+                               // if we successfully parse this argument)
+                               if ((numTokens + 3) >= TS_MAXTOKENS)
+                                       return error("Too many tokens in argument #%d in MACRO invocation", nargs + 1);
+
                                for(int i=0; i<3; i++)
-                                       *p++ = *tok.u32++;
+                                       *p++ = *tok++;
+
+                               numTokens += 3;
                        }
-                       else if (*tok.u32 == CONST)             // Constants are 64-bits
+                       else if (*tok == CONST)         // Constants are 64-bits
                        {
-                               *p++ = *tok.u32++;                      // Token
+                               // Sanity checking (it's numTokens + 1 because we need an EOL
+                               // if we successfully parse this argument)
+                               if ((numTokens + 3) >= TS_MAXTOKENS)
+                                       return error("Too many tokens in argument #%d in MACRO invocation", nargs + 1);
+
+                               *p++ = *tok++;                  // Token
                                uint64_t *p64 = (uint64_t *)p;
-                               uint64_t *tok64 = (uint64_t *)tok.u32;
+                               uint64_t *tok64 = (uint64_t *)tok;
                                *p64++ = *tok64++;
-                               tok.u32 = (TOKEN *)tok64;
+                               tok = (TOKEN *)tok64;
                                p = (uint32_t *)p64;
+                               numTokens += 3;
                        }
-                       else if ((*tok.u32 == STRING) || (*tok.u32 == SYMBOL))
+                       else if ((*tok == STRING) || (*tok == SYMBOL))
                        {
-                               *p++ = *tok.u32++;
-                               imacro->argument[nargs].string[stringNum] = strdup(string[*tok.u32++]);
+                               // Sanity checking (it's numTokens + 1 because we need an EOL
+                               // if we successfully parse this argument)
+                               if (stringNum >= TS_MAXSTRINGS)
+                                       return error("Too many strings in argument #%d in MACRO invocation", nargs + 1);
+
+                               if ((numTokens + 2) >= TS_MAXTOKENS)
+                                       return error("Too many tokens in argument #%d in MACRO invocation", nargs + 1);
+
+                               *p++ = *tok++;
+                               imacro->argument[nargs].string[stringNum] = strdup(string[*tok++]);
                                *p++ = stringNum++;
+                               numTokens += 2;
                        }
-                       else if (*tok.u32 == ',')
+                       else if (*tok == ',')
                        {
+                               // Sanity checking
+                               if ((nargs + 1) >= TS_MAXARGS)
+                                       return error("Too many arguments in MACRO invocation");
+
                                // Comma delimiter was found, so set up for next argument
                                *p++ = EOL;
-                               tok.u32++;
+                               tok++;
                                stringNum = 0;
+                               numTokens = 0;
                                nargs++;
                                p = imacro->argument[nargs].token;
                        }
                        else
                        {
-                               *p++ = *tok.u32++;
+                               // Sanity checking (it's numTokens + 1 because we need an EOL
+                               // if we successfully parse this argument)
+                               if ((numTokens + 1) >= TS_MAXTOKENS)
+                                       return error("Too many tokens in argument #%d in MACRO invocation", nargs + 1);
+
+                               *p++ = *tok++;
+                               numTokens++;
                        }
                }