-/* nasmlib.c library routines for the Netwide Assembler
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 1996-2012 The NASM Authors - All Rights Reserved
+ * See the file AUTHORS included with the NASM distribution for
+ * the specific copyright holders.
*
- * 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"
- * distributed in the NASM archive.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * nasmlib.c library routines for the Netwide Assembler
*/
+#include "compiler.h"
+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "insns.h"
int globalbits = 0; /* defined in nasm.h, works better here for ASM+DISASM */
-static efunc nasm_malloc_error;
+static vefunc nasm_verror; /* Global error handling function */
#ifdef LOGALLOC
static FILE *logfp;
#endif
-void nasm_set_malloc_error(efunc error)
+/* Uninitialized -> all zero by C spec */
+const uint8_t zero_buffer[ZERO_BUF_SIZE];
+
+/*
+ * Prepare a table of tolower() results. This avoids function calls
+ * on some platforms.
+ */
+
+unsigned char nasm_tolower_tab[256];
+
+void tolower_init(void)
+{
+ int i;
+
+ for (i = 0; i < 256; i++)
+ nasm_tolower_tab[i] = tolower(i);
+}
+
+void nasm_set_verror(vefunc ve)
+{
+ nasm_verror = ve;
+}
+
+void nasm_error(int severity, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ nasm_verror(severity, fmt, ap);
+ va_end(ap);
+}
+
+void nasm_init_malloc_error(void)
{
- nasm_malloc_error = error;
#ifdef LOGALLOC
logfp = fopen("malloc.log", "w");
- setvbuf(logfp, NULL, _IOLBF, BUFSIZ);
+ if (logfp) {
+ setvbuf(logfp, NULL, _IOLBF, BUFSIZ);
+ } else {
+ nasm_error(ERR_NONFATAL | ERR_NOFILE, "Unable to open %s", logfp);
+ logfp = stderr;
+ }
fprintf(logfp, "null pointer is %p\n", NULL);
#endif
}
#ifdef LOGALLOC
-void *nasm_malloc_log(char *file, int line, size_t size)
+void *nasm_malloc_log(const char *file, int line, size_t size)
#else
void *nasm_malloc(size_t size)
#endif
{
void *p = malloc(size);
if (!p)
- nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
+ nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
#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(const char *file, int line, size_t size)
+#else
+void *nasm_zalloc(size_t size)
+#endif
+{
+ void *p = calloc(size, 1);
+ if (!p)
+ nasm_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;
}
#ifdef LOGALLOC
-void *nasm_realloc_log(char *file, int line, void *q, size_t size)
+void *nasm_realloc_log(const char *file, int line, void *q, size_t size)
#else
void *nasm_realloc(void *q, size_t size)
#endif
{
void *p = q ? realloc(q, size) : malloc(size);
if (!p)
- nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
+ nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
#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;
}
#ifdef LOGALLOC
-void nasm_free_log(char *file, int line, void *q)
+void nasm_free_log(const char *file, int line, void *q)
#else
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);
}
}
#ifdef LOGALLOC
-char *nasm_strdup_log(char *file, int line, const char *s)
+char *nasm_strdup_log(const char *file, int line, const char *s)
#else
char *nasm_strdup(const char *s)
#endif
p = malloc(size);
if (!p)
- nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
+ nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
#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;
}
#ifdef LOGALLOC
-char *nasm_strndup_log(char *file, int line, char *s, size_t len)
+char *nasm_strndup_log(const char *file, int line, const char *s, size_t len)
#else
-char *nasm_strndup(char *s, size_t len)
+char *nasm_strndup(const char *s, size_t len)
#endif
{
char *p;
p = malloc(size);
if (!p)
- nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
+ nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
#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)
+no_return nasm_assert_failed(const char *file, int line, const char *msg)
+{
+ nasm_error(ERR_FATAL, "assertion %s failed at %s:%d", msg, file, line);
+ exit(1);
+}
+
+#ifndef nasm_stricmp
int nasm_stricmp(const char *s1, const char *s2)
{
- while (*s1 && tolower(*s1) == tolower(*s2))
- s1++, s2++;
- if (!*s1 && !*s2)
- return 0;
- else if (tolower(*s1) < tolower(*s2))
- return -1;
- else
- return 1;
+ unsigned char c1, c2;
+ int d;
+
+ while (1) {
+ c1 = nasm_tolower(*s1++);
+ c2 = nasm_tolower(*s2++);
+ d = c1-c2;
+
+ if (d)
+ return d;
+ if (!c1)
+ break;
+ }
+ return 0;
}
#endif
-#if !defined(strnicmp) && !defined(strncasecmp)
-int nasm_strnicmp(const char *s1, const char *s2, int n)
+#ifndef nasm_strnicmp
+int nasm_strnicmp(const char *s1, const char *s2, size_t n)
{
- while (n > 0 && *s1 && tolower(*s1) == tolower(*s2))
- s1++, s2++, n--;
- if ((!*s1 && !*s2) || n == 0)
- return 0;
- else if (tolower(*s1) < tolower(*s2))
- return -1;
- else
- return 1;
+ unsigned char c1, c2;
+ int d;
+
+ while (n--) {
+ c1 = nasm_tolower(*s1++);
+ c2 = nasm_tolower(*s2++);
+ d = c1-c2;
+
+ if (d)
+ return d;
+ if (!c1)
+ break;
+ }
+ return 0;
}
#endif
-#if !defined(strsep)
+int nasm_memicmp(const char *s1, const char *s2, size_t n)
+{
+ unsigned char c1, c2;
+ int d;
+
+ while (n--) {
+ c1 = nasm_tolower(*s1++);
+ c2 = nasm_tolower(*s2++);
+ d = c1-c2;
+ if (d)
+ return d;
+ }
+ return 0;
+}
+
+#ifndef nasm_strsep
char *nasm_strsep(char **stringp, const char *delim)
{
char *s = *stringp;
#endif
-#define lib_isnumchar(c) ( isalnum(c) || (c) == '$')
-#define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
+#define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
+
+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, int *error)
+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))
+ while (nasm_isspace(*r))
r++; /* find start of number */
/*
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 = UINT64_C(0x8000000000000000) / (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",
- str);
+ nasm_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
+ "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;
+ if (charconst & UINT64_C(0xFF00000000000000))
+ *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;
}
}
return (next_seg += 2) - 2;
}
-void fwriteint16_t(int data, FILE * fp)
-{
- fputc((int)(data & 255), fp);
- fputc((int)((data >> 8) & 255), fp);
-}
-
-void fwriteint32_t(int32_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);
-}
-
-void fwriteint64_t(int64_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);
- fputc((int)((data >> 32) & 255), fp);
- fputc((int)((data >> 40) & 255), fp);
- fputc((int)((data >> 48) & 255), fp);
- fputc((int)((data >> 56) & 255), fp);
-}
-
-void standard_extension(char *inname, char *outname, char *extension,
- efunc error)
-{
- char *p, *q;
-
- if (*outname) /* file name already exists, */
- return; /* so do nothing */
- q = inname;
- p = outname;
- while (*q)
- *p++ = *q++; /* copy, and find end of string */
- *p = '\0'; /* terminate it */
- while (p > outname && *--p != '.') ; /* find final period (or whatever) */
- if (*p != '.')
- while (*p)
- p++; /* go back to end if none found */
- if (!strcmp(p, extension)) { /* is the extension already there? */
- if (*extension)
- error(ERR_WARNING | ERR_NOFILE,
- "file name already ends in `%s': "
- "output will be in `nasm.out'", extension);
- else
- error(ERR_WARNING | ERR_NOFILE,
- "file name already has no extension: "
- "output will be in `nasm.out'");
- strcpy(outname, "nasm.out");
- } else
- strcpy(p, 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 )
-
-static struct RAA *real_raa_init(int layers)
-{
- struct RAA *r;
- 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;
- } 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;
- }
- return r;
-}
+#ifdef WORDS_LITTLEENDIAN
-struct RAA *raa_init(void)
+void fwriteint16_t(uint16_t data, FILE * fp)
{
- return real_raa_init(0);
+ fwrite(&data, 1, 2, fp);
}
-void raa_free(struct RAA *r)
+void fwriteint32_t(uint32_t data, FILE * fp)
{
- if (r->layers == 0)
- nasm_free(r);
- else {
- struct RAA **p;
- for (p = r->u.b.data; p - r->u.b.data < RAA_LAYERSIZE; p++)
- if (*p)
- raa_free(*p);
- }
+ fwrite(&data, 1, 4, fp);
}
-int32_t raa_read(struct RAA *r, int32_t posn)
+void fwriteint64_t(uint64_t data, FILE * fp)
{
- if (posn >= r->stepsize * LAYERSIZ(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;
- if (!r)
- return 0; /* Return 0 for undefined entries */
- }
- return r->u.l.data[posn];
+ fwrite(&data, 1, 8, fp);
}
-struct RAA *raa_write(struct RAA *r, int32_t posn, int32_t value)
+void fwriteaddr(uint64_t data, int size, FILE * fp)
{
- struct RAA *result;
-
- if (posn < 0)
- nasm_malloc_error(ERR_PANIC, "negative position in raa_write");
-
- while (r->stepsize * LAYERSIZ(r) <= posn) {
- /*
- * Must add a layer.
- */
- struct RAA *s;
- int i;
-
- s = nasm_malloc(BRANCHSIZ);
- 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->u.b.data[0] = r;
- r = s;
- }
-
- result = r;
-
- while (r->layers > 0) {
- ldiv_t l;
- struct RAA **s;
- l = ldiv(posn, r->stepsize);
- s = &r->u.b.data[l.quot];
- if (!*s)
- *s = real_raa_init(r->layers - 1);
- r = *s;
- posn = l.rem;
- }
-
- r->u.l.data[posn] = value;
-
- return result;
+ fwrite(&data, 1, size, fp);
}
-#define SAA_MAXLEN 8192
-
-struct SAA *saa_init(int32_t elem_len)
-{
- struct SAA *s;
-
- if (elem_len > SAA_MAXLEN)
- nasm_malloc_error(ERR_PANIC | ERR_NOFILE,
- "SAA with huge elements");
+#else /* not WORDS_LITTLEENDIAN */
- 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;
-
- return s;
-}
-
-void saa_free(struct SAA *s)
+void fwriteint16_t(uint16_t data, FILE * fp)
{
- struct SAA *t;
-
- while (s) {
- t = s->next;
- nasm_free(s->data);
- nasm_free(s);
- s = t;
- }
+ char buffer[2], *p = buffer;
+ WRITESHORT(p, data);
+ fwrite(buffer, 1, 2, fp);
}
-void *saa_wstruct(struct SAA *s)
+void fwriteint32_t(uint32_t data, FILE * fp)
{
- 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);
- }
-
- p = s->end->data + s->end->posn;
- s->end->posn += s->elem_len;
- return p;
+ char buffer[4], *p = buffer;
+ WRITELONG(p, data);
+ fwrite(buffer, 1, 4, fp);
}
-void saa_wbytes(struct SAA *s, const void *data, int32_t len)
+void fwriteint64_t(uint64_t data, FILE * fp)
{
- const char *d = data;
-
- while (len > 0) {
- int32_t l = s->end->length - s->end->posn;
- if (l > len)
- l = len;
- if (l > 0) {
- if (d) {
- memcpy(s->end->data + s->end->posn, d, l);
- d += l;
- } else
- memset(s->end->data + s->end->posn, 0, l);
- s->end->posn += l;
- len -= l;
- }
- 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);
- }
- }
+ char buffer[8], *p = buffer;
+ WRITEDLONG(p, data);
+ fwrite(buffer, 1, 8, fp);
}
-void saa_rewind(struct SAA *s)
+void fwriteaddr(uint64_t data, int size, FILE * fp)
{
- s->rptr = s;
- s->rpos = 0L;
+ char buffer[8], *p = buffer;
+ WRITEADDR(p, data, size);
+ fwrite(buffer, 1, size, fp);
}
-void *saa_rstruct(struct SAA *s)
-{
- void *p;
-
- if (!s->rptr)
- return NULL;
-
- 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;
- }
-
- p = s->rptr->data + s->rpos;
- s->rpos += s->elem_len;
- return p;
-}
-
-void *saa_rbytes(struct SAA *s, int32_t *len)
-{
- void *p;
-
- if (!s->rptr)
- return NULL;
-
- p = s->rptr->data + s->rpos;
- *len = s->rptr->posn - s->rpos;
- s->rptr = s->rptr->next;
- s->rpos = 0L;
- return p;
-}
-
-void saa_rnbytes(struct SAA *s, void *data, int32_t len)
-{
- char *d = data;
-
- while (len > 0) {
- int32_t l;
-
- if (!s->rptr)
- return;
-
- 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;
- }
- }
-}
+#endif
-void saa_fread(struct SAA *s, int32_t posn, void *data, int32_t len)
+size_t fwritezero(size_t bytes, FILE *fp)
{
- struct SAA *p;
- int64_t pos;
- char *cdata = data;
+ size_t count = 0;
+ size_t blksize;
+ size_t rv;
- 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?! */
- }
+ while (bytes) {
+ blksize = (bytes < ZERO_BUF_SIZE) ? bytes : ZERO_BUF_SIZE;
- 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;
-}
+ rv = fwrite(zero_buffer, 1, blksize, fp);
+ if (!rv)
+ break;
-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?! */
+ count += rv;
+ bytes -= rv;
}
- 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;
- }
- s->rptr = p;
+ return count;
}
-void saa_fpwrite(struct SAA *s, FILE * fp)
+void standard_extension(char *inname, char *outname, char *extension)
{
- char *data;
- int32_t len;
+ char *p, *q;
- saa_rewind(s);
-// while ((data = saa_rbytes(s, &len)))
- for (; (data = saa_rbytes(s, &len));)
- fwrite(data, 1, len, fp);
+ if (*outname) /* file name already exists, */
+ return; /* so do nothing */
+ q = inname;
+ p = outname;
+ while (*q)
+ *p++ = *q++; /* copy, and find end of string */
+ *p = '\0'; /* terminate it */
+ while (p > outname && *--p != '.') ; /* find final period (or whatever) */
+ if (*p != '.')
+ while (*p)
+ p++; /* go back to end if none found */
+ if (!strcmp(p, extension)) { /* is the extension already there? */
+ if (*extension)
+ nasm_error(ERR_WARNING | ERR_NOFILE,
+ "file name already ends in `%s': "
+ "output will be in `nasm.out'", extension);
+ else
+ nasm_error(ERR_WARNING | ERR_NOFILE,
+ "file name already has no extension: "
+ "output will be in `nasm.out'");
+ strcpy(outname, "nasm.out");
+ } else
+ strcpy(p, extension);
}
/*
* Common list of prefix names
*/
static const char *prefix_names[] = {
- "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
- "repnz", "repz", "times"
+ "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
+ "rep", "repe", "repne", "repnz", "repz", "times", "wait",
+ "xacquire", "xrelease"
};
const char *prefix_name(int token)
{
unsigned int prefix = token-PREFIX_ENUM_START;
- if (prefix > sizeof prefix_names / sizeof(const char *))
+ if (prefix >= ARRAY_SIZE(prefix_names))
return NULL;
return prefix_names[prefix];
}
/*
- * 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;
-}
-
-/*
- * Return TRUE if the argument is a simple scalar, _NOT_ a far-
- * absolute.
- */
-int is_really_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)
- return 0;
- return 1;
-}
-
-/*
- * 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 */
-}
-
-/*
- * Return TRUE if the argument contains an `unknown' part.
- */
-int is_unknown(expr * vect)
-{
- while (vect->type && vect->type < EXPR_UNKNOWN)
- vect++;
- return (vect->type == EXPR_UNKNOWN);
-}
-
-/*
- * Return TRUE if the argument contains nothing but an `unknown'
- * part.
- */
-int is_just_unknown(expr * vect)
-{
- while (vect->type && !vect->value)
- vect++;
- return (vect->type == EXPR_UNKNOWN);
-}
-
-/*
- * 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 the segment number of a relocatable vector, or NO_SEG for
- * simple scalars.
- */
-int32_t reloc_seg(expr * vect)
-{
- 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));
- }
- 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;
-}
-
-/*
* Binary search.
*/
-int bsi(char *string, const char **array, int size)
+int bsi(const char *string, const char **array, int size)
{
int i = -1, j = size; /* always, i < index < j */
while (j - i >= 2) {
return -1; /* we haven't got it :( */
}
-int bsii(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) {
return 0;
}
-void nasm_quote(char **str)
-{
- int ln = strlen(*str);
- char q = (*str)[0];
- char *p;
- if (ln > 1 && (*str)[ln - 1] == q && (q == '"' || q == '\''))
- return;
- q = '"';
- if (strchr(*str, q))
- q = '\'';
- p = nasm_malloc(ln + 3);
- strcpy(p + 1, *str);
- nasm_free(*str);
- p[ln + 1] = p[0] = q;
- p[ln + 2] = 0;
- *str = p;
-}
-
-char *nasm_strcat(char *one, char *two)
+char *nasm_strcat(const char *one, const char *two)
{
char *rslt;
int l1 = strlen(one);
return rslt;
}
-void null_debug_init(struct ofmt *of, void *id, FILE * fp, efunc error)
+/* skip leading spaces */
+char *nasm_skip_spaces(const char *p)
{
- (void)of;
- (void)id;
- (void)fp;
- (void)error;
+ if (p)
+ while (*p && nasm_isspace(*p))
+ p++;
+ return (char *)p;
}
-void null_debug_linenum(const char *filename, int32_t linenumber, int32_t segto)
+
+/* skip leading non-spaces */
+char *nasm_skip_word(const char *p)
{
- (void)filename;
- (void)linenumber;
- (void)segto;
+ if (p)
+ while (*p && !nasm_isspace(*p))
+ p++;
+ return (char *)p;
}
-void null_debug_deflabel(char *name, int32_t segment, int32_t offset,
- int is_global, char *special)
+
+/* zap leading spaces with zero */
+char *nasm_zap_spaces_fwd(char *p)
{
- (void)name;
- (void)segment;
- (void)offset;
- (void)is_global;
- (void)special;
+ if (p)
+ while (*p && nasm_isspace(*p))
+ *p++ = 0x0;
+ return p;
}
-void null_debug_routine(const char *directive, const char *params)
+
+/* zap spaces with zero in reverse order */
+char *nasm_zap_spaces_rev(char *p)
{
- (void)directive;
- (void)params;
+ if (p)
+ while (*p && nasm_isspace(*p))
+ *p-- = 0x0;
+ return p;
}
-void null_debug_typevalue(int32_t type)
+
+/* zap leading and trailing spaces */
+char *nasm_trim_spaces(char *p)
{
- (void)type;
+ p = nasm_zap_spaces_fwd(p);
+ nasm_zap_spaces_fwd(nasm_skip_word(p));
+
+ return p;
}
-void null_debug_output(int type, void *param)
+
+/*
+ * return the word extracted from a stream
+ * or NULL if nothing left
+ */
+char *nasm_get_word(char *p, char **tail)
{
- (void)type;
- (void)param;
+ char *word = nasm_skip_spaces(p);
+ char *next = nasm_skip_word(word);
+
+ if (word && *word) {
+ if (*next)
+ *next++ = '\0';
+ } else
+ word = next = NULL;
+
+ /* NOTE: the tail may start with spaces */
+ *tail = next;
+
+ return word;
}
-void null_debug_cleanup(void)
+
+/*
+ * Extract "opt=val" values from the stream and
+ * returns "opt"
+ *
+ * Exceptions:
+ * 1) If "=val" passed the NULL returned though
+ * you may continue handling the tail via "next"
+ * 2) If "=" passed the NULL is returned and "val"
+ * is set to NULL as well
+ */
+char *nasm_opt_val(char *p, char **val, char **next)
{
-}
+ char *q, *nxt;
-struct dfmt null_debug_form = {
- "Null debug format",
- "null",
- null_debug_init,
- null_debug_linenum,
- null_debug_deflabel,
- null_debug_routine,
- null_debug_typevalue,
- null_debug_output,
- null_debug_cleanup
-};
+ *val = *next = NULL;
+
+ p = nasm_get_word(p, &nxt);
+ if (!p)
+ return NULL;
+
+ q = strchr(p, '=');
+ if (q) {
+ if (q == p)
+ p = NULL;
+ *q++='\0';
+ if (*q) {
+ *val = q;
+ } else {
+ q = nasm_get_word(q + 1, &nxt);
+ if (q)
+ *val = q;
+ }
+ } else {
+ q = nasm_skip_spaces(nxt);
+ if (q && *q == '=') {
+ q = nasm_get_word(q + 1, &nxt);
+ if (q)
+ *val = q;
+ }
+ }
+
+ *next = nxt;
+ return p;
+}
-struct dfmt *null_debug_arr[2] = { &null_debug_form, NULL };
+/*
+ * initialized data bytes length from opcode
+ */
+int idata_bytes(int opcode)
+{
+ switch (opcode) {
+ case I_DB:
+ return 1;
+ case I_DW:
+ return 2;
+ case I_DD:
+ return 4;
+ case I_DQ:
+ return 8;
+ case I_DT:
+ return 10;
+ case I_DO:
+ return 16;
+ case I_DY:
+ return 32;
+ case I_none:
+ return -1;
+ default:
+ return 0;
+ }
+}