regularized spelling of license to match name of LICENSE file
[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 license given in the file "LICENSE"
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**64 / radix. We can't do that in
287      * 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**63 by radix/2 instead.
290      */
291     checklimit = 0x8000000000000000ULL / (radix >> 1);
292
293     /*
294      * Calculate the highest allowable value for the last digit of a
295      * 64-bit constant... in radix 10, it is 6, otherwise it is 0
296      */
297     last = (radix == 10 ? 6 : 0);
298
299     result = 0;
300     while (*r && r < q) {
301         if (*r != '_') {
302             if (*r < '0' || (*r > '9' && *r < 'A')
303                 || (digit = numvalue(*r)) >= radix) {
304                 *error = true;
305                 return 0;
306             }
307             if (result > checklimit ||
308                 (result == checklimit && digit >= last)) {
309                 warn = true;
310             }
311
312             result = radix * result + digit;
313         }
314         r++;
315     }
316
317     if (warn)
318         nasm_malloc_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
319                           "numeric constant %s does not fit in 32 bits",
320                           str);
321
322     return result * sign;
323 }
324
325 int64_t readstrnum(char *str, int length, bool *warn)
326 {
327     int64_t charconst = 0;
328     int i;
329
330     *warn = false;
331
332     str += length;
333     if (globalbits == 64) {
334         for (i = 0; i < length; i++) {
335             if (charconst & 0xFF00000000000000ULL)
336                 *warn = true;
337             charconst = (charconst << 8) + (uint8_t)*--str;
338         }
339     } else {
340         for (i = 0; i < length; i++) {
341             if (charconst & 0xFF000000UL)
342                 *warn = true;
343             charconst = (charconst << 8) + (uint8_t)*--str;
344         }
345     }
346     return charconst;
347 }
348
349 static int32_t next_seg;
350
351 void seg_init(void)
352 {
353     next_seg = 0;
354 }
355
356 int32_t seg_alloc(void)
357 {
358     return (next_seg += 2) - 2;
359 }
360
361 #if X86_MEMORY
362
363 void fwriteint16_t(uint16_t data, FILE * fp)
364 {
365     fwrite(&data, 1, 2, fp);
366 }
367
368 void fwriteint32_t(uint32_t data, FILE * fp)
369 {
370     fwrite(&data, 1, 4, fp);
371 }
372
373 void fwriteint64_t(uint64_t data, FILE * fp)
374 {
375     fwrite(&data, 1, 8, fp);
376 }
377
378 void fwriteaddr(uint64_t data, int size, FILE * fp)
379 {
380     fwrite(&data, 1, size, fp);
381 }
382
383 #else /* !X86_MEMORY */
384
385 void fwriteint16_t(uint16_t data, FILE * fp)
386 {
387     char buffer[2], *p = buffer;
388     WRITESHORT(p, data);
389     fwrite(buffer, 1, 2, fp);
390 }
391
392 void fwriteint32_t(uint32_t data, FILE * fp)
393 {
394     char buffer[4], *p = buffer;
395     WRITELONG(p, data);
396     fwrite(buffer, 1, 4, fp);
397 }
398
399 void fwriteint64_t(uint64_t data, FILE * fp)
400 {
401     char buffer[8], *p = buffer;
402     WRITEDLONG(p, data);
403     fwrite(buffer, 1, 8, fp);
404 }
405
406 void fwriteaddr(uint64_t data, int size, FILE * fp)
407 {
408     char buffer[8], *p = buffer;
409     WRITEADDR(p, data, size);
410     fwrite(buffer, 1, size, fp);
411 }
412
413 #endif
414
415 void standard_extension(char *inname, char *outname, char *extension,
416                         efunc error)
417 {
418     char *p, *q;
419
420     if (*outname)               /* file name already exists, */
421         return;                 /* so do nothing */
422     q = inname;
423     p = outname;
424     while (*q)
425         *p++ = *q++;            /* copy, and find end of string */
426     *p = '\0';                  /* terminate it */
427     while (p > outname && *--p != '.') ;        /* find final period (or whatever) */
428     if (*p != '.')
429         while (*p)
430             p++;                /* go back to end if none found */
431     if (!strcmp(p, extension)) {        /* is the extension already there? */
432         if (*extension)
433             error(ERR_WARNING | ERR_NOFILE,
434                   "file name already ends in `%s': "
435                   "output will be in `nasm.out'", extension);
436         else
437             error(ERR_WARNING | ERR_NOFILE,
438                   "file name already has no extension: "
439                   "output will be in `nasm.out'");
440         strcpy(outname, "nasm.out");
441     } else
442         strcpy(p, extension);
443 }
444
445 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
446 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
447
448 #define LAYERSIZ(r) ( (r)->layers==0 ? RAA_BLKSIZE : RAA_LAYERSIZE )
449
450 static struct RAA *real_raa_init(int layers)
451 {
452     struct RAA *r;
453     int i;
454
455     if (layers == 0) {
456         r = nasm_zalloc(LEAFSIZ);
457         r->stepsize = 1L;
458     } else {
459         r = nasm_malloc(BRANCHSIZ);
460         r->layers = layers;
461         for (i = 0; i < RAA_LAYERSIZE; i++)
462             r->u.b.data[i] = NULL;
463         r->stepsize = RAA_BLKSIZE;
464         while (--layers)
465             r->stepsize *= RAA_LAYERSIZE;
466     }
467     return r;
468 }
469
470 struct RAA *raa_init(void)
471 {
472     return real_raa_init(0);
473 }
474
475 void raa_free(struct RAA *r)
476 {
477     if (r->layers) {
478         struct RAA **p;
479         for (p = r->u.b.data; p - r->u.b.data < RAA_LAYERSIZE; p++)
480             if (*p)
481                 raa_free(*p);
482     }
483     nasm_free(r);
484 }
485
486 int64_t raa_read(struct RAA *r, int32_t posn)
487 {
488     if (posn >= r->stepsize * LAYERSIZ(r))
489         return 0;               /* Return 0 for undefined entries */
490     while (r->layers > 0) {
491         ldiv_t l;
492         l = ldiv(posn, r->stepsize);
493         r = r->u.b.data[l.quot];
494         posn = l.rem;
495         if (!r)
496             return 0;           /* Return 0 for undefined entries */
497     }
498     return r->u.l.data[posn];
499 }
500
501 struct RAA *raa_write(struct RAA *r, int32_t posn, int64_t value)
502 {
503     struct RAA *result;
504
505     if (posn < 0)
506         nasm_malloc_error(ERR_PANIC, "negative position in raa_write");
507
508     while (r->stepsize * LAYERSIZ(r) <= posn) {
509         /*
510          * Must add a layer.
511          */
512         struct RAA *s;
513         int i;
514
515         s = nasm_malloc(BRANCHSIZ);
516         for (i = 0; i < RAA_LAYERSIZE; i++)
517             s->u.b.data[i] = NULL;
518         s->layers = r->layers + 1;
519         s->stepsize = LAYERSIZ(r) * r->stepsize;
520         s->u.b.data[0] = r;
521         r = s;
522     }
523
524     result = r;
525
526     while (r->layers > 0) {
527         ldiv_t l;
528         struct RAA **s;
529         l = ldiv(posn, r->stepsize);
530         s = &r->u.b.data[l.quot];
531         if (!*s)
532             *s = real_raa_init(r->layers - 1);
533         r = *s;
534         posn = l.rem;
535     }
536
537     r->u.l.data[posn] = value;
538
539     return result;
540 }
541
542 /* Aggregate SAA components smaller than this */
543 #define SAA_BLKLEN 65536
544
545 struct SAA *saa_init(size_t elem_len)
546 {
547     struct SAA *s;
548     char *data;
549
550     s = nasm_zalloc(sizeof(struct SAA));
551
552     if (elem_len >= SAA_BLKLEN)
553         s->blk_len = elem_len;
554     else
555         s->blk_len = SAA_BLKLEN - (SAA_BLKLEN % elem_len);
556
557     s->elem_len = elem_len;
558     s->length = s->blk_len;
559     data = nasm_malloc(s->blk_len);
560     s->nblkptrs = s->nblks = 1;
561     s->blk_ptrs = nasm_malloc(sizeof(char *));
562     s->blk_ptrs[0] = data;
563     s->wblk = s->rblk = &s->blk_ptrs[0];
564
565     return s;
566 }
567
568 void saa_free(struct SAA *s)
569 {
570     char **p;
571     size_t n;
572
573     for (p = s->blk_ptrs, n = s->nblks; n; p++, n--)
574         nasm_free(*p);
575
576     nasm_free(s->blk_ptrs);
577     nasm_free(s);
578 }
579
580 /* Add one allocation block to an SAA */
581 static void saa_extend(struct SAA *s)
582 {
583     size_t blkn = s->nblks++;
584
585     if (blkn >= s->nblkptrs) {
586         size_t rindex = s->rblk - s->blk_ptrs;
587         size_t windex = s->wblk - s->blk_ptrs;
588
589         s->nblkptrs <<= 1;
590         s->blk_ptrs = nasm_realloc(s->blk_ptrs, s->nblkptrs*sizeof(char *));
591
592         s->rblk = s->blk_ptrs + rindex;
593         s->wblk = s->blk_ptrs + windex;
594     }
595
596     s->blk_ptrs[blkn] = nasm_malloc(s->blk_len);
597     s->length += s->blk_len;
598 }
599
600 void *saa_wstruct(struct SAA *s)
601 {
602     void *p;
603
604     if (s->wpos % s->elem_len)
605             nasm_malloc_error(ERR_PANIC|ERR_NOFILE,
606                               "misaligned wpos in saa_wstruct");
607
608     if (s->wpos + s->elem_len > s->blk_len) {
609         if (s->wpos != s->blk_len)
610             nasm_malloc_error(ERR_PANIC|ERR_NOFILE,
611                               "unfilled block in saa_wstruct");
612
613         if (s->wptr + s->elem_len > s->length)
614             saa_extend(s);
615         s->wblk++;
616         s->wpos = 0;
617     }
618
619     p = *s->wblk + s->wpos;
620     s->wpos += s->elem_len;
621     s->wptr += s->elem_len;
622
623     if (s->wptr > s->datalen)
624         s->datalen = s->wptr;
625
626     return p;
627 }
628
629 void saa_wbytes(struct SAA *s, const void *data, size_t len)
630 {
631     const char *d = data;
632
633     while (len) {
634         size_t l = s->blk_len - s->wpos;
635         if (l > len)
636             l = len;
637         if (l) {
638             if (d) {
639                 memcpy(*s->wblk + s->wpos, d, l);
640                 d += l;
641             } else
642                 memset(*s->wblk + s->wpos, 0, l);
643             s->wpos += l;
644             s->wptr += l;
645             len -= l;
646
647             if (s->datalen < s->wptr)
648                 s->datalen = s->wptr;
649         }
650         if (len) {
651             if (s->wptr >= s->length)
652                 saa_extend(s);
653             s->wblk++;
654             s->wpos = 0;
655         }
656     }
657 }
658
659 void saa_rewind(struct SAA *s)
660 {
661     s->rblk = s->blk_ptrs;
662     s->rpos = s->rptr = 0;
663 }
664
665 void *saa_rstruct(struct SAA *s)
666 {
667     void *p;
668
669     if (s->rptr + s->elem_len > s->datalen)
670         return NULL;
671
672     if (s->rpos % s->elem_len)
673             nasm_malloc_error(ERR_PANIC|ERR_NOFILE,
674                               "misaligned rpos in saa_rstruct");
675
676     if (s->rpos + s->elem_len > s->blk_len) {
677         s->rblk++;
678         s->rpos = 0;
679     }
680
681     p = *s->rblk + s->rpos;
682     s->rpos += s->elem_len;
683     s->rptr += s->elem_len;
684
685     return p;
686 }
687
688 const void *saa_rbytes(struct SAA *s, size_t *lenp)
689 {
690     const void *p;
691     size_t len;
692
693     if (s->rptr >= s->datalen) {
694         *lenp = 0;
695         return NULL;
696     }
697
698     if (s->rpos >= s->blk_len) {
699         s->rblk++;
700         s->rpos = 0;
701     }
702
703     len = *lenp;
704     if (len > s->datalen - s->rptr)
705         len = s->datalen - s->rptr;
706     if (len > s->blk_len - s->rpos)
707         len = s->blk_len - s->rpos;
708
709     *lenp = len;
710     p = *s->rblk + s->rpos;
711
712     s->rpos += len;
713     s->rptr += len;
714
715     return p;
716 }
717
718 void saa_rnbytes(struct SAA *s, void *data, size_t len)
719 {
720     char *d = data;
721
722     if (s->rptr + len > s->datalen) {
723         nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_rnbytes");
724         return;
725     }
726
727     while (len) {
728         size_t l;
729         const void *p;
730
731         l = len;
732         p = saa_rbytes(s, &l);
733
734         memcpy(d, p, l);
735         d   += l;
736         len -= l;
737     }
738 }
739
740 /* Same as saa_rnbytes, except position the counter first */
741 void saa_fread(struct SAA *s, size_t posn, void *data, size_t len)
742 {
743     size_t ix;
744
745     if (posn+len > s->datalen) {
746         nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_fread");
747         return;
748     }
749
750     ix = posn / s->blk_len;
751     s->rptr = posn;
752     s->rpos = posn % s->blk_len;
753     s->rblk = &s->blk_ptrs[ix];
754
755     saa_rnbytes(s, data, len);
756 }
757
758 /* Same as saa_wbytes, except position the counter first */
759 void saa_fwrite(struct SAA *s, size_t posn, const void *data, size_t len)
760 {
761     size_t ix;
762
763     if (posn > s->datalen) {
764         /* Seek beyond the end of the existing array not supported */
765         nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_fwrite");
766         return;
767     }
768
769     ix = posn / s->blk_len;
770     s->wptr = posn;
771     s->wpos = posn % s->blk_len;
772     s->wblk = &s->blk_ptrs[ix];
773
774     if (!s->wpos) {
775         s->wpos = s->blk_len;
776         s->wblk--;
777     }
778
779     saa_wbytes(s, data, len);
780 }
781
782 void saa_fpwrite(struct SAA *s, FILE * fp)
783 {
784     const char *data;
785     size_t len;
786
787     saa_rewind(s);
788     while (len = s->datalen, (data = saa_rbytes(s, &len)) != NULL)
789         fwrite(data, 1, len, fp);
790 }
791
792 /*
793  * Common list of prefix names
794  */
795 static const char *prefix_names[] = {
796     "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
797     "repnz", "repz", "times"
798 };
799
800 const char *prefix_name(int token)
801 {
802     unsigned int prefix = token-PREFIX_ENUM_START;
803     if (prefix > elements(prefix_names))
804         return NULL;
805
806     return prefix_names[prefix];
807 }
808
809 /*
810  * Binary search.
811  */
812 int bsi(const char *string, const char **array, int size)
813 {
814     int i = -1, j = size;       /* always, i < index < j */
815     while (j - i >= 2) {
816         int k = (i + j) / 2;
817         int l = strcmp(string, array[k]);
818         if (l < 0)              /* it's in the first half */
819             j = k;
820         else if (l > 0)         /* it's in the second half */
821             i = k;
822         else                    /* we've got it :) */
823             return k;
824     }
825     return -1;                  /* we haven't got it :( */
826 }
827
828 int bsii(const char *string, const char **array, int size)
829 {
830     int i = -1, j = size;       /* always, i < index < j */
831     while (j - i >= 2) {
832         int k = (i + j) / 2;
833         int l = nasm_stricmp(string, array[k]);
834         if (l < 0)              /* it's in the first half */
835             j = k;
836         else if (l > 0)         /* it's in the second half */
837             i = k;
838         else                    /* we've got it :) */
839             return k;
840     }
841     return -1;                  /* we haven't got it :( */
842 }
843
844 static char *file_name = NULL;
845 static int32_t line_number = 0;
846
847 char *src_set_fname(char *newname)
848 {
849     char *oldname = file_name;
850     file_name = newname;
851     return oldname;
852 }
853
854 int32_t src_set_linnum(int32_t newline)
855 {
856     int32_t oldline = line_number;
857     line_number = newline;
858     return oldline;
859 }
860
861 int32_t src_get_linnum(void)
862 {
863     return line_number;
864 }
865
866 int src_get(int32_t *xline, char **xname)
867 {
868     if (!file_name || !*xname || strcmp(*xname, file_name)) {
869         nasm_free(*xname);
870         *xname = file_name ? nasm_strdup(file_name) : NULL;
871         *xline = line_number;
872         return -2;
873     }
874     if (*xline != line_number) {
875         int32_t tmp = line_number - *xline;
876         *xline = line_number;
877         return tmp;
878     }
879     return 0;
880 }
881
882 void nasm_quote(char **str)
883 {
884     int ln = strlen(*str);
885     char q = (*str)[0];
886     char *p;
887     if (ln > 1 && (*str)[ln - 1] == q && (q == '"' || q == '\''))
888         return;
889     q = '"';
890     if (strchr(*str, q))
891         q = '\'';
892     p = nasm_malloc(ln + 3);
893     strcpy(p + 1, *str);
894     nasm_free(*str);
895     p[ln + 1] = p[0] = q;
896     p[ln + 2] = 0;
897     *str = p;
898 }
899
900 char *nasm_strcat(char *one, char *two)
901 {
902     char *rslt;
903     int l1 = strlen(one);
904     rslt = nasm_malloc(l1 + strlen(two) + 1);
905     strcpy(rslt, one);
906     strcpy(rslt + l1, two);
907     return rslt;
908 }
909
910 void null_debug_init(struct ofmt *of, void *id, FILE * fp, efunc error)
911 {
912         (void)of;
913         (void)id;
914         (void)fp;
915         (void)error;
916 }
917 void null_debug_linenum(const char *filename, int32_t linenumber, int32_t segto)
918 {
919         (void)filename;
920         (void)linenumber;
921         (void)segto;
922 }
923 void null_debug_deflabel(char *name, int32_t segment, int64_t offset,
924                          int is_global, char *special)
925 {
926         (void)name;
927         (void)segment;
928         (void)offset;
929         (void)is_global;
930         (void)special;
931 }
932 void null_debug_routine(const char *directive, const char *params)
933 {
934         (void)directive;
935         (void)params;
936 }
937 void null_debug_typevalue(int32_t type)
938 {
939         (void)type;
940 }
941 void null_debug_output(int type, void *param)
942 {
943         (void)type;
944         (void)param;
945 }
946 void null_debug_cleanup(void)
947 {
948 }
949
950 struct dfmt null_debug_form = {
951     "Null debug format",
952     "null",
953     null_debug_init,
954     null_debug_linenum,
955     null_debug_deflabel,
956     null_debug_routine,
957     null_debug_typevalue,
958     null_debug_output,
959     null_debug_cleanup
960 };
961
962 struct dfmt *null_debug_arr[2] = { &null_debug_form, NULL };