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 */
29 * Prepare a table of tolower() results. This avoids function calls
33 unsigned char nasm_tolower_tab
[256];
35 void tolower_init(void)
39 for (i
= 0; i
< 256; i
++)
40 nasm_tolower_tab
[i
] = tolower(i
);
43 void nasm_set_malloc_error(efunc error
)
45 nasm_malloc_error
= error
;
47 logfp
= fopen("malloc.log", "w");
48 setvbuf(logfp
, NULL
, _IOLBF
, BUFSIZ
);
49 fprintf(logfp
, "null pointer is %p\n", NULL
);
54 void *nasm_malloc_log(char *file
, int line
, size_t size
)
56 void *nasm_malloc(size_t size
)
59 void *p
= malloc(size
);
61 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
64 fprintf(logfp
, "%s %d malloc(%ld) returns %p\n",
65 file
, line
, (long)size
, p
);
71 void *nasm_zalloc_log(char *file
, int line
, size_t size
)
73 void *nasm_zalloc(size_t size
)
76 void *p
= calloc(size
, 1);
78 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
81 fprintf(logfp
, "%s %d calloc(%ld, 1) returns %p\n",
82 file
, line
, (long)size
, p
);
88 void *nasm_realloc_log(char *file
, int line
, void *q
, size_t size
)
90 void *nasm_realloc(void *q
, size_t size
)
93 void *p
= q
? realloc(q
, size
) : malloc(size
);
95 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
98 fprintf(logfp
, "%s %d realloc(%p,%ld) returns %p\n",
99 file
, line
, q
, (long)size
, p
);
101 fprintf(logfp
, "%s %d malloc(%ld) returns %p\n",
102 file
, line
, (long)size
, p
);
108 void nasm_free_log(char *file
, int line
, void *q
)
110 void nasm_free(void *q
)
115 fprintf(logfp
, "%s %d free(%p)\n", file
, line
, q
);
122 char *nasm_strdup_log(char *file
, int line
, const char *s
)
124 char *nasm_strdup(const char *s
)
128 int size
= strlen(s
) + 1;
132 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
135 fprintf(logfp
, "%s %d strdup(%ld) returns %p\n",
136 file
, line
, (long)size
, p
);
143 char *nasm_strndup_log(char *file
, int line
, char *s
, size_t len
)
145 char *nasm_strndup(char *s
, size_t len
)
153 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
156 fprintf(logfp
, "%s %d strndup(%ld) returns %p\n",
157 file
, line
, (long)size
, p
);
165 int nasm_stricmp(const char *s1
, const char *s2
)
167 unsigned char c1
, c2
;
171 c1
= nasm_tolower(*s1
++);
172 c2
= nasm_tolower(*s2
++);
184 #ifndef nasm_strnicmp
185 int nasm_strnicmp(const char *s1
, const char *s2
, size_t n
)
187 unsigned char c1
, c2
;
191 c1
= nasm_tolower(*s1
++);
192 c2
= nasm_tolower(*s2
++);
204 int nasm_memicmp(const char *s1
, const char *s2
, size_t n
)
206 unsigned char c1
, c2
;
210 c1
= nasm_tolower(*s1
++);
211 c2
= nasm_tolower(*s2
++);
220 char *nasm_strsep(char **stringp
, const char *delim
)
228 e
= strpbrk(s
, delim
);
238 #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
239 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
241 static int radix_letter(char c
)
246 return 2; /* Binary */
249 return 8; /* Octal */
252 return 16; /* Hexadecimal */
255 return 10; /* Decimal */
257 return 0; /* Not a known radix letter */
261 int64_t readnum(char *str
, bool *error
)
264 int32_t pradix
, sradix
, radix
;
266 uint64_t result
, checklimit
;
273 while (nasm_isspace(*r
))
274 r
++; /* find start of number */
277 * If the number came from make_tok_num (as a result of an %assign), it
278 * might have a '-' built into it (rather than in a preceeding token).
287 while (lib_isnumchar(*q
))
288 q
++; /* find end of number */
298 * Handle radix formats:
300 * 0<radix-letter><string>
301 * $<string> (hexadecimal)
302 * <string><radix-letter>
307 if (len
> 2 && *r
== '0' && (pradix
= radix_letter(r
[1])) != 0)
309 else if (len
> 1 && *r
== '$')
310 pradix
= 16, plen
= 1;
312 if (len
> 1 && (sradix
= radix_letter(q
[-1])) != 0)
315 if (pradix
> sradix
) {
318 } else if (sradix
> pradix
) {
322 /* Either decimal, or invalid -- if invalid, we'll trip up
328 * `checklimit' must be 2**64 / radix. We can't do that in
329 * 64-bit arithmetic, which we're (probably) using, so we
330 * cheat: since we know that all radices we use are even, we
331 * can divide 2**63 by radix/2 instead.
333 checklimit
= 0x8000000000000000ULL
/ (radix
>> 1);
336 * Calculate the highest allowable value for the last digit of a
337 * 64-bit constant... in radix 10, it is 6, otherwise it is 0
339 last
= (radix
== 10 ? 6 : 0);
342 while (*r
&& r
< q
) {
344 if (*r
< '0' || (*r
> '9' && *r
< 'A')
345 || (digit
= numvalue(*r
)) >= radix
) {
349 if (result
> checklimit
||
350 (result
== checklimit
&& digit
>= last
)) {
354 result
= radix
* result
+ digit
;
360 nasm_malloc_error(ERR_WARNING
| ERR_PASS1
| ERR_WARN_NOV
,
361 "numeric constant %s does not fit in 64 bits",
364 return result
* sign
;
367 int64_t readstrnum(char *str
, int length
, bool *warn
)
369 int64_t charconst
= 0;
375 if (globalbits
== 64) {
376 for (i
= 0; i
< length
; i
++) {
377 if (charconst
& 0xFF00000000000000ULL
)
379 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
382 for (i
= 0; i
< length
; i
++) {
383 if (charconst
& 0xFF000000UL
)
385 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
391 static int32_t next_seg
;
398 int32_t seg_alloc(void)
400 return (next_seg
+= 2) - 2;
403 #ifdef WORDS_LITTLEENDIAN
405 void fwriteint16_t(uint16_t data
, FILE * fp
)
407 fwrite(&data
, 1, 2, fp
);
410 void fwriteint32_t(uint32_t data
, FILE * fp
)
412 fwrite(&data
, 1, 4, fp
);
415 void fwriteint64_t(uint64_t data
, FILE * fp
)
417 fwrite(&data
, 1, 8, fp
);
420 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
422 fwrite(&data
, 1, size
, fp
);
425 #else /* not WORDS_LITTLEENDIAN */
427 void fwriteint16_t(uint16_t data
, FILE * fp
)
429 char buffer
[2], *p
= buffer
;
431 fwrite(buffer
, 1, 2, fp
);
434 void fwriteint32_t(uint32_t data
, FILE * fp
)
436 char buffer
[4], *p
= buffer
;
438 fwrite(buffer
, 1, 4, fp
);
441 void fwriteint64_t(uint64_t data
, FILE * fp
)
443 char buffer
[8], *p
= buffer
;
445 fwrite(buffer
, 1, 8, fp
);
448 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
450 char buffer
[8], *p
= buffer
;
451 WRITEADDR(p
, data
, size
);
452 fwrite(buffer
, 1, size
, fp
);
457 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 error(ERR_WARNING
| ERR_NOFILE
,
476 "file name already ends in `%s': "
477 "output will be in `nasm.out'", extension
);
479 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", "lock", "o16", "o32", "rep", "repe", "repne",
492 "repnz", "repz", "times"
495 const char *prefix_name(int token
)
497 unsigned int prefix
= token
-PREFIX_ENUM_START
;
498 if (prefix
> elements(prefix_names
))
501 return prefix_names
[prefix
];
507 int bsi(const char *string
, const char **array
, int size
)
509 int i
= -1, j
= size
; /* always, i < index < j */
512 int l
= strcmp(string
, array
[k
]);
513 if (l
< 0) /* it's in the first half */
515 else if (l
> 0) /* it's in the second half */
517 else /* we've got it :) */
520 return -1; /* we haven't got it :( */
523 int bsii(const char *string
, const char **array
, int size
)
525 int i
= -1, j
= size
; /* always, i < index < j */
528 int l
= nasm_stricmp(string
, array
[k
]);
529 if (l
< 0) /* it's in the first half */
531 else if (l
> 0) /* it's in the second half */
533 else /* we've got it :) */
536 return -1; /* we haven't got it :( */
539 static char *file_name
= NULL
;
540 static int32_t line_number
= 0;
542 char *src_set_fname(char *newname
)
544 char *oldname
= file_name
;
549 int32_t src_set_linnum(int32_t newline
)
551 int32_t oldline
= line_number
;
552 line_number
= newline
;
556 int32_t src_get_linnum(void)
561 int src_get(int32_t *xline
, char **xname
)
563 if (!file_name
|| !*xname
|| strcmp(*xname
, file_name
)) {
565 *xname
= file_name
? nasm_strdup(file_name
) : NULL
;
566 *xline
= line_number
;
569 if (*xline
!= line_number
) {
570 int32_t tmp
= line_number
- *xline
;
571 *xline
= line_number
;
577 char *nasm_strcat(char *one
, char *two
)
580 int l1
= strlen(one
);
581 rslt
= nasm_malloc(l1
+ strlen(two
) + 1);
583 strcpy(rslt
+ l1
, two
);
587 void null_debug_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
594 void null_debug_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
600 void null_debug_deflabel(char *name
, int32_t segment
, int64_t offset
,
601 int is_global
, char *special
)
609 void null_debug_routine(const char *directive
, const char *params
)
614 void null_debug_typevalue(int32_t type
)
618 void null_debug_output(int type
, void *param
)
623 void null_debug_cleanup(void)
627 struct dfmt null_debug_form
= {
634 null_debug_typevalue
,
639 struct dfmt
*null_debug_arr
[2] = { &null_debug_form
, NULL
};