]> git.ipfire.org Git - thirdparty/gcc.git/blob - libcpp/expr.c
tree-optimization/95495 - use SLP_TREE_REPRESENTATIVE in assertion
[thirdparty/gcc.git] / libcpp / expr.c
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987-2020 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any
8 later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING3. If not see
17 <http://www.gnu.org/licenses/>. */
18
19 #include "config.h"
20 #include "system.h"
21 #include "cpplib.h"
22 #include "internal.h"
23
24 #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
25 #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
26 #define LOW_PART(num_part) (num_part & HALF_MASK)
27 #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
28
29 struct op
30 {
31 const cpp_token *token; /* The token forming op (for diagnostics). */
32 cpp_num value; /* The value logically "right" of op. */
33 location_t loc; /* The location of this value. */
34 enum cpp_ttype op;
35 };
36
37 /* Some simple utility routines on double integers. */
38 #define num_zerop(num) ((num.low | num.high) == 0)
39 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
40 static bool num_positive (cpp_num, size_t);
41 static bool num_greater_eq (cpp_num, cpp_num, size_t);
42 static cpp_num num_trim (cpp_num, size_t);
43 static cpp_num num_part_mul (cpp_num_part, cpp_num_part);
44
45 static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype);
46 static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
47 static cpp_num num_negate (cpp_num, size_t);
48 static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
49 static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num,
50 enum cpp_ttype);
51 static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num,
52 enum cpp_ttype);
53 static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num);
54 static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype,
55 location_t);
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 *, location_t);
62 static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
63 static unsigned int interpret_float_suffix (cpp_reader *, const uchar *, size_t);
64 static unsigned int interpret_int_suffix (cpp_reader *, const uchar *, size_t);
65 static void check_promotion (cpp_reader *, const struct op *);
66
67 /* Token type abuse to create unary plus and minus operators. */
68 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
69 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
70
71 /* With -O2, gcc appears to produce nice code, moving the error
72 message load and subsequent jump completely out of the main path. */
73 #define SYNTAX_ERROR(msgid) \
74 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
75 #define SYNTAX_ERROR2(msgid, arg) \
76 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
77 while(0)
78 #define SYNTAX_ERROR_AT(loc, msgid) \
79 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \
80 while(0)
81 #define SYNTAX_ERROR2_AT(loc, msgid, arg) \
82 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \
83 while(0)
84
85 /* Subroutine of cpp_classify_number. S points to a float suffix of
86 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
87 flag vector (of CPP_N_* bits) describing the suffix. */
88 static unsigned int
89 interpret_float_suffix (cpp_reader *pfile, const uchar *s, size_t len)
90 {
91 size_t orig_len = len;
92 const uchar *orig_s = s;
93 size_t flags;
94 size_t f, d, l, w, q, i, fn, fnx, fn_bits;
95
96 flags = 0;
97 f = d = l = w = q = i = fn = fnx = fn_bits = 0;
98
99 /* The following decimal float suffixes, from TR 24732:2009, TS
100 18661-2:2015 and C2X, are supported:
101
102 df, DF - _Decimal32.
103 dd, DD - _Decimal64.
104 dl, DL - _Decimal128.
105
106 The dN and DN suffixes for _DecimalN, and dNx and DNx for
107 _DecimalNx, defined in TS 18661-3:2015, are not supported.
108
109 Fixed-point suffixes, from TR 18037:2008, are supported. They
110 consist of three parts, in order:
111
112 (i) An optional u or U, for unsigned types.
113
114 (ii) An optional h or H, for short types, or l or L, for long
115 types, or ll or LL, for long long types. Use of ll or LL is a
116 GNU extension.
117
118 (iii) r or R, for _Fract types, or k or K, for _Accum types.
119
120 Otherwise the suffix is for a binary or standard floating-point
121 type. Such a suffix, or the absence of a suffix, may be preceded
122 or followed by i, I, j or J, to indicate an imaginary number with
123 the corresponding complex type. The following suffixes for
124 binary or standard floating-point types are supported:
125
126 f, F - float (ISO C and C++).
127 l, L - long double (ISO C and C++).
128 d, D - double, even with the FLOAT_CONST_DECIMAL64 pragma in
129 operation (from TR 24732:2009; the pragma and the suffix
130 are not included in TS 18661-2:2015).
131 w, W - machine-specific type such as __float80 (GNU extension).
132 q, Q - machine-specific type such as __float128 (GNU extension).
133 fN, FN - _FloatN (TS 18661-3:2015).
134 fNx, FNx - _FloatNx (TS 18661-3:2015). */
135
136 /* Process decimal float suffixes, which are two letters starting
137 with d or D. Order and case are significant. */
138 if (len == 2 && (*s == 'd' || *s == 'D'))
139 {
140 bool uppercase = (*s == 'D');
141 switch (s[1])
142 {
143 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break;
144 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break;
145 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break;
146 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break;
147 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
148 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
149 default:
150 /* Additional two-character suffixes beginning with D are not
151 for decimal float constants. */
152 break;
153 }
154 }
155
156 if (CPP_OPTION (pfile, ext_numeric_literals))
157 {
158 /* Recognize a fixed-point suffix. */
159 if (len != 0)
160 switch (s[len-1])
161 {
162 case 'k': case 'K': flags = CPP_N_ACCUM; break;
163 case 'r': case 'R': flags = CPP_N_FRACT; break;
164 default: break;
165 }
166
167 /* Continue processing a fixed-point suffix. The suffix is case
168 insensitive except for ll or LL. Order is significant. */
169 if (flags)
170 {
171 if (len == 1)
172 return flags;
173 len--;
174
175 if (*s == 'u' || *s == 'U')
176 {
177 flags |= CPP_N_UNSIGNED;
178 if (len == 1)
179 return flags;
180 len--;
181 s++;
182 }
183
184 switch (*s)
185 {
186 case 'h': case 'H':
187 if (len == 1)
188 return flags |= CPP_N_SMALL;
189 break;
190 case 'l':
191 if (len == 1)
192 return flags |= CPP_N_MEDIUM;
193 if (len == 2 && s[1] == 'l')
194 return flags |= CPP_N_LARGE;
195 break;
196 case 'L':
197 if (len == 1)
198 return flags |= CPP_N_MEDIUM;
199 if (len == 2 && s[1] == 'L')
200 return flags |= CPP_N_LARGE;
201 break;
202 default:
203 break;
204 }
205 /* Anything left at this point is invalid. */
206 return 0;
207 }
208 }
209
210 /* In any remaining valid suffix, the case and order don't matter. */
211 while (len--)
212 {
213 switch (s[0])
214 {
215 case 'f': case 'F':
216 f++;
217 if (len > 0
218 && !CPP_OPTION (pfile, cplusplus)
219 && s[1] >= '1'
220 && s[1] <= '9'
221 && fn_bits == 0)
222 {
223 f--;
224 while (len > 0
225 && s[1] >= '0'
226 && s[1] <= '9'
227 && fn_bits < CPP_FLOATN_MAX)
228 {
229 fn_bits = fn_bits * 10 + (s[1] - '0');
230 len--;
231 s++;
232 }
233 if (len > 0 && s[1] == 'x')
234 {
235 fnx++;
236 len--;
237 s++;
238 }
239 else
240 fn++;
241 }
242 break;
243 case 'd': case 'D': d++; break;
244 case 'l': case 'L': l++; break;
245 case 'w': case 'W': w++; break;
246 case 'q': case 'Q': q++; break;
247 case 'i': case 'I':
248 case 'j': case 'J': i++; break;
249 default:
250 return 0;
251 }
252 s++;
253 }
254
255 /* Reject any case of multiple suffixes specifying types, multiple
256 suffixes specifying an imaginary constant, _FloatN or _FloatNx
257 suffixes for invalid values of N, and _FloatN suffixes for values
258 of N larger than can be represented in the return value. The
259 caller is responsible for rejecting _FloatN suffixes where
260 _FloatN is not supported on the chosen target. */
261 if (f + d + l + w + q + fn + fnx > 1 || i > 1)
262 return 0;
263 if (fn_bits > CPP_FLOATN_MAX)
264 return 0;
265 if (fnx && fn_bits != 32 && fn_bits != 64 && fn_bits != 128)
266 return 0;
267 if (fn && fn_bits != 16 && fn_bits % 32 != 0)
268 return 0;
269 if (fn && fn_bits == 96)
270 return 0;
271
272 if (i)
273 {
274 if (!CPP_OPTION (pfile, ext_numeric_literals))
275 return 0;
276
277 /* In C++14 and up these suffixes are in the standard library, so treat
278 them as user-defined literals. */
279 if (CPP_OPTION (pfile, cplusplus)
280 && CPP_OPTION (pfile, lang) > CLK_CXX11
281 && orig_s[0] == 'i'
282 && (orig_len == 1
283 || (orig_len == 2
284 && (orig_s[1] == 'f' || orig_s[1] == 'l'))))
285 return 0;
286 }
287
288 if ((w || q) && !CPP_OPTION (pfile, ext_numeric_literals))
289 return 0;
290
291 return ((i ? CPP_N_IMAGINARY : 0)
292 | (f ? CPP_N_SMALL :
293 d ? CPP_N_MEDIUM :
294 l ? CPP_N_LARGE :
295 w ? CPP_N_MD_W :
296 q ? CPP_N_MD_Q :
297 fn ? CPP_N_FLOATN | (fn_bits << CPP_FLOATN_SHIFT) :
298 fnx ? CPP_N_FLOATNX | (fn_bits << CPP_FLOATN_SHIFT) :
299 CPP_N_DEFAULT));
300 }
301
302 /* Return the classification flags for a float suffix. */
303 unsigned int
304 cpp_interpret_float_suffix (cpp_reader *pfile, const char *s, size_t len)
305 {
306 return interpret_float_suffix (pfile, (const unsigned char *)s, len);
307 }
308
309 /* Subroutine of cpp_classify_number. S points to an integer suffix
310 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
311 flag vector describing the suffix. */
312 static unsigned int
313 interpret_int_suffix (cpp_reader *pfile, const uchar *s, size_t len)
314 {
315 size_t orig_len = len;
316 size_t u, l, i;
317
318 u = l = i = 0;
319
320 while (len--)
321 switch (s[len])
322 {
323 case 'u': case 'U': u++; break;
324 case 'i': case 'I':
325 case 'j': case 'J': i++; break;
326 case 'l': case 'L': l++;
327 /* If there are two Ls, they must be adjacent and the same case. */
328 if (l == 2 && s[len] != s[len + 1])
329 return 0;
330 break;
331 default:
332 return 0;
333 }
334
335 if (l > 2 || u > 1 || i > 1)
336 return 0;
337
338 if (i)
339 {
340 if (!CPP_OPTION (pfile, ext_numeric_literals))
341 return 0;
342
343 /* In C++14 and up these suffixes are in the standard library, so treat
344 them as user-defined literals. */
345 if (CPP_OPTION (pfile, cplusplus)
346 && CPP_OPTION (pfile, lang) > CLK_CXX11
347 && s[0] == 'i'
348 && (orig_len == 1 || (orig_len == 2 && s[1] == 'l')))
349 return 0;
350 }
351
352 return ((i ? CPP_N_IMAGINARY : 0)
353 | (u ? CPP_N_UNSIGNED : 0)
354 | ((l == 0) ? CPP_N_SMALL
355 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
356 }
357
358 /* Return the classification flags for an int suffix. */
359 unsigned int
360 cpp_interpret_int_suffix (cpp_reader *pfile, const char *s, size_t len)
361 {
362 return interpret_int_suffix (pfile, (const unsigned char *)s, len);
363 }
364
365 /* Return the string type corresponding to the the input user-defined string
366 literal type. If the input type is not a user-defined string literal
367 type return the input type. */
368 enum cpp_ttype
369 cpp_userdef_string_remove_type (enum cpp_ttype type)
370 {
371 if (type == CPP_STRING_USERDEF)
372 return CPP_STRING;
373 else if (type == CPP_WSTRING_USERDEF)
374 return CPP_WSTRING;
375 else if (type == CPP_STRING16_USERDEF)
376 return CPP_STRING16;
377 else if (type == CPP_STRING32_USERDEF)
378 return CPP_STRING32;
379 else if (type == CPP_UTF8STRING_USERDEF)
380 return CPP_UTF8STRING;
381 else
382 return type;
383 }
384
385 /* Return the user-defined string literal type corresponding to the input
386 string type. If the input type is not a string type return the input
387 type. */
388 enum cpp_ttype
389 cpp_userdef_string_add_type (enum cpp_ttype type)
390 {
391 if (type == CPP_STRING)
392 return CPP_STRING_USERDEF;
393 else if (type == CPP_WSTRING)
394 return CPP_WSTRING_USERDEF;
395 else if (type == CPP_STRING16)
396 return CPP_STRING16_USERDEF;
397 else if (type == CPP_STRING32)
398 return CPP_STRING32_USERDEF;
399 else if (type == CPP_UTF8STRING)
400 return CPP_UTF8STRING_USERDEF;
401 else
402 return type;
403 }
404
405 /* Return the char type corresponding to the the input user-defined char
406 literal type. If the input type is not a user-defined char literal
407 type return the input type. */
408 enum cpp_ttype
409 cpp_userdef_char_remove_type (enum cpp_ttype type)
410 {
411 if (type == CPP_CHAR_USERDEF)
412 return CPP_CHAR;
413 else if (type == CPP_WCHAR_USERDEF)
414 return CPP_WCHAR;
415 else if (type == CPP_CHAR16_USERDEF)
416 return CPP_CHAR16;
417 else if (type == CPP_CHAR32_USERDEF)
418 return CPP_CHAR32;
419 else if (type == CPP_UTF8CHAR_USERDEF)
420 return CPP_UTF8CHAR;
421 else
422 return type;
423 }
424
425 /* Return the user-defined char literal type corresponding to the input
426 char type. If the input type is not a char type return the input
427 type. */
428 enum cpp_ttype
429 cpp_userdef_char_add_type (enum cpp_ttype type)
430 {
431 if (type == CPP_CHAR)
432 return CPP_CHAR_USERDEF;
433 else if (type == CPP_WCHAR)
434 return CPP_WCHAR_USERDEF;
435 else if (type == CPP_CHAR16)
436 return CPP_CHAR16_USERDEF;
437 else if (type == CPP_CHAR32)
438 return CPP_CHAR32_USERDEF;
439 else if (type == CPP_UTF8CHAR)
440 return CPP_UTF8CHAR_USERDEF;
441 else
442 return type;
443 }
444
445 /* Return true if the token type is a user-defined string literal. */
446 bool
447 cpp_userdef_string_p (enum cpp_ttype type)
448 {
449 if (type == CPP_STRING_USERDEF
450 || type == CPP_WSTRING_USERDEF
451 || type == CPP_STRING16_USERDEF
452 || type == CPP_STRING32_USERDEF
453 || type == CPP_UTF8STRING_USERDEF)
454 return true;
455 else
456 return false;
457 }
458
459 /* Return true if the token type is a user-defined char literal. */
460 bool
461 cpp_userdef_char_p (enum cpp_ttype type)
462 {
463 if (type == CPP_CHAR_USERDEF
464 || type == CPP_WCHAR_USERDEF
465 || type == CPP_CHAR16_USERDEF
466 || type == CPP_CHAR32_USERDEF
467 || type == CPP_UTF8CHAR_USERDEF)
468 return true;
469 else
470 return false;
471 }
472
473 /* Extract the suffix from a user-defined literal string or char. */
474 const char *
475 cpp_get_userdef_suffix (const cpp_token *tok)
476 {
477 unsigned int len = tok->val.str.len;
478 const char *text = (const char *)tok->val.str.text;
479 char delim;
480 unsigned int i;
481 for (i = 0; i < len; ++i)
482 if (text[i] == '\'' || text[i] == '"')
483 break;
484 if (i == len)
485 return text + len;
486 delim = text[i];
487 for (i = len; i > 0; --i)
488 if (text[i - 1] == delim)
489 break;
490 return text + i;
491 }
492
493 /* Categorize numeric constants according to their field (integer,
494 floating point, or invalid), radix (decimal, octal, hexadecimal),
495 and type suffixes.
496
497 TOKEN is the token that represents the numeric constant to
498 classify.
499
500 In C++0X if UD_SUFFIX is non null it will be assigned
501 any unrecognized suffix for a user-defined literal.
502
503 VIRTUAL_LOCATION is the virtual location for TOKEN. */
504 unsigned int
505 cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
506 const char **ud_suffix, location_t virtual_location)
507 {
508 const uchar *str = token->val.str.text;
509 const uchar *limit;
510 unsigned int max_digit, result, radix;
511 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
512 bool seen_digit;
513 bool seen_digit_sep;
514
515 if (ud_suffix)
516 *ud_suffix = NULL;
517
518 /* If the lexer has done its job, length one can only be a single
519 digit. Fast-path this very common case. */
520 if (token->val.str.len == 1)
521 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
522
523 limit = str + token->val.str.len;
524 float_flag = NOT_FLOAT;
525 max_digit = 0;
526 radix = 10;
527 seen_digit = false;
528 seen_digit_sep = false;
529
530 /* First, interpret the radix. */
531 if (*str == '0')
532 {
533 radix = 8;
534 str++;
535
536 /* Require at least one hex digit to classify it as hex. */
537 if (*str == 'x' || *str == 'X')
538 {
539 if (str[1] == '.' || ISXDIGIT (str[1]))
540 {
541 radix = 16;
542 str++;
543 }
544 else if (DIGIT_SEP (str[1]))
545 SYNTAX_ERROR_AT (virtual_location,
546 "digit separator after base indicator");
547 }
548 else if (*str == 'b' || *str == 'B')
549 {
550 if (str[1] == '0' || str[1] == '1')
551 {
552 radix = 2;
553 str++;
554 }
555 else if (DIGIT_SEP (str[1]))
556 SYNTAX_ERROR_AT (virtual_location,
557 "digit separator after base indicator");
558 }
559 }
560
561 /* Now scan for a well-formed integer or float. */
562 for (;;)
563 {
564 unsigned int c = *str++;
565
566 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
567 {
568 seen_digit_sep = false;
569 seen_digit = true;
570 c = hex_value (c);
571 if (c > max_digit)
572 max_digit = c;
573 }
574 else if (DIGIT_SEP (c))
575 {
576 if (seen_digit_sep)
577 SYNTAX_ERROR_AT (virtual_location, "adjacent digit separators");
578 seen_digit_sep = true;
579 }
580 else if (c == '.')
581 {
582 if (seen_digit_sep || DIGIT_SEP (*str))
583 SYNTAX_ERROR_AT (virtual_location,
584 "digit separator adjacent to decimal point");
585 seen_digit_sep = false;
586 if (float_flag == NOT_FLOAT)
587 float_flag = AFTER_POINT;
588 else
589 SYNTAX_ERROR_AT (virtual_location,
590 "too many decimal points in number");
591 }
592 else if ((radix <= 10 && (c == 'e' || c == 'E'))
593 || (radix == 16 && (c == 'p' || c == 'P')))
594 {
595 if (seen_digit_sep || DIGIT_SEP (*str))
596 SYNTAX_ERROR_AT (virtual_location,
597 "digit separator adjacent to exponent");
598 float_flag = AFTER_EXPON;
599 break;
600 }
601 else
602 {
603 /* Start of suffix. */
604 str--;
605 break;
606 }
607 }
608
609 if (seen_digit_sep && float_flag != AFTER_EXPON)
610 SYNTAX_ERROR_AT (virtual_location,
611 "digit separator outside digit sequence");
612
613 /* The suffix may be for decimal fixed-point constants without exponent. */
614 if (radix != 16 && float_flag == NOT_FLOAT)
615 {
616 result = interpret_float_suffix (pfile, str, limit - str);
617 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
618 {
619 result |= CPP_N_FLOATING;
620 /* We need to restore the radix to 10, if the radix is 8. */
621 if (radix == 8)
622 radix = 10;
623
624 if (CPP_PEDANTIC (pfile))
625 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
626 "fixed-point constants are a GCC extension");
627 goto syntax_ok;
628 }
629 else
630 result = 0;
631 }
632
633 if (float_flag != NOT_FLOAT && radix == 8)
634 radix = 10;
635
636 if (max_digit >= radix)
637 {
638 if (radix == 2)
639 SYNTAX_ERROR2_AT (virtual_location,
640 "invalid digit \"%c\" in binary constant", '0' + max_digit);
641 else
642 SYNTAX_ERROR2_AT (virtual_location,
643 "invalid digit \"%c\" in octal constant", '0' + max_digit);
644 }
645
646 if (float_flag != NOT_FLOAT)
647 {
648 if (radix == 2)
649 {
650 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
651 "invalid prefix \"0b\" for floating constant");
652 return CPP_N_INVALID;
653 }
654
655 if (radix == 16 && !seen_digit)
656 SYNTAX_ERROR_AT (virtual_location,
657 "no digits in hexadecimal floating constant");
658
659 if (radix == 16 && CPP_PEDANTIC (pfile)
660 && !CPP_OPTION (pfile, extended_numbers))
661 {
662 if (CPP_OPTION (pfile, cplusplus))
663 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
664 "use of C++17 hexadecimal floating constant");
665 else
666 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
667 "use of C99 hexadecimal floating constant");
668 }
669
670 if (float_flag == AFTER_EXPON)
671 {
672 if (*str == '+' || *str == '-')
673 str++;
674
675 /* Exponent is decimal, even if string is a hex float. */
676 if (!ISDIGIT (*str))
677 {
678 if (DIGIT_SEP (*str))
679 SYNTAX_ERROR_AT (virtual_location,
680 "digit separator adjacent to exponent");
681 else
682 SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
683 }
684 do
685 {
686 seen_digit_sep = DIGIT_SEP (*str);
687 str++;
688 }
689 while (ISDIGIT (*str) || DIGIT_SEP (*str));
690 }
691 else if (radix == 16)
692 SYNTAX_ERROR_AT (virtual_location,
693 "hexadecimal floating constants require an exponent");
694
695 if (seen_digit_sep)
696 SYNTAX_ERROR_AT (virtual_location,
697 "digit separator outside digit sequence");
698
699 result = interpret_float_suffix (pfile, str, limit - str);
700 if (result == 0)
701 {
702 if (CPP_OPTION (pfile, user_literals))
703 {
704 if (ud_suffix)
705 *ud_suffix = (const char *) str;
706 result = CPP_N_LARGE | CPP_N_USERDEF;
707 }
708 else
709 {
710 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
711 "invalid suffix \"%.*s\" on floating constant",
712 (int) (limit - str), str);
713 return CPP_N_INVALID;
714 }
715 }
716
717 /* Traditional C didn't accept any floating suffixes. */
718 if (limit != str
719 && CPP_WTRADITIONAL (pfile)
720 && ! cpp_sys_macro_p (pfile))
721 cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
722 "traditional C rejects the \"%.*s\" suffix",
723 (int) (limit - str), str);
724
725 /* A suffix for double is a GCC extension via decimal float support.
726 If the suffix also specifies an imaginary value we'll catch that
727 later. */
728 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
729 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
730 "suffix for double constant is a GCC extension");
731
732 /* Radix must be 10 for decimal floats. */
733 if ((result & CPP_N_DFLOAT) && radix != 10)
734 {
735 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
736 "invalid suffix \"%.*s\" with hexadecimal floating constant",
737 (int) (limit - str), str);
738 return CPP_N_INVALID;
739 }
740
741 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
742 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
743 "fixed-point constants are a GCC extension");
744
745 if (result & CPP_N_DFLOAT)
746 {
747 if (CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, dfp_constants))
748 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
749 "decimal float constants are a C2X feature");
750 else if (CPP_OPTION (pfile, cpp_warn_c11_c2x_compat) > 0)
751 cpp_warning_with_line (pfile, CPP_W_C11_C2X_COMPAT,
752 virtual_location, 0,
753 "decimal float constants are a C2X feature");
754 }
755
756 result |= CPP_N_FLOATING;
757 }
758 else
759 {
760 result = interpret_int_suffix (pfile, str, limit - str);
761 if (result == 0)
762 {
763 if (CPP_OPTION (pfile, user_literals))
764 {
765 if (ud_suffix)
766 *ud_suffix = (const char *) str;
767 result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF;
768 }
769 else
770 {
771 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
772 "invalid suffix \"%.*s\" on integer constant",
773 (int) (limit - str), str);
774 return CPP_N_INVALID;
775 }
776 }
777
778 /* Traditional C only accepted the 'L' suffix.
779 Suppress warning about 'LL' with -Wno-long-long. */
780 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
781 {
782 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
783 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE
784 && CPP_OPTION (pfile, cpp_warn_long_long);
785
786 if (u_or_i || large)
787 cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
788 virtual_location, 0,
789 "traditional C rejects the \"%.*s\" suffix",
790 (int) (limit - str), str);
791 }
792
793 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
794 && CPP_OPTION (pfile, cpp_warn_long_long))
795 {
796 const char *message = CPP_OPTION (pfile, cplusplus)
797 ? N_("use of C++11 long long integer constant")
798 : N_("use of C99 long long integer constant");
799
800 if (CPP_OPTION (pfile, c99))
801 cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
802 0, message);
803 else
804 cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
805 virtual_location, 0, message);
806 }
807
808 result |= CPP_N_INTEGER;
809 }
810
811 syntax_ok:
812 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
813 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
814 "imaginary constants are a GCC extension");
815 if (radix == 2
816 && !CPP_OPTION (pfile, binary_constants)
817 && CPP_PEDANTIC (pfile))
818 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
819 CPP_OPTION (pfile, cplusplus)
820 ? N_("binary constants are a C++14 feature "
821 "or GCC extension")
822 : N_("binary constants are a GCC extension"));
823
824 if (radix == 10)
825 result |= CPP_N_DECIMAL;
826 else if (radix == 16)
827 result |= CPP_N_HEX;
828 else if (radix == 2)
829 result |= CPP_N_BINARY;
830 else
831 result |= CPP_N_OCTAL;
832
833 return result;
834
835 syntax_error:
836 return CPP_N_INVALID;
837 }
838
839 /* cpp_interpret_integer converts an integer constant into a cpp_num,
840 of precision options->precision.
841
842 We do not provide any interface for decimal->float conversion,
843 because the preprocessor doesn't need it and we don't want to
844 drag in GCC's floating point emulator. */
845 cpp_num
846 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
847 unsigned int type)
848 {
849 const uchar *p, *end;
850 cpp_num result;
851
852 result.low = 0;
853 result.high = 0;
854 result.unsignedp = !!(type & CPP_N_UNSIGNED);
855 result.overflow = false;
856
857 p = token->val.str.text;
858 end = p + token->val.str.len;
859
860 /* Common case of a single digit. */
861 if (token->val.str.len == 1)
862 result.low = p[0] - '0';
863 else
864 {
865 cpp_num_part max;
866 size_t precision = CPP_OPTION (pfile, precision);
867 unsigned int base = 10, c = 0;
868 bool overflow = false;
869
870 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
871 {
872 base = 8;
873 p++;
874 }
875 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
876 {
877 base = 16;
878 p += 2;
879 }
880 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
881 {
882 base = 2;
883 p += 2;
884 }
885
886 /* We can add a digit to numbers strictly less than this without
887 needing the precision and slowness of double integers. */
888 max = ~(cpp_num_part) 0;
889 if (precision < PART_PRECISION)
890 max >>= PART_PRECISION - precision;
891 max = (max - base + 1) / base + 1;
892
893 for (; p < end; p++)
894 {
895 c = *p;
896
897 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
898 c = hex_value (c);
899 else if (DIGIT_SEP (c))
900 continue;
901 else
902 break;
903
904 /* Strict inequality for when max is set to zero. */
905 if (result.low < max)
906 result.low = result.low * base + c;
907 else
908 {
909 result = append_digit (result, c, base, precision);
910 overflow |= result.overflow;
911 max = 0;
912 }
913 }
914
915 if (overflow && !(type & CPP_N_USERDEF))
916 cpp_error (pfile, CPP_DL_PEDWARN,
917 "integer constant is too large for its type");
918 /* If too big to be signed, consider it unsigned. Only warn for
919 decimal numbers. Traditional numbers were always signed (but
920 we still honor an explicit U suffix); but we only have
921 traditional semantics in directives. */
922 else if (!result.unsignedp
923 && !(CPP_OPTION (pfile, traditional)
924 && pfile->state.in_directive)
925 && !num_positive (result, precision))
926 {
927 /* This is for constants within the range of uintmax_t but
928 not that of intmax_t. For such decimal constants, a
929 diagnostic is required for C99 as the selected type must
930 be signed and not having a type is a constraint violation
931 (DR#298, TC3), so this must be a pedwarn. For C90,
932 unsigned long is specified to be used for a constant that
933 does not fit in signed long; if uintmax_t has the same
934 range as unsigned long this means only a warning is
935 appropriate here. C90 permits the preprocessor to use a
936 wider range than unsigned long in the compiler, so if
937 uintmax_t is wider than unsigned long no diagnostic is
938 required for such constants in preprocessor #if
939 expressions and the compiler will pedwarn for such
940 constants outside the range of unsigned long that reach
941 the compiler so a diagnostic is not required there
942 either; thus, pedwarn for C99 but use a plain warning for
943 C90. */
944 if (base == 10)
945 cpp_error (pfile, (CPP_OPTION (pfile, c99)
946 ? CPP_DL_PEDWARN
947 : CPP_DL_WARNING),
948 "integer constant is so large that it is unsigned");
949 result.unsignedp = true;
950 }
951 }
952
953 return result;
954 }
955
956 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
957 static cpp_num
958 append_digit (cpp_num num, int digit, int base, size_t precision)
959 {
960 cpp_num result;
961 unsigned int shift;
962 bool overflow;
963 cpp_num_part add_high, add_low;
964
965 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
966 need to worry about add_high overflowing. */
967 switch (base)
968 {
969 case 2:
970 shift = 1;
971 break;
972
973 case 16:
974 shift = 4;
975 break;
976
977 default:
978 shift = 3;
979 }
980 overflow = !!(num.high >> (PART_PRECISION - shift));
981 result.high = num.high << shift;
982 result.low = num.low << shift;
983 result.high |= num.low >> (PART_PRECISION - shift);
984 result.unsignedp = num.unsignedp;
985
986 if (base == 10)
987 {
988 add_low = num.low << 1;
989 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
990 }
991 else
992 add_high = add_low = 0;
993
994 if (add_low + digit < add_low)
995 add_high++;
996 add_low += digit;
997
998 if (result.low + add_low < result.low)
999 add_high++;
1000 if (result.high + add_high < result.high)
1001 overflow = true;
1002
1003 result.low += add_low;
1004 result.high += add_high;
1005 result.overflow = overflow;
1006
1007 /* The above code catches overflow of a cpp_num type. This catches
1008 overflow of the (possibly shorter) target precision. */
1009 num.low = result.low;
1010 num.high = result.high;
1011 result = num_trim (result, precision);
1012 if (!num_eq (result, num))
1013 result.overflow = true;
1014
1015 return result;
1016 }
1017
1018 /* Handle meeting "defined" in a preprocessor expression. */
1019 static cpp_num
1020 parse_defined (cpp_reader *pfile)
1021 {
1022 cpp_num result;
1023 int paren = 0;
1024 cpp_hashnode *node = 0;
1025 const cpp_token *token;
1026 cpp_context *initial_context = pfile->context;
1027
1028 /* Don't expand macros. */
1029 pfile->state.prevent_expansion++;
1030
1031 token = cpp_get_token (pfile);
1032 if (token->type == CPP_OPEN_PAREN)
1033 {
1034 paren = 1;
1035 token = cpp_get_token (pfile);
1036 }
1037
1038 if (token->type == CPP_NAME)
1039 {
1040 node = token->val.node.node;
1041 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
1042 {
1043 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
1044 node = 0;
1045 }
1046 }
1047 else
1048 {
1049 cpp_error (pfile, CPP_DL_ERROR,
1050 "operator \"defined\" requires an identifier");
1051 if (token->flags & NAMED_OP)
1052 {
1053 cpp_token op;
1054
1055 op.flags = 0;
1056 op.type = token->type;
1057 cpp_error (pfile, CPP_DL_ERROR,
1058 "(\"%s\" is an alternative token for \"%s\" in C++)",
1059 cpp_token_as_text (pfile, token),
1060 cpp_token_as_text (pfile, &op));
1061 }
1062 }
1063
1064 if (node)
1065 {
1066 if ((pfile->context != initial_context
1067 || initial_context != &pfile->base_context)
1068 && CPP_OPTION (pfile, warn_expansion_to_defined))
1069 cpp_pedwarning (pfile, CPP_W_EXPANSION_TO_DEFINED,
1070 "this use of \"defined\" may not be portable");
1071
1072 _cpp_mark_macro_used (node);
1073 _cpp_maybe_notify_macro_use (pfile, node);
1074
1075 /* A possible controlling macro of the form #if !defined ().
1076 _cpp_parse_expr checks there was no other junk on the line. */
1077 pfile->mi_ind_cmacro = node;
1078 }
1079
1080 pfile->state.prevent_expansion--;
1081
1082 /* Do not treat conditional macros as being defined. This is due to the
1083 powerpc port using conditional macros for 'vector', 'bool', and 'pixel'
1084 to act as conditional keywords. This messes up tests like #ifndef
1085 bool. */
1086 result.unsignedp = false;
1087 result.high = 0;
1088 result.overflow = false;
1089 result.low = node && _cpp_defined_macro_p (node);
1090 return result;
1091 }
1092
1093 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
1094 number or character constant, or the result of the "defined" or "#"
1095 operators). */
1096 static cpp_num
1097 eval_token (cpp_reader *pfile, const cpp_token *token,
1098 location_t virtual_location)
1099 {
1100 cpp_num result;
1101 unsigned int temp;
1102 int unsignedp = 0;
1103
1104 result.unsignedp = false;
1105 result.overflow = false;
1106
1107 switch (token->type)
1108 {
1109 case CPP_NUMBER:
1110 temp = cpp_classify_number (pfile, token, NULL, virtual_location);
1111 if (temp & CPP_N_USERDEF)
1112 cpp_error (pfile, CPP_DL_ERROR,
1113 "user-defined literal in preprocessor expression");
1114 switch (temp & CPP_N_CATEGORY)
1115 {
1116 case CPP_N_FLOATING:
1117 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1118 "floating constant in preprocessor expression");
1119 break;
1120 case CPP_N_INTEGER:
1121 if (!(temp & CPP_N_IMAGINARY))
1122 return cpp_interpret_integer (pfile, token, temp);
1123 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1124 "imaginary number in preprocessor expression");
1125 break;
1126
1127 case CPP_N_INVALID:
1128 /* Error already issued. */
1129 break;
1130 }
1131 result.high = result.low = 0;
1132 break;
1133
1134 case CPP_WCHAR:
1135 case CPP_CHAR:
1136 case CPP_CHAR16:
1137 case CPP_CHAR32:
1138 case CPP_UTF8CHAR:
1139 {
1140 cppchar_t cc = cpp_interpret_charconst (pfile, token,
1141 &temp, &unsignedp);
1142
1143 result.high = 0;
1144 result.low = cc;
1145 /* Sign-extend the result if necessary. */
1146 if (!unsignedp && (cppchar_signed_t) cc < 0)
1147 {
1148 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
1149 result.low |= ~(~(cpp_num_part) 0
1150 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
1151 result.high = ~(cpp_num_part) 0;
1152 result = num_trim (result, CPP_OPTION (pfile, precision));
1153 }
1154 }
1155 break;
1156
1157 case CPP_NAME:
1158 if (token->val.node.node == pfile->spec_nodes.n_defined)
1159 return parse_defined (pfile);
1160 else if (CPP_OPTION (pfile, cplusplus)
1161 && (token->val.node.node == pfile->spec_nodes.n_true
1162 || token->val.node.node == pfile->spec_nodes.n_false))
1163 {
1164 result.high = 0;
1165 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
1166 }
1167 else
1168 {
1169 result.high = 0;
1170 result.low = 0;
1171 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
1172 cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
1173 "\"%s\" is not defined, evaluates to 0",
1174 NODE_NAME (token->val.node.node));
1175 }
1176 break;
1177
1178 case CPP_HASH:
1179 if (!pfile->state.skipping)
1180 {
1181 /* A pedantic warning takes precedence over a deprecated
1182 warning here. */
1183 if (CPP_PEDANTIC (pfile))
1184 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1185 virtual_location, 0,
1186 "assertions are a GCC extension");
1187 else if (CPP_OPTION (pfile, cpp_warn_deprecated))
1188 cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
1189 "assertions are a deprecated extension");
1190 }
1191 _cpp_test_assertion (pfile, &temp);
1192 result.high = 0;
1193 result.low = temp;
1194 break;
1195
1196 default:
1197 abort ();
1198 }
1199
1200 result.unsignedp = !!unsignedp;
1201 return result;
1202 }
1203 \f
1204 /* Operator precedence and flags table.
1205
1206 After an operator is returned from the lexer, if it has priority less
1207 than the operator on the top of the stack, we reduce the stack by one
1208 operator and repeat the test. Since equal priorities do not reduce,
1209 this is naturally right-associative.
1210
1211 We handle left-associative operators by decrementing the priority of
1212 just-lexed operators by one, but retaining the priority of operators
1213 already on the stack.
1214
1215 The remaining cases are '(' and ')'. We handle '(' by skipping the
1216 reduction phase completely. ')' is given lower priority than
1217 everything else, including '(', effectively forcing a reduction of the
1218 parenthesized expression. If there is a matching '(', the routine
1219 reduce() exits immediately. If the normal exit route sees a ')', then
1220 there cannot have been a matching '(' and an error message is output.
1221
1222 The parser assumes all shifted operators require a left operand unless
1223 the flag NO_L_OPERAND is set. These semantics are automatic; any
1224 extra semantics need to be handled with operator-specific code. */
1225
1226 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1227 operand changes because of integer promotions. */
1228 #define NO_L_OPERAND (1 << 0)
1229 #define LEFT_ASSOC (1 << 1)
1230 #define CHECK_PROMOTION (1 << 2)
1231
1232 /* Operator to priority map. Must be in the same order as the first
1233 N entries of enum cpp_ttype. */
1234 static const struct cpp_operator
1235 {
1236 uchar prio;
1237 uchar flags;
1238 } optab[] =
1239 {
1240 /* EQ */ {0, 0}, /* Shouldn't happen. */
1241 /* NOT */ {16, NO_L_OPERAND},
1242 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1243 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1244 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1245 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1246 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1247 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1248 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1249 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
1250 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
1251 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
1252 /* RSHIFT */ {13, LEFT_ASSOC},
1253 /* LSHIFT */ {13, LEFT_ASSOC},
1254
1255 /* COMPL */ {16, NO_L_OPERAND},
1256 /* AND_AND */ {6, LEFT_ASSOC},
1257 /* OR_OR */ {5, LEFT_ASSOC},
1258 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1259 However, there are some special cases for these in reduce(). */
1260 /* QUERY */ {4, 0},
1261 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
1262 /* COMMA */ {4, LEFT_ASSOC},
1263 /* OPEN_PAREN */ {1, NO_L_OPERAND},
1264 /* CLOSE_PAREN */ {0, 0},
1265 /* EOF */ {0, 0},
1266 /* EQ_EQ */ {11, LEFT_ASSOC},
1267 /* NOT_EQ */ {11, LEFT_ASSOC},
1268 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1269 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1270 /* UPLUS */ {16, NO_L_OPERAND},
1271 /* UMINUS */ {16, NO_L_OPERAND}
1272 };
1273
1274 /* Parse and evaluate a C expression, reading from PFILE.
1275 Returns the truth value of the expression.
1276
1277 The implementation is an operator precedence parser, i.e. a
1278 bottom-up parser, using a stack for not-yet-reduced tokens.
1279
1280 The stack base is op_stack, and the current stack pointer is 'top'.
1281 There is a stack element for each operator (only), and the most
1282 recently pushed operator is 'top->op'. An operand (value) is
1283 stored in the 'value' field of the stack element of the operator
1284 that precedes it. */
1285 bool
1286 _cpp_parse_expr (cpp_reader *pfile, bool is_if)
1287 {
1288 struct op *top = pfile->op_stack;
1289 unsigned int lex_count;
1290 bool saw_leading_not, want_value = true;
1291 location_t virtual_location = 0;
1292
1293 pfile->state.skip_eval = 0;
1294
1295 /* Set up detection of #if ! defined(). */
1296 pfile->mi_ind_cmacro = 0;
1297 saw_leading_not = false;
1298 lex_count = 0;
1299
1300 /* Lowest priority operator prevents further reductions. */
1301 top->op = CPP_EOF;
1302
1303 for (;;)
1304 {
1305 struct op op;
1306
1307 lex_count++;
1308 op.token = cpp_get_token_with_location (pfile, &virtual_location);
1309 op.op = op.token->type;
1310 op.loc = virtual_location;
1311
1312 switch (op.op)
1313 {
1314 /* These tokens convert into values. */
1315 case CPP_NUMBER:
1316 case CPP_CHAR:
1317 case CPP_WCHAR:
1318 case CPP_CHAR16:
1319 case CPP_CHAR32:
1320 case CPP_UTF8CHAR:
1321 case CPP_NAME:
1322 case CPP_HASH:
1323 if (!want_value)
1324 SYNTAX_ERROR2_AT (op.loc,
1325 "missing binary operator before token \"%s\"",
1326 cpp_token_as_text (pfile, op.token));
1327 want_value = false;
1328 top->value = eval_token (pfile, op.token, op.loc);
1329 continue;
1330
1331 case CPP_NOT:
1332 saw_leading_not = lex_count == 1;
1333 break;
1334 case CPP_PLUS:
1335 if (want_value)
1336 op.op = CPP_UPLUS;
1337 break;
1338 case CPP_MINUS:
1339 if (want_value)
1340 op.op = CPP_UMINUS;
1341 break;
1342
1343 default:
1344 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
1345 SYNTAX_ERROR2_AT (op.loc,
1346 "token \"%s\" is not valid in preprocessor expressions",
1347 cpp_token_as_text (pfile, op.token));
1348 break;
1349 }
1350
1351 /* Check we have a value or operator as appropriate. */
1352 if (optab[op.op].flags & NO_L_OPERAND)
1353 {
1354 if (!want_value)
1355 SYNTAX_ERROR2_AT (op.loc,
1356 "missing binary operator before token \"%s\"",
1357 cpp_token_as_text (pfile, op.token));
1358 }
1359 else if (want_value)
1360 {
1361 /* We want a number (or expression) and haven't got one.
1362 Try to emit a specific diagnostic. */
1363 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
1364 SYNTAX_ERROR_AT (op.loc,
1365 "missing expression between '(' and ')'");
1366
1367 if (op.op == CPP_EOF && top->op == CPP_EOF)
1368 SYNTAX_ERROR2_AT (op.loc,
1369 "%s with no expression", is_if ? "#if" : "#elif");
1370
1371 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
1372 SYNTAX_ERROR2_AT (op.loc,
1373 "operator '%s' has no right operand",
1374 cpp_token_as_text (pfile, top->token));
1375 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
1376 /* Complain about missing paren during reduction. */;
1377 else
1378 SYNTAX_ERROR2_AT (op.loc,
1379 "operator '%s' has no left operand",
1380 cpp_token_as_text (pfile, op.token));
1381 }
1382
1383 top = reduce (pfile, top, op.op);
1384 if (!top)
1385 goto syntax_error;
1386
1387 if (op.op == CPP_EOF)
1388 break;
1389
1390 switch (op.op)
1391 {
1392 case CPP_CLOSE_PAREN:
1393 continue;
1394 case CPP_OR_OR:
1395 if (!num_zerop (top->value))
1396 pfile->state.skip_eval++;
1397 break;
1398 case CPP_AND_AND:
1399 case CPP_QUERY:
1400 if (num_zerop (top->value))
1401 pfile->state.skip_eval++;
1402 break;
1403 case CPP_COLON:
1404 if (top->op != CPP_QUERY)
1405 SYNTAX_ERROR_AT (op.loc,
1406 " ':' without preceding '?'");
1407 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
1408 pfile->state.skip_eval++;
1409 else
1410 pfile->state.skip_eval--;
1411 default:
1412 break;
1413 }
1414
1415 want_value = true;
1416
1417 /* Check for and handle stack overflow. */
1418 if (++top == pfile->op_limit)
1419 top = _cpp_expand_op_stack (pfile);
1420
1421 top->op = op.op;
1422 top->token = op.token;
1423 top->loc = op.loc;
1424 }
1425
1426 /* The controlling macro expression is only valid if we called lex 3
1427 times: <!> <defined expression> and <EOF>. push_conditional ()
1428 checks that we are at top-of-file. */
1429 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1430 pfile->mi_ind_cmacro = 0;
1431
1432 if (top != pfile->op_stack)
1433 {
1434 cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
1435 "unbalanced stack in %s",
1436 is_if ? "#if" : "#elif");
1437 syntax_error:
1438 return false; /* Return false on syntax error. */
1439 }
1440
1441 return !num_zerop (top->value);
1442 }
1443
1444 /* Reduce the operator / value stack if possible, in preparation for
1445 pushing operator OP. Returns NULL on error, otherwise the top of
1446 the stack. */
1447 static struct op *
1448 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1449 {
1450 unsigned int prio;
1451
1452 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1453 {
1454 bad_op:
1455 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1456 return 0;
1457 }
1458
1459 if (op == CPP_OPEN_PAREN)
1460 return top;
1461
1462 /* Decrement the priority of left-associative operators to force a
1463 reduction with operators of otherwise equal priority. */
1464 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1465 while (prio < optab[top->op].prio)
1466 {
1467 if (CPP_OPTION (pfile, warn_num_sign_change)
1468 && optab[top->op].flags & CHECK_PROMOTION)
1469 check_promotion (pfile, top);
1470
1471 switch (top->op)
1472 {
1473 case CPP_UPLUS:
1474 case CPP_UMINUS:
1475 case CPP_NOT:
1476 case CPP_COMPL:
1477 top[-1].value = num_unary_op (pfile, top->value, top->op);
1478 top[-1].loc = top->loc;
1479 break;
1480
1481 case CPP_PLUS:
1482 case CPP_MINUS:
1483 case CPP_RSHIFT:
1484 case CPP_LSHIFT:
1485 case CPP_COMMA:
1486 top[-1].value = num_binary_op (pfile, top[-1].value,
1487 top->value, top->op);
1488 top[-1].loc = top->loc;
1489 break;
1490
1491 case CPP_GREATER:
1492 case CPP_LESS:
1493 case CPP_GREATER_EQ:
1494 case CPP_LESS_EQ:
1495 top[-1].value
1496 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1497 top[-1].loc = top->loc;
1498 break;
1499
1500 case CPP_EQ_EQ:
1501 case CPP_NOT_EQ:
1502 top[-1].value
1503 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1504 top[-1].loc = top->loc;
1505 break;
1506
1507 case CPP_AND:
1508 case CPP_OR:
1509 case CPP_XOR:
1510 top[-1].value
1511 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1512 top[-1].loc = top->loc;
1513 break;
1514
1515 case CPP_MULT:
1516 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1517 top[-1].loc = top->loc;
1518 break;
1519
1520 case CPP_DIV:
1521 case CPP_MOD:
1522 top[-1].value = num_div_op (pfile, top[-1].value,
1523 top->value, top->op, top->loc);
1524 top[-1].loc = top->loc;
1525 break;
1526
1527 case CPP_OR_OR:
1528 top--;
1529 if (!num_zerop (top->value))
1530 pfile->state.skip_eval--;
1531 top->value.low = (!num_zerop (top->value)
1532 || !num_zerop (top[1].value));
1533 top->value.high = 0;
1534 top->value.unsignedp = false;
1535 top->value.overflow = false;
1536 top->loc = top[1].loc;
1537 continue;
1538
1539 case CPP_AND_AND:
1540 top--;
1541 if (num_zerop (top->value))
1542 pfile->state.skip_eval--;
1543 top->value.low = (!num_zerop (top->value)
1544 && !num_zerop (top[1].value));
1545 top->value.high = 0;
1546 top->value.unsignedp = false;
1547 top->value.overflow = false;
1548 top->loc = top[1].loc;
1549 continue;
1550
1551 case CPP_OPEN_PAREN:
1552 if (op != CPP_CLOSE_PAREN)
1553 {
1554 cpp_error_with_line (pfile, CPP_DL_ERROR,
1555 top->token->src_loc,
1556 0, "missing ')' in expression");
1557 return 0;
1558 }
1559 top--;
1560 top->value = top[1].value;
1561 top->loc = top[1].loc;
1562 return top;
1563
1564 case CPP_COLON:
1565 top -= 2;
1566 if (!num_zerop (top->value))
1567 {
1568 pfile->state.skip_eval--;
1569 top->value = top[1].value;
1570 top->loc = top[1].loc;
1571 }
1572 else
1573 {
1574 top->value = top[2].value;
1575 top->loc = top[2].loc;
1576 }
1577 top->value.unsignedp = (top[1].value.unsignedp
1578 || top[2].value.unsignedp);
1579 continue;
1580
1581 case CPP_QUERY:
1582 /* COMMA and COLON should not reduce a QUERY operator. */
1583 if (op == CPP_COMMA || op == CPP_COLON)
1584 return top;
1585 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1586 return 0;
1587
1588 default:
1589 goto bad_op;
1590 }
1591
1592 top--;
1593 if (top->value.overflow && !pfile->state.skip_eval)
1594 cpp_error (pfile, CPP_DL_PEDWARN,
1595 "integer overflow in preprocessor expression");
1596 }
1597
1598 if (op == CPP_CLOSE_PAREN)
1599 {
1600 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1601 return 0;
1602 }
1603
1604 return top;
1605 }
1606
1607 /* Returns the position of the old top of stack after expansion. */
1608 struct op *
1609 _cpp_expand_op_stack (cpp_reader *pfile)
1610 {
1611 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1612 size_t new_size = old_size * 2 + 20;
1613
1614 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1615 pfile->op_limit = pfile->op_stack + new_size;
1616
1617 return pfile->op_stack + old_size;
1618 }
1619
1620 /* Emits a warning if the effective sign of either operand of OP
1621 changes because of integer promotions. */
1622 static void
1623 check_promotion (cpp_reader *pfile, const struct op *op)
1624 {
1625 if (op->value.unsignedp == op[-1].value.unsignedp)
1626 return;
1627
1628 if (op->value.unsignedp)
1629 {
1630 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1631 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1632 "the left operand of \"%s\" changes sign when promoted",
1633 cpp_token_as_text (pfile, op->token));
1634 }
1635 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1636 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1637 "the right operand of \"%s\" changes sign when promoted",
1638 cpp_token_as_text (pfile, op->token));
1639 }
1640
1641 /* Clears the unused high order bits of the number pointed to by PNUM. */
1642 static cpp_num
1643 num_trim (cpp_num num, size_t precision)
1644 {
1645 if (precision > PART_PRECISION)
1646 {
1647 precision -= PART_PRECISION;
1648 if (precision < PART_PRECISION)
1649 num.high &= ((cpp_num_part) 1 << precision) - 1;
1650 }
1651 else
1652 {
1653 if (precision < PART_PRECISION)
1654 num.low &= ((cpp_num_part) 1 << precision) - 1;
1655 num.high = 0;
1656 }
1657
1658 return num;
1659 }
1660
1661 /* True iff A (presumed signed) >= 0. */
1662 static bool
1663 num_positive (cpp_num num, size_t precision)
1664 {
1665 if (precision > PART_PRECISION)
1666 {
1667 precision -= PART_PRECISION;
1668 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1669 }
1670
1671 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1672 }
1673
1674 /* Sign extend a number, with PRECISION significant bits and all
1675 others assumed clear, to fill out a cpp_num structure. */
1676 cpp_num
1677 cpp_num_sign_extend (cpp_num num, size_t precision)
1678 {
1679 if (!num.unsignedp)
1680 {
1681 if (precision > PART_PRECISION)
1682 {
1683 precision -= PART_PRECISION;
1684 if (precision < PART_PRECISION
1685 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1686 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1687 }
1688 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1689 {
1690 if (precision < PART_PRECISION)
1691 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1692 num.high = ~(cpp_num_part) 0;
1693 }
1694 }
1695
1696 return num;
1697 }
1698
1699 /* Returns the negative of NUM. */
1700 static cpp_num
1701 num_negate (cpp_num num, size_t precision)
1702 {
1703 cpp_num copy;
1704
1705 copy = num;
1706 num.high = ~num.high;
1707 num.low = ~num.low;
1708 if (++num.low == 0)
1709 num.high++;
1710 num = num_trim (num, precision);
1711 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1712
1713 return num;
1714 }
1715
1716 /* Returns true if A >= B. */
1717 static bool
1718 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1719 {
1720 bool unsignedp;
1721
1722 unsignedp = pa.unsignedp || pb.unsignedp;
1723
1724 if (!unsignedp)
1725 {
1726 /* Both numbers have signed type. If they are of different
1727 sign, the answer is the sign of A. */
1728 unsignedp = num_positive (pa, precision);
1729
1730 if (unsignedp != num_positive (pb, precision))
1731 return unsignedp;
1732
1733 /* Otherwise we can do an unsigned comparison. */
1734 }
1735
1736 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1737 }
1738
1739 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1740 static cpp_num
1741 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1742 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1743 {
1744 lhs.overflow = false;
1745 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1746
1747 /* As excess precision is zeroed, there is no need to num_trim () as
1748 these operations cannot introduce a set bit there. */
1749 if (op == CPP_AND)
1750 {
1751 lhs.low &= rhs.low;
1752 lhs.high &= rhs.high;
1753 }
1754 else if (op == CPP_OR)
1755 {
1756 lhs.low |= rhs.low;
1757 lhs.high |= rhs.high;
1758 }
1759 else
1760 {
1761 lhs.low ^= rhs.low;
1762 lhs.high ^= rhs.high;
1763 }
1764
1765 return lhs;
1766 }
1767
1768 /* Returns LHS OP RHS, where OP is an inequality. */
1769 static cpp_num
1770 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1771 enum cpp_ttype op)
1772 {
1773 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1774
1775 if (op == CPP_GREATER_EQ)
1776 lhs.low = gte;
1777 else if (op == CPP_LESS)
1778 lhs.low = !gte;
1779 else if (op == CPP_GREATER)
1780 lhs.low = gte && !num_eq (lhs, rhs);
1781 else /* CPP_LESS_EQ. */
1782 lhs.low = !gte || num_eq (lhs, rhs);
1783
1784 lhs.high = 0;
1785 lhs.overflow = false;
1786 lhs.unsignedp = false;
1787 return lhs;
1788 }
1789
1790 /* Returns LHS OP RHS, where OP is == or !=. */
1791 static cpp_num
1792 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1793 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1794 {
1795 /* Work around a 3.0.4 bug; see PR 6950. */
1796 bool eq = num_eq (lhs, rhs);
1797 if (op == CPP_NOT_EQ)
1798 eq = !eq;
1799 lhs.low = eq;
1800 lhs.high = 0;
1801 lhs.overflow = false;
1802 lhs.unsignedp = false;
1803 return lhs;
1804 }
1805
1806 /* Shift NUM, of width PRECISION, right by N bits. */
1807 static cpp_num
1808 num_rshift (cpp_num num, size_t precision, size_t n)
1809 {
1810 cpp_num_part sign_mask;
1811 bool x = num_positive (num, precision);
1812
1813 if (num.unsignedp || x)
1814 sign_mask = 0;
1815 else
1816 sign_mask = ~(cpp_num_part) 0;
1817
1818 if (n >= precision)
1819 num.high = num.low = sign_mask;
1820 else
1821 {
1822 /* Sign-extend. */
1823 if (precision < PART_PRECISION)
1824 num.high = sign_mask, num.low |= sign_mask << precision;
1825 else if (precision < 2 * PART_PRECISION)
1826 num.high |= sign_mask << (precision - PART_PRECISION);
1827
1828 if (n >= PART_PRECISION)
1829 {
1830 n -= PART_PRECISION;
1831 num.low = num.high;
1832 num.high = sign_mask;
1833 }
1834
1835 if (n)
1836 {
1837 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1838 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1839 }
1840 }
1841
1842 num = num_trim (num, precision);
1843 num.overflow = false;
1844 return num;
1845 }
1846
1847 /* Shift NUM, of width PRECISION, left by N bits. */
1848 static cpp_num
1849 num_lshift (cpp_num num, size_t precision, size_t n)
1850 {
1851 if (n >= precision)
1852 {
1853 num.overflow = !num.unsignedp && !num_zerop (num);
1854 num.high = num.low = 0;
1855 }
1856 else
1857 {
1858 cpp_num orig, maybe_orig;
1859 size_t m = n;
1860
1861 orig = num;
1862 if (m >= PART_PRECISION)
1863 {
1864 m -= PART_PRECISION;
1865 num.high = num.low;
1866 num.low = 0;
1867 }
1868 if (m)
1869 {
1870 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1871 num.low <<= m;
1872 }
1873 num = num_trim (num, precision);
1874
1875 if (num.unsignedp)
1876 num.overflow = false;
1877 else
1878 {
1879 maybe_orig = num_rshift (num, precision, n);
1880 num.overflow = !num_eq (orig, maybe_orig);
1881 }
1882 }
1883
1884 return num;
1885 }
1886
1887 /* The four unary operators: +, -, ! and ~. */
1888 static cpp_num
1889 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1890 {
1891 switch (op)
1892 {
1893 case CPP_UPLUS:
1894 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1895 cpp_warning (pfile, CPP_W_TRADITIONAL,
1896 "traditional C rejects the unary plus operator");
1897 num.overflow = false;
1898 break;
1899
1900 case CPP_UMINUS:
1901 num = num_negate (num, CPP_OPTION (pfile, precision));
1902 break;
1903
1904 case CPP_COMPL:
1905 num.high = ~num.high;
1906 num.low = ~num.low;
1907 num = num_trim (num, CPP_OPTION (pfile, precision));
1908 num.overflow = false;
1909 break;
1910
1911 default: /* case CPP_NOT: */
1912 num.low = num_zerop (num);
1913 num.high = 0;
1914 num.overflow = false;
1915 num.unsignedp = false;
1916 break;
1917 }
1918
1919 return num;
1920 }
1921
1922 /* The various binary operators. */
1923 static cpp_num
1924 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1925 {
1926 cpp_num result;
1927 size_t precision = CPP_OPTION (pfile, precision);
1928 size_t n;
1929
1930 switch (op)
1931 {
1932 /* Shifts. */
1933 case CPP_LSHIFT:
1934 case CPP_RSHIFT:
1935 if (!rhs.unsignedp && !num_positive (rhs, precision))
1936 {
1937 /* A negative shift is a positive shift the other way. */
1938 if (op == CPP_LSHIFT)
1939 op = CPP_RSHIFT;
1940 else
1941 op = CPP_LSHIFT;
1942 rhs = num_negate (rhs, precision);
1943 }
1944 if (rhs.high)
1945 n = ~0; /* Maximal. */
1946 else
1947 n = rhs.low;
1948 if (op == CPP_LSHIFT)
1949 lhs = num_lshift (lhs, precision, n);
1950 else
1951 lhs = num_rshift (lhs, precision, n);
1952 break;
1953
1954 /* Arithmetic. */
1955 case CPP_MINUS:
1956 result.low = lhs.low - rhs.low;
1957 result.high = lhs.high - rhs.high;
1958 if (result.low > lhs.low)
1959 result.high--;
1960 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1961 result.overflow = false;
1962
1963 result = num_trim (result, precision);
1964 if (!result.unsignedp)
1965 {
1966 bool lhsp = num_positive (lhs, precision);
1967 result.overflow = (lhsp != num_positive (rhs, precision)
1968 && lhsp != num_positive (result, precision));
1969 }
1970 return result;
1971
1972 case CPP_PLUS:
1973 result.low = lhs.low + rhs.low;
1974 result.high = lhs.high + rhs.high;
1975 if (result.low < lhs.low)
1976 result.high++;
1977 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1978 result.overflow = false;
1979
1980 result = num_trim (result, precision);
1981 if (!result.unsignedp)
1982 {
1983 bool lhsp = num_positive (lhs, precision);
1984 result.overflow = (lhsp == num_positive (rhs, precision)
1985 && lhsp != num_positive (result, precision));
1986 }
1987 return result;
1988
1989 /* Comma. */
1990 default: /* case CPP_COMMA: */
1991 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1992 || !pfile->state.skip_eval))
1993 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
1994 "comma operator in operand of #if");
1995 lhs = rhs;
1996 break;
1997 }
1998
1999 return lhs;
2000 }
2001
2002 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
2003 cannot overflow. */
2004 static cpp_num
2005 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
2006 {
2007 cpp_num result;
2008 cpp_num_part middle[2], temp;
2009
2010 result.low = LOW_PART (lhs) * LOW_PART (rhs);
2011 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
2012
2013 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
2014 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
2015
2016 temp = result.low;
2017 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
2018 if (result.low < temp)
2019 result.high++;
2020
2021 temp = result.low;
2022 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
2023 if (result.low < temp)
2024 result.high++;
2025
2026 result.high += HIGH_PART (middle[0]);
2027 result.high += HIGH_PART (middle[1]);
2028 result.unsignedp = true;
2029 result.overflow = false;
2030
2031 return result;
2032 }
2033
2034 /* Multiply two preprocessing numbers. */
2035 static cpp_num
2036 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
2037 {
2038 cpp_num result, temp;
2039 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
2040 bool overflow, negate = false;
2041 size_t precision = CPP_OPTION (pfile, precision);
2042
2043 /* Prepare for unsigned multiplication. */
2044 if (!unsignedp)
2045 {
2046 if (!num_positive (lhs, precision))
2047 negate = !negate, lhs = num_negate (lhs, precision);
2048 if (!num_positive (rhs, precision))
2049 negate = !negate, rhs = num_negate (rhs, precision);
2050 }
2051
2052 overflow = lhs.high && rhs.high;
2053 result = num_part_mul (lhs.low, rhs.low);
2054
2055 temp = num_part_mul (lhs.high, rhs.low);
2056 result.high += temp.low;
2057 if (temp.high)
2058 overflow = true;
2059
2060 temp = num_part_mul (lhs.low, rhs.high);
2061 result.high += temp.low;
2062 if (temp.high)
2063 overflow = true;
2064
2065 temp.low = result.low, temp.high = result.high;
2066 result = num_trim (result, precision);
2067 if (!num_eq (result, temp))
2068 overflow = true;
2069
2070 if (negate)
2071 result = num_negate (result, precision);
2072
2073 if (unsignedp)
2074 result.overflow = false;
2075 else
2076 result.overflow = overflow || (num_positive (result, precision) ^ !negate
2077 && !num_zerop (result));
2078 result.unsignedp = unsignedp;
2079
2080 return result;
2081 }
2082
2083 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
2084 or the remainder depending upon OP. LOCATION is the source location
2085 of this operator (for diagnostics). */
2086
2087 static cpp_num
2088 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
2089 location_t location)
2090 {
2091 cpp_num result, sub;
2092 cpp_num_part mask;
2093 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
2094 bool negate = false, lhs_neg = false;
2095 size_t i, precision = CPP_OPTION (pfile, precision);
2096
2097 /* Prepare for unsigned division. */
2098 if (!unsignedp)
2099 {
2100 if (!num_positive (lhs, precision))
2101 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
2102 if (!num_positive (rhs, precision))
2103 negate = !negate, rhs = num_negate (rhs, precision);
2104 }
2105
2106 /* Find the high bit. */
2107 if (rhs.high)
2108 {
2109 i = precision - 1;
2110 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
2111 for (; ; i--, mask >>= 1)
2112 if (rhs.high & mask)
2113 break;
2114 }
2115 else if (rhs.low)
2116 {
2117 if (precision > PART_PRECISION)
2118 i = precision - PART_PRECISION - 1;
2119 else
2120 i = precision - 1;
2121 mask = (cpp_num_part) 1 << i;
2122 for (; ; i--, mask >>= 1)
2123 if (rhs.low & mask)
2124 break;
2125 }
2126 else
2127 {
2128 if (!pfile->state.skip_eval)
2129 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
2130 "division by zero in #if");
2131 return lhs;
2132 }
2133
2134 /* First nonzero bit of RHS is bit I. Do naive division by
2135 shifting the RHS fully left, and subtracting from LHS if LHS is
2136 at least as big, and then repeating but with one less shift.
2137 This is not very efficient, but is easy to understand. */
2138
2139 rhs.unsignedp = true;
2140 lhs.unsignedp = true;
2141 i = precision - i - 1;
2142 sub = num_lshift (rhs, precision, i);
2143
2144 result.high = result.low = 0;
2145 for (;;)
2146 {
2147 if (num_greater_eq (lhs, sub, precision))
2148 {
2149 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
2150 if (i >= PART_PRECISION)
2151 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
2152 else
2153 result.low |= (cpp_num_part) 1 << i;
2154 }
2155 if (i-- == 0)
2156 break;
2157 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
2158 sub.high >>= 1;
2159 }
2160
2161 /* We divide so that the remainder has the sign of the LHS. */
2162 if (op == CPP_DIV)
2163 {
2164 result.unsignedp = unsignedp;
2165 result.overflow = false;
2166 if (!unsignedp)
2167 {
2168 if (negate)
2169 result = num_negate (result, precision);
2170 result.overflow = (num_positive (result, precision) ^ !negate
2171 && !num_zerop (result));
2172 }
2173
2174 return result;
2175 }
2176
2177 /* CPP_MOD. */
2178 lhs.unsignedp = unsignedp;
2179 lhs.overflow = false;
2180 if (lhs_neg)
2181 lhs = num_negate (lhs, precision);
2182
2183 return lhs;
2184 }
2185