OSDN Git Service

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