OSDN Git Service

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