OSDN Git Service

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