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
|
|
|
|
* redistributable under the licence given in the file "Licence"
|
|
|
|
* 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
|
|
|
|
|
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) {
|
2005-01-16 06:15:51 +08:00
|
|
|
free(q);
|
2002-05-01 04:52:08 +08:00
|
|
|
#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
|
|
|
|
}
|
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
|
|
|
{
|
2002-05-01 04:59:21 +08:00
|
|
|
while (*s1 && tolower(*s1) == tolower(*s2))
|
2005-01-16 06:15:51 +08:00
|
|
|
s1++, s2++;
|
2002-05-01 04:51:32 +08:00
|
|
|
if (!*s1 && !*s2)
|
2005-01-16 06:15:51 +08:00
|
|
|
return 0;
|
2002-05-01 04:59:21 +08:00
|
|
|
else if (tolower(*s1) < tolower(*s2))
|
2005-01-16 06:15:51 +08:00
|
|
|
return -1;
|
2002-05-01 04:51:32 +08:00
|
|
|
else
|
2005-01-16 06:15:51 +08:00
|
|
|
return 1;
|
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
|
2007-04-14 00:47:53 +08:00
|
|
|
int nasm_strnicmp(const char *s1, const char *s2, int n)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:59:21 +08:00
|
|
|
while (n > 0 && *s1 && tolower(*s1) == tolower(*s2))
|
2005-01-16 06:15:51 +08:00
|
|
|
s1++, s2++, n--;
|
|
|
|
if ((!*s1 && !*s2) || n == 0)
|
|
|
|
return 0;
|
2002-05-01 04:59:21 +08:00
|
|
|
else if (tolower(*s1) < tolower(*s2))
|
2005-01-16 06:15:51 +08:00
|
|
|
return -1;
|
2002-05-01 04:51:32 +08:00
|
|
|
else
|
2005-01-16 06:15:51 +08:00
|
|
|
return 1;
|
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_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
|
|
|
|
|
|
|
|
|
2002-05-01 04:52:08 +08:00
|
|
|
#define lib_isnumchar(c) ( isalnum(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-04-14 00:47:53 +08:00
|
|
|
int64_t readnum(char *str, int *error)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
char *r = str, *q;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t radix;
|
|
|
|
uint64_t result, checklimit;
|
2002-05-01 04:53:55 +08:00
|
|
|
int digit, last;
|
2007-10-11 05:58:45 +08:00
|
|
|
int 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
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
while (isspace(*r))
|
|
|
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
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--;
|
2002-05-01 04:51:32 +08:00
|
|
|
else
|
2005-01-16 06:15:51 +08:00
|
|
|
radix = 10;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:52:49 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
if (r >= q) {
|
2007-10-11 05:58:45 +08:00
|
|
|
*error = true;
|
2005-01-16 06:15:51 +08:00
|
|
|
return 0;
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
2007-04-12 10:40:54 +08:00
|
|
|
|
2002-05-01 04:52:49 +08:00
|
|
|
/*
|
2007-04-12 10:40:54 +08:00
|
|
|
* `checklimit' must be 2**(32|64) / radix. We can't do that in
|
|
|
|
* 32/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-04-12 10:40:54 +08:00
|
|
|
* can divide 2**(31|63) by radix/2 instead.
|
2002-05-01 04:52:49 +08:00
|
|
|
*/
|
2007-04-12 10:40:54 +08:00
|
|
|
if (globalbits == 64)
|
|
|
|
checklimit = 0x8000000000000000ULL / (radix >> 1);
|
|
|
|
else
|
|
|
|
checklimit = 0x80000000UL / (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
|
|
|
|
* 32-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) {
|
2005-01-16 06:15:51 +08:00
|
|
|
if (*r < '0' || (*r > '9' && *r < 'A')
|
|
|
|
|| (digit = numvalue(*r)) >= radix) {
|
2007-10-11 05:58:45 +08:00
|
|
|
*error = true;
|
2005-01-16 06:15:51 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (result > checklimit || (result == checklimit && digit >= last)) {
|
2007-10-11 05:58:45 +08:00
|
|
|
warn = true;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
result = radix * result + digit;
|
|
|
|
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,
|
|
|
|
"numeric constant %s does not fit in 32 bits",
|
|
|
|
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-04-14 00:47:53 +08:00
|
|
|
int64_t readstrnum(char *str, int length, int *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;
|
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
void fwriteint16_t(int data, FILE * fp)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
fputc((int)(data & 255), fp);
|
|
|
|
fputc((int)((data >> 8) & 255), fp);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
void fwriteint32_t(int32_t data, FILE * fp)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
fputc((int)(data & 255), fp);
|
|
|
|
fputc((int)((data >> 8) & 255), fp);
|
|
|
|
fputc((int)((data >> 16) & 255), fp);
|
|
|
|
fputc((int)((data >> 24) & 255), fp);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-04-28 14:18:48 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#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 )
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static struct RAA *real_raa_init(int layers)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
struct RAA *r;
|
2002-05-27 06:30:48 +08:00
|
|
|
int i;
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
if (layers == 0) {
|
2007-09-26 05:27:34 +08:00
|
|
|
r = nasm_zalloc(LEAFSIZ);
|
2005-01-16 06:15:51 +08:00
|
|
|
r->stepsize = 1L;
|
2002-05-01 04:51:32 +08:00
|
|
|
} else {
|
2005-01-16 06:15:51 +08:00
|
|
|
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;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
struct RAA *raa_init(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
return real_raa_init(0);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
void raa_free(struct RAA *r)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
if (r->layers == 0)
|
2005-01-16 06:15:51 +08:00
|
|
|
nasm_free(r);
|
2002-05-01 04:51:32 +08:00
|
|
|
else {
|
2005-01-16 06:15:51 +08:00
|
|
|
struct RAA **p;
|
|
|
|
for (p = r->u.b.data; p - r->u.b.data < RAA_LAYERSIZE; p++)
|
|
|
|
if (*p)
|
|
|
|
raa_free(*p);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t raa_read(struct RAA *r, int32_t posn)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-09-13 06:03:10 +08:00
|
|
|
if (posn >= r->stepsize * LAYERSIZ(r))
|
2005-01-16 06:15:51 +08:00
|
|
|
return 0; /* Return 0 for undefined entries */
|
2002-05-01 04:51:32 +08:00
|
|
|
while (r->layers > 0) {
|
2005-01-16 06:15:51 +08:00
|
|
|
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 */
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
return r->u.l.data[posn];
|
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
struct RAA *raa_write(struct RAA *r, int32_t posn, int32_t value)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
struct RAA *result;
|
|
|
|
|
|
|
|
if (posn < 0)
|
2005-01-16 06:15:51 +08:00
|
|
|
nasm_malloc_error(ERR_PANIC, "negative position in raa_write");
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-09-13 06:03:10 +08:00
|
|
|
while (r->stepsize * LAYERSIZ(r) <= posn) {
|
2005-01-16 06:15:51 +08:00
|
|
|
/*
|
|
|
|
* 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;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
result = r;
|
|
|
|
|
|
|
|
while (r->layers > 0) {
|
2005-01-16 06:15:51 +08:00
|
|
|
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;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
r->u.l.data[posn] = value;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
/* Aggregate SAA components smaller than this */
|
|
|
|
#define SAA_BLKLEN 65536
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
struct SAA *saa_init(size_t elem_len)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
struct SAA *s;
|
2007-10-05 04:42:56 +08:00
|
|
|
char *data;
|
|
|
|
|
|
|
|
s = nasm_zalloc(sizeof(struct SAA));
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
if (elem_len >= SAA_BLKLEN)
|
|
|
|
s->blk_len = elem_len;
|
|
|
|
else
|
|
|
|
s->blk_len = SAA_BLKLEN - (SAA_BLKLEN % elem_len);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
s->elem_len = elem_len;
|
2007-10-05 04:42:56 +08:00
|
|
|
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];
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
void saa_free(struct SAA *s)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-10-05 04:42:56 +08:00
|
|
|
char **p;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
for (p = s->blk_ptrs, n = s->nblks; n; p++, n--)
|
|
|
|
nasm_free(*p);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
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;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2007-10-05 04:42:56 +08:00
|
|
|
|
|
|
|
s->blk_ptrs[blkn] = nasm_malloc(s->blk_len);
|
|
|
|
s->length += s->blk_len;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
void *saa_wstruct(struct SAA *s)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
void *p;
|
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
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;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
p = *s->wblk + s->wpos;
|
|
|
|
s->wpos += s->elem_len;
|
|
|
|
s->wptr += s->elem_len;
|
|
|
|
|
|
|
|
if (s->wptr > s->datalen)
|
|
|
|
s->datalen = s->wptr;
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
void saa_wbytes(struct SAA *s, const void *data, size_t len)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
const char *d = data;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
while (len) {
|
|
|
|
size_t l = s->blk_len - s->wpos;
|
2005-01-16 06:15:51 +08:00
|
|
|
if (l > len)
|
|
|
|
l = len;
|
2007-10-05 04:42:56 +08:00
|
|
|
if (l) {
|
2005-01-16 06:15:51 +08:00
|
|
|
if (d) {
|
2007-10-05 04:42:56 +08:00
|
|
|
memcpy(*s->wblk + s->wpos, d, l);
|
2005-01-16 06:15:51 +08:00
|
|
|
d += l;
|
|
|
|
} else
|
2007-10-05 04:42:56 +08:00
|
|
|
memset(*s->wblk + s->wpos, 0, l);
|
|
|
|
s->wpos += l;
|
|
|
|
s->wptr += l;
|
2005-01-16 06:15:51 +08:00
|
|
|
len -= l;
|
2007-10-05 04:42:56 +08:00
|
|
|
|
|
|
|
if (s->datalen < s->wptr)
|
|
|
|
s->datalen = s->wptr;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2007-10-05 04:42:56 +08:00
|
|
|
if (len) {
|
|
|
|
if (s->wptr >= s->length)
|
|
|
|
saa_extend(s);
|
|
|
|
s->wblk++;
|
|
|
|
s->wpos = 0;
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
void saa_rewind(struct SAA *s)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-10-05 04:42:56 +08:00
|
|
|
s->rblk = s->blk_ptrs;
|
|
|
|
s->rpos = s->rptr = 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
void *saa_rstruct(struct SAA *s)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
void *p;
|
|
|
|
|
2007-10-09 03:41:00 +08:00
|
|
|
if (s->rptr + s->elem_len > s->datalen)
|
2007-10-05 04:42:56 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (s->rpos % s->elem_len)
|
|
|
|
nasm_malloc_error(ERR_PANIC|ERR_NOFILE,
|
|
|
|
"misaligned rpos in saa_rstruct");
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
if (s->rpos + s->elem_len > s->blk_len) {
|
|
|
|
s->rblk++;
|
|
|
|
s->rpos = 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
p = *s->rblk + s->rpos;
|
2002-05-01 04:51:32 +08:00
|
|
|
s->rpos += s->elem_len;
|
2007-10-05 04:42:56 +08:00
|
|
|
s->rptr += s->elem_len;
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
const void *saa_rbytes(struct SAA *s, size_t *lenp)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-10-05 04:42:56 +08:00
|
|
|
const void *p;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (s->rptr >= s->datalen) {
|
|
|
|
*lenp = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->rpos >= s->blk_len) {
|
|
|
|
s->rblk++;
|
|
|
|
s->rpos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
*lenp = len;
|
|
|
|
p = *s->rblk + s->rpos;
|
|
|
|
|
|
|
|
s->rpos += len;
|
|
|
|
s->rptr += len;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
void saa_rnbytes(struct SAA *s, void *data, size_t len)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
char *d = data;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
if (s->rptr + len > s->datalen) {
|
|
|
|
nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_rnbytes");
|
|
|
|
return;
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
while (len) {
|
|
|
|
size_t l;
|
|
|
|
const void *p;
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
l = len;
|
|
|
|
p = saa_rbytes(s, &l);
|
|
|
|
|
|
|
|
memcpy(d, p, l);
|
|
|
|
d += l;
|
|
|
|
len -= l;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
/* Same as saa_rnbytes, except position the counter first */
|
|
|
|
void saa_fread(struct SAA *s, size_t posn, void *data, size_t len)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-10-05 04:42:56 +08:00
|
|
|
size_t ix;
|
|
|
|
|
|
|
|
if (posn+len > s->datalen) {
|
|
|
|
nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_fread");
|
|
|
|
return;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
ix = posn / s->blk_len;
|
|
|
|
s->rpos = posn % s->blk_len;
|
|
|
|
s->rblk = &s->blk_ptrs[ix];
|
|
|
|
|
|
|
|
saa_rnbytes(s, data, len);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
/* Same as saa_wbytes, except position the counter first */
|
|
|
|
void saa_fwrite(struct SAA *s, size_t posn, const void *data, size_t len)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-10-05 04:42:56 +08:00
|
|
|
size_t ix;
|
|
|
|
|
|
|
|
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;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-10-05 04:42:56 +08:00
|
|
|
ix = posn / s->blk_len;
|
|
|
|
s->wpos = posn % s->blk_len;
|
|
|
|
s->wblk = &s->blk_ptrs[ix];
|
|
|
|
|
|
|
|
if (!s->wpos) {
|
|
|
|
s->wpos = s->blk_len;
|
|
|
|
s->wblk--;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2007-10-05 04:42:56 +08:00
|
|
|
|
|
|
|
saa_wbytes(s, data, len);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
void saa_fpwrite(struct SAA *s, FILE * fp)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-10-05 04:42:56 +08:00
|
|
|
const char *data;
|
|
|
|
size_t len;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
saa_rewind(s);
|
2007-10-08 12:13:14 +08:00
|
|
|
while (len = s->datalen, (data = saa_rbytes(s, &len)) != NULL)
|
2005-01-16 06:15:51 +08:00
|
|
|
fwrite(data, 1, len, fp);
|
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;
|
|
|
|
if (prefix > sizeof prefix_names / sizeof(const char *))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return prefix_names[prefix];
|
|
|
|
}
|
|
|
|
|
2002-05-01 04:52:49 +08:00
|
|
|
/*
|
|
|
|
* Binary search.
|
|
|
|
*/
|
2007-04-14 00:47:53 +08:00
|
|
|
int bsi(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-08-30 00:25:46 +08:00
|
|
|
int bsii(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 :( */
|
|
|
|
}
|
|
|
|
|
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
|
|
|
void nasm_quote(char **str)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
int ln = strlen(*str);
|
2007-04-14 00:47:53 +08:00
|
|
|
char q = (*str)[0];
|
|
|
|
char *p;
|
2005-01-16 06:15:51 +08:00
|
|
|
if (ln > 1 && (*str)[ln - 1] == q && (q == '"' || q == '\''))
|
|
|
|
return;
|
2002-05-01 04:53:55 +08:00
|
|
|
q = '"';
|
2005-01-16 06:15:51 +08:00
|
|
|
if (strchr(*str, q))
|
|
|
|
q = '\'';
|
|
|
|
p = nasm_malloc(ln + 3);
|
|
|
|
strcpy(p + 1, *str);
|
2002-05-01 04:53:55 +08:00
|
|
|
nasm_free(*str);
|
2005-01-16 06:15:51 +08:00
|
|
|
p[ln + 1] = p[0] = q;
|
|
|
|
p[ln + 2] = 0;
|
2002-05-01 04:53:55 +08:00
|
|
|
*str = p;
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
|
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;
|
|
|
|
(void)segto;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2007-04-14 00:47:53 +08:00
|
|
|
void null_debug_deflabel(char *name, int32_t segment, int32_t offset,
|
|
|
|
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 };
|