2003-04-11 Eric Christopher <echristo@redhat.com>
[official-gcc.git] / gcc / cppexp.c
blob59a08e90a672063d78a258a856adece3b6913731
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')
179 && (str[1] == '.' || ISXDIGIT (str[1])))
181 radix = 16;
182 str++;
186 /* Now scan for a well-formed integer or float. */
187 for (;;)
189 unsigned int c = *str++;
191 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
193 c = hex_value (c);
194 if (c > max_digit)
195 max_digit = c;
197 else if (c == '.')
199 if (float_flag == NOT_FLOAT)
200 float_flag = AFTER_POINT;
201 else
202 SYNTAX_ERROR ("too many decimal points in number");
204 else if ((radix <= 10 && (c == 'e' || c == 'E'))
205 || (radix == 16 && (c == 'p' || c == 'P')))
207 float_flag = AFTER_EXPON;
208 break;
210 else
212 /* Start of suffix. */
213 str--;
214 break;
218 if (float_flag != NOT_FLOAT && radix == 8)
219 radix = 10;
221 if (max_digit >= radix)
222 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
224 if (float_flag != NOT_FLOAT)
226 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
227 cpp_error (pfile, DL_PEDWARN,
228 "use of C99 hexadecimal floating constant");
230 if (float_flag == AFTER_EXPON)
232 if (*str == '+' || *str == '-')
233 str++;
235 /* Exponent is decimal, even if string is a hex float. */
236 if (!ISDIGIT (*str))
237 SYNTAX_ERROR ("exponent has no digits");
240 str++;
241 while (ISDIGIT (*str));
243 else if (radix == 16)
244 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
246 result = interpret_float_suffix (str, limit - str);
247 if (result == 0)
249 cpp_error (pfile, DL_ERROR,
250 "invalid suffix \"%.*s\" on floating constant",
251 (int) (limit - str), str);
252 return CPP_N_INVALID;
255 /* Traditional C didn't accept any floating suffixes. */
256 if (limit != str
257 && CPP_WTRADITIONAL (pfile)
258 && ! cpp_sys_macro_p (pfile))
259 cpp_error (pfile, DL_WARNING,
260 "traditional C rejects the \"%.*s\" suffix",
261 (int) (limit - str), str);
263 result |= CPP_N_FLOATING;
265 else
267 result = interpret_int_suffix (str, limit - str);
268 if (result == 0)
270 cpp_error (pfile, DL_ERROR,
271 "invalid suffix \"%.*s\" on integer constant",
272 (int) (limit - str), str);
273 return CPP_N_INVALID;
276 /* Traditional C only accepted the 'L' suffix.
277 Suppress warning about 'LL' with -Wno-long-long. */
278 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
280 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
281 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
283 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
284 cpp_error (pfile, DL_WARNING,
285 "traditional C rejects the \"%.*s\" suffix",
286 (int) (limit - str), str);
289 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
290 && ! CPP_OPTION (pfile, c99)
291 && CPP_OPTION (pfile, warn_long_long))
292 cpp_error (pfile, DL_PEDWARN, "use of C99 long long integer constant");
294 result |= CPP_N_INTEGER;
297 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
298 cpp_error (pfile, DL_PEDWARN, "imaginary constants are a GCC extension");
300 if (radix == 10)
301 result |= CPP_N_DECIMAL;
302 else if (radix == 16)
303 result |= CPP_N_HEX;
304 else
305 result |= CPP_N_OCTAL;
307 return result;
309 syntax_error:
310 return CPP_N_INVALID;
313 /* cpp_interpret_integer converts an integer constant into a cpp_num,
314 of precision options->precision.
316 We do not provide any interface for decimal->float conversion,
317 because the preprocessor doesn't need it and the floating point
318 handling in GCC proper is too ugly to speak of. */
319 cpp_num
320 cpp_interpret_integer (pfile, token, type)
321 cpp_reader *pfile;
322 const cpp_token *token;
323 unsigned int type;
325 const uchar *p, *end;
326 cpp_num result;
328 result.low = 0;
329 result.high = 0;
330 result.unsignedp = !!(type & CPP_N_UNSIGNED);
331 result.overflow = false;
333 p = token->val.str.text;
334 end = p + token->val.str.len;
336 /* Common case of a single digit. */
337 if (token->val.str.len == 1)
338 result.low = p[0] - '0';
339 else
341 cpp_num_part max;
342 size_t precision = CPP_OPTION (pfile, precision);
343 unsigned int base = 10, c = 0;
344 bool overflow = false;
346 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
348 base = 8;
349 p++;
351 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
353 base = 16;
354 p += 2;
357 /* We can add a digit to numbers strictly less than this without
358 needing the precision and slowness of double integers. */
359 max = ~(cpp_num_part) 0;
360 if (precision < PART_PRECISION)
361 max >>= PART_PRECISION - precision;
362 max = (max - base + 1) / base + 1;
364 for (; p < end; p++)
366 c = *p;
368 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
369 c = hex_value (c);
370 else
371 break;
373 /* Strict inequality for when max is set to zero. */
374 if (result.low < max)
375 result.low = result.low * base + c;
376 else
378 result = append_digit (result, c, base, precision);
379 overflow |= result.overflow;
380 max = 0;
384 if (overflow)
385 cpp_error (pfile, DL_PEDWARN,
386 "integer constant is too large for its type");
387 /* If too big to be signed, consider it unsigned. Only warn for
388 decimal numbers. Traditional numbers were always signed (but
389 we still honor an explicit U suffix); but we only have
390 traditional semantics in directives. */
391 else if (!result.unsignedp
392 && !(CPP_OPTION (pfile, traditional)
393 && pfile->state.in_directive)
394 && !num_positive (result, precision))
396 if (base == 10)
397 cpp_error (pfile, DL_WARNING,
398 "integer constant is so large that it is unsigned");
399 result.unsignedp = true;
403 return result;
406 /* Append DIGIT to NUM, a number of PRECISION bits being read in base
407 BASE. */
408 static cpp_num
409 append_digit (num, digit, base, precision)
410 cpp_num num;
411 int digit, base;
412 size_t precision;
414 cpp_num result;
415 unsigned int shift = 3 + (base == 16);
416 bool overflow;
417 cpp_num_part add_high, add_low;
419 /* Multiply by 8 or 16. Catching this overflow here means we don't
420 need to worry about add_high overflowing. */
421 overflow = !!(num.high >> (PART_PRECISION - shift));
422 result.high = num.high << shift;
423 result.low = num.low << shift;
424 result.high |= num.low >> (PART_PRECISION - shift);
426 if (base == 10)
428 add_low = num.low << 1;
429 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
431 else
432 add_high = add_low = 0;
434 if (add_low + digit < add_low)
435 add_high++;
436 add_low += digit;
438 if (result.low + add_low < result.low)
439 add_high++;
440 if (result.high + add_high < result.high)
441 overflow = true;
443 result.low += add_low;
444 result.high += add_high;
446 /* The above code catches overflow of a cpp_num type. This catches
447 overflow of the (possibly shorter) target precision. */
448 num.low = result.low;
449 num.high = result.high;
450 result = num_trim (result, precision);
451 if (!num_eq (result, num))
452 overflow = true;
454 result.unsignedp = num.unsignedp;
455 result.overflow = overflow;
456 return result;
459 /* Handle meeting "defined" in a preprocessor expression. */
460 static cpp_num
461 parse_defined (pfile)
462 cpp_reader *pfile;
464 cpp_num result;
465 int paren = 0;
466 cpp_hashnode *node = 0;
467 const cpp_token *token;
468 cpp_context *initial_context = pfile->context;
470 /* Don't expand macros. */
471 pfile->state.prevent_expansion++;
473 token = cpp_get_token (pfile);
474 if (token->type == CPP_OPEN_PAREN)
476 paren = 1;
477 token = cpp_get_token (pfile);
480 if (token->type == CPP_NAME)
482 node = token->val.node;
483 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
485 cpp_error (pfile, DL_ERROR, "missing ')' after \"defined\"");
486 node = 0;
489 else
491 cpp_error (pfile, DL_ERROR,
492 "operator \"defined\" requires an identifier");
493 if (token->flags & NAMED_OP)
495 cpp_token op;
497 op.flags = 0;
498 op.type = token->type;
499 cpp_error (pfile, DL_ERROR,
500 "(\"%s\" is an alternative token for \"%s\" in C++)",
501 cpp_token_as_text (pfile, token),
502 cpp_token_as_text (pfile, &op));
506 if (node)
508 if (pfile->context != initial_context)
509 cpp_error (pfile, DL_WARNING,
510 "this use of \"defined\" may not be portable");
512 _cpp_mark_macro_used (node);
514 /* A possible controlling macro of the form #if !defined ().
515 _cpp_parse_expr checks there was no other junk on the line. */
516 pfile->mi_ind_cmacro = node;
519 pfile->state.prevent_expansion--;
521 result.unsignedp = false;
522 result.high = 0;
523 result.overflow = false;
524 result.low = node && node->type == NT_MACRO;
525 return result;
528 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
529 number or character constant, or the result of the "defined" or "#"
530 operators). */
531 static cpp_num
532 eval_token (pfile, token)
533 cpp_reader *pfile;
534 const cpp_token *token;
536 cpp_num result;
537 unsigned int temp;
538 int unsignedp = 0;
540 switch (token->type)
542 case CPP_NUMBER:
543 temp = cpp_classify_number (pfile, token);
544 switch (temp & CPP_N_CATEGORY)
546 case CPP_N_FLOATING:
547 cpp_error (pfile, DL_ERROR,
548 "floating constant in preprocessor expression");
549 break;
550 case CPP_N_INTEGER:
551 if (!(temp & CPP_N_IMAGINARY))
552 return cpp_interpret_integer (pfile, token, temp);
553 cpp_error (pfile, DL_ERROR,
554 "imaginary number in preprocessor expression");
555 break;
557 case CPP_N_INVALID:
558 /* Error already issued. */
559 break;
561 result.high = result.low = 0;
562 break;
564 case CPP_WCHAR:
565 case CPP_CHAR:
567 cppchar_t cc = cpp_interpret_charconst (pfile, token,
568 &temp, &unsignedp);
570 result.high = 0;
571 result.low = cc;
572 /* Sign-extend the result if necessary. */
573 if (!unsignedp && (cppchar_signed_t) cc < 0)
575 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
576 result.low |= ~(~(cpp_num_part) 0
577 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
578 result.high = ~(cpp_num_part) 0;
579 result = num_trim (result, CPP_OPTION (pfile, precision));
582 break;
584 case CPP_NAME:
585 if (token->val.node == pfile->spec_nodes.n_defined)
586 return parse_defined (pfile);
587 else if (CPP_OPTION (pfile, cplusplus)
588 && (token->val.node == pfile->spec_nodes.n_true
589 || token->val.node == pfile->spec_nodes.n_false))
591 result.high = 0;
592 result.low = (token->val.node == pfile->spec_nodes.n_true);
594 /* Warn about use of true or false in #if when pedantic
595 and stdbool.h has not been included. */
596 if (CPP_PEDANTIC (pfile)
597 && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
598 cpp_error (pfile, DL_PEDWARN,
599 "ISO C++ does not permit \"%s\" in #if",
600 NODE_NAME (token->val.node));
602 else
604 result.high = 0;
605 result.low = 0;
606 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
607 cpp_error (pfile, DL_WARNING, "\"%s\" is not defined",
608 NODE_NAME (token->val.node));
610 break;
612 default: /* CPP_HASH */
613 _cpp_test_assertion (pfile, &temp);
614 result.high = 0;
615 result.low = temp;
618 result.unsignedp = !!unsignedp;
619 result.overflow = false;
620 return result;
623 /* Operator precedence and flags table.
625 After an operator is returned from the lexer, if it has priority less
626 than the operator on the top of the stack, we reduce the stack by one
627 operator and repeat the test. Since equal priorities do not reduce,
628 this is naturally right-associative.
630 We handle left-associative operators by decrementing the priority of
631 just-lexed operators by one, but retaining the priority of operators
632 already on the stack.
634 The remaining cases are '(' and ')'. We handle '(' by skipping the
635 reduction phase completely. ')' is given lower priority than
636 everything else, including '(', effectively forcing a reduction of the
637 parenthesized expression. If there is a matching '(', the routine
638 reduce() exits immediately. If the normal exit route sees a ')', then
639 there cannot have been a matching '(' and an error message is output.
641 The parser assumes all shifted operators require a left operand unless
642 the flag NO_L_OPERAND is set. These semantics are automatic; any
643 extra semantics need to be handled with operator-specific code. */
645 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
646 operand changes because of integer promotions. */
647 #define NO_L_OPERAND (1 << 0)
648 #define LEFT_ASSOC (1 << 1)
649 #define CHECK_PROMOTION (1 << 2)
651 /* Operator to priority map. Must be in the same order as the first
652 N entries of enum cpp_ttype. */
653 static const struct operator
655 uchar prio;
656 uchar flags;
657 } optab[] =
659 /* EQ */ {0, 0}, /* Shouldn't happen. */
660 /* NOT */ {16, NO_L_OPERAND},
661 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
662 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
663 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
664 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
665 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
666 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
667 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
668 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
669 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
670 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
671 /* RSHIFT */ {13, LEFT_ASSOC},
672 /* LSHIFT */ {13, LEFT_ASSOC},
674 /* MIN */ {10, LEFT_ASSOC | CHECK_PROMOTION},
675 /* MAX */ {10, LEFT_ASSOC | CHECK_PROMOTION},
677 /* COMPL */ {16, NO_L_OPERAND},
678 /* AND_AND */ {6, LEFT_ASSOC},
679 /* OR_OR */ {5, LEFT_ASSOC},
680 /* QUERY */ {3, 0},
681 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
682 /* COMMA */ {2, LEFT_ASSOC},
683 /* OPEN_PAREN */ {1, NO_L_OPERAND},
684 /* CLOSE_PAREN */ {0, 0},
685 /* EOF */ {0, 0},
686 /* EQ_EQ */ {11, LEFT_ASSOC},
687 /* NOT_EQ */ {11, LEFT_ASSOC},
688 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
689 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
690 /* UPLUS */ {16, NO_L_OPERAND},
691 /* UMINUS */ {16, NO_L_OPERAND}
694 /* Parse and evaluate a C expression, reading from PFILE.
695 Returns the truth value of the expression.
697 The implementation is an operator precedence parser, i.e. a
698 bottom-up parser, using a stack for not-yet-reduced tokens.
700 The stack base is op_stack, and the current stack pointer is 'top'.
701 There is a stack element for each operator (only), and the most
702 recently pushed operator is 'top->op'. An operand (value) is
703 stored in the 'value' field of the stack element of the operator
704 that precedes it. */
705 bool
706 _cpp_parse_expr (pfile)
707 cpp_reader *pfile;
709 struct op *top = pfile->op_stack;
710 unsigned int lex_count;
711 bool saw_leading_not, want_value = true;
713 pfile->state.skip_eval = 0;
715 /* Set up detection of #if ! defined(). */
716 pfile->mi_ind_cmacro = 0;
717 saw_leading_not = false;
718 lex_count = 0;
720 /* Lowest priority operator prevents further reductions. */
721 top->op = CPP_EOF;
723 for (;;)
725 struct op op;
727 lex_count++;
728 op.token = cpp_get_token (pfile);
729 op.op = op.token->type;
731 switch (op.op)
733 /* These tokens convert into values. */
734 case CPP_NUMBER:
735 case CPP_CHAR:
736 case CPP_WCHAR:
737 case CPP_NAME:
738 case CPP_HASH:
739 if (!want_value)
740 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
741 cpp_token_as_text (pfile, op.token));
742 want_value = false;
743 top->value = eval_token (pfile, op.token);
744 continue;
746 case CPP_NOT:
747 saw_leading_not = lex_count == 1;
748 break;
749 case CPP_PLUS:
750 if (want_value)
751 op.op = CPP_UPLUS;
752 break;
753 case CPP_MINUS:
754 if (want_value)
755 op.op = CPP_UMINUS;
756 break;
757 case CPP_OTHER:
758 if (ISGRAPH (op.token->val.c))
759 SYNTAX_ERROR2 ("invalid character '%c' in #if", op.token->val.c);
760 else
761 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if",
762 op.token->val.c);
764 default:
765 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
766 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
767 cpp_token_as_text (pfile, op.token));
768 break;
771 /* Check we have a value or operator as appropriate. */
772 if (optab[op.op].flags & NO_L_OPERAND)
774 if (!want_value)
775 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
776 cpp_token_as_text (pfile, op.token));
778 else if (want_value)
780 /* Ordering here is subtle and intended to favor the
781 missing parenthesis diagnostics over alternatives. */
782 if (op.op == CPP_CLOSE_PAREN)
784 if (top->op == CPP_OPEN_PAREN)
785 SYNTAX_ERROR ("void expression between '(' and ')'");
787 else if (top->op == CPP_EOF)
788 SYNTAX_ERROR ("#if with no expression");
789 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
790 SYNTAX_ERROR2 ("operator '%s' has no right operand",
791 cpp_token_as_text (pfile, top->token));
794 top = reduce (pfile, top, op.op);
795 if (!top)
796 goto syntax_error;
798 if (op.op == CPP_EOF)
799 break;
801 switch (op.op)
803 case CPP_CLOSE_PAREN:
804 continue;
805 case CPP_OR_OR:
806 if (!num_zerop (top->value))
807 pfile->state.skip_eval++;
808 break;
809 case CPP_AND_AND:
810 case CPP_QUERY:
811 if (num_zerop (top->value))
812 pfile->state.skip_eval++;
813 break;
814 case CPP_COLON:
815 if (top->op != CPP_QUERY)
816 SYNTAX_ERROR (" ':' without preceding '?'");
817 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
818 pfile->state.skip_eval++;
819 else
820 pfile->state.skip_eval--;
821 default:
822 break;
825 want_value = true;
827 /* Check for and handle stack overflow. */
828 if (++top == pfile->op_limit)
829 top = _cpp_expand_op_stack (pfile);
831 top->op = op.op;
832 top->token = op.token;
835 /* The controlling macro expression is only valid if we called lex 3
836 times: <!> <defined expression> and <EOF>. push_conditional ()
837 checks that we are at top-of-file. */
838 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
839 pfile->mi_ind_cmacro = 0;
841 if (top != pfile->op_stack)
843 cpp_error (pfile, DL_ICE, "unbalanced stack in #if");
844 syntax_error:
845 return false; /* Return false on syntax error. */
848 return !num_zerop (top->value);
851 /* Reduce the operator / value stack if possible, in preparation for
852 pushing operator OP. Returns NULL on error, otherwise the top of
853 the stack. */
854 static struct op *
855 reduce (pfile, top, op)
856 cpp_reader *pfile;
857 struct op *top;
858 enum cpp_ttype op;
860 unsigned int prio;
862 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
864 bad_op:
865 cpp_error (pfile, DL_ICE, "impossible operator '%u'", top->op);
866 return 0;
869 if (op == CPP_OPEN_PAREN)
870 return top;
872 /* Decrement the priority of left-associative operators to force a
873 reduction with operators of otherwise equal priority. */
874 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
875 while (prio < optab[top->op].prio)
877 if (CPP_OPTION (pfile, warn_num_sign_change)
878 && optab[top->op].flags & CHECK_PROMOTION)
879 check_promotion (pfile, top);
881 switch (top->op)
883 case CPP_UPLUS:
884 case CPP_UMINUS:
885 case CPP_NOT:
886 case CPP_COMPL:
887 top[-1].value = num_unary_op (pfile, top->value, top->op);
888 break;
890 case CPP_PLUS:
891 case CPP_MINUS:
892 case CPP_RSHIFT:
893 case CPP_LSHIFT:
894 case CPP_MIN:
895 case CPP_MAX:
896 case CPP_COMMA:
897 top[-1].value = num_binary_op (pfile, top[-1].value,
898 top->value, top->op);
899 break;
901 case CPP_GREATER:
902 case CPP_LESS:
903 case CPP_GREATER_EQ:
904 case CPP_LESS_EQ:
905 top[-1].value
906 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
907 break;
909 case CPP_EQ_EQ:
910 case CPP_NOT_EQ:
911 top[-1].value
912 = num_equality_op (pfile, top[-1].value, top->value, top->op);
913 break;
915 case CPP_AND:
916 case CPP_OR:
917 case CPP_XOR:
918 top[-1].value
919 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
920 break;
922 case CPP_MULT:
923 top[-1].value = num_mul (pfile, top[-1].value, top->value);
924 break;
926 case CPP_DIV:
927 case CPP_MOD:
928 top[-1].value = num_div_op (pfile, top[-1].value,
929 top->value, top->op);
930 break;
932 case CPP_OR_OR:
933 top--;
934 if (!num_zerop (top->value))
935 pfile->state.skip_eval--;
936 top->value.low = (!num_zerop (top->value)
937 || !num_zerop (top[1].value));
938 top->value.high = 0;
939 top->value.unsignedp = false;
940 top->value.overflow = false;
941 continue;
943 case CPP_AND_AND:
944 top--;
945 if (num_zerop (top->value))
946 pfile->state.skip_eval--;
947 top->value.low = (!num_zerop (top->value)
948 && !num_zerop (top[1].value));
949 top->value.high = 0;
950 top->value.unsignedp = false;
951 top->value.overflow = false;
952 continue;
954 case CPP_OPEN_PAREN:
955 if (op != CPP_CLOSE_PAREN)
957 cpp_error (pfile, DL_ERROR, "missing ')' in expression");
958 return 0;
960 top--;
961 top->value = top[1].value;
962 return top;
964 case CPP_COLON:
965 top -= 2;
966 if (!num_zerop (top->value))
968 pfile->state.skip_eval--;
969 top->value = top[1].value;
971 else
972 top->value = top[2].value;
973 top->value.unsignedp = (top[1].value.unsignedp
974 || top[2].value.unsignedp);
975 continue;
977 case CPP_QUERY:
978 cpp_error (pfile, DL_ERROR, "'?' without following ':'");
979 return 0;
981 default:
982 goto bad_op;
985 top--;
986 if (top->value.overflow && !pfile->state.skip_eval)
987 cpp_error (pfile, DL_PEDWARN,
988 "integer overflow in preprocessor expression");
991 if (op == CPP_CLOSE_PAREN)
993 cpp_error (pfile, DL_ERROR, "missing '(' in expression");
994 return 0;
997 return top;
1000 /* Returns the position of the old top of stack after expansion. */
1001 struct op *
1002 _cpp_expand_op_stack (pfile)
1003 cpp_reader *pfile;
1005 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1006 size_t new_size = old_size * 2 + 20;
1008 pfile->op_stack = (struct op *) xrealloc (pfile->op_stack,
1009 new_size * sizeof (struct op));
1010 pfile->op_limit = pfile->op_stack + new_size;
1012 return pfile->op_stack + old_size;
1015 /* Emits a warning if the effective sign of either operand of OP
1016 changes because of integer promotions. */
1017 static void
1018 check_promotion (pfile, op)
1019 cpp_reader *pfile;
1020 const struct op *op;
1022 if (op->value.unsignedp == op[-1].value.unsignedp)
1023 return;
1025 if (op->value.unsignedp)
1027 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1028 cpp_error (pfile, DL_WARNING,
1029 "the left operand of \"%s\" changes sign when promoted",
1030 cpp_token_as_text (pfile, op->token));
1032 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1033 cpp_error (pfile, DL_WARNING,
1034 "the right operand of \"%s\" changes sign when promoted",
1035 cpp_token_as_text (pfile, op->token));
1038 /* Clears the unused high order bits of the number pointed to by PNUM. */
1039 static cpp_num
1040 num_trim (num, precision)
1041 cpp_num num;
1042 size_t precision;
1044 if (precision > PART_PRECISION)
1046 precision -= PART_PRECISION;
1047 if (precision < PART_PRECISION)
1048 num.high &= ((cpp_num_part) 1 << precision) - 1;
1050 else
1052 if (precision < PART_PRECISION)
1053 num.low &= ((cpp_num_part) 1 << precision) - 1;
1054 num.high = 0;
1057 return num;
1060 /* True iff A (presumed signed) >= 0. */
1061 static bool
1062 num_positive (num, precision)
1063 cpp_num num;
1064 size_t precision;
1066 if (precision > PART_PRECISION)
1068 precision -= PART_PRECISION;
1069 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1072 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1075 /* Sign extend a number, with PRECISION significant bits and all
1076 others assumed clear, to fill out a cpp_num structure. */
1077 cpp_num
1078 cpp_num_sign_extend (num, precision)
1079 cpp_num num;
1080 size_t precision;
1082 if (!num.unsignedp)
1084 if (precision > PART_PRECISION)
1086 precision -= PART_PRECISION;
1087 if (precision < PART_PRECISION
1088 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1089 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1091 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1093 if (precision < PART_PRECISION)
1094 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1095 num.high = ~(cpp_num_part) 0;
1099 return num;
1102 /* Returns the negative of NUM. */
1103 static cpp_num
1104 num_negate (num, precision)
1105 cpp_num num;
1106 size_t precision;
1108 cpp_num copy;
1110 copy = num;
1111 num.high = ~num.high;
1112 num.low = ~num.low;
1113 if (++num.low == 0)
1114 num.high++;
1115 num = num_trim (num, precision);
1116 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1118 return num;
1121 /* Returns true if A >= B. */
1122 static bool
1123 num_greater_eq (pa, pb, precision)
1124 cpp_num pa, pb;
1125 size_t precision;
1127 bool unsignedp;
1129 unsignedp = pa.unsignedp || pb.unsignedp;
1131 if (!unsignedp)
1133 /* Both numbers have signed type. If they are of different
1134 sign, the answer is the sign of A. */
1135 unsignedp = num_positive (pa, precision);
1137 if (unsignedp != num_positive (pb, precision))
1138 return unsignedp;
1140 /* Otherwise we can do an unsigned comparison. */
1143 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1146 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1147 static cpp_num
1148 num_bitwise_op (pfile, lhs, rhs, op)
1149 cpp_reader *pfile ATTRIBUTE_UNUSED;
1150 cpp_num lhs, rhs;
1151 enum cpp_ttype op;
1153 lhs.overflow = false;
1154 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1156 /* As excess precision is zeroed, there is no need to num_trim () as
1157 these operations cannot introduce a set bit there. */
1158 if (op == CPP_AND)
1160 lhs.low &= rhs.low;
1161 lhs.high &= rhs.high;
1163 else if (op == CPP_OR)
1165 lhs.low |= rhs.low;
1166 lhs.high |= rhs.high;
1168 else
1170 lhs.low ^= rhs.low;
1171 lhs.high ^= rhs.high;
1174 return lhs;
1177 /* Returns LHS OP RHS, where OP is an inequality. */
1178 static cpp_num
1179 num_inequality_op (pfile, lhs, rhs, op)
1180 cpp_reader *pfile;
1181 cpp_num lhs, rhs;
1182 enum cpp_ttype op;
1184 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1186 if (op == CPP_GREATER_EQ)
1187 lhs.low = gte;
1188 else if (op == CPP_LESS)
1189 lhs.low = !gte;
1190 else if (op == CPP_GREATER)
1191 lhs.low = gte && !num_eq (lhs, rhs);
1192 else /* CPP_LESS_EQ. */
1193 lhs.low = !gte || num_eq (lhs, rhs);
1195 lhs.high = 0;
1196 lhs.overflow = false;
1197 lhs.unsignedp = false;
1198 return lhs;
1201 /* Returns LHS OP RHS, where OP is == or !=. */
1202 static cpp_num
1203 num_equality_op (pfile, lhs, rhs, op)
1204 cpp_reader *pfile ATTRIBUTE_UNUSED;
1205 cpp_num lhs, rhs;
1206 enum cpp_ttype op;
1208 /* Work around a 3.0.4 bug; see PR 6950. */
1209 bool eq = num_eq (lhs, rhs);
1210 if (op == CPP_NOT_EQ)
1211 eq = !eq;
1212 lhs.low = eq;
1213 lhs.high = 0;
1214 lhs.overflow = false;
1215 lhs.unsignedp = false;
1216 return lhs;
1219 /* Shift NUM, of width PRECISION, right by N bits. */
1220 static cpp_num
1221 num_rshift (num, precision, n)
1222 cpp_num num;
1223 size_t precision, n;
1225 cpp_num_part sign_mask;
1227 if (num.unsignedp || num_positive (num, precision))
1228 sign_mask = 0;
1229 else
1230 sign_mask = ~(cpp_num_part) 0;
1232 if (n >= precision)
1233 num.high = num.low = sign_mask;
1234 else
1236 /* Sign-extend. */
1237 if (precision < PART_PRECISION)
1238 num.high = sign_mask, num.low |= sign_mask << precision;
1239 else if (precision < 2 * PART_PRECISION)
1240 num.high |= sign_mask << (precision - PART_PRECISION);
1242 if (n >= PART_PRECISION)
1244 n -= PART_PRECISION;
1245 num.low = num.high;
1246 num.high = sign_mask;
1249 if (n)
1251 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1252 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1256 num = num_trim (num, precision);
1257 num.overflow = false;
1258 return num;
1261 /* Shift NUM, of width PRECISION, left by N bits. */
1262 static cpp_num
1263 num_lshift (num, precision, n)
1264 cpp_num num;
1265 size_t precision, n;
1267 if (n >= precision)
1269 num.overflow = !num.unsignedp && !num_zerop (num);
1270 num.high = num.low = 0;
1272 else
1274 cpp_num orig, maybe_orig;
1275 size_t m = n;
1277 orig = num;
1278 if (m >= PART_PRECISION)
1280 m -= PART_PRECISION;
1281 num.high = num.low;
1282 num.low = 0;
1284 if (m)
1286 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1287 num.low <<= m;
1289 num = num_trim (num, precision);
1291 if (num.unsignedp)
1292 num.overflow = false;
1293 else
1295 maybe_orig = num_rshift (num, precision, n);
1296 num.overflow = !num_eq (orig, maybe_orig);
1300 return num;
1303 /* The four unary operators: +, -, ! and ~. */
1304 static cpp_num
1305 num_unary_op (pfile, num, op)
1306 cpp_reader *pfile;
1307 cpp_num num;
1308 enum cpp_ttype op;
1310 switch (op)
1312 case CPP_UPLUS:
1313 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1314 cpp_error (pfile, DL_WARNING,
1315 "traditional C rejects the unary plus operator");
1316 num.overflow = false;
1317 break;
1319 case CPP_UMINUS:
1320 num = num_negate (num, CPP_OPTION (pfile, precision));
1321 break;
1323 case CPP_COMPL:
1324 num.high = ~num.high;
1325 num.low = ~num.low;
1326 num = num_trim (num, CPP_OPTION (pfile, precision));
1327 num.overflow = false;
1328 break;
1330 default: /* case CPP_NOT: */
1331 num.low = num_zerop (num);
1332 num.high = 0;
1333 num.overflow = false;
1334 num.unsignedp = false;
1335 break;
1338 return num;
1341 /* The various binary operators. */
1342 static cpp_num
1343 num_binary_op (pfile, lhs, rhs, op)
1344 cpp_reader *pfile;
1345 cpp_num lhs, rhs;
1346 enum cpp_ttype op;
1348 cpp_num result;
1349 size_t precision = CPP_OPTION (pfile, precision);
1350 bool gte;
1351 size_t n;
1353 switch (op)
1355 /* Shifts. */
1356 case CPP_LSHIFT:
1357 case CPP_RSHIFT:
1358 if (!rhs.unsignedp && !num_positive (rhs, precision))
1360 /* A negative shift is a positive shift the other way. */
1361 if (op == CPP_LSHIFT)
1362 op = CPP_RSHIFT;
1363 else
1364 op = CPP_LSHIFT;
1365 rhs = num_negate (rhs, precision);
1367 if (rhs.high)
1368 n = ~0; /* Maximal. */
1369 else
1370 n = rhs.low;
1371 if (op == CPP_LSHIFT)
1372 lhs = num_lshift (lhs, precision, n);
1373 else
1374 lhs = num_rshift (lhs, precision, n);
1375 break;
1377 /* Min / Max. */
1378 case CPP_MIN:
1379 case CPP_MAX:
1381 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1383 gte = num_greater_eq (lhs, rhs, precision);
1384 if (op == CPP_MIN)
1385 gte = !gte;
1386 if (!gte)
1387 lhs = rhs;
1388 lhs.unsignedp = unsignedp;
1390 break;
1392 /* Arithmetic. */
1393 case CPP_MINUS:
1394 rhs = num_negate (rhs, precision);
1395 case CPP_PLUS:
1396 result.low = lhs.low + rhs.low;
1397 result.high = lhs.high + rhs.high;
1398 if (result.low < lhs.low)
1399 result.high++;
1401 result = num_trim (result, precision);
1402 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1403 if (result.unsignedp)
1404 result.overflow = false;
1405 else
1407 bool lhsp = num_positive (lhs, precision);
1408 result.overflow = (lhsp == num_positive (rhs, precision)
1409 && lhsp != num_positive (result, precision));
1411 return result;
1413 /* Comma. */
1414 default: /* case CPP_COMMA: */
1415 if (CPP_PEDANTIC (pfile) && !pfile->state.skip_eval)
1416 cpp_error (pfile, DL_PEDWARN,
1417 "comma operator in operand of #if");
1418 lhs = rhs;
1419 break;
1422 return lhs;
1425 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1426 cannot overflow. */
1427 static cpp_num
1428 num_part_mul (lhs, rhs)
1429 cpp_num_part lhs, rhs;
1431 cpp_num result;
1432 cpp_num_part middle[2], temp;
1434 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1435 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1437 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1438 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1440 temp = result.low;
1441 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1442 if (result.low < temp)
1443 result.high++;
1445 temp = result.low;
1446 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1447 if (result.low < temp)
1448 result.high++;
1450 result.high += HIGH_PART (middle[0]);
1451 result.high += HIGH_PART (middle[1]);
1452 result.unsignedp = 1;
1454 return result;
1457 /* Multiply two preprocessing numbers. */
1458 static cpp_num
1459 num_mul (pfile, lhs, rhs)
1460 cpp_reader *pfile;
1461 cpp_num lhs, 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 (pfile, lhs, rhs, op)
1512 cpp_reader *pfile;
1513 cpp_num lhs, rhs;
1514 enum cpp_ttype op;
1516 cpp_num result, sub;
1517 cpp_num_part mask;
1518 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1519 bool negate = false, lhs_neg = false;
1520 size_t i, precision = CPP_OPTION (pfile, precision);
1522 /* Prepare for unsigned division. */
1523 if (!unsignedp)
1525 if (!num_positive (lhs, precision))
1526 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1527 if (!num_positive (rhs, precision))
1528 negate = !negate, rhs = num_negate (rhs, precision);
1531 /* Find the high bit. */
1532 if (rhs.high)
1534 i = precision - 1;
1535 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1536 for (; ; i--, mask >>= 1)
1537 if (rhs.high & mask)
1538 break;
1540 else if (rhs.low)
1542 if (precision > PART_PRECISION)
1543 i = precision - PART_PRECISION - 1;
1544 else
1545 i = precision - 1;
1546 mask = (cpp_num_part) 1 << i;
1547 for (; ; i--, mask >>= 1)
1548 if (rhs.low & mask)
1549 break;
1551 else
1553 if (!pfile->state.skip_eval)
1554 cpp_error (pfile, DL_ERROR, "division by zero in #if");
1555 return lhs;
1558 /* First nonzero bit of RHS is bit I. Do naive division by
1559 shifting the RHS fully left, and subtracting from LHS if LHS is
1560 at least as big, and then repeating but with one less shift.
1561 This is not very efficient, but is easy to understand. */
1563 rhs.unsignedp = true;
1564 lhs.unsignedp = true;
1565 i = precision - i - 1;
1566 sub = num_lshift (rhs, precision, i);
1568 result.high = result.low = 0;
1569 for (;;)
1571 if (num_greater_eq (lhs, sub, precision))
1573 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1574 if (i >= PART_PRECISION)
1575 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1576 else
1577 result.low |= (cpp_num_part) 1 << i;
1579 if (i-- == 0)
1580 break;
1581 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1582 sub.high >>= 1;
1585 /* We divide so that the remainder has the sign of the LHS. */
1586 if (op == CPP_DIV)
1588 result.unsignedp = unsignedp;
1589 if (unsignedp)
1590 result.overflow = false;
1591 else
1593 if (negate)
1594 result = num_negate (result, precision);
1595 result.overflow = num_positive (result, precision) ^ !negate;
1598 return result;
1601 /* CPP_MOD. */
1602 lhs.unsignedp = unsignedp;
1603 lhs.overflow = false;
1604 if (lhs_neg)
1605 lhs = num_negate (lhs, precision);
1607 return lhs;