Version 0.99.04
[nasm.git] / nasmlib.c
blob0feb3ed3363818ac03a2698d5709799a7213b934
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.
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <inttypes.h>
15 #include "nasm.h"
16 #include "nasmlib.h"
17 #include "insns.h"
19 int globalbits = 0; /* defined in nasm.h, works better here for ASM+DISASM */
20 static efunc nasm_malloc_error;
22 #ifdef LOGALLOC
23 static FILE *logfp;
24 #endif
26 void nasm_set_malloc_error(efunc error)
28 nasm_malloc_error = error;
29 #ifdef LOGALLOC
30 logfp = fopen("malloc.log", "w");
31 setvbuf(logfp, NULL, _IOLBF, BUFSIZ);
32 fprintf(logfp, "null pointer is %p\n", NULL);
33 #endif
36 #ifdef LOGALLOC
37 void *nasm_malloc_log(char *file, int line, size_t size)
38 #else
39 void *nasm_malloc(size_t size)
40 #endif
42 void *p = malloc(size);
43 if (!p)
44 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
45 #ifdef LOGALLOC
46 else
47 fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
48 file, line, (long)size, p);
49 #endif
50 return p;
53 #ifdef LOGALLOC
54 void *nasm_zalloc_log(char *file, int line, size_t size)
55 #else
56 void *nasm_zalloc(size_t size)
57 #endif
59 void *p = calloc(size, 1);
60 if (!p)
61 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
62 #ifdef LOGALLOC
63 else
64 fprintf(logfp, "%s %d calloc(%ld, 1) returns %p\n",
65 file, line, (long)size, p);
66 #endif
67 return p;
70 #ifdef LOGALLOC
71 void *nasm_realloc_log(char *file, int line, void *q, size_t size)
72 #else
73 void *nasm_realloc(void *q, size_t size)
74 #endif
76 void *p = q ? realloc(q, size) : malloc(size);
77 if (!p)
78 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
79 #ifdef LOGALLOC
80 else if (q)
81 fprintf(logfp, "%s %d realloc(%p,%ld) returns %p\n",
82 file, line, q, (long)size, p);
83 else
84 fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
85 file, line, (long)size, p);
86 #endif
87 return p;
90 #ifdef LOGALLOC
91 void nasm_free_log(char *file, int line, void *q)
92 #else
93 void nasm_free(void *q)
94 #endif
96 if (q) {
97 free(q);
98 #ifdef LOGALLOC
99 fprintf(logfp, "%s %d free(%p)\n", file, line, q);
100 #endif
104 #ifdef LOGALLOC
105 char *nasm_strdup_log(char *file, int line, const char *s)
106 #else
107 char *nasm_strdup(const char *s)
108 #endif
110 char *p;
111 int size = strlen(s) + 1;
113 p = malloc(size);
114 if (!p)
115 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
116 #ifdef LOGALLOC
117 else
118 fprintf(logfp, "%s %d strdup(%ld) returns %p\n",
119 file, line, (long)size, p);
120 #endif
121 strcpy(p, s);
122 return p;
125 #ifdef LOGALLOC
126 char *nasm_strndup_log(char *file, int line, char *s, size_t len)
127 #else
128 char *nasm_strndup(char *s, size_t len)
129 #endif
131 char *p;
132 int size = len + 1;
134 p = malloc(size);
135 if (!p)
136 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
137 #ifdef LOGALLOC
138 else
139 fprintf(logfp, "%s %d strndup(%ld) returns %p\n",
140 file, line, (long)size, p);
141 #endif
142 strncpy(p, s, len);
143 p[len] = '\0';
144 return p;
147 #if !defined(stricmp) && !defined(strcasecmp)
148 int nasm_stricmp(const char *s1, const char *s2)
150 while (*s1 && tolower(*s1) == tolower(*s2))
151 s1++, s2++;
152 if (!*s1 && !*s2)
153 return 0;
154 else if (tolower(*s1) < tolower(*s2))
155 return -1;
156 else
157 return 1;
159 #endif
161 #if !defined(strnicmp) && !defined(strncasecmp)
162 int nasm_strnicmp(const char *s1, const char *s2, int n)
164 while (n > 0 && *s1 && tolower(*s1) == tolower(*s2))
165 s1++, s2++, n--;
166 if ((!*s1 && !*s2) || n == 0)
167 return 0;
168 else if (tolower(*s1) < tolower(*s2))
169 return -1;
170 else
171 return 1;
173 #endif
175 #if !defined(strsep)
176 char *nasm_strsep(char **stringp, const char *delim)
178 char *s = *stringp;
179 char *e;
181 if (!s)
182 return NULL;
184 e = strpbrk(s, delim);
185 if (e)
186 *e++ = '\0';
188 *stringp = e;
189 return s;
191 #endif
194 #define lib_isnumchar(c) ( isalnum(c) || (c) == '$')
195 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
197 int64_t readnum(char *str, int *error)
199 char *r = str, *q;
200 int32_t radix;
201 uint64_t result, checklimit;
202 int digit, last;
203 int warn = FALSE;
204 int sign = 1;
206 *error = FALSE;
208 while (isspace(*r))
209 r++; /* find start of number */
212 * If the number came from make_tok_num (as a result of an %assign), it
213 * might have a '-' built into it (rather than in a preceeding token).
215 if (*r == '-') {
216 r++;
217 sign = -1;
220 q = r;
222 while (lib_isnumchar(*q))
223 q++; /* find end of number */
226 * If it begins 0x, 0X or $, or ends in H, it's in hex. if it
227 * ends in Q, it's octal. if it ends in B, it's binary.
228 * Otherwise, it's ordinary decimal.
230 if (*r == '0' && (r[1] == 'x' || r[1] == 'X'))
231 radix = 16, r += 2;
232 else if (*r == '$')
233 radix = 16, r++;
234 else if (q[-1] == 'H' || q[-1] == 'h')
235 radix = 16, q--;
236 else if (q[-1] == 'Q' || q[-1] == 'q' || q[-1] == 'O' || q[-1] == 'o')
237 radix = 8, q--;
238 else if (q[-1] == 'B' || q[-1] == 'b')
239 radix = 2, q--;
240 else
241 radix = 10;
244 * If this number has been found for us by something other than
245 * the ordinary scanners, then it might be malformed by having
246 * nothing between the prefix and the suffix. Check this case
247 * now.
249 if (r >= q) {
250 *error = TRUE;
251 return 0;
255 * `checklimit' must be 2**(32|64) / radix. We can't do that in
256 * 32/64-bit arithmetic, which we're (probably) using, so we
257 * cheat: since we know that all radices we use are even, we
258 * can divide 2**(31|63) by radix/2 instead.
260 if (globalbits == 64)
261 checklimit = 0x8000000000000000ULL / (radix >> 1);
262 else
263 checklimit = 0x80000000UL / (radix >> 1);
266 * Calculate the highest allowable value for the last digit of a
267 * 32-bit constant... in radix 10, it is 6, otherwise it is 0
269 last = (radix == 10 ? 6 : 0);
271 result = 0;
272 while (*r && r < q) {
273 if (*r < '0' || (*r > '9' && *r < 'A')
274 || (digit = numvalue(*r)) >= radix) {
275 *error = TRUE;
276 return 0;
278 if (result > checklimit || (result == checklimit && digit >= last)) {
279 warn = TRUE;
282 result = radix * result + digit;
283 r++;
286 if (warn)
287 nasm_malloc_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
288 "numeric constant %s does not fit in 32 bits",
289 str);
291 return result * sign;
294 int64_t readstrnum(char *str, int length, int *warn)
296 int64_t charconst = 0;
297 int i;
299 *warn = FALSE;
301 str += length;
302 if (globalbits == 64) {
303 for (i = 0; i < length; i++) {
304 if (charconst & 0xFF00000000000000ULL)
305 *warn = TRUE;
306 charconst = (charconst << 8) + (uint8_t)*--str;
308 } else {
309 for (i = 0; i < length; i++) {
310 if (charconst & 0xFF000000UL)
311 *warn = TRUE;
312 charconst = (charconst << 8) + (uint8_t)*--str;
315 return charconst;
318 static int32_t next_seg;
320 void seg_init(void)
322 next_seg = 0;
325 int32_t seg_alloc(void)
327 return (next_seg += 2) - 2;
330 void fwriteint16_t(int data, FILE * fp)
332 fputc((int)(data & 255), fp);
333 fputc((int)((data >> 8) & 255), fp);
336 void fwriteint32_t(int32_t data, FILE * fp)
338 fputc((int)(data & 255), fp);
339 fputc((int)((data >> 8) & 255), fp);
340 fputc((int)((data >> 16) & 255), fp);
341 fputc((int)((data >> 24) & 255), fp);
344 void fwriteint64_t(int64_t data, FILE * fp)
346 fputc((int)(data & 255), fp);
347 fputc((int)((data >> 8) & 255), fp);
348 fputc((int)((data >> 16) & 255), fp);
349 fputc((int)((data >> 24) & 255), fp);
350 fputc((int)((data >> 32) & 255), fp);
351 fputc((int)((data >> 40) & 255), fp);
352 fputc((int)((data >> 48) & 255), fp);
353 fputc((int)((data >> 56) & 255), fp);
356 void standard_extension(char *inname, char *outname, char *extension,
357 efunc error)
359 char *p, *q;
361 if (*outname) /* file name already exists, */
362 return; /* so do nothing */
363 q = inname;
364 p = outname;
365 while (*q)
366 *p++ = *q++; /* copy, and find end of string */
367 *p = '\0'; /* terminate it */
368 while (p > outname && *--p != '.') ; /* find final period (or whatever) */
369 if (*p != '.')
370 while (*p)
371 p++; /* go back to end if none found */
372 if (!strcmp(p, extension)) { /* is the extension already there? */
373 if (*extension)
374 error(ERR_WARNING | ERR_NOFILE,
375 "file name already ends in `%s': "
376 "output will be in `nasm.out'", extension);
377 else
378 error(ERR_WARNING | ERR_NOFILE,
379 "file name already has no extension: "
380 "output will be in `nasm.out'");
381 strcpy(outname, "nasm.out");
382 } else
383 strcpy(p, extension);
386 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
387 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
389 #define LAYERSIZ(r) ( (r)->layers==0 ? RAA_BLKSIZE : RAA_LAYERSIZE )
391 static struct RAA *real_raa_init(int layers)
393 struct RAA *r;
394 int i;
396 if (layers == 0) {
397 r = nasm_zalloc(LEAFSIZ);
398 r->stepsize = 1L;
399 } else {
400 r = nasm_malloc(BRANCHSIZ);
401 r->layers = layers;
402 for (i = 0; i < RAA_LAYERSIZE; i++)
403 r->u.b.data[i] = NULL;
404 r->stepsize = RAA_BLKSIZE;
405 while (--layers)
406 r->stepsize *= RAA_LAYERSIZE;
408 return r;
411 struct RAA *raa_init(void)
413 return real_raa_init(0);
416 void raa_free(struct RAA *r)
418 if (r->layers == 0)
419 nasm_free(r);
420 else {
421 struct RAA **p;
422 for (p = r->u.b.data; p - r->u.b.data < RAA_LAYERSIZE; p++)
423 if (*p)
424 raa_free(*p);
428 int32_t raa_read(struct RAA *r, int32_t posn)
430 if (posn >= r->stepsize * LAYERSIZ(r))
431 return 0; /* Return 0 for undefined entries */
432 while (r->layers > 0) {
433 ldiv_t l;
434 l = ldiv(posn, r->stepsize);
435 r = r->u.b.data[l.quot];
436 posn = l.rem;
437 if (!r)
438 return 0; /* Return 0 for undefined entries */
440 return r->u.l.data[posn];
443 struct RAA *raa_write(struct RAA *r, int32_t posn, int32_t value)
445 struct RAA *result;
447 if (posn < 0)
448 nasm_malloc_error(ERR_PANIC, "negative position in raa_write");
450 while (r->stepsize * LAYERSIZ(r) <= posn) {
452 * Must add a layer.
454 struct RAA *s;
455 int i;
457 s = nasm_malloc(BRANCHSIZ);
458 for (i = 0; i < RAA_LAYERSIZE; i++)
459 s->u.b.data[i] = NULL;
460 s->layers = r->layers + 1;
461 s->stepsize = LAYERSIZ(r) * r->stepsize;
462 s->u.b.data[0] = r;
463 r = s;
466 result = r;
468 while (r->layers > 0) {
469 ldiv_t l;
470 struct RAA **s;
471 l = ldiv(posn, r->stepsize);
472 s = &r->u.b.data[l.quot];
473 if (!*s)
474 *s = real_raa_init(r->layers - 1);
475 r = *s;
476 posn = l.rem;
479 r->u.l.data[posn] = value;
481 return result;
484 #define SAA_MAXLEN 8192
486 struct SAA *saa_init(int32_t elem_len)
488 struct SAA *s;
490 if (elem_len > SAA_MAXLEN)
491 nasm_malloc_error(ERR_PANIC | ERR_NOFILE,
492 "SAA with huge elements");
494 s = nasm_malloc(sizeof(struct SAA));
495 s->posn = s->start = 0L;
496 s->elem_len = elem_len;
497 s->length = SAA_MAXLEN - (SAA_MAXLEN % elem_len);
498 s->data = nasm_malloc(s->length);
499 s->next = NULL;
500 s->end = s;
502 return s;
505 void saa_free(struct SAA *s)
507 struct SAA *t;
509 while (s) {
510 t = s->next;
511 nasm_free(s->data);
512 nasm_free(s);
513 s = t;
517 void *saa_wstruct(struct SAA *s)
519 void *p;
521 if (s->end->length - s->end->posn < s->elem_len) {
522 s->end->next = nasm_malloc(sizeof(struct SAA));
523 s->end->next->start = s->end->start + s->end->posn;
524 s->end = s->end->next;
525 s->end->length = s->length;
526 s->end->next = NULL;
527 s->end->posn = 0L;
528 s->end->data = nasm_malloc(s->length);
531 p = s->end->data + s->end->posn;
532 s->end->posn += s->elem_len;
533 return p;
536 void saa_wbytes(struct SAA *s, const void *data, int32_t len)
538 const char *d = data;
540 while (len > 0) {
541 int32_t l = s->end->length - s->end->posn;
542 if (l > len)
543 l = len;
544 if (l > 0) {
545 if (d) {
546 memcpy(s->end->data + s->end->posn, d, l);
547 d += l;
548 } else
549 memset(s->end->data + s->end->posn, 0, l);
550 s->end->posn += l;
551 len -= l;
553 if (len > 0) {
554 s->end->next = nasm_malloc(sizeof(struct SAA));
555 s->end->next->start = s->end->start + s->end->posn;
556 s->end = s->end->next;
557 s->end->length = s->length;
558 s->end->next = NULL;
559 s->end->posn = 0L;
560 s->end->data = nasm_malloc(s->length);
565 void saa_rewind(struct SAA *s)
567 s->rptr = s;
568 s->rpos = 0L;
571 void *saa_rstruct(struct SAA *s)
573 void *p;
575 if (!s->rptr)
576 return NULL;
578 if (s->rptr->posn - s->rpos < s->elem_len) {
579 s->rptr = s->rptr->next;
580 if (!s->rptr)
581 return NULL; /* end of array */
582 s->rpos = 0L;
585 p = s->rptr->data + s->rpos;
586 s->rpos += s->elem_len;
587 return p;
590 void *saa_rbytes(struct SAA *s, int32_t *len)
592 void *p;
594 if (!s->rptr)
595 return NULL;
597 p = s->rptr->data + s->rpos;
598 *len = s->rptr->posn - s->rpos;
599 s->rptr = s->rptr->next;
600 s->rpos = 0L;
601 return p;
604 void saa_rnbytes(struct SAA *s, void *data, int32_t len)
606 char *d = data;
608 while (len > 0) {
609 int32_t l;
611 if (!s->rptr)
612 return;
614 l = s->rptr->posn - s->rpos;
615 if (l > len)
616 l = len;
617 if (l > 0) {
618 memcpy(d, s->rptr->data + s->rpos, l);
619 d += l;
620 s->rpos += l;
621 len -= l;
623 if (len > 0) {
624 s->rptr = s->rptr->next;
625 s->rpos = 0L;
630 void saa_fread(struct SAA *s, int32_t posn, void *data, int32_t len)
632 struct SAA *p;
633 int64_t pos;
634 char *cdata = data;
636 if (!s->rptr || posn < s->rptr->start)
637 saa_rewind(s);
638 p = s->rptr;
639 while (posn >= p->start + p->posn) {
640 p = p->next;
641 if (!p)
642 return; /* what else can we do?! */
645 pos = posn - p->start;
646 while (len) {
647 int64_t l = p->posn - pos;
648 if (l > len)
649 l = len;
650 memcpy(cdata, p->data + pos, l);
651 len -= l;
652 cdata += l;
653 p = p->next;
654 if (!p)
655 return;
656 pos = 0LL;
658 s->rptr = p;
661 void saa_fwrite(struct SAA *s, int32_t posn, void *data, int32_t len)
663 struct SAA *p;
664 int64_t pos;
665 char *cdata = data;
667 if (!s->rptr || posn < s->rptr->start)
668 saa_rewind(s);
669 p = s->rptr;
670 while (posn >= p->start + p->posn) {
671 p = p->next;
672 if (!p)
673 return; /* what else can we do?! */
676 pos = posn - p->start;
677 while (len) {
678 int64_t l = p->posn - pos;
679 if (l > len)
680 l = len;
681 memcpy(p->data + pos, cdata, l);
682 len -= l;
683 cdata += l;
684 p = p->next;
685 if (!p)
686 return;
687 pos = 0LL;
689 s->rptr = p;
692 void saa_fpwrite(struct SAA *s, FILE * fp)
694 char *data;
695 int32_t len;
697 saa_rewind(s);
698 // while ((data = saa_rbytes(s, &len)))
699 for (; (data = saa_rbytes(s, &len));)
700 fwrite(data, 1, len, fp);
704 * Common list of prefix names
706 static const char *prefix_names[] = {
707 "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
708 "repnz", "repz", "times"
711 const char *prefix_name(int token)
713 unsigned int prefix = token-PREFIX_ENUM_START;
714 if (prefix > sizeof prefix_names / sizeof(const char *))
715 return NULL;
717 return prefix_names[prefix];
721 * Binary search.
723 int bsi(char *string, const char **array, int size)
725 int i = -1, j = size; /* always, i < index < j */
726 while (j - i >= 2) {
727 int k = (i + j) / 2;
728 int l = strcmp(string, array[k]);
729 if (l < 0) /* it's in the first half */
730 j = k;
731 else if (l > 0) /* it's in the second half */
732 i = k;
733 else /* we've got it :) */
734 return k;
736 return -1; /* we haven't got it :( */
739 int bsii(char *string, const char **array, int size)
741 int i = -1, j = size; /* always, i < index < j */
742 while (j - i >= 2) {
743 int k = (i + j) / 2;
744 int l = nasm_stricmp(string, array[k]);
745 if (l < 0) /* it's in the first half */
746 j = k;
747 else if (l > 0) /* it's in the second half */
748 i = k;
749 else /* we've got it :) */
750 return k;
752 return -1; /* we haven't got it :( */
755 static char *file_name = NULL;
756 static int32_t line_number = 0;
758 char *src_set_fname(char *newname)
760 char *oldname = file_name;
761 file_name = newname;
762 return oldname;
765 int32_t src_set_linnum(int32_t newline)
767 int32_t oldline = line_number;
768 line_number = newline;
769 return oldline;
772 int32_t src_get_linnum(void)
774 return line_number;
777 int src_get(int32_t *xline, char **xname)
779 if (!file_name || !*xname || strcmp(*xname, file_name)) {
780 nasm_free(*xname);
781 *xname = file_name ? nasm_strdup(file_name) : NULL;
782 *xline = line_number;
783 return -2;
785 if (*xline != line_number) {
786 int32_t tmp = line_number - *xline;
787 *xline = line_number;
788 return tmp;
790 return 0;
793 void nasm_quote(char **str)
795 int ln = strlen(*str);
796 char q = (*str)[0];
797 char *p;
798 if (ln > 1 && (*str)[ln - 1] == q && (q == '"' || q == '\''))
799 return;
800 q = '"';
801 if (strchr(*str, q))
802 q = '\'';
803 p = nasm_malloc(ln + 3);
804 strcpy(p + 1, *str);
805 nasm_free(*str);
806 p[ln + 1] = p[0] = q;
807 p[ln + 2] = 0;
808 *str = p;
811 char *nasm_strcat(char *one, char *two)
813 char *rslt;
814 int l1 = strlen(one);
815 rslt = nasm_malloc(l1 + strlen(two) + 1);
816 strcpy(rslt, one);
817 strcpy(rslt + l1, two);
818 return rslt;
821 void null_debug_init(struct ofmt *of, void *id, FILE * fp, efunc error)
823 (void)of;
824 (void)id;
825 (void)fp;
826 (void)error;
828 void null_debug_linenum(const char *filename, int32_t linenumber, int32_t segto)
830 (void)filename;
831 (void)linenumber;
832 (void)segto;
834 void null_debug_deflabel(char *name, int32_t segment, int32_t offset,
835 int is_global, char *special)
837 (void)name;
838 (void)segment;
839 (void)offset;
840 (void)is_global;
841 (void)special;
843 void null_debug_routine(const char *directive, const char *params)
845 (void)directive;
846 (void)params;
848 void null_debug_typevalue(int32_t type)
850 (void)type;
852 void null_debug_output(int type, void *param)
854 (void)type;
855 (void)param;
857 void null_debug_cleanup(void)
861 struct dfmt null_debug_form = {
862 "Null debug format",
863 "null",
864 null_debug_init,
865 null_debug_linenum,
866 null_debug_deflabel,
867 null_debug_routine,
868 null_debug_typevalue,
869 null_debug_output,
870 null_debug_cleanup
873 struct dfmt *null_debug_arr[2] = { &null_debug_form, NULL };