OSDN Git Service

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