]> Shamusworld >> Repos - rmac/blobdiff - token.c
Fix for slow .incbin performance.
[rmac] / token.c
diff --git a/token.c b/token.c
index cd515a80a5ea8cde304470096bc0663194b03020..53f76a98c2e283e4e9042c4987aac4d081201e54 100644 (file)
--- a/token.c
+++ b/token.c
@@ -7,15 +7,16 @@
 //
 
 #include "token.h"
-#include "symbol.h"
-#include "procln.h"
-#include "macro.h"
 #include "error.h"
+#include "macro.h"
+#include "procln.h"
+#include "symbol.h"
 
 #define DECL_KW                                // Declare keyword arrays
 #define DEF_KW                         // Declare keyword values 
 #include "kwtab.h"                     // Incl generated keyword tables & defs
 
+
 int lnsave;                                    // 1; strcpy() text of current line
 int curlineno;                         // Current line number
 int totlines;                          // Total # of lines
@@ -32,7 +33,7 @@ WORD cfileno;                         // Current file number
 TOKEN * tok;                           // Ptr to current token
 TOKEN * etok;                          // Ptr past last token in tokbuf[]
 TOKEN tokeol[1] = {EOL};       // Bailout end-of-line token
-char * string[TOKBUFSIZE];     // Token buffer string pointer storage
+char * string[TOKBUFSIZE*2];   // Token buffer string pointer storage
 
 // File record, used to maintain a list of every include file ever visited
 #define FILEREC struct _filerec
@@ -116,19 +117,34 @@ static char * riscregname[] = {
 };
 
 
-//
-// Make `fnum' the Current `curfname'
-//
-void setfnum(WORD fnum)
+void SetFilenameForErrorReporting(void)
 {
-       FILEREC * fr;
+       WORD fnum = cfileno;
 
-       for(fr=filerec; fr!=NULL && fnum--; fr=fr->frec_next);
+       // Check for absolute top filename (this should never happen)
+       if (fnum == -1)
+       {
+               curfname = "(*top*)";
+               return;
+       }
+
+       FILEREC * fr = filerec;
+
+       // Advance to the correct record...
+       while (fr != NULL && fnum != 0)
+       {
+               fr = fr->frec_next;
+               fnum--;
+       }
 
+       // Check for file # record not found (this should never happen either)
        if (fr == NULL)
-               curfname = "(*top*)";
-       else
-               curfname = fr->frec_name;
+       {
+               curfname = "(*NOT FOUND*)";
+               return;
+       }
+
+       curfname = fr->frec_name;
 }
 
 
@@ -143,8 +159,7 @@ INOBJ * a_inobj(int typ)
 
        // Allocate and initialize INOBJ first
        if (f_inobj == NULL)
-//             inobj = (INOBJ *)amem((LONG)sizeof(INOBJ));
-               inobj = (INOBJ *)malloc(sizeof(INOBJ));
+               inobj = malloc(sizeof(INOBJ));
        else
        {
                inobj = f_inobj;
@@ -155,8 +170,7 @@ INOBJ * a_inobj(int typ)
        {
        case SRC_IFILE:                                                 // Alloc and init an IFILE
                if (f_ifile == NULL)
-//                     ifile = (IFILE *)amem((LONG)sizeof(IFILE));
-                       ifile = (IFILE *)malloc(sizeof(IFILE));
+                       ifile = malloc(sizeof(IFILE));
                else
                {
                        ifile = f_ifile;
@@ -167,8 +181,7 @@ INOBJ * a_inobj(int typ)
                break;
        case SRC_IMACRO:                                                // Alloc and init an IMACRO 
                if (f_imacro == NULL)
-//                     imacro = (IMACRO *)amem((LONG)sizeof(IMACRO));
-                       imacro = (IMACRO *)malloc(sizeof(IMACRO));
+                       imacro = malloc(sizeof(IMACRO));
                else
                {
                        imacro = f_imacro;
@@ -178,8 +191,7 @@ INOBJ * a_inobj(int typ)
                inobj->inobj.imacro = imacro;
                break;
        case SRC_IREPT:                                                 // Alloc and init an IREPT
-//             inobj->inobj.irept = (IREPT *)amem((LONG)sizeof(IREPT));
-               inobj->inobj.irept = (IREPT *)malloc(sizeof(IREPT));
+               inobj->inobj.irept = malloc(sizeof(IREPT));
                DEBUG printf("alloc IREPT\n");
                break;
        }
@@ -210,30 +222,28 @@ INOBJ * a_inobj(int typ)
 // (the colon must be in the first column). These labels are stripped before
 // macro expansion takes place.
 //
-int mexpand(char * src, char * dest, int destsiz)
+int ExpandMacro(char * src, char * dest, int destsiz)
 {
-       char * s;
-       char * d = NULL;
-       char * dst;                             // Next dest slot
-       char * edst;                    // End+1 of dest buffer
        int i;
        int questmark;                  // \? for testing argument existence
-       TOKEN * tk;
        char mname[128];                // Assume max size of a formal arg name
-       int macnum;
-       SYM * arg;
-       IMACRO * imacro;
        char numbuf[20];                // Buffer for text of CONSTs
+       TOKEN * tk;
+       SYM * arg;
+       char ** symbolString;
+
+       DEBUG { printf("ExM: src=\"%s\"\n", src); }
 
-       imacro = cur_inobj->inobj.imacro;
-       macnum = (int)(imacro->im_macro->sattr);
+       IMACRO * imacro = cur_inobj->inobj.imacro;
+       int macnum = (int)(imacro->im_macro->sattr);
 
-       destsiz--;
-       dst = dest;
-       edst = dest + destsiz;
+//     destsiz--;
+       char * dst = dest;                                              // Next dest slot
+       char * edst = dest + destsiz - 1;               // End + 1(?) of dest buffer
 
        // Check for (and skip over) any "label" on the line
-       s = src;
+       char * s = src;
+       char * d = NULL;
 
        if (*s == ':')
        {
@@ -287,11 +297,11 @@ int mexpand(char * src, char * dest, int destsiz)
 
                                goto copy_d;
                        case '~':                                               // ==> unique label string Mnnnn... 
-                               sprintf(numbuf, "M%ud", curuniq);
+                               sprintf(numbuf, "M%u", curuniq);
 copystr:
                                d = numbuf;
 copy_d:
-                               ++s;
+                               s++;
 
                                while (*d != EOS)
                                {
@@ -320,7 +330,7 @@ copy_d:
                        // Get argument name: \name, \{name}
                        d = mname;
 
-                       // \foo
+                       // \label
                        if (*s != '{')
                        {
                                do
@@ -329,7 +339,7 @@ copy_d:
                                }
                                while (chrtab[*s] & CTSYM);
                        }
-                       // \\{foo} 
+                       // \\{label}
                        else
                        {
                                for(++s; *s != EOS && *s != '}';)
@@ -338,14 +348,14 @@ copy_d:
                                if (*s != '}')
                                        return error("missing '}'");
                                else
-                                       ++s;
+                                       s++;
                        }
 
                        *d = EOS;
 
                        // Lookup the argument and copy its (string) value into the
                        // destination string
-                       DEBUG printf("mname='%s'\n", mname);
+                       DEBUG printf("argument='%s'\n", mname);
 
                        if ((arg = lookup(mname, MACARG, macnum)) == NULL)
                                return errors("undefined argument: '%s'", mname);
@@ -356,12 +366,25 @@ copy_d:
                                // macro invocation) then it is ignored.
                                i = (int)arg->svalue;
 arg_num:
-                               DEBUG printf("~argnumber=%d\n", i);
-
+                               DEBUG printf("~argnumber=%d (argBase=%u)\n", i, imacro->argBase);
                                tk = NULL;
 
                                if (i < imacro->im_nargs)
-                                       tk = argp[i];
+                               {
+#if 0
+//                                     tk = argp[i];
+//                                     tk = argPtrs[i];
+                                       tk = argPtrs[imacro->argBase + i];
+#else
+                                       tk = imacro->argument[i].token;
+                                       symbolString = imacro->argument[i].string;
+//DEBUG
+//{
+//     printf("ExM: Preparing to parse argument #%u...\n", i);
+//     dumptok(tk);
+//}
+#endif
+                               }
 
                                // \?arg yields:
                                //    0  if the argument is empty or non-existant,
@@ -378,7 +401,7 @@ arg_num:
                                        continue;
                                }
 
-                               if (tk != NULL)                         // arg# is in range, so expand it
+                               if (tk != NULL)                         // arg # is in range, so expand it
                                {
                                        while (*tk != EOL)
                                        {
@@ -401,11 +424,22 @@ arg_num:
                                                        switch ((int)*tk++)
                                                        {
                                                        case SYMBOL:
-                                                               d = (char *)*tk++;
+#if 0
+//                                                             d = (char *)*tk++;
+                                                               d = string[*tk++];
+#else
+                                                               // This fix should be done for strings too
+                                                               d = symbolString[*tk++];
+DEBUG printf("ExM: SYMBOL=\"%s\"", d);
+#endif
                                                                break;
                                                        case STRING:
-                                                               d = (char *)*tk++;
-
+#if 0
+//                                                             d = (char *)*tk++;
+                                                               d = string[*tk++];
+#else
+                                                               d = symbolString[*tk++];
+#endif
                                                                if (dst >= edst)
                                                                        goto overflow;
 
@@ -516,10 +550,12 @@ strcopy:
        }
 
        *dst = EOS;
+       DEBUG { printf("ExM: dst=\"%s\"\n", dest); }
        return OK;
 
 overflow:
        *dst = EOS;
+       DEBUG printf("*** OVERFLOW LINE ***\n%s\n", dest);
        return fatal("line too long as a result of macro expansion");
 }
 
@@ -532,20 +568,25 @@ char * getmln(void)
        unsigned source_addr;
 
        IMACRO * imacro = cur_inobj->inobj.imacro;
-       LONG * strp = imacro->im_nextln;
+//     LONG * strp = imacro->im_nextln;
+       struct LineList * strp = imacro->im_nextln;
 
        if (strp == NULL)                                               // End-of-macro
                return NULL;
 
-       imacro->im_nextln = (LONG *)*strp;
-       mexpand((char *)(strp + 1), imacro->im_lnbuf, LNSIZ);
+//     imacro->im_nextln = (LONG *)*strp;
+       imacro->im_nextln = strp->next;
+//     ExpandMacro((char *)(strp + 1), imacro->im_lnbuf, LNSIZ);
+       ExpandMacro(strp->line, imacro->im_lnbuf, LNSIZ);
 
+// bollocks
+#if 0
        if (!strcmp(imacro->im_macro->sname, "mjump") && !mjump_align)
        {
                // if we need to adjust the alignment of the jump source address to
                // meet the rules of gpu main execution we need to skip the first nop
                // of the macro. This is simpler than trying to insert nop's mid macro.
-               source_addr = (orgactive) ? orgaddr : sloc;
+               source_addr = (orgactive ? orgaddr : sloc);
                source_addr += 8;
 
                if (source_addr % 4)
@@ -555,12 +596,15 @@ char * getmln(void)
                        if (strp == NULL)
                                return NULL;
 
-                       imacro->im_nextln = (LONG *)*strp;
-                       mexpand((char *)(strp + 1), imacro->im_lnbuf, LNSIZ);
+//                     imacro->im_nextln = (LONG *)*strp;
+//                     ExpandMacro((char *)(strp + 1), imacro->im_lnbuf, LNSIZ);
+                       imacro->im_nextln = strp->next;
+                       ExpandMacro(strp->line, imacro->im_lnbuf, LNSIZ);
                }
 
                mjump_align = 1;
        }
+#endif
 
        return imacro->im_lnbuf;
 }
@@ -587,7 +631,7 @@ char * getrln(void)
                        return NULL;
                }
 
-               strp = irept->ir_nextln;                        //strp
+               strp = irept->ir_nextln;
        }
 
        strcpy(irbuf, (char *)(irept->ir_nextln + 1));
@@ -609,7 +653,7 @@ int include(int handle, char * fname)
 
        // Verbose mode
        if (verb_flag)
-               printf("[Including: %s]\n", fname);
+               printf("[include: %s, cfileno=%u]\n", fname, cfileno);
 
        // Alloc and initialize include-descriptors
        inobj = a_inobj(SRC_IFILE);
@@ -620,12 +664,14 @@ int include(int handle, char * fname)
        ifile->ifoldlineno = curlineno;                 // Save old line number
        ifile->ifoldfname = curfname;                   // Save old filename
        ifile->ifno = cfileno;                                  // Save old file number
-       cfileno = filecount++;                                  // Compute new file number
+
+//     cfileno = filecount++;                                  // Compute new file number
+       // NB: This *must* be preincrement, we're adding one to the filecount here!
+       cfileno = ++filecount;                                  // Compute NEW file number
        curfname = strdup(fname);                               // Set current filename (alloc storage)
        curlineno = 0;                                                  // Start on line zero
 
        // Add another file to the file-record
-//     fr = (FILEREC *)amem((LONG)sizeof(FILEREC));
        fr = (FILEREC *)malloc(sizeof(FILEREC));
        fr->frec_next = NULL;
        fr->frec_name = curfname;
@@ -636,6 +682,7 @@ int include(int handle, char * fname)
                last_fr->frec_next = fr;                        // Append to list of filerecs 
 
        last_fr = fr;
+       DEBUG printf("[include: curfname: %s, cfileno=%u]\n", curfname, cfileno);
 
        return OK;
 }
@@ -697,12 +744,10 @@ void init_token(void)
 //
 int fpop(void)
 {
-       INOBJ * inobj;
        IFILE * ifile;
        IMACRO * imacro;
        LONG * p, * p1;
-
-       inobj = cur_inobj;
+       INOBJ * inobj = cur_inobj;
 
        if (inobj != NULL)
        {
@@ -724,10 +769,14 @@ int fpop(void)
                        ifile->if_link = f_ifile;
                        f_ifile = ifile;
                        close(ifile->ifhandle);                 // Close source file
+if (verb_flag) printf("[fpop (pre):  curfname=%s]\n", curfname);
                        curfname = ifile->ifoldfname;   // Set current filename
+if (verb_flag) printf("[fpop (post): curfname=%s]\n", curfname);
+if (verb_flag) printf("[fpop: (pre)  cfileno=%d ifile->ifno=%d]\n", (int)cfileno, (int)ifile->ifno);
                        curlineno = ifile->ifoldlineno; // Set current line# 
                        DEBUG printf("cfileno=%d ifile->ifno=%d\n", (int)cfileno, (int)ifile->ifno);
                        cfileno = ifile->ifno;                  // Restore current file number
+if (verb_flag) printf("[fpop: (post) cfileno=%d ifile->ifno=%d]\n", (int)cfileno, (int)ifile->ifno);
                        break;
                case SRC_IMACRO:                                        // Pop and release an IMACRO
                        imacro = inobj->inobj.imacro;
@@ -876,6 +925,7 @@ int tokln(void)
        case SRC_IFILE:
                if ((ln = getln()) == NULL)
                {
+if (verb_flag) printf("tokln: Calling fpop() from SRC_IFILE...\n");
                        fpop();                                                 // Pop input level
                        goto retry;                                             // Try for more lines 
                }
@@ -892,7 +942,7 @@ int tokln(void)
                                *ln = ';';
                        else
                        {
-                               for(p=ln; *p!=EOS; ++p)
+                               for(p=ln; *p!=EOS; p++)
                                {
                                        if (*p == '*')
                                        {
@@ -910,7 +960,7 @@ int tokln(void)
        case SRC_IMACRO:
                if ((ln = getmln()) == NULL)
                {
-                       exitmac();                                              // Exit macro (pop args, do fpop(), etc)
+                       ExitMacro();                                    // Exit macro (pop args, do fpop(), etc)
                        goto retry;                                             // Try for more lines...
                }
 
@@ -922,6 +972,7 @@ int tokln(void)
        case SRC_IREPT:
                if ((ln = getrln()) == NULL)
                {
+if (verb_flag) printf("tokln: Calling fpop() from SRC_IREPT...\n");
                        fpop();
                        goto retry;
                }
@@ -970,7 +1021,7 @@ int tokln(void)
 
                if (c & STSYM)
                {
-                       if (stuffnull)                                  // Terminate old symbol 
+                       if (stuffnull)                                  // Terminate old symbol from previous pass
                                *nullspot = EOS;
 
                        v = 0;                                                  // Assume no DOT attrib follows symbol
@@ -1047,10 +1098,16 @@ int tokln(void)
                        if (j < 0 || state < 0)
                        {
                                *tk++ = SYMBOL;
-#warning
+//#warning
 //problem here: nullspot is a char * but TOKEN is a uint32_t. On a 64-bit system,
 //this will cause all kinds of mischief.
+#if 0
                                *tk++ = (TOKEN)nullspot;
+#else
+                               string[stringNum] = nullspot;
+                               *tk++ = stringNum;
+                               stringNum++;
+#endif
                        }
                        else
                        {
@@ -1061,7 +1118,7 @@ int tokln(void)
                        if (v)                                                  // Record attribute token (if any)
                                *tk++ = (TOKEN)v;
 
-                       if (stuffnull)                                  // Arrange for string termination 
+                       if (stuffnull)                                  // Arrange for string termination on next pass
                                nullspot = ln;
 
                        continue;
@@ -1093,10 +1150,16 @@ int tokln(void)
                        case '\"':                                      // "string" 
                                c1 = ln[-1];
                                *tk++ = STRING;
-#warning
+//#warning
 // More char * stuffing (8 bytes) into the space of 4 (TOKEN).
 // Need to figure out how to fix this crap.
+#if 0
                                *tk++ = (TOKEN)ln;
+#else
+                               string[stringNum] = ln;
+                               *tk++ = stringNum;
+                               stringNum++;
+#endif
 
                                for(p=ln; *ln!=EOS && *ln!=c1;)
                                {
@@ -1434,35 +1497,43 @@ goteol:
 // be EOL.
 //
 //int d_goto(WORD siz) {
-int d_goto(void)
+//int d_goto(void)
+int d_goto(WORD unused)
 {
-       char * sym;                                               // Label to search for 
-       LONG * defln;                                             // Macro definition strings 
+//     char * sym;                                               // Label to search for 
+//     LONG * defln;                                             // Macro definition strings 
        char * s1;                                                // Temps for string comparison 
        char * s2;
-       IMACRO * imacro;                                          // Macro invocation block
+//     IMACRO * imacro;                                          // Macro invocation block
 
        // Setup for the search
        if (*tok != SYMBOL)
                return error("missing label");
 
-       sym = (char *)tok[1];
+//     sym = (char *)tok[1];
+       char * sym = string[tok[1]];
        tok += 2;
 
        if (cur_inobj->in_type != SRC_IMACRO)
                return error("goto not in macro");
 
-       imacro = cur_inobj->inobj.imacro;
-       defln = (LONG *)imacro->im_macro->svalue;
+       IMACRO * imacro = cur_inobj->inobj.imacro;
+//     defln = (LONG *)imacro->im_macro->svalue;
+       struct LineList * defln = imacro->im_macro->lineList;
 
        // Find the label, starting with the first line.
-       for(; defln!=NULL; defln=(LONG *)*defln)
+//     for(; defln!=NULL; defln=(LONG *)*defln)
+       for(; defln!=NULL; defln=defln->next)
        {
-               if (*(char *)(defln + 1) == ':')
+//             if (*(char *)(defln + 1) == ':')
+               if (defln->line[0] == ':')
                {
                        // Compare names (sleazo string compare)
+                       // This string compare is not right. Doesn't check for lengths.
+#warning "!!! Bad string comparison !!!"
                        s1 = sym;
-                       s2 = (char *)(defln + 1) + 1;
+//                     s2 = (char *)(defln + 1) + 1;
+                       s2 = defln->line;
 
                        while (*s1 == *s2)
                        {
@@ -1470,8 +1541,8 @@ int d_goto(void)
                                        break;
                                else
                                {
-                                       ++s1;
-                                       ++s2;
+                                       s1++;
+                                       s2++;
                                }
                        }
 
@@ -1487,10 +1558,11 @@ int d_goto(void)
        return error("goto label not found");
 }
 
+
 void DumpTokenBuffer(void)
 {
        TOKEN * t;
-       printf("Tokens: ");
+       printf("Tokens [%X]: ", sloc);
 
        for(t=tokbuf; *t!=EOL; t++)
        {
@@ -1504,11 +1576,15 @@ void DumpTokenBuffer(void)
                else if (*t == ACONST)
                        printf("[ACONST]");
                else if (*t == STRING)
-                       printf("[STRING]");
+//                     printf("[STRING]");
+               {
+                       t++;
+                       printf("[STRING:\"%s\"]", string[*t]);
+               }
                else if (*t == SYMBOL)
                {
                        t++;
-                       printf("[SYMBOL:\"%s\"]", (char *)*t);
+                       printf("[SYMBOL:\"%s\"]", string[*t]);
                }
                else if (*t == EOS)
                        printf("[EOS]");
@@ -1558,14 +1634,17 @@ void DumpTokenBuffer(void)
                        printf("[CR_DATE]");
                else if (*t >= 0x20 && *t <= 0x2F)
                        printf("[%c]", (char)*t);
+               else if (*t >= 0x3A && *t <= 0x3F)
+                       printf("[%c]", (char)*t);
                else if (*t >= 0x80 && *t <= 0x87)
                        printf("[D%u]", ((uint32_t)*t) - 0x80);
                else if (*t >= 0x88 && *t <= 0x8F)
                        printf("[A%u]", ((uint32_t)*t) - 0x88);
                else
-//                     printf("[%X:%c]", (uint32_t)*t, (char)*t);
-                       printf("[%X]", (uint32_t)*t);
+                       printf("[%X:%c]", (uint32_t)*t, (char)*t);
+//                     printf("[%X]", (uint32_t)*t);
        }
 
        printf("[EOL]\n");
 }
+