2002-05-01 04:51:32 +08:00
|
|
|
/* nasmlib.c library routines for the Netwide Assembler
|
|
|
|
*
|
|
|
|
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
|
|
|
|
* Julian Hall. All rights reserved. The software is
|
2007-12-29 22:44:23 +08:00
|
|
|
* redistributable under the license given in the file "LICENSE"
|
2002-05-01 04:51:32 +08:00
|
|
|
* distributed in the NASM archive.
|
|
|
|
*/
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#include "compiler.h"
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2007-04-12 10:40:54 +08:00
|
|
|
#include <inttypes.h>
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
#include "nasm.h"
|
|
|
|
#include "nasmlib.h"
|
2007-08-31 06:35:34 +08:00
|
|
|
#include "insns.h"
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
int globalbits = 0; /* defined in nasm.h, works better here for ASM+DISASM */
|
2007-09-29 01:50:20 +08:00
|
|
|
efunc nasm_malloc_error; /* Exported for the benefit of vsnprintf.c */
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:52:08 +08:00
|
|
|
#ifdef LOGALLOC
|
|
|
|
static FILE *logfp;
|
|
|
|
#endif
|
|
|
|
|
2008-06-12 06:49:41 +08:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
void nasm_set_malloc_error(efunc error)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
nasm_malloc_error = error;
|
2002-05-01 04:52:08 +08:00
|
|
|
#ifdef LOGALLOC
|
2005-01-16 06:15:51 +08:00
|
|
|
logfp = fopen("malloc.log", "w");
|
|
|
|
setvbuf(logfp, NULL, _IOLBF, BUFSIZ);
|
|
|
|
fprintf(logfp, "null pointer is %p\n", NULL);
|
2002-05-01 04:52:08 +08:00
|
|
|
#endif
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2002-05-01 04:52:08 +08:00
|
|
|
#ifdef LOGALLOC
|
2007-04-14 00:47:53 +08:00
|
|
|
void *nasm_malloc_log(char *file, int line, size_t size)
|
2002-05-01 04:52:08 +08:00
|
|
|
#else
|
2005-01-16 06:15:51 +08:00
|
|
|
void *nasm_malloc(size_t size)
|
2002-05-01 04:52:08 +08:00
|
|
|
#endif
|
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
void *p = malloc(size);
|
|
|
|
if (!p)
|
2005-01-16 06:15:51 +08:00
|
|
|
nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
|
2002-05-01 04:52:08 +08:00
|
|
|
#ifdef LOGALLOC
|
|
|
|
else
|
2005-01-16 06:15:51 +08:00
|
|
|
fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
|
2007-09-26 05:27:34 +08:00
|
|
|
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);
|
2002-05-01 04:52:08 +08:00
|
|
|
#endif
|
2002-05-01 04:51:32 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2002-05-01 04:52:08 +08:00
|
|
|
#ifdef LOGALLOC
|
2007-04-14 00:47:53 +08:00
|
|
|
void *nasm_realloc_log(char *file, int line, void *q, size_t size)
|
2002-05-01 04:52:08 +08:00
|
|
|
#else
|
2005-01-16 06:15:51 +08:00
|
|
|
void *nasm_realloc(void *q, size_t size)
|
2002-05-01 04:52:08 +08:00
|
|
|
#endif
|
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
void *p = q ? realloc(q, size) : malloc(size);
|
|
|
|
if (!p)
|
2005-01-16 06:15:51 +08:00
|
|
|
nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
|
2002-05-01 04:52:08 +08:00
|
|
|
#ifdef LOGALLOC
|
|
|
|
else if (q)
|
2005-01-16 06:15:51 +08:00
|
|
|
fprintf(logfp, "%s %d realloc(%p,%ld) returns %p\n",
|
2007-09-26 05:27:34 +08:00
|
|
|
file, line, q, (long)size, p);
|
2002-05-01 04:52:08 +08:00
|
|
|
else
|
2005-01-16 06:15:51 +08:00
|
|
|
fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
|
2007-09-26 05:27:34 +08:00
|
|
|
file, line, (long)size, p);
|
2002-05-01 04:52:08 +08:00
|
|
|
#endif
|
2002-05-01 04:51:32 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2002-05-01 04:52:08 +08:00
|
|
|
#ifdef LOGALLOC
|
2007-04-14 00:47:53 +08:00
|
|
|
void nasm_free_log(char *file, int line, void *q)
|
2002-05-01 04:52:08 +08:00
|
|
|
#else
|
2005-01-16 06:15:51 +08:00
|
|
|
void nasm_free(void *q)
|
2002-05-01 04:52:08 +08:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (q) {
|
|
|
|
#ifdef LOGALLOC
|
2005-01-16 06:15:51 +08:00
|
|
|
fprintf(logfp, "%s %d free(%p)\n", file, line, q);
|
2002-05-01 04:52:08 +08:00
|
|
|
#endif
|
2008-01-22 08:29:10 +08:00
|
|
|
free(q);
|
2002-05-01 04:52:08 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2002-05-01 04:52:08 +08:00
|
|
|
#ifdef LOGALLOC
|
2007-04-14 00:47:53 +08:00
|
|
|
char *nasm_strdup_log(char *file, int line, const char *s)
|
2002-05-01 04:52:08 +08:00
|
|
|
#else
|
2007-04-14 00:47:53 +08:00
|
|
|
char *nasm_strdup(const char *s)
|
2002-05-01 04:52:08 +08:00
|
|
|
#endif
|
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
char *p;
|
2005-01-16 06:15:51 +08:00
|
|
|
int size = strlen(s) + 1;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:52:08 +08:00
|
|
|
p = malloc(size);
|
|
|
|
if (!p)
|
2005-01-16 06:15:51 +08:00
|
|
|
nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
|
2002-05-01 04:52:08 +08:00
|
|
|
#ifdef LOGALLOC
|
|
|
|
else
|
2005-01-16 06:15:51 +08:00
|
|
|
fprintf(logfp, "%s %d strdup(%ld) returns %p\n",
|
2007-09-26 05:27:34 +08:00
|
|
|
file, line, (long)size, p);
|
2002-05-01 04:52:08 +08:00
|
|
|
#endif
|
2005-01-16 06:15:51 +08:00
|
|
|
strcpy(p, s);
|
2002-05-01 04:51:32 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2002-05-01 04:52:49 +08:00
|
|
|
#ifdef LOGALLOC
|
2007-04-14 00:47:53 +08:00
|
|
|
char *nasm_strndup_log(char *file, int line, char *s, size_t len)
|
2002-05-01 04:52:49 +08:00
|
|
|
#else
|
2007-04-14 00:47:53 +08:00
|
|
|
char *nasm_strndup(char *s, size_t len)
|
2002-05-01 04:52:49 +08:00
|
|
|
#endif
|
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
char *p;
|
2005-01-16 06:15:51 +08:00
|
|
|
int size = len + 1;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
|
|
|
p = malloc(size);
|
|
|
|
if (!p)
|
2005-01-16 06:15:51 +08:00
|
|
|
nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
|
2002-05-01 04:52:49 +08:00
|
|
|
#ifdef LOGALLOC
|
|
|
|
else
|
2005-01-16 06:15:51 +08:00
|
|
|
fprintf(logfp, "%s %d strndup(%ld) returns %p\n",
|
2007-09-26 05:27:34 +08:00
|
|
|
file, line, (long)size, p);
|
2002-05-01 04:52:49 +08:00
|
|
|
#endif
|
2005-01-16 06:15:51 +08:00
|
|
|
strncpy(p, s, len);
|
2002-05-01 04:52:49 +08:00
|
|
|
p[len] = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#ifndef nasm_stricmp
|
2007-04-14 00:47:53 +08:00
|
|
|
int nasm_stricmp(const char *s1, const char *s2)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2008-06-02 12:34:49 +08:00
|
|
|
unsigned char c1, c2;
|
|
|
|
int d;
|
|
|
|
|
|
|
|
while (1) {
|
2008-06-29 09:31:08 +08:00
|
|
|
c1 = nasm_tolower(*s1++);
|
|
|
|
c2 = nasm_tolower(*s2++);
|
2008-06-02 12:34:49 +08:00
|
|
|
d = c1-c2;
|
|
|
|
|
|
|
|
if (d)
|
|
|
|
return d;
|
|
|
|
if (!c1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2002-05-01 04:59:21 +08:00
|
|
|
#endif
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#ifndef nasm_strnicmp
|
2008-06-02 12:34:49 +08:00
|
|
|
int nasm_strnicmp(const char *s1, const char *s2, size_t n)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2008-06-02 12:34:49 +08:00
|
|
|
unsigned char c1, c2;
|
|
|
|
int d;
|
|
|
|
|
|
|
|
while (n--) {
|
2008-06-29 09:31:08 +08:00
|
|
|
c1 = nasm_tolower(*s1++);
|
|
|
|
c2 = nasm_tolower(*s2++);
|
2008-06-02 12:34:49 +08:00
|
|
|
d = c1-c2;
|
|
|
|
|
|
|
|
if (d)
|
|
|
|
return d;
|
|
|
|
if (!c1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2002-05-01 04:59:21 +08:00
|
|
|
#endif
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2008-06-02 12:34:49 +08:00
|
|
|
int nasm_memicmp(const char *s1, const char *s2, size_t n)
|
|
|
|
{
|
|
|
|
unsigned char c1, c2;
|
|
|
|
int d;
|
|
|
|
|
|
|
|
while (n--) {
|
2008-06-12 06:49:41 +08:00
|
|
|
c1 = nasm_tolower(*s1++);
|
|
|
|
c2 = nasm_tolower(*s2++);
|
2008-06-02 12:34:49 +08:00
|
|
|
d = c1-c2;
|
|
|
|
if (d)
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#ifndef nasm_strsep
|
2007-08-26 13:48:54 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2008-06-22 01:23:17 +08:00
|
|
|
#define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
|
2002-05-01 04:51:32 +08:00
|
|
|
#define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
|
|
|
|
|
2007-10-23 07:53:48 +08: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 */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 15:05:31 +08:00
|
|
|
int64_t readnum(char *str, bool *error)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
char *r = str, *q;
|
2007-10-23 10:48:06 +08:00
|
|
|
int32_t pradix, sradix, radix;
|
2007-10-27 12:38:02 +08:00
|
|
|
int plen, slen, len;
|
2007-04-12 10:40:54 +08:00
|
|
|
uint64_t result, checklimit;
|
2002-05-01 04:53:55 +08:00
|
|
|
int digit, last;
|
2007-10-11 15:05:31 +08:00
|
|
|
bool warn = false;
|
2002-05-01 04:53:55 +08:00
|
|
|
int sign = 1;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-11 05:58:45 +08:00
|
|
|
*error = false;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2008-06-22 01:23:17 +08:00
|
|
|
while (nasm_isspace(*r))
|
2005-01-16 06:15:51 +08:00
|
|
|
r++; /* find start of number */
|
2002-05-01 04:53:55 +08: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-16 06:15:51 +08:00
|
|
|
if (*r == '-') {
|
|
|
|
r++;
|
|
|
|
sign = -1;
|
2002-05-01 04:53:55 +08:00
|
|
|
}
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
q = r;
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
while (lib_isnumchar(*q))
|
|
|
|
q++; /* find end of number */
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-27 12:38:02 +08:00
|
|
|
len = q-r;
|
|
|
|
if (!len) {
|
|
|
|
/* Not numeric */
|
|
|
|
*error = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/*
|
2007-10-23 07:53:48 +08:00
|
|
|
* Handle radix formats:
|
|
|
|
*
|
|
|
|
* 0<radix-letter><string>
|
|
|
|
* $<string> (hexadecimal)
|
|
|
|
* <string><radix-letter>
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
2007-10-23 10:48:06 +08:00
|
|
|
pradix = sradix = 0;
|
|
|
|
plen = slen = 0;
|
|
|
|
|
2007-10-27 12:38:02 +08:00
|
|
|
if (len > 2 && *r == '0' && (pradix = radix_letter(r[1])) != 0)
|
2007-10-23 10:48:06 +08:00
|
|
|
plen = 2;
|
2007-10-27 12:38:02 +08:00
|
|
|
else if (len > 1 && *r == '$')
|
2007-10-23 10:48:06 +08:00
|
|
|
pradix = 16, plen = 1;
|
|
|
|
|
2007-10-27 12:38:02 +08:00
|
|
|
if (len > 1 && (sradix = radix_letter(q[-1])) != 0)
|
2007-10-23 10:48:06 +08:00
|
|
|
slen = 1;
|
|
|
|
|
2007-10-23 15:08:58 +08:00
|
|
|
if (pradix > sradix) {
|
2007-10-23 10:48:06 +08:00
|
|
|
radix = pradix;
|
|
|
|
r += plen;
|
2007-10-23 15:08:58 +08:00
|
|
|
} else if (sradix > pradix) {
|
2007-10-23 10:48:06 +08:00
|
|
|
radix = sradix;
|
|
|
|
q -= slen;
|
|
|
|
} else {
|
2007-10-23 15:08:58 +08:00
|
|
|
/* Either decimal, or invalid -- if invalid, we'll trip up
|
|
|
|
further down. */
|
2007-10-23 07:53:48 +08:00
|
|
|
radix = 10;
|
2007-10-23 10:48:06 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:52:49 +08:00
|
|
|
/*
|
2007-11-18 13:08:33 +08:00
|
|
|
* `checklimit' must be 2**64 / radix. We can't do that in
|
|
|
|
* 64-bit arithmetic, which we're (probably) using, so we
|
2002-05-01 04:52:49 +08:00
|
|
|
* cheat: since we know that all radices we use are even, we
|
2007-11-18 13:08:33 +08:00
|
|
|
* can divide 2**63 by radix/2 instead.
|
2002-05-01 04:52:49 +08:00
|
|
|
*/
|
2007-11-18 13:08:33 +08:00
|
|
|
checklimit = 0x8000000000000000ULL / (radix >> 1);
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2002-05-01 04:53:55 +08:00
|
|
|
/*
|
2007-04-12 10:40:54 +08:00
|
|
|
* Calculate the highest allowable value for the last digit of a
|
2007-11-18 13:08:33 +08:00
|
|
|
* 64-bit constant... in radix 10, it is 6, otherwise it is 0
|
2002-05-01 04:53:55 +08:00
|
|
|
*/
|
|
|
|
last = (radix == 10 ? 6 : 0);
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
result = 0;
|
|
|
|
while (*r && r < q) {
|
2007-10-20 04:10:46 +08:00
|
|
|
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;
|
|
|
|
}
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-10-20 04:10:46 +08:00
|
|
|
result = radix * result + digit;
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
r++;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2002-05-01 04:52:49 +08:00
|
|
|
|
|
|
|
if (warn)
|
2005-01-16 06:15:51 +08:00
|
|
|
nasm_malloc_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
|
2008-01-09 14:13:48 +08:00
|
|
|
"numeric constant %s does not fit in 64 bits",
|
2005-01-16 06:15:51 +08:00
|
|
|
str);
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
return result * sign;
|
2002-05-01 04:53:55 +08:00
|
|
|
}
|
|
|
|
|
2007-10-11 15:05:31 +08:00
|
|
|
int64_t readstrnum(char *str, int length, bool *warn)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-12 10:40:54 +08:00
|
|
|
int64_t charconst = 0;
|
2002-05-01 04:53:55 +08:00
|
|
|
int i;
|
|
|
|
|
2007-10-11 05:58:45 +08:00
|
|
|
*warn = false;
|
2002-05-01 04:53:55 +08:00
|
|
|
|
|
|
|
str += length;
|
2007-04-12 10:40:54 +08:00
|
|
|
if (globalbits == 64) {
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
if (charconst & 0xFF00000000000000ULL)
|
2007-10-11 05:58:45 +08:00
|
|
|
*warn = true;
|
2007-04-12 10:40:54 +08:00
|
|
|
charconst = (charconst << 8) + (uint8_t)*--str;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
if (charconst & 0xFF000000UL)
|
2007-10-11 05:58:45 +08:00
|
|
|
*warn = true;
|
2007-04-12 10:40:54 +08:00
|
|
|
charconst = (charconst << 8) + (uint8_t)*--str;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2002-05-01 04:53:55 +08:00
|
|
|
}
|
|
|
|
return charconst;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
static int32_t next_seg;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
void seg_init(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
next_seg = 0;
|
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t seg_alloc(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
return (next_seg += 2) - 2;
|
|
|
|
}
|
|
|
|
|
2008-06-11 05:44:09 +08:00
|
|
|
#ifdef WORDS_LITTLEENDIAN
|
2007-11-14 01:46:38 +08:00
|
|
|
|
2007-11-14 02:37:23 +08:00
|
|
|
void fwriteint16_t(uint16_t data, FILE * fp)
|
2007-11-14 01:46:38 +08:00
|
|
|
{
|
2007-11-14 02:37:23 +08:00
|
|
|
fwrite(&data, 1, 2, fp);
|
2007-11-14 01:46:38 +08:00
|
|
|
}
|
|
|
|
|
2007-11-14 02:37:23 +08:00
|
|
|
void fwriteint32_t(uint32_t data, FILE * fp)
|
2007-11-14 01:46:38 +08:00
|
|
|
{
|
2007-11-14 02:37:23 +08:00
|
|
|
fwrite(&data, 1, 4, fp);
|
2007-11-14 01:46:38 +08:00
|
|
|
}
|
|
|
|
|
2007-11-14 02:37:23 +08:00
|
|
|
void fwriteint64_t(uint64_t data, FILE * fp)
|
2007-11-14 01:46:38 +08:00
|
|
|
{
|
2007-11-14 02:37:23 +08:00
|
|
|
fwrite(&data, 1, 8, fp);
|
2007-11-14 01:46:38 +08:00
|
|
|
}
|
|
|
|
|
2007-11-14 02:37:23 +08:00
|
|
|
void fwriteaddr(uint64_t data, int size, FILE * fp)
|
2007-11-14 01:46:38 +08:00
|
|
|
{
|
2007-11-14 02:37:23 +08:00
|
|
|
fwrite(&data, 1, size, fp);
|
2007-11-14 01:46:38 +08:00
|
|
|
}
|
|
|
|
|
2008-06-11 05:44:09 +08:00
|
|
|
#else /* not WORDS_LITTLEENDIAN */
|
2007-11-14 01:46:38 +08:00
|
|
|
|
2007-11-14 02:37:23 +08:00
|
|
|
void fwriteint16_t(uint16_t data, FILE * fp)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-11-14 01:37:59 +08:00
|
|
|
char buffer[2], *p = buffer;
|
|
|
|
WRITESHORT(p, data);
|
|
|
|
fwrite(buffer, 1, 2, fp);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-11-14 02:37:23 +08:00
|
|
|
void fwriteint32_t(uint32_t data, FILE * fp)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-11-14 01:37:59 +08:00
|
|
|
char buffer[4], *p = buffer;
|
|
|
|
WRITELONG(p, data);
|
|
|
|
fwrite(buffer, 1, 4, fp);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-11-14 02:37:23 +08:00
|
|
|
void fwriteint64_t(uint64_t data, FILE * fp)
|
2007-04-28 14:18:48 +08:00
|
|
|
{
|
2007-11-14 01:37:59 +08:00
|
|
|
char buffer[8], *p = buffer;
|
|
|
|
WRITEDLONG(p, data);
|
|
|
|
fwrite(buffer, 1, 8, fp);
|
|
|
|
}
|
|
|
|
|
2007-11-14 02:37:23 +08:00
|
|
|
void fwriteaddr(uint64_t data, int size, FILE * fp)
|
2007-11-14 01:37:59 +08:00
|
|
|
{
|
|
|
|
char buffer[8], *p = buffer;
|
|
|
|
WRITEADDR(p, data, size);
|
|
|
|
fwrite(buffer, 1, size, fp);
|
2007-04-28 14:18:48 +08:00
|
|
|
}
|
|
|
|
|
2007-11-14 01:46:38 +08:00
|
|
|
#endif
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
void standard_extension(char *inname, char *outname, char *extension,
|
2005-01-16 06:15:51 +08:00
|
|
|
efunc error)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
char *p, *q;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (*outname) /* file name already exists, */
|
|
|
|
return; /* so do nothing */
|
2002-05-01 04:51:32 +08:00
|
|
|
q = inname;
|
|
|
|
p = outname;
|
2005-01-16 06:15:51 +08: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)
|
|
|
|
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");
|
2002-05-01 04:51:32 +08:00
|
|
|
} else
|
2005-01-16 06:15:51 +08:00
|
|
|
strcpy(p, extension);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2002-05-01 04:52:49 +08:00
|
|
|
/*
|
2007-08-31 08:16:10 +08:00
|
|
|
* Common list of prefix names
|
2002-05-01 04:52:49 +08:00
|
|
|
*/
|
2007-04-14 00:47:53 +08:00
|
|
|
static const char *prefix_names[] = {
|
2002-05-01 04:52:49 +08:00
|
|
|
"a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
|
|
|
|
"repnz", "repz", "times"
|
|
|
|
};
|
|
|
|
|
2007-04-18 04:23:11 +08:00
|
|
|
const char *prefix_name(int token)
|
|
|
|
{
|
|
|
|
unsigned int prefix = token-PREFIX_ENUM_START;
|
2007-11-09 11:34:01 +08:00
|
|
|
if (prefix > elements(prefix_names))
|
2007-04-18 04:23:11 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return prefix_names[prefix];
|
|
|
|
}
|
|
|
|
|
2002-05-01 04:52:49 +08:00
|
|
|
/*
|
|
|
|
* Binary search.
|
|
|
|
*/
|
2007-11-09 11:15:33 +08:00
|
|
|
int bsi(const char *string, const char **array, int size)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08: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-05-01 04:52:49 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
return -1; /* we haven't got it :( */
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2007-11-09 11:15:33 +08:00
|
|
|
int bsii(const char *string, const char **array, int size)
|
2007-08-30 00:25:46 +08:00
|
|
|
{
|
|
|
|
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 :( */
|
|
|
|
}
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static char *file_name = NULL;
|
2007-04-12 10:40:54 +08:00
|
|
|
static int32_t line_number = 0;
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
char *src_set_fname(char *newname)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
char *oldname = file_name;
|
2002-05-01 04:53:55 +08:00
|
|
|
file_name = newname;
|
|
|
|
return oldname;
|
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t src_set_linnum(int32_t newline)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t oldline = line_number;
|
2002-05-01 04:53:55 +08:00
|
|
|
line_number = newline;
|
|
|
|
return oldline;
|
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t src_get_linnum(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
|
|
|
return line_number;
|
|
|
|
}
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
int src_get(int32_t *xline, char **xname)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!file_name || !*xname || strcmp(*xname, file_name)) {
|
|
|
|
nasm_free(*xname);
|
|
|
|
*xname = file_name ? nasm_strdup(file_name) : NULL;
|
|
|
|
*xline = line_number;
|
|
|
|
return -2;
|
2002-05-01 04:53:55 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
if (*xline != line_number) {
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t tmp = line_number - *xline;
|
2005-01-16 06:15:51 +08:00
|
|
|
*xline = line_number;
|
|
|
|
return tmp;
|
2002-05-01 04:53:55 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
char *nasm_strcat(char *one, char *two)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
char *rslt;
|
2005-01-16 06:15:51 +08:00
|
|
|
int l1 = strlen(one);
|
|
|
|
rslt = nasm_malloc(l1 + strlen(two) + 1);
|
2002-05-01 04:53:55 +08:00
|
|
|
strcpy(rslt, one);
|
2005-01-16 06:15:51 +08:00
|
|
|
strcpy(rslt + l1, two);
|
2002-05-01 04:53:55 +08:00
|
|
|
return rslt;
|
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
void null_debug_init(struct ofmt *of, void *id, FILE * fp, efunc error)
|
|
|
|
{
|
2007-04-14 08:46:25 +08:00
|
|
|
(void)of;
|
|
|
|
(void)id;
|
|
|
|
(void)fp;
|
|
|
|
(void)error;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2007-04-14 00:47:53 +08:00
|
|
|
void null_debug_linenum(const char *filename, int32_t linenumber, int32_t segto)
|
2005-01-16 06:15:51 +08:00
|
|
|
{
|
2007-04-14 08:46:25 +08:00
|
|
|
(void)filename;
|
|
|
|
(void)linenumber;
|
2007-10-20 05:42:29 +08:00
|
|
|
(void)segto;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2007-11-06 09:19:32 +08:00
|
|
|
void null_debug_deflabel(char *name, int32_t segment, int64_t offset,
|
2007-04-14 00:47:53 +08:00
|
|
|
int is_global, char *special)
|
2005-01-16 06:15:51 +08:00
|
|
|
{
|
2007-04-14 08:46:25 +08:00
|
|
|
(void)name;
|
|
|
|
(void)segment;
|
|
|
|
(void)offset;
|
|
|
|
(void)is_global;
|
|
|
|
(void)special;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2007-04-14 00:47:53 +08:00
|
|
|
void null_debug_routine(const char *directive, const char *params)
|
2005-01-16 06:15:51 +08:00
|
|
|
{
|
2007-04-14 08:46:25 +08:00
|
|
|
(void)directive;
|
|
|
|
(void)params;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2007-04-12 10:40:54 +08:00
|
|
|
void null_debug_typevalue(int32_t type)
|
2005-01-16 06:15:51 +08:00
|
|
|
{
|
2007-04-14 08:46:25 +08:00
|
|
|
(void)type;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
void null_debug_output(int type, void *param)
|
|
|
|
{
|
2007-04-14 08:46:25 +08:00
|
|
|
(void)type;
|
|
|
|
(void)param;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
void null_debug_cleanup(void)
|
|
|
|
{
|
|
|
|
}
|
2002-05-15 06:38:55 +08:00
|
|
|
|
2002-05-01 04:53:55 +08:00
|
|
|
struct dfmt null_debug_form = {
|
|
|
|
"Null debug format",
|
|
|
|
"null",
|
2002-09-12 10:23:54 +08:00
|
|
|
null_debug_init,
|
|
|
|
null_debug_linenum,
|
|
|
|
null_debug_deflabel,
|
2002-05-01 04:53:55 +08:00
|
|
|
null_debug_routine,
|
2002-09-12 10:23:54 +08:00
|
|
|
null_debug_typevalue,
|
|
|
|
null_debug_output,
|
|
|
|
null_debug_cleanup
|
2002-05-01 04:53:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct dfmt *null_debug_arr[2] = { &null_debug_form, NULL };
|