1 /* nasmlib.c library routines for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
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 */
28 void nasm_set_malloc_error(efunc error
)
30 nasm_malloc_error
= error
;
32 logfp
= fopen("malloc.log", "w");
33 setvbuf(logfp
, NULL
, _IOLBF
, BUFSIZ
);
34 fprintf(logfp
, "null pointer is %p\n", NULL
);
39 void *nasm_malloc_log(char *file
, int line
, size_t size
)
41 void *nasm_malloc(size_t size
)
44 void *p
= malloc(size
);
46 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
49 fprintf(logfp
, "%s %d malloc(%ld) returns %p\n",
50 file
, line
, (long)size
, p
);
56 void *nasm_zalloc_log(char *file
, int line
, size_t size
)
58 void *nasm_zalloc(size_t size
)
61 void *p
= calloc(size
, 1);
63 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
66 fprintf(logfp
, "%s %d calloc(%ld, 1) returns %p\n",
67 file
, line
, (long)size
, p
);
73 void *nasm_realloc_log(char *file
, int line
, void *q
, size_t size
)
75 void *nasm_realloc(void *q
, size_t size
)
78 void *p
= q
? realloc(q
, size
) : malloc(size
);
80 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
83 fprintf(logfp
, "%s %d realloc(%p,%ld) returns %p\n",
84 file
, line
, q
, (long)size
, p
);
86 fprintf(logfp
, "%s %d malloc(%ld) returns %p\n",
87 file
, line
, (long)size
, p
);
93 void nasm_free_log(char *file
, int line
, void *q
)
95 void nasm_free(void *q
)
100 fprintf(logfp
, "%s %d free(%p)\n", file
, line
, q
);
107 char *nasm_strdup_log(char *file
, int line
, const char *s
)
109 char *nasm_strdup(const char *s
)
113 int size
= strlen(s
) + 1;
117 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
120 fprintf(logfp
, "%s %d strdup(%ld) returns %p\n",
121 file
, line
, (long)size
, p
);
128 char *nasm_strndup_log(char *file
, int line
, char *s
, size_t len
)
130 char *nasm_strndup(char *s
, size_t len
)
138 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
141 fprintf(logfp
, "%s %d strndup(%ld) returns %p\n",
142 file
, line
, (long)size
, p
);
150 int nasm_stricmp(const char *s1
, const char *s2
)
152 while (*s1
&& tolower(*s1
) == tolower(*s2
))
156 else if (tolower(*s1
) < tolower(*s2
))
163 #ifndef nasm_strnicmp
164 int nasm_strnicmp(const char *s1
, const char *s2
, int n
)
166 while (n
> 0 && *s1
&& tolower(*s1
) == tolower(*s2
))
168 if ((!*s1
&& !*s2
) || n
== 0)
170 else if (tolower(*s1
) < tolower(*s2
))
178 char *nasm_strsep(char **stringp
, const char *delim
)
186 e
= strpbrk(s
, delim
);
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')
199 static int radix_letter(char c
)
204 return 2; /* Binary */
207 return 8; /* Octal */
210 return 16; /* Hexadecimal */
213 return 10; /* Decimal */
215 return 0; /* Not a known radix letter */
219 int64_t readnum(char *str
, bool *error
)
222 int32_t pradix
, sradix
, radix
;
224 uint64_t result
, checklimit
;
232 r
++; /* find start of number */
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).
245 while (lib_isnumchar(*q
))
246 q
++; /* find end of number */
256 * Handle radix formats:
258 * 0<radix-letter><string>
259 * $<string> (hexadecimal)
260 * <string><radix-letter>
265 if (len
> 2 && *r
== '0' && (pradix
= radix_letter(r
[1])) != 0)
267 else if (len
> 1 && *r
== '$')
268 pradix
= 16, plen
= 1;
270 if (len
> 1 && (sradix
= radix_letter(q
[-1])) != 0)
273 if (pradix
> sradix
) {
276 } else if (sradix
> pradix
) {
280 /* Either decimal, or invalid -- if invalid, we'll trip up
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.
291 checklimit
= 0x8000000000000000ULL
/ (radix
>> 1);
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
297 last
= (radix
== 10 ? 6 : 0);
300 while (*r
&& r
< q
) {
302 if (*r
< '0' || (*r
> '9' && *r
< 'A')
303 || (digit
= numvalue(*r
)) >= radix
) {
307 if (result
> checklimit
||
308 (result
== checklimit
&& digit
>= last
)) {
312 result
= radix
* result
+ digit
;
318 nasm_malloc_error(ERR_WARNING
| ERR_PASS1
| ERR_WARN_NOV
,
319 "numeric constant %s does not fit in 64 bits",
322 return result
* sign
;
325 int64_t readstrnum(char *str
, int length
, bool *warn
)
327 int64_t charconst
= 0;
333 if (globalbits
== 64) {
334 for (i
= 0; i
< length
; i
++) {
335 if (charconst
& 0xFF00000000000000ULL
)
337 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
340 for (i
= 0; i
< length
; i
++) {
341 if (charconst
& 0xFF000000UL
)
343 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
349 static int32_t next_seg
;
356 int32_t seg_alloc(void)
358 return (next_seg
+= 2) - 2;
363 void fwriteint16_t(uint16_t data
, FILE * fp
)
365 fwrite(&data
, 1, 2, fp
);
368 void fwriteint32_t(uint32_t data
, FILE * fp
)
370 fwrite(&data
, 1, 4, fp
);
373 void fwriteint64_t(uint64_t data
, FILE * fp
)
375 fwrite(&data
, 1, 8, fp
);
378 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
380 fwrite(&data
, 1, size
, fp
);
383 #else /* !X86_MEMORY */
385 void fwriteint16_t(uint16_t data
, FILE * fp
)
387 char buffer
[2], *p
= buffer
;
389 fwrite(buffer
, 1, 2, fp
);
392 void fwriteint32_t(uint32_t data
, FILE * fp
)
394 char buffer
[4], *p
= buffer
;
396 fwrite(buffer
, 1, 4, fp
);
399 void fwriteint64_t(uint64_t data
, FILE * fp
)
401 char buffer
[8], *p
= buffer
;
403 fwrite(buffer
, 1, 8, fp
);
406 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
408 char buffer
[8], *p
= buffer
;
409 WRITEADDR(p
, data
, size
);
410 fwrite(buffer
, 1, size
, fp
);
415 void standard_extension(char *inname
, char *outname
, char *extension
,
420 if (*outname
) /* file name already exists, */
421 return; /* so do nothing */
425 *p
++ = *q
++; /* copy, and find end of string */
426 *p
= '\0'; /* terminate it */
427 while (p
> outname
&& *--p
!= '.') ; /* find final period (or whatever) */
430 p
++; /* go back to end if none found */
431 if (!strcmp(p
, extension
)) { /* is the extension already there? */
433 error(ERR_WARNING
| ERR_NOFILE
,
434 "file name already ends in `%s': "
435 "output will be in `nasm.out'", extension
);
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");
442 strcpy(p
, extension
);
445 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
446 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
448 #define LAYERSHIFT(r) ( (r)->layers==0 ? RAA_BLKSHIFT : RAA_LAYERSHIFT )
450 static struct RAA
*real_raa_init(int layers
)
456 r
= nasm_zalloc(LEAFSIZ
);
459 r
= nasm_malloc(BRANCHSIZ
);
461 for (i
= 0; i
< RAA_LAYERSIZE
; i
++)
462 r
->u
.b
.data
[i
] = NULL
;
463 r
->shift
= (RAA_BLKSHIFT
-RAA_LAYERSHIFT
) + layers
*RAA_LAYERSHIFT
;
468 struct RAA
*raa_init(void)
470 return real_raa_init(0);
473 void raa_free(struct RAA
*r
)
477 for (p
= r
->u
.b
.data
; p
- r
->u
.b
.data
< RAA_LAYERSIZE
; p
++)
484 int64_t raa_read(struct RAA
*r
, int32_t posn
)
486 if ((uint32_t)posn
>= (UINT32_C(1) << (r
->shift
+ LAYERSHIFT(r
))))
487 return 0; /* Return 0 for undefined entries */
488 while (r
->layers
> 0) {
489 int32_t l
= posn
>> r
->shift
;
490 posn
&= (UINT32_C(1) << r
->shift
)-1;
493 return 0; /* Return 0 for undefined entries */
495 return r
->u
.l
.data
[posn
];
498 struct RAA
*raa_write(struct RAA
*r
, int32_t posn
, int64_t value
)
503 nasm_malloc_error(ERR_PANIC
, "negative position in raa_write");
505 while ((UINT32_C(1) << (r
->shift
+LAYERSHIFT(r
))) <= (uint32_t)posn
) {
512 s
= nasm_malloc(BRANCHSIZ
);
513 for (i
= 0; i
< RAA_LAYERSIZE
; i
++)
514 s
->u
.b
.data
[i
] = NULL
;
515 s
->layers
= r
->layers
+ 1;
516 s
->shift
= LAYERSHIFT(r
) + r
->shift
;
523 while (r
->layers
> 0) {
525 int32_t l
= posn
>> r
->shift
;
526 posn
&= (UINT32_C(1) << r
->shift
)-1;
529 *s
= real_raa_init(r
->layers
- 1);
533 r
->u
.l
.data
[posn
] = value
;
538 /* Aggregate SAA components smaller than this */
539 #define SAA_BLKLEN 65536
541 struct SAA
*saa_init(size_t elem_len
)
546 s
= nasm_zalloc(sizeof(struct SAA
));
548 if (elem_len
>= SAA_BLKLEN
)
549 s
->blk_len
= elem_len
;
551 s
->blk_len
= SAA_BLKLEN
- (SAA_BLKLEN
% elem_len
);
553 s
->elem_len
= elem_len
;
554 s
->length
= s
->blk_len
;
555 data
= nasm_malloc(s
->blk_len
);
556 s
->nblkptrs
= s
->nblks
= 1;
557 s
->blk_ptrs
= nasm_malloc(sizeof(char *));
558 s
->blk_ptrs
[0] = data
;
559 s
->wblk
= s
->rblk
= &s
->blk_ptrs
[0];
564 void saa_free(struct SAA
*s
)
569 for (p
= s
->blk_ptrs
, n
= s
->nblks
; n
; p
++, n
--)
572 nasm_free(s
->blk_ptrs
);
576 /* Add one allocation block to an SAA */
577 static void saa_extend(struct SAA
*s
)
579 size_t blkn
= s
->nblks
++;
581 if (blkn
>= s
->nblkptrs
) {
582 size_t rindex
= s
->rblk
- s
->blk_ptrs
;
583 size_t windex
= s
->wblk
- s
->blk_ptrs
;
586 s
->blk_ptrs
= nasm_realloc(s
->blk_ptrs
, s
->nblkptrs
*sizeof(char *));
588 s
->rblk
= s
->blk_ptrs
+ rindex
;
589 s
->wblk
= s
->blk_ptrs
+ windex
;
592 s
->blk_ptrs
[blkn
] = nasm_malloc(s
->blk_len
);
593 s
->length
+= s
->blk_len
;
596 void *saa_wstruct(struct SAA
*s
)
600 if (s
->wpos
% s
->elem_len
)
601 nasm_malloc_error(ERR_PANIC
|ERR_NOFILE
,
602 "misaligned wpos in saa_wstruct");
604 if (s
->wpos
+ s
->elem_len
> s
->blk_len
) {
605 if (s
->wpos
!= s
->blk_len
)
606 nasm_malloc_error(ERR_PANIC
|ERR_NOFILE
,
607 "unfilled block in saa_wstruct");
609 if (s
->wptr
+ s
->elem_len
> s
->length
)
615 p
= *s
->wblk
+ s
->wpos
;
616 s
->wpos
+= s
->elem_len
;
617 s
->wptr
+= s
->elem_len
;
619 if (s
->wptr
> s
->datalen
)
620 s
->datalen
= s
->wptr
;
625 void saa_wbytes(struct SAA
*s
, const void *data
, size_t len
)
627 const char *d
= data
;
630 size_t l
= s
->blk_len
- s
->wpos
;
635 memcpy(*s
->wblk
+ s
->wpos
, d
, l
);
638 memset(*s
->wblk
+ s
->wpos
, 0, l
);
643 if (s
->datalen
< s
->wptr
)
644 s
->datalen
= s
->wptr
;
647 if (s
->wptr
>= s
->length
)
655 /* write unsigned LEB128 value to SAA */
656 void saa_wleb128u(struct SAA
*psaa
, int value
)
658 char temp
[64], *ptemp
;
668 if (value
!= 0) /* more bytes to come */
673 } while (value
!= 0);
674 saa_wbytes(psaa
, temp
, len
);
677 /* write signed LEB128 value to SAA */
678 void saa_wleb128s(struct SAA
*psaa
, int value
)
680 char temp
[64], *ptemp
;
687 negative
= (value
< 0);
688 size
= sizeof(int) * 8;
696 value
|= - (1 <<(size
- 7));
697 /* sign bit of byte is second high order bit (0x40) */
698 if ((value
== 0 && ! (byte
& 0x40)) ||
699 ((value
== -1) && (byte
& 0x40)))
707 saa_wbytes(psaa
, temp
, len
);
710 void saa_rewind(struct SAA
*s
)
712 s
->rblk
= s
->blk_ptrs
;
713 s
->rpos
= s
->rptr
= 0;
716 void *saa_rstruct(struct SAA
*s
)
720 if (s
->rptr
+ s
->elem_len
> s
->datalen
)
723 if (s
->rpos
% s
->elem_len
)
724 nasm_malloc_error(ERR_PANIC
|ERR_NOFILE
,
725 "misaligned rpos in saa_rstruct");
727 if (s
->rpos
+ s
->elem_len
> s
->blk_len
) {
732 p
= *s
->rblk
+ s
->rpos
;
733 s
->rpos
+= s
->elem_len
;
734 s
->rptr
+= s
->elem_len
;
739 const void *saa_rbytes(struct SAA
*s
, size_t *lenp
)
744 if (s
->rptr
>= s
->datalen
) {
749 if (s
->rpos
>= s
->blk_len
) {
755 if (len
> s
->datalen
- s
->rptr
)
756 len
= s
->datalen
- s
->rptr
;
757 if (len
> s
->blk_len
- s
->rpos
)
758 len
= s
->blk_len
- s
->rpos
;
761 p
= *s
->rblk
+ s
->rpos
;
769 void saa_rnbytes(struct SAA
*s
, void *data
, size_t len
)
773 if (s
->rptr
+ len
> s
->datalen
) {
774 nasm_malloc_error(ERR_PANIC
|ERR_NOFILE
, "overrun in saa_rnbytes");
783 p
= saa_rbytes(s
, &l
);
791 /* Same as saa_rnbytes, except position the counter first */
792 void saa_fread(struct SAA
*s
, size_t posn
, void *data
, size_t len
)
796 if (posn
+len
> s
->datalen
) {
797 nasm_malloc_error(ERR_PANIC
|ERR_NOFILE
, "overrun in saa_fread");
801 ix
= posn
/ s
->blk_len
;
803 s
->rpos
= posn
% s
->blk_len
;
804 s
->rblk
= &s
->blk_ptrs
[ix
];
806 saa_rnbytes(s
, data
, len
);
809 /* Same as saa_wbytes, except position the counter first */
810 void saa_fwrite(struct SAA
*s
, size_t posn
, const void *data
, size_t len
)
814 if (posn
> s
->datalen
) {
815 /* Seek beyond the end of the existing array not supported */
816 nasm_malloc_error(ERR_PANIC
|ERR_NOFILE
, "overrun in saa_fwrite");
820 ix
= posn
/ s
->blk_len
;
822 s
->wpos
= posn
% s
->blk_len
;
823 s
->wblk
= &s
->blk_ptrs
[ix
];
826 s
->wpos
= s
->blk_len
;
830 saa_wbytes(s
, data
, len
);
833 void saa_fpwrite(struct SAA
*s
, FILE * fp
)
839 while (len
= s
->datalen
, (data
= saa_rbytes(s
, &len
)) != NULL
)
840 fwrite(data
, 1, len
, fp
);
844 * Common list of prefix names
846 static const char *prefix_names
[] = {
847 "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
848 "repnz", "repz", "times"
851 const char *prefix_name(int token
)
853 unsigned int prefix
= token
-PREFIX_ENUM_START
;
854 if (prefix
> elements(prefix_names
))
857 return prefix_names
[prefix
];
863 int bsi(const char *string
, const char **array
, int size
)
865 int i
= -1, j
= size
; /* always, i < index < j */
868 int l
= strcmp(string
, array
[k
]);
869 if (l
< 0) /* it's in the first half */
871 else if (l
> 0) /* it's in the second half */
873 else /* we've got it :) */
876 return -1; /* we haven't got it :( */
879 int bsii(const char *string
, const char **array
, int size
)
881 int i
= -1, j
= size
; /* always, i < index < j */
884 int l
= nasm_stricmp(string
, array
[k
]);
885 if (l
< 0) /* it's in the first half */
887 else if (l
> 0) /* it's in the second half */
889 else /* we've got it :) */
892 return -1; /* we haven't got it :( */
895 static char *file_name
= NULL
;
896 static int32_t line_number
= 0;
898 char *src_set_fname(char *newname
)
900 char *oldname
= file_name
;
905 int32_t src_set_linnum(int32_t newline
)
907 int32_t oldline
= line_number
;
908 line_number
= newline
;
912 int32_t src_get_linnum(void)
917 int src_get(int32_t *xline
, char **xname
)
919 if (!file_name
|| !*xname
|| strcmp(*xname
, file_name
)) {
921 *xname
= file_name
? nasm_strdup(file_name
) : NULL
;
922 *xline
= line_number
;
925 if (*xline
!= line_number
) {
926 int32_t tmp
= line_number
- *xline
;
927 *xline
= line_number
;
933 void nasm_quote(char **str
)
935 int ln
= strlen(*str
);
938 if (ln
> 1 && (*str
)[ln
- 1] == q
&& (q
== '"' || q
== '\''))
943 p
= nasm_malloc(ln
+ 3);
946 p
[ln
+ 1] = p
[0] = q
;
951 char *nasm_strcat(char *one
, char *two
)
954 int l1
= strlen(one
);
955 rslt
= nasm_malloc(l1
+ strlen(two
) + 1);
957 strcpy(rslt
+ l1
, two
);
961 void null_debug_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
968 void null_debug_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
974 void null_debug_deflabel(char *name
, int32_t segment
, int64_t offset
,
975 int is_global
, char *special
)
983 void null_debug_routine(const char *directive
, const char *params
)
988 void null_debug_typevalue(int32_t type
)
992 void null_debug_output(int type
, void *param
)
997 void null_debug_cleanup(void)
1001 struct dfmt null_debug_form
= {
1002 "Null debug format",
1006 null_debug_deflabel
,
1008 null_debug_typevalue
,
1013 struct dfmt
*null_debug_arr
[2] = { &null_debug_form
, NULL
};