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
44 #include "nasm.h" /* For globalbits */
46 #define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
48 static int radix_letter(char c
)
53 return 2; /* Binary */
59 return 16; /* Hexadecimal */
62 return 10; /* Decimal */
64 return 0; /* Not a known radix letter */
68 int64_t readnum(const char *str
, bool *error
)
70 const char *r
= str
, *q
;
71 int32_t pradix
, sradix
, radix
;
73 uint64_t result
, checklimit
;
80 while (nasm_isspace(*r
))
81 r
++; /* find start of number */
84 * If the number came from make_tok_num (as a result of an %assign), it
85 * might have a '-' built into it (rather than in a preceeding token).
94 while (lib_isnumchar(*q
))
95 q
++; /* find end of number */
105 * Handle radix formats:
107 * 0<radix-letter><string>
108 * $<string> (hexadecimal)
109 * <string><radix-letter>
114 if (len
> 2 && *r
== '0' && (pradix
= radix_letter(r
[1])) != 0)
116 else if (len
> 1 && *r
== '$')
117 pradix
= 16, plen
= 1;
119 if (len
> 1 && (sradix
= radix_letter(q
[-1])) != 0)
122 if (pradix
> sradix
) {
125 } else if (sradix
> pradix
) {
129 /* Either decimal, or invalid -- if invalid, we'll trip up
135 * `checklimit' must be 2**64 / radix. We can't do that in
136 * 64-bit arithmetic, which we're (probably) using, so we
137 * cheat: since we know that all radices we use are even, we
138 * can divide 2**63 by radix/2 instead.
140 checklimit
= UINT64_C(0x8000000000000000) / (radix
>> 1);
143 * Calculate the highest allowable value for the last digit of a
144 * 64-bit constant... in radix 10, it is 6, otherwise it is 0
146 last
= (radix
== 10 ? 6 : 0);
149 while (*r
&& r
< q
) {
151 if (*r
< '0' || (*r
> '9' && *r
< 'A')
152 || (digit
= numvalue(*r
)) >= radix
) {
156 if (result
> checklimit
||
157 (result
== checklimit
&& digit
>= last
)) {
161 result
= radix
* result
+ digit
;
168 *!number-overflow [on] numeric constant does not fit
169 *! covers warnings about numeric constants which
170 *! don't fit in 64 bits.
172 nasm_warn(WARN_NUMBER_OVERFLOW
,
173 "numeric constant %s does not fit in 64 bits",
177 return result
* sign
;