003-02-18 Nick Clifton <nickc@redhat.com>
[official-gcc.git] / gcc / cppexp.c
blob202b2d01a597c042ed3b6f4e893a84772e96b003
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "cpplib.h"
26 #include "cpphash.h"
28 #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
29 #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
30 #define LOW_PART(num_part) (num_part & HALF_MASK)
31 #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
33 struct op
35 const cpp_token *token; /* The token forming op (for diagnostics). */
36 cpp_num value; /* The value logically "right" of op. */
37 enum cpp_ttype op;
40 /* Some simple utility routines on double integers. */
41 #define num_zerop(num) ((num.low | num.high) == 0)
42 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
43 static bool num_positive PARAMS ((cpp_num, size_t));
44 static bool num_greater_eq PARAMS ((cpp_num, cpp_num, size_t));
45 static cpp_num num_trim PARAMS ((cpp_num, size_t));
46 static cpp_num num_part_mul PARAMS ((cpp_num_part, cpp_num_part));
48 static cpp_num num_unary_op PARAMS ((cpp_reader *, cpp_num, enum cpp_ttype));
49 static cpp_num num_binary_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
50 enum cpp_ttype));
51 static cpp_num num_negate PARAMS ((cpp_num, size_t));
52 static cpp_num num_bitwise_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
53 enum cpp_ttype));
54 static cpp_num num_inequality_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
55 enum cpp_ttype));
56 static cpp_num num_equality_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
57 enum cpp_ttype));
58 static cpp_num num_mul PARAMS ((cpp_reader *, cpp_num, cpp_num));
59 static cpp_num num_div_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
60 enum cpp_ttype));
61 static cpp_num num_lshift PARAMS ((cpp_num, size_t, size_t));
62 static cpp_num num_rshift PARAMS ((cpp_num, size_t, size_t));
64 static cpp_num append_digit PARAMS ((cpp_num, int, int, size_t));
65 static cpp_num parse_defined PARAMS ((cpp_reader *));
66 static cpp_num eval_token PARAMS ((cpp_reader *, const cpp_token *));
67 static struct op *reduce PARAMS ((cpp_reader *, struct op *, enum cpp_ttype));
68 static unsigned int interpret_float_suffix PARAMS ((const uchar *, size_t));
69 static unsigned int interpret_int_suffix PARAMS ((const uchar *, size_t));
70 static void check_promotion PARAMS ((cpp_reader *, const struct op *));
72 /* Token type abuse to create unary plus and minus operators. */
73 #define CPP_UPLUS (CPP_LAST_CPP_OP + 1)
74 #define CPP_UMINUS (CPP_LAST_CPP_OP + 2)
76 /* With -O2, gcc appears to produce nice code, moving the error
77 message load and subsequent jump completely out of the main path. */
78 #define SYNTAX_ERROR(msgid) \
79 do { cpp_error (pfile, DL_ERROR, msgid); goto syntax_error; } while(0)
80 #define SYNTAX_ERROR2(msgid, arg) \
81 do { cpp_error (pfile, DL_ERROR, msgid, arg); goto syntax_error; } while(0)
83 /* Subroutine of cpp_classify_number. S points to a float suffix of
84 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
85 flag vector describing the suffix. */
86 static unsigned int
87 interpret_float_suffix (s, len)
88 const uchar *s;
89 size_t len;
91 size_t f = 0, l = 0, i = 0;
93 while (len--)
94 switch (s[len])
96 case 'f': case 'F': f++; break;
97 case 'l': case 'L': l++; break;
98 case 'i': case 'I':
99 case 'j': case 'J': i++; break;
100 default:
101 return 0;
104 if (f + l > 1 || i > 1)
105 return 0;
107 return ((i ? CPP_N_IMAGINARY : 0)
108 | (f ? CPP_N_SMALL :
109 l ? CPP_N_LARGE : CPP_N_MEDIUM));
112 /* Subroutine of cpp_classify_number. S points to an integer suffix
113 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
114 flag vector describing the suffix. */
115 static unsigned int
116 interpret_int_suffix (s, len)
117 const uchar *s;
118 size_t len;
120 size_t u, l, i;
122 u = l = i = 0;
124 while (len--)
125 switch (s[len])
127 case 'u': case 'U': u++; break;
128 case 'i': case 'I':
129 case 'j': case 'J': i++; break;
130 case 'l': case 'L': l++;
131 /* If there are two Ls, they must be adjacent and the same case. */
132 if (l == 2 && s[len] != s[len + 1])
133 return 0;
134 break;
135 default:
136 return 0;
139 if (l > 2 || u > 1 || i > 1)
140 return 0;
142 return ((i ? CPP_N_IMAGINARY : 0)
143 | (u ? CPP_N_UNSIGNED : 0)
144 | ((l == 0) ? CPP_N_SMALL
145 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
148 /* Categorize numeric constants according to their field (integer,
149 floating point, or invalid), radix (decimal, octal, hexadecimal),
150 and type suffixes. */
151 unsigned int
152 cpp_classify_number (pfile, token)
153 cpp_reader *pfile;
154 const cpp_token *token;
156 const uchar *str = token->val.str.text;
157 const uchar *limit;
158 unsigned int max_digit, result, radix;
159 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
161 /* If the lexer has done its job, length one can only be a single
162 digit. Fast-path this very common case. */
163 if (token->val.str.len == 1)
164 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
166 limit = str + token->val.str.len;
167 float_flag = NOT_FLOAT;
168 max_digit = 0;
169 radix = 10;
171 /* First, interpret the radix. */
172 if (*str == '0')
174 radix = 8;
175 str++;
177 /* Require at least one hex digit to classify it as hex. */
178 if ((*str == 'x' || *str == 'X') && ISXDIGIT (str[1]))
180 radix = 16;
181 str++;
185 /* Now scan for a well-formed integer or float. */
186 for (;;)
188 unsigned int c = *str++;
190 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
192 c = hex_value (c);
193 if (c > max_digit)
194 max_digit = c;
196 else if (c == '.')
198 if (float_flag == NOT_FLOAT)
199 float_flag = AFTER_POINT;
200 else
201 SYNTAX_ERROR ("too many decimal points in number");
203 else if ((radix <= 10 && (c == 'e' || c == 'E'))
204 || (radix == 16 && (c == 'p' || c == 'P')))
206 float_flag = AFTER_EXPON;
207 break;
209 else
211 /* Start of suffix. */
212 str--;
213 break;
217 if (float_flag != NOT_FLOAT && radix == 8)
218 radix = 10;
220 if (max_digit >= radix)
221 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
223 if (float_flag != NOT_FLOAT)
225 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
226 cpp_error (pfile, DL_PEDWARN,
227 "use of C99 hexadecimal floating constant");
229 if (float_flag == AFTER_EXPON)
231 if (*str == '+' || *str == '-')
232 str++;
234 /* Exponent is decimal, even if string is a hex float. */
235 if (!ISDIGIT (*str))
236 SYNTAX_ERROR ("exponent has no digits");
239 str++;
240 while (ISDIGIT (*str));
242 else if (radix == 16)
243 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
245 result = interpret_float_suffix (str, limit - str);
246 if (result == 0)
248 cpp_error (pfile, DL_ERROR,
249 "invalid suffix \"%.*s\" on floating constant",
250 (int) (limit - str), str);
251 return CPP_N_INVALID;
254 /* Traditional C didn't accept any floating suffixes. */
255 if (limit != str
256 && CPP_WTRADITIONAL (pfile)
257 && ! cpp_sys_macro_p (pfile))
258 cpp_error (pfile, DL_WARNING,
259 "traditional C rejects the \"%.*s\" suffix",
260 (int) (limit - str), str);
262 result |= CPP_N_FLOATING;
264 else
266 result = interpret_int_suffix (str, limit - str);
267 if (result == 0)
269 cpp_error (pfile, DL_ERROR,
270 "invalid suffix \"%.*s\" on integer constant",
271 (int) (limit - str), str);
272 return CPP_N_INVALID;
275 /* Traditional C only accepted the 'L' suffix.
276 Suppress warning about 'LL' with -Wno-long-long. */
277 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
279 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
280 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
282 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
283 cpp_error (pfile, DL_WARNING,
284 "traditional C rejects the \"%.*s\" suffix",
285 (int) (limit - str), str);
288 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
289 && ! CPP_OPTION (pfile, c99)
290 && CPP_OPTION (pfile, warn_long_long))
291 cpp_error (pfile, DL_PEDWARN, "use of C99 long long integer constant");
293 result |= CPP_N_INTEGER;
296 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
297 cpp_error (pfile, DL_PEDWARN, "imaginary constants are a GCC extension");
299 if (radix == 10)
300 result |= CPP_N_DECIMAL;
301 else if (radix == 16)
302 result |= CPP_N_HEX;
303 else
304 result |= CPP_N_OCTAL;
306 return result;
308 syntax_error:
309 return CPP_N_INVALID;
312 /* cpp_interpret_integer converts an integer constant into a cpp_num,
313 of precision options->precision.
315 We do not provide any interface for decimal->float conversion,
316 because the preprocessor doesn't need it and the floating point
317 handling in GCC proper is too ugly to speak of. */
318 cpp_num
319 cpp_interpret_integer (pfile, token, type)
320 cpp_reader *pfile;
321 const cpp_token *token;
322 unsigned int type;
324 const uchar *p, *end;
325 cpp_num result;
327 result.low = 0;
328 result.high = 0;
329 result.unsignedp = !!(type & CPP_N_UNSIGNED);
330 result.overflow = false;
332 p = token->val.str.text;
333 end = p + token->val.str.len;
335 /* Common case of a single digit. */
336 if (token->val.str.len == 1)
337 result.low = p[0] - '0';
338 else
340 cpp_num_part max;
341 size_t precision = CPP_OPTION (pfile, precision);
342 unsigned int base = 10, c = 0;
343 bool overflow = false;
345 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
347 base = 8;
348 p++;
350 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
352 base = 16;
353 p += 2;
356 /* We can add a digit to numbers strictly less than this without
357 needing the precision and slowness of double integers. */
358 max = ~(cpp_num_part) 0;
359 if (precision < PART_PRECISION)
360 max >>= PART_PRECISION - precision;
361 max = (max - base + 1) / base + 1;
363 for (; p < end; p++)
365 c = *p;
367 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
368 c = hex_value (c);
369 else
370 break;
372 /* Strict inequality for when max is set to zero. */
373 if (result.low < max)
374 result.low = result.low * base + c;
375 else
377 result = append_digit (result, c, base, precision);
378 overflow |= result.overflow;
379 max = 0;
383 if (overflow)
384 cpp_error (pfile, DL_PEDWARN,
385 "integer constant is too large for its type");
386 /* If too big to be signed, consider it unsigned. Only warn for
387 decimal numbers. Traditional numbers were always signed (but
388 we still honor an explicit U suffix); but we only have
389 traditional semantics in directives. */
390 else if (!result.unsignedp
391 && !(CPP_OPTION (pfile, traditional)
392 && pfile->state.in_directive)
393 && !num_positive (result, precision))
395 if (base == 10)
396 cpp_error (pfile, DL_WARNING,
397 "integer constant is so large that it is unsigned");
398 result.unsignedp = true;
402 return result;
405 /* Append DIGIT to NUM, a number of PRECISION bits being read in base
406 BASE. */
407 static cpp_num
408 append_digit (num, digit, base, precision)
409 cpp_num num;
410 int digit, base;
411 size_t precision;
413 cpp_num result;
414 unsigned int shift = 3 + (base == 16);
415 bool overflow;
416 cpp_num_part add_high, add_low;
418 /* Multiply by 8 or 16. Catching this overflow here means we don't
419 need to worry about add_high overflowing. */
420 overflow = !!(num.high >> (PART_PRECISION - shift));
421 result.high = num.high << shift;
422 result.low = num.low << shift;
423 result.high |= num.low >> (PART_PRECISION - shift);
425 if (base == 10)
427 add_low = num.low << 1;
428 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
430 else
431 add_high = add_low = 0;
433 if (add_low + digit < add_low)
434 add_high++;
435 add_low += digit;
437 if (result.low + add_low < result.low)
438 add_high++;
439 if (result.high + add_high < result.high)
440 overflow = true;
442 result.low += add_low;
443 result.high += add_high;
445 /* The above code catches overflow of a cpp_num type. This catches
446 overflow of the (possibly shorter) target precision. */
447 num.low = result.low;
448 num.high = result.high;
449 result = num_trim (result, precision);
450 if (!num_eq (result, num))
451 overflow = true;
453 result.unsignedp = num.unsignedp;
454 result.overflow = overflow;
455 return result;
458 /* Handle meeting "defined" in a preprocessor expression. */
459 static cpp_num
460 parse_defined (pfile)
461 cpp_reader *pfile;
463 cpp_num result;
464 int paren = 0;
465 cpp_hashnode *node = 0;
466 const cpp_token *token;
467 cpp_context *initial_context = pfile->context;
469 /* Don't expand macros. */
470 pfile->state.prevent_expansion++;
472 token = cpp_get_token (pfile);
473 if (token->type == CPP_OPEN_PAREN)
475 paren = 1;
476 token = cpp_get_token (pfile);
479 if (token->type == CPP_NAME)
481 node = token->val.node;
482 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
484 cpp_error (pfile, DL_ERROR, "missing ')' after \"defined\"");
485 node = 0;
488 else
490 cpp_error (pfile, DL_ERROR,
491 "operator \"defined\" requires an identifier");
492 if (token->flags & NAMED_OP)
494 cpp_token op;
496 op.flags = 0;
497 op.type = token->type;
498 cpp_error (pfile, DL_ERROR,
499 "(\"%s\" is an alternative token for \"%s\" in C++)",
500 cpp_token_as_text (pfile, token),
501 cpp_token_as_text (pfile, &op));
505 if (node)
507 if (pfile->context != initial_context)
508 cpp_error (pfile, DL_WARNING,
509 "this use of \"defined\" may not be portable");
511 _cpp_mark_macro_used (node);
513 /* A possible controlling macro of the form #if !defined ().
514 _cpp_parse_expr checks there was no other junk on the line. */
515 pfile->mi_ind_cmacro = node;
518 pfile->state.prevent_expansion--;
520 result.unsignedp = false;
521 result.high = 0;
522 result.overflow = false;
523 result.low = node && node->type == NT_MACRO;
524 return result;
527 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
528 number or character constant, or the result of the "defined" or "#"
529 operators). */
530 static cpp_num
531 eval_token (pfile, token)
532 cpp_reader *pfile;
533 const cpp_token *token;
535 cpp_num result;
536 unsigned int temp;
537 int unsignedp = 0;
539 switch (token->type)
541 case CPP_NUMBER:
542 temp = cpp_classify_number (pfile, token);
543 switch (temp & CPP_N_CATEGORY)
545 case CPP_N_FLOATING:
546 cpp_error (pfile, DL_ERROR,
547 "floating constant in preprocessor expression");
548 break;
549 case CPP_N_INTEGER:
550 if (!(temp & CPP_N_IMAGINARY))
551 return cpp_interpret_integer (pfile, token, temp);
552 cpp_error (pfile, DL_ERROR,
553 "imaginary number in preprocessor expression");
554 break;
556 case CPP_N_INVALID:
557 /* Error already issued. */
558 break;
560 result.high = result.low = 0;
561 break;
563 case CPP_WCHAR:
564 case CPP_CHAR:
566 cppchar_t cc = cpp_interpret_charconst (pfile, token,
567 &temp, &unsignedp);
569 result.high = 0;
570 result.low = cc;
571 /* Sign-extend the result if necessary. */
572 if (!unsignedp && (cppchar_signed_t) cc < 0)
574 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
575 result.low |= ~(~(cpp_num_part) 0
576 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
577 result.high = ~(cpp_num_part) 0;
578 result = num_trim (result, CPP_OPTION (pfile, precision));
581 break;
583 case CPP_NAME:
584 if (token->val.node == pfile->spec_nodes.n_defined)
585 return parse_defined (pfile);
586 else if (CPP_OPTION (pfile, cplusplus)
587 && (token->val.node == pfile->spec_nodes.n_true
588 || token->val.node == pfile->spec_nodes.n_false))
590 result.high = 0;
591 result.low = (token->val.node == pfile->spec_nodes.n_true);
593 /* Warn about use of true or false in #if when pedantic
594 and stdbool.h has not been included. */
595 if (CPP_PEDANTIC (pfile)
596 && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
597 cpp_error (pfile, DL_PEDWARN,
598 "ISO C++ does not permit \"%s\" in #if",
599 NODE_NAME (token->val.node));
601 else
603 result.high = 0;
604 result.low = 0;
605 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
606 cpp_error (pfile, DL_WARNING, "\"%s\" is not defined",
607 NODE_NAME (token->val.node));
609 break;
611 default: /* CPP_HASH */
612 _cpp_test_assertion (pfile, &temp);
613 result.high = 0;
614 result.low = temp;
617 result.unsignedp = !!unsignedp;
618 result.overflow = false;
619 return result;
622 /* Operator precedence and flags table.
624 After an operator is returned from the lexer, if it has priority less
625 than the operator on the top of the stack, we reduce the stack by one
626 operator and repeat the test. Since equal priorities do not reduce,
627 this is naturally right-associative.
629 We handle left-associative operators by decrementing the priority of
630 just-lexed operators by one, but retaining the priority of operators
631 already on the stack.
633 The remaining cases are '(' and ')'. We handle '(' by skipping the
634 reduction phase completely. ')' is given lower priority than
635 everything else, including '(', effectively forcing a reduction of the
636 parenthesized expression. If there is a matching '(', the routine
637 reduce() exits immediately. If the normal exit route sees a ')', then
638 there cannot have been a matching '(' and an error message is output.
640 The parser assumes all shifted operators require a left operand unless
641 the flag NO_L_OPERAND is set. These semantics are automatic; any
642 extra semantics need to be handled with operator-specific code. */
644 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
645 operand changes because of integer promotions. */
646 #define NO_L_OPERAND (1 << 0)
647 #define LEFT_ASSOC (1 << 1)
648 #define CHECK_PROMOTION (1 << 2)
650 /* Operator to priority map. Must be in the same order as the first
651 N entries of enum cpp_ttype. */
652 static const struct operator
654 uchar prio;
655 uchar flags;
656 } optab[] =
658 /* EQ */ {0, 0}, /* Shouldn't happen. */
659 /* NOT */ {16, NO_L_OPERAND},
660 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
661 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
662 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
663 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
664 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
665 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
666 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
667 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
668 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
669 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
670 /* RSHIFT */ {13, LEFT_ASSOC},
671 /* LSHIFT */ {13, LEFT_ASSOC},
673 /* MIN */ {10, LEFT_ASSOC | CHECK_PROMOTION},
674 /* MAX */ {10, LEFT_ASSOC | CHECK_PROMOTION},
676 /* COMPL */ {16, NO_L_OPERAND},
677 /* AND_AND */ {6, LEFT_ASSOC},
678 /* OR_OR */ {5, LEFT_ASSOC},
679 /* QUERY */ {3, 0},
680 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
681 /* COMMA */ {2, LEFT_ASSOC},
682 /* OPEN_PAREN */ {1, NO_L_OPERAND},
683 /* CLOSE_PAREN */ {0, 0},
684 /* EOF */ {0, 0},
685 /* EQ_EQ */ {11, LEFT_ASSOC},
686 /* NOT_EQ */ {11, LEFT_ASSOC},
687 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
688 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
689 /* UPLUS */ {16, NO_L_OPERAND},
690 /* UMINUS */ {16, NO_L_OPERAND}
693 /* Parse and evaluate a C expression, reading from PFILE.
694 Returns the truth value of the expression.
696 The implementation is an operator precedence parser, i.e. a
697 bottom-up parser, using a stack for not-yet-reduced tokens.
699 The stack base is op_stack, and the current stack pointer is 'top'.
700 There is a stack element for each operator (only), and the most
701 recently pushed operator is 'top->op'. An operand (value) is
702 stored in the 'value' field of the stack element of the operator
703 that precedes it. */
704 bool
705 _cpp_parse_expr (pfile)
706 cpp_reader *pfile;
708 struct op *top = pfile->op_stack;
709 unsigned int lex_count;
710 bool saw_leading_not, want_value = true;
712 pfile->state.skip_eval = 0;
714 /* Set up detection of #if ! defined(). */
715 pfile->mi_ind_cmacro = 0;
716 saw_leading_not = false;
717 lex_count = 0;
719 /* Lowest priority operator prevents further reductions. */
720 top->op = CPP_EOF;
722 for (;;)
724 struct op op;
726 lex_count++;
727 op.token = cpp_get_token (pfile);
728 op.op = op.token->type;
730 switch (op.op)
732 /* These tokens convert into values. */
733 case CPP_NUMBER:
734 case CPP_CHAR:
735 case CPP_WCHAR:
736 case CPP_NAME:
737 case CPP_HASH:
738 if (!want_value)
739 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
740 cpp_token_as_text (pfile, op.token));
741 want_value = false;
742 top->value = eval_token (pfile, op.token);
743 continue;
745 case CPP_NOT:
746 saw_leading_not = lex_count == 1;
747 break;
748 case CPP_PLUS:
749 if (want_value)
750 op.op = CPP_UPLUS;
751 break;
752 case CPP_MINUS:
753 if (want_value)
754 op.op = CPP_UMINUS;
755 break;
756 case CPP_OTHER:
757 if (ISGRAPH (op.token->val.c))
758 SYNTAX_ERROR2 ("invalid character '%c' in #if", op.token->val.c);
759 else
760 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if",
761 op.token->val.c);
763 default:
764 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
765 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
766 cpp_token_as_text (pfile, op.token));
767 break;
770 /* Check we have a value or operator as appropriate. */
771 if (optab[op.op].flags & NO_L_OPERAND)
773 if (!want_value)
774 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
775 cpp_token_as_text (pfile, op.token));
777 else if (want_value)
779 /* Ordering here is subtle and intended to favor the
780 missing parenthesis diagnostics over alternatives. */
781 if (op.op == CPP_CLOSE_PAREN)
783 if (top->op == CPP_OPEN_PAREN)
784 SYNTAX_ERROR ("void expression between '(' and ')'");
786 else if (top->op == CPP_EOF)
787 SYNTAX_ERROR ("#if with no expression");
788 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
789 SYNTAX_ERROR2 ("operator '%s' has no right operand",
790 cpp_token_as_text (pfile, top->token));
793 top = reduce (pfile, top, op.op);
794 if (!top)
795 goto syntax_error;
797 if (op.op == CPP_EOF)
798 break;
800 switch (op.op)
802 case CPP_CLOSE_PAREN:
803 continue;
804 case CPP_OR_OR:
805 if (!num_zerop (top->value))
806 pfile->state.skip_eval++;
807 break;
808 case CPP_AND_AND:
809 case CPP_QUERY:
810 if (num_zerop (top->value))
811 pfile->state.skip_eval++;
812 break;
813 case CPP_COLON:
814 if (top->op != CPP_QUERY)
815 SYNTAX_ERROR (" ':' without preceding '?'");
816 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
817 pfile->state.skip_eval++;
818 else
819 pfile->state.skip_eval--;
820 default:
821 break;
824 want_value = true;
826 /* Check for and handle stack overflow. */
827 if (++top == pfile->op_limit)
828 top = _cpp_expand_op_stack (pfile);
830 top->op = op.op;
831 top->token = op.token;
834 /* The controlling macro expression is only valid if we called lex 3
835 times: <!> <defined expression> and <EOF>. push_conditional ()
836 checks that we are at top-of-file. */
837 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
838 pfile->mi_ind_cmacro = 0;
840 if (top != pfile->op_stack)
842 cpp_error (pfile, DL_ICE, "unbalanced stack in #if");
843 syntax_error:
844 return false; /* Return false on syntax error. */
847 return !num_zerop (top->value);
850 /* Reduce the operator / value stack if possible, in preparation for
851 pushing operator OP. Returns NULL on error, otherwise the top of
852 the stack. */
853 static struct op *
854 reduce (pfile, top, op)
855 cpp_reader *pfile;
856 struct op *top;
857 enum cpp_ttype op;
859 unsigned int prio;
861 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
863 bad_op:
864 cpp_error (pfile, DL_ICE, "impossible operator '%u'", top->op);
865 return 0;
868 if (op == CPP_OPEN_PAREN)
869 return top;
871 /* Decrement the priority of left-associative operators to force a
872 reduction with operators of otherwise equal priority. */
873 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
874 while (prio < optab[top->op].prio)
876 if (CPP_OPTION (pfile, warn_num_sign_change)
877 && optab[top->op].flags & CHECK_PROMOTION)
878 check_promotion (pfile, top);
880 switch (top->op)
882 case CPP_UPLUS:
883 case CPP_UMINUS:
884 case CPP_NOT:
885 case CPP_COMPL:
886 top[-1].value = num_unary_op (pfile, top->value, top->op);
887 break;
889 case CPP_PLUS:
890 case CPP_MINUS:
891 case CPP_RSHIFT:
892 case CPP_LSHIFT:
893 case CPP_MIN:
894 case CPP_MAX:
895 case CPP_COMMA:
896 top[-1].value = num_binary_op (pfile, top[-1].value,
897 top->value, top->op);
898 break;
900 case CPP_GREATER:
901 case CPP_LESS:
902 case CPP_GREATER_EQ:
903 case CPP_LESS_EQ:
904 top[-1].value
905 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
906 break;
908 case CPP_EQ_EQ:
909 case CPP_NOT_EQ:
910 top[-1].value
911 = num_equality_op (pfile, top[-1].value, top->value, top->op);
912 break;
914 case CPP_AND:
915 case CPP_OR:
916 case CPP_XOR:
917 top[-1].value
918 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
919 break;
921 case CPP_MULT:
922 top[-1].value = num_mul (pfile, top[-1].value, top->value);
923 break;
925 case CPP_DIV:
926 case CPP_MOD:
927 top[-1].value = num_div_op (pfile, top[-1].value,
928 top->value, top->op);
929 break;
931 case CPP_OR_OR:
932 top--;
933 if (!num_zerop (top->value))
934 pfile->state.skip_eval--;
935 top->value.low = (!num_zerop (top->value)
936 || !num_zerop (top[1].value));
937 top->value.high = 0;
938 top->value.unsignedp = false;
939 top->value.overflow = false;
940 continue;
942 case CPP_AND_AND:
943 top--;
944 if (num_zerop (top->value))
945 pfile->state.skip_eval--;
946 top->value.low = (!num_zerop (top->value)
947 && !num_zerop (top[1].value));
948 top->value.high = 0;
949 top->value.unsignedp = false;
950 top->value.overflow = false;
951 continue;
953 case CPP_OPEN_PAREN:
954 if (op != CPP_CLOSE_PAREN)
956 cpp_error (pfile, DL_ERROR, "missing ')' in expression");
957 return 0;
959 top--;
960 top->value = top[1].value;
961 return top;
963 case CPP_COLON:
964 top -= 2;
965 if (!num_zerop (top->value))
967 pfile->state.skip_eval--;
968 top->value = top[1].value;
970 else
971 top->value = top[2].value;
972 top->value.unsignedp = (top[1].value.unsignedp
973 || top[2].value.unsignedp);
974 continue;
976 case CPP_QUERY:
977 cpp_error (pfile, DL_ERROR, "'?' without following ':'");
978 return 0;
980 default:
981 goto bad_op;
984 top--;
985 if (top->value.overflow && !pfile->state.skip_eval)
986 cpp_error (pfile, DL_PEDWARN,
987 "integer overflow in preprocessor expression");
990 if (op == CPP_CLOSE_PAREN)
992 cpp_error (pfile, DL_ERROR, "missing '(' in expression");
993 return 0;
996 return top;
999 /* Returns the position of the old top of stack after expansion. */
1000 struct op *
1001 _cpp_expand_op_stack (pfile)
1002 cpp_reader *pfile;
1004 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1005 size_t new_size = old_size * 2 + 20;
1007 pfile->op_stack = (struct op *) xrealloc (pfile->op_stack,
1008 new_size * sizeof (struct op));
1009 pfile->op_limit = pfile->op_stack + new_size;
1011 return pfile->op_stack + old_size;
1014 /* Emits a warning if the effective sign of either operand of OP
1015 changes because of integer promotions. */
1016 static void
1017 check_promotion (pfile, op)
1018 cpp_reader *pfile;
1019 const struct op *op;
1021 if (op->value.unsignedp == op[-1].value.unsignedp)
1022 return;
1024 if (op->value.unsignedp)
1026 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1027 cpp_error (pfile, DL_WARNING,
1028 "the left operand of \"%s\" changes sign when promoted",
1029 cpp_token_as_text (pfile, op->token));
1031 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1032 cpp_error (pfile, DL_WARNING,
1033 "the right operand of \"%s\" changes sign when promoted",
1034 cpp_token_as_text (pfile, op->token));
1037 /* Clears the unused high order bits of the number pointed to by PNUM. */
1038 static cpp_num
1039 num_trim (num, precision)
1040 cpp_num num;
1041 size_t precision;
1043 if (precision > PART_PRECISION)
1045 precision -= PART_PRECISION;
1046 if (precision < PART_PRECISION)
1047 num.high &= ((cpp_num_part) 1 << precision) - 1;
1049 else
1051 if (precision < PART_PRECISION)
1052 num.low &= ((cpp_num_part) 1 << precision) - 1;
1053 num.high = 0;
1056 return num;
1059 /* True iff A (presumed signed) >= 0. */
1060 static bool
1061 num_positive (num, precision)
1062 cpp_num num;
1063 size_t precision;
1065 if (precision > PART_PRECISION)
1067 precision -= PART_PRECISION;
1068 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1071 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1074 /* Sign extend a number, with PRECISION significant bits and all
1075 others assumed clear, to fill out a cpp_num structure. */
1076 cpp_num
1077 cpp_num_sign_extend (num, precision)
1078 cpp_num num;
1079 size_t precision;
1081 if (!num.unsignedp)
1083 if (precision > PART_PRECISION)
1085 precision -= PART_PRECISION;
1086 if (precision < PART_PRECISION
1087 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1088 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1090 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1092 if (precision < PART_PRECISION)
1093 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1094 num.high = ~(cpp_num_part) 0;
1098 return num;
1101 /* Returns the negative of NUM. */
1102 static cpp_num
1103 num_negate (num, precision)
1104 cpp_num num;
1105 size_t precision;
1107 cpp_num copy;
1109 copy = num;
1110 num.high = ~num.high;
1111 num.low = ~num.low;
1112 if (++num.low == 0)
1113 num.high++;
1114 num = num_trim (num, precision);
1115 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1117 return num;
1120 /* Returns true if A >= B. */
1121 static bool
1122 num_greater_eq (pa, pb, precision)
1123 cpp_num pa, pb;
1124 size_t precision;
1126 bool unsignedp;
1128 unsignedp = pa.unsignedp || pb.unsignedp;
1130 if (!unsignedp)
1132 /* Both numbers have signed type. If they are of different
1133 sign, the answer is the sign of A. */
1134 unsignedp = num_positive (pa, precision);
1136 if (unsignedp != num_positive (pb, precision))
1137 return unsignedp;
1139 /* Otherwise we can do an unsigned comparison. */
1142 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1145 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1146 static cpp_num
1147 num_bitwise_op (pfile, lhs, rhs, op)
1148 cpp_reader *pfile ATTRIBUTE_UNUSED;
1149 cpp_num lhs, rhs;
1150 enum cpp_ttype op;
1152 lhs.overflow = false;
1153 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1155 /* As excess precision is zeroed, there is no need to num_trim () as
1156 these operations cannot introduce a set bit there. */
1157 if (op == CPP_AND)
1159 lhs.low &= rhs.low;
1160 lhs.high &= rhs.high;
1162 else if (op == CPP_OR)
1164 lhs.low |= rhs.low;
1165 lhs.high |= rhs.high;
1167 else
1169 lhs.low ^= rhs.low;
1170 lhs.high ^= rhs.high;
1173 return lhs;
1176 /* Returns LHS OP RHS, where OP is an inequality. */
1177 static cpp_num
1178 num_inequality_op (pfile, lhs, rhs, op)
1179 cpp_reader *pfile;
1180 cpp_num lhs, rhs;
1181 enum cpp_ttype op;
1183 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1185 if (op == CPP_GREATER_EQ)
1186 lhs.low = gte;
1187 else if (op == CPP_LESS)
1188 lhs.low = !gte;
1189 else if (op == CPP_GREATER)
1190 lhs.low = gte && !num_eq (lhs, rhs);
1191 else /* CPP_LESS_EQ. */
1192 lhs.low = !gte || num_eq (lhs, rhs);
1194 lhs.high = 0;
1195 lhs.overflow = false;
1196 lhs.unsignedp = false;
1197 return lhs;
1200 /* Returns LHS OP RHS, where OP is == or !=. */
1201 static cpp_num
1202 num_equality_op (pfile, lhs, rhs, op)
1203 cpp_reader *pfile ATTRIBUTE_UNUSED;
1204 cpp_num lhs, rhs;
1205 enum cpp_ttype op;
1207 /* Work around a 3.0.4 bug; see PR 6950. */
1208 bool eq = num_eq (lhs, rhs);
1209 if (op == CPP_NOT_EQ)
1210 eq = !eq;
1211 lhs.low = eq;
1212 lhs.high = 0;
1213 lhs.overflow = false;
1214 lhs.unsignedp = false;
1215 return lhs;
1218 /* Shift NUM, of width PRECISION, right by N bits. */
1219 static cpp_num
1220 num_rshift (num, precision, n)
1221 cpp_num num;
1222 size_t precision, n;
1224 cpp_num_part sign_mask;
1226 if (num.unsignedp || num_positive (num, precision))
1227 sign_mask = 0;
1228 else
1229 sign_mask = ~(cpp_num_part) 0;
1231 if (n >= precision)
1232 num.high = num.low = sign_mask;
1233 else
1235 /* Sign-extend. */
1236 if (precision < PART_PRECISION)
1237 num.high = sign_mask, num.low |= sign_mask << precision;
1238 else if (precision < 2 * PART_PRECISION)
1239 num.high |= sign_mask << (precision - PART_PRECISION);
1241 if (n >= PART_PRECISION)
1243 n -= PART_PRECISION;
1244 num.low = num.high;
1245 num.high = sign_mask;
1248 if (n)
1250 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1251 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1255 num = num_trim (num, precision);
1256 num.overflow = false;
1257 return num;
1260 /* Shift NUM, of width PRECISION, left by N bits. */
1261 static cpp_num
1262 num_lshift (num, precision, n)
1263 cpp_num num;
1264 size_t precision, n;
1266 if (n >= precision)
1268 num.overflow = !num.unsignedp && !num_zerop (num);
1269 num.high = num.low = 0;
1271 else
1273 cpp_num orig, maybe_orig;
1274 size_t m = n;
1276 orig = num;
1277 if (m >= PART_PRECISION)
1279 m -= PART_PRECISION;
1280 num.high = num.low;
1281 num.low = 0;
1283 if (m)
1285 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1286 num.low <<= m;
1288 num = num_trim (num, precision);
1290 if (num.unsignedp)
1291 num.overflow = false;
1292 else
1294 maybe_orig = num_rshift (num, precision, n);
1295 num.overflow = !num_eq (orig, maybe_orig);
1299 return num;
1302 /* The four unary operators: +, -, ! and ~. */
1303 static cpp_num
1304 num_unary_op (pfile, num, op)
1305 cpp_reader *pfile;
1306 cpp_num num;
1307 enum cpp_ttype op;
1309 switch (op)
1311 case CPP_UPLUS:
1312 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1313 cpp_error (pfile, DL_WARNING,
1314 "traditional C rejects the unary plus operator");
1315 num.overflow = false;
1316 break;
1318 case CPP_UMINUS:
1319 num = num_negate (num, CPP_OPTION (pfile, precision));
1320 break;
1322 case CPP_COMPL:
1323 num.high = ~num.high;
1324 num.low = ~num.low;
1325 num = num_trim (num, CPP_OPTION (pfile, precision));
1326 num.overflow = false;
1327 break;
1329 default: /* case CPP_NOT: */
1330 num.low = num_zerop (num);
1331 num.high = 0;
1332 num.overflow = false;
1333 num.unsignedp = false;
1334 break;
1337 return num;
1340 /* The various binary operators. */
1341 static cpp_num
1342 num_binary_op (pfile, lhs, rhs, op)
1343 cpp_reader *pfile;
1344 cpp_num lhs, rhs;
1345 enum cpp_ttype op;
1347 cpp_num result;
1348 size_t precision = CPP_OPTION (pfile, precision);
1349 bool gte;
1350 size_t n;
1352 switch (op)
1354 /* Shifts. */
1355 case CPP_LSHIFT:
1356 case CPP_RSHIFT:
1357 if (!rhs.unsignedp && !num_positive (rhs, precision))
1359 /* A negative shift is a positive shift the other way. */
1360 if (op == CPP_LSHIFT)
1361 op = CPP_RSHIFT;
1362 else
1363 op = CPP_LSHIFT;
1364 rhs = num_negate (rhs, precision);
1366 if (rhs.high)
1367 n = ~0; /* Maximal. */
1368 else
1369 n = rhs.low;
1370 if (op == CPP_LSHIFT)
1371 lhs = num_lshift (lhs, precision, n);
1372 else
1373 lhs = num_rshift (lhs, precision, n);
1374 break;
1376 /* Min / Max. */
1377 case CPP_MIN:
1378 case CPP_MAX:
1380 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1382 gte = num_greater_eq (lhs, rhs, precision);
1383 if (op == CPP_MIN)
1384 gte = !gte;
1385 if (!gte)
1386 lhs = rhs;
1387 lhs.unsignedp = unsignedp;
1389 break;
1391 /* Arithmetic. */
1392 case CPP_MINUS:
1393 rhs = num_negate (rhs, precision);
1394 case CPP_PLUS:
1395 result.low = lhs.low + rhs.low;
1396 result.high = lhs.high + rhs.high;
1397 if (result.low < lhs.low)
1398 result.high++;
1400 result = num_trim (result, precision);
1401 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1402 if (result.unsignedp)
1403 result.overflow = false;
1404 else
1406 bool lhsp = num_positive (lhs, precision);
1407 result.overflow = (lhsp == num_positive (rhs, precision)
1408 && lhsp != num_positive (result, precision));
1410 return result;
1412 /* Comma. */
1413 default: /* case CPP_COMMA: */
1414 if (CPP_PEDANTIC (pfile) && !pfile->state.skip_eval)
1415 cpp_error (pfile, DL_PEDWARN,
1416 "comma operator in operand of #if");
1417 lhs = rhs;
1418 break;
1421 return lhs;
1424 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1425 cannot overflow. */
1426 static cpp_num
1427 num_part_mul (lhs, rhs)
1428 cpp_num_part lhs, rhs;
1430 cpp_num result;
1431 cpp_num_part middle[2], temp;
1433 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1434 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1436 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1437 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1439 temp = result.low;
1440 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1441 if (result.low < temp)
1442 result.high++;
1444 temp = result.low;
1445 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1446 if (result.low < temp)
1447 result.high++;
1449 result.high += HIGH_PART (middle[0]);
1450 result.high += HIGH_PART (middle[1]);
1451 result.unsignedp = 1;
1453 return result;
1456 /* Multiply two preprocessing numbers. */
1457 static cpp_num
1458 num_mul (pfile, lhs, rhs)
1459 cpp_reader *pfile;
1460 cpp_num lhs, rhs;
1462 cpp_num result, temp;
1463 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1464 bool overflow, negate = false;
1465 size_t precision = CPP_OPTION (pfile, precision);
1467 /* Prepare for unsigned multiplication. */
1468 if (!unsignedp)
1470 if (!num_positive (lhs, precision))
1471 negate = !negate, lhs = num_negate (lhs, precision);
1472 if (!num_positive (rhs, precision))
1473 negate = !negate, rhs = num_negate (rhs, precision);
1476 overflow = lhs.high && rhs.high;
1477 result = num_part_mul (lhs.low, rhs.low);
1479 temp = num_part_mul (lhs.high, rhs.low);
1480 result.high += temp.low;
1481 if (temp.high)
1482 overflow = true;
1484 temp = num_part_mul (lhs.low, rhs.high);
1485 result.high += temp.low;
1486 if (temp.high)
1487 overflow = true;
1489 temp.low = result.low, temp.high = result.high;
1490 result = num_trim (result, precision);
1491 if (!num_eq (result, temp))
1492 overflow = true;
1494 if (negate)
1495 result = num_negate (result, precision);
1497 if (unsignedp)
1498 result.overflow = false;
1499 else
1500 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1501 && !num_zerop (result));
1502 result.unsignedp = unsignedp;
1504 return result;
1507 /* Divide two preprocessing numbers, returning the answer or the
1508 remainder depending upon OP. */
1509 static cpp_num
1510 num_div_op (pfile, lhs, rhs, op)
1511 cpp_reader *pfile;
1512 cpp_num lhs, rhs;
1513 enum cpp_ttype op;
1515 cpp_num result, sub;
1516 cpp_num_part mask;
1517 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1518 bool negate = false, lhs_neg = false;
1519 size_t i, precision = CPP_OPTION (pfile, precision);
1521 /* Prepare for unsigned division. */
1522 if (!unsignedp)
1524 if (!num_positive (lhs, precision))
1525 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1526 if (!num_positive (rhs, precision))
1527 negate = !negate, rhs = num_negate (rhs, precision);
1530 /* Find the high bit. */
1531 if (rhs.high)
1533 i = precision - 1;
1534 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1535 for (; ; i--, mask >>= 1)
1536 if (rhs.high & mask)
1537 break;
1539 else if (rhs.low)
1541 if (precision > PART_PRECISION)
1542 i = precision - PART_PRECISION - 1;
1543 else
1544 i = precision - 1;
1545 mask = (cpp_num_part) 1 << i;
1546 for (; ; i--, mask >>= 1)
1547 if (rhs.low & mask)
1548 break;
1550 else
1552 if (!pfile->state.skip_eval)
1553 cpp_error (pfile, DL_ERROR, "division by zero in #if");
1554 return lhs;
1557 /* First nonzero bit of RHS is bit I. Do naive division by
1558 shifting the RHS fully left, and subtracting from LHS if LHS is
1559 at least as big, and then repeating but with one less shift.
1560 This is not very efficient, but is easy to understand. */
1562 rhs.unsignedp = true;
1563 lhs.unsignedp = true;
1564 i = precision - i - 1;
1565 sub = num_lshift (rhs, precision, i);
1567 result.high = result.low = 0;
1568 for (;;)
1570 if (num_greater_eq (lhs, sub, precision))
1572 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1573 if (i >= PART_PRECISION)
1574 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1575 else
1576 result.low |= (cpp_num_part) 1 << i;
1578 if (i-- == 0)
1579 break;
1580 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1581 sub.high >>= 1;
1584 /* We divide so that the remainder has the sign of the LHS. */
1585 if (op == CPP_DIV)
1587 result.unsignedp = unsignedp;
1588 if (unsignedp)
1589 result.overflow = false;
1590 else
1592 if (negate)
1593 result = num_negate (result, precision);
1594 result.overflow = num_positive (result, precision) ^ !negate;
1597 return result;
1600 /* CPP_MOD. */
1601 lhs.unsignedp = unsignedp;
1602 lhs.overflow = false;
1603 if (lhs_neg)
1604 lhs = num_negate (lhs, precision);
1606 return lhs;