middle-end: Disable section anchors for VAR_DECLs if -fdata-sections
[official-gcc.git] / libcpp / expr.c
blobdd5611dce0e9fd9e06e7538528f2053f94f2e5c8
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987-2021 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994.
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.
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.
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/>. */
19 #include "config.h"
20 #include "system.h"
21 #include "cpplib.h"
22 #include "internal.h"
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))
29 struct op
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;
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);
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);
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 *);
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))
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)
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)
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;
96 flags = 0;
97 f = d = l = w = q = i = fn = fnx = fn_bits = 0;
99 /* The following decimal float suffixes, from TR 24732:2009, TS
100 18661-2:2015 and C2X, are supported:
102 df, DF - _Decimal32.
103 dd, DD - _Decimal64.
104 dl, DL - _Decimal128.
106 The dN and DN suffixes for _DecimalN, and dNx and DNx for
107 _DecimalNx, defined in TS 18661-3:2015, are not supported.
109 Fixed-point suffixes, from TR 18037:2008, are supported. They
110 consist of three parts, in order:
112 (i) An optional u or U, for unsigned types.
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.
118 (iii) r or R, for _Fract types, or k or K, for _Accum types.
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:
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). */
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'))
140 bool uppercase = (*s == 'D');
141 switch (s[1])
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;
156 if (CPP_OPTION (pfile, ext_numeric_literals))
158 /* Recognize a fixed-point suffix. */
159 if (len != 0)
160 switch (s[len-1])
162 case 'k': case 'K': flags = CPP_N_ACCUM; break;
163 case 'r': case 'R': flags = CPP_N_FRACT; break;
164 default: break;
167 /* Continue processing a fixed-point suffix. The suffix is case
168 insensitive except for ll or LL. Order is significant. */
169 if (flags)
171 if (len == 1)
172 return flags;
173 len--;
175 if (*s == 'u' || *s == 'U')
177 flags |= CPP_N_UNSIGNED;
178 if (len == 1)
179 return flags;
180 len--;
181 s++;
184 switch (*s)
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;
205 /* Anything left at this point is invalid. */
206 return 0;
210 /* In any remaining valid suffix, the case and order don't matter. */
211 while (len--)
213 switch (s[0])
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)
223 f--;
224 while (len > 0
225 && s[1] >= '0'
226 && s[1] <= '9'
227 && fn_bits < CPP_FLOATN_MAX)
229 fn_bits = fn_bits * 10 + (s[1] - '0');
230 len--;
231 s++;
233 if (len > 0 && s[1] == 'x')
235 fnx++;
236 len--;
237 s++;
239 else
240 fn++;
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;
252 s++;
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;
272 if (i)
274 if (!CPP_OPTION (pfile, ext_numeric_literals))
275 return 0;
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;
288 if ((w || q) && !CPP_OPTION (pfile, ext_numeric_literals))
289 return 0;
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));
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)
306 return interpret_float_suffix (pfile, (const unsigned char *)s, len);
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)
315 size_t orig_len = len;
316 size_t u, l, i, z;
318 u = l = i = z = 0;
320 while (len--)
321 switch (s[len])
323 case 'z': case 'Z': z++; break;
324 case 'u': case 'U': u++; break;
325 case 'i': case 'I':
326 case 'j': case 'J': i++; break;
327 case 'l': case 'L': l++;
328 /* If there are two Ls, they must be adjacent and the same case. */
329 if (l == 2 && s[len] != s[len + 1])
330 return 0;
331 break;
332 default:
333 return 0;
336 if (l > 2 || u > 1 || i > 1 || z > 1)
337 return 0;
339 if (z)
341 if (l > 0 || i > 0)
342 return 0;
343 if (!CPP_OPTION (pfile, cplusplus))
344 return 0;
347 if (i)
349 if (!CPP_OPTION (pfile, ext_numeric_literals))
350 return 0;
352 /* In C++14 and up these suffixes are in the standard library, so treat
353 them as user-defined literals. */
354 if (CPP_OPTION (pfile, cplusplus)
355 && CPP_OPTION (pfile, lang) > CLK_CXX11
356 && s[0] == 'i'
357 && (orig_len == 1 || (orig_len == 2 && s[1] == 'l')))
358 return 0;
361 return ((i ? CPP_N_IMAGINARY : 0)
362 | (u ? CPP_N_UNSIGNED : 0)
363 | ((l == 0) ? CPP_N_SMALL
364 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE)
365 | (z ? CPP_N_SIZE_T : 0));
368 /* Return the classification flags for an int suffix. */
369 unsigned int
370 cpp_interpret_int_suffix (cpp_reader *pfile, const char *s, size_t len)
372 return interpret_int_suffix (pfile, (const unsigned char *)s, len);
375 /* Return the string type corresponding to the the input user-defined string
376 literal type. If the input type is not a user-defined string literal
377 type return the input type. */
378 enum cpp_ttype
379 cpp_userdef_string_remove_type (enum cpp_ttype type)
381 if (type == CPP_STRING_USERDEF)
382 return CPP_STRING;
383 else if (type == CPP_WSTRING_USERDEF)
384 return CPP_WSTRING;
385 else if (type == CPP_STRING16_USERDEF)
386 return CPP_STRING16;
387 else if (type == CPP_STRING32_USERDEF)
388 return CPP_STRING32;
389 else if (type == CPP_UTF8STRING_USERDEF)
390 return CPP_UTF8STRING;
391 else
392 return type;
395 /* Return the user-defined string literal type corresponding to the input
396 string type. If the input type is not a string type return the input
397 type. */
398 enum cpp_ttype
399 cpp_userdef_string_add_type (enum cpp_ttype type)
401 if (type == CPP_STRING)
402 return CPP_STRING_USERDEF;
403 else if (type == CPP_WSTRING)
404 return CPP_WSTRING_USERDEF;
405 else if (type == CPP_STRING16)
406 return CPP_STRING16_USERDEF;
407 else if (type == CPP_STRING32)
408 return CPP_STRING32_USERDEF;
409 else if (type == CPP_UTF8STRING)
410 return CPP_UTF8STRING_USERDEF;
411 else
412 return type;
415 /* Return the char type corresponding to the the input user-defined char
416 literal type. If the input type is not a user-defined char literal
417 type return the input type. */
418 enum cpp_ttype
419 cpp_userdef_char_remove_type (enum cpp_ttype type)
421 if (type == CPP_CHAR_USERDEF)
422 return CPP_CHAR;
423 else if (type == CPP_WCHAR_USERDEF)
424 return CPP_WCHAR;
425 else if (type == CPP_CHAR16_USERDEF)
426 return CPP_CHAR16;
427 else if (type == CPP_CHAR32_USERDEF)
428 return CPP_CHAR32;
429 else if (type == CPP_UTF8CHAR_USERDEF)
430 return CPP_UTF8CHAR;
431 else
432 return type;
435 /* Return the user-defined char literal type corresponding to the input
436 char type. If the input type is not a char type return the input
437 type. */
438 enum cpp_ttype
439 cpp_userdef_char_add_type (enum cpp_ttype type)
441 if (type == CPP_CHAR)
442 return CPP_CHAR_USERDEF;
443 else if (type == CPP_WCHAR)
444 return CPP_WCHAR_USERDEF;
445 else if (type == CPP_CHAR16)
446 return CPP_CHAR16_USERDEF;
447 else if (type == CPP_CHAR32)
448 return CPP_CHAR32_USERDEF;
449 else if (type == CPP_UTF8CHAR)
450 return CPP_UTF8CHAR_USERDEF;
451 else
452 return type;
455 /* Return true if the token type is a user-defined string literal. */
456 bool
457 cpp_userdef_string_p (enum cpp_ttype type)
459 if (type == CPP_STRING_USERDEF
460 || type == CPP_WSTRING_USERDEF
461 || type == CPP_STRING16_USERDEF
462 || type == CPP_STRING32_USERDEF
463 || type == CPP_UTF8STRING_USERDEF)
464 return true;
465 else
466 return false;
469 /* Return true if the token type is a user-defined char literal. */
470 bool
471 cpp_userdef_char_p (enum cpp_ttype type)
473 if (type == CPP_CHAR_USERDEF
474 || type == CPP_WCHAR_USERDEF
475 || type == CPP_CHAR16_USERDEF
476 || type == CPP_CHAR32_USERDEF
477 || type == CPP_UTF8CHAR_USERDEF)
478 return true;
479 else
480 return false;
483 /* Extract the suffix from a user-defined literal string or char. */
484 const char *
485 cpp_get_userdef_suffix (const cpp_token *tok)
487 unsigned int len = tok->val.str.len;
488 const char *text = (const char *)tok->val.str.text;
489 char delim;
490 unsigned int i;
491 for (i = 0; i < len; ++i)
492 if (text[i] == '\'' || text[i] == '"')
493 break;
494 if (i == len)
495 return text + len;
496 delim = text[i];
497 for (i = len; i > 0; --i)
498 if (text[i - 1] == delim)
499 break;
500 return text + i;
503 /* Categorize numeric constants according to their field (integer,
504 floating point, or invalid), radix (decimal, octal, hexadecimal),
505 and type suffixes.
507 TOKEN is the token that represents the numeric constant to
508 classify.
510 In C++0X if UD_SUFFIX is non null it will be assigned
511 any unrecognized suffix for a user-defined literal.
513 VIRTUAL_LOCATION is the virtual location for TOKEN. */
514 unsigned int
515 cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
516 const char **ud_suffix, location_t virtual_location)
518 const uchar *str = token->val.str.text;
519 const uchar *limit;
520 unsigned int max_digit, result, radix;
521 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
522 bool seen_digit;
523 bool seen_digit_sep;
525 if (ud_suffix)
526 *ud_suffix = NULL;
528 /* If the lexer has done its job, length one can only be a single
529 digit. Fast-path this very common case. */
530 if (token->val.str.len == 1)
531 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
533 limit = str + token->val.str.len;
534 float_flag = NOT_FLOAT;
535 max_digit = 0;
536 radix = 10;
537 seen_digit = false;
538 seen_digit_sep = false;
540 /* First, interpret the radix. */
541 if (*str == '0')
543 radix = 8;
544 str++;
546 /* Require at least one hex digit to classify it as hex. */
547 if (*str == 'x' || *str == 'X')
549 if (str[1] == '.' || ISXDIGIT (str[1]))
551 radix = 16;
552 str++;
554 else if (DIGIT_SEP (str[1]))
555 SYNTAX_ERROR_AT (virtual_location,
556 "digit separator after base indicator");
558 else if (*str == 'b' || *str == 'B')
560 if (str[1] == '0' || str[1] == '1')
562 radix = 2;
563 str++;
565 else if (DIGIT_SEP (str[1]))
566 SYNTAX_ERROR_AT (virtual_location,
567 "digit separator after base indicator");
571 /* Now scan for a well-formed integer or float. */
572 for (;;)
574 unsigned int c = *str++;
576 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
578 seen_digit_sep = false;
579 seen_digit = true;
580 c = hex_value (c);
581 if (c > max_digit)
582 max_digit = c;
584 else if (DIGIT_SEP (c))
586 if (seen_digit_sep)
587 SYNTAX_ERROR_AT (virtual_location, "adjacent digit separators");
588 seen_digit_sep = true;
590 else if (c == '.')
592 if (seen_digit_sep || DIGIT_SEP (*str))
593 SYNTAX_ERROR_AT (virtual_location,
594 "digit separator adjacent to decimal point");
595 seen_digit_sep = false;
596 if (float_flag == NOT_FLOAT)
597 float_flag = AFTER_POINT;
598 else
599 SYNTAX_ERROR_AT (virtual_location,
600 "too many decimal points in number");
602 else if ((radix <= 10 && (c == 'e' || c == 'E'))
603 || (radix == 16 && (c == 'p' || c == 'P')))
605 if (seen_digit_sep || DIGIT_SEP (*str))
606 SYNTAX_ERROR_AT (virtual_location,
607 "digit separator adjacent to exponent");
608 float_flag = AFTER_EXPON;
609 break;
611 else
613 /* Start of suffix. */
614 str--;
615 break;
619 if (seen_digit_sep && float_flag != AFTER_EXPON)
620 SYNTAX_ERROR_AT (virtual_location,
621 "digit separator outside digit sequence");
623 /* The suffix may be for decimal fixed-point constants without exponent. */
624 if (radix != 16 && float_flag == NOT_FLOAT)
626 result = interpret_float_suffix (pfile, str, limit - str);
627 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
629 result |= CPP_N_FLOATING;
630 /* We need to restore the radix to 10, if the radix is 8. */
631 if (radix == 8)
632 radix = 10;
634 if (CPP_PEDANTIC (pfile))
635 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
636 "fixed-point constants are a GCC extension");
637 goto syntax_ok;
639 else
640 result = 0;
643 if (float_flag != NOT_FLOAT && radix == 8)
644 radix = 10;
646 if (max_digit >= radix)
648 if (radix == 2)
649 SYNTAX_ERROR2_AT (virtual_location,
650 "invalid digit \"%c\" in binary constant", '0' + max_digit);
651 else
652 SYNTAX_ERROR2_AT (virtual_location,
653 "invalid digit \"%c\" in octal constant", '0' + max_digit);
656 if (float_flag != NOT_FLOAT)
658 if (radix == 2)
660 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
661 "invalid prefix \"0b\" for floating constant");
662 return CPP_N_INVALID;
665 if (radix == 16 && !seen_digit)
666 SYNTAX_ERROR_AT (virtual_location,
667 "no digits in hexadecimal floating constant");
669 if (radix == 16 && CPP_PEDANTIC (pfile)
670 && !CPP_OPTION (pfile, extended_numbers))
672 if (CPP_OPTION (pfile, cplusplus))
673 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
674 "use of C++17 hexadecimal floating constant");
675 else
676 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
677 "use of C99 hexadecimal floating constant");
680 if (float_flag == AFTER_EXPON)
682 if (*str == '+' || *str == '-')
683 str++;
685 /* Exponent is decimal, even if string is a hex float. */
686 if (!ISDIGIT (*str))
688 if (DIGIT_SEP (*str))
689 SYNTAX_ERROR_AT (virtual_location,
690 "digit separator adjacent to exponent");
691 else
692 SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
696 seen_digit_sep = DIGIT_SEP (*str);
697 str++;
699 while (ISDIGIT (*str) || DIGIT_SEP (*str));
701 else if (radix == 16)
702 SYNTAX_ERROR_AT (virtual_location,
703 "hexadecimal floating constants require an exponent");
705 if (seen_digit_sep)
706 SYNTAX_ERROR_AT (virtual_location,
707 "digit separator outside digit sequence");
709 result = interpret_float_suffix (pfile, str, limit - str);
710 if (result == 0)
712 if (CPP_OPTION (pfile, user_literals))
714 if (ud_suffix)
715 *ud_suffix = (const char *) str;
716 result = CPP_N_LARGE | CPP_N_USERDEF;
718 else
720 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
721 "invalid suffix \"%.*s\" on floating constant",
722 (int) (limit - str), str);
723 return CPP_N_INVALID;
727 /* Traditional C didn't accept any floating suffixes. */
728 if (limit != str
729 && CPP_WTRADITIONAL (pfile)
730 && ! cpp_sys_macro_p (pfile))
731 cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
732 "traditional C rejects the \"%.*s\" suffix",
733 (int) (limit - str), str);
735 /* A suffix for double is a GCC extension via decimal float support.
736 If the suffix also specifies an imaginary value we'll catch that
737 later. */
738 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
739 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
740 "suffix for double constant is a GCC extension");
742 /* Radix must be 10 for decimal floats. */
743 if ((result & CPP_N_DFLOAT) && radix != 10)
745 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
746 "invalid suffix \"%.*s\" with hexadecimal floating constant",
747 (int) (limit - str), str);
748 return CPP_N_INVALID;
751 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
752 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
753 "fixed-point constants are a GCC extension");
755 if (result & CPP_N_DFLOAT)
757 if (CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, dfp_constants))
758 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
759 "decimal float constants are a C2X feature");
760 else if (CPP_OPTION (pfile, cpp_warn_c11_c2x_compat) > 0)
761 cpp_warning_with_line (pfile, CPP_W_C11_C2X_COMPAT,
762 virtual_location, 0,
763 "decimal float constants are a C2X feature");
766 result |= CPP_N_FLOATING;
768 else
770 result = interpret_int_suffix (pfile, str, limit - str);
771 if (result == 0)
773 if (CPP_OPTION (pfile, user_literals))
775 if (ud_suffix)
776 *ud_suffix = (const char *) str;
777 result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF;
779 else
781 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
782 "invalid suffix \"%.*s\" on integer constant",
783 (int) (limit - str), str);
784 return CPP_N_INVALID;
788 /* Traditional C only accepted the 'L' suffix.
789 Suppress warning about 'LL' with -Wno-long-long. */
790 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
792 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
793 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE
794 && CPP_OPTION (pfile, cpp_warn_long_long);
796 if (u_or_i || large)
797 cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
798 virtual_location, 0,
799 "traditional C rejects the \"%.*s\" suffix",
800 (int) (limit - str), str);
803 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
804 && CPP_OPTION (pfile, cpp_warn_long_long))
806 const char *message = CPP_OPTION (pfile, cplusplus)
807 ? N_("use of C++11 long long integer constant")
808 : N_("use of C99 long long integer constant");
810 if (CPP_OPTION (pfile, c99))
811 cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
812 0, message);
813 else
814 cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
815 virtual_location, 0, message);
818 if ((result & CPP_N_SIZE_T) == CPP_N_SIZE_T
819 && !CPP_OPTION (pfile, size_t_literals))
821 const char *message = (result & CPP_N_UNSIGNED) == CPP_N_UNSIGNED
822 ? N_("use of C++23 %<size_t%> integer constant")
823 : N_("use of C++23 %<make_signed_t<size_t>%> integer constant");
824 cpp_warning_with_line (pfile, CPP_W_SIZE_T_LITERALS,
825 virtual_location, 0, message);
828 result |= CPP_N_INTEGER;
831 syntax_ok:
832 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
833 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
834 "imaginary constants are a GCC extension");
835 if (radix == 2)
837 if (!CPP_OPTION (pfile, binary_constants)
838 && CPP_PEDANTIC (pfile))
839 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
840 CPP_OPTION (pfile, cplusplus)
841 ? N_("binary constants are a C++14 feature "
842 "or GCC extension")
843 : N_("binary constants are a C2X feature "
844 "or GCC extension"));
845 else if (CPP_OPTION (pfile, cpp_warn_c11_c2x_compat) > 0)
846 cpp_warning_with_line (pfile, CPP_W_C11_C2X_COMPAT,
847 virtual_location, 0,
848 "binary constants are a C2X feature");
851 if (radix == 10)
852 result |= CPP_N_DECIMAL;
853 else if (radix == 16)
854 result |= CPP_N_HEX;
855 else if (radix == 2)
856 result |= CPP_N_BINARY;
857 else
858 result |= CPP_N_OCTAL;
860 return result;
862 syntax_error:
863 return CPP_N_INVALID;
866 /* cpp_interpret_integer converts an integer constant into a cpp_num,
867 of precision options->precision.
869 We do not provide any interface for decimal->float conversion,
870 because the preprocessor doesn't need it and we don't want to
871 drag in GCC's floating point emulator. */
872 cpp_num
873 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
874 unsigned int type)
876 const uchar *p, *end;
877 cpp_num result;
879 result.low = 0;
880 result.high = 0;
881 result.unsignedp = !!(type & CPP_N_UNSIGNED);
882 result.overflow = false;
884 p = token->val.str.text;
885 end = p + token->val.str.len;
887 /* Common case of a single digit. */
888 if (token->val.str.len == 1)
889 result.low = p[0] - '0';
890 else
892 cpp_num_part max;
893 size_t precision = CPP_OPTION (pfile, precision);
894 unsigned int base = 10, c = 0;
895 bool overflow = false;
897 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
899 base = 8;
900 p++;
902 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
904 base = 16;
905 p += 2;
907 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
909 base = 2;
910 p += 2;
913 /* We can add a digit to numbers strictly less than this without
914 needing the precision and slowness of double integers. */
915 max = ~(cpp_num_part) 0;
916 if (precision < PART_PRECISION)
917 max >>= PART_PRECISION - precision;
918 max = (max - base + 1) / base + 1;
920 for (; p < end; p++)
922 c = *p;
924 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
925 c = hex_value (c);
926 else if (DIGIT_SEP (c))
927 continue;
928 else
929 break;
931 /* Strict inequality for when max is set to zero. */
932 if (result.low < max)
933 result.low = result.low * base + c;
934 else
936 result = append_digit (result, c, base, precision);
937 overflow |= result.overflow;
938 max = 0;
942 if (overflow && !(type & CPP_N_USERDEF))
943 cpp_error (pfile, CPP_DL_PEDWARN,
944 "integer constant is too large for its type");
945 /* If too big to be signed, consider it unsigned. Only warn for
946 decimal numbers. Traditional numbers were always signed (but
947 we still honor an explicit U suffix); but we only have
948 traditional semantics in directives. */
949 else if (!result.unsignedp
950 && !(CPP_OPTION (pfile, traditional)
951 && pfile->state.in_directive)
952 && !num_positive (result, precision))
954 /* This is for constants within the range of uintmax_t but
955 not that of intmax_t. For such decimal constants, a
956 diagnostic is required for C99 as the selected type must
957 be signed and not having a type is a constraint violation
958 (DR#298, TC3), so this must be a pedwarn. For C90,
959 unsigned long is specified to be used for a constant that
960 does not fit in signed long; if uintmax_t has the same
961 range as unsigned long this means only a warning is
962 appropriate here. C90 permits the preprocessor to use a
963 wider range than unsigned long in the compiler, so if
964 uintmax_t is wider than unsigned long no diagnostic is
965 required for such constants in preprocessor #if
966 expressions and the compiler will pedwarn for such
967 constants outside the range of unsigned long that reach
968 the compiler so a diagnostic is not required there
969 either; thus, pedwarn for C99 but use a plain warning for
970 C90. */
971 if (base == 10)
972 cpp_error (pfile, (CPP_OPTION (pfile, c99)
973 ? CPP_DL_PEDWARN
974 : CPP_DL_WARNING),
975 "integer constant is so large that it is unsigned");
976 result.unsignedp = true;
980 return result;
983 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
984 static cpp_num
985 append_digit (cpp_num num, int digit, int base, size_t precision)
987 cpp_num result;
988 unsigned int shift;
989 bool overflow;
990 cpp_num_part add_high, add_low;
992 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
993 need to worry about add_high overflowing. */
994 switch (base)
996 case 2:
997 shift = 1;
998 break;
1000 case 16:
1001 shift = 4;
1002 break;
1004 default:
1005 shift = 3;
1007 overflow = !!(num.high >> (PART_PRECISION - shift));
1008 result.high = num.high << shift;
1009 result.low = num.low << shift;
1010 result.high |= num.low >> (PART_PRECISION - shift);
1011 result.unsignedp = num.unsignedp;
1013 if (base == 10)
1015 add_low = num.low << 1;
1016 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
1018 else
1019 add_high = add_low = 0;
1021 if (add_low + digit < add_low)
1022 add_high++;
1023 add_low += digit;
1025 if (result.low + add_low < result.low)
1026 add_high++;
1027 if (result.high + add_high < result.high)
1028 overflow = true;
1030 result.low += add_low;
1031 result.high += add_high;
1032 result.overflow = overflow;
1034 /* The above code catches overflow of a cpp_num type. This catches
1035 overflow of the (possibly shorter) target precision. */
1036 num.low = result.low;
1037 num.high = result.high;
1038 result = num_trim (result, precision);
1039 if (!num_eq (result, num))
1040 result.overflow = true;
1042 return result;
1045 /* Handle meeting "defined" in a preprocessor expression. */
1046 static cpp_num
1047 parse_defined (cpp_reader *pfile)
1049 cpp_num result;
1050 int paren = 0;
1051 cpp_hashnode *node = 0;
1052 const cpp_token *token;
1053 cpp_context *initial_context = pfile->context;
1055 /* Don't expand macros. */
1056 pfile->state.prevent_expansion++;
1058 token = cpp_get_token (pfile);
1059 if (token->type == CPP_OPEN_PAREN)
1061 paren = 1;
1062 token = cpp_get_token (pfile);
1065 if (token->type == CPP_NAME)
1067 node = token->val.node.node;
1068 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
1070 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
1071 node = 0;
1074 else
1076 cpp_error (pfile, CPP_DL_ERROR,
1077 "operator \"defined\" requires an identifier");
1078 if (token->flags & NAMED_OP)
1080 cpp_token op;
1082 op.flags = 0;
1083 op.type = token->type;
1084 cpp_error (pfile, CPP_DL_ERROR,
1085 "(\"%s\" is an alternative token for \"%s\" in C++)",
1086 cpp_token_as_text (pfile, token),
1087 cpp_token_as_text (pfile, &op));
1091 bool is_defined = false;
1092 if (node)
1094 if ((pfile->context != initial_context
1095 || initial_context != &pfile->base_context)
1096 && CPP_OPTION (pfile, warn_expansion_to_defined))
1097 cpp_pedwarning (pfile, CPP_W_EXPANSION_TO_DEFINED,
1098 "this use of \"defined\" may not be portable");
1099 is_defined = _cpp_defined_macro_p (node);
1100 if (!_cpp_maybe_notify_macro_use (pfile, node, token->src_loc))
1101 /* It wasn't a macro after all. */
1102 is_defined = false;
1103 _cpp_mark_macro_used (node);
1105 /* A possible controlling macro of the form #if !defined ().
1106 _cpp_parse_expr checks there was no other junk on the line. */
1107 pfile->mi_ind_cmacro = node;
1110 pfile->state.prevent_expansion--;
1112 /* Do not treat conditional macros as being defined. This is due to the
1113 powerpc port using conditional macros for 'vector', 'bool', and 'pixel'
1114 to act as conditional keywords. This messes up tests like #ifndef
1115 bool. */
1116 result.unsignedp = false;
1117 result.high = 0;
1118 result.overflow = false;
1119 result.low = is_defined;
1120 return result;
1123 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
1124 number or character constant, or the result of the "defined" or "#"
1125 operators). */
1126 static cpp_num
1127 eval_token (cpp_reader *pfile, const cpp_token *token,
1128 location_t virtual_location)
1130 cpp_num result;
1131 unsigned int temp;
1132 int unsignedp = 0;
1134 result.unsignedp = false;
1135 result.overflow = false;
1137 switch (token->type)
1139 case CPP_NUMBER:
1140 temp = cpp_classify_number (pfile, token, NULL, virtual_location);
1141 if (temp & CPP_N_USERDEF)
1142 cpp_error (pfile, CPP_DL_ERROR,
1143 "user-defined literal in preprocessor expression");
1144 switch (temp & CPP_N_CATEGORY)
1146 case CPP_N_FLOATING:
1147 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1148 "floating constant in preprocessor expression");
1149 break;
1150 case CPP_N_INTEGER:
1151 if (!(temp & CPP_N_IMAGINARY))
1152 return cpp_interpret_integer (pfile, token, temp);
1153 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1154 "imaginary number in preprocessor expression");
1155 break;
1157 case CPP_N_INVALID:
1158 /* Error already issued. */
1159 break;
1161 result.high = result.low = 0;
1162 break;
1164 case CPP_WCHAR:
1165 case CPP_CHAR:
1166 case CPP_CHAR16:
1167 case CPP_CHAR32:
1168 case CPP_UTF8CHAR:
1170 cppchar_t cc = cpp_interpret_charconst (pfile, token,
1171 &temp, &unsignedp);
1173 result.high = 0;
1174 result.low = cc;
1175 /* Sign-extend the result if necessary. */
1176 if (!unsignedp && (cppchar_signed_t) cc < 0)
1178 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
1179 result.low |= ~(~(cpp_num_part) 0
1180 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
1181 result.high = ~(cpp_num_part) 0;
1182 result = num_trim (result, CPP_OPTION (pfile, precision));
1185 break;
1187 case CPP_NAME:
1188 if (token->val.node.node == pfile->spec_nodes.n_defined)
1189 return parse_defined (pfile);
1190 else if (CPP_OPTION (pfile, cplusplus)
1191 && (token->val.node.node == pfile->spec_nodes.n_true
1192 || token->val.node.node == pfile->spec_nodes.n_false))
1194 result.high = 0;
1195 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
1197 else
1199 result.high = 0;
1200 result.low = 0;
1201 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
1202 cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
1203 "\"%s\" is not defined, evaluates to 0",
1204 NODE_NAME (token->val.node.node));
1206 break;
1208 case CPP_HASH:
1209 if (!pfile->state.skipping)
1211 /* A pedantic warning takes precedence over a deprecated
1212 warning here. */
1213 if (CPP_PEDANTIC (pfile))
1214 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1215 virtual_location, 0,
1216 "assertions are a GCC extension");
1217 else if (CPP_OPTION (pfile, cpp_warn_deprecated))
1218 cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
1219 "assertions are a deprecated extension");
1221 _cpp_test_assertion (pfile, &temp);
1222 result.high = 0;
1223 result.low = temp;
1224 break;
1226 default:
1227 abort ();
1230 result.unsignedp = !!unsignedp;
1231 return result;
1234 /* Operator precedence and flags table.
1236 After an operator is returned from the lexer, if it has priority less
1237 than the operator on the top of the stack, we reduce the stack by one
1238 operator and repeat the test. Since equal priorities do not reduce,
1239 this is naturally right-associative.
1241 We handle left-associative operators by decrementing the priority of
1242 just-lexed operators by one, but retaining the priority of operators
1243 already on the stack.
1245 The remaining cases are '(' and ')'. We handle '(' by skipping the
1246 reduction phase completely. ')' is given lower priority than
1247 everything else, including '(', effectively forcing a reduction of the
1248 parenthesized expression. If there is a matching '(', the routine
1249 reduce() exits immediately. If the normal exit route sees a ')', then
1250 there cannot have been a matching '(' and an error message is output.
1252 The parser assumes all shifted operators require a left operand unless
1253 the flag NO_L_OPERAND is set. These semantics are automatic; any
1254 extra semantics need to be handled with operator-specific code. */
1256 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1257 operand changes because of integer promotions. */
1258 #define NO_L_OPERAND (1 << 0)
1259 #define LEFT_ASSOC (1 << 1)
1260 #define CHECK_PROMOTION (1 << 2)
1262 /* Operator to priority map. Must be in the same order as the first
1263 N entries of enum cpp_ttype. */
1264 static const struct cpp_operator
1266 uchar prio;
1267 uchar flags;
1268 } optab[] =
1270 /* EQ */ {0, 0}, /* Shouldn't happen. */
1271 /* NOT */ {16, NO_L_OPERAND},
1272 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1273 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1274 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1275 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1276 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1277 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1278 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1279 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
1280 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
1281 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
1282 /* RSHIFT */ {13, LEFT_ASSOC},
1283 /* LSHIFT */ {13, LEFT_ASSOC},
1285 /* COMPL */ {16, NO_L_OPERAND},
1286 /* AND_AND */ {6, LEFT_ASSOC},
1287 /* OR_OR */ {5, LEFT_ASSOC},
1288 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1289 However, there are some special cases for these in reduce(). */
1290 /* QUERY */ {4, 0},
1291 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
1292 /* COMMA */ {4, LEFT_ASSOC},
1293 /* OPEN_PAREN */ {1, NO_L_OPERAND},
1294 /* CLOSE_PAREN */ {0, 0},
1295 /* EOF */ {0, 0},
1296 /* EQ_EQ */ {11, LEFT_ASSOC},
1297 /* NOT_EQ */ {11, LEFT_ASSOC},
1298 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1299 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1300 /* UPLUS */ {16, NO_L_OPERAND},
1301 /* UMINUS */ {16, NO_L_OPERAND}
1304 /* Parse and evaluate a C expression, reading from PFILE.
1305 Returns the truth value of the expression.
1307 The implementation is an operator precedence parser, i.e. a
1308 bottom-up parser, using a stack for not-yet-reduced tokens.
1310 The stack base is op_stack, and the current stack pointer is 'top'.
1311 There is a stack element for each operator (only), and the most
1312 recently pushed operator is 'top->op'. An operand (value) is
1313 stored in the 'value' field of the stack element of the operator
1314 that precedes it. */
1315 bool
1316 _cpp_parse_expr (cpp_reader *pfile, bool is_if)
1318 struct op *top = pfile->op_stack;
1319 unsigned int lex_count;
1320 bool saw_leading_not, want_value = true;
1321 location_t virtual_location = 0;
1323 pfile->state.skip_eval = 0;
1325 /* Set up detection of #if ! defined(). */
1326 pfile->mi_ind_cmacro = 0;
1327 saw_leading_not = false;
1328 lex_count = 0;
1330 /* Lowest priority operator prevents further reductions. */
1331 top->op = CPP_EOF;
1333 for (;;)
1335 struct op op;
1337 lex_count++;
1338 op.token = cpp_get_token_with_location (pfile, &virtual_location);
1339 op.op = op.token->type;
1340 op.loc = virtual_location;
1342 switch (op.op)
1344 /* These tokens convert into values. */
1345 case CPP_NUMBER:
1346 case CPP_CHAR:
1347 case CPP_WCHAR:
1348 case CPP_CHAR16:
1349 case CPP_CHAR32:
1350 case CPP_UTF8CHAR:
1351 case CPP_NAME:
1352 case CPP_HASH:
1353 if (!want_value)
1354 SYNTAX_ERROR2_AT (op.loc,
1355 "missing binary operator before token \"%s\"",
1356 cpp_token_as_text (pfile, op.token));
1357 want_value = false;
1358 top->value = eval_token (pfile, op.token, op.loc);
1359 continue;
1361 case CPP_NOT:
1362 saw_leading_not = lex_count == 1;
1363 break;
1364 case CPP_PLUS:
1365 if (want_value)
1366 op.op = CPP_UPLUS;
1367 break;
1368 case CPP_MINUS:
1369 if (want_value)
1370 op.op = CPP_UMINUS;
1371 break;
1373 default:
1374 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
1375 SYNTAX_ERROR2_AT (op.loc,
1376 "token \"%s\" is not valid in preprocessor expressions",
1377 cpp_token_as_text (pfile, op.token));
1378 break;
1381 /* Check we have a value or operator as appropriate. */
1382 if (optab[op.op].flags & NO_L_OPERAND)
1384 if (!want_value)
1385 SYNTAX_ERROR2_AT (op.loc,
1386 "missing binary operator before token \"%s\"",
1387 cpp_token_as_text (pfile, op.token));
1389 else if (want_value)
1391 /* We want a number (or expression) and haven't got one.
1392 Try to emit a specific diagnostic. */
1393 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
1394 SYNTAX_ERROR_AT (op.loc,
1395 "missing expression between '(' and ')'");
1397 if (op.op == CPP_EOF && top->op == CPP_EOF)
1398 SYNTAX_ERROR2_AT (op.loc,
1399 "%s with no expression", is_if ? "#if" : "#elif");
1401 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
1402 SYNTAX_ERROR2_AT (op.loc,
1403 "operator '%s' has no right operand",
1404 cpp_token_as_text (pfile, top->token));
1405 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
1406 /* Complain about missing paren during reduction. */;
1407 else
1408 SYNTAX_ERROR2_AT (op.loc,
1409 "operator '%s' has no left operand",
1410 cpp_token_as_text (pfile, op.token));
1413 top = reduce (pfile, top, op.op);
1414 if (!top)
1415 goto syntax_error;
1417 if (op.op == CPP_EOF)
1418 break;
1420 switch (op.op)
1422 case CPP_CLOSE_PAREN:
1423 continue;
1424 case CPP_OR_OR:
1425 if (!num_zerop (top->value))
1426 pfile->state.skip_eval++;
1427 break;
1428 case CPP_AND_AND:
1429 case CPP_QUERY:
1430 if (num_zerop (top->value))
1431 pfile->state.skip_eval++;
1432 break;
1433 case CPP_COLON:
1434 if (top->op != CPP_QUERY)
1435 SYNTAX_ERROR_AT (op.loc,
1436 " ':' without preceding '?'");
1437 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
1438 pfile->state.skip_eval++;
1439 else
1440 pfile->state.skip_eval--;
1441 default:
1442 break;
1445 want_value = true;
1447 /* Check for and handle stack overflow. */
1448 if (++top == pfile->op_limit)
1449 top = _cpp_expand_op_stack (pfile);
1451 top->op = op.op;
1452 top->token = op.token;
1453 top->loc = op.loc;
1456 /* The controlling macro expression is only valid if we called lex 3
1457 times: <!> <defined expression> and <EOF>. push_conditional ()
1458 checks that we are at top-of-file. */
1459 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1460 pfile->mi_ind_cmacro = 0;
1462 if (top != pfile->op_stack)
1464 cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
1465 "unbalanced stack in %s",
1466 is_if ? "#if" : "#elif");
1467 syntax_error:
1468 return false; /* Return false on syntax error. */
1471 return !num_zerop (top->value);
1474 /* Reduce the operator / value stack if possible, in preparation for
1475 pushing operator OP. Returns NULL on error, otherwise the top of
1476 the stack. */
1477 static struct op *
1478 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1480 unsigned int prio;
1482 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1484 bad_op:
1485 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1486 return 0;
1489 if (op == CPP_OPEN_PAREN)
1490 return top;
1492 /* Decrement the priority of left-associative operators to force a
1493 reduction with operators of otherwise equal priority. */
1494 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1495 while (prio < optab[top->op].prio)
1497 if (CPP_OPTION (pfile, warn_num_sign_change)
1498 && optab[top->op].flags & CHECK_PROMOTION)
1499 check_promotion (pfile, top);
1501 switch (top->op)
1503 case CPP_UPLUS:
1504 case CPP_UMINUS:
1505 case CPP_NOT:
1506 case CPP_COMPL:
1507 top[-1].value = num_unary_op (pfile, top->value, top->op);
1508 top[-1].loc = top->loc;
1509 break;
1511 case CPP_PLUS:
1512 case CPP_MINUS:
1513 case CPP_RSHIFT:
1514 case CPP_LSHIFT:
1515 case CPP_COMMA:
1516 top[-1].value = num_binary_op (pfile, top[-1].value,
1517 top->value, top->op);
1518 top[-1].loc = top->loc;
1519 break;
1521 case CPP_GREATER:
1522 case CPP_LESS:
1523 case CPP_GREATER_EQ:
1524 case CPP_LESS_EQ:
1525 top[-1].value
1526 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1527 top[-1].loc = top->loc;
1528 break;
1530 case CPP_EQ_EQ:
1531 case CPP_NOT_EQ:
1532 top[-1].value
1533 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1534 top[-1].loc = top->loc;
1535 break;
1537 case CPP_AND:
1538 case CPP_OR:
1539 case CPP_XOR:
1540 top[-1].value
1541 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1542 top[-1].loc = top->loc;
1543 break;
1545 case CPP_MULT:
1546 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1547 top[-1].loc = top->loc;
1548 break;
1550 case CPP_DIV:
1551 case CPP_MOD:
1552 top[-1].value = num_div_op (pfile, top[-1].value,
1553 top->value, top->op, top->loc);
1554 top[-1].loc = top->loc;
1555 break;
1557 case CPP_OR_OR:
1558 top--;
1559 if (!num_zerop (top->value))
1560 pfile->state.skip_eval--;
1561 top->value.low = (!num_zerop (top->value)
1562 || !num_zerop (top[1].value));
1563 top->value.high = 0;
1564 top->value.unsignedp = false;
1565 top->value.overflow = false;
1566 top->loc = top[1].loc;
1567 continue;
1569 case CPP_AND_AND:
1570 top--;
1571 if (num_zerop (top->value))
1572 pfile->state.skip_eval--;
1573 top->value.low = (!num_zerop (top->value)
1574 && !num_zerop (top[1].value));
1575 top->value.high = 0;
1576 top->value.unsignedp = false;
1577 top->value.overflow = false;
1578 top->loc = top[1].loc;
1579 continue;
1581 case CPP_OPEN_PAREN:
1582 if (op != CPP_CLOSE_PAREN)
1584 cpp_error_with_line (pfile, CPP_DL_ERROR,
1585 top->token->src_loc,
1586 0, "missing ')' in expression");
1587 return 0;
1589 top--;
1590 top->value = top[1].value;
1591 top->loc = top[1].loc;
1592 return top;
1594 case CPP_COLON:
1595 top -= 2;
1596 if (!num_zerop (top->value))
1598 pfile->state.skip_eval--;
1599 top->value = top[1].value;
1600 top->loc = top[1].loc;
1602 else
1604 top->value = top[2].value;
1605 top->loc = top[2].loc;
1607 top->value.unsignedp = (top[1].value.unsignedp
1608 || top[2].value.unsignedp);
1609 continue;
1611 case CPP_QUERY:
1612 /* COMMA and COLON should not reduce a QUERY operator. */
1613 if (op == CPP_COMMA || op == CPP_COLON)
1614 return top;
1615 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1616 return 0;
1618 default:
1619 goto bad_op;
1622 top--;
1623 if (top->value.overflow && !pfile->state.skip_eval)
1624 cpp_error (pfile, CPP_DL_PEDWARN,
1625 "integer overflow in preprocessor expression");
1628 if (op == CPP_CLOSE_PAREN)
1630 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1631 return 0;
1634 return top;
1637 /* Returns the position of the old top of stack after expansion. */
1638 struct op *
1639 _cpp_expand_op_stack (cpp_reader *pfile)
1641 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1642 size_t new_size = old_size * 2 + 20;
1644 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1645 pfile->op_limit = pfile->op_stack + new_size;
1647 return pfile->op_stack + old_size;
1650 /* Emits a warning if the effective sign of either operand of OP
1651 changes because of integer promotions. */
1652 static void
1653 check_promotion (cpp_reader *pfile, const struct op *op)
1655 if (op->value.unsignedp == op[-1].value.unsignedp)
1656 return;
1658 if (op->value.unsignedp)
1660 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1661 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1662 "the left operand of \"%s\" changes sign when promoted",
1663 cpp_token_as_text (pfile, op->token));
1665 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1666 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1667 "the right operand of \"%s\" changes sign when promoted",
1668 cpp_token_as_text (pfile, op->token));
1671 /* Clears the unused high order bits of the number pointed to by PNUM. */
1672 static cpp_num
1673 num_trim (cpp_num num, size_t precision)
1675 if (precision > PART_PRECISION)
1677 precision -= PART_PRECISION;
1678 if (precision < PART_PRECISION)
1679 num.high &= ((cpp_num_part) 1 << precision) - 1;
1681 else
1683 if (precision < PART_PRECISION)
1684 num.low &= ((cpp_num_part) 1 << precision) - 1;
1685 num.high = 0;
1688 return num;
1691 /* True iff A (presumed signed) >= 0. */
1692 static bool
1693 num_positive (cpp_num num, size_t precision)
1695 if (precision > PART_PRECISION)
1697 precision -= PART_PRECISION;
1698 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1701 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1704 /* Sign extend a number, with PRECISION significant bits and all
1705 others assumed clear, to fill out a cpp_num structure. */
1706 cpp_num
1707 cpp_num_sign_extend (cpp_num num, size_t precision)
1709 if (!num.unsignedp)
1711 if (precision > PART_PRECISION)
1713 precision -= PART_PRECISION;
1714 if (precision < PART_PRECISION
1715 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1716 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1718 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1720 if (precision < PART_PRECISION)
1721 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1722 num.high = ~(cpp_num_part) 0;
1726 return num;
1729 /* Returns the negative of NUM. */
1730 static cpp_num
1731 num_negate (cpp_num num, size_t precision)
1733 cpp_num copy;
1735 copy = num;
1736 num.high = ~num.high;
1737 num.low = ~num.low;
1738 if (++num.low == 0)
1739 num.high++;
1740 num = num_trim (num, precision);
1741 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1743 return num;
1746 /* Returns true if A >= B. */
1747 static bool
1748 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1750 bool unsignedp;
1752 unsignedp = pa.unsignedp || pb.unsignedp;
1754 if (!unsignedp)
1756 /* Both numbers have signed type. If they are of different
1757 sign, the answer is the sign of A. */
1758 unsignedp = num_positive (pa, precision);
1760 if (unsignedp != num_positive (pb, precision))
1761 return unsignedp;
1763 /* Otherwise we can do an unsigned comparison. */
1766 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1769 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1770 static cpp_num
1771 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1772 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1774 lhs.overflow = false;
1775 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1777 /* As excess precision is zeroed, there is no need to num_trim () as
1778 these operations cannot introduce a set bit there. */
1779 if (op == CPP_AND)
1781 lhs.low &= rhs.low;
1782 lhs.high &= rhs.high;
1784 else if (op == CPP_OR)
1786 lhs.low |= rhs.low;
1787 lhs.high |= rhs.high;
1789 else
1791 lhs.low ^= rhs.low;
1792 lhs.high ^= rhs.high;
1795 return lhs;
1798 /* Returns LHS OP RHS, where OP is an inequality. */
1799 static cpp_num
1800 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1801 enum cpp_ttype op)
1803 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1805 if (op == CPP_GREATER_EQ)
1806 lhs.low = gte;
1807 else if (op == CPP_LESS)
1808 lhs.low = !gte;
1809 else if (op == CPP_GREATER)
1810 lhs.low = gte && !num_eq (lhs, rhs);
1811 else /* CPP_LESS_EQ. */
1812 lhs.low = !gte || num_eq (lhs, rhs);
1814 lhs.high = 0;
1815 lhs.overflow = false;
1816 lhs.unsignedp = false;
1817 return lhs;
1820 /* Returns LHS OP RHS, where OP is == or !=. */
1821 static cpp_num
1822 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1823 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1825 /* Work around a 3.0.4 bug; see PR 6950. */
1826 bool eq = num_eq (lhs, rhs);
1827 if (op == CPP_NOT_EQ)
1828 eq = !eq;
1829 lhs.low = eq;
1830 lhs.high = 0;
1831 lhs.overflow = false;
1832 lhs.unsignedp = false;
1833 return lhs;
1836 /* Shift NUM, of width PRECISION, right by N bits. */
1837 static cpp_num
1838 num_rshift (cpp_num num, size_t precision, size_t n)
1840 cpp_num_part sign_mask;
1841 bool x = num_positive (num, precision);
1843 if (num.unsignedp || x)
1844 sign_mask = 0;
1845 else
1846 sign_mask = ~(cpp_num_part) 0;
1848 if (n >= precision)
1849 num.high = num.low = sign_mask;
1850 else
1852 /* Sign-extend. */
1853 if (precision < PART_PRECISION)
1854 num.high = sign_mask, num.low |= sign_mask << precision;
1855 else if (precision < 2 * PART_PRECISION)
1856 num.high |= sign_mask << (precision - PART_PRECISION);
1858 if (n >= PART_PRECISION)
1860 n -= PART_PRECISION;
1861 num.low = num.high;
1862 num.high = sign_mask;
1865 if (n)
1867 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1868 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1872 num = num_trim (num, precision);
1873 num.overflow = false;
1874 return num;
1877 /* Shift NUM, of width PRECISION, left by N bits. */
1878 static cpp_num
1879 num_lshift (cpp_num num, size_t precision, size_t n)
1881 if (n >= precision)
1883 num.overflow = !num.unsignedp && !num_zerop (num);
1884 num.high = num.low = 0;
1886 else
1888 cpp_num orig, maybe_orig;
1889 size_t m = n;
1891 orig = num;
1892 if (m >= PART_PRECISION)
1894 m -= PART_PRECISION;
1895 num.high = num.low;
1896 num.low = 0;
1898 if (m)
1900 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1901 num.low <<= m;
1903 num = num_trim (num, precision);
1905 if (num.unsignedp)
1906 num.overflow = false;
1907 else
1909 maybe_orig = num_rshift (num, precision, n);
1910 num.overflow = !num_eq (orig, maybe_orig);
1914 return num;
1917 /* The four unary operators: +, -, ! and ~. */
1918 static cpp_num
1919 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1921 switch (op)
1923 case CPP_UPLUS:
1924 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1925 cpp_warning (pfile, CPP_W_TRADITIONAL,
1926 "traditional C rejects the unary plus operator");
1927 num.overflow = false;
1928 break;
1930 case CPP_UMINUS:
1931 num = num_negate (num, CPP_OPTION (pfile, precision));
1932 break;
1934 case CPP_COMPL:
1935 num.high = ~num.high;
1936 num.low = ~num.low;
1937 num = num_trim (num, CPP_OPTION (pfile, precision));
1938 num.overflow = false;
1939 break;
1941 default: /* case CPP_NOT: */
1942 num.low = num_zerop (num);
1943 num.high = 0;
1944 num.overflow = false;
1945 num.unsignedp = false;
1946 break;
1949 return num;
1952 /* The various binary operators. */
1953 static cpp_num
1954 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1956 cpp_num result;
1957 size_t precision = CPP_OPTION (pfile, precision);
1958 size_t n;
1960 switch (op)
1962 /* Shifts. */
1963 case CPP_LSHIFT:
1964 case CPP_RSHIFT:
1965 if (!rhs.unsignedp && !num_positive (rhs, precision))
1967 /* A negative shift is a positive shift the other way. */
1968 if (op == CPP_LSHIFT)
1969 op = CPP_RSHIFT;
1970 else
1971 op = CPP_LSHIFT;
1972 rhs = num_negate (rhs, precision);
1974 if (rhs.high)
1975 n = ~0; /* Maximal. */
1976 else
1977 n = rhs.low;
1978 if (op == CPP_LSHIFT)
1979 lhs = num_lshift (lhs, precision, n);
1980 else
1981 lhs = num_rshift (lhs, precision, n);
1982 break;
1984 /* Arithmetic. */
1985 case CPP_MINUS:
1986 result.low = lhs.low - rhs.low;
1987 result.high = lhs.high - rhs.high;
1988 if (result.low > lhs.low)
1989 result.high--;
1990 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1991 result.overflow = false;
1993 result = num_trim (result, precision);
1994 if (!result.unsignedp)
1996 bool lhsp = num_positive (lhs, precision);
1997 result.overflow = (lhsp != num_positive (rhs, precision)
1998 && lhsp != num_positive (result, precision));
2000 return result;
2002 case CPP_PLUS:
2003 result.low = lhs.low + rhs.low;
2004 result.high = lhs.high + rhs.high;
2005 if (result.low < lhs.low)
2006 result.high++;
2007 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
2008 result.overflow = false;
2010 result = num_trim (result, precision);
2011 if (!result.unsignedp)
2013 bool lhsp = num_positive (lhs, precision);
2014 result.overflow = (lhsp == num_positive (rhs, precision)
2015 && lhsp != num_positive (result, precision));
2017 return result;
2019 /* Comma. */
2020 default: /* case CPP_COMMA: */
2021 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
2022 || !pfile->state.skip_eval))
2023 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
2024 "comma operator in operand of #if");
2025 lhs = rhs;
2026 break;
2029 return lhs;
2032 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
2033 cannot overflow. */
2034 static cpp_num
2035 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
2037 cpp_num result;
2038 cpp_num_part middle[2], temp;
2040 result.low = LOW_PART (lhs) * LOW_PART (rhs);
2041 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
2043 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
2044 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
2046 temp = result.low;
2047 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
2048 if (result.low < temp)
2049 result.high++;
2051 temp = result.low;
2052 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
2053 if (result.low < temp)
2054 result.high++;
2056 result.high += HIGH_PART (middle[0]);
2057 result.high += HIGH_PART (middle[1]);
2058 result.unsignedp = true;
2059 result.overflow = false;
2061 return result;
2064 /* Multiply two preprocessing numbers. */
2065 static cpp_num
2066 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
2068 cpp_num result, temp;
2069 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
2070 bool overflow, negate = false;
2071 size_t precision = CPP_OPTION (pfile, precision);
2073 /* Prepare for unsigned multiplication. */
2074 if (!unsignedp)
2076 if (!num_positive (lhs, precision))
2077 negate = !negate, lhs = num_negate (lhs, precision);
2078 if (!num_positive (rhs, precision))
2079 negate = !negate, rhs = num_negate (rhs, precision);
2082 overflow = lhs.high && rhs.high;
2083 result = num_part_mul (lhs.low, rhs.low);
2085 temp = num_part_mul (lhs.high, rhs.low);
2086 result.high += temp.low;
2087 if (temp.high)
2088 overflow = true;
2090 temp = num_part_mul (lhs.low, rhs.high);
2091 result.high += temp.low;
2092 if (temp.high)
2093 overflow = true;
2095 temp.low = result.low, temp.high = result.high;
2096 result = num_trim (result, precision);
2097 if (!num_eq (result, temp))
2098 overflow = true;
2100 if (negate)
2101 result = num_negate (result, precision);
2103 if (unsignedp)
2104 result.overflow = false;
2105 else
2106 result.overflow = overflow || (num_positive (result, precision) ^ !negate
2107 && !num_zerop (result));
2108 result.unsignedp = unsignedp;
2110 return result;
2113 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
2114 or the remainder depending upon OP. LOCATION is the source location
2115 of this operator (for diagnostics). */
2117 static cpp_num
2118 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
2119 location_t location)
2121 cpp_num result, sub;
2122 cpp_num_part mask;
2123 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
2124 bool negate = false, lhs_neg = false;
2125 size_t i, precision = CPP_OPTION (pfile, precision);
2127 /* Prepare for unsigned division. */
2128 if (!unsignedp)
2130 if (!num_positive (lhs, precision))
2131 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
2132 if (!num_positive (rhs, precision))
2133 negate = !negate, rhs = num_negate (rhs, precision);
2136 /* Find the high bit. */
2137 if (rhs.high)
2139 i = precision - 1;
2140 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
2141 for (; ; i--, mask >>= 1)
2142 if (rhs.high & mask)
2143 break;
2145 else if (rhs.low)
2147 if (precision > PART_PRECISION)
2148 i = precision - PART_PRECISION - 1;
2149 else
2150 i = precision - 1;
2151 mask = (cpp_num_part) 1 << i;
2152 for (; ; i--, mask >>= 1)
2153 if (rhs.low & mask)
2154 break;
2156 else
2158 if (!pfile->state.skip_eval)
2159 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
2160 "division by zero in #if");
2161 return lhs;
2164 /* First nonzero bit of RHS is bit I. Do naive division by
2165 shifting the RHS fully left, and subtracting from LHS if LHS is
2166 at least as big, and then repeating but with one less shift.
2167 This is not very efficient, but is easy to understand. */
2169 rhs.unsignedp = true;
2170 lhs.unsignedp = true;
2171 i = precision - i - 1;
2172 sub = num_lshift (rhs, precision, i);
2174 result.high = result.low = 0;
2175 for (;;)
2177 if (num_greater_eq (lhs, sub, precision))
2179 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
2180 if (i >= PART_PRECISION)
2181 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
2182 else
2183 result.low |= (cpp_num_part) 1 << i;
2185 if (i-- == 0)
2186 break;
2187 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
2188 sub.high >>= 1;
2191 /* We divide so that the remainder has the sign of the LHS. */
2192 if (op == CPP_DIV)
2194 result.unsignedp = unsignedp;
2195 result.overflow = false;
2196 if (!unsignedp)
2198 if (negate)
2199 result = num_negate (result, precision);
2200 result.overflow = (num_positive (result, precision) ^ !negate
2201 && !num_zerop (result));
2204 return result;
2207 /* CPP_MOD. */
2208 lhs.unsignedp = unsignedp;
2209 lhs.overflow = false;
2210 if (lhs_neg)
2211 lhs = num_negate (lhs, precision);
2213 return lhs;