1 /* ----------------------------------------------------------------------- *
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
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
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
62 unsigned char nasm_tolower_tab
[256];
64 void tolower_init(void)
68 for (i
= 0; i
< 256; i
++)
69 nasm_tolower_tab
[i
] = tolower(i
);
72 void nasm_error(int severity
, const char *fmt
, ...)
77 nasm_verror(severity
, fmt
, ap
);
81 no_return
nasm_fatal(int flags
, const char *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
, ...)
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
);
108 nasm_fatal(ERR_NOFILE
, "out of memory");
112 void *nasm_zalloc(size_t size
)
114 void *p
= calloc(size
, 1);
116 nasm_fatal(ERR_NOFILE
, "out of memory");
120 void *nasm_realloc(void *q
, size_t size
)
122 void *p
= q
? realloc(q
, size
) : malloc(size
);
124 nasm_fatal(ERR_NOFILE
, "out of memory");
128 void nasm_free(void *q
)
134 char *nasm_strdup(const char *s
)
137 int size
= strlen(s
) + 1;
141 nasm_fatal(ERR_NOFILE
, "out of memory");
146 char *nasm_strndup(const char *s
, size_t len
)
153 nasm_fatal(ERR_NOFILE
, "out of memory");
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
);
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
));
173 int nasm_stricmp(const char *s1
, const char *s2
)
175 unsigned char c1
, c2
;
179 c1
= nasm_tolower(*s1
++);
180 c2
= nasm_tolower(*s2
++);
192 #ifndef nasm_strnicmp
193 int nasm_strnicmp(const char *s1
, const char *s2
, size_t n
)
195 unsigned char c1
, c2
;
199 c1
= nasm_tolower(*s1
++);
200 c2
= nasm_tolower(*s2
++);
212 int nasm_memicmp(const char *s1
, const char *s2
, size_t n
)
214 unsigned char c1
, c2
;
218 c1
= nasm_tolower(*s1
++);
219 c2
= nasm_tolower(*s2
++);
228 char *nasm_strsep(char **stringp
, const char *delim
)
236 e
= strpbrk(s
, delim
);
246 #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
248 static int radix_letter(char c
)
253 return 2; /* Binary */
256 return 8; /* Octal */
259 return 16; /* Hexadecimal */
262 return 10; /* Decimal */
264 return 0; /* Not a known radix letter */
268 int64_t readnum(char *str
, bool *error
)
271 int32_t pradix
, sradix
, radix
;
273 uint64_t result
, checklimit
;
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).
294 while (lib_isnumchar(*q
))
295 q
++; /* find end of number */
305 * Handle radix formats:
307 * 0<radix-letter><string>
308 * $<string> (hexadecimal)
309 * <string><radix-letter>
314 if (len
> 2 && *r
== '0' && (pradix
= radix_letter(r
[1])) != 0)
316 else if (len
> 1 && *r
== '$')
317 pradix
= 16, plen
= 1;
319 if (len
> 1 && (sradix
= radix_letter(q
[-1])) != 0)
322 if (pradix
> sradix
) {
325 } else if (sradix
> pradix
) {
329 /* Either decimal, or invalid -- if invalid, we'll trip up
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);
349 while (*r
&& r
< q
) {
351 if (*r
< '0' || (*r
> '9' && *r
< 'A')
352 || (digit
= numvalue(*r
)) >= radix
) {
356 if (result
> checklimit
||
357 (result
== checklimit
&& digit
>= last
)) {
361 result
= radix
* result
+ digit
;
367 nasm_error(ERR_WARNING
| ERR_PASS1
| ERR_WARN_NOV
,
368 "numeric constant %s does not fit in 64 bits",
371 return result
* sign
;
374 int64_t readstrnum(char *str
, int length
, bool *warn
)
376 int64_t charconst
= 0;
382 if (globalbits
== 64) {
383 for (i
= 0; i
< length
; i
++) {
384 if (charconst
& UINT64_C(0xFF00000000000000))
386 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
389 for (i
= 0; i
< length
; i
++) {
390 if (charconst
& 0xFF000000UL
)
392 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
398 int32_t seg_alloc(void)
400 static int32_t next_seg
= 0;
401 int32_t this_seg
= next_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
;
436 nasm_write(buffer
, 2, fp
);
439 void fwriteint32_t(uint32_t data
, FILE * fp
)
441 char buffer
[4], *p
= buffer
;
443 nasm_write(buffer
, 4, fp
);
446 void fwriteint64_t(uint64_t data
, FILE * fp
)
448 char buffer
[8], *p
= buffer
;
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
);
463 # define fseeko fseek
464 # define ftello ftell
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)
477 void fwritezero(size_t bytes
, FILE *fp
)
481 #ifdef nasm_ftruncate
482 if (bytes
>= BUFSIZ
&& !ferror(fp
) && !feof(fp
)) {
483 off_t pos
= ftello(fp
);
486 !nasm_ftruncate(fileno(fp
), pos
+ bytes
) &&
487 !fseeko(fp
, pos
+bytes
, SEEK_SET
))
494 blksize
= (bytes
< ZERO_BUF_SIZE
) ? bytes
: ZERO_BUF_SIZE
;
496 nasm_write(zero_buffer
, blksize
, fp
);
501 void standard_extension(char *inname
, char *outname
, char *extension
)
505 if (*outname
) /* file name already exists, */
506 return; /* so do nothing */
510 *p
++ = *q
++; /* copy, and find end of string */
511 *p
= '\0'; /* terminate it */
512 while (p
> outname
&& *--p
!= '.') ; /* find final period (or whatever) */
515 p
++; /* go back to end if none found */
516 if (!strcmp(p
, extension
)) { /* is the extension already there? */
518 nasm_error(ERR_WARNING
| ERR_NOFILE
,
519 "file name already ends in `%s': "
520 "output will be in `nasm.out'", extension
);
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");
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
))
545 return prefix_names
[prefix
];
551 int bsi(const char *string
, const char **array
, int size
)
553 int i
= -1, j
= size
; /* always, i < index < j */
556 int l
= strcmp(string
, array
[k
]);
557 if (l
< 0) /* it's in the first half */
559 else if (l
> 0) /* it's in the second half */
561 else /* we've got it :) */
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 */
572 int l
= nasm_stricmp(string
, array
[k
]);
573 if (l
< 0) /* it's in the first half */
575 else if (l
> 0) /* it's in the second half */
577 else /* we've got it :) */
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
;
593 int32_t src_set_linnum(int32_t newline
)
595 int32_t oldline
= line_number
;
596 line_number
= newline
;
600 int32_t src_get_linnum(void)
605 int src_get(int32_t *xline
, char **xname
)
607 if (!file_name
|| !*xname
|| strcmp(*xname
, file_name
)) {
609 *xname
= file_name
? nasm_strdup(file_name
) : NULL
;
610 *xline
= line_number
;
613 if (*xline
!= line_number
) {
614 int32_t tmp
= line_number
- *xline
;
615 *xline
= line_number
;
621 char *nasm_strcat(const char *one
, const char *two
)
624 int l1
= strlen(one
);
625 rslt
= nasm_malloc(l1
+ strlen(two
) + 1);
627 strcpy(rslt
+ l1
, two
);
631 /* skip leading spaces */
632 char *nasm_skip_spaces(const char *p
)
635 while (*p
&& nasm_isspace(*p
))
640 /* skip leading non-spaces */
641 char *nasm_skip_word(const char *p
)
644 while (*p
&& !nasm_isspace(*p
))
649 /* zap leading spaces with zero */
650 char *nasm_zap_spaces_fwd(char *p
)
653 while (*p
&& nasm_isspace(*p
))
658 /* zap spaces with zero in reverse order */
659 char *nasm_zap_spaces_rev(char *p
)
662 while (*p
&& nasm_isspace(*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
));
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
);
691 /* NOTE: the tail may start with spaces */
698 * Extract "opt=val" values from the stream and
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
)
713 p
= nasm_get_word(p
, &nxt
);
725 q
= nasm_get_word(q
+ 1, &nxt
);
730 q
= nasm_skip_spaces(nxt
);
731 if (q
&& *q
== '=') {
732 q
= nasm_get_word(q
+ 1, &nxt
);
743 * initialized data bytes length from opcode
745 int idata_bytes(int opcode
)