1 /* ----------------------------------------------------------------------- *
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
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
43 #include "nasm.h" /* For globalbits */
45 #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
47 static int radix_letter(char c
)
52 return 2; /* Binary */
58 return 16; /* Hexadecimal */
61 return 10; /* Decimal */
63 return 0; /* Not a known radix letter */
67 int64_t readnum(char *str
, bool *error
)
70 int32_t pradix
, sradix
, radix
;
72 uint64_t result
, checklimit
;
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).
93 while (lib_isnumchar(*q
))
94 q
++; /* find end of number */
104 * Handle radix formats:
106 * 0<radix-letter><string>
107 * $<string> (hexadecimal)
108 * <string><radix-letter>
113 if (len
> 2 && *r
== '0' && (pradix
= radix_letter(r
[1])) != 0)
115 else if (len
> 1 && *r
== '$')
116 pradix
= 16, plen
= 1;
118 if (len
> 1 && (sradix
= radix_letter(q
[-1])) != 0)
121 if (pradix
> sradix
) {
124 } else if (sradix
> pradix
) {
128 /* Either decimal, or invalid -- if invalid, we'll trip up
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);
148 while (*r
&& r
< q
) {
150 if (*r
< '0' || (*r
> '9' && *r
< 'A')
151 || (digit
= numvalue(*r
)) >= radix
) {
155 if (result
> checklimit
||
156 (result
== checklimit
&& digit
>= last
)) {
160 result
= radix
* result
+ digit
;
166 nasm_error(ERR_WARNING
| ERR_PASS1
| ERR_WARN_NOV
,
167 "numeric constant %s does not fit in 64 bits",
170 return result
* sign
;