config: add configuration file for unknown compilers
[nasm.git] / nasmlib / readnum.c
blob7253c5b0c6896a73729874e8a0e611c9bfea913b
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2016 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 <ctype.h>
42 #include "nasmlib.h"
43 #include "nasm.h" /* For globalbits */
45 #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
47 static int radix_letter(char c)
49 switch (c) {
50 case 'b': case 'B':
51 case 'y': case 'Y':
52 return 2; /* Binary */
53 case 'o': case 'O':
54 case 'q': case 'Q':
55 return 8; /* Octal */
56 case 'h': case 'H':
57 case 'x': case 'X':
58 return 16; /* Hexadecimal */
59 case 'd': case 'D':
60 case 't': case 'T':
61 return 10; /* Decimal */
62 default:
63 return 0; /* Not a known radix letter */
67 int64_t readnum(char *str, bool *error)
69 char *r = str, *q;
70 int32_t pradix, sradix, radix;
71 int plen, slen, len;
72 uint64_t result, checklimit;
73 int digit, last;
74 bool warn = false;
75 int sign = 1;
77 *error = false;
79 while (nasm_isspace(*r))
80 r++; /* find start of number */
83 * If the number came from make_tok_num (as a result of an %assign), it
84 * might have a '-' built into it (rather than in a preceeding token).
86 if (*r == '-') {
87 r++;
88 sign = -1;
91 q = r;
93 while (lib_isnumchar(*q))
94 q++; /* find end of number */
96 len = q-r;
97 if (!len) {
98 /* Not numeric */
99 *error = true;
100 return 0;
104 * Handle radix formats:
106 * 0<radix-letter><string>
107 * $<string> (hexadecimal)
108 * <string><radix-letter>
110 pradix = sradix = 0;
111 plen = slen = 0;
113 if (len > 2 && *r == '0' && (pradix = radix_letter(r[1])) != 0)
114 plen = 2;
115 else if (len > 1 && *r == '$')
116 pradix = 16, plen = 1;
118 if (len > 1 && (sradix = radix_letter(q[-1])) != 0)
119 slen = 1;
121 if (pradix > sradix) {
122 radix = pradix;
123 r += plen;
124 } else if (sradix > pradix) {
125 radix = sradix;
126 q -= slen;
127 } else {
128 /* Either decimal, or invalid -- if invalid, we'll trip up
129 further down. */
130 radix = 10;
134 * `checklimit' must be 2**64 / radix. We can't do that in
135 * 64-bit arithmetic, which we're (probably) using, so we
136 * cheat: since we know that all radices we use are even, we
137 * can divide 2**63 by radix/2 instead.
139 checklimit = UINT64_C(0x8000000000000000) / (radix >> 1);
142 * Calculate the highest allowable value for the last digit of a
143 * 64-bit constant... in radix 10, it is 6, otherwise it is 0
145 last = (radix == 10 ? 6 : 0);
147 result = 0;
148 while (*r && r < q) {
149 if (*r != '_') {
150 if (*r < '0' || (*r > '9' && *r < 'A')
151 || (digit = numvalue(*r)) >= radix) {
152 *error = true;
153 return 0;
155 if (result > checklimit ||
156 (result == checklimit && digit >= last)) {
157 warn = true;
160 result = radix * result + digit;
162 r++;
165 if (warn)
166 nasm_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
167 "numeric constant %s does not fit in 64 bits",
168 str);
170 return result * sign;