preproc: Use nasm_zalloc in new_Block
[nasm.git] / nasmlib.c
blob4588ff3848562b33a1e2bc50046af83749a7b8fe
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2013 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 <inttypes.h>
46 #include "nasm.h"
47 #include "nasmlib.h"
48 #include "insns.h"
50 int globalbits = 0; /* defined in nasm.h, works better here for ASM+DISASM */
51 static vefunc nasm_verror; /* Global error handling function */
53 /* Uninitialized -> all zero by C spec */
54 const uint8_t zero_buffer[ZERO_BUF_SIZE];
57 * Prepare a table of tolower() results. This avoids function calls
58 * on some platforms.
61 unsigned char nasm_tolower_tab[256];
63 void tolower_init(void)
65 int i;
67 for (i = 0; i < 256; i++)
68 nasm_tolower_tab[i] = tolower(i);
71 void nasm_set_verror(vefunc ve)
73 nasm_verror = ve;
76 void nasm_error(int severity, const char *fmt, ...)
78 va_list ap;
80 va_start(ap, fmt);
81 nasm_verror(severity, fmt, ap);
82 va_end(ap);
85 void *nasm_malloc(size_t size)
87 void *p = malloc(size);
88 if (!p)
89 nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
90 return p;
93 void *nasm_zalloc(size_t size)
95 void *p = calloc(size, 1);
96 if (!p)
97 nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
98 return p;
101 void *nasm_realloc(void *q, size_t size)
103 void *p = q ? realloc(q, size) : malloc(size);
104 if (!p)
105 nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
106 return p;
109 void nasm_free(void *q)
111 if (q)
112 free(q);
115 char *nasm_strdup(const char *s)
117 char *p;
118 int size = strlen(s) + 1;
120 p = malloc(size);
121 if (!p)
122 nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
123 strcpy(p, s);
124 return p;
127 char *nasm_strndup(const char *s, size_t len)
129 char *p;
130 int size = len + 1;
132 p = malloc(size);
133 if (!p)
134 nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory");
135 strncpy(p, s, len);
136 p[len] = '\0';
137 return p;
140 no_return nasm_assert_failed(const char *file, int line, const char *msg)
142 nasm_error(ERR_FATAL, "assertion %s failed at %s:%d", msg, file, line);
143 exit(1);
146 #ifndef nasm_stricmp
147 int nasm_stricmp(const char *s1, const char *s2)
149 unsigned char c1, c2;
150 int d;
152 while (1) {
153 c1 = nasm_tolower(*s1++);
154 c2 = nasm_tolower(*s2++);
155 d = c1-c2;
157 if (d)
158 return d;
159 if (!c1)
160 break;
162 return 0;
164 #endif
166 #ifndef nasm_strnicmp
167 int nasm_strnicmp(const char *s1, const char *s2, size_t n)
169 unsigned char c1, c2;
170 int d;
172 while (n--) {
173 c1 = nasm_tolower(*s1++);
174 c2 = nasm_tolower(*s2++);
175 d = c1-c2;
177 if (d)
178 return d;
179 if (!c1)
180 break;
182 return 0;
184 #endif
186 int nasm_memicmp(const char *s1, const char *s2, size_t n)
188 unsigned char c1, c2;
189 int d;
191 while (n--) {
192 c1 = nasm_tolower(*s1++);
193 c2 = nasm_tolower(*s2++);
194 d = c1-c2;
195 if (d)
196 return d;
198 return 0;
201 #ifndef nasm_strsep
202 char *nasm_strsep(char **stringp, const char *delim)
204 char *s = *stringp;
205 char *e;
207 if (!s)
208 return NULL;
210 e = strpbrk(s, delim);
211 if (e)
212 *e++ = '\0';
214 *stringp = e;
215 return s;
217 #endif
220 #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
222 static int radix_letter(char c)
224 switch (c) {
225 case 'b': case 'B':
226 case 'y': case 'Y':
227 return 2; /* Binary */
228 case 'o': case 'O':
229 case 'q': case 'Q':
230 return 8; /* Octal */
231 case 'h': case 'H':
232 case 'x': case 'X':
233 return 16; /* Hexadecimal */
234 case 'd': case 'D':
235 case 't': case 'T':
236 return 10; /* Decimal */
237 default:
238 return 0; /* Not a known radix letter */
242 int64_t readnum(char *str, bool *error)
244 char *r = str, *q;
245 int32_t pradix, sradix, radix;
246 int plen, slen, len;
247 uint64_t result, checklimit;
248 int digit, last;
249 bool warn = false;
250 int sign = 1;
252 *error = false;
254 while (nasm_isspace(*r))
255 r++; /* find start of number */
258 * If the number came from make_tok_num (as a result of an %assign), it
259 * might have a '-' built into it (rather than in a preceeding token).
261 if (*r == '-') {
262 r++;
263 sign = -1;
266 q = r;
268 while (lib_isnumchar(*q))
269 q++; /* find end of number */
271 len = q-r;
272 if (!len) {
273 /* Not numeric */
274 *error = true;
275 return 0;
279 * Handle radix formats:
281 * 0<radix-letter><string>
282 * $<string> (hexadecimal)
283 * <string><radix-letter>
285 pradix = sradix = 0;
286 plen = slen = 0;
288 if (len > 2 && *r == '0' && (pradix = radix_letter(r[1])) != 0)
289 plen = 2;
290 else if (len > 1 && *r == '$')
291 pradix = 16, plen = 1;
293 if (len > 1 && (sradix = radix_letter(q[-1])) != 0)
294 slen = 1;
296 if (pradix > sradix) {
297 radix = pradix;
298 r += plen;
299 } else if (sradix > pradix) {
300 radix = sradix;
301 q -= slen;
302 } else {
303 /* Either decimal, or invalid -- if invalid, we'll trip up
304 further down. */
305 radix = 10;
309 * `checklimit' must be 2**64 / radix. We can't do that in
310 * 64-bit arithmetic, which we're (probably) using, so we
311 * cheat: since we know that all radices we use are even, we
312 * can divide 2**63 by radix/2 instead.
314 checklimit = UINT64_C(0x8000000000000000) / (radix >> 1);
317 * Calculate the highest allowable value for the last digit of a
318 * 64-bit constant... in radix 10, it is 6, otherwise it is 0
320 last = (radix == 10 ? 6 : 0);
322 result = 0;
323 while (*r && r < q) {
324 if (*r != '_') {
325 if (*r < '0' || (*r > '9' && *r < 'A')
326 || (digit = numvalue(*r)) >= radix) {
327 *error = true;
328 return 0;
330 if (result > checklimit ||
331 (result == checklimit && digit >= last)) {
332 warn = true;
335 result = radix * result + digit;
337 r++;
340 if (warn)
341 nasm_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
342 "numeric constant %s does not fit in 64 bits",
343 str);
345 return result * sign;
348 int64_t readstrnum(char *str, int length, bool *warn)
350 int64_t charconst = 0;
351 int i;
353 *warn = false;
355 str += length;
356 if (globalbits == 64) {
357 for (i = 0; i < length; i++) {
358 if (charconst & UINT64_C(0xFF00000000000000))
359 *warn = true;
360 charconst = (charconst << 8) + (uint8_t)*--str;
362 } else {
363 for (i = 0; i < length; i++) {
364 if (charconst & 0xFF000000UL)
365 *warn = true;
366 charconst = (charconst << 8) + (uint8_t)*--str;
369 return charconst;
372 static int32_t next_seg;
374 void seg_init(void)
376 next_seg = 0;
379 int32_t seg_alloc(void)
381 return (next_seg += 2) - 2;
384 #ifdef WORDS_LITTLEENDIAN
386 void fwriteint16_t(uint16_t data, FILE * fp)
388 fwrite(&data, 1, 2, fp);
391 void fwriteint32_t(uint32_t data, FILE * fp)
393 fwrite(&data, 1, 4, fp);
396 void fwriteint64_t(uint64_t data, FILE * fp)
398 fwrite(&data, 1, 8, fp);
401 void fwriteaddr(uint64_t data, int size, FILE * fp)
403 fwrite(&data, 1, size, fp);
406 #else /* not WORDS_LITTLEENDIAN */
408 void fwriteint16_t(uint16_t data, FILE * fp)
410 char buffer[2], *p = buffer;
411 WRITESHORT(p, data);
412 fwrite(buffer, 1, 2, fp);
415 void fwriteint32_t(uint32_t data, FILE * fp)
417 char buffer[4], *p = buffer;
418 WRITELONG(p, data);
419 fwrite(buffer, 1, 4, fp);
422 void fwriteint64_t(uint64_t data, FILE * fp)
424 char buffer[8], *p = buffer;
425 WRITEDLONG(p, data);
426 fwrite(buffer, 1, 8, fp);
429 void fwriteaddr(uint64_t data, int size, FILE * fp)
431 char buffer[8], *p = buffer;
432 WRITEADDR(p, data, size);
433 fwrite(buffer, 1, size, fp);
436 #endif
438 size_t fwritezero(size_t bytes, FILE *fp)
440 size_t count = 0;
441 size_t blksize;
442 size_t rv;
444 while (bytes) {
445 blksize = (bytes < ZERO_BUF_SIZE) ? bytes : ZERO_BUF_SIZE;
447 rv = fwrite(zero_buffer, 1, blksize, fp);
448 if (!rv)
449 break;
451 count += rv;
452 bytes -= rv;
455 return count;
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;