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.
16 #include "insns.h" /* For MAX_KEYWORD */
18 static efunc nasm_malloc_error
;
24 void nasm_set_malloc_error (efunc error
)
26 nasm_malloc_error
= error
;
28 logfp
= fopen ("malloc.log", "w");
29 setvbuf (logfp
, NULL
, _IOLBF
, BUFSIZ
);
30 fprintf (logfp
, "null pointer is %p\n", NULL
);
35 void *nasm_malloc_log (char *file
, int line
, size_t size
)
37 void *nasm_malloc (size_t size
)
40 void *p
= malloc(size
);
42 nasm_malloc_error (ERR_FATAL
| ERR_NOFILE
, "out of memory");
45 fprintf(logfp
, "%s %d malloc(%ld) returns %p\n",
46 file
, line
, (long)size
, p
);
52 void *nasm_realloc_log (char *file
, int line
, void *q
, size_t size
)
54 void *nasm_realloc (void *q
, size_t size
)
57 void *p
= q
? realloc(q
, size
) : malloc(size
);
59 nasm_malloc_error (ERR_FATAL
| ERR_NOFILE
, "out of memory");
62 fprintf(logfp
, "%s %d realloc(%p,%ld) returns %p\n",
63 file
, line
, q
, (long)size
, p
);
65 fprintf(logfp
, "%s %d malloc(%ld) returns %p\n",
66 file
, line
, (long)size
, p
);
72 void nasm_free_log (char *file
, int line
, void *q
)
74 void nasm_free (void *q
)
80 fprintf(logfp
, "%s %d free(%p)\n",
87 char *nasm_strdup_log (char *file
, int line
, const char *s
)
89 char *nasm_strdup (const char *s
)
93 int size
= strlen(s
)+1;
97 nasm_malloc_error (ERR_FATAL
| ERR_NOFILE
, "out of memory");
100 fprintf(logfp
, "%s %d strdup(%ld) returns %p\n",
101 file
, line
, (long)size
, p
);
108 char *nasm_strndup_log (char *file
, int line
, char *s
, size_t len
)
110 char *nasm_strndup (char *s
, size_t len
)
118 nasm_malloc_error (ERR_FATAL
| ERR_NOFILE
, "out of memory");
121 fprintf(logfp
, "%s %d strndup(%ld) returns %p\n",
122 file
, line
, (long)size
, p
);
129 #if !defined(stricmp) && !defined(strcasecmp)
130 int nasm_stricmp (const char *s1
, const char *s2
)
132 while (*s1
&& tolower(*s1
) == tolower(*s2
))
136 else if (tolower(*s1
) < tolower(*s2
))
143 #if !defined(strnicmp) && !defined(strncasecmp)
144 int nasm_strnicmp (const char *s1
, const char *s2
, int n
)
146 while (n
> 0 && *s1
&& tolower(*s1
) == tolower(*s2
))
148 if ((!*s1
&& !*s2
) || n
==0)
150 else if (tolower(*s1
) < tolower(*s2
))
157 #define lib_isnumchar(c) ( isalnum(c) || (c) == '$')
158 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
160 long readnum (char *str
, int *error
)
164 unsigned long result
, checklimit
;
171 while (isspace(*r
)) r
++; /* find start of number */
174 * If the number came from make_tok_num (as a result of an %assign), it
175 * might have a '-' built into it (rather than in a preceeding token).
185 while (lib_isnumchar(*q
)) q
++; /* find end of number */
188 * If it begins 0x, 0X or $, or ends in H, it's in hex. if it
189 * ends in Q, it's octal. if it ends in B, it's binary.
190 * Otherwise, it's ordinary decimal.
192 if (*r
=='0' && (r
[1]=='x' || r
[1]=='X'))
196 else if (q
[-1]=='H' || q
[-1]=='h')
198 else if (q
[-1]=='Q' || q
[-1]=='q' || q
[-1]=='O' || q
[-1]=='o')
200 else if (q
[-1]=='B' || q
[-1]=='b')
206 * If this number has been found for us by something other than
207 * the ordinary scanners, then it might be malformed by having
208 * nothing between the prefix and the suffix. Check this case
217 * `checklimit' must be 2**32 / radix. We can't do that in
218 * 32-bit arithmetic, which we're (probably) using, so we
219 * cheat: since we know that all radices we use are even, we
220 * can divide 2**31 by radix/2 instead.
222 checklimit
= 0x80000000UL
/ (radix
>>1);
225 * Calculate the highest allowable value for the last digit
226 * of a 32 bit constant... in radix 10, it is 6, otherwise it is 0
228 last
= (radix
== 10 ? 6 : 0);
231 while (*r
&& r
< q
) {
232 if (*r
<'0' || (*r
>'9' && *r
<'A') || (digit
= numvalue(*r
)) >= radix
)
237 if (result
> checklimit
||
238 (result
== checklimit
&& digit
>= last
))
243 result
= radix
* result
+ digit
;
248 nasm_malloc_error (ERR_WARNING
| ERR_PASS1
| ERR_WARN_NOV
,
249 "numeric constant %s does not fit in 32 bits",
255 long readstrnum (char *str
, int length
, int *warn
)
263 for (i
=0; i
<length
; i
++) {
264 if (charconst
& 0xff000000UL
) {
267 charconst
= (charconst
<<8) + (unsigned char) *--str
;
272 static long next_seg
;
281 return (next_seg
+= 2) - 2;
284 void fwriteshort (int data
, FILE *fp
)
286 fputc ((int) (data
& 255), fp
);
287 fputc ((int) ((data
>> 8) & 255), fp
);
290 void fwritelong (long data
, FILE *fp
)
292 fputc ((int) (data
& 255), fp
);
293 fputc ((int) ((data
>> 8) & 255), fp
);
294 fputc ((int) ((data
>> 16) & 255), fp
);
295 fputc ((int) ((data
>> 24) & 255), fp
);
298 void standard_extension (char *inname
, char *outname
, char *extension
,
303 if (*outname
) /* file name already exists, */
304 return; /* so do nothing */
307 while (*q
) *p
++ = *q
++; /* copy, and find end of string */
308 *p
= '\0'; /* terminate it */
309 while (p
> outname
&& *--p
!= '.');/* find final period (or whatever) */
310 if (*p
!= '.') while (*p
) p
++; /* go back to end if none found */
311 if (!strcmp(p
, extension
)) { /* is the extension already there? */
313 error(ERR_WARNING
| ERR_NOFILE
,
314 "file name already ends in `%s': "
315 "output will be in `nasm.out'",
318 error(ERR_WARNING
| ERR_NOFILE
,
319 "file name already has no extension: "
320 "output will be in `nasm.out'");
321 strcpy(outname
, "nasm.out");
323 strcpy(p
, extension
);
326 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
327 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
329 #define LAYERSIZ(r) ( (r)->layers==0 ? RAA_BLKSIZE : RAA_LAYERSIZE )
331 static struct RAA
*real_raa_init (int layers
)
337 r
= nasm_malloc (LEAFSIZ
);
339 memset (r
->u
.l
.data
, 0, sizeof(r
->u
.l
.data
));
342 r
= nasm_malloc (BRANCHSIZ
);
344 for ( i
= 0 ; i
< RAA_LAYERSIZE
; i
++ )
345 r
->u
.b
.data
[i
] = NULL
;
346 r
->stepsize
= RAA_BLKSIZE
;
348 r
->stepsize
*= RAA_LAYERSIZE
;
353 struct RAA
*raa_init (void)
355 return real_raa_init (0);
358 void raa_free (struct RAA
*r
)
364 for (p
= r
->u
.b
.data
; p
- r
->u
.b
.data
< RAA_LAYERSIZE
; p
++)
370 long raa_read (struct RAA
*r
, long posn
)
372 if (posn
>= r
->stepsize
* LAYERSIZ(r
))
373 return 0; /* Return 0 for undefined entries */
374 while (r
->layers
> 0) {
376 l
= ldiv (posn
, r
->stepsize
);
377 r
= r
->u
.b
.data
[l
.quot
];
380 return 0; /* Return 0 for undefined entries */
382 return r
->u
.l
.data
[posn
];
385 struct RAA
*raa_write (struct RAA
*r
, long posn
, long value
)
390 nasm_malloc_error (ERR_PANIC
, "negative position in raa_write");
392 while (r
->stepsize
* LAYERSIZ(r
) <= posn
) {
399 s
= nasm_malloc (BRANCHSIZ
);
400 for ( i
= 0 ; i
< RAA_LAYERSIZE
; i
++ )
401 s
->u
.b
.data
[i
] = NULL
;
402 s
->layers
= r
->layers
+ 1;
403 s
->stepsize
= LAYERSIZ(r
) * r
->stepsize
;
410 while (r
->layers
> 0) {
413 l
= ldiv (posn
, r
->stepsize
);
414 s
= &r
->u
.b
.data
[l
.quot
];
416 *s
= real_raa_init (r
->layers
- 1);
421 r
->u
.l
.data
[posn
] = value
;
426 #define SAA_MAXLEN 8192
428 struct SAA
*saa_init (long elem_len
)
432 if (elem_len
> SAA_MAXLEN
)
433 nasm_malloc_error (ERR_PANIC
| ERR_NOFILE
, "SAA with huge elements");
435 s
= nasm_malloc (sizeof(struct SAA
));
436 s
->posn
= s
->start
= 0L;
437 s
->elem_len
= elem_len
;
438 s
->length
= SAA_MAXLEN
- (SAA_MAXLEN
% elem_len
);
439 s
->data
= nasm_malloc (s
->length
);
446 void saa_free (struct SAA
*s
)
458 void *saa_wstruct (struct SAA
*s
)
462 if (s
->end
->length
- s
->end
->posn
< s
->elem_len
) {
463 s
->end
->next
= nasm_malloc (sizeof(struct SAA
));
464 s
->end
->next
->start
= s
->end
->start
+ s
->end
->posn
;
465 s
->end
= s
->end
->next
;
466 s
->end
->length
= s
->length
;
469 s
->end
->data
= nasm_malloc (s
->length
);
472 p
= s
->end
->data
+ s
->end
->posn
;
473 s
->end
->posn
+= s
->elem_len
;
477 void saa_wbytes (struct SAA
*s
, const void *data
, long len
)
479 const char *d
= data
;
482 long l
= s
->end
->length
- s
->end
->posn
;
487 memcpy (s
->end
->data
+ s
->end
->posn
, d
, l
);
490 memset (s
->end
->data
+ s
->end
->posn
, 0, l
);
495 s
->end
->next
= nasm_malloc (sizeof(struct SAA
));
496 s
->end
->next
->start
= s
->end
->start
+ s
->end
->posn
;
497 s
->end
= s
->end
->next
;
498 s
->end
->length
= s
->length
;
501 s
->end
->data
= nasm_malloc (s
->length
);
506 void saa_rewind (struct SAA
*s
)
512 void *saa_rstruct (struct SAA
*s
)
519 if (s
->rptr
->posn
- s
->rpos
< s
->elem_len
) {
520 s
->rptr
= s
->rptr
->next
;
522 return NULL
; /* end of array */
526 p
= s
->rptr
->data
+ s
->rpos
;
527 s
->rpos
+= s
->elem_len
;
531 void *saa_rbytes (struct SAA
*s
, long *len
)
538 p
= s
->rptr
->data
+ s
->rpos
;
539 *len
= s
->rptr
->posn
- s
->rpos
;
540 s
->rptr
= s
->rptr
->next
;
545 void saa_rnbytes (struct SAA
*s
, void *data
, long len
)
555 l
= s
->rptr
->posn
- s
->rpos
;
559 memcpy (d
, s
->rptr
->data
+ s
->rpos
, l
);
565 s
->rptr
= s
->rptr
->next
;
571 void saa_fread (struct SAA
*s
, long posn
, void *data
, long len
)
577 if (!s
->rptr
|| posn
< s
->rptr
->start
)
580 while (posn
>= p
->start
+ p
->posn
) {
583 return; /* what else can we do?! */
586 pos
= posn
- p
->start
;
588 long l
= p
->posn
- pos
;
591 memcpy (cdata
, p
->data
+pos
, l
);
602 void saa_fwrite (struct SAA
*s
, long posn
, void *data
, long len
)
608 if (!s
->rptr
|| posn
< s
->rptr
->start
)
611 while (posn
>= p
->start
+ p
->posn
) {
614 return; /* what else can we do?! */
617 pos
= posn
- p
->start
;
619 long l
= p
->posn
- pos
;
622 memcpy (p
->data
+pos
, cdata
, l
);
633 void saa_fpwrite (struct SAA
*s
, FILE *fp
)
639 while ( (data
= saa_rbytes (s
, &len
)) )
640 fwrite (data
, 1, len
, fp
);
644 * Register, instruction, condition-code and prefix keywords used
648 static const char *special_names
[] = {
649 "byte", "dword", "far", "long", "near", "nosplit", "qword",
650 "short", "strict", "to", "tword", "word"
652 static const char *prefix_names
[] = {
653 "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
654 "repnz", "repz", "times"
659 * Standard scanner routine used by parser.c and some output
660 * formats. It keeps a succession of temporary-storage strings in
661 * stdscan_tempstorage, which can be cleared using stdscan_reset.
663 static char **stdscan_tempstorage
= NULL
;
664 static int stdscan_tempsize
= 0, stdscan_templen
= 0;
665 #define STDSCAN_TEMP_DELTA 256
667 static void stdscan_pop(void)
669 nasm_free (stdscan_tempstorage
[--stdscan_templen
]);
672 void stdscan_reset(void)
674 while (stdscan_templen
> 0)
679 * Unimportant cleanup is done to avoid confusing people who are trying
680 * to debug real memory leaks
682 void nasmlib_cleanup (void)
685 nasm_free (stdscan_tempstorage
);
688 static char *stdscan_copy(char *p
, int len
)
692 text
= nasm_malloc(len
+1);
693 strncpy (text
, p
, len
);
696 if (stdscan_templen
>= stdscan_tempsize
) {
697 stdscan_tempsize
+= STDSCAN_TEMP_DELTA
;
698 stdscan_tempstorage
= nasm_realloc(stdscan_tempstorage
,
699 stdscan_tempsize
*sizeof(char *));
701 stdscan_tempstorage
[stdscan_templen
++] = text
;
706 char *stdscan_bufptr
= NULL
;
707 int stdscan (void *private_data
, struct tokenval
*tv
)
709 char ourcopy
[MAX_KEYWORD
+1], *r
, *s
;
711 (void) private_data
; /* Don't warn that this parameter is unused */
713 while (isspace(*stdscan_bufptr
)) stdscan_bufptr
++;
714 if (!*stdscan_bufptr
)
715 return tv
->t_type
= 0;
717 /* we have a token; either an id, a number or a char */
718 if (isidstart(*stdscan_bufptr
) ||
719 (*stdscan_bufptr
== '$' && isidstart(stdscan_bufptr
[1]))) {
720 /* now we've got an identifier */
724 if (*stdscan_bufptr
== '$') {
729 r
= stdscan_bufptr
++;
730 /* read the entire buffer to advance the buffer pointer but... */
731 while (isidchar(*stdscan_bufptr
)) stdscan_bufptr
++;
733 /* ... copy only up to IDLEN_MAX-1 characters */
734 tv
->t_charptr
= stdscan_copy(r
, stdscan_bufptr
- r
< IDLEN_MAX
?
735 stdscan_bufptr
- r
: IDLEN_MAX
- 1);
737 if (is_sym
|| stdscan_bufptr
-r
> MAX_KEYWORD
)
738 return tv
->t_type
= TOKEN_ID
;/* bypass all other checks */
740 for (s
=tv
->t_charptr
, r
=ourcopy
; *s
; s
++)
743 /* right, so we have an identifier sitting in temp storage. now,
744 * is it actually a register or instruction name, or what? */
745 if ((tv
->t_integer
=bsi(ourcopy
, reg_names
,
746 elements(reg_names
)))>=0) {
747 tv
->t_integer
+= EXPR_REG_START
;
748 return tv
->t_type
= TOKEN_REG
;
749 } else if ((tv
->t_integer
=bsi(ourcopy
, insn_names
,
750 elements(insn_names
)))>=0) {
751 return tv
->t_type
= TOKEN_INSN
;
753 for (i
=0; i
<elements(icn
); i
++)
754 if (!strncmp(ourcopy
, icn
[i
], strlen(icn
[i
]))) {
755 char *p
= ourcopy
+ strlen(icn
[i
]);
756 tv
->t_integer
= ico
[i
];
757 if ((tv
->t_inttwo
=bsi(p
, conditions
,
758 elements(conditions
)))>=0)
759 return tv
->t_type
= TOKEN_INSN
;
761 if ((tv
->t_integer
=bsi(ourcopy
, prefix_names
,
762 elements(prefix_names
)))>=0) {
763 tv
->t_integer
+= PREFIX_ENUM_START
;
764 return tv
->t_type
= TOKEN_PREFIX
;
766 if ((tv
->t_integer
=bsi(ourcopy
, special_names
,
767 elements(special_names
)))>=0)
768 return tv
->t_type
= TOKEN_SPECIAL
;
769 if (!nasm_stricmp(ourcopy
, "seg"))
770 return tv
->t_type
= TOKEN_SEG
;
771 if (!nasm_stricmp(ourcopy
, "wrt"))
772 return tv
->t_type
= TOKEN_WRT
;
773 return tv
->t_type
= TOKEN_ID
;
774 } else if (*stdscan_bufptr
== '$' && !isnumchar(stdscan_bufptr
[1])) {
776 * It's a $ sign with no following hex number; this must
777 * mean it's a Here token ($), evaluating to the current
778 * assembly location, or a Base token ($$), evaluating to
779 * the base of the current segment.
782 if (*stdscan_bufptr
== '$') {
784 return tv
->t_type
= TOKEN_BASE
;
786 return tv
->t_type
= TOKEN_HERE
;
787 } else if (isnumstart(*stdscan_bufptr
)) { /* now we've got a number */
790 r
= stdscan_bufptr
++;
791 while (isnumchar(*stdscan_bufptr
))
794 if (*stdscan_bufptr
== '.') {
796 * a floating point constant
799 while (isnumchar(*stdscan_bufptr
) ||
800 ((stdscan_bufptr
[-1] == 'e' || stdscan_bufptr
[-1] == 'E')
801 && (*stdscan_bufptr
== '-' || *stdscan_bufptr
== '+')) )
805 tv
->t_charptr
= stdscan_copy(r
, stdscan_bufptr
- r
);
806 return tv
->t_type
= TOKEN_FLOAT
;
808 r
= stdscan_copy(r
, stdscan_bufptr
- r
);
809 tv
->t_integer
= readnum(r
, &rn_error
);
812 return tv
->t_type
= TOKEN_ERRNUM
;/* some malformation occurred */
813 tv
->t_charptr
= NULL
;
814 return tv
->t_type
= TOKEN_NUM
;
815 } else if (*stdscan_bufptr
== '\'' ||
816 *stdscan_bufptr
== '"') {/* a char constant */
817 char quote
= *stdscan_bufptr
++, *r
;
819 r
= tv
->t_charptr
= stdscan_bufptr
;
820 while (*stdscan_bufptr
&& *stdscan_bufptr
!= quote
) stdscan_bufptr
++;
821 tv
->t_inttwo
= stdscan_bufptr
- r
; /* store full version */
822 if (!*stdscan_bufptr
)
823 return tv
->t_type
= TOKEN_ERRNUM
; /* unmatched quotes */
824 stdscan_bufptr
++; /* skip over final quote */
825 tv
->t_integer
= readstrnum(r
, tv
->t_inttwo
, &rn_warn
);
826 /* FIXME: rn_warn is not checked! */
827 return tv
->t_type
= TOKEN_NUM
;
828 } else if (*stdscan_bufptr
== ';') { /* a comment has happened - stay */
829 return tv
->t_type
= 0;
830 } else if (stdscan_bufptr
[0] == '>' && stdscan_bufptr
[1] == '>') {
832 return tv
->t_type
= TOKEN_SHR
;
833 } else if (stdscan_bufptr
[0] == '<' && stdscan_bufptr
[1] == '<') {
835 return tv
->t_type
= TOKEN_SHL
;
836 } else if (stdscan_bufptr
[0] == '/' && stdscan_bufptr
[1] == '/') {
838 return tv
->t_type
= TOKEN_SDIV
;
839 } else if (stdscan_bufptr
[0] == '%' && stdscan_bufptr
[1] == '%') {
841 return tv
->t_type
= TOKEN_SMOD
;
842 } else if (stdscan_bufptr
[0] == '=' && stdscan_bufptr
[1] == '=') {
844 return tv
->t_type
= TOKEN_EQ
;
845 } else if (stdscan_bufptr
[0] == '<' && stdscan_bufptr
[1] == '>') {
847 return tv
->t_type
= TOKEN_NE
;
848 } else if (stdscan_bufptr
[0] == '!' && stdscan_bufptr
[1] == '=') {
850 return tv
->t_type
= TOKEN_NE
;
851 } else if (stdscan_bufptr
[0] == '<' && stdscan_bufptr
[1] == '=') {
853 return tv
->t_type
= TOKEN_LE
;
854 } else if (stdscan_bufptr
[0] == '>' && stdscan_bufptr
[1] == '=') {
856 return tv
->t_type
= TOKEN_GE
;
857 } else if (stdscan_bufptr
[0] == '&' && stdscan_bufptr
[1] == '&') {
859 return tv
->t_type
= TOKEN_DBL_AND
;
860 } else if (stdscan_bufptr
[0] == '^' && stdscan_bufptr
[1] == '^') {
862 return tv
->t_type
= TOKEN_DBL_XOR
;
863 } else if (stdscan_bufptr
[0] == '|' && stdscan_bufptr
[1] == '|') {
865 return tv
->t_type
= TOKEN_DBL_OR
;
866 } else /* just an ordinary char */
867 return tv
->t_type
= (unsigned char) (*stdscan_bufptr
++);
871 * Return TRUE if the argument is a simple scalar. (Or a far-
872 * absolute, which counts.)
874 int is_simple (expr
*vect
)
876 while (vect
->type
&& !vect
->value
)
880 if (vect
->type
!= EXPR_SIMPLE
)
884 } while (vect
->type
&& !vect
->value
);
885 if (vect
->type
&& vect
->type
< EXPR_SEGBASE
+SEG_ABS
) return 0;
890 * Return TRUE if the argument is a simple scalar, _NOT_ a far-
893 int is_really_simple (expr
*vect
)
895 while (vect
->type
&& !vect
->value
)
899 if (vect
->type
!= EXPR_SIMPLE
)
903 } while (vect
->type
&& !vect
->value
);
904 if (vect
->type
) return 0;
909 * Return TRUE if the argument is relocatable (i.e. a simple
910 * scalar, plus at most one segment-base, plus possibly a WRT).
912 int is_reloc (expr
*vect
)
914 while (vect
->type
&& !vect
->value
) /* skip initial value-0 terms */
916 if (!vect
->type
) /* trivially return TRUE if nothing */
917 return 1; /* is present apart from value-0s */
918 if (vect
->type
< EXPR_SIMPLE
) /* FALSE if a register is present */
920 if (vect
->type
== EXPR_SIMPLE
) { /* skip over a pure number term... */
923 } while (vect
->type
&& !vect
->value
);
924 if (!vect
->type
) /* ...returning TRUE if that's all */
927 if (vect
->type
== EXPR_WRT
) { /* skip over a WRT term... */
930 } while (vect
->type
&& !vect
->value
);
931 if (!vect
->type
) /* ...returning TRUE if that's all */
934 if (vect
->value
!= 0 && vect
->value
!= 1)
935 return 0; /* segment base multiplier non-unity */
936 do { /* skip over _one_ seg-base term... */
938 } while (vect
->type
&& !vect
->value
);
939 if (!vect
->type
) /* ...returning TRUE if that's all */
941 return 0; /* And return FALSE if there's more */
945 * Return TRUE if the argument contains an `unknown' part.
947 int is_unknown(expr
*vect
)
949 while (vect
->type
&& vect
->type
< EXPR_UNKNOWN
)
951 return (vect
->type
== EXPR_UNKNOWN
);
955 * Return TRUE if the argument contains nothing but an `unknown'
958 int is_just_unknown(expr
*vect
)
960 while (vect
->type
&& !vect
->value
)
962 return (vect
->type
== EXPR_UNKNOWN
);
966 * Return the scalar part of a relocatable vector. (Including
967 * simple scalar vectors - those qualify as relocatable.)
969 long reloc_value (expr
*vect
)
971 while (vect
->type
&& !vect
->value
)
973 if (!vect
->type
) return 0;
974 if (vect
->type
== EXPR_SIMPLE
)
981 * Return the segment number of a relocatable vector, or NO_SEG for
984 long reloc_seg (expr
*vect
)
986 while (vect
->type
&& (vect
->type
== EXPR_WRT
|| !vect
->value
))
988 if (vect
->type
== EXPR_SIMPLE
) {
991 } while (vect
->type
&& (vect
->type
== EXPR_WRT
|| !vect
->value
));
996 return vect
->type
- EXPR_SEGBASE
;
1000 * Return the WRT segment number of a relocatable vector, or NO_SEG
1001 * if no WRT part is present.
1003 long reloc_wrt (expr
*vect
)
1005 while (vect
->type
&& vect
->type
< EXPR_WRT
)
1007 if (vect
->type
== EXPR_WRT
) {
1016 int bsi (char *string
, const char **array
, int size
)
1018 int i
= -1, j
= size
; /* always, i < index < j */
1021 int l
= strcmp(string
, array
[k
]);
1022 if (l
<0) /* it's in the first half */
1024 else if (l
>0) /* it's in the second half */
1026 else /* we've got it :) */
1029 return -1; /* we haven't got it :( */
1032 static char *file_name
= NULL
;
1033 static long line_number
= 0;
1035 char *src_set_fname(char *newname
)
1037 char *oldname
= file_name
;
1038 file_name
= newname
;
1042 long src_set_linnum(long newline
)
1044 long oldline
= line_number
;
1045 line_number
= newline
;
1049 long src_get_linnum(void)
1054 int src_get(long *xline
, char **xname
)
1056 if (!file_name
|| !*xname
|| strcmp(*xname
, file_name
))
1059 *xname
= file_name
? nasm_strdup(file_name
) : NULL
;
1060 *xline
= line_number
;
1063 if (*xline
!= line_number
)
1065 long tmp
= line_number
- *xline
;
1066 *xline
= line_number
;
1072 void nasm_quote(char **str
)
1074 int ln
=strlen(*str
);
1077 if (ln
>1 && (*str
)[ln
-1]==q
&& (q
=='"' || q
=='\''))
1082 p
= nasm_malloc(ln
+3);
1090 char *nasm_strcat(char *one
, char *two
)
1094 rslt
= nasm_malloc(l1
+strlen(two
)+1);
1096 strcpy(rslt
+l1
, two
);
1100 void null_debug_init(struct ofmt
*of
, void *id
, FILE *fp
, efunc error
) {}
1101 void null_debug_linenum(const char *filename
, long linenumber
, long segto
) {}
1102 void null_debug_deflabel(char *name
, long segment
, long offset
, int is_global
, char *special
) {}
1103 void null_debug_routine(const char *directive
, const char *params
) {}
1104 void null_debug_typevalue(long type
) {}
1105 void null_debug_output(int type
, void *param
) {}
1106 void null_debug_cleanup(void){}
1108 struct dfmt null_debug_form
= {
1109 "Null debug format",
1113 null_debug_deflabel
,
1115 null_debug_typevalue
,
1120 struct dfmt
*null_debug_arr
[2] = { &null_debug_form
, NULL
};