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 static efunc nasm_malloc_error
;
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
, (int32_t)size
, p
);
54 void *nasm_realloc_log(char *file
, int line
, void *q
, size_t size
)
56 void *nasm_realloc(void *q
, size_t size
)
59 void *p
= q
? realloc(q
, size
) : malloc(size
);
61 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
64 fprintf(logfp
, "%s %d realloc(%p,%ld) returns %p\n",
65 file
, line
, q
, (int32_t)size
, p
);
67 fprintf(logfp
, "%s %d malloc(%ld) returns %p\n",
68 file
, line
, (int32_t)size
, p
);
74 void nasm_free_log(char *file
, int line
, void *q
)
76 void nasm_free(void *q
)
82 fprintf(logfp
, "%s %d free(%p)\n", file
, line
, q
);
88 char *nasm_strdup_log(char *file
, int line
, const char *s
)
90 char *nasm_strdup(const char *s
)
94 int size
= strlen(s
) + 1;
98 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
101 fprintf(logfp
, "%s %d strdup(%ld) returns %p\n",
102 file
, line
, (int32_t)size
, p
);
109 char *nasm_strndup_log(char *file
, int line
, char *s
, size_t len
)
111 char *nasm_strndup(char *s
, size_t len
)
119 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
122 fprintf(logfp
, "%s %d strndup(%ld) returns %p\n",
123 file
, line
, (int32_t)size
, p
);
130 #if !defined(stricmp) && !defined(strcasecmp)
131 int nasm_stricmp(const char *s1
, const char *s2
)
133 while (*s1
&& tolower(*s1
) == tolower(*s2
))
137 else if (tolower(*s1
) < tolower(*s2
))
144 #if !defined(strnicmp) && !defined(strncasecmp)
145 int nasm_strnicmp(const char *s1
, const char *s2
, int n
)
147 while (n
> 0 && *s1
&& tolower(*s1
) == tolower(*s2
))
149 if ((!*s1
&& !*s2
) || n
== 0)
151 else if (tolower(*s1
) < tolower(*s2
))
159 char *nasm_strsep(char **stringp
, const char *delim
)
167 e
= strpbrk(s
, delim
);
177 #define lib_isnumchar(c) ( isalnum(c) || (c) == '$')
178 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
180 int64_t readnum(char *str
, int *error
)
184 uint64_t result
, checklimit
;
192 r
++; /* find start of number */
195 * If the number came from make_tok_num (as a result of an %assign), it
196 * might have a '-' built into it (rather than in a preceeding token).
205 while (lib_isnumchar(*q
))
206 q
++; /* find end of number */
209 * If it begins 0x, 0X or $, or ends in H, it's in hex. if it
210 * ends in Q, it's octal. if it ends in B, it's binary.
211 * Otherwise, it's ordinary decimal.
213 if (*r
== '0' && (r
[1] == 'x' || r
[1] == 'X'))
217 else if (q
[-1] == 'H' || q
[-1] == 'h')
219 else if (q
[-1] == 'Q' || q
[-1] == 'q' || q
[-1] == 'O' || q
[-1] == 'o')
221 else if (q
[-1] == 'B' || q
[-1] == 'b')
227 * If this number has been found for us by something other than
228 * the ordinary scanners, then it might be malformed by having
229 * nothing between the prefix and the suffix. Check this case
238 * `checklimit' must be 2**(32|64) / radix. We can't do that in
239 * 32/64-bit arithmetic, which we're (probably) using, so we
240 * cheat: since we know that all radices we use are even, we
241 * can divide 2**(31|63) by radix/2 instead.
243 if (globalbits
== 64)
244 checklimit
= 0x8000000000000000ULL
/ (radix
>> 1);
246 checklimit
= 0x80000000UL
/ (radix
>> 1);
249 * Calculate the highest allowable value for the last digit of a
250 * 32-bit constant... in radix 10, it is 6, otherwise it is 0
252 last
= (radix
== 10 ? 6 : 0);
255 while (*r
&& r
< q
) {
256 if (*r
< '0' || (*r
> '9' && *r
< 'A')
257 || (digit
= numvalue(*r
)) >= radix
) {
261 if (result
> checklimit
|| (result
== checklimit
&& digit
>= last
)) {
265 result
= radix
* result
+ digit
;
270 nasm_malloc_error(ERR_WARNING
| ERR_PASS1
| ERR_WARN_NOV
,
271 "numeric constant %s does not fit in 32 bits",
274 return result
* sign
;
277 int64_t readstrnum(char *str
, int length
, int *warn
)
279 int64_t charconst
= 0;
285 if (globalbits
== 64) {
286 for (i
= 0; i
< length
; i
++) {
287 if (charconst
& 0xFF00000000000000ULL
)
289 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
292 for (i
= 0; i
< length
; i
++) {
293 if (charconst
& 0xFF000000UL
)
295 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
301 static int32_t next_seg
;
308 int32_t seg_alloc(void)
310 return (next_seg
+= 2) - 2;
313 void fwriteint16_t(int data
, FILE * fp
)
315 fputc((int)(data
& 255), fp
);
316 fputc((int)((data
>> 8) & 255), fp
);
319 void fwriteint32_t(int32_t data
, FILE * fp
)
321 fputc((int)(data
& 255), fp
);
322 fputc((int)((data
>> 8) & 255), fp
);
323 fputc((int)((data
>> 16) & 255), fp
);
324 fputc((int)((data
>> 24) & 255), fp
);
327 void fwriteint64_t(int64_t data
, FILE * fp
)
329 fputc((int)(data
& 255), fp
);
330 fputc((int)((data
>> 8) & 255), fp
);
331 fputc((int)((data
>> 16) & 255), fp
);
332 fputc((int)((data
>> 24) & 255), fp
);
333 fputc((int)((data
>> 32) & 255), fp
);
334 fputc((int)((data
>> 40) & 255), fp
);
335 fputc((int)((data
>> 48) & 255), fp
);
336 fputc((int)((data
>> 56) & 255), fp
);
339 void standard_extension(char *inname
, char *outname
, char *extension
,
344 if (*outname
) /* file name already exists, */
345 return; /* so do nothing */
349 *p
++ = *q
++; /* copy, and find end of string */
350 *p
= '\0'; /* terminate it */
351 while (p
> outname
&& *--p
!= '.') ; /* find final period (or whatever) */
354 p
++; /* go back to end if none found */
355 if (!strcmp(p
, extension
)) { /* is the extension already there? */
357 error(ERR_WARNING
| ERR_NOFILE
,
358 "file name already ends in `%s': "
359 "output will be in `nasm.out'", extension
);
361 error(ERR_WARNING
| ERR_NOFILE
,
362 "file name already has no extension: "
363 "output will be in `nasm.out'");
364 strcpy(outname
, "nasm.out");
366 strcpy(p
, extension
);
369 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
370 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
372 #define LAYERSIZ(r) ( (r)->layers==0 ? RAA_BLKSIZE : RAA_LAYERSIZE )
374 static struct RAA
*real_raa_init(int layers
)
380 r
= nasm_malloc(LEAFSIZ
);
382 memset(r
->u
.l
.data
, 0, sizeof(r
->u
.l
.data
));
385 r
= nasm_malloc(BRANCHSIZ
);
387 for (i
= 0; i
< RAA_LAYERSIZE
; i
++)
388 r
->u
.b
.data
[i
] = NULL
;
389 r
->stepsize
= RAA_BLKSIZE
;
391 r
->stepsize
*= RAA_LAYERSIZE
;
396 struct RAA
*raa_init(void)
398 return real_raa_init(0);
401 void raa_free(struct RAA
*r
)
407 for (p
= r
->u
.b
.data
; p
- r
->u
.b
.data
< RAA_LAYERSIZE
; p
++)
413 int32_t raa_read(struct RAA
*r
, int32_t posn
)
415 if (posn
>= r
->stepsize
* LAYERSIZ(r
))
416 return 0; /* Return 0 for undefined entries */
417 while (r
->layers
> 0) {
419 l
= ldiv(posn
, r
->stepsize
);
420 r
= r
->u
.b
.data
[l
.quot
];
423 return 0; /* Return 0 for undefined entries */
425 return r
->u
.l
.data
[posn
];
428 struct RAA
*raa_write(struct RAA
*r
, int32_t posn
, int32_t value
)
433 nasm_malloc_error(ERR_PANIC
, "negative position in raa_write");
435 while (r
->stepsize
* LAYERSIZ(r
) <= posn
) {
442 s
= nasm_malloc(BRANCHSIZ
);
443 for (i
= 0; i
< RAA_LAYERSIZE
; i
++)
444 s
->u
.b
.data
[i
] = NULL
;
445 s
->layers
= r
->layers
+ 1;
446 s
->stepsize
= LAYERSIZ(r
) * r
->stepsize
;
453 while (r
->layers
> 0) {
456 l
= ldiv(posn
, r
->stepsize
);
457 s
= &r
->u
.b
.data
[l
.quot
];
459 *s
= real_raa_init(r
->layers
- 1);
464 r
->u
.l
.data
[posn
] = value
;
469 #define SAA_MAXLEN 8192
471 struct SAA
*saa_init(int32_t elem_len
)
475 if (elem_len
> SAA_MAXLEN
)
476 nasm_malloc_error(ERR_PANIC
| ERR_NOFILE
,
477 "SAA with huge elements");
479 s
= nasm_malloc(sizeof(struct SAA
));
480 s
->posn
= s
->start
= 0L;
481 s
->elem_len
= elem_len
;
482 s
->length
= SAA_MAXLEN
- (SAA_MAXLEN
% elem_len
);
483 s
->data
= nasm_malloc(s
->length
);
490 void saa_free(struct SAA
*s
)
502 void *saa_wstruct(struct SAA
*s
)
506 if (s
->end
->length
- s
->end
->posn
< s
->elem_len
) {
507 s
->end
->next
= nasm_malloc(sizeof(struct SAA
));
508 s
->end
->next
->start
= s
->end
->start
+ s
->end
->posn
;
509 s
->end
= s
->end
->next
;
510 s
->end
->length
= s
->length
;
513 s
->end
->data
= nasm_malloc(s
->length
);
516 p
= s
->end
->data
+ s
->end
->posn
;
517 s
->end
->posn
+= s
->elem_len
;
521 void saa_wbytes(struct SAA
*s
, const void *data
, int32_t len
)
523 const char *d
= data
;
526 int32_t l
= s
->end
->length
- s
->end
->posn
;
531 memcpy(s
->end
->data
+ s
->end
->posn
, d
, l
);
534 memset(s
->end
->data
+ s
->end
->posn
, 0, l
);
539 s
->end
->next
= nasm_malloc(sizeof(struct SAA
));
540 s
->end
->next
->start
= s
->end
->start
+ s
->end
->posn
;
541 s
->end
= s
->end
->next
;
542 s
->end
->length
= s
->length
;
545 s
->end
->data
= nasm_malloc(s
->length
);
550 void saa_rewind(struct SAA
*s
)
556 void *saa_rstruct(struct SAA
*s
)
563 if (s
->rptr
->posn
- s
->rpos
< s
->elem_len
) {
564 s
->rptr
= s
->rptr
->next
;
566 return NULL
; /* end of array */
570 p
= s
->rptr
->data
+ s
->rpos
;
571 s
->rpos
+= s
->elem_len
;
575 void *saa_rbytes(struct SAA
*s
, int32_t *len
)
582 p
= s
->rptr
->data
+ s
->rpos
;
583 *len
= s
->rptr
->posn
- s
->rpos
;
584 s
->rptr
= s
->rptr
->next
;
589 void saa_rnbytes(struct SAA
*s
, void *data
, int32_t len
)
599 l
= s
->rptr
->posn
- s
->rpos
;
603 memcpy(d
, s
->rptr
->data
+ s
->rpos
, l
);
609 s
->rptr
= s
->rptr
->next
;
615 void saa_fread(struct SAA
*s
, int32_t posn
, void *data
, int32_t len
)
621 if (!s
->rptr
|| posn
< s
->rptr
->start
)
624 while (posn
>= p
->start
+ p
->posn
) {
627 return; /* what else can we do?! */
630 pos
= posn
- p
->start
;
632 int64_t l
= p
->posn
- pos
;
635 memcpy(cdata
, p
->data
+ pos
, l
);
646 void saa_fwrite(struct SAA
*s
, int32_t posn
, void *data
, int32_t len
)
652 if (!s
->rptr
|| posn
< s
->rptr
->start
)
655 while (posn
>= p
->start
+ p
->posn
) {
658 return; /* what else can we do?! */
661 pos
= posn
- p
->start
;
663 int64_t l
= p
->posn
- pos
;
666 memcpy(p
->data
+ pos
, cdata
, l
);
677 void saa_fpwrite(struct SAA
*s
, FILE * fp
)
683 // while ((data = saa_rbytes(s, &len)))
684 for (; (data
= saa_rbytes(s
, &len
));)
685 fwrite(data
, 1, len
, fp
);
689 * Common list of prefix names
691 static const char *prefix_names
[] = {
692 "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
693 "repnz", "repz", "times"
696 const char *prefix_name(int token
)
698 unsigned int prefix
= token
-PREFIX_ENUM_START
;
699 if (prefix
> sizeof prefix_names
/ sizeof(const char *))
702 return prefix_names
[prefix
];
706 * Return TRUE if the argument is a simple scalar. (Or a far-
707 * absolute, which counts.)
709 int is_simple(expr
* vect
)
711 while (vect
->type
&& !vect
->value
)
715 if (vect
->type
!= EXPR_SIMPLE
)
719 } while (vect
->type
&& !vect
->value
);
720 if (vect
->type
&& vect
->type
< EXPR_SEGBASE
+ SEG_ABS
)
726 * Return TRUE if the argument is a simple scalar, _NOT_ a far-
729 int is_really_simple(expr
* vect
)
731 while (vect
->type
&& !vect
->value
)
735 if (vect
->type
!= EXPR_SIMPLE
)
739 } while (vect
->type
&& !vect
->value
);
746 * Return TRUE if the argument is relocatable (i.e. a simple
747 * scalar, plus at most one segment-base, plus possibly a WRT).
749 int is_reloc(expr
* vect
)
751 while (vect
->type
&& !vect
->value
) /* skip initial value-0 terms */
753 if (!vect
->type
) /* trivially return TRUE if nothing */
754 return 1; /* is present apart from value-0s */
755 if (vect
->type
< EXPR_SIMPLE
) /* FALSE if a register is present */
757 if (vect
->type
== EXPR_SIMPLE
) { /* skip over a pure number term... */
760 } while (vect
->type
&& !vect
->value
);
761 if (!vect
->type
) /* ...returning TRUE if that's all */
764 if (vect
->type
== EXPR_WRT
) { /* skip over a WRT term... */
767 } while (vect
->type
&& !vect
->value
);
768 if (!vect
->type
) /* ...returning TRUE if that's all */
771 if (vect
->value
!= 0 && vect
->value
!= 1)
772 return 0; /* segment base multiplier non-unity */
773 do { /* skip over _one_ seg-base term... */
775 } while (vect
->type
&& !vect
->value
);
776 if (!vect
->type
) /* ...returning TRUE if that's all */
778 return 0; /* And return FALSE if there's more */
782 * Return TRUE if the argument contains an `unknown' part.
784 int is_unknown(expr
* vect
)
786 while (vect
->type
&& vect
->type
< EXPR_UNKNOWN
)
788 return (vect
->type
== EXPR_UNKNOWN
);
792 * Return TRUE if the argument contains nothing but an `unknown'
795 int is_just_unknown(expr
* vect
)
797 while (vect
->type
&& !vect
->value
)
799 return (vect
->type
== EXPR_UNKNOWN
);
803 * Return the scalar part of a relocatable vector. (Including
804 * simple scalar vectors - those qualify as relocatable.)
806 int64_t reloc_value(expr
* vect
)
808 while (vect
->type
&& !vect
->value
)
812 if (vect
->type
== EXPR_SIMPLE
)
819 * Return the segment number of a relocatable vector, or NO_SEG for
822 int32_t reloc_seg(expr
* vect
)
824 while (vect
->type
&& (vect
->type
== EXPR_WRT
|| !vect
->value
))
826 if (vect
->type
== EXPR_SIMPLE
) {
829 } while (vect
->type
&& (vect
->type
== EXPR_WRT
|| !vect
->value
));
834 return vect
->type
- EXPR_SEGBASE
;
838 * Return the WRT segment number of a relocatable vector, or NO_SEG
839 * if no WRT part is present.
841 int32_t reloc_wrt(expr
* vect
)
843 while (vect
->type
&& vect
->type
< EXPR_WRT
)
845 if (vect
->type
== EXPR_WRT
) {
854 int bsi(char *string
, const char **array
, int size
)
856 int i
= -1, j
= size
; /* always, i < index < j */
859 int l
= strcmp(string
, array
[k
]);
860 if (l
< 0) /* it's in the first half */
862 else if (l
> 0) /* it's in the second half */
864 else /* we've got it :) */
867 return -1; /* we haven't got it :( */
870 int bsii(char *string
, const char **array
, int size
)
872 int i
= -1, j
= size
; /* always, i < index < j */
875 int l
= nasm_stricmp(string
, array
[k
]);
876 if (l
< 0) /* it's in the first half */
878 else if (l
> 0) /* it's in the second half */
880 else /* we've got it :) */
883 return -1; /* we haven't got it :( */
886 static char *file_name
= NULL
;
887 static int32_t line_number
= 0;
889 char *src_set_fname(char *newname
)
891 char *oldname
= file_name
;
896 int32_t src_set_linnum(int32_t newline
)
898 int32_t oldline
= line_number
;
899 line_number
= newline
;
903 int32_t src_get_linnum(void)
908 int src_get(int32_t *xline
, char **xname
)
910 if (!file_name
|| !*xname
|| strcmp(*xname
, file_name
)) {
912 *xname
= file_name
? nasm_strdup(file_name
) : NULL
;
913 *xline
= line_number
;
916 if (*xline
!= line_number
) {
917 int32_t tmp
= line_number
- *xline
;
918 *xline
= line_number
;
924 void nasm_quote(char **str
)
926 int ln
= strlen(*str
);
929 if (ln
> 1 && (*str
)[ln
- 1] == q
&& (q
== '"' || q
== '\''))
934 p
= nasm_malloc(ln
+ 3);
937 p
[ln
+ 1] = p
[0] = q
;
942 char *nasm_strcat(char *one
, char *two
)
945 int l1
= strlen(one
);
946 rslt
= nasm_malloc(l1
+ strlen(two
) + 1);
948 strcpy(rslt
+ l1
, two
);
952 void null_debug_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
959 void null_debug_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
965 void null_debug_deflabel(char *name
, int32_t segment
, int32_t offset
,
966 int is_global
, char *special
)
974 void null_debug_routine(const char *directive
, const char *params
)
979 void null_debug_typevalue(int32_t type
)
983 void null_debug_output(int type
, void *param
)
988 void null_debug_cleanup(void)
992 struct dfmt null_debug_form
= {
999 null_debug_typevalue
,
1004 struct dfmt
*null_debug_arr
[2] = { &null_debug_form
, NULL
};