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
50 int globalbits
= 0; /* defined in nasm.h, works better here for ASM+DISASM */
51 vefunc nasm_verror
; /* Global error handling function */
53 /* Uninitialized -> all zero by C spec */
54 const uint8_t zero_buffer
[ZERO_BUF_SIZE
];
57 * Prepare a table of tolower() results. This avoids function calls
61 unsigned char nasm_tolower_tab
[256];
63 void tolower_init(void)
67 for (i
= 0; i
< 256; i
++)
68 nasm_tolower_tab
[i
] = tolower(i
);
71 void nasm_error(int severity
, const char *fmt
, ...)
76 nasm_verror(severity
, fmt
, ap
);
80 no_return
nasm_fatal(int flags
, const char *fmt
, ...)
85 nasm_verror(flags
| ERR_FATAL
, fmt
, ap
);
86 abort(); /* We should never get here */
89 no_return
nasm_panic(int flags
, const char *fmt
, ...)
94 nasm_verror(flags
| ERR_PANIC
, fmt
, ap
);
95 abort(); /* We should never get here */
98 no_return
nasm_panic_from_macro(const char *file
, int line
)
100 nasm_panic(ERR_NOFILE
, "Internal error at %s:%d\n", file
, line
);
103 void *nasm_malloc(size_t size
)
105 void *p
= malloc(size
);
107 nasm_fatal(ERR_NOFILE
, "out of memory");
111 void *nasm_calloc(size_t size
, size_t nelem
)
113 void *p
= calloc(size
, nelem
);
115 nasm_fatal(ERR_NOFILE
, "out of memory");
119 void *nasm_zalloc(size_t size
)
121 return nasm_calloc(size
, 1);
124 void *nasm_realloc(void *q
, size_t size
)
126 void *p
= q
? realloc(q
, size
) : malloc(size
);
128 nasm_fatal(ERR_NOFILE
, "out of memory");
132 void nasm_free(void *q
)
138 char *nasm_strdup(const char *s
)
141 int size
= strlen(s
) + 1;
145 nasm_fatal(ERR_NOFILE
, "out of memory");
150 char *nasm_strndup(const char *s
, size_t len
)
157 nasm_fatal(ERR_NOFILE
, "out of memory");
163 no_return
nasm_assert_failed(const char *file
, int line
, const char *msg
)
165 nasm_fatal(0, "assertion %s failed at %s:%d", msg
, file
, line
);
170 int nasm_stricmp(const char *s1
, const char *s2
)
172 unsigned char c1
, c2
;
176 c1
= nasm_tolower(*s1
++);
177 c2
= nasm_tolower(*s2
++);
189 #ifndef nasm_strnicmp
190 int nasm_strnicmp(const char *s1
, const char *s2
, size_t n
)
192 unsigned char c1
, c2
;
196 c1
= nasm_tolower(*s1
++);
197 c2
= nasm_tolower(*s2
++);
209 int nasm_memicmp(const char *s1
, const char *s2
, size_t n
)
211 unsigned char c1
, c2
;
215 c1
= nasm_tolower(*s1
++);
216 c2
= nasm_tolower(*s2
++);
225 char *nasm_strsep(char **stringp
, const char *delim
)
233 e
= strpbrk(s
, delim
);
243 #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
245 static int radix_letter(char c
)
250 return 2; /* Binary */
253 return 8; /* Octal */
256 return 16; /* Hexadecimal */
259 return 10; /* Decimal */
261 return 0; /* Not a known radix letter */
265 int64_t readnum(char *str
, bool *error
)
268 int32_t pradix
, sradix
, radix
;
270 uint64_t result
, checklimit
;
277 while (nasm_isspace(*r
))
278 r
++; /* find start of number */
281 * If the number came from make_tok_num (as a result of an %assign), it
282 * might have a '-' built into it (rather than in a preceeding token).
291 while (lib_isnumchar(*q
))
292 q
++; /* find end of number */
302 * Handle radix formats:
304 * 0<radix-letter><string>
305 * $<string> (hexadecimal)
306 * <string><radix-letter>
311 if (len
> 2 && *r
== '0' && (pradix
= radix_letter(r
[1])) != 0)
313 else if (len
> 1 && *r
== '$')
314 pradix
= 16, plen
= 1;
316 if (len
> 1 && (sradix
= radix_letter(q
[-1])) != 0)
319 if (pradix
> sradix
) {
322 } else if (sradix
> pradix
) {
326 /* Either decimal, or invalid -- if invalid, we'll trip up
332 * `checklimit' must be 2**64 / radix. We can't do that in
333 * 64-bit arithmetic, which we're (probably) using, so we
334 * cheat: since we know that all radices we use are even, we
335 * can divide 2**63 by radix/2 instead.
337 checklimit
= UINT64_C(0x8000000000000000) / (radix
>> 1);
340 * Calculate the highest allowable value for the last digit of a
341 * 64-bit constant... in radix 10, it is 6, otherwise it is 0
343 last
= (radix
== 10 ? 6 : 0);
346 while (*r
&& r
< q
) {
348 if (*r
< '0' || (*r
> '9' && *r
< 'A')
349 || (digit
= numvalue(*r
)) >= radix
) {
353 if (result
> checklimit
||
354 (result
== checklimit
&& digit
>= last
)) {
358 result
= radix
* result
+ digit
;
364 nasm_error(ERR_WARNING
| ERR_PASS1
| ERR_WARN_NOV
,
365 "numeric constant %s does not fit in 64 bits",
368 return result
* sign
;
371 int64_t readstrnum(char *str
, int length
, bool *warn
)
373 int64_t charconst
= 0;
379 if (globalbits
== 64) {
380 for (i
= 0; i
< length
; i
++) {
381 if (charconst
& UINT64_C(0xFF00000000000000))
383 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
386 for (i
= 0; i
< length
; i
++) {
387 if (charconst
& 0xFF000000UL
)
389 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
395 int32_t seg_alloc(void)
397 static int32_t next_seg
= 0;
398 int32_t this_seg
= next_seg
;
405 void standard_extension(char *inname
, char *outname
, char *extension
)
409 if (*outname
) /* file name already exists, */
410 return; /* so do nothing */
414 *p
++ = *q
++; /* copy, and find end of string */
415 *p
= '\0'; /* terminate it */
416 while (p
> outname
&& *--p
!= '.') ; /* find final period (or whatever) */
419 p
++; /* go back to end if none found */
420 if (!strcmp(p
, extension
)) { /* is the extension already there? */
422 nasm_error(ERR_WARNING
| ERR_NOFILE
,
423 "file name already ends in `%s': "
424 "output will be in `nasm.out'", extension
);
426 nasm_error(ERR_WARNING
| ERR_NOFILE
,
427 "file name already has no extension: "
428 "output will be in `nasm.out'");
429 strcpy(outname
, "nasm.out");
431 strcpy(p
, extension
);
435 * Common list of prefix names
437 static const char *prefix_names
[] = {
438 "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
439 "rep", "repe", "repne", "repnz", "repz", "times", "wait",
440 "xacquire", "xrelease", "bnd"
443 const char *prefix_name(int token
)
445 unsigned int prefix
= token
-PREFIX_ENUM_START
;
446 if (prefix
>= ARRAY_SIZE(prefix_names
))
449 return prefix_names
[prefix
];
455 int bsi(const char *string
, const char **array
, int size
)
457 int i
= -1, j
= size
; /* always, i < index < j */
460 int l
= strcmp(string
, array
[k
]);
461 if (l
< 0) /* it's in the first half */
463 else if (l
> 0) /* it's in the second half */
465 else /* we've got it :) */
468 return -1; /* we haven't got it :( */
471 int bsii(const char *string
, const char **array
, int size
)
473 int i
= -1, j
= size
; /* always, i < index < j */
476 int l
= nasm_stricmp(string
, array
[k
]);
477 if (l
< 0) /* it's in the first half */
479 else if (l
> 0) /* it's in the second half */
481 else /* we've got it :) */
484 return -1; /* we haven't got it :( */
487 char *nasm_strcat(const char *one
, const char *two
)
490 int l1
= strlen(one
);
491 rslt
= nasm_malloc(l1
+ strlen(two
) + 1);
493 strcpy(rslt
+ l1
, two
);
497 /* skip leading spaces */
498 char *nasm_skip_spaces(const char *p
)
501 while (*p
&& nasm_isspace(*p
))
506 /* skip leading non-spaces */
507 char *nasm_skip_word(const char *p
)
510 while (*p
&& !nasm_isspace(*p
))
515 /* zap leading spaces with zero */
516 char *nasm_zap_spaces_fwd(char *p
)
519 while (*p
&& nasm_isspace(*p
))
524 /* zap spaces with zero in reverse order */
525 char *nasm_zap_spaces_rev(char *p
)
528 while (*p
&& nasm_isspace(*p
))
533 /* zap leading and trailing spaces */
534 char *nasm_trim_spaces(char *p
)
536 p
= nasm_zap_spaces_fwd(p
);
537 nasm_zap_spaces_fwd(nasm_skip_word(p
));
543 * return the word extracted from a stream
544 * or NULL if nothing left
546 char *nasm_get_word(char *p
, char **tail
)
548 char *word
= nasm_skip_spaces(p
);
549 char *next
= nasm_skip_word(word
);
557 /* NOTE: the tail may start with spaces */
564 * Extract "opt=val" values from the stream and
568 * 1) If "=val" passed the NULL returned though
569 * you may continue handling the tail via "next"
570 * 2) If "=" passed the NULL is returned and "val"
571 * is set to NULL as well
573 char *nasm_opt_val(char *p
, char **val
, char **next
)
579 p
= nasm_get_word(p
, &nxt
);
591 q
= nasm_get_word(q
+ 1, &nxt
);
596 q
= nasm_skip_spaces(nxt
);
597 if (q
&& *q
== '=') {
598 q
= nasm_get_word(q
+ 1, &nxt
);
609 * initialized data bytes length from opcode
611 int idata_bytes(int opcode
)