1 /* nasmlib.c library routines for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
21 int globalbits
= 0; /* defined in nasm.h, works better here for ASM+DISASM */
22 efunc nasm_malloc_error
; /* Exported for the benefit of vsnprintf.c */
28 /* Uninitialized -> all zero by C spec */
29 const uint8_t zero_buffer
[ZERO_BUF_SIZE
];
32 * Prepare a table of tolower() results. This avoids function calls
36 unsigned char nasm_tolower_tab
[256];
38 void tolower_init(void)
42 for (i
= 0; i
< 256; i
++)
43 nasm_tolower_tab
[i
] = tolower(i
);
46 void nasm_set_malloc_error(efunc error
)
48 nasm_malloc_error
= error
;
50 logfp
= fopen("malloc.log", "w");
51 setvbuf(logfp
, NULL
, _IOLBF
, BUFSIZ
);
52 fprintf(logfp
, "null pointer is %p\n", NULL
);
57 void *nasm_malloc_log(char *file
, int line
, size_t size
)
59 void *nasm_malloc(size_t size
)
62 void *p
= malloc(size
);
64 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
67 fprintf(logfp
, "%s %d malloc(%ld) returns %p\n",
68 file
, line
, (long)size
, p
);
74 void *nasm_zalloc_log(char *file
, int line
, size_t size
)
76 void *nasm_zalloc(size_t size
)
79 void *p
= calloc(size
, 1);
81 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
84 fprintf(logfp
, "%s %d calloc(%ld, 1) returns %p\n",
85 file
, line
, (long)size
, p
);
91 void *nasm_realloc_log(char *file
, int line
, void *q
, size_t size
)
93 void *nasm_realloc(void *q
, size_t size
)
96 void *p
= q
? realloc(q
, size
) : malloc(size
);
98 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
101 fprintf(logfp
, "%s %d realloc(%p,%ld) returns %p\n",
102 file
, line
, q
, (long)size
, p
);
104 fprintf(logfp
, "%s %d malloc(%ld) returns %p\n",
105 file
, line
, (long)size
, p
);
111 void nasm_free_log(char *file
, int line
, void *q
)
113 void nasm_free(void *q
)
118 fprintf(logfp
, "%s %d free(%p)\n", file
, line
, q
);
125 char *nasm_strdup_log(char *file
, int line
, const char *s
)
127 char *nasm_strdup(const char *s
)
131 int size
= strlen(s
) + 1;
135 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
138 fprintf(logfp
, "%s %d strdup(%ld) returns %p\n",
139 file
, line
, (long)size
, p
);
146 char *nasm_strndup_log(char *file
, int line
, char *s
, size_t len
)
148 char *nasm_strndup(char *s
, size_t len
)
156 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
159 fprintf(logfp
, "%s %d strndup(%ld) returns %p\n",
160 file
, line
, (long)size
, p
);
168 int nasm_stricmp(const char *s1
, const char *s2
)
170 unsigned char c1
, c2
;
174 c1
= nasm_tolower(*s1
++);
175 c2
= nasm_tolower(*s2
++);
187 #ifndef nasm_strnicmp
188 int nasm_strnicmp(const char *s1
, const char *s2
, size_t n
)
190 unsigned char c1
, c2
;
194 c1
= nasm_tolower(*s1
++);
195 c2
= nasm_tolower(*s2
++);
207 int nasm_memicmp(const char *s1
, const char *s2
, size_t n
)
209 unsigned char c1
, c2
;
213 c1
= nasm_tolower(*s1
++);
214 c2
= nasm_tolower(*s2
++);
223 char *nasm_strsep(char **stringp
, const char *delim
)
231 e
= strpbrk(s
, delim
);
241 #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
242 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
244 static int radix_letter(char c
)
249 return 2; /* Binary */
252 return 8; /* Octal */
255 return 16; /* Hexadecimal */
258 return 10; /* Decimal */
260 return 0; /* Not a known radix letter */
264 int64_t readnum(char *str
, bool *error
)
267 int32_t pradix
, sradix
, radix
;
269 uint64_t result
, checklimit
;
276 while (nasm_isspace(*r
))
277 r
++; /* find start of number */
280 * If the number came from make_tok_num (as a result of an %assign), it
281 * might have a '-' built into it (rather than in a preceeding token).
290 while (lib_isnumchar(*q
))
291 q
++; /* find end of number */
301 * Handle radix formats:
303 * 0<radix-letter><string>
304 * $<string> (hexadecimal)
305 * <string><radix-letter>
310 if (len
> 2 && *r
== '0' && (pradix
= radix_letter(r
[1])) != 0)
312 else if (len
> 1 && *r
== '$')
313 pradix
= 16, plen
= 1;
315 if (len
> 1 && (sradix
= radix_letter(q
[-1])) != 0)
318 if (pradix
> sradix
) {
321 } else if (sradix
> pradix
) {
325 /* Either decimal, or invalid -- if invalid, we'll trip up
331 * `checklimit' must be 2**64 / radix. We can't do that in
332 * 64-bit arithmetic, which we're (probably) using, so we
333 * cheat: since we know that all radices we use are even, we
334 * can divide 2**63 by radix/2 instead.
336 checklimit
= 0x8000000000000000ULL
/ (radix
>> 1);
339 * Calculate the highest allowable value for the last digit of a
340 * 64-bit constant... in radix 10, it is 6, otherwise it is 0
342 last
= (radix
== 10 ? 6 : 0);
345 while (*r
&& r
< q
) {
347 if (*r
< '0' || (*r
> '9' && *r
< 'A')
348 || (digit
= numvalue(*r
)) >= radix
) {
352 if (result
> checklimit
||
353 (result
== checklimit
&& digit
>= last
)) {
357 result
= radix
* result
+ digit
;
363 nasm_malloc_error(ERR_WARNING
| ERR_PASS1
| ERR_WARN_NOV
,
364 "numeric constant %s does not fit in 64 bits",
367 return result
* sign
;
370 int64_t readstrnum(char *str
, int length
, bool *warn
)
372 int64_t charconst
= 0;
378 if (globalbits
== 64) {
379 for (i
= 0; i
< length
; i
++) {
380 if (charconst
& 0xFF00000000000000ULL
)
382 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
385 for (i
= 0; i
< length
; i
++) {
386 if (charconst
& 0xFF000000UL
)
388 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
394 static int32_t next_seg
;
401 int32_t seg_alloc(void)
403 return (next_seg
+= 2) - 2;
406 #ifdef WORDS_LITTLEENDIAN
408 void fwriteint16_t(uint16_t data
, FILE * fp
)
410 fwrite(&data
, 1, 2, fp
);
413 void fwriteint32_t(uint32_t data
, FILE * fp
)
415 fwrite(&data
, 1, 4, fp
);
418 void fwriteint64_t(uint64_t data
, FILE * fp
)
420 fwrite(&data
, 1, 8, fp
);
423 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
425 fwrite(&data
, 1, size
, fp
);
428 #else /* not WORDS_LITTLEENDIAN */
430 void fwriteint16_t(uint16_t data
, FILE * fp
)
432 char buffer
[2], *p
= buffer
;
434 fwrite(buffer
, 1, 2, fp
);
437 void fwriteint32_t(uint32_t data
, FILE * fp
)
439 char buffer
[4], *p
= buffer
;
441 fwrite(buffer
, 1, 4, fp
);
444 void fwriteint64_t(uint64_t data
, FILE * fp
)
446 char buffer
[8], *p
= buffer
;
448 fwrite(buffer
, 1, 8, fp
);
451 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
453 char buffer
[8], *p
= buffer
;
454 WRITEADDR(p
, data
, size
);
455 fwrite(buffer
, 1, size
, fp
);
460 size_t fwritezero(size_t bytes
, FILE *fp
)
467 blksize
= (bytes
< ZERO_BUF_SIZE
) ? bytes
: ZERO_BUF_SIZE
;
469 rv
= fwrite(zero_buffer
, 1, blksize
, fp
);
480 void standard_extension(char *inname
, char *outname
, char *extension
,
485 if (*outname
) /* file name already exists, */
486 return; /* so do nothing */
490 *p
++ = *q
++; /* copy, and find end of string */
491 *p
= '\0'; /* terminate it */
492 while (p
> outname
&& *--p
!= '.') ; /* find final period (or whatever) */
495 p
++; /* go back to end if none found */
496 if (!strcmp(p
, extension
)) { /* is the extension already there? */
498 error(ERR_WARNING
| ERR_NOFILE
,
499 "file name already ends in `%s': "
500 "output will be in `nasm.out'", extension
);
502 error(ERR_WARNING
| ERR_NOFILE
,
503 "file name already has no extension: "
504 "output will be in `nasm.out'");
505 strcpy(outname
, "nasm.out");
507 strcpy(p
, extension
);
511 * Common list of prefix names
513 static const char *prefix_names
[] = {
514 "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
515 "rep", "repe", "repne", "repnz", "repz", "times", "wait"
518 const char *prefix_name(int token
)
520 unsigned int prefix
= token
-PREFIX_ENUM_START
;
521 if (prefix
> elements(prefix_names
))
524 return prefix_names
[prefix
];
530 int bsi(const char *string
, const char **array
, int size
)
532 int i
= -1, j
= size
; /* always, i < index < j */
535 int l
= strcmp(string
, array
[k
]);
536 if (l
< 0) /* it's in the first half */
538 else if (l
> 0) /* it's in the second half */
540 else /* we've got it :) */
543 return -1; /* we haven't got it :( */
546 int bsii(const char *string
, const char **array
, int size
)
548 int i
= -1, j
= size
; /* always, i < index < j */
551 int l
= nasm_stricmp(string
, array
[k
]);
552 if (l
< 0) /* it's in the first half */
554 else if (l
> 0) /* it's in the second half */
556 else /* we've got it :) */
559 return -1; /* we haven't got it :( */
562 static char *file_name
= NULL
;
563 static int32_t line_number
= 0;
565 char *src_set_fname(char *newname
)
567 char *oldname
= file_name
;
572 int32_t src_set_linnum(int32_t newline
)
574 int32_t oldline
= line_number
;
575 line_number
= newline
;
579 int32_t src_get_linnum(void)
584 int src_get(int32_t *xline
, char **xname
)
586 if (!file_name
|| !*xname
|| strcmp(*xname
, file_name
)) {
588 *xname
= file_name
? nasm_strdup(file_name
) : NULL
;
589 *xline
= line_number
;
592 if (*xline
!= line_number
) {
593 int32_t tmp
= line_number
- *xline
;
594 *xline
= line_number
;
600 char *nasm_strcat(const char *one
, const char *two
)
603 int l1
= strlen(one
);
604 rslt
= nasm_malloc(l1
+ strlen(two
) + 1);
606 strcpy(rslt
+ l1
, two
);