Fix PR43464: loop close phi nodes can contain more than one argument.
[official-gcc/graphite-test-results.git] / libcpp / expr.c
blob60cb2816a7ab9d3fed07c567362e62dea5ef24c0
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2004, 2008, 2009, 2010 Free Software Foundation.
4 Contributed by Per Bothner, 1994.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "cpplib.h"
23 #include "internal.h"
25 #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
26 #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
27 #define LOW_PART(num_part) (num_part & HALF_MASK)
28 #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
30 struct op
32 const cpp_token *token; /* The token forming op (for diagnostics). */
33 cpp_num value; /* The value logically "right" of op. */
34 source_location loc; /* The location of this value. */
35 enum cpp_ttype op;
38 /* Some simple utility routines on double integers. */
39 #define num_zerop(num) ((num.low | num.high) == 0)
40 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
41 static bool num_positive (cpp_num, size_t);
42 static bool num_greater_eq (cpp_num, cpp_num, size_t);
43 static cpp_num num_trim (cpp_num, size_t);
44 static cpp_num num_part_mul (cpp_num_part, cpp_num_part);
46 static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype);
47 static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
48 static cpp_num num_negate (cpp_num, size_t);
49 static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
50 static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num,
51 enum cpp_ttype);
52 static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num,
53 enum cpp_ttype);
54 static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num);
55 static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype,
56 source_location);
57 static cpp_num num_lshift (cpp_num, size_t, size_t);
58 static cpp_num num_rshift (cpp_num, size_t, size_t);
60 static cpp_num append_digit (cpp_num, int, int, size_t);
61 static cpp_num parse_defined (cpp_reader *);
62 static cpp_num eval_token (cpp_reader *, const cpp_token *);
63 static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
64 static unsigned int interpret_float_suffix (const uchar *, size_t);
65 static unsigned int interpret_int_suffix (const uchar *, size_t);
66 static void check_promotion (cpp_reader *, const struct op *);
68 /* Token type abuse to create unary plus and minus operators. */
69 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
70 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
72 /* With -O2, gcc appears to produce nice code, moving the error
73 message load and subsequent jump completely out of the main path. */
74 #define SYNTAX_ERROR(msgid) \
75 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
76 #define SYNTAX_ERROR2(msgid, arg) \
77 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
78 while(0)
80 /* Subroutine of cpp_classify_number. S points to a float suffix of
81 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
82 flag vector describing the suffix. */
83 static unsigned int
84 interpret_float_suffix (const uchar *s, size_t len)
86 size_t flags;
87 size_t f, d, l, w, q, i;
89 flags = 0;
90 f = d = l = w = q = i = 0;
92 /* Process decimal float suffixes, which are two letters starting
93 with d or D. Order and case are significant. */
94 if (len == 2 && (*s == 'd' || *s == 'D'))
96 bool uppercase = (*s == 'D');
97 switch (s[1])
99 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break;
100 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break;
101 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break;
102 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break;
103 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
104 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
105 default:
106 /* Additional two-character suffixes beginning with D are not
107 for decimal float constants. */
108 break;
112 /* Recognize a fixed-point suffix. */
113 switch (s[len-1])
115 case 'k': case 'K': flags = CPP_N_ACCUM; break;
116 case 'r': case 'R': flags = CPP_N_FRACT; break;
117 default: break;
120 /* Continue processing a fixed-point suffix. The suffix is case
121 insensitive except for ll or LL. Order is significant. */
122 if (flags)
124 if (len == 1)
125 return flags;
126 len--;
128 if (*s == 'u' || *s == 'U')
130 flags |= CPP_N_UNSIGNED;
131 if (len == 1)
132 return flags;
133 len--;
134 s++;
137 switch (*s)
139 case 'h': case 'H':
140 if (len == 1)
141 return flags |= CPP_N_SMALL;
142 break;
143 case 'l':
144 if (len == 1)
145 return flags |= CPP_N_MEDIUM;
146 if (len == 2 && s[1] == 'l')
147 return flags |= CPP_N_LARGE;
148 break;
149 case 'L':
150 if (len == 1)
151 return flags |= CPP_N_MEDIUM;
152 if (len == 2 && s[1] == 'L')
153 return flags |= CPP_N_LARGE;
154 break;
155 default:
156 break;
158 /* Anything left at this point is invalid. */
159 return 0;
162 /* In any remaining valid suffix, the case and order don't matter. */
163 while (len--)
164 switch (s[len])
166 case 'f': case 'F': f++; break;
167 case 'd': case 'D': d++; break;
168 case 'l': case 'L': l++; break;
169 case 'w': case 'W': w++; break;
170 case 'q': case 'Q': q++; break;
171 case 'i': case 'I':
172 case 'j': case 'J': i++; break;
173 default:
174 return 0;
177 if (f + d + l + w + q > 1 || i > 1)
178 return 0;
180 return ((i ? CPP_N_IMAGINARY : 0)
181 | (f ? CPP_N_SMALL :
182 d ? CPP_N_MEDIUM :
183 l ? CPP_N_LARGE :
184 w ? CPP_N_MD_W :
185 q ? CPP_N_MD_Q : CPP_N_DEFAULT));
188 /* Subroutine of cpp_classify_number. S points to an integer suffix
189 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
190 flag vector describing the suffix. */
191 static unsigned int
192 interpret_int_suffix (const uchar *s, size_t len)
194 size_t u, l, i;
196 u = l = i = 0;
198 while (len--)
199 switch (s[len])
201 case 'u': case 'U': u++; break;
202 case 'i': case 'I':
203 case 'j': case 'J': i++; break;
204 case 'l': case 'L': l++;
205 /* If there are two Ls, they must be adjacent and the same case. */
206 if (l == 2 && s[len] != s[len + 1])
207 return 0;
208 break;
209 default:
210 return 0;
213 if (l > 2 || u > 1 || i > 1)
214 return 0;
216 return ((i ? CPP_N_IMAGINARY : 0)
217 | (u ? CPP_N_UNSIGNED : 0)
218 | ((l == 0) ? CPP_N_SMALL
219 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
222 /* Categorize numeric constants according to their field (integer,
223 floating point, or invalid), radix (decimal, octal, hexadecimal),
224 and type suffixes. */
225 unsigned int
226 cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
228 const uchar *str = token->val.str.text;
229 const uchar *limit;
230 unsigned int max_digit, result, radix;
231 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
232 bool seen_digit;
234 /* If the lexer has done its job, length one can only be a single
235 digit. Fast-path this very common case. */
236 if (token->val.str.len == 1)
237 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
239 limit = str + token->val.str.len;
240 float_flag = NOT_FLOAT;
241 max_digit = 0;
242 radix = 10;
243 seen_digit = false;
245 /* First, interpret the radix. */
246 if (*str == '0')
248 radix = 8;
249 str++;
251 /* Require at least one hex digit to classify it as hex. */
252 if ((*str == 'x' || *str == 'X')
253 && (str[1] == '.' || ISXDIGIT (str[1])))
255 radix = 16;
256 str++;
258 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
260 radix = 2;
261 str++;
265 /* Now scan for a well-formed integer or float. */
266 for (;;)
268 unsigned int c = *str++;
270 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
272 seen_digit = true;
273 c = hex_value (c);
274 if (c > max_digit)
275 max_digit = c;
277 else if (c == '.')
279 if (float_flag == NOT_FLOAT)
280 float_flag = AFTER_POINT;
281 else
282 SYNTAX_ERROR ("too many decimal points in number");
284 else if ((radix <= 10 && (c == 'e' || c == 'E'))
285 || (radix == 16 && (c == 'p' || c == 'P')))
287 float_flag = AFTER_EXPON;
288 break;
290 else
292 /* Start of suffix. */
293 str--;
294 break;
298 /* The suffix may be for decimal fixed-point constants without exponent. */
299 if (radix != 16 && float_flag == NOT_FLOAT)
301 result = interpret_float_suffix (str, limit - str);
302 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
304 result |= CPP_N_FLOATING;
305 /* We need to restore the radix to 10, if the radix is 8. */
306 if (radix == 8)
307 radix = 10;
309 if (CPP_PEDANTIC (pfile))
310 cpp_error (pfile, CPP_DL_PEDWARN,
311 "fixed-point constants are a GCC extension");
312 goto syntax_ok;
314 else
315 result = 0;
318 if (float_flag != NOT_FLOAT && radix == 8)
319 radix = 10;
321 if (max_digit >= radix)
323 if (radix == 2)
324 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
325 else
326 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
329 if (float_flag != NOT_FLOAT)
331 if (radix == 2)
333 cpp_error (pfile, CPP_DL_ERROR,
334 "invalid prefix \"0b\" for floating constant");
335 return CPP_N_INVALID;
338 if (radix == 16 && !seen_digit)
339 SYNTAX_ERROR ("no digits in hexadecimal floating constant");
341 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
342 cpp_error (pfile, CPP_DL_PEDWARN,
343 "use of C99 hexadecimal floating constant");
345 if (float_flag == AFTER_EXPON)
347 if (*str == '+' || *str == '-')
348 str++;
350 /* Exponent is decimal, even if string is a hex float. */
351 if (!ISDIGIT (*str))
352 SYNTAX_ERROR ("exponent has no digits");
355 str++;
356 while (ISDIGIT (*str));
358 else if (radix == 16)
359 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
361 result = interpret_float_suffix (str, limit - str);
362 if (result == 0)
364 cpp_error (pfile, CPP_DL_ERROR,
365 "invalid suffix \"%.*s\" on floating constant",
366 (int) (limit - str), str);
367 return CPP_N_INVALID;
370 /* Traditional C didn't accept any floating suffixes. */
371 if (limit != str
372 && CPP_WTRADITIONAL (pfile)
373 && ! cpp_sys_macro_p (pfile))
374 cpp_error (pfile, CPP_DL_WARNING,
375 "traditional C rejects the \"%.*s\" suffix",
376 (int) (limit - str), str);
378 /* A suffix for double is a GCC extension via decimal float support.
379 If the suffix also specifies an imaginary value we'll catch that
380 later. */
381 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
382 cpp_error (pfile, CPP_DL_PEDWARN,
383 "suffix for double constant is a GCC extension");
385 /* Radix must be 10 for decimal floats. */
386 if ((result & CPP_N_DFLOAT) && radix != 10)
388 cpp_error (pfile, CPP_DL_ERROR,
389 "invalid suffix \"%.*s\" with hexadecimal floating constant",
390 (int) (limit - str), str);
391 return CPP_N_INVALID;
394 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
395 cpp_error (pfile, CPP_DL_PEDWARN,
396 "fixed-point constants are a GCC extension");
398 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
399 cpp_error (pfile, CPP_DL_PEDWARN,
400 "decimal float constants are a GCC extension");
402 result |= CPP_N_FLOATING;
404 else
406 result = interpret_int_suffix (str, limit - str);
407 if (result == 0)
409 cpp_error (pfile, CPP_DL_ERROR,
410 "invalid suffix \"%.*s\" on integer constant",
411 (int) (limit - str), str);
412 return CPP_N_INVALID;
415 /* Traditional C only accepted the 'L' suffix.
416 Suppress warning about 'LL' with -Wno-long-long. */
417 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
419 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
420 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
422 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
423 cpp_error (pfile, CPP_DL_WARNING,
424 "traditional C rejects the \"%.*s\" suffix",
425 (int) (limit - str), str);
428 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
429 && CPP_OPTION (pfile, warn_long_long))
430 cpp_error (pfile,
431 CPP_OPTION (pfile, c99) ? CPP_DL_WARNING : CPP_DL_PEDWARN,
432 CPP_OPTION (pfile, cplusplus)
433 ? "use of C++0x long long integer constant"
434 : "use of C99 long long integer constant");
436 result |= CPP_N_INTEGER;
439 syntax_ok:
440 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
441 cpp_error (pfile, CPP_DL_PEDWARN,
442 "imaginary constants are a GCC extension");
443 if (radix == 2 && CPP_PEDANTIC (pfile))
444 cpp_error (pfile, CPP_DL_PEDWARN,
445 "binary constants are a GCC extension");
447 if (radix == 10)
448 result |= CPP_N_DECIMAL;
449 else if (radix == 16)
450 result |= CPP_N_HEX;
451 else if (radix == 2)
452 result |= CPP_N_BINARY;
453 else
454 result |= CPP_N_OCTAL;
456 return result;
458 syntax_error:
459 return CPP_N_INVALID;
462 /* cpp_interpret_integer converts an integer constant into a cpp_num,
463 of precision options->precision.
465 We do not provide any interface for decimal->float conversion,
466 because the preprocessor doesn't need it and we don't want to
467 drag in GCC's floating point emulator. */
468 cpp_num
469 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
470 unsigned int type)
472 const uchar *p, *end;
473 cpp_num result;
475 result.low = 0;
476 result.high = 0;
477 result.unsignedp = !!(type & CPP_N_UNSIGNED);
478 result.overflow = false;
480 p = token->val.str.text;
481 end = p + token->val.str.len;
483 /* Common case of a single digit. */
484 if (token->val.str.len == 1)
485 result.low = p[0] - '0';
486 else
488 cpp_num_part max;
489 size_t precision = CPP_OPTION (pfile, precision);
490 unsigned int base = 10, c = 0;
491 bool overflow = false;
493 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
495 base = 8;
496 p++;
498 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
500 base = 16;
501 p += 2;
503 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
505 base = 2;
506 p += 2;
509 /* We can add a digit to numbers strictly less than this without
510 needing the precision and slowness of double integers. */
511 max = ~(cpp_num_part) 0;
512 if (precision < PART_PRECISION)
513 max >>= PART_PRECISION - precision;
514 max = (max - base + 1) / base + 1;
516 for (; p < end; p++)
518 c = *p;
520 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
521 c = hex_value (c);
522 else
523 break;
525 /* Strict inequality for when max is set to zero. */
526 if (result.low < max)
527 result.low = result.low * base + c;
528 else
530 result = append_digit (result, c, base, precision);
531 overflow |= result.overflow;
532 max = 0;
536 if (overflow)
537 cpp_error (pfile, CPP_DL_PEDWARN,
538 "integer constant is too large for its type");
539 /* If too big to be signed, consider it unsigned. Only warn for
540 decimal numbers. Traditional numbers were always signed (but
541 we still honor an explicit U suffix); but we only have
542 traditional semantics in directives. */
543 else if (!result.unsignedp
544 && !(CPP_OPTION (pfile, traditional)
545 && pfile->state.in_directive)
546 && !num_positive (result, precision))
548 /* This is for constants within the range of uintmax_t but
549 not that of intmax_t. For such decimal constants, a
550 diagnostic is required for C99 as the selected type must
551 be signed and not having a type is a constraint violation
552 (DR#298, TC3), so this must be a pedwarn. For C90,
553 unsigned long is specified to be used for a constant that
554 does not fit in signed long; if uintmax_t has the same
555 range as unsigned long this means only a warning is
556 appropriate here. C90 permits the preprocessor to use a
557 wider range than unsigned long in the compiler, so if
558 uintmax_t is wider than unsigned long no diagnostic is
559 required for such constants in preprocessor #if
560 expressions and the compiler will pedwarn for such
561 constants outside the range of unsigned long that reach
562 the compiler so a diagnostic is not required there
563 either; thus, pedwarn for C99 but use a plain warning for
564 C90. */
565 if (base == 10)
566 cpp_error (pfile, (CPP_OPTION (pfile, c99)
567 ? CPP_DL_PEDWARN
568 : CPP_DL_WARNING),
569 "integer constant is so large that it is unsigned");
570 result.unsignedp = true;
574 return result;
577 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
578 static cpp_num
579 append_digit (cpp_num num, int digit, int base, size_t precision)
581 cpp_num result;
582 unsigned int shift;
583 bool overflow;
584 cpp_num_part add_high, add_low;
586 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
587 need to worry about add_high overflowing. */
588 switch (base)
590 case 2:
591 shift = 1;
592 break;
594 case 16:
595 shift = 4;
596 break;
598 default:
599 shift = 3;
601 overflow = !!(num.high >> (PART_PRECISION - shift));
602 result.high = num.high << shift;
603 result.low = num.low << shift;
604 result.high |= num.low >> (PART_PRECISION - shift);
605 result.unsignedp = num.unsignedp;
607 if (base == 10)
609 add_low = num.low << 1;
610 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
612 else
613 add_high = add_low = 0;
615 if (add_low + digit < add_low)
616 add_high++;
617 add_low += digit;
619 if (result.low + add_low < result.low)
620 add_high++;
621 if (result.high + add_high < result.high)
622 overflow = true;
624 result.low += add_low;
625 result.high += add_high;
626 result.overflow = overflow;
628 /* The above code catches overflow of a cpp_num type. This catches
629 overflow of the (possibly shorter) target precision. */
630 num.low = result.low;
631 num.high = result.high;
632 result = num_trim (result, precision);
633 if (!num_eq (result, num))
634 result.overflow = true;
636 return result;
639 /* Handle meeting "defined" in a preprocessor expression. */
640 static cpp_num
641 parse_defined (cpp_reader *pfile)
643 cpp_num result;
644 int paren = 0;
645 cpp_hashnode *node = 0;
646 const cpp_token *token;
647 cpp_context *initial_context = pfile->context;
649 /* Don't expand macros. */
650 pfile->state.prevent_expansion++;
652 token = cpp_get_token (pfile);
653 if (token->type == CPP_OPEN_PAREN)
655 paren = 1;
656 token = cpp_get_token (pfile);
659 if (token->type == CPP_NAME)
661 node = token->val.node.node;
662 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
664 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
665 node = 0;
668 else
670 cpp_error (pfile, CPP_DL_ERROR,
671 "operator \"defined\" requires an identifier");
672 if (token->flags & NAMED_OP)
674 cpp_token op;
676 op.flags = 0;
677 op.type = token->type;
678 cpp_error (pfile, CPP_DL_ERROR,
679 "(\"%s\" is an alternative token for \"%s\" in C++)",
680 cpp_token_as_text (pfile, token),
681 cpp_token_as_text (pfile, &op));
685 if (node)
687 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
688 cpp_error (pfile, CPP_DL_WARNING,
689 "this use of \"defined\" may not be portable");
691 _cpp_mark_macro_used (node);
692 if (!(node->flags & NODE_USED))
694 node->flags |= NODE_USED;
695 if (node->type == NT_MACRO)
697 if (pfile->cb.used_define)
698 pfile->cb.used_define (pfile, pfile->directive_line, node);
700 else
702 if (pfile->cb.used_undef)
703 pfile->cb.used_undef (pfile, pfile->directive_line, node);
707 /* A possible controlling macro of the form #if !defined ().
708 _cpp_parse_expr checks there was no other junk on the line. */
709 pfile->mi_ind_cmacro = node;
712 pfile->state.prevent_expansion--;
714 result.unsignedp = false;
715 result.high = 0;
716 result.overflow = false;
717 result.low = node && node->type == NT_MACRO;
718 return result;
721 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
722 number or character constant, or the result of the "defined" or "#"
723 operators). */
724 static cpp_num
725 eval_token (cpp_reader *pfile, const cpp_token *token)
727 cpp_num result;
728 unsigned int temp;
729 int unsignedp = 0;
731 result.unsignedp = false;
732 result.overflow = false;
734 switch (token->type)
736 case CPP_NUMBER:
737 temp = cpp_classify_number (pfile, token);
738 switch (temp & CPP_N_CATEGORY)
740 case CPP_N_FLOATING:
741 cpp_error (pfile, CPP_DL_ERROR,
742 "floating constant in preprocessor expression");
743 break;
744 case CPP_N_INTEGER:
745 if (!(temp & CPP_N_IMAGINARY))
746 return cpp_interpret_integer (pfile, token, temp);
747 cpp_error (pfile, CPP_DL_ERROR,
748 "imaginary number in preprocessor expression");
749 break;
751 case CPP_N_INVALID:
752 /* Error already issued. */
753 break;
755 result.high = result.low = 0;
756 break;
758 case CPP_WCHAR:
759 case CPP_CHAR:
760 case CPP_CHAR16:
761 case CPP_CHAR32:
763 cppchar_t cc = cpp_interpret_charconst (pfile, token,
764 &temp, &unsignedp);
766 result.high = 0;
767 result.low = cc;
768 /* Sign-extend the result if necessary. */
769 if (!unsignedp && (cppchar_signed_t) cc < 0)
771 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
772 result.low |= ~(~(cpp_num_part) 0
773 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
774 result.high = ~(cpp_num_part) 0;
775 result = num_trim (result, CPP_OPTION (pfile, precision));
778 break;
780 case CPP_NAME:
781 if (token->val.node.node == pfile->spec_nodes.n_defined)
782 return parse_defined (pfile);
783 else if (CPP_OPTION (pfile, cplusplus)
784 && (token->val.node.node == pfile->spec_nodes.n_true
785 || token->val.node.node == pfile->spec_nodes.n_false))
787 result.high = 0;
788 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
790 else
792 result.high = 0;
793 result.low = 0;
794 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
795 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined",
796 NODE_NAME (token->val.node.node));
798 break;
800 case CPP_HASH:
801 if (!pfile->state.skipping)
803 /* A pedantic warning takes precedence over a deprecated
804 warning here. */
805 if (CPP_PEDANTIC (pfile))
806 cpp_error (pfile, CPP_DL_PEDWARN,
807 "assertions are a GCC extension");
808 else if (CPP_OPTION (pfile, warn_deprecated))
809 cpp_error (pfile, CPP_DL_WARNING,
810 "assertions are a deprecated extension");
812 _cpp_test_assertion (pfile, &temp);
813 result.high = 0;
814 result.low = temp;
815 break;
817 default:
818 abort ();
821 result.unsignedp = !!unsignedp;
822 return result;
825 /* Operator precedence and flags table.
827 After an operator is returned from the lexer, if it has priority less
828 than the operator on the top of the stack, we reduce the stack by one
829 operator and repeat the test. Since equal priorities do not reduce,
830 this is naturally right-associative.
832 We handle left-associative operators by decrementing the priority of
833 just-lexed operators by one, but retaining the priority of operators
834 already on the stack.
836 The remaining cases are '(' and ')'. We handle '(' by skipping the
837 reduction phase completely. ')' is given lower priority than
838 everything else, including '(', effectively forcing a reduction of the
839 parenthesized expression. If there is a matching '(', the routine
840 reduce() exits immediately. If the normal exit route sees a ')', then
841 there cannot have been a matching '(' and an error message is output.
843 The parser assumes all shifted operators require a left operand unless
844 the flag NO_L_OPERAND is set. These semantics are automatic; any
845 extra semantics need to be handled with operator-specific code. */
847 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
848 operand changes because of integer promotions. */
849 #define NO_L_OPERAND (1 << 0)
850 #define LEFT_ASSOC (1 << 1)
851 #define CHECK_PROMOTION (1 << 2)
853 /* Operator to priority map. Must be in the same order as the first
854 N entries of enum cpp_ttype. */
855 static const struct cpp_operator
857 uchar prio;
858 uchar flags;
859 } optab[] =
861 /* EQ */ {0, 0}, /* Shouldn't happen. */
862 /* NOT */ {16, NO_L_OPERAND},
863 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
864 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
865 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
866 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
867 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
868 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
869 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
870 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
871 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
872 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
873 /* RSHIFT */ {13, LEFT_ASSOC},
874 /* LSHIFT */ {13, LEFT_ASSOC},
876 /* COMPL */ {16, NO_L_OPERAND},
877 /* AND_AND */ {6, LEFT_ASSOC},
878 /* OR_OR */ {5, LEFT_ASSOC},
879 /* Note that QUERY, COLON, and COMMA must have the same precedence.
880 However, there are some special cases for these in reduce(). */
881 /* QUERY */ {4, 0},
882 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
883 /* COMMA */ {4, LEFT_ASSOC},
884 /* OPEN_PAREN */ {1, NO_L_OPERAND},
885 /* CLOSE_PAREN */ {0, 0},
886 /* EOF */ {0, 0},
887 /* EQ_EQ */ {11, LEFT_ASSOC},
888 /* NOT_EQ */ {11, LEFT_ASSOC},
889 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
890 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
891 /* UPLUS */ {16, NO_L_OPERAND},
892 /* UMINUS */ {16, NO_L_OPERAND}
895 /* Parse and evaluate a C expression, reading from PFILE.
896 Returns the truth value of the expression.
898 The implementation is an operator precedence parser, i.e. a
899 bottom-up parser, using a stack for not-yet-reduced tokens.
901 The stack base is op_stack, and the current stack pointer is 'top'.
902 There is a stack element for each operator (only), and the most
903 recently pushed operator is 'top->op'. An operand (value) is
904 stored in the 'value' field of the stack element of the operator
905 that precedes it. */
906 bool
907 _cpp_parse_expr (cpp_reader *pfile, bool is_if)
909 struct op *top = pfile->op_stack;
910 unsigned int lex_count;
911 bool saw_leading_not, want_value = true;
913 pfile->state.skip_eval = 0;
915 /* Set up detection of #if ! defined(). */
916 pfile->mi_ind_cmacro = 0;
917 saw_leading_not = false;
918 lex_count = 0;
920 /* Lowest priority operator prevents further reductions. */
921 top->op = CPP_EOF;
923 for (;;)
925 struct op op;
927 lex_count++;
928 op.token = cpp_get_token (pfile);
929 op.op = op.token->type;
930 op.loc = op.token->src_loc;
932 switch (op.op)
934 /* These tokens convert into values. */
935 case CPP_NUMBER:
936 case CPP_CHAR:
937 case CPP_WCHAR:
938 case CPP_CHAR16:
939 case CPP_CHAR32:
940 case CPP_NAME:
941 case CPP_HASH:
942 if (!want_value)
943 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
944 cpp_token_as_text (pfile, op.token));
945 want_value = false;
946 top->value = eval_token (pfile, op.token);
947 continue;
949 case CPP_NOT:
950 saw_leading_not = lex_count == 1;
951 break;
952 case CPP_PLUS:
953 if (want_value)
954 op.op = CPP_UPLUS;
955 break;
956 case CPP_MINUS:
957 if (want_value)
958 op.op = CPP_UMINUS;
959 break;
961 default:
962 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
963 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
964 cpp_token_as_text (pfile, op.token));
965 break;
968 /* Check we have a value or operator as appropriate. */
969 if (optab[op.op].flags & NO_L_OPERAND)
971 if (!want_value)
972 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
973 cpp_token_as_text (pfile, op.token));
975 else if (want_value)
977 /* We want a number (or expression) and haven't got one.
978 Try to emit a specific diagnostic. */
979 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
980 SYNTAX_ERROR ("missing expression between '(' and ')'");
982 if (op.op == CPP_EOF && top->op == CPP_EOF)
983 SYNTAX_ERROR2 ("%s with no expression", is_if ? "#if" : "#elif");
985 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
986 SYNTAX_ERROR2 ("operator '%s' has no right operand",
987 cpp_token_as_text (pfile, top->token));
988 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
989 /* Complain about missing paren during reduction. */;
990 else
991 SYNTAX_ERROR2 ("operator '%s' has no left operand",
992 cpp_token_as_text (pfile, op.token));
995 top = reduce (pfile, top, op.op);
996 if (!top)
997 goto syntax_error;
999 if (op.op == CPP_EOF)
1000 break;
1002 switch (op.op)
1004 case CPP_CLOSE_PAREN:
1005 continue;
1006 case CPP_OR_OR:
1007 if (!num_zerop (top->value))
1008 pfile->state.skip_eval++;
1009 break;
1010 case CPP_AND_AND:
1011 case CPP_QUERY:
1012 if (num_zerop (top->value))
1013 pfile->state.skip_eval++;
1014 break;
1015 case CPP_COLON:
1016 if (top->op != CPP_QUERY)
1017 SYNTAX_ERROR (" ':' without preceding '?'");
1018 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
1019 pfile->state.skip_eval++;
1020 else
1021 pfile->state.skip_eval--;
1022 default:
1023 break;
1026 want_value = true;
1028 /* Check for and handle stack overflow. */
1029 if (++top == pfile->op_limit)
1030 top = _cpp_expand_op_stack (pfile);
1032 top->op = op.op;
1033 top->token = op.token;
1034 top->loc = op.token->src_loc;
1037 /* The controlling macro expression is only valid if we called lex 3
1038 times: <!> <defined expression> and <EOF>. push_conditional ()
1039 checks that we are at top-of-file. */
1040 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1041 pfile->mi_ind_cmacro = 0;
1043 if (top != pfile->op_stack)
1045 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in %s",
1046 is_if ? "#if" : "#elif");
1047 syntax_error:
1048 return false; /* Return false on syntax error. */
1051 return !num_zerop (top->value);
1054 /* Reduce the operator / value stack if possible, in preparation for
1055 pushing operator OP. Returns NULL on error, otherwise the top of
1056 the stack. */
1057 static struct op *
1058 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1060 unsigned int prio;
1062 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1064 bad_op:
1065 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1066 return 0;
1069 if (op == CPP_OPEN_PAREN)
1070 return top;
1072 /* Decrement the priority of left-associative operators to force a
1073 reduction with operators of otherwise equal priority. */
1074 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1075 while (prio < optab[top->op].prio)
1077 if (CPP_OPTION (pfile, warn_num_sign_change)
1078 && optab[top->op].flags & CHECK_PROMOTION)
1079 check_promotion (pfile, top);
1081 switch (top->op)
1083 case CPP_UPLUS:
1084 case CPP_UMINUS:
1085 case CPP_NOT:
1086 case CPP_COMPL:
1087 top[-1].value = num_unary_op (pfile, top->value, top->op);
1088 top[-1].loc = top->loc;
1089 break;
1091 case CPP_PLUS:
1092 case CPP_MINUS:
1093 case CPP_RSHIFT:
1094 case CPP_LSHIFT:
1095 case CPP_COMMA:
1096 top[-1].value = num_binary_op (pfile, top[-1].value,
1097 top->value, top->op);
1098 top[-1].loc = top->loc;
1099 break;
1101 case CPP_GREATER:
1102 case CPP_LESS:
1103 case CPP_GREATER_EQ:
1104 case CPP_LESS_EQ:
1105 top[-1].value
1106 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1107 top[-1].loc = top->loc;
1108 break;
1110 case CPP_EQ_EQ:
1111 case CPP_NOT_EQ:
1112 top[-1].value
1113 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1114 top[-1].loc = top->loc;
1115 break;
1117 case CPP_AND:
1118 case CPP_OR:
1119 case CPP_XOR:
1120 top[-1].value
1121 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1122 top[-1].loc = top->loc;
1123 break;
1125 case CPP_MULT:
1126 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1127 top[-1].loc = top->loc;
1128 break;
1130 case CPP_DIV:
1131 case CPP_MOD:
1132 top[-1].value = num_div_op (pfile, top[-1].value,
1133 top->value, top->op, top->loc);
1134 top[-1].loc = top->loc;
1135 break;
1137 case CPP_OR_OR:
1138 top--;
1139 if (!num_zerop (top->value))
1140 pfile->state.skip_eval--;
1141 top->value.low = (!num_zerop (top->value)
1142 || !num_zerop (top[1].value));
1143 top->value.high = 0;
1144 top->value.unsignedp = false;
1145 top->value.overflow = false;
1146 top->loc = top[1].loc;
1147 continue;
1149 case CPP_AND_AND:
1150 top--;
1151 if (num_zerop (top->value))
1152 pfile->state.skip_eval--;
1153 top->value.low = (!num_zerop (top->value)
1154 && !num_zerop (top[1].value));
1155 top->value.high = 0;
1156 top->value.unsignedp = false;
1157 top->value.overflow = false;
1158 top->loc = top[1].loc;
1159 continue;
1161 case CPP_OPEN_PAREN:
1162 if (op != CPP_CLOSE_PAREN)
1164 cpp_error_with_line (pfile, CPP_DL_ERROR,
1165 top->token->src_loc,
1166 0, "missing ')' in expression");
1167 return 0;
1169 top--;
1170 top->value = top[1].value;
1171 top->loc = top[1].loc;
1172 return top;
1174 case CPP_COLON:
1175 top -= 2;
1176 if (!num_zerop (top->value))
1178 pfile->state.skip_eval--;
1179 top->value = top[1].value;
1180 top->loc = top[1].loc;
1182 else
1184 top->value = top[2].value;
1185 top->loc = top[2].loc;
1187 top->value.unsignedp = (top[1].value.unsignedp
1188 || top[2].value.unsignedp);
1189 continue;
1191 case CPP_QUERY:
1192 /* COMMA and COLON should not reduce a QUERY operator. */
1193 if (op == CPP_COMMA || op == CPP_COLON)
1194 return top;
1195 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1196 return 0;
1198 default:
1199 goto bad_op;
1202 top--;
1203 if (top->value.overflow && !pfile->state.skip_eval)
1204 cpp_error (pfile, CPP_DL_PEDWARN,
1205 "integer overflow in preprocessor expression");
1208 if (op == CPP_CLOSE_PAREN)
1210 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1211 return 0;
1214 return top;
1217 /* Returns the position of the old top of stack after expansion. */
1218 struct op *
1219 _cpp_expand_op_stack (cpp_reader *pfile)
1221 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1222 size_t new_size = old_size * 2 + 20;
1224 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1225 pfile->op_limit = pfile->op_stack + new_size;
1227 return pfile->op_stack + old_size;
1230 /* Emits a warning if the effective sign of either operand of OP
1231 changes because of integer promotions. */
1232 static void
1233 check_promotion (cpp_reader *pfile, const struct op *op)
1235 if (op->value.unsignedp == op[-1].value.unsignedp)
1236 return;
1238 if (op->value.unsignedp)
1240 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1241 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1242 "the left operand of \"%s\" changes sign when promoted",
1243 cpp_token_as_text (pfile, op->token));
1245 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1246 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1247 "the right operand of \"%s\" changes sign when promoted",
1248 cpp_token_as_text (pfile, op->token));
1251 /* Clears the unused high order bits of the number pointed to by PNUM. */
1252 static cpp_num
1253 num_trim (cpp_num num, size_t precision)
1255 if (precision > PART_PRECISION)
1257 precision -= PART_PRECISION;
1258 if (precision < PART_PRECISION)
1259 num.high &= ((cpp_num_part) 1 << precision) - 1;
1261 else
1263 if (precision < PART_PRECISION)
1264 num.low &= ((cpp_num_part) 1 << precision) - 1;
1265 num.high = 0;
1268 return num;
1271 /* True iff A (presumed signed) >= 0. */
1272 static bool
1273 num_positive (cpp_num num, size_t precision)
1275 if (precision > PART_PRECISION)
1277 precision -= PART_PRECISION;
1278 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1281 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1284 /* Sign extend a number, with PRECISION significant bits and all
1285 others assumed clear, to fill out a cpp_num structure. */
1286 cpp_num
1287 cpp_num_sign_extend (cpp_num num, size_t precision)
1289 if (!num.unsignedp)
1291 if (precision > PART_PRECISION)
1293 precision -= PART_PRECISION;
1294 if (precision < PART_PRECISION
1295 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1296 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1298 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1300 if (precision < PART_PRECISION)
1301 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1302 num.high = ~(cpp_num_part) 0;
1306 return num;
1309 /* Returns the negative of NUM. */
1310 static cpp_num
1311 num_negate (cpp_num num, size_t precision)
1313 cpp_num copy;
1315 copy = num;
1316 num.high = ~num.high;
1317 num.low = ~num.low;
1318 if (++num.low == 0)
1319 num.high++;
1320 num = num_trim (num, precision);
1321 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1323 return num;
1326 /* Returns true if A >= B. */
1327 static bool
1328 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1330 bool unsignedp;
1332 unsignedp = pa.unsignedp || pb.unsignedp;
1334 if (!unsignedp)
1336 /* Both numbers have signed type. If they are of different
1337 sign, the answer is the sign of A. */
1338 unsignedp = num_positive (pa, precision);
1340 if (unsignedp != num_positive (pb, precision))
1341 return unsignedp;
1343 /* Otherwise we can do an unsigned comparison. */
1346 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1349 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1350 static cpp_num
1351 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1352 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1354 lhs.overflow = false;
1355 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1357 /* As excess precision is zeroed, there is no need to num_trim () as
1358 these operations cannot introduce a set bit there. */
1359 if (op == CPP_AND)
1361 lhs.low &= rhs.low;
1362 lhs.high &= rhs.high;
1364 else if (op == CPP_OR)
1366 lhs.low |= rhs.low;
1367 lhs.high |= rhs.high;
1369 else
1371 lhs.low ^= rhs.low;
1372 lhs.high ^= rhs.high;
1375 return lhs;
1378 /* Returns LHS OP RHS, where OP is an inequality. */
1379 static cpp_num
1380 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1381 enum cpp_ttype op)
1383 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1385 if (op == CPP_GREATER_EQ)
1386 lhs.low = gte;
1387 else if (op == CPP_LESS)
1388 lhs.low = !gte;
1389 else if (op == CPP_GREATER)
1390 lhs.low = gte && !num_eq (lhs, rhs);
1391 else /* CPP_LESS_EQ. */
1392 lhs.low = !gte || num_eq (lhs, rhs);
1394 lhs.high = 0;
1395 lhs.overflow = false;
1396 lhs.unsignedp = false;
1397 return lhs;
1400 /* Returns LHS OP RHS, where OP is == or !=. */
1401 static cpp_num
1402 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1403 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1405 /* Work around a 3.0.4 bug; see PR 6950. */
1406 bool eq = num_eq (lhs, rhs);
1407 if (op == CPP_NOT_EQ)
1408 eq = !eq;
1409 lhs.low = eq;
1410 lhs.high = 0;
1411 lhs.overflow = false;
1412 lhs.unsignedp = false;
1413 return lhs;
1416 /* Shift NUM, of width PRECISION, right by N bits. */
1417 static cpp_num
1418 num_rshift (cpp_num num, size_t precision, size_t n)
1420 cpp_num_part sign_mask;
1421 bool x = num_positive (num, precision);
1423 if (num.unsignedp || x)
1424 sign_mask = 0;
1425 else
1426 sign_mask = ~(cpp_num_part) 0;
1428 if (n >= precision)
1429 num.high = num.low = sign_mask;
1430 else
1432 /* Sign-extend. */
1433 if (precision < PART_PRECISION)
1434 num.high = sign_mask, num.low |= sign_mask << precision;
1435 else if (precision < 2 * PART_PRECISION)
1436 num.high |= sign_mask << (precision - PART_PRECISION);
1438 if (n >= PART_PRECISION)
1440 n -= PART_PRECISION;
1441 num.low = num.high;
1442 num.high = sign_mask;
1445 if (n)
1447 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1448 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1452 num = num_trim (num, precision);
1453 num.overflow = false;
1454 return num;
1457 /* Shift NUM, of width PRECISION, left by N bits. */
1458 static cpp_num
1459 num_lshift (cpp_num num, size_t precision, size_t n)
1461 if (n >= precision)
1463 num.overflow = !num.unsignedp && !num_zerop (num);
1464 num.high = num.low = 0;
1466 else
1468 cpp_num orig, maybe_orig;
1469 size_t m = n;
1471 orig = num;
1472 if (m >= PART_PRECISION)
1474 m -= PART_PRECISION;
1475 num.high = num.low;
1476 num.low = 0;
1478 if (m)
1480 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1481 num.low <<= m;
1483 num = num_trim (num, precision);
1485 if (num.unsignedp)
1486 num.overflow = false;
1487 else
1489 maybe_orig = num_rshift (num, precision, n);
1490 num.overflow = !num_eq (orig, maybe_orig);
1494 return num;
1497 /* The four unary operators: +, -, ! and ~. */
1498 static cpp_num
1499 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1501 switch (op)
1503 case CPP_UPLUS:
1504 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1505 cpp_error (pfile, CPP_DL_WARNING,
1506 "traditional C rejects the unary plus operator");
1507 num.overflow = false;
1508 break;
1510 case CPP_UMINUS:
1511 num = num_negate (num, CPP_OPTION (pfile, precision));
1512 break;
1514 case CPP_COMPL:
1515 num.high = ~num.high;
1516 num.low = ~num.low;
1517 num = num_trim (num, CPP_OPTION (pfile, precision));
1518 num.overflow = false;
1519 break;
1521 default: /* case CPP_NOT: */
1522 num.low = num_zerop (num);
1523 num.high = 0;
1524 num.overflow = false;
1525 num.unsignedp = false;
1526 break;
1529 return num;
1532 /* The various binary operators. */
1533 static cpp_num
1534 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1536 cpp_num result;
1537 size_t precision = CPP_OPTION (pfile, precision);
1538 size_t n;
1540 switch (op)
1542 /* Shifts. */
1543 case CPP_LSHIFT:
1544 case CPP_RSHIFT:
1545 if (!rhs.unsignedp && !num_positive (rhs, precision))
1547 /* A negative shift is a positive shift the other way. */
1548 if (op == CPP_LSHIFT)
1549 op = CPP_RSHIFT;
1550 else
1551 op = CPP_LSHIFT;
1552 rhs = num_negate (rhs, precision);
1554 if (rhs.high)
1555 n = ~0; /* Maximal. */
1556 else
1557 n = rhs.low;
1558 if (op == CPP_LSHIFT)
1559 lhs = num_lshift (lhs, precision, n);
1560 else
1561 lhs = num_rshift (lhs, precision, n);
1562 break;
1564 /* Arithmetic. */
1565 case CPP_MINUS:
1566 rhs = num_negate (rhs, precision);
1567 case CPP_PLUS:
1568 result.low = lhs.low + rhs.low;
1569 result.high = lhs.high + rhs.high;
1570 if (result.low < lhs.low)
1571 result.high++;
1572 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1573 result.overflow = false;
1575 result = num_trim (result, precision);
1576 if (!result.unsignedp)
1578 bool lhsp = num_positive (lhs, precision);
1579 result.overflow = (lhsp == num_positive (rhs, precision)
1580 && lhsp != num_positive (result, precision));
1582 return result;
1584 /* Comma. */
1585 default: /* case CPP_COMMA: */
1586 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1587 || !pfile->state.skip_eval))
1588 cpp_error (pfile, CPP_DL_PEDWARN,
1589 "comma operator in operand of #if");
1590 lhs = rhs;
1591 break;
1594 return lhs;
1597 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1598 cannot overflow. */
1599 static cpp_num
1600 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1602 cpp_num result;
1603 cpp_num_part middle[2], temp;
1605 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1606 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1608 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1609 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1611 temp = result.low;
1612 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1613 if (result.low < temp)
1614 result.high++;
1616 temp = result.low;
1617 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1618 if (result.low < temp)
1619 result.high++;
1621 result.high += HIGH_PART (middle[0]);
1622 result.high += HIGH_PART (middle[1]);
1623 result.unsignedp = true;
1624 result.overflow = false;
1626 return result;
1629 /* Multiply two preprocessing numbers. */
1630 static cpp_num
1631 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1633 cpp_num result, temp;
1634 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1635 bool overflow, negate = false;
1636 size_t precision = CPP_OPTION (pfile, precision);
1638 /* Prepare for unsigned multiplication. */
1639 if (!unsignedp)
1641 if (!num_positive (lhs, precision))
1642 negate = !negate, lhs = num_negate (lhs, precision);
1643 if (!num_positive (rhs, precision))
1644 negate = !negate, rhs = num_negate (rhs, precision);
1647 overflow = lhs.high && rhs.high;
1648 result = num_part_mul (lhs.low, rhs.low);
1650 temp = num_part_mul (lhs.high, rhs.low);
1651 result.high += temp.low;
1652 if (temp.high)
1653 overflow = true;
1655 temp = num_part_mul (lhs.low, rhs.high);
1656 result.high += temp.low;
1657 if (temp.high)
1658 overflow = true;
1660 temp.low = result.low, temp.high = result.high;
1661 result = num_trim (result, precision);
1662 if (!num_eq (result, temp))
1663 overflow = true;
1665 if (negate)
1666 result = num_negate (result, precision);
1668 if (unsignedp)
1669 result.overflow = false;
1670 else
1671 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1672 && !num_zerop (result));
1673 result.unsignedp = unsignedp;
1675 return result;
1678 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
1679 or the remainder depending upon OP. LOCATION is the source location
1680 of this operator (for diagnostics). */
1682 static cpp_num
1683 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
1684 source_location location)
1686 cpp_num result, sub;
1687 cpp_num_part mask;
1688 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1689 bool negate = false, lhs_neg = false;
1690 size_t i, precision = CPP_OPTION (pfile, precision);
1692 /* Prepare for unsigned division. */
1693 if (!unsignedp)
1695 if (!num_positive (lhs, precision))
1696 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1697 if (!num_positive (rhs, precision))
1698 negate = !negate, rhs = num_negate (rhs, precision);
1701 /* Find the high bit. */
1702 if (rhs.high)
1704 i = precision - 1;
1705 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1706 for (; ; i--, mask >>= 1)
1707 if (rhs.high & mask)
1708 break;
1710 else if (rhs.low)
1712 if (precision > PART_PRECISION)
1713 i = precision - PART_PRECISION - 1;
1714 else
1715 i = precision - 1;
1716 mask = (cpp_num_part) 1 << i;
1717 for (; ; i--, mask >>= 1)
1718 if (rhs.low & mask)
1719 break;
1721 else
1723 if (!pfile->state.skip_eval)
1724 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
1725 "division by zero in #if");
1726 return lhs;
1729 /* First nonzero bit of RHS is bit I. Do naive division by
1730 shifting the RHS fully left, and subtracting from LHS if LHS is
1731 at least as big, and then repeating but with one less shift.
1732 This is not very efficient, but is easy to understand. */
1734 rhs.unsignedp = true;
1735 lhs.unsignedp = true;
1736 i = precision - i - 1;
1737 sub = num_lshift (rhs, precision, i);
1739 result.high = result.low = 0;
1740 for (;;)
1742 if (num_greater_eq (lhs, sub, precision))
1744 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1745 if (i >= PART_PRECISION)
1746 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1747 else
1748 result.low |= (cpp_num_part) 1 << i;
1750 if (i-- == 0)
1751 break;
1752 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1753 sub.high >>= 1;
1756 /* We divide so that the remainder has the sign of the LHS. */
1757 if (op == CPP_DIV)
1759 result.unsignedp = unsignedp;
1760 result.overflow = false;
1761 if (!unsignedp)
1763 if (negate)
1764 result = num_negate (result, precision);
1765 result.overflow = (num_positive (result, precision) ^ !negate
1766 && !num_zerop (result));
1769 return result;
1772 /* CPP_MOD. */
1773 lhs.unsignedp = unsignedp;
1774 lhs.overflow = false;
1775 if (lhs_neg)
1776 lhs = num_negate (lhs, precision);
1778 return lhs;