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.
19 int globalbits
= 0; /* defined in nasm.h, works better here for ASM+DISASM */
20 efunc nasm_malloc_error
; /* Exported for the benefit of vsnprintf.c */
26 void nasm_set_malloc_error(efunc error
)
28 nasm_malloc_error
= error
;
30 logfp
= fopen("malloc.log", "w");
31 setvbuf(logfp
, NULL
, _IOLBF
, BUFSIZ
);
32 fprintf(logfp
, "null pointer is %p\n", NULL
);
37 void *nasm_malloc_log(char *file
, int line
, size_t size
)
39 void *nasm_malloc(size_t size
)
42 void *p
= malloc(size
);
44 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
47 fprintf(logfp
, "%s %d malloc(%ld) returns %p\n",
48 file
, line
, (long)size
, p
);
54 void *nasm_zalloc_log(char *file
, int line
, size_t size
)
56 void *nasm_zalloc(size_t size
)
59 void *p
= calloc(size
, 1);
61 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
64 fprintf(logfp
, "%s %d calloc(%ld, 1) returns %p\n",
65 file
, line
, (long)size
, p
);
71 void *nasm_realloc_log(char *file
, int line
, void *q
, size_t size
)
73 void *nasm_realloc(void *q
, size_t size
)
76 void *p
= q
? realloc(q
, size
) : malloc(size
);
78 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
81 fprintf(logfp
, "%s %d realloc(%p,%ld) returns %p\n",
82 file
, line
, q
, (long)size
, p
);
84 fprintf(logfp
, "%s %d malloc(%ld) returns %p\n",
85 file
, line
, (long)size
, p
);
91 void nasm_free_log(char *file
, int line
, void *q
)
93 void nasm_free(void *q
)
99 fprintf(logfp
, "%s %d free(%p)\n", file
, line
, q
);
105 char *nasm_strdup_log(char *file
, int line
, const char *s
)
107 char *nasm_strdup(const char *s
)
111 int size
= strlen(s
) + 1;
115 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
118 fprintf(logfp
, "%s %d strdup(%ld) returns %p\n",
119 file
, line
, (long)size
, p
);
126 char *nasm_strndup_log(char *file
, int line
, char *s
, size_t len
)
128 char *nasm_strndup(char *s
, size_t len
)
136 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
139 fprintf(logfp
, "%s %d strndup(%ld) returns %p\n",
140 file
, line
, (long)size
, p
);
147 #if !defined(stricmp) && !defined(strcasecmp)
148 int nasm_stricmp(const char *s1
, const char *s2
)
150 while (*s1
&& tolower(*s1
) == tolower(*s2
))
154 else if (tolower(*s1
) < tolower(*s2
))
161 #if !defined(strnicmp) && !defined(strncasecmp)
162 int nasm_strnicmp(const char *s1
, const char *s2
, int n
)
164 while (n
> 0 && *s1
&& tolower(*s1
) == tolower(*s2
))
166 if ((!*s1
&& !*s2
) || n
== 0)
168 else if (tolower(*s1
) < tolower(*s2
))
176 char *nasm_strsep(char **stringp
, const char *delim
)
184 e
= strpbrk(s
, delim
);
194 #define lib_isnumchar(c) ( isalnum(c) || (c) == '$')
195 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
197 int64_t readnum(char *str
, int *error
)
201 uint64_t result
, checklimit
;
209 r
++; /* find start of number */
212 * If the number came from make_tok_num (as a result of an %assign), it
213 * might have a '-' built into it (rather than in a preceeding token).
222 while (lib_isnumchar(*q
))
223 q
++; /* find end of number */
226 * If it begins 0x, 0X or $, or ends in H, it's in hex. if it
227 * ends in Q, it's octal. if it ends in B, it's binary.
228 * Otherwise, it's ordinary decimal.
230 if (*r
== '0' && (r
[1] == 'x' || r
[1] == 'X'))
234 else if (q
[-1] == 'H' || q
[-1] == 'h')
236 else if (q
[-1] == 'Q' || q
[-1] == 'q' || q
[-1] == 'O' || q
[-1] == 'o')
238 else if (q
[-1] == 'B' || q
[-1] == 'b')
244 * If this number has been found for us by something other than
245 * the ordinary scanners, then it might be malformed by having
246 * nothing between the prefix and the suffix. Check this case
255 * `checklimit' must be 2**(32|64) / radix. We can't do that in
256 * 32/64-bit arithmetic, which we're (probably) using, so we
257 * cheat: since we know that all radices we use are even, we
258 * can divide 2**(31|63) by radix/2 instead.
260 if (globalbits
== 64)
261 checklimit
= 0x8000000000000000ULL
/ (radix
>> 1);
263 checklimit
= 0x80000000UL
/ (radix
>> 1);
266 * Calculate the highest allowable value for the last digit of a
267 * 32-bit constant... in radix 10, it is 6, otherwise it is 0
269 last
= (radix
== 10 ? 6 : 0);
272 while (*r
&& r
< q
) {
273 if (*r
< '0' || (*r
> '9' && *r
< 'A')
274 || (digit
= numvalue(*r
)) >= radix
) {
278 if (result
> checklimit
|| (result
== checklimit
&& digit
>= last
)) {
282 result
= radix
* result
+ digit
;
287 nasm_malloc_error(ERR_WARNING
| ERR_PASS1
| ERR_WARN_NOV
,
288 "numeric constant %s does not fit in 32 bits",
291 return result
* sign
;
294 int64_t readstrnum(char *str
, int length
, int *warn
)
296 int64_t charconst
= 0;
302 if (globalbits
== 64) {
303 for (i
= 0; i
< length
; i
++) {
304 if (charconst
& 0xFF00000000000000ULL
)
306 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
309 for (i
= 0; i
< length
; i
++) {
310 if (charconst
& 0xFF000000UL
)
312 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
318 static int32_t next_seg
;
325 int32_t seg_alloc(void)
327 return (next_seg
+= 2) - 2;
330 void fwriteint16_t(int data
, FILE * fp
)
332 fputc((int)(data
& 255), fp
);
333 fputc((int)((data
>> 8) & 255), fp
);
336 void fwriteint32_t(int32_t data
, FILE * fp
)
338 fputc((int)(data
& 255), fp
);
339 fputc((int)((data
>> 8) & 255), fp
);
340 fputc((int)((data
>> 16) & 255), fp
);
341 fputc((int)((data
>> 24) & 255), fp
);
344 void fwriteint64_t(int64_t data
, FILE * fp
)
346 fputc((int)(data
& 255), fp
);
347 fputc((int)((data
>> 8) & 255), fp
);
348 fputc((int)((data
>> 16) & 255), fp
);
349 fputc((int)((data
>> 24) & 255), fp
);
350 fputc((int)((data
>> 32) & 255), fp
);
351 fputc((int)((data
>> 40) & 255), fp
);
352 fputc((int)((data
>> 48) & 255), fp
);
353 fputc((int)((data
>> 56) & 255), fp
);
356 void standard_extension(char *inname
, char *outname
, char *extension
,
361 if (*outname
) /* file name already exists, */
362 return; /* so do nothing */
366 *p
++ = *q
++; /* copy, and find end of string */
367 *p
= '\0'; /* terminate it */
368 while (p
> outname
&& *--p
!= '.') ; /* find final period (or whatever) */
371 p
++; /* go back to end if none found */
372 if (!strcmp(p
, extension
)) { /* is the extension already there? */
374 error(ERR_WARNING
| ERR_NOFILE
,
375 "file name already ends in `%s': "
376 "output will be in `nasm.out'", extension
);
378 error(ERR_WARNING
| ERR_NOFILE
,
379 "file name already has no extension: "
380 "output will be in `nasm.out'");
381 strcpy(outname
, "nasm.out");
383 strcpy(p
, extension
);
386 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
387 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
389 #define LAYERSIZ(r) ( (r)->layers==0 ? RAA_BLKSIZE : RAA_LAYERSIZE )
391 static struct RAA
*real_raa_init(int layers
)
397 r
= nasm_zalloc(LEAFSIZ
);
400 r
= nasm_malloc(BRANCHSIZ
);
402 for (i
= 0; i
< RAA_LAYERSIZE
; i
++)
403 r
->u
.b
.data
[i
] = NULL
;
404 r
->stepsize
= RAA_BLKSIZE
;
406 r
->stepsize
*= RAA_LAYERSIZE
;
411 struct RAA
*raa_init(void)
413 return real_raa_init(0);
416 void raa_free(struct RAA
*r
)
422 for (p
= r
->u
.b
.data
; p
- r
->u
.b
.data
< RAA_LAYERSIZE
; p
++)
428 int32_t raa_read(struct RAA
*r
, int32_t posn
)
430 if (posn
>= r
->stepsize
* LAYERSIZ(r
))
431 return 0; /* Return 0 for undefined entries */
432 while (r
->layers
> 0) {
434 l
= ldiv(posn
, r
->stepsize
);
435 r
= r
->u
.b
.data
[l
.quot
];
438 return 0; /* Return 0 for undefined entries */
440 return r
->u
.l
.data
[posn
];
443 struct RAA
*raa_write(struct RAA
*r
, int32_t posn
, int32_t value
)
448 nasm_malloc_error(ERR_PANIC
, "negative position in raa_write");
450 while (r
->stepsize
* LAYERSIZ(r
) <= posn
) {
457 s
= nasm_malloc(BRANCHSIZ
);
458 for (i
= 0; i
< RAA_LAYERSIZE
; i
++)
459 s
->u
.b
.data
[i
] = NULL
;
460 s
->layers
= r
->layers
+ 1;
461 s
->stepsize
= LAYERSIZ(r
) * r
->stepsize
;
468 while (r
->layers
> 0) {
471 l
= ldiv(posn
, r
->stepsize
);
472 s
= &r
->u
.b
.data
[l
.quot
];
474 *s
= real_raa_init(r
->layers
- 1);
479 r
->u
.l
.data
[posn
] = value
;
484 #define SAA_MAXLEN 8192
486 struct SAA
*saa_init(int32_t elem_len
)
490 if (elem_len
> SAA_MAXLEN
)
491 nasm_malloc_error(ERR_PANIC
| ERR_NOFILE
,
492 "SAA with huge elements");
494 s
= nasm_malloc(sizeof(struct SAA
));
495 s
->posn
= s
->start
= 0L;
496 s
->elem_len
= elem_len
;
497 s
->length
= SAA_MAXLEN
- (SAA_MAXLEN
% elem_len
);
498 s
->data
= nasm_malloc(s
->length
);
505 void saa_free(struct SAA
*s
)
517 void *saa_wstruct(struct SAA
*s
)
521 if (s
->end
->length
- s
->end
->posn
< s
->elem_len
) {
522 s
->end
->next
= nasm_malloc(sizeof(struct SAA
));
523 s
->end
->next
->start
= s
->end
->start
+ s
->end
->posn
;
524 s
->end
= s
->end
->next
;
525 s
->end
->length
= s
->length
;
528 s
->end
->data
= nasm_malloc(s
->length
);
531 p
= s
->end
->data
+ s
->end
->posn
;
532 s
->end
->posn
+= s
->elem_len
;
536 void saa_wbytes(struct SAA
*s
, const void *data
, int32_t len
)
538 const char *d
= data
;
541 int32_t l
= s
->end
->length
- s
->end
->posn
;
546 memcpy(s
->end
->data
+ s
->end
->posn
, d
, l
);
549 memset(s
->end
->data
+ s
->end
->posn
, 0, l
);
554 s
->end
->next
= nasm_malloc(sizeof(struct SAA
));
555 s
->end
->next
->start
= s
->end
->start
+ s
->end
->posn
;
556 s
->end
= s
->end
->next
;
557 s
->end
->length
= s
->length
;
560 s
->end
->data
= nasm_malloc(s
->length
);
565 void saa_rewind(struct SAA
*s
)
571 void *saa_rstruct(struct SAA
*s
)
578 if (s
->rptr
->posn
- s
->rpos
< s
->elem_len
) {
579 s
->rptr
= s
->rptr
->next
;
581 return NULL
; /* end of array */
585 p
= s
->rptr
->data
+ s
->rpos
;
586 s
->rpos
+= s
->elem_len
;
590 void *saa_rbytes(struct SAA
*s
, int32_t *len
)
597 p
= s
->rptr
->data
+ s
->rpos
;
598 *len
= s
->rptr
->posn
- s
->rpos
;
599 s
->rptr
= s
->rptr
->next
;
604 void saa_rnbytes(struct SAA
*s
, void *data
, int32_t len
)
614 l
= s
->rptr
->posn
- s
->rpos
;
618 memcpy(d
, s
->rptr
->data
+ s
->rpos
, l
);
624 s
->rptr
= s
->rptr
->next
;
630 void saa_fread(struct SAA
*s
, int32_t posn
, void *data
, int32_t len
)
636 if (!s
->rptr
|| posn
< s
->rptr
->start
)
639 while (posn
>= p
->start
+ p
->posn
) {
642 return; /* what else can we do?! */
645 pos
= posn
- p
->start
;
647 int64_t l
= p
->posn
- pos
;
650 memcpy(cdata
, p
->data
+ pos
, l
);
661 void saa_fwrite(struct SAA
*s
, int32_t posn
, void *data
, int32_t len
)
667 if (!s
->rptr
|| posn
< s
->rptr
->start
)
670 while (posn
>= p
->start
+ p
->posn
) {
673 return; /* what else can we do?! */
676 pos
= posn
- p
->start
;
678 int64_t l
= p
->posn
- pos
;
681 memcpy(p
->data
+ pos
, cdata
, l
);
692 void saa_fpwrite(struct SAA
*s
, FILE * fp
)
698 // while ((data = saa_rbytes(s, &len)))
699 for (; (data
= saa_rbytes(s
, &len
));)
700 fwrite(data
, 1, len
, fp
);
704 * Common list of prefix names
706 static const char *prefix_names
[] = {
707 "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
708 "repnz", "repz", "times"
711 const char *prefix_name(int token
)
713 unsigned int prefix
= token
-PREFIX_ENUM_START
;
714 if (prefix
> sizeof prefix_names
/ sizeof(const char *))
717 return prefix_names
[prefix
];
723 int bsi(char *string
, const char **array
, int size
)
725 int i
= -1, j
= size
; /* always, i < index < j */
728 int l
= strcmp(string
, array
[k
]);
729 if (l
< 0) /* it's in the first half */
731 else if (l
> 0) /* it's in the second half */
733 else /* we've got it :) */
736 return -1; /* we haven't got it :( */
739 int bsii(char *string
, const char **array
, int size
)
741 int i
= -1, j
= size
; /* always, i < index < j */
744 int l
= nasm_stricmp(string
, array
[k
]);
745 if (l
< 0) /* it's in the first half */
747 else if (l
> 0) /* it's in the second half */
749 else /* we've got it :) */
752 return -1; /* we haven't got it :( */
755 static char *file_name
= NULL
;
756 static int32_t line_number
= 0;
758 char *src_set_fname(char *newname
)
760 char *oldname
= file_name
;
765 int32_t src_set_linnum(int32_t newline
)
767 int32_t oldline
= line_number
;
768 line_number
= newline
;
772 int32_t src_get_linnum(void)
777 int src_get(int32_t *xline
, char **xname
)
779 if (!file_name
|| !*xname
|| strcmp(*xname
, file_name
)) {
781 *xname
= file_name
? nasm_strdup(file_name
) : NULL
;
782 *xline
= line_number
;
785 if (*xline
!= line_number
) {
786 int32_t tmp
= line_number
- *xline
;
787 *xline
= line_number
;
793 void nasm_quote(char **str
)
795 int ln
= strlen(*str
);
798 if (ln
> 1 && (*str
)[ln
- 1] == q
&& (q
== '"' || q
== '\''))
803 p
= nasm_malloc(ln
+ 3);
806 p
[ln
+ 1] = p
[0] = q
;
811 char *nasm_strcat(char *one
, char *two
)
814 int l1
= strlen(one
);
815 rslt
= nasm_malloc(l1
+ strlen(two
) + 1);
817 strcpy(rslt
+ l1
, two
);
821 void null_debug_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
828 void null_debug_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
834 void null_debug_deflabel(char *name
, int32_t segment
, int32_t offset
,
835 int is_global
, char *special
)
843 void null_debug_routine(const char *directive
, const char *params
)
848 void null_debug_typevalue(int32_t type
)
852 void null_debug_output(int type
, void *param
)
857 void null_debug_cleanup(void)
861 struct dfmt null_debug_form
= {
868 null_debug_typevalue
,
873 struct dfmt
*null_debug_arr
[2] = { &null_debug_form
, NULL
};