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