fix outobj.c bug - every 256th extern crashed nasm
[nasm/autotest.git] / nasmlib.c
blob535ade93d074d3818c52f95a83b64c264f439374
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>
14 #include "nasm.h"
15 #include "nasmlib.h"
16 #include "insns.h" /* For MAX_KEYWORD */
18 static efunc nasm_malloc_error;
20 #ifdef LOGALLOC
21 static FILE *logfp;
22 #endif
24 void nasm_set_malloc_error(efunc error)
26 nasm_malloc_error = error;
27 #ifdef LOGALLOC
28 logfp = fopen("malloc.log", "w");
29 setvbuf(logfp, NULL, _IOLBF, BUFSIZ);
30 fprintf(logfp, "null pointer is %p\n", NULL);
31 #endif
34 #ifdef LOGALLOC
35 void *nasm_malloc_log(char *file, int line, size_t size)
36 #else
37 void *nasm_malloc(size_t size)
38 #endif
40 void *p = malloc(size);
41 if (!p)
42 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
43 #ifdef LOGALLOC
44 else
45 fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
46 file, line, (long)size, p);
47 #endif
48 return p;
51 #ifdef LOGALLOC
52 void *nasm_realloc_log(char *file, int line, void *q, size_t size)
53 #else
54 void *nasm_realloc(void *q, size_t size)
55 #endif
57 void *p = q ? realloc(q, size) : malloc(size);
58 if (!p)
59 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
60 #ifdef LOGALLOC
61 else if (q)
62 fprintf(logfp, "%s %d realloc(%p,%ld) returns %p\n",
63 file, line, q, (long)size, p);
64 else
65 fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
66 file, line, (long)size, p);
67 #endif
68 return p;
71 #ifdef LOGALLOC
72 void nasm_free_log(char *file, int line, void *q)
73 #else
74 void nasm_free(void *q)
75 #endif
77 if (q) {
78 free(q);
79 #ifdef LOGALLOC
80 fprintf(logfp, "%s %d free(%p)\n", file, line, q);
81 #endif
85 #ifdef LOGALLOC
86 char *nasm_strdup_log(char *file, int line, const char *s)
87 #else
88 char *nasm_strdup(const char *s)
89 #endif
91 char *p;
92 int size = strlen(s) + 1;
94 p = malloc(size);
95 if (!p)
96 nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
97 #ifdef LOGALLOC
98 else
99 fprintf(logfp, "%s %d strdup(%ld) returns %p\n",
100 file, line, (long)size, p);
101 #endif
102 strcpy(p, s);
103 return p;
106 #ifdef LOGALLOC
107 char *nasm_strndup_log(char *file, int line, char *s, size_t len)
108 #else
109 char *nasm_strndup(char *s, size_t len)
110 #endif
112 char *p;
113 int size = len + 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 strndup(%ld) returns %p\n",
121 file, line, (long)size, p);
122 #endif
123 strncpy(p, s, len);
124 p[len] = '\0';
125 return p;
128 #if !defined(stricmp) && !defined(strcasecmp)
129 int nasm_stricmp(const char *s1, const char *s2)
131 while (*s1 && tolower(*s1) == tolower(*s2))
132 s1++, s2++;
133 if (!*s1 && !*s2)
134 return 0;
135 else if (tolower(*s1) < tolower(*s2))
136 return -1;
137 else
138 return 1;
140 #endif
142 #if !defined(strnicmp) && !defined(strncasecmp)
143 int nasm_strnicmp(const char *s1, const char *s2, int n)
145 while (n > 0 && *s1 && tolower(*s1) == tolower(*s2))
146 s1++, s2++, n--;
147 if ((!*s1 && !*s2) || n == 0)
148 return 0;
149 else if (tolower(*s1) < tolower(*s2))
150 return -1;
151 else
152 return 1;
154 #endif
156 #define lib_isnumchar(c) ( isalnum(c) || (c) == '$')
157 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
159 long readnum(char *str, int *error)
161 char *r = str, *q;
162 long radix;
163 unsigned long result, checklimit;
164 int digit, last;
165 int warn = FALSE;
166 int sign = 1;
168 *error = FALSE;
170 while (isspace(*r))
171 r++; /* find start of number */
174 * If the number came from make_tok_num (as a result of an %assign), it
175 * might have a '-' built into it (rather than in a preceeding token).
177 if (*r == '-') {
178 r++;
179 sign = -1;
182 q = r;
184 while (lib_isnumchar(*q))
185 q++; /* find end of number */
188 * If it begins 0x, 0X or $, or ends in H, it's in hex. if it
189 * ends in Q, it's octal. if it ends in B, it's binary.
190 * Otherwise, it's ordinary decimal.
192 if (*r == '0' && (r[1] == 'x' || r[1] == 'X'))
193 radix = 16, r += 2;
194 else if (*r == '$')
195 radix = 16, r++;
196 else if (q[-1] == 'H' || q[-1] == 'h')
197 radix = 16, q--;
198 else if (q[-1] == 'Q' || q[-1] == 'q' || q[-1] == 'O' || q[-1] == 'o')
199 radix = 8, q--;
200 else if (q[-1] == 'B' || q[-1] == 'b')
201 radix = 2, q--;
202 else
203 radix = 10;
206 * If this number has been found for us by something other than
207 * the ordinary scanners, then it might be malformed by having
208 * nothing between the prefix and the suffix. Check this case
209 * now.
211 if (r >= q) {
212 *error = TRUE;
213 return 0;
217 * `checklimit' must be 2**32 / radix. We can't do that in
218 * 32-bit arithmetic, which we're (probably) using, so we
219 * cheat: since we know that all radices we use are even, we
220 * can divide 2**31 by radix/2 instead.
222 checklimit = 0x80000000UL / (radix >> 1);
225 * Calculate the highest allowable value for the last digit
226 * of a 32 bit constant... in radix 10, it is 6, otherwise it is 0
228 last = (radix == 10 ? 6 : 0);
230 result = 0;
231 while (*r && r < q) {
232 if (*r < '0' || (*r > '9' && *r < 'A')
233 || (digit = numvalue(*r)) >= radix) {
234 *error = TRUE;
235 return 0;
237 if (result > checklimit || (result == checklimit && digit >= last)) {
238 warn = TRUE;
241 result = radix * result + digit;
242 r++;
245 if (warn)
246 nasm_malloc_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
247 "numeric constant %s does not fit in 32 bits",
248 str);
250 return result * sign;
253 long readstrnum(char *str, int length, int *warn)
255 long charconst = 0;
256 int i;
258 *warn = FALSE;
260 str += length;
261 for (i = 0; i < length; i++) {
262 if (charconst & 0xff000000UL) {
263 *warn = TRUE;
265 charconst = (charconst << 8) + (unsigned char)*--str;
267 return charconst;
270 static long next_seg;
272 void seg_init(void)
274 next_seg = 0;
277 long seg_alloc(void)
279 return (next_seg += 2) - 2;
282 void fwriteshort(int data, FILE * fp)
284 fputc((int)(data & 255), fp);
285 fputc((int)((data >> 8) & 255), fp);
288 void fwritelong(long data, FILE * fp)
290 fputc((int)(data & 255), fp);
291 fputc((int)((data >> 8) & 255), fp);
292 fputc((int)((data >> 16) & 255), fp);
293 fputc((int)((data >> 24) & 255), fp);
296 void standard_extension(char *inname, char *outname, char *extension,
297 efunc error)
299 char *p, *q;
301 if (*outname) /* file name already exists, */
302 return; /* so do nothing */
303 q = inname;
304 p = outname;
305 while (*q)
306 *p++ = *q++; /* copy, and find end of string */
307 *p = '\0'; /* terminate it */
308 while (p > outname && *--p != '.') ; /* find final period (or whatever) */
309 if (*p != '.')
310 while (*p)
311 p++; /* go back to end if none found */
312 if (!strcmp(p, extension)) { /* is the extension already there? */
313 if (*extension)
314 error(ERR_WARNING | ERR_NOFILE,
315 "file name already ends in `%s': "
316 "output will be in `nasm.out'", extension);
317 else
318 error(ERR_WARNING | ERR_NOFILE,
319 "file name already has no extension: "
320 "output will be in `nasm.out'");
321 strcpy(outname, "nasm.out");
322 } else
323 strcpy(p, extension);
326 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
327 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
329 #define LAYERSIZ(r) ( (r)->layers==0 ? RAA_BLKSIZE : RAA_LAYERSIZE )
331 static struct RAA *real_raa_init(int layers)
333 struct RAA *r;
334 int i;
336 if (layers == 0) {
337 r = nasm_malloc(LEAFSIZ);
338 r->layers = 0;
339 memset(r->u.l.data, 0, sizeof(r->u.l.data));
340 r->stepsize = 1L;
341 } else {
342 r = nasm_malloc(BRANCHSIZ);
343 r->layers = layers;
344 for (i = 0; i < RAA_LAYERSIZE; i++)
345 r->u.b.data[i] = NULL;
346 r->stepsize = RAA_BLKSIZE;
347 while (--layers)
348 r->stepsize *= RAA_LAYERSIZE;
350 return r;
353 struct RAA *raa_init(void)
355 return real_raa_init(0);
358 void raa_free(struct RAA *r)
360 if (r->layers == 0)
361 nasm_free(r);
362 else {
363 struct RAA **p;
364 for (p = r->u.b.data; p - r->u.b.data < RAA_LAYERSIZE; p++)
365 if (*p)
366 raa_free(*p);
370 long raa_read(struct RAA *r, long posn)
372 if (posn >= r->stepsize * LAYERSIZ(r))
373 return 0; /* Return 0 for undefined entries */
374 while (r->layers > 0) {
375 ldiv_t l;
376 l = ldiv(posn, r->stepsize);
377 r = r->u.b.data[l.quot];
378 posn = l.rem;
379 if (!r)
380 return 0; /* Return 0 for undefined entries */
382 return r->u.l.data[posn];
385 struct RAA *raa_write(struct RAA *r, long posn, long value)
387 struct RAA *result;
389 if (posn < 0)
390 nasm_malloc_error(ERR_PANIC, "negative position in raa_write");
392 while (r->stepsize * LAYERSIZ(r) <= posn) {
394 * Must add a layer.
396 struct RAA *s;
397 int i;
399 s = nasm_malloc(BRANCHSIZ);
400 for (i = 0; i < RAA_LAYERSIZE; i++)
401 s->u.b.data[i] = NULL;
402 s->layers = r->layers + 1;
403 s->stepsize = LAYERSIZ(r) * r->stepsize;
404 s->u.b.data[0] = r;
405 r = s;
408 result = r;
410 while (r->layers > 0) {
411 ldiv_t l;
412 struct RAA **s;
413 l = ldiv(posn, r->stepsize);
414 s = &r->u.b.data[l.quot];
415 if (!*s)
416 *s = real_raa_init(r->layers - 1);
417 r = *s;
418 posn = l.rem;
421 r->u.l.data[posn] = value;
423 return result;
426 #define SAA_MAXLEN 8192
428 struct SAA *saa_init(long elem_len)
430 struct SAA *s;
432 if (elem_len > SAA_MAXLEN)
433 nasm_malloc_error(ERR_PANIC | ERR_NOFILE,
434 "SAA with huge elements");
436 s = nasm_malloc(sizeof(struct SAA));
437 s->posn = s->start = 0L;
438 s->elem_len = elem_len;
439 s->length = SAA_MAXLEN - (SAA_MAXLEN % elem_len);
440 s->data = nasm_malloc(s->length);
441 s->next = NULL;
442 s->end = s;
444 return s;
447 void saa_free(struct SAA *s)
449 struct SAA *t;
451 while (s) {
452 t = s->next;
453 nasm_free(s->data);
454 nasm_free(s);
455 s = t;
459 void *saa_wstruct(struct SAA *s)
461 void *p;
463 if (s->end->length - s->end->posn < s->elem_len) {
464 s->end->next = nasm_malloc(sizeof(struct SAA));
465 s->end->next->start = s->end->start + s->end->posn;
466 s->end = s->end->next;
467 s->end->length = s->length;
468 s->end->next = NULL;
469 s->end->posn = 0L;
470 s->end->data = nasm_malloc(s->length);
473 p = s->end->data + s->end->posn;
474 s->end->posn += s->elem_len;
475 return p;
478 void saa_wbytes(struct SAA *s, const void *data, long len)
480 const char *d = data;
482 while (len > 0) {
483 long l = s->end->length - s->end->posn;
484 if (l > len)
485 l = len;
486 if (l > 0) {
487 if (d) {
488 memcpy(s->end->data + s->end->posn, d, l);
489 d += l;
490 } else
491 memset(s->end->data + s->end->posn, 0, l);
492 s->end->posn += l;
493 len -= l;
495 if (len > 0) {
496 s->end->next = nasm_malloc(sizeof(struct SAA));
497 s->end->next->start = s->end->start + s->end->posn;
498 s->end = s->end->next;
499 s->end->length = s->length;
500 s->end->next = NULL;
501 s->end->posn = 0L;
502 s->end->data = nasm_malloc(s->length);
507 void saa_rewind(struct SAA *s)
509 s->rptr = s;
510 s->rpos = 0L;
513 void *saa_rstruct(struct SAA *s)
515 void *p;
517 if (!s->rptr)
518 return NULL;
520 if (s->rptr->posn - s->rpos < s->elem_len) {
521 s->rptr = s->rptr->next;
522 if (!s->rptr)
523 return NULL; /* end of array */
524 s->rpos = 0L;
527 p = s->rptr->data + s->rpos;
528 s->rpos += s->elem_len;
529 return p;
532 void *saa_rbytes(struct SAA *s, long *len)
534 void *p;
536 if (!s->rptr)
537 return NULL;
539 p = s->rptr->data + s->rpos;
540 *len = s->rptr->posn - s->rpos;
541 s->rptr = s->rptr->next;
542 s->rpos = 0L;
543 return p;
546 void saa_rnbytes(struct SAA *s, void *data, long len)
548 char *d = data;
550 while (len > 0) {
551 long l;
553 if (!s->rptr)
554 return;
556 l = s->rptr->posn - s->rpos;
557 if (l > len)
558 l = len;
559 if (l > 0) {
560 memcpy(d, s->rptr->data + s->rpos, l);
561 d += l;
562 s->rpos += l;
563 len -= l;
565 if (len > 0) {
566 s->rptr = s->rptr->next;
567 s->rpos = 0L;
572 void saa_fread(struct SAA *s, long posn, void *data, long len)
574 struct SAA *p;
575 long pos;
576 char *cdata = data;
578 if (!s->rptr || posn < s->rptr->start)
579 saa_rewind(s);
580 p = s->rptr;
581 while (posn >= p->start + p->posn) {
582 p = p->next;
583 if (!p)
584 return; /* what else can we do?! */
587 pos = posn - p->start;
588 while (len) {
589 long l = p->posn - pos;
590 if (l > len)
591 l = len;
592 memcpy(cdata, p->data + pos, l);
593 len -= l;
594 cdata += l;
595 p = p->next;
596 if (!p)
597 return;
598 pos = 0L;
600 s->rptr = p;
603 void saa_fwrite(struct SAA *s, long posn, void *data, long len)
605 struct SAA *p;
606 long pos;
607 char *cdata = data;
609 if (!s->rptr || posn < s->rptr->start)
610 saa_rewind(s);
611 p = s->rptr;
612 while (posn >= p->start + p->posn) {
613 p = p->next;
614 if (!p)
615 return; /* what else can we do?! */
618 pos = posn - p->start;
619 while (len) {
620 long l = p->posn - pos;
621 if (l > len)
622 l = len;
623 memcpy(p->data + pos, cdata, l);
624 len -= l;
625 cdata += l;
626 p = p->next;
627 if (!p)
628 return;
629 pos = 0L;
631 s->rptr = p;
634 void saa_fpwrite(struct SAA *s, FILE * fp)
636 char *data;
637 long len;
639 saa_rewind(s);
640 while ((data = saa_rbytes(s, &len)))
641 fwrite(data, 1, len, fp);
645 * Register, instruction, condition-code and prefix keywords used
646 * by the scanner.
648 #include "names.c"
649 static const char *special_names[] = {
650 "byte", "dword", "far", "long", "near", "nosplit", "qword",
651 "short", "strict", "to", "tword", "word"
653 static const char *prefix_names[] = {
654 "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
655 "repnz", "repz", "times"
659 * Standard scanner routine used by parser.c and some output
660 * formats. It keeps a succession of temporary-storage strings in
661 * stdscan_tempstorage, which can be cleared using stdscan_reset.
663 static char **stdscan_tempstorage = NULL;
664 static int stdscan_tempsize = 0, stdscan_templen = 0;
665 #define STDSCAN_TEMP_DELTA 256
667 static void stdscan_pop(void)
669 nasm_free(stdscan_tempstorage[--stdscan_templen]);
672 void stdscan_reset(void)
674 while (stdscan_templen > 0)
675 stdscan_pop();
679 * Unimportant cleanup is done to avoid confusing people who are trying
680 * to debug real memory leaks
682 void nasmlib_cleanup(void)
684 stdscan_reset();
685 nasm_free(stdscan_tempstorage);
688 static char *stdscan_copy(char *p, int len)
690 char *text;
692 text = nasm_malloc(len + 1);
693 strncpy(text, p, len);
694 text[len] = '\0';
696 if (stdscan_templen >= stdscan_tempsize) {
697 stdscan_tempsize += STDSCAN_TEMP_DELTA;
698 stdscan_tempstorage = nasm_realloc(stdscan_tempstorage,
699 stdscan_tempsize *
700 sizeof(char *));
702 stdscan_tempstorage[stdscan_templen++] = text;
704 return text;
707 char *stdscan_bufptr = NULL;
708 int stdscan(void *private_data, struct tokenval *tv)
710 char ourcopy[MAX_KEYWORD + 1], *r, *s;
712 (void)private_data; /* Don't warn that this parameter is unused */
714 while (isspace(*stdscan_bufptr))
715 stdscan_bufptr++;
716 if (!*stdscan_bufptr)
717 return tv->t_type = 0;
719 /* we have a token; either an id, a number or a char */
720 if (isidstart(*stdscan_bufptr) ||
721 (*stdscan_bufptr == '$' && isidstart(stdscan_bufptr[1]))) {
722 /* now we've got an identifier */
723 int i;
724 int is_sym = FALSE;
726 if (*stdscan_bufptr == '$') {
727 is_sym = TRUE;
728 stdscan_bufptr++;
731 r = stdscan_bufptr++;
732 /* read the entire buffer to advance the buffer pointer but... */
733 while (isidchar(*stdscan_bufptr))
734 stdscan_bufptr++;
736 /* ... copy only up to IDLEN_MAX-1 characters */
737 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r < IDLEN_MAX ?
738 stdscan_bufptr - r : IDLEN_MAX - 1);
740 if (is_sym || stdscan_bufptr - r > MAX_KEYWORD)
741 return tv->t_type = TOKEN_ID; /* bypass all other checks */
743 for (s = tv->t_charptr, r = ourcopy; *s; s++)
744 *r++ = tolower(*s);
745 *r = '\0';
746 /* right, so we have an identifier sitting in temp storage. now,
747 * is it actually a register or instruction name, or what? */
748 if ((tv->t_integer = bsi(ourcopy, reg_names,
749 elements(reg_names))) >= 0) {
750 tv->t_integer += EXPR_REG_START;
751 return tv->t_type = TOKEN_REG;
752 } else if ((tv->t_integer = bsi(ourcopy, insn_names,
753 elements(insn_names))) >= 0) {
754 return tv->t_type = TOKEN_INSN;
756 for (i = 0; i < elements(icn); i++)
757 if (!strncmp(ourcopy, icn[i], strlen(icn[i]))) {
758 char *p = ourcopy + strlen(icn[i]);
759 tv->t_integer = ico[i];
760 if ((tv->t_inttwo = bsi(p, conditions,
761 elements(conditions))) >= 0)
762 return tv->t_type = TOKEN_INSN;
764 if ((tv->t_integer = bsi(ourcopy, prefix_names,
765 elements(prefix_names))) >= 0) {
766 tv->t_integer += PREFIX_ENUM_START;
767 return tv->t_type = TOKEN_PREFIX;
769 if ((tv->t_integer = bsi(ourcopy, special_names,
770 elements(special_names))) >= 0)
771 return tv->t_type = TOKEN_SPECIAL;
772 if (!nasm_stricmp(ourcopy, "seg"))
773 return tv->t_type = TOKEN_SEG;
774 if (!nasm_stricmp(ourcopy, "wrt"))
775 return tv->t_type = TOKEN_WRT;
776 return tv->t_type = TOKEN_ID;
777 } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) {
779 * It's a $ sign with no following hex number; this must
780 * mean it's a Here token ($), evaluating to the current
781 * assembly location, or a Base token ($$), evaluating to
782 * the base of the current segment.
784 stdscan_bufptr++;
785 if (*stdscan_bufptr == '$') {
786 stdscan_bufptr++;
787 return tv->t_type = TOKEN_BASE;
789 return tv->t_type = TOKEN_HERE;
790 } else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */
791 int rn_error;
793 r = stdscan_bufptr++;
794 while (isnumchar(*stdscan_bufptr))
795 stdscan_bufptr++;
797 if (*stdscan_bufptr == '.') {
799 * a floating point constant
801 stdscan_bufptr++;
802 while (isnumchar(*stdscan_bufptr) ||
803 ((stdscan_bufptr[-1] == 'e'
804 || stdscan_bufptr[-1] == 'E')
805 && (*stdscan_bufptr == '-' || *stdscan_bufptr == '+'))) {
806 stdscan_bufptr++;
808 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
809 return tv->t_type = TOKEN_FLOAT;
811 r = stdscan_copy(r, stdscan_bufptr - r);
812 tv->t_integer = readnum(r, &rn_error);
813 stdscan_pop();
814 if (rn_error)
815 return tv->t_type = TOKEN_ERRNUM; /* some malformation occurred */
816 tv->t_charptr = NULL;
817 return tv->t_type = TOKEN_NUM;
818 } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"') { /* a char constant */
819 char quote = *stdscan_bufptr++, *r;
820 int rn_warn;
821 r = tv->t_charptr = stdscan_bufptr;
822 while (*stdscan_bufptr && *stdscan_bufptr != quote)
823 stdscan_bufptr++;
824 tv->t_inttwo = stdscan_bufptr - r; /* store full version */
825 if (!*stdscan_bufptr)
826 return tv->t_type = TOKEN_ERRNUM; /* unmatched quotes */
827 stdscan_bufptr++; /* skip over final quote */
828 tv->t_integer = readstrnum(r, tv->t_inttwo, &rn_warn);
829 /* FIXME: rn_warn is not checked! */
830 return tv->t_type = TOKEN_NUM;
831 } else if (*stdscan_bufptr == ';') { /* a comment has happened - stay */
832 return tv->t_type = 0;
833 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
834 stdscan_bufptr += 2;
835 return tv->t_type = TOKEN_SHR;
836 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
837 stdscan_bufptr += 2;
838 return tv->t_type = TOKEN_SHL;
839 } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
840 stdscan_bufptr += 2;
841 return tv->t_type = TOKEN_SDIV;
842 } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
843 stdscan_bufptr += 2;
844 return tv->t_type = TOKEN_SMOD;
845 } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
846 stdscan_bufptr += 2;
847 return tv->t_type = TOKEN_EQ;
848 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
849 stdscan_bufptr += 2;
850 return tv->t_type = TOKEN_NE;
851 } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
852 stdscan_bufptr += 2;
853 return tv->t_type = TOKEN_NE;
854 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
855 stdscan_bufptr += 2;
856 return tv->t_type = TOKEN_LE;
857 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
858 stdscan_bufptr += 2;
859 return tv->t_type = TOKEN_GE;
860 } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
861 stdscan_bufptr += 2;
862 return tv->t_type = TOKEN_DBL_AND;
863 } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
864 stdscan_bufptr += 2;
865 return tv->t_type = TOKEN_DBL_XOR;
866 } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
867 stdscan_bufptr += 2;
868 return tv->t_type = TOKEN_DBL_OR;
869 } else /* just an ordinary char */
870 return tv->t_type = (unsigned char)(*stdscan_bufptr++);
874 * Return TRUE if the argument is a simple scalar. (Or a far-
875 * absolute, which counts.)
877 int is_simple(expr * vect)
879 while (vect->type && !vect->value)
880 vect++;
881 if (!vect->type)
882 return 1;
883 if (vect->type != EXPR_SIMPLE)
884 return 0;
885 do {
886 vect++;
887 } while (vect->type && !vect->value);
888 if (vect->type && vect->type < EXPR_SEGBASE + SEG_ABS)
889 return 0;
890 return 1;
894 * Return TRUE if the argument is a simple scalar, _NOT_ a far-
895 * absolute.
897 int is_really_simple(expr * vect)
899 while (vect->type && !vect->value)
900 vect++;
901 if (!vect->type)
902 return 1;
903 if (vect->type != EXPR_SIMPLE)
904 return 0;
905 do {
906 vect++;
907 } while (vect->type && !vect->value);
908 if (vect->type)
909 return 0;
910 return 1;
914 * Return TRUE if the argument is relocatable (i.e. a simple
915 * scalar, plus at most one segment-base, plus possibly a WRT).
917 int is_reloc(expr * vect)
919 while (vect->type && !vect->value) /* skip initial value-0 terms */
920 vect++;
921 if (!vect->type) /* trivially return TRUE if nothing */
922 return 1; /* is present apart from value-0s */
923 if (vect->type < EXPR_SIMPLE) /* FALSE if a register is present */
924 return 0;
925 if (vect->type == EXPR_SIMPLE) { /* skip over a pure number term... */
926 do {
927 vect++;
928 } while (vect->type && !vect->value);
929 if (!vect->type) /* ...returning TRUE if that's all */
930 return 1;
932 if (vect->type == EXPR_WRT) { /* skip over a WRT term... */
933 do {
934 vect++;
935 } while (vect->type && !vect->value);
936 if (!vect->type) /* ...returning TRUE if that's all */
937 return 1;
939 if (vect->value != 0 && vect->value != 1)
940 return 0; /* segment base multiplier non-unity */
941 do { /* skip over _one_ seg-base term... */
942 vect++;
943 } while (vect->type && !vect->value);
944 if (!vect->type) /* ...returning TRUE if that's all */
945 return 1;
946 return 0; /* And return FALSE if there's more */
950 * Return TRUE if the argument contains an `unknown' part.
952 int is_unknown(expr * vect)
954 while (vect->type && vect->type < EXPR_UNKNOWN)
955 vect++;
956 return (vect->type == EXPR_UNKNOWN);
960 * Return TRUE if the argument contains nothing but an `unknown'
961 * part.
963 int is_just_unknown(expr * vect)
965 while (vect->type && !vect->value)
966 vect++;
967 return (vect->type == EXPR_UNKNOWN);
971 * Return the scalar part of a relocatable vector. (Including
972 * simple scalar vectors - those qualify as relocatable.)
974 long reloc_value(expr * vect)
976 while (vect->type && !vect->value)
977 vect++;
978 if (!vect->type)
979 return 0;
980 if (vect->type == EXPR_SIMPLE)
981 return vect->value;
982 else
983 return 0;
987 * Return the segment number of a relocatable vector, or NO_SEG for
988 * simple scalars.
990 long reloc_seg(expr * vect)
992 while (vect->type && (vect->type == EXPR_WRT || !vect->value))
993 vect++;
994 if (vect->type == EXPR_SIMPLE) {
995 do {
996 vect++;
997 } while (vect->type && (vect->type == EXPR_WRT || !vect->value));
999 if (!vect->type)
1000 return NO_SEG;
1001 else
1002 return vect->type - EXPR_SEGBASE;
1006 * Return the WRT segment number of a relocatable vector, or NO_SEG
1007 * if no WRT part is present.
1009 long reloc_wrt(expr * vect)
1011 while (vect->type && vect->type < EXPR_WRT)
1012 vect++;
1013 if (vect->type == EXPR_WRT) {
1014 return vect->value;
1015 } else
1016 return NO_SEG;
1020 * Binary search.
1022 int bsi(char *string, const char **array, int size)
1024 int i = -1, j = size; /* always, i < index < j */
1025 while (j - i >= 2) {
1026 int k = (i + j) / 2;
1027 int l = strcmp(string, array[k]);
1028 if (l < 0) /* it's in the first half */
1029 j = k;
1030 else if (l > 0) /* it's in the second half */
1031 i = k;
1032 else /* we've got it :) */
1033 return k;
1035 return -1; /* we haven't got it :( */
1038 static char *file_name = NULL;
1039 static long line_number = 0;
1041 char *src_set_fname(char *newname)
1043 char *oldname = file_name;
1044 file_name = newname;
1045 return oldname;
1048 long src_set_linnum(long newline)
1050 long oldline = line_number;
1051 line_number = newline;
1052 return oldline;
1055 long src_get_linnum(void)
1057 return line_number;
1060 int src_get(long *xline, char **xname)
1062 if (!file_name || !*xname || strcmp(*xname, file_name)) {
1063 nasm_free(*xname);
1064 *xname = file_name ? nasm_strdup(file_name) : NULL;
1065 *xline = line_number;
1066 return -2;
1068 if (*xline != line_number) {
1069 long tmp = line_number - *xline;
1070 *xline = line_number;
1071 return tmp;
1073 return 0;
1076 void nasm_quote(char **str)
1078 int ln = strlen(*str);
1079 char q = (*str)[0];
1080 char *p;
1081 if (ln > 1 && (*str)[ln - 1] == q && (q == '"' || q == '\''))
1082 return;
1083 q = '"';
1084 if (strchr(*str, q))
1085 q = '\'';
1086 p = nasm_malloc(ln + 3);
1087 strcpy(p + 1, *str);
1088 nasm_free(*str);
1089 p[ln + 1] = p[0] = q;
1090 p[ln + 2] = 0;
1091 *str = p;
1094 char *nasm_strcat(char *one, char *two)
1096 char *rslt;
1097 int l1 = strlen(one);
1098 rslt = nasm_malloc(l1 + strlen(two) + 1);
1099 strcpy(rslt, one);
1100 strcpy(rslt + l1, two);
1101 return rslt;
1104 void null_debug_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1107 void null_debug_linenum(const char *filename, long linenumber, long segto)
1110 void null_debug_deflabel(char *name, long segment, long offset,
1111 int is_global, char *special)
1114 void null_debug_routine(const char *directive, const char *params)
1117 void null_debug_typevalue(long type)
1120 void null_debug_output(int type, void *param)
1123 void null_debug_cleanup(void)
1127 struct dfmt null_debug_form = {
1128 "Null debug format",
1129 "null",
1130 null_debug_init,
1131 null_debug_linenum,
1132 null_debug_deflabel,
1133 null_debug_routine,
1134 null_debug_typevalue,
1135 null_debug_output,
1136 null_debug_cleanup
1139 struct dfmt *null_debug_arr[2] = { &null_debug_form, NULL };