Fix FISTTP opcodes (BR 689695)
[nasm.git] / nasmlib.c
blob8c81d6c02a2838fda3e6c0c1b9facaac7ca3b01a
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 "compiler.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include <inttypes.h>
17 #include "nasm.h"
18 #include "nasmlib.h"
19 #include "insns.h"
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 */
24 #ifdef LOGALLOC
25 static FILE *logfp;
26 #endif
28 void nasm_set_malloc_error(efunc error)
30 nasm_malloc_error = error;
31 #ifdef LOGALLOC
32 logfp = fopen("malloc.log", "w");
33 setvbuf(logfp, NULL, _IOLBF, BUFSIZ);
34 fprintf(logfp, "null pointer is %p\n", NULL);
35 #endif
38 #ifdef LOGALLOC
39 void *nasm_malloc_log(char *file, int line, size_t size)
40 #else
41 void *nasm_malloc(size_t size)
42 #endif
44 void *p = malloc(size);
45 if (!p)
46 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
47 #ifdef LOGALLOC
48 else
49 fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
50 file, line, (long)size, p);
51 #endif
52 return p;
55 #ifdef LOGALLOC
56 void *nasm_zalloc_log(char *file, int line, size_t size)
57 #else
58 void *nasm_zalloc(size_t size)
59 #endif
61 void *p = calloc(size, 1);
62 if (!p)
63 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
64 #ifdef LOGALLOC
65 else
66 fprintf(logfp, "%s %d calloc(%ld, 1) returns %p\n",
67 file, line, (long)size, p);
68 #endif
69 return p;
72 #ifdef LOGALLOC
73 void *nasm_realloc_log(char *file, int line, void *q, size_t size)
74 #else
75 void *nasm_realloc(void *q, size_t size)
76 #endif
78 void *p = q ? realloc(q, size) : malloc(size);
79 if (!p)
80 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
81 #ifdef LOGALLOC
82 else if (q)
83 fprintf(logfp, "%s %d realloc(%p,%ld) returns %p\n",
84 file, line, q, (long)size, p);
85 else
86 fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
87 file, line, (long)size, p);
88 #endif
89 return p;
92 #ifdef LOGALLOC
93 void nasm_free_log(char *file, int line, void *q)
94 #else
95 void nasm_free(void *q)
96 #endif
98 if (q) {
99 free(q);
100 #ifdef LOGALLOC
101 fprintf(logfp, "%s %d free(%p)\n", file, line, q);
102 #endif
106 #ifdef LOGALLOC
107 char *nasm_strdup_log(char *file, int line, const char *s)
108 #else
109 char *nasm_strdup(const char *s)
110 #endif
112 char *p;
113 int size = strlen(s) + 1;
115 p = malloc(size);
116 if (!p)
117 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
118 #ifdef LOGALLOC
119 else
120 fprintf(logfp, "%s %d strdup(%ld) returns %p\n",
121 file, line, (long)size, p);
122 #endif
123 strcpy(p, s);
124 return p;
127 #ifdef LOGALLOC
128 char *nasm_strndup_log(char *file, int line, char *s, size_t len)
129 #else
130 char *nasm_strndup(char *s, size_t len)
131 #endif
133 char *p;
134 int size = len + 1;
136 p = malloc(size);
137 if (!p)
138 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
139 #ifdef LOGALLOC
140 else
141 fprintf(logfp, "%s %d strndup(%ld) returns %p\n",
142 file, line, (long)size, p);
143 #endif
144 strncpy(p, s, len);
145 p[len] = '\0';
146 return p;
149 #ifndef nasm_stricmp
150 int nasm_stricmp(const char *s1, const char *s2)
152 while (*s1 && tolower(*s1) == tolower(*s2))
153 s1++, s2++;
154 if (!*s1 && !*s2)
155 return 0;
156 else if (tolower(*s1) < tolower(*s2))
157 return -1;
158 else
159 return 1;
161 #endif
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))
167 s1++, s2++, n--;
168 if ((!*s1 && !*s2) || n == 0)
169 return 0;
170 else if (tolower(*s1) < tolower(*s2))
171 return -1;
172 else
173 return 1;
175 #endif
177 #ifndef nasm_strsep
178 char *nasm_strsep(char **stringp, const char *delim)
180 char *s = *stringp;
181 char *e;
183 if (!s)
184 return NULL;
186 e = strpbrk(s, delim);
187 if (e)
188 *e++ = '\0';
190 *stringp = e;
191 return s;
193 #endif
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, bool *error)
201 char *r = str, *q;
202 int32_t radix;
203 uint64_t result, checklimit;
204 int digit, last;
205 bool warn = false;
206 int sign = 1;
208 *error = false;
210 while (isspace(*r))
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).
217 if (*r == '-') {
218 r++;
219 sign = -1;
222 q = r;
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'))
233 radix = 16, r += 2;
234 else if (*r == '$')
235 radix = 16, r++;
236 else if (q[-1] == 'H' || q[-1] == 'h')
237 radix = 16, q--;
238 else if (q[-1] == 'Q' || q[-1] == 'q' || q[-1] == 'O' || q[-1] == 'o')
239 radix = 8, q--;
240 else if (q[-1] == 'B' || q[-1] == 'b')
241 radix = 2, q--;
242 else
243 radix = 10;
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
249 * now.
251 if (r >= q) {
252 *error = true;
253 return 0;
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);
264 else
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);
273 result = 0;
274 while (*r && r < q) {
275 if (*r < '0' || (*r > '9' && *r < 'A')
276 || (digit = numvalue(*r)) >= radix) {
277 *error = true;
278 return 0;
280 if (result > checklimit || (result == checklimit && digit >= last)) {
281 warn = true;
284 result = radix * result + digit;
285 r++;
288 if (warn)
289 nasm_malloc_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
290 "numeric constant %s does not fit in 32 bits",
291 str);
293 return result * sign;
296 int64_t readstrnum(char *str, int length, bool *warn)
298 int64_t charconst = 0;
299 int i;
301 *warn = false;
303 str += length;
304 if (globalbits == 64) {
305 for (i = 0; i < length; i++) {
306 if (charconst & 0xFF00000000000000ULL)
307 *warn = true;
308 charconst = (charconst << 8) + (uint8_t)*--str;
310 } else {
311 for (i = 0; i < length; i++) {
312 if (charconst & 0xFF000000UL)
313 *warn = true;
314 charconst = (charconst << 8) + (uint8_t)*--str;
317 return charconst;
320 static int32_t next_seg;
322 void seg_init(void)
324 next_seg = 0;
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,
359 efunc error)
361 char *p, *q;
363 if (*outname) /* file name already exists, */
364 return; /* so do nothing */
365 q = inname;
366 p = outname;
367 while (*q)
368 *p++ = *q++; /* copy, and find end of string */
369 *p = '\0'; /* terminate it */
370 while (p > outname && *--p != '.') ; /* find final period (or whatever) */
371 if (*p != '.')
372 while (*p)
373 p++; /* go back to end if none found */
374 if (!strcmp(p, extension)) { /* is the extension already there? */
375 if (*extension)
376 error(ERR_WARNING | ERR_NOFILE,
377 "file name already ends in `%s': "
378 "output will be in `nasm.out'", extension);
379 else
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");
384 } else
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)
395 struct RAA *r;
396 int i;
398 if (layers == 0) {
399 r = nasm_zalloc(LEAFSIZ);
400 r->stepsize = 1L;
401 } else {
402 r = nasm_malloc(BRANCHSIZ);
403 r->layers = layers;
404 for (i = 0; i < RAA_LAYERSIZE; i++)
405 r->u.b.data[i] = NULL;
406 r->stepsize = RAA_BLKSIZE;
407 while (--layers)
408 r->stepsize *= RAA_LAYERSIZE;
410 return r;
413 struct RAA *raa_init(void)
415 return real_raa_init(0);
418 void raa_free(struct RAA *r)
420 if (r->layers == 0)
421 nasm_free(r);
422 else {
423 struct RAA **p;
424 for (p = r->u.b.data; p - r->u.b.data < RAA_LAYERSIZE; p++)
425 if (*p)
426 raa_free(*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) {
435 ldiv_t l;
436 l = ldiv(posn, r->stepsize);
437 r = r->u.b.data[l.quot];
438 posn = l.rem;
439 if (!r)
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)
447 struct RAA *result;
449 if (posn < 0)
450 nasm_malloc_error(ERR_PANIC, "negative position in raa_write");
452 while (r->stepsize * LAYERSIZ(r) <= posn) {
454 * Must add a layer.
456 struct RAA *s;
457 int i;
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;
464 s->u.b.data[0] = r;
465 r = s;
468 result = r;
470 while (r->layers > 0) {
471 ldiv_t l;
472 struct RAA **s;
473 l = ldiv(posn, r->stepsize);
474 s = &r->u.b.data[l.quot];
475 if (!*s)
476 *s = real_raa_init(r->layers - 1);
477 r = *s;
478 posn = l.rem;
481 r->u.l.data[posn] = value;
483 return result;
486 /* Aggregate SAA components smaller than this */
487 #define SAA_BLKLEN 65536
489 struct SAA *saa_init(size_t elem_len)
491 struct SAA *s;
492 char *data;
494 s = nasm_zalloc(sizeof(struct SAA));
496 if (elem_len >= SAA_BLKLEN)
497 s->blk_len = elem_len;
498 else
499 s->blk_len = SAA_BLKLEN - (SAA_BLKLEN % elem_len);
501 s->elem_len = elem_len;
502 s->length = s->blk_len;
503 data = nasm_malloc(s->blk_len);
504 s->nblkptrs = s->nblks = 1;
505 s->blk_ptrs = nasm_malloc(sizeof(char *));
506 s->blk_ptrs[0] = data;
507 s->wblk = s->rblk = &s->blk_ptrs[0];
509 return s;
512 void saa_free(struct SAA *s)
514 char **p;
515 size_t n;
517 for (p = s->blk_ptrs, n = s->nblks; n; p++, n--)
518 nasm_free(*p);
520 nasm_free(s->blk_ptrs);
521 nasm_free(s);
524 /* Add one allocation block to an SAA */
525 static void saa_extend(struct SAA *s)
527 size_t blkn = s->nblks++;
529 if (blkn >= s->nblkptrs) {
530 size_t rindex = s->rblk - s->blk_ptrs;
531 size_t windex = s->wblk - s->blk_ptrs;
533 s->nblkptrs <<= 1;
534 s->blk_ptrs = nasm_realloc(s->blk_ptrs, s->nblkptrs*sizeof(char *));
536 s->rblk = s->blk_ptrs + rindex;
537 s->wblk = s->blk_ptrs + windex;
540 s->blk_ptrs[blkn] = nasm_malloc(s->blk_len);
541 s->length += s->blk_len;
544 void *saa_wstruct(struct SAA *s)
546 void *p;
548 if (s->wpos % s->elem_len)
549 nasm_malloc_error(ERR_PANIC|ERR_NOFILE,
550 "misaligned wpos in saa_wstruct");
552 if (s->wpos + s->elem_len > s->blk_len) {
553 if (s->wpos != s->blk_len)
554 nasm_malloc_error(ERR_PANIC|ERR_NOFILE,
555 "unfilled block in saa_wstruct");
557 if (s->wptr + s->elem_len > s->length)
558 saa_extend(s);
559 s->wblk++;
560 s->wpos = 0;
563 p = *s->wblk + s->wpos;
564 s->wpos += s->elem_len;
565 s->wptr += s->elem_len;
567 if (s->wptr > s->datalen)
568 s->datalen = s->wptr;
570 return p;
573 void saa_wbytes(struct SAA *s, const void *data, size_t len)
575 const char *d = data;
577 while (len) {
578 size_t l = s->blk_len - s->wpos;
579 if (l > len)
580 l = len;
581 if (l) {
582 if (d) {
583 memcpy(*s->wblk + s->wpos, d, l);
584 d += l;
585 } else
586 memset(*s->wblk + s->wpos, 0, l);
587 s->wpos += l;
588 s->wptr += l;
589 len -= l;
591 if (s->datalen < s->wptr)
592 s->datalen = s->wptr;
594 if (len) {
595 if (s->wptr >= s->length)
596 saa_extend(s);
597 s->wblk++;
598 s->wpos = 0;
603 void saa_rewind(struct SAA *s)
605 s->rblk = s->blk_ptrs;
606 s->rpos = s->rptr = 0;
609 void *saa_rstruct(struct SAA *s)
611 void *p;
613 if (s->rptr + s->elem_len > s->datalen)
614 return NULL;
616 if (s->rpos % s->elem_len)
617 nasm_malloc_error(ERR_PANIC|ERR_NOFILE,
618 "misaligned rpos in saa_rstruct");
620 if (s->rpos + s->elem_len > s->blk_len) {
621 s->rblk++;
622 s->rpos = 0;
625 p = *s->rblk + s->rpos;
626 s->rpos += s->elem_len;
627 s->rptr += s->elem_len;
629 return p;
632 const void *saa_rbytes(struct SAA *s, size_t *lenp)
634 const void *p;
635 size_t len;
637 if (s->rptr >= s->datalen) {
638 *lenp = 0;
639 return NULL;
642 if (s->rpos >= s->blk_len) {
643 s->rblk++;
644 s->rpos = 0;
647 len = *lenp;
648 if (len > s->datalen - s->rptr)
649 len = s->datalen - s->rptr;
650 if (len > s->blk_len - s->rpos)
651 len = s->blk_len - s->rpos;
653 *lenp = len;
654 p = *s->rblk + s->rpos;
656 s->rpos += len;
657 s->rptr += len;
659 return p;
662 void saa_rnbytes(struct SAA *s, void *data, size_t len)
664 char *d = data;
666 if (s->rptr + len > s->datalen) {
667 nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_rnbytes");
668 return;
671 while (len) {
672 size_t l;
673 const void *p;
675 l = len;
676 p = saa_rbytes(s, &l);
678 memcpy(d, p, l);
679 d += l;
680 len -= l;
684 /* Same as saa_rnbytes, except position the counter first */
685 void saa_fread(struct SAA *s, size_t posn, void *data, size_t len)
687 size_t ix;
689 if (posn+len > s->datalen) {
690 nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_fread");
691 return;
694 ix = posn / s->blk_len;
695 s->rptr = posn;
696 s->rpos = posn % s->blk_len;
697 s->rblk = &s->blk_ptrs[ix];
699 saa_rnbytes(s, data, len);
702 /* Same as saa_wbytes, except position the counter first */
703 void saa_fwrite(struct SAA *s, size_t posn, const void *data, size_t len)
705 size_t ix;
707 if (posn > s->datalen) {
708 /* Seek beyond the end of the existing array not supported */
709 nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_fwrite");
710 return;
713 ix = posn / s->blk_len;
714 s->wptr = posn;
715 s->wpos = posn % s->blk_len;
716 s->wblk = &s->blk_ptrs[ix];
718 if (!s->wpos) {
719 s->wpos = s->blk_len;
720 s->wblk--;
723 saa_wbytes(s, data, len);
726 void saa_fpwrite(struct SAA *s, FILE * fp)
728 const char *data;
729 size_t len;
731 saa_rewind(s);
732 while (len = s->datalen, (data = saa_rbytes(s, &len)) != NULL)
733 fwrite(data, 1, len, fp);
737 * Common list of prefix names
739 static const char *prefix_names[] = {
740 "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
741 "repnz", "repz", "times"
744 const char *prefix_name(int token)
746 unsigned int prefix = token-PREFIX_ENUM_START;
747 if (prefix > sizeof prefix_names / sizeof(const char *))
748 return NULL;
750 return prefix_names[prefix];
754 * Binary search.
756 int bsi(char *string, const char **array, int size)
758 int i = -1, j = size; /* always, i < index < j */
759 while (j - i >= 2) {
760 int k = (i + j) / 2;
761 int l = strcmp(string, array[k]);
762 if (l < 0) /* it's in the first half */
763 j = k;
764 else if (l > 0) /* it's in the second half */
765 i = k;
766 else /* we've got it :) */
767 return k;
769 return -1; /* we haven't got it :( */
772 int bsii(char *string, const char **array, int size)
774 int i = -1, j = size; /* always, i < index < j */
775 while (j - i >= 2) {
776 int k = (i + j) / 2;
777 int l = nasm_stricmp(string, array[k]);
778 if (l < 0) /* it's in the first half */
779 j = k;
780 else if (l > 0) /* it's in the second half */
781 i = k;
782 else /* we've got it :) */
783 return k;
785 return -1; /* we haven't got it :( */
788 static char *file_name = NULL;
789 static int32_t line_number = 0;
791 char *src_set_fname(char *newname)
793 char *oldname = file_name;
794 file_name = newname;
795 return oldname;
798 int32_t src_set_linnum(int32_t newline)
800 int32_t oldline = line_number;
801 line_number = newline;
802 return oldline;
805 int32_t src_get_linnum(void)
807 return line_number;
810 int src_get(int32_t *xline, char **xname)
812 if (!file_name || !*xname || strcmp(*xname, file_name)) {
813 nasm_free(*xname);
814 *xname = file_name ? nasm_strdup(file_name) : NULL;
815 *xline = line_number;
816 return -2;
818 if (*xline != line_number) {
819 int32_t tmp = line_number - *xline;
820 *xline = line_number;
821 return tmp;
823 return 0;
826 void nasm_quote(char **str)
828 int ln = strlen(*str);
829 char q = (*str)[0];
830 char *p;
831 if (ln > 1 && (*str)[ln - 1] == q && (q == '"' || q == '\''))
832 return;
833 q = '"';
834 if (strchr(*str, q))
835 q = '\'';
836 p = nasm_malloc(ln + 3);
837 strcpy(p + 1, *str);
838 nasm_free(*str);
839 p[ln + 1] = p[0] = q;
840 p[ln + 2] = 0;
841 *str = p;
844 char *nasm_strcat(char *one, char *two)
846 char *rslt;
847 int l1 = strlen(one);
848 rslt = nasm_malloc(l1 + strlen(two) + 1);
849 strcpy(rslt, one);
850 strcpy(rslt + l1, two);
851 return rslt;
854 void null_debug_init(struct ofmt *of, void *id, FILE * fp, efunc error)
856 (void)of;
857 (void)id;
858 (void)fp;
859 (void)error;
861 void null_debug_linenum(const char *filename, int32_t linenumber, int32_t segto)
863 (void)filename;
864 (void)linenumber;
865 (void)segto;
867 void null_debug_deflabel(char *name, int32_t segment, int32_t offset,
868 int is_global, char *special)
870 (void)name;
871 (void)segment;
872 (void)offset;
873 (void)is_global;
874 (void)special;
876 void null_debug_routine(const char *directive, const char *params)
878 (void)directive;
879 (void)params;
881 void null_debug_typevalue(int32_t type)
883 (void)type;
885 void null_debug_output(int type, void *param)
887 (void)type;
888 (void)param;
890 void null_debug_cleanup(void)
894 struct dfmt null_debug_form = {
895 "Null debug format",
896 "null",
897 null_debug_init,
898 null_debug_linenum,
899 null_debug_deflabel,
900 null_debug_routine,
901 null_debug_typevalue,
902 null_debug_output,
903 null_debug_cleanup
906 struct dfmt *null_debug_arr[2] = { &null_debug_form, NULL };