OSDN Git Service

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