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 licence given in the file "Licence"
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
)
101 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**(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.
291 if (globalbits
== 64)
292 checklimit
= 0x8000000000000000ULL
/ (radix
>> 1);
294 checklimit
= 0x80000000UL
/ (radix
>> 1);
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
300 last
= (radix
== 10 ? 6 : 0);
303 while (*r
&& r
< q
) {
305 if (*r
< '0' || (*r
> '9' && *r
< 'A')
306 || (digit
= numvalue(*r
)) >= radix
) {
310 if (result
> checklimit
||
311 (result
== checklimit
&& digit
>= last
)) {
315 result
= radix
* result
+ digit
;
321 nasm_malloc_error(ERR_WARNING
| ERR_PASS1
| ERR_WARN_NOV
,
322 "numeric constant %s does not fit in 32 bits",
325 return result
* sign
;
328 int64_t readstrnum(char *str
, int length
, bool *warn
)
330 int64_t charconst
= 0;
336 if (globalbits
== 64) {
337 for (i
= 0; i
< length
; i
++) {
338 if (charconst
& 0xFF00000000000000ULL
)
340 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
343 for (i
= 0; i
< length
; i
++) {
344 if (charconst
& 0xFF000000UL
)
346 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
352 static int32_t next_seg
;
359 int32_t seg_alloc(void)
361 return (next_seg
+= 2) - 2;
366 void fwriteint16_t(uint16_t data
, FILE * fp
)
368 fwrite(&data
, 1, 2, fp
);
371 void fwriteint32_t(uint32_t data
, FILE * fp
)
373 fwrite(&data
, 1, 4, fp
);
376 void fwriteint64_t(uint64_t data
, FILE * fp
)
378 fwrite(&data
, 1, 8, fp
);
381 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
383 fwrite(&data
, 1, size
, fp
);
386 #else /* !X86_MEMORY */
388 void fwriteint16_t(uint16_t data
, FILE * fp
)
390 char buffer
[2], *p
= buffer
;
392 fwrite(buffer
, 1, 2, fp
);
395 void fwriteint32_t(uint32_t data
, FILE * fp
)
397 char buffer
[4], *p
= buffer
;
399 fwrite(buffer
, 1, 4, fp
);
402 void fwriteint64_t(uint64_t data
, FILE * fp
)
404 char buffer
[8], *p
= buffer
;
406 fwrite(buffer
, 1, 8, fp
);
409 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
411 char buffer
[8], *p
= buffer
;
412 WRITEADDR(p
, data
, size
);
413 fwrite(buffer
, 1, size
, fp
);
418 void standard_extension(char *inname
, char *outname
, char *extension
,
423 if (*outname
) /* file name already exists, */
424 return; /* so do nothing */
428 *p
++ = *q
++; /* copy, and find end of string */
429 *p
= '\0'; /* terminate it */
430 while (p
> outname
&& *--p
!= '.') ; /* find final period (or whatever) */
433 p
++; /* go back to end if none found */
434 if (!strcmp(p
, extension
)) { /* is the extension already there? */
436 error(ERR_WARNING
| ERR_NOFILE
,
437 "file name already ends in `%s': "
438 "output will be in `nasm.out'", extension
);
440 error(ERR_WARNING
| ERR_NOFILE
,
441 "file name already has no extension: "
442 "output will be in `nasm.out'");
443 strcpy(outname
, "nasm.out");
445 strcpy(p
, extension
);
448 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
449 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
451 #define LAYERSIZ(r) ( (r)->layers==0 ? RAA_BLKSIZE : RAA_LAYERSIZE )
453 static struct RAA
*real_raa_init(int layers
)
459 r
= nasm_zalloc(LEAFSIZ
);
462 r
= nasm_malloc(BRANCHSIZ
);
464 for (i
= 0; i
< RAA_LAYERSIZE
; i
++)
465 r
->u
.b
.data
[i
] = NULL
;
466 r
->stepsize
= RAA_BLKSIZE
;
468 r
->stepsize
*= RAA_LAYERSIZE
;
473 struct RAA
*raa_init(void)
475 return real_raa_init(0);
478 void raa_free(struct RAA
*r
)
484 for (p
= r
->u
.b
.data
; p
- r
->u
.b
.data
< RAA_LAYERSIZE
; p
++)
490 int64_t raa_read(struct RAA
*r
, int32_t posn
)
492 if (posn
>= r
->stepsize
* LAYERSIZ(r
))
493 return 0; /* Return 0 for undefined entries */
494 while (r
->layers
> 0) {
496 l
= ldiv(posn
, r
->stepsize
);
497 r
= r
->u
.b
.data
[l
.quot
];
500 return 0; /* Return 0 for undefined entries */
502 return r
->u
.l
.data
[posn
];
505 struct RAA
*raa_write(struct RAA
*r
, int32_t posn
, int64_t value
)
510 nasm_malloc_error(ERR_PANIC
, "negative position in raa_write");
512 while (r
->stepsize
* LAYERSIZ(r
) <= posn
) {
519 s
= nasm_malloc(BRANCHSIZ
);
520 for (i
= 0; i
< RAA_LAYERSIZE
; i
++)
521 s
->u
.b
.data
[i
] = NULL
;
522 s
->layers
= r
->layers
+ 1;
523 s
->stepsize
= LAYERSIZ(r
) * r
->stepsize
;
530 while (r
->layers
> 0) {
533 l
= ldiv(posn
, r
->stepsize
);
534 s
= &r
->u
.b
.data
[l
.quot
];
536 *s
= real_raa_init(r
->layers
- 1);
541 r
->u
.l
.data
[posn
] = value
;
546 /* Aggregate SAA components smaller than this */
547 #define SAA_BLKLEN 65536
549 struct SAA
*saa_init(size_t elem_len
)
554 s
= nasm_zalloc(sizeof(struct SAA
));
556 if (elem_len
>= SAA_BLKLEN
)
557 s
->blk_len
= elem_len
;
559 s
->blk_len
= SAA_BLKLEN
- (SAA_BLKLEN
% elem_len
);
561 s
->elem_len
= elem_len
;
562 s
->length
= s
->blk_len
;
563 data
= nasm_malloc(s
->blk_len
);
564 s
->nblkptrs
= s
->nblks
= 1;
565 s
->blk_ptrs
= nasm_malloc(sizeof(char *));
566 s
->blk_ptrs
[0] = data
;
567 s
->wblk
= s
->rblk
= &s
->blk_ptrs
[0];
572 void saa_free(struct SAA
*s
)
577 for (p
= s
->blk_ptrs
, n
= s
->nblks
; n
; p
++, n
--)
580 nasm_free(s
->blk_ptrs
);
584 /* Add one allocation block to an SAA */
585 static void saa_extend(struct SAA
*s
)
587 size_t blkn
= s
->nblks
++;
589 if (blkn
>= s
->nblkptrs
) {
590 size_t rindex
= s
->rblk
- s
->blk_ptrs
;
591 size_t windex
= s
->wblk
- s
->blk_ptrs
;
594 s
->blk_ptrs
= nasm_realloc(s
->blk_ptrs
, s
->nblkptrs
*sizeof(char *));
596 s
->rblk
= s
->blk_ptrs
+ rindex
;
597 s
->wblk
= s
->blk_ptrs
+ windex
;
600 s
->blk_ptrs
[blkn
] = nasm_malloc(s
->blk_len
);
601 s
->length
+= s
->blk_len
;
604 void *saa_wstruct(struct SAA
*s
)
608 if (s
->wpos
% s
->elem_len
)
609 nasm_malloc_error(ERR_PANIC
|ERR_NOFILE
,
610 "misaligned wpos in saa_wstruct");
612 if (s
->wpos
+ s
->elem_len
> s
->blk_len
) {
613 if (s
->wpos
!= s
->blk_len
)
614 nasm_malloc_error(ERR_PANIC
|ERR_NOFILE
,
615 "unfilled block in saa_wstruct");
617 if (s
->wptr
+ s
->elem_len
> s
->length
)
623 p
= *s
->wblk
+ s
->wpos
;
624 s
->wpos
+= s
->elem_len
;
625 s
->wptr
+= s
->elem_len
;
627 if (s
->wptr
> s
->datalen
)
628 s
->datalen
= s
->wptr
;
633 void saa_wbytes(struct SAA
*s
, const void *data
, size_t len
)
635 const char *d
= data
;
638 size_t l
= s
->blk_len
- s
->wpos
;
643 memcpy(*s
->wblk
+ s
->wpos
, d
, l
);
646 memset(*s
->wblk
+ s
->wpos
, 0, l
);
651 if (s
->datalen
< s
->wptr
)
652 s
->datalen
= s
->wptr
;
655 if (s
->wptr
>= s
->length
)
663 void saa_rewind(struct SAA
*s
)
665 s
->rblk
= s
->blk_ptrs
;
666 s
->rpos
= s
->rptr
= 0;
669 void *saa_rstruct(struct SAA
*s
)
673 if (s
->rptr
+ s
->elem_len
> s
->datalen
)
676 if (s
->rpos
% s
->elem_len
)
677 nasm_malloc_error(ERR_PANIC
|ERR_NOFILE
,
678 "misaligned rpos in saa_rstruct");
680 if (s
->rpos
+ s
->elem_len
> s
->blk_len
) {
685 p
= *s
->rblk
+ s
->rpos
;
686 s
->rpos
+= s
->elem_len
;
687 s
->rptr
+= s
->elem_len
;
692 const void *saa_rbytes(struct SAA
*s
, size_t *lenp
)
697 if (s
->rptr
>= s
->datalen
) {
702 if (s
->rpos
>= s
->blk_len
) {
708 if (len
> s
->datalen
- s
->rptr
)
709 len
= s
->datalen
- s
->rptr
;
710 if (len
> s
->blk_len
- s
->rpos
)
711 len
= s
->blk_len
- s
->rpos
;
714 p
= *s
->rblk
+ s
->rpos
;
722 void saa_rnbytes(struct SAA
*s
, void *data
, size_t len
)
726 if (s
->rptr
+ len
> s
->datalen
) {
727 nasm_malloc_error(ERR_PANIC
|ERR_NOFILE
, "overrun in saa_rnbytes");
736 p
= saa_rbytes(s
, &l
);
744 /* Same as saa_rnbytes, except position the counter first */
745 void saa_fread(struct SAA
*s
, size_t posn
, void *data
, size_t len
)
749 if (posn
+len
> s
->datalen
) {
750 nasm_malloc_error(ERR_PANIC
|ERR_NOFILE
, "overrun in saa_fread");
754 ix
= posn
/ s
->blk_len
;
756 s
->rpos
= posn
% s
->blk_len
;
757 s
->rblk
= &s
->blk_ptrs
[ix
];
759 saa_rnbytes(s
, data
, len
);
762 /* Same as saa_wbytes, except position the counter first */
763 void saa_fwrite(struct SAA
*s
, size_t posn
, const void *data
, size_t len
)
767 if (posn
> s
->datalen
) {
768 /* Seek beyond the end of the existing array not supported */
769 nasm_malloc_error(ERR_PANIC
|ERR_NOFILE
, "overrun in saa_fwrite");
773 ix
= posn
/ s
->blk_len
;
775 s
->wpos
= posn
% s
->blk_len
;
776 s
->wblk
= &s
->blk_ptrs
[ix
];
779 s
->wpos
= s
->blk_len
;
783 saa_wbytes(s
, data
, len
);
786 void saa_fpwrite(struct SAA
*s
, FILE * fp
)
792 while (len
= s
->datalen
, (data
= saa_rbytes(s
, &len
)) != NULL
)
793 fwrite(data
, 1, len
, fp
);
797 * Common list of prefix names
799 static const char *prefix_names
[] = {
800 "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
801 "repnz", "repz", "times"
804 const char *prefix_name(int token
)
806 unsigned int prefix
= token
-PREFIX_ENUM_START
;
807 if (prefix
> elements(prefix_names
))
810 return prefix_names
[prefix
];
816 int bsi(const char *string
, const char **array
, int size
)
818 int i
= -1, j
= size
; /* always, i < index < j */
821 int l
= strcmp(string
, array
[k
]);
822 if (l
< 0) /* it's in the first half */
824 else if (l
> 0) /* it's in the second half */
826 else /* we've got it :) */
829 return -1; /* we haven't got it :( */
832 int bsii(const char *string
, const char **array
, int size
)
834 int i
= -1, j
= size
; /* always, i < index < j */
837 int l
= nasm_stricmp(string
, array
[k
]);
838 if (l
< 0) /* it's in the first half */
840 else if (l
> 0) /* it's in the second half */
842 else /* we've got it :) */
845 return -1; /* we haven't got it :( */
848 static char *file_name
= NULL
;
849 static int32_t line_number
= 0;
851 char *src_set_fname(char *newname
)
853 char *oldname
= file_name
;
858 int32_t src_set_linnum(int32_t newline
)
860 int32_t oldline
= line_number
;
861 line_number
= newline
;
865 int32_t src_get_linnum(void)
870 int src_get(int32_t *xline
, char **xname
)
872 if (!file_name
|| !*xname
|| strcmp(*xname
, file_name
)) {
874 *xname
= file_name
? nasm_strdup(file_name
) : NULL
;
875 *xline
= line_number
;
878 if (*xline
!= line_number
) {
879 int32_t tmp
= line_number
- *xline
;
880 *xline
= line_number
;
886 void nasm_quote(char **str
)
888 int ln
= strlen(*str
);
891 if (ln
> 1 && (*str
)[ln
- 1] == q
&& (q
== '"' || q
== '\''))
896 p
= nasm_malloc(ln
+ 3);
899 p
[ln
+ 1] = p
[0] = q
;
904 char *nasm_strcat(char *one
, char *two
)
907 int l1
= strlen(one
);
908 rslt
= nasm_malloc(l1
+ strlen(two
) + 1);
910 strcpy(rslt
+ l1
, two
);
914 void null_debug_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
921 void null_debug_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
927 void null_debug_deflabel(char *name
, int32_t segment
, int64_t offset
,
928 int is_global
, char *special
)
936 void null_debug_routine(const char *directive
, const char *params
)
941 void null_debug_typevalue(int32_t type
)
945 void null_debug_output(int type
, void *param
)
950 void null_debug_cleanup(void)
954 struct dfmt null_debug_form
= {
961 null_debug_typevalue
,
966 struct dfmt
*null_debug_arr
[2] = { &null_debug_form
, NULL
};