OSDN Git Service

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