gcse.c (do_local_cprop): Do not extend lifetimes of registers set by do_local_cprop.
[official-gcc.git] / gcc / cppexp.c
blobb66fea925e788a05723b58d0052be0de59864d2a
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.
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.
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.
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. */
21 #include "config.h"
22 #include "system.h"
23 #include "cpplib.h"
24 #include "cpphash.h"
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))
31 struct op
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;
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 PARAMS ((cpp_num, size_t));
42 static bool num_greater_eq PARAMS ((cpp_num, cpp_num, size_t));
43 static cpp_num num_trim PARAMS ((cpp_num, size_t));
44 static cpp_num num_part_mul PARAMS ((cpp_num_part, cpp_num_part));
46 static cpp_num num_unary_op PARAMS ((cpp_reader *, cpp_num, enum cpp_ttype));
47 static cpp_num num_binary_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
48 enum cpp_ttype));
49 static cpp_num num_negate PARAMS ((cpp_num, size_t));
50 static cpp_num num_bitwise_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
51 enum cpp_ttype));
52 static cpp_num num_inequality_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
53 enum cpp_ttype));
54 static cpp_num num_equality_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
55 enum cpp_ttype));
56 static cpp_num num_mul PARAMS ((cpp_reader *, cpp_num, cpp_num));
57 static cpp_num num_div_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
58 enum cpp_ttype));
59 static cpp_num num_lshift PARAMS ((cpp_num, size_t, size_t));
60 static cpp_num num_rshift PARAMS ((cpp_num, size_t, size_t));
62 static cpp_num append_digit PARAMS ((cpp_num, int, int, size_t));
63 static cpp_num parse_defined PARAMS ((cpp_reader *));
64 static cpp_num eval_token PARAMS ((cpp_reader *, const cpp_token *));
65 static struct op *reduce PARAMS ((cpp_reader *, struct op *, enum cpp_ttype));
66 static unsigned int interpret_float_suffix PARAMS ((const uchar *, size_t));
67 static unsigned int interpret_int_suffix PARAMS ((const uchar *, size_t));
68 static void check_promotion PARAMS ((cpp_reader *, const struct op *));
70 /* Token type abuse to create unary plus and minus operators. */
71 #define CPP_UPLUS (CPP_LAST_CPP_OP + 1)
72 #define CPP_UMINUS (CPP_LAST_CPP_OP + 2)
74 /* With -O2, gcc appears to produce nice code, moving the error
75 message load and subsequent jump completely out of the main path. */
76 #define SYNTAX_ERROR(msgid) \
77 do { cpp_error (pfile, DL_ERROR, msgid); goto syntax_error; } while(0)
78 #define SYNTAX_ERROR2(msgid, arg) \
79 do { cpp_error (pfile, DL_ERROR, msgid, arg); goto syntax_error; } while(0)
81 /* Subroutine of cpp_classify_number. S points to a float suffix of
82 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
83 flag vector describing the suffix. */
84 static unsigned int
85 interpret_float_suffix (s, len)
86 const uchar *s;
87 size_t len;
89 size_t f = 0, l = 0, i = 0;
91 while (len--)
92 switch (s[len])
94 case 'f': case 'F': f++; break;
95 case 'l': case 'L': l++; break;
96 case 'i': case 'I':
97 case 'j': case 'J': i++; break;
98 default:
99 return 0;
102 if (f + l > 1 || i > 1)
103 return 0;
105 return ((i ? CPP_N_IMAGINARY : 0)
106 | (f ? CPP_N_SMALL :
107 l ? CPP_N_LARGE : CPP_N_MEDIUM));
110 /* Subroutine of cpp_classify_number. S points to an integer suffix
111 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
112 flag vector describing the suffix. */
113 static unsigned int
114 interpret_int_suffix (s, len)
115 const uchar *s;
116 size_t len;
118 size_t u, l, i;
120 u = l = i = 0;
122 while (len--)
123 switch (s[len])
125 case 'u': case 'U': u++; break;
126 case 'i': case 'I':
127 case 'j': case 'J': i++; break;
128 case 'l': case 'L': l++;
129 /* If there are two Ls, they must be adjacent and the same case. */
130 if (l == 2 && s[len] != s[len + 1])
131 return 0;
132 break;
133 default:
134 return 0;
137 if (l > 2 || u > 1 || i > 1)
138 return 0;
140 return ((i ? CPP_N_IMAGINARY : 0)
141 | (u ? CPP_N_UNSIGNED : 0)
142 | ((l == 0) ? CPP_N_SMALL
143 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
146 /* Categorize numeric constants according to their field (integer,
147 floating point, or invalid), radix (decimal, octal, hexadecimal),
148 and type suffixes. */
149 unsigned int
150 cpp_classify_number (pfile, token)
151 cpp_reader *pfile;
152 const cpp_token *token;
154 const uchar *str = token->val.str.text;
155 const uchar *limit;
156 unsigned int max_digit, result, radix;
157 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
159 /* If the lexer has done its job, length one can only be a single
160 digit. Fast-path this very common case. */
161 if (token->val.str.len == 1)
162 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
164 limit = str + token->val.str.len;
165 float_flag = NOT_FLOAT;
166 max_digit = 0;
167 radix = 10;
169 /* First, interpret the radix. */
170 if (*str == '0')
172 radix = 8;
173 str++;
175 /* Require at least one hex digit to classify it as hex. */
176 if ((*str == 'x' || *str == 'X') && ISXDIGIT (str[1]))
178 radix = 16;
179 str++;
183 /* Now scan for a well-formed integer or float. */
184 for (;;)
186 unsigned int c = *str++;
188 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
190 c = hex_value (c);
191 if (c > max_digit)
192 max_digit = c;
194 else if (c == '.')
196 if (float_flag == NOT_FLOAT)
197 float_flag = AFTER_POINT;
198 else
199 SYNTAX_ERROR ("too many decimal points in number");
201 else if ((radix <= 10 && (c == 'e' || c == 'E'))
202 || (radix == 16 && (c == 'p' || c == 'P')))
204 float_flag = AFTER_EXPON;
205 break;
207 else
209 /* Start of suffix. */
210 str--;
211 break;
215 if (float_flag != NOT_FLOAT && radix == 8)
216 radix = 10;
218 if (max_digit >= radix)
219 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
221 if (float_flag != NOT_FLOAT)
223 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
224 cpp_error (pfile, DL_PEDWARN,
225 "use of C99 hexadecimal floating constant");
227 if (float_flag == AFTER_EXPON)
229 if (*str == '+' || *str == '-')
230 str++;
232 /* Exponent is decimal, even if string is a hex float. */
233 if (!ISDIGIT (*str))
234 SYNTAX_ERROR ("exponent has no digits");
237 str++;
238 while (ISDIGIT (*str));
240 else if (radix == 16)
241 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
243 result = interpret_float_suffix (str, limit - str);
244 if (result == 0)
246 cpp_error (pfile, DL_ERROR,
247 "invalid suffix \"%.*s\" on floating constant",
248 (int) (limit - str), str);
249 return CPP_N_INVALID;
252 /* Traditional C didn't accept any floating suffixes. */
253 if (limit != str
254 && CPP_WTRADITIONAL (pfile)
255 && ! cpp_sys_macro_p (pfile))
256 cpp_error (pfile, DL_WARNING,
257 "traditional C rejects the \"%.*s\" suffix",
258 (int) (limit - str), str);
260 result |= CPP_N_FLOATING;
262 else
264 result = interpret_int_suffix (str, limit - str);
265 if (result == 0)
267 cpp_error (pfile, DL_ERROR,
268 "invalid suffix \"%.*s\" on integer constant",
269 (int) (limit - str), str);
270 return CPP_N_INVALID;
273 /* Traditional C only accepted the 'L' suffix. */
274 if (result != CPP_N_SMALL && result != CPP_N_MEDIUM
275 && CPP_WTRADITIONAL (pfile)
276 && ! cpp_sys_macro_p (pfile))
277 cpp_error (pfile, DL_WARNING,
278 "traditional C rejects the \"%.*s\" suffix",
279 (int) (limit - str), str);
281 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
282 && ! CPP_OPTION (pfile, c99)
283 && CPP_OPTION (pfile, warn_long_long))
284 cpp_error (pfile, DL_PEDWARN, "use of C99 long long integer constant");
286 result |= CPP_N_INTEGER;
289 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
290 cpp_error (pfile, DL_PEDWARN, "imaginary constants are a GCC extension");
292 if (radix == 10)
293 result |= CPP_N_DECIMAL;
294 else if (radix == 16)
295 result |= CPP_N_HEX;
296 else
297 result |= CPP_N_OCTAL;
299 return result;
301 syntax_error:
302 return CPP_N_INVALID;
305 /* cpp_interpret_integer converts an integer constant into a cpp_num,
306 of precision options->precision.
308 We do not provide any interface for decimal->float conversion,
309 because the preprocessor doesn't need it and the floating point
310 handling in GCC proper is too ugly to speak of. */
311 cpp_num
312 cpp_interpret_integer (pfile, token, type)
313 cpp_reader *pfile;
314 const cpp_token *token;
315 unsigned int type;
317 const uchar *p, *end;
318 cpp_num result;
320 result.low = 0;
321 result.high = 0;
322 result.unsignedp = !!(type & CPP_N_UNSIGNED);
323 result.overflow = false;
325 p = token->val.str.text;
326 end = p + token->val.str.len;
328 /* Common case of a single digit. */
329 if (token->val.str.len == 1)
330 result.low = p[0] - '0';
331 else
333 cpp_num_part max;
334 size_t precision = CPP_OPTION (pfile, precision);
335 unsigned int base = 10, c = 0;
336 bool overflow = false;
338 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
340 base = 8;
341 p++;
343 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
345 base = 16;
346 p += 2;
349 /* We can add a digit to numbers strictly less than this without
350 needing the precision and slowness of double integers. */
351 max = ~(cpp_num_part) 0;
352 if (precision < PART_PRECISION)
353 max >>= PART_PRECISION - precision;
354 max = (max - base + 1) / base + 1;
356 for (; p < end; p++)
358 c = *p;
360 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
361 c = hex_value (c);
362 else
363 break;
365 /* Strict inequality for when max is set to zero. */
366 if (result.low < max)
367 result.low = result.low * base + c;
368 else
370 result = append_digit (result, c, base, precision);
371 overflow |= result.overflow;
372 max = 0;
376 if (overflow)
377 cpp_error (pfile, DL_PEDWARN,
378 "integer constant is too large for its type");
379 /* If too big to be signed, consider it unsigned. Only warn for
380 decimal numbers. Traditional numbers were always signed (but
381 we still honour an explicit U suffix); but we only have
382 traditional semantics in directives. */
383 else if (!result.unsignedp
384 && !(CPP_OPTION (pfile, traditional)
385 && pfile->state.in_directive)
386 && !num_positive (result, precision))
388 if (base == 10)
389 cpp_error (pfile, DL_WARNING,
390 "integer constant is so large that it is unsigned");
391 result.unsignedp = true;
395 return result;
398 /* Append DIGIT to NUM, a number of PRECISION bits being read in base
399 BASE. */
400 static cpp_num
401 append_digit (num, digit, base, precision)
402 cpp_num num;
403 int digit, base;
404 size_t precision;
406 cpp_num result;
407 unsigned int shift = 3 + (base == 16);
408 bool overflow;
409 cpp_num_part add_high, add_low;
411 /* Multiply by 8 or 16. Catching this overflow here means we don't
412 need to worry about add_high overflowing. */
413 overflow = !!(num.high >> (PART_PRECISION - shift));
414 result.high = num.high << shift;
415 result.low = num.low << shift;
416 result.high |= num.low >> (PART_PRECISION - shift);
418 if (base == 10)
420 add_low = num.low << 1;
421 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
423 else
424 add_high = add_low = 0;
426 if (add_low + digit < add_low)
427 add_high++;
428 add_low += digit;
430 if (result.low + add_low < result.low)
431 add_high++;
432 if (result.high + add_high < result.high)
433 overflow = true;
435 result.low += add_low;
436 result.high += add_high;
438 /* The above code catches overflow of a cpp_num type. This catches
439 overflow of the (possibly shorter) target precision. */
440 num.low = result.low;
441 num.high = result.high;
442 result = num_trim (result, precision);
443 if (!num_eq (result, num))
444 overflow = true;
446 result.unsignedp = num.unsignedp;
447 result.overflow = overflow;
448 return result;
451 /* Handle meeting "defined" in a preprocessor expression. */
452 static cpp_num
453 parse_defined (pfile)
454 cpp_reader *pfile;
456 cpp_num result;
457 int paren = 0;
458 cpp_hashnode *node = 0;
459 const cpp_token *token;
460 cpp_context *initial_context = pfile->context;
462 /* Don't expand macros. */
463 pfile->state.prevent_expansion++;
465 token = cpp_get_token (pfile);
466 if (token->type == CPP_OPEN_PAREN)
468 paren = 1;
469 token = cpp_get_token (pfile);
472 if (token->type == CPP_NAME)
474 node = token->val.node;
475 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
477 cpp_error (pfile, DL_ERROR, "missing ')' after \"defined\"");
478 node = 0;
481 else
483 cpp_error (pfile, DL_ERROR,
484 "operator \"defined\" requires an identifier");
485 if (token->flags & NAMED_OP)
487 cpp_token op;
489 op.flags = 0;
490 op.type = token->type;
491 cpp_error (pfile, DL_ERROR,
492 "(\"%s\" is an alternative token for \"%s\" in C++)",
493 cpp_token_as_text (pfile, token),
494 cpp_token_as_text (pfile, &op));
498 if (node)
500 if (pfile->context != initial_context)
501 cpp_error (pfile, DL_WARNING,
502 "this use of \"defined\" may not be portable");
504 /* A possible controlling macro of the form #if !defined ().
505 _cpp_parse_expr checks there was no other junk on the line. */
506 pfile->mi_ind_cmacro = node;
509 pfile->state.prevent_expansion--;
511 result.unsignedp = false;
512 result.high = 0;
513 result.overflow = false;
514 result.low = node && node->type == NT_MACRO;
515 return result;
518 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
519 number or character constant, or the result of the "defined" or "#"
520 operators). */
521 static cpp_num
522 eval_token (pfile, token)
523 cpp_reader *pfile;
524 const cpp_token *token;
526 cpp_num result;
527 unsigned int temp;
528 int unsignedp = 0;
530 switch (token->type)
532 case CPP_NUMBER:
533 temp = cpp_classify_number (pfile, token);
534 switch (temp & CPP_N_CATEGORY)
536 case CPP_N_FLOATING:
537 cpp_error (pfile, DL_ERROR,
538 "floating constant in preprocessor expression");
539 break;
540 case CPP_N_INTEGER:
541 if (!(temp & CPP_N_IMAGINARY))
542 return cpp_interpret_integer (pfile, token, temp);
543 cpp_error (pfile, DL_ERROR,
544 "imaginary number in preprocessor expression");
545 break;
547 case CPP_N_INVALID:
548 /* Error already issued. */
549 break;
551 result.high = result.low = 0;
552 break;
554 case CPP_WCHAR:
555 case CPP_CHAR:
557 cppchar_t cc = cpp_interpret_charconst (pfile, token,
558 &temp, &unsignedp);
560 result.high = 0;
561 result.low = cc;
562 /* Sign-extend the result if necessary. */
563 if (!unsignedp && (cppchar_signed_t) cc < 0)
565 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
566 result.low |= ~(~(cpp_num_part) 0
567 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
568 result.high = ~(cpp_num_part) 0;
569 result = num_trim (result, CPP_OPTION (pfile, precision));
572 break;
574 case CPP_NAME:
575 if (token->val.node == pfile->spec_nodes.n_defined)
576 return parse_defined (pfile);
577 else if (CPP_OPTION (pfile, cplusplus)
578 && (token->val.node == pfile->spec_nodes.n_true
579 || token->val.node == pfile->spec_nodes.n_false))
581 result.high = 0;
582 result.low = (token->val.node == pfile->spec_nodes.n_true);
584 /* Warn about use of true or false in #if when pedantic
585 and stdbool.h has not been included. */
586 if (CPP_PEDANTIC (pfile)
587 && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
588 cpp_error (pfile, DL_PEDWARN,
589 "ISO C++ does not permit \"%s\" in #if",
590 NODE_NAME (token->val.node));
592 else
594 result.high = 0;
595 result.low = 0;
596 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
597 cpp_error (pfile, DL_WARNING, "\"%s\" is not defined",
598 NODE_NAME (token->val.node));
600 break;
602 default: /* CPP_HASH */
603 _cpp_test_assertion (pfile, &temp);
604 result.high = 0;
605 result.low = temp;
608 result.unsignedp = !!unsignedp;
609 result.overflow = false;
610 return result;
613 /* Operator precedence and flags table.
615 After an operator is returned from the lexer, if it has priority less
616 than the operator on the top of the stack, we reduce the stack by one
617 operator and repeat the test. Since equal priorities do not reduce,
618 this is naturally right-associative.
620 We handle left-associative operators by decrementing the priority of
621 just-lexed operators by one, but retaining the priority of operators
622 already on the stack.
624 The remaining cases are '(' and ')'. We handle '(' by skipping the
625 reduction phase completely. ')' is given lower priority than
626 everything else, including '(', effectively forcing a reduction of the
627 parenthesised expression. If there is a matching '(', the routine
628 reduce() exits immediately. If the normal exit route sees a ')', then
629 there cannot have been a matching '(' and an error message is output.
631 The parser assumes all shifted operators require a left operand unless
632 the flag NO_L_OPERAND is set. These semantics are automatic; any
633 extra semantics need to be handled with operator-specific code. */
635 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
636 operand changes because of integer promotions. */
637 #define NO_L_OPERAND (1 << 0)
638 #define LEFT_ASSOC (1 << 1)
639 #define CHECK_PROMOTION (1 << 2)
641 /* Operator to priority map. Must be in the same order as the first
642 N entries of enum cpp_ttype. */
643 static const struct operator
645 uchar prio;
646 uchar flags;
647 } optab[] =
649 /* EQ */ {0, 0}, /* Shouldn't happen. */
650 /* NOT */ {16, NO_L_OPERAND},
651 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
652 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
653 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
654 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
655 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
656 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
657 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
658 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
659 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
660 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
661 /* RSHIFT */ {13, LEFT_ASSOC},
662 /* LSHIFT */ {13, LEFT_ASSOC},
664 /* MIN */ {10, LEFT_ASSOC | CHECK_PROMOTION},
665 /* MAX */ {10, LEFT_ASSOC | CHECK_PROMOTION},
667 /* COMPL */ {16, NO_L_OPERAND},
668 /* AND_AND */ {6, LEFT_ASSOC},
669 /* OR_OR */ {5, LEFT_ASSOC},
670 /* QUERY */ {3, 0},
671 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
672 /* COMMA */ {2, LEFT_ASSOC},
673 /* OPEN_PAREN */ {1, NO_L_OPERAND},
674 /* CLOSE_PAREN */ {0, 0},
675 /* EOF */ {0, 0},
676 /* EQ_EQ */ {11, LEFT_ASSOC},
677 /* NOT_EQ */ {11, LEFT_ASSOC},
678 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
679 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
680 /* UPLUS */ {16, NO_L_OPERAND},
681 /* UMINUS */ {16, NO_L_OPERAND}
684 /* Parse and evaluate a C expression, reading from PFILE.
685 Returns the truth value of the expression.
687 The implementation is an operator precedence parser, i.e. a
688 bottom-up parser, using a stack for not-yet-reduced tokens.
690 The stack base is op_stack, and the current stack pointer is 'top'.
691 There is a stack element for each operator (only), and the most
692 recently pushed operator is 'top->op'. An operand (value) is
693 stored in the 'value' field of the stack element of the operator
694 that precedes it. */
695 bool
696 _cpp_parse_expr (pfile)
697 cpp_reader *pfile;
699 struct op *top = pfile->op_stack;
700 unsigned int lex_count;
701 bool saw_leading_not, want_value = true;
703 pfile->state.skip_eval = 0;
705 /* Set up detection of #if ! defined(). */
706 pfile->mi_ind_cmacro = 0;
707 saw_leading_not = false;
708 lex_count = 0;
710 /* Lowest priority operator prevents further reductions. */
711 top->op = CPP_EOF;
713 for (;;)
715 struct op op;
717 lex_count++;
718 op.token = cpp_get_token (pfile);
719 op.op = op.token->type;
721 switch (op.op)
723 /* These tokens convert into values. */
724 case CPP_NUMBER:
725 case CPP_CHAR:
726 case CPP_WCHAR:
727 case CPP_NAME:
728 case CPP_HASH:
729 if (!want_value)
730 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
731 cpp_token_as_text (pfile, op.token));
732 want_value = false;
733 top->value = eval_token (pfile, op.token);
734 continue;
736 case CPP_NOT:
737 saw_leading_not = lex_count == 1;
738 break;
739 case CPP_PLUS:
740 if (want_value)
741 op.op = CPP_UPLUS;
742 break;
743 case CPP_MINUS:
744 if (want_value)
745 op.op = CPP_UMINUS;
746 break;
747 case CPP_OTHER:
748 if (ISGRAPH (op.token->val.c))
749 SYNTAX_ERROR2 ("invalid character '%c' in #if", op.token->val.c);
750 else
751 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if",
752 op.token->val.c);
754 default:
755 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
756 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
757 cpp_token_as_text (pfile, op.token));
758 break;
761 /* Check we have a value or operator as appropriate. */
762 if (optab[op.op].flags & NO_L_OPERAND)
764 if (!want_value)
765 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
766 cpp_token_as_text (pfile, op.token));
768 else if (want_value)
770 /* Ordering here is subtle and intended to favour the
771 missing parenthesis diagnostics over alternatives. */
772 if (op.op == CPP_CLOSE_PAREN)
774 if (top->op == CPP_OPEN_PAREN)
775 SYNTAX_ERROR ("void expression between '(' and ')'");
777 else if (top->op == CPP_EOF)
778 SYNTAX_ERROR ("#if with no expression");
779 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
780 SYNTAX_ERROR2 ("operator '%s' has no right operand",
781 cpp_token_as_text (pfile, top->token));
784 top = reduce (pfile, top, op.op);
785 if (!top)
786 goto syntax_error;
788 if (op.op == CPP_EOF)
789 break;
791 switch (op.op)
793 case CPP_CLOSE_PAREN:
794 continue;
795 case CPP_OR_OR:
796 if (!num_zerop (top->value))
797 pfile->state.skip_eval++;
798 break;
799 case CPP_AND_AND:
800 case CPP_QUERY:
801 if (num_zerop (top->value))
802 pfile->state.skip_eval++;
803 break;
804 case CPP_COLON:
805 if (top->op != CPP_QUERY)
806 SYNTAX_ERROR (" ':' without preceding '?'");
807 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
808 pfile->state.skip_eval++;
809 else
810 pfile->state.skip_eval--;
811 default:
812 break;
815 want_value = true;
817 /* Check for and handle stack overflow. */
818 if (++top == pfile->op_limit)
819 top = _cpp_expand_op_stack (pfile);
821 top->op = op.op;
822 top->token = op.token;
825 /* The controlling macro expression is only valid if we called lex 3
826 times: <!> <defined expression> and <EOF>. push_conditional ()
827 checks that we are at top-of-file. */
828 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
829 pfile->mi_ind_cmacro = 0;
831 if (top != pfile->op_stack)
833 cpp_error (pfile, DL_ICE, "unbalanced stack in #if");
834 syntax_error:
835 return false; /* Return false on syntax error. */
838 return !num_zerop (top->value);
841 /* Reduce the operator / value stack if possible, in preparation for
842 pushing operator OP. Returns NULL on error, otherwise the top of
843 the stack. */
844 static struct op *
845 reduce (pfile, top, op)
846 cpp_reader *pfile;
847 struct op *top;
848 enum cpp_ttype op;
850 unsigned int prio;
852 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
854 bad_op:
855 cpp_error (pfile, DL_ICE, "impossible operator '%u'", top->op);
856 return 0;
859 if (op == CPP_OPEN_PAREN)
860 return top;
862 /* Decrement the priority of left-associative operators to force a
863 reduction with operators of otherwise equal priority. */
864 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
865 while (prio < optab[top->op].prio)
867 if (CPP_OPTION (pfile, warn_num_sign_change)
868 && optab[top->op].flags & CHECK_PROMOTION)
869 check_promotion (pfile, top);
871 switch (top->op)
873 case CPP_UPLUS:
874 case CPP_UMINUS:
875 case CPP_NOT:
876 case CPP_COMPL:
877 top[-1].value = num_unary_op (pfile, top->value, top->op);
878 break;
880 case CPP_PLUS:
881 case CPP_MINUS:
882 case CPP_RSHIFT:
883 case CPP_LSHIFT:
884 case CPP_MIN:
885 case CPP_MAX:
886 case CPP_COMMA:
887 top[-1].value = num_binary_op (pfile, top[-1].value,
888 top->value, top->op);
889 break;
891 case CPP_GREATER:
892 case CPP_LESS:
893 case CPP_GREATER_EQ:
894 case CPP_LESS_EQ:
895 top[-1].value
896 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
897 break;
899 case CPP_EQ_EQ:
900 case CPP_NOT_EQ:
901 top[-1].value
902 = num_equality_op (pfile, top[-1].value, top->value, top->op);
903 break;
905 case CPP_AND:
906 case CPP_OR:
907 case CPP_XOR:
908 top[-1].value
909 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
910 break;
912 case CPP_MULT:
913 top[-1].value = num_mul (pfile, top[-1].value, top->value);
914 break;
916 case CPP_DIV:
917 case CPP_MOD:
918 top[-1].value = num_div_op (pfile, top[-1].value,
919 top->value, top->op);
920 break;
922 case CPP_OR_OR:
923 top--;
924 if (!num_zerop (top->value))
925 pfile->state.skip_eval--;
926 top->value.low = (!num_zerop (top->value)
927 || !num_zerop (top[1].value));
928 top->value.high = 0;
929 top->value.unsignedp = false;
930 top->value.overflow = false;
931 continue;
933 case CPP_AND_AND:
934 top--;
935 if (num_zerop (top->value))
936 pfile->state.skip_eval--;
937 top->value.low = (!num_zerop (top->value)
938 && !num_zerop (top[1].value));
939 top->value.high = 0;
940 top->value.unsignedp = false;
941 top->value.overflow = false;
942 continue;
944 case CPP_OPEN_PAREN:
945 if (op != CPP_CLOSE_PAREN)
947 cpp_error (pfile, DL_ERROR, "missing ')' in expression");
948 return 0;
950 top--;
951 top->value = top[1].value;
952 return top;
954 case CPP_COLON:
955 top -= 2;
956 if (!num_zerop (top->value))
958 pfile->state.skip_eval--;
959 top->value = top[1].value;
961 else
962 top->value = top[2].value;
963 top->value.unsignedp = (top[1].value.unsignedp
964 || top[2].value.unsignedp);
965 continue;
967 case CPP_QUERY:
968 cpp_error (pfile, DL_ERROR, "'?' without following ':'");
969 return 0;
971 default:
972 goto bad_op;
975 top--;
976 if (top->value.overflow && !pfile->state.skip_eval)
977 cpp_error (pfile, DL_PEDWARN,
978 "integer overflow in preprocessor expression");
981 if (op == CPP_CLOSE_PAREN)
983 cpp_error (pfile, DL_ERROR, "missing '(' in expression");
984 return 0;
987 return top;
990 /* Returns the position of the old top of stack after expansion. */
991 struct op *
992 _cpp_expand_op_stack (pfile)
993 cpp_reader *pfile;
995 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
996 size_t new_size = old_size * 2 + 20;
998 pfile->op_stack = (struct op *) xrealloc (pfile->op_stack,
999 new_size * sizeof (struct op));
1000 pfile->op_limit = pfile->op_stack + new_size;
1002 return pfile->op_stack + old_size;
1005 /* Emits a warning if the effective sign of either operand of OP
1006 changes because of integer promotions. */
1007 static void
1008 check_promotion (pfile, op)
1009 cpp_reader *pfile;
1010 const struct op *op;
1012 if (op->value.unsignedp == op[-1].value.unsignedp)
1013 return;
1015 if (op->value.unsignedp)
1017 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1018 cpp_error (pfile, DL_WARNING,
1019 "the left operand of \"%s\" changes sign when promoted",
1020 cpp_token_as_text (pfile, op->token));
1022 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1023 cpp_error (pfile, DL_WARNING,
1024 "the right operand of \"%s\" changes sign when promoted",
1025 cpp_token_as_text (pfile, op->token));
1028 /* Clears the unused high order bits of the number pointed to by PNUM. */
1029 static cpp_num
1030 num_trim (num, precision)
1031 cpp_num num;
1032 size_t precision;
1034 if (precision > PART_PRECISION)
1036 precision -= PART_PRECISION;
1037 if (precision < PART_PRECISION)
1038 num.high &= ((cpp_num_part) 1 << precision) - 1;
1040 else
1042 if (precision < PART_PRECISION)
1043 num.low &= ((cpp_num_part) 1 << precision) - 1;
1044 num.high = 0;
1047 return num;
1050 /* True iff A (presumed signed) >= 0. */
1051 static bool
1052 num_positive (num, precision)
1053 cpp_num num;
1054 size_t precision;
1056 if (precision > PART_PRECISION)
1058 precision -= PART_PRECISION;
1059 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1062 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1065 /* Sign extend a number, with PRECISION significant bits and all
1066 others assumed clear, to fill out a cpp_num structure. */
1067 cpp_num
1068 cpp_num_sign_extend (num, precision)
1069 cpp_num num;
1070 size_t precision;
1072 if (!num.unsignedp)
1074 if (precision > PART_PRECISION)
1076 precision -= PART_PRECISION;
1077 if (precision < PART_PRECISION
1078 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1079 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1081 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1083 if (precision < PART_PRECISION)
1084 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1085 num.high = ~(cpp_num_part) 0;
1089 return num;
1092 /* Returns the negative of NUM. */
1093 static cpp_num
1094 num_negate (num, precision)
1095 cpp_num num;
1096 size_t precision;
1098 cpp_num copy;
1100 copy = num;
1101 num.high = ~num.high;
1102 num.low = ~num.low;
1103 if (++num.low == 0)
1104 num.high++;
1105 num = num_trim (num, precision);
1106 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1108 return num;
1111 /* Returns true if A >= B. */
1112 static bool
1113 num_greater_eq (pa, pb, precision)
1114 cpp_num pa, pb;
1115 size_t precision;
1117 bool unsignedp;
1119 unsignedp = pa.unsignedp || pb.unsignedp;
1121 if (!unsignedp)
1123 /* Both numbers have signed type. If they are of different
1124 sign, the answer is the sign of A. */
1125 unsignedp = num_positive (pa, precision);
1127 if (unsignedp != num_positive (pb, precision))
1128 return unsignedp;
1130 /* Otherwise we can do an unsigned comparison. */
1133 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1136 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1137 static cpp_num
1138 num_bitwise_op (pfile, lhs, rhs, op)
1139 cpp_reader *pfile ATTRIBUTE_UNUSED;
1140 cpp_num lhs, rhs;
1141 enum cpp_ttype op;
1143 lhs.overflow = false;
1144 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1146 /* As excess precision is zeroed, there is no need to num_trim () as
1147 these operations cannot introduce a set bit there. */
1148 if (op == CPP_AND)
1150 lhs.low &= rhs.low;
1151 lhs.high &= rhs.high;
1153 else if (op == CPP_OR)
1155 lhs.low |= rhs.low;
1156 lhs.high |= rhs.high;
1158 else
1160 lhs.low ^= rhs.low;
1161 lhs.high ^= rhs.high;
1164 return lhs;
1167 /* Returns LHS OP RHS, where OP is an inequality. */
1168 static cpp_num
1169 num_inequality_op (pfile, lhs, rhs, op)
1170 cpp_reader *pfile;
1171 cpp_num lhs, rhs;
1172 enum cpp_ttype op;
1174 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1176 if (op == CPP_GREATER_EQ)
1177 lhs.low = gte;
1178 else if (op == CPP_LESS)
1179 lhs.low = !gte;
1180 else if (op == CPP_GREATER)
1181 lhs.low = gte && !num_eq (lhs, rhs);
1182 else /* CPP_LESS_EQ. */
1183 lhs.low = !gte || num_eq (lhs, rhs);
1185 lhs.high = 0;
1186 lhs.overflow = false;
1187 lhs.unsignedp = false;
1188 return lhs;
1191 /* Returns LHS OP RHS, where OP is == or !=. */
1192 static cpp_num
1193 num_equality_op (pfile, lhs, rhs, op)
1194 cpp_reader *pfile ATTRIBUTE_UNUSED;
1195 cpp_num lhs, rhs;
1196 enum cpp_ttype op;
1198 /* Work around a 3.0.4 bug; see PR 6950. */
1199 bool eq = num_eq (lhs, rhs);
1200 if (op == CPP_NOT_EQ)
1201 eq = !eq;
1202 lhs.low = eq;
1203 lhs.high = 0;
1204 lhs.overflow = false;
1205 lhs.unsignedp = false;
1206 return lhs;
1209 /* Shift NUM, of width PRECISION, right by N bits. */
1210 static cpp_num
1211 num_rshift (num, precision, n)
1212 cpp_num num;
1213 size_t precision, n;
1215 cpp_num_part sign_mask;
1217 if (num.unsignedp || num_positive (num, precision))
1218 sign_mask = 0;
1219 else
1220 sign_mask = ~(cpp_num_part) 0;
1222 if (n >= precision)
1223 num.high = num.low = sign_mask;
1224 else
1226 /* Sign-extend. */
1227 if (precision < PART_PRECISION)
1228 num.high = sign_mask, num.low |= sign_mask << precision;
1229 else if (precision < 2 * PART_PRECISION)
1230 num.high |= sign_mask << (precision - PART_PRECISION);
1232 if (n >= PART_PRECISION)
1234 n -= PART_PRECISION;
1235 num.low = num.high;
1236 num.high = sign_mask;
1239 if (n)
1241 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1242 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1246 num = num_trim (num, precision);
1247 num.overflow = false;
1248 return num;
1251 /* Shift NUM, of width PRECISION, left by N bits. */
1252 static cpp_num
1253 num_lshift (num, precision, n)
1254 cpp_num num;
1255 size_t precision, n;
1257 if (n >= precision)
1259 num.overflow = !num.unsignedp && !num_zerop (num);
1260 num.high = num.low = 0;
1262 else
1264 cpp_num orig, maybe_orig;
1265 size_t m = n;
1267 orig = num;
1268 if (m >= PART_PRECISION)
1270 m -= PART_PRECISION;
1271 num.high = num.low;
1272 num.low = 0;
1274 if (m)
1276 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1277 num.low <<= m;
1279 num = num_trim (num, precision);
1281 if (num.unsignedp)
1282 num.overflow = false;
1283 else
1285 maybe_orig = num_rshift (num, precision, n);
1286 num.overflow = !num_eq (orig, maybe_orig);
1290 return num;
1293 /* The four unary operators: +, -, ! and ~. */
1294 static cpp_num
1295 num_unary_op (pfile, num, op)
1296 cpp_reader *pfile;
1297 cpp_num num;
1298 enum cpp_ttype op;
1300 switch (op)
1302 case CPP_UPLUS:
1303 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1304 cpp_error (pfile, DL_WARNING,
1305 "traditional C rejects the unary plus operator");
1306 num.overflow = false;
1307 break;
1309 case CPP_UMINUS:
1310 num = num_negate (num, CPP_OPTION (pfile, precision));
1311 break;
1313 case CPP_COMPL:
1314 num.high = ~num.high;
1315 num.low = ~num.low;
1316 num = num_trim (num, CPP_OPTION (pfile, precision));
1317 num.overflow = false;
1318 break;
1320 default: /* case CPP_NOT: */
1321 num.low = num_zerop (num);
1322 num.high = 0;
1323 num.overflow = false;
1324 num.unsignedp = false;
1325 break;
1328 return num;
1331 /* The various binary operators. */
1332 static cpp_num
1333 num_binary_op (pfile, lhs, rhs, op)
1334 cpp_reader *pfile;
1335 cpp_num lhs, rhs;
1336 enum cpp_ttype op;
1338 cpp_num result;
1339 size_t precision = CPP_OPTION (pfile, precision);
1340 bool gte;
1341 size_t n;
1343 switch (op)
1345 /* Shifts. */
1346 case CPP_LSHIFT:
1347 case CPP_RSHIFT:
1348 if (!rhs.unsignedp && !num_positive (rhs, precision))
1350 /* A negative shift is a positive shift the other way. */
1351 if (op == CPP_LSHIFT)
1352 op = CPP_RSHIFT;
1353 else
1354 op = CPP_LSHIFT;
1355 rhs = num_negate (rhs, precision);
1357 if (rhs.high)
1358 n = ~0; /* Maximal. */
1359 else
1360 n = rhs.low;
1361 if (op == CPP_LSHIFT)
1362 lhs = num_lshift (lhs, precision, n);
1363 else
1364 lhs = num_rshift (lhs, precision, n);
1365 break;
1367 /* Min / Max. */
1368 case CPP_MIN:
1369 case CPP_MAX:
1371 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1373 gte = num_greater_eq (lhs, rhs, precision);
1374 if (op == CPP_MIN)
1375 gte = !gte;
1376 if (!gte)
1377 lhs = rhs;
1378 lhs.unsignedp = unsignedp;
1380 break;
1382 /* Arithmetic. */
1383 case CPP_MINUS:
1384 rhs = num_negate (rhs, precision);
1385 case CPP_PLUS:
1386 result.low = lhs.low + rhs.low;
1387 result.high = lhs.high + rhs.high;
1388 if (result.low < lhs.low)
1389 result.high++;
1391 result = num_trim (result, precision);
1392 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1393 if (result.unsignedp)
1394 result.overflow = false;
1395 else
1397 bool lhsp = num_positive (lhs, precision);
1398 result.overflow = (lhsp == num_positive (rhs, precision)
1399 && lhsp != num_positive (result, precision));
1401 return result;
1403 /* Comma. */
1404 default: /* case CPP_COMMA: */
1405 if (CPP_PEDANTIC (pfile) && !pfile->state.skip_eval)
1406 cpp_error (pfile, DL_PEDWARN,
1407 "comma operator in operand of #if");
1408 lhs = rhs;
1409 break;
1412 return lhs;
1415 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1416 cannot overflow. */
1417 static cpp_num
1418 num_part_mul (lhs, rhs)
1419 cpp_num_part lhs, rhs;
1421 cpp_num result;
1422 cpp_num_part middle[2], temp;
1424 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1425 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1427 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1428 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1430 temp = result.low;
1431 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1432 if (result.low < temp)
1433 result.high++;
1435 temp = result.low;
1436 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1437 if (result.low < temp)
1438 result.high++;
1440 result.high += HIGH_PART (middle[0]);
1441 result.high += HIGH_PART (middle[1]);
1443 return result;
1446 /* Multiply two preprocessing numbers. */
1447 static cpp_num
1448 num_mul (pfile, lhs, rhs)
1449 cpp_reader *pfile;
1450 cpp_num lhs, rhs;
1452 cpp_num result, temp;
1453 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1454 bool overflow, negate = false;
1455 size_t precision = CPP_OPTION (pfile, precision);
1457 /* Prepare for unsigned multiplication. */
1458 if (!unsignedp)
1460 if (!num_positive (lhs, precision))
1461 negate = !negate, lhs = num_negate (lhs, precision);
1462 if (!num_positive (rhs, precision))
1463 negate = !negate, rhs = num_negate (rhs, precision);
1466 overflow = lhs.high && rhs.high;
1467 result = num_part_mul (lhs.low, rhs.low);
1469 temp = num_part_mul (lhs.high, rhs.low);
1470 result.high += temp.low;
1471 if (temp.high)
1472 overflow = true;
1474 temp = num_part_mul (lhs.low, rhs.high);
1475 result.high += temp.low;
1476 if (temp.high)
1477 overflow = true;
1479 temp.low = result.low, temp.high = result.high;
1480 result = num_trim (result, precision);
1481 if (!num_eq (result, temp))
1482 overflow = true;
1484 if (negate)
1485 result = num_negate (result, precision);
1487 if (unsignedp)
1488 result.overflow = false;
1489 else
1490 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1491 && !num_zerop (result));
1492 result.unsignedp = unsignedp;
1494 return result;
1497 /* Divide two preprocessing numbers, returning the answer or the
1498 remainder depending upon OP. */
1499 static cpp_num
1500 num_div_op (pfile, lhs, rhs, op)
1501 cpp_reader *pfile;
1502 cpp_num lhs, rhs;
1503 enum cpp_ttype op;
1505 cpp_num result, sub;
1506 cpp_num_part mask;
1507 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1508 bool negate = false, lhs_neg = false;
1509 size_t i, precision = CPP_OPTION (pfile, precision);
1511 /* Prepare for unsigned division. */
1512 if (!unsignedp)
1514 if (!num_positive (lhs, precision))
1515 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1516 if (!num_positive (rhs, precision))
1517 negate = !negate, rhs = num_negate (rhs, precision);
1520 /* Find the high bit. */
1521 if (rhs.high)
1523 i = precision - 1;
1524 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1525 for (; ; i--, mask >>= 1)
1526 if (rhs.high & mask)
1527 break;
1529 else if (rhs.low)
1531 if (precision > PART_PRECISION)
1532 i = precision - PART_PRECISION - 1;
1533 else
1534 i = precision - 1;
1535 mask = (cpp_num_part) 1 << i;
1536 for (; ; i--, mask >>= 1)
1537 if (rhs.low & mask)
1538 break;
1540 else
1542 if (!pfile->state.skip_eval)
1543 cpp_error (pfile, DL_ERROR, "division by zero in #if");
1544 return lhs;
1547 /* First non-zero bit of RHS is bit I. Do naive division by
1548 shifting the RHS fully left, and subtracting from LHS if LHS is
1549 at least as big, and then repeating but with one less shift.
1550 This is not very efficient, but is easy to understand. */
1552 rhs.unsignedp = true;
1553 lhs.unsignedp = true;
1554 i = precision - i - 1;
1555 sub = num_lshift (rhs, precision, i);
1557 result.high = result.low = 0;
1558 for (;;)
1560 if (num_greater_eq (lhs, sub, precision))
1562 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1563 if (i >= PART_PRECISION)
1564 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1565 else
1566 result.low |= (cpp_num_part) 1 << i;
1568 if (i-- == 0)
1569 break;
1570 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1571 sub.high >>= 1;
1574 /* We divide so that the remainder has the sign of the LHS. */
1575 if (op == CPP_DIV)
1577 result.unsignedp = unsignedp;
1578 if (unsignedp)
1579 result.overflow = false;
1580 else
1582 if (negate)
1583 result = num_negate (result, precision);
1584 result.overflow = num_positive (result, precision) ^ !negate;
1587 return result;
1590 /* CPP_MOD. */
1591 lhs.unsignedp = unsignedp;
1592 lhs.overflow = false;
1593 if (lhs_neg)
1594 lhs = num_negate (lhs, precision);
1596 return lhs;