Upgrade label functions to 64-bit
[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 "compiler.h"
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include <inttypes.h>
16
17 #include "nasm.h"
18 #include "nasmlib.h"
19 #include "insns.h"
20
21 int globalbits = 0;    /* defined in nasm.h, works better here for ASM+DISASM */
22 efunc nasm_malloc_error;        /* Exported for the benefit of vsnprintf.c */
23
24 #ifdef LOGALLOC
25 static FILE *logfp;
26 #endif
27
28 void nasm_set_malloc_error(efunc error)
29 {
30     nasm_malloc_error = error;
31 #ifdef LOGALLOC
32     logfp = fopen("malloc.log", "w");
33     setvbuf(logfp, NULL, _IOLBF, BUFSIZ);
34     fprintf(logfp, "null pointer is %p\n", NULL);
35 #endif
36 }
37
38 #ifdef LOGALLOC
39 void *nasm_malloc_log(char *file, int line, size_t size)
40 #else
41 void *nasm_malloc(size_t size)
42 #endif
43 {
44     void *p = malloc(size);
45     if (!p)
46         nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
47 #ifdef LOGALLOC
48     else
49         fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
50                 file, line, (long)size, p);
51 #endif
52     return p;
53 }
54
55 #ifdef LOGALLOC
56 void *nasm_zalloc_log(char *file, int line, size_t size)
57 #else
58 void *nasm_zalloc(size_t size)
59 #endif
60 {
61     void *p = calloc(size, 1);
62     if (!p)
63         nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
64 #ifdef LOGALLOC
65     else
66         fprintf(logfp, "%s %d calloc(%ld, 1) returns %p\n",
67                 file, line, (long)size, p);
68 #endif
69     return p;
70 }
71
72 #ifdef LOGALLOC
73 void *nasm_realloc_log(char *file, int line, void *q, size_t size)
74 #else
75 void *nasm_realloc(void *q, size_t size)
76 #endif
77 {
78     void *p = q ? realloc(q, size) : malloc(size);
79     if (!p)
80         nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
81 #ifdef LOGALLOC
82     else if (q)
83         fprintf(logfp, "%s %d realloc(%p,%ld) returns %p\n",
84                 file, line, q, (long)size, p);
85     else
86         fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
87                 file, line, (long)size, p);
88 #endif
89     return p;
90 }
91
92 #ifdef LOGALLOC
93 void nasm_free_log(char *file, int line, void *q)
94 #else
95 void nasm_free(void *q)
96 #endif
97 {
98     if (q) {
99         free(q);
100 #ifdef LOGALLOC
101         fprintf(logfp, "%s %d free(%p)\n", file, line, q);
102 #endif
103     }
104 }
105
106 #ifdef LOGALLOC
107 char *nasm_strdup_log(char *file, int line, const char *s)
108 #else
109 char *nasm_strdup(const char *s)
110 #endif
111 {
112     char *p;
113     int size = strlen(s) + 1;
114
115     p = malloc(size);
116     if (!p)
117         nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
118 #ifdef LOGALLOC
119     else
120         fprintf(logfp, "%s %d strdup(%ld) returns %p\n",
121                 file, line, (long)size, p);
122 #endif
123     strcpy(p, s);
124     return p;
125 }
126
127 #ifdef LOGALLOC
128 char *nasm_strndup_log(char *file, int line, char *s, size_t len)
129 #else
130 char *nasm_strndup(char *s, size_t len)
131 #endif
132 {
133     char *p;
134     int size = len + 1;
135
136     p = malloc(size);
137     if (!p)
138         nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory");
139 #ifdef LOGALLOC
140     else
141         fprintf(logfp, "%s %d strndup(%ld) returns %p\n",
142                 file, line, (long)size, p);
143 #endif
144     strncpy(p, s, len);
145     p[len] = '\0';
146     return p;
147 }
148
149 #ifndef nasm_stricmp
150 int nasm_stricmp(const char *s1, const char *s2)
151 {
152     while (*s1 && tolower(*s1) == tolower(*s2))
153         s1++, s2++;
154     if (!*s1 && !*s2)
155         return 0;
156     else if (tolower(*s1) < tolower(*s2))
157         return -1;
158     else
159         return 1;
160 }
161 #endif
162
163 #ifndef nasm_strnicmp
164 int nasm_strnicmp(const char *s1, const char *s2, int n)
165 {
166     while (n > 0 && *s1 && tolower(*s1) == tolower(*s2))
167         s1++, s2++, n--;
168     if ((!*s1 && !*s2) || n == 0)
169         return 0;
170     else if (tolower(*s1) < tolower(*s2))
171         return -1;
172     else
173         return 1;
174 }
175 #endif
176
177 #ifndef nasm_strsep
178 char *nasm_strsep(char **stringp, const char *delim)
179 {
180         char *s = *stringp;
181         char *e;
182
183         if (!s)
184                 return NULL;
185
186         e = strpbrk(s, delim);
187         if (e)
188                 *e++ = '\0';
189
190         *stringp = e;
191         return s;
192 }
193 #endif
194
195
196 #define lib_isnumchar(c)   (isalnum(c) || (c) == '$' || (c) == '_')
197 #define numvalue(c)  ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
198
199 static int radix_letter(char c)
200 {
201     switch (c) {
202     case 'b': case 'B':
203     case 'y': case 'Y':
204         return 2;               /* Binary */
205     case 'o': case 'O':
206     case 'q': case 'Q':
207         return 8;               /* Octal */
208     case 'h': case 'H':
209     case 'x': case 'X':
210         return 16;              /* Hexadecimal */
211     case 'd': case 'D':
212     case 't': case 'T':
213         return 10;              /* Decimal */
214     default:
215         return 0;               /* Not a known radix letter */
216     }
217 }
218
219 int64_t readnum(char *str, bool *error)
220 {
221     char *r = str, *q;
222     int32_t pradix, sradix, radix;
223     int plen, slen, len;
224     uint64_t result, checklimit;
225     int digit, last;
226     bool warn = false;
227     int sign = 1;
228
229     *error = false;
230
231     while (isspace(*r))
232         r++;                    /* find start of number */
233
234     /*
235      * If the number came from make_tok_num (as a result of an %assign), it
236      * might have a '-' built into it (rather than in a preceeding token).
237      */
238     if (*r == '-') {
239         r++;
240         sign = -1;
241     }
242
243     q = r;
244
245     while (lib_isnumchar(*q))
246         q++;                    /* find end of number */
247
248     len = q-r;
249     if (!len) {
250         /* Not numeric */
251         *error = true;
252         return 0;
253     }
254
255     /*
256      * Handle radix formats:
257      *
258      * 0<radix-letter><string>
259      * $<string>                (hexadecimal)
260      * <string><radix-letter>
261      */
262     pradix = sradix = 0;
263     plen = slen = 0;
264
265     if (len > 2 && *r == '0' && (pradix = radix_letter(r[1])) != 0)
266         plen = 2;
267     else if (len > 1 && *r == '$')
268         pradix = 16, plen = 1;
269
270     if (len > 1 && (sradix = radix_letter(q[-1])) != 0)
271         slen = 1;
272
273     if (pradix > sradix) {
274         radix = pradix;
275         r += plen;
276     } else if (sradix > pradix) {
277         radix = sradix;
278         q -= slen;
279     } else {
280         /* Either decimal, or invalid -- if invalid, we'll trip up
281            further down. */
282         radix = 10;
283     }
284
285     /*
286      * `checklimit' must be 2**(32|64) / radix. We can't do that in
287      * 32/64-bit arithmetic, which we're (probably) using, so we
288      * cheat: since we know that all radices we use are even, we
289      * can divide 2**(31|63) by radix/2 instead.
290      */
291     if (globalbits == 64)
292         checklimit = 0x8000000000000000ULL / (radix >> 1);
293     else
294         checklimit = 0x80000000UL / (radix >> 1);
295
296     /*
297      * Calculate the highest allowable value for the last digit of a
298      * 32-bit constant... in radix 10, it is 6, otherwise it is 0
299      */
300     last = (radix == 10 ? 6 : 0);
301
302     result = 0;
303     while (*r && r < q) {
304         if (*r != '_') {
305             if (*r < '0' || (*r > '9' && *r < 'A')
306                 || (digit = numvalue(*r)) >= radix) {
307                 *error = true;
308                 return 0;
309             }
310             if (result > checklimit ||
311                 (result == checklimit && digit >= last)) {
312                 warn = true;
313             }
314
315             result = radix * result + digit;
316         }
317         r++;
318     }
319
320     if (warn)
321         nasm_malloc_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
322                           "numeric constant %s does not fit in 32 bits",
323                           str);
324
325     return result * sign;
326 }
327
328 int64_t readstrnum(char *str, int length, bool *warn)
329 {
330     int64_t charconst = 0;
331     int i;
332
333     *warn = false;
334
335     str += length;
336     if (globalbits == 64) {
337         for (i = 0; i < length; i++) {
338             if (charconst & 0xFF00000000000000ULL)
339                 *warn = true;
340             charconst = (charconst << 8) + (uint8_t)*--str;
341         }
342     } else {
343         for (i = 0; i < length; i++) {
344             if (charconst & 0xFF000000UL)
345                 *warn = true;
346             charconst = (charconst << 8) + (uint8_t)*--str;
347         }
348     }
349     return charconst;
350 }
351
352 static int32_t next_seg;
353
354 void seg_init(void)
355 {
356     next_seg = 0;
357 }
358
359 int32_t seg_alloc(void)
360 {
361     return (next_seg += 2) - 2;
362 }
363
364 void fwriteint16_t(int data, FILE * fp)
365 {
366     fputc((int)(data & 255), fp);
367     fputc((int)((data >> 8) & 255), fp);
368 }
369
370 void fwriteint32_t(int32_t data, FILE * fp)
371 {
372     fputc((int)(data & 255), fp);
373     fputc((int)((data >> 8) & 255), fp);
374     fputc((int)((data >> 16) & 255), fp);
375     fputc((int)((data >> 24) & 255), fp);
376 }
377
378 void fwriteint64_t(int64_t data, FILE * fp)
379 {
380     fputc((int)(data & 255), fp);
381     fputc((int)((data >> 8) & 255), fp);
382     fputc((int)((data >> 16) & 255), fp);
383     fputc((int)((data >> 24) & 255), fp);
384     fputc((int)((data >> 32) & 255), fp);
385     fputc((int)((data >> 40) & 255), fp);
386     fputc((int)((data >> 48) & 255), fp);
387     fputc((int)((data >> 56) & 255), fp);
388 }
389
390 void standard_extension(char *inname, char *outname, char *extension,
391                         efunc error)
392 {
393     char *p, *q;
394
395     if (*outname)               /* file name already exists, */
396         return;                 /* so do nothing */
397     q = inname;
398     p = outname;
399     while (*q)
400         *p++ = *q++;            /* copy, and find end of string */
401     *p = '\0';                  /* terminate it */
402     while (p > outname && *--p != '.') ;        /* find final period (or whatever) */
403     if (*p != '.')
404         while (*p)
405             p++;                /* go back to end if none found */
406     if (!strcmp(p, extension)) {        /* is the extension already there? */
407         if (*extension)
408             error(ERR_WARNING | ERR_NOFILE,
409                   "file name already ends in `%s': "
410                   "output will be in `nasm.out'", extension);
411         else
412             error(ERR_WARNING | ERR_NOFILE,
413                   "file name already has no extension: "
414                   "output will be in `nasm.out'");
415         strcpy(outname, "nasm.out");
416     } else
417         strcpy(p, extension);
418 }
419
420 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
421 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
422
423 #define LAYERSIZ(r) ( (r)->layers==0 ? RAA_BLKSIZE : RAA_LAYERSIZE )
424
425 static struct RAA *real_raa_init(int layers)
426 {
427     struct RAA *r;
428     int i;
429
430     if (layers == 0) {
431         r = nasm_zalloc(LEAFSIZ);
432         r->stepsize = 1L;
433     } else {
434         r = nasm_malloc(BRANCHSIZ);
435         r->layers = layers;
436         for (i = 0; i < RAA_LAYERSIZE; i++)
437             r->u.b.data[i] = NULL;
438         r->stepsize = RAA_BLKSIZE;
439         while (--layers)
440             r->stepsize *= RAA_LAYERSIZE;
441     }
442     return r;
443 }
444
445 struct RAA *raa_init(void)
446 {
447     return real_raa_init(0);
448 }
449
450 void raa_free(struct RAA *r)
451 {
452     if (r->layers == 0)
453         nasm_free(r);
454     else {
455         struct RAA **p;
456         for (p = r->u.b.data; p - r->u.b.data < RAA_LAYERSIZE; p++)
457             if (*p)
458                 raa_free(*p);
459     }
460 }
461
462 int32_t raa_read(struct RAA *r, int32_t posn)
463 {
464     if (posn >= r->stepsize * LAYERSIZ(r))
465         return 0;               /* Return 0 for undefined entries */
466     while (r->layers > 0) {
467         ldiv_t l;
468         l = ldiv(posn, r->stepsize);
469         r = r->u.b.data[l.quot];
470         posn = l.rem;
471         if (!r)
472             return 0;           /* Return 0 for undefined entries */
473     }
474     return r->u.l.data[posn];
475 }
476
477 struct RAA *raa_write(struct RAA *r, int32_t posn, int32_t value)
478 {
479     struct RAA *result;
480
481     if (posn < 0)
482         nasm_malloc_error(ERR_PANIC, "negative position in raa_write");
483
484     while (r->stepsize * LAYERSIZ(r) <= posn) {
485         /*
486          * Must add a layer.
487          */
488         struct RAA *s;
489         int i;
490
491         s = nasm_malloc(BRANCHSIZ);
492         for (i = 0; i < RAA_LAYERSIZE; i++)
493             s->u.b.data[i] = NULL;
494         s->layers = r->layers + 1;
495         s->stepsize = LAYERSIZ(r) * r->stepsize;
496         s->u.b.data[0] = r;
497         r = s;
498     }
499
500     result = r;
501
502     while (r->layers > 0) {
503         ldiv_t l;
504         struct RAA **s;
505         l = ldiv(posn, r->stepsize);
506         s = &r->u.b.data[l.quot];
507         if (!*s)
508             *s = real_raa_init(r->layers - 1);
509         r = *s;
510         posn = l.rem;
511     }
512
513     r->u.l.data[posn] = value;
514
515     return result;
516 }
517
518 /* Aggregate SAA components smaller than this */
519 #define SAA_BLKLEN 65536
520
521 struct SAA *saa_init(size_t elem_len)
522 {
523     struct SAA *s;
524     char *data;
525
526     s = nasm_zalloc(sizeof(struct SAA));
527
528     if (elem_len >= SAA_BLKLEN)
529         s->blk_len = elem_len;
530     else
531         s->blk_len = SAA_BLKLEN - (SAA_BLKLEN % elem_len);
532
533     s->elem_len = elem_len;
534     s->length = s->blk_len;
535     data = nasm_malloc(s->blk_len);
536     s->nblkptrs = s->nblks = 1;
537     s->blk_ptrs = nasm_malloc(sizeof(char *));
538     s->blk_ptrs[0] = data;
539     s->wblk = s->rblk = &s->blk_ptrs[0];
540
541     return s;
542 }
543
544 void saa_free(struct SAA *s)
545 {
546     char **p;
547     size_t n;
548
549     for (p = s->blk_ptrs, n = s->nblks; n; p++, n--)
550         nasm_free(*p);
551
552     nasm_free(s->blk_ptrs);
553     nasm_free(s);
554 }
555
556 /* Add one allocation block to an SAA */
557 static void saa_extend(struct SAA *s)
558 {
559     size_t blkn = s->nblks++;
560
561     if (blkn >= s->nblkptrs) {
562         size_t rindex = s->rblk - s->blk_ptrs;
563         size_t windex = s->wblk - s->blk_ptrs;
564
565         s->nblkptrs <<= 1;
566         s->blk_ptrs = nasm_realloc(s->blk_ptrs, s->nblkptrs*sizeof(char *));
567
568         s->rblk = s->blk_ptrs + rindex;
569         s->wblk = s->blk_ptrs + windex;
570     }
571
572     s->blk_ptrs[blkn] = nasm_malloc(s->blk_len);
573     s->length += s->blk_len;
574 }
575
576 void *saa_wstruct(struct SAA *s)
577 {
578     void *p;
579
580     if (s->wpos % s->elem_len)
581             nasm_malloc_error(ERR_PANIC|ERR_NOFILE,
582                               "misaligned wpos in saa_wstruct");
583
584     if (s->wpos + s->elem_len > s->blk_len) {
585         if (s->wpos != s->blk_len)
586             nasm_malloc_error(ERR_PANIC|ERR_NOFILE,
587                               "unfilled block in saa_wstruct");
588
589         if (s->wptr + s->elem_len > s->length)
590             saa_extend(s);
591         s->wblk++;
592         s->wpos = 0;
593     }
594
595     p = *s->wblk + s->wpos;
596     s->wpos += s->elem_len;
597     s->wptr += s->elem_len;
598
599     if (s->wptr > s->datalen)
600         s->datalen = s->wptr;
601
602     return p;
603 }
604
605 void saa_wbytes(struct SAA *s, const void *data, size_t len)
606 {
607     const char *d = data;
608
609     while (len) {
610         size_t l = s->blk_len - s->wpos;
611         if (l > len)
612             l = len;
613         if (l) {
614             if (d) {
615                 memcpy(*s->wblk + s->wpos, d, l);
616                 d += l;
617             } else
618                 memset(*s->wblk + s->wpos, 0, l);
619             s->wpos += l;
620             s->wptr += l;
621             len -= l;
622
623             if (s->datalen < s->wptr)
624                 s->datalen = s->wptr;
625         }
626         if (len) {
627             if (s->wptr >= s->length)
628                 saa_extend(s);
629             s->wblk++;
630             s->wpos = 0;
631         }
632     }
633 }
634
635 void saa_rewind(struct SAA *s)
636 {
637     s->rblk = s->blk_ptrs;
638     s->rpos = s->rptr = 0;
639 }
640
641 void *saa_rstruct(struct SAA *s)
642 {
643     void *p;
644
645     if (s->rptr + s->elem_len > s->datalen)
646         return NULL;
647
648     if (s->rpos % s->elem_len)
649             nasm_malloc_error(ERR_PANIC|ERR_NOFILE,
650                               "misaligned rpos in saa_rstruct");
651
652     if (s->rpos + s->elem_len > s->blk_len) {
653         s->rblk++;
654         s->rpos = 0;
655     }
656
657     p = *s->rblk + s->rpos;
658     s->rpos += s->elem_len;
659     s->rptr += s->elem_len;
660
661     return p;
662 }
663
664 const void *saa_rbytes(struct SAA *s, size_t *lenp)
665 {
666     const void *p;
667     size_t len;
668
669     if (s->rptr >= s->datalen) {
670         *lenp = 0;
671         return NULL;
672     }
673
674     if (s->rpos >= s->blk_len) {
675         s->rblk++;
676         s->rpos = 0;
677     }
678
679     len = *lenp;
680     if (len > s->datalen - s->rptr)
681         len = s->datalen - s->rptr;
682     if (len > s->blk_len - s->rpos)
683         len = s->blk_len - s->rpos;
684
685     *lenp = len;
686     p = *s->rblk + s->rpos;
687
688     s->rpos += len;
689     s->rptr += len;
690
691     return p;
692 }
693
694 void saa_rnbytes(struct SAA *s, void *data, size_t len)
695 {
696     char *d = data;
697
698     if (s->rptr + len > s->datalen) {
699         nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_rnbytes");
700         return;
701     }
702
703     while (len) {
704         size_t l;
705         const void *p;
706
707         l = len;
708         p = saa_rbytes(s, &l);
709
710         memcpy(d, p, l);
711         d   += l;
712         len -= l;
713     }
714 }
715
716 /* Same as saa_rnbytes, except position the counter first */
717 void saa_fread(struct SAA *s, size_t posn, void *data, size_t len)
718 {
719     size_t ix;
720
721     if (posn+len > s->datalen) {
722         nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_fread");
723         return;
724     }
725
726     ix = posn / s->blk_len;
727     s->rptr = posn;
728     s->rpos = posn % s->blk_len;
729     s->rblk = &s->blk_ptrs[ix];
730
731     saa_rnbytes(s, data, len);
732 }
733
734 /* Same as saa_wbytes, except position the counter first */
735 void saa_fwrite(struct SAA *s, size_t posn, const void *data, size_t len)
736 {
737     size_t ix;
738
739     if (posn > s->datalen) {
740         /* Seek beyond the end of the existing array not supported */
741         nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_fwrite");
742         return;
743     }
744
745     ix = posn / s->blk_len;
746     s->wptr = posn;
747     s->wpos = posn % s->blk_len;
748     s->wblk = &s->blk_ptrs[ix];
749
750     if (!s->wpos) {
751         s->wpos = s->blk_len;
752         s->wblk--;
753     }
754
755     saa_wbytes(s, data, len);
756 }
757
758 void saa_fpwrite(struct SAA *s, FILE * fp)
759 {
760     const char *data;
761     size_t len;
762
763     saa_rewind(s);
764     while (len = s->datalen, (data = saa_rbytes(s, &len)) != NULL)
765         fwrite(data, 1, len, fp);
766 }
767
768 /*
769  * Common list of prefix names
770  */
771 static const char *prefix_names[] = {
772     "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
773     "repnz", "repz", "times"
774 };
775
776 const char *prefix_name(int token)
777 {
778     unsigned int prefix = token-PREFIX_ENUM_START;
779     if (prefix > sizeof prefix_names / sizeof(const char *))
780         return NULL;
781
782     return prefix_names[prefix];
783 }
784
785 /*
786  * Binary search.
787  */
788 int bsi(char *string, const char **array, int size)
789 {
790     int i = -1, j = size;       /* always, i < index < j */
791     while (j - i >= 2) {
792         int k = (i + j) / 2;
793         int l = strcmp(string, array[k]);
794         if (l < 0)              /* it's in the first half */
795             j = k;
796         else if (l > 0)         /* it's in the second half */
797             i = k;
798         else                    /* we've got it :) */
799             return k;
800     }
801     return -1;                  /* we haven't got it :( */
802 }
803
804 int bsii(char *string, const char **array, int size)
805 {
806     int i = -1, j = size;       /* always, i < index < j */
807     while (j - i >= 2) {
808         int k = (i + j) / 2;
809         int l = nasm_stricmp(string, array[k]);
810         if (l < 0)              /* it's in the first half */
811             j = k;
812         else if (l > 0)         /* it's in the second half */
813             i = k;
814         else                    /* we've got it :) */
815             return k;
816     }
817     return -1;                  /* we haven't got it :( */
818 }
819
820 static char *file_name = NULL;
821 static int32_t line_number = 0;
822
823 char *src_set_fname(char *newname)
824 {
825     char *oldname = file_name;
826     file_name = newname;
827     return oldname;
828 }
829
830 int32_t src_set_linnum(int32_t newline)
831 {
832     int32_t oldline = line_number;
833     line_number = newline;
834     return oldline;
835 }
836
837 int32_t src_get_linnum(void)
838 {
839     return line_number;
840 }
841
842 int src_get(int32_t *xline, char **xname)
843 {
844     if (!file_name || !*xname || strcmp(*xname, file_name)) {
845         nasm_free(*xname);
846         *xname = file_name ? nasm_strdup(file_name) : NULL;
847         *xline = line_number;
848         return -2;
849     }
850     if (*xline != line_number) {
851         int32_t tmp = line_number - *xline;
852         *xline = line_number;
853         return tmp;
854     }
855     return 0;
856 }
857
858 void nasm_quote(char **str)
859 {
860     int ln = strlen(*str);
861     char q = (*str)[0];
862     char *p;
863     if (ln > 1 && (*str)[ln - 1] == q && (q == '"' || q == '\''))
864         return;
865     q = '"';
866     if (strchr(*str, q))
867         q = '\'';
868     p = nasm_malloc(ln + 3);
869     strcpy(p + 1, *str);
870     nasm_free(*str);
871     p[ln + 1] = p[0] = q;
872     p[ln + 2] = 0;
873     *str = p;
874 }
875
876 char *nasm_strcat(char *one, char *two)
877 {
878     char *rslt;
879     int l1 = strlen(one);
880     rslt = nasm_malloc(l1 + strlen(two) + 1);
881     strcpy(rslt, one);
882     strcpy(rslt + l1, two);
883     return rslt;
884 }
885
886 void null_debug_init(struct ofmt *of, void *id, FILE * fp, efunc error)
887 {
888         (void)of;
889         (void)id;
890         (void)fp;
891         (void)error;
892 }
893 void null_debug_linenum(const char *filename, int32_t linenumber, int32_t segto)
894 {
895         (void)filename;
896         (void)linenumber;
897         (void)segto;
898 }
899 void null_debug_deflabel(char *name, int32_t segment, int64_t offset,
900                          int is_global, char *special)
901 {
902         (void)name;
903         (void)segment;
904         (void)offset;
905         (void)is_global;
906         (void)special;
907 }
908 void null_debug_routine(const char *directive, const char *params)
909 {
910         (void)directive;
911         (void)params;
912 }
913 void null_debug_typevalue(int32_t type)
914 {
915         (void)type;
916 }
917 void null_debug_output(int type, void *param)
918 {
919         (void)type;
920         (void)param;
921 }
922 void null_debug_cleanup(void)
923 {
924 }
925
926 struct dfmt null_debug_form = {
927     "Null debug format",
928     "null",
929     null_debug_init,
930     null_debug_linenum,
931     null_debug_deflabel,
932     null_debug_routine,
933     null_debug_typevalue,
934     null_debug_output,
935     null_debug_cleanup
936 };
937
938 struct dfmt *null_debug_arr[2] = { &null_debug_form, NULL };