]>
Commit | Line | Data |
---|---|---|
b0699dad | 1 | /* Parse C expressions for cpplib. |
5d8ebbd8 | 2 | Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001, |
ca2b05ba | 3 | 2002, 2004 Free Software Foundation. |
e38992e8 | 4 | Contributed by Per Bothner, 1994. |
7f2935c7 PB |
5 | |
6 | This program is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU General Public License as published by the | |
8 | Free Software Foundation; either version 2, or (at your option) any | |
9 | later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
200031d1 KC |
18 | Foundation, 51 Franklin Street, Fifth Floor, |
19 | Boston, MA 02110-1301, USA. */ | |
7f2935c7 | 20 | |
7f2935c7 | 21 | #include "config.h" |
b04cd507 | 22 | #include "system.h" |
487a6e06 | 23 | #include "cpplib.h" |
4f4e53dd | 24 | #include "internal.h" |
7f2935c7 | 25 | |
91318908 NB |
26 | #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT) |
27 | #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2)) | |
28 | #define LOW_PART(num_part) (num_part & HALF_MASK) | |
29 | #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2)) | |
30 | ||
cf00a885 | 31 | struct op |
b0699dad | 32 | { |
68e65275 | 33 | const cpp_token *token; /* The token forming op (for diagnostics). */ |
ad28cff7 | 34 | cpp_num value; /* The value logically "right" of op. */ |
cf00a885 | 35 | enum cpp_ttype op; |
7f2935c7 | 36 | }; |
7f2935c7 | 37 | |
91318908 NB |
38 | /* Some simple utility routines on double integers. */ |
39 | #define num_zerop(num) ((num.low | num.high) == 0) | |
40 | #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high) | |
6cf87ca4 ZW |
41 | static bool num_positive (cpp_num, size_t); |
42 | static bool num_greater_eq (cpp_num, cpp_num, size_t); | |
43 | static cpp_num num_trim (cpp_num, size_t); | |
44 | static cpp_num num_part_mul (cpp_num_part, cpp_num_part); | |
45 | ||
46 | static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype); | |
47 | static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype); | |
48 | static cpp_num num_negate (cpp_num, size_t); | |
49 | static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype); | |
50 | static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num, | |
51 | enum cpp_ttype); | |
52 | static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num, | |
53 | enum cpp_ttype); | |
54 | static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num); | |
55 | static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype); | |
56 | static cpp_num num_lshift (cpp_num, size_t, size_t); | |
57 | static cpp_num num_rshift (cpp_num, size_t, size_t); | |
58 | ||
59 | static cpp_num append_digit (cpp_num, int, int, size_t); | |
60 | static cpp_num parse_defined (cpp_reader *); | |
61 | static cpp_num eval_token (cpp_reader *, const cpp_token *); | |
62 | static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype); | |
63 | static unsigned int interpret_float_suffix (const uchar *, size_t); | |
64 | static unsigned int interpret_int_suffix (const uchar *, size_t); | |
65 | static void check_promotion (cpp_reader *, const struct op *); | |
91318908 | 66 | |
cd7ab83f | 67 | /* Token type abuse to create unary plus and minus operators. */ |
c3f829c1 GDR |
68 | #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1)) |
69 | #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2)) | |
cf00a885 | 70 | |
15dad1d9 ZW |
71 | /* With -O2, gcc appears to produce nice code, moving the error |
72 | message load and subsequent jump completely out of the main path. */ | |
15dad1d9 | 73 | #define SYNTAX_ERROR(msgid) \ |
0527bc4e | 74 | do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0) |
15dad1d9 | 75 | #define SYNTAX_ERROR2(msgid, arg) \ |
0527bc4e JDA |
76 | do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \ |
77 | while(0) | |
15dad1d9 | 78 | |
cd7ab83f NB |
79 | /* Subroutine of cpp_classify_number. S points to a float suffix of |
80 | length LEN, possibly zero. Returns 0 for an invalid suffix, or a | |
81 | flag vector describing the suffix. */ | |
82 | static unsigned int | |
6cf87ca4 | 83 | interpret_float_suffix (const uchar *s, size_t len) |
cf00a885 | 84 | { |
c77cd3d1 | 85 | size_t f, l, w, q, i, d; |
ac6b1c67 | 86 | size_t r, k, u, h; |
c77cd3d1 UB |
87 | |
88 | f = l = w = q = i = d = 0; | |
ac6b1c67 | 89 | r = k = u = h = 0; |
cf00a885 | 90 | |
cd7ab83f NB |
91 | while (len--) |
92 | switch (s[len]) | |
93 | { | |
ac6b1c67 CF |
94 | case 'r': case 'R': r++; break; |
95 | case 'k': case 'K': k++; break; | |
96 | case 'u': case 'U': u++; break; | |
97 | case 'h': case 'H': h++; break; | |
30e04921 JJ |
98 | case 'f': case 'F': |
99 | if (d > 0) | |
100 | return 0; | |
101 | f++; | |
102 | break; | |
103 | case 'l': case 'L': | |
104 | if (d > 0) | |
ad6ed77e | 105 | return 0; |
30e04921 | 106 | l++; |
ac6b1c67 CF |
107 | /* If there are two Ls, they must be adjacent and the same case. */ |
108 | if (l == 2 && s[len] != s[len + 1]) | |
109 | return 0; | |
ad6ed77e | 110 | break; |
c77cd3d1 UB |
111 | case 'w': case 'W': |
112 | if (d > 0) | |
113 | return 0; | |
114 | w++; | |
115 | break; | |
116 | case 'q': case 'Q': | |
117 | if (d > 0) | |
118 | return 0; | |
119 | q++; | |
120 | break; | |
30e04921 JJ |
121 | case 'i': case 'I': |
122 | case 'j': case 'J': i++; break; | |
123 | case 'd': case 'D': d++; break; | |
cd7ab83f NB |
124 | default: |
125 | return 0; | |
126 | } | |
cf00a885 | 127 | |
ac6b1c67 CF |
128 | if (r + k > 1 || h > 1 || l > 2 || u > 1) |
129 | return 0; | |
130 | ||
131 | if (r == 1) | |
132 | { | |
133 | if (f || i || d || w || q) | |
134 | return 0; | |
135 | ||
136 | return (CPP_N_FRACT | |
137 | | (u ? CPP_N_UNSIGNED : 0) | |
138 | | (h ? CPP_N_SMALL : | |
139 | l == 2 ? CPP_N_LARGE : | |
140 | l == 1 ? CPP_N_MEDIUM : 0)); | |
141 | } | |
142 | ||
143 | if (k == 1) | |
144 | { | |
145 | if (f || i || d || w || q) | |
146 | return 0; | |
147 | ||
148 | return (CPP_N_ACCUM | |
149 | | (u ? CPP_N_UNSIGNED : 0) | |
150 | | (h ? CPP_N_SMALL : | |
151 | l == 2 ? CPP_N_LARGE : | |
152 | l == 1 ? CPP_N_MEDIUM : 0)); | |
153 | } | |
154 | ||
155 | if (f + l + w + q > 1 || i > 1 || h + u > 0) | |
cd7ab83f | 156 | return 0; |
7f2935c7 | 157 | |
ad6ed77e JG |
158 | /* Allow dd, df, dl suffixes for decimal float constants. */ |
159 | if (d && ((d + f + l != 2) || i)) | |
160 | return 0; | |
161 | ||
cd7ab83f NB |
162 | return ((i ? CPP_N_IMAGINARY : 0) |
163 | | (f ? CPP_N_SMALL : | |
c77cd3d1 UB |
164 | l ? CPP_N_LARGE : |
165 | w ? CPP_N_MD_W : | |
166 | q ? CPP_N_MD_Q : CPP_N_MEDIUM) | |
ad6ed77e | 167 | | (d ? CPP_N_DFLOAT : 0)); |
cd7ab83f NB |
168 | } |
169 | ||
170 | /* Subroutine of cpp_classify_number. S points to an integer suffix | |
171 | of length LEN, possibly zero. Returns 0 for an invalid suffix, or a | |
172 | flag vector describing the suffix. */ | |
173 | static unsigned int | |
6cf87ca4 | 174 | interpret_int_suffix (const uchar *s, size_t len) |
cd7ab83f NB |
175 | { |
176 | size_t u, l, i; | |
177 | ||
178 | u = l = i = 0; | |
179 | ||
180 | while (len--) | |
181 | switch (s[len]) | |
182 | { | |
183 | case 'u': case 'U': u++; break; | |
184 | case 'i': case 'I': | |
185 | case 'j': case 'J': i++; break; | |
186 | case 'l': case 'L': l++; | |
187 | /* If there are two Ls, they must be adjacent and the same case. */ | |
188 | if (l == 2 && s[len] != s[len + 1]) | |
189 | return 0; | |
190 | break; | |
191 | default: | |
192 | return 0; | |
193 | } | |
194 | ||
195 | if (l > 2 || u > 1 || i > 1) | |
196 | return 0; | |
197 | ||
198 | return ((i ? CPP_N_IMAGINARY : 0) | |
199 | | (u ? CPP_N_UNSIGNED : 0) | |
200 | | ((l == 0) ? CPP_N_SMALL | |
201 | : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE)); | |
202 | } | |
203 | ||
204 | /* Categorize numeric constants according to their field (integer, | |
205 | floating point, or invalid), radix (decimal, octal, hexadecimal), | |
206 | and type suffixes. */ | |
207 | unsigned int | |
6cf87ca4 | 208 | cpp_classify_number (cpp_reader *pfile, const cpp_token *token) |
cd7ab83f NB |
209 | { |
210 | const uchar *str = token->val.str.text; | |
211 | const uchar *limit; | |
212 | unsigned int max_digit, result, radix; | |
213 | enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag; | |
214 | ||
215 | /* If the lexer has done its job, length one can only be a single | |
216 | digit. Fast-path this very common case. */ | |
217 | if (token->val.str.len == 1) | |
218 | return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL; | |
219 | ||
220 | limit = str + token->val.str.len; | |
221 | float_flag = NOT_FLOAT; | |
222 | max_digit = 0; | |
223 | radix = 10; | |
224 | ||
225 | /* First, interpret the radix. */ | |
226 | if (*str == '0') | |
227 | { | |
228 | radix = 8; | |
229 | str++; | |
230 | ||
231 | /* Require at least one hex digit to classify it as hex. */ | |
7f1fc38e MM |
232 | if ((*str == 'x' || *str == 'X') |
233 | && (str[1] == '.' || ISXDIGIT (str[1]))) | |
cd7ab83f NB |
234 | { |
235 | radix = 16; | |
236 | str++; | |
237 | } | |
f7fd775f JW |
238 | else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1')) |
239 | { | |
240 | radix = 2; | |
241 | str++; | |
242 | } | |
cd7ab83f NB |
243 | } |
244 | ||
245 | /* Now scan for a well-formed integer or float. */ | |
246 | for (;;) | |
247 | { | |
248 | unsigned int c = *str++; | |
249 | ||
250 | if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16)) | |
251 | { | |
252 | c = hex_value (c); | |
253 | if (c > max_digit) | |
254 | max_digit = c; | |
255 | } | |
256 | else if (c == '.') | |
257 | { | |
258 | if (float_flag == NOT_FLOAT) | |
259 | float_flag = AFTER_POINT; | |
260 | else | |
261 | SYNTAX_ERROR ("too many decimal points in number"); | |
262 | } | |
263 | else if ((radix <= 10 && (c == 'e' || c == 'E')) | |
264 | || (radix == 16 && (c == 'p' || c == 'P'))) | |
265 | { | |
266 | float_flag = AFTER_EXPON; | |
267 | break; | |
268 | } | |
269 | else | |
270 | { | |
271 | /* Start of suffix. */ | |
272 | str--; | |
273 | break; | |
274 | } | |
275 | } | |
276 | ||
ac6b1c67 CF |
277 | /* The suffix may be for decimal fixed-point constants without exponent. */ |
278 | if (radix != 16 && float_flag == NOT_FLOAT) | |
279 | { | |
280 | result = interpret_float_suffix (str, limit - str); | |
281 | if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM)) | |
282 | { | |
283 | result |= CPP_N_FLOATING; | |
284 | /* We need to restore the radix to 10, if the radix is 8. */ | |
285 | if (radix == 8) | |
286 | radix = 10; | |
287 | ||
288 | if (CPP_PEDANTIC (pfile)) | |
289 | cpp_error (pfile, CPP_DL_PEDWARN, | |
290 | "fixed-point constants are a GCC extension"); | |
291 | goto syntax_ok; | |
292 | } | |
293 | else | |
294 | result = 0; | |
295 | } | |
296 | ||
cd7ab83f NB |
297 | if (float_flag != NOT_FLOAT && radix == 8) |
298 | radix = 10; | |
299 | ||
300 | if (max_digit >= radix) | |
f7fd775f JW |
301 | { |
302 | if (radix == 2) | |
303 | SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit); | |
304 | else | |
305 | SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit); | |
306 | } | |
cd7ab83f NB |
307 | |
308 | if (float_flag != NOT_FLOAT) | |
309 | { | |
f7fd775f JW |
310 | if (radix == 2) |
311 | { | |
312 | cpp_error (pfile, CPP_DL_ERROR, | |
313 | "invalid prefix \"0b\" for floating constant"); | |
314 | return CPP_N_INVALID; | |
315 | } | |
316 | ||
cd7ab83f | 317 | if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99)) |
0527bc4e | 318 | cpp_error (pfile, CPP_DL_PEDWARN, |
cd7ab83f NB |
319 | "use of C99 hexadecimal floating constant"); |
320 | ||
321 | if (float_flag == AFTER_EXPON) | |
322 | { | |
323 | if (*str == '+' || *str == '-') | |
324 | str++; | |
325 | ||
326 | /* Exponent is decimal, even if string is a hex float. */ | |
327 | if (!ISDIGIT (*str)) | |
328 | SYNTAX_ERROR ("exponent has no digits"); | |
329 | ||
330 | do | |
331 | str++; | |
332 | while (ISDIGIT (*str)); | |
333 | } | |
334 | else if (radix == 16) | |
335 | SYNTAX_ERROR ("hexadecimal floating constants require an exponent"); | |
336 | ||
337 | result = interpret_float_suffix (str, limit - str); | |
338 | if (result == 0) | |
339 | { | |
0527bc4e | 340 | cpp_error (pfile, CPP_DL_ERROR, |
cd7ab83f | 341 | "invalid suffix \"%.*s\" on floating constant", |
91b12472 | 342 | (int) (limit - str), str); |
cd7ab83f NB |
343 | return CPP_N_INVALID; |
344 | } | |
345 | ||
346 | /* Traditional C didn't accept any floating suffixes. */ | |
347 | if (limit != str | |
348 | && CPP_WTRADITIONAL (pfile) | |
349 | && ! cpp_sys_macro_p (pfile)) | |
0527bc4e | 350 | cpp_error (pfile, CPP_DL_WARNING, |
cd7ab83f | 351 | "traditional C rejects the \"%.*s\" suffix", |
91b12472 | 352 | (int) (limit - str), str); |
cd7ab83f | 353 | |
ad6ed77e JG |
354 | /* Radix must be 10 for decimal floats. */ |
355 | if ((result & CPP_N_DFLOAT) && radix != 10) | |
356 | { | |
357 | cpp_error (pfile, CPP_DL_ERROR, | |
358 | "invalid suffix \"%.*s\" with hexadecimal floating constant", | |
359 | (int) (limit - str), str); | |
360 | return CPP_N_INVALID; | |
361 | } | |
362 | ||
ac6b1c67 CF |
363 | if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile)) |
364 | cpp_error (pfile, CPP_DL_PEDWARN, | |
365 | "fixed-point constants are a GCC extension"); | |
366 | ||
5a6bb57e JJ |
367 | if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile)) |
368 | cpp_error (pfile, CPP_DL_PEDWARN, | |
369 | "decimal float constants are a GCC extension"); | |
370 | ||
cd7ab83f NB |
371 | result |= CPP_N_FLOATING; |
372 | } | |
373 | else | |
374 | { | |
375 | result = interpret_int_suffix (str, limit - str); | |
376 | if (result == 0) | |
377 | { | |
0527bc4e | 378 | cpp_error (pfile, CPP_DL_ERROR, |
cd7ab83f | 379 | "invalid suffix \"%.*s\" on integer constant", |
91b12472 | 380 | (int) (limit - str), str); |
cd7ab83f NB |
381 | return CPP_N_INVALID; |
382 | } | |
383 | ||
56da7207 ZW |
384 | /* Traditional C only accepted the 'L' suffix. |
385 | Suppress warning about 'LL' with -Wno-long-long. */ | |
386 | if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile)) | |
387 | { | |
388 | int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY)); | |
389 | int large = (result & CPP_N_WIDTH) == CPP_N_LARGE; | |
390 | ||
391 | if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long))) | |
0527bc4e | 392 | cpp_error (pfile, CPP_DL_WARNING, |
56da7207 ZW |
393 | "traditional C rejects the \"%.*s\" suffix", |
394 | (int) (limit - str), str); | |
395 | } | |
cd7ab83f NB |
396 | |
397 | if ((result & CPP_N_WIDTH) == CPP_N_LARGE | |
398 | && ! CPP_OPTION (pfile, c99) | |
399 | && CPP_OPTION (pfile, warn_long_long)) | |
0527bc4e JDA |
400 | cpp_error (pfile, CPP_DL_PEDWARN, |
401 | "use of C99 long long integer constant"); | |
cd7ab83f NB |
402 | |
403 | result |= CPP_N_INTEGER; | |
404 | } | |
405 | ||
ac6b1c67 | 406 | syntax_ok: |
cd7ab83f | 407 | if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile)) |
0527bc4e JDA |
408 | cpp_error (pfile, CPP_DL_PEDWARN, |
409 | "imaginary constants are a GCC extension"); | |
f7fd775f JW |
410 | if (radix == 2 && CPP_PEDANTIC (pfile)) |
411 | cpp_error (pfile, CPP_DL_PEDWARN, | |
412 | "binary constants are a GCC extension"); | |
cd7ab83f NB |
413 | |
414 | if (radix == 10) | |
415 | result |= CPP_N_DECIMAL; | |
416 | else if (radix == 16) | |
417 | result |= CPP_N_HEX; | |
f7fd775f JW |
418 | else if (radix == 2) |
419 | result |= CPP_N_BINARY; | |
cd7ab83f NB |
420 | else |
421 | result |= CPP_N_OCTAL; | |
422 | ||
423 | return result; | |
424 | ||
425 | syntax_error: | |
426 | return CPP_N_INVALID; | |
427 | } | |
428 | ||
429 | /* cpp_interpret_integer converts an integer constant into a cpp_num, | |
430 | of precision options->precision. | |
431 | ||
432 | We do not provide any interface for decimal->float conversion, | |
6cf87ca4 ZW |
433 | because the preprocessor doesn't need it and we don't want to |
434 | drag in GCC's floating point emulator. */ | |
cd7ab83f | 435 | cpp_num |
6cf87ca4 ZW |
436 | cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token, |
437 | unsigned int type) | |
cd7ab83f NB |
438 | { |
439 | const uchar *p, *end; | |
440 | cpp_num result; | |
441 | ||
442 | result.low = 0; | |
443 | result.high = 0; | |
23ff0223 NB |
444 | result.unsignedp = !!(type & CPP_N_UNSIGNED); |
445 | result.overflow = false; | |
cd7ab83f NB |
446 | |
447 | p = token->val.str.text; | |
448 | end = p + token->val.str.len; | |
449 | ||
450 | /* Common case of a single digit. */ | |
451 | if (token->val.str.len == 1) | |
452 | result.low = p[0] - '0'; | |
453 | else | |
454 | { | |
455 | cpp_num_part max; | |
456 | size_t precision = CPP_OPTION (pfile, precision); | |
457 | unsigned int base = 10, c = 0; | |
458 | bool overflow = false; | |
459 | ||
460 | if ((type & CPP_N_RADIX) == CPP_N_OCTAL) | |
461 | { | |
462 | base = 8; | |
463 | p++; | |
464 | } | |
465 | else if ((type & CPP_N_RADIX) == CPP_N_HEX) | |
466 | { | |
467 | base = 16; | |
468 | p += 2; | |
469 | } | |
f7fd775f JW |
470 | else if ((type & CPP_N_RADIX) == CPP_N_BINARY) |
471 | { | |
472 | base = 2; | |
473 | p += 2; | |
474 | } | |
cd7ab83f NB |
475 | |
476 | /* We can add a digit to numbers strictly less than this without | |
477 | needing the precision and slowness of double integers. */ | |
478 | max = ~(cpp_num_part) 0; | |
479 | if (precision < PART_PRECISION) | |
480 | max >>= PART_PRECISION - precision; | |
481 | max = (max - base + 1) / base + 1; | |
482 | ||
483 | for (; p < end; p++) | |
484 | { | |
485 | c = *p; | |
486 | ||
487 | if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c))) | |
488 | c = hex_value (c); | |
489 | else | |
490 | break; | |
491 | ||
492 | /* Strict inequality for when max is set to zero. */ | |
493 | if (result.low < max) | |
494 | result.low = result.low * base + c; | |
495 | else | |
496 | { | |
497 | result = append_digit (result, c, base, precision); | |
498 | overflow |= result.overflow; | |
499 | max = 0; | |
500 | } | |
501 | } | |
502 | ||
503 | if (overflow) | |
0527bc4e | 504 | cpp_error (pfile, CPP_DL_PEDWARN, |
cd7ab83f | 505 | "integer constant is too large for its type"); |
017acb41 NB |
506 | /* If too big to be signed, consider it unsigned. Only warn for |
507 | decimal numbers. Traditional numbers were always signed (but | |
8d9afc4e | 508 | we still honor an explicit U suffix); but we only have |
cd98faa1 | 509 | traditional semantics in directives. */ |
017acb41 | 510 | else if (!result.unsignedp |
cd98faa1 NB |
511 | && !(CPP_OPTION (pfile, traditional) |
512 | && pfile->state.in_directive) | |
017acb41 | 513 | && !num_positive (result, precision)) |
cd7ab83f | 514 | { |
cd7ab83f | 515 | if (base == 10) |
0527bc4e | 516 | cpp_error (pfile, CPP_DL_WARNING, |
cd7ab83f | 517 | "integer constant is so large that it is unsigned"); |
23ff0223 | 518 | result.unsignedp = true; |
cd7ab83f NB |
519 | } |
520 | } | |
521 | ||
522 | return result; | |
523 | } | |
cf00a885 | 524 | |
6cf87ca4 | 525 | /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */ |
91318908 | 526 | static cpp_num |
6cf87ca4 | 527 | append_digit (cpp_num num, int digit, int base, size_t precision) |
91318908 NB |
528 | { |
529 | cpp_num result; | |
f7fd775f | 530 | unsigned int shift; |
91318908 NB |
531 | bool overflow; |
532 | cpp_num_part add_high, add_low; | |
533 | ||
f7fd775f | 534 | /* Multiply by 2, 8 or 16. Catching this overflow here means we don't |
91318908 | 535 | need to worry about add_high overflowing. */ |
f7fd775f JW |
536 | switch (base) |
537 | { | |
538 | case 2: | |
539 | shift = 1; | |
540 | break; | |
541 | ||
542 | case 16: | |
543 | shift = 4; | |
544 | break; | |
545 | ||
546 | default: | |
547 | shift = 3; | |
548 | } | |
23ff0223 | 549 | overflow = !!(num.high >> (PART_PRECISION - shift)); |
91318908 NB |
550 | result.high = num.high << shift; |
551 | result.low = num.low << shift; | |
552 | result.high |= num.low >> (PART_PRECISION - shift); | |
6de9cd9a | 553 | result.unsignedp = num.unsignedp; |
91318908 NB |
554 | |
555 | if (base == 10) | |
556 | { | |
557 | add_low = num.low << 1; | |
558 | add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1)); | |
559 | } | |
560 | else | |
561 | add_high = add_low = 0; | |
562 | ||
563 | if (add_low + digit < add_low) | |
564 | add_high++; | |
565 | add_low += digit; | |
22a8a52d | 566 | |
91318908 NB |
567 | if (result.low + add_low < result.low) |
568 | add_high++; | |
569 | if (result.high + add_high < result.high) | |
570 | overflow = true; | |
571 | ||
572 | result.low += add_low; | |
573 | result.high += add_high; | |
6de9cd9a | 574 | result.overflow = overflow; |
91318908 NB |
575 | |
576 | /* The above code catches overflow of a cpp_num type. This catches | |
577 | overflow of the (possibly shorter) target precision. */ | |
578 | num.low = result.low; | |
579 | num.high = result.high; | |
580 | result = num_trim (result, precision); | |
581 | if (!num_eq (result, num)) | |
6de9cd9a | 582 | result.overflow = true; |
91318908 | 583 | |
91318908 NB |
584 | return result; |
585 | } | |
586 | ||
5d8ebbd8 | 587 | /* Handle meeting "defined" in a preprocessor expression. */ |
91318908 | 588 | static cpp_num |
6cf87ca4 | 589 | parse_defined (cpp_reader *pfile) |
ba412f14 | 590 | { |
91318908 | 591 | cpp_num result; |
93c80368 NB |
592 | int paren = 0; |
593 | cpp_hashnode *node = 0; | |
4ed5bcfb | 594 | const cpp_token *token; |
63d75005 | 595 | cpp_context *initial_context = pfile->context; |
ba412f14 | 596 | |
93c80368 NB |
597 | /* Don't expand macros. */ |
598 | pfile->state.prevent_expansion++; | |
599 | ||
4ed5bcfb NB |
600 | token = cpp_get_token (pfile); |
601 | if (token->type == CPP_OPEN_PAREN) | |
ba412f14 | 602 | { |
cf00a885 | 603 | paren = 1; |
4ed5bcfb | 604 | token = cpp_get_token (pfile); |
ba412f14 ZW |
605 | } |
606 | ||
4ed5bcfb | 607 | if (token->type == CPP_NAME) |
93c80368 | 608 | { |
4ed5bcfb NB |
609 | node = token->val.node; |
610 | if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN) | |
93c80368 | 611 | { |
0527bc4e | 612 | cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\""); |
4ed5bcfb | 613 | node = 0; |
93c80368 NB |
614 | } |
615 | } | |
616 | else | |
3c8465d0 | 617 | { |
0527bc4e | 618 | cpp_error (pfile, CPP_DL_ERROR, |
ebef4e8c | 619 | "operator \"defined\" requires an identifier"); |
4ed5bcfb | 620 | if (token->flags & NAMED_OP) |
3c8465d0 NB |
621 | { |
622 | cpp_token op; | |
623 | ||
624 | op.flags = 0; | |
4ed5bcfb | 625 | op.type = token->type; |
0527bc4e | 626 | cpp_error (pfile, CPP_DL_ERROR, |
3c8465d0 | 627 | "(\"%s\" is an alternative token for \"%s\" in C++)", |
4ed5bcfb | 628 | cpp_token_as_text (pfile, token), |
3c8465d0 NB |
629 | cpp_token_as_text (pfile, &op)); |
630 | } | |
631 | } | |
15dad1d9 | 632 | |
91318908 | 633 | if (node) |
15dad1d9 | 634 | { |
335d03ec | 635 | if (pfile->context != initial_context && CPP_PEDANTIC (pfile)) |
0527bc4e | 636 | cpp_error (pfile, CPP_DL_WARNING, |
ebef4e8c | 637 | "this use of \"defined\" may not be portable"); |
63d75005 | 638 | |
a69cbaac NB |
639 | _cpp_mark_macro_used (node); |
640 | ||
6d18adbc NB |
641 | /* A possible controlling macro of the form #if !defined (). |
642 | _cpp_parse_expr checks there was no other junk on the line. */ | |
643 | pfile->mi_ind_cmacro = node; | |
15dad1d9 | 644 | } |
93c80368 NB |
645 | |
646 | pfile->state.prevent_expansion--; | |
91318908 | 647 | |
23ff0223 | 648 | result.unsignedp = false; |
91318908 | 649 | result.high = 0; |
23ff0223 | 650 | result.overflow = false; |
91318908 NB |
651 | result.low = node && node->type == NT_MACRO; |
652 | return result; | |
15dad1d9 ZW |
653 | } |
654 | ||
60284a59 NB |
655 | /* Convert a token into a CPP_NUMBER (an interpreted preprocessing |
656 | number or character constant, or the result of the "defined" or "#" | |
cd7ab83f | 657 | operators). */ |
91318908 | 658 | static cpp_num |
6cf87ca4 | 659 | eval_token (cpp_reader *pfile, const cpp_token *token) |
7f2935c7 | 660 | { |
91318908 | 661 | cpp_num result; |
60284a59 | 662 | unsigned int temp; |
4268e8bb | 663 | int unsignedp = 0; |
041c3194 | 664 | |
6de9cd9a DN |
665 | result.unsignedp = false; |
666 | result.overflow = false; | |
667 | ||
93c80368 | 668 | switch (token->type) |
ba412f14 | 669 | { |
7f2935c7 | 670 | case CPP_NUMBER: |
cd7ab83f NB |
671 | temp = cpp_classify_number (pfile, token); |
672 | switch (temp & CPP_N_CATEGORY) | |
673 | { | |
674 | case CPP_N_FLOATING: | |
0527bc4e | 675 | cpp_error (pfile, CPP_DL_ERROR, |
cd7ab83f NB |
676 | "floating constant in preprocessor expression"); |
677 | break; | |
678 | case CPP_N_INTEGER: | |
679 | if (!(temp & CPP_N_IMAGINARY)) | |
680 | return cpp_interpret_integer (pfile, token, temp); | |
0527bc4e | 681 | cpp_error (pfile, CPP_DL_ERROR, |
cd7ab83f NB |
682 | "imaginary number in preprocessor expression"); |
683 | break; | |
684 | ||
685 | case CPP_N_INVALID: | |
686 | /* Error already issued. */ | |
687 | break; | |
688 | } | |
689 | result.high = result.low = 0; | |
690 | break; | |
7f2f1a66 | 691 | |
cf00a885 | 692 | case CPP_WCHAR: |
4268e8bb | 693 | case CPP_CHAR: |
a5a49440 | 694 | { |
91318908 NB |
695 | cppchar_t cc = cpp_interpret_charconst (pfile, token, |
696 | &temp, &unsignedp); | |
697 | ||
698 | result.high = 0; | |
699 | result.low = cc; | |
a5a49440 | 700 | /* Sign-extend the result if necessary. */ |
91318908 NB |
701 | if (!unsignedp && (cppchar_signed_t) cc < 0) |
702 | { | |
703 | if (PART_PRECISION > BITS_PER_CPPCHAR_T) | |
704 | result.low |= ~(~(cpp_num_part) 0 | |
705 | >> (PART_PRECISION - BITS_PER_CPPCHAR_T)); | |
706 | result.high = ~(cpp_num_part) 0; | |
707 | result = num_trim (result, CPP_OPTION (pfile, precision)); | |
708 | } | |
a5a49440 | 709 | } |
60284a59 | 710 | break; |
ba412f14 | 711 | |
92936ecf | 712 | case CPP_NAME: |
93c80368 | 713 | if (token->val.node == pfile->spec_nodes.n_defined) |
63d75005 | 714 | return parse_defined (pfile); |
7d4918a2 ZW |
715 | else if (CPP_OPTION (pfile, cplusplus) |
716 | && (token->val.node == pfile->spec_nodes.n_true | |
717 | || token->val.node == pfile->spec_nodes.n_false)) | |
718 | { | |
91318908 NB |
719 | result.high = 0; |
720 | result.low = (token->val.node == pfile->spec_nodes.n_true); | |
7d4918a2 ZW |
721 | } |
722 | else | |
723 | { | |
91318908 NB |
724 | result.high = 0; |
725 | result.low = 0; | |
87ed109f | 726 | if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval) |
0527bc4e | 727 | cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined", |
ebef4e8c | 728 | NODE_NAME (token->val.node)); |
7f2935c7 | 729 | } |
60284a59 | 730 | break; |
7f2935c7 | 731 | |
60284a59 | 732 | default: /* CPP_HASH */ |
91318908 NB |
733 | _cpp_test_assertion (pfile, &temp); |
734 | result.high = 0; | |
735 | result.low = temp; | |
93c80368 | 736 | } |
7c3bb1de | 737 | |
23ff0223 | 738 | result.unsignedp = !!unsignedp; |
91318908 | 739 | return result; |
7f2935c7 PB |
740 | } |
741 | \f | |
4063b943 | 742 | /* Operator precedence and flags table. |
dbac4aff NB |
743 | |
744 | After an operator is returned from the lexer, if it has priority less | |
87ed109f NB |
745 | than the operator on the top of the stack, we reduce the stack by one |
746 | operator and repeat the test. Since equal priorities do not reduce, | |
747 | this is naturally right-associative. | |
748 | ||
749 | We handle left-associative operators by decrementing the priority of | |
750 | just-lexed operators by one, but retaining the priority of operators | |
751 | already on the stack. | |
dbac4aff NB |
752 | |
753 | The remaining cases are '(' and ')'. We handle '(' by skipping the | |
754 | reduction phase completely. ')' is given lower priority than | |
755 | everything else, including '(', effectively forcing a reduction of the | |
272d0bee | 756 | parenthesized expression. If there is a matching '(', the routine |
87ed109f NB |
757 | reduce() exits immediately. If the normal exit route sees a ')', then |
758 | there cannot have been a matching '(' and an error message is output. | |
4063b943 | 759 | |
f8b954fc NB |
760 | The parser assumes all shifted operators require a left operand unless |
761 | the flag NO_L_OPERAND is set. These semantics are automatic; any | |
762 | extra semantics need to be handled with operator-specific code. */ | |
4063b943 | 763 | |
68e65275 NB |
764 | /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an |
765 | operand changes because of integer promotions. */ | |
87ed109f NB |
766 | #define NO_L_OPERAND (1 << 0) |
767 | #define LEFT_ASSOC (1 << 1) | |
68e65275 | 768 | #define CHECK_PROMOTION (1 << 2) |
eba30526 | 769 | |
cf00a885 ZW |
770 | /* Operator to priority map. Must be in the same order as the first |
771 | N entries of enum cpp_ttype. */ | |
c3f829c1 | 772 | static const struct cpp_operator |
87ed109f | 773 | { |
60284a59 | 774 | uchar prio; |
87ed109f NB |
775 | uchar flags; |
776 | } optab[] = | |
cf00a885 | 777 | { |
ad28cff7 NB |
778 | /* EQ */ {0, 0}, /* Shouldn't happen. */ |
779 | /* NOT */ {16, NO_L_OPERAND}, | |
68e65275 NB |
780 | /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION}, |
781 | /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION}, | |
782 | /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION}, | |
783 | /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION}, | |
784 | /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION}, | |
785 | /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION}, | |
786 | /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION}, | |
787 | /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION}, | |
788 | /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION}, | |
789 | /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION}, | |
ad28cff7 NB |
790 | /* RSHIFT */ {13, LEFT_ASSOC}, |
791 | /* LSHIFT */ {13, LEFT_ASSOC}, | |
792 | ||
ad28cff7 | 793 | /* COMPL */ {16, NO_L_OPERAND}, |
75aef48a NB |
794 | /* AND_AND */ {6, LEFT_ASSOC}, |
795 | /* OR_OR */ {5, LEFT_ASSOC}, | |
796 | /* QUERY */ {3, 0}, | |
68e65275 | 797 | /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION}, |
ad28cff7 | 798 | /* COMMA */ {2, LEFT_ASSOC}, |
75aef48a | 799 | /* OPEN_PAREN */ {1, NO_L_OPERAND}, |
ad28cff7 NB |
800 | /* CLOSE_PAREN */ {0, 0}, |
801 | /* EOF */ {0, 0}, | |
802 | /* EQ_EQ */ {11, LEFT_ASSOC}, | |
803 | /* NOT_EQ */ {11, LEFT_ASSOC}, | |
68e65275 NB |
804 | /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION}, |
805 | /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION}, | |
ad28cff7 NB |
806 | /* UPLUS */ {16, NO_L_OPERAND}, |
807 | /* UMINUS */ {16, NO_L_OPERAND} | |
cf00a885 ZW |
808 | }; |
809 | ||
7f2935c7 | 810 | /* Parse and evaluate a C expression, reading from PFILE. |
df383483 | 811 | Returns the truth value of the expression. |
87ed109f NB |
812 | |
813 | The implementation is an operator precedence parser, i.e. a | |
814 | bottom-up parser, using a stack for not-yet-reduced tokens. | |
815 | ||
816 | The stack base is op_stack, and the current stack pointer is 'top'. | |
817 | There is a stack element for each operator (only), and the most | |
818 | recently pushed operator is 'top->op'. An operand (value) is | |
819 | stored in the 'value' field of the stack element of the operator | |
820 | that precedes it. */ | |
821 | bool | |
6cf87ca4 | 822 | _cpp_parse_expr (cpp_reader *pfile) |
7f2935c7 | 823 | { |
87ed109f NB |
824 | struct op *top = pfile->op_stack; |
825 | unsigned int lex_count; | |
826 | bool saw_leading_not, want_value = true; | |
827 | ||
828 | pfile->state.skip_eval = 0; | |
7f2935c7 | 829 | |
93c80368 | 830 | /* Set up detection of #if ! defined(). */ |
6d18adbc | 831 | pfile->mi_ind_cmacro = 0; |
87ed109f | 832 | saw_leading_not = false; |
6d18adbc | 833 | lex_count = 0; |
93c80368 | 834 | |
87ed109f | 835 | /* Lowest priority operator prevents further reductions. */ |
cf00a885 | 836 | top->op = CPP_EOF; |
4063b943 | 837 | |
7f2935c7 PB |
838 | for (;;) |
839 | { | |
cf00a885 | 840 | struct op op; |
7f2935c7 | 841 | |
6d18adbc | 842 | lex_count++; |
68e65275 NB |
843 | op.token = cpp_get_token (pfile); |
844 | op.op = op.token->type; | |
7f2935c7 | 845 | |
7f2935c7 PB |
846 | switch (op.op) |
847 | { | |
60284a59 | 848 | /* These tokens convert into values. */ |
c60e94a7 | 849 | case CPP_NUMBER: |
60284a59 NB |
850 | case CPP_CHAR: |
851 | case CPP_WCHAR: | |
852 | case CPP_NAME: | |
853 | case CPP_HASH: | |
f8b954fc | 854 | if (!want_value) |
60284a59 | 855 | SYNTAX_ERROR2 ("missing binary operator before token \"%s\"", |
68e65275 | 856 | cpp_token_as_text (pfile, op.token)); |
f8b954fc | 857 | want_value = false; |
68e65275 | 858 | top->value = eval_token (pfile, op.token); |
9ee70313 NB |
859 | continue; |
860 | ||
6d18adbc NB |
861 | case CPP_NOT: |
862 | saw_leading_not = lex_count == 1; | |
6d18adbc | 863 | break; |
cf00a885 | 864 | case CPP_PLUS: |
f8b954fc NB |
865 | if (want_value) |
866 | op.op = CPP_UPLUS; | |
867 | break; | |
868 | case CPP_MINUS: | |
869 | if (want_value) | |
870 | op.op = CPP_UMINUS; | |
871 | break; | |
60284a59 | 872 | |
f8b954fc | 873 | default: |
60284a59 | 874 | if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ) |
cd7ab83f | 875 | SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions", |
68e65275 | 876 | cpp_token_as_text (pfile, op.token)); |
f8b954fc | 877 | break; |
7f2935c7 | 878 | } |
7f2935c7 | 879 | |
87ed109f NB |
880 | /* Check we have a value or operator as appropriate. */ |
881 | if (optab[op.op].flags & NO_L_OPERAND) | |
7f2935c7 | 882 | { |
87ed109f | 883 | if (!want_value) |
60284a59 | 884 | SYNTAX_ERROR2 ("missing binary operator before token \"%s\"", |
68e65275 | 885 | cpp_token_as_text (pfile, op.token)); |
87ed109f NB |
886 | } |
887 | else if (want_value) | |
888 | { | |
a09d4744 NB |
889 | /* We want a number (or expression) and haven't got one. |
890 | Try to emit a specific diagnostic. */ | |
891 | if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN) | |
892 | SYNTAX_ERROR ("missing expression between '(' and ')'"); | |
893 | ||
894 | if (op.op == CPP_EOF && top->op == CPP_EOF) | |
895 | SYNTAX_ERROR ("#if with no expression"); | |
896 | ||
897 | if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN) | |
898 | SYNTAX_ERROR2 ("operator '%s' has no right operand", | |
899 | cpp_token_as_text (pfile, top->token)); | |
900 | else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF) | |
901 | /* Complain about missing paren during reduction. */; | |
902 | else | |
903 | SYNTAX_ERROR2 ("operator '%s' has no left operand", | |
904 | cpp_token_as_text (pfile, op.token)); | |
7f2935c7 | 905 | } |
9ee70313 | 906 | |
87ed109f NB |
907 | top = reduce (pfile, top, op.op); |
908 | if (!top) | |
909 | goto syntax_error; | |
9ee70313 | 910 | |
60284a59 NB |
911 | if (op.op == CPP_EOF) |
912 | break; | |
913 | ||
87ed109f | 914 | switch (op.op) |
b22ef131 | 915 | { |
87ed109f NB |
916 | case CPP_CLOSE_PAREN: |
917 | continue; | |
87ed109f | 918 | case CPP_OR_OR: |
91318908 | 919 | if (!num_zerop (top->value)) |
87ed109f NB |
920 | pfile->state.skip_eval++; |
921 | break; | |
922 | case CPP_AND_AND: | |
923 | case CPP_QUERY: | |
91318908 | 924 | if (num_zerop (top->value)) |
87ed109f NB |
925 | pfile->state.skip_eval++; |
926 | break; | |
927 | case CPP_COLON: | |
60284a59 NB |
928 | if (top->op != CPP_QUERY) |
929 | SYNTAX_ERROR (" ':' without preceding '?'"); | |
91318908 | 930 | if (!num_zerop (top[-1].value)) /* Was '?' condition true? */ |
87ed109f NB |
931 | pfile->state.skip_eval++; |
932 | else | |
933 | pfile->state.skip_eval--; | |
934 | default: | |
935 | break; | |
4063b943 | 936 | } |
87ed109f | 937 | |
f8b954fc | 938 | want_value = true; |
4063b943 | 939 | |
0f41302f | 940 | /* Check for and handle stack overflow. */ |
87ed109f NB |
941 | if (++top == pfile->op_limit) |
942 | top = _cpp_expand_op_stack (pfile); | |
df383483 | 943 | |
7f2935c7 | 944 | top->op = op.op; |
68e65275 | 945 | top->token = op.token; |
7f2935c7 | 946 | } |
9ee70313 | 947 | |
6d18adbc NB |
948 | /* The controlling macro expression is only valid if we called lex 3 |
949 | times: <!> <defined expression> and <EOF>. push_conditional () | |
950 | checks that we are at top-of-file. */ | |
951 | if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3)) | |
952 | pfile->mi_ind_cmacro = 0; | |
953 | ||
87ed109f | 954 | if (top != pfile->op_stack) |
ebef4e8c | 955 | { |
0527bc4e | 956 | cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in #if"); |
4063b943 | 957 | syntax_error: |
87ed109f | 958 | return false; /* Return false on syntax error. */ |
4063b943 | 959 | } |
9ee70313 | 960 | |
91318908 | 961 | return !num_zerop (top->value); |
87ed109f NB |
962 | } |
963 | ||
964 | /* Reduce the operator / value stack if possible, in preparation for | |
965 | pushing operator OP. Returns NULL on error, otherwise the top of | |
966 | the stack. */ | |
967 | static struct op * | |
6cf87ca4 | 968 | reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op) |
87ed109f NB |
969 | { |
970 | unsigned int prio; | |
971 | ||
91318908 NB |
972 | if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2) |
973 | { | |
974 | bad_op: | |
0527bc4e | 975 | cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op); |
91318908 NB |
976 | return 0; |
977 | } | |
978 | ||
87ed109f NB |
979 | if (op == CPP_OPEN_PAREN) |
980 | return top; | |
981 | ||
982 | /* Decrement the priority of left-associative operators to force a | |
983 | reduction with operators of otherwise equal priority. */ | |
984 | prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0); | |
985 | while (prio < optab[top->op].prio) | |
986 | { | |
68e65275 NB |
987 | if (CPP_OPTION (pfile, warn_num_sign_change) |
988 | && optab[top->op].flags & CHECK_PROMOTION) | |
989 | check_promotion (pfile, top); | |
990 | ||
75aef48a NB |
991 | switch (top->op) |
992 | { | |
993 | case CPP_UPLUS: | |
994 | case CPP_UMINUS: | |
995 | case CPP_NOT: | |
996 | case CPP_COMPL: | |
997 | top[-1].value = num_unary_op (pfile, top->value, top->op); | |
998 | break; | |
87ed109f | 999 | |
75aef48a NB |
1000 | case CPP_PLUS: |
1001 | case CPP_MINUS: | |
1002 | case CPP_RSHIFT: | |
1003 | case CPP_LSHIFT: | |
75aef48a NB |
1004 | case CPP_COMMA: |
1005 | top[-1].value = num_binary_op (pfile, top[-1].value, | |
1006 | top->value, top->op); | |
1007 | break; | |
91318908 | 1008 | |
75aef48a NB |
1009 | case CPP_GREATER: |
1010 | case CPP_LESS: | |
1011 | case CPP_GREATER_EQ: | |
1012 | case CPP_LESS_EQ: | |
1013 | top[-1].value | |
1014 | = num_inequality_op (pfile, top[-1].value, top->value, top->op); | |
1015 | break; | |
91318908 | 1016 | |
75aef48a NB |
1017 | case CPP_EQ_EQ: |
1018 | case CPP_NOT_EQ: | |
1019 | top[-1].value | |
1020 | = num_equality_op (pfile, top[-1].value, top->value, top->op); | |
1021 | break; | |
87ed109f | 1022 | |
75aef48a NB |
1023 | case CPP_AND: |
1024 | case CPP_OR: | |
1025 | case CPP_XOR: | |
1026 | top[-1].value | |
1027 | = num_bitwise_op (pfile, top[-1].value, top->value, top->op); | |
1028 | break; | |
91318908 | 1029 | |
75aef48a NB |
1030 | case CPP_MULT: |
1031 | top[-1].value = num_mul (pfile, top[-1].value, top->value); | |
1032 | break; | |
ad28cff7 | 1033 | |
75aef48a NB |
1034 | case CPP_DIV: |
1035 | case CPP_MOD: | |
1036 | top[-1].value = num_div_op (pfile, top[-1].value, | |
1037 | top->value, top->op); | |
1038 | break; | |
ad28cff7 | 1039 | |
75aef48a NB |
1040 | case CPP_OR_OR: |
1041 | top--; | |
1042 | if (!num_zerop (top->value)) | |
1043 | pfile->state.skip_eval--; | |
1044 | top->value.low = (!num_zerop (top->value) | |
1045 | || !num_zerop (top[1].value)); | |
1046 | top->value.high = 0; | |
1047 | top->value.unsignedp = false; | |
1048 | top->value.overflow = false; | |
1049 | continue; | |
1050 | ||
1051 | case CPP_AND_AND: | |
1052 | top--; | |
1053 | if (num_zerop (top->value)) | |
1054 | pfile->state.skip_eval--; | |
1055 | top->value.low = (!num_zerop (top->value) | |
1056 | && !num_zerop (top[1].value)); | |
1057 | top->value.high = 0; | |
1058 | top->value.unsignedp = false; | |
1059 | top->value.overflow = false; | |
1060 | continue; | |
1061 | ||
1062 | case CPP_OPEN_PAREN: | |
1063 | if (op != CPP_CLOSE_PAREN) | |
1064 | { | |
0527bc4e | 1065 | cpp_error (pfile, CPP_DL_ERROR, "missing ')' in expression"); |
75aef48a NB |
1066 | return 0; |
1067 | } | |
1068 | top--; | |
1069 | top->value = top[1].value; | |
1070 | return top; | |
1071 | ||
1072 | case CPP_COLON: | |
1073 | top -= 2; | |
1074 | if (!num_zerop (top->value)) | |
1075 | { | |
ad28cff7 | 1076 | pfile->state.skip_eval--; |
75aef48a NB |
1077 | top->value = top[1].value; |
1078 | } | |
1079 | else | |
1080 | top->value = top[2].value; | |
1081 | top->value.unsignedp = (top[1].value.unsignedp | |
1082 | || top[2].value.unsignedp); | |
1083 | continue; | |
1084 | ||
1085 | case CPP_QUERY: | |
0527bc4e | 1086 | cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'"); |
75aef48a NB |
1087 | return 0; |
1088 | ||
1089 | default: | |
1090 | goto bad_op; | |
1091 | } | |
91318908 | 1092 | |
ad28cff7 | 1093 | top--; |
91318908 | 1094 | if (top->value.overflow && !pfile->state.skip_eval) |
0527bc4e | 1095 | cpp_error (pfile, CPP_DL_PEDWARN, |
91318908 | 1096 | "integer overflow in preprocessor expression"); |
87ed109f NB |
1097 | } |
1098 | ||
1099 | if (op == CPP_CLOSE_PAREN) | |
1100 | { | |
0527bc4e | 1101 | cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression"); |
87ed109f NB |
1102 | return 0; |
1103 | } | |
1104 | ||
1105 | return top; | |
1106 | } | |
1107 | ||
1108 | /* Returns the position of the old top of stack after expansion. */ | |
1109 | struct op * | |
6cf87ca4 | 1110 | _cpp_expand_op_stack (cpp_reader *pfile) |
87ed109f | 1111 | { |
32fa4565 NB |
1112 | size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack); |
1113 | size_t new_size = old_size * 2 + 20; | |
87ed109f | 1114 | |
c3f829c1 | 1115 | pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size); |
32fa4565 | 1116 | pfile->op_limit = pfile->op_stack + new_size; |
87ed109f | 1117 | |
32fa4565 | 1118 | return pfile->op_stack + old_size; |
7f2935c7 | 1119 | } |
91318908 | 1120 | |
68e65275 NB |
1121 | /* Emits a warning if the effective sign of either operand of OP |
1122 | changes because of integer promotions. */ | |
1123 | static void | |
6cf87ca4 | 1124 | check_promotion (cpp_reader *pfile, const struct op *op) |
68e65275 NB |
1125 | { |
1126 | if (op->value.unsignedp == op[-1].value.unsignedp) | |
1127 | return; | |
1128 | ||
1129 | if (op->value.unsignedp) | |
1130 | { | |
1131 | if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision))) | |
0527bc4e | 1132 | cpp_error (pfile, CPP_DL_WARNING, |
68e65275 NB |
1133 | "the left operand of \"%s\" changes sign when promoted", |
1134 | cpp_token_as_text (pfile, op->token)); | |
1135 | } | |
1136 | else if (!num_positive (op->value, CPP_OPTION (pfile, precision))) | |
0527bc4e | 1137 | cpp_error (pfile, CPP_DL_WARNING, |
68e65275 NB |
1138 | "the right operand of \"%s\" changes sign when promoted", |
1139 | cpp_token_as_text (pfile, op->token)); | |
1140 | } | |
1141 | ||
91318908 NB |
1142 | /* Clears the unused high order bits of the number pointed to by PNUM. */ |
1143 | static cpp_num | |
6cf87ca4 | 1144 | num_trim (cpp_num num, size_t precision) |
91318908 NB |
1145 | { |
1146 | if (precision > PART_PRECISION) | |
1147 | { | |
1148 | precision -= PART_PRECISION; | |
1149 | if (precision < PART_PRECISION) | |
359b0bec | 1150 | num.high &= ((cpp_num_part) 1 << precision) - 1; |
91318908 NB |
1151 | } |
1152 | else | |
1153 | { | |
1154 | if (precision < PART_PRECISION) | |
359b0bec | 1155 | num.low &= ((cpp_num_part) 1 << precision) - 1; |
91318908 NB |
1156 | num.high = 0; |
1157 | } | |
1158 | ||
1159 | return num; | |
1160 | } | |
1161 | ||
1162 | /* True iff A (presumed signed) >= 0. */ | |
1163 | static bool | |
6cf87ca4 | 1164 | num_positive (cpp_num num, size_t precision) |
91318908 NB |
1165 | { |
1166 | if (precision > PART_PRECISION) | |
1167 | { | |
1168 | precision -= PART_PRECISION; | |
359b0bec | 1169 | return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0; |
91318908 NB |
1170 | } |
1171 | ||
359b0bec | 1172 | return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0; |
91318908 NB |
1173 | } |
1174 | ||
ceeedfc1 NB |
1175 | /* Sign extend a number, with PRECISION significant bits and all |
1176 | others assumed clear, to fill out a cpp_num structure. */ | |
1177 | cpp_num | |
6cf87ca4 | 1178 | cpp_num_sign_extend (cpp_num num, size_t precision) |
ceeedfc1 NB |
1179 | { |
1180 | if (!num.unsignedp) | |
1181 | { | |
1182 | if (precision > PART_PRECISION) | |
1183 | { | |
1184 | precision -= PART_PRECISION; | |
1185 | if (precision < PART_PRECISION | |
1186 | && (num.high & (cpp_num_part) 1 << (precision - 1))) | |
1187 | num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision)); | |
1188 | } | |
1189 | else if (num.low & (cpp_num_part) 1 << (precision - 1)) | |
1190 | { | |
1191 | if (precision < PART_PRECISION) | |
1192 | num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision)); | |
1193 | num.high = ~(cpp_num_part) 0; | |
1194 | } | |
1195 | } | |
1196 | ||
1197 | return num; | |
1198 | } | |
1199 | ||
91318908 NB |
1200 | /* Returns the negative of NUM. */ |
1201 | static cpp_num | |
6cf87ca4 | 1202 | num_negate (cpp_num num, size_t precision) |
91318908 NB |
1203 | { |
1204 | cpp_num copy; | |
1205 | ||
1206 | copy = num; | |
1207 | num.high = ~num.high; | |
1208 | num.low = ~num.low; | |
1209 | if (++num.low == 0) | |
1210 | num.high++; | |
1211 | num = num_trim (num, precision); | |
1212 | num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num)); | |
1213 | ||
1214 | return num; | |
1215 | } | |
1216 | ||
1217 | /* Returns true if A >= B. */ | |
1218 | static bool | |
6cf87ca4 | 1219 | num_greater_eq (cpp_num pa, cpp_num pb, size_t precision) |
91318908 NB |
1220 | { |
1221 | bool unsignedp; | |
1222 | ||
1223 | unsignedp = pa.unsignedp || pb.unsignedp; | |
1224 | ||
1225 | if (!unsignedp) | |
1226 | { | |
1227 | /* Both numbers have signed type. If they are of different | |
1228 | sign, the answer is the sign of A. */ | |
1229 | unsignedp = num_positive (pa, precision); | |
1230 | ||
1231 | if (unsignedp != num_positive (pb, precision)) | |
1232 | return unsignedp; | |
1233 | ||
1234 | /* Otherwise we can do an unsigned comparison. */ | |
1235 | } | |
1236 | ||
1237 | return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low); | |
1238 | } | |
1239 | ||
1240 | /* Returns LHS OP RHS, where OP is a bit-wise operation. */ | |
1241 | static cpp_num | |
6cf87ca4 ZW |
1242 | num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED, |
1243 | cpp_num lhs, cpp_num rhs, enum cpp_ttype op) | |
91318908 NB |
1244 | { |
1245 | lhs.overflow = false; | |
1246 | lhs.unsignedp = lhs.unsignedp || rhs.unsignedp; | |
1247 | ||
1248 | /* As excess precision is zeroed, there is no need to num_trim () as | |
1249 | these operations cannot introduce a set bit there. */ | |
1250 | if (op == CPP_AND) | |
1251 | { | |
1252 | lhs.low &= rhs.low; | |
1253 | lhs.high &= rhs.high; | |
1254 | } | |
1255 | else if (op == CPP_OR) | |
1256 | { | |
1257 | lhs.low |= rhs.low; | |
1258 | lhs.high |= rhs.high; | |
1259 | } | |
1260 | else | |
1261 | { | |
1262 | lhs.low ^= rhs.low; | |
1263 | lhs.high ^= rhs.high; | |
1264 | } | |
1265 | ||
1266 | return lhs; | |
1267 | } | |
1268 | ||
1269 | /* Returns LHS OP RHS, where OP is an inequality. */ | |
1270 | static cpp_num | |
6cf87ca4 ZW |
1271 | num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, |
1272 | enum cpp_ttype op) | |
91318908 NB |
1273 | { |
1274 | bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision)); | |
1275 | ||
1276 | if (op == CPP_GREATER_EQ) | |
1277 | lhs.low = gte; | |
1278 | else if (op == CPP_LESS) | |
1279 | lhs.low = !gte; | |
1280 | else if (op == CPP_GREATER) | |
1281 | lhs.low = gte && !num_eq (lhs, rhs); | |
1282 | else /* CPP_LESS_EQ. */ | |
1283 | lhs.low = !gte || num_eq (lhs, rhs); | |
1284 | ||
1285 | lhs.high = 0; | |
1286 | lhs.overflow = false; | |
1287 | lhs.unsignedp = false; | |
1288 | return lhs; | |
1289 | } | |
1290 | ||
1291 | /* Returns LHS OP RHS, where OP is == or !=. */ | |
1292 | static cpp_num | |
6cf87ca4 ZW |
1293 | num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED, |
1294 | cpp_num lhs, cpp_num rhs, enum cpp_ttype op) | |
91318908 | 1295 | { |
97459791 JM |
1296 | /* Work around a 3.0.4 bug; see PR 6950. */ |
1297 | bool eq = num_eq (lhs, rhs); | |
91318908 | 1298 | if (op == CPP_NOT_EQ) |
97459791 JM |
1299 | eq = !eq; |
1300 | lhs.low = eq; | |
91318908 NB |
1301 | lhs.high = 0; |
1302 | lhs.overflow = false; | |
1303 | lhs.unsignedp = false; | |
1304 | return lhs; | |
1305 | } | |
1306 | ||
1307 | /* Shift NUM, of width PRECISION, right by N bits. */ | |
1308 | static cpp_num | |
6cf87ca4 | 1309 | num_rshift (cpp_num num, size_t precision, size_t n) |
91318908 NB |
1310 | { |
1311 | cpp_num_part sign_mask; | |
6de9cd9a | 1312 | bool x = num_positive (num, precision); |
91318908 | 1313 | |
6de9cd9a | 1314 | if (num.unsignedp || x) |
91318908 NB |
1315 | sign_mask = 0; |
1316 | else | |
1317 | sign_mask = ~(cpp_num_part) 0; | |
1318 | ||
1319 | if (n >= precision) | |
1320 | num.high = num.low = sign_mask; | |
1321 | else | |
1322 | { | |
1323 | /* Sign-extend. */ | |
1324 | if (precision < PART_PRECISION) | |
1325 | num.high = sign_mask, num.low |= sign_mask << precision; | |
1326 | else if (precision < 2 * PART_PRECISION) | |
1327 | num.high |= sign_mask << (precision - PART_PRECISION); | |
1328 | ||
1329 | if (n >= PART_PRECISION) | |
1330 | { | |
1331 | n -= PART_PRECISION; | |
1332 | num.low = num.high; | |
1333 | num.high = sign_mask; | |
1334 | } | |
1335 | ||
1336 | if (n) | |
1337 | { | |
1338 | num.low = (num.low >> n) | (num.high << (PART_PRECISION - n)); | |
1339 | num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n)); | |
1340 | } | |
1341 | } | |
1342 | ||
1343 | num = num_trim (num, precision); | |
1344 | num.overflow = false; | |
1345 | return num; | |
1346 | } | |
1347 | ||
1348 | /* Shift NUM, of width PRECISION, left by N bits. */ | |
1349 | static cpp_num | |
6cf87ca4 | 1350 | num_lshift (cpp_num num, size_t precision, size_t n) |
91318908 NB |
1351 | { |
1352 | if (n >= precision) | |
1353 | { | |
1354 | num.overflow = !num.unsignedp && !num_zerop (num); | |
1355 | num.high = num.low = 0; | |
1356 | } | |
1357 | else | |
1358 | { | |
1359 | cpp_num orig, maybe_orig; | |
1360 | size_t m = n; | |
1361 | ||
1362 | orig = num; | |
1363 | if (m >= PART_PRECISION) | |
1364 | { | |
1365 | m -= PART_PRECISION; | |
1366 | num.high = num.low; | |
1367 | num.low = 0; | |
1368 | } | |
1369 | if (m) | |
1370 | { | |
1371 | num.high = (num.high << m) | (num.low >> (PART_PRECISION - m)); | |
1372 | num.low <<= m; | |
1373 | } | |
1374 | num = num_trim (num, precision); | |
1375 | ||
1376 | if (num.unsignedp) | |
1377 | num.overflow = false; | |
1378 | else | |
1379 | { | |
1380 | maybe_orig = num_rshift (num, precision, n); | |
1381 | num.overflow = !num_eq (orig, maybe_orig); | |
1382 | } | |
1383 | } | |
1384 | ||
1385 | return num; | |
1386 | } | |
1387 | ||
1388 | /* The four unary operators: +, -, ! and ~. */ | |
1389 | static cpp_num | |
6cf87ca4 | 1390 | num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op) |
91318908 NB |
1391 | { |
1392 | switch (op) | |
1393 | { | |
1394 | case CPP_UPLUS: | |
75aef48a | 1395 | if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval) |
0527bc4e | 1396 | cpp_error (pfile, CPP_DL_WARNING, |
91318908 NB |
1397 | "traditional C rejects the unary plus operator"); |
1398 | num.overflow = false; | |
1399 | break; | |
1400 | ||
1401 | case CPP_UMINUS: | |
1402 | num = num_negate (num, CPP_OPTION (pfile, precision)); | |
1403 | break; | |
1404 | ||
1405 | case CPP_COMPL: | |
1406 | num.high = ~num.high; | |
1407 | num.low = ~num.low; | |
1408 | num = num_trim (num, CPP_OPTION (pfile, precision)); | |
1409 | num.overflow = false; | |
1410 | break; | |
1411 | ||
1412 | default: /* case CPP_NOT: */ | |
1413 | num.low = num_zerop (num); | |
1414 | num.high = 0; | |
1415 | num.overflow = false; | |
1416 | num.unsignedp = false; | |
1417 | break; | |
1418 | } | |
1419 | ||
1420 | return num; | |
1421 | } | |
1422 | ||
1423 | /* The various binary operators. */ | |
1424 | static cpp_num | |
6cf87ca4 | 1425 | num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op) |
91318908 NB |
1426 | { |
1427 | cpp_num result; | |
1428 | size_t precision = CPP_OPTION (pfile, precision); | |
91318908 NB |
1429 | size_t n; |
1430 | ||
1431 | switch (op) | |
1432 | { | |
1433 | /* Shifts. */ | |
1434 | case CPP_LSHIFT: | |
1435 | case CPP_RSHIFT: | |
1436 | if (!rhs.unsignedp && !num_positive (rhs, precision)) | |
1437 | { | |
1438 | /* A negative shift is a positive shift the other way. */ | |
1439 | if (op == CPP_LSHIFT) | |
1440 | op = CPP_RSHIFT; | |
1441 | else | |
1442 | op = CPP_LSHIFT; | |
1443 | rhs = num_negate (rhs, precision); | |
1444 | } | |
1445 | if (rhs.high) | |
1446 | n = ~0; /* Maximal. */ | |
1447 | else | |
1448 | n = rhs.low; | |
1449 | if (op == CPP_LSHIFT) | |
1450 | lhs = num_lshift (lhs, precision, n); | |
1451 | else | |
1452 | lhs = num_rshift (lhs, precision, n); | |
1453 | break; | |
1454 | ||
91318908 NB |
1455 | /* Arithmetic. */ |
1456 | case CPP_MINUS: | |
1457 | rhs = num_negate (rhs, precision); | |
1458 | case CPP_PLUS: | |
1459 | result.low = lhs.low + rhs.low; | |
1460 | result.high = lhs.high + rhs.high; | |
1461 | if (result.low < lhs.low) | |
1462 | result.high++; | |
6de9cd9a DN |
1463 | result.unsignedp = lhs.unsignedp || rhs.unsignedp; |
1464 | result.overflow = false; | |
91318908 NB |
1465 | |
1466 | result = num_trim (result, precision); | |
6de9cd9a | 1467 | if (!result.unsignedp) |
91318908 NB |
1468 | { |
1469 | bool lhsp = num_positive (lhs, precision); | |
1470 | result.overflow = (lhsp == num_positive (rhs, precision) | |
1471 | && lhsp != num_positive (result, precision)); | |
1472 | } | |
1473 | return result; | |
1474 | ||
1475 | /* Comma. */ | |
1476 | default: /* case CPP_COMMA: */ | |
32e8aa9a JM |
1477 | if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99) |
1478 | || !pfile->state.skip_eval)) | |
0527bc4e | 1479 | cpp_error (pfile, CPP_DL_PEDWARN, |
91318908 NB |
1480 | "comma operator in operand of #if"); |
1481 | lhs = rhs; | |
1482 | break; | |
1483 | } | |
1484 | ||
1485 | return lhs; | |
1486 | } | |
1487 | ||
1488 | /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This | |
1489 | cannot overflow. */ | |
1490 | static cpp_num | |
6cf87ca4 | 1491 | num_part_mul (cpp_num_part lhs, cpp_num_part rhs) |
91318908 NB |
1492 | { |
1493 | cpp_num result; | |
1494 | cpp_num_part middle[2], temp; | |
1495 | ||
1496 | result.low = LOW_PART (lhs) * LOW_PART (rhs); | |
1497 | result.high = HIGH_PART (lhs) * HIGH_PART (rhs); | |
1498 | ||
1499 | middle[0] = LOW_PART (lhs) * HIGH_PART (rhs); | |
1500 | middle[1] = HIGH_PART (lhs) * LOW_PART (rhs); | |
1501 | ||
1502 | temp = result.low; | |
1503 | result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2); | |
1504 | if (result.low < temp) | |
1505 | result.high++; | |
1506 | ||
1507 | temp = result.low; | |
1508 | result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2); | |
1509 | if (result.low < temp) | |
1510 | result.high++; | |
1511 | ||
1512 | result.high += HIGH_PART (middle[0]); | |
1513 | result.high += HIGH_PART (middle[1]); | |
6de9cd9a DN |
1514 | result.unsignedp = true; |
1515 | result.overflow = false; | |
91318908 NB |
1516 | |
1517 | return result; | |
1518 | } | |
1519 | ||
1520 | /* Multiply two preprocessing numbers. */ | |
1521 | static cpp_num | |
6cf87ca4 | 1522 | num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs) |
91318908 NB |
1523 | { |
1524 | cpp_num result, temp; | |
1525 | bool unsignedp = lhs.unsignedp || rhs.unsignedp; | |
1526 | bool overflow, negate = false; | |
1527 | size_t precision = CPP_OPTION (pfile, precision); | |
1528 | ||
1529 | /* Prepare for unsigned multiplication. */ | |
1530 | if (!unsignedp) | |
1531 | { | |
1532 | if (!num_positive (lhs, precision)) | |
1533 | negate = !negate, lhs = num_negate (lhs, precision); | |
1534 | if (!num_positive (rhs, precision)) | |
1535 | negate = !negate, rhs = num_negate (rhs, precision); | |
1536 | } | |
1537 | ||
1538 | overflow = lhs.high && rhs.high; | |
1539 | result = num_part_mul (lhs.low, rhs.low); | |
1540 | ||
1541 | temp = num_part_mul (lhs.high, rhs.low); | |
1542 | result.high += temp.low; | |
1543 | if (temp.high) | |
1544 | overflow = true; | |
1545 | ||
1546 | temp = num_part_mul (lhs.low, rhs.high); | |
1547 | result.high += temp.low; | |
1548 | if (temp.high) | |
1549 | overflow = true; | |
1550 | ||
1551 | temp.low = result.low, temp.high = result.high; | |
1552 | result = num_trim (result, precision); | |
1553 | if (!num_eq (result, temp)) | |
1554 | overflow = true; | |
1555 | ||
1556 | if (negate) | |
1557 | result = num_negate (result, precision); | |
1558 | ||
1559 | if (unsignedp) | |
1560 | result.overflow = false; | |
1561 | else | |
1562 | result.overflow = overflow || (num_positive (result, precision) ^ !negate | |
1563 | && !num_zerop (result)); | |
1564 | result.unsignedp = unsignedp; | |
1565 | ||
1566 | return result; | |
1567 | } | |
1568 | ||
1569 | /* Divide two preprocessing numbers, returning the answer or the | |
1570 | remainder depending upon OP. */ | |
1571 | static cpp_num | |
6cf87ca4 | 1572 | num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op) |
91318908 NB |
1573 | { |
1574 | cpp_num result, sub; | |
1575 | cpp_num_part mask; | |
1576 | bool unsignedp = lhs.unsignedp || rhs.unsignedp; | |
1577 | bool negate = false, lhs_neg = false; | |
1578 | size_t i, precision = CPP_OPTION (pfile, precision); | |
1579 | ||
1580 | /* Prepare for unsigned division. */ | |
1581 | if (!unsignedp) | |
1582 | { | |
1583 | if (!num_positive (lhs, precision)) | |
1584 | negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision); | |
1585 | if (!num_positive (rhs, precision)) | |
1586 | negate = !negate, rhs = num_negate (rhs, precision); | |
1587 | } | |
1588 | ||
1589 | /* Find the high bit. */ | |
1590 | if (rhs.high) | |
1591 | { | |
1592 | i = precision - 1; | |
359b0bec | 1593 | mask = (cpp_num_part) 1 << (i - PART_PRECISION); |
91318908 NB |
1594 | for (; ; i--, mask >>= 1) |
1595 | if (rhs.high & mask) | |
1596 | break; | |
1597 | } | |
1598 | else if (rhs.low) | |
1599 | { | |
1600 | if (precision > PART_PRECISION) | |
1601 | i = precision - PART_PRECISION - 1; | |
1602 | else | |
1603 | i = precision - 1; | |
359b0bec | 1604 | mask = (cpp_num_part) 1 << i; |
91318908 NB |
1605 | for (; ; i--, mask >>= 1) |
1606 | if (rhs.low & mask) | |
1607 | break; | |
1608 | } | |
1609 | else | |
1610 | { | |
75aef48a | 1611 | if (!pfile->state.skip_eval) |
0527bc4e | 1612 | cpp_error (pfile, CPP_DL_ERROR, "division by zero in #if"); |
91318908 NB |
1613 | return lhs; |
1614 | } | |
1615 | ||
da7d8304 | 1616 | /* First nonzero bit of RHS is bit I. Do naive division by |
91318908 NB |
1617 | shifting the RHS fully left, and subtracting from LHS if LHS is |
1618 | at least as big, and then repeating but with one less shift. | |
1619 | This is not very efficient, but is easy to understand. */ | |
1620 | ||
1621 | rhs.unsignedp = true; | |
1622 | lhs.unsignedp = true; | |
1623 | i = precision - i - 1; | |
1624 | sub = num_lshift (rhs, precision, i); | |
1625 | ||
1626 | result.high = result.low = 0; | |
1627 | for (;;) | |
1628 | { | |
1629 | if (num_greater_eq (lhs, sub, precision)) | |
1630 | { | |
1631 | lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS); | |
1632 | if (i >= PART_PRECISION) | |
359b0bec | 1633 | result.high |= (cpp_num_part) 1 << (i - PART_PRECISION); |
91318908 | 1634 | else |
359b0bec | 1635 | result.low |= (cpp_num_part) 1 << i; |
91318908 NB |
1636 | } |
1637 | if (i-- == 0) | |
1638 | break; | |
1639 | sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1)); | |
1640 | sub.high >>= 1; | |
1641 | } | |
1642 | ||
1643 | /* We divide so that the remainder has the sign of the LHS. */ | |
1644 | if (op == CPP_DIV) | |
1645 | { | |
1646 | result.unsignedp = unsignedp; | |
6de9cd9a DN |
1647 | result.overflow = false; |
1648 | if (!unsignedp) | |
91318908 NB |
1649 | { |
1650 | if (negate) | |
1651 | result = num_negate (result, precision); | |
22a8a52d EC |
1652 | result.overflow = (num_positive (result, precision) ^ !negate |
1653 | && !num_zerop (result)); | |
91318908 NB |
1654 | } |
1655 | ||
1656 | return result; | |
1657 | } | |
1658 | ||
1659 | /* CPP_MOD. */ | |
1660 | lhs.unsignedp = unsignedp; | |
1661 | lhs.overflow = false; | |
1662 | if (lhs_neg) | |
1663 | lhs = num_negate (lhs, precision); | |
1664 | ||
1665 | return lhs; | |
1666 | } |