2009-10-28 Paolo Bonzini <bonzini@gnu.org>
[official-gcc.git] / libcpp / expr.c
blob96dd2fde24ca93e538b3a40083c4fa59dc70fbce
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2004, 2008, 2009 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;
233 /* If the lexer has done its job, length one can only be a single
234 digit. Fast-path this very common case. */
235 if (token->val.str.len == 1)
236 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
238 limit = str + token->val.str.len;
239 float_flag = NOT_FLOAT;
240 max_digit = 0;
241 radix = 10;
243 /* First, interpret the radix. */
244 if (*str == '0')
246 radix = 8;
247 str++;
249 /* Require at least one hex digit to classify it as hex. */
250 if ((*str == 'x' || *str == 'X')
251 && (str[1] == '.' || ISXDIGIT (str[1])))
253 radix = 16;
254 str++;
256 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
258 radix = 2;
259 str++;
263 /* Now scan for a well-formed integer or float. */
264 for (;;)
266 unsigned int c = *str++;
268 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
270 c = hex_value (c);
271 if (c > max_digit)
272 max_digit = c;
274 else if (c == '.')
276 if (float_flag == NOT_FLOAT)
277 float_flag = AFTER_POINT;
278 else
279 SYNTAX_ERROR ("too many decimal points in number");
281 else if ((radix <= 10 && (c == 'e' || c == 'E'))
282 || (radix == 16 && (c == 'p' || c == 'P')))
284 float_flag = AFTER_EXPON;
285 break;
287 else
289 /* Start of suffix. */
290 str--;
291 break;
295 /* The suffix may be for decimal fixed-point constants without exponent. */
296 if (radix != 16 && float_flag == NOT_FLOAT)
298 result = interpret_float_suffix (str, limit - str);
299 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
301 result |= CPP_N_FLOATING;
302 /* We need to restore the radix to 10, if the radix is 8. */
303 if (radix == 8)
304 radix = 10;
306 if (CPP_PEDANTIC (pfile))
307 cpp_error (pfile, CPP_DL_PEDWARN,
308 "fixed-point constants are a GCC extension");
309 goto syntax_ok;
311 else
312 result = 0;
315 if (float_flag != NOT_FLOAT && radix == 8)
316 radix = 10;
318 if (max_digit >= radix)
320 if (radix == 2)
321 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
322 else
323 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
326 if (float_flag != NOT_FLOAT)
328 if (radix == 2)
330 cpp_error (pfile, CPP_DL_ERROR,
331 "invalid prefix \"0b\" for floating constant");
332 return CPP_N_INVALID;
335 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
336 cpp_error (pfile, CPP_DL_PEDWARN,
337 "use of C99 hexadecimal floating constant");
339 if (float_flag == AFTER_EXPON)
341 if (*str == '+' || *str == '-')
342 str++;
344 /* Exponent is decimal, even if string is a hex float. */
345 if (!ISDIGIT (*str))
346 SYNTAX_ERROR ("exponent has no digits");
349 str++;
350 while (ISDIGIT (*str));
352 else if (radix == 16)
353 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
355 result = interpret_float_suffix (str, limit - str);
356 if (result == 0)
358 cpp_error (pfile, CPP_DL_ERROR,
359 "invalid suffix \"%.*s\" on floating constant",
360 (int) (limit - str), str);
361 return CPP_N_INVALID;
364 /* Traditional C didn't accept any floating suffixes. */
365 if (limit != str
366 && CPP_WTRADITIONAL (pfile)
367 && ! cpp_sys_macro_p (pfile))
368 cpp_error (pfile, CPP_DL_WARNING,
369 "traditional C rejects the \"%.*s\" suffix",
370 (int) (limit - str), str);
372 /* A suffix for double is a GCC extension via decimal float support.
373 If the suffix also specifies an imaginary value we'll catch that
374 later. */
375 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
376 cpp_error (pfile, CPP_DL_PEDWARN,
377 "suffix for double constant is a GCC extension");
379 /* Radix must be 10 for decimal floats. */
380 if ((result & CPP_N_DFLOAT) && radix != 10)
382 cpp_error (pfile, CPP_DL_ERROR,
383 "invalid suffix \"%.*s\" with hexadecimal floating constant",
384 (int) (limit - str), str);
385 return CPP_N_INVALID;
388 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
389 cpp_error (pfile, CPP_DL_PEDWARN,
390 "fixed-point constants are a GCC extension");
392 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
393 cpp_error (pfile, CPP_DL_PEDWARN,
394 "decimal float constants are a GCC extension");
396 result |= CPP_N_FLOATING;
398 else
400 result = interpret_int_suffix (str, limit - str);
401 if (result == 0)
403 cpp_error (pfile, CPP_DL_ERROR,
404 "invalid suffix \"%.*s\" on integer constant",
405 (int) (limit - str), str);
406 return CPP_N_INVALID;
409 /* Traditional C only accepted the 'L' suffix.
410 Suppress warning about 'LL' with -Wno-long-long. */
411 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
413 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
414 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
416 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
417 cpp_error (pfile, CPP_DL_WARNING,
418 "traditional C rejects the \"%.*s\" suffix",
419 (int) (limit - str), str);
422 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
423 && CPP_OPTION (pfile, warn_long_long))
424 cpp_error (pfile,
425 CPP_OPTION (pfile, c99) ? CPP_DL_WARNING : CPP_DL_PEDWARN,
426 CPP_OPTION (pfile, cplusplus)
427 ? "use of C++0x long long integer constant"
428 : "use of C99 long long integer constant");
430 result |= CPP_N_INTEGER;
433 syntax_ok:
434 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
435 cpp_error (pfile, CPP_DL_PEDWARN,
436 "imaginary constants are a GCC extension");
437 if (radix == 2 && CPP_PEDANTIC (pfile))
438 cpp_error (pfile, CPP_DL_PEDWARN,
439 "binary constants are a GCC extension");
441 if (radix == 10)
442 result |= CPP_N_DECIMAL;
443 else if (radix == 16)
444 result |= CPP_N_HEX;
445 else if (radix == 2)
446 result |= CPP_N_BINARY;
447 else
448 result |= CPP_N_OCTAL;
450 return result;
452 syntax_error:
453 return CPP_N_INVALID;
456 /* cpp_interpret_integer converts an integer constant into a cpp_num,
457 of precision options->precision.
459 We do not provide any interface for decimal->float conversion,
460 because the preprocessor doesn't need it and we don't want to
461 drag in GCC's floating point emulator. */
462 cpp_num
463 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
464 unsigned int type)
466 const uchar *p, *end;
467 cpp_num result;
469 result.low = 0;
470 result.high = 0;
471 result.unsignedp = !!(type & CPP_N_UNSIGNED);
472 result.overflow = false;
474 p = token->val.str.text;
475 end = p + token->val.str.len;
477 /* Common case of a single digit. */
478 if (token->val.str.len == 1)
479 result.low = p[0] - '0';
480 else
482 cpp_num_part max;
483 size_t precision = CPP_OPTION (pfile, precision);
484 unsigned int base = 10, c = 0;
485 bool overflow = false;
487 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
489 base = 8;
490 p++;
492 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
494 base = 16;
495 p += 2;
497 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
499 base = 2;
500 p += 2;
503 /* We can add a digit to numbers strictly less than this without
504 needing the precision and slowness of double integers. */
505 max = ~(cpp_num_part) 0;
506 if (precision < PART_PRECISION)
507 max >>= PART_PRECISION - precision;
508 max = (max - base + 1) / base + 1;
510 for (; p < end; p++)
512 c = *p;
514 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
515 c = hex_value (c);
516 else
517 break;
519 /* Strict inequality for when max is set to zero. */
520 if (result.low < max)
521 result.low = result.low * base + c;
522 else
524 result = append_digit (result, c, base, precision);
525 overflow |= result.overflow;
526 max = 0;
530 if (overflow)
531 cpp_error (pfile, CPP_DL_PEDWARN,
532 "integer constant is too large for its type");
533 /* If too big to be signed, consider it unsigned. Only warn for
534 decimal numbers. Traditional numbers were always signed (but
535 we still honor an explicit U suffix); but we only have
536 traditional semantics in directives. */
537 else if (!result.unsignedp
538 && !(CPP_OPTION (pfile, traditional)
539 && pfile->state.in_directive)
540 && !num_positive (result, precision))
542 /* This is for constants within the range of uintmax_t but
543 not that of intmax_t. For such decimal constants, a
544 diagnostic is required for C99 as the selected type must
545 be signed and not having a type is a constraint violation
546 (DR#298, TC3), so this must be a pedwarn. For C90,
547 unsigned long is specified to be used for a constant that
548 does not fit in signed long; if uintmax_t has the same
549 range as unsigned long this means only a warning is
550 appropriate here. C90 permits the preprocessor to use a
551 wider range than unsigned long in the compiler, so if
552 uintmax_t is wider than unsigned long no diagnostic is
553 required for such constants in preprocessor #if
554 expressions and the compiler will pedwarn for such
555 constants outside the range of unsigned long that reach
556 the compiler so a diagnostic is not required there
557 either; thus, pedwarn for C99 but use a plain warning for
558 C90. */
559 if (base == 10)
560 cpp_error (pfile, (CPP_OPTION (pfile, c99)
561 ? CPP_DL_PEDWARN
562 : CPP_DL_WARNING),
563 "integer constant is so large that it is unsigned");
564 result.unsignedp = true;
568 return result;
571 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
572 static cpp_num
573 append_digit (cpp_num num, int digit, int base, size_t precision)
575 cpp_num result;
576 unsigned int shift;
577 bool overflow;
578 cpp_num_part add_high, add_low;
580 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
581 need to worry about add_high overflowing. */
582 switch (base)
584 case 2:
585 shift = 1;
586 break;
588 case 16:
589 shift = 4;
590 break;
592 default:
593 shift = 3;
595 overflow = !!(num.high >> (PART_PRECISION - shift));
596 result.high = num.high << shift;
597 result.low = num.low << shift;
598 result.high |= num.low >> (PART_PRECISION - shift);
599 result.unsignedp = num.unsignedp;
601 if (base == 10)
603 add_low = num.low << 1;
604 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
606 else
607 add_high = add_low = 0;
609 if (add_low + digit < add_low)
610 add_high++;
611 add_low += digit;
613 if (result.low + add_low < result.low)
614 add_high++;
615 if (result.high + add_high < result.high)
616 overflow = true;
618 result.low += add_low;
619 result.high += add_high;
620 result.overflow = overflow;
622 /* The above code catches overflow of a cpp_num type. This catches
623 overflow of the (possibly shorter) target precision. */
624 num.low = result.low;
625 num.high = result.high;
626 result = num_trim (result, precision);
627 if (!num_eq (result, num))
628 result.overflow = true;
630 return result;
633 /* Handle meeting "defined" in a preprocessor expression. */
634 static cpp_num
635 parse_defined (cpp_reader *pfile)
637 cpp_num result;
638 int paren = 0;
639 cpp_hashnode *node = 0;
640 const cpp_token *token;
641 cpp_context *initial_context = pfile->context;
643 /* Don't expand macros. */
644 pfile->state.prevent_expansion++;
646 token = cpp_get_token (pfile);
647 if (token->type == CPP_OPEN_PAREN)
649 paren = 1;
650 token = cpp_get_token (pfile);
653 if (token->type == CPP_NAME)
655 node = token->val.node.node;
656 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
658 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
659 node = 0;
662 else
664 cpp_error (pfile, CPP_DL_ERROR,
665 "operator \"defined\" requires an identifier");
666 if (token->flags & NAMED_OP)
668 cpp_token op;
670 op.flags = 0;
671 op.type = token->type;
672 cpp_error (pfile, CPP_DL_ERROR,
673 "(\"%s\" is an alternative token for \"%s\" in C++)",
674 cpp_token_as_text (pfile, token),
675 cpp_token_as_text (pfile, &op));
679 if (node)
681 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
682 cpp_error (pfile, CPP_DL_WARNING,
683 "this use of \"defined\" may not be portable");
685 _cpp_mark_macro_used (node);
686 if (!(node->flags & NODE_USED))
688 node->flags |= NODE_USED;
689 if (node->type == NT_MACRO)
691 if (pfile->cb.used_define)
692 pfile->cb.used_define (pfile, pfile->directive_line, node);
694 else
696 if (pfile->cb.used_undef)
697 pfile->cb.used_undef (pfile, pfile->directive_line, node);
701 /* A possible controlling macro of the form #if !defined ().
702 _cpp_parse_expr checks there was no other junk on the line. */
703 pfile->mi_ind_cmacro = node;
706 pfile->state.prevent_expansion--;
708 result.unsignedp = false;
709 result.high = 0;
710 result.overflow = false;
711 result.low = node && node->type == NT_MACRO;
712 return result;
715 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
716 number or character constant, or the result of the "defined" or "#"
717 operators). */
718 static cpp_num
719 eval_token (cpp_reader *pfile, const cpp_token *token)
721 cpp_num result;
722 unsigned int temp;
723 int unsignedp = 0;
725 result.unsignedp = false;
726 result.overflow = false;
728 switch (token->type)
730 case CPP_NUMBER:
731 temp = cpp_classify_number (pfile, token);
732 switch (temp & CPP_N_CATEGORY)
734 case CPP_N_FLOATING:
735 cpp_error (pfile, CPP_DL_ERROR,
736 "floating constant in preprocessor expression");
737 break;
738 case CPP_N_INTEGER:
739 if (!(temp & CPP_N_IMAGINARY))
740 return cpp_interpret_integer (pfile, token, temp);
741 cpp_error (pfile, CPP_DL_ERROR,
742 "imaginary number in preprocessor expression");
743 break;
745 case CPP_N_INVALID:
746 /* Error already issued. */
747 break;
749 result.high = result.low = 0;
750 break;
752 case CPP_WCHAR:
753 case CPP_CHAR:
754 case CPP_CHAR16:
755 case CPP_CHAR32:
757 cppchar_t cc = cpp_interpret_charconst (pfile, token,
758 &temp, &unsignedp);
760 result.high = 0;
761 result.low = cc;
762 /* Sign-extend the result if necessary. */
763 if (!unsignedp && (cppchar_signed_t) cc < 0)
765 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
766 result.low |= ~(~(cpp_num_part) 0
767 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
768 result.high = ~(cpp_num_part) 0;
769 result = num_trim (result, CPP_OPTION (pfile, precision));
772 break;
774 case CPP_NAME:
775 if (token->val.node.node == pfile->spec_nodes.n_defined)
776 return parse_defined (pfile);
777 else if (CPP_OPTION (pfile, cplusplus)
778 && (token->val.node.node == pfile->spec_nodes.n_true
779 || token->val.node.node == pfile->spec_nodes.n_false))
781 result.high = 0;
782 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
784 else
786 result.high = 0;
787 result.low = 0;
788 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
789 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined",
790 NODE_NAME (token->val.node.node));
792 break;
794 case CPP_HASH:
795 if (!pfile->state.skipping)
797 /* A pedantic warning takes precedence over a deprecated
798 warning here. */
799 if (CPP_PEDANTIC (pfile))
800 cpp_error (pfile, CPP_DL_PEDWARN,
801 "assertions are a GCC extension");
802 else if (CPP_OPTION (pfile, warn_deprecated))
803 cpp_error (pfile, CPP_DL_WARNING,
804 "assertions are a deprecated extension");
806 _cpp_test_assertion (pfile, &temp);
807 result.high = 0;
808 result.low = temp;
809 break;
811 default:
812 abort ();
815 result.unsignedp = !!unsignedp;
816 return result;
819 /* Operator precedence and flags table.
821 After an operator is returned from the lexer, if it has priority less
822 than the operator on the top of the stack, we reduce the stack by one
823 operator and repeat the test. Since equal priorities do not reduce,
824 this is naturally right-associative.
826 We handle left-associative operators by decrementing the priority of
827 just-lexed operators by one, but retaining the priority of operators
828 already on the stack.
830 The remaining cases are '(' and ')'. We handle '(' by skipping the
831 reduction phase completely. ')' is given lower priority than
832 everything else, including '(', effectively forcing a reduction of the
833 parenthesized expression. If there is a matching '(', the routine
834 reduce() exits immediately. If the normal exit route sees a ')', then
835 there cannot have been a matching '(' and an error message is output.
837 The parser assumes all shifted operators require a left operand unless
838 the flag NO_L_OPERAND is set. These semantics are automatic; any
839 extra semantics need to be handled with operator-specific code. */
841 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
842 operand changes because of integer promotions. */
843 #define NO_L_OPERAND (1 << 0)
844 #define LEFT_ASSOC (1 << 1)
845 #define CHECK_PROMOTION (1 << 2)
847 /* Operator to priority map. Must be in the same order as the first
848 N entries of enum cpp_ttype. */
849 static const struct cpp_operator
851 uchar prio;
852 uchar flags;
853 } optab[] =
855 /* EQ */ {0, 0}, /* Shouldn't happen. */
856 /* NOT */ {16, NO_L_OPERAND},
857 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
858 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
859 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
860 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
861 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
862 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
863 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
864 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
865 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
866 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
867 /* RSHIFT */ {13, LEFT_ASSOC},
868 /* LSHIFT */ {13, LEFT_ASSOC},
870 /* COMPL */ {16, NO_L_OPERAND},
871 /* AND_AND */ {6, LEFT_ASSOC},
872 /* OR_OR */ {5, LEFT_ASSOC},
873 /* Note that QUERY, COLON, and COMMA must have the same precedence.
874 However, there are some special cases for these in reduce(). */
875 /* QUERY */ {4, 0},
876 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
877 /* COMMA */ {4, LEFT_ASSOC},
878 /* OPEN_PAREN */ {1, NO_L_OPERAND},
879 /* CLOSE_PAREN */ {0, 0},
880 /* EOF */ {0, 0},
881 /* EQ_EQ */ {11, LEFT_ASSOC},
882 /* NOT_EQ */ {11, LEFT_ASSOC},
883 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
884 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
885 /* UPLUS */ {16, NO_L_OPERAND},
886 /* UMINUS */ {16, NO_L_OPERAND}
889 /* Parse and evaluate a C expression, reading from PFILE.
890 Returns the truth value of the expression.
892 The implementation is an operator precedence parser, i.e. a
893 bottom-up parser, using a stack for not-yet-reduced tokens.
895 The stack base is op_stack, and the current stack pointer is 'top'.
896 There is a stack element for each operator (only), and the most
897 recently pushed operator is 'top->op'. An operand (value) is
898 stored in the 'value' field of the stack element of the operator
899 that precedes it. */
900 bool
901 _cpp_parse_expr (cpp_reader *pfile, bool is_if)
903 struct op *top = pfile->op_stack;
904 unsigned int lex_count;
905 bool saw_leading_not, want_value = true;
907 pfile->state.skip_eval = 0;
909 /* Set up detection of #if ! defined(). */
910 pfile->mi_ind_cmacro = 0;
911 saw_leading_not = false;
912 lex_count = 0;
914 /* Lowest priority operator prevents further reductions. */
915 top->op = CPP_EOF;
917 for (;;)
919 struct op op;
921 lex_count++;
922 op.token = cpp_get_token (pfile);
923 op.op = op.token->type;
924 op.loc = op.token->src_loc;
926 switch (op.op)
928 /* These tokens convert into values. */
929 case CPP_NUMBER:
930 case CPP_CHAR:
931 case CPP_WCHAR:
932 case CPP_CHAR16:
933 case CPP_CHAR32:
934 case CPP_NAME:
935 case CPP_HASH:
936 if (!want_value)
937 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
938 cpp_token_as_text (pfile, op.token));
939 want_value = false;
940 top->value = eval_token (pfile, op.token);
941 continue;
943 case CPP_NOT:
944 saw_leading_not = lex_count == 1;
945 break;
946 case CPP_PLUS:
947 if (want_value)
948 op.op = CPP_UPLUS;
949 break;
950 case CPP_MINUS:
951 if (want_value)
952 op.op = CPP_UMINUS;
953 break;
955 default:
956 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
957 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
958 cpp_token_as_text (pfile, op.token));
959 break;
962 /* Check we have a value or operator as appropriate. */
963 if (optab[op.op].flags & NO_L_OPERAND)
965 if (!want_value)
966 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
967 cpp_token_as_text (pfile, op.token));
969 else if (want_value)
971 /* We want a number (or expression) and haven't got one.
972 Try to emit a specific diagnostic. */
973 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
974 SYNTAX_ERROR ("missing expression between '(' and ')'");
976 if (op.op == CPP_EOF && top->op == CPP_EOF)
977 SYNTAX_ERROR2 ("%s with no expression", is_if ? "#if" : "#elif");
979 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
980 SYNTAX_ERROR2 ("operator '%s' has no right operand",
981 cpp_token_as_text (pfile, top->token));
982 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
983 /* Complain about missing paren during reduction. */;
984 else
985 SYNTAX_ERROR2 ("operator '%s' has no left operand",
986 cpp_token_as_text (pfile, op.token));
989 top = reduce (pfile, top, op.op);
990 if (!top)
991 goto syntax_error;
993 if (op.op == CPP_EOF)
994 break;
996 switch (op.op)
998 case CPP_CLOSE_PAREN:
999 continue;
1000 case CPP_OR_OR:
1001 if (!num_zerop (top->value))
1002 pfile->state.skip_eval++;
1003 break;
1004 case CPP_AND_AND:
1005 case CPP_QUERY:
1006 if (num_zerop (top->value))
1007 pfile->state.skip_eval++;
1008 break;
1009 case CPP_COLON:
1010 if (top->op != CPP_QUERY)
1011 SYNTAX_ERROR (" ':' without preceding '?'");
1012 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
1013 pfile->state.skip_eval++;
1014 else
1015 pfile->state.skip_eval--;
1016 default:
1017 break;
1020 want_value = true;
1022 /* Check for and handle stack overflow. */
1023 if (++top == pfile->op_limit)
1024 top = _cpp_expand_op_stack (pfile);
1026 top->op = op.op;
1027 top->token = op.token;
1028 top->loc = op.token->src_loc;
1031 /* The controlling macro expression is only valid if we called lex 3
1032 times: <!> <defined expression> and <EOF>. push_conditional ()
1033 checks that we are at top-of-file. */
1034 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1035 pfile->mi_ind_cmacro = 0;
1037 if (top != pfile->op_stack)
1039 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in %s",
1040 is_if ? "#if" : "#elif");
1041 syntax_error:
1042 return false; /* Return false on syntax error. */
1045 return !num_zerop (top->value);
1048 /* Reduce the operator / value stack if possible, in preparation for
1049 pushing operator OP. Returns NULL on error, otherwise the top of
1050 the stack. */
1051 static struct op *
1052 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1054 unsigned int prio;
1056 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1058 bad_op:
1059 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1060 return 0;
1063 if (op == CPP_OPEN_PAREN)
1064 return top;
1066 /* Decrement the priority of left-associative operators to force a
1067 reduction with operators of otherwise equal priority. */
1068 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1069 while (prio < optab[top->op].prio)
1071 if (CPP_OPTION (pfile, warn_num_sign_change)
1072 && optab[top->op].flags & CHECK_PROMOTION)
1073 check_promotion (pfile, top);
1075 switch (top->op)
1077 case CPP_UPLUS:
1078 case CPP_UMINUS:
1079 case CPP_NOT:
1080 case CPP_COMPL:
1081 top[-1].value = num_unary_op (pfile, top->value, top->op);
1082 top[-1].loc = top->loc;
1083 break;
1085 case CPP_PLUS:
1086 case CPP_MINUS:
1087 case CPP_RSHIFT:
1088 case CPP_LSHIFT:
1089 case CPP_COMMA:
1090 top[-1].value = num_binary_op (pfile, top[-1].value,
1091 top->value, top->op);
1092 top[-1].loc = top->loc;
1093 break;
1095 case CPP_GREATER:
1096 case CPP_LESS:
1097 case CPP_GREATER_EQ:
1098 case CPP_LESS_EQ:
1099 top[-1].value
1100 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1101 top[-1].loc = top->loc;
1102 break;
1104 case CPP_EQ_EQ:
1105 case CPP_NOT_EQ:
1106 top[-1].value
1107 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1108 top[-1].loc = top->loc;
1109 break;
1111 case CPP_AND:
1112 case CPP_OR:
1113 case CPP_XOR:
1114 top[-1].value
1115 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1116 top[-1].loc = top->loc;
1117 break;
1119 case CPP_MULT:
1120 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1121 top[-1].loc = top->loc;
1122 break;
1124 case CPP_DIV:
1125 case CPP_MOD:
1126 top[-1].value = num_div_op (pfile, top[-1].value,
1127 top->value, top->op, top->loc);
1128 top[-1].loc = top->loc;
1129 break;
1131 case CPP_OR_OR:
1132 top--;
1133 if (!num_zerop (top->value))
1134 pfile->state.skip_eval--;
1135 top->value.low = (!num_zerop (top->value)
1136 || !num_zerop (top[1].value));
1137 top->value.high = 0;
1138 top->value.unsignedp = false;
1139 top->value.overflow = false;
1140 top->loc = top[1].loc;
1141 continue;
1143 case CPP_AND_AND:
1144 top--;
1145 if (num_zerop (top->value))
1146 pfile->state.skip_eval--;
1147 top->value.low = (!num_zerop (top->value)
1148 && !num_zerop (top[1].value));
1149 top->value.high = 0;
1150 top->value.unsignedp = false;
1151 top->value.overflow = false;
1152 top->loc = top[1].loc;
1153 continue;
1155 case CPP_OPEN_PAREN:
1156 if (op != CPP_CLOSE_PAREN)
1158 cpp_error_with_line (pfile, CPP_DL_ERROR,
1159 top->token->src_loc,
1160 0, "missing ')' in expression");
1161 return 0;
1163 top--;
1164 top->value = top[1].value;
1165 top->loc = top[1].loc;
1166 return top;
1168 case CPP_COLON:
1169 top -= 2;
1170 if (!num_zerop (top->value))
1172 pfile->state.skip_eval--;
1173 top->value = top[1].value;
1174 top->loc = top[1].loc;
1176 else
1178 top->value = top[2].value;
1179 top->loc = top[2].loc;
1181 top->value.unsignedp = (top[1].value.unsignedp
1182 || top[2].value.unsignedp);
1183 continue;
1185 case CPP_QUERY:
1186 /* COMMA and COLON should not reduce a QUERY operator. */
1187 if (op == CPP_COMMA || op == CPP_COLON)
1188 return top;
1189 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1190 return 0;
1192 default:
1193 goto bad_op;
1196 top--;
1197 if (top->value.overflow && !pfile->state.skip_eval)
1198 cpp_error (pfile, CPP_DL_PEDWARN,
1199 "integer overflow in preprocessor expression");
1202 if (op == CPP_CLOSE_PAREN)
1204 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1205 return 0;
1208 return top;
1211 /* Returns the position of the old top of stack after expansion. */
1212 struct op *
1213 _cpp_expand_op_stack (cpp_reader *pfile)
1215 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1216 size_t new_size = old_size * 2 + 20;
1218 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1219 pfile->op_limit = pfile->op_stack + new_size;
1221 return pfile->op_stack + old_size;
1224 /* Emits a warning if the effective sign of either operand of OP
1225 changes because of integer promotions. */
1226 static void
1227 check_promotion (cpp_reader *pfile, const struct op *op)
1229 if (op->value.unsignedp == op[-1].value.unsignedp)
1230 return;
1232 if (op->value.unsignedp)
1234 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1235 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1236 "the left operand of \"%s\" changes sign when promoted",
1237 cpp_token_as_text (pfile, op->token));
1239 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1240 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1241 "the right operand of \"%s\" changes sign when promoted",
1242 cpp_token_as_text (pfile, op->token));
1245 /* Clears the unused high order bits of the number pointed to by PNUM. */
1246 static cpp_num
1247 num_trim (cpp_num num, size_t precision)
1249 if (precision > PART_PRECISION)
1251 precision -= PART_PRECISION;
1252 if (precision < PART_PRECISION)
1253 num.high &= ((cpp_num_part) 1 << precision) - 1;
1255 else
1257 if (precision < PART_PRECISION)
1258 num.low &= ((cpp_num_part) 1 << precision) - 1;
1259 num.high = 0;
1262 return num;
1265 /* True iff A (presumed signed) >= 0. */
1266 static bool
1267 num_positive (cpp_num num, size_t precision)
1269 if (precision > PART_PRECISION)
1271 precision -= PART_PRECISION;
1272 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1275 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1278 /* Sign extend a number, with PRECISION significant bits and all
1279 others assumed clear, to fill out a cpp_num structure. */
1280 cpp_num
1281 cpp_num_sign_extend (cpp_num num, size_t precision)
1283 if (!num.unsignedp)
1285 if (precision > PART_PRECISION)
1287 precision -= PART_PRECISION;
1288 if (precision < PART_PRECISION
1289 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1290 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1292 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1294 if (precision < PART_PRECISION)
1295 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1296 num.high = ~(cpp_num_part) 0;
1300 return num;
1303 /* Returns the negative of NUM. */
1304 static cpp_num
1305 num_negate (cpp_num num, size_t precision)
1307 cpp_num copy;
1309 copy = num;
1310 num.high = ~num.high;
1311 num.low = ~num.low;
1312 if (++num.low == 0)
1313 num.high++;
1314 num = num_trim (num, precision);
1315 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1317 return num;
1320 /* Returns true if A >= B. */
1321 static bool
1322 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1324 bool unsignedp;
1326 unsignedp = pa.unsignedp || pb.unsignedp;
1328 if (!unsignedp)
1330 /* Both numbers have signed type. If they are of different
1331 sign, the answer is the sign of A. */
1332 unsignedp = num_positive (pa, precision);
1334 if (unsignedp != num_positive (pb, precision))
1335 return unsignedp;
1337 /* Otherwise we can do an unsigned comparison. */
1340 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1343 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1344 static cpp_num
1345 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1346 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1348 lhs.overflow = false;
1349 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1351 /* As excess precision is zeroed, there is no need to num_trim () as
1352 these operations cannot introduce a set bit there. */
1353 if (op == CPP_AND)
1355 lhs.low &= rhs.low;
1356 lhs.high &= rhs.high;
1358 else if (op == CPP_OR)
1360 lhs.low |= rhs.low;
1361 lhs.high |= rhs.high;
1363 else
1365 lhs.low ^= rhs.low;
1366 lhs.high ^= rhs.high;
1369 return lhs;
1372 /* Returns LHS OP RHS, where OP is an inequality. */
1373 static cpp_num
1374 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1375 enum cpp_ttype op)
1377 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1379 if (op == CPP_GREATER_EQ)
1380 lhs.low = gte;
1381 else if (op == CPP_LESS)
1382 lhs.low = !gte;
1383 else if (op == CPP_GREATER)
1384 lhs.low = gte && !num_eq (lhs, rhs);
1385 else /* CPP_LESS_EQ. */
1386 lhs.low = !gte || num_eq (lhs, rhs);
1388 lhs.high = 0;
1389 lhs.overflow = false;
1390 lhs.unsignedp = false;
1391 return lhs;
1394 /* Returns LHS OP RHS, where OP is == or !=. */
1395 static cpp_num
1396 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1397 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1399 /* Work around a 3.0.4 bug; see PR 6950. */
1400 bool eq = num_eq (lhs, rhs);
1401 if (op == CPP_NOT_EQ)
1402 eq = !eq;
1403 lhs.low = eq;
1404 lhs.high = 0;
1405 lhs.overflow = false;
1406 lhs.unsignedp = false;
1407 return lhs;
1410 /* Shift NUM, of width PRECISION, right by N bits. */
1411 static cpp_num
1412 num_rshift (cpp_num num, size_t precision, size_t n)
1414 cpp_num_part sign_mask;
1415 bool x = num_positive (num, precision);
1417 if (num.unsignedp || x)
1418 sign_mask = 0;
1419 else
1420 sign_mask = ~(cpp_num_part) 0;
1422 if (n >= precision)
1423 num.high = num.low = sign_mask;
1424 else
1426 /* Sign-extend. */
1427 if (precision < PART_PRECISION)
1428 num.high = sign_mask, num.low |= sign_mask << precision;
1429 else if (precision < 2 * PART_PRECISION)
1430 num.high |= sign_mask << (precision - PART_PRECISION);
1432 if (n >= PART_PRECISION)
1434 n -= PART_PRECISION;
1435 num.low = num.high;
1436 num.high = sign_mask;
1439 if (n)
1441 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1442 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1446 num = num_trim (num, precision);
1447 num.overflow = false;
1448 return num;
1451 /* Shift NUM, of width PRECISION, left by N bits. */
1452 static cpp_num
1453 num_lshift (cpp_num num, size_t precision, size_t n)
1455 if (n >= precision)
1457 num.overflow = !num.unsignedp && !num_zerop (num);
1458 num.high = num.low = 0;
1460 else
1462 cpp_num orig, maybe_orig;
1463 size_t m = n;
1465 orig = num;
1466 if (m >= PART_PRECISION)
1468 m -= PART_PRECISION;
1469 num.high = num.low;
1470 num.low = 0;
1472 if (m)
1474 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1475 num.low <<= m;
1477 num = num_trim (num, precision);
1479 if (num.unsignedp)
1480 num.overflow = false;
1481 else
1483 maybe_orig = num_rshift (num, precision, n);
1484 num.overflow = !num_eq (orig, maybe_orig);
1488 return num;
1491 /* The four unary operators: +, -, ! and ~. */
1492 static cpp_num
1493 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1495 switch (op)
1497 case CPP_UPLUS:
1498 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1499 cpp_error (pfile, CPP_DL_WARNING,
1500 "traditional C rejects the unary plus operator");
1501 num.overflow = false;
1502 break;
1504 case CPP_UMINUS:
1505 num = num_negate (num, CPP_OPTION (pfile, precision));
1506 break;
1508 case CPP_COMPL:
1509 num.high = ~num.high;
1510 num.low = ~num.low;
1511 num = num_trim (num, CPP_OPTION (pfile, precision));
1512 num.overflow = false;
1513 break;
1515 default: /* case CPP_NOT: */
1516 num.low = num_zerop (num);
1517 num.high = 0;
1518 num.overflow = false;
1519 num.unsignedp = false;
1520 break;
1523 return num;
1526 /* The various binary operators. */
1527 static cpp_num
1528 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1530 cpp_num result;
1531 size_t precision = CPP_OPTION (pfile, precision);
1532 size_t n;
1534 switch (op)
1536 /* Shifts. */
1537 case CPP_LSHIFT:
1538 case CPP_RSHIFT:
1539 if (!rhs.unsignedp && !num_positive (rhs, precision))
1541 /* A negative shift is a positive shift the other way. */
1542 if (op == CPP_LSHIFT)
1543 op = CPP_RSHIFT;
1544 else
1545 op = CPP_LSHIFT;
1546 rhs = num_negate (rhs, precision);
1548 if (rhs.high)
1549 n = ~0; /* Maximal. */
1550 else
1551 n = rhs.low;
1552 if (op == CPP_LSHIFT)
1553 lhs = num_lshift (lhs, precision, n);
1554 else
1555 lhs = num_rshift (lhs, precision, n);
1556 break;
1558 /* Arithmetic. */
1559 case CPP_MINUS:
1560 rhs = num_negate (rhs, precision);
1561 case CPP_PLUS:
1562 result.low = lhs.low + rhs.low;
1563 result.high = lhs.high + rhs.high;
1564 if (result.low < lhs.low)
1565 result.high++;
1566 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1567 result.overflow = false;
1569 result = num_trim (result, precision);
1570 if (!result.unsignedp)
1572 bool lhsp = num_positive (lhs, precision);
1573 result.overflow = (lhsp == num_positive (rhs, precision)
1574 && lhsp != num_positive (result, precision));
1576 return result;
1578 /* Comma. */
1579 default: /* case CPP_COMMA: */
1580 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1581 || !pfile->state.skip_eval))
1582 cpp_error (pfile, CPP_DL_PEDWARN,
1583 "comma operator in operand of #if");
1584 lhs = rhs;
1585 break;
1588 return lhs;
1591 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1592 cannot overflow. */
1593 static cpp_num
1594 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1596 cpp_num result;
1597 cpp_num_part middle[2], temp;
1599 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1600 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1602 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1603 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1605 temp = result.low;
1606 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1607 if (result.low < temp)
1608 result.high++;
1610 temp = result.low;
1611 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1612 if (result.low < temp)
1613 result.high++;
1615 result.high += HIGH_PART (middle[0]);
1616 result.high += HIGH_PART (middle[1]);
1617 result.unsignedp = true;
1618 result.overflow = false;
1620 return result;
1623 /* Multiply two preprocessing numbers. */
1624 static cpp_num
1625 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1627 cpp_num result, temp;
1628 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1629 bool overflow, negate = false;
1630 size_t precision = CPP_OPTION (pfile, precision);
1632 /* Prepare for unsigned multiplication. */
1633 if (!unsignedp)
1635 if (!num_positive (lhs, precision))
1636 negate = !negate, lhs = num_negate (lhs, precision);
1637 if (!num_positive (rhs, precision))
1638 negate = !negate, rhs = num_negate (rhs, precision);
1641 overflow = lhs.high && rhs.high;
1642 result = num_part_mul (lhs.low, rhs.low);
1644 temp = num_part_mul (lhs.high, rhs.low);
1645 result.high += temp.low;
1646 if (temp.high)
1647 overflow = true;
1649 temp = num_part_mul (lhs.low, rhs.high);
1650 result.high += temp.low;
1651 if (temp.high)
1652 overflow = true;
1654 temp.low = result.low, temp.high = result.high;
1655 result = num_trim (result, precision);
1656 if (!num_eq (result, temp))
1657 overflow = true;
1659 if (negate)
1660 result = num_negate (result, precision);
1662 if (unsignedp)
1663 result.overflow = false;
1664 else
1665 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1666 && !num_zerop (result));
1667 result.unsignedp = unsignedp;
1669 return result;
1672 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
1673 or the remainder depending upon OP. LOCATION is the source location
1674 of this operator (for diagnostics). */
1676 static cpp_num
1677 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
1678 source_location location)
1680 cpp_num result, sub;
1681 cpp_num_part mask;
1682 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1683 bool negate = false, lhs_neg = false;
1684 size_t i, precision = CPP_OPTION (pfile, precision);
1686 /* Prepare for unsigned division. */
1687 if (!unsignedp)
1689 if (!num_positive (lhs, precision))
1690 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1691 if (!num_positive (rhs, precision))
1692 negate = !negate, rhs = num_negate (rhs, precision);
1695 /* Find the high bit. */
1696 if (rhs.high)
1698 i = precision - 1;
1699 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1700 for (; ; i--, mask >>= 1)
1701 if (rhs.high & mask)
1702 break;
1704 else if (rhs.low)
1706 if (precision > PART_PRECISION)
1707 i = precision - PART_PRECISION - 1;
1708 else
1709 i = precision - 1;
1710 mask = (cpp_num_part) 1 << i;
1711 for (; ; i--, mask >>= 1)
1712 if (rhs.low & mask)
1713 break;
1715 else
1717 if (!pfile->state.skip_eval)
1718 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
1719 "division by zero in #if");
1720 return lhs;
1723 /* First nonzero bit of RHS is bit I. Do naive division by
1724 shifting the RHS fully left, and subtracting from LHS if LHS is
1725 at least as big, and then repeating but with one less shift.
1726 This is not very efficient, but is easy to understand. */
1728 rhs.unsignedp = true;
1729 lhs.unsignedp = true;
1730 i = precision - i - 1;
1731 sub = num_lshift (rhs, precision, i);
1733 result.high = result.low = 0;
1734 for (;;)
1736 if (num_greater_eq (lhs, sub, precision))
1738 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1739 if (i >= PART_PRECISION)
1740 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1741 else
1742 result.low |= (cpp_num_part) 1 << i;
1744 if (i-- == 0)
1745 break;
1746 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1747 sub.high >>= 1;
1750 /* We divide so that the remainder has the sign of the LHS. */
1751 if (op == CPP_DIV)
1753 result.unsignedp = unsignedp;
1754 result.overflow = false;
1755 if (!unsignedp)
1757 if (negate)
1758 result = num_negate (result, precision);
1759 result.overflow = (num_positive (result, precision) ^ !negate
1760 && !num_zerop (result));
1763 return result;
1766 /* CPP_MOD. */
1767 lhs.unsignedp = unsignedp;
1768 lhs.overflow = false;
1769 if (lhs_neg)
1770 lhs = num_negate (lhs, precision);
1772 return lhs;