nasm/nasmlib.c

705 lines
15 KiB
C
Raw Normal View History

/* ----------------------------------------------------------------------- *
*
* Copyright 1996-2016 The NASM Authors - All Rights Reserved
* See the file AUTHORS included with the NASM distribution for
* the specific copyright holders.
2002-04-30 20:51:32 +00:00
*
* 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
2002-04-30 20:51:32 +00:00
*/
#include "compiler.h"
2002-04-30 20:51:32 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
2002-04-30 20:51:32 +00:00
#include "nasm.h"
#include "nasmlib.h"
#include "insns.h"
2002-04-30 20:51:32 +00:00
int globalbits = 0; /* defined in nasm.h, works better here for ASM+DISASM */
vefunc nasm_verror; /* Global error handling function */
2002-04-30 20:51:32 +00:00
/* 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_error(int severity, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
nasm_verror(severity, fmt, ap);
va_end(ap);
}
no_return nasm_fatal(int flags, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
nasm_verror(flags | ERR_FATAL, fmt, ap);
abort(); /* We should never get here */
}
no_return nasm_panic(int flags, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
nasm_verror(flags | ERR_PANIC, fmt, ap);
abort(); /* We should never get here */
}
no_return nasm_panic_from_macro(const char *file, int line)
{
nasm_panic(ERR_NOFILE, "Internal error at %s:%d\n", file, line);
}
2005-01-15 22:15:51 +00:00
void *nasm_malloc(size_t size)
2002-04-30 20:52:08 +00:00
{
2002-04-30 20:51:32 +00:00
void *p = malloc(size);
if (!p)
nasm_fatal(ERR_NOFILE, "out of memory");
return p;
}
void *nasm_zalloc(size_t size)
{
void *p = calloc(size, 1);
if (!p)
nasm_fatal(ERR_NOFILE, "out of memory");
2002-04-30 20:51:32 +00:00
return p;
}
2005-01-15 22:15:51 +00:00
void *nasm_realloc(void *q, size_t size)
2002-04-30 20:52:08 +00:00
{
2002-04-30 20:51:32 +00:00
void *p = q ? realloc(q, size) : malloc(size);
if (!p)
nasm_fatal(ERR_NOFILE, "out of memory");
2002-04-30 20:51:32 +00:00
return p;
}
2005-01-15 22:15:51 +00:00
void nasm_free(void *q)
2002-04-30 20:52:08 +00:00
{
if (q)
free(q);
2002-04-30 20:51:32 +00:00
}
char *nasm_strdup(const char *s)
2002-04-30 20:52:08 +00:00
{
char *p;
2005-01-15 22:15:51 +00:00
int size = strlen(s) + 1;
2002-04-30 20:51:32 +00:00
2002-04-30 20:52:08 +00:00
p = malloc(size);
if (!p)
nasm_fatal(ERR_NOFILE, "out of memory");
2005-01-15 22:15:51 +00:00
strcpy(p, s);
2002-04-30 20:51:32 +00:00
return p;
}
char *nasm_strndup(const char *s, size_t len)
2002-04-30 20:52:49 +00:00
{
char *p;
2005-01-15 22:15:51 +00:00
int size = len + 1;
2002-04-30 20:52:49 +00:00
p = malloc(size);
if (!p)
nasm_fatal(ERR_NOFILE, "out of memory");
2005-01-15 22:15:51 +00:00
strncpy(p, s, len);
2002-04-30 20:52:49 +00:00
p[len] = '\0';
return p;
}
no_return nasm_assert_failed(const char *file, int line, const char *msg)
{
nasm_fatal(0, "assertion %s failed at %s:%d", msg, file, line);
exit(1);
}
void nasm_write(const void *ptr, size_t size, FILE *f)
{
size_t n = fwrite(ptr, 1, size, f);
if (n != size)
nasm_fatal(0, "unable to write output: %s", strerror(errno));
}
#ifndef nasm_stricmp
int nasm_stricmp(const char *s1, const char *s2)
2002-04-30 20:53:55 +00:00
{
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;
2002-04-30 20:51:32 +00:00
}
2002-04-30 20:59:21 +00:00
#endif
2002-04-30 20:51:32 +00:00
#ifndef nasm_strnicmp
int nasm_strnicmp(const char *s1, const char *s2, size_t n)
2002-04-30 20:53:55 +00:00
{
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;
2002-04-30 20:51:32 +00:00
}
2002-04-30 20:59:21 +00:00
#endif
2002-04-30 20:51:32 +00:00
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;
char *e;
if (!s)
return NULL;
e = strpbrk(s, delim);
if (e)
*e++ = '\0';
*stringp = e;
return s;
}
#endif
#define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
2002-04-30 20:51:32 +00:00
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)
2002-04-30 20:53:55 +00:00
{
char *r = str, *q;
int32_t pradix, sradix, radix;
int plen, slen, len;
uint64_t result, checklimit;
2002-04-30 20:53:55 +00:00
int digit, last;
bool warn = false;
2002-04-30 20:53:55 +00:00
int sign = 1;
2002-04-30 20:51:32 +00:00
*error = false;
2002-04-30 20:51:32 +00:00
while (nasm_isspace(*r))
2005-01-15 22:15:51 +00:00
r++; /* find start of number */
2002-04-30 20:53:55 +00:00
/*
* If the number came from make_tok_num (as a result of an %assign), it
* might have a '-' built into it (rather than in a preceeding token).
*/
2005-01-15 22:15:51 +00:00
if (*r == '-') {
r++;
sign = -1;
2002-04-30 20:53:55 +00:00
}
2002-04-30 20:51:32 +00:00
q = r;
2005-01-15 22:15:51 +00:00
while (lib_isnumchar(*q))
q++; /* find end of number */
2002-04-30 20:51:32 +00:00
len = q-r;
if (!len) {
/* Not numeric */
*error = true;
return 0;
}
2002-04-30 20:51:32 +00:00
/*
* Handle radix formats:
*
* 0<radix-letter><string>
* $<string> (hexadecimal)
* <string><radix-letter>
2002-04-30 20:51:32 +00:00
*/
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;
}
2002-04-30 20:51:32 +00:00
2002-04-30 20:52:49 +00:00
/*
* `checklimit' must be 2**64 / radix. We can't do that in
* 64-bit arithmetic, which we're (probably) using, so we
2002-04-30 20:52:49 +00:00
* cheat: since we know that all radices we use are even, we
* can divide 2**63 by radix/2 instead.
2002-04-30 20:52:49 +00:00
*/
checklimit = UINT64_C(0x8000000000000000) / (radix >> 1);
2002-04-30 20:52:49 +00:00
2002-04-30 20:53:55 +00:00
/*
* Calculate the highest allowable value for the last digit of a
* 64-bit constant... in radix 10, it is 6, otherwise it is 0
2002-04-30 20:53:55 +00:00
*/
last = (radix == 10 ? 6 : 0);
2002-04-30 20:51:32 +00:00
result = 0;
while (*r && r < q) {
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;
}
2005-01-15 22:15:51 +00:00
r++;
2002-04-30 20:51:32 +00:00
}
2002-04-30 20:52:49 +00:00
if (warn)
nasm_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
"numeric constant %s does not fit in 64 bits",
str);
2002-04-30 20:52:49 +00:00
2005-01-15 22:15:51 +00:00
return result * sign;
2002-04-30 20:53:55 +00:00
}
int64_t readstrnum(char *str, int length, bool *warn)
2002-04-30 20:53:55 +00:00
{
int64_t charconst = 0;
2002-04-30 20:53:55 +00:00
int i;
*warn = false;
2002-04-30 20:53:55 +00:00
str += length;
if (globalbits == 64) {
for (i = 0; i < length; i++) {
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;
charconst = (charconst << 8) + (uint8_t)*--str;
2005-01-15 22:15:51 +00:00
}
2002-04-30 20:53:55 +00:00
}
return charconst;
2002-04-30 20:51:32 +00:00
}
int32_t seg_alloc(void)
2002-04-30 20:53:55 +00:00
{
static int32_t next_seg = 0;
int32_t this_seg = next_seg;
next_seg += 2;
return this_seg;
2002-04-30 20:51:32 +00:00
}
#ifdef WORDS_LITTLEENDIAN
void fwriteint16_t(uint16_t data, FILE * fp)
{
nasm_write(&data, 2, fp);
}
void fwriteint32_t(uint32_t data, FILE * fp)
{
nasm_write(&data, 4, fp);
}
void fwriteint64_t(uint64_t data, FILE * fp)
{
nasm_write(&data, 8, fp);
}
void fwriteaddr(uint64_t data, int size, FILE * fp)
{
nasm_write(&data, size, fp);
}
#else /* not WORDS_LITTLEENDIAN */
void fwriteint16_t(uint16_t data, FILE * fp)
2002-04-30 20:53:55 +00:00
{
char buffer[2], *p = buffer;
WRITESHORT(p, data);
nasm_write(buffer, 2, fp);
2002-04-30 20:51:32 +00:00
}
void fwriteint32_t(uint32_t data, FILE * fp)
2002-04-30 20:53:55 +00:00
{
char buffer[4], *p = buffer;
WRITELONG(p, data);
nasm_write(buffer, 4, fp);
2002-04-30 20:51:32 +00:00
}
void fwriteint64_t(uint64_t data, FILE * fp)
2007-04-28 06:18:48 +00:00
{
char buffer[8], *p = buffer;
WRITEDLONG(p, data);
nasm_write(buffer, 8, fp);
}
void fwriteaddr(uint64_t data, int size, FILE * fp)
{
char buffer[8], *p = buffer;
WRITEADDR(p, data, size);
nasm_write(buffer, size, fp);
2007-04-28 06:18:48 +00:00
}
#endif
void fwritezero(size_t bytes, FILE *fp)
{
size_t blksize;
while (bytes) {
blksize = (bytes < ZERO_BUF_SIZE) ? bytes : ZERO_BUF_SIZE;
nasm_write(zero_buffer, blksize, fp);
bytes -= blksize;
}
}
void standard_extension(char *inname, char *outname, char *extension)
2002-04-30 20:53:55 +00:00
{
char *p, *q;
2002-04-30 20:51:32 +00:00
2005-01-15 22:15:51 +00:00
if (*outname) /* file name already exists, */
return; /* so do nothing */
2002-04-30 20:51:32 +00:00
q = inname;
p = outname;
2005-01-15 22:15:51 +00:00
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);
2005-01-15 22:15:51 +00:00
else
nasm_error(ERR_WARNING | ERR_NOFILE,
"file name already has no extension: "
"output will be in `nasm.out'");
2005-01-15 22:15:51 +00:00
strcpy(outname, "nasm.out");
2002-04-30 20:51:32 +00:00
} else
2005-01-15 22:15:51 +00:00
strcpy(p, extension);
2002-04-30 20:51:32 +00:00
}
2002-04-30 20:52:49 +00:00
/*
* Common list of prefix names
2002-04-30 20:52:49 +00:00
*/
static const char *prefix_names[] = {
"a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
"rep", "repe", "repne", "repnz", "repz", "times", "wait",
"xacquire", "xrelease", "bnd"
2002-04-30 20:52:49 +00:00
};
const char *prefix_name(int token)
{
unsigned int prefix = token-PREFIX_ENUM_START;
if (prefix >= ARRAY_SIZE(prefix_names))
return NULL;
return prefix_names[prefix];
}
2002-04-30 20:52:49 +00:00
/*
* Binary search.
*/
int bsi(const char *string, const char **array, int size)
2002-04-30 20:53:55 +00:00
{
2005-01-15 22:15:51 +00:00
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;
2002-04-30 20:52:49 +00:00
}
2005-01-15 22:15:51 +00:00
return -1; /* we haven't got it :( */
2002-04-30 20:52:49 +00:00
}
2002-04-30 20:53:55 +00:00
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 = 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 */
i = k;
else /* we've got it :) */
return k;
}
return -1; /* we haven't got it :( */
}
char *nasm_strcat(const char *one, const char *two)
2002-04-30 20:53:55 +00:00
{
char *rslt;
2005-01-15 22:15:51 +00:00
int l1 = strlen(one);
rslt = nasm_malloc(l1 + strlen(two) + 1);
2002-04-30 20:53:55 +00:00
strcpy(rslt, one);
2005-01-15 22:15:51 +00:00
strcpy(rslt + l1, two);
2002-04-30 20:53:55 +00:00
return rslt;
}
/* skip leading spaces */
char *nasm_skip_spaces(const char *p)
{
if (p)
while (*p && nasm_isspace(*p))
p++;
return (char *)p;
}
/* skip leading non-spaces */
char *nasm_skip_word(const char *p)
{
if (p)
while (*p && !nasm_isspace(*p))
p++;
return (char *)p;
}
/* zap leading spaces with zero */
char *nasm_zap_spaces_fwd(char *p)
{
if (p)
while (*p && nasm_isspace(*p))
*p++ = 0x0;
return p;
}
/* zap spaces with zero in reverse order */
char *nasm_zap_spaces_rev(char *p)
{
if (p)
while (*p && nasm_isspace(*p))
*p-- = 0x0;
return p;
}
/* zap leading and trailing spaces */
char *nasm_trim_spaces(char *p)
{
p = nasm_zap_spaces_fwd(p);
nasm_zap_spaces_fwd(nasm_skip_word(p));
return p;
}
/*
* return the word extracted from a stream
* or NULL if nothing left
*/
char *nasm_get_word(char *p, char **tail)
{
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;
}
/*
* 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;
*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;
}
/*
* 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_DZ:
return 64;
case I_none:
return -1;
default:
return 0;
}
}