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 licence given in the file "Licence"
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)
101 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 while (*s1 && tolower(*s1) == tolower(*s2))
156 else if (tolower(*s1) < tolower(*s2))
163 #ifndef nasm_strnicmp
164 int nasm_strnicmp(const char *s1, const char *s2, int n)
166 while (n > 0 && *s1 && tolower(*s1) == tolower(*s2))
168 if ((!*s1 && !*s2) || n == 0)
170 else if (tolower(*s1) < tolower(*s2))
178 char *nasm_strsep(char **stringp, const char *delim)
186 e = strpbrk(s, delim);
196 #define lib_isnumchar(c) ( isalnum(c) || (c) == '$')
197 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
199 int64_t readnum(char *str, int *error)
203 uint64_t result, checklimit;
211 r++; /* find start of number */
214 * If the number came from make_tok_num (as a result of an %assign), it
215 * might have a '-' built into it (rather than in a preceeding token).
224 while (lib_isnumchar(*q))
225 q++; /* find end of number */
228 * If it begins 0x, 0X or $, or ends in H, it's in hex. if it
229 * ends in Q, it's octal. if it ends in B, it's binary.
230 * Otherwise, it's ordinary decimal.
232 if (*r == '0' && (r[1] == 'x' || r[1] == 'X'))
236 else if (q[-1] == 'H' || q[-1] == 'h')
238 else if (q[-1] == 'Q' || q[-1] == 'q' || q[-1] == 'O' || q[-1] == 'o')
240 else if (q[-1] == 'B' || q[-1] == 'b')
246 * If this number has been found for us by something other than
247 * the ordinary scanners, then it might be malformed by having
248 * nothing between the prefix and the suffix. Check this case
257 * `checklimit' must be 2**(32|64) / radix. We can't do that in
258 * 32/64-bit arithmetic, which we're (probably) using, so we
259 * cheat: since we know that all radices we use are even, we
260 * can divide 2**(31|63) by radix/2 instead.
262 if (globalbits == 64)
263 checklimit = 0x8000000000000000ULL / (radix >> 1);
265 checklimit = 0x80000000UL / (radix >> 1);
268 * Calculate the highest allowable value for the last digit of a
269 * 32-bit constant... in radix 10, it is 6, otherwise it is 0
271 last = (radix == 10 ? 6 : 0);
274 while (*r && r < q) {
275 if (*r < '0' || (*r > '9' && *r < 'A')
276 || (digit = numvalue(*r)) >= radix) {
280 if (result > checklimit || (result == checklimit && digit >= last)) {
284 result = radix * result + digit;
289 nasm_malloc_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
290 "numeric constant %s does not fit in 32 bits",
293 return result * sign;
296 int64_t readstrnum(char *str, int length, int *warn)
298 int64_t charconst = 0;
304 if (globalbits == 64) {
305 for (i = 0; i < length; i++) {
306 if (charconst & 0xFF00000000000000ULL)
308 charconst = (charconst << 8) + (uint8_t)*--str;
311 for (i = 0; i < length; i++) {
312 if (charconst & 0xFF000000UL)
314 charconst = (charconst << 8) + (uint8_t)*--str;
320 static int32_t next_seg;
327 int32_t seg_alloc(void)
329 return (next_seg += 2) - 2;
332 void fwriteint16_t(int data, FILE * fp)
334 fputc((int)(data & 255), fp);
335 fputc((int)((data >> 8) & 255), fp);
338 void fwriteint32_t(int32_t data, FILE * fp)
340 fputc((int)(data & 255), fp);
341 fputc((int)((data >> 8) & 255), fp);
342 fputc((int)((data >> 16) & 255), fp);
343 fputc((int)((data >> 24) & 255), fp);
346 void fwriteint64_t(int64_t data, FILE * fp)
348 fputc((int)(data & 255), fp);
349 fputc((int)((data >> 8) & 255), fp);
350 fputc((int)((data >> 16) & 255), fp);
351 fputc((int)((data >> 24) & 255), fp);
352 fputc((int)((data >> 32) & 255), fp);
353 fputc((int)((data >> 40) & 255), fp);
354 fputc((int)((data >> 48) & 255), fp);
355 fputc((int)((data >> 56) & 255), fp);
358 void standard_extension(char *inname, char *outname, char *extension,
363 if (*outname) /* file name already exists, */
364 return; /* so do nothing */
368 *p++ = *q++; /* copy, and find end of string */
369 *p = '\0'; /* terminate it */
370 while (p > outname && *--p != '.') ; /* find final period (or whatever) */
373 p++; /* go back to end if none found */
374 if (!strcmp(p, extension)) { /* is the extension already there? */
376 error(ERR_WARNING | ERR_NOFILE,
377 "file name already ends in `%s': "
378 "output will be in `nasm.out'", extension);
380 error(ERR_WARNING | ERR_NOFILE,
381 "file name already has no extension: "
382 "output will be in `nasm.out'");
383 strcpy(outname, "nasm.out");
385 strcpy(p, extension);
388 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
389 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
391 #define LAYERSIZ(r) ( (r)->layers==0 ? RAA_BLKSIZE : RAA_LAYERSIZE )
393 static struct RAA *real_raa_init(int layers)
399 r = nasm_zalloc(LEAFSIZ);
402 r = nasm_malloc(BRANCHSIZ);
404 for (i = 0; i < RAA_LAYERSIZE; i++)
405 r->u.b.data[i] = NULL;
406 r->stepsize = RAA_BLKSIZE;
408 r->stepsize *= RAA_LAYERSIZE;
413 struct RAA *raa_init(void)
415 return real_raa_init(0);
418 void raa_free(struct RAA *r)
424 for (p = r->u.b.data; p - r->u.b.data < RAA_LAYERSIZE; p++)
430 int32_t raa_read(struct RAA *r, int32_t posn)
432 if (posn >= r->stepsize * LAYERSIZ(r))
433 return 0; /* Return 0 for undefined entries */
434 while (r->layers > 0) {
436 l = ldiv(posn, r->stepsize);
437 r = r->u.b.data[l.quot];
440 return 0; /* Return 0 for undefined entries */
442 return r->u.l.data[posn];
445 struct RAA *raa_write(struct RAA *r, int32_t posn, int32_t value)
450 nasm_malloc_error(ERR_PANIC, "negative position in raa_write");
452 while (r->stepsize * LAYERSIZ(r) <= posn) {
459 s = nasm_malloc(BRANCHSIZ);
460 for (i = 0; i < RAA_LAYERSIZE; i++)
461 s->u.b.data[i] = NULL;
462 s->layers = r->layers + 1;
463 s->stepsize = LAYERSIZ(r) * r->stepsize;
470 while (r->layers > 0) {
473 l = ldiv(posn, r->stepsize);
474 s = &r->u.b.data[l.quot];
476 *s = real_raa_init(r->layers - 1);
481 r->u.l.data[posn] = value;
486 #define SAA_MAXLEN 8192
488 struct SAA *saa_init(int32_t elem_len)
492 if (elem_len > SAA_MAXLEN)
493 nasm_malloc_error(ERR_PANIC | ERR_NOFILE,
494 "SAA with huge elements");
496 s = nasm_malloc(sizeof(struct SAA));
497 s->posn = s->start = 0L;
498 s->elem_len = elem_len;
499 s->length = SAA_MAXLEN - (SAA_MAXLEN % elem_len);
500 s->data = nasm_malloc(s->length);
507 void saa_free(struct SAA *s)
519 void *saa_wstruct(struct SAA *s)
523 if (s->end->length - s->end->posn < s->elem_len) {
524 s->end->next = nasm_malloc(sizeof(struct SAA));
525 s->end->next->start = s->end->start + s->end->posn;
526 s->end = s->end->next;
527 s->end->length = s->length;
530 s->end->data = nasm_malloc(s->length);
533 p = s->end->data + s->end->posn;
534 s->end->posn += s->elem_len;
538 void saa_wbytes(struct SAA *s, const void *data, int32_t len)
540 const char *d = data;
543 int32_t l = s->end->length - s->end->posn;
548 memcpy(s->end->data + s->end->posn, d, l);
551 memset(s->end->data + s->end->posn, 0, l);
556 s->end->next = nasm_malloc(sizeof(struct SAA));
557 s->end->next->start = s->end->start + s->end->posn;
558 s->end = s->end->next;
559 s->end->length = s->length;
562 s->end->data = nasm_malloc(s->length);
567 void saa_rewind(struct SAA *s)
573 void *saa_rstruct(struct SAA *s)
580 if (s->rptr->posn - s->rpos < s->elem_len) {
581 s->rptr = s->rptr->next;
583 return NULL; /* end of array */
587 p = s->rptr->data + s->rpos;
588 s->rpos += s->elem_len;
592 void *saa_rbytes(struct SAA *s, int32_t *len)
599 p = s->rptr->data + s->rpos;
600 *len = s->rptr->posn - s->rpos;
601 s->rptr = s->rptr->next;
606 void saa_rnbytes(struct SAA *s, void *data, int32_t len)
616 l = s->rptr->posn - s->rpos;
620 memcpy(d, s->rptr->data + s->rpos, l);
626 s->rptr = s->rptr->next;
632 void saa_fread(struct SAA *s, int32_t posn, void *data, int32_t len)
638 if (!s->rptr || posn < s->rptr->start)
641 while (posn >= p->start + p->posn) {
644 return; /* what else can we do?! */
647 pos = posn - p->start;
649 int64_t l = p->posn - pos;
652 memcpy(cdata, p->data + pos, l);
663 void saa_fwrite(struct SAA *s, int32_t posn, void *data, int32_t len)
669 if (!s->rptr || posn < s->rptr->start)
672 while (posn >= p->start + p->posn) {
675 return; /* what else can we do?! */
678 pos = posn - p->start;
680 int64_t l = p->posn - pos;
683 memcpy(p->data + pos, cdata, l);
694 void saa_fpwrite(struct SAA *s, FILE * fp)
700 // while ((data = saa_rbytes(s, &len)))
701 for (; (data = saa_rbytes(s, &len));)
702 fwrite(data, 1, len, fp);
706 * Common list of prefix names
708 static const char *prefix_names[] = {
709 "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
710 "repnz", "repz", "times"
713 const char *prefix_name(int token)
715 unsigned int prefix = token-PREFIX_ENUM_START;
716 if (prefix > sizeof prefix_names / sizeof(const char *))
719 return prefix_names[prefix];
725 int bsi(char *string, const char **array, int size)
727 int i = -1, j = size; /* always, i < index < j */
730 int l = strcmp(string, array[k]);
731 if (l < 0) /* it's in the first half */
733 else if (l > 0) /* it's in the second half */
735 else /* we've got it :) */
738 return -1; /* we haven't got it :( */
741 int bsii(char *string, const char **array, int size)
743 int i = -1, j = size; /* always, i < index < j */
746 int l = nasm_stricmp(string, array[k]);
747 if (l < 0) /* it's in the first half */
749 else if (l > 0) /* it's in the second half */
751 else /* we've got it :) */
754 return -1; /* we haven't got it :( */
757 static char *file_name = NULL;
758 static int32_t line_number = 0;
760 char *src_set_fname(char *newname)
762 char *oldname = file_name;
767 int32_t src_set_linnum(int32_t newline)
769 int32_t oldline = line_number;
770 line_number = newline;
774 int32_t src_get_linnum(void)
779 int src_get(int32_t *xline, char **xname)
781 if (!file_name || !*xname || strcmp(*xname, file_name)) {
783 *xname = file_name ? nasm_strdup(file_name) : NULL;
784 *xline = line_number;
787 if (*xline != line_number) {
788 int32_t tmp = line_number - *xline;
789 *xline = line_number;
795 void nasm_quote(char **str)
797 int ln = strlen(*str);
800 if (ln > 1 && (*str)[ln - 1] == q && (q == '"' || q == '\''))
805 p = nasm_malloc(ln + 3);
808 p[ln + 1] = p[0] = q;
813 char *nasm_strcat(char *one, char *two)
816 int l1 = strlen(one);
817 rslt = nasm_malloc(l1 + strlen(two) + 1);
819 strcpy(rslt + l1, two);
823 void null_debug_init(struct ofmt *of, void *id, FILE * fp, efunc error)
830 void null_debug_linenum(const char *filename, int32_t linenumber, int32_t segto)
836 void null_debug_deflabel(char *name, int32_t segment, int32_t offset,
837 int is_global, char *special)
845 void null_debug_routine(const char *directive, const char *params)
850 void null_debug_typevalue(int32_t type)
854 void null_debug_output(int type, void *param)
859 void null_debug_cleanup(void)
863 struct dfmt null_debug_form = {
870 null_debug_typevalue,
875 struct dfmt *null_debug_arr[2] = { &null_debug_form, NULL };