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 unsigned char c1
, c2
;
169 #ifndef nasm_strnicmp
170 int nasm_strnicmp(const char *s1
, const char *s2
, size_t n
)
172 unsigned char c1
, c2
;
189 int nasm_memicmp(const char *s1
, const char *s2
, size_t n
)
191 unsigned char c1
, c2
;
205 char *nasm_strsep(char **stringp
, const char *delim
)
213 e
= strpbrk(s
, delim
);
223 #define lib_isnumchar(c) (isalnum(c) || (c) == '$' || (c) == '_')
224 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
226 static int radix_letter(char c
)
231 return 2; /* Binary */
234 return 8; /* Octal */
237 return 16; /* Hexadecimal */
240 return 10; /* Decimal */
242 return 0; /* Not a known radix letter */
246 int64_t readnum(char *str
, bool *error
)
249 int32_t pradix
, sradix
, radix
;
251 uint64_t result
, checklimit
;
259 r
++; /* find start of number */
262 * If the number came from make_tok_num (as a result of an %assign), it
263 * might have a '-' built into it (rather than in a preceeding token).
272 while (lib_isnumchar(*q
))
273 q
++; /* find end of number */
283 * Handle radix formats:
285 * 0<radix-letter><string>
286 * $<string> (hexadecimal)
287 * <string><radix-letter>
292 if (len
> 2 && *r
== '0' && (pradix
= radix_letter(r
[1])) != 0)
294 else if (len
> 1 && *r
== '$')
295 pradix
= 16, plen
= 1;
297 if (len
> 1 && (sradix
= radix_letter(q
[-1])) != 0)
300 if (pradix
> sradix
) {
303 } else if (sradix
> pradix
) {
307 /* Either decimal, or invalid -- if invalid, we'll trip up
313 * `checklimit' must be 2**64 / radix. We can't do that in
314 * 64-bit arithmetic, which we're (probably) using, so we
315 * cheat: since we know that all radices we use are even, we
316 * can divide 2**63 by radix/2 instead.
318 checklimit
= 0x8000000000000000ULL
/ (radix
>> 1);
321 * Calculate the highest allowable value for the last digit of a
322 * 64-bit constant... in radix 10, it is 6, otherwise it is 0
324 last
= (radix
== 10 ? 6 : 0);
327 while (*r
&& r
< q
) {
329 if (*r
< '0' || (*r
> '9' && *r
< 'A')
330 || (digit
= numvalue(*r
)) >= radix
) {
334 if (result
> checklimit
||
335 (result
== checklimit
&& digit
>= last
)) {
339 result
= radix
* result
+ digit
;
345 nasm_malloc_error(ERR_WARNING
| ERR_PASS1
| ERR_WARN_NOV
,
346 "numeric constant %s does not fit in 64 bits",
349 return result
* sign
;
352 int64_t readstrnum(char *str
, int length
, bool *warn
)
354 int64_t charconst
= 0;
360 if (globalbits
== 64) {
361 for (i
= 0; i
< length
; i
++) {
362 if (charconst
& 0xFF00000000000000ULL
)
364 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
367 for (i
= 0; i
< length
; i
++) {
368 if (charconst
& 0xFF000000UL
)
370 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
376 static int32_t next_seg
;
383 int32_t seg_alloc(void)
385 return (next_seg
+= 2) - 2;
390 void fwriteint16_t(uint16_t data
, FILE * fp
)
392 fwrite(&data
, 1, 2, fp
);
395 void fwriteint32_t(uint32_t data
, FILE * fp
)
397 fwrite(&data
, 1, 4, fp
);
400 void fwriteint64_t(uint64_t data
, FILE * fp
)
402 fwrite(&data
, 1, 8, fp
);
405 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
407 fwrite(&data
, 1, size
, fp
);
410 #else /* !X86_MEMORY */
412 void fwriteint16_t(uint16_t data
, FILE * fp
)
414 char buffer
[2], *p
= buffer
;
416 fwrite(buffer
, 1, 2, fp
);
419 void fwriteint32_t(uint32_t data
, FILE * fp
)
421 char buffer
[4], *p
= buffer
;
423 fwrite(buffer
, 1, 4, fp
);
426 void fwriteint64_t(uint64_t data
, FILE * fp
)
428 char buffer
[8], *p
= buffer
;
430 fwrite(buffer
, 1, 8, fp
);
433 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
435 char buffer
[8], *p
= buffer
;
436 WRITEADDR(p
, data
, size
);
437 fwrite(buffer
, 1, size
, fp
);
442 void standard_extension(char *inname
, char *outname
, char *extension
,
447 if (*outname
) /* file name already exists, */
448 return; /* so do nothing */
452 *p
++ = *q
++; /* copy, and find end of string */
453 *p
= '\0'; /* terminate it */
454 while (p
> outname
&& *--p
!= '.') ; /* find final period (or whatever) */
457 p
++; /* go back to end if none found */
458 if (!strcmp(p
, extension
)) { /* is the extension already there? */
460 error(ERR_WARNING
| ERR_NOFILE
,
461 "file name already ends in `%s': "
462 "output will be in `nasm.out'", extension
);
464 error(ERR_WARNING
| ERR_NOFILE
,
465 "file name already has no extension: "
466 "output will be in `nasm.out'");
467 strcpy(outname
, "nasm.out");
469 strcpy(p
, extension
);
472 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
473 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
475 #define LAYERSHIFT(r) ( (r)->layers==0 ? RAA_BLKSHIFT : RAA_LAYERSHIFT )
477 static struct RAA
*real_raa_init(int layers
)
483 r
= nasm_zalloc(LEAFSIZ
);
486 r
= nasm_malloc(BRANCHSIZ
);
488 for (i
= 0; i
< RAA_LAYERSIZE
; i
++)
489 r
->u
.b
.data
[i
] = NULL
;
490 r
->shift
= (RAA_BLKSHIFT
-RAA_LAYERSHIFT
) + layers
*RAA_LAYERSHIFT
;
495 struct RAA
*raa_init(void)
497 return real_raa_init(0);
500 void raa_free(struct RAA
*r
)
504 for (p
= r
->u
.b
.data
; p
- r
->u
.b
.data
< RAA_LAYERSIZE
; p
++)
511 int64_t raa_read(struct RAA
*r
, int32_t posn
)
513 if ((uint32_t)posn
>= (UINT32_C(1) << (r
->shift
+ LAYERSHIFT(r
))))
514 return 0; /* Return 0 for undefined entries */
515 while (r
->layers
> 0) {
516 int32_t l
= posn
>> r
->shift
;
517 posn
&= (UINT32_C(1) << r
->shift
)-1;
520 return 0; /* Return 0 for undefined entries */
522 return r
->u
.l
.data
[posn
];
525 struct RAA
*raa_write(struct RAA
*r
, int32_t posn
, int64_t value
)
530 nasm_malloc_error(ERR_PANIC
, "negative position in raa_write");
532 while ((UINT32_C(1) << (r
->shift
+LAYERSHIFT(r
))) <= (uint32_t)posn
) {
539 s
= nasm_malloc(BRANCHSIZ
);
540 for (i
= 0; i
< RAA_LAYERSIZE
; i
++)
541 s
->u
.b
.data
[i
] = NULL
;
542 s
->layers
= r
->layers
+ 1;
543 s
->shift
= LAYERSHIFT(r
) + r
->shift
;
550 while (r
->layers
> 0) {
552 int32_t l
= posn
>> r
->shift
;
553 posn
&= (UINT32_C(1) << r
->shift
)-1;
556 *s
= real_raa_init(r
->layers
- 1);
560 r
->u
.l
.data
[posn
] = value
;
565 /* Aggregate SAA components smaller than this */
566 #define SAA_BLKLEN 65536
568 struct SAA
*saa_init(size_t elem_len
)
573 s
= nasm_zalloc(sizeof(struct SAA
));
575 if (elem_len
>= SAA_BLKLEN
)
576 s
->blk_len
= elem_len
;
578 s
->blk_len
= SAA_BLKLEN
- (SAA_BLKLEN
% elem_len
);
580 s
->elem_len
= elem_len
;
581 s
->length
= s
->blk_len
;
582 data
= nasm_malloc(s
->blk_len
);
583 s
->nblkptrs
= s
->nblks
= 1;
584 s
->blk_ptrs
= nasm_malloc(sizeof(char *));
585 s
->blk_ptrs
[0] = data
;
586 s
->wblk
= s
->rblk
= &s
->blk_ptrs
[0];
591 void saa_free(struct SAA
*s
)
596 for (p
= s
->blk_ptrs
, n
= s
->nblks
; n
; p
++, n
--)
599 nasm_free(s
->blk_ptrs
);
603 /* Add one allocation block to an SAA */
604 static void saa_extend(struct SAA
*s
)
606 size_t blkn
= s
->nblks
++;
608 if (blkn
>= s
->nblkptrs
) {
609 size_t rindex
= s
->rblk
- s
->blk_ptrs
;
610 size_t windex
= s
->wblk
- s
->blk_ptrs
;
613 s
->blk_ptrs
= nasm_realloc(s
->blk_ptrs
, s
->nblkptrs
*sizeof(char *));
615 s
->rblk
= s
->blk_ptrs
+ rindex
;
616 s
->wblk
= s
->blk_ptrs
+ windex
;
619 s
->blk_ptrs
[blkn
] = nasm_malloc(s
->blk_len
);
620 s
->length
+= s
->blk_len
;
623 void *saa_wstruct(struct SAA
*s
)
627 if (s
->wpos
% s
->elem_len
)
628 nasm_malloc_error(ERR_PANIC
|ERR_NOFILE
,
629 "misaligned wpos in saa_wstruct");
631 if (s
->wpos
+ s
->elem_len
> s
->blk_len
) {
632 if (s
->wpos
!= s
->blk_len
)
633 nasm_malloc_error(ERR_PANIC
|ERR_NOFILE
,
634 "unfilled block in saa_wstruct");
636 if (s
->wptr
+ s
->elem_len
> s
->length
)
642 p
= *s
->wblk
+ s
->wpos
;
643 s
->wpos
+= s
->elem_len
;
644 s
->wptr
+= s
->elem_len
;
646 if (s
->wptr
> s
->datalen
)
647 s
->datalen
= s
->wptr
;
652 void saa_wbytes(struct SAA
*s
, const void *data
, size_t len
)
654 const char *d
= data
;
657 size_t l
= s
->blk_len
- s
->wpos
;
662 memcpy(*s
->wblk
+ s
->wpos
, d
, l
);
665 memset(*s
->wblk
+ s
->wpos
, 0, l
);
670 if (s
->datalen
< s
->wptr
)
671 s
->datalen
= s
->wptr
;
674 if (s
->wptr
>= s
->length
)
682 /* write unsigned LEB128 value to SAA */
683 void saa_wleb128u(struct SAA
*psaa
, int value
)
685 char temp
[64], *ptemp
;
695 if (value
!= 0) /* more bytes to come */
700 } while (value
!= 0);
701 saa_wbytes(psaa
, temp
, len
);
704 /* write signed LEB128 value to SAA */
705 void saa_wleb128s(struct SAA
*psaa
, int value
)
707 char temp
[64], *ptemp
;
714 negative
= (value
< 0);
715 size
= sizeof(int) * 8;
723 value
|= - (1 <<(size
- 7));
724 /* sign bit of byte is second high order bit (0x40) */
725 if ((value
== 0 && ! (byte
& 0x40)) ||
726 ((value
== -1) && (byte
& 0x40)))
734 saa_wbytes(psaa
, temp
, len
);
737 void saa_rewind(struct SAA
*s
)
739 s
->rblk
= s
->blk_ptrs
;
740 s
->rpos
= s
->rptr
= 0;
743 void *saa_rstruct(struct SAA
*s
)
747 if (s
->rptr
+ s
->elem_len
> s
->datalen
)
750 if (s
->rpos
% s
->elem_len
)
751 nasm_malloc_error(ERR_PANIC
|ERR_NOFILE
,
752 "misaligned rpos in saa_rstruct");
754 if (s
->rpos
+ s
->elem_len
> s
->blk_len
) {
759 p
= *s
->rblk
+ s
->rpos
;
760 s
->rpos
+= s
->elem_len
;
761 s
->rptr
+= s
->elem_len
;
766 const void *saa_rbytes(struct SAA
*s
, size_t *lenp
)
771 if (s
->rptr
>= s
->datalen
) {
776 if (s
->rpos
>= s
->blk_len
) {
782 if (len
> s
->datalen
- s
->rptr
)
783 len
= s
->datalen
- s
->rptr
;
784 if (len
> s
->blk_len
- s
->rpos
)
785 len
= s
->blk_len
- s
->rpos
;
788 p
= *s
->rblk
+ s
->rpos
;
796 void saa_rnbytes(struct SAA
*s
, void *data
, size_t len
)
800 if (s
->rptr
+ len
> s
->datalen
) {
801 nasm_malloc_error(ERR_PANIC
|ERR_NOFILE
, "overrun in saa_rnbytes");
810 p
= saa_rbytes(s
, &l
);
818 /* Same as saa_rnbytes, except position the counter first */
819 void saa_fread(struct SAA
*s
, size_t posn
, void *data
, size_t len
)
823 if (posn
+len
> s
->datalen
) {
824 nasm_malloc_error(ERR_PANIC
|ERR_NOFILE
, "overrun in saa_fread");
828 ix
= posn
/ s
->blk_len
;
830 s
->rpos
= posn
% s
->blk_len
;
831 s
->rblk
= &s
->blk_ptrs
[ix
];
833 saa_rnbytes(s
, data
, len
);
836 /* Same as saa_wbytes, except position the counter first */
837 void saa_fwrite(struct SAA
*s
, size_t posn
, const void *data
, size_t len
)
841 if (posn
> s
->datalen
) {
842 /* Seek beyond the end of the existing array not supported */
843 nasm_malloc_error(ERR_PANIC
|ERR_NOFILE
, "overrun in saa_fwrite");
847 ix
= posn
/ s
->blk_len
;
849 s
->wpos
= posn
% s
->blk_len
;
850 s
->wblk
= &s
->blk_ptrs
[ix
];
853 s
->wpos
= s
->blk_len
;
857 saa_wbytes(s
, data
, len
);
860 void saa_fpwrite(struct SAA
*s
, FILE * fp
)
866 while (len
= s
->datalen
, (data
= saa_rbytes(s
, &len
)) != NULL
)
867 fwrite(data
, 1, len
, fp
);
871 * Common list of prefix names
873 static const char *prefix_names
[] = {
874 "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
875 "repnz", "repz", "times"
878 const char *prefix_name(int token
)
880 unsigned int prefix
= token
-PREFIX_ENUM_START
;
881 if (prefix
> elements(prefix_names
))
884 return prefix_names
[prefix
];
890 int bsi(const char *string
, const char **array
, int size
)
892 int i
= -1, j
= size
; /* always, i < index < j */
895 int l
= strcmp(string
, array
[k
]);
896 if (l
< 0) /* it's in the first half */
898 else if (l
> 0) /* it's in the second half */
900 else /* we've got it :) */
903 return -1; /* we haven't got it :( */
906 int bsii(const char *string
, const char **array
, int size
)
908 int i
= -1, j
= size
; /* always, i < index < j */
911 int l
= nasm_stricmp(string
, array
[k
]);
912 if (l
< 0) /* it's in the first half */
914 else if (l
> 0) /* it's in the second half */
916 else /* we've got it :) */
919 return -1; /* we haven't got it :( */
922 static char *file_name
= NULL
;
923 static int32_t line_number
= 0;
925 char *src_set_fname(char *newname
)
927 char *oldname
= file_name
;
932 int32_t src_set_linnum(int32_t newline
)
934 int32_t oldline
= line_number
;
935 line_number
= newline
;
939 int32_t src_get_linnum(void)
944 int src_get(int32_t *xline
, char **xname
)
946 if (!file_name
|| !*xname
|| strcmp(*xname
, file_name
)) {
948 *xname
= file_name
? nasm_strdup(file_name
) : NULL
;
949 *xline
= line_number
;
952 if (*xline
!= line_number
) {
953 int32_t tmp
= line_number
- *xline
;
954 *xline
= line_number
;
960 char *nasm_strcat(char *one
, char *two
)
963 int l1
= strlen(one
);
964 rslt
= nasm_malloc(l1
+ strlen(two
) + 1);
966 strcpy(rslt
+ l1
, two
);
970 void null_debug_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
977 void null_debug_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
983 void null_debug_deflabel(char *name
, int32_t segment
, int64_t offset
,
984 int is_global
, char *special
)
992 void null_debug_routine(const char *directive
, const char *params
)
997 void null_debug_typevalue(int32_t type
)
1001 void null_debug_output(int type
, void *param
)
1006 void null_debug_cleanup(void)
1010 struct dfmt null_debug_form
= {
1011 "Null debug format",
1015 null_debug_deflabel
,
1017 null_debug_typevalue
,
1022 struct dfmt
*null_debug_arr
[2] = { &null_debug_form
, NULL
};