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_fatal(0, "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
);
169 nasm_fatal(0, "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
);
462 void fwritezero(size_t bytes
, FILE *fp
)
467 blksize
= (bytes
< ZERO_BUF_SIZE
) ? bytes
: ZERO_BUF_SIZE
;
469 nasm_write(zero_buffer
, blksize
, fp
);
474 void standard_extension(char *inname
, char *outname
, char *extension
)
478 if (*outname
) /* file name already exists, */
479 return; /* so do nothing */
483 *p
++ = *q
++; /* copy, and find end of string */
484 *p
= '\0'; /* terminate it */
485 while (p
> outname
&& *--p
!= '.') ; /* find final period (or whatever) */
488 p
++; /* go back to end if none found */
489 if (!strcmp(p
, extension
)) { /* is the extension already there? */
491 nasm_error(ERR_WARNING
| ERR_NOFILE
,
492 "file name already ends in `%s': "
493 "output will be in `nasm.out'", extension
);
495 nasm_error(ERR_WARNING
| ERR_NOFILE
,
496 "file name already has no extension: "
497 "output will be in `nasm.out'");
498 strcpy(outname
, "nasm.out");
500 strcpy(p
, extension
);
504 * Common list of prefix names
506 static const char *prefix_names
[] = {
507 "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
508 "rep", "repe", "repne", "repnz", "repz", "times", "wait",
509 "xacquire", "xrelease", "bnd"
512 const char *prefix_name(int token
)
514 unsigned int prefix
= token
-PREFIX_ENUM_START
;
515 if (prefix
>= ARRAY_SIZE(prefix_names
))
518 return prefix_names
[prefix
];
524 int bsi(const char *string
, const char **array
, int size
)
526 int i
= -1, j
= size
; /* always, i < index < j */
529 int l
= strcmp(string
, array
[k
]);
530 if (l
< 0) /* it's in the first half */
532 else if (l
> 0) /* it's in the second half */
534 else /* we've got it :) */
537 return -1; /* we haven't got it :( */
540 int bsii(const char *string
, const char **array
, int size
)
542 int i
= -1, j
= size
; /* always, i < index < j */
545 int l
= nasm_stricmp(string
, array
[k
]);
546 if (l
< 0) /* it's in the first half */
548 else if (l
> 0) /* it's in the second half */
550 else /* we've got it :) */
553 return -1; /* we haven't got it :( */
556 char *nasm_strcat(const char *one
, const char *two
)
559 int l1
= strlen(one
);
560 rslt
= nasm_malloc(l1
+ strlen(two
) + 1);
562 strcpy(rslt
+ l1
, two
);
566 /* skip leading spaces */
567 char *nasm_skip_spaces(const char *p
)
570 while (*p
&& nasm_isspace(*p
))
575 /* skip leading non-spaces */
576 char *nasm_skip_word(const char *p
)
579 while (*p
&& !nasm_isspace(*p
))
584 /* zap leading spaces with zero */
585 char *nasm_zap_spaces_fwd(char *p
)
588 while (*p
&& nasm_isspace(*p
))
593 /* zap spaces with zero in reverse order */
594 char *nasm_zap_spaces_rev(char *p
)
597 while (*p
&& nasm_isspace(*p
))
602 /* zap leading and trailing spaces */
603 char *nasm_trim_spaces(char *p
)
605 p
= nasm_zap_spaces_fwd(p
);
606 nasm_zap_spaces_fwd(nasm_skip_word(p
));
612 * return the word extracted from a stream
613 * or NULL if nothing left
615 char *nasm_get_word(char *p
, char **tail
)
617 char *word
= nasm_skip_spaces(p
);
618 char *next
= nasm_skip_word(word
);
626 /* NOTE: the tail may start with spaces */
633 * Extract "opt=val" values from the stream and
637 * 1) If "=val" passed the NULL returned though
638 * you may continue handling the tail via "next"
639 * 2) If "=" passed the NULL is returned and "val"
640 * is set to NULL as well
642 char *nasm_opt_val(char *p
, char **val
, char **next
)
648 p
= nasm_get_word(p
, &nxt
);
660 q
= nasm_get_word(q
+ 1, &nxt
);
665 q
= nasm_skip_spaces(nxt
);
666 if (q
&& *q
== '=') {
667 q
= nasm_get_word(q
+ 1, &nxt
);
678 * initialized data bytes length from opcode
680 int idata_bytes(int opcode
)