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 void *nasm_malloc(size_t size
)
97 void *p
= malloc(size
);
99 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
103 void *nasm_zalloc(size_t size
)
105 void *p
= calloc(size
, 1);
107 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
111 void *nasm_realloc(void *q
, size_t size
)
113 void *p
= q
? realloc(q
, size
) : malloc(size
);
115 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
119 void nasm_free(void *q
)
125 char *nasm_strdup(const char *s
)
128 int size
= strlen(s
) + 1;
132 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
137 char *nasm_strndup(const char *s
, size_t len
)
144 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
150 no_return
nasm_assert_failed(const char *file
, int line
, const char *msg
)
152 nasm_error(ERR_FATAL
, "assertion %s failed at %s:%d", msg
, file
, line
);
156 void nasm_write(const void *ptr
, size_t size
, FILE *f
)
158 size_t n
= fwrite(ptr
, 1, size
, f
);
160 nasm_error(ERR_FATAL
, "unable to write output: %s", strerror(errno
));
164 int nasm_stricmp(const char *s1
, const char *s2
)
166 unsigned char c1
, c2
;
170 c1
= nasm_tolower(*s1
++);
171 c2
= nasm_tolower(*s2
++);
183 #ifndef nasm_strnicmp
184 int nasm_strnicmp(const char *s1
, const char *s2
, size_t n
)
186 unsigned char c1
, c2
;
190 c1
= nasm_tolower(*s1
++);
191 c2
= nasm_tolower(*s2
++);
203 int nasm_memicmp(const char *s1
, const char *s2
, size_t n
)
205 unsigned char c1
, c2
;
209 c1
= nasm_tolower(*s1
++);
210 c2
= nasm_tolower(*s2
++);
219 char *nasm_strsep(char **stringp
, const char *delim
)
227 e
= strpbrk(s
, delim
);
237 #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
239 static int radix_letter(char c
)
244 return 2; /* Binary */
247 return 8; /* Octal */
250 return 16; /* Hexadecimal */
253 return 10; /* Decimal */
255 return 0; /* Not a known radix letter */
259 int64_t readnum(char *str
, bool *error
)
262 int32_t pradix
, sradix
, radix
;
264 uint64_t result
, checklimit
;
271 while (nasm_isspace(*r
))
272 r
++; /* find start of number */
275 * If the number came from make_tok_num (as a result of an %assign), it
276 * might have a '-' built into it (rather than in a preceeding token).
285 while (lib_isnumchar(*q
))
286 q
++; /* find end of number */
296 * Handle radix formats:
298 * 0<radix-letter><string>
299 * $<string> (hexadecimal)
300 * <string><radix-letter>
305 if (len
> 2 && *r
== '0' && (pradix
= radix_letter(r
[1])) != 0)
307 else if (len
> 1 && *r
== '$')
308 pradix
= 16, plen
= 1;
310 if (len
> 1 && (sradix
= radix_letter(q
[-1])) != 0)
313 if (pradix
> sradix
) {
316 } else if (sradix
> pradix
) {
320 /* Either decimal, or invalid -- if invalid, we'll trip up
326 * `checklimit' must be 2**64 / radix. We can't do that in
327 * 64-bit arithmetic, which we're (probably) using, so we
328 * cheat: since we know that all radices we use are even, we
329 * can divide 2**63 by radix/2 instead.
331 checklimit
= UINT64_C(0x8000000000000000) / (radix
>> 1);
334 * Calculate the highest allowable value for the last digit of a
335 * 64-bit constant... in radix 10, it is 6, otherwise it is 0
337 last
= (radix
== 10 ? 6 : 0);
340 while (*r
&& r
< q
) {
342 if (*r
< '0' || (*r
> '9' && *r
< 'A')
343 || (digit
= numvalue(*r
)) >= radix
) {
347 if (result
> checklimit
||
348 (result
== checklimit
&& digit
>= last
)) {
352 result
= radix
* result
+ digit
;
358 nasm_error(ERR_WARNING
| ERR_PASS1
| ERR_WARN_NOV
,
359 "numeric constant %s does not fit in 64 bits",
362 return result
* sign
;
365 int64_t readstrnum(char *str
, int length
, bool *warn
)
367 int64_t charconst
= 0;
373 if (globalbits
== 64) {
374 for (i
= 0; i
< length
; i
++) {
375 if (charconst
& UINT64_C(0xFF00000000000000))
377 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
380 for (i
= 0; i
< length
; i
++) {
381 if (charconst
& 0xFF000000UL
)
383 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
389 static int32_t next_seg
;
396 int32_t seg_alloc(void)
398 return (next_seg
+= 2) - 2;
401 #ifdef WORDS_LITTLEENDIAN
403 void fwriteint16_t(uint16_t data
, FILE * fp
)
405 nasm_write(&data
, 2, fp
);
408 void fwriteint32_t(uint32_t data
, FILE * fp
)
410 nasm_write(&data
, 4, fp
);
413 void fwriteint64_t(uint64_t data
, FILE * fp
)
415 nasm_write(&data
, 8, fp
);
418 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
420 nasm_write(&data
, size
, fp
);
423 #else /* not WORDS_LITTLEENDIAN */
425 void fwriteint16_t(uint16_t data
, FILE * fp
)
427 char buffer
[2], *p
= buffer
;
429 nasm_write(buffer
, 2, fp
);
432 void fwriteint32_t(uint32_t data
, FILE * fp
)
434 char buffer
[4], *p
= buffer
;
436 nasm_write(buffer
, 4, fp
);
439 void fwriteint64_t(uint64_t data
, FILE * fp
)
441 char buffer
[8], *p
= buffer
;
443 nasm_write(buffer
, 8, fp
);
446 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
448 char buffer
[8], *p
= buffer
;
449 WRITEADDR(p
, data
, size
);
450 nasm_write(buffer
, size
, fp
);
455 void fwritezero(size_t bytes
, FILE *fp
)
460 blksize
= (bytes
< ZERO_BUF_SIZE
) ? bytes
: ZERO_BUF_SIZE
;
462 nasm_write(zero_buffer
, blksize
, fp
);
467 void standard_extension(char *inname
, char *outname
, char *extension
)
471 if (*outname
) /* file name already exists, */
472 return; /* so do nothing */
476 *p
++ = *q
++; /* copy, and find end of string */
477 *p
= '\0'; /* terminate it */
478 while (p
> outname
&& *--p
!= '.') ; /* find final period (or whatever) */
481 p
++; /* go back to end if none found */
482 if (!strcmp(p
, extension
)) { /* is the extension already there? */
484 nasm_error(ERR_WARNING
| ERR_NOFILE
,
485 "file name already ends in `%s': "
486 "output will be in `nasm.out'", extension
);
488 nasm_error(ERR_WARNING
| ERR_NOFILE
,
489 "file name already has no extension: "
490 "output will be in `nasm.out'");
491 strcpy(outname
, "nasm.out");
493 strcpy(p
, extension
);
497 * Common list of prefix names
499 static const char *prefix_names
[] = {
500 "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
501 "rep", "repe", "repne", "repnz", "repz", "times", "wait",
502 "xacquire", "xrelease", "bnd"
505 const char *prefix_name(int token
)
507 unsigned int prefix
= token
-PREFIX_ENUM_START
;
508 if (prefix
>= ARRAY_SIZE(prefix_names
))
511 return prefix_names
[prefix
];
517 int bsi(const char *string
, const char **array
, int size
)
519 int i
= -1, j
= size
; /* always, i < index < j */
522 int l
= strcmp(string
, array
[k
]);
523 if (l
< 0) /* it's in the first half */
525 else if (l
> 0) /* it's in the second half */
527 else /* we've got it :) */
530 return -1; /* we haven't got it :( */
533 int bsii(const char *string
, const char **array
, int size
)
535 int i
= -1, j
= size
; /* always, i < index < j */
538 int l
= nasm_stricmp(string
, array
[k
]);
539 if (l
< 0) /* it's in the first half */
541 else if (l
> 0) /* it's in the second half */
543 else /* we've got it :) */
546 return -1; /* we haven't got it :( */
549 static char *file_name
= NULL
;
550 static int32_t line_number
= 0;
552 char *src_set_fname(char *newname
)
554 char *oldname
= file_name
;
559 int32_t src_set_linnum(int32_t newline
)
561 int32_t oldline
= line_number
;
562 line_number
= newline
;
566 int32_t src_get_linnum(void)
571 int src_get(int32_t *xline
, char **xname
)
573 if (!file_name
|| !*xname
|| strcmp(*xname
, file_name
)) {
575 *xname
= file_name
? nasm_strdup(file_name
) : NULL
;
576 *xline
= line_number
;
579 if (*xline
!= line_number
) {
580 int32_t tmp
= line_number
- *xline
;
581 *xline
= line_number
;
587 char *nasm_strcat(const char *one
, const char *two
)
590 int l1
= strlen(one
);
591 rslt
= nasm_malloc(l1
+ strlen(two
) + 1);
593 strcpy(rslt
+ l1
, two
);
597 /* skip leading spaces */
598 char *nasm_skip_spaces(const char *p
)
601 while (*p
&& nasm_isspace(*p
))
606 /* skip leading non-spaces */
607 char *nasm_skip_word(const char *p
)
610 while (*p
&& !nasm_isspace(*p
))
615 /* zap leading spaces with zero */
616 char *nasm_zap_spaces_fwd(char *p
)
619 while (*p
&& nasm_isspace(*p
))
624 /* zap spaces with zero in reverse order */
625 char *nasm_zap_spaces_rev(char *p
)
628 while (*p
&& nasm_isspace(*p
))
633 /* zap leading and trailing spaces */
634 char *nasm_trim_spaces(char *p
)
636 p
= nasm_zap_spaces_fwd(p
);
637 nasm_zap_spaces_fwd(nasm_skip_word(p
));
643 * return the word extracted from a stream
644 * or NULL if nothing left
646 char *nasm_get_word(char *p
, char **tail
)
648 char *word
= nasm_skip_spaces(p
);
649 char *next
= nasm_skip_word(word
);
657 /* NOTE: the tail may start with spaces */
664 * Extract "opt=val" values from the stream and
668 * 1) If "=val" passed the NULL returned though
669 * you may continue handling the tail via "next"
670 * 2) If "=" passed the NULL is returned and "val"
671 * is set to NULL as well
673 char *nasm_opt_val(char *p
, char **val
, char **next
)
679 p
= nasm_get_word(p
, &nxt
);
691 q
= nasm_get_word(q
+ 1, &nxt
);
696 q
= nasm_skip_spaces(nxt
);
697 if (q
&& *q
== '=') {
698 q
= nasm_get_word(q
+ 1, &nxt
);
709 * initialized data bytes length from opcode
711 int idata_bytes(int opcode
)