* config.gcc (cygwin tm_file): Add cygwin-stdint.h.
[official-gcc.git] / libcpp / expr.c
blob688439799f926d66d7b3aa8e377499dde463bcd4
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2004, 2008 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 2, 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; if not, write to the Free Software
18 Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "cpplib.h"
24 #include "internal.h"
26 #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
27 #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
28 #define LOW_PART(num_part) (num_part & HALF_MASK)
29 #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
31 struct op
33 const cpp_token *token; /* The token forming op (for diagnostics). */
34 cpp_num value; /* The value logically "right" of op. */
35 source_location loc; /* The location of this value. */
36 enum cpp_ttype op;
39 /* Some simple utility routines on double integers. */
40 #define num_zerop(num) ((num.low | num.high) == 0)
41 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
42 static bool num_positive (cpp_num, size_t);
43 static bool num_greater_eq (cpp_num, cpp_num, size_t);
44 static cpp_num num_trim (cpp_num, size_t);
45 static cpp_num num_part_mul (cpp_num_part, cpp_num_part);
47 static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype);
48 static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
49 static cpp_num num_negate (cpp_num, size_t);
50 static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
51 static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num,
52 enum cpp_ttype);
53 static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num,
54 enum cpp_ttype);
55 static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num);
56 static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
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, c99)
424 && CPP_OPTION (pfile, warn_long_long))
425 cpp_error (pfile, CPP_DL_PEDWARN,
426 "use of C99 long long integer constant");
428 result |= CPP_N_INTEGER;
431 syntax_ok:
432 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
433 cpp_error (pfile, CPP_DL_PEDWARN,
434 "imaginary constants are a GCC extension");
435 if (radix == 2 && CPP_PEDANTIC (pfile))
436 cpp_error (pfile, CPP_DL_PEDWARN,
437 "binary constants are a GCC extension");
439 if (radix == 10)
440 result |= CPP_N_DECIMAL;
441 else if (radix == 16)
442 result |= CPP_N_HEX;
443 else if (radix == 2)
444 result |= CPP_N_BINARY;
445 else
446 result |= CPP_N_OCTAL;
448 return result;
450 syntax_error:
451 return CPP_N_INVALID;
454 /* cpp_interpret_integer converts an integer constant into a cpp_num,
455 of precision options->precision.
457 We do not provide any interface for decimal->float conversion,
458 because the preprocessor doesn't need it and we don't want to
459 drag in GCC's floating point emulator. */
460 cpp_num
461 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
462 unsigned int type)
464 const uchar *p, *end;
465 cpp_num result;
467 result.low = 0;
468 result.high = 0;
469 result.unsignedp = !!(type & CPP_N_UNSIGNED);
470 result.overflow = false;
472 p = token->val.str.text;
473 end = p + token->val.str.len;
475 /* Common case of a single digit. */
476 if (token->val.str.len == 1)
477 result.low = p[0] - '0';
478 else
480 cpp_num_part max;
481 size_t precision = CPP_OPTION (pfile, precision);
482 unsigned int base = 10, c = 0;
483 bool overflow = false;
485 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
487 base = 8;
488 p++;
490 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
492 base = 16;
493 p += 2;
495 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
497 base = 2;
498 p += 2;
501 /* We can add a digit to numbers strictly less than this without
502 needing the precision and slowness of double integers. */
503 max = ~(cpp_num_part) 0;
504 if (precision < PART_PRECISION)
505 max >>= PART_PRECISION - precision;
506 max = (max - base + 1) / base + 1;
508 for (; p < end; p++)
510 c = *p;
512 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
513 c = hex_value (c);
514 else
515 break;
517 /* Strict inequality for when max is set to zero. */
518 if (result.low < max)
519 result.low = result.low * base + c;
520 else
522 result = append_digit (result, c, base, precision);
523 overflow |= result.overflow;
524 max = 0;
528 if (overflow)
529 cpp_error (pfile, CPP_DL_PEDWARN,
530 "integer constant is too large for its type");
531 /* If too big to be signed, consider it unsigned. Only warn for
532 decimal numbers. Traditional numbers were always signed (but
533 we still honor an explicit U suffix); but we only have
534 traditional semantics in directives. */
535 else if (!result.unsignedp
536 && !(CPP_OPTION (pfile, traditional)
537 && pfile->state.in_directive)
538 && !num_positive (result, precision))
540 if (base == 10)
541 cpp_error (pfile, CPP_DL_WARNING,
542 "integer constant is so large that it is unsigned");
543 result.unsignedp = true;
547 return result;
550 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
551 static cpp_num
552 append_digit (cpp_num num, int digit, int base, size_t precision)
554 cpp_num result;
555 unsigned int shift;
556 bool overflow;
557 cpp_num_part add_high, add_low;
559 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
560 need to worry about add_high overflowing. */
561 switch (base)
563 case 2:
564 shift = 1;
565 break;
567 case 16:
568 shift = 4;
569 break;
571 default:
572 shift = 3;
574 overflow = !!(num.high >> (PART_PRECISION - shift));
575 result.high = num.high << shift;
576 result.low = num.low << shift;
577 result.high |= num.low >> (PART_PRECISION - shift);
578 result.unsignedp = num.unsignedp;
580 if (base == 10)
582 add_low = num.low << 1;
583 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
585 else
586 add_high = add_low = 0;
588 if (add_low + digit < add_low)
589 add_high++;
590 add_low += digit;
592 if (result.low + add_low < result.low)
593 add_high++;
594 if (result.high + add_high < result.high)
595 overflow = true;
597 result.low += add_low;
598 result.high += add_high;
599 result.overflow = overflow;
601 /* The above code catches overflow of a cpp_num type. This catches
602 overflow of the (possibly shorter) target precision. */
603 num.low = result.low;
604 num.high = result.high;
605 result = num_trim (result, precision);
606 if (!num_eq (result, num))
607 result.overflow = true;
609 return result;
612 /* Handle meeting "defined" in a preprocessor expression. */
613 static cpp_num
614 parse_defined (cpp_reader *pfile)
616 cpp_num result;
617 int paren = 0;
618 cpp_hashnode *node = 0;
619 const cpp_token *token;
620 cpp_context *initial_context = pfile->context;
622 /* Don't expand macros. */
623 pfile->state.prevent_expansion++;
625 token = cpp_get_token (pfile);
626 if (token->type == CPP_OPEN_PAREN)
628 paren = 1;
629 token = cpp_get_token (pfile);
632 if (token->type == CPP_NAME)
634 node = token->val.node;
635 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
637 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
638 node = 0;
641 else
643 cpp_error (pfile, CPP_DL_ERROR,
644 "operator \"defined\" requires an identifier");
645 if (token->flags & NAMED_OP)
647 cpp_token op;
649 op.flags = 0;
650 op.type = token->type;
651 cpp_error (pfile, CPP_DL_ERROR,
652 "(\"%s\" is an alternative token for \"%s\" in C++)",
653 cpp_token_as_text (pfile, token),
654 cpp_token_as_text (pfile, &op));
658 if (node)
660 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
661 cpp_error (pfile, CPP_DL_WARNING,
662 "this use of \"defined\" may not be portable");
664 _cpp_mark_macro_used (node);
665 if (!(node->flags & NODE_USED))
667 node->flags |= NODE_USED;
668 if (node->type == NT_MACRO)
670 if (pfile->cb.used_define)
671 pfile->cb.used_define (pfile, pfile->directive_line, node);
673 else
675 if (pfile->cb.used_undef)
676 pfile->cb.used_undef (pfile, pfile->directive_line, node);
680 /* A possible controlling macro of the form #if !defined ().
681 _cpp_parse_expr checks there was no other junk on the line. */
682 pfile->mi_ind_cmacro = node;
685 pfile->state.prevent_expansion--;
687 result.unsignedp = false;
688 result.high = 0;
689 result.overflow = false;
690 result.low = node && node->type == NT_MACRO;
691 return result;
694 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
695 number or character constant, or the result of the "defined" or "#"
696 operators). */
697 static cpp_num
698 eval_token (cpp_reader *pfile, const cpp_token *token)
700 cpp_num result;
701 unsigned int temp;
702 int unsignedp = 0;
704 result.unsignedp = false;
705 result.overflow = false;
707 switch (token->type)
709 case CPP_NUMBER:
710 temp = cpp_classify_number (pfile, token);
711 switch (temp & CPP_N_CATEGORY)
713 case CPP_N_FLOATING:
714 cpp_error (pfile, CPP_DL_ERROR,
715 "floating constant in preprocessor expression");
716 break;
717 case CPP_N_INTEGER:
718 if (!(temp & CPP_N_IMAGINARY))
719 return cpp_interpret_integer (pfile, token, temp);
720 cpp_error (pfile, CPP_DL_ERROR,
721 "imaginary number in preprocessor expression");
722 break;
724 case CPP_N_INVALID:
725 /* Error already issued. */
726 break;
728 result.high = result.low = 0;
729 break;
731 case CPP_WCHAR:
732 case CPP_CHAR:
733 case CPP_CHAR16:
734 case CPP_CHAR32:
736 cppchar_t cc = cpp_interpret_charconst (pfile, token,
737 &temp, &unsignedp);
739 result.high = 0;
740 result.low = cc;
741 /* Sign-extend the result if necessary. */
742 if (!unsignedp && (cppchar_signed_t) cc < 0)
744 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
745 result.low |= ~(~(cpp_num_part) 0
746 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
747 result.high = ~(cpp_num_part) 0;
748 result = num_trim (result, CPP_OPTION (pfile, precision));
751 break;
753 case CPP_NAME:
754 if (token->val.node == pfile->spec_nodes.n_defined)
755 return parse_defined (pfile);
756 else if (CPP_OPTION (pfile, cplusplus)
757 && (token->val.node == pfile->spec_nodes.n_true
758 || token->val.node == pfile->spec_nodes.n_false))
760 result.high = 0;
761 result.low = (token->val.node == pfile->spec_nodes.n_true);
763 else
765 result.high = 0;
766 result.low = 0;
767 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
768 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined",
769 NODE_NAME (token->val.node));
771 break;
773 case CPP_HASH:
774 if (!pfile->state.skipping)
776 /* A pedantic warning takes precedence over a deprecated
777 warning here. */
778 if (CPP_PEDANTIC (pfile))
779 cpp_error (pfile, CPP_DL_PEDWARN,
780 "assertions are a GCC extension");
781 else if (CPP_OPTION (pfile, warn_deprecated))
782 cpp_error (pfile, CPP_DL_WARNING,
783 "assertions are a deprecated extension");
785 _cpp_test_assertion (pfile, &temp);
786 result.high = 0;
787 result.low = temp;
788 break;
790 default:
791 abort ();
794 result.unsignedp = !!unsignedp;
795 return result;
798 /* Operator precedence and flags table.
800 After an operator is returned from the lexer, if it has priority less
801 than the operator on the top of the stack, we reduce the stack by one
802 operator and repeat the test. Since equal priorities do not reduce,
803 this is naturally right-associative.
805 We handle left-associative operators by decrementing the priority of
806 just-lexed operators by one, but retaining the priority of operators
807 already on the stack.
809 The remaining cases are '(' and ')'. We handle '(' by skipping the
810 reduction phase completely. ')' is given lower priority than
811 everything else, including '(', effectively forcing a reduction of the
812 parenthesized expression. If there is a matching '(', the routine
813 reduce() exits immediately. If the normal exit route sees a ')', then
814 there cannot have been a matching '(' and an error message is output.
816 The parser assumes all shifted operators require a left operand unless
817 the flag NO_L_OPERAND is set. These semantics are automatic; any
818 extra semantics need to be handled with operator-specific code. */
820 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
821 operand changes because of integer promotions. */
822 #define NO_L_OPERAND (1 << 0)
823 #define LEFT_ASSOC (1 << 1)
824 #define CHECK_PROMOTION (1 << 2)
826 /* Operator to priority map. Must be in the same order as the first
827 N entries of enum cpp_ttype. */
828 static const struct cpp_operator
830 uchar prio;
831 uchar flags;
832 } optab[] =
834 /* EQ */ {0, 0}, /* Shouldn't happen. */
835 /* NOT */ {16, NO_L_OPERAND},
836 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
837 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
838 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
839 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
840 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
841 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
842 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
843 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
844 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
845 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
846 /* RSHIFT */ {13, LEFT_ASSOC},
847 /* LSHIFT */ {13, LEFT_ASSOC},
849 /* COMPL */ {16, NO_L_OPERAND},
850 /* AND_AND */ {6, LEFT_ASSOC},
851 /* OR_OR */ {5, LEFT_ASSOC},
852 /* Note that QUERY, COLON, and COMMA must have the same precedence.
853 However, there are some special cases for these in reduce(). */
854 /* QUERY */ {4, 0},
855 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
856 /* COMMA */ {4, LEFT_ASSOC},
857 /* OPEN_PAREN */ {1, NO_L_OPERAND},
858 /* CLOSE_PAREN */ {0, 0},
859 /* EOF */ {0, 0},
860 /* EQ_EQ */ {11, LEFT_ASSOC},
861 /* NOT_EQ */ {11, LEFT_ASSOC},
862 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
863 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
864 /* UPLUS */ {16, NO_L_OPERAND},
865 /* UMINUS */ {16, NO_L_OPERAND}
868 /* Parse and evaluate a C expression, reading from PFILE.
869 Returns the truth value of the expression.
871 The implementation is an operator precedence parser, i.e. a
872 bottom-up parser, using a stack for not-yet-reduced tokens.
874 The stack base is op_stack, and the current stack pointer is 'top'.
875 There is a stack element for each operator (only), and the most
876 recently pushed operator is 'top->op'. An operand (value) is
877 stored in the 'value' field of the stack element of the operator
878 that precedes it. */
879 bool
880 _cpp_parse_expr (cpp_reader *pfile, bool is_if)
882 struct op *top = pfile->op_stack;
883 unsigned int lex_count;
884 bool saw_leading_not, want_value = true;
886 pfile->state.skip_eval = 0;
888 /* Set up detection of #if ! defined(). */
889 pfile->mi_ind_cmacro = 0;
890 saw_leading_not = false;
891 lex_count = 0;
893 /* Lowest priority operator prevents further reductions. */
894 top->op = CPP_EOF;
896 for (;;)
898 struct op op;
900 lex_count++;
901 op.token = cpp_get_token (pfile);
902 op.op = op.token->type;
903 op.loc = op.token->src_loc;
905 switch (op.op)
907 /* These tokens convert into values. */
908 case CPP_NUMBER:
909 case CPP_CHAR:
910 case CPP_WCHAR:
911 case CPP_CHAR16:
912 case CPP_CHAR32:
913 case CPP_NAME:
914 case CPP_HASH:
915 if (!want_value)
916 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
917 cpp_token_as_text (pfile, op.token));
918 want_value = false;
919 top->value = eval_token (pfile, op.token);
920 continue;
922 case CPP_NOT:
923 saw_leading_not = lex_count == 1;
924 break;
925 case CPP_PLUS:
926 if (want_value)
927 op.op = CPP_UPLUS;
928 break;
929 case CPP_MINUS:
930 if (want_value)
931 op.op = CPP_UMINUS;
932 break;
934 default:
935 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
936 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
937 cpp_token_as_text (pfile, op.token));
938 break;
941 /* Check we have a value or operator as appropriate. */
942 if (optab[op.op].flags & NO_L_OPERAND)
944 if (!want_value)
945 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
946 cpp_token_as_text (pfile, op.token));
948 else if (want_value)
950 /* We want a number (or expression) and haven't got one.
951 Try to emit a specific diagnostic. */
952 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
953 SYNTAX_ERROR ("missing expression between '(' and ')'");
955 if (op.op == CPP_EOF && top->op == CPP_EOF)
956 SYNTAX_ERROR2 ("%s with no expression", is_if ? "#if" : "#elif");
958 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
959 SYNTAX_ERROR2 ("operator '%s' has no right operand",
960 cpp_token_as_text (pfile, top->token));
961 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
962 /* Complain about missing paren during reduction. */;
963 else
964 SYNTAX_ERROR2 ("operator '%s' has no left operand",
965 cpp_token_as_text (pfile, op.token));
968 top = reduce (pfile, top, op.op);
969 if (!top)
970 goto syntax_error;
972 if (op.op == CPP_EOF)
973 break;
975 switch (op.op)
977 case CPP_CLOSE_PAREN:
978 continue;
979 case CPP_OR_OR:
980 if (!num_zerop (top->value))
981 pfile->state.skip_eval++;
982 break;
983 case CPP_AND_AND:
984 case CPP_QUERY:
985 if (num_zerop (top->value))
986 pfile->state.skip_eval++;
987 break;
988 case CPP_COLON:
989 if (top->op != CPP_QUERY)
990 SYNTAX_ERROR (" ':' without preceding '?'");
991 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
992 pfile->state.skip_eval++;
993 else
994 pfile->state.skip_eval--;
995 default:
996 break;
999 want_value = true;
1001 /* Check for and handle stack overflow. */
1002 if (++top == pfile->op_limit)
1003 top = _cpp_expand_op_stack (pfile);
1005 top->op = op.op;
1006 top->token = op.token;
1007 top->loc = op.token->src_loc;
1010 /* The controlling macro expression is only valid if we called lex 3
1011 times: <!> <defined expression> and <EOF>. push_conditional ()
1012 checks that we are at top-of-file. */
1013 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1014 pfile->mi_ind_cmacro = 0;
1016 if (top != pfile->op_stack)
1018 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in %s",
1019 is_if ? "#if" : "#elif");
1020 syntax_error:
1021 return false; /* Return false on syntax error. */
1024 return !num_zerop (top->value);
1027 /* Reduce the operator / value stack if possible, in preparation for
1028 pushing operator OP. Returns NULL on error, otherwise the top of
1029 the stack. */
1030 static struct op *
1031 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1033 unsigned int prio;
1035 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1037 bad_op:
1038 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1039 return 0;
1042 if (op == CPP_OPEN_PAREN)
1043 return top;
1045 /* Decrement the priority of left-associative operators to force a
1046 reduction with operators of otherwise equal priority. */
1047 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1048 while (prio < optab[top->op].prio)
1050 if (CPP_OPTION (pfile, warn_num_sign_change)
1051 && optab[top->op].flags & CHECK_PROMOTION)
1052 check_promotion (pfile, top);
1054 switch (top->op)
1056 case CPP_UPLUS:
1057 case CPP_UMINUS:
1058 case CPP_NOT:
1059 case CPP_COMPL:
1060 top[-1].value = num_unary_op (pfile, top->value, top->op);
1061 top[-1].loc = top->loc;
1062 break;
1064 case CPP_PLUS:
1065 case CPP_MINUS:
1066 case CPP_RSHIFT:
1067 case CPP_LSHIFT:
1068 case CPP_COMMA:
1069 top[-1].value = num_binary_op (pfile, top[-1].value,
1070 top->value, top->op);
1071 top[-1].loc = top->loc;
1072 break;
1074 case CPP_GREATER:
1075 case CPP_LESS:
1076 case CPP_GREATER_EQ:
1077 case CPP_LESS_EQ:
1078 top[-1].value
1079 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1080 top[-1].loc = top->loc;
1081 break;
1083 case CPP_EQ_EQ:
1084 case CPP_NOT_EQ:
1085 top[-1].value
1086 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1087 top[-1].loc = top->loc;
1088 break;
1090 case CPP_AND:
1091 case CPP_OR:
1092 case CPP_XOR:
1093 top[-1].value
1094 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1095 top[-1].loc = top->loc;
1096 break;
1098 case CPP_MULT:
1099 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1100 top[-1].loc = top->loc;
1101 break;
1103 case CPP_DIV:
1104 case CPP_MOD:
1105 top[-1].value = num_div_op (pfile, top[-1].value,
1106 top->value, top->op);
1107 top[-1].loc = top->loc;
1108 break;
1110 case CPP_OR_OR:
1111 top--;
1112 if (!num_zerop (top->value))
1113 pfile->state.skip_eval--;
1114 top->value.low = (!num_zerop (top->value)
1115 || !num_zerop (top[1].value));
1116 top->value.high = 0;
1117 top->value.unsignedp = false;
1118 top->value.overflow = false;
1119 top->loc = top[1].loc;
1120 continue;
1122 case CPP_AND_AND:
1123 top--;
1124 if (num_zerop (top->value))
1125 pfile->state.skip_eval--;
1126 top->value.low = (!num_zerop (top->value)
1127 && !num_zerop (top[1].value));
1128 top->value.high = 0;
1129 top->value.unsignedp = false;
1130 top->value.overflow = false;
1131 top->loc = top[1].loc;
1132 continue;
1134 case CPP_OPEN_PAREN:
1135 if (op != CPP_CLOSE_PAREN)
1137 cpp_error_with_line (pfile, CPP_DL_ERROR,
1138 top->token->src_loc,
1139 0, "missing ')' in expression");
1140 return 0;
1142 top--;
1143 top->value = top[1].value;
1144 top->loc = top[1].loc;
1145 return top;
1147 case CPP_COLON:
1148 top -= 2;
1149 if (!num_zerop (top->value))
1151 pfile->state.skip_eval--;
1152 top->value = top[1].value;
1153 top->loc = top[1].loc;
1155 else
1157 top->value = top[2].value;
1158 top->loc = top[2].loc;
1160 top->value.unsignedp = (top[1].value.unsignedp
1161 || top[2].value.unsignedp);
1162 continue;
1164 case CPP_QUERY:
1165 /* COMMA and COLON should not reduce a QUERY operator. */
1166 if (op == CPP_COMMA || op == CPP_COLON)
1167 return top;
1168 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1169 return 0;
1171 default:
1172 goto bad_op;
1175 top--;
1176 if (top->value.overflow && !pfile->state.skip_eval)
1177 cpp_error (pfile, CPP_DL_PEDWARN,
1178 "integer overflow in preprocessor expression");
1181 if (op == CPP_CLOSE_PAREN)
1183 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1184 return 0;
1187 return top;
1190 /* Returns the position of the old top of stack after expansion. */
1191 struct op *
1192 _cpp_expand_op_stack (cpp_reader *pfile)
1194 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1195 size_t new_size = old_size * 2 + 20;
1197 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1198 pfile->op_limit = pfile->op_stack + new_size;
1200 return pfile->op_stack + old_size;
1203 /* Emits a warning if the effective sign of either operand of OP
1204 changes because of integer promotions. */
1205 static void
1206 check_promotion (cpp_reader *pfile, const struct op *op)
1208 if (op->value.unsignedp == op[-1].value.unsignedp)
1209 return;
1211 if (op->value.unsignedp)
1213 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1214 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1215 "the left operand of \"%s\" changes sign when promoted",
1216 cpp_token_as_text (pfile, op->token));
1218 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1219 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1220 "the right operand of \"%s\" changes sign when promoted",
1221 cpp_token_as_text (pfile, op->token));
1224 /* Clears the unused high order bits of the number pointed to by PNUM. */
1225 static cpp_num
1226 num_trim (cpp_num num, size_t precision)
1228 if (precision > PART_PRECISION)
1230 precision -= PART_PRECISION;
1231 if (precision < PART_PRECISION)
1232 num.high &= ((cpp_num_part) 1 << precision) - 1;
1234 else
1236 if (precision < PART_PRECISION)
1237 num.low &= ((cpp_num_part) 1 << precision) - 1;
1238 num.high = 0;
1241 return num;
1244 /* True iff A (presumed signed) >= 0. */
1245 static bool
1246 num_positive (cpp_num num, size_t precision)
1248 if (precision > PART_PRECISION)
1250 precision -= PART_PRECISION;
1251 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1254 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1257 /* Sign extend a number, with PRECISION significant bits and all
1258 others assumed clear, to fill out a cpp_num structure. */
1259 cpp_num
1260 cpp_num_sign_extend (cpp_num num, size_t precision)
1262 if (!num.unsignedp)
1264 if (precision > PART_PRECISION)
1266 precision -= PART_PRECISION;
1267 if (precision < PART_PRECISION
1268 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1269 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1271 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1273 if (precision < PART_PRECISION)
1274 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1275 num.high = ~(cpp_num_part) 0;
1279 return num;
1282 /* Returns the negative of NUM. */
1283 static cpp_num
1284 num_negate (cpp_num num, size_t precision)
1286 cpp_num copy;
1288 copy = num;
1289 num.high = ~num.high;
1290 num.low = ~num.low;
1291 if (++num.low == 0)
1292 num.high++;
1293 num = num_trim (num, precision);
1294 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1296 return num;
1299 /* Returns true if A >= B. */
1300 static bool
1301 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1303 bool unsignedp;
1305 unsignedp = pa.unsignedp || pb.unsignedp;
1307 if (!unsignedp)
1309 /* Both numbers have signed type. If they are of different
1310 sign, the answer is the sign of A. */
1311 unsignedp = num_positive (pa, precision);
1313 if (unsignedp != num_positive (pb, precision))
1314 return unsignedp;
1316 /* Otherwise we can do an unsigned comparison. */
1319 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1322 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1323 static cpp_num
1324 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1325 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1327 lhs.overflow = false;
1328 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1330 /* As excess precision is zeroed, there is no need to num_trim () as
1331 these operations cannot introduce a set bit there. */
1332 if (op == CPP_AND)
1334 lhs.low &= rhs.low;
1335 lhs.high &= rhs.high;
1337 else if (op == CPP_OR)
1339 lhs.low |= rhs.low;
1340 lhs.high |= rhs.high;
1342 else
1344 lhs.low ^= rhs.low;
1345 lhs.high ^= rhs.high;
1348 return lhs;
1351 /* Returns LHS OP RHS, where OP is an inequality. */
1352 static cpp_num
1353 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1354 enum cpp_ttype op)
1356 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1358 if (op == CPP_GREATER_EQ)
1359 lhs.low = gte;
1360 else if (op == CPP_LESS)
1361 lhs.low = !gte;
1362 else if (op == CPP_GREATER)
1363 lhs.low = gte && !num_eq (lhs, rhs);
1364 else /* CPP_LESS_EQ. */
1365 lhs.low = !gte || num_eq (lhs, rhs);
1367 lhs.high = 0;
1368 lhs.overflow = false;
1369 lhs.unsignedp = false;
1370 return lhs;
1373 /* Returns LHS OP RHS, where OP is == or !=. */
1374 static cpp_num
1375 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1376 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1378 /* Work around a 3.0.4 bug; see PR 6950. */
1379 bool eq = num_eq (lhs, rhs);
1380 if (op == CPP_NOT_EQ)
1381 eq = !eq;
1382 lhs.low = eq;
1383 lhs.high = 0;
1384 lhs.overflow = false;
1385 lhs.unsignedp = false;
1386 return lhs;
1389 /* Shift NUM, of width PRECISION, right by N bits. */
1390 static cpp_num
1391 num_rshift (cpp_num num, size_t precision, size_t n)
1393 cpp_num_part sign_mask;
1394 bool x = num_positive (num, precision);
1396 if (num.unsignedp || x)
1397 sign_mask = 0;
1398 else
1399 sign_mask = ~(cpp_num_part) 0;
1401 if (n >= precision)
1402 num.high = num.low = sign_mask;
1403 else
1405 /* Sign-extend. */
1406 if (precision < PART_PRECISION)
1407 num.high = sign_mask, num.low |= sign_mask << precision;
1408 else if (precision < 2 * PART_PRECISION)
1409 num.high |= sign_mask << (precision - PART_PRECISION);
1411 if (n >= PART_PRECISION)
1413 n -= PART_PRECISION;
1414 num.low = num.high;
1415 num.high = sign_mask;
1418 if (n)
1420 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1421 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1425 num = num_trim (num, precision);
1426 num.overflow = false;
1427 return num;
1430 /* Shift NUM, of width PRECISION, left by N bits. */
1431 static cpp_num
1432 num_lshift (cpp_num num, size_t precision, size_t n)
1434 if (n >= precision)
1436 num.overflow = !num.unsignedp && !num_zerop (num);
1437 num.high = num.low = 0;
1439 else
1441 cpp_num orig, maybe_orig;
1442 size_t m = n;
1444 orig = num;
1445 if (m >= PART_PRECISION)
1447 m -= PART_PRECISION;
1448 num.high = num.low;
1449 num.low = 0;
1451 if (m)
1453 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1454 num.low <<= m;
1456 num = num_trim (num, precision);
1458 if (num.unsignedp)
1459 num.overflow = false;
1460 else
1462 maybe_orig = num_rshift (num, precision, n);
1463 num.overflow = !num_eq (orig, maybe_orig);
1467 return num;
1470 /* The four unary operators: +, -, ! and ~. */
1471 static cpp_num
1472 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1474 switch (op)
1476 case CPP_UPLUS:
1477 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1478 cpp_error (pfile, CPP_DL_WARNING,
1479 "traditional C rejects the unary plus operator");
1480 num.overflow = false;
1481 break;
1483 case CPP_UMINUS:
1484 num = num_negate (num, CPP_OPTION (pfile, precision));
1485 break;
1487 case CPP_COMPL:
1488 num.high = ~num.high;
1489 num.low = ~num.low;
1490 num = num_trim (num, CPP_OPTION (pfile, precision));
1491 num.overflow = false;
1492 break;
1494 default: /* case CPP_NOT: */
1495 num.low = num_zerop (num);
1496 num.high = 0;
1497 num.overflow = false;
1498 num.unsignedp = false;
1499 break;
1502 return num;
1505 /* The various binary operators. */
1506 static cpp_num
1507 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1509 cpp_num result;
1510 size_t precision = CPP_OPTION (pfile, precision);
1511 size_t n;
1513 switch (op)
1515 /* Shifts. */
1516 case CPP_LSHIFT:
1517 case CPP_RSHIFT:
1518 if (!rhs.unsignedp && !num_positive (rhs, precision))
1520 /* A negative shift is a positive shift the other way. */
1521 if (op == CPP_LSHIFT)
1522 op = CPP_RSHIFT;
1523 else
1524 op = CPP_LSHIFT;
1525 rhs = num_negate (rhs, precision);
1527 if (rhs.high)
1528 n = ~0; /* Maximal. */
1529 else
1530 n = rhs.low;
1531 if (op == CPP_LSHIFT)
1532 lhs = num_lshift (lhs, precision, n);
1533 else
1534 lhs = num_rshift (lhs, precision, n);
1535 break;
1537 /* Arithmetic. */
1538 case CPP_MINUS:
1539 rhs = num_negate (rhs, precision);
1540 case CPP_PLUS:
1541 result.low = lhs.low + rhs.low;
1542 result.high = lhs.high + rhs.high;
1543 if (result.low < lhs.low)
1544 result.high++;
1545 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1546 result.overflow = false;
1548 result = num_trim (result, precision);
1549 if (!result.unsignedp)
1551 bool lhsp = num_positive (lhs, precision);
1552 result.overflow = (lhsp == num_positive (rhs, precision)
1553 && lhsp != num_positive (result, precision));
1555 return result;
1557 /* Comma. */
1558 default: /* case CPP_COMMA: */
1559 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1560 || !pfile->state.skip_eval))
1561 cpp_error (pfile, CPP_DL_PEDWARN,
1562 "comma operator in operand of #if");
1563 lhs = rhs;
1564 break;
1567 return lhs;
1570 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1571 cannot overflow. */
1572 static cpp_num
1573 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1575 cpp_num result;
1576 cpp_num_part middle[2], temp;
1578 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1579 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1581 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1582 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1584 temp = result.low;
1585 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1586 if (result.low < temp)
1587 result.high++;
1589 temp = result.low;
1590 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1591 if (result.low < temp)
1592 result.high++;
1594 result.high += HIGH_PART (middle[0]);
1595 result.high += HIGH_PART (middle[1]);
1596 result.unsignedp = true;
1597 result.overflow = false;
1599 return result;
1602 /* Multiply two preprocessing numbers. */
1603 static cpp_num
1604 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1606 cpp_num result, temp;
1607 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1608 bool overflow, negate = false;
1609 size_t precision = CPP_OPTION (pfile, precision);
1611 /* Prepare for unsigned multiplication. */
1612 if (!unsignedp)
1614 if (!num_positive (lhs, precision))
1615 negate = !negate, lhs = num_negate (lhs, precision);
1616 if (!num_positive (rhs, precision))
1617 negate = !negate, rhs = num_negate (rhs, precision);
1620 overflow = lhs.high && rhs.high;
1621 result = num_part_mul (lhs.low, rhs.low);
1623 temp = num_part_mul (lhs.high, rhs.low);
1624 result.high += temp.low;
1625 if (temp.high)
1626 overflow = true;
1628 temp = num_part_mul (lhs.low, rhs.high);
1629 result.high += temp.low;
1630 if (temp.high)
1631 overflow = true;
1633 temp.low = result.low, temp.high = result.high;
1634 result = num_trim (result, precision);
1635 if (!num_eq (result, temp))
1636 overflow = true;
1638 if (negate)
1639 result = num_negate (result, precision);
1641 if (unsignedp)
1642 result.overflow = false;
1643 else
1644 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1645 && !num_zerop (result));
1646 result.unsignedp = unsignedp;
1648 return result;
1651 /* Divide two preprocessing numbers, returning the answer or the
1652 remainder depending upon OP. */
1653 static cpp_num
1654 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1656 cpp_num result, sub;
1657 cpp_num_part mask;
1658 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1659 bool negate = false, lhs_neg = false;
1660 size_t i, precision = CPP_OPTION (pfile, precision);
1662 /* Prepare for unsigned division. */
1663 if (!unsignedp)
1665 if (!num_positive (lhs, precision))
1666 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1667 if (!num_positive (rhs, precision))
1668 negate = !negate, rhs = num_negate (rhs, precision);
1671 /* Find the high bit. */
1672 if (rhs.high)
1674 i = precision - 1;
1675 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1676 for (; ; i--, mask >>= 1)
1677 if (rhs.high & mask)
1678 break;
1680 else if (rhs.low)
1682 if (precision > PART_PRECISION)
1683 i = precision - PART_PRECISION - 1;
1684 else
1685 i = precision - 1;
1686 mask = (cpp_num_part) 1 << i;
1687 for (; ; i--, mask >>= 1)
1688 if (rhs.low & mask)
1689 break;
1691 else
1693 if (!pfile->state.skip_eval)
1694 cpp_error (pfile, CPP_DL_ERROR, "division by zero in #if");
1695 return lhs;
1698 /* First nonzero bit of RHS is bit I. Do naive division by
1699 shifting the RHS fully left, and subtracting from LHS if LHS is
1700 at least as big, and then repeating but with one less shift.
1701 This is not very efficient, but is easy to understand. */
1703 rhs.unsignedp = true;
1704 lhs.unsignedp = true;
1705 i = precision - i - 1;
1706 sub = num_lshift (rhs, precision, i);
1708 result.high = result.low = 0;
1709 for (;;)
1711 if (num_greater_eq (lhs, sub, precision))
1713 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1714 if (i >= PART_PRECISION)
1715 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1716 else
1717 result.low |= (cpp_num_part) 1 << i;
1719 if (i-- == 0)
1720 break;
1721 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1722 sub.high >>= 1;
1725 /* We divide so that the remainder has the sign of the LHS. */
1726 if (op == CPP_DIV)
1728 result.unsignedp = unsignedp;
1729 result.overflow = false;
1730 if (!unsignedp)
1732 if (negate)
1733 result = num_negate (result, precision);
1734 result.overflow = (num_positive (result, precision) ^ !negate
1735 && !num_zerop (result));
1738 return result;
1741 /* CPP_MOD. */
1742 lhs.unsignedp = unsignedp;
1743 lhs.overflow = false;
1744 if (lhs_neg)
1745 lhs = num_negate (lhs, precision);
1747 return lhs;