Change rights 644 to 755
[nasm/github.git] / nasmlib.c
blobecd6d8dfb8a4ac45adbe0a26b772a9b8da90e95c
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2014 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * nasmlib.c library routines for the Netwide Assembler
38 #include "compiler.h"
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <errno.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "insns.h"
51 int globalbits = 0; /* defined in nasm.h, works better here for ASM+DISASM */
52 static vefunc nasm_verror; /* Global error handling function */
54 /* Uninitialized -> all zero by C spec */
55 const uint8_t zero_buffer[ZERO_BUF_SIZE];
58 * Prepare a table of tolower() results. This avoids function calls
59 * on some platforms.
62 unsigned char nasm_tolower_tab[256];
64 void tolower_init(void)
66 int i;
68 for (i = 0; i < 256; i++)
69 nasm_tolower_tab[i] = tolower(i);
72 void nasm_set_verror(vefunc ve)
74 nasm_verror = ve;
77 void nasm_error(int severity, const char *fmt, ...)
79 va_list ap;
81 va_start(ap, fmt);
82 nasm_verror(severity, fmt, ap);
83 va_end(ap);
86 void *nasm_malloc(size_t size)
88 void *p = malloc(size);
89 if (!p)
90 nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
91 return p;
94 void *nasm_zalloc(size_t size)
96 void *p = calloc(size, 1);
97 if (!p)
98 nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
99 return p;
102 void *nasm_realloc(void *q, size_t size)
104 void *p = q ? realloc(q, size) : malloc(size);
105 if (!p)
106 nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
107 return p;
110 void nasm_free(void *q)
112 if (q)
113 free(q);
116 char *nasm_strdup(const char *s)
118 char *p;
119 int size = strlen(s) + 1;
121 p = malloc(size);
122 if (!p)
123 nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
124 strcpy(p, s);
125 return p;
128 char *nasm_strndup(const char *s, size_t len)
130 char *p;
131 int size = len + 1;
133 p = malloc(size);
134 if (!p)
135 nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
136 strncpy(p, s, len);
137 p[len] = '\0';
138 return p;
141 no_return nasm_assert_failed(const char *file, int line, const char *msg)
143 nasm_error(ERR_FATAL, "assertion %s failed at %s:%d", msg, file, line);
144 exit(1);
147 void nasm_write(const void *ptr, size_t size, FILE *f)
149 size_t n = fwrite(ptr, 1, size, f);
150 if (n != size)
151 nasm_error(ERR_FATAL, "unable to write output: %s", strerror(errno));
154 #ifndef nasm_stricmp
155 int nasm_stricmp(const char *s1, const char *s2)
157 unsigned char c1, c2;
158 int d;
160 while (1) {
161 c1 = nasm_tolower(*s1++);
162 c2 = nasm_tolower(*s2++);
163 d = c1-c2;
165 if (d)
166 return d;
167 if (!c1)
168 break;
170 return 0;
172 #endif
174 #ifndef nasm_strnicmp
175 int nasm_strnicmp(const char *s1, const char *s2, size_t n)
177 unsigned char c1, c2;
178 int d;
180 while (n--) {
181 c1 = nasm_tolower(*s1++);
182 c2 = nasm_tolower(*s2++);
183 d = c1-c2;
185 if (d)
186 return d;
187 if (!c1)
188 break;
190 return 0;
192 #endif
194 int nasm_memicmp(const char *s1, const char *s2, size_t n)
196 unsigned char c1, c2;
197 int d;
199 while (n--) {
200 c1 = nasm_tolower(*s1++);
201 c2 = nasm_tolower(*s2++);
202 d = c1-c2;
203 if (d)
204 return d;
206 return 0;
209 #ifndef nasm_strsep
210 char *nasm_strsep(char **stringp, const char *delim)
212 char *s = *stringp;
213 char *e;
215 if (!s)
216 return NULL;
218 e = strpbrk(s, delim);
219 if (e)
220 *e++ = '\0';
222 *stringp = e;
223 return s;
225 #endif
228 #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
230 static int radix_letter(char c)
232 switch (c) {
233 case 'b': case 'B':
234 case 'y': case 'Y':
235 return 2; /* Binary */
236 case 'o': case 'O':
237 case 'q': case 'Q':
238 return 8; /* Octal */
239 case 'h': case 'H':
240 case 'x': case 'X':
241 return 16; /* Hexadecimal */
242 case 'd': case 'D':
243 case 't': case 'T':
244 return 10; /* Decimal */
245 default:
246 return 0; /* Not a known radix letter */
250 int64_t readnum(char *str, bool *error)
252 char *r = str, *q;
253 int32_t pradix, sradix, radix;
254 int plen, slen, len;
255 uint64_t result, checklimit;
256 int digit, last;
257 bool warn = false;
258 int sign = 1;
260 *error = false;
262 while (nasm_isspace(*r))
263 r++; /* find start of number */
266 * If the number came from make_tok_num (as a result of an %assign), it
267 * might have a '-' built into it (rather than in a preceeding token).
269 if (*r == '-') {
270 r++;
271 sign = -1;
274 q = r;
276 while (lib_isnumchar(*q))
277 q++; /* find end of number */
279 len = q-r;
280 if (!len) {
281 /* Not numeric */
282 *error = true;
283 return 0;
287 * Handle radix formats:
289 * 0<radix-letter><string>
290 * $<string> (hexadecimal)
291 * <string><radix-letter>
293 pradix = sradix = 0;
294 plen = slen = 0;
296 if (len > 2 && *r == '0' && (pradix = radix_letter(r[1])) != 0)
297 plen = 2;
298 else if (len > 1 && *r == '$')
299 pradix = 16, plen = 1;
301 if (len > 1 && (sradix = radix_letter(q[-1])) != 0)
302 slen = 1;
304 if (pradix > sradix) {
305 radix = pradix;
306 r += plen;
307 } else if (sradix > pradix) {
308 radix = sradix;
309 q -= slen;
310 } else {
311 /* Either decimal, or invalid -- if invalid, we'll trip up
312 further down. */
313 radix = 10;
317 * `checklimit' must be 2**64 / radix. We can't do that in
318 * 64-bit arithmetic, which we're (probably) using, so we
319 * cheat: since we know that all radices we use are even, we
320 * can divide 2**63 by radix/2 instead.
322 checklimit = UINT64_C(0x8000000000000000) / (radix >> 1);
325 * Calculate the highest allowable value for the last digit of a
326 * 64-bit constant... in radix 10, it is 6, otherwise it is 0
328 last = (radix == 10 ? 6 : 0);
330 result = 0;
331 while (*r && r < q) {
332 if (*r != '_') {
333 if (*r < '0' || (*r > '9' && *r < 'A')
334 || (digit = numvalue(*r)) >= radix) {
335 *error = true;
336 return 0;
338 if (result > checklimit ||
339 (result == checklimit && digit >= last)) {
340 warn = true;
343 result = radix * result + digit;
345 r++;
348 if (warn)
349 nasm_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
350 "numeric constant %s does not fit in 64 bits",
351 str);
353 return result * sign;
356 int64_t readstrnum(char *str, int length, bool *warn)
358 int64_t charconst = 0;
359 int i;
361 *warn = false;
363 str += length;
364 if (globalbits == 64) {
365 for (i = 0; i < length; i++) {
366 if (charconst & UINT64_C(0xFF00000000000000))
367 *warn = true;
368 charconst = (charconst << 8) + (uint8_t)*--str;
370 } else {
371 for (i = 0; i < length; i++) {
372 if (charconst & 0xFF000000UL)
373 *warn = true;
374 charconst = (charconst << 8) + (uint8_t)*--str;
377 return charconst;
380 static int32_t next_seg;
382 void seg_init(void)
384 next_seg = 0;
387 int32_t seg_alloc(void)
389 return (next_seg += 2) - 2;
392 #ifdef WORDS_LITTLEENDIAN
394 void fwriteint16_t(uint16_t data, FILE * fp)
396 nasm_write(&data, 2, fp);
399 void fwriteint32_t(uint32_t data, FILE * fp)
401 nasm_write(&data, 4, fp);
404 void fwriteint64_t(uint64_t data, FILE * fp)
406 nasm_write(&data, 8, fp);
409 void fwriteaddr(uint64_t data, int size, FILE * fp)
411 nasm_write(&data, size, fp);
414 #else /* not WORDS_LITTLEENDIAN */
416 void fwriteint16_t(uint16_t data, FILE * fp)
418 char buffer[2], *p = buffer;
419 WRITESHORT(p, data);
420 nasm_write(buffer, 2, fp);
423 void fwriteint32_t(uint32_t data, FILE * fp)
425 char buffer[4], *p = buffer;
426 WRITELONG(p, data);
427 nasm_write(buffer, 4, fp);
430 void fwriteint64_t(uint64_t data, FILE * fp)
432 char buffer[8], *p = buffer;
433 WRITEDLONG(p, data);
434 nasm_write(buffer, 8, fp);
437 void fwriteaddr(uint64_t data, int size, FILE * fp)
439 char buffer[8], *p = buffer;
440 WRITEADDR(p, data, size);
441 nasm_write(buffer, size, fp);
444 #endif
446 void fwritezero(size_t bytes, FILE *fp)
448 size_t blksize;
450 while (bytes) {
451 blksize = (bytes < ZERO_BUF_SIZE) ? bytes : ZERO_BUF_SIZE;
453 nasm_write(zero_buffer, blksize, fp);
454 bytes -= blksize;
458 void standard_extension(char *inname, char *outname, char *extension)
460 char *p, *q;
462 if (*outname) /* file name already exists, */
463 return; /* so do nothing */
464 q = inname;
465 p = outname;
466 while (*q)
467 *p++ = *q++; /* copy, and find end of string */
468 *p = '\0'; /* terminate it */
469 while (p > outname && *--p != '.') ; /* find final period (or whatever) */
470 if (*p != '.')
471 while (*p)
472 p++; /* go back to end if none found */
473 if (!strcmp(p, extension)) { /* is the extension already there? */
474 if (*extension)
475 nasm_error(ERR_WARNING | ERR_NOFILE,
476 "file name already ends in `%s': "
477 "output will be in `nasm.out'", extension);
478 else
479 nasm_error(ERR_WARNING | ERR_NOFILE,
480 "file name already has no extension: "
481 "output will be in `nasm.out'");
482 strcpy(outname, "nasm.out");
483 } else
484 strcpy(p, extension);
488 * Common list of prefix names
490 static const char *prefix_names[] = {
491 "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
492 "rep", "repe", "repne", "repnz", "repz", "times", "wait",
493 "xacquire", "xrelease", "bnd"
496 const char *prefix_name(int token)
498 unsigned int prefix = token-PREFIX_ENUM_START;
499 if (prefix >= ARRAY_SIZE(prefix_names))
500 return NULL;
502 return prefix_names[prefix];
506 * Binary search.
508 int bsi(const char *string, const char **array, int size)
510 int i = -1, j = size; /* always, i < index < j */
511 while (j - i >= 2) {
512 int k = (i + j) / 2;
513 int l = strcmp(string, array[k]);
514 if (l < 0) /* it's in the first half */
515 j = k;
516 else if (l > 0) /* it's in the second half */
517 i = k;
518 else /* we've got it :) */
519 return k;
521 return -1; /* we haven't got it :( */
524 int bsii(const char *string, const char **array, int size)
526 int i = -1, j = size; /* always, i < index < j */
527 while (j - i >= 2) {
528 int k = (i + j) / 2;
529 int l = nasm_stricmp(string, array[k]);
530 if (l < 0) /* it's in the first half */
531 j = k;
532 else if (l > 0) /* it's in the second half */
533 i = k;
534 else /* we've got it :) */
535 return k;
537 return -1; /* we haven't got it :( */
540 static char *file_name = NULL;
541 static int32_t line_number = 0;
543 char *src_set_fname(char *newname)
545 char *oldname = file_name;
546 file_name = newname;
547 return oldname;
550 int32_t src_set_linnum(int32_t newline)
552 int32_t oldline = line_number;
553 line_number = newline;
554 return oldline;
557 int32_t src_get_linnum(void)
559 return line_number;
562 int src_get(int32_t *xline, char **xname)
564 if (!file_name || !*xname || strcmp(*xname, file_name)) {
565 nasm_free(*xname);
566 *xname = file_name ? nasm_strdup(file_name) : NULL;
567 *xline = line_number;
568 return -2;
570 if (*xline != line_number) {
571 int32_t tmp = line_number - *xline;
572 *xline = line_number;
573 return tmp;
575 return 0;
578 char *nasm_strcat(const char *one, const char *two)
580 char *rslt;
581 int l1 = strlen(one);
582 rslt = nasm_malloc(l1 + strlen(two) + 1);
583 strcpy(rslt, one);
584 strcpy(rslt + l1, two);
585 return rslt;
588 /* skip leading spaces */
589 char *nasm_skip_spaces(const char *p)
591 if (p)
592 while (*p && nasm_isspace(*p))
593 p++;
594 return (char *)p;
597 /* skip leading non-spaces */
598 char *nasm_skip_word(const char *p)
600 if (p)
601 while (*p && !nasm_isspace(*p))
602 p++;
603 return (char *)p;
606 /* zap leading spaces with zero */
607 char *nasm_zap_spaces_fwd(char *p)
609 if (p)
610 while (*p && nasm_isspace(*p))
611 *p++ = 0x0;
612 return p;
615 /* zap spaces with zero in reverse order */
616 char *nasm_zap_spaces_rev(char *p)
618 if (p)
619 while (*p && nasm_isspace(*p))
620 *p-- = 0x0;
621 return p;
624 /* zap leading and trailing spaces */
625 char *nasm_trim_spaces(char *p)
627 p = nasm_zap_spaces_fwd(p);
628 nasm_zap_spaces_fwd(nasm_skip_word(p));
630 return p;
634 * return the word extracted from a stream
635 * or NULL if nothing left
637 char *nasm_get_word(char *p, char **tail)
639 char *word = nasm_skip_spaces(p);
640 char *next = nasm_skip_word(word);
642 if (word && *word) {
643 if (*next)
644 *next++ = '\0';
645 } else
646 word = next = NULL;
648 /* NOTE: the tail may start with spaces */
649 *tail = next;
651 return word;
655 * Extract "opt=val" values from the stream and
656 * returns "opt"
658 * Exceptions:
659 * 1) If "=val" passed the NULL returned though
660 * you may continue handling the tail via "next"
661 * 2) If "=" passed the NULL is returned and "val"
662 * is set to NULL as well
664 char *nasm_opt_val(char *p, char **val, char **next)
666 char *q, *nxt;
668 *val = *next = NULL;
670 p = nasm_get_word(p, &nxt);
671 if (!p)
672 return NULL;
674 q = strchr(p, '=');
675 if (q) {
676 if (q == p)
677 p = NULL;
678 *q++='\0';
679 if (*q) {
680 *val = q;
681 } else {
682 q = nasm_get_word(q + 1, &nxt);
683 if (q)
684 *val = q;
686 } else {
687 q = nasm_skip_spaces(nxt);
688 if (q && *q == '=') {
689 q = nasm_get_word(q + 1, &nxt);
690 if (q)
691 *val = q;
695 *next = nxt;
696 return p;
700 * initialized data bytes length from opcode
702 int idata_bytes(int opcode)
704 switch (opcode) {
705 case I_DB:
706 return 1;
707 case I_DW:
708 return 2;
709 case I_DD:
710 return 4;
711 case I_DQ:
712 return 8;
713 case I_DT:
714 return 10;
715 case I_DO:
716 return 16;
717 case I_DY:
718 return 32;
719 case I_DZ:
720 return 64;
721 case I_none:
722 return -1;
723 default:
724 return 0;