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