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 static 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_set_verror(vefunc ve
)
77 void nasm_error(int severity
, const char *fmt
, ...)
82 nasm_verror(severity
, fmt
, ap
);
86 no_return
nasm_panic(int flags
, const char *fmt
, ...)
91 nasm_verror(flags
| ERR_PANIC
, fmt
, ap
);
92 abort(); /* We should never get here */
95 no_return
nasm_panic_from_macro(const char *file
, int line
)
97 nasm_panic(ERR_NOFILE
, "Internal error at %s:%d\n", file
, line
);
100 void *nasm_malloc(size_t size
)
102 void *p
= malloc(size
);
104 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
108 void *nasm_zalloc(size_t size
)
110 void *p
= calloc(size
, 1);
112 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
116 void *nasm_realloc(void *q
, size_t size
)
118 void *p
= q
? realloc(q
, size
) : malloc(size
);
120 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
124 void nasm_free(void *q
)
130 char *nasm_strdup(const char *s
)
133 int size
= strlen(s
) + 1;
137 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
142 char *nasm_strndup(const char *s
, size_t len
)
149 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
155 no_return
nasm_assert_failed(const char *file
, int line
, const char *msg
)
157 nasm_error(ERR_FATAL
, "assertion %s failed at %s:%d", msg
, file
, line
);
161 void nasm_write(const void *ptr
, size_t size
, FILE *f
)
163 size_t n
= fwrite(ptr
, 1, size
, f
);
165 nasm_error(ERR_FATAL
, "unable to write output: %s", strerror(errno
));
169 int nasm_stricmp(const char *s1
, const char *s2
)
171 unsigned char c1
, c2
;
175 c1
= nasm_tolower(*s1
++);
176 c2
= nasm_tolower(*s2
++);
188 #ifndef nasm_strnicmp
189 int nasm_strnicmp(const char *s1
, const char *s2
, size_t n
)
191 unsigned char c1
, c2
;
195 c1
= nasm_tolower(*s1
++);
196 c2
= nasm_tolower(*s2
++);
208 int nasm_memicmp(const char *s1
, const char *s2
, size_t n
)
210 unsigned char c1
, c2
;
214 c1
= nasm_tolower(*s1
++);
215 c2
= nasm_tolower(*s2
++);
224 char *nasm_strsep(char **stringp
, const char *delim
)
232 e
= strpbrk(s
, delim
);
242 #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
244 static int radix_letter(char c
)
249 return 2; /* Binary */
252 return 8; /* Octal */
255 return 16; /* Hexadecimal */
258 return 10; /* Decimal */
260 return 0; /* Not a known radix letter */
264 int64_t readnum(char *str
, bool *error
)
267 int32_t pradix
, sradix
, radix
;
269 uint64_t result
, checklimit
;
276 while (nasm_isspace(*r
))
277 r
++; /* find start of number */
280 * If the number came from make_tok_num (as a result of an %assign), it
281 * might have a '-' built into it (rather than in a preceeding token).
290 while (lib_isnumchar(*q
))
291 q
++; /* find end of number */
301 * Handle radix formats:
303 * 0<radix-letter><string>
304 * $<string> (hexadecimal)
305 * <string><radix-letter>
310 if (len
> 2 && *r
== '0' && (pradix
= radix_letter(r
[1])) != 0)
312 else if (len
> 1 && *r
== '$')
313 pradix
= 16, plen
= 1;
315 if (len
> 1 && (sradix
= radix_letter(q
[-1])) != 0)
318 if (pradix
> sradix
) {
321 } else if (sradix
> pradix
) {
325 /* Either decimal, or invalid -- if invalid, we'll trip up
331 * `checklimit' must be 2**64 / radix. We can't do that in
332 * 64-bit arithmetic, which we're (probably) using, so we
333 * cheat: since we know that all radices we use are even, we
334 * can divide 2**63 by radix/2 instead.
336 checklimit
= UINT64_C(0x8000000000000000) / (radix
>> 1);
339 * Calculate the highest allowable value for the last digit of a
340 * 64-bit constant... in radix 10, it is 6, otherwise it is 0
342 last
= (radix
== 10 ? 6 : 0);
345 while (*r
&& r
< q
) {
347 if (*r
< '0' || (*r
> '9' && *r
< 'A')
348 || (digit
= numvalue(*r
)) >= radix
) {
352 if (result
> checklimit
||
353 (result
== checklimit
&& digit
>= last
)) {
357 result
= radix
* result
+ digit
;
363 nasm_error(ERR_WARNING
| ERR_PASS1
| ERR_WARN_NOV
,
364 "numeric constant %s does not fit in 64 bits",
367 return result
* sign
;
370 int64_t readstrnum(char *str
, int length
, bool *warn
)
372 int64_t charconst
= 0;
378 if (globalbits
== 64) {
379 for (i
= 0; i
< length
; i
++) {
380 if (charconst
& UINT64_C(0xFF00000000000000))
382 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
385 for (i
= 0; i
< length
; i
++) {
386 if (charconst
& 0xFF000000UL
)
388 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
394 int32_t seg_alloc(void)
396 static int32_t next_seg
= 0;
397 int32_t this_seg
= next_seg
;
404 #ifdef WORDS_LITTLEENDIAN
406 void fwriteint16_t(uint16_t data
, FILE * fp
)
408 nasm_write(&data
, 2, fp
);
411 void fwriteint32_t(uint32_t data
, FILE * fp
)
413 nasm_write(&data
, 4, fp
);
416 void fwriteint64_t(uint64_t data
, FILE * fp
)
418 nasm_write(&data
, 8, fp
);
421 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
423 nasm_write(&data
, size
, fp
);
426 #else /* not WORDS_LITTLEENDIAN */
428 void fwriteint16_t(uint16_t data
, FILE * fp
)
430 char buffer
[2], *p
= buffer
;
432 nasm_write(buffer
, 2, fp
);
435 void fwriteint32_t(uint32_t data
, FILE * fp
)
437 char buffer
[4], *p
= buffer
;
439 nasm_write(buffer
, 4, fp
);
442 void fwriteint64_t(uint64_t data
, FILE * fp
)
444 char buffer
[8], *p
= buffer
;
446 nasm_write(buffer
, 8, fp
);
449 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
451 char buffer
[8], *p
= buffer
;
452 WRITEADDR(p
, data
, size
);
453 nasm_write(buffer
, size
, fp
);
458 void fwritezero(size_t bytes
, FILE *fp
)
463 blksize
= (bytes
< ZERO_BUF_SIZE
) ? bytes
: ZERO_BUF_SIZE
;
465 nasm_write(zero_buffer
, blksize
, fp
);
470 void standard_extension(char *inname
, char *outname
, char *extension
)
474 if (*outname
) /* file name already exists, */
475 return; /* so do nothing */
479 *p
++ = *q
++; /* copy, and find end of string */
480 *p
= '\0'; /* terminate it */
481 while (p
> outname
&& *--p
!= '.') ; /* find final period (or whatever) */
484 p
++; /* go back to end if none found */
485 if (!strcmp(p
, extension
)) { /* is the extension already there? */
487 nasm_error(ERR_WARNING
| ERR_NOFILE
,
488 "file name already ends in `%s': "
489 "output will be in `nasm.out'", extension
);
491 nasm_error(ERR_WARNING
| ERR_NOFILE
,
492 "file name already has no extension: "
493 "output will be in `nasm.out'");
494 strcpy(outname
, "nasm.out");
496 strcpy(p
, extension
);
500 * Common list of prefix names
502 static const char *prefix_names
[] = {
503 "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
504 "rep", "repe", "repne", "repnz", "repz", "times", "wait",
505 "xacquire", "xrelease", "bnd"
508 const char *prefix_name(int token
)
510 unsigned int prefix
= token
-PREFIX_ENUM_START
;
511 if (prefix
>= ARRAY_SIZE(prefix_names
))
514 return prefix_names
[prefix
];
520 int bsi(const char *string
, const char **array
, int size
)
522 int i
= -1, j
= size
; /* always, i < index < j */
525 int l
= strcmp(string
, array
[k
]);
526 if (l
< 0) /* it's in the first half */
528 else if (l
> 0) /* it's in the second half */
530 else /* we've got it :) */
533 return -1; /* we haven't got it :( */
536 int bsii(const char *string
, const char **array
, int size
)
538 int i
= -1, j
= size
; /* always, i < index < j */
541 int l
= nasm_stricmp(string
, array
[k
]);
542 if (l
< 0) /* it's in the first half */
544 else if (l
> 0) /* it's in the second half */
546 else /* we've got it :) */
549 return -1; /* we haven't got it :( */
552 static char *file_name
= NULL
;
553 static int32_t line_number
= 0;
555 char *src_set_fname(char *newname
)
557 char *oldname
= file_name
;
562 int32_t src_set_linnum(int32_t newline
)
564 int32_t oldline
= line_number
;
565 line_number
= newline
;
569 int32_t src_get_linnum(void)
574 int src_get(int32_t *xline
, char **xname
)
576 if (!file_name
|| !*xname
|| strcmp(*xname
, file_name
)) {
578 *xname
= file_name
? nasm_strdup(file_name
) : NULL
;
579 *xline
= line_number
;
582 if (*xline
!= line_number
) {
583 int32_t tmp
= line_number
- *xline
;
584 *xline
= line_number
;
590 char *nasm_strcat(const char *one
, const char *two
)
593 int l1
= strlen(one
);
594 rslt
= nasm_malloc(l1
+ strlen(two
) + 1);
596 strcpy(rslt
+ l1
, two
);
600 /* skip leading spaces */
601 char *nasm_skip_spaces(const char *p
)
604 while (*p
&& nasm_isspace(*p
))
609 /* skip leading non-spaces */
610 char *nasm_skip_word(const char *p
)
613 while (*p
&& !nasm_isspace(*p
))
618 /* zap leading spaces with zero */
619 char *nasm_zap_spaces_fwd(char *p
)
622 while (*p
&& nasm_isspace(*p
))
627 /* zap spaces with zero in reverse order */
628 char *nasm_zap_spaces_rev(char *p
)
631 while (*p
&& nasm_isspace(*p
))
636 /* zap leading and trailing spaces */
637 char *nasm_trim_spaces(char *p
)
639 p
= nasm_zap_spaces_fwd(p
);
640 nasm_zap_spaces_fwd(nasm_skip_word(p
));
646 * return the word extracted from a stream
647 * or NULL if nothing left
649 char *nasm_get_word(char *p
, char **tail
)
651 char *word
= nasm_skip_spaces(p
);
652 char *next
= nasm_skip_word(word
);
660 /* NOTE: the tail may start with spaces */
667 * Extract "opt=val" values from the stream and
671 * 1) If "=val" passed the NULL returned though
672 * you may continue handling the tail via "next"
673 * 2) If "=" passed the NULL is returned and "val"
674 * is set to NULL as well
676 char *nasm_opt_val(char *p
, char **val
, char **next
)
682 p
= nasm_get_word(p
, &nxt
);
694 q
= nasm_get_word(q
+ 1, &nxt
);
699 q
= nasm_skip_spaces(nxt
);
700 if (q
&& *q
== '=') {
701 q
= nasm_get_word(q
+ 1, &nxt
);
712 * initialized data bytes length from opcode
714 int idata_bytes(int opcode
)