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_zalloc(size_t size
)
113 void *p
= calloc(size
, 1);
115 nasm_fatal(ERR_NOFILE
, "out of memory");
119 void *nasm_realloc(void *q
, size_t size
)
121 void *p
= q
? realloc(q
, size
) : malloc(size
);
123 nasm_fatal(ERR_NOFILE
, "out of memory");
127 void nasm_free(void *q
)
133 char *nasm_strdup(const char *s
)
136 int size
= strlen(s
) + 1;
140 nasm_fatal(ERR_NOFILE
, "out of memory");
145 char *nasm_strndup(const char *s
, size_t len
)
152 nasm_fatal(ERR_NOFILE
, "out of memory");
158 no_return
nasm_assert_failed(const char *file
, int line
, const char *msg
)
160 nasm_fatal(0, "assertion %s failed at %s:%d", msg
, file
, line
);
165 int nasm_stricmp(const char *s1
, const char *s2
)
167 unsigned char c1
, c2
;
171 c1
= nasm_tolower(*s1
++);
172 c2
= nasm_tolower(*s2
++);
184 #ifndef nasm_strnicmp
185 int nasm_strnicmp(const char *s1
, const char *s2
, size_t n
)
187 unsigned char c1
, c2
;
191 c1
= nasm_tolower(*s1
++);
192 c2
= nasm_tolower(*s2
++);
204 int nasm_memicmp(const char *s1
, const char *s2
, size_t n
)
206 unsigned char c1
, c2
;
210 c1
= nasm_tolower(*s1
++);
211 c2
= nasm_tolower(*s2
++);
220 char *nasm_strsep(char **stringp
, const char *delim
)
228 e
= strpbrk(s
, delim
);
238 #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
240 static int radix_letter(char c
)
245 return 2; /* Binary */
248 return 8; /* Octal */
251 return 16; /* Hexadecimal */
254 return 10; /* Decimal */
256 return 0; /* Not a known radix letter */
260 int64_t readnum(char *str
, bool *error
)
263 int32_t pradix
, sradix
, radix
;
265 uint64_t result
, checklimit
;
272 while (nasm_isspace(*r
))
273 r
++; /* find start of number */
276 * If the number came from make_tok_num (as a result of an %assign), it
277 * might have a '-' built into it (rather than in a preceeding token).
286 while (lib_isnumchar(*q
))
287 q
++; /* find end of number */
297 * Handle radix formats:
299 * 0<radix-letter><string>
300 * $<string> (hexadecimal)
301 * <string><radix-letter>
306 if (len
> 2 && *r
== '0' && (pradix
= radix_letter(r
[1])) != 0)
308 else if (len
> 1 && *r
== '$')
309 pradix
= 16, plen
= 1;
311 if (len
> 1 && (sradix
= radix_letter(q
[-1])) != 0)
314 if (pradix
> sradix
) {
317 } else if (sradix
> pradix
) {
321 /* Either decimal, or invalid -- if invalid, we'll trip up
327 * `checklimit' must be 2**64 / radix. We can't do that in
328 * 64-bit arithmetic, which we're (probably) using, so we
329 * cheat: since we know that all radices we use are even, we
330 * can divide 2**63 by radix/2 instead.
332 checklimit
= UINT64_C(0x8000000000000000) / (radix
>> 1);
335 * Calculate the highest allowable value for the last digit of a
336 * 64-bit constant... in radix 10, it is 6, otherwise it is 0
338 last
= (radix
== 10 ? 6 : 0);
341 while (*r
&& r
< q
) {
343 if (*r
< '0' || (*r
> '9' && *r
< 'A')
344 || (digit
= numvalue(*r
)) >= radix
) {
348 if (result
> checklimit
||
349 (result
== checklimit
&& digit
>= last
)) {
353 result
= radix
* result
+ digit
;
359 nasm_error(ERR_WARNING
| ERR_PASS1
| ERR_WARN_NOV
,
360 "numeric constant %s does not fit in 64 bits",
363 return result
* sign
;
366 int64_t readstrnum(char *str
, int length
, bool *warn
)
368 int64_t charconst
= 0;
374 if (globalbits
== 64) {
375 for (i
= 0; i
< length
; i
++) {
376 if (charconst
& UINT64_C(0xFF00000000000000))
378 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
381 for (i
= 0; i
< length
; i
++) {
382 if (charconst
& 0xFF000000UL
)
384 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
390 int32_t seg_alloc(void)
392 static int32_t next_seg
= 0;
393 int32_t this_seg
= next_seg
;
400 void standard_extension(char *inname
, char *outname
, char *extension
)
404 if (*outname
) /* file name already exists, */
405 return; /* so do nothing */
409 *p
++ = *q
++; /* copy, and find end of string */
410 *p
= '\0'; /* terminate it */
411 while (p
> outname
&& *--p
!= '.') ; /* find final period (or whatever) */
414 p
++; /* go back to end if none found */
415 if (!strcmp(p
, extension
)) { /* is the extension already there? */
417 nasm_error(ERR_WARNING
| ERR_NOFILE
,
418 "file name already ends in `%s': "
419 "output will be in `nasm.out'", extension
);
421 nasm_error(ERR_WARNING
| ERR_NOFILE
,
422 "file name already has no extension: "
423 "output will be in `nasm.out'");
424 strcpy(outname
, "nasm.out");
426 strcpy(p
, extension
);
430 * Common list of prefix names
432 static const char *prefix_names
[] = {
433 "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
434 "rep", "repe", "repne", "repnz", "repz", "times", "wait",
435 "xacquire", "xrelease", "bnd"
438 const char *prefix_name(int token
)
440 unsigned int prefix
= token
-PREFIX_ENUM_START
;
441 if (prefix
>= ARRAY_SIZE(prefix_names
))
444 return prefix_names
[prefix
];
450 int bsi(const char *string
, const char **array
, int size
)
452 int i
= -1, j
= size
; /* always, i < index < j */
455 int l
= strcmp(string
, array
[k
]);
456 if (l
< 0) /* it's in the first half */
458 else if (l
> 0) /* it's in the second half */
460 else /* we've got it :) */
463 return -1; /* we haven't got it :( */
466 int bsii(const char *string
, const char **array
, int size
)
468 int i
= -1, j
= size
; /* always, i < index < j */
471 int l
= nasm_stricmp(string
, array
[k
]);
472 if (l
< 0) /* it's in the first half */
474 else if (l
> 0) /* it's in the second half */
476 else /* we've got it :) */
479 return -1; /* we haven't got it :( */
482 static char *file_name
= NULL
;
483 static int32_t line_number
= 0;
485 char *src_set_fname(char *newname
)
487 char *oldname
= file_name
;
492 int32_t src_set_linnum(int32_t newline
)
494 int32_t oldline
= line_number
;
495 line_number
= newline
;
499 int32_t src_get_linnum(void)
504 int src_get(int32_t *xline
, char **xname
)
506 if (!file_name
|| !*xname
|| strcmp(*xname
, file_name
)) {
508 *xname
= file_name
? nasm_strdup(file_name
) : NULL
;
509 *xline
= line_number
;
512 if (*xline
!= line_number
) {
513 int32_t tmp
= line_number
- *xline
;
514 *xline
= line_number
;
520 char *nasm_strcat(const char *one
, const char *two
)
523 int l1
= strlen(one
);
524 rslt
= nasm_malloc(l1
+ strlen(two
) + 1);
526 strcpy(rslt
+ l1
, two
);
530 /* skip leading spaces */
531 char *nasm_skip_spaces(const char *p
)
534 while (*p
&& nasm_isspace(*p
))
539 /* skip leading non-spaces */
540 char *nasm_skip_word(const char *p
)
543 while (*p
&& !nasm_isspace(*p
))
548 /* zap leading spaces with zero */
549 char *nasm_zap_spaces_fwd(char *p
)
552 while (*p
&& nasm_isspace(*p
))
557 /* zap spaces with zero in reverse order */
558 char *nasm_zap_spaces_rev(char *p
)
561 while (*p
&& nasm_isspace(*p
))
566 /* zap leading and trailing spaces */
567 char *nasm_trim_spaces(char *p
)
569 p
= nasm_zap_spaces_fwd(p
);
570 nasm_zap_spaces_fwd(nasm_skip_word(p
));
576 * return the word extracted from a stream
577 * or NULL if nothing left
579 char *nasm_get_word(char *p
, char **tail
)
581 char *word
= nasm_skip_spaces(p
);
582 char *next
= nasm_skip_word(word
);
590 /* NOTE: the tail may start with spaces */
597 * Extract "opt=val" values from the stream and
601 * 1) If "=val" passed the NULL returned though
602 * you may continue handling the tail via "next"
603 * 2) If "=" passed the NULL is returned and "val"
604 * is set to NULL as well
606 char *nasm_opt_val(char *p
, char **val
, char **next
)
612 p
= nasm_get_word(p
, &nxt
);
624 q
= nasm_get_word(q
+ 1, &nxt
);
629 q
= nasm_skip_spaces(nxt
);
630 if (q
&& *q
== '=') {
631 q
= nasm_get_word(q
+ 1, &nxt
);
642 * initialized data bytes length from opcode
644 int idata_bytes(int opcode
)