1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2014 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 void *nasm_malloc(size_t size
)
88 void *p
= malloc(size
);
90 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
94 void *nasm_zalloc(size_t size
)
96 void *p
= calloc(size
, 1);
98 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
102 void *nasm_realloc(void *q
, size_t size
)
104 void *p
= q
? realloc(q
, size
) : malloc(size
);
106 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
110 void nasm_free(void *q
)
116 char *nasm_strdup(const char *s
)
119 int size
= strlen(s
) + 1;
123 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
128 char *nasm_strndup(const char *s
, size_t len
)
135 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
141 no_return
nasm_assert_failed(const char *file
, int line
, const char *msg
)
143 nasm_error(ERR_FATAL
, "assertion %s failed at %s:%d", msg
, file
, line
);
147 void nasm_write(const void *ptr
, size_t size
, FILE *f
)
149 size_t n
= fwrite(ptr
, 1, size
, f
);
151 nasm_error(ERR_FATAL
, "unable to write output: %s", strerror(errno
));
155 int nasm_stricmp(const char *s1
, const char *s2
)
157 unsigned char c1
, c2
;
161 c1
= nasm_tolower(*s1
++);
162 c2
= nasm_tolower(*s2
++);
174 #ifndef nasm_strnicmp
175 int nasm_strnicmp(const char *s1
, const char *s2
, size_t n
)
177 unsigned char c1
, c2
;
181 c1
= nasm_tolower(*s1
++);
182 c2
= nasm_tolower(*s2
++);
194 int nasm_memicmp(const char *s1
, const char *s2
, size_t n
)
196 unsigned char c1
, c2
;
200 c1
= nasm_tolower(*s1
++);
201 c2
= nasm_tolower(*s2
++);
210 char *nasm_strsep(char **stringp
, const char *delim
)
218 e
= strpbrk(s
, delim
);
228 #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
230 static int radix_letter(char c
)
235 return 2; /* Binary */
238 return 8; /* Octal */
241 return 16; /* Hexadecimal */
244 return 10; /* Decimal */
246 return 0; /* Not a known radix letter */
250 int64_t readnum(char *str
, bool *error
)
253 int32_t pradix
, sradix
, radix
;
255 uint64_t result
, checklimit
;
262 while (nasm_isspace(*r
))
263 r
++; /* find start of number */
266 * If the number came from make_tok_num (as a result of an %assign), it
267 * might have a '-' built into it (rather than in a preceeding token).
276 while (lib_isnumchar(*q
))
277 q
++; /* find end of number */
287 * Handle radix formats:
289 * 0<radix-letter><string>
290 * $<string> (hexadecimal)
291 * <string><radix-letter>
296 if (len
> 2 && *r
== '0' && (pradix
= radix_letter(r
[1])) != 0)
298 else if (len
> 1 && *r
== '$')
299 pradix
= 16, plen
= 1;
301 if (len
> 1 && (sradix
= radix_letter(q
[-1])) != 0)
304 if (pradix
> sradix
) {
307 } else if (sradix
> pradix
) {
311 /* Either decimal, or invalid -- if invalid, we'll trip up
317 * `checklimit' must be 2**64 / radix. We can't do that in
318 * 64-bit arithmetic, which we're (probably) using, so we
319 * cheat: since we know that all radices we use are even, we
320 * can divide 2**63 by radix/2 instead.
322 checklimit
= UINT64_C(0x8000000000000000) / (radix
>> 1);
325 * Calculate the highest allowable value for the last digit of a
326 * 64-bit constant... in radix 10, it is 6, otherwise it is 0
328 last
= (radix
== 10 ? 6 : 0);
331 while (*r
&& r
< q
) {
333 if (*r
< '0' || (*r
> '9' && *r
< 'A')
334 || (digit
= numvalue(*r
)) >= radix
) {
338 if (result
> checklimit
||
339 (result
== checklimit
&& digit
>= last
)) {
343 result
= radix
* result
+ digit
;
349 nasm_error(ERR_WARNING
| ERR_PASS1
| ERR_WARN_NOV
,
350 "numeric constant %s does not fit in 64 bits",
353 return result
* sign
;
356 int64_t readstrnum(char *str
, int length
, bool *warn
)
358 int64_t charconst
= 0;
364 if (globalbits
== 64) {
365 for (i
= 0; i
< length
; i
++) {
366 if (charconst
& UINT64_C(0xFF00000000000000))
368 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
371 for (i
= 0; i
< length
; i
++) {
372 if (charconst
& 0xFF000000UL
)
374 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
380 static int32_t next_seg
;
387 int32_t seg_alloc(void)
389 return (next_seg
+= 2) - 2;
392 #ifdef WORDS_LITTLEENDIAN
394 void fwriteint16_t(uint16_t data
, FILE * fp
)
396 nasm_write(&data
, 2, fp
);
399 void fwriteint32_t(uint32_t data
, FILE * fp
)
401 nasm_write(&data
, 4, fp
);
404 void fwriteint64_t(uint64_t data
, FILE * fp
)
406 nasm_write(&data
, 8, fp
);
409 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
411 nasm_write(&data
, size
, fp
);
414 #else /* not WORDS_LITTLEENDIAN */
416 void fwriteint16_t(uint16_t data
, FILE * fp
)
418 char buffer
[2], *p
= buffer
;
420 nasm_write(buffer
, 2, fp
);
423 void fwriteint32_t(uint32_t data
, FILE * fp
)
425 char buffer
[4], *p
= buffer
;
427 nasm_write(buffer
, 4, fp
);
430 void fwriteint64_t(uint64_t data
, FILE * fp
)
432 char buffer
[8], *p
= buffer
;
434 nasm_write(buffer
, 8, fp
);
437 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
439 char buffer
[8], *p
= buffer
;
440 WRITEADDR(p
, data
, size
);
441 nasm_write(buffer
, size
, fp
);
446 void fwritezero(size_t bytes
, FILE *fp
)
451 blksize
= (bytes
< ZERO_BUF_SIZE
) ? bytes
: ZERO_BUF_SIZE
;
453 nasm_write(zero_buffer
, blksize
, fp
);
458 void standard_extension(char *inname
, char *outname
, char *extension
)
462 if (*outname
) /* file name already exists, */
463 return; /* so do nothing */
467 *p
++ = *q
++; /* copy, and find end of string */
468 *p
= '\0'; /* terminate it */
469 while (p
> outname
&& *--p
!= '.') ; /* find final period (or whatever) */
472 p
++; /* go back to end if none found */
473 if (!strcmp(p
, extension
)) { /* is the extension already there? */
475 nasm_error(ERR_WARNING
| ERR_NOFILE
,
476 "file name already ends in `%s': "
477 "output will be in `nasm.out'", extension
);
479 nasm_error(ERR_WARNING
| ERR_NOFILE
,
480 "file name already has no extension: "
481 "output will be in `nasm.out'");
482 strcpy(outname
, "nasm.out");
484 strcpy(p
, extension
);
488 * Common list of prefix names
490 static const char *prefix_names
[] = {
491 "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
492 "rep", "repe", "repne", "repnz", "repz", "times", "wait",
493 "xacquire", "xrelease", "bnd"
496 const char *prefix_name(int token
)
498 unsigned int prefix
= token
-PREFIX_ENUM_START
;
499 if (prefix
>= ARRAY_SIZE(prefix_names
))
502 return prefix_names
[prefix
];
508 int bsi(const char *string
, const char **array
, int size
)
510 int i
= -1, j
= size
; /* always, i < index < j */
513 int l
= strcmp(string
, array
[k
]);
514 if (l
< 0) /* it's in the first half */
516 else if (l
> 0) /* it's in the second half */
518 else /* we've got it :) */
521 return -1; /* we haven't got it :( */
524 int bsii(const char *string
, const char **array
, int size
)
526 int i
= -1, j
= size
; /* always, i < index < j */
529 int l
= nasm_stricmp(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 static char *file_name
= NULL
;
541 static int32_t line_number
= 0;
543 char *src_set_fname(char *newname
)
545 char *oldname
= file_name
;
550 int32_t src_set_linnum(int32_t newline
)
552 int32_t oldline
= line_number
;
553 line_number
= newline
;
557 int32_t src_get_linnum(void)
562 int src_get(int32_t *xline
, char **xname
)
564 if (!file_name
|| !*xname
|| strcmp(*xname
, file_name
)) {
566 *xname
= file_name
? nasm_strdup(file_name
) : NULL
;
567 *xline
= line_number
;
570 if (*xline
!= line_number
) {
571 int32_t tmp
= line_number
- *xline
;
572 *xline
= line_number
;
578 char *nasm_strcat(const char *one
, const char *two
)
581 int l1
= strlen(one
);
582 rslt
= nasm_malloc(l1
+ strlen(two
) + 1);
584 strcpy(rslt
+ l1
, two
);
588 /* skip leading spaces */
589 char *nasm_skip_spaces(const char *p
)
592 while (*p
&& nasm_isspace(*p
))
597 /* skip leading non-spaces */
598 char *nasm_skip_word(const char *p
)
601 while (*p
&& !nasm_isspace(*p
))
606 /* zap leading spaces with zero */
607 char *nasm_zap_spaces_fwd(char *p
)
610 while (*p
&& nasm_isspace(*p
))
615 /* zap spaces with zero in reverse order */
616 char *nasm_zap_spaces_rev(char *p
)
619 while (*p
&& nasm_isspace(*p
))
624 /* zap leading and trailing spaces */
625 char *nasm_trim_spaces(char *p
)
627 p
= nasm_zap_spaces_fwd(p
);
628 nasm_zap_spaces_fwd(nasm_skip_word(p
));
634 * return the word extracted from a stream
635 * or NULL if nothing left
637 char *nasm_get_word(char *p
, char **tail
)
639 char *word
= nasm_skip_spaces(p
);
640 char *next
= nasm_skip_word(word
);
648 /* NOTE: the tail may start with spaces */
655 * Extract "opt=val" values from the stream and
659 * 1) If "=val" passed the NULL returned though
660 * you may continue handling the tail via "next"
661 * 2) If "=" passed the NULL is returned and "val"
662 * is set to NULL as well
664 char *nasm_opt_val(char *p
, char **val
, char **next
)
670 p
= nasm_get_word(p
, &nxt
);
682 q
= nasm_get_word(q
+ 1, &nxt
);
687 q
= nasm_skip_spaces(nxt
);
688 if (q
&& *q
== '=') {
689 q
= nasm_get_word(q
+ 1, &nxt
);
700 * initialized data bytes length from opcode
702 int idata_bytes(int opcode
)