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.
17 #include "insns.h" /* For MAX_KEYWORD */
19 int globalbits
= 0; /* defined in nasm.h, works better here for ASM+DISASM */
21 static efunc nasm_malloc_error
;
27 void nasm_set_malloc_error(efunc error
)
29 nasm_malloc_error
= error
;
31 logfp
= fopen("malloc.log", "w");
32 setvbuf(logfp
, NULL
, _IOLBF
, BUFSIZ
);
33 fprintf(logfp
, "null pointer is %p\n", NULL
);
38 void *nasm_malloc_log(char *file
, int line
, size_t size
)
40 void *nasm_malloc(size_t size
)
43 void *p
= malloc(size
);
45 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
48 fprintf(logfp
, "%s %d malloc(%ld) returns %p\n",
49 file
, line
, (int32_t)size
, p
);
55 void *nasm_realloc_log(char *file
, int line
, void *q
, size_t size
)
57 void *nasm_realloc(void *q
, size_t size
)
60 void *p
= q
? realloc(q
, size
) : malloc(size
);
62 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
65 fprintf(logfp
, "%s %d realloc(%p,%ld) returns %p\n",
66 file
, line
, q
, (int32_t)size
, p
);
68 fprintf(logfp
, "%s %d malloc(%ld) returns %p\n",
69 file
, line
, (int32_t)size
, p
);
75 void nasm_free_log(char *file
, int line
, void *q
)
77 void nasm_free(void *q
)
83 fprintf(logfp
, "%s %d free(%p)\n", file
, line
, q
);
89 char *nasm_strdup_log(char *file
, int line
, const char *s
)
91 char *nasm_strdup(const char *s
)
95 int size
= strlen(s
) + 1;
99 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
102 fprintf(logfp
, "%s %d strdup(%ld) returns %p\n",
103 file
, line
, (int32_t)size
, p
);
110 char *nasm_strndup_log(char *file
, int line
, char *s
, size_t len
)
112 char *nasm_strndup(char *s
, size_t len
)
120 nasm_malloc_error(ERR_FATAL
| ERR_NOFILE
, "out of memory");
123 fprintf(logfp
, "%s %d strndup(%ld) returns %p\n",
124 file
, line
, (int32_t)size
, p
);
131 #if !defined(stricmp) && !defined(strcasecmp)
132 int nasm_stricmp(const char *s1
, const char *s2
)
134 while (*s1
&& tolower(*s1
) == tolower(*s2
))
138 else if (tolower(*s1
) < tolower(*s2
))
145 #if !defined(strnicmp) && !defined(strncasecmp)
146 int nasm_strnicmp(const char *s1
, const char *s2
, int n
)
148 while (n
> 0 && *s1
&& tolower(*s1
) == tolower(*s2
))
150 if ((!*s1
&& !*s2
) || n
== 0)
152 else if (tolower(*s1
) < tolower(*s2
))
159 #define lib_isnumchar(c) ( isalnum(c) || (c) == '$')
160 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
162 int64_t readnum(char *str
, int *error
)
166 uint64_t result
, checklimit
;
174 r
++; /* find start of number */
177 * If the number came from make_tok_num (as a result of an %assign), it
178 * might have a '-' built into it (rather than in a preceeding token).
187 while (lib_isnumchar(*q
))
188 q
++; /* find end of number */
191 * If it begins 0x, 0X or $, or ends in H, it's in hex. if it
192 * ends in Q, it's octal. if it ends in B, it's binary.
193 * Otherwise, it's ordinary decimal.
195 if (*r
== '0' && (r
[1] == 'x' || r
[1] == 'X'))
199 else if (q
[-1] == 'H' || q
[-1] == 'h')
201 else if (q
[-1] == 'Q' || q
[-1] == 'q' || q
[-1] == 'O' || q
[-1] == 'o')
203 else if (q
[-1] == 'B' || q
[-1] == 'b')
209 * If this number has been found for us by something other than
210 * the ordinary scanners, then it might be malformed by having
211 * nothing between the prefix and the suffix. Check this case
220 * `checklimit' must be 2**(32|64) / radix. We can't do that in
221 * 32/64-bit arithmetic, which we're (probably) using, so we
222 * cheat: since we know that all radices we use are even, we
223 * can divide 2**(31|63) by radix/2 instead.
225 if (globalbits
== 64)
226 checklimit
= 0x8000000000000000ULL
/ (radix
>> 1);
228 checklimit
= 0x80000000UL
/ (radix
>> 1);
231 * Calculate the highest allowable value for the last digit of a
232 * 32-bit constant... in radix 10, it is 6, otherwise it is 0
234 last
= (radix
== 10 ? 6 : 0);
237 while (*r
&& r
< q
) {
238 if (*r
< '0' || (*r
> '9' && *r
< 'A')
239 || (digit
= numvalue(*r
)) >= radix
) {
243 if (result
> checklimit
|| (result
== checklimit
&& digit
>= last
)) {
247 result
= radix
* result
+ digit
;
252 nasm_malloc_error(ERR_WARNING
| ERR_PASS1
| ERR_WARN_NOV
,
253 "numeric constant %s does not fit in 32 bits",
256 return result
* sign
;
259 int64_t readstrnum(char *str
, int length
, int *warn
)
261 int64_t charconst
= 0;
267 if (globalbits
== 64) {
268 for (i
= 0; i
< length
; i
++) {
269 if (charconst
& 0xFF00000000000000ULL
)
271 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
274 for (i
= 0; i
< length
; i
++) {
275 if (charconst
& 0xFF000000UL
)
277 charconst
= (charconst
<< 8) + (uint8_t)*--str
;
283 static int32_t next_seg
;
290 int32_t seg_alloc(void)
292 return (next_seg
+= 2) - 2;
295 void fwriteint16_t(int data
, FILE * fp
)
297 fputc((int)(data
& 255), fp
);
298 fputc((int)((data
>> 8) & 255), fp
);
301 void fwriteint32_t(int32_t data
, FILE * fp
)
303 fputc((int)(data
& 255), fp
);
304 fputc((int)((data
>> 8) & 255), fp
);
305 fputc((int)((data
>> 16) & 255), fp
);
306 fputc((int)((data
>> 24) & 255), fp
);
309 void standard_extension(char *inname
, char *outname
, char *extension
,
314 if (*outname
) /* file name already exists, */
315 return; /* so do nothing */
319 *p
++ = *q
++; /* copy, and find end of string */
320 *p
= '\0'; /* terminate it */
321 while (p
> outname
&& *--p
!= '.') ; /* find final period (or whatever) */
324 p
++; /* go back to end if none found */
325 if (!strcmp(p
, extension
)) { /* is the extension already there? */
327 error(ERR_WARNING
| ERR_NOFILE
,
328 "file name already ends in `%s': "
329 "output will be in `nasm.out'", extension
);
331 error(ERR_WARNING
| ERR_NOFILE
,
332 "file name already has no extension: "
333 "output will be in `nasm.out'");
334 strcpy(outname
, "nasm.out");
336 strcpy(p
, extension
);
339 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
340 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
342 #define LAYERSIZ(r) ( (r)->layers==0 ? RAA_BLKSIZE : RAA_LAYERSIZE )
344 static struct RAA
*real_raa_init(int layers
)
350 r
= nasm_malloc(LEAFSIZ
);
352 memset(r
->u
.l
.data
, 0, sizeof(r
->u
.l
.data
));
355 r
= nasm_malloc(BRANCHSIZ
);
357 for (i
= 0; i
< RAA_LAYERSIZE
; i
++)
358 r
->u
.b
.data
[i
] = NULL
;
359 r
->stepsize
= RAA_BLKSIZE
;
361 r
->stepsize
*= RAA_LAYERSIZE
;
366 struct RAA
*raa_init(void)
368 return real_raa_init(0);
371 void raa_free(struct RAA
*r
)
377 for (p
= r
->u
.b
.data
; p
- r
->u
.b
.data
< RAA_LAYERSIZE
; p
++)
383 int32_t raa_read(struct RAA
*r
, int32_t posn
)
385 if (posn
>= r
->stepsize
* LAYERSIZ(r
))
386 return 0; /* Return 0 for undefined entries */
387 while (r
->layers
> 0) {
389 l
= ldiv(posn
, r
->stepsize
);
390 r
= r
->u
.b
.data
[l
.quot
];
393 return 0; /* Return 0 for undefined entries */
395 return r
->u
.l
.data
[posn
];
398 struct RAA
*raa_write(struct RAA
*r
, int32_t posn
, int32_t value
)
403 nasm_malloc_error(ERR_PANIC
, "negative position in raa_write");
405 while (r
->stepsize
* LAYERSIZ(r
) <= posn
) {
412 s
= nasm_malloc(BRANCHSIZ
);
413 for (i
= 0; i
< RAA_LAYERSIZE
; i
++)
414 s
->u
.b
.data
[i
] = NULL
;
415 s
->layers
= r
->layers
+ 1;
416 s
->stepsize
= LAYERSIZ(r
) * r
->stepsize
;
423 while (r
->layers
> 0) {
426 l
= ldiv(posn
, r
->stepsize
);
427 s
= &r
->u
.b
.data
[l
.quot
];
429 *s
= real_raa_init(r
->layers
- 1);
434 r
->u
.l
.data
[posn
] = value
;
439 #define SAA_MAXLEN 8192
441 struct SAA
*saa_init(int32_t elem_len
)
445 if (elem_len
> SAA_MAXLEN
)
446 nasm_malloc_error(ERR_PANIC
| ERR_NOFILE
,
447 "SAA with huge elements");
449 s
= nasm_malloc(sizeof(struct SAA
));
450 s
->posn
= s
->start
= 0L;
451 s
->elem_len
= elem_len
;
452 s
->length
= SAA_MAXLEN
- (SAA_MAXLEN
% elem_len
);
453 s
->data
= nasm_malloc(s
->length
);
460 void saa_free(struct SAA
*s
)
472 void *saa_wstruct(struct SAA
*s
)
476 if (s
->end
->length
- s
->end
->posn
< s
->elem_len
) {
477 s
->end
->next
= nasm_malloc(sizeof(struct SAA
));
478 s
->end
->next
->start
= s
->end
->start
+ s
->end
->posn
;
479 s
->end
= s
->end
->next
;
480 s
->end
->length
= s
->length
;
483 s
->end
->data
= nasm_malloc(s
->length
);
486 p
= s
->end
->data
+ s
->end
->posn
;
487 s
->end
->posn
+= s
->elem_len
;
491 void saa_wbytes(struct SAA
*s
, const void *data
, int32_t len
)
493 const char *d
= data
;
496 int32_t l
= s
->end
->length
- s
->end
->posn
;
501 memcpy(s
->end
->data
+ s
->end
->posn
, d
, l
);
504 memset(s
->end
->data
+ s
->end
->posn
, 0, l
);
509 s
->end
->next
= nasm_malloc(sizeof(struct SAA
));
510 s
->end
->next
->start
= s
->end
->start
+ s
->end
->posn
;
511 s
->end
= s
->end
->next
;
512 s
->end
->length
= s
->length
;
515 s
->end
->data
= nasm_malloc(s
->length
);
520 void saa_rewind(struct SAA
*s
)
526 void *saa_rstruct(struct SAA
*s
)
533 if (s
->rptr
->posn
- s
->rpos
< s
->elem_len
) {
534 s
->rptr
= s
->rptr
->next
;
536 return NULL
; /* end of array */
540 p
= s
->rptr
->data
+ s
->rpos
;
541 s
->rpos
+= s
->elem_len
;
545 void *saa_rbytes(struct SAA
*s
, int32_t *len
)
552 p
= s
->rptr
->data
+ s
->rpos
;
553 *len
= s
->rptr
->posn
- s
->rpos
;
554 s
->rptr
= s
->rptr
->next
;
559 void saa_rnbytes(struct SAA
*s
, void *data
, int32_t len
)
569 l
= s
->rptr
->posn
- s
->rpos
;
573 memcpy(d
, s
->rptr
->data
+ s
->rpos
, l
);
579 s
->rptr
= s
->rptr
->next
;
585 void saa_fread(struct SAA
*s
, int32_t posn
, void *data
, int32_t len
)
591 if (!s
->rptr
|| posn
< s
->rptr
->start
)
594 while (posn
>= p
->start
+ p
->posn
) {
597 return; /* what else can we do?! */
600 pos
= posn
- p
->start
;
602 int64_t l
= p
->posn
- pos
;
605 memcpy(cdata
, p
->data
+ pos
, l
);
616 void saa_fwrite(struct SAA
*s
, int32_t posn
, void *data
, int32_t len
)
622 if (!s
->rptr
|| posn
< s
->rptr
->start
)
625 while (posn
>= p
->start
+ p
->posn
) {
628 return; /* what else can we do?! */
631 pos
= posn
- p
->start
;
633 int64_t l
= p
->posn
- pos
;
636 memcpy(p
->data
+ pos
, cdata
, l
);
647 void saa_fpwrite(struct SAA
*s
, FILE * fp
)
653 // while ((data = saa_rbytes(s, &len)))
654 for (; (data
= saa_rbytes(s
, &len
));)
655 fwrite(data
, 1, len
, fp
);
659 * Register, instruction, condition-code and prefix keywords used
663 static const char *special_names
[] = {
664 "byte", "dword", "far", "long", "near", "nosplit", "qword",
665 "short", "strict", "to", "tword", "word"
667 static const char *prefix_names
[] = {
668 "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
669 "repnz", "repz", "times"
673 * Standard scanner routine used by parser.c and some output
674 * formats. It keeps a succession of temporary-storage strings in
675 * stdscan_tempstorage, which can be cleared using stdscan_reset.
677 static char **stdscan_tempstorage
= NULL
;
678 static int stdscan_tempsize
= 0, stdscan_templen
= 0;
679 #define STDSCAN_TEMP_DELTA 256
681 static void stdscan_pop(void)
683 nasm_free(stdscan_tempstorage
[--stdscan_templen
]);
686 void stdscan_reset(void)
688 while (stdscan_templen
> 0)
693 * Unimportant cleanup is done to avoid confusing people who are trying
694 * to debug real memory leaks
696 void nasmlib_cleanup(void)
699 nasm_free(stdscan_tempstorage
);
702 static char *stdscan_copy(char *p
, int len
)
706 text
= nasm_malloc(len
+ 1);
707 strncpy(text
, p
, len
);
710 if (stdscan_templen
>= stdscan_tempsize
) {
711 stdscan_tempsize
+= STDSCAN_TEMP_DELTA
;
712 stdscan_tempstorage
= nasm_realloc(stdscan_tempstorage
,
716 stdscan_tempstorage
[stdscan_templen
++] = text
;
721 char *stdscan_bufptr
= NULL
;
722 int stdscan(void *private_data
, struct tokenval
*tv
)
724 char ourcopy
[MAX_KEYWORD
+ 1], *r
, *s
;
726 (void)private_data
; /* Don't warn that this parameter is unused */
728 while (isspace(*stdscan_bufptr
))
730 if (!*stdscan_bufptr
)
731 return tv
->t_type
= 0;
733 /* we have a token; either an id, a number or a char */
734 if (isidstart(*stdscan_bufptr
) ||
735 (*stdscan_bufptr
== '$' && isidstart(stdscan_bufptr
[1]))) {
736 /* now we've got an identifier */
740 if (*stdscan_bufptr
== '$') {
745 r
= stdscan_bufptr
++;
746 /* read the entire buffer to advance the buffer pointer but... */
747 while (isidchar(*stdscan_bufptr
))
750 /* ... copy only up to IDLEN_MAX-1 characters */
751 tv
->t_charptr
= stdscan_copy(r
, stdscan_bufptr
- r
< IDLEN_MAX
?
752 stdscan_bufptr
- r
: IDLEN_MAX
- 1);
754 if (is_sym
|| stdscan_bufptr
- r
> MAX_KEYWORD
)
755 return tv
->t_type
= TOKEN_ID
; /* bypass all other checks */
757 for (s
= tv
->t_charptr
, r
= ourcopy
; *s
; s
++)
760 /* right, so we have an identifier sitting in temp storage. now,
761 * is it actually a register or instruction name, or what? */
762 if ((tv
->t_integer
= bsi(ourcopy
, reg_names
,
763 elements(reg_names
))) >= 0) {
764 tv
->t_integer
+= EXPR_REG_START
;
765 return tv
->t_type
= TOKEN_REG
;
766 } else if ((tv
->t_integer
= bsi(ourcopy
, insn_names
,
767 elements(insn_names
))) >= 0) {
768 return tv
->t_type
= TOKEN_INSN
;
770 for (i
= 0; i
< elements(icn
); i
++)
771 if (!strncmp(ourcopy
, icn
[i
], strlen(icn
[i
]))) {
772 char *p
= ourcopy
+ strlen(icn
[i
]);
773 tv
->t_integer
= ico
[i
];
774 if ((tv
->t_inttwo
= bsi(p
, conditions
,
775 elements(conditions
))) >= 0)
776 return tv
->t_type
= TOKEN_INSN
;
778 if ((tv
->t_integer
= bsi(ourcopy
, prefix_names
,
779 elements(prefix_names
))) >= 0) {
780 tv
->t_integer
+= PREFIX_ENUM_START
;
781 return tv
->t_type
= TOKEN_PREFIX
;
783 if ((tv
->t_integer
= bsi(ourcopy
, special_names
,
784 elements(special_names
))) >= 0)
785 return tv
->t_type
= TOKEN_SPECIAL
;
786 if (!nasm_stricmp(ourcopy
, "seg"))
787 return tv
->t_type
= TOKEN_SEG
;
788 if (!nasm_stricmp(ourcopy
, "wrt"))
789 return tv
->t_type
= TOKEN_WRT
;
790 return tv
->t_type
= TOKEN_ID
;
791 } else if (*stdscan_bufptr
== '$' && !isnumchar(stdscan_bufptr
[1])) {
793 * It's a $ sign with no following hex number; this must
794 * mean it's a Here token ($), evaluating to the current
795 * assembly location, or a Base token ($$), evaluating to
796 * the base of the current segment.
799 if (*stdscan_bufptr
== '$') {
801 return tv
->t_type
= TOKEN_BASE
;
803 return tv
->t_type
= TOKEN_HERE
;
804 } else if (isnumstart(*stdscan_bufptr
)) { /* now we've got a number */
807 r
= stdscan_bufptr
++;
808 while (isnumchar(*stdscan_bufptr
))
811 if (*stdscan_bufptr
== '.') {
813 * a floating point constant
816 while (isnumchar(*stdscan_bufptr
) ||
817 ((stdscan_bufptr
[-1] == 'e'
818 || stdscan_bufptr
[-1] == 'E')
819 && (*stdscan_bufptr
== '-' || *stdscan_bufptr
== '+'))) {
822 tv
->t_charptr
= stdscan_copy(r
, stdscan_bufptr
- r
);
823 return tv
->t_type
= TOKEN_FLOAT
;
825 r
= stdscan_copy(r
, stdscan_bufptr
- r
);
826 tv
->t_integer
= readnum(r
, &rn_error
);
829 return tv
->t_type
= TOKEN_ERRNUM
; /* some malformation occurred */
830 tv
->t_charptr
= NULL
;
831 return tv
->t_type
= TOKEN_NUM
;
832 } else if (*stdscan_bufptr
== '\'' || *stdscan_bufptr
== '"') { /* a char constant */
833 char quote
= *stdscan_bufptr
++, *r
;
835 r
= tv
->t_charptr
= stdscan_bufptr
;
836 while (*stdscan_bufptr
&& *stdscan_bufptr
!= quote
)
838 tv
->t_inttwo
= stdscan_bufptr
- r
; /* store full version */
839 if (!*stdscan_bufptr
)
840 return tv
->t_type
= TOKEN_ERRNUM
; /* unmatched quotes */
841 stdscan_bufptr
++; /* skip over final quote */
842 tv
->t_integer
= readstrnum(r
, tv
->t_inttwo
, &rn_warn
);
843 /* FIXME: rn_warn is not checked! */
844 return tv
->t_type
= TOKEN_NUM
;
845 } else if (*stdscan_bufptr
== ';') { /* a comment has happened - stay */
846 return tv
->t_type
= 0;
847 } else if (stdscan_bufptr
[0] == '>' && stdscan_bufptr
[1] == '>') {
849 return tv
->t_type
= TOKEN_SHR
;
850 } else if (stdscan_bufptr
[0] == '<' && stdscan_bufptr
[1] == '<') {
852 return tv
->t_type
= TOKEN_SHL
;
853 } else if (stdscan_bufptr
[0] == '/' && stdscan_bufptr
[1] == '/') {
855 return tv
->t_type
= TOKEN_SDIV
;
856 } else if (stdscan_bufptr
[0] == '%' && stdscan_bufptr
[1] == '%') {
858 return tv
->t_type
= TOKEN_SMOD
;
859 } else if (stdscan_bufptr
[0] == '=' && stdscan_bufptr
[1] == '=') {
861 return tv
->t_type
= TOKEN_EQ
;
862 } else if (stdscan_bufptr
[0] == '<' && stdscan_bufptr
[1] == '>') {
864 return tv
->t_type
= TOKEN_NE
;
865 } else if (stdscan_bufptr
[0] == '!' && stdscan_bufptr
[1] == '=') {
867 return tv
->t_type
= TOKEN_NE
;
868 } else if (stdscan_bufptr
[0] == '<' && stdscan_bufptr
[1] == '=') {
870 return tv
->t_type
= TOKEN_LE
;
871 } else if (stdscan_bufptr
[0] == '>' && stdscan_bufptr
[1] == '=') {
873 return tv
->t_type
= TOKEN_GE
;
874 } else if (stdscan_bufptr
[0] == '&' && stdscan_bufptr
[1] == '&') {
876 return tv
->t_type
= TOKEN_DBL_AND
;
877 } else if (stdscan_bufptr
[0] == '^' && stdscan_bufptr
[1] == '^') {
879 return tv
->t_type
= TOKEN_DBL_XOR
;
880 } else if (stdscan_bufptr
[0] == '|' && stdscan_bufptr
[1] == '|') {
882 return tv
->t_type
= TOKEN_DBL_OR
;
883 } else /* just an ordinary char */
884 return tv
->t_type
= (uint8_t)(*stdscan_bufptr
++);
888 * Return TRUE if the argument is a simple scalar. (Or a far-
889 * absolute, which counts.)
891 int is_simple(expr
* vect
)
893 while (vect
->type
&& !vect
->value
)
897 if (vect
->type
!= EXPR_SIMPLE
)
901 } while (vect
->type
&& !vect
->value
);
902 if (vect
->type
&& vect
->type
< EXPR_SEGBASE
+ SEG_ABS
)
908 * Return TRUE if the argument is a simple scalar, _NOT_ a far-
911 int is_really_simple(expr
* vect
)
913 while (vect
->type
&& !vect
->value
)
917 if (vect
->type
!= EXPR_SIMPLE
)
921 } while (vect
->type
&& !vect
->value
);
928 * Return TRUE if the argument is relocatable (i.e. a simple
929 * scalar, plus at most one segment-base, plus possibly a WRT).
931 int is_reloc(expr
* vect
)
933 while (vect
->type
&& !vect
->value
) /* skip initial value-0 terms */
935 if (!vect
->type
) /* trivially return TRUE if nothing */
936 return 1; /* is present apart from value-0s */
937 if (vect
->type
< EXPR_SIMPLE
) /* FALSE if a register is present */
939 if (vect
->type
== EXPR_SIMPLE
) { /* skip over a pure number term... */
942 } while (vect
->type
&& !vect
->value
);
943 if (!vect
->type
) /* ...returning TRUE if that's all */
946 if (vect
->type
== EXPR_WRT
) { /* skip over a WRT term... */
949 } while (vect
->type
&& !vect
->value
);
950 if (!vect
->type
) /* ...returning TRUE if that's all */
953 if (vect
->value
!= 0 && vect
->value
!= 1)
954 return 0; /* segment base multiplier non-unity */
955 do { /* skip over _one_ seg-base term... */
957 } while (vect
->type
&& !vect
->value
);
958 if (!vect
->type
) /* ...returning TRUE if that's all */
960 return 0; /* And return FALSE if there's more */
964 * Return TRUE if the argument contains an `unknown' part.
966 int is_unknown(expr
* vect
)
968 while (vect
->type
&& vect
->type
< EXPR_UNKNOWN
)
970 return (vect
->type
== EXPR_UNKNOWN
);
974 * Return TRUE if the argument contains nothing but an `unknown'
977 int is_just_unknown(expr
* vect
)
979 while (vect
->type
&& !vect
->value
)
981 return (vect
->type
== EXPR_UNKNOWN
);
985 * Return the scalar part of a relocatable vector. (Including
986 * simple scalar vectors - those qualify as relocatable.)
988 int64_t reloc_value(expr
* vect
)
990 while (vect
->type
&& !vect
->value
)
994 if (vect
->type
== EXPR_SIMPLE
)
1001 * Return the segment number of a relocatable vector, or NO_SEG for
1004 int32_t reloc_seg(expr
* vect
)
1006 while (vect
->type
&& (vect
->type
== EXPR_WRT
|| !vect
->value
))
1008 if (vect
->type
== EXPR_SIMPLE
) {
1011 } while (vect
->type
&& (vect
->type
== EXPR_WRT
|| !vect
->value
));
1016 return vect
->type
- EXPR_SEGBASE
;
1020 * Return the WRT segment number of a relocatable vector, or NO_SEG
1021 * if no WRT part is present.
1023 int32_t reloc_wrt(expr
* vect
)
1025 while (vect
->type
&& vect
->type
< EXPR_WRT
)
1027 if (vect
->type
== EXPR_WRT
) {
1036 int bsi(char *string
, const char **array
, int size
)
1038 int i
= -1, j
= size
; /* always, i < index < j */
1039 while (j
- i
>= 2) {
1040 int k
= (i
+ j
) / 2;
1041 int l
= strcmp(string
, array
[k
]);
1042 if (l
< 0) /* it's in the first half */
1044 else if (l
> 0) /* it's in the second half */
1046 else /* we've got it :) */
1049 return -1; /* we haven't got it :( */
1052 static char *file_name
= NULL
;
1053 static int32_t line_number
= 0;
1055 char *src_set_fname(char *newname
)
1057 char *oldname
= file_name
;
1058 file_name
= newname
;
1062 int32_t src_set_linnum(int32_t newline
)
1064 int32_t oldline
= line_number
;
1065 line_number
= newline
;
1069 int32_t src_get_linnum(void)
1074 int src_get(int32_t *xline
, char **xname
)
1076 if (!file_name
|| !*xname
|| strcmp(*xname
, file_name
)) {
1078 *xname
= file_name
? nasm_strdup(file_name
) : NULL
;
1079 *xline
= line_number
;
1082 if (*xline
!= line_number
) {
1083 int32_t tmp
= line_number
- *xline
;
1084 *xline
= line_number
;
1090 void nasm_quote(char **str
)
1092 int ln
= strlen(*str
);
1095 if (ln
> 1 && (*str
)[ln
- 1] == q
&& (q
== '"' || q
== '\''))
1098 if (strchr(*str
, q
))
1100 p
= nasm_malloc(ln
+ 3);
1101 strcpy(p
+ 1, *str
);
1103 p
[ln
+ 1] = p
[0] = q
;
1108 char *nasm_strcat(char *one
, char *two
)
1111 int l1
= strlen(one
);
1112 rslt
= nasm_malloc(l1
+ strlen(two
) + 1);
1114 strcpy(rslt
+ l1
, two
);
1118 void null_debug_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1125 void null_debug_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1131 void null_debug_deflabel(char *name
, int32_t segment
, int32_t offset
,
1132 int is_global
, char *special
)
1140 void null_debug_routine(const char *directive
, const char *params
)
1145 void null_debug_typevalue(int32_t type
)
1149 void null_debug_output(int type
, void *param
)
1154 void null_debug_cleanup(void)
1158 struct dfmt null_debug_form
= {
1159 "Null debug format",
1163 null_debug_deflabel
,
1165 null_debug_typevalue
,
1170 struct dfmt
*null_debug_arr
[2] = { &null_debug_form
, NULL
};