NASM 0.98bf
[nasm.git] / nasmlib.c
blob27179ebaf1b34a62e3b32dceb1ad181d804c61fb
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"
17 static efunc nasm_malloc_error;
19 #ifdef LOGALLOC
20 static FILE *logfp;
21 #endif
23 void nasm_set_malloc_error (efunc error)
25 nasm_malloc_error = error;
26 #ifdef LOGALLOC
27 logfp = fopen ("malloc.log", "w");
28 setvbuf (logfp, NULL, _IOLBF, BUFSIZ);
29 fprintf (logfp, "null pointer is %p\n", NULL);
30 #endif
33 #ifdef LOGALLOC
34 void *nasm_malloc_log (char *file, int line, size_t size)
35 #else
36 void *nasm_malloc (size_t size)
37 #endif
39 void *p = malloc(size);
40 if (!p)
41 nasm_malloc_error (ERR_FATAL | ERR_NOFILE, "out of memory");
42 #ifdef LOGALLOC
43 else
44 fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
45 file, line, (long)size, p);
46 #endif
47 return p;
50 #ifdef LOGALLOC
51 void *nasm_realloc_log (char *file, int line, void *q, size_t size)
52 #else
53 void *nasm_realloc (void *q, size_t size)
54 #endif
56 void *p = q ? realloc(q, size) : malloc(size);
57 if (!p)
58 nasm_malloc_error (ERR_FATAL | ERR_NOFILE, "out of memory");
59 #ifdef LOGALLOC
60 else if (q)
61 fprintf(logfp, "%s %d realloc(%p,%ld) returns %p\n",
62 file, line, q, (long)size, p);
63 else
64 fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
65 file, line, (long)size, p);
66 #endif
67 return p;
70 #ifdef LOGALLOC
71 void nasm_free_log (char *file, int line, void *q)
72 #else
73 void nasm_free (void *q)
74 #endif
76 if (q) {
77 free (q);
78 #ifdef LOGALLOC
79 fprintf(logfp, "%s %d free(%p)\n",
80 file, line, q);
81 #endif
85 #ifdef LOGALLOC
86 char *nasm_strdup_log (char *file, int line, char *s)
87 #else
88 char *nasm_strdup (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 int nasm_stricmp (const char *s1, const char *s2)
130 while (*s1 && toupper(*s1) == toupper(*s2))
131 s1++, s2++;
132 if (!*s1 && !*s2)
133 return 0;
134 else if (toupper(*s1) < toupper(*s2))
135 return -1;
136 else
137 return 1;
140 int nasm_strnicmp (const char *s1, const char *s2, int n)
142 while (n > 0 && *s1 && toupper(*s1) == toupper(*s2))
143 s1++, s2++, n--;
144 if ((!*s1 && !*s2) || n==0)
145 return 0;
146 else if (toupper(*s1) < toupper(*s2))
147 return -1;
148 else
149 return 1;
152 #define lib_isnumchar(c) ( isalnum(c) || (c) == '$')
153 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
155 long readnum (char *str, int *error)
157 char *r = str, *q;
158 long radix;
159 unsigned long result, checklimit;
160 int digit, last;
161 int warn = FALSE;
162 int sign = 1;
164 *error = FALSE;
166 while (isspace(*r)) r++; /* find start of number */
169 * If the number came from make_tok_num (as a result of an %assign), it
170 * might have a '-' built into it (rather than in a preceeding token).
172 if (*r == '-')
174 r++;
175 sign = -1;
178 q = r;
180 while (lib_isnumchar(*q)) q++; /* find end of number */
183 * If it begins 0x, 0X or $, or ends in H, it's in hex. if it
184 * ends in Q, it's octal. if it ends in B, it's binary.
185 * Otherwise, it's ordinary decimal.
187 if (*r=='0' && (r[1]=='x' || r[1]=='X'))
188 radix = 16, r += 2;
189 else if (*r=='$')
190 radix = 16, r++;
191 else if (q[-1]=='H' || q[-1]=='h')
192 radix = 16 , q--;
193 else if (q[-1]=='Q' || q[-1]=='q')
194 radix = 8 , q--;
195 else if (q[-1]=='B' || q[-1]=='b')
196 radix = 2 , q--;
197 else
198 radix = 10;
201 * If this number has been found for us by something other than
202 * the ordinary scanners, then it might be malformed by having
203 * nothing between the prefix and the suffix. Check this case
204 * now.
206 if (r >= q) {
207 *error = TRUE;
208 return 0;
212 * `checklimit' must be 2**32 / radix. We can't do that in
213 * 32-bit arithmetic, which we're (probably) using, so we
214 * cheat: since we know that all radices we use are even, we
215 * can divide 2**31 by radix/2 instead.
217 checklimit = 0x80000000UL / (radix>>1);
220 * Calculate the highest allowable value for the last digit
221 * of a 32 bit constant... in radix 10, it is 6, otherwise it is 0
223 last = (radix == 10 ? 6 : 0);
225 result = 0;
226 while (*r && r < q) {
227 if (*r<'0' || (*r>'9' && *r<'A') || (digit = numvalue(*r)) >= radix)
229 *error = TRUE;
230 return 0;
232 if (result > checklimit ||
233 (result == checklimit && digit >= last))
235 warn = TRUE;
238 result = radix * result + digit;
239 r++;
242 if (warn)
243 nasm_malloc_error (ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
244 "numeric constant %s does not fit in 32 bits",
245 str);
247 return result*sign;
250 long readstrnum (char *str, int length, int *warn)
252 long charconst = 0;
253 int i;
255 *warn = FALSE;
257 str += length;
258 for (i=0; i<length; i++) {
259 if (charconst & 0xff000000UL) {
260 *warn = TRUE;
262 charconst = (charconst<<8) + (unsigned char) *--str;
264 return charconst;
267 static long next_seg;
269 void seg_init(void)
271 next_seg = 0;
274 long seg_alloc(void)
276 return (next_seg += 2) - 2;
279 void fwriteshort (int data, FILE *fp)
281 fputc ((int) (data & 255), fp);
282 fputc ((int) ((data >> 8) & 255), fp);
285 void fwritelong (long data, FILE *fp)
287 fputc ((int) (data & 255), fp);
288 fputc ((int) ((data >> 8) & 255), fp);
289 fputc ((int) ((data >> 16) & 255), fp);
290 fputc ((int) ((data >> 24) & 255), fp);
293 void standard_extension (char *inname, char *outname, char *extension,
294 efunc error)
296 char *p, *q;
298 if (*outname) /* file name already exists, */
299 return; /* so do nothing */
300 q = inname;
301 p = outname;
302 while (*q) *p++ = *q++; /* copy, and find end of string */
303 *p = '\0'; /* terminate it */
304 while (p > outname && *--p != '.');/* find final period (or whatever) */
305 if (*p != '.') while (*p) p++; /* go back to end if none found */
306 if (!strcmp(p, extension)) { /* is the extension already there? */
307 if (*extension)
308 error(ERR_WARNING | ERR_NOFILE,
309 "file name already ends in `%s': "
310 "output will be in `nasm.out'",
311 extension);
312 else
313 error(ERR_WARNING | ERR_NOFILE,
314 "file name already has no extension: "
315 "output will be in `nasm.out'");
316 strcpy(outname, "nasm.out");
317 } else
318 strcpy(p, extension);
321 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
322 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
324 #define LAYERSIZ(r) ( (r)->layers==0 ? RAA_BLKSIZE : RAA_LAYERSIZE )
326 static struct RAA *real_raa_init (int layers)
328 struct RAA *r;
330 if (layers == 0) {
331 r = nasm_malloc (LEAFSIZ);
332 memset (r->u.l.data, 0, sizeof(r->u.l.data));
333 r->layers = 0;
334 r->stepsize = 1L;
335 } else {
336 r = nasm_malloc (BRANCHSIZ);
337 memset (r->u.b.data, 0, sizeof(r->u.b.data));
338 r->layers = layers;
339 r->stepsize = RAA_BLKSIZE;
340 while (--layers)
341 r->stepsize *= RAA_LAYERSIZE;
343 return r;
346 struct RAA *raa_init (void)
348 return real_raa_init (0);
351 void raa_free (struct RAA *r)
353 if (r->layers == 0)
354 nasm_free (r);
355 else {
356 struct RAA **p;
357 for (p = r->u.b.data; p - r->u.b.data < RAA_LAYERSIZE; p++)
358 if (*p)
359 raa_free (*p);
363 long raa_read (struct RAA *r, long posn)
365 if (posn > r->stepsize * LAYERSIZ(r))
366 return 0L;
367 while (r->layers > 0) {
368 ldiv_t l;
369 l = ldiv (posn, r->stepsize);
370 r = r->u.b.data[l.quot];
371 posn = l.rem;
372 if (!r) /* better check this */
373 return 0L;
375 return r->u.l.data[posn];
378 struct RAA *raa_write (struct RAA *r, long posn, long value)
380 struct RAA *result;
382 if (posn < 0)
383 nasm_malloc_error (ERR_PANIC, "negative position in raa_write");
385 while (r->stepsize * LAYERSIZ(r) < posn) {
387 * Must go up a layer.
389 struct RAA *s;
391 s = nasm_malloc (BRANCHSIZ);
392 memset (s->u.b.data, 0, sizeof(r->u.b.data));
393 s->layers = r->layers + 1;
394 s->stepsize = RAA_LAYERSIZE * r->stepsize;
395 s->u.b.data[0] = r;
396 r = s;
399 result = r;
401 while (r->layers > 0) {
402 ldiv_t l;
403 struct RAA **s;
404 l = ldiv (posn, r->stepsize);
405 s = &r->u.b.data[l.quot];
406 if (!*s)
407 *s = real_raa_init (r->layers - 1);
408 r = *s;
409 posn = l.rem;
412 r->u.l.data[posn] = value;
414 return result;
417 #define SAA_MAXLEN 8192
419 struct SAA *saa_init (long elem_len)
421 struct SAA *s;
423 if (elem_len > SAA_MAXLEN)
424 nasm_malloc_error (ERR_PANIC | ERR_NOFILE, "SAA with huge elements");
426 s = nasm_malloc (sizeof(struct SAA));
427 s->posn = s->start = 0L;
428 s->elem_len = elem_len;
429 s->length = SAA_MAXLEN - (SAA_MAXLEN % elem_len);
430 s->data = nasm_malloc (s->length);
431 s->next = NULL;
432 s->end = s;
434 return s;
437 void saa_free (struct SAA *s)
439 struct SAA *t;
441 while (s) {
442 t = s->next;
443 nasm_free (s->data);
444 nasm_free (s);
445 s = t;
449 void *saa_wstruct (struct SAA *s)
451 void *p;
453 if (s->end->length - s->end->posn < s->elem_len) {
454 s->end->next = nasm_malloc (sizeof(struct SAA));
455 s->end->next->start = s->end->start + s->end->posn;
456 s->end = s->end->next;
457 s->end->length = s->length;
458 s->end->next = NULL;
459 s->end->posn = 0L;
460 s->end->data = nasm_malloc (s->length);
463 p = s->end->data + s->end->posn;
464 s->end->posn += s->elem_len;
465 return p;
468 void saa_wbytes (struct SAA *s, void *data, long len)
470 char *d = data;
472 while (len > 0) {
473 long l = s->end->length - s->end->posn;
474 if (l > len)
475 l = len;
476 if (l > 0) {
477 if (d) {
478 memcpy (s->end->data + s->end->posn, d, l);
479 d += l;
480 } else
481 memset (s->end->data + s->end->posn, 0, l);
482 s->end->posn += l;
483 len -= l;
485 if (len > 0) {
486 s->end->next = nasm_malloc (sizeof(struct SAA));
487 s->end->next->start = s->end->start + s->end->posn;
488 s->end = s->end->next;
489 s->end->length = s->length;
490 s->end->next = NULL;
491 s->end->posn = 0L;
492 s->end->data = nasm_malloc (s->length);
497 void saa_rewind (struct SAA *s)
499 s->rptr = s;
500 s->rpos = 0L;
503 void *saa_rstruct (struct SAA *s)
505 void *p;
507 if (!s->rptr)
508 return NULL;
510 if (s->rptr->posn - s->rpos < s->elem_len) {
511 s->rptr = s->rptr->next;
512 if (!s->rptr)
513 return NULL; /* end of array */
514 s->rpos = 0L;
517 p = s->rptr->data + s->rpos;
518 s->rpos += s->elem_len;
519 return p;
522 void *saa_rbytes (struct SAA *s, long *len)
524 void *p;
526 if (!s->rptr)
527 return NULL;
529 p = s->rptr->data + s->rpos;
530 *len = s->rptr->posn - s->rpos;
531 s->rptr = s->rptr->next;
532 s->rpos = 0L;
533 return p;
536 void saa_rnbytes (struct SAA *s, void *data, long len)
538 char *d = data;
540 while (len > 0) {
541 long l;
543 if (!s->rptr)
544 return;
546 l = s->rptr->posn - s->rpos;
547 if (l > len)
548 l = len;
549 if (l > 0) {
550 memcpy (d, s->rptr->data + s->rpos, l);
551 d += l;
552 s->rpos += l;
553 len -= l;
555 if (len > 0) {
556 s->rptr = s->rptr->next;
557 s->rpos = 0L;
562 void saa_fread (struct SAA *s, long posn, void *data, long len)
564 struct SAA *p;
565 long pos;
566 char *cdata = data;
568 if (!s->rptr || posn < s->rptr->start)
569 saa_rewind (s);
570 p = s->rptr;
571 while (posn >= p->start + p->posn) {
572 p = p->next;
573 if (!p)
574 return; /* what else can we do?! */
577 pos = posn - p->start;
578 while (len) {
579 long l = p->posn - pos;
580 if (l > len)
581 l = len;
582 memcpy (cdata, p->data+pos, l);
583 len -= l;
584 cdata += l;
585 p = p->next;
586 if (!p)
587 return;
588 pos = 0L;
590 s->rptr = p;
593 void saa_fwrite (struct SAA *s, long posn, void *data, long len)
595 struct SAA *p;
596 long pos;
597 char *cdata = data;
599 if (!s->rptr || posn < s->rptr->start)
600 saa_rewind (s);
601 p = s->rptr;
602 while (posn >= p->start + p->posn) {
603 p = p->next;
604 if (!p)
605 return; /* what else can we do?! */
608 pos = posn - p->start;
609 while (len) {
610 long l = p->posn - pos;
611 if (l > len)
612 l = len;
613 memcpy (p->data+pos, cdata, l);
614 len -= l;
615 cdata += l;
616 p = p->next;
617 if (!p)
618 return;
619 pos = 0L;
621 s->rptr = p;
624 void saa_fpwrite (struct SAA *s, FILE *fp)
626 char *data;
627 long len;
629 saa_rewind (s);
630 while ( (data = saa_rbytes (s, &len)) != NULL )
631 fwrite (data, 1, len, fp);
635 * Register, instruction, condition-code and prefix keywords used
636 * by the scanner.
638 #include "names.c"
639 static char *special_names[] = {
640 "byte", "dword", "far", "long", "near", "nosplit", "qword",
641 "short", "to", "tword", "word"
643 static char *prefix_names[] = {
644 "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
645 "repnz", "repz", "times"
650 * Standard scanner routine used by parser.c and some output
651 * formats. It keeps a succession of temporary-storage strings in
652 * stdscan_tempstorage, which can be cleared using stdscan_reset.
654 static char **stdscan_tempstorage = NULL;
655 static int stdscan_tempsize = 0, stdscan_templen = 0;
656 #define STDSCAN_TEMP_DELTA 256
658 static void stdscan_pop(void)
660 nasm_free (stdscan_tempstorage[--stdscan_templen]);
663 void stdscan_reset(void)
665 while (stdscan_templen > 0)
666 stdscan_pop();
670 * Unimportant cleanup is done to avoid confusing people who are trying
671 * to debug real memory leaks
673 void nasmlib_cleanup (void)
675 stdscan_reset();
676 nasm_free (stdscan_tempstorage);
679 static char *stdscan_copy(char *p, int len)
681 char *text;
683 text = nasm_malloc(len+1);
684 strncpy (text, p, len);
685 text[len] = '\0';
687 if (stdscan_templen >= stdscan_tempsize) {
688 stdscan_tempsize += STDSCAN_TEMP_DELTA;
689 stdscan_tempstorage = nasm_realloc(stdscan_tempstorage,
690 stdscan_tempsize*sizeof(char *));
692 stdscan_tempstorage[stdscan_templen++] = text;
694 return text;
697 char *stdscan_bufptr = NULL;
698 int stdscan (void *private_data, struct tokenval *tv)
700 char ourcopy[MAX_KEYWORD+1], *r, *s;
702 (void) private_data; /* Don't warn that this parameter is unused */
704 while (isspace(*stdscan_bufptr)) stdscan_bufptr++;
705 if (!*stdscan_bufptr)
706 return tv->t_type = 0;
708 /* we have a token; either an id, a number or a char */
709 if (isidstart(*stdscan_bufptr) ||
710 (*stdscan_bufptr == '$' && isidstart(stdscan_bufptr[1]))) {
711 /* now we've got an identifier */
712 int i;
713 int is_sym = FALSE;
715 if (*stdscan_bufptr == '$') {
716 is_sym = TRUE;
717 stdscan_bufptr++;
720 r = stdscan_bufptr++;
721 while (isidchar(*stdscan_bufptr)) stdscan_bufptr++;
722 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
724 if (is_sym || stdscan_bufptr-r > MAX_KEYWORD)
725 return tv->t_type = TOKEN_ID;/* bypass all other checks */
727 for (s=tv->t_charptr, r=ourcopy; *s; s++)
728 *r++ = tolower (*s);
729 *r = '\0';
730 /* right, so we have an identifier sitting in temp storage. now,
731 * is it actually a register or instruction name, or what? */
732 if ((tv->t_integer=bsi(ourcopy, reg_names,
733 elements(reg_names)))>=0) {
734 tv->t_integer += EXPR_REG_START;
735 return tv->t_type = TOKEN_REG;
736 } else if ((tv->t_integer=bsi(ourcopy, insn_names,
737 elements(insn_names)))>=0) {
738 return tv->t_type = TOKEN_INSN;
740 for (i=0; i<elements(icn); i++)
741 if (!strncmp(ourcopy, icn[i], strlen(icn[i]))) {
742 char *p = ourcopy + strlen(icn[i]);
743 tv->t_integer = ico[i];
744 if ((tv->t_inttwo=bsi(p, conditions,
745 elements(conditions)))>=0)
746 return tv->t_type = TOKEN_INSN;
748 if ((tv->t_integer=bsi(ourcopy, prefix_names,
749 elements(prefix_names)))>=0) {
750 tv->t_integer += PREFIX_ENUM_START;
751 return tv->t_type = TOKEN_PREFIX;
753 if ((tv->t_integer=bsi(ourcopy, special_names,
754 elements(special_names)))>=0)
755 return tv->t_type = TOKEN_SPECIAL;
756 if (!strcmp(ourcopy, "seg"))
757 return tv->t_type = TOKEN_SEG;
758 if (!strcmp(ourcopy, "wrt"))
759 return tv->t_type = TOKEN_WRT;
760 return tv->t_type = TOKEN_ID;
761 } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) {
763 * It's a $ sign with no following hex number; this must
764 * mean it's a Here token ($), evaluating to the current
765 * assembly location, or a Base token ($$), evaluating to
766 * the base of the current segment.
768 stdscan_bufptr++;
769 if (*stdscan_bufptr == '$') {
770 stdscan_bufptr++;
771 return tv->t_type = TOKEN_BASE;
773 return tv->t_type = TOKEN_HERE;
774 } else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */
775 int rn_error;
777 r = stdscan_bufptr++;
778 while (isnumchar(*stdscan_bufptr))
779 stdscan_bufptr++;
781 if (*stdscan_bufptr == '.') {
783 * a floating point constant
785 stdscan_bufptr++;
786 while (isnumchar(*stdscan_bufptr) ||
787 ((stdscan_bufptr[-1] == 'e' || stdscan_bufptr[-1] == 'E')
788 && (*stdscan_bufptr == '-' || *stdscan_bufptr == '+')) )
790 stdscan_bufptr++;
792 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
793 return tv->t_type = TOKEN_FLOAT;
795 r = stdscan_copy(r, stdscan_bufptr - r);
796 tv->t_integer = readnum(r, &rn_error);
797 stdscan_pop();
798 if (rn_error)
799 return tv->t_type = TOKEN_ERRNUM;/* some malformation occurred */
800 tv->t_charptr = NULL;
801 return tv->t_type = TOKEN_NUM;
802 } else if (*stdscan_bufptr == '\'' ||
803 *stdscan_bufptr == '"') {/* a char constant */
804 char quote = *stdscan_bufptr++, *r;
805 int rn_warn;
806 r = tv->t_charptr = stdscan_bufptr;
807 while (*stdscan_bufptr && *stdscan_bufptr != quote) stdscan_bufptr++;
808 tv->t_inttwo = stdscan_bufptr - r; /* store full version */
809 if (!*stdscan_bufptr)
810 return tv->t_type = TOKEN_ERRNUM; /* unmatched quotes */
811 stdscan_bufptr++; /* skip over final quote */
812 tv->t_integer = readstrnum(r, tv->t_inttwo, &rn_warn);
813 /* FIXME: rn_warn is not checked! */
814 return tv->t_type = TOKEN_NUM;
815 } else if (*stdscan_bufptr == ';') { /* a comment has happened - stay */
816 return tv->t_type = 0;
817 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
818 stdscan_bufptr += 2;
819 return tv->t_type = TOKEN_SHR;
820 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
821 stdscan_bufptr += 2;
822 return tv->t_type = TOKEN_SHL;
823 } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
824 stdscan_bufptr += 2;
825 return tv->t_type = TOKEN_SDIV;
826 } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
827 stdscan_bufptr += 2;
828 return tv->t_type = TOKEN_SMOD;
829 } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
830 stdscan_bufptr += 2;
831 return tv->t_type = TOKEN_EQ;
832 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
833 stdscan_bufptr += 2;
834 return tv->t_type = TOKEN_NE;
835 } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
836 stdscan_bufptr += 2;
837 return tv->t_type = TOKEN_NE;
838 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
839 stdscan_bufptr += 2;
840 return tv->t_type = TOKEN_LE;
841 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
842 stdscan_bufptr += 2;
843 return tv->t_type = TOKEN_GE;
844 } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
845 stdscan_bufptr += 2;
846 return tv->t_type = TOKEN_DBL_AND;
847 } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
848 stdscan_bufptr += 2;
849 return tv->t_type = TOKEN_DBL_XOR;
850 } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
851 stdscan_bufptr += 2;
852 return tv->t_type = TOKEN_DBL_OR;
853 } else /* just an ordinary char */
854 return tv->t_type = (unsigned char) (*stdscan_bufptr++);
858 * Return TRUE if the argument is a simple scalar. (Or a far-
859 * absolute, which counts.)
861 int is_simple (expr *vect)
863 while (vect->type && !vect->value)
864 vect++;
865 if (!vect->type)
866 return 1;
867 if (vect->type != EXPR_SIMPLE)
868 return 0;
869 do {
870 vect++;
871 } while (vect->type && !vect->value);
872 if (vect->type && vect->type < EXPR_SEGBASE+SEG_ABS) return 0;
873 return 1;
877 * Return TRUE if the argument is a simple scalar, _NOT_ a far-
878 * absolute.
880 int is_really_simple (expr *vect)
882 while (vect->type && !vect->value)
883 vect++;
884 if (!vect->type)
885 return 1;
886 if (vect->type != EXPR_SIMPLE)
887 return 0;
888 do {
889 vect++;
890 } while (vect->type && !vect->value);
891 if (vect->type) return 0;
892 return 1;
896 * Return TRUE if the argument is relocatable (i.e. a simple
897 * scalar, plus at most one segment-base, plus possibly a WRT).
899 int is_reloc (expr *vect)
901 while (vect->type && !vect->value) /* skip initial value-0 terms */
902 vect++;
903 if (!vect->type) /* trivially return TRUE if nothing */
904 return 1; /* is present apart from value-0s */
905 if (vect->type < EXPR_SIMPLE) /* FALSE if a register is present */
906 return 0;
907 if (vect->type == EXPR_SIMPLE) { /* skip over a pure number term... */
908 do {
909 vect++;
910 } while (vect->type && !vect->value);
911 if (!vect->type) /* ...returning TRUE if that's all */
912 return 1;
914 if (vect->type == EXPR_WRT) { /* skip over a WRT term... */
915 do {
916 vect++;
917 } while (vect->type && !vect->value);
918 if (!vect->type) /* ...returning TRUE if that's all */
919 return 1;
921 if (vect->value != 0 && vect->value != 1)
922 return 0; /* segment base multiplier non-unity */
923 do { /* skip over _one_ seg-base term... */
924 vect++;
925 } while (vect->type && !vect->value);
926 if (!vect->type) /* ...returning TRUE if that's all */
927 return 1;
928 return 0; /* And return FALSE if there's more */
932 * Return TRUE if the argument contains an `unknown' part.
934 int is_unknown(expr *vect)
936 while (vect->type && vect->type < EXPR_UNKNOWN)
937 vect++;
938 return (vect->type == EXPR_UNKNOWN);
942 * Return TRUE if the argument contains nothing but an `unknown'
943 * part.
945 int is_just_unknown(expr *vect)
947 while (vect->type && !vect->value)
948 vect++;
949 return (vect->type == EXPR_UNKNOWN);
953 * Return the scalar part of a relocatable vector. (Including
954 * simple scalar vectors - those qualify as relocatable.)
956 long reloc_value (expr *vect)
958 while (vect->type && !vect->value)
959 vect++;
960 if (!vect->type) return 0;
961 if (vect->type == EXPR_SIMPLE)
962 return vect->value;
963 else
964 return 0;
968 * Return the segment number of a relocatable vector, or NO_SEG for
969 * simple scalars.
971 long reloc_seg (expr *vect)
973 while (vect->type && (vect->type == EXPR_WRT || !vect->value))
974 vect++;
975 if (vect->type == EXPR_SIMPLE) {
976 do {
977 vect++;
978 } while (vect->type && (vect->type == EXPR_WRT || !vect->value));
980 if (!vect->type)
981 return NO_SEG;
982 else
983 return vect->type - EXPR_SEGBASE;
987 * Return the WRT segment number of a relocatable vector, or NO_SEG
988 * if no WRT part is present.
990 long reloc_wrt (expr *vect)
992 while (vect->type && vect->type < EXPR_WRT)
993 vect++;
994 if (vect->type == EXPR_WRT) {
995 return vect->value;
996 } else
997 return NO_SEG;
1001 * Binary search.
1003 int bsi (char *string, char **array, int size)
1005 int i = -1, j = size; /* always, i < index < j */
1006 while (j-i >= 2) {
1007 int k = (i+j)/2;
1008 int l = strcmp(string, array[k]);
1009 if (l<0) /* it's in the first half */
1010 j = k;
1011 else if (l>0) /* it's in the second half */
1012 i = k;
1013 else /* we've got it :) */
1014 return k;
1016 return -1; /* we haven't got it :( */
1019 static char *file_name = NULL;
1020 static long line_number = 0;
1022 char *src_set_fname(char *newname)
1024 char *oldname = file_name;
1025 file_name = newname;
1026 return oldname;
1029 long src_set_linnum(long newline)
1031 long oldline = line_number;
1032 line_number = newline;
1033 return oldline;
1036 long src_get_linnum(void)
1038 return line_number;
1041 int src_get(long *xline, char **xname)
1043 if (!file_name || !*xname || strcmp(*xname, file_name))
1045 nasm_free(*xname);
1046 *xname = file_name ? nasm_strdup(file_name) : NULL;
1047 *xline = line_number;
1048 return -2;
1050 if (*xline != line_number)
1052 long tmp = line_number - *xline;
1053 *xline = line_number;
1054 return tmp;
1056 return 0;
1059 void nasm_quote(char **str)
1061 int ln=strlen(*str);
1062 char q=(*str)[0];
1063 char *p;
1064 if (ln>1 && (*str)[ln-1]==q && (q=='"' || q=='\''))
1065 return;
1066 q = '"';
1067 if (strchr(*str,q))
1068 q = '\'';
1069 p = nasm_malloc(ln+3);
1070 strcpy(p+1, *str);
1071 nasm_free(*str);
1072 p[ln+1] = p[0] = q;
1073 p[ln+2] = 0;
1074 *str = p;
1077 char *nasm_strcat(char *one, char *two)
1079 char *rslt;
1080 int l1=strlen(one);
1081 rslt = nasm_malloc(l1+strlen(two)+1);
1082 strcpy(rslt, one);
1083 strcpy(rslt+l1, two);
1084 return rslt;
1087 void null_debug_routine()
1090 struct dfmt null_debug_form = {
1091 "Null debug format",
1092 "null",
1093 null_debug_routine,
1094 null_debug_routine,
1095 null_debug_routine,
1096 null_debug_routine,
1097 null_debug_routine,
1098 null_debug_routine,
1099 null_debug_routine,
1102 struct dfmt *null_debug_arr[2] = { &null_debug_form, NULL };