OSDN Git Service

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