NASM 2.08.02
[nasm/avx512.git] / nasmlib.c
blob22d3b6208a91526e8a9d94d039ca000daac8960e
1 /* ----------------------------------------------------------------------- *
2 *
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
9 * conditions are met:
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
38 #include "compiler.h"
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <inttypes.h>
46 #include "nasm.h"
47 #include "nasmlib.h"
48 #include "insns.h"
50 int globalbits = 0; /* defined in nasm.h, works better here for ASM+DISASM */
51 static vefunc nasm_verror; /* Global error handling function */
53 #ifdef LOGALLOC
54 static FILE *logfp;
55 #endif
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
62 * on some platforms.
65 unsigned char nasm_tolower_tab[256];
67 void tolower_init(void)
69 int i;
71 for (i = 0; i < 256; i++)
72 nasm_tolower_tab[i] = tolower(i);
75 void nasm_set_verror(vefunc ve)
77 nasm_verror = ve;
80 void nasm_error(int severity, const char *fmt, ...)
82 va_list ap;
84 va_start(ap, fmt);
85 nasm_verror(severity, fmt, ap);
86 va_end(ap);
89 void nasm_init_malloc_error(void)
91 #ifdef LOGALLOC
92 logfp = fopen("malloc.log", "w");
93 setvbuf(logfp, NULL, _IOLBF, BUFSIZ);
94 fprintf(logfp, "null pointer is %p\n", NULL);
95 #endif
98 #ifdef LOGALLOC
99 void *nasm_malloc_log(const char *file, int line, size_t size)
100 #else
101 void *nasm_malloc(size_t size)
102 #endif
104 void *p = malloc(size);
105 if (!p)
106 nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
107 #ifdef LOGALLOC
108 else
109 fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
110 file, line, (long)size, p);
111 #endif
112 return p;
115 #ifdef LOGALLOC
116 void *nasm_zalloc_log(const char *file, int line, size_t size)
117 #else
118 void *nasm_zalloc(size_t size)
119 #endif
121 void *p = calloc(size, 1);
122 if (!p)
123 nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
124 #ifdef LOGALLOC
125 else
126 fprintf(logfp, "%s %d calloc(%ld, 1) returns %p\n",
127 file, line, (long)size, p);
128 #endif
129 return p;
132 #ifdef LOGALLOC
133 void *nasm_realloc_log(const char *file, int line, void *q, size_t size)
134 #else
135 void *nasm_realloc(void *q, size_t size)
136 #endif
138 void *p = q ? realloc(q, size) : malloc(size);
139 if (!p)
140 nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
141 #ifdef LOGALLOC
142 else if (q)
143 fprintf(logfp, "%s %d realloc(%p,%ld) returns %p\n",
144 file, line, q, (long)size, p);
145 else
146 fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
147 file, line, (long)size, p);
148 #endif
149 return p;
152 #ifdef LOGALLOC
153 void nasm_free_log(const char *file, int line, void *q)
154 #else
155 void nasm_free(void *q)
156 #endif
158 if (q) {
159 #ifdef LOGALLOC
160 fprintf(logfp, "%s %d free(%p)\n", file, line, q);
161 #endif
162 free(q);
166 #ifdef LOGALLOC
167 char *nasm_strdup_log(const char *file, int line, const char *s)
168 #else
169 char *nasm_strdup(const char *s)
170 #endif
172 char *p;
173 int size = strlen(s) + 1;
175 p = malloc(size);
176 if (!p)
177 nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
178 #ifdef LOGALLOC
179 else
180 fprintf(logfp, "%s %d strdup(%ld) returns %p\n",
181 file, line, (long)size, p);
182 #endif
183 strcpy(p, s);
184 return p;
187 #ifdef LOGALLOC
188 char *nasm_strndup_log(const char *file, int line, const char *s, size_t len)
189 #else
190 char *nasm_strndup(const char *s, size_t len)
191 #endif
193 char *p;
194 int size = len + 1;
196 p = malloc(size);
197 if (!p)
198 nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
199 #ifdef LOGALLOC
200 else
201 fprintf(logfp, "%s %d strndup(%ld) returns %p\n",
202 file, line, (long)size, p);
203 #endif
204 strncpy(p, s, len);
205 p[len] = '\0';
206 return p;
209 no_return nasm_assert_failed(const char *file, int line, const char *msg)
211 nasm_error(ERR_FATAL, "assertion %s failed at %s:%d", msg, file, line);
212 exit(1);
215 #ifndef nasm_stricmp
216 int nasm_stricmp(const char *s1, const char *s2)
218 unsigned char c1, c2;
219 int d;
221 while (1) {
222 c1 = nasm_tolower(*s1++);
223 c2 = nasm_tolower(*s2++);
224 d = c1-c2;
226 if (d)
227 return d;
228 if (!c1)
229 break;
231 return 0;
233 #endif
235 #ifndef nasm_strnicmp
236 int nasm_strnicmp(const char *s1, const char *s2, size_t n)
238 unsigned char c1, c2;
239 int d;
241 while (n--) {
242 c1 = nasm_tolower(*s1++);
243 c2 = nasm_tolower(*s2++);
244 d = c1-c2;
246 if (d)
247 return d;
248 if (!c1)
249 break;
251 return 0;
253 #endif
255 int nasm_memicmp(const char *s1, const char *s2, size_t n)
257 unsigned char c1, c2;
258 int d;
260 while (n--) {
261 c1 = nasm_tolower(*s1++);
262 c2 = nasm_tolower(*s2++);
263 d = c1-c2;
264 if (d)
265 return d;
267 return 0;
270 #ifndef nasm_strsep
271 char *nasm_strsep(char **stringp, const char *delim)
273 char *s = *stringp;
274 char *e;
276 if (!s)
277 return NULL;
279 e = strpbrk(s, delim);
280 if (e)
281 *e++ = '\0';
283 *stringp = e;
284 return s;
286 #endif
289 #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
290 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
292 static int radix_letter(char c)
294 switch (c) {
295 case 'b': case 'B':
296 case 'y': case 'Y':
297 return 2; /* Binary */
298 case 'o': case 'O':
299 case 'q': case 'Q':
300 return 8; /* Octal */
301 case 'h': case 'H':
302 case 'x': case 'X':
303 return 16; /* Hexadecimal */
304 case 'd': case 'D':
305 case 't': case 'T':
306 return 10; /* Decimal */
307 default:
308 return 0; /* Not a known radix letter */
312 int64_t readnum(char *str, bool *error)
314 char *r = str, *q;
315 int32_t pradix, sradix, radix;
316 int plen, slen, len;
317 uint64_t result, checklimit;
318 int digit, last;
319 bool warn = false;
320 int sign = 1;
322 *error = false;
324 while (nasm_isspace(*r))
325 r++; /* find start of number */
328 * If the number came from make_tok_num (as a result of an %assign), it
329 * might have a '-' built into it (rather than in a preceeding token).
331 if (*r == '-') {
332 r++;
333 sign = -1;
336 q = r;
338 while (lib_isnumchar(*q))
339 q++; /* find end of number */
341 len = q-r;
342 if (!len) {
343 /* Not numeric */
344 *error = true;
345 return 0;
349 * Handle radix formats:
351 * 0<radix-letter><string>
352 * $<string> (hexadecimal)
353 * <string><radix-letter>
355 pradix = sradix = 0;
356 plen = slen = 0;
358 if (len > 2 && *r == '0' && (pradix = radix_letter(r[1])) != 0)
359 plen = 2;
360 else if (len > 1 && *r == '$')
361 pradix = 16, plen = 1;
363 if (len > 1 && (sradix = radix_letter(q[-1])) != 0)
364 slen = 1;
366 if (pradix > sradix) {
367 radix = pradix;
368 r += plen;
369 } else if (sradix > pradix) {
370 radix = sradix;
371 q -= slen;
372 } else {
373 /* Either decimal, or invalid -- if invalid, we'll trip up
374 further down. */
375 radix = 10;
379 * `checklimit' must be 2**64 / radix. We can't do that in
380 * 64-bit arithmetic, which we're (probably) using, so we
381 * cheat: since we know that all radices we use are even, we
382 * can divide 2**63 by radix/2 instead.
384 checklimit = UINT64_C(0x8000000000000000) / (radix >> 1);
387 * Calculate the highest allowable value for the last digit of a
388 * 64-bit constant... in radix 10, it is 6, otherwise it is 0
390 last = (radix == 10 ? 6 : 0);
392 result = 0;
393 while (*r && r < q) {
394 if (*r != '_') {
395 if (*r < '0' || (*r > '9' && *r < 'A')
396 || (digit = numvalue(*r)) >= radix) {
397 *error = true;
398 return 0;
400 if (result > checklimit ||
401 (result == checklimit && digit >= last)) {
402 warn = true;
405 result = radix * result + digit;
407 r++;
410 if (warn)
411 nasm_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
412 "numeric constant %s does not fit in 64 bits",
413 str);
415 return result * sign;
418 int64_t readstrnum(char *str, int length, bool *warn)
420 int64_t charconst = 0;
421 int i;
423 *warn = false;
425 str += length;
426 if (globalbits == 64) {
427 for (i = 0; i < length; i++) {
428 if (charconst & UINT64_C(0xFF00000000000000))
429 *warn = true;
430 charconst = (charconst << 8) + (uint8_t)*--str;
432 } else {
433 for (i = 0; i < length; i++) {
434 if (charconst & 0xFF000000UL)
435 *warn = true;
436 charconst = (charconst << 8) + (uint8_t)*--str;
439 return charconst;
442 static int32_t next_seg;
444 void seg_init(void)
446 next_seg = 0;
449 int32_t seg_alloc(void)
451 return (next_seg += 2) - 2;
454 #ifdef WORDS_LITTLEENDIAN
456 void fwriteint16_t(uint16_t data, FILE * fp)
458 fwrite(&data, 1, 2, fp);
461 void fwriteint32_t(uint32_t data, FILE * fp)
463 fwrite(&data, 1, 4, fp);
466 void fwriteint64_t(uint64_t data, FILE * fp)
468 fwrite(&data, 1, 8, fp);
471 void fwriteaddr(uint64_t data, int size, FILE * fp)
473 fwrite(&data, 1, size, fp);
476 #else /* not WORDS_LITTLEENDIAN */
478 void fwriteint16_t(uint16_t data, FILE * fp)
480 char buffer[2], *p = buffer;
481 WRITESHORT(p, data);
482 fwrite(buffer, 1, 2, fp);
485 void fwriteint32_t(uint32_t data, FILE * fp)
487 char buffer[4], *p = buffer;
488 WRITELONG(p, data);
489 fwrite(buffer, 1, 4, fp);
492 void fwriteint64_t(uint64_t data, FILE * fp)
494 char buffer[8], *p = buffer;
495 WRITEDLONG(p, data);
496 fwrite(buffer, 1, 8, fp);
499 void fwriteaddr(uint64_t data, int size, FILE * fp)
501 char buffer[8], *p = buffer;
502 WRITEADDR(p, data, size);
503 fwrite(buffer, 1, size, fp);
506 #endif
508 size_t fwritezero(size_t bytes, FILE *fp)
510 size_t count = 0;
511 size_t blksize;
512 size_t rv;
514 while (bytes) {
515 blksize = (bytes < ZERO_BUF_SIZE) ? bytes : ZERO_BUF_SIZE;
517 rv = fwrite(zero_buffer, 1, blksize, fp);
518 if (!rv)
519 break;
521 count += rv;
522 bytes -= rv;
525 return count;
528 void standard_extension(char *inname, char *outname, char *extension)
530 char *p, *q;
532 if (*outname) /* file name already exists, */
533 return; /* so do nothing */
534 q = inname;
535 p = outname;
536 while (*q)
537 *p++ = *q++; /* copy, and find end of string */
538 *p = '\0'; /* terminate it */
539 while (p > outname && *--p != '.') ; /* find final period (or whatever) */
540 if (*p != '.')
541 while (*p)
542 p++; /* go back to end if none found */
543 if (!strcmp(p, extension)) { /* is the extension already there? */
544 if (*extension)
545 nasm_error(ERR_WARNING | ERR_NOFILE,
546 "file name already ends in `%s': "
547 "output will be in `nasm.out'", extension);
548 else
549 nasm_error(ERR_WARNING | ERR_NOFILE,
550 "file name already has no extension: "
551 "output will be in `nasm.out'");
552 strcpy(outname, "nasm.out");
553 } else
554 strcpy(p, extension);
558 * Common list of prefix names
560 static const char *prefix_names[] = {
561 "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
562 "rep", "repe", "repne", "repnz", "repz", "times", "wait"
565 const char *prefix_name(int token)
567 unsigned int prefix = token-PREFIX_ENUM_START;
568 if (prefix > elements(prefix_names))
569 return NULL;
571 return prefix_names[prefix];
575 * Binary search.
577 int bsi(const char *string, const char **array, int size)
579 int i = -1, j = size; /* always, i < index < j */
580 while (j - i >= 2) {
581 int k = (i + j) / 2;
582 int l = strcmp(string, array[k]);
583 if (l < 0) /* it's in the first half */
584 j = k;
585 else if (l > 0) /* it's in the second half */
586 i = k;
587 else /* we've got it :) */
588 return k;
590 return -1; /* we haven't got it :( */
593 int bsii(const char *string, const char **array, int size)
595 int i = -1, j = size; /* always, i < index < j */
596 while (j - i >= 2) {
597 int k = (i + j) / 2;
598 int l = nasm_stricmp(string, array[k]);
599 if (l < 0) /* it's in the first half */
600 j = k;
601 else if (l > 0) /* it's in the second half */
602 i = k;
603 else /* we've got it :) */
604 return k;
606 return -1; /* we haven't got it :( */
609 static char *file_name = NULL;
610 static int32_t line_number = 0;
612 char *src_set_fname(char *newname)
614 char *oldname = file_name;
615 file_name = newname;
616 return oldname;
619 int32_t src_set_linnum(int32_t newline)
621 int32_t oldline = line_number;
622 line_number = newline;
623 return oldline;
626 int32_t src_get_linnum(void)
628 return line_number;
631 int src_get(int32_t *xline, char **xname)
633 if (!file_name || !*xname || strcmp(*xname, file_name)) {
634 nasm_free(*xname);
635 *xname = file_name ? nasm_strdup(file_name) : NULL;
636 *xline = line_number;
637 return -2;
639 if (*xline != line_number) {
640 int32_t tmp = line_number - *xline;
641 *xline = line_number;
642 return tmp;
644 return 0;
647 char *nasm_strcat(const char *one, const char *two)
649 char *rslt;
650 int l1 = strlen(one);
651 rslt = nasm_malloc(l1 + strlen(two) + 1);
652 strcpy(rslt, one);
653 strcpy(rslt + l1, two);
654 return rslt;
657 /* skip leading spaces */
658 char *nasm_skip_spaces(const char *p)
660 if (p)
661 while (*p && nasm_isspace(*p))
662 p++;
663 return (char *)p;
666 /* skip leading non-spaces */
667 char *nasm_skip_word(const char *p)
669 if (p)
670 while (*p && !nasm_isspace(*p))
671 p++;
672 return (char *)p;
675 /* zap leading spaces with zero */
676 char *nasm_zap_spaces_fwd(char *p)
678 if (p)
679 while (*p && nasm_isspace(*p))
680 *p++ = 0x0;
681 return p;
684 /* zap spaces with zero in reverse order */
685 char *nasm_zap_spaces_rev(char *p)
687 if (p)
688 while (*p && nasm_isspace(*p))
689 *p-- = 0x0;
690 return p;
694 * initialized data bytes length from opcode
696 int idata_bytes(int opcode)
698 int ret;
699 switch (opcode) {
700 case I_DB:
701 ret = 1;
702 break;
703 case I_DW:
704 ret = 2;
705 break;
706 case I_DD:
707 ret = 4;
708 break;
709 case I_DQ:
710 ret = 8;
711 break;
712 case I_DT:
713 ret = 10;
714 break;
715 case I_DO:
716 ret = 16;
717 break;
718 case I_DY:
719 ret = 32;
720 break;
721 case I_none:
722 ret = -1;
723 break;
724 default:
725 ret = 0;
726 break;
728 return ret;