OSDN Git Service

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