OSDN Git Service

* cppexp.c (cpp_interpret_integer, append_digit, parse_defined,
[pf3gnuchains/gcc-fork.git] / gcc / cppexp.c
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.
5
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.
10
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.
15
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.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "cpplib.h"
24 #include "cpphash.h"
25
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))
30
31 struct op
32 {
33   cpp_num value;                     /* The value logically "right" of op.  */
34   enum cpp_ttype op;
35 };
36
37 /* Some simple utility routines on double integers.  */
38 #define num_zerop(num) ((num.low | num.high) == 0)
39 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
40 static bool num_positive PARAMS ((cpp_num, size_t));
41 static bool num_greater_eq PARAMS ((cpp_num, cpp_num, size_t));
42 static cpp_num num_trim PARAMS ((cpp_num, size_t));
43 static cpp_num num_part_mul PARAMS ((cpp_num_part, cpp_num_part));
44
45 static cpp_num num_unary_op PARAMS ((cpp_reader *, cpp_num, enum cpp_ttype));
46 static cpp_num num_binary_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
47                                       enum cpp_ttype));
48 static cpp_num num_negate PARAMS ((cpp_num, size_t));
49 static cpp_num num_bitwise_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
50                                        enum cpp_ttype));
51 static cpp_num num_inequality_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
52                                           enum cpp_ttype));
53 static cpp_num num_equality_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
54                                         enum cpp_ttype));
55 static cpp_num num_mul PARAMS ((cpp_reader *, cpp_num, cpp_num,
56                                 enum cpp_ttype));
57 static cpp_num num_div_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
58                                    enum cpp_ttype));
59 static cpp_num num_lshift PARAMS ((cpp_num, size_t, size_t));
60 static cpp_num num_rshift PARAMS ((cpp_num, size_t, size_t));
61
62 static cpp_num append_digit PARAMS ((cpp_num, int, int, size_t));
63 static cpp_num parse_defined PARAMS ((cpp_reader *));
64 static cpp_num eval_token PARAMS ((cpp_reader *, const cpp_token *));
65 static struct op *reduce PARAMS ((cpp_reader *, struct op *, enum cpp_ttype));
66 static unsigned int interpret_float_suffix PARAMS ((const uchar *, size_t));
67 static unsigned int interpret_int_suffix PARAMS ((const uchar *, size_t));
68
69 /* Token type abuse to create unary plus and minus operators.  */
70 #define CPP_UPLUS (CPP_LAST_CPP_OP + 1)
71 #define CPP_UMINUS (CPP_LAST_CPP_OP + 2)
72
73 /* With -O2, gcc appears to produce nice code, moving the error
74    message load and subsequent jump completely out of the main path.  */
75 #define SYNTAX_ERROR(msgid) \
76   do { cpp_error (pfile, DL_ERROR, msgid); goto syntax_error; } while(0)
77 #define SYNTAX_ERROR2(msgid, arg) \
78   do { cpp_error (pfile, DL_ERROR, msgid, arg); goto syntax_error; } while(0)
79
80 /* Subroutine of cpp_classify_number.  S points to a float suffix of
81    length LEN, possibly zero.  Returns 0 for an invalid suffix, or a
82    flag vector describing the suffix.  */
83 static unsigned int
84 interpret_float_suffix (s, len)
85      const uchar *s;
86      size_t len;
87 {
88   size_t f = 0, l = 0, i = 0;
89
90   while (len--)
91     switch (s[len])
92       {
93       case 'f': case 'F': f++; break;
94       case 'l': case 'L': l++; break;
95       case 'i': case 'I':
96       case 'j': case 'J': i++; break;
97       default:
98         return 0;
99       }
100
101   if (f + l > 1 || i > 1)
102     return 0;
103
104   return ((i ? CPP_N_IMAGINARY : 0)
105           | (f ? CPP_N_SMALL :
106              l ? CPP_N_LARGE : CPP_N_MEDIUM));
107 }
108
109 /* Subroutine of cpp_classify_number.  S points to an integer suffix
110    of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
111    flag vector describing the suffix.  */
112 static unsigned int
113 interpret_int_suffix (s, len)
114      const uchar *s;
115      size_t len;
116 {
117   size_t u, l, i;
118
119   u = l = i = 0;
120
121   while (len--)
122     switch (s[len])
123       {
124       case 'u': case 'U':       u++; break;
125       case 'i': case 'I':
126       case 'j': case 'J':       i++; break;
127       case 'l': case 'L':       l++;
128         /* If there are two Ls, they must be adjacent and the same case.  */
129         if (l == 2 && s[len] != s[len + 1])
130           return 0;
131         break;
132       default:
133         return 0;
134       }
135
136   if (l > 2 || u > 1 || i > 1)
137     return 0;
138
139   return ((i ? CPP_N_IMAGINARY : 0)
140           | (u ? CPP_N_UNSIGNED : 0)
141           | ((l == 0) ? CPP_N_SMALL
142              : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
143 }
144
145 /* Categorize numeric constants according to their field (integer,
146    floating point, or invalid), radix (decimal, octal, hexadecimal),
147    and type suffixes.  */
148 unsigned int
149 cpp_classify_number (pfile, token)
150      cpp_reader *pfile;
151      const cpp_token *token;
152 {
153   const uchar *str = token->val.str.text;
154   const uchar *limit;
155   unsigned int max_digit, result, radix;
156   enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
157
158   /* If the lexer has done its job, length one can only be a single
159      digit.  Fast-path this very common case.  */
160   if (token->val.str.len == 1)
161     return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
162
163   limit = str + token->val.str.len;
164   float_flag = NOT_FLOAT;
165   max_digit = 0;
166   radix = 10;
167
168   /* First, interpret the radix.  */
169   if (*str == '0')
170     {
171       radix = 8;
172       str++;
173
174       /* Require at least one hex digit to classify it as hex.  */
175       if ((*str == 'x' || *str == 'X') && ISXDIGIT (str[1]))
176         {
177           radix = 16;
178           str++;
179         }
180     }
181
182   /* Now scan for a well-formed integer or float.  */
183   for (;;)
184     {
185       unsigned int c = *str++;
186
187       if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
188         {
189           c = hex_value (c);
190           if (c > max_digit)
191             max_digit = c;
192         }
193       else if (c == '.')
194         {
195           if (float_flag == NOT_FLOAT)
196             float_flag = AFTER_POINT;
197           else
198             SYNTAX_ERROR ("too many decimal points in number");
199         }
200       else if ((radix <= 10 && (c == 'e' || c == 'E'))
201                || (radix == 16 && (c == 'p' || c == 'P')))
202         {
203           float_flag = AFTER_EXPON;
204           break;
205         }
206       else
207         {
208           /* Start of suffix.  */
209           str--;
210           break;
211         }
212     }
213
214   if (float_flag != NOT_FLOAT && radix == 8)
215     radix = 10;
216
217   if (max_digit >= radix)
218     SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
219
220   if (float_flag != NOT_FLOAT)
221     {
222       if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
223         cpp_error (pfile, DL_PEDWARN,
224                    "use of C99 hexadecimal floating constant");
225
226       if (float_flag == AFTER_EXPON)
227         {
228           if (*str == '+' || *str == '-')
229             str++;
230
231           /* Exponent is decimal, even if string is a hex float.  */
232           if (!ISDIGIT (*str))
233             SYNTAX_ERROR ("exponent has no digits");
234
235           do
236             str++;
237           while (ISDIGIT (*str));
238         }
239       else if (radix == 16)
240         SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
241
242       result = interpret_float_suffix (str, limit - str);
243       if (result == 0)
244         {
245           cpp_error (pfile, DL_ERROR,
246                      "invalid suffix \"%.*s\" on floating constant",
247                      (int) (limit - str), str);
248           return CPP_N_INVALID;
249         }
250
251       /* Traditional C didn't accept any floating suffixes.  */
252       if (limit != str
253           && CPP_WTRADITIONAL (pfile)
254           && ! cpp_sys_macro_p (pfile))
255         cpp_error (pfile, DL_WARNING,
256                    "traditional C rejects the \"%.*s\" suffix",
257                    (int) (limit - str), str);
258
259       result |= CPP_N_FLOATING;
260     }
261   else
262     {
263       result = interpret_int_suffix (str, limit - str);
264       if (result == 0)
265         {
266           cpp_error (pfile, DL_ERROR,
267                      "invalid suffix \"%.*s\" on integer constant",
268                      (int) (limit - str), str);
269           return CPP_N_INVALID;
270         }
271
272       /* Traditional C only accepted the 'L' suffix.  */
273       if (result != CPP_N_SMALL && result != CPP_N_MEDIUM
274           && CPP_WTRADITIONAL (pfile)
275           && ! cpp_sys_macro_p (pfile))
276         cpp_error (pfile, DL_WARNING,
277                    "traditional C rejects the \"%.*s\" suffix",
278                    (int) (limit - str), str);
279
280       if ((result & CPP_N_WIDTH) == CPP_N_LARGE
281           && ! CPP_OPTION (pfile, c99)
282           && CPP_OPTION (pfile, warn_long_long))
283         cpp_error (pfile, DL_PEDWARN, "use of C99 long long integer constant");
284
285       result |= CPP_N_INTEGER;
286     }
287
288   if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
289     cpp_error (pfile, DL_PEDWARN, "imaginary constants are a GCC extension");
290
291   if (radix == 10)
292     result |= CPP_N_DECIMAL;
293   else if (radix == 16)
294     result |= CPP_N_HEX;
295   else
296     result |= CPP_N_OCTAL;
297
298   return result;
299
300  syntax_error:
301   return CPP_N_INVALID;
302 }
303
304 /* cpp_interpret_integer converts an integer constant into a cpp_num,
305    of precision options->precision.
306
307    We do not provide any interface for decimal->float conversion,
308    because the preprocessor doesn't need it and the floating point
309    handling in GCC proper is too ugly to speak of.  */
310 cpp_num
311 cpp_interpret_integer (pfile, token, type)
312      cpp_reader *pfile;
313      const cpp_token *token;
314      unsigned int type;
315 {
316   const uchar *p, *end;
317   cpp_num result;
318
319   result.low = 0;
320   result.high = 0;
321   result.unsignedp = !!(type & CPP_N_UNSIGNED);
322   result.overflow = false;
323
324   p = token->val.str.text;
325   end = p + token->val.str.len;
326
327   /* Common case of a single digit.  */
328   if (token->val.str.len == 1)
329     result.low = p[0] - '0';
330   else
331     {
332       cpp_num_part max;
333       size_t precision = CPP_OPTION (pfile, precision);
334       unsigned int base = 10, c = 0;
335       bool overflow = false;
336
337       if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
338         {
339           base = 8;
340           p++;
341         }
342       else if ((type & CPP_N_RADIX) == CPP_N_HEX)
343         {
344           base = 16;
345           p += 2;
346         }
347
348       /* We can add a digit to numbers strictly less than this without
349          needing the precision and slowness of double integers.  */
350       max = ~(cpp_num_part) 0;
351       if (precision < PART_PRECISION)
352         max >>= PART_PRECISION - precision;
353       max = (max - base + 1) / base + 1;
354
355       for (; p < end; p++)
356         {
357           c = *p;
358
359           if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
360             c = hex_value (c);
361           else
362             break;
363
364           /* Strict inequality for when max is set to zero.  */
365           if (result.low < max)
366             result.low = result.low * base + c;
367           else
368             {
369               result = append_digit (result, c, base, precision);
370               overflow |= result.overflow;
371               max = 0;
372             }
373         }
374
375       if (overflow)
376         cpp_error (pfile, DL_PEDWARN,
377                    "integer constant is too large for its type");
378       /* If too big to be signed, consider it unsigned.  Only warn for
379          decimal numbers.  Traditional numbers were always signed (but
380          we still honour an explicit U suffix); but we only have
381          traditional semantics in directives.  */
382       else if (!result.unsignedp
383                && !(CPP_OPTION (pfile, traditional)
384                     && pfile->state.in_directive)
385                && !num_positive (result, precision))
386         {
387           if (base == 10)
388             cpp_error (pfile, DL_WARNING,
389                        "integer constant is so large that it is unsigned");
390           result.unsignedp = true;
391         }
392     }
393
394   return result;
395 }
396
397 /* Append DIGIT to NUM, a number of PRECISION bits being read in base
398    BASE.  */
399 static cpp_num
400 append_digit (num, digit, base, precision)
401      cpp_num num;
402      int digit, base;
403      size_t precision;
404 {
405   cpp_num result;
406   unsigned int shift = 3 + (base == 16);
407   bool overflow;
408   cpp_num_part add_high, add_low;
409
410   /* Multiply by 8 or 16.  Catching this overflow here means we don't
411      need to worry about add_high overflowing.  */
412   overflow = !!(num.high >> (PART_PRECISION - shift));
413   result.high = num.high << shift;
414   result.low = num.low << shift;
415   result.high |= num.low >> (PART_PRECISION - shift);
416
417   if (base == 10)
418     {
419       add_low = num.low << 1;
420       add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
421     }
422   else
423     add_high = add_low = 0;
424
425   if (add_low + digit < add_low)
426     add_high++;
427   add_low += digit;
428     
429   if (result.low + add_low < result.low)
430     add_high++;
431   if (result.high + add_high < result.high)
432     overflow = true;
433
434   result.low += add_low;
435   result.high += add_high;
436
437   /* The above code catches overflow of a cpp_num type.  This catches
438      overflow of the (possibly shorter) target precision.  */
439   num.low = result.low;
440   num.high = result.high;
441   result = num_trim (result, precision);
442   if (!num_eq (result, num))
443     overflow = true;
444
445   result.unsignedp = num.unsignedp;
446   result.overflow = overflow;
447   return result;
448 }
449
450 /* Handle meeting "defined" in a preprocessor expression.  */
451 static cpp_num
452 parse_defined (pfile)
453      cpp_reader *pfile;
454 {
455   cpp_num result;
456   int paren = 0;
457   cpp_hashnode *node = 0;
458   const cpp_token *token;
459   cpp_context *initial_context = pfile->context;
460
461   /* Don't expand macros.  */
462   pfile->state.prevent_expansion++;
463
464   token = cpp_get_token (pfile);
465   if (token->type == CPP_OPEN_PAREN)
466     {
467       paren = 1;
468       token = cpp_get_token (pfile);
469     }
470
471   if (token->type == CPP_NAME)
472     {
473       node = token->val.node;
474       if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
475         {
476           cpp_error (pfile, DL_ERROR, "missing ')' after \"defined\"");
477           node = 0;
478         }
479     }
480   else
481     {
482       cpp_error (pfile, DL_ERROR,
483                  "operator \"defined\" requires an identifier");
484       if (token->flags & NAMED_OP)
485         {
486           cpp_token op;
487
488           op.flags = 0;
489           op.type = token->type;
490           cpp_error (pfile, DL_ERROR,
491                      "(\"%s\" is an alternative token for \"%s\" in C++)",
492                      cpp_token_as_text (pfile, token),
493                      cpp_token_as_text (pfile, &op));
494         }
495     }
496
497   if (node)
498     {
499       if (pfile->context != initial_context)
500         cpp_error (pfile, DL_WARNING,
501                    "this use of \"defined\" may not be portable");
502
503       /* A possible controlling macro of the form #if !defined ().
504          _cpp_parse_expr checks there was no other junk on the line.  */
505       pfile->mi_ind_cmacro = node;
506     }
507
508   pfile->state.prevent_expansion--;
509
510   result.unsignedp = false;
511   result.high = 0;
512   result.overflow = false;
513   result.low = node && node->type == NT_MACRO;
514   return result;
515 }
516
517 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
518    number or character constant, or the result of the "defined" or "#"
519    operators).  */
520 static cpp_num
521 eval_token (pfile, token)
522      cpp_reader *pfile;
523      const cpp_token *token;
524 {
525   cpp_num result;
526   unsigned int temp;
527   int unsignedp = 0;
528
529   switch (token->type)
530     {
531     case CPP_NUMBER:
532       temp = cpp_classify_number (pfile, token);
533       switch (temp & CPP_N_CATEGORY)
534         {
535         case CPP_N_FLOATING:
536           cpp_error (pfile, DL_ERROR,
537                      "floating constant in preprocessor expression");
538           break;
539         case CPP_N_INTEGER:
540           if (!(temp & CPP_N_IMAGINARY))
541             return cpp_interpret_integer (pfile, token, temp);
542           cpp_error (pfile, DL_ERROR,
543                      "imaginary number in preprocessor expression");
544           break;
545
546         case CPP_N_INVALID:
547           /* Error already issued.  */
548           break;
549         }
550       result.high = result.low = 0;
551       break;
552
553     case CPP_WCHAR:
554     case CPP_CHAR:
555       {
556         cppchar_t cc = cpp_interpret_charconst (pfile, token,
557                                                 &temp, &unsignedp);
558
559         result.high = 0;
560         result.low = cc;
561         /* Sign-extend the result if necessary.  */
562         if (!unsignedp && (cppchar_signed_t) cc < 0)
563           {
564             if (PART_PRECISION > BITS_PER_CPPCHAR_T)
565               result.low |= ~(~(cpp_num_part) 0
566                               >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
567             result.high = ~(cpp_num_part) 0;
568             result = num_trim (result, CPP_OPTION (pfile, precision));
569           }
570       }
571       break;
572
573     case CPP_NAME:
574       if (token->val.node == pfile->spec_nodes.n_defined)
575         return parse_defined (pfile);
576       else if (CPP_OPTION (pfile, cplusplus)
577                && (token->val.node == pfile->spec_nodes.n_true
578                    || token->val.node == pfile->spec_nodes.n_false))
579         {
580           result.high = 0;
581           result.low = (token->val.node == pfile->spec_nodes.n_true);
582
583           /* Warn about use of true or false in #if when pedantic
584              and stdbool.h has not been included.  */
585           if (CPP_PEDANTIC (pfile)
586               && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
587             cpp_error (pfile, DL_PEDWARN,
588                        "ISO C++ does not permit \"%s\" in #if",
589                        NODE_NAME (token->val.node));
590         }
591       else
592         {
593           result.high = 0;
594           result.low = 0;
595           if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
596             cpp_error (pfile, DL_WARNING, "\"%s\" is not defined",
597                        NODE_NAME (token->val.node));
598         }
599       break;
600
601     default: /* CPP_HASH */
602       _cpp_test_assertion (pfile, &temp);
603       result.high = 0;
604       result.low = temp;
605     }
606
607   result.unsignedp = !!unsignedp;
608   result.overflow = false;
609   return result;
610 }
611 \f
612 /* Operator precedence and flags table.
613
614 After an operator is returned from the lexer, if it has priority less
615 than the operator on the top of the stack, we reduce the stack by one
616 operator and repeat the test.  Since equal priorities do not reduce,
617 this is naturally right-associative.
618
619 We handle left-associative operators by decrementing the priority of
620 just-lexed operators by one, but retaining the priority of operators
621 already on the stack.
622
623 The remaining cases are '(' and ')'.  We handle '(' by skipping the
624 reduction phase completely.  ')' is given lower priority than
625 everything else, including '(', effectively forcing a reduction of the
626 parenthesised expression.  If there is a matching '(', the routine
627 reduce() exits immediately.  If the normal exit route sees a ')', then
628 there cannot have been a matching '(' and an error message is output.
629
630 The parser assumes all shifted operators require a left operand unless
631 the flag NO_L_OPERAND is set.  These semantics are automatic; any
632 extra semantics need to be handled with operator-specific code.  */
633
634 /* Flags.  */
635 #define NO_L_OPERAND    (1 << 0)
636 #define LEFT_ASSOC      (1 << 1)
637
638 /* Arity. */
639 #define UNARY           (1 << 0)
640 #define BINARY          (1 << 1)
641 #define OTHER           (1 << 2)
642
643 typedef cpp_num (*binary_handler) PARAMS ((cpp_reader *, cpp_num, cpp_num,
644                                            enum cpp_ttype));
645 /* Operator to priority map.  Must be in the same order as the first
646    N entries of enum cpp_ttype.  */
647 static const struct operator
648 {
649   uchar prio;
650   uchar flags;
651   uchar arity;
652   binary_handler handler;
653 } optab[] =
654 {
655   /* EQ */              {0, 0, OTHER, NULL},    /* Shouldn't happen.  */
656   /* NOT */             {16, NO_L_OPERAND, UNARY, NULL},
657   /* GREATER */         {12, LEFT_ASSOC, BINARY, num_inequality_op},
658   /* LESS */            {12, LEFT_ASSOC, BINARY, num_inequality_op},
659   /* PLUS */            {14, LEFT_ASSOC, BINARY, num_binary_op},
660   /* MINUS */           {14, LEFT_ASSOC, BINARY, num_binary_op},
661   /* MULT */            {15, LEFT_ASSOC, BINARY, num_mul},
662   /* DIV */             {15, LEFT_ASSOC, BINARY, num_div_op},
663   /* MOD */             {15, LEFT_ASSOC, BINARY, num_div_op},
664   /* AND */             {9, LEFT_ASSOC, BINARY, num_bitwise_op},
665   /* OR */              {7, LEFT_ASSOC, BINARY, num_bitwise_op},
666   /* XOR */             {8, LEFT_ASSOC, BINARY, num_bitwise_op},
667   /* RSHIFT */          {13, LEFT_ASSOC, BINARY, num_binary_op},
668   /* LSHIFT */          {13, LEFT_ASSOC, BINARY, num_binary_op},
669
670   /* MIN */             {10, LEFT_ASSOC, BINARY, num_binary_op},
671   /* MAX */             {10, LEFT_ASSOC, BINARY, num_binary_op},
672
673   /* COMPL */           {16, NO_L_OPERAND, UNARY, NULL},
674   /* AND_AND */         {6, LEFT_ASSOC, OTHER, NULL},
675   /* OR_OR */           {5, LEFT_ASSOC, OTHER, NULL},
676   /* QUERY */           {3, 0, OTHER, NULL},
677   /* COLON */           {4, LEFT_ASSOC, OTHER, NULL},
678   /* COMMA */           {2, LEFT_ASSOC, BINARY, num_binary_op},
679   /* OPEN_PAREN */      {1, NO_L_OPERAND, OTHER, NULL},
680   /* CLOSE_PAREN */     {0, 0, OTHER, NULL},
681   /* EOF */             {0, 0, OTHER, NULL},
682   /* EQ_EQ */           {11, LEFT_ASSOC, BINARY, num_equality_op},
683   /* NOT_EQ */          {11, LEFT_ASSOC, BINARY, num_equality_op},
684   /* GREATER_EQ */      {12, LEFT_ASSOC, BINARY, num_inequality_op},
685   /* LESS_EQ */         {12, LEFT_ASSOC, BINARY, num_inequality_op},
686   /* UPLUS */           {16, NO_L_OPERAND, UNARY, NULL},
687   /* UMINUS */          {16, NO_L_OPERAND, UNARY, NULL}
688 };
689
690 /* Parse and evaluate a C expression, reading from PFILE.
691    Returns the truth value of the expression.
692
693    The implementation is an operator precedence parser, i.e. a
694    bottom-up parser, using a stack for not-yet-reduced tokens.
695
696    The stack base is op_stack, and the current stack pointer is 'top'.
697    There is a stack element for each operator (only), and the most
698    recently pushed operator is 'top->op'.  An operand (value) is
699    stored in the 'value' field of the stack element of the operator
700    that precedes it.  */
701 bool
702 _cpp_parse_expr (pfile)
703      cpp_reader *pfile;
704 {
705   struct op *top = pfile->op_stack;
706   const cpp_token *token = NULL, *prev_token;
707   unsigned int lex_count;
708   bool saw_leading_not, want_value = true;
709
710   pfile->state.skip_eval = 0;
711
712   /* Set up detection of #if ! defined().  */
713   pfile->mi_ind_cmacro = 0;
714   saw_leading_not = false;
715   lex_count = 0;
716
717   /* Lowest priority operator prevents further reductions.  */
718   top->op = CPP_EOF;
719
720   for (;;)
721     {
722       struct op op;
723
724       prev_token = token;
725       token = cpp_get_token (pfile);
726       lex_count++;
727       op.op = token->type;
728
729       switch (op.op)
730         {
731           /* These tokens convert into values.  */
732         case CPP_NUMBER:
733         case CPP_CHAR:
734         case CPP_WCHAR:
735         case CPP_NAME:
736         case CPP_HASH:
737           if (!want_value)
738             SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
739                            cpp_token_as_text (pfile, token));
740           want_value = false;
741           top->value = eval_token (pfile, token);
742           continue;
743
744         case CPP_NOT:
745           saw_leading_not = lex_count == 1;
746           break;
747         case CPP_PLUS:
748           if (want_value)
749             op.op = CPP_UPLUS;
750           break;
751         case CPP_MINUS:
752           if (want_value)
753             op.op = CPP_UMINUS;
754           break;
755         case CPP_OTHER:
756           if (ISGRAPH (token->val.c))
757             SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
758           else
759             SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
760
761         default:
762           if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
763             SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
764                            cpp_token_as_text (pfile, token));
765           break;
766         }
767
768       /* Check we have a value or operator as appropriate.  */
769       if (optab[op.op].flags & NO_L_OPERAND)
770         {
771           if (!want_value)
772             SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
773                            cpp_token_as_text (pfile, token));
774         }
775       else if (want_value)
776         {
777           /* Ordering here is subtle and intended to favour the
778              missing parenthesis diagnostics over alternatives.  */
779           if (op.op == CPP_CLOSE_PAREN)
780             {
781               if (top->op == CPP_OPEN_PAREN)
782                 SYNTAX_ERROR ("void expression between '(' and ')'");
783             }
784           else if (top->op == CPP_EOF)
785             SYNTAX_ERROR ("#if with no expression");
786           if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
787             SYNTAX_ERROR2 ("operator '%s' has no right operand",
788                            cpp_token_as_text (pfile, prev_token));
789         }
790
791       top = reduce (pfile, top, op.op);
792       if (!top)
793         goto syntax_error;
794
795       if (op.op == CPP_EOF)
796         break;
797
798       switch (op.op)
799         {
800         case CPP_CLOSE_PAREN:
801           continue;
802         case CPP_OR_OR:
803           if (!num_zerop (top->value))
804             pfile->state.skip_eval++;
805           break;
806         case CPP_AND_AND:
807         case CPP_QUERY:
808           if (num_zerop (top->value))
809             pfile->state.skip_eval++;
810           break;
811         case CPP_COLON:
812           if (top->op != CPP_QUERY)
813             SYNTAX_ERROR (" ':' without preceding '?'");
814           if (!num_zerop (top[-1].value)) /* Was '?' condition true?  */
815             pfile->state.skip_eval++;
816           else
817             pfile->state.skip_eval--;
818         default:
819           break;
820         }
821
822       want_value = true;
823
824       /* Check for and handle stack overflow.  */
825       if (++top == pfile->op_limit)
826         top = _cpp_expand_op_stack (pfile);
827
828       top->op = op.op;
829     }
830
831   /* The controlling macro expression is only valid if we called lex 3
832      times: <!> <defined expression> and <EOF>.  push_conditional ()
833      checks that we are at top-of-file.  */
834   if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
835     pfile->mi_ind_cmacro = 0;
836
837   if (top != pfile->op_stack)
838     {
839       cpp_error (pfile, DL_ICE, "unbalanced stack in #if");
840     syntax_error:
841       return false;  /* Return false on syntax error.  */
842     }
843
844   return !num_zerop (top->value);
845 }
846
847 /* Reduce the operator / value stack if possible, in preparation for
848    pushing operator OP.  Returns NULL on error, otherwise the top of
849    the stack.  */
850 static struct op *
851 reduce (pfile, top, op)
852      cpp_reader *pfile;
853      struct op *top;
854      enum cpp_ttype op;
855 {
856   unsigned int prio;
857
858   if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
859     {
860     bad_op:
861       cpp_error (pfile, DL_ICE, "impossible operator '%u'", top->op);
862       return 0;
863     }
864
865   if (op == CPP_OPEN_PAREN)
866     return top;
867
868   /* Decrement the priority of left-associative operators to force a
869      reduction with operators of otherwise equal priority.  */
870   prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
871   while (prio < optab[top->op].prio)
872     {
873       if (optab[top->op].arity == UNARY)
874         {
875           if (!pfile->state.skip_eval)
876             top[-1].value = num_unary_op (pfile, top->value, top->op);
877           top--;
878         }
879       else if (optab[top->op].arity == BINARY)
880         {
881           if (!pfile->state.skip_eval)
882             top[-1].value = (* (binary_handler) optab[top->op].handler)
883               (pfile, top[-1].value, top->value, top->op);
884           top--;
885         }
886       /* Anything changing skip_eval has to be handled here.  */
887       else switch (top--->op)
888         {
889         case CPP_OR_OR:
890           if (!num_zerop (top->value))
891             pfile->state.skip_eval--;
892           top->value.low = !num_zerop (top->value) || !num_zerop (top[1].value);
893           top->value.high = 0;
894           top->value.unsignedp = false;
895           top->value.overflow = false;
896           break;
897
898         case CPP_AND_AND:
899           if (num_zerop (top->value))
900             pfile->state.skip_eval--;
901           top->value.low = !num_zerop (top->value) && !num_zerop (top[1].value);
902           top->value.high = 0;
903           top->value.unsignedp = false;
904           top->value.overflow = false;
905           break;
906
907         case CPP_OPEN_PAREN:
908           if (op != CPP_CLOSE_PAREN)
909             {
910               cpp_error (pfile, DL_ERROR, "missing ')' in expression");
911               return 0;
912             }
913           top->value = top[1].value;
914           return top;
915
916         case CPP_COLON:
917           top--;
918           if (!num_zerop (top->value))
919             {
920               pfile->state.skip_eval--;
921               top->value = top[1].value;
922             }
923           else
924             top->value = top[2].value;
925           top->value.unsignedp = (top[1].value.unsignedp
926                                   || top[2].value.unsignedp);
927           break;
928
929         case CPP_QUERY:
930           cpp_error (pfile, DL_ERROR, "'?' without following ':'");
931           return 0;
932
933         default:
934           goto bad_op;
935         }
936
937       if (top->value.overflow && !pfile->state.skip_eval)
938         cpp_error (pfile, DL_PEDWARN,
939                    "integer overflow in preprocessor expression");
940     }
941
942   if (op == CPP_CLOSE_PAREN)
943     {
944       cpp_error (pfile, DL_ERROR, "missing '(' in expression");
945       return 0;
946     }
947
948   return top;
949 }
950
951 /* Returns the position of the old top of stack after expansion.  */
952 struct op *
953 _cpp_expand_op_stack (pfile)
954      cpp_reader *pfile;
955 {
956   size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
957   size_t new_size = old_size * 2 + 20;
958
959   pfile->op_stack = (struct op *) xrealloc (pfile->op_stack,
960                                             new_size * sizeof (struct op));
961   pfile->op_limit = pfile->op_stack + new_size;
962
963   return pfile->op_stack + old_size;
964 }
965
966 /* Clears the unused high order bits of the number pointed to by PNUM.  */
967 static cpp_num
968 num_trim (num, precision)
969      cpp_num num;
970      size_t precision;
971 {
972   if (precision > PART_PRECISION)
973     {
974       precision -= PART_PRECISION;
975       if (precision < PART_PRECISION)
976         num.high &= ((cpp_num_part) 1 << precision) - 1;
977     }
978   else
979     {
980       if (precision < PART_PRECISION)
981         num.low &= ((cpp_num_part) 1 << precision) - 1;
982       num.high = 0;
983     }
984
985   return num;
986 }
987
988 /* True iff A (presumed signed) >= 0.  */
989 static bool
990 num_positive (num, precision)
991      cpp_num num;
992      size_t precision;
993 {
994   if (precision > PART_PRECISION)
995     {
996       precision -= PART_PRECISION;
997       return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
998     }
999
1000   return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1001 }
1002
1003 /* Sign extend a number, with PRECISION significant bits and all
1004    others assumed clear, to fill out a cpp_num structure.  */
1005 cpp_num
1006 cpp_num_sign_extend (num, precision)
1007      cpp_num num;
1008      size_t precision;
1009 {
1010   if (!num.unsignedp)
1011     {
1012       if (precision > PART_PRECISION)
1013         {
1014           precision -= PART_PRECISION;
1015           if (precision < PART_PRECISION
1016               && (num.high & (cpp_num_part) 1 << (precision - 1)))
1017             num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1018         }
1019       else if (num.low & (cpp_num_part) 1 << (precision - 1))
1020         {
1021           if (precision < PART_PRECISION)
1022             num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1023           num.high = ~(cpp_num_part) 0;
1024         }
1025     }
1026
1027   return num;
1028 }
1029
1030 /* Returns the negative of NUM.  */
1031 static cpp_num
1032 num_negate (num, precision)
1033      cpp_num num;
1034      size_t precision;
1035 {
1036   cpp_num copy;
1037
1038   copy = num;
1039   num.high = ~num.high;
1040   num.low = ~num.low;
1041   if (++num.low == 0)
1042     num.high++;
1043   num = num_trim (num, precision);
1044   num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1045
1046   return num;
1047 }
1048
1049 /* Returns true if A >= B.  */
1050 static bool
1051 num_greater_eq (pa, pb, precision)
1052      cpp_num pa, pb;
1053      size_t precision;
1054 {
1055   bool unsignedp;
1056
1057   unsignedp = pa.unsignedp || pb.unsignedp;
1058
1059   if (!unsignedp)
1060     {
1061       /* Both numbers have signed type.  If they are of different
1062        sign, the answer is the sign of A.  */
1063       unsignedp = num_positive (pa, precision);
1064
1065       if (unsignedp != num_positive (pb, precision))
1066         return unsignedp;
1067
1068       /* Otherwise we can do an unsigned comparison.  */
1069     }
1070
1071   return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1072 }
1073
1074 /* Returns LHS OP RHS, where OP is a bit-wise operation.  */
1075 static cpp_num
1076 num_bitwise_op (pfile, lhs, rhs, op)
1077      cpp_reader *pfile ATTRIBUTE_UNUSED;
1078      cpp_num lhs, rhs;
1079      enum cpp_ttype op;
1080 {
1081   lhs.overflow = false;
1082   lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1083
1084   /* As excess precision is zeroed, there is no need to num_trim () as
1085      these operations cannot introduce a set bit there.  */
1086   if (op == CPP_AND)
1087     {
1088       lhs.low &= rhs.low;
1089       lhs.high &= rhs.high;
1090     }
1091   else if (op == CPP_OR)
1092     {
1093       lhs.low |= rhs.low;
1094       lhs.high |= rhs.high;
1095     }
1096   else
1097     {
1098       lhs.low ^= rhs.low;
1099       lhs.high ^= rhs.high;
1100     }
1101
1102   return lhs;
1103 }
1104
1105 /* Returns LHS OP RHS, where OP is an inequality.  */
1106 static cpp_num
1107 num_inequality_op (pfile, lhs, rhs, op)
1108      cpp_reader *pfile;
1109      cpp_num lhs, rhs;
1110      enum cpp_ttype op;
1111 {
1112   bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1113
1114   if (op == CPP_GREATER_EQ)
1115     lhs.low = gte;
1116   else if (op == CPP_LESS)
1117     lhs.low = !gte;
1118   else if (op == CPP_GREATER)
1119     lhs.low = gte && !num_eq (lhs, rhs);
1120   else /* CPP_LESS_EQ.  */
1121     lhs.low = !gte || num_eq (lhs, rhs);
1122
1123   lhs.high = 0;
1124   lhs.overflow = false;
1125   lhs.unsignedp = false;
1126   return lhs;
1127 }
1128
1129 /* Returns LHS OP RHS, where OP is == or !=.  */
1130 static cpp_num
1131 num_equality_op (pfile, lhs, rhs, op)
1132      cpp_reader *pfile ATTRIBUTE_UNUSED;
1133      cpp_num lhs, rhs;
1134      enum cpp_ttype op;
1135 {
1136   /* Work around a 3.0.4 bug; see PR 6950.  */
1137   bool eq = num_eq (lhs, rhs);
1138   if (op == CPP_NOT_EQ)
1139     eq = !eq;
1140   lhs.low = eq;
1141   lhs.high = 0;
1142   lhs.overflow = false;
1143   lhs.unsignedp = false;
1144   return lhs;
1145 }
1146
1147 /* Shift NUM, of width PRECISION, right by N bits.  */
1148 static cpp_num
1149 num_rshift (num, precision, n)
1150      cpp_num num;
1151      size_t precision, n;
1152 {
1153   cpp_num_part sign_mask;
1154
1155   if (num.unsignedp || num_positive (num, precision))
1156     sign_mask = 0;
1157   else
1158     sign_mask = ~(cpp_num_part) 0;
1159
1160   if (n >= precision)
1161     num.high = num.low = sign_mask;
1162   else
1163     {
1164       /* Sign-extend.  */
1165       if (precision < PART_PRECISION)
1166         num.high = sign_mask, num.low |= sign_mask << precision;
1167       else if (precision < 2 * PART_PRECISION)
1168         num.high |= sign_mask << (precision - PART_PRECISION);
1169
1170       if (n >= PART_PRECISION)
1171         {
1172           n -= PART_PRECISION;
1173           num.low = num.high;
1174           num.high = sign_mask;
1175         }
1176
1177       if (n)
1178         {
1179           num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1180           num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1181         }
1182     }
1183
1184   num = num_trim (num, precision);
1185   num.overflow = false;
1186   return num;
1187 }
1188
1189 /* Shift NUM, of width PRECISION, left by N bits.  */
1190 static cpp_num
1191 num_lshift (num, precision, n)
1192      cpp_num num;
1193      size_t precision, n;
1194 {
1195   if (n >= precision)
1196     {
1197       num.overflow = !num.unsignedp && !num_zerop (num);
1198       num.high = num.low = 0;
1199     }
1200   else
1201     {
1202       cpp_num orig, maybe_orig;
1203       size_t m = n;
1204
1205       orig = num;
1206       if (m >= PART_PRECISION)
1207         {
1208           m -= PART_PRECISION;
1209           num.high = num.low;
1210           num.low = 0;
1211         }
1212       if (m)
1213         {
1214           num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1215           num.low <<= m;
1216         }
1217       num = num_trim (num, precision);
1218
1219       if (num.unsignedp)
1220         num.overflow = false;
1221       else
1222         {
1223           maybe_orig = num_rshift (num, precision, n);
1224           num.overflow = !num_eq (orig, maybe_orig);
1225         }
1226     }
1227
1228   return num;
1229 }
1230
1231 /* The four unary operators: +, -, ! and ~.  */
1232 static cpp_num
1233 num_unary_op (pfile, num, op)
1234      cpp_reader *pfile;
1235      cpp_num num;
1236      enum cpp_ttype op;
1237 {
1238   switch (op)
1239     {
1240     case CPP_UPLUS:
1241       if (CPP_WTRADITIONAL (pfile))
1242         cpp_error (pfile, DL_WARNING,
1243                    "traditional C rejects the unary plus operator");
1244       num.overflow = false;
1245       break;
1246
1247     case CPP_UMINUS:
1248       num = num_negate (num, CPP_OPTION (pfile, precision));
1249       break;
1250
1251     case CPP_COMPL:
1252       num.high = ~num.high;
1253       num.low = ~num.low;
1254       num = num_trim (num, CPP_OPTION (pfile, precision));
1255       num.overflow = false;
1256       break;
1257
1258     default: /* case CPP_NOT: */
1259       num.low = num_zerop (num);
1260       num.high = 0;
1261       num.overflow = false;
1262       num.unsignedp = false;
1263       break;
1264     }
1265
1266   return num;
1267 }
1268
1269 /* The various binary operators.  */
1270 static cpp_num
1271 num_binary_op (pfile, lhs, rhs, op)
1272      cpp_reader *pfile;
1273      cpp_num lhs, rhs;
1274      enum cpp_ttype op;
1275 {
1276   cpp_num result;
1277   size_t precision = CPP_OPTION (pfile, precision);
1278   bool gte;
1279   size_t n;
1280
1281   switch (op)
1282     {
1283       /* Shifts.  */
1284     case CPP_LSHIFT:
1285     case CPP_RSHIFT:
1286       if (!rhs.unsignedp && !num_positive (rhs, precision))
1287         {
1288           /* A negative shift is a positive shift the other way.  */
1289           if (op == CPP_LSHIFT)
1290             op = CPP_RSHIFT;
1291           else
1292             op = CPP_LSHIFT;
1293           rhs = num_negate (rhs, precision);
1294         }
1295       if (rhs.high)
1296         n = ~0;                 /* Maximal.  */
1297       else
1298         n = rhs.low;
1299       if (op == CPP_LSHIFT)
1300         lhs = num_lshift (lhs, precision, n);
1301       else
1302         lhs = num_rshift (lhs, precision, n);
1303       break;
1304
1305       /* Min / Max.  */
1306     case CPP_MIN:
1307     case CPP_MAX:
1308       {
1309         bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1310
1311         gte = num_greater_eq (lhs, rhs, precision);
1312         if (op == CPP_MIN)
1313           gte = !gte;
1314         if (!gte)
1315           lhs = rhs;
1316         lhs.unsignedp = unsignedp;
1317       }
1318       break;
1319
1320       /* Arithmetic.  */
1321     case CPP_MINUS:
1322       rhs = num_negate (rhs, precision);
1323     case CPP_PLUS:
1324       result.low = lhs.low + rhs.low;
1325       result.high = lhs.high + rhs.high;
1326       if (result.low < lhs.low)
1327         result.high++;
1328
1329       result = num_trim (result, precision);
1330       result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1331       if (result.unsignedp)
1332         result.overflow = false;
1333       else
1334         {
1335           bool lhsp = num_positive (lhs, precision);
1336           result.overflow = (lhsp == num_positive (rhs, precision)
1337                              && lhsp != num_positive (result, precision));
1338         }
1339       return result;
1340
1341       /* Comma.  */
1342     default: /* case CPP_COMMA: */
1343       if (CPP_PEDANTIC (pfile))
1344         cpp_error (pfile, DL_PEDWARN,
1345                    "comma operator in operand of #if");
1346       lhs = rhs;
1347       break;
1348     }
1349
1350   return lhs;
1351 }
1352
1353 /* Multiplies two unsigned cpp_num_parts to give a cpp_num.  This
1354    cannot overflow.  */
1355 static cpp_num
1356 num_part_mul (lhs, rhs)
1357      cpp_num_part lhs, rhs;
1358 {
1359   cpp_num result;
1360   cpp_num_part middle[2], temp;
1361
1362   result.low = LOW_PART (lhs) * LOW_PART (rhs);
1363   result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1364
1365   middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1366   middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1367
1368   temp = result.low;
1369   result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1370   if (result.low < temp)
1371     result.high++;
1372
1373   temp = result.low;
1374   result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1375   if (result.low < temp)
1376     result.high++;
1377
1378   result.high += HIGH_PART (middle[0]);
1379   result.high += HIGH_PART (middle[1]);
1380
1381   return result;
1382 }
1383
1384 /* Multiply two preprocessing numbers.  */
1385 static cpp_num
1386 num_mul (pfile, lhs, rhs, op)
1387      cpp_reader *pfile;
1388      cpp_num lhs, rhs;
1389      enum cpp_ttype op ATTRIBUTE_UNUSED;
1390 {
1391   cpp_num result, temp;
1392   bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1393   bool overflow, negate = false;
1394   size_t precision = CPP_OPTION (pfile, precision);
1395
1396   /* Prepare for unsigned multiplication.  */
1397   if (!unsignedp)
1398     {
1399       if (!num_positive (lhs, precision))
1400         negate = !negate, lhs = num_negate (lhs, precision);
1401       if (!num_positive (rhs, precision))
1402         negate = !negate, rhs = num_negate (rhs, precision);
1403     }
1404
1405   overflow = lhs.high && rhs.high;
1406   result = num_part_mul (lhs.low, rhs.low);
1407
1408   temp = num_part_mul (lhs.high, rhs.low);
1409   result.high += temp.low;
1410   if (temp.high)
1411     overflow = true;
1412
1413   temp = num_part_mul (lhs.low, rhs.high);
1414   result.high += temp.low;
1415   if (temp.high)
1416     overflow = true;
1417
1418   temp.low = result.low, temp.high = result.high;
1419   result = num_trim (result, precision);
1420   if (!num_eq (result, temp))
1421     overflow = true;
1422
1423   if (negate)
1424     result = num_negate (result, precision);
1425
1426   if (unsignedp)
1427     result.overflow = false;
1428   else
1429     result.overflow = overflow || (num_positive (result, precision) ^ !negate
1430                                    && !num_zerop (result));
1431   result.unsignedp = unsignedp;
1432
1433   return result;
1434 }
1435
1436 /* Divide two preprocessing numbers, returning the answer or the
1437    remainder depending upon OP.  */
1438 static cpp_num
1439 num_div_op (pfile, lhs, rhs, op)
1440      cpp_reader *pfile;
1441      cpp_num lhs, rhs;
1442      enum cpp_ttype op;
1443 {
1444   cpp_num result, sub;
1445   cpp_num_part mask;
1446   bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1447   bool negate = false, lhs_neg = false;
1448   size_t i, precision = CPP_OPTION (pfile, precision);
1449
1450   /* Prepare for unsigned division.  */
1451   if (!unsignedp)
1452     {
1453       if (!num_positive (lhs, precision))
1454         negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1455       if (!num_positive (rhs, precision))
1456         negate = !negate, rhs = num_negate (rhs, precision);
1457     }
1458
1459   /* Find the high bit.  */
1460   if (rhs.high)
1461     {
1462       i = precision - 1;
1463       mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1464       for (; ; i--, mask >>= 1)
1465         if (rhs.high & mask)
1466           break;
1467     }
1468   else if (rhs.low)
1469     {
1470       if (precision > PART_PRECISION)
1471         i = precision - PART_PRECISION - 1;
1472       else
1473         i = precision - 1;
1474       mask = (cpp_num_part) 1 << i;
1475       for (; ; i--, mask >>= 1)
1476         if (rhs.low & mask)
1477           break;
1478     }
1479   else
1480     {
1481       cpp_error (pfile, DL_ERROR, "division by zero in #if");
1482       return lhs;
1483     }
1484
1485   /* First non-zero bit of RHS is bit I.  Do naive division by
1486      shifting the RHS fully left, and subtracting from LHS if LHS is
1487      at least as big, and then repeating but with one less shift.
1488      This is not very efficient, but is easy to understand.  */
1489
1490   rhs.unsignedp = true;
1491   lhs.unsignedp = true;
1492   i = precision - i - 1;
1493   sub = num_lshift (rhs, precision, i);
1494
1495   result.high = result.low = 0;
1496   for (;;)
1497     {
1498       if (num_greater_eq (lhs, sub, precision))
1499         {
1500           lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1501           if (i >= PART_PRECISION)
1502             result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1503           else
1504             result.low |= (cpp_num_part) 1 << i;
1505         }
1506       if (i-- == 0)
1507         break;
1508       sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1509       sub.high >>= 1;
1510     }
1511
1512   /* We divide so that the remainder has the sign of the LHS.  */
1513   if (op == CPP_DIV)
1514     {
1515       result.unsignedp = unsignedp;
1516       if (unsignedp)
1517         result.overflow = false;
1518       else
1519         {
1520           if (negate)
1521             result = num_negate (result, precision);
1522           result.overflow = num_positive (result, precision) ^ !negate;
1523         }
1524
1525       return result;
1526     }
1527
1528   /* CPP_MOD.  */
1529   lhs.unsignedp = unsignedp;
1530   lhs.overflow = false;
1531   if (lhs_neg)
1532     lhs = num_negate (lhs, precision);
1533
1534   return lhs;
1535 }