OSDN Git Service

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