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 fwriteint64_t(int64_t data, FILE * fp)
311 fputc((int)(data & 255), fp);
312 fputc((int)((data >> 8) & 255), fp);
313 fputc((int)((data >> 16) & 255), fp);
314 fputc((int)((data >> 24) & 255), fp);
315 fputc((int)((data >> 32) & 255), fp);
316 fputc((int)((data >> 40) & 255), fp);
317 fputc((int)((data >> 48) & 255), fp);
318 fputc((int)((data >> 56) & 255), fp);
321 void standard_extension(char *inname, char *outname, char *extension,
326 if (*outname) /* file name already exists, */
327 return; /* so do nothing */
331 *p++ = *q++; /* copy, and find end of string */
332 *p = '\0'; /* terminate it */
333 while (p > outname && *--p != '.') ; /* find final period (or whatever) */
336 p++; /* go back to end if none found */
337 if (!strcmp(p, extension)) { /* is the extension already there? */
339 error(ERR_WARNING | ERR_NOFILE,
340 "file name already ends in `%s': "
341 "output will be in `nasm.out'", extension);
343 error(ERR_WARNING | ERR_NOFILE,
344 "file name already has no extension: "
345 "output will be in `nasm.out'");
346 strcpy(outname, "nasm.out");
348 strcpy(p, extension);
351 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
352 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
354 #define LAYERSIZ(r) ( (r)->layers==0 ? RAA_BLKSIZE : RAA_LAYERSIZE )
356 static struct RAA *real_raa_init(int layers)
362 r = nasm_malloc(LEAFSIZ);
364 memset(r->u.l.data, 0, sizeof(r->u.l.data));
367 r = nasm_malloc(BRANCHSIZ);
369 for (i = 0; i < RAA_LAYERSIZE; i++)
370 r->u.b.data[i] = NULL;
371 r->stepsize = RAA_BLKSIZE;
373 r->stepsize *= RAA_LAYERSIZE;
378 struct RAA *raa_init(void)
380 return real_raa_init(0);
383 void raa_free(struct RAA *r)
389 for (p = r->u.b.data; p - r->u.b.data < RAA_LAYERSIZE; p++)
395 int32_t raa_read(struct RAA *r, int32_t posn)
397 if (posn >= r->stepsize * LAYERSIZ(r))
398 return 0; /* Return 0 for undefined entries */
399 while (r->layers > 0) {
401 l = ldiv(posn, r->stepsize);
402 r = r->u.b.data[l.quot];
405 return 0; /* Return 0 for undefined entries */
407 return r->u.l.data[posn];
410 struct RAA *raa_write(struct RAA *r, int32_t posn, int32_t value)
415 nasm_malloc_error(ERR_PANIC, "negative position in raa_write");
417 while (r->stepsize * LAYERSIZ(r) <= posn) {
424 s = nasm_malloc(BRANCHSIZ);
425 for (i = 0; i < RAA_LAYERSIZE; i++)
426 s->u.b.data[i] = NULL;
427 s->layers = r->layers + 1;
428 s->stepsize = LAYERSIZ(r) * r->stepsize;
435 while (r->layers > 0) {
438 l = ldiv(posn, r->stepsize);
439 s = &r->u.b.data[l.quot];
441 *s = real_raa_init(r->layers - 1);
446 r->u.l.data[posn] = value;
451 #define SAA_MAXLEN 8192
453 struct SAA *saa_init(int32_t elem_len)
457 if (elem_len > SAA_MAXLEN)
458 nasm_malloc_error(ERR_PANIC | ERR_NOFILE,
459 "SAA with huge elements");
461 s = nasm_malloc(sizeof(struct SAA));
462 s->posn = s->start = 0L;
463 s->elem_len = elem_len;
464 s->length = SAA_MAXLEN - (SAA_MAXLEN % elem_len);
465 s->data = nasm_malloc(s->length);
472 void saa_free(struct SAA *s)
484 void *saa_wstruct(struct SAA *s)
488 if (s->end->length - s->end->posn < s->elem_len) {
489 s->end->next = nasm_malloc(sizeof(struct SAA));
490 s->end->next->start = s->end->start + s->end->posn;
491 s->end = s->end->next;
492 s->end->length = s->length;
495 s->end->data = nasm_malloc(s->length);
498 p = s->end->data + s->end->posn;
499 s->end->posn += s->elem_len;
503 void saa_wbytes(struct SAA *s, const void *data, int32_t len)
505 const char *d = data;
508 int32_t l = s->end->length - s->end->posn;
513 memcpy(s->end->data + s->end->posn, d, l);
516 memset(s->end->data + s->end->posn, 0, l);
521 s->end->next = nasm_malloc(sizeof(struct SAA));
522 s->end->next->start = s->end->start + s->end->posn;
523 s->end = s->end->next;
524 s->end->length = s->length;
527 s->end->data = nasm_malloc(s->length);
532 void saa_rewind(struct SAA *s)
538 void *saa_rstruct(struct SAA *s)
545 if (s->rptr->posn - s->rpos < s->elem_len) {
546 s->rptr = s->rptr->next;
548 return NULL; /* end of array */
552 p = s->rptr->data + s->rpos;
553 s->rpos += s->elem_len;
557 void *saa_rbytes(struct SAA *s, int32_t *len)
564 p = s->rptr->data + s->rpos;
565 *len = s->rptr->posn - s->rpos;
566 s->rptr = s->rptr->next;
571 void saa_rnbytes(struct SAA *s, void *data, int32_t len)
581 l = s->rptr->posn - s->rpos;
585 memcpy(d, s->rptr->data + s->rpos, l);
591 s->rptr = s->rptr->next;
597 void saa_fread(struct SAA *s, int32_t posn, void *data, int32_t len)
603 if (!s->rptr || posn < s->rptr->start)
606 while (posn >= p->start + p->posn) {
609 return; /* what else can we do?! */
612 pos = posn - p->start;
614 int64_t l = p->posn - pos;
617 memcpy(cdata, p->data + pos, l);
628 void saa_fwrite(struct SAA *s, int32_t posn, void *data, int32_t len)
634 if (!s->rptr || posn < s->rptr->start)
637 while (posn >= p->start + p->posn) {
640 return; /* what else can we do?! */
643 pos = posn - p->start;
645 int64_t l = p->posn - pos;
648 memcpy(p->data + pos, cdata, l);
659 void saa_fpwrite(struct SAA *s, FILE * fp)
665 // while ((data = saa_rbytes(s, &len)))
666 for (; (data = saa_rbytes(s, &len));)
667 fwrite(data, 1, len, fp);
671 * Register, instruction, condition-code and prefix keywords used
675 static const char *special_names[] = {
676 "byte", "dword", "far", "long", "near", "nosplit", "qword",
677 "short", "strict", "to", "tword", "word"
679 static const char *prefix_names[] = {
680 "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
681 "repnz", "repz", "times"
684 const char *prefix_name(int token)
686 unsigned int prefix = token-PREFIX_ENUM_START;
687 if (prefix > sizeof prefix_names / sizeof(const char *))
690 return prefix_names[prefix];
694 * Standard scanner routine used by parser.c and some output
695 * formats. It keeps a succession of temporary-storage strings in
696 * stdscan_tempstorage, which can be cleared using stdscan_reset.
698 static char **stdscan_tempstorage = NULL;
699 static int stdscan_tempsize = 0, stdscan_templen = 0;
700 #define STDSCAN_TEMP_DELTA 256
702 static void stdscan_pop(void)
704 nasm_free(stdscan_tempstorage[--stdscan_templen]);
707 void stdscan_reset(void)
709 while (stdscan_templen > 0)
714 * Unimportant cleanup is done to avoid confusing people who are trying
715 * to debug real memory leaks
717 void nasmlib_cleanup(void)
720 nasm_free(stdscan_tempstorage);
723 static char *stdscan_copy(char *p, int len)
727 text = nasm_malloc(len + 1);
728 strncpy(text, p, len);
731 if (stdscan_templen >= stdscan_tempsize) {
732 stdscan_tempsize += STDSCAN_TEMP_DELTA;
733 stdscan_tempstorage = nasm_realloc(stdscan_tempstorage,
737 stdscan_tempstorage[stdscan_templen++] = text;
742 char *stdscan_bufptr = NULL;
743 int stdscan(void *private_data, struct tokenval *tv)
745 char ourcopy[MAX_KEYWORD + 1], *r, *s;
747 (void)private_data; /* Don't warn that this parameter is unused */
749 while (isspace(*stdscan_bufptr))
751 if (!*stdscan_bufptr)
752 return tv->t_type = 0;
754 /* we have a token; either an id, a number or a char */
755 if (isidstart(*stdscan_bufptr) ||
756 (*stdscan_bufptr == '$' && isidstart(stdscan_bufptr[1]))) {
757 /* now we've got an identifier */
761 if (*stdscan_bufptr == '$') {
766 r = stdscan_bufptr++;
767 /* read the entire buffer to advance the buffer pointer but... */
768 while (isidchar(*stdscan_bufptr))
771 /* ... copy only up to IDLEN_MAX-1 characters */
772 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r < IDLEN_MAX ?
773 stdscan_bufptr - r : IDLEN_MAX - 1);
775 if (is_sym || stdscan_bufptr - r > MAX_KEYWORD)
776 return tv->t_type = TOKEN_ID; /* bypass all other checks */
778 for (s = tv->t_charptr, r = ourcopy; *s; s++)
781 /* right, so we have an identifier sitting in temp storage. now,
782 * is it actually a register or instruction name, or what? */
783 if ((tv->t_integer = bsi(ourcopy, reg_names,
784 elements(reg_names))) >= 0) {
785 tv->t_integer += EXPR_REG_START;
786 return tv->t_type = TOKEN_REG;
787 } else if ((tv->t_integer = bsi(ourcopy, insn_names,
788 elements(insn_names))) >= 0) {
789 return tv->t_type = TOKEN_INSN;
791 for (i = 0; i < elements(icn); i++)
792 if (!strncmp(ourcopy, icn[i], strlen(icn[i]))) {
793 char *p = ourcopy + strlen(icn[i]);
794 tv->t_integer = ico[i];
795 if ((tv->t_inttwo = bsi(p, conditions,
796 elements(conditions))) >= 0)
797 return tv->t_type = TOKEN_INSN;
799 if ((tv->t_integer = bsi(ourcopy, prefix_names,
800 elements(prefix_names))) >= 0) {
801 tv->t_integer += PREFIX_ENUM_START;
802 return tv->t_type = TOKEN_PREFIX;
804 if ((tv->t_integer = bsi(ourcopy, special_names,
805 elements(special_names))) >= 0)
806 return tv->t_type = TOKEN_SPECIAL;
807 if (!nasm_stricmp(ourcopy, "seg"))
808 return tv->t_type = TOKEN_SEG;
809 if (!nasm_stricmp(ourcopy, "wrt"))
810 return tv->t_type = TOKEN_WRT;
811 return tv->t_type = TOKEN_ID;
812 } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) {
814 * It's a $ sign with no following hex number; this must
815 * mean it's a Here token ($), evaluating to the current
816 * assembly location, or a Base token ($$), evaluating to
817 * the base of the current segment.
820 if (*stdscan_bufptr == '$') {
822 return tv->t_type = TOKEN_BASE;
824 return tv->t_type = TOKEN_HERE;
825 } else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */
828 r = stdscan_bufptr++;
829 while (isnumchar(*stdscan_bufptr))
832 if (*stdscan_bufptr == '.') {
834 * a floating point constant
837 while (isnumchar(*stdscan_bufptr) ||
838 ((stdscan_bufptr[-1] == 'e'
839 || stdscan_bufptr[-1] == 'E')
840 && (*stdscan_bufptr == '-' || *stdscan_bufptr == '+'))) {
843 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
844 return tv->t_type = TOKEN_FLOAT;
846 r = stdscan_copy(r, stdscan_bufptr - r);
847 tv->t_integer = readnum(r, &rn_error);
850 return tv->t_type = TOKEN_ERRNUM; /* some malformation occurred */
851 tv->t_charptr = NULL;
852 return tv->t_type = TOKEN_NUM;
853 } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"') { /* a char constant */
854 char quote = *stdscan_bufptr++, *r;
856 r = tv->t_charptr = stdscan_bufptr;
857 while (*stdscan_bufptr && *stdscan_bufptr != quote)
859 tv->t_inttwo = stdscan_bufptr - r; /* store full version */
860 if (!*stdscan_bufptr)
861 return tv->t_type = TOKEN_ERRNUM; /* unmatched quotes */
862 stdscan_bufptr++; /* skip over final quote */
863 tv->t_integer = readstrnum(r, tv->t_inttwo, &rn_warn);
864 /* FIXME: rn_warn is not checked! */
865 return tv->t_type = TOKEN_NUM;
866 } else if (*stdscan_bufptr == ';') { /* a comment has happened - stay */
867 return tv->t_type = 0;
868 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
870 return tv->t_type = TOKEN_SHR;
871 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
873 return tv->t_type = TOKEN_SHL;
874 } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
876 return tv->t_type = TOKEN_SDIV;
877 } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
879 return tv->t_type = TOKEN_SMOD;
880 } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
882 return tv->t_type = TOKEN_EQ;
883 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
885 return tv->t_type = TOKEN_NE;
886 } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
888 return tv->t_type = TOKEN_NE;
889 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
891 return tv->t_type = TOKEN_LE;
892 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
894 return tv->t_type = TOKEN_GE;
895 } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
897 return tv->t_type = TOKEN_DBL_AND;
898 } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
900 return tv->t_type = TOKEN_DBL_XOR;
901 } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
903 return tv->t_type = TOKEN_DBL_OR;
904 } else /* just an ordinary char */
905 return tv->t_type = (uint8_t)(*stdscan_bufptr++);
909 * Return TRUE if the argument is a simple scalar. (Or a far-
910 * absolute, which counts.)
912 int is_simple(expr * vect)
914 while (vect->type && !vect->value)
918 if (vect->type != EXPR_SIMPLE)
922 } while (vect->type && !vect->value);
923 if (vect->type && vect->type < EXPR_SEGBASE + SEG_ABS)
929 * Return TRUE if the argument is a simple scalar, _NOT_ a far-
932 int is_really_simple(expr * vect)
934 while (vect->type && !vect->value)
938 if (vect->type != EXPR_SIMPLE)
942 } while (vect->type && !vect->value);
949 * Return TRUE if the argument is relocatable (i.e. a simple
950 * scalar, plus at most one segment-base, plus possibly a WRT).
952 int is_reloc(expr * vect)
954 while (vect->type && !vect->value) /* skip initial value-0 terms */
956 if (!vect->type) /* trivially return TRUE if nothing */
957 return 1; /* is present apart from value-0s */
958 if (vect->type < EXPR_SIMPLE) /* FALSE if a register is present */
960 if (vect->type == EXPR_SIMPLE) { /* skip over a pure number term... */
963 } while (vect->type && !vect->value);
964 if (!vect->type) /* ...returning TRUE if that's all */
967 if (vect->type == EXPR_WRT) { /* skip over a WRT term... */
970 } while (vect->type && !vect->value);
971 if (!vect->type) /* ...returning TRUE if that's all */
974 if (vect->value != 0 && vect->value != 1)
975 return 0; /* segment base multiplier non-unity */
976 do { /* skip over _one_ seg-base term... */
978 } while (vect->type && !vect->value);
979 if (!vect->type) /* ...returning TRUE if that's all */
981 return 0; /* And return FALSE if there's more */
985 * Return TRUE if the argument contains an `unknown' part.
987 int is_unknown(expr * vect)
989 while (vect->type && vect->type < EXPR_UNKNOWN)
991 return (vect->type == EXPR_UNKNOWN);
995 * Return TRUE if the argument contains nothing but an `unknown'
998 int is_just_unknown(expr * vect)
1000 while (vect->type && !vect->value)
1002 return (vect->type == EXPR_UNKNOWN);
1006 * Return the scalar part of a relocatable vector. (Including
1007 * simple scalar vectors - those qualify as relocatable.)
1009 int64_t reloc_value(expr * vect)
1011 while (vect->type && !vect->value)
1015 if (vect->type == EXPR_SIMPLE)
1022 * Return the segment number of a relocatable vector, or NO_SEG for
1025 int32_t reloc_seg(expr * vect)
1027 while (vect->type && (vect->type == EXPR_WRT || !vect->value))
1029 if (vect->type == EXPR_SIMPLE) {
1032 } while (vect->type && (vect->type == EXPR_WRT || !vect->value));
1037 return vect->type - EXPR_SEGBASE;
1041 * Return the WRT segment number of a relocatable vector, or NO_SEG
1042 * if no WRT part is present.
1044 int32_t reloc_wrt(expr * vect)
1046 while (vect->type && vect->type < EXPR_WRT)
1048 if (vect->type == EXPR_WRT) {
1057 int bsi(char *string, const char **array, int size)
1059 int i = -1, j = size; /* always, i < index < j */
1060 while (j - i >= 2) {
1061 int k = (i + j) / 2;
1062 int l = strcmp(string, array[k]);
1063 if (l < 0) /* it's in the first half */
1065 else if (l > 0) /* it's in the second half */
1067 else /* we've got it :) */
1070 return -1; /* we haven't got it :( */
1073 static char *file_name = NULL;
1074 static int32_t line_number = 0;
1076 char *src_set_fname(char *newname)
1078 char *oldname = file_name;
1079 file_name = newname;
1083 int32_t src_set_linnum(int32_t newline)
1085 int32_t oldline = line_number;
1086 line_number = newline;
1090 int32_t src_get_linnum(void)
1095 int src_get(int32_t *xline, char **xname)
1097 if (!file_name || !*xname || strcmp(*xname, file_name)) {
1099 *xname = file_name ? nasm_strdup(file_name) : NULL;
1100 *xline = line_number;
1103 if (*xline != line_number) {
1104 int32_t tmp = line_number - *xline;
1105 *xline = line_number;
1111 void nasm_quote(char **str)
1113 int ln = strlen(*str);
1116 if (ln > 1 && (*str)[ln - 1] == q && (q == '"' || q == '\''))
1119 if (strchr(*str, q))
1121 p = nasm_malloc(ln + 3);
1122 strcpy(p + 1, *str);
1124 p[ln + 1] = p[0] = q;
1129 char *nasm_strcat(char *one, char *two)
1132 int l1 = strlen(one);
1133 rslt = nasm_malloc(l1 + strlen(two) + 1);
1135 strcpy(rslt + l1, two);
1139 void null_debug_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1146 void null_debug_linenum(const char *filename, int32_t linenumber, int32_t segto)
1152 void null_debug_deflabel(char *name, int32_t segment, int32_t offset,
1153 int is_global, char *special)
1161 void null_debug_routine(const char *directive, const char *params)
1166 void null_debug_typevalue(int32_t type)
1170 void null_debug_output(int type, void *param)
1175 void null_debug_cleanup(void)
1179 struct dfmt null_debug_form = {
1180 "Null debug format",
1184 null_debug_deflabel,
1186 null_debug_typevalue,
1191 struct dfmt *null_debug_arr[2] = { &null_debug_form, NULL };