Daily bump.
[official-gcc.git] / libcpp / expr.c
blobf20d50c3fdd671e5c7146583f505a6403fc1b3df
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2004 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 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 static cpp_num num_lshift (cpp_num, size_t, size_t);
57 static cpp_num num_rshift (cpp_num, size_t, size_t);
59 static cpp_num append_digit (cpp_num, int, int, size_t);
60 static cpp_num parse_defined (cpp_reader *);
61 static cpp_num eval_token (cpp_reader *, const cpp_token *);
62 static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
63 static unsigned int interpret_float_suffix (const uchar *, size_t);
64 static unsigned int interpret_int_suffix (const uchar *, size_t);
65 static void check_promotion (cpp_reader *, const struct op *);
67 /* Token type abuse to create unary plus and minus operators. */
68 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
69 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
71 /* With -O2, gcc appears to produce nice code, moving the error
72 message load and subsequent jump completely out of the main path. */
73 #define SYNTAX_ERROR(msgid) \
74 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
75 #define SYNTAX_ERROR2(msgid, arg) \
76 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
77 while(0)
79 /* Subroutine of cpp_classify_number. S points to a float suffix of
80 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
81 flag vector describing the suffix. */
82 static unsigned int
83 interpret_float_suffix (const uchar *s, size_t len)
85 size_t f, l, w, q, i, d;
87 f = l = w = q = i = d = 0;
89 while (len--)
90 switch (s[len])
92 case 'f': case 'F':
93 if (d > 0)
94 return 0;
95 f++;
96 break;
97 case 'l': case 'L':
98 if (d > 0)
99 return 0;
100 l++;
101 break;
102 case 'w': case 'W':
103 if (d > 0)
104 return 0;
105 w++;
106 break;
107 case 'q': case 'Q':
108 if (d > 0)
109 return 0;
110 q++;
111 break;
112 case 'i': case 'I':
113 case 'j': case 'J': i++; break;
114 case 'd': case 'D': d++; break;
115 default:
116 return 0;
119 if (f + l + w + q > 1 || i > 1)
120 return 0;
122 /* Allow dd, df, dl suffixes for decimal float constants. */
123 if (d && ((d + f + l != 2) || i))
124 return 0;
126 return ((i ? CPP_N_IMAGINARY : 0)
127 | (f ? CPP_N_SMALL :
128 l ? CPP_N_LARGE :
129 w ? CPP_N_MD_W :
130 q ? CPP_N_MD_Q : CPP_N_MEDIUM)
131 | (d ? CPP_N_DFLOAT : 0));
134 /* Subroutine of cpp_classify_number. S points to an integer suffix
135 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
136 flag vector describing the suffix. */
137 static unsigned int
138 interpret_int_suffix (const uchar *s, size_t len)
140 size_t u, l, i;
142 u = l = i = 0;
144 while (len--)
145 switch (s[len])
147 case 'u': case 'U': u++; break;
148 case 'i': case 'I':
149 case 'j': case 'J': i++; break;
150 case 'l': case 'L': l++;
151 /* If there are two Ls, they must be adjacent and the same case. */
152 if (l == 2 && s[len] != s[len + 1])
153 return 0;
154 break;
155 default:
156 return 0;
159 if (l > 2 || u > 1 || i > 1)
160 return 0;
162 return ((i ? CPP_N_IMAGINARY : 0)
163 | (u ? CPP_N_UNSIGNED : 0)
164 | ((l == 0) ? CPP_N_SMALL
165 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
168 /* Categorize numeric constants according to their field (integer,
169 floating point, or invalid), radix (decimal, octal, hexadecimal),
170 and type suffixes. */
171 unsigned int
172 cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
174 const uchar *str = token->val.str.text;
175 const uchar *limit;
176 unsigned int max_digit, result, radix;
177 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
179 /* If the lexer has done its job, length one can only be a single
180 digit. Fast-path this very common case. */
181 if (token->val.str.len == 1)
182 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
184 limit = str + token->val.str.len;
185 float_flag = NOT_FLOAT;
186 max_digit = 0;
187 radix = 10;
189 /* First, interpret the radix. */
190 if (*str == '0')
192 radix = 8;
193 str++;
195 /* Require at least one hex digit to classify it as hex. */
196 if ((*str == 'x' || *str == 'X')
197 && (str[1] == '.' || ISXDIGIT (str[1])))
199 radix = 16;
200 str++;
202 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
204 radix = 2;
205 str++;
209 /* Now scan for a well-formed integer or float. */
210 for (;;)
212 unsigned int c = *str++;
214 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
216 c = hex_value (c);
217 if (c > max_digit)
218 max_digit = c;
220 else if (c == '.')
222 if (float_flag == NOT_FLOAT)
223 float_flag = AFTER_POINT;
224 else
225 SYNTAX_ERROR ("too many decimal points in number");
227 else if ((radix <= 10 && (c == 'e' || c == 'E'))
228 || (radix == 16 && (c == 'p' || c == 'P')))
230 float_flag = AFTER_EXPON;
231 break;
233 else
235 /* Start of suffix. */
236 str--;
237 break;
241 if (float_flag != NOT_FLOAT && radix == 8)
242 radix = 10;
244 if (max_digit >= radix)
246 if (radix == 2)
247 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
248 else
249 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
252 if (float_flag != NOT_FLOAT)
254 if (radix == 2)
256 cpp_error (pfile, CPP_DL_ERROR,
257 "invalid prefix \"0b\" for floating constant");
258 return CPP_N_INVALID;
261 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
262 cpp_error (pfile, CPP_DL_PEDWARN,
263 "use of C99 hexadecimal floating constant");
265 if (float_flag == AFTER_EXPON)
267 if (*str == '+' || *str == '-')
268 str++;
270 /* Exponent is decimal, even if string is a hex float. */
271 if (!ISDIGIT (*str))
272 SYNTAX_ERROR ("exponent has no digits");
275 str++;
276 while (ISDIGIT (*str));
278 else if (radix == 16)
279 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
281 result = interpret_float_suffix (str, limit - str);
282 if (result == 0)
284 cpp_error (pfile, CPP_DL_ERROR,
285 "invalid suffix \"%.*s\" on floating constant",
286 (int) (limit - str), str);
287 return CPP_N_INVALID;
290 /* Traditional C didn't accept any floating suffixes. */
291 if (limit != str
292 && CPP_WTRADITIONAL (pfile)
293 && ! cpp_sys_macro_p (pfile))
294 cpp_error (pfile, CPP_DL_WARNING,
295 "traditional C rejects the \"%.*s\" suffix",
296 (int) (limit - str), str);
298 /* Radix must be 10 for decimal floats. */
299 if ((result & CPP_N_DFLOAT) && radix != 10)
301 cpp_error (pfile, CPP_DL_ERROR,
302 "invalid suffix \"%.*s\" with hexadecimal floating constant",
303 (int) (limit - str), str);
304 return CPP_N_INVALID;
307 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
308 cpp_error (pfile, CPP_DL_PEDWARN,
309 "decimal float constants are a GCC extension");
311 result |= CPP_N_FLOATING;
313 else
315 result = interpret_int_suffix (str, limit - str);
316 if (result == 0)
318 cpp_error (pfile, CPP_DL_ERROR,
319 "invalid suffix \"%.*s\" on integer constant",
320 (int) (limit - str), str);
321 return CPP_N_INVALID;
324 /* Traditional C only accepted the 'L' suffix.
325 Suppress warning about 'LL' with -Wno-long-long. */
326 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
328 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
329 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
331 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
332 cpp_error (pfile, CPP_DL_WARNING,
333 "traditional C rejects the \"%.*s\" suffix",
334 (int) (limit - str), str);
337 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
338 && ! CPP_OPTION (pfile, c99)
339 && CPP_OPTION (pfile, warn_long_long))
340 cpp_error (pfile, CPP_DL_PEDWARN,
341 "use of C99 long long integer constant");
343 result |= CPP_N_INTEGER;
346 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
347 cpp_error (pfile, CPP_DL_PEDWARN,
348 "imaginary constants are a GCC extension");
349 if (radix == 2 && CPP_PEDANTIC (pfile))
350 cpp_error (pfile, CPP_DL_PEDWARN,
351 "binary constants are a GCC extension");
353 if (radix == 10)
354 result |= CPP_N_DECIMAL;
355 else if (radix == 16)
356 result |= CPP_N_HEX;
357 else if (radix == 2)
358 result |= CPP_N_BINARY;
359 else
360 result |= CPP_N_OCTAL;
362 return result;
364 syntax_error:
365 return CPP_N_INVALID;
368 /* cpp_interpret_integer converts an integer constant into a cpp_num,
369 of precision options->precision.
371 We do not provide any interface for decimal->float conversion,
372 because the preprocessor doesn't need it and we don't want to
373 drag in GCC's floating point emulator. */
374 cpp_num
375 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
376 unsigned int type)
378 const uchar *p, *end;
379 cpp_num result;
381 result.low = 0;
382 result.high = 0;
383 result.unsignedp = !!(type & CPP_N_UNSIGNED);
384 result.overflow = false;
386 p = token->val.str.text;
387 end = p + token->val.str.len;
389 /* Common case of a single digit. */
390 if (token->val.str.len == 1)
391 result.low = p[0] - '0';
392 else
394 cpp_num_part max;
395 size_t precision = CPP_OPTION (pfile, precision);
396 unsigned int base = 10, c = 0;
397 bool overflow = false;
399 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
401 base = 8;
402 p++;
404 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
406 base = 16;
407 p += 2;
409 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
411 base = 2;
412 p += 2;
415 /* We can add a digit to numbers strictly less than this without
416 needing the precision and slowness of double integers. */
417 max = ~(cpp_num_part) 0;
418 if (precision < PART_PRECISION)
419 max >>= PART_PRECISION - precision;
420 max = (max - base + 1) / base + 1;
422 for (; p < end; p++)
424 c = *p;
426 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
427 c = hex_value (c);
428 else
429 break;
431 /* Strict inequality for when max is set to zero. */
432 if (result.low < max)
433 result.low = result.low * base + c;
434 else
436 result = append_digit (result, c, base, precision);
437 overflow |= result.overflow;
438 max = 0;
442 if (overflow)
443 cpp_error (pfile, CPP_DL_PEDWARN,
444 "integer constant is too large for its type");
445 /* If too big to be signed, consider it unsigned. Only warn for
446 decimal numbers. Traditional numbers were always signed (but
447 we still honor an explicit U suffix); but we only have
448 traditional semantics in directives. */
449 else if (!result.unsignedp
450 && !(CPP_OPTION (pfile, traditional)
451 && pfile->state.in_directive)
452 && !num_positive (result, precision))
454 if (base == 10)
455 cpp_error (pfile, CPP_DL_WARNING,
456 "integer constant is so large that it is unsigned");
457 result.unsignedp = true;
461 return result;
464 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
465 static cpp_num
466 append_digit (cpp_num num, int digit, int base, size_t precision)
468 cpp_num result;
469 unsigned int shift;
470 bool overflow;
471 cpp_num_part add_high, add_low;
473 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
474 need to worry about add_high overflowing. */
475 switch (base)
477 case 2:
478 shift = 1;
479 break;
481 case 16:
482 shift = 4;
483 break;
485 default:
486 shift = 3;
488 overflow = !!(num.high >> (PART_PRECISION - shift));
489 result.high = num.high << shift;
490 result.low = num.low << shift;
491 result.high |= num.low >> (PART_PRECISION - shift);
492 result.unsignedp = num.unsignedp;
494 if (base == 10)
496 add_low = num.low << 1;
497 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
499 else
500 add_high = add_low = 0;
502 if (add_low + digit < add_low)
503 add_high++;
504 add_low += digit;
506 if (result.low + add_low < result.low)
507 add_high++;
508 if (result.high + add_high < result.high)
509 overflow = true;
511 result.low += add_low;
512 result.high += add_high;
513 result.overflow = overflow;
515 /* The above code catches overflow of a cpp_num type. This catches
516 overflow of the (possibly shorter) target precision. */
517 num.low = result.low;
518 num.high = result.high;
519 result = num_trim (result, precision);
520 if (!num_eq (result, num))
521 result.overflow = true;
523 return result;
526 /* Handle meeting "defined" in a preprocessor expression. */
527 static cpp_num
528 parse_defined (cpp_reader *pfile)
530 cpp_num result;
531 int paren = 0;
532 cpp_hashnode *node = 0;
533 const cpp_token *token;
534 cpp_context *initial_context = pfile->context;
536 /* Don't expand macros. */
537 pfile->state.prevent_expansion++;
539 token = cpp_get_token (pfile);
540 if (token->type == CPP_OPEN_PAREN)
542 paren = 1;
543 token = cpp_get_token (pfile);
546 if (token->type == CPP_NAME)
548 node = token->val.node;
549 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
551 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
552 node = 0;
555 else
557 cpp_error (pfile, CPP_DL_ERROR,
558 "operator \"defined\" requires an identifier");
559 if (token->flags & NAMED_OP)
561 cpp_token op;
563 op.flags = 0;
564 op.type = token->type;
565 cpp_error (pfile, CPP_DL_ERROR,
566 "(\"%s\" is an alternative token for \"%s\" in C++)",
567 cpp_token_as_text (pfile, token),
568 cpp_token_as_text (pfile, &op));
572 if (node)
574 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
575 cpp_error (pfile, CPP_DL_WARNING,
576 "this use of \"defined\" may not be portable");
578 _cpp_mark_macro_used (node);
580 /* A possible controlling macro of the form #if !defined ().
581 _cpp_parse_expr checks there was no other junk on the line. */
582 pfile->mi_ind_cmacro = node;
585 pfile->state.prevent_expansion--;
587 result.unsignedp = false;
588 result.high = 0;
589 result.overflow = false;
590 result.low = node && node->type == NT_MACRO;
591 return result;
594 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
595 number or character constant, or the result of the "defined" or "#"
596 operators). */
597 static cpp_num
598 eval_token (cpp_reader *pfile, const cpp_token *token)
600 cpp_num result;
601 unsigned int temp;
602 int unsignedp = 0;
604 result.unsignedp = false;
605 result.overflow = false;
607 switch (token->type)
609 case CPP_NUMBER:
610 temp = cpp_classify_number (pfile, token);
611 switch (temp & CPP_N_CATEGORY)
613 case CPP_N_FLOATING:
614 cpp_error (pfile, CPP_DL_ERROR,
615 "floating constant in preprocessor expression");
616 break;
617 case CPP_N_INTEGER:
618 if (!(temp & CPP_N_IMAGINARY))
619 return cpp_interpret_integer (pfile, token, temp);
620 cpp_error (pfile, CPP_DL_ERROR,
621 "imaginary number in preprocessor expression");
622 break;
624 case CPP_N_INVALID:
625 /* Error already issued. */
626 break;
628 result.high = result.low = 0;
629 break;
631 case CPP_WCHAR:
632 case CPP_CHAR:
634 cppchar_t cc = cpp_interpret_charconst (pfile, token,
635 &temp, &unsignedp);
637 result.high = 0;
638 result.low = cc;
639 /* Sign-extend the result if necessary. */
640 if (!unsignedp && (cppchar_signed_t) cc < 0)
642 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
643 result.low |= ~(~(cpp_num_part) 0
644 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
645 result.high = ~(cpp_num_part) 0;
646 result = num_trim (result, CPP_OPTION (pfile, precision));
649 break;
651 case CPP_NAME:
652 if (token->val.node == pfile->spec_nodes.n_defined)
653 return parse_defined (pfile);
654 else if (CPP_OPTION (pfile, cplusplus)
655 && (token->val.node == pfile->spec_nodes.n_true
656 || token->val.node == pfile->spec_nodes.n_false))
658 result.high = 0;
659 result.low = (token->val.node == pfile->spec_nodes.n_true);
661 else
663 result.high = 0;
664 result.low = 0;
665 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
666 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined",
667 NODE_NAME (token->val.node));
669 break;
671 default: /* CPP_HASH */
672 _cpp_test_assertion (pfile, &temp);
673 result.high = 0;
674 result.low = temp;
677 result.unsignedp = !!unsignedp;
678 return result;
681 /* Operator precedence and flags table.
683 After an operator is returned from the lexer, if it has priority less
684 than the operator on the top of the stack, we reduce the stack by one
685 operator and repeat the test. Since equal priorities do not reduce,
686 this is naturally right-associative.
688 We handle left-associative operators by decrementing the priority of
689 just-lexed operators by one, but retaining the priority of operators
690 already on the stack.
692 The remaining cases are '(' and ')'. We handle '(' by skipping the
693 reduction phase completely. ')' is given lower priority than
694 everything else, including '(', effectively forcing a reduction of the
695 parenthesized expression. If there is a matching '(', the routine
696 reduce() exits immediately. If the normal exit route sees a ')', then
697 there cannot have been a matching '(' and an error message is output.
699 The parser assumes all shifted operators require a left operand unless
700 the flag NO_L_OPERAND is set. These semantics are automatic; any
701 extra semantics need to be handled with operator-specific code. */
703 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
704 operand changes because of integer promotions. */
705 #define NO_L_OPERAND (1 << 0)
706 #define LEFT_ASSOC (1 << 1)
707 #define CHECK_PROMOTION (1 << 2)
709 /* Operator to priority map. Must be in the same order as the first
710 N entries of enum cpp_ttype. */
711 static const struct cpp_operator
713 uchar prio;
714 uchar flags;
715 } optab[] =
717 /* EQ */ {0, 0}, /* Shouldn't happen. */
718 /* NOT */ {16, NO_L_OPERAND},
719 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
720 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
721 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
722 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
723 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
724 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
725 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
726 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
727 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
728 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
729 /* RSHIFT */ {13, LEFT_ASSOC},
730 /* LSHIFT */ {13, LEFT_ASSOC},
732 /* COMPL */ {16, NO_L_OPERAND},
733 /* AND_AND */ {6, LEFT_ASSOC},
734 /* OR_OR */ {5, LEFT_ASSOC},
735 /* QUERY */ {3, 0},
736 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
737 /* COMMA */ {2, LEFT_ASSOC},
738 /* OPEN_PAREN */ {1, NO_L_OPERAND},
739 /* CLOSE_PAREN */ {0, 0},
740 /* EOF */ {0, 0},
741 /* EQ_EQ */ {11, LEFT_ASSOC},
742 /* NOT_EQ */ {11, LEFT_ASSOC},
743 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
744 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
745 /* UPLUS */ {16, NO_L_OPERAND},
746 /* UMINUS */ {16, NO_L_OPERAND}
749 /* Parse and evaluate a C expression, reading from PFILE.
750 Returns the truth value of the expression.
752 The implementation is an operator precedence parser, i.e. a
753 bottom-up parser, using a stack for not-yet-reduced tokens.
755 The stack base is op_stack, and the current stack pointer is 'top'.
756 There is a stack element for each operator (only), and the most
757 recently pushed operator is 'top->op'. An operand (value) is
758 stored in the 'value' field of the stack element of the operator
759 that precedes it. */
760 bool
761 _cpp_parse_expr (cpp_reader *pfile)
763 struct op *top = pfile->op_stack;
764 unsigned int lex_count;
765 bool saw_leading_not, want_value = true;
767 pfile->state.skip_eval = 0;
769 /* Set up detection of #if ! defined(). */
770 pfile->mi_ind_cmacro = 0;
771 saw_leading_not = false;
772 lex_count = 0;
774 /* Lowest priority operator prevents further reductions. */
775 top->op = CPP_EOF;
777 for (;;)
779 struct op op;
781 lex_count++;
782 op.token = cpp_get_token (pfile);
783 op.op = op.token->type;
785 switch (op.op)
787 /* These tokens convert into values. */
788 case CPP_NUMBER:
789 case CPP_CHAR:
790 case CPP_WCHAR:
791 case CPP_NAME:
792 case CPP_HASH:
793 if (!want_value)
794 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
795 cpp_token_as_text (pfile, op.token));
796 want_value = false;
797 top->value = eval_token (pfile, op.token);
798 continue;
800 case CPP_NOT:
801 saw_leading_not = lex_count == 1;
802 break;
803 case CPP_PLUS:
804 if (want_value)
805 op.op = CPP_UPLUS;
806 break;
807 case CPP_MINUS:
808 if (want_value)
809 op.op = CPP_UMINUS;
810 break;
812 default:
813 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
814 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
815 cpp_token_as_text (pfile, op.token));
816 break;
819 /* Check we have a value or operator as appropriate. */
820 if (optab[op.op].flags & NO_L_OPERAND)
822 if (!want_value)
823 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
824 cpp_token_as_text (pfile, op.token));
826 else if (want_value)
828 /* We want a number (or expression) and haven't got one.
829 Try to emit a specific diagnostic. */
830 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
831 SYNTAX_ERROR ("missing expression between '(' and ')'");
833 if (op.op == CPP_EOF && top->op == CPP_EOF)
834 SYNTAX_ERROR ("#if with no expression");
836 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
837 SYNTAX_ERROR2 ("operator '%s' has no right operand",
838 cpp_token_as_text (pfile, top->token));
839 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
840 /* Complain about missing paren during reduction. */;
841 else
842 SYNTAX_ERROR2 ("operator '%s' has no left operand",
843 cpp_token_as_text (pfile, op.token));
846 top = reduce (pfile, top, op.op);
847 if (!top)
848 goto syntax_error;
850 if (op.op == CPP_EOF)
851 break;
853 switch (op.op)
855 case CPP_CLOSE_PAREN:
856 continue;
857 case CPP_OR_OR:
858 if (!num_zerop (top->value))
859 pfile->state.skip_eval++;
860 break;
861 case CPP_AND_AND:
862 case CPP_QUERY:
863 if (num_zerop (top->value))
864 pfile->state.skip_eval++;
865 break;
866 case CPP_COLON:
867 if (top->op != CPP_QUERY)
868 SYNTAX_ERROR (" ':' without preceding '?'");
869 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
870 pfile->state.skip_eval++;
871 else
872 pfile->state.skip_eval--;
873 default:
874 break;
877 want_value = true;
879 /* Check for and handle stack overflow. */
880 if (++top == pfile->op_limit)
881 top = _cpp_expand_op_stack (pfile);
883 top->op = op.op;
884 top->token = op.token;
887 /* The controlling macro expression is only valid if we called lex 3
888 times: <!> <defined expression> and <EOF>. push_conditional ()
889 checks that we are at top-of-file. */
890 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
891 pfile->mi_ind_cmacro = 0;
893 if (top != pfile->op_stack)
895 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in #if");
896 syntax_error:
897 return false; /* Return false on syntax error. */
900 return !num_zerop (top->value);
903 /* Reduce the operator / value stack if possible, in preparation for
904 pushing operator OP. Returns NULL on error, otherwise the top of
905 the stack. */
906 static struct op *
907 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
909 unsigned int prio;
911 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
913 bad_op:
914 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
915 return 0;
918 if (op == CPP_OPEN_PAREN)
919 return top;
921 /* Decrement the priority of left-associative operators to force a
922 reduction with operators of otherwise equal priority. */
923 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
924 while (prio < optab[top->op].prio)
926 if (CPP_OPTION (pfile, warn_num_sign_change)
927 && optab[top->op].flags & CHECK_PROMOTION)
928 check_promotion (pfile, top);
930 switch (top->op)
932 case CPP_UPLUS:
933 case CPP_UMINUS:
934 case CPP_NOT:
935 case CPP_COMPL:
936 top[-1].value = num_unary_op (pfile, top->value, top->op);
937 break;
939 case CPP_PLUS:
940 case CPP_MINUS:
941 case CPP_RSHIFT:
942 case CPP_LSHIFT:
943 case CPP_COMMA:
944 top[-1].value = num_binary_op (pfile, top[-1].value,
945 top->value, top->op);
946 break;
948 case CPP_GREATER:
949 case CPP_LESS:
950 case CPP_GREATER_EQ:
951 case CPP_LESS_EQ:
952 top[-1].value
953 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
954 break;
956 case CPP_EQ_EQ:
957 case CPP_NOT_EQ:
958 top[-1].value
959 = num_equality_op (pfile, top[-1].value, top->value, top->op);
960 break;
962 case CPP_AND:
963 case CPP_OR:
964 case CPP_XOR:
965 top[-1].value
966 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
967 break;
969 case CPP_MULT:
970 top[-1].value = num_mul (pfile, top[-1].value, top->value);
971 break;
973 case CPP_DIV:
974 case CPP_MOD:
975 top[-1].value = num_div_op (pfile, top[-1].value,
976 top->value, top->op);
977 break;
979 case CPP_OR_OR:
980 top--;
981 if (!num_zerop (top->value))
982 pfile->state.skip_eval--;
983 top->value.low = (!num_zerop (top->value)
984 || !num_zerop (top[1].value));
985 top->value.high = 0;
986 top->value.unsignedp = false;
987 top->value.overflow = false;
988 continue;
990 case CPP_AND_AND:
991 top--;
992 if (num_zerop (top->value))
993 pfile->state.skip_eval--;
994 top->value.low = (!num_zerop (top->value)
995 && !num_zerop (top[1].value));
996 top->value.high = 0;
997 top->value.unsignedp = false;
998 top->value.overflow = false;
999 continue;
1001 case CPP_OPEN_PAREN:
1002 if (op != CPP_CLOSE_PAREN)
1004 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in expression");
1005 return 0;
1007 top--;
1008 top->value = top[1].value;
1009 return top;
1011 case CPP_COLON:
1012 top -= 2;
1013 if (!num_zerop (top->value))
1015 pfile->state.skip_eval--;
1016 top->value = top[1].value;
1018 else
1019 top->value = top[2].value;
1020 top->value.unsignedp = (top[1].value.unsignedp
1021 || top[2].value.unsignedp);
1022 continue;
1024 case CPP_QUERY:
1025 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1026 return 0;
1028 default:
1029 goto bad_op;
1032 top--;
1033 if (top->value.overflow && !pfile->state.skip_eval)
1034 cpp_error (pfile, CPP_DL_PEDWARN,
1035 "integer overflow in preprocessor expression");
1038 if (op == CPP_CLOSE_PAREN)
1040 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1041 return 0;
1044 return top;
1047 /* Returns the position of the old top of stack after expansion. */
1048 struct op *
1049 _cpp_expand_op_stack (cpp_reader *pfile)
1051 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1052 size_t new_size = old_size * 2 + 20;
1054 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1055 pfile->op_limit = pfile->op_stack + new_size;
1057 return pfile->op_stack + old_size;
1060 /* Emits a warning if the effective sign of either operand of OP
1061 changes because of integer promotions. */
1062 static void
1063 check_promotion (cpp_reader *pfile, const struct op *op)
1065 if (op->value.unsignedp == op[-1].value.unsignedp)
1066 return;
1068 if (op->value.unsignedp)
1070 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1071 cpp_error (pfile, CPP_DL_WARNING,
1072 "the left operand of \"%s\" changes sign when promoted",
1073 cpp_token_as_text (pfile, op->token));
1075 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1076 cpp_error (pfile, CPP_DL_WARNING,
1077 "the right operand of \"%s\" changes sign when promoted",
1078 cpp_token_as_text (pfile, op->token));
1081 /* Clears the unused high order bits of the number pointed to by PNUM. */
1082 static cpp_num
1083 num_trim (cpp_num num, size_t precision)
1085 if (precision > PART_PRECISION)
1087 precision -= PART_PRECISION;
1088 if (precision < PART_PRECISION)
1089 num.high &= ((cpp_num_part) 1 << precision) - 1;
1091 else
1093 if (precision < PART_PRECISION)
1094 num.low &= ((cpp_num_part) 1 << precision) - 1;
1095 num.high = 0;
1098 return num;
1101 /* True iff A (presumed signed) >= 0. */
1102 static bool
1103 num_positive (cpp_num num, size_t precision)
1105 if (precision > PART_PRECISION)
1107 precision -= PART_PRECISION;
1108 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1111 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1114 /* Sign extend a number, with PRECISION significant bits and all
1115 others assumed clear, to fill out a cpp_num structure. */
1116 cpp_num
1117 cpp_num_sign_extend (cpp_num num, size_t precision)
1119 if (!num.unsignedp)
1121 if (precision > PART_PRECISION)
1123 precision -= PART_PRECISION;
1124 if (precision < PART_PRECISION
1125 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1126 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1128 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1130 if (precision < PART_PRECISION)
1131 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1132 num.high = ~(cpp_num_part) 0;
1136 return num;
1139 /* Returns the negative of NUM. */
1140 static cpp_num
1141 num_negate (cpp_num num, size_t precision)
1143 cpp_num copy;
1145 copy = num;
1146 num.high = ~num.high;
1147 num.low = ~num.low;
1148 if (++num.low == 0)
1149 num.high++;
1150 num = num_trim (num, precision);
1151 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1153 return num;
1156 /* Returns true if A >= B. */
1157 static bool
1158 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1160 bool unsignedp;
1162 unsignedp = pa.unsignedp || pb.unsignedp;
1164 if (!unsignedp)
1166 /* Both numbers have signed type. If they are of different
1167 sign, the answer is the sign of A. */
1168 unsignedp = num_positive (pa, precision);
1170 if (unsignedp != num_positive (pb, precision))
1171 return unsignedp;
1173 /* Otherwise we can do an unsigned comparison. */
1176 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1179 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1180 static cpp_num
1181 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1182 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1184 lhs.overflow = false;
1185 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1187 /* As excess precision is zeroed, there is no need to num_trim () as
1188 these operations cannot introduce a set bit there. */
1189 if (op == CPP_AND)
1191 lhs.low &= rhs.low;
1192 lhs.high &= rhs.high;
1194 else if (op == CPP_OR)
1196 lhs.low |= rhs.low;
1197 lhs.high |= rhs.high;
1199 else
1201 lhs.low ^= rhs.low;
1202 lhs.high ^= rhs.high;
1205 return lhs;
1208 /* Returns LHS OP RHS, where OP is an inequality. */
1209 static cpp_num
1210 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1211 enum cpp_ttype op)
1213 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1215 if (op == CPP_GREATER_EQ)
1216 lhs.low = gte;
1217 else if (op == CPP_LESS)
1218 lhs.low = !gte;
1219 else if (op == CPP_GREATER)
1220 lhs.low = gte && !num_eq (lhs, rhs);
1221 else /* CPP_LESS_EQ. */
1222 lhs.low = !gte || num_eq (lhs, rhs);
1224 lhs.high = 0;
1225 lhs.overflow = false;
1226 lhs.unsignedp = false;
1227 return lhs;
1230 /* Returns LHS OP RHS, where OP is == or !=. */
1231 static cpp_num
1232 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1233 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1235 /* Work around a 3.0.4 bug; see PR 6950. */
1236 bool eq = num_eq (lhs, rhs);
1237 if (op == CPP_NOT_EQ)
1238 eq = !eq;
1239 lhs.low = eq;
1240 lhs.high = 0;
1241 lhs.overflow = false;
1242 lhs.unsignedp = false;
1243 return lhs;
1246 /* Shift NUM, of width PRECISION, right by N bits. */
1247 static cpp_num
1248 num_rshift (cpp_num num, size_t precision, size_t n)
1250 cpp_num_part sign_mask;
1251 bool x = num_positive (num, precision);
1253 if (num.unsignedp || x)
1254 sign_mask = 0;
1255 else
1256 sign_mask = ~(cpp_num_part) 0;
1258 if (n >= precision)
1259 num.high = num.low = sign_mask;
1260 else
1262 /* Sign-extend. */
1263 if (precision < PART_PRECISION)
1264 num.high = sign_mask, num.low |= sign_mask << precision;
1265 else if (precision < 2 * PART_PRECISION)
1266 num.high |= sign_mask << (precision - PART_PRECISION);
1268 if (n >= PART_PRECISION)
1270 n -= PART_PRECISION;
1271 num.low = num.high;
1272 num.high = sign_mask;
1275 if (n)
1277 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1278 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1282 num = num_trim (num, precision);
1283 num.overflow = false;
1284 return num;
1287 /* Shift NUM, of width PRECISION, left by N bits. */
1288 static cpp_num
1289 num_lshift (cpp_num num, size_t precision, size_t n)
1291 if (n >= precision)
1293 num.overflow = !num.unsignedp && !num_zerop (num);
1294 num.high = num.low = 0;
1296 else
1298 cpp_num orig, maybe_orig;
1299 size_t m = n;
1301 orig = num;
1302 if (m >= PART_PRECISION)
1304 m -= PART_PRECISION;
1305 num.high = num.low;
1306 num.low = 0;
1308 if (m)
1310 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1311 num.low <<= m;
1313 num = num_trim (num, precision);
1315 if (num.unsignedp)
1316 num.overflow = false;
1317 else
1319 maybe_orig = num_rshift (num, precision, n);
1320 num.overflow = !num_eq (orig, maybe_orig);
1324 return num;
1327 /* The four unary operators: +, -, ! and ~. */
1328 static cpp_num
1329 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1331 switch (op)
1333 case CPP_UPLUS:
1334 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1335 cpp_error (pfile, CPP_DL_WARNING,
1336 "traditional C rejects the unary plus operator");
1337 num.overflow = false;
1338 break;
1340 case CPP_UMINUS:
1341 num = num_negate (num, CPP_OPTION (pfile, precision));
1342 break;
1344 case CPP_COMPL:
1345 num.high = ~num.high;
1346 num.low = ~num.low;
1347 num = num_trim (num, CPP_OPTION (pfile, precision));
1348 num.overflow = false;
1349 break;
1351 default: /* case CPP_NOT: */
1352 num.low = num_zerop (num);
1353 num.high = 0;
1354 num.overflow = false;
1355 num.unsignedp = false;
1356 break;
1359 return num;
1362 /* The various binary operators. */
1363 static cpp_num
1364 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1366 cpp_num result;
1367 size_t precision = CPP_OPTION (pfile, precision);
1368 size_t n;
1370 switch (op)
1372 /* Shifts. */
1373 case CPP_LSHIFT:
1374 case CPP_RSHIFT:
1375 if (!rhs.unsignedp && !num_positive (rhs, precision))
1377 /* A negative shift is a positive shift the other way. */
1378 if (op == CPP_LSHIFT)
1379 op = CPP_RSHIFT;
1380 else
1381 op = CPP_LSHIFT;
1382 rhs = num_negate (rhs, precision);
1384 if (rhs.high)
1385 n = ~0; /* Maximal. */
1386 else
1387 n = rhs.low;
1388 if (op == CPP_LSHIFT)
1389 lhs = num_lshift (lhs, precision, n);
1390 else
1391 lhs = num_rshift (lhs, precision, n);
1392 break;
1394 /* Arithmetic. */
1395 case CPP_MINUS:
1396 rhs = num_negate (rhs, precision);
1397 case CPP_PLUS:
1398 result.low = lhs.low + rhs.low;
1399 result.high = lhs.high + rhs.high;
1400 if (result.low < lhs.low)
1401 result.high++;
1402 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1403 result.overflow = false;
1405 result = num_trim (result, precision);
1406 if (!result.unsignedp)
1408 bool lhsp = num_positive (lhs, precision);
1409 result.overflow = (lhsp == num_positive (rhs, precision)
1410 && lhsp != num_positive (result, precision));
1412 return result;
1414 /* Comma. */
1415 default: /* case CPP_COMMA: */
1416 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1417 || !pfile->state.skip_eval))
1418 cpp_error (pfile, CPP_DL_PEDWARN,
1419 "comma operator in operand of #if");
1420 lhs = rhs;
1421 break;
1424 return lhs;
1427 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1428 cannot overflow. */
1429 static cpp_num
1430 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1432 cpp_num result;
1433 cpp_num_part middle[2], temp;
1435 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1436 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1438 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1439 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1441 temp = result.low;
1442 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1443 if (result.low < temp)
1444 result.high++;
1446 temp = result.low;
1447 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1448 if (result.low < temp)
1449 result.high++;
1451 result.high += HIGH_PART (middle[0]);
1452 result.high += HIGH_PART (middle[1]);
1453 result.unsignedp = true;
1454 result.overflow = false;
1456 return result;
1459 /* Multiply two preprocessing numbers. */
1460 static cpp_num
1461 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1463 cpp_num result, temp;
1464 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1465 bool overflow, negate = false;
1466 size_t precision = CPP_OPTION (pfile, precision);
1468 /* Prepare for unsigned multiplication. */
1469 if (!unsignedp)
1471 if (!num_positive (lhs, precision))
1472 negate = !negate, lhs = num_negate (lhs, precision);
1473 if (!num_positive (rhs, precision))
1474 negate = !negate, rhs = num_negate (rhs, precision);
1477 overflow = lhs.high && rhs.high;
1478 result = num_part_mul (lhs.low, rhs.low);
1480 temp = num_part_mul (lhs.high, rhs.low);
1481 result.high += temp.low;
1482 if (temp.high)
1483 overflow = true;
1485 temp = num_part_mul (lhs.low, rhs.high);
1486 result.high += temp.low;
1487 if (temp.high)
1488 overflow = true;
1490 temp.low = result.low, temp.high = result.high;
1491 result = num_trim (result, precision);
1492 if (!num_eq (result, temp))
1493 overflow = true;
1495 if (negate)
1496 result = num_negate (result, precision);
1498 if (unsignedp)
1499 result.overflow = false;
1500 else
1501 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1502 && !num_zerop (result));
1503 result.unsignedp = unsignedp;
1505 return result;
1508 /* Divide two preprocessing numbers, returning the answer or the
1509 remainder depending upon OP. */
1510 static cpp_num
1511 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1513 cpp_num result, sub;
1514 cpp_num_part mask;
1515 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1516 bool negate = false, lhs_neg = false;
1517 size_t i, precision = CPP_OPTION (pfile, precision);
1519 /* Prepare for unsigned division. */
1520 if (!unsignedp)
1522 if (!num_positive (lhs, precision))
1523 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1524 if (!num_positive (rhs, precision))
1525 negate = !negate, rhs = num_negate (rhs, precision);
1528 /* Find the high bit. */
1529 if (rhs.high)
1531 i = precision - 1;
1532 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1533 for (; ; i--, mask >>= 1)
1534 if (rhs.high & mask)
1535 break;
1537 else if (rhs.low)
1539 if (precision > PART_PRECISION)
1540 i = precision - PART_PRECISION - 1;
1541 else
1542 i = precision - 1;
1543 mask = (cpp_num_part) 1 << i;
1544 for (; ; i--, mask >>= 1)
1545 if (rhs.low & mask)
1546 break;
1548 else
1550 if (!pfile->state.skip_eval)
1551 cpp_error (pfile, CPP_DL_ERROR, "division by zero in #if");
1552 return lhs;
1555 /* First nonzero bit of RHS is bit I. Do naive division by
1556 shifting the RHS fully left, and subtracting from LHS if LHS is
1557 at least as big, and then repeating but with one less shift.
1558 This is not very efficient, but is easy to understand. */
1560 rhs.unsignedp = true;
1561 lhs.unsignedp = true;
1562 i = precision - i - 1;
1563 sub = num_lshift (rhs, precision, i);
1565 result.high = result.low = 0;
1566 for (;;)
1568 if (num_greater_eq (lhs, sub, precision))
1570 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1571 if (i >= PART_PRECISION)
1572 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1573 else
1574 result.low |= (cpp_num_part) 1 << i;
1576 if (i-- == 0)
1577 break;
1578 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1579 sub.high >>= 1;
1582 /* We divide so that the remainder has the sign of the LHS. */
1583 if (op == CPP_DIV)
1585 result.unsignedp = unsignedp;
1586 result.overflow = false;
1587 if (!unsignedp)
1589 if (negate)
1590 result = num_negate (result, precision);
1591 result.overflow = (num_positive (result, precision) ^ !negate
1592 && !num_zerop (result));
1595 return result;
1598 /* CPP_MOD. */
1599 lhs.unsignedp = unsignedp;
1600 lhs.overflow = false;
1601 if (lhs_neg)
1602 lhs = num_negate (lhs, precision);
1604 return lhs;