OSDN Git Service

* pt.c (can_complete_type_without_circularity): Add static to
[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 = 0;
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).  */
381       else if (!result.unsignedp
382                && !CPP_OPTION (pfile, traditional)
383                && !num_positive (result, precision))
384         {
385           if (base == 10)
386             cpp_error (pfile, DL_WARNING,
387                        "integer constant is so large that it is unsigned");
388           result.unsignedp = 1;
389         }
390     }
391
392   return result;
393 }
394
395 /* Append DIGIT to NUM, a number of PRECISION bits being read in base
396    BASE.  */
397 static cpp_num
398 append_digit (num, digit, base, precision)
399      cpp_num num;
400      int digit, base;
401      size_t precision;
402 {
403   cpp_num result;
404   unsigned int shift = 3 + (base == 16);
405   bool overflow;
406   cpp_num_part add_high, add_low;
407
408   /* Multiply by 8 or 16.  Catching this overflow here means we don't
409      need to worry about add_high overflowing.  */
410   overflow = num.high >> (PART_PRECISION - shift);
411   result.high = num.high << shift;
412   result.low = num.low << shift;
413   result.high |= num.low >> (PART_PRECISION - shift);
414
415   if (base == 10)
416     {
417       add_low = num.low << 1;
418       add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
419     }
420   else
421     add_high = add_low = 0;
422
423   if (add_low + digit < add_low)
424     add_high++;
425   add_low += digit;
426     
427   if (result.low + add_low < result.low)
428     add_high++;
429   if (result.high + add_high < result.high)
430     overflow = true;
431
432   result.low += add_low;
433   result.high += add_high;
434
435   /* The above code catches overflow of a cpp_num type.  This catches
436      overflow of the (possibly shorter) target precision.  */
437   num.low = result.low;
438   num.high = result.high;
439   result = num_trim (result, precision);
440   if (!num_eq (result, num))
441     overflow = true;
442
443   result.unsignedp = num.unsignedp;
444   result.overflow = overflow;
445   return result;
446 }
447
448 /* Handle meeting "defined" in a preprocessor expression.  */
449 static cpp_num
450 parse_defined (pfile)
451      cpp_reader *pfile;
452 {
453   cpp_num result;
454   int paren = 0;
455   cpp_hashnode *node = 0;
456   const cpp_token *token;
457   cpp_context *initial_context = pfile->context;
458
459   /* Don't expand macros.  */
460   pfile->state.prevent_expansion++;
461
462   token = cpp_get_token (pfile);
463   if (token->type == CPP_OPEN_PAREN)
464     {
465       paren = 1;
466       token = cpp_get_token (pfile);
467     }
468
469   if (token->type == CPP_NAME)
470     {
471       node = token->val.node;
472       if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
473         {
474           cpp_error (pfile, DL_ERROR, "missing ')' after \"defined\"");
475           node = 0;
476         }
477     }
478   else
479     {
480       cpp_error (pfile, DL_ERROR,
481                  "operator \"defined\" requires an identifier");
482       if (token->flags & NAMED_OP)
483         {
484           cpp_token op;
485
486           op.flags = 0;
487           op.type = token->type;
488           cpp_error (pfile, DL_ERROR,
489                      "(\"%s\" is an alternative token for \"%s\" in C++)",
490                      cpp_token_as_text (pfile, token),
491                      cpp_token_as_text (pfile, &op));
492         }
493     }
494
495   if (node)
496     {
497       if (pfile->context != initial_context)
498         cpp_error (pfile, DL_WARNING,
499                    "this use of \"defined\" may not be portable");
500
501       /* A possible controlling macro of the form #if !defined ().
502          _cpp_parse_expr checks there was no other junk on the line.  */
503       pfile->mi_ind_cmacro = node;
504     }
505
506   pfile->state.prevent_expansion--;
507
508   result.unsignedp = 0;
509   result.high = 0;
510   result.overflow = 0;
511   result.low = node && node->type == NT_MACRO;
512   return result;
513 }
514
515 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
516    number or character constant, or the result of the "defined" or "#"
517    operators).  */
518 static cpp_num
519 eval_token (pfile, token)
520      cpp_reader *pfile;
521      const cpp_token *token;
522 {
523   cpp_num result;
524   unsigned int temp;
525   int unsignedp = 0;
526
527   switch (token->type)
528     {
529     case CPP_NUMBER:
530       temp = cpp_classify_number (pfile, token);
531       switch (temp & CPP_N_CATEGORY)
532         {
533         case CPP_N_FLOATING:
534           cpp_error (pfile, DL_ERROR,
535                      "floating constant in preprocessor expression");
536           break;
537         case CPP_N_INTEGER:
538           if (!(temp & CPP_N_IMAGINARY))
539             return cpp_interpret_integer (pfile, token, temp);
540           cpp_error (pfile, DL_ERROR,
541                      "imaginary number in preprocessor expression");
542           break;
543
544         case CPP_N_INVALID:
545           /* Error already issued.  */
546           break;
547         }
548       result.high = result.low = 0;
549       break;
550
551     case CPP_WCHAR:
552     case CPP_CHAR:
553       {
554         cppchar_t cc = cpp_interpret_charconst (pfile, token,
555                                                 &temp, &unsignedp);
556
557         result.high = 0;
558         result.low = cc;
559         /* Sign-extend the result if necessary.  */
560         if (!unsignedp && (cppchar_signed_t) cc < 0)
561           {
562             if (PART_PRECISION > BITS_PER_CPPCHAR_T)
563               result.low |= ~(~(cpp_num_part) 0
564                               >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
565             result.high = ~(cpp_num_part) 0;
566             result = num_trim (result, CPP_OPTION (pfile, precision));
567           }
568       }
569       break;
570
571     case CPP_NAME:
572       if (token->val.node == pfile->spec_nodes.n_defined)
573         return parse_defined (pfile);
574       else if (CPP_OPTION (pfile, cplusplus)
575                && (token->val.node == pfile->spec_nodes.n_true
576                    || token->val.node == pfile->spec_nodes.n_false))
577         {
578           result.high = 0;
579           result.low = (token->val.node == pfile->spec_nodes.n_true);
580
581           /* Warn about use of true or false in #if when pedantic
582              and stdbool.h has not been included.  */
583           if (CPP_PEDANTIC (pfile)
584               && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
585             cpp_error (pfile, DL_PEDWARN,
586                        "ISO C++ does not permit \"%s\" in #if",
587                        NODE_NAME (token->val.node));
588         }
589       else
590         {
591           result.high = 0;
592           result.low = 0;
593           if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
594             cpp_error (pfile, DL_WARNING, "\"%s\" is not defined",
595                        NODE_NAME (token->val.node));
596         }
597       break;
598
599     default: /* CPP_HASH */
600       _cpp_test_assertion (pfile, &temp);
601       result.high = 0;
602       result.low = temp;
603     }
604
605   result.unsignedp = unsignedp;
606   result.overflow = 0;
607   return result;
608 }
609 \f
610 /* Operator precedence and flags table.
611
612 After an operator is returned from the lexer, if it has priority less
613 than the operator on the top of the stack, we reduce the stack by one
614 operator and repeat the test.  Since equal priorities do not reduce,
615 this is naturally right-associative.
616
617 We handle left-associative operators by decrementing the priority of
618 just-lexed operators by one, but retaining the priority of operators
619 already on the stack.
620
621 The remaining cases are '(' and ')'.  We handle '(' by skipping the
622 reduction phase completely.  ')' is given lower priority than
623 everything else, including '(', effectively forcing a reduction of the
624 parenthesised expression.  If there is a matching '(', the routine
625 reduce() exits immediately.  If the normal exit route sees a ')', then
626 there cannot have been a matching '(' and an error message is output.
627
628 The parser assumes all shifted operators require a left operand unless
629 the flag NO_L_OPERAND is set.  These semantics are automatic; any
630 extra semantics need to be handled with operator-specific code.  */
631
632 /* Flags.  */
633 #define NO_L_OPERAND    (1 << 0)
634 #define LEFT_ASSOC      (1 << 1)
635
636 /* Arity. */
637 #define UNARY           (1 << 0)
638 #define BINARY          (1 << 1)
639 #define OTHER           (1 << 2)
640
641 typedef cpp_num (*binary_handler) PARAMS ((cpp_reader *, cpp_num, cpp_num,
642                                            enum cpp_ttype));
643 /* Operator to priority map.  Must be in the same order as the first
644    N entries of enum cpp_ttype.  */
645 static const struct operator
646 {
647   uchar prio;
648   uchar flags;
649   uchar arity;
650   binary_handler handler;
651 } optab[] =
652 {
653   /* EQ */              {0, 0, OTHER, NULL},    /* Shouldn't happen.  */
654   /* NOT */             {16, NO_L_OPERAND, UNARY, NULL},
655   /* GREATER */         {12, LEFT_ASSOC, BINARY, num_inequality_op},
656   /* LESS */            {12, LEFT_ASSOC, BINARY, num_inequality_op},
657   /* PLUS */            {14, LEFT_ASSOC, BINARY, num_binary_op},
658   /* MINUS */           {14, LEFT_ASSOC, BINARY, num_binary_op},
659   /* MULT */            {15, LEFT_ASSOC, BINARY, num_mul},
660   /* DIV */             {15, LEFT_ASSOC, BINARY, num_div_op},
661   /* MOD */             {15, LEFT_ASSOC, BINARY, num_div_op},
662   /* AND */             {9, LEFT_ASSOC, BINARY, num_bitwise_op},
663   /* OR */              {7, LEFT_ASSOC, BINARY, num_bitwise_op},
664   /* XOR */             {8, LEFT_ASSOC, BINARY, num_bitwise_op},
665   /* RSHIFT */          {13, LEFT_ASSOC, BINARY, num_binary_op},
666   /* LSHIFT */          {13, LEFT_ASSOC, BINARY, num_binary_op},
667
668   /* MIN */             {10, LEFT_ASSOC, BINARY, num_binary_op},
669   /* MAX */             {10, LEFT_ASSOC, BINARY, num_binary_op},
670
671   /* COMPL */           {16, NO_L_OPERAND, UNARY, NULL},
672   /* AND_AND */         {6, LEFT_ASSOC, OTHER, NULL},
673   /* OR_OR */           {5, LEFT_ASSOC, OTHER, NULL},
674   /* QUERY */           {3, 0, OTHER, NULL},
675   /* COLON */           {4, LEFT_ASSOC, OTHER, NULL},
676   /* COMMA */           {2, LEFT_ASSOC, BINARY, num_binary_op},
677   /* OPEN_PAREN */      {1, NO_L_OPERAND, OTHER, NULL},
678   /* CLOSE_PAREN */     {0, 0, OTHER, NULL},
679   /* EOF */             {0, 0, OTHER, NULL},
680   /* EQ_EQ */           {11, LEFT_ASSOC, BINARY, num_equality_op},
681   /* NOT_EQ */          {11, LEFT_ASSOC, BINARY, num_equality_op},
682   /* GREATER_EQ */      {12, LEFT_ASSOC, BINARY, num_inequality_op},
683   /* LESS_EQ */         {12, LEFT_ASSOC, BINARY, num_inequality_op},
684   /* UPLUS */           {16, NO_L_OPERAND, UNARY, NULL},
685   /* UMINUS */          {16, NO_L_OPERAND, UNARY, NULL}
686 };
687
688 /* Parse and evaluate a C expression, reading from PFILE.
689    Returns the truth value of the expression.
690
691    The implementation is an operator precedence parser, i.e. a
692    bottom-up parser, using a stack for not-yet-reduced tokens.
693
694    The stack base is op_stack, and the current stack pointer is 'top'.
695    There is a stack element for each operator (only), and the most
696    recently pushed operator is 'top->op'.  An operand (value) is
697    stored in the 'value' field of the stack element of the operator
698    that precedes it.  */
699 bool
700 _cpp_parse_expr (pfile)
701      cpp_reader *pfile;
702 {
703   struct op *top = pfile->op_stack;
704   const cpp_token *token = NULL, *prev_token;
705   unsigned int lex_count;
706   bool saw_leading_not, want_value = true;
707
708   pfile->state.skip_eval = 0;
709
710   /* Set up detection of #if ! defined().  */
711   pfile->mi_ind_cmacro = 0;
712   saw_leading_not = false;
713   lex_count = 0;
714
715   /* Lowest priority operator prevents further reductions.  */
716   top->op = CPP_EOF;
717
718   for (;;)
719     {
720       struct op op;
721
722       prev_token = token;
723       token = cpp_get_token (pfile);
724       lex_count++;
725       op.op = token->type;
726
727       switch (op.op)
728         {
729           /* These tokens convert into values.  */
730         case CPP_NUMBER:
731         case CPP_CHAR:
732         case CPP_WCHAR:
733         case CPP_NAME:
734         case CPP_HASH:
735           if (!want_value)
736             SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
737                            cpp_token_as_text (pfile, token));
738           want_value = false;
739           top->value = eval_token (pfile, token);
740           continue;
741
742         case CPP_NOT:
743           saw_leading_not = lex_count == 1;
744           break;
745         case CPP_PLUS:
746           if (want_value)
747             op.op = CPP_UPLUS;
748           break;
749         case CPP_MINUS:
750           if (want_value)
751             op.op = CPP_UMINUS;
752           break;
753         case CPP_OTHER:
754           if (ISGRAPH (token->val.c))
755             SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
756           else
757             SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
758
759         default:
760           if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
761             SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
762                            cpp_token_as_text (pfile, token));
763           break;
764         }
765
766       /* Check we have a value or operator as appropriate.  */
767       if (optab[op.op].flags & NO_L_OPERAND)
768         {
769           if (!want_value)
770             SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
771                            cpp_token_as_text (pfile, token));
772         }
773       else if (want_value)
774         {
775           /* Ordering here is subtle and intended to favour the
776              missing parenthesis diagnostics over alternatives.  */
777           if (op.op == CPP_CLOSE_PAREN)
778             {
779               if (top->op == CPP_OPEN_PAREN)
780                 SYNTAX_ERROR ("void expression between '(' and ')'");
781             }
782           else if (top->op == CPP_EOF)
783             SYNTAX_ERROR ("#if with no expression");
784           if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
785             SYNTAX_ERROR2 ("operator '%s' has no right operand",
786                            cpp_token_as_text (pfile, prev_token));
787         }
788
789       top = reduce (pfile, top, op.op);
790       if (!top)
791         goto syntax_error;
792
793       if (op.op == CPP_EOF)
794         break;
795
796       switch (op.op)
797         {
798         case CPP_CLOSE_PAREN:
799           continue;
800         case CPP_OR_OR:
801           if (!num_zerop (top->value))
802             pfile->state.skip_eval++;
803           break;
804         case CPP_AND_AND:
805         case CPP_QUERY:
806           if (num_zerop (top->value))
807             pfile->state.skip_eval++;
808           break;
809         case CPP_COLON:
810           if (top->op != CPP_QUERY)
811             SYNTAX_ERROR (" ':' without preceding '?'");
812           if (!num_zerop (top[-1].value)) /* Was '?' condition true?  */
813             pfile->state.skip_eval++;
814           else
815             pfile->state.skip_eval--;
816         default:
817           break;
818         }
819
820       want_value = true;
821
822       /* Check for and handle stack overflow.  */
823       if (++top == pfile->op_limit)
824         top = _cpp_expand_op_stack (pfile);
825
826       top->op = op.op;
827     }
828
829   /* The controlling macro expression is only valid if we called lex 3
830      times: <!> <defined expression> and <EOF>.  push_conditional ()
831      checks that we are at top-of-file.  */
832   if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
833     pfile->mi_ind_cmacro = 0;
834
835   if (top != pfile->op_stack)
836     {
837       cpp_error (pfile, DL_ICE, "unbalanced stack in #if");
838     syntax_error:
839       return false;  /* Return false on syntax error.  */
840     }
841
842   return !num_zerop (top->value);
843 }
844
845 /* Reduce the operator / value stack if possible, in preparation for
846    pushing operator OP.  Returns NULL on error, otherwise the top of
847    the stack.  */
848 static struct op *
849 reduce (pfile, top, op)
850      cpp_reader *pfile;
851      struct op *top;
852      enum cpp_ttype op;
853 {
854   unsigned int prio;
855
856   if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
857     {
858     bad_op:
859       cpp_error (pfile, DL_ICE, "impossible operator '%u'", top->op);
860       return 0;
861     }
862
863   if (op == CPP_OPEN_PAREN)
864     return top;
865
866   /* Decrement the priority of left-associative operators to force a
867      reduction with operators of otherwise equal priority.  */
868   prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
869   while (prio < optab[top->op].prio)
870     {
871       if (optab[top->op].arity == UNARY)
872         {
873           if (!pfile->state.skip_eval)
874             top[-1].value = num_unary_op (pfile, top->value, top->op);
875           top--;
876         }
877       else if (optab[top->op].arity == BINARY)
878         {
879           if (!pfile->state.skip_eval)
880             top[-1].value = (* (binary_handler) optab[top->op].handler)
881               (pfile, top[-1].value, top->value, top->op);
882           top--;
883         }
884       /* Anything changing skip_eval has to be handled here.  */
885       else switch (top--->op)
886         {
887         case CPP_OR_OR:
888           if (!num_zerop (top->value))
889             pfile->state.skip_eval--;
890           top->value.low = !num_zerop (top->value) || !num_zerop (top[1].value);
891           top->value.high = 0;
892           top->value.unsignedp = false;
893           top->value.overflow = false;
894           break;
895
896         case CPP_AND_AND:
897           if (num_zerop (top->value))
898             pfile->state.skip_eval--;
899           top->value.low = !num_zerop (top->value) && !num_zerop (top[1].value);
900           top->value.high = 0;
901           top->value.unsignedp = false;
902           top->value.overflow = false;
903           break;
904
905         case CPP_OPEN_PAREN:
906           if (op != CPP_CLOSE_PAREN)
907             {
908               cpp_error (pfile, DL_ERROR, "missing ')' in expression");
909               return 0;
910             }
911           top->value = top[1].value;
912           return top;
913
914         case CPP_COLON:
915           top--;
916           if (!num_zerop (top->value))
917             {
918               pfile->state.skip_eval--;
919               top->value = top[1].value;
920             }
921           else
922             top->value = top[2].value;
923           top->value.unsignedp = (top[1].value.unsignedp
924                                   || top[2].value.unsignedp);
925           break;
926
927         case CPP_QUERY:
928           cpp_error (pfile, DL_ERROR, "'?' without following ':'");
929           return 0;
930
931         default:
932           goto bad_op;
933         }
934
935       if (top->value.overflow && !pfile->state.skip_eval)
936         cpp_error (pfile, DL_PEDWARN,
937                    "integer overflow in preprocessor expression");
938     }
939
940   if (op == CPP_CLOSE_PAREN)
941     {
942       cpp_error (pfile, DL_ERROR, "missing '(' in expression");
943       return 0;
944     }
945
946   return top;
947 }
948
949 /* Returns the position of the old top of stack after expansion.  */
950 struct op *
951 _cpp_expand_op_stack (pfile)
952      cpp_reader *pfile;
953 {
954   size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
955   size_t new_size = old_size * 2 + 20;
956
957   pfile->op_stack = (struct op *) xrealloc (pfile->op_stack,
958                                             new_size * sizeof (struct op));
959   pfile->op_limit = pfile->op_stack + new_size;
960
961   return pfile->op_stack + old_size;
962 }
963
964 /* Clears the unused high order bits of the number pointed to by PNUM.  */
965 static cpp_num
966 num_trim (num, precision)
967      cpp_num num;
968      size_t precision;
969 {
970   if (precision > PART_PRECISION)
971     {
972       precision -= PART_PRECISION;
973       if (precision < PART_PRECISION)
974         num.high &= ((cpp_num_part) 1 << precision) - 1;
975     }
976   else
977     {
978       if (precision < PART_PRECISION)
979         num.low &= ((cpp_num_part) 1 << precision) - 1;
980       num.high = 0;
981     }
982
983   return num;
984 }
985
986 /* True iff A (presumed signed) >= 0.  */
987 static bool
988 num_positive (num, precision)
989      cpp_num num;
990      size_t precision;
991 {
992   if (precision > PART_PRECISION)
993     {
994       precision -= PART_PRECISION;
995       return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
996     }
997
998   return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
999 }
1000
1001 /* Sign extend a number, with PRECISION significant bits and all
1002    others assumed clear, to fill out a cpp_num structure.  */
1003 cpp_num
1004 cpp_num_sign_extend (num, precision)
1005      cpp_num num;
1006      size_t precision;
1007 {
1008   if (!num.unsignedp)
1009     {
1010       if (precision > PART_PRECISION)
1011         {
1012           precision -= PART_PRECISION;
1013           if (precision < PART_PRECISION
1014               && (num.high & (cpp_num_part) 1 << (precision - 1)))
1015             num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1016         }
1017       else if (num.low & (cpp_num_part) 1 << (precision - 1))
1018         {
1019           if (precision < PART_PRECISION)
1020             num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1021           num.high = ~(cpp_num_part) 0;
1022         }
1023     }
1024
1025   return num;
1026 }
1027
1028 /* Returns the negative of NUM.  */
1029 static cpp_num
1030 num_negate (num, precision)
1031      cpp_num num;
1032      size_t precision;
1033 {
1034   cpp_num copy;
1035
1036   copy = num;
1037   num.high = ~num.high;
1038   num.low = ~num.low;
1039   if (++num.low == 0)
1040     num.high++;
1041   num = num_trim (num, precision);
1042   num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1043
1044   return num;
1045 }
1046
1047 /* Returns true if A >= B.  */
1048 static bool
1049 num_greater_eq (pa, pb, precision)
1050      cpp_num pa, pb;
1051      size_t precision;
1052 {
1053   bool unsignedp;
1054
1055   unsignedp = pa.unsignedp || pb.unsignedp;
1056
1057   if (!unsignedp)
1058     {
1059       /* Both numbers have signed type.  If they are of different
1060        sign, the answer is the sign of A.  */
1061       unsignedp = num_positive (pa, precision);
1062
1063       if (unsignedp != num_positive (pb, precision))
1064         return unsignedp;
1065
1066       /* Otherwise we can do an unsigned comparison.  */
1067     }
1068
1069   return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1070 }
1071
1072 /* Returns LHS OP RHS, where OP is a bit-wise operation.  */
1073 static cpp_num
1074 num_bitwise_op (pfile, lhs, rhs, op)
1075      cpp_reader *pfile ATTRIBUTE_UNUSED;
1076      cpp_num lhs, rhs;
1077      enum cpp_ttype op;
1078 {
1079   lhs.overflow = false;
1080   lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1081
1082   /* As excess precision is zeroed, there is no need to num_trim () as
1083      these operations cannot introduce a set bit there.  */
1084   if (op == CPP_AND)
1085     {
1086       lhs.low &= rhs.low;
1087       lhs.high &= rhs.high;
1088     }
1089   else if (op == CPP_OR)
1090     {
1091       lhs.low |= rhs.low;
1092       lhs.high |= rhs.high;
1093     }
1094   else
1095     {
1096       lhs.low ^= rhs.low;
1097       lhs.high ^= rhs.high;
1098     }
1099
1100   return lhs;
1101 }
1102
1103 /* Returns LHS OP RHS, where OP is an inequality.  */
1104 static cpp_num
1105 num_inequality_op (pfile, lhs, rhs, op)
1106      cpp_reader *pfile;
1107      cpp_num lhs, rhs;
1108      enum cpp_ttype op;
1109 {
1110   bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1111
1112   if (op == CPP_GREATER_EQ)
1113     lhs.low = gte;
1114   else if (op == CPP_LESS)
1115     lhs.low = !gte;
1116   else if (op == CPP_GREATER)
1117     lhs.low = gte && !num_eq (lhs, rhs);
1118   else /* CPP_LESS_EQ.  */
1119     lhs.low = !gte || num_eq (lhs, rhs);
1120
1121   lhs.high = 0;
1122   lhs.overflow = false;
1123   lhs.unsignedp = false;
1124   return lhs;
1125 }
1126
1127 /* Returns LHS OP RHS, where OP is == or !=.  */
1128 static cpp_num
1129 num_equality_op (pfile, lhs, rhs, op)
1130      cpp_reader *pfile ATTRIBUTE_UNUSED;
1131      cpp_num lhs, rhs;
1132      enum cpp_ttype op;
1133 {
1134   /* Work around a 3.0.4 bug; see PR 6950.  */
1135   bool eq = num_eq (lhs, rhs);
1136   if (op == CPP_NOT_EQ)
1137     eq = !eq;
1138   lhs.low = eq;
1139   lhs.high = 0;
1140   lhs.overflow = false;
1141   lhs.unsignedp = false;
1142   return lhs;
1143 }
1144
1145 /* Shift NUM, of width PRECISION, right by N bits.  */
1146 static cpp_num
1147 num_rshift (num, precision, n)
1148      cpp_num num;
1149      size_t precision, n;
1150 {
1151   cpp_num_part sign_mask;
1152
1153   if (num.unsignedp || num_positive (num, precision))
1154     sign_mask = 0;
1155   else
1156     sign_mask = ~(cpp_num_part) 0;
1157
1158   if (n >= precision)
1159     num.high = num.low = sign_mask;
1160   else
1161     {
1162       /* Sign-extend.  */
1163       if (precision < PART_PRECISION)
1164         num.high = sign_mask, num.low |= sign_mask << precision;
1165       else if (precision < 2 * PART_PRECISION)
1166         num.high |= sign_mask << (precision - PART_PRECISION);
1167
1168       if (n >= PART_PRECISION)
1169         {
1170           n -= PART_PRECISION;
1171           num.low = num.high;
1172           num.high = sign_mask;
1173         }
1174
1175       if (n)
1176         {
1177           num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1178           num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1179         }
1180     }
1181
1182   num = num_trim (num, precision);
1183   num.overflow = false;
1184   return num;
1185 }
1186
1187 /* Shift NUM, of width PRECISION, left by N bits.  */
1188 static cpp_num
1189 num_lshift (num, precision, n)
1190      cpp_num num;
1191      size_t precision, n;
1192 {
1193   if (n >= precision)
1194     {
1195       num.overflow = !num.unsignedp && !num_zerop (num);
1196       num.high = num.low = 0;
1197     }
1198   else
1199     {
1200       cpp_num orig, maybe_orig;
1201       size_t m = n;
1202
1203       orig = num;
1204       if (m >= PART_PRECISION)
1205         {
1206           m -= PART_PRECISION;
1207           num.high = num.low;
1208           num.low = 0;
1209         }
1210       if (m)
1211         {
1212           num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1213           num.low <<= m;
1214         }
1215       num = num_trim (num, precision);
1216
1217       if (num.unsignedp)
1218         num.overflow = false;
1219       else
1220         {
1221           maybe_orig = num_rshift (num, precision, n);
1222           num.overflow = !num_eq (orig, maybe_orig);
1223         }
1224     }
1225
1226   return num;
1227 }
1228
1229 /* The four unary operators: +, -, ! and ~.  */
1230 static cpp_num
1231 num_unary_op (pfile, num, op)
1232      cpp_reader *pfile;
1233      cpp_num num;
1234      enum cpp_ttype op;
1235 {
1236   switch (op)
1237     {
1238     case CPP_UPLUS:
1239       if (CPP_WTRADITIONAL (pfile))
1240         cpp_error (pfile, DL_WARNING,
1241                    "traditional C rejects the unary plus operator");
1242       num.overflow = false;
1243       break;
1244
1245     case CPP_UMINUS:
1246       num = num_negate (num, CPP_OPTION (pfile, precision));
1247       break;
1248
1249     case CPP_COMPL:
1250       num.high = ~num.high;
1251       num.low = ~num.low;
1252       num = num_trim (num, CPP_OPTION (pfile, precision));
1253       num.overflow = false;
1254       break;
1255
1256     default: /* case CPP_NOT: */
1257       num.low = num_zerop (num);
1258       num.high = 0;
1259       num.overflow = false;
1260       num.unsignedp = false;
1261       break;
1262     }
1263
1264   return num;
1265 }
1266
1267 /* The various binary operators.  */
1268 static cpp_num
1269 num_binary_op (pfile, lhs, rhs, op)
1270      cpp_reader *pfile;
1271      cpp_num lhs, rhs;
1272      enum cpp_ttype op;
1273 {
1274   cpp_num result;
1275   size_t precision = CPP_OPTION (pfile, precision);
1276   bool gte;
1277   size_t n;
1278
1279   switch (op)
1280     {
1281       /* Shifts.  */
1282     case CPP_LSHIFT:
1283     case CPP_RSHIFT:
1284       if (!rhs.unsignedp && !num_positive (rhs, precision))
1285         {
1286           /* A negative shift is a positive shift the other way.  */
1287           if (op == CPP_LSHIFT)
1288             op = CPP_RSHIFT;
1289           else
1290             op = CPP_LSHIFT;
1291           rhs = num_negate (rhs, precision);
1292         }
1293       if (rhs.high)
1294         n = ~0;                 /* Maximal.  */
1295       else
1296         n = rhs.low;
1297       if (op == CPP_LSHIFT)
1298         lhs = num_lshift (lhs, precision, n);
1299       else
1300         lhs = num_rshift (lhs, precision, n);
1301       break;
1302
1303       /* Min / Max.  */
1304     case CPP_MIN:
1305     case CPP_MAX:
1306       {
1307         bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1308
1309         gte = num_greater_eq (lhs, rhs, precision);
1310         if (op == CPP_MIN)
1311           gte = !gte;
1312         if (!gte)
1313           lhs = rhs;
1314         lhs.unsignedp = unsignedp;
1315       }
1316       break;
1317
1318       /* Arithmetic.  */
1319     case CPP_MINUS:
1320       rhs = num_negate (rhs, precision);
1321     case CPP_PLUS:
1322       result.low = lhs.low + rhs.low;
1323       result.high = lhs.high + rhs.high;
1324       if (result.low < lhs.low)
1325         result.high++;
1326
1327       result = num_trim (result, precision);
1328       result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1329       if (result.unsignedp)
1330         result.overflow = false;
1331       else
1332         {
1333           bool lhsp = num_positive (lhs, precision);
1334           result.overflow = (lhsp == num_positive (rhs, precision)
1335                              && lhsp != num_positive (result, precision));
1336         }
1337       return result;
1338
1339       /* Comma.  */
1340     default: /* case CPP_COMMA: */
1341       if (CPP_PEDANTIC (pfile))
1342         cpp_error (pfile, DL_PEDWARN,
1343                    "comma operator in operand of #if");
1344       lhs = rhs;
1345       break;
1346     }
1347
1348   return lhs;
1349 }
1350
1351 /* Multiplies two unsigned cpp_num_parts to give a cpp_num.  This
1352    cannot overflow.  */
1353 static cpp_num
1354 num_part_mul (lhs, rhs)
1355      cpp_num_part lhs, rhs;
1356 {
1357   cpp_num result;
1358   cpp_num_part middle[2], temp;
1359
1360   result.low = LOW_PART (lhs) * LOW_PART (rhs);
1361   result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1362
1363   middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1364   middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1365
1366   temp = result.low;
1367   result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1368   if (result.low < temp)
1369     result.high++;
1370
1371   temp = result.low;
1372   result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1373   if (result.low < temp)
1374     result.high++;
1375
1376   result.high += HIGH_PART (middle[0]);
1377   result.high += HIGH_PART (middle[1]);
1378
1379   return result;
1380 }
1381
1382 /* Multiply two preprocessing numbers.  */
1383 static cpp_num
1384 num_mul (pfile, lhs, rhs, op)
1385      cpp_reader *pfile;
1386      cpp_num lhs, rhs;
1387      enum cpp_ttype op ATTRIBUTE_UNUSED;
1388 {
1389   cpp_num result, temp;
1390   bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1391   bool overflow, negate = false;
1392   size_t precision = CPP_OPTION (pfile, precision);
1393
1394   /* Prepare for unsigned multiplication.  */
1395   if (!unsignedp)
1396     {
1397       if (!num_positive (lhs, precision))
1398         negate = !negate, lhs = num_negate (lhs, precision);
1399       if (!num_positive (rhs, precision))
1400         negate = !negate, rhs = num_negate (rhs, precision);
1401     }
1402
1403   overflow = lhs.high && rhs.high;
1404   result = num_part_mul (lhs.low, rhs.low);
1405
1406   temp = num_part_mul (lhs.high, rhs.low);
1407   result.high += temp.low;
1408   if (temp.high)
1409     overflow = true;
1410
1411   temp = num_part_mul (lhs.low, rhs.high);
1412   result.high += temp.low;
1413   if (temp.high)
1414     overflow = true;
1415
1416   temp.low = result.low, temp.high = result.high;
1417   result = num_trim (result, precision);
1418   if (!num_eq (result, temp))
1419     overflow = true;
1420
1421   if (negate)
1422     result = num_negate (result, precision);
1423
1424   if (unsignedp)
1425     result.overflow = false;
1426   else
1427     result.overflow = overflow || (num_positive (result, precision) ^ !negate
1428                                    && !num_zerop (result));
1429   result.unsignedp = unsignedp;
1430
1431   return result;
1432 }
1433
1434 /* Divide two preprocessing numbers, returning the answer or the
1435    remainder depending upon OP.  */
1436 static cpp_num
1437 num_div_op (pfile, lhs, rhs, op)
1438      cpp_reader *pfile;
1439      cpp_num lhs, rhs;
1440      enum cpp_ttype op;
1441 {
1442   cpp_num result, sub;
1443   cpp_num_part mask;
1444   bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1445   bool negate = false, lhs_neg = false;
1446   size_t i, precision = CPP_OPTION (pfile, precision);
1447
1448   /* Prepare for unsigned division.  */
1449   if (!unsignedp)
1450     {
1451       if (!num_positive (lhs, precision))
1452         negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1453       if (!num_positive (rhs, precision))
1454         negate = !negate, rhs = num_negate (rhs, precision);
1455     }
1456
1457   /* Find the high bit.  */
1458   if (rhs.high)
1459     {
1460       i = precision - 1;
1461       mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1462       for (; ; i--, mask >>= 1)
1463         if (rhs.high & mask)
1464           break;
1465     }
1466   else if (rhs.low)
1467     {
1468       if (precision > PART_PRECISION)
1469         i = precision - PART_PRECISION - 1;
1470       else
1471         i = precision - 1;
1472       mask = (cpp_num_part) 1 << i;
1473       for (; ; i--, mask >>= 1)
1474         if (rhs.low & mask)
1475           break;
1476     }
1477   else
1478     {
1479       cpp_error (pfile, DL_ERROR, "division by zero in #if");
1480       return lhs;
1481     }
1482
1483   /* First non-zero bit of RHS is bit I.  Do naive division by
1484      shifting the RHS fully left, and subtracting from LHS if LHS is
1485      at least as big, and then repeating but with one less shift.
1486      This is not very efficient, but is easy to understand.  */
1487
1488   rhs.unsignedp = true;
1489   lhs.unsignedp = true;
1490   i = precision - i - 1;
1491   sub = num_lshift (rhs, precision, i);
1492
1493   result.high = result.low = 0;
1494   for (;;)
1495     {
1496       if (num_greater_eq (lhs, sub, precision))
1497         {
1498           lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1499           if (i >= PART_PRECISION)
1500             result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1501           else
1502             result.low |= (cpp_num_part) 1 << i;
1503         }
1504       if (i-- == 0)
1505         break;
1506       sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1507       sub.high >>= 1;
1508     }
1509
1510   /* We divide so that the remainder has the sign of the LHS.  */
1511   if (op == CPP_DIV)
1512     {
1513       result.unsignedp = unsignedp;
1514       if (unsignedp)
1515         result.overflow = false;
1516       else
1517         {
1518           if (negate)
1519             result = num_negate (result, precision);
1520           result.overflow = num_positive (result, precision) ^ !negate;
1521         }
1522
1523       return result;
1524     }
1525
1526   /* CPP_MOD.  */
1527   lhs.unsignedp = unsignedp;
1528   lhs.overflow = false;
1529   if (lhs_neg)
1530     lhs = num_negate (lhs, precision);
1531
1532   return lhs;
1533 }