Report dwarf as V2 for ELF32
[platform/upstream/nasm.git] / nasmlib.c
index 37c19dd..d5cf207 100644 (file)
--- a/nasmlib.c
+++ b/nasmlib.c
@@ -2,10 +2,12 @@
  *
  * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
  * Julian Hall. All rights reserved. The software is
- * redistributable under the licence given in the file "Licence"
+ * redistributable under the license given in the file "LICENSE"
  * distributed in the NASM archive.
  */
 
+#include "compiler.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include "nasm.h"
 #include "nasmlib.h"
-#include "insns.h"              /* For MAX_KEYWORD */
+#include "insns.h"
 
 int globalbits = 0;    /* defined in nasm.h, works better here for ASM+DISASM */
-
-static efunc nasm_malloc_error;
+efunc nasm_malloc_error;       /* Exported for the benefit of vsnprintf.c */
 
 #ifdef LOGALLOC
 static FILE *logfp;
@@ -46,7 +47,24 @@ void *nasm_malloc(size_t size)
 #ifdef LOGALLOC
     else
         fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
-                file, line, (int32_t)size, p);
+                file, line, (long)size, p);
+#endif
+    return p;
+}
+
+#ifdef LOGALLOC
+void *nasm_zalloc_log(char *file, int line, size_t size)
+#else
+void *nasm_zalloc(size_t size)
+#endif
+{
+    void *p = calloc(size, 1);
+    if (!p)
+        nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
+#ifdef LOGALLOC
+    else
+        fprintf(logfp, "%s %d calloc(%ld, 1) returns %p\n",
+                file, line, (long)size, p);
 #endif
     return p;
 }
@@ -63,10 +81,10 @@ void *nasm_realloc(void *q, size_t size)
 #ifdef LOGALLOC
     else if (q)
         fprintf(logfp, "%s %d realloc(%p,%ld) returns %p\n",
-                file, line, q, (int32_t)size, p);
+                file, line, q, (long)size, p);
     else
         fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
-                file, line, (int32_t)size, p);
+                file, line, (long)size, p);
 #endif
     return p;
 }
@@ -78,10 +96,10 @@ void nasm_free(void *q)
 #endif
 {
     if (q) {
-        free(q);
 #ifdef LOGALLOC
         fprintf(logfp, "%s %d free(%p)\n", file, line, q);
 #endif
+        free(q);
     }
 }
 
@@ -100,7 +118,7 @@ char *nasm_strdup(const char *s)
 #ifdef LOGALLOC
     else
         fprintf(logfp, "%s %d strdup(%ld) returns %p\n",
-                file, line, (int32_t)size, p);
+                file, line, (long)size, p);
 #endif
     strcpy(p, s);
     return p;
@@ -121,14 +139,14 @@ char *nasm_strndup(char *s, size_t len)
 #ifdef LOGALLOC
     else
         fprintf(logfp, "%s %d strndup(%ld) returns %p\n",
-                file, line, (int32_t)size, p);
+                file, line, (long)size, p);
 #endif
     strncpy(p, s, len);
     p[len] = '\0';
     return p;
 }
 
-#if !defined(stricmp) && !defined(strcasecmp)
+#ifndef nasm_stricmp
 int nasm_stricmp(const char *s1, const char *s2)
 {
     while (*s1 && tolower(*s1) == tolower(*s2))
@@ -142,7 +160,7 @@ int nasm_stricmp(const char *s1, const char *s2)
 }
 #endif
 
-#if !defined(strnicmp) && !defined(strncasecmp)
+#ifndef nasm_strnicmp
 int nasm_strnicmp(const char *s1, const char *s2, int n)
 {
     while (n > 0 && *s1 && tolower(*s1) == tolower(*s2))
@@ -156,19 +174,59 @@ int nasm_strnicmp(const char *s1, const char *s2, int n)
 }
 #endif
 
-#define lib_isnumchar(c)   ( isalnum(c) || (c) == '$')
+#ifndef nasm_strsep
+char *nasm_strsep(char **stringp, const char *delim)
+{
+        char *s = *stringp;
+        char *e;
+
+        if (!s)
+                return NULL;
+
+        e = strpbrk(s, delim);
+        if (e)
+                *e++ = '\0';
+
+        *stringp = e;
+        return s;
+}
+#endif
+
+
+#define lib_isnumchar(c)   (isalnum(c) || (c) == '$' || (c) == '_')
 #define numvalue(c)  ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
 
-int64_t readnum(char *str, int *error)
+static int radix_letter(char c)
+{
+    switch (c) {
+    case 'b': case 'B':
+    case 'y': case 'Y':
+       return 2;               /* Binary */
+    case 'o': case 'O':
+    case 'q': case 'Q':
+       return 8;               /* Octal */
+    case 'h': case 'H':
+    case 'x': case 'X':
+       return 16;              /* Hexadecimal */
+    case 'd': case 'D':
+    case 't': case 'T':
+       return 10;              /* Decimal */
+    default:
+       return 0;               /* Not a known radix letter */
+    }
+}
+
+int64_t readnum(char *str, bool *error)
 {
     char *r = str, *q;
-    int32_t radix;
+    int32_t pradix, sradix, radix;
+    int plen, slen, len;
     uint64_t result, checklimit;
     int digit, last;
-    int warn = FALSE;
+    bool warn = false;
     int sign = 1;
 
-    *error = FALSE;
+    *error = false;
 
     while (isspace(*r))
         r++;                    /* find start of number */
@@ -187,93 +245,101 @@ int64_t readnum(char *str, int *error)
     while (lib_isnumchar(*q))
         q++;                    /* find end of number */
 
-    /*
-     * If it begins 0x, 0X or $, or ends in H, it's in hex. if it
-     * ends in Q, it's octal. if it ends in B, it's binary.
-     * Otherwise, it's ordinary decimal.
-     */
-    if (*r == '0' && (r[1] == 'x' || r[1] == 'X'))
-        radix = 16, r += 2;
-    else if (*r == '$')
-        radix = 16, r++;
-    else if (q[-1] == 'H' || q[-1] == 'h')
-        radix = 16, q--;
-    else if (q[-1] == 'Q' || q[-1] == 'q' || q[-1] == 'O' || q[-1] == 'o')
-        radix = 8, q--;
-    else if (q[-1] == 'B' || q[-1] == 'b')
-        radix = 2, q--;
-    else
-        radix = 10;
+    len = q-r;
+    if (!len) {
+       /* Not numeric */
+       *error = true;
+       return 0;
+    }
 
     /*
-     * If this number has been found for us by something other than
-     * the ordinary scanners, then it might be malformed by having
-     * nothing between the prefix and the suffix. Check this case
-     * now.
+     * Handle radix formats:
+     *
+     * 0<radix-letter><string>
+     * $<string>               (hexadecimal)
+     * <string><radix-letter>
      */
-    if (r >= q) {
-        *error = TRUE;
-        return 0;
+    pradix = sradix = 0;
+    plen = slen = 0;
+
+    if (len > 2 && *r == '0' && (pradix = radix_letter(r[1])) != 0)
+       plen = 2;
+    else if (len > 1 && *r == '$')
+       pradix = 16, plen = 1;
+
+    if (len > 1 && (sradix = radix_letter(q[-1])) != 0)
+       slen = 1;
+
+    if (pradix > sradix) {
+       radix = pradix;
+       r += plen;
+    } else if (sradix > pradix) {
+       radix = sradix;
+       q -= slen;
+    } else {
+       /* Either decimal, or invalid -- if invalid, we'll trip up
+          further down. */
+       radix = 10;
     }
-    
+
     /*
-     * `checklimit' must be 2**(32|64) / radix. We can't do that in
-     * 32/64-bit arithmetic, which we're (probably) using, so we
+     * `checklimit' must be 2**64 / radix. We can't do that in
+     * 64-bit arithmetic, which we're (probably) using, so we
      * cheat: since we know that all radices we use are even, we
-     * can divide 2**(31|63) by radix/2 instead.
+     * can divide 2**63 by radix/2 instead.
      */
-    if (globalbits == 64)
-        checklimit = 0x8000000000000000ULL / (radix >> 1);
-    else
-        checklimit = 0x80000000UL / (radix >> 1);
+    checklimit = 0x8000000000000000ULL / (radix >> 1);
 
     /*
      * Calculate the highest allowable value for the last digit of a
-     * 32-bit constant... in radix 10, it is 6, otherwise it is 0
+     * 64-bit constant... in radix 10, it is 6, otherwise it is 0
      */
     last = (radix == 10 ? 6 : 0);
 
     result = 0;
     while (*r && r < q) {
-        if (*r < '0' || (*r > '9' && *r < 'A')
-            || (digit = numvalue(*r)) >= radix) {
-            *error = TRUE;
-            return 0;
-        }
-        if (result > checklimit || (result == checklimit && digit >= last)) {
-            warn = TRUE;
-        }
-
-        result = radix * result + digit;
+       if (*r != '_') {
+           if (*r < '0' || (*r > '9' && *r < 'A')
+               || (digit = numvalue(*r)) >= radix) {
+               *error = true;
+               return 0;
+           }
+           if (result > checklimit ||
+               (result == checklimit && digit >= last)) {
+               warn = true;
+           }
+
+           result = radix * result + digit;
+       }
         r++;
     }
 
     if (warn)
         nasm_malloc_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
-                          "numeric constant %s does not fit in 32 bits",
+                          "numeric constant %s does not fit in 64 bits",
                           str);
 
     return result * sign;
 }
 
-int64_t readstrnum(char *str, int length, int *warn)
+int64_t readstrnum(char *str, int length, bool *warn)
 {
     int64_t charconst = 0;
     int i;
 
-    *warn = FALSE;
+    *warn = false;
 
     str += length;
     if (globalbits == 64) {
         for (i = 0; i < length; i++) {
             if (charconst & 0xFF00000000000000ULL)
-                *warn = TRUE;
+                *warn = true;
             charconst = (charconst << 8) + (uint8_t)*--str;
         }
     } else {
         for (i = 0; i < length; i++) {
             if (charconst & 0xFF000000UL)
-                *warn = TRUE;
+                *warn = true;
             charconst = (charconst << 8) + (uint8_t)*--str;
         }
     }
@@ -292,20 +358,60 @@ int32_t seg_alloc(void)
     return (next_seg += 2) - 2;
 }
 
-void fwriteint16_t(int data, FILE * fp)
+#if X86_MEMORY
+
+void fwriteint16_t(uint16_t data, FILE * fp)
+{
+    fwrite(&data, 1, 2, fp);
+}
+
+void fwriteint32_t(uint32_t data, FILE * fp)
+{
+    fwrite(&data, 1, 4, fp);
+}
+
+void fwriteint64_t(uint64_t data, FILE * fp)
+{
+    fwrite(&data, 1, 8, fp);
+}
+
+void fwriteaddr(uint64_t data, int size, FILE * fp)
+{
+    fwrite(&data, 1, size, fp);
+}
+
+#else /* !X86_MEMORY */
+
+void fwriteint16_t(uint16_t data, FILE * fp)
+{
+    char buffer[2], *p = buffer;
+    WRITESHORT(p, data);
+    fwrite(buffer, 1, 2, fp);
+}
+
+void fwriteint32_t(uint32_t data, FILE * fp)
 {
-    fputc((int)(data & 255), fp);
-    fputc((int)((data >> 8) & 255), fp);
+    char buffer[4], *p = buffer;
+    WRITELONG(p, data);
+    fwrite(buffer, 1, 4, fp);
 }
 
-void fwriteint32_t(int32_t data, FILE * fp)
+void fwriteint64_t(uint64_t data, FILE * fp)
 {
-    fputc((int)(data & 255), fp);
-    fputc((int)((data >> 8) & 255), fp);
-    fputc((int)((data >> 16) & 255), fp);
-    fputc((int)((data >> 24) & 255), fp);
+    char buffer[8], *p = buffer;
+    WRITEDLONG(p, data);
+    fwrite(buffer, 1, 8, fp);
 }
 
+void fwriteaddr(uint64_t data, int size, FILE * fp)
+{
+    char buffer[8], *p = buffer;
+    WRITEADDR(p, data, size);
+    fwrite(buffer, 1, size, fp);
+}
+
+#endif
+
 void standard_extension(char *inname, char *outname, char *extension,
                         efunc error)
 {
@@ -339,7 +445,7 @@ void standard_extension(char *inname, char *outname, char *extension,
 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
 
-#define LAYERSIZ(r) ( (r)->layers==0 ? RAA_BLKSIZE : RAA_LAYERSIZE )
+#define LAYERSHIFT(r) ( (r)->layers==0 ? RAA_BLKSHIFT : RAA_LAYERSHIFT )
 
 static struct RAA *real_raa_init(int layers)
 {
@@ -347,18 +453,14 @@ static struct RAA *real_raa_init(int layers)
     int i;
 
     if (layers == 0) {
-        r = nasm_malloc(LEAFSIZ);
-        r->layers = 0;
-        memset(r->u.l.data, 0, sizeof(r->u.l.data));
-        r->stepsize = 1L;
+        r = nasm_zalloc(LEAFSIZ);
+        r->shift = 0;
     } else {
         r = nasm_malloc(BRANCHSIZ);
         r->layers = layers;
         for (i = 0; i < RAA_LAYERSIZE; i++)
             r->u.b.data[i] = NULL;
-        r->stepsize = RAA_BLKSIZE;
-        while (--layers)
-            r->stepsize *= RAA_LAYERSIZE;
+        r->shift = (RAA_BLKSHIFT-RAA_LAYERSHIFT) + layers*RAA_LAYERSHIFT;
     }
     return r;
 }
@@ -370,39 +472,37 @@ struct RAA *raa_init(void)
 
 void raa_free(struct RAA *r)
 {
-    if (r->layers == 0)
-        nasm_free(r);
-    else {
+    if (r->layers) {
         struct RAA **p;
         for (p = r->u.b.data; p - r->u.b.data < RAA_LAYERSIZE; p++)
             if (*p)
                 raa_free(*p);
     }
+    nasm_free(r);
 }
 
-int32_t raa_read(struct RAA *r, int32_t posn)
+int64_t raa_read(struct RAA *r, int32_t posn)
 {
-    if (posn >= r->stepsize * LAYERSIZ(r))
+    if ((uint32_t)posn >= (UINT32_C(1) << (r->shift + LAYERSHIFT(r))))
         return 0;               /* Return 0 for undefined entries */
     while (r->layers > 0) {
-        ldiv_t l;
-        l = ldiv(posn, r->stepsize);
-        r = r->u.b.data[l.quot];
-        posn = l.rem;
+       int32_t l = posn >> r->shift;
+       posn &= (UINT32_C(1) << r->shift)-1;
+        r = r->u.b.data[l];
         if (!r)
             return 0;           /* Return 0 for undefined entries */
     }
     return r->u.l.data[posn];
 }
 
-struct RAA *raa_write(struct RAA *r, int32_t posn, int32_t value)
+struct RAA *raa_write(struct RAA *r, int32_t posn, int64_t value)
 {
     struct RAA *result;
 
     if (posn < 0)
         nasm_malloc_error(ERR_PANIC, "negative position in raa_write");
 
-    while (r->stepsize * LAYERSIZ(r) <= posn) {
+    while ((UINT32_C(1) << (r->shift+LAYERSHIFT(r))) <= (uint32_t)posn) {
         /*
          * Must add a layer.
          */
@@ -413,7 +513,7 @@ struct RAA *raa_write(struct RAA *r, int32_t posn, int32_t value)
         for (i = 0; i < RAA_LAYERSIZE; i++)
             s->u.b.data[i] = NULL;
         s->layers = r->layers + 1;
-        s->stepsize = LAYERSIZ(r) * r->stepsize;
+        s->shift = LAYERSHIFT(r) + r->shift;
         s->u.b.data[0] = r;
         r = s;
     }
@@ -421,14 +521,13 @@ struct RAA *raa_write(struct RAA *r, int32_t posn, int32_t value)
     result = r;
 
     while (r->layers > 0) {
-        ldiv_t l;
         struct RAA **s;
-        l = ldiv(posn, r->stepsize);
-        s = &r->u.b.data[l.quot];
+       int32_t l = posn >> r->shift;
+       posn &= (UINT32_C(1) << r->shift)-1;
+        s = &r->u.b.data[l];
         if (!*s)
             *s = real_raa_init(r->layers - 1);
         r = *s;
-        posn = l.rem;
     }
 
     r->u.l.data[posn] = value;
@@ -436,609 +535,353 @@ struct RAA *raa_write(struct RAA *r, int32_t posn, int32_t value)
     return result;
 }
 
-#define SAA_MAXLEN 8192
+/* Aggregate SAA components smaller than this */
+#define SAA_BLKLEN 65536
 
-struct SAA *saa_init(int32_t elem_len)
+struct SAA *saa_init(size_t elem_len)
 {
     struct SAA *s;
+    char *data;
+
+    s = nasm_zalloc(sizeof(struct SAA));
 
-    if (elem_len > SAA_MAXLEN)
-        nasm_malloc_error(ERR_PANIC | ERR_NOFILE,
-                          "SAA with huge elements");
+    if (elem_len >= SAA_BLKLEN)
+       s->blk_len = elem_len;
+    else
+       s->blk_len = SAA_BLKLEN - (SAA_BLKLEN % elem_len);
 
-    s = nasm_malloc(sizeof(struct SAA));
-    s->posn = s->start = 0L;
     s->elem_len = elem_len;
-    s->length = SAA_MAXLEN - (SAA_MAXLEN % elem_len);
-    s->data = nasm_malloc(s->length);
-    s->next = NULL;
-    s->end = s;
+    s->length = s->blk_len;
+    data = nasm_malloc(s->blk_len);
+    s->nblkptrs = s->nblks = 1;
+    s->blk_ptrs = nasm_malloc(sizeof(char *));
+    s->blk_ptrs[0] = data;
+    s->wblk = s->rblk = &s->blk_ptrs[0];
 
     return s;
 }
 
 void saa_free(struct SAA *s)
 {
-    struct SAA *t;
+    char **p;
+    size_t n;
+
+    for (p = s->blk_ptrs, n = s->nblks; n; p++, n--)
+       nasm_free(*p);
 
-    while (s) {
-        t = s->next;
-        nasm_free(s->data);
-        nasm_free(s);
-        s = t;
+    nasm_free(s->blk_ptrs);
+    nasm_free(s);
+}
+
+/* Add one allocation block to an SAA */
+static void saa_extend(struct SAA *s)
+{
+    size_t blkn = s->nblks++;
+
+    if (blkn >= s->nblkptrs) {
+       size_t rindex = s->rblk - s->blk_ptrs;
+       size_t windex = s->wblk - s->blk_ptrs;
+
+       s->nblkptrs <<= 1;
+       s->blk_ptrs = nasm_realloc(s->blk_ptrs, s->nblkptrs*sizeof(char *));
+
+       s->rblk = s->blk_ptrs + rindex;
+       s->wblk = s->blk_ptrs + windex;
     }
+
+    s->blk_ptrs[blkn] = nasm_malloc(s->blk_len);
+    s->length += s->blk_len;
 }
 
 void *saa_wstruct(struct SAA *s)
 {
     void *p;
 
-    if (s->end->length - s->end->posn < s->elem_len) {
-        s->end->next = nasm_malloc(sizeof(struct SAA));
-        s->end->next->start = s->end->start + s->end->posn;
-        s->end = s->end->next;
-        s->end->length = s->length;
-        s->end->next = NULL;
-        s->end->posn = 0L;
-        s->end->data = nasm_malloc(s->length);
+    if (s->wpos % s->elem_len)
+           nasm_malloc_error(ERR_PANIC|ERR_NOFILE,
+                             "misaligned wpos in saa_wstruct");
+
+    if (s->wpos + s->elem_len > s->blk_len) {
+       if (s->wpos != s->blk_len)
+           nasm_malloc_error(ERR_PANIC|ERR_NOFILE,
+                             "unfilled block in saa_wstruct");
+
+       if (s->wptr + s->elem_len > s->length)
+           saa_extend(s);
+       s->wblk++;
+       s->wpos = 0;
     }
 
-    p = s->end->data + s->end->posn;
-    s->end->posn += s->elem_len;
+    p = *s->wblk + s->wpos;
+    s->wpos += s->elem_len;
+    s->wptr += s->elem_len;
+
+    if (s->wptr > s->datalen)
+       s->datalen = s->wptr;
+
     return p;
 }
 
-void saa_wbytes(struct SAA *s, const void *data, int32_t len)
+void saa_wbytes(struct SAA *s, const void *data, size_t len)
 {
     const char *d = data;
 
-    while (len > 0) {
-        int32_t l = s->end->length - s->end->posn;
+    while (len) {
+        size_t l = s->blk_len - s->wpos;
         if (l > len)
             l = len;
-        if (l > 0) {
+        if (l) {
             if (d) {
-                memcpy(s->end->data + s->end->posn, d, l);
+                memcpy(*s->wblk + s->wpos, d, l);
                 d += l;
             } else
-                memset(s->end->data + s->end->posn, 0, l);
-            s->end->posn += l;
+                memset(*s->wblk + s->wpos, 0, l);
+            s->wpos += l;
+           s->wptr += l;
             len -= l;
+
+           if (s->datalen < s->wptr)
+               s->datalen = s->wptr;
         }
-        if (len > 0) {
-            s->end->next = nasm_malloc(sizeof(struct SAA));
-            s->end->next->start = s->end->start + s->end->posn;
-            s->end = s->end->next;
-            s->end->length = s->length;
-            s->end->next = NULL;
-            s->end->posn = 0L;
-            s->end->data = nasm_malloc(s->length);
-        }
+        if (len) {
+           if (s->wptr >= s->length)
+               saa_extend(s);
+           s->wblk++;
+           s->wpos = 0;
+       }
     }
 }
 
+/* write unsigned LEB128 value to SAA */
+void saa_wleb128u(struct SAA *psaa, int value)
+{
+  char temp[64], *ptemp;
+  uint8_t byte;
+  int len;
+
+  ptemp = temp;
+  len = 0;
+  do
+  {
+     byte = value & 127;
+     value >>= 7;
+     if (value != 0) /* more bytes to come */
+        byte |= 0x80;
+     *ptemp = byte;
+     ptemp++;
+     len++;
+  } while (value != 0);
+  saa_wbytes(psaa, temp, len);  
+}
+
+/* write signed LEB128 value to SAA */
+void saa_wleb128s(struct SAA *psaa, int value)
+{
+  char temp[64], *ptemp;
+  uint8_t byte;
+  bool more, negative;
+  int size, len;
+
+  ptemp = temp;
+  more = 1;
+  negative = (value < 0);
+  size = sizeof(int) * 8;
+  len = 0;
+  while(more)
+  {
+    byte = value & 0x7f;
+    value >>= 7;
+    if (negative)
+     /* sign extend */
+     value |= - (1 <<(size - 7));
+    /* sign bit of byte is second high order bit (0x40) */
+    if ((value == 0 && ! (byte & 0x40)) ||
+       ((value == -1) && (byte & 0x40)))
+       more = 0;
+    else
+      byte |= 0x80;
+    *ptemp = byte;
+    ptemp++;
+    len++;
+  } 
+  saa_wbytes(psaa, temp, len);  
+}
+
 void saa_rewind(struct SAA *s)
 {
-    s->rptr = s;
-    s->rpos = 0L;
+    s->rblk = s->blk_ptrs;
+    s->rpos = s->rptr = 0;
 }
 
 void *saa_rstruct(struct SAA *s)
 {
     void *p;
 
-    if (!s->rptr)
-        return NULL;
+    if (s->rptr + s->elem_len > s->datalen)
+       return NULL;
+
+    if (s->rpos % s->elem_len)
+           nasm_malloc_error(ERR_PANIC|ERR_NOFILE,
+                             "misaligned rpos in saa_rstruct");
 
-    if (s->rptr->posn - s->rpos < s->elem_len) {
-        s->rptr = s->rptr->next;
-        if (!s->rptr)
-            return NULL;        /* end of array */
-        s->rpos = 0L;
+    if (s->rpos + s->elem_len > s->blk_len) {
+       s->rblk++;
+       s->rpos = 0;
     }
 
-    p = s->rptr->data + s->rpos;
+    p = *s->rblk + s->rpos;
     s->rpos += s->elem_len;
+    s->rptr += s->elem_len;
+
     return p;
 }
 
-void *saa_rbytes(struct SAA *s, int32_t *len)
+const void *saa_rbytes(struct SAA *s, size_t *lenp)
 {
-    void *p;
+    const void *p;
+    size_t len;
 
-    if (!s->rptr)
-        return NULL;
+    if (s->rptr >= s->datalen) {
+       *lenp = 0;
+       return NULL;
+    }
 
-    p = s->rptr->data + s->rpos;
-    *len = s->rptr->posn - s->rpos;
-    s->rptr = s->rptr->next;
-    s->rpos = 0L;
-    return p;
-}
+    if (s->rpos >= s->blk_len) {
+       s->rblk++;
+       s->rpos = 0;
+    }
 
-void saa_rnbytes(struct SAA *s, void *data, int32_t len)
-{
-    char *d = data;
+    len = *lenp;
+    if (len > s->datalen - s->rptr)
+       len = s->datalen - s->rptr;
+    if (len > s->blk_len - s->rpos)
+       len = s->blk_len - s->rpos;
 
-    while (len > 0) {
-        int32_t l;
+    *lenp = len;
+    p = *s->rblk + s->rpos;
 
-        if (!s->rptr)
-            return;
+    s->rpos += len;
+    s->rptr += len;
 
-        l = s->rptr->posn - s->rpos;
-        if (l > len)
-            l = len;
-        if (l > 0) {
-            memcpy(d, s->rptr->data + s->rpos, l);
-            d += l;
-            s->rpos += l;
-            len -= l;
-        }
-        if (len > 0) {
-            s->rptr = s->rptr->next;
-            s->rpos = 0L;
-        }
-    }
+    return p;
 }
 
-void saa_fread(struct SAA *s, int32_t posn, void *data, int32_t len)
+void saa_rnbytes(struct SAA *s, void *data, size_t len)
 {
-    struct SAA *p;
-    int64_t pos;
-    char *cdata = data;
-
-    if (!s->rptr || posn < s->rptr->start)
-        saa_rewind(s);
-    p = s->rptr;
-    while (posn >= p->start + p->posn) {
-        p = p->next;
-        if (!p)
-            return;             /* what else can we do?! */
+    char *d = data;
+
+    if (s->rptr + len > s->datalen) {
+       nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_rnbytes");
+       return;
     }
 
-    pos = posn - p->start;
     while (len) {
-        int64_t l = p->posn - pos;
-        if (l > len)
-            l = len;
-        memcpy(cdata, p->data + pos, l);
-        len -= l;
-        cdata += l;
-        p = p->next;
-        if (!p)
-            return;
-        pos = 0LL;
-    }
-    s->rptr = p;
-}
+        size_t l;
+       const void *p;
 
-void saa_fwrite(struct SAA *s, int32_t posn, void *data, int32_t len)
-{
-    struct SAA *p;
-    int64_t pos;
-    char *cdata = data;
-
-    if (!s->rptr || posn < s->rptr->start)
-        saa_rewind(s);
-    p = s->rptr;
-    while (posn >= p->start + p->posn) {
-        p = p->next;
-        if (!p)
-            return;             /* what else can we do?! */
-    }
+       l = len;
+       p = saa_rbytes(s, &l);
 
-    pos = posn - p->start;
-    while (len) {
-        int64_t l = p->posn - pos;
-        if (l > len)
-            l = len;
-        memcpy(p->data + pos, cdata, l);
-        len -= l;
-        cdata += l;
-        p = p->next;
-        if (!p)
-            return;
-        pos = 0LL;
+       memcpy(d, p, l);
+       d   += l;
+       len -= l;
     }
-    s->rptr = p;
 }
 
-void saa_fpwrite(struct SAA *s, FILE * fp)
+/* Same as saa_rnbytes, except position the counter first */
+void saa_fread(struct SAA *s, size_t posn, void *data, size_t len)
 {
-    char *data;
-    int32_t len;
-
-    saa_rewind(s);
-//    while ((data = saa_rbytes(s, &len)))
-    for (; (data = saa_rbytes(s, &len));)
-        fwrite(data, 1, len, fp);
-}
+    size_t ix;
 
-/*
- * Register, instruction, condition-code and prefix keywords used
- * by the scanner.
- */
-#include "names.c"
-static const char *special_names[] = {
-    "byte", "dword", "far", "long", "near", "nosplit", "qword",
-    "short", "strict", "to", "tword", "word"
-};
-static const char *prefix_names[] = {
-    "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
-    "repnz", "repz", "times"
-};
-
-/*
- * Standard scanner routine used by parser.c and some output
- * formats. It keeps a succession of temporary-storage strings in
- * stdscan_tempstorage, which can be cleared using stdscan_reset.
- */
-static char **stdscan_tempstorage = NULL;
-static int stdscan_tempsize = 0, stdscan_templen = 0;
-#define STDSCAN_TEMP_DELTA 256
-
-static void stdscan_pop(void)
-{
-    nasm_free(stdscan_tempstorage[--stdscan_templen]);
-}
+    if (posn+len > s->datalen) {
+       nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_fread");
+       return;
+    }
 
-void stdscan_reset(void)
-{
-    while (stdscan_templen > 0)
-        stdscan_pop();
-}
+    ix = posn / s->blk_len;
+    s->rptr = posn;
+    s->rpos = posn % s->blk_len;
+    s->rblk = &s->blk_ptrs[ix];
 
-/*
- * Unimportant cleanup is done to avoid confusing people who are trying
- * to debug real memory leaks
- */
-void nasmlib_cleanup(void)
-{
-    stdscan_reset();
-    nasm_free(stdscan_tempstorage);
+    saa_rnbytes(s, data, len);
 }
 
-static char *stdscan_copy(char *p, int len)
+/* Same as saa_wbytes, except position the counter first */
+void saa_fwrite(struct SAA *s, size_t posn, const void *data, size_t len)
 {
-    char *text;
+    size_t ix;
 
-    text = nasm_malloc(len + 1);
-    strncpy(text, p, len);
-    text[len] = '\0';
-
-    if (stdscan_templen >= stdscan_tempsize) {
-        stdscan_tempsize += STDSCAN_TEMP_DELTA;
-        stdscan_tempstorage = nasm_realloc(stdscan_tempstorage,
-                                           stdscan_tempsize *
-                                           sizeof(char *));
+    if (posn > s->datalen) {
+       /* Seek beyond the end of the existing array not supported */
+       nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_fwrite");
+       return;
     }
-    stdscan_tempstorage[stdscan_templen++] = text;
-
-    return text;
-}
 
-char *stdscan_bufptr = NULL;
-int stdscan(void *private_data, struct tokenval *tv)
-{
-    char ourcopy[MAX_KEYWORD + 1], *r, *s;
+    ix = posn / s->blk_len;
+    s->wptr = posn;
+    s->wpos = posn % s->blk_len;
+    s->wblk = &s->blk_ptrs[ix];
 
-    (void)private_data;         /* Don't warn that this parameter is unused */
-
-    while (isspace(*stdscan_bufptr))
-        stdscan_bufptr++;
-    if (!*stdscan_bufptr)
-        return tv->t_type = 0;
-
-    /* we have a token; either an id, a number or a char */
-    if (isidstart(*stdscan_bufptr) ||
-        (*stdscan_bufptr == '$' && isidstart(stdscan_bufptr[1]))) {
-        /* now we've got an identifier */
-        int i;
-        int is_sym = FALSE;
-
-        if (*stdscan_bufptr == '$') {
-            is_sym = TRUE;
-            stdscan_bufptr++;
-        }
-
-        r = stdscan_bufptr++;
-        /* read the entire buffer to advance the buffer pointer but... */
-        while (isidchar(*stdscan_bufptr))
-            stdscan_bufptr++;
-
-        /* ... copy only up to IDLEN_MAX-1 characters */
-        tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r < IDLEN_MAX ?
-                                     stdscan_bufptr - r : IDLEN_MAX - 1);
-
-        if (is_sym || stdscan_bufptr - r > MAX_KEYWORD)
-            return tv->t_type = TOKEN_ID;       /* bypass all other checks */
-
-        for (s = tv->t_charptr, r = ourcopy; *s; s++)
-            *r++ = tolower(*s);
-        *r = '\0';
-        /* right, so we have an identifier sitting in temp storage. now,
-         * is it actually a register or instruction name, or what? */
-        if ((tv->t_integer = bsi(ourcopy, reg_names,
-                                 elements(reg_names))) >= 0) {
-            tv->t_integer += EXPR_REG_START;
-            return tv->t_type = TOKEN_REG;
-        } else if ((tv->t_integer = bsi(ourcopy, insn_names,
-                                        elements(insn_names))) >= 0) {
-            return tv->t_type = TOKEN_INSN;
-        }
-        for (i = 0; i < elements(icn); i++)
-            if (!strncmp(ourcopy, icn[i], strlen(icn[i]))) {
-                char *p = ourcopy + strlen(icn[i]);
-                tv->t_integer = ico[i];
-                if ((tv->t_inttwo = bsi(p, conditions,
-                                        elements(conditions))) >= 0)
-                    return tv->t_type = TOKEN_INSN;
-            }
-        if ((tv->t_integer = bsi(ourcopy, prefix_names,
-                                 elements(prefix_names))) >= 0) {
-            tv->t_integer += PREFIX_ENUM_START;
-            return tv->t_type = TOKEN_PREFIX;
-        }
-        if ((tv->t_integer = bsi(ourcopy, special_names,
-                                 elements(special_names))) >= 0)
-            return tv->t_type = TOKEN_SPECIAL;
-        if (!nasm_stricmp(ourcopy, "seg"))
-            return tv->t_type = TOKEN_SEG;
-        if (!nasm_stricmp(ourcopy, "wrt"))
-            return tv->t_type = TOKEN_WRT;
-        return tv->t_type = TOKEN_ID;
-    } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) {
-        /*
-         * It's a $ sign with no following hex number; this must
-         * mean it's a Here token ($), evaluating to the current
-         * assembly location, or a Base token ($$), evaluating to
-         * the base of the current segment.
-         */
-        stdscan_bufptr++;
-        if (*stdscan_bufptr == '$') {
-            stdscan_bufptr++;
-            return tv->t_type = TOKEN_BASE;
-        }
-        return tv->t_type = TOKEN_HERE;
-    } else if (isnumstart(*stdscan_bufptr)) {   /* now we've got a number */
-        int rn_error;
-
-        r = stdscan_bufptr++;
-        while (isnumchar(*stdscan_bufptr))
-            stdscan_bufptr++;
-
-        if (*stdscan_bufptr == '.') {
-            /*
-             * a floating point constant
-             */
-            stdscan_bufptr++;
-            while (isnumchar(*stdscan_bufptr) ||
-                   ((stdscan_bufptr[-1] == 'e'
-                     || stdscan_bufptr[-1] == 'E')
-                    && (*stdscan_bufptr == '-' || *stdscan_bufptr == '+'))) {
-                stdscan_bufptr++;
-            }
-            tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
-            return tv->t_type = TOKEN_FLOAT;
-        }
-        r = stdscan_copy(r, stdscan_bufptr - r);
-        tv->t_integer = readnum(r, &rn_error);
-        stdscan_pop();
-        if (rn_error)
-            return tv->t_type = TOKEN_ERRNUM;   /* some malformation occurred */
-        tv->t_charptr = NULL;
-        return tv->t_type = TOKEN_NUM;
-    } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"') {     /* a char constant */
-        char quote = *stdscan_bufptr++, *r;
-        int rn_warn;
-        r = tv->t_charptr = stdscan_bufptr;
-        while (*stdscan_bufptr && *stdscan_bufptr != quote)
-            stdscan_bufptr++;
-        tv->t_inttwo = stdscan_bufptr - r;      /* store full version */
-        if (!*stdscan_bufptr)
-            return tv->t_type = TOKEN_ERRNUM;   /* unmatched quotes */
-        stdscan_bufptr++;       /* skip over final quote */
-        tv->t_integer = readstrnum(r, tv->t_inttwo, &rn_warn);
-        /* FIXME: rn_warn is not checked! */
-        return tv->t_type = TOKEN_NUM;
-    } else if (*stdscan_bufptr == ';') {        /* a comment has happened - stay */
-        return tv->t_type = 0;
-    } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
-        stdscan_bufptr += 2;
-        return tv->t_type = TOKEN_SHR;
-    } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
-        stdscan_bufptr += 2;
-        return tv->t_type = TOKEN_SHL;
-    } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
-        stdscan_bufptr += 2;
-        return tv->t_type = TOKEN_SDIV;
-    } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
-        stdscan_bufptr += 2;
-        return tv->t_type = TOKEN_SMOD;
-    } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
-        stdscan_bufptr += 2;
-        return tv->t_type = TOKEN_EQ;
-    } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
-        stdscan_bufptr += 2;
-        return tv->t_type = TOKEN_NE;
-    } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
-        stdscan_bufptr += 2;
-        return tv->t_type = TOKEN_NE;
-    } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
-        stdscan_bufptr += 2;
-        return tv->t_type = TOKEN_LE;
-    } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
-        stdscan_bufptr += 2;
-        return tv->t_type = TOKEN_GE;
-    } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
-        stdscan_bufptr += 2;
-        return tv->t_type = TOKEN_DBL_AND;
-    } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
-        stdscan_bufptr += 2;
-        return tv->t_type = TOKEN_DBL_XOR;
-    } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
-        stdscan_bufptr += 2;
-        return tv->t_type = TOKEN_DBL_OR;
-    } else                      /* just an ordinary char */
-        return tv->t_type = (uint8_t)(*stdscan_bufptr++);
-}
+    if (!s->wpos) {
+       s->wpos = s->blk_len;
+       s->wblk--;
+    }
 
-/*
- * Return TRUE if the argument is a simple scalar. (Or a far-
- * absolute, which counts.)
- */
-int is_simple(expr * vect)
-{
-    while (vect->type && !vect->value)
-        vect++;
-    if (!vect->type)
-        return 1;
-    if (vect->type != EXPR_SIMPLE)
-        return 0;
-    do {
-        vect++;
-    } while (vect->type && !vect->value);
-    if (vect->type && vect->type < EXPR_SEGBASE + SEG_ABS)
-        return 0;
-    return 1;
+    saa_wbytes(s, data, len);
 }
 
-/*
- * Return TRUE if the argument is a simple scalar, _NOT_ a far-
- * absolute.
- */
-int is_really_simple(expr * vect)
+void saa_fpwrite(struct SAA *s, FILE * fp)
 {
-    while (vect->type && !vect->value)
-        vect++;
-    if (!vect->type)
-        return 1;
-    if (vect->type != EXPR_SIMPLE)
-        return 0;
-    do {
-        vect++;
-    } while (vect->type && !vect->value);
-    if (vect->type)
-        return 0;
-    return 1;
-}
+    const char *data;
+    size_t len;
 
-/*
- * Return TRUE if the argument is relocatable (i.e. a simple
- * scalar, plus at most one segment-base, plus possibly a WRT).
- */
-int is_reloc(expr * vect)
-{
-    while (vect->type && !vect->value)  /* skip initial value-0 terms */
-        vect++;
-    if (!vect->type)            /* trivially return TRUE if nothing */
-        return 1;               /* is present apart from value-0s */
-    if (vect->type < EXPR_SIMPLE)       /* FALSE if a register is present */
-        return 0;
-    if (vect->type == EXPR_SIMPLE) {    /* skip over a pure number term... */
-        do {
-            vect++;
-        } while (vect->type && !vect->value);
-        if (!vect->type)        /* ...returning TRUE if that's all */
-            return 1;
-    }
-    if (vect->type == EXPR_WRT) {       /* skip over a WRT term... */
-        do {
-            vect++;
-        } while (vect->type && !vect->value);
-        if (!vect->type)        /* ...returning TRUE if that's all */
-            return 1;
-    }
-    if (vect->value != 0 && vect->value != 1)
-        return 0;               /* segment base multiplier non-unity */
-    do {                        /* skip over _one_ seg-base term... */
-        vect++;
-    } while (vect->type && !vect->value);
-    if (!vect->type)            /* ...returning TRUE if that's all */
-        return 1;
-    return 0;                   /* And return FALSE if there's more */
+    saa_rewind(s);
+    while (len = s->datalen, (data = saa_rbytes(s, &len)) != NULL)
+        fwrite(data, 1, len, fp);
 }
 
 /*
- * Return TRUE if the argument contains an `unknown' part.
+ * Common list of prefix names
  */
-int is_unknown(expr * vect)
-{
-    while (vect->type && vect->type < EXPR_UNKNOWN)
-        vect++;
-    return (vect->type == EXPR_UNKNOWN);
-}
+static const char *prefix_names[] = {
+    "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
+    "repnz", "repz", "times"
+};
 
-/*
- * Return TRUE if the argument contains nothing but an `unknown'
- * part.
- */
-int is_just_unknown(expr * vect)
+const char *prefix_name(int token)
 {
-    while (vect->type && !vect->value)
-        vect++;
-    return (vect->type == EXPR_UNKNOWN);
-}
+    unsigned int prefix = token-PREFIX_ENUM_START;
+    if (prefix > elements(prefix_names))
+       return NULL;
 
-/*
- * Return the scalar part of a relocatable vector. (Including
- * simple scalar vectors - those qualify as relocatable.)
- */
-int64_t reloc_value(expr * vect)
-{
-    while (vect->type && !vect->value)
-        vect++;
-    if (!vect->type)
-        return 0;
-    if (vect->type == EXPR_SIMPLE)
-        return vect->value;
-    else
-        return 0;
+    return prefix_names[prefix];
 }
 
 /*
- * Return the segment number of a relocatable vector, or NO_SEG for
- * simple scalars.
+ * Binary search.
  */
-int32_t reloc_seg(expr * vect)
+int bsi(const char *string, const char **array, int size)
 {
-    while (vect->type && (vect->type == EXPR_WRT || !vect->value))
-        vect++;
-    if (vect->type == EXPR_SIMPLE) {
-        do {
-            vect++;
-        } while (vect->type && (vect->type == EXPR_WRT || !vect->value));
+    int i = -1, j = size;       /* always, i < index < j */
+    while (j - i >= 2) {
+        int k = (i + j) / 2;
+        int l = strcmp(string, array[k]);
+        if (l < 0)              /* it's in the first half */
+            j = k;
+        else if (l > 0)         /* it's in the second half */
+            i = k;
+        else                    /* we've got it :) */
+            return k;
     }
-    if (!vect->type)
-        return NO_SEG;
-    else
-        return vect->type - EXPR_SEGBASE;
-}
-
-/*
- * Return the WRT segment number of a relocatable vector, or NO_SEG
- * if no WRT part is present.
- */
-int32_t reloc_wrt(expr * vect)
-{
-    while (vect->type && vect->type < EXPR_WRT)
-        vect++;
-    if (vect->type == EXPR_WRT) {
-        return vect->value;
-    } else
-        return NO_SEG;
+    return -1;                  /* we haven't got it :( */
 }
 
-/*
- * Binary search.
- */
-int bsi(char *string, const char **array, int size)
+int bsii(const char *string, const char **array, int size)
 {
     int i = -1, j = size;       /* always, i < index < j */
     while (j - i >= 2) {
         int k = (i + j) / 2;
-        int l = strcmp(string, array[k]);
+        int l = nasm_stricmp(string, array[k]);
         if (l < 0)              /* it's in the first half */
             j = k;
         else if (l > 0)         /* it's in the second half */
@@ -1117,22 +960,39 @@ char *nasm_strcat(char *one, char *two)
 
 void null_debug_init(struct ofmt *of, void *id, FILE * fp, efunc error)
 {
+       (void)of;
+       (void)id;
+       (void)fp;
+       (void)error;
 }
 void null_debug_linenum(const char *filename, int32_t linenumber, int32_t segto)
 {
+       (void)filename;
+       (void)linenumber;
+       (void)segto;
 }
-void null_debug_deflabel(char *name, int32_t segment, int32_t offset,
+void null_debug_deflabel(char *name, int32_t segment, int64_t offset,
                          int is_global, char *special)
 {
+       (void)name;
+       (void)segment;
+       (void)offset;
+       (void)is_global;
+       (void)special;
 }
 void null_debug_routine(const char *directive, const char *params)
 {
+       (void)directive;
+       (void)params;
 }
 void null_debug_typevalue(int32_t type)
 {
+       (void)type;
 }
 void null_debug_output(int type, void *param)
 {
+       (void)type;
+       (void)param;
 }
 void null_debug_cleanup(void)
 {