nasmlib: allow writing of sparse files
[nasm.git] / nasmlib.c
blobeb0217d1fd5827500543995f0fc8290fc98ce76b
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 || ferror(f) || feof(f))
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 #ifndef HAVE_FSEEKO
463 # define fseeko fseek
464 # define ftello ftell
465 #endif
467 #ifdef HAVE_FILENO /* Useless without fileno() */
468 # ifdef HAVE__CHSIZE_S
469 # define nasm_ftruncate(fd,size) _chsize_s(fd,size)
470 # elif defined(HAVE__CHSIZE)
471 # define nasm_ftruncate(fd,size) _chsize(fd,size)
472 # elif defined(HAVE_FTRUNCATE)
473 # define nasm_ftruncate(fd,size) ftruncate(fd,size)
474 # endif
475 #endif
477 void fwritezero(size_t bytes, FILE *fp)
479 size_t blksize;
481 #ifdef nasm_ftruncate
482 if (bytes >= BUFSIZ && !ferror(fp) && !feof(fp)) {
483 off_t pos = ftello(fp);
484 if (pos >= 0) {
485 if (!fflush(fp) &&
486 !nasm_ftruncate(fileno(fp), pos + bytes) &&
487 !fseeko(fp, pos+bytes, SEEK_SET))
488 return;
491 #endif
493 while (bytes) {
494 blksize = (bytes < ZERO_BUF_SIZE) ? bytes : ZERO_BUF_SIZE;
496 nasm_write(zero_buffer, blksize, fp);
497 bytes -= blksize;
501 void standard_extension(char *inname, char *outname, char *extension)
503 char *p, *q;
505 if (*outname) /* file name already exists, */
506 return; /* so do nothing */
507 q = inname;
508 p = outname;
509 while (*q)
510 *p++ = *q++; /* copy, and find end of string */
511 *p = '\0'; /* terminate it */
512 while (p > outname && *--p != '.') ; /* find final period (or whatever) */
513 if (*p != '.')
514 while (*p)
515 p++; /* go back to end if none found */
516 if (!strcmp(p, extension)) { /* is the extension already there? */
517 if (*extension)
518 nasm_error(ERR_WARNING | ERR_NOFILE,
519 "file name already ends in `%s': "
520 "output will be in `nasm.out'", extension);
521 else
522 nasm_error(ERR_WARNING | ERR_NOFILE,
523 "file name already has no extension: "
524 "output will be in `nasm.out'");
525 strcpy(outname, "nasm.out");
526 } else
527 strcpy(p, extension);
531 * Common list of prefix names
533 static const char *prefix_names[] = {
534 "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
535 "rep", "repe", "repne", "repnz", "repz", "times", "wait",
536 "xacquire", "xrelease", "bnd"
539 const char *prefix_name(int token)
541 unsigned int prefix = token-PREFIX_ENUM_START;
542 if (prefix >= ARRAY_SIZE(prefix_names))
543 return NULL;
545 return prefix_names[prefix];
549 * Binary search.
551 int bsi(const char *string, const char **array, int size)
553 int i = -1, j = size; /* always, i < index < j */
554 while (j - i >= 2) {
555 int k = (i + j) / 2;
556 int l = strcmp(string, array[k]);
557 if (l < 0) /* it's in the first half */
558 j = k;
559 else if (l > 0) /* it's in the second half */
560 i = k;
561 else /* we've got it :) */
562 return k;
564 return -1; /* we haven't got it :( */
567 int bsii(const char *string, const char **array, int size)
569 int i = -1, j = size; /* always, i < index < j */
570 while (j - i >= 2) {
571 int k = (i + j) / 2;
572 int l = nasm_stricmp(string, array[k]);
573 if (l < 0) /* it's in the first half */
574 j = k;
575 else if (l > 0) /* it's in the second half */
576 i = k;
577 else /* we've got it :) */
578 return k;
580 return -1; /* we haven't got it :( */
583 static char *file_name = NULL;
584 static int32_t line_number = 0;
586 char *src_set_fname(char *newname)
588 char *oldname = file_name;
589 file_name = newname;
590 return oldname;
593 int32_t src_set_linnum(int32_t newline)
595 int32_t oldline = line_number;
596 line_number = newline;
597 return oldline;
600 int32_t src_get_linnum(void)
602 return line_number;
605 int src_get(int32_t *xline, char **xname)
607 if (!file_name || !*xname || strcmp(*xname, file_name)) {
608 nasm_free(*xname);
609 *xname = file_name ? nasm_strdup(file_name) : NULL;
610 *xline = line_number;
611 return -2;
613 if (*xline != line_number) {
614 int32_t tmp = line_number - *xline;
615 *xline = line_number;
616 return tmp;
618 return 0;
621 char *nasm_strcat(const char *one, const char *two)
623 char *rslt;
624 int l1 = strlen(one);
625 rslt = nasm_malloc(l1 + strlen(two) + 1);
626 strcpy(rslt, one);
627 strcpy(rslt + l1, two);
628 return rslt;
631 /* skip leading spaces */
632 char *nasm_skip_spaces(const char *p)
634 if (p)
635 while (*p && nasm_isspace(*p))
636 p++;
637 return (char *)p;
640 /* skip leading non-spaces */
641 char *nasm_skip_word(const char *p)
643 if (p)
644 while (*p && !nasm_isspace(*p))
645 p++;
646 return (char *)p;
649 /* zap leading spaces with zero */
650 char *nasm_zap_spaces_fwd(char *p)
652 if (p)
653 while (*p && nasm_isspace(*p))
654 *p++ = 0x0;
655 return p;
658 /* zap spaces with zero in reverse order */
659 char *nasm_zap_spaces_rev(char *p)
661 if (p)
662 while (*p && nasm_isspace(*p))
663 *p-- = 0x0;
664 return p;
667 /* zap leading and trailing spaces */
668 char *nasm_trim_spaces(char *p)
670 p = nasm_zap_spaces_fwd(p);
671 nasm_zap_spaces_fwd(nasm_skip_word(p));
673 return p;
677 * return the word extracted from a stream
678 * or NULL if nothing left
680 char *nasm_get_word(char *p, char **tail)
682 char *word = nasm_skip_spaces(p);
683 char *next = nasm_skip_word(word);
685 if (word && *word) {
686 if (*next)
687 *next++ = '\0';
688 } else
689 word = next = NULL;
691 /* NOTE: the tail may start with spaces */
692 *tail = next;
694 return word;
698 * Extract "opt=val" values from the stream and
699 * returns "opt"
701 * Exceptions:
702 * 1) If "=val" passed the NULL returned though
703 * you may continue handling the tail via "next"
704 * 2) If "=" passed the NULL is returned and "val"
705 * is set to NULL as well
707 char *nasm_opt_val(char *p, char **val, char **next)
709 char *q, *nxt;
711 *val = *next = NULL;
713 p = nasm_get_word(p, &nxt);
714 if (!p)
715 return NULL;
717 q = strchr(p, '=');
718 if (q) {
719 if (q == p)
720 p = NULL;
721 *q++='\0';
722 if (*q) {
723 *val = q;
724 } else {
725 q = nasm_get_word(q + 1, &nxt);
726 if (q)
727 *val = q;
729 } else {
730 q = nasm_skip_spaces(nxt);
731 if (q && *q == '=') {
732 q = nasm_get_word(q + 1, &nxt);
733 if (q)
734 *val = q;
738 *next = nxt;
739 return p;
743 * initialized data bytes length from opcode
745 int idata_bytes(int opcode)
747 switch (opcode) {
748 case I_DB:
749 return 1;
750 case I_DW:
751 return 2;
752 case I_DD:
753 return 4;
754 case I_DQ:
755 return 8;
756 case I_DT:
757 return 10;
758 case I_DO:
759 return 16;
760 case I_DY:
761 return 32;
762 case I_DZ:
763 return 64;
764 case I_none:
765 return -1;
766 default:
767 return 0;