Merge branch 'master' into elfmerge
[nasm.git] / nasmlib.c
blob49f1e470191aaa05f833af0df5c5b8c02653734d
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2016 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 <errno.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "insns.h"
51 int globalbits = 0; /* defined in nasm.h, works better here for ASM+DISASM */
52 vefunc nasm_verror; /* Global error handling function */
54 /* Uninitialized -> all zero by C spec */
55 const uint8_t zero_buffer[ZERO_BUF_SIZE];
58 * Prepare a table of tolower() results. This avoids function calls
59 * on some platforms.
62 unsigned char nasm_tolower_tab[256];
64 void tolower_init(void)
66 int i;
68 for (i = 0; i < 256; i++)
69 nasm_tolower_tab[i] = tolower(i);
72 void nasm_error(int severity, const char *fmt, ...)
74 va_list ap;
76 va_start(ap, fmt);
77 nasm_verror(severity, fmt, ap);
78 va_end(ap);
81 no_return nasm_fatal(int flags, const char *fmt, ...)
83 va_list ap;
85 va_start(ap, fmt);
86 nasm_verror(flags | ERR_FATAL, fmt, ap);
87 abort(); /* We should never get here */
90 no_return nasm_panic(int flags, const char *fmt, ...)
92 va_list ap;
94 va_start(ap, fmt);
95 nasm_verror(flags | ERR_PANIC, fmt, ap);
96 abort(); /* We should never get here */
99 no_return nasm_panic_from_macro(const char *file, int line)
101 nasm_panic(ERR_NOFILE, "Internal error at %s:%d\n", file, line);
104 void *nasm_malloc(size_t size)
106 void *p = malloc(size);
107 if (!p)
108 nasm_fatal(ERR_NOFILE, "out of memory");
109 return p;
112 void *nasm_zalloc(size_t size)
114 void *p = calloc(size, 1);
115 if (!p)
116 nasm_fatal(ERR_NOFILE, "out of memory");
117 return p;
120 void *nasm_realloc(void *q, size_t size)
122 void *p = q ? realloc(q, size) : malloc(size);
123 if (!p)
124 nasm_fatal(ERR_NOFILE, "out of memory");
125 return p;
128 void nasm_free(void *q)
130 if (q)
131 free(q);
134 char *nasm_strdup(const char *s)
136 char *p;
137 int size = strlen(s) + 1;
139 p = malloc(size);
140 if (!p)
141 nasm_fatal(ERR_NOFILE, "out of memory");
142 strcpy(p, s);
143 return p;
146 char *nasm_strndup(const char *s, size_t len)
148 char *p;
149 int size = len + 1;
151 p = malloc(size);
152 if (!p)
153 nasm_fatal(ERR_NOFILE, "out of memory");
154 strncpy(p, s, len);
155 p[len] = '\0';
156 return p;
159 no_return nasm_assert_failed(const char *file, int line, const char *msg)
161 nasm_error(ERR_FATAL, "assertion %s failed at %s:%d", msg, file, line);
162 exit(1);
165 void nasm_write(const void *ptr, size_t size, FILE *f)
167 size_t n = fwrite(ptr, 1, size, f);
168 if (n != size)
169 nasm_error(ERR_FATAL, "unable to write output: %s", strerror(errno));
172 #ifndef nasm_stricmp
173 int nasm_stricmp(const char *s1, const char *s2)
175 unsigned char c1, c2;
176 int d;
178 while (1) {
179 c1 = nasm_tolower(*s1++);
180 c2 = nasm_tolower(*s2++);
181 d = c1-c2;
183 if (d)
184 return d;
185 if (!c1)
186 break;
188 return 0;
190 #endif
192 #ifndef nasm_strnicmp
193 int nasm_strnicmp(const char *s1, const char *s2, size_t n)
195 unsigned char c1, c2;
196 int d;
198 while (n--) {
199 c1 = nasm_tolower(*s1++);
200 c2 = nasm_tolower(*s2++);
201 d = c1-c2;
203 if (d)
204 return d;
205 if (!c1)
206 break;
208 return 0;
210 #endif
212 int nasm_memicmp(const char *s1, const char *s2, size_t n)
214 unsigned char c1, c2;
215 int d;
217 while (n--) {
218 c1 = nasm_tolower(*s1++);
219 c2 = nasm_tolower(*s2++);
220 d = c1-c2;
221 if (d)
222 return d;
224 return 0;
227 #ifndef nasm_strsep
228 char *nasm_strsep(char **stringp, const char *delim)
230 char *s = *stringp;
231 char *e;
233 if (!s)
234 return NULL;
236 e = strpbrk(s, delim);
237 if (e)
238 *e++ = '\0';
240 *stringp = e;
241 return s;
243 #endif
246 #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
248 static int radix_letter(char c)
250 switch (c) {
251 case 'b': case 'B':
252 case 'y': case 'Y':
253 return 2; /* Binary */
254 case 'o': case 'O':
255 case 'q': case 'Q':
256 return 8; /* Octal */
257 case 'h': case 'H':
258 case 'x': case 'X':
259 return 16; /* Hexadecimal */
260 case 'd': case 'D':
261 case 't': case 'T':
262 return 10; /* Decimal */
263 default:
264 return 0; /* Not a known radix letter */
268 int64_t readnum(char *str, bool *error)
270 char *r = str, *q;
271 int32_t pradix, sradix, radix;
272 int plen, slen, len;
273 uint64_t result, checklimit;
274 int digit, last;
275 bool warn = false;
276 int sign = 1;
278 *error = false;
280 while (nasm_isspace(*r))
281 r++; /* find start of number */
284 * If the number came from make_tok_num (as a result of an %assign), it
285 * might have a '-' built into it (rather than in a preceeding token).
287 if (*r == '-') {
288 r++;
289 sign = -1;
292 q = r;
294 while (lib_isnumchar(*q))
295 q++; /* find end of number */
297 len = q-r;
298 if (!len) {
299 /* Not numeric */
300 *error = true;
301 return 0;
305 * Handle radix formats:
307 * 0<radix-letter><string>
308 * $<string> (hexadecimal)
309 * <string><radix-letter>
311 pradix = sradix = 0;
312 plen = slen = 0;
314 if (len > 2 && *r == '0' && (pradix = radix_letter(r[1])) != 0)
315 plen = 2;
316 else if (len > 1 && *r == '$')
317 pradix = 16, plen = 1;
319 if (len > 1 && (sradix = radix_letter(q[-1])) != 0)
320 slen = 1;
322 if (pradix > sradix) {
323 radix = pradix;
324 r += plen;
325 } else if (sradix > pradix) {
326 radix = sradix;
327 q -= slen;
328 } else {
329 /* Either decimal, or invalid -- if invalid, we'll trip up
330 further down. */
331 radix = 10;
335 * `checklimit' must be 2**64 / radix. We can't do that in
336 * 64-bit arithmetic, which we're (probably) using, so we
337 * cheat: since we know that all radices we use are even, we
338 * can divide 2**63 by radix/2 instead.
340 checklimit = UINT64_C(0x8000000000000000) / (radix >> 1);
343 * Calculate the highest allowable value for the last digit of a
344 * 64-bit constant... in radix 10, it is 6, otherwise it is 0
346 last = (radix == 10 ? 6 : 0);
348 result = 0;
349 while (*r && r < q) {
350 if (*r != '_') {
351 if (*r < '0' || (*r > '9' && *r < 'A')
352 || (digit = numvalue(*r)) >= radix) {
353 *error = true;
354 return 0;
356 if (result > checklimit ||
357 (result == checklimit && digit >= last)) {
358 warn = true;
361 result = radix * result + digit;
363 r++;
366 if (warn)
367 nasm_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
368 "numeric constant %s does not fit in 64 bits",
369 str);
371 return result * sign;
374 int64_t readstrnum(char *str, int length, bool *warn)
376 int64_t charconst = 0;
377 int i;
379 *warn = false;
381 str += length;
382 if (globalbits == 64) {
383 for (i = 0; i < length; i++) {
384 if (charconst & UINT64_C(0xFF00000000000000))
385 *warn = true;
386 charconst = (charconst << 8) + (uint8_t)*--str;
388 } else {
389 for (i = 0; i < length; i++) {
390 if (charconst & 0xFF000000UL)
391 *warn = true;
392 charconst = (charconst << 8) + (uint8_t)*--str;
395 return charconst;
398 int32_t seg_alloc(void)
400 static int32_t next_seg = 0;
401 int32_t this_seg = next_seg;
403 next_seg += 2;
405 return this_seg;
408 #ifdef WORDS_LITTLEENDIAN
410 void fwriteint16_t(uint16_t data, FILE * fp)
412 nasm_write(&data, 2, fp);
415 void fwriteint32_t(uint32_t data, FILE * fp)
417 nasm_write(&data, 4, fp);
420 void fwriteint64_t(uint64_t data, FILE * fp)
422 nasm_write(&data, 8, fp);
425 void fwriteaddr(uint64_t data, int size, FILE * fp)
427 nasm_write(&data, size, fp);
430 #else /* not WORDS_LITTLEENDIAN */
432 void fwriteint16_t(uint16_t data, FILE * fp)
434 char buffer[2], *p = buffer;
435 WRITESHORT(p, data);
436 nasm_write(buffer, 2, fp);
439 void fwriteint32_t(uint32_t data, FILE * fp)
441 char buffer[4], *p = buffer;
442 WRITELONG(p, data);
443 nasm_write(buffer, 4, fp);
446 void fwriteint64_t(uint64_t data, FILE * fp)
448 char buffer[8], *p = buffer;
449 WRITEDLONG(p, data);
450 nasm_write(buffer, 8, fp);
453 void fwriteaddr(uint64_t data, int size, FILE * fp)
455 char buffer[8], *p = buffer;
456 WRITEADDR(p, data, size);
457 nasm_write(buffer, size, fp);
460 #endif
462 void fwritezero(size_t bytes, FILE *fp)
464 size_t blksize;
466 while (bytes) {
467 blksize = (bytes < ZERO_BUF_SIZE) ? bytes : ZERO_BUF_SIZE;
469 nasm_write(zero_buffer, blksize, fp);
470 bytes -= blksize;
474 void standard_extension(char *inname, char *outname, char *extension)
476 char *p, *q;
478 if (*outname) /* file name already exists, */
479 return; /* so do nothing */
480 q = inname;
481 p = outname;
482 while (*q)
483 *p++ = *q++; /* copy, and find end of string */
484 *p = '\0'; /* terminate it */
485 while (p > outname && *--p != '.') ; /* find final period (or whatever) */
486 if (*p != '.')
487 while (*p)
488 p++; /* go back to end if none found */
489 if (!strcmp(p, extension)) { /* is the extension already there? */
490 if (*extension)
491 nasm_error(ERR_WARNING | ERR_NOFILE,
492 "file name already ends in `%s': "
493 "output will be in `nasm.out'", extension);
494 else
495 nasm_error(ERR_WARNING | ERR_NOFILE,
496 "file name already has no extension: "
497 "output will be in `nasm.out'");
498 strcpy(outname, "nasm.out");
499 } else
500 strcpy(p, extension);
504 * Common list of prefix names
506 static const char *prefix_names[] = {
507 "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
508 "rep", "repe", "repne", "repnz", "repz", "times", "wait",
509 "xacquire", "xrelease", "bnd"
512 const char *prefix_name(int token)
514 unsigned int prefix = token-PREFIX_ENUM_START;
515 if (prefix >= ARRAY_SIZE(prefix_names))
516 return NULL;
518 return prefix_names[prefix];
522 * Binary search.
524 int bsi(const char *string, const char **array, int size)
526 int i = -1, j = size; /* always, i < index < j */
527 while (j - i >= 2) {
528 int k = (i + j) / 2;
529 int l = strcmp(string, array[k]);
530 if (l < 0) /* it's in the first half */
531 j = k;
532 else if (l > 0) /* it's in the second half */
533 i = k;
534 else /* we've got it :) */
535 return k;
537 return -1; /* we haven't got it :( */
540 int bsii(const char *string, const char **array, int size)
542 int i = -1, j = size; /* always, i < index < j */
543 while (j - i >= 2) {
544 int k = (i + j) / 2;
545 int l = nasm_stricmp(string, array[k]);
546 if (l < 0) /* it's in the first half */
547 j = k;
548 else if (l > 0) /* it's in the second half */
549 i = k;
550 else /* we've got it :) */
551 return k;
553 return -1; /* we haven't got it :( */
556 static char *file_name = NULL;
557 static int32_t line_number = 0;
559 char *src_set_fname(char *newname)
561 char *oldname = file_name;
562 file_name = newname;
563 return oldname;
566 int32_t src_set_linnum(int32_t newline)
568 int32_t oldline = line_number;
569 line_number = newline;
570 return oldline;
573 int32_t src_get_linnum(void)
575 return line_number;
578 int src_get(int32_t *xline, char **xname)
580 if (!file_name || !*xname || strcmp(*xname, file_name)) {
581 nasm_free(*xname);
582 *xname = file_name ? nasm_strdup(file_name) : NULL;
583 *xline = line_number;
584 return -2;
586 if (*xline != line_number) {
587 int32_t tmp = line_number - *xline;
588 *xline = line_number;
589 return tmp;
591 return 0;
594 char *nasm_strcat(const char *one, const char *two)
596 char *rslt;
597 int l1 = strlen(one);
598 rslt = nasm_malloc(l1 + strlen(two) + 1);
599 strcpy(rslt, one);
600 strcpy(rslt + l1, two);
601 return rslt;
604 /* skip leading spaces */
605 char *nasm_skip_spaces(const char *p)
607 if (p)
608 while (*p && nasm_isspace(*p))
609 p++;
610 return (char *)p;
613 /* skip leading non-spaces */
614 char *nasm_skip_word(const char *p)
616 if (p)
617 while (*p && !nasm_isspace(*p))
618 p++;
619 return (char *)p;
622 /* zap leading spaces with zero */
623 char *nasm_zap_spaces_fwd(char *p)
625 if (p)
626 while (*p && nasm_isspace(*p))
627 *p++ = 0x0;
628 return p;
631 /* zap spaces with zero in reverse order */
632 char *nasm_zap_spaces_rev(char *p)
634 if (p)
635 while (*p && nasm_isspace(*p))
636 *p-- = 0x0;
637 return p;
640 /* zap leading and trailing spaces */
641 char *nasm_trim_spaces(char *p)
643 p = nasm_zap_spaces_fwd(p);
644 nasm_zap_spaces_fwd(nasm_skip_word(p));
646 return p;
650 * return the word extracted from a stream
651 * or NULL if nothing left
653 char *nasm_get_word(char *p, char **tail)
655 char *word = nasm_skip_spaces(p);
656 char *next = nasm_skip_word(word);
658 if (word && *word) {
659 if (*next)
660 *next++ = '\0';
661 } else
662 word = next = NULL;
664 /* NOTE: the tail may start with spaces */
665 *tail = next;
667 return word;
671 * Extract "opt=val" values from the stream and
672 * returns "opt"
674 * Exceptions:
675 * 1) If "=val" passed the NULL returned though
676 * you may continue handling the tail via "next"
677 * 2) If "=" passed the NULL is returned and "val"
678 * is set to NULL as well
680 char *nasm_opt_val(char *p, char **val, char **next)
682 char *q, *nxt;
684 *val = *next = NULL;
686 p = nasm_get_word(p, &nxt);
687 if (!p)
688 return NULL;
690 q = strchr(p, '=');
691 if (q) {
692 if (q == p)
693 p = NULL;
694 *q++='\0';
695 if (*q) {
696 *val = q;
697 } else {
698 q = nasm_get_word(q + 1, &nxt);
699 if (q)
700 *val = q;
702 } else {
703 q = nasm_skip_spaces(nxt);
704 if (q && *q == '=') {
705 q = nasm_get_word(q + 1, &nxt);
706 if (q)
707 *val = q;
711 *next = nxt;
712 return p;
716 * initialized data bytes length from opcode
718 int idata_bytes(int opcode)
720 switch (opcode) {
721 case I_DB:
722 return 1;
723 case I_DW:
724 return 2;
725 case I_DD:
726 return 4;
727 case I_DQ:
728 return 8;
729 case I_DT:
730 return 10;
731 case I_DO:
732 return 16;
733 case I_DY:
734 return 32;
735 case I_DZ:
736 return 64;
737 case I_none:
738 return -1;
739 default:
740 return 0;