Implement REL/ABS modifiers
[platform/upstream/nasm.git] / nasmlib.c
1 /* nasmlib.c    library routines for the Netwide Assembler
2  *
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  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <inttypes.h>
14
15 #include "nasm.h"
16 #include "nasmlib.h"
17 #include "insns.h"              /* For MAX_KEYWORD */
18
19 int globalbits = 0;    /* defined in nasm.h, works better here for ASM+DISASM */
20 static efunc nasm_malloc_error;
21
22 #ifdef LOGALLOC
23 static FILE *logfp;
24 #endif
25
26 void nasm_set_malloc_error(efunc error)
27 {
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
34 }
35
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
41 {
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, (int32_t)size, p);
49 #endif
50     return p;
51 }
52
53 #ifdef LOGALLOC
54 void *nasm_realloc_log(char *file, int line, void *q, size_t size)
55 #else
56 void *nasm_realloc(void *q, size_t size)
57 #endif
58 {
59     void *p = q ? realloc(q, size) : malloc(size);
60     if (!p)
61         nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
62 #ifdef LOGALLOC
63     else if (q)
64         fprintf(logfp, "%s %d realloc(%p,%ld) returns %p\n",
65                 file, line, q, (int32_t)size, p);
66     else
67         fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
68                 file, line, (int32_t)size, p);
69 #endif
70     return p;
71 }
72
73 #ifdef LOGALLOC
74 void nasm_free_log(char *file, int line, void *q)
75 #else
76 void nasm_free(void *q)
77 #endif
78 {
79     if (q) {
80         free(q);
81 #ifdef LOGALLOC
82         fprintf(logfp, "%s %d free(%p)\n", file, line, q);
83 #endif
84     }
85 }
86
87 #ifdef LOGALLOC
88 char *nasm_strdup_log(char *file, int line, const char *s)
89 #else
90 char *nasm_strdup(const char *s)
91 #endif
92 {
93     char *p;
94     int size = strlen(s) + 1;
95
96     p = malloc(size);
97     if (!p)
98         nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
99 #ifdef LOGALLOC
100     else
101         fprintf(logfp, "%s %d strdup(%ld) returns %p\n",
102                 file, line, (int32_t)size, p);
103 #endif
104     strcpy(p, s);
105     return p;
106 }
107
108 #ifdef LOGALLOC
109 char *nasm_strndup_log(char *file, int line, char *s, size_t len)
110 #else
111 char *nasm_strndup(char *s, size_t len)
112 #endif
113 {
114     char *p;
115     int size = len + 1;
116
117     p = malloc(size);
118     if (!p)
119         nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
120 #ifdef LOGALLOC
121     else
122         fprintf(logfp, "%s %d strndup(%ld) returns %p\n",
123                 file, line, (int32_t)size, p);
124 #endif
125     strncpy(p, s, len);
126     p[len] = '\0';
127     return p;
128 }
129
130 #if !defined(stricmp) && !defined(strcasecmp)
131 int nasm_stricmp(const char *s1, const char *s2)
132 {
133     while (*s1 && tolower(*s1) == tolower(*s2))
134         s1++, s2++;
135     if (!*s1 && !*s2)
136         return 0;
137     else if (tolower(*s1) < tolower(*s2))
138         return -1;
139     else
140         return 1;
141 }
142 #endif
143
144 #if !defined(strnicmp) && !defined(strncasecmp)
145 int nasm_strnicmp(const char *s1, const char *s2, int n)
146 {
147     while (n > 0 && *s1 && tolower(*s1) == tolower(*s2))
148         s1++, s2++, n--;
149     if ((!*s1 && !*s2) || n == 0)
150         return 0;
151     else if (tolower(*s1) < tolower(*s2))
152         return -1;
153     else
154         return 1;
155 }
156 #endif
157
158 #if !defined(strsep)
159 char *nasm_strsep(char **stringp, const char *delim)
160 {
161         char *s = *stringp;
162         char *e;
163
164         if (!s)
165                 return NULL;
166
167         e = strpbrk(s, delim);
168         if (e)
169                 *e++ = '\0';
170
171         *stringp = e;
172         return s;
173 }
174 #endif
175
176
177 #define lib_isnumchar(c)   ( isalnum(c) || (c) == '$')
178 #define numvalue(c)  ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
179
180 int64_t readnum(char *str, int *error)
181 {
182     char *r = str, *q;
183     int32_t radix;
184     uint64_t result, checklimit;
185     int digit, last;
186     int warn = FALSE;
187     int sign = 1;
188
189     *error = FALSE;
190
191     while (isspace(*r))
192         r++;                    /* find start of number */
193
194     /*
195      * If the number came from make_tok_num (as a result of an %assign), it
196      * might have a '-' built into it (rather than in a preceeding token).
197      */
198     if (*r == '-') {
199         r++;
200         sign = -1;
201     }
202
203     q = r;
204
205     while (lib_isnumchar(*q))
206         q++;                    /* find end of number */
207
208     /*
209      * If it begins 0x, 0X or $, or ends in H, it's in hex. if it
210      * ends in Q, it's octal. if it ends in B, it's binary.
211      * Otherwise, it's ordinary decimal.
212      */
213     if (*r == '0' && (r[1] == 'x' || r[1] == 'X'))
214         radix = 16, r += 2;
215     else if (*r == '$')
216         radix = 16, r++;
217     else if (q[-1] == 'H' || q[-1] == 'h')
218         radix = 16, q--;
219     else if (q[-1] == 'Q' || q[-1] == 'q' || q[-1] == 'O' || q[-1] == 'o')
220         radix = 8, q--;
221     else if (q[-1] == 'B' || q[-1] == 'b')
222         radix = 2, q--;
223     else
224         radix = 10;
225
226     /*
227      * If this number has been found for us by something other than
228      * the ordinary scanners, then it might be malformed by having
229      * nothing between the prefix and the suffix. Check this case
230      * now.
231      */
232     if (r >= q) {
233         *error = TRUE;
234         return 0;
235     }
236     
237     /*
238      * `checklimit' must be 2**(32|64) / radix. We can't do that in
239      * 32/64-bit arithmetic, which we're (probably) using, so we
240      * cheat: since we know that all radices we use are even, we
241      * can divide 2**(31|63) by radix/2 instead.
242      */
243     if (globalbits == 64)
244         checklimit = 0x8000000000000000ULL / (radix >> 1);
245     else
246         checklimit = 0x80000000UL / (radix >> 1);
247
248     /*
249      * Calculate the highest allowable value for the last digit of a
250      * 32-bit constant... in radix 10, it is 6, otherwise it is 0
251      */
252     last = (radix == 10 ? 6 : 0);
253
254     result = 0;
255     while (*r && r < q) {
256         if (*r < '0' || (*r > '9' && *r < 'A')
257             || (digit = numvalue(*r)) >= radix) {
258             *error = TRUE;
259             return 0;
260         }
261         if (result > checklimit || (result == checklimit && digit >= last)) {
262             warn = TRUE;
263         }
264
265         result = radix * result + digit;
266         r++;
267     }
268
269     if (warn)
270         nasm_malloc_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
271                           "numeric constant %s does not fit in 32 bits",
272                           str);
273
274     return result * sign;
275 }
276
277 int64_t readstrnum(char *str, int length, int *warn)
278 {
279     int64_t charconst = 0;
280     int i;
281
282     *warn = FALSE;
283
284     str += length;
285     if (globalbits == 64) {
286         for (i = 0; i < length; i++) {
287             if (charconst & 0xFF00000000000000ULL)
288                 *warn = TRUE;
289             charconst = (charconst << 8) + (uint8_t)*--str;
290         }
291     } else {
292         for (i = 0; i < length; i++) {
293             if (charconst & 0xFF000000UL)
294                 *warn = TRUE;
295             charconst = (charconst << 8) + (uint8_t)*--str;
296         }
297     }
298     return charconst;
299 }
300
301 static int32_t next_seg;
302
303 void seg_init(void)
304 {
305     next_seg = 0;
306 }
307
308 int32_t seg_alloc(void)
309 {
310     return (next_seg += 2) - 2;
311 }
312
313 void fwriteint16_t(int data, FILE * fp)
314 {
315     fputc((int)(data & 255), fp);
316     fputc((int)((data >> 8) & 255), fp);
317 }
318
319 void fwriteint32_t(int32_t data, FILE * fp)
320 {
321     fputc((int)(data & 255), fp);
322     fputc((int)((data >> 8) & 255), fp);
323     fputc((int)((data >> 16) & 255), fp);
324     fputc((int)((data >> 24) & 255), fp);
325 }
326
327 void fwriteint64_t(int64_t data, FILE * fp)
328 {
329     fputc((int)(data & 255), fp);
330     fputc((int)((data >> 8) & 255), fp);
331     fputc((int)((data >> 16) & 255), fp);
332     fputc((int)((data >> 24) & 255), fp);
333     fputc((int)((data >> 32) & 255), fp);
334     fputc((int)((data >> 40) & 255), fp);
335     fputc((int)((data >> 48) & 255), fp);
336     fputc((int)((data >> 56) & 255), fp);
337 }
338
339 void standard_extension(char *inname, char *outname, char *extension,
340                         efunc error)
341 {
342     char *p, *q;
343
344     if (*outname)               /* file name already exists, */
345         return;                 /* so do nothing */
346     q = inname;
347     p = outname;
348     while (*q)
349         *p++ = *q++;            /* copy, and find end of string */
350     *p = '\0';                  /* terminate it */
351     while (p > outname && *--p != '.') ;        /* find final period (or whatever) */
352     if (*p != '.')
353         while (*p)
354             p++;                /* go back to end if none found */
355     if (!strcmp(p, extension)) {        /* is the extension already there? */
356         if (*extension)
357             error(ERR_WARNING | ERR_NOFILE,
358                   "file name already ends in `%s': "
359                   "output will be in `nasm.out'", extension);
360         else
361             error(ERR_WARNING | ERR_NOFILE,
362                   "file name already has no extension: "
363                   "output will be in `nasm.out'");
364         strcpy(outname, "nasm.out");
365     } else
366         strcpy(p, extension);
367 }
368
369 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
370 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
371
372 #define LAYERSIZ(r) ( (r)->layers==0 ? RAA_BLKSIZE : RAA_LAYERSIZE )
373
374 static struct RAA *real_raa_init(int layers)
375 {
376     struct RAA *r;
377     int i;
378
379     if (layers == 0) {
380         r = nasm_malloc(LEAFSIZ);
381         r->layers = 0;
382         memset(r->u.l.data, 0, sizeof(r->u.l.data));
383         r->stepsize = 1L;
384     } else {
385         r = nasm_malloc(BRANCHSIZ);
386         r->layers = layers;
387         for (i = 0; i < RAA_LAYERSIZE; i++)
388             r->u.b.data[i] = NULL;
389         r->stepsize = RAA_BLKSIZE;
390         while (--layers)
391             r->stepsize *= RAA_LAYERSIZE;
392     }
393     return r;
394 }
395
396 struct RAA *raa_init(void)
397 {
398     return real_raa_init(0);
399 }
400
401 void raa_free(struct RAA *r)
402 {
403     if (r->layers == 0)
404         nasm_free(r);
405     else {
406         struct RAA **p;
407         for (p = r->u.b.data; p - r->u.b.data < RAA_LAYERSIZE; p++)
408             if (*p)
409                 raa_free(*p);
410     }
411 }
412
413 int32_t raa_read(struct RAA *r, int32_t posn)
414 {
415     if (posn >= r->stepsize * LAYERSIZ(r))
416         return 0;               /* Return 0 for undefined entries */
417     while (r->layers > 0) {
418         ldiv_t l;
419         l = ldiv(posn, r->stepsize);
420         r = r->u.b.data[l.quot];
421         posn = l.rem;
422         if (!r)
423             return 0;           /* Return 0 for undefined entries */
424     }
425     return r->u.l.data[posn];
426 }
427
428 struct RAA *raa_write(struct RAA *r, int32_t posn, int32_t value)
429 {
430     struct RAA *result;
431
432     if (posn < 0)
433         nasm_malloc_error(ERR_PANIC, "negative position in raa_write");
434
435     while (r->stepsize * LAYERSIZ(r) <= posn) {
436         /*
437          * Must add a layer.
438          */
439         struct RAA *s;
440         int i;
441
442         s = nasm_malloc(BRANCHSIZ);
443         for (i = 0; i < RAA_LAYERSIZE; i++)
444             s->u.b.data[i] = NULL;
445         s->layers = r->layers + 1;
446         s->stepsize = LAYERSIZ(r) * r->stepsize;
447         s->u.b.data[0] = r;
448         r = s;
449     }
450
451     result = r;
452
453     while (r->layers > 0) {
454         ldiv_t l;
455         struct RAA **s;
456         l = ldiv(posn, r->stepsize);
457         s = &r->u.b.data[l.quot];
458         if (!*s)
459             *s = real_raa_init(r->layers - 1);
460         r = *s;
461         posn = l.rem;
462     }
463
464     r->u.l.data[posn] = value;
465
466     return result;
467 }
468
469 #define SAA_MAXLEN 8192
470
471 struct SAA *saa_init(int32_t elem_len)
472 {
473     struct SAA *s;
474
475     if (elem_len > SAA_MAXLEN)
476         nasm_malloc_error(ERR_PANIC | ERR_NOFILE,
477                           "SAA with huge elements");
478
479     s = nasm_malloc(sizeof(struct SAA));
480     s->posn = s->start = 0L;
481     s->elem_len = elem_len;
482     s->length = SAA_MAXLEN - (SAA_MAXLEN % elem_len);
483     s->data = nasm_malloc(s->length);
484     s->next = NULL;
485     s->end = s;
486
487     return s;
488 }
489
490 void saa_free(struct SAA *s)
491 {
492     struct SAA *t;
493
494     while (s) {
495         t = s->next;
496         nasm_free(s->data);
497         nasm_free(s);
498         s = t;
499     }
500 }
501
502 void *saa_wstruct(struct SAA *s)
503 {
504     void *p;
505
506     if (s->end->length - s->end->posn < s->elem_len) {
507         s->end->next = nasm_malloc(sizeof(struct SAA));
508         s->end->next->start = s->end->start + s->end->posn;
509         s->end = s->end->next;
510         s->end->length = s->length;
511         s->end->next = NULL;
512         s->end->posn = 0L;
513         s->end->data = nasm_malloc(s->length);
514     }
515
516     p = s->end->data + s->end->posn;
517     s->end->posn += s->elem_len;
518     return p;
519 }
520
521 void saa_wbytes(struct SAA *s, const void *data, int32_t len)
522 {
523     const char *d = data;
524
525     while (len > 0) {
526         int32_t l = s->end->length - s->end->posn;
527         if (l > len)
528             l = len;
529         if (l > 0) {
530             if (d) {
531                 memcpy(s->end->data + s->end->posn, d, l);
532                 d += l;
533             } else
534                 memset(s->end->data + s->end->posn, 0, l);
535             s->end->posn += l;
536             len -= l;
537         }
538         if (len > 0) {
539             s->end->next = nasm_malloc(sizeof(struct SAA));
540             s->end->next->start = s->end->start + s->end->posn;
541             s->end = s->end->next;
542             s->end->length = s->length;
543             s->end->next = NULL;
544             s->end->posn = 0L;
545             s->end->data = nasm_malloc(s->length);
546         }
547     }
548 }
549
550 void saa_rewind(struct SAA *s)
551 {
552     s->rptr = s;
553     s->rpos = 0L;
554 }
555
556 void *saa_rstruct(struct SAA *s)
557 {
558     void *p;
559
560     if (!s->rptr)
561         return NULL;
562
563     if (s->rptr->posn - s->rpos < s->elem_len) {
564         s->rptr = s->rptr->next;
565         if (!s->rptr)
566             return NULL;        /* end of array */
567         s->rpos = 0L;
568     }
569
570     p = s->rptr->data + s->rpos;
571     s->rpos += s->elem_len;
572     return p;
573 }
574
575 void *saa_rbytes(struct SAA *s, int32_t *len)
576 {
577     void *p;
578
579     if (!s->rptr)
580         return NULL;
581
582     p = s->rptr->data + s->rpos;
583     *len = s->rptr->posn - s->rpos;
584     s->rptr = s->rptr->next;
585     s->rpos = 0L;
586     return p;
587 }
588
589 void saa_rnbytes(struct SAA *s, void *data, int32_t len)
590 {
591     char *d = data;
592
593     while (len > 0) {
594         int32_t l;
595
596         if (!s->rptr)
597             return;
598
599         l = s->rptr->posn - s->rpos;
600         if (l > len)
601             l = len;
602         if (l > 0) {
603             memcpy(d, s->rptr->data + s->rpos, l);
604             d += l;
605             s->rpos += l;
606             len -= l;
607         }
608         if (len > 0) {
609             s->rptr = s->rptr->next;
610             s->rpos = 0L;
611         }
612     }
613 }
614
615 void saa_fread(struct SAA *s, int32_t posn, void *data, int32_t len)
616 {
617     struct SAA *p;
618     int64_t pos;
619     char *cdata = data;
620
621     if (!s->rptr || posn < s->rptr->start)
622         saa_rewind(s);
623     p = s->rptr;
624     while (posn >= p->start + p->posn) {
625         p = p->next;
626         if (!p)
627             return;             /* what else can we do?! */
628     }
629
630     pos = posn - p->start;
631     while (len) {
632         int64_t l = p->posn - pos;
633         if (l > len)
634             l = len;
635         memcpy(cdata, p->data + pos, l);
636         len -= l;
637         cdata += l;
638         p = p->next;
639         if (!p)
640             return;
641         pos = 0LL;
642     }
643     s->rptr = p;
644 }
645
646 void saa_fwrite(struct SAA *s, int32_t posn, void *data, int32_t len)
647 {
648     struct SAA *p;
649     int64_t pos;
650     char *cdata = data;
651
652     if (!s->rptr || posn < s->rptr->start)
653         saa_rewind(s);
654     p = s->rptr;
655     while (posn >= p->start + p->posn) {
656         p = p->next;
657         if (!p)
658             return;             /* what else can we do?! */
659     }
660
661     pos = posn - p->start;
662     while (len) {
663         int64_t l = p->posn - pos;
664         if (l > len)
665             l = len;
666         memcpy(p->data + pos, cdata, l);
667         len -= l;
668         cdata += l;
669         p = p->next;
670         if (!p)
671             return;
672         pos = 0LL;
673     }
674     s->rptr = p;
675 }
676
677 void saa_fpwrite(struct SAA *s, FILE * fp)
678 {
679     char *data;
680     int32_t len;
681
682     saa_rewind(s);
683 //    while ((data = saa_rbytes(s, &len)))
684     for (; (data = saa_rbytes(s, &len));)
685         fwrite(data, 1, len, fp);
686 }
687
688 /*
689  * Register, instruction, condition-code and prefix keywords used
690  * by the scanner.
691  */
692 #include "names.c"
693 static const char *special_names[] = {
694     "abs", "byte", "dword", "far", "long", "near", "nosplit", "qword", "rel",
695     "short", "strict", "to", "tword", "word"
696 };
697 static const char *prefix_names[] = {
698     "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
699     "repnz", "repz", "times"
700 };
701
702 const char *prefix_name(int token)
703 {
704     unsigned int prefix = token-PREFIX_ENUM_START;
705     if (prefix > sizeof prefix_names / sizeof(const char *))
706         return NULL;
707
708     return prefix_names[prefix];
709 }
710
711 /*
712  * Standard scanner routine used by parser.c and some output
713  * formats. It keeps a succession of temporary-storage strings in
714  * stdscan_tempstorage, which can be cleared using stdscan_reset.
715  */
716 static char **stdscan_tempstorage = NULL;
717 static int stdscan_tempsize = 0, stdscan_templen = 0;
718 #define STDSCAN_TEMP_DELTA 256
719
720 static void stdscan_pop(void)
721 {
722     nasm_free(stdscan_tempstorage[--stdscan_templen]);
723 }
724
725 void stdscan_reset(void)
726 {
727     while (stdscan_templen > 0)
728         stdscan_pop();
729 }
730
731 /*
732  * Unimportant cleanup is done to avoid confusing people who are trying
733  * to debug real memory leaks
734  */
735 void nasmlib_cleanup(void)
736 {
737     stdscan_reset();
738     nasm_free(stdscan_tempstorage);
739 }
740
741 static char *stdscan_copy(char *p, int len)
742 {
743     char *text;
744
745     text = nasm_malloc(len + 1);
746     strncpy(text, p, len);
747     text[len] = '\0';
748
749     if (stdscan_templen >= stdscan_tempsize) {
750         stdscan_tempsize += STDSCAN_TEMP_DELTA;
751         stdscan_tempstorage = nasm_realloc(stdscan_tempstorage,
752                                            stdscan_tempsize *
753                                            sizeof(char *));
754     }
755     stdscan_tempstorage[stdscan_templen++] = text;
756
757     return text;
758 }
759
760 char *stdscan_bufptr = NULL;
761 int stdscan(void *private_data, struct tokenval *tv)
762 {
763     char ourcopy[MAX_KEYWORD + 1], *r, *s;
764
765     (void)private_data;         /* Don't warn that this parameter is unused */
766
767     while (isspace(*stdscan_bufptr))
768         stdscan_bufptr++;
769     if (!*stdscan_bufptr)
770         return tv->t_type = 0;
771
772     /* we have a token; either an id, a number or a char */
773     if (isidstart(*stdscan_bufptr) ||
774         (*stdscan_bufptr == '$' && isidstart(stdscan_bufptr[1]))) {
775         /* now we've got an identifier */
776         uint32_t i;
777         int is_sym = FALSE;
778
779         if (*stdscan_bufptr == '$') {
780             is_sym = TRUE;
781             stdscan_bufptr++;
782         }
783
784         r = stdscan_bufptr++;
785         /* read the entire buffer to advance the buffer pointer but... */
786         while (isidchar(*stdscan_bufptr))
787             stdscan_bufptr++;
788
789         /* ... copy only up to IDLEN_MAX-1 characters */
790         tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r < IDLEN_MAX ?
791                                      stdscan_bufptr - r : IDLEN_MAX - 1);
792
793         if (is_sym || stdscan_bufptr - r > MAX_KEYWORD)
794             return tv->t_type = TOKEN_ID;       /* bypass all other checks */
795
796         for (s = tv->t_charptr, r = ourcopy; *s; s++)
797             *r++ = tolower(*s);
798         *r = '\0';
799         /* right, so we have an identifier sitting in temp storage. now,
800          * is it actually a register or instruction name, or what? */
801         if ((tv->t_integer = bsi(ourcopy, reg_names,
802                                  elements(reg_names))) >= 0) {
803             tv->t_integer += EXPR_REG_START;
804             return tv->t_type = TOKEN_REG;
805         } else if ((tv->t_integer = bsi(ourcopy, insn_names,
806                                         elements(insn_names))) >= 0) {
807             return tv->t_type = TOKEN_INSN;
808         }
809         for (i = 0; i < elements(icn); i++)
810             if (!strncmp(ourcopy, icn[i], strlen(icn[i]))) {
811                 char *p = ourcopy + strlen(icn[i]);
812                 tv->t_integer = ico[i];
813                 if ((tv->t_inttwo = bsi(p, conditions,
814                                         elements(conditions))) >= 0)
815                     return tv->t_type = TOKEN_INSN;
816             }
817         if ((tv->t_integer = bsi(ourcopy, prefix_names,
818                                  elements(prefix_names))) >= 0) {
819             tv->t_integer += PREFIX_ENUM_START;
820             return tv->t_type = TOKEN_PREFIX;
821         }
822         if ((tv->t_integer = bsi(ourcopy, special_names,
823                                  elements(special_names))) >= 0)
824             return tv->t_type = TOKEN_SPECIAL;
825         if (!nasm_stricmp(ourcopy, "seg"))
826             return tv->t_type = TOKEN_SEG;
827         if (!nasm_stricmp(ourcopy, "wrt"))
828             return tv->t_type = TOKEN_WRT;
829         return tv->t_type = TOKEN_ID;
830     } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) {
831         /*
832          * It's a $ sign with no following hex number; this must
833          * mean it's a Here token ($), evaluating to the current
834          * assembly location, or a Base token ($$), evaluating to
835          * the base of the current segment.
836          */
837         stdscan_bufptr++;
838         if (*stdscan_bufptr == '$') {
839             stdscan_bufptr++;
840             return tv->t_type = TOKEN_BASE;
841         }
842         return tv->t_type = TOKEN_HERE;
843     } else if (isnumstart(*stdscan_bufptr)) {   /* now we've got a number */
844         int rn_error;
845
846         r = stdscan_bufptr++;
847         while (isnumchar(*stdscan_bufptr))
848             stdscan_bufptr++;
849
850         if (*stdscan_bufptr == '.') {
851             /*
852              * a floating point constant
853              */
854             stdscan_bufptr++;
855             while (isnumchar(*stdscan_bufptr) ||
856                    ((stdscan_bufptr[-1] == 'e'
857                      || stdscan_bufptr[-1] == 'E')
858                     && (*stdscan_bufptr == '-' || *stdscan_bufptr == '+'))) {
859                 stdscan_bufptr++;
860             }
861             tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
862             return tv->t_type = TOKEN_FLOAT;
863         }
864         r = stdscan_copy(r, stdscan_bufptr - r);
865         tv->t_integer = readnum(r, &rn_error);
866         stdscan_pop();
867         if (rn_error)
868             return tv->t_type = TOKEN_ERRNUM;   /* some malformation occurred */
869         tv->t_charptr = NULL;
870         return tv->t_type = TOKEN_NUM;
871     } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"') {     /* a char constant */
872         char quote = *stdscan_bufptr++, *r;
873         int rn_warn;
874         r = tv->t_charptr = stdscan_bufptr;
875         while (*stdscan_bufptr && *stdscan_bufptr != quote)
876             stdscan_bufptr++;
877         tv->t_inttwo = stdscan_bufptr - r;      /* store full version */
878         if (!*stdscan_bufptr)
879             return tv->t_type = TOKEN_ERRNUM;   /* unmatched quotes */
880         stdscan_bufptr++;       /* skip over final quote */
881         tv->t_integer = readstrnum(r, tv->t_inttwo, &rn_warn);
882         /* FIXME: rn_warn is not checked! */
883         return tv->t_type = TOKEN_NUM;
884     } else if (*stdscan_bufptr == ';') {        /* a comment has happened - stay */
885         return tv->t_type = 0;
886     } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
887         stdscan_bufptr += 2;
888         return tv->t_type = TOKEN_SHR;
889     } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
890         stdscan_bufptr += 2;
891         return tv->t_type = TOKEN_SHL;
892     } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
893         stdscan_bufptr += 2;
894         return tv->t_type = TOKEN_SDIV;
895     } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
896         stdscan_bufptr += 2;
897         return tv->t_type = TOKEN_SMOD;
898     } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
899         stdscan_bufptr += 2;
900         return tv->t_type = TOKEN_EQ;
901     } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
902         stdscan_bufptr += 2;
903         return tv->t_type = TOKEN_NE;
904     } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
905         stdscan_bufptr += 2;
906         return tv->t_type = TOKEN_NE;
907     } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
908         stdscan_bufptr += 2;
909         return tv->t_type = TOKEN_LE;
910     } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
911         stdscan_bufptr += 2;
912         return tv->t_type = TOKEN_GE;
913     } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
914         stdscan_bufptr += 2;
915         return tv->t_type = TOKEN_DBL_AND;
916     } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
917         stdscan_bufptr += 2;
918         return tv->t_type = TOKEN_DBL_XOR;
919     } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
920         stdscan_bufptr += 2;
921         return tv->t_type = TOKEN_DBL_OR;
922     } else                      /* just an ordinary char */
923         return tv->t_type = (uint8_t)(*stdscan_bufptr++);
924 }
925
926 /*
927  * Return TRUE if the argument is a simple scalar. (Or a far-
928  * absolute, which counts.)
929  */
930 int is_simple(expr * vect)
931 {
932     while (vect->type && !vect->value)
933         vect++;
934     if (!vect->type)
935         return 1;
936     if (vect->type != EXPR_SIMPLE)
937         return 0;
938     do {
939         vect++;
940     } while (vect->type && !vect->value);
941     if (vect->type && vect->type < EXPR_SEGBASE + SEG_ABS)
942         return 0;
943     return 1;
944 }
945
946 /*
947  * Return TRUE if the argument is a simple scalar, _NOT_ a far-
948  * absolute.
949  */
950 int is_really_simple(expr * vect)
951 {
952     while (vect->type && !vect->value)
953         vect++;
954     if (!vect->type)
955         return 1;
956     if (vect->type != EXPR_SIMPLE)
957         return 0;
958     do {
959         vect++;
960     } while (vect->type && !vect->value);
961     if (vect->type)
962         return 0;
963     return 1;
964 }
965
966 /*
967  * Return TRUE if the argument is relocatable (i.e. a simple
968  * scalar, plus at most one segment-base, plus possibly a WRT).
969  */
970 int is_reloc(expr * vect)
971 {
972     while (vect->type && !vect->value)  /* skip initial value-0 terms */
973         vect++;
974     if (!vect->type)            /* trivially return TRUE if nothing */
975         return 1;               /* is present apart from value-0s */
976     if (vect->type < EXPR_SIMPLE)       /* FALSE if a register is present */
977         return 0;
978     if (vect->type == EXPR_SIMPLE) {    /* skip over a pure number term... */
979         do {
980             vect++;
981         } while (vect->type && !vect->value);
982         if (!vect->type)        /* ...returning TRUE if that's all */
983             return 1;
984     }
985     if (vect->type == EXPR_WRT) {       /* skip over a WRT term... */
986         do {
987             vect++;
988         } while (vect->type && !vect->value);
989         if (!vect->type)        /* ...returning TRUE if that's all */
990             return 1;
991     }
992     if (vect->value != 0 && vect->value != 1)
993         return 0;               /* segment base multiplier non-unity */
994     do {                        /* skip over _one_ seg-base term... */
995         vect++;
996     } while (vect->type && !vect->value);
997     if (!vect->type)            /* ...returning TRUE if that's all */
998         return 1;
999     return 0;                   /* And return FALSE if there's more */
1000 }
1001
1002 /*
1003  * Return TRUE if the argument contains an `unknown' part.
1004  */
1005 int is_unknown(expr * vect)
1006 {
1007     while (vect->type && vect->type < EXPR_UNKNOWN)
1008         vect++;
1009     return (vect->type == EXPR_UNKNOWN);
1010 }
1011
1012 /*
1013  * Return TRUE if the argument contains nothing but an `unknown'
1014  * part.
1015  */
1016 int is_just_unknown(expr * vect)
1017 {
1018     while (vect->type && !vect->value)
1019         vect++;
1020     return (vect->type == EXPR_UNKNOWN);
1021 }
1022
1023 /*
1024  * Return the scalar part of a relocatable vector. (Including
1025  * simple scalar vectors - those qualify as relocatable.)
1026  */
1027 int64_t reloc_value(expr * vect)
1028 {
1029     while (vect->type && !vect->value)
1030         vect++;
1031     if (!vect->type)
1032         return 0;
1033     if (vect->type == EXPR_SIMPLE)
1034         return vect->value;
1035     else
1036         return 0;
1037 }
1038
1039 /*
1040  * Return the segment number of a relocatable vector, or NO_SEG for
1041  * simple scalars.
1042  */
1043 int32_t reloc_seg(expr * vect)
1044 {
1045     while (vect->type && (vect->type == EXPR_WRT || !vect->value))
1046         vect++;
1047     if (vect->type == EXPR_SIMPLE) {
1048         do {
1049             vect++;
1050         } while (vect->type && (vect->type == EXPR_WRT || !vect->value));
1051     }
1052     if (!vect->type)
1053         return NO_SEG;
1054     else
1055         return vect->type - EXPR_SEGBASE;
1056 }
1057
1058 /*
1059  * Return the WRT segment number of a relocatable vector, or NO_SEG
1060  * if no WRT part is present.
1061  */
1062 int32_t reloc_wrt(expr * vect)
1063 {
1064     while (vect->type && vect->type < EXPR_WRT)
1065         vect++;
1066     if (vect->type == EXPR_WRT) {
1067         return vect->value;
1068     } else
1069         return NO_SEG;
1070 }
1071
1072 /*
1073  * Binary search.
1074  */
1075 int bsi(char *string, const char **array, int size)
1076 {
1077     int i = -1, j = size;       /* always, i < index < j */
1078     while (j - i >= 2) {
1079         int k = (i + j) / 2;
1080         int l = strcmp(string, array[k]);
1081         if (l < 0)              /* it's in the first half */
1082             j = k;
1083         else if (l > 0)         /* it's in the second half */
1084             i = k;
1085         else                    /* we've got it :) */
1086             return k;
1087     }
1088     return -1;                  /* we haven't got it :( */
1089 }
1090
1091 static char *file_name = NULL;
1092 static int32_t line_number = 0;
1093
1094 char *src_set_fname(char *newname)
1095 {
1096     char *oldname = file_name;
1097     file_name = newname;
1098     return oldname;
1099 }
1100
1101 int32_t src_set_linnum(int32_t newline)
1102 {
1103     int32_t oldline = line_number;
1104     line_number = newline;
1105     return oldline;
1106 }
1107
1108 int32_t src_get_linnum(void)
1109 {
1110     return line_number;
1111 }
1112
1113 int src_get(int32_t *xline, char **xname)
1114 {
1115     if (!file_name || !*xname || strcmp(*xname, file_name)) {
1116         nasm_free(*xname);
1117         *xname = file_name ? nasm_strdup(file_name) : NULL;
1118         *xline = line_number;
1119         return -2;
1120     }
1121     if (*xline != line_number) {
1122         int32_t tmp = line_number - *xline;
1123         *xline = line_number;
1124         return tmp;
1125     }
1126     return 0;
1127 }
1128
1129 void nasm_quote(char **str)
1130 {
1131     int ln = strlen(*str);
1132     char q = (*str)[0];
1133     char *p;
1134     if (ln > 1 && (*str)[ln - 1] == q && (q == '"' || q == '\''))
1135         return;
1136     q = '"';
1137     if (strchr(*str, q))
1138         q = '\'';
1139     p = nasm_malloc(ln + 3);
1140     strcpy(p + 1, *str);
1141     nasm_free(*str);
1142     p[ln + 1] = p[0] = q;
1143     p[ln + 2] = 0;
1144     *str = p;
1145 }
1146
1147 char *nasm_strcat(char *one, char *two)
1148 {
1149     char *rslt;
1150     int l1 = strlen(one);
1151     rslt = nasm_malloc(l1 + strlen(two) + 1);
1152     strcpy(rslt, one);
1153     strcpy(rslt + l1, two);
1154     return rslt;
1155 }
1156
1157 void null_debug_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1158 {
1159         (void)of;
1160         (void)id;
1161         (void)fp;
1162         (void)error;
1163 }
1164 void null_debug_linenum(const char *filename, int32_t linenumber, int32_t segto)
1165 {
1166         (void)filename;
1167         (void)linenumber;
1168         (void)segto;    
1169 }
1170 void null_debug_deflabel(char *name, int32_t segment, int32_t offset,
1171                          int is_global, char *special)
1172 {
1173         (void)name;
1174         (void)segment;
1175         (void)offset;
1176         (void)is_global;
1177         (void)special;
1178 }
1179 void null_debug_routine(const char *directive, const char *params)
1180 {
1181         (void)directive;
1182         (void)params;
1183 }
1184 void null_debug_typevalue(int32_t type)
1185 {
1186         (void)type;
1187 }
1188 void null_debug_output(int type, void *param)
1189 {
1190         (void)type;
1191         (void)param;
1192 }
1193 void null_debug_cleanup(void)
1194 {
1195 }
1196
1197 struct dfmt null_debug_form = {
1198     "Null debug format",
1199     "null",
1200     null_debug_init,
1201     null_debug_linenum,
1202     null_debug_deflabel,
1203     null_debug_routine,
1204     null_debug_typevalue,
1205     null_debug_output,
1206     null_debug_cleanup
1207 };
1208
1209 struct dfmt *null_debug_arr[2] = { &null_debug_form, NULL };