1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2010 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 static vefunc nasm_verror
; /* Global error handling function */
57 /* Uninitialized -> all zero by C spec */
58 const uint8_t zero_buffer
[ZERO_BUF_SIZE
];
61 * Prepare a table of tolower() results. This avoids function calls
65 unsigned char nasm_tolower_tab
[256];
67 void tolower_init(void)
71 for (i
= 0; i
< 256; i
++)
72 nasm_tolower_tab
[i
] = tolower(i
);
75 void nasm_set_verror(vefunc ve
)
80 void nasm_error(int severity
, const char *fmt
, ...)
85 nasm_verror(severity
, fmt
, ap
);
89 void nasm_init_malloc_error(void)
92 logfp
= fopen("malloc.log", "w");
94 setvbuf(logfp
, NULL
, _IOLBF
, BUFSIZ
);
96 nasm_error(ERR_NONFATAL
| ERR_NOFILE
, "Unable to open %s", logfp
);
99 fprintf(logfp
, "null pointer is %p\n", NULL
);
104 void *nasm_malloc_log(const char *file
, int line
, size_t size
)
106 void *nasm_malloc(size_t size
)
109 void *p
= malloc(size
);
111 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
114 fprintf(logfp
, "%s %d malloc(%ld) returns %p\n",
115 file
, line
, (long)size
, p
);
121 void *nasm_zalloc_log(const char *file
, int line
, size_t size
)
123 void *nasm_zalloc(size_t size
)
126 void *p
= calloc(size
, 1);
128 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
131 fprintf(logfp
, "%s %d calloc(%ld, 1) returns %p\n",
132 file
, line
, (long)size
, p
);
138 void *nasm_realloc_log(const char *file
, int line
, void *q
, size_t size
)
140 void *nasm_realloc(void *q
, size_t size
)
143 void *p
= q
? realloc(q
, size
) : malloc(size
);
145 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
148 fprintf(logfp
, "%s %d realloc(%p,%ld) returns %p\n",
149 file
, line
, q
, (long)size
, p
);
151 fprintf(logfp
, "%s %d malloc(%ld) returns %p\n",
152 file
, line
, (long)size
, p
);
158 void nasm_free_log(const char *file
, int line
, void *q
)
160 void nasm_free(void *q
)
165 fprintf(logfp
, "%s %d free(%p)\n", file
, line
, q
);
172 char *nasm_strdup_log(const char *file
, int line
, const char *s
)
174 char *nasm_strdup(const char *s
)
178 int size
= strlen(s
) + 1;
182 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
185 fprintf(logfp
, "%s %d strdup(%ld) returns %p\n",
186 file
, line
, (long)size
, p
);
193 char *nasm_strndup_log(const char *file
, int line
, const char *s
, size_t len
)
195 char *nasm_strndup(const char *s
, size_t len
)
203 nasm_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
206 fprintf(logfp
, "%s %d strndup(%ld) returns %p\n",
207 file
, line
, (long)size
, p
);
214 no_return
nasm_assert_failed(const char *file
, int line
, const char *msg
)
216 nasm_error(ERR_FATAL
, "assertion %s failed at %s:%d", msg
, file
, line
);
221 int nasm_stricmp(const char *s1
, const char *s2
)
223 unsigned char c1
, c2
;
227 c1
= nasm_tolower(*s1
++);
228 c2
= nasm_tolower(*s2
++);
240 #ifndef nasm_strnicmp
241 int nasm_strnicmp(const char *s1
, const char *s2
, size_t n
)
243 unsigned char c1
, c2
;
247 c1
= nasm_tolower(*s1
++);
248 c2
= nasm_tolower(*s2
++);
260 int nasm_memicmp(const char *s1
, const char *s2
, size_t n
)
262 unsigned char c1
, c2
;
266 c1
= nasm_tolower(*s1
++);
267 c2
= nasm_tolower(*s2
++);
276 char *nasm_strsep(char **stringp
, const char *delim
)
284 e
= strpbrk(s
, delim
);
293 char *nasm_strrep(const char *str
, const char *sub
, char *lin
, bool casesense
)
298 char *l
, *ll
, *lp
, *lt
, *ls
;
300 int str_len
, sub_len
, lin_len
;
303 str_len
= strlen(str
);
304 sub_len
= strlen(sub
);
305 lin_len
= strlen(lin
);
307 if ((str_len
> 0) && (lin_len
> 0)) {
308 if (casesense
== false) {
309 l
= nasm_strdup(lin
);
310 for (i
= 0; i
< lin_len
; i
++) {
311 l
[i
] = (char)nasm_tolower_tab
[(int)l
[i
]];
313 ls
= nasm_strdup(str
);
314 for (i
= 0; i
< str_len
; i
++) {
315 ls
[i
] = (char)nasm_tolower_tab
[(int)ls
[i
]];
335 i
= (lin_len
- (count
* str_len
));
336 i
+= (count
* sub_len
);
337 outline
= nasm_zalloc(i
);
342 for (i
= 0; i
< count
; i
++) {
347 strncat(outline
, lt
, c
);
350 strncat(outline
, sub
, sub_len
);
357 strncat(outline
, lt
, (lin_len
-c
));
376 #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
377 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
379 static int radix_letter(char c
)
384 return 2; /* Binary */
387 return 8; /* Octal */
390 return 16; /* Hexadecimal */
393 return 10; /* Decimal */
395 return 0; /* Not a known radix letter */
399 int64_t readnum(char *str
, bool *error
)
402 int32_t pradix
, sradix
, radix
;
404 uint64_t result
, checklimit
;
411 while (nasm_isspace(*r
))
412 r
++; /* find start of number */
415 * If the number came from make_tok_num (as a result of an %assign), it
416 * might have a '-' built into it (rather than in a preceeding token).
425 while (lib_isnumchar(*q
))
426 q
++; /* find end of number */
436 * Handle radix formats:
438 * 0<radix-letter><string>
439 * $<string> (hexadecimal)
440 * <string><radix-letter>
445 if (len
> 2 && *r
== '0' && (pradix
= radix_letter(r
[1])) != 0)
447 else if (len
> 1 && *r
== '$')
448 pradix
= 16, plen
= 1;
450 if (len
> 1 && (sradix
= radix_letter(q
[-1])) != 0)
453 if (pradix
> sradix
) {
456 } else if (sradix
> pradix
) {
460 /* Either decimal, or invalid -- if invalid, we'll trip up
466 * `checklimit' must be 2**64 / radix. We can't do that in
467 * 64-bit arithmetic, which we're (probably) using, so we
468 * cheat: since we know that all radices we use are even, we
469 * can divide 2**63 by radix/2 instead.
471 checklimit
= UINT64_C(0x8000000000000000) / (radix
>> 1);
474 * Calculate the highest allowable value for the last digit of a
475 * 64-bit constant... in radix 10, it is 6, otherwise it is 0
477 last
= (radix
== 10 ? 6 : 0);
480 while (*r
&& r
< q
) {
482 if (*r
< '0' || (*r
> '9' && *r
< 'A')
483 || (digit
= numvalue(*r
)) >= radix
) {
487 if (result
> checklimit
||
488 (result
== checklimit
&& digit
>= last
)) {
492 result
= radix
* result
+ digit
;
498 nasm_error(ERR_WARNING
| ERR_PASS1
| ERR_WARN_NOV
,
499 "numeric constant %s does not fit in 64 bits",
502 return result
* sign
;
505 int64_t readstrnum(char *str
, int length
, bool *warn
)
507 int64_t charconst
= 0;
513 if (globalbits
== 64) {
514 for (i
= 0; i
< length
; i
++) {
515 if (charconst
& UINT64_C(0xFF00000000000000))
517 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
520 for (i
= 0; i
< length
; i
++) {
521 if (charconst
& 0xFF000000UL
)
523 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
529 static int32_t next_seg
;
536 int32_t seg_alloc(void)
538 return (next_seg
+= 2) - 2;
541 #ifdef WORDS_LITTLEENDIAN
543 void fwriteint16_t(uint16_t data
, FILE * fp
)
545 fwrite(&data
, 1, 2, fp
);
548 void fwriteint32_t(uint32_t data
, FILE * fp
)
550 fwrite(&data
, 1, 4, fp
);
553 void fwriteint64_t(uint64_t data
, FILE * fp
)
555 fwrite(&data
, 1, 8, fp
);
558 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
560 fwrite(&data
, 1, size
, fp
);
563 #else /* not WORDS_LITTLEENDIAN */
565 void fwriteint16_t(uint16_t data
, FILE * fp
)
567 char buffer
[2], *p
= buffer
;
569 fwrite(buffer
, 1, 2, fp
);
572 void fwriteint32_t(uint32_t data
, FILE * fp
)
574 char buffer
[4], *p
= buffer
;
576 fwrite(buffer
, 1, 4, fp
);
579 void fwriteint64_t(uint64_t data
, FILE * fp
)
581 char buffer
[8], *p
= buffer
;
583 fwrite(buffer
, 1, 8, fp
);
586 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
588 char buffer
[8], *p
= buffer
;
589 WRITEADDR(p
, data
, size
);
590 fwrite(buffer
, 1, size
, fp
);
595 size_t fwritezero(size_t bytes
, FILE *fp
)
602 blksize
= (bytes
< ZERO_BUF_SIZE
) ? bytes
: ZERO_BUF_SIZE
;
604 rv
= fwrite(zero_buffer
, 1, blksize
, fp
);
615 void standard_extension(char *inname
, char *outname
, char *extension
)
619 if (*outname
) /* file name already exists, */
620 return; /* so do nothing */
624 *p
++ = *q
++; /* copy, and find end of string */
625 *p
= '\0'; /* terminate it */
626 while (p
> outname
&& *--p
!= '.') ; /* find final period (or whatever) */
629 p
++; /* go back to end if none found */
630 if (!strcmp(p
, extension
)) { /* is the extension already there? */
632 nasm_error(ERR_WARNING
| ERR_NOFILE
,
633 "file name already ends in `%s': "
634 "output will be in `nasm.out'", extension
);
636 nasm_error(ERR_WARNING
| ERR_NOFILE
,
637 "file name already has no extension: "
638 "output will be in `nasm.out'");
639 strcpy(outname
, "nasm.out");
641 strcpy(p
, extension
);
645 * Common list of prefix names
647 static const char *prefix_names
[] = {
648 "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
649 "rep", "repe", "repne", "repnz", "repz", "times", "wait"
652 const char *prefix_name(int token
)
654 unsigned int prefix
= token
-PREFIX_ENUM_START
;
655 if (prefix
>= ARRAY_SIZE(prefix_names
))
658 return prefix_names
[prefix
];
664 int bsi(const char *string
, const char **array
, int size
)
666 int i
= -1, j
= size
; /* always, i < index < j */
669 int l
= strcmp(string
, array
[k
]);
670 if (l
< 0) /* it's in the first half */
672 else if (l
> 0) /* it's in the second half */
674 else /* we've got it :) */
677 return -1; /* we haven't got it :( */
680 int bsii(const char *string
, const char **array
, int size
)
682 int i
= -1, j
= size
; /* always, i < index < j */
685 int l
= nasm_stricmp(string
, array
[k
]);
686 if (l
< 0) /* it's in the first half */
688 else if (l
> 0) /* it's in the second half */
690 else /* we've got it :) */
693 return -1; /* we haven't got it :( */
696 static char *file_name
= NULL
;
697 static int32_t line_number
= 0;
699 char *src_set_fname(char *newname
)
701 char *oldname
= file_name
;
706 int32_t src_set_linnum(int32_t newline
)
708 int32_t oldline
= line_number
;
709 line_number
= newline
;
713 int32_t src_get_linnum(void)
718 int src_get(int32_t *xline
, char **xname
)
720 if (!file_name
|| !*xname
|| strcmp(*xname
, file_name
)) {
722 *xname
= file_name
? nasm_strdup(file_name
) : NULL
;
723 *xline
= line_number
;
726 if (*xline
!= line_number
) {
727 int32_t tmp
= line_number
- *xline
;
728 *xline
= line_number
;
734 char *nasm_strcat(const char *one
, const char *two
)
737 int l1
= strlen(one
);
738 rslt
= nasm_malloc(l1
+ strlen(two
) + 1);
740 strcpy(rslt
+ l1
, two
);
744 /* skip leading spaces */
745 char *nasm_skip_spaces(const char *p
)
748 while (*p
&& nasm_isspace(*p
))
753 /* skip leading non-spaces */
754 char *nasm_skip_word(const char *p
)
757 while (*p
&& !nasm_isspace(*p
))
762 /* zap leading spaces with zero */
763 char *nasm_zap_spaces_fwd(char *p
)
766 while (*p
&& nasm_isspace(*p
))
771 /* zap spaces with zero in reverse order */
772 char *nasm_zap_spaces_rev(char *p
)
775 while (*p
&& nasm_isspace(*p
))
780 /* zap leading and trailing spaces */
781 char *nasm_trim_spaces(char *p
)
783 p
= nasm_zap_spaces_fwd(p
);
784 nasm_zap_spaces_fwd(nasm_skip_word(p
));
790 * return the word extracted from a stream
791 * or NULL if nothing left
793 char *nasm_get_word(char *p
, char **tail
)
795 char *word
= nasm_skip_spaces(p
);
796 char *next
= nasm_skip_word(word
);
804 /* NOTE: the tail may start with spaces */
811 * Extract "opt=val" values from the stream and
815 * 1) If "=val" passed the NULL returned though
816 * you may continue handling the tail via "next"
817 * 2) If "=" passed the NULL is returned and "val"
818 * is set to NULL as well
820 char *nasm_opt_val(char *p
, char **val
, char **next
)
824 opt
= *val
= *next
= NULL
;
826 p
= nasm_get_word(p
, &nxt
);
838 q
= nasm_get_word(q
+ 1, &nxt
);
843 q
= nasm_skip_spaces(nxt
);
844 if (q
&& *q
== '=') {
845 q
= nasm_get_word(q
+ 1, &nxt
);
856 * initialized data bytes length from opcode
858 int idata_bytes(int opcode
)