1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * nasmlib.c library routines for the Netwide Assembler
50 int globalbits = 0; /* defined in nasm.h, works better here for ASM+DISASM */
51 efunc nasm_malloc_error; /* Exported for the benefit of vsnprintf.c */
57 /* Uninitialized -> all zero by C spec */
58 const uint8_t zero_buffer[ZERO_BUF_SIZE];
61 * Prepare a table of tolower() results. This avoids function calls
65 unsigned char nasm_tolower_tab[256];
67 void tolower_init(void)
71 for (i = 0; i < 256; i++)
72 nasm_tolower_tab[i] = tolower(i);
75 void nasm_set_malloc_error(efunc error)
77 nasm_malloc_error = error;
79 logfp = fopen("malloc.log", "w");
80 setvbuf(logfp, NULL, _IOLBF, BUFSIZ);
81 fprintf(logfp, "null pointer is %p\n", NULL);
86 void *nasm_malloc_log(const char *file, int line, size_t size)
88 void *nasm_malloc(size_t size)
91 void *p = malloc(size);
93 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
96 fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
97 file, line, (long)size, p);
103 void *nasm_zalloc_log(const char *file, int line, size_t size)
105 void *nasm_zalloc(size_t size)
108 void *p = calloc(size, 1);
110 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
113 fprintf(logfp, "%s %d calloc(%ld, 1) returns %p\n",
114 file, line, (long)size, p);
120 void *nasm_realloc_log(const char *file, int line, void *q, size_t size)
122 void *nasm_realloc(void *q, size_t size)
125 void *p = q ? realloc(q, size) : malloc(size);
127 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
130 fprintf(logfp, "%s %d realloc(%p,%ld) returns %p\n",
131 file, line, q, (long)size, p);
133 fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
134 file, line, (long)size, p);
140 void nasm_free_log(const char *file, int line, void *q)
142 void nasm_free(void *q)
147 fprintf(logfp, "%s %d free(%p)\n", file, line, q);
154 char *nasm_strdup_log(const char *file, int line, const char *s)
156 char *nasm_strdup(const char *s)
160 int size = strlen(s) + 1;
164 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
167 fprintf(logfp, "%s %d strdup(%ld) returns %p\n",
168 file, line, (long)size, p);
175 char *nasm_strndup_log(const char *file, int line, const char *s, size_t len)
177 char *nasm_strndup(const char *s, size_t len)
185 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
188 fprintf(logfp, "%s %d strndup(%ld) returns %p\n",
189 file, line, (long)size, p);
196 no_return nasm_assert_failed(const char *file, int line, const char *msg)
198 nasm_malloc_error(ERR_FATAL, "assertion %s failed at %s:%d",
204 int nasm_stricmp(const char *s1, const char *s2)
206 unsigned char c1, c2;
210 c1 = nasm_tolower(*s1++);
211 c2 = nasm_tolower(*s2++);
223 #ifndef nasm_strnicmp
224 int nasm_strnicmp(const char *s1, const char *s2, size_t n)
226 unsigned char c1, c2;
230 c1 = nasm_tolower(*s1++);
231 c2 = nasm_tolower(*s2++);
243 int nasm_memicmp(const char *s1, const char *s2, size_t n)
245 unsigned char c1, c2;
249 c1 = nasm_tolower(*s1++);
250 c2 = nasm_tolower(*s2++);
259 char *nasm_strsep(char **stringp, const char *delim)
267 e = strpbrk(s, delim);
277 #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
278 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
280 static int radix_letter(char c)
285 return 2; /* Binary */
288 return 8; /* Octal */
291 return 16; /* Hexadecimal */
294 return 10; /* Decimal */
296 return 0; /* Not a known radix letter */
300 int64_t readnum(char *str, bool *error)
303 int32_t pradix, sradix, radix;
305 uint64_t result, checklimit;
312 while (nasm_isspace(*r))
313 r++; /* find start of number */
316 * If the number came from make_tok_num (as a result of an %assign), it
317 * might have a '-' built into it (rather than in a preceeding token).
326 while (lib_isnumchar(*q))
327 q++; /* find end of number */
337 * Handle radix formats:
339 * 0<radix-letter><string>
340 * $<string> (hexadecimal)
341 * <string><radix-letter>
346 if (len > 2 && *r == '0' && (pradix = radix_letter(r[1])) != 0)
348 else if (len > 1 && *r == '$')
349 pradix = 16, plen = 1;
351 if (len > 1 && (sradix = radix_letter(q[-1])) != 0)
354 if (pradix > sradix) {
357 } else if (sradix > pradix) {
361 /* Either decimal, or invalid -- if invalid, we'll trip up
367 * `checklimit' must be 2**64 / radix. We can't do that in
368 * 64-bit arithmetic, which we're (probably) using, so we
369 * cheat: since we know that all radices we use are even, we
370 * can divide 2**63 by radix/2 instead.
372 checklimit = 0x8000000000000000ULL / (radix >> 1);
375 * Calculate the highest allowable value for the last digit of a
376 * 64-bit constant... in radix 10, it is 6, otherwise it is 0
378 last = (radix == 10 ? 6 : 0);
381 while (*r && r < q) {
383 if (*r < '0' || (*r > '9' && *r < 'A')
384 || (digit = numvalue(*r)) >= radix) {
388 if (result > checklimit ||
389 (result == checklimit && digit >= last)) {
393 result = radix * result + digit;
399 nasm_malloc_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
400 "numeric constant %s does not fit in 64 bits",
403 return result * sign;
406 int64_t readstrnum(char *str, int length, bool *warn)
408 int64_t charconst = 0;
414 if (globalbits == 64) {
415 for (i = 0; i < length; i++) {
416 if (charconst & 0xFF00000000000000ULL)
418 charconst = (charconst << 8) + (uint8_t)*--str;
421 for (i = 0; i < length; i++) {
422 if (charconst & 0xFF000000UL)
424 charconst = (charconst << 8) + (uint8_t)*--str;
430 static int32_t next_seg;
437 int32_t seg_alloc(void)
439 return (next_seg += 2) - 2;
442 #ifdef WORDS_LITTLEENDIAN
444 void fwriteint16_t(uint16_t data, FILE * fp)
446 fwrite(&data, 1, 2, fp);
449 void fwriteint32_t(uint32_t data, FILE * fp)
451 fwrite(&data, 1, 4, fp);
454 void fwriteint64_t(uint64_t data, FILE * fp)
456 fwrite(&data, 1, 8, fp);
459 void fwriteaddr(uint64_t data, int size, FILE * fp)
461 fwrite(&data, 1, size, fp);
464 #else /* not WORDS_LITTLEENDIAN */
466 void fwriteint16_t(uint16_t data, FILE * fp)
468 char buffer[2], *p = buffer;
470 fwrite(buffer, 1, 2, fp);
473 void fwriteint32_t(uint32_t data, FILE * fp)
475 char buffer[4], *p = buffer;
477 fwrite(buffer, 1, 4, fp);
480 void fwriteint64_t(uint64_t data, FILE * fp)
482 char buffer[8], *p = buffer;
484 fwrite(buffer, 1, 8, fp);
487 void fwriteaddr(uint64_t data, int size, FILE * fp)
489 char buffer[8], *p = buffer;
490 WRITEADDR(p, data, size);
491 fwrite(buffer, 1, size, fp);
496 size_t fwritezero(size_t bytes, FILE *fp)
503 blksize = (bytes < ZERO_BUF_SIZE) ? bytes : ZERO_BUF_SIZE;
505 rv = fwrite(zero_buffer, 1, blksize, fp);
516 void standard_extension(char *inname, char *outname, char *extension,
521 if (*outname) /* file name already exists, */
522 return; /* so do nothing */
526 *p++ = *q++; /* copy, and find end of string */
527 *p = '\0'; /* terminate it */
528 while (p > outname && *--p != '.') ; /* find final period (or whatever) */
531 p++; /* go back to end if none found */
532 if (!strcmp(p, extension)) { /* is the extension already there? */
534 error(ERR_WARNING | ERR_NOFILE,
535 "file name already ends in `%s': "
536 "output will be in `nasm.out'", extension);
538 error(ERR_WARNING | ERR_NOFILE,
539 "file name already has no extension: "
540 "output will be in `nasm.out'");
541 strcpy(outname, "nasm.out");
543 strcpy(p, extension);
547 * Common list of prefix names
549 static const char *prefix_names[] = {
550 "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
551 "rep", "repe", "repne", "repnz", "repz", "times", "wait"
554 const char *prefix_name(int token)
556 unsigned int prefix = token-PREFIX_ENUM_START;
557 if (prefix > elements(prefix_names))
560 return prefix_names[prefix];
566 int bsi(const char *string, const char **array, int size)
568 int i = -1, j = size; /* always, i < index < j */
571 int l = strcmp(string, array[k]);
572 if (l < 0) /* it's in the first half */
574 else if (l > 0) /* it's in the second half */
576 else /* we've got it :) */
579 return -1; /* we haven't got it :( */
582 int bsii(const char *string, const char **array, int size)
584 int i = -1, j = size; /* always, i < index < j */
587 int l = nasm_stricmp(string, array[k]);
588 if (l < 0) /* it's in the first half */
590 else if (l > 0) /* it's in the second half */
592 else /* we've got it :) */
595 return -1; /* we haven't got it :( */
598 static char *file_name = NULL;
599 static int32_t line_number = 0;
601 char *src_set_fname(char *newname)
603 char *oldname = file_name;
608 int32_t src_set_linnum(int32_t newline)
610 int32_t oldline = line_number;
611 line_number = newline;
615 int32_t src_get_linnum(void)
620 int src_get(int32_t *xline, char **xname)
622 if (!file_name || !*xname || strcmp(*xname, file_name)) {
624 *xname = file_name ? nasm_strdup(file_name) : NULL;
625 *xline = line_number;
628 if (*xline != line_number) {
629 int32_t tmp = line_number - *xline;
630 *xline = line_number;
636 char *nasm_strcat(const char *one, const char *two)
639 int l1 = strlen(one);
640 rslt = nasm_malloc(l1 + strlen(two) + 1);
642 strcpy(rslt + l1, two);