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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, Inc.,
10 * 51 Franklin St, Fifth Floor, Boston MA 02110-1301, USA; version 2.1,
11 * or, at your option, any later version, incorporated herein by
14 * Patches submitted to this file are required to be dual licensed
15 * under the LGPL 2.1+ and the 2-clause BSD license:
17 * Copyright 1996-2009 the NASM Authors - All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above
26 * copyright notice, this list of conditions and the following
27 * disclaimer in the documentation and/or other materials provided
28 * with the distribution.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
41 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 * ----------------------------------------------------------------------- */
47 * nasmlib.c library routines for the Netwide Assembler
62 int globalbits = 0; /* defined in nasm.h, works better here for ASM+DISASM */
63 efunc nasm_malloc_error; /* Exported for the benefit of vsnprintf.c */
69 /* Uninitialized -> all zero by C spec */
70 const uint8_t zero_buffer[ZERO_BUF_SIZE];
73 * Prepare a table of tolower() results. This avoids function calls
77 unsigned char nasm_tolower_tab[256];
79 void tolower_init(void)
83 for (i = 0; i < 256; i++)
84 nasm_tolower_tab[i] = tolower(i);
87 void nasm_set_malloc_error(efunc error)
89 nasm_malloc_error = error;
91 logfp = fopen("malloc.log", "w");
92 setvbuf(logfp, NULL, _IOLBF, BUFSIZ);
93 fprintf(logfp, "null pointer is %p\n", NULL);
98 void *nasm_malloc_log(char *file, int line, size_t size)
100 void *nasm_malloc(size_t size)
103 void *p = malloc(size);
105 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
108 fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
109 file, line, (long)size, p);
115 void *nasm_zalloc_log(char *file, int line, size_t size)
117 void *nasm_zalloc(size_t size)
120 void *p = calloc(size, 1);
122 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
125 fprintf(logfp, "%s %d calloc(%ld, 1) returns %p\n",
126 file, line, (long)size, p);
132 void *nasm_realloc_log(char *file, int line, void *q, size_t size)
134 void *nasm_realloc(void *q, size_t size)
137 void *p = q ? realloc(q, size) : malloc(size);
139 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
142 fprintf(logfp, "%s %d realloc(%p,%ld) returns %p\n",
143 file, line, q, (long)size, p);
145 fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
146 file, line, (long)size, p);
152 void nasm_free_log(char *file, int line, void *q)
154 void nasm_free(void *q)
159 fprintf(logfp, "%s %d free(%p)\n", file, line, q);
166 char *nasm_strdup_log(char *file, int line, const char *s)
168 char *nasm_strdup(const char *s)
172 int size = strlen(s) + 1;
176 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
179 fprintf(logfp, "%s %d strdup(%ld) returns %p\n",
180 file, line, (long)size, p);
187 char *nasm_strndup_log(char *file, int line, char *s, size_t len)
189 char *nasm_strndup(char *s, size_t len)
197 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
200 fprintf(logfp, "%s %d strndup(%ld) returns %p\n",
201 file, line, (long)size, p);
209 int nasm_stricmp(const char *s1, const char *s2)
211 unsigned char c1, c2;
215 c1 = nasm_tolower(*s1++);
216 c2 = nasm_tolower(*s2++);
228 #ifndef nasm_strnicmp
229 int nasm_strnicmp(const char *s1, const char *s2, size_t n)
231 unsigned char c1, c2;
235 c1 = nasm_tolower(*s1++);
236 c2 = nasm_tolower(*s2++);
248 int nasm_memicmp(const char *s1, const char *s2, size_t n)
250 unsigned char c1, c2;
254 c1 = nasm_tolower(*s1++);
255 c2 = nasm_tolower(*s2++);
264 char *nasm_strsep(char **stringp, const char *delim)
272 e = strpbrk(s, delim);
282 #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
283 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
285 static int radix_letter(char c)
290 return 2; /* Binary */
293 return 8; /* Octal */
296 return 16; /* Hexadecimal */
299 return 10; /* Decimal */
301 return 0; /* Not a known radix letter */
305 int64_t readnum(char *str, bool *error)
308 int32_t pradix, sradix, radix;
310 uint64_t result, checklimit;
317 while (nasm_isspace(*r))
318 r++; /* find start of number */
321 * If the number came from make_tok_num (as a result of an %assign), it
322 * might have a '-' built into it (rather than in a preceeding token).
331 while (lib_isnumchar(*q))
332 q++; /* find end of number */
342 * Handle radix formats:
344 * 0<radix-letter><string>
345 * $<string> (hexadecimal)
346 * <string><radix-letter>
351 if (len > 2 && *r == '0' && (pradix = radix_letter(r[1])) != 0)
353 else if (len > 1 && *r == '$')
354 pradix = 16, plen = 1;
356 if (len > 1 && (sradix = radix_letter(q[-1])) != 0)
359 if (pradix > sradix) {
362 } else if (sradix > pradix) {
366 /* Either decimal, or invalid -- if invalid, we'll trip up
372 * `checklimit' must be 2**64 / radix. We can't do that in
373 * 64-bit arithmetic, which we're (probably) using, so we
374 * cheat: since we know that all radices we use are even, we
375 * can divide 2**63 by radix/2 instead.
377 checklimit = 0x8000000000000000ULL / (radix >> 1);
380 * Calculate the highest allowable value for the last digit of a
381 * 64-bit constant... in radix 10, it is 6, otherwise it is 0
383 last = (radix == 10 ? 6 : 0);
386 while (*r && r < q) {
388 if (*r < '0' || (*r > '9' && *r < 'A')
389 || (digit = numvalue(*r)) >= radix) {
393 if (result > checklimit ||
394 (result == checklimit && digit >= last)) {
398 result = radix * result + digit;
404 nasm_malloc_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
405 "numeric constant %s does not fit in 64 bits",
408 return result * sign;
411 int64_t readstrnum(char *str, int length, bool *warn)
413 int64_t charconst = 0;
419 if (globalbits == 64) {
420 for (i = 0; i < length; i++) {
421 if (charconst & 0xFF00000000000000ULL)
423 charconst = (charconst << 8) + (uint8_t)*--str;
426 for (i = 0; i < length; i++) {
427 if (charconst & 0xFF000000UL)
429 charconst = (charconst << 8) + (uint8_t)*--str;
435 static int32_t next_seg;
442 int32_t seg_alloc(void)
444 return (next_seg += 2) - 2;
447 #ifdef WORDS_LITTLEENDIAN
449 void fwriteint16_t(uint16_t data, FILE * fp)
451 fwrite(&data, 1, 2, fp);
454 void fwriteint32_t(uint32_t data, FILE * fp)
456 fwrite(&data, 1, 4, fp);
459 void fwriteint64_t(uint64_t data, FILE * fp)
461 fwrite(&data, 1, 8, fp);
464 void fwriteaddr(uint64_t data, int size, FILE * fp)
466 fwrite(&data, 1, size, fp);
469 #else /* not WORDS_LITTLEENDIAN */
471 void fwriteint16_t(uint16_t data, FILE * fp)
473 char buffer[2], *p = buffer;
475 fwrite(buffer, 1, 2, fp);
478 void fwriteint32_t(uint32_t data, FILE * fp)
480 char buffer[4], *p = buffer;
482 fwrite(buffer, 1, 4, fp);
485 void fwriteint64_t(uint64_t data, FILE * fp)
487 char buffer[8], *p = buffer;
489 fwrite(buffer, 1, 8, fp);
492 void fwriteaddr(uint64_t data, int size, FILE * fp)
494 char buffer[8], *p = buffer;
495 WRITEADDR(p, data, size);
496 fwrite(buffer, 1, size, fp);
501 size_t fwritezero(size_t bytes, FILE *fp)
508 blksize = (bytes < ZERO_BUF_SIZE) ? bytes : ZERO_BUF_SIZE;
510 rv = fwrite(zero_buffer, 1, blksize, fp);
521 void standard_extension(char *inname, char *outname, char *extension,
526 if (*outname) /* file name already exists, */
527 return; /* so do nothing */
531 *p++ = *q++; /* copy, and find end of string */
532 *p = '\0'; /* terminate it */
533 while (p > outname && *--p != '.') ; /* find final period (or whatever) */
536 p++; /* go back to end if none found */
537 if (!strcmp(p, extension)) { /* is the extension already there? */
539 error(ERR_WARNING | ERR_NOFILE,
540 "file name already ends in `%s': "
541 "output will be in `nasm.out'", extension);
543 error(ERR_WARNING | ERR_NOFILE,
544 "file name already has no extension: "
545 "output will be in `nasm.out'");
546 strcpy(outname, "nasm.out");
548 strcpy(p, extension);
552 * Common list of prefix names
554 static const char *prefix_names[] = {
555 "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
556 "rep", "repe", "repne", "repnz", "repz", "times", "wait"
559 const char *prefix_name(int token)
561 unsigned int prefix = token-PREFIX_ENUM_START;
562 if (prefix > elements(prefix_names))
565 return prefix_names[prefix];
571 int bsi(const char *string, const char **array, int size)
573 int i = -1, j = size; /* always, i < index < j */
576 int l = strcmp(string, array[k]);
577 if (l < 0) /* it's in the first half */
579 else if (l > 0) /* it's in the second half */
581 else /* we've got it :) */
584 return -1; /* we haven't got it :( */
587 int bsii(const char *string, const char **array, int size)
589 int i = -1, j = size; /* always, i < index < j */
592 int l = nasm_stricmp(string, array[k]);
593 if (l < 0) /* it's in the first half */
595 else if (l > 0) /* it's in the second half */
597 else /* we've got it :) */
600 return -1; /* we haven't got it :( */
603 static char *file_name = NULL;
604 static int32_t line_number = 0;
606 char *src_set_fname(char *newname)
608 char *oldname = file_name;
613 int32_t src_set_linnum(int32_t newline)
615 int32_t oldline = line_number;
616 line_number = newline;
620 int32_t src_get_linnum(void)
625 int src_get(int32_t *xline, char **xname)
627 if (!file_name || !*xname || strcmp(*xname, file_name)) {
629 *xname = file_name ? nasm_strdup(file_name) : NULL;
630 *xline = line_number;
633 if (*xline != line_number) {
634 int32_t tmp = line_number - *xline;
635 *xline = line_number;
641 char *nasm_strcat(const char *one, const char *two)
644 int l1 = strlen(one);
645 rslt = nasm_malloc(l1 + strlen(two) + 1);
647 strcpy(rslt + l1, two);