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 void nasm_set_malloc_error(efunc error)
30 nasm_malloc_error = error;
32 logfp = fopen("malloc.log", "w");
33 setvbuf(logfp, NULL, _IOLBF, BUFSIZ);
34 fprintf(logfp, "null pointer is %p\n", NULL);
39 void *nasm_malloc_log(char *file, int line, size_t size)
41 void *nasm_malloc(size_t size)
44 void *p = malloc(size);
46 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
49 fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
50 file, line, (long)size, p);
56 void *nasm_zalloc_log(char *file, int line, size_t size)
58 void *nasm_zalloc(size_t size)
61 void *p = calloc(size, 1);
63 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
66 fprintf(logfp, "%s %d calloc(%ld, 1) returns %p\n",
67 file, line, (long)size, p);
73 void *nasm_realloc_log(char *file, int line, void *q, size_t size)
75 void *nasm_realloc(void *q, size_t size)
78 void *p = q ? realloc(q, size) : malloc(size);
80 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
83 fprintf(logfp, "%s %d realloc(%p,%ld) returns %p\n",
84 file, line, q, (long)size, p);
86 fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
87 file, line, (long)size, p);
93 void nasm_free_log(char *file, int line, void *q)
95 void nasm_free(void *q)
100 fprintf(logfp, "%s %d free(%p)\n", file, line, q);
107 char *nasm_strdup_log(char *file, int line, const char *s)
109 char *nasm_strdup(const char *s)
113 int size = strlen(s) + 1;
117 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
120 fprintf(logfp, "%s %d strdup(%ld) returns %p\n",
121 file, line, (long)size, p);
128 char *nasm_strndup_log(char *file, int line, char *s, size_t len)
130 char *nasm_strndup(char *s, size_t len)
138 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
141 fprintf(logfp, "%s %d strndup(%ld) returns %p\n",
142 file, line, (long)size, p);
150 int nasm_stricmp(const char *s1, const char *s2)
152 unsigned char c1, c2;
169 #ifndef nasm_strnicmp
170 int nasm_strnicmp(const char *s1, const char *s2, size_t n)
172 unsigned char c1, c2;
189 int nasm_memicmp(const char *s1, const char *s2, size_t n)
191 unsigned char c1, c2;
205 char *nasm_strsep(char **stringp, const char *delim)
213 e = strpbrk(s, delim);
223 #define lib_isnumchar(c) (isalnum(c) || (c) == '$' || (c) == '_')
224 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
226 static int radix_letter(char c)
231 return 2; /* Binary */
234 return 8; /* Octal */
237 return 16; /* Hexadecimal */
240 return 10; /* Decimal */
242 return 0; /* Not a known radix letter */
246 int64_t readnum(char *str, bool *error)
249 int32_t pradix, sradix, radix;
251 uint64_t result, checklimit;
259 r++; /* find start of number */
262 * If the number came from make_tok_num (as a result of an %assign), it
263 * might have a '-' built into it (rather than in a preceeding token).
272 while (lib_isnumchar(*q))
273 q++; /* find end of number */
283 * Handle radix formats:
285 * 0<radix-letter><string>
286 * $<string> (hexadecimal)
287 * <string><radix-letter>
292 if (len > 2 && *r == '0' && (pradix = radix_letter(r[1])) != 0)
294 else if (len > 1 && *r == '$')
295 pradix = 16, plen = 1;
297 if (len > 1 && (sradix = radix_letter(q[-1])) != 0)
300 if (pradix > sradix) {
303 } else if (sradix > pradix) {
307 /* Either decimal, or invalid -- if invalid, we'll trip up
313 * `checklimit' must be 2**64 / radix. We can't do that in
314 * 64-bit arithmetic, which we're (probably) using, so we
315 * cheat: since we know that all radices we use are even, we
316 * can divide 2**63 by radix/2 instead.
318 checklimit = 0x8000000000000000ULL / (radix >> 1);
321 * Calculate the highest allowable value for the last digit of a
322 * 64-bit constant... in radix 10, it is 6, otherwise it is 0
324 last = (radix == 10 ? 6 : 0);
327 while (*r && r < q) {
329 if (*r < '0' || (*r > '9' && *r < 'A')
330 || (digit = numvalue(*r)) >= radix) {
334 if (result > checklimit ||
335 (result == checklimit && digit >= last)) {
339 result = radix * result + digit;
345 nasm_malloc_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
346 "numeric constant %s does not fit in 64 bits",
349 return result * sign;
352 int64_t readstrnum(char *str, int length, bool *warn)
354 int64_t charconst = 0;
360 if (globalbits == 64) {
361 for (i = 0; i < length; i++) {
362 if (charconst & 0xFF00000000000000ULL)
364 charconst = (charconst << 8) + (uint8_t)*--str;
367 for (i = 0; i < length; i++) {
368 if (charconst & 0xFF000000UL)
370 charconst = (charconst << 8) + (uint8_t)*--str;
376 static int32_t next_seg;
383 int32_t seg_alloc(void)
385 return (next_seg += 2) - 2;
390 void fwriteint16_t(uint16_t data, FILE * fp)
392 fwrite(&data, 1, 2, fp);
395 void fwriteint32_t(uint32_t data, FILE * fp)
397 fwrite(&data, 1, 4, fp);
400 void fwriteint64_t(uint64_t data, FILE * fp)
402 fwrite(&data, 1, 8, fp);
405 void fwriteaddr(uint64_t data, int size, FILE * fp)
407 fwrite(&data, 1, size, fp);
410 #else /* !X86_MEMORY */
412 void fwriteint16_t(uint16_t data, FILE * fp)
414 char buffer[2], *p = buffer;
416 fwrite(buffer, 1, 2, fp);
419 void fwriteint32_t(uint32_t data, FILE * fp)
421 char buffer[4], *p = buffer;
423 fwrite(buffer, 1, 4, fp);
426 void fwriteint64_t(uint64_t data, FILE * fp)
428 char buffer[8], *p = buffer;
430 fwrite(buffer, 1, 8, fp);
433 void fwriteaddr(uint64_t data, int size, FILE * fp)
435 char buffer[8], *p = buffer;
436 WRITEADDR(p, data, size);
437 fwrite(buffer, 1, size, fp);
442 void standard_extension(char *inname, char *outname, char *extension,
447 if (*outname) /* file name already exists, */
448 return; /* so do nothing */
452 *p++ = *q++; /* copy, and find end of string */
453 *p = '\0'; /* terminate it */
454 while (p > outname && *--p != '.') ; /* find final period (or whatever) */
457 p++; /* go back to end if none found */
458 if (!strcmp(p, extension)) { /* is the extension already there? */
460 error(ERR_WARNING | ERR_NOFILE,
461 "file name already ends in `%s': "
462 "output will be in `nasm.out'", extension);
464 error(ERR_WARNING | ERR_NOFILE,
465 "file name already has no extension: "
466 "output will be in `nasm.out'");
467 strcpy(outname, "nasm.out");
469 strcpy(p, extension);
473 * Common list of prefix names
475 static const char *prefix_names[] = {
476 "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
477 "repnz", "repz", "times"
480 const char *prefix_name(int token)
482 unsigned int prefix = token-PREFIX_ENUM_START;
483 if (prefix > elements(prefix_names))
486 return prefix_names[prefix];
492 int bsi(const char *string, const char **array, int size)
494 int i = -1, j = size; /* always, i < index < j */
497 int l = strcmp(string, array[k]);
498 if (l < 0) /* it's in the first half */
500 else if (l > 0) /* it's in the second half */
502 else /* we've got it :) */
505 return -1; /* we haven't got it :( */
508 int bsii(const char *string, const char **array, int size)
510 int i = -1, j = size; /* always, i < index < j */
513 int l = nasm_stricmp(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 static char *file_name = NULL;
525 static int32_t line_number = 0;
527 char *src_set_fname(char *newname)
529 char *oldname = file_name;
534 int32_t src_set_linnum(int32_t newline)
536 int32_t oldline = line_number;
537 line_number = newline;
541 int32_t src_get_linnum(void)
546 int src_get(int32_t *xline, char **xname)
548 if (!file_name || !*xname || strcmp(*xname, file_name)) {
550 *xname = file_name ? nasm_strdup(file_name) : NULL;
551 *xline = line_number;
554 if (*xline != line_number) {
555 int32_t tmp = line_number - *xline;
556 *xline = line_number;
562 char *nasm_strcat(char *one, char *two)
565 int l1 = strlen(one);
566 rslt = nasm_malloc(l1 + strlen(two) + 1);
568 strcpy(rslt + l1, two);
572 void null_debug_init(struct ofmt *of, void *id, FILE * fp, efunc error)
579 void null_debug_linenum(const char *filename, int32_t linenumber, int32_t segto)
585 void null_debug_deflabel(char *name, int32_t segment, int64_t offset,
586 int is_global, char *special)
594 void null_debug_routine(const char *directive, const char *params)
599 void null_debug_typevalue(int32_t type)
603 void null_debug_output(int type, void *param)
608 void null_debug_cleanup(void)
612 struct dfmt null_debug_form = {
619 null_debug_typevalue,
624 struct dfmt *null_debug_arr[2] = { &null_debug_form, NULL };