libcpp/
[official-gcc.git] / libcpp / expr.c
blob8401daee032f24cb655766cc4b92f9948c64ced7
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.
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, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "cpplib.h"
24 #include "internal.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 (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);
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);
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 *);
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))
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)
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)
85 size_t f = 0, l = 0, i = 0, d = 0;
87 while (len--)
88 switch (s[len])
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;
107 if (f + l > 1 || i > 1)
108 return 0;
110 /* Allow dd, df, dl suffixes for decimal float constants. */
111 if (d && ((d + f + l != 2) || i))
112 return 0;
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));
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)
126 size_t u, l, i;
128 u = l = i = 0;
130 while (len--)
131 switch (s[len])
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;
145 if (l > 2 || u > 1 || i > 1)
146 return 0;
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));
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)
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;
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;
170 limit = str + token->val.str.len;
171 float_flag = NOT_FLOAT;
172 max_digit = 0;
173 radix = 10;
175 /* First, interpret the radix. */
176 if (*str == '0')
178 radix = 8;
179 str++;
181 /* Require at least one hex digit to classify it as hex. */
182 if ((*str == 'x' || *str == 'X')
183 && (str[1] == '.' || ISXDIGIT (str[1])))
185 radix = 16;
186 str++;
190 /* Now scan for a well-formed integer or float. */
191 for (;;)
193 unsigned int c = *str++;
195 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
197 c = hex_value (c);
198 if (c > max_digit)
199 max_digit = c;
201 else if (c == '.')
203 if (float_flag == NOT_FLOAT)
204 float_flag = AFTER_POINT;
205 else
206 SYNTAX_ERROR ("too many decimal points in number");
208 else if ((radix <= 10 && (c == 'e' || c == 'E'))
209 || (radix == 16 && (c == 'p' || c == 'P')))
211 float_flag = AFTER_EXPON;
212 break;
214 else
216 /* Start of suffix. */
217 str--;
218 break;
222 if (float_flag != NOT_FLOAT && radix == 8)
223 radix = 10;
225 if (max_digit >= radix)
226 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
228 if (float_flag != NOT_FLOAT)
230 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
231 cpp_error (pfile, CPP_DL_PEDWARN,
232 "use of C99 hexadecimal floating constant");
234 if (float_flag == AFTER_EXPON)
236 if (*str == '+' || *str == '-')
237 str++;
239 /* Exponent is decimal, even if string is a hex float. */
240 if (!ISDIGIT (*str))
241 SYNTAX_ERROR ("exponent has no digits");
244 str++;
245 while (ISDIGIT (*str));
247 else if (radix == 16)
248 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
250 result = interpret_float_suffix (str, limit - str);
251 if (result == 0)
253 cpp_error (pfile, CPP_DL_ERROR,
254 "invalid suffix \"%.*s\" on floating constant",
255 (int) (limit - str), str);
256 return CPP_N_INVALID;
259 /* Traditional C didn't accept any floating suffixes. */
260 if (limit != str
261 && CPP_WTRADITIONAL (pfile)
262 && ! cpp_sys_macro_p (pfile))
263 cpp_error (pfile, CPP_DL_WARNING,
264 "traditional C rejects the \"%.*s\" suffix",
265 (int) (limit - str), str);
267 /* Radix must be 10 for decimal floats. */
268 if ((result & CPP_N_DFLOAT) && radix != 10)
270 cpp_error (pfile, CPP_DL_ERROR,
271 "invalid suffix \"%.*s\" with hexadecimal floating constant",
272 (int) (limit - str), str);
273 return CPP_N_INVALID;
276 result |= CPP_N_FLOATING;
278 else
280 result = interpret_int_suffix (str, limit - str);
281 if (result == 0)
283 cpp_error (pfile, CPP_DL_ERROR,
284 "invalid suffix \"%.*s\" on integer constant",
285 (int) (limit - str), str);
286 return CPP_N_INVALID;
289 /* Traditional C only accepted the 'L' suffix.
290 Suppress warning about 'LL' with -Wno-long-long. */
291 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
293 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
294 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
296 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
297 cpp_error (pfile, CPP_DL_WARNING,
298 "traditional C rejects the \"%.*s\" suffix",
299 (int) (limit - str), str);
302 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
303 && ! CPP_OPTION (pfile, c99)
304 && CPP_OPTION (pfile, warn_long_long))
305 cpp_error (pfile, CPP_DL_PEDWARN,
306 "use of C99 long long integer constant");
308 result |= CPP_N_INTEGER;
311 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
312 cpp_error (pfile, CPP_DL_PEDWARN,
313 "imaginary constants are a GCC extension");
315 if (radix == 10)
316 result |= CPP_N_DECIMAL;
317 else if (radix == 16)
318 result |= CPP_N_HEX;
319 else
320 result |= CPP_N_OCTAL;
322 return result;
324 syntax_error:
325 return CPP_N_INVALID;
328 /* cpp_interpret_integer converts an integer constant into a cpp_num,
329 of precision options->precision.
331 We do not provide any interface for decimal->float conversion,
332 because the preprocessor doesn't need it and we don't want to
333 drag in GCC's floating point emulator. */
334 cpp_num
335 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
336 unsigned int type)
338 const uchar *p, *end;
339 cpp_num result;
341 result.low = 0;
342 result.high = 0;
343 result.unsignedp = !!(type & CPP_N_UNSIGNED);
344 result.overflow = false;
346 p = token->val.str.text;
347 end = p + token->val.str.len;
349 /* Common case of a single digit. */
350 if (token->val.str.len == 1)
351 result.low = p[0] - '0';
352 else
354 cpp_num_part max;
355 size_t precision = CPP_OPTION (pfile, precision);
356 unsigned int base = 10, c = 0;
357 bool overflow = false;
359 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
361 base = 8;
362 p++;
364 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
366 base = 16;
367 p += 2;
370 /* We can add a digit to numbers strictly less than this without
371 needing the precision and slowness of double integers. */
372 max = ~(cpp_num_part) 0;
373 if (precision < PART_PRECISION)
374 max >>= PART_PRECISION - precision;
375 max = (max - base + 1) / base + 1;
377 for (; p < end; p++)
379 c = *p;
381 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
382 c = hex_value (c);
383 else
384 break;
386 /* Strict inequality for when max is set to zero. */
387 if (result.low < max)
388 result.low = result.low * base + c;
389 else
391 result = append_digit (result, c, base, precision);
392 overflow |= result.overflow;
393 max = 0;
397 if (overflow)
398 cpp_error (pfile, CPP_DL_PEDWARN,
399 "integer constant is too large for its type");
400 /* If too big to be signed, consider it unsigned. Only warn for
401 decimal numbers. Traditional numbers were always signed (but
402 we still honor an explicit U suffix); but we only have
403 traditional semantics in directives. */
404 else if (!result.unsignedp
405 && !(CPP_OPTION (pfile, traditional)
406 && pfile->state.in_directive)
407 && !num_positive (result, precision))
409 if (base == 10)
410 cpp_error (pfile, CPP_DL_WARNING,
411 "integer constant is so large that it is unsigned");
412 result.unsignedp = true;
416 return result;
419 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
420 static cpp_num
421 append_digit (cpp_num num, int digit, int base, size_t precision)
423 cpp_num result;
424 unsigned int shift = 3 + (base == 16);
425 bool overflow;
426 cpp_num_part add_high, add_low;
428 /* Multiply by 8 or 16. Catching this overflow here means we don't
429 need to worry about add_high overflowing. */
430 overflow = !!(num.high >> (PART_PRECISION - shift));
431 result.high = num.high << shift;
432 result.low = num.low << shift;
433 result.high |= num.low >> (PART_PRECISION - shift);
434 result.unsignedp = num.unsignedp;
436 if (base == 10)
438 add_low = num.low << 1;
439 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
441 else
442 add_high = add_low = 0;
444 if (add_low + digit < add_low)
445 add_high++;
446 add_low += digit;
448 if (result.low + add_low < result.low)
449 add_high++;
450 if (result.high + add_high < result.high)
451 overflow = true;
453 result.low += add_low;
454 result.high += add_high;
455 result.overflow = overflow;
457 /* The above code catches overflow of a cpp_num type. This catches
458 overflow of the (possibly shorter) target precision. */
459 num.low = result.low;
460 num.high = result.high;
461 result = num_trim (result, precision);
462 if (!num_eq (result, num))
463 result.overflow = true;
465 return result;
468 /* Handle meeting "defined" in a preprocessor expression. */
469 static cpp_num
470 parse_defined (cpp_reader *pfile)
472 cpp_num result;
473 int paren = 0;
474 cpp_hashnode *node = 0;
475 const cpp_token *token;
476 cpp_context *initial_context = pfile->context;
478 /* Don't expand macros. */
479 pfile->state.prevent_expansion++;
481 token = cpp_get_token (pfile);
482 if (token->type == CPP_OPEN_PAREN)
484 paren = 1;
485 token = cpp_get_token (pfile);
488 if (token->type == CPP_NAME)
490 node = token->val.node;
491 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
493 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
494 node = 0;
497 else
499 cpp_error (pfile, CPP_DL_ERROR,
500 "operator \"defined\" requires an identifier");
501 if (token->flags & NAMED_OP)
503 cpp_token op;
505 op.flags = 0;
506 op.type = token->type;
507 cpp_error (pfile, CPP_DL_ERROR,
508 "(\"%s\" is an alternative token for \"%s\" in C++)",
509 cpp_token_as_text (pfile, token),
510 cpp_token_as_text (pfile, &op));
514 if (node)
516 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
517 cpp_error (pfile, CPP_DL_WARNING,
518 "this use of \"defined\" may not be portable");
520 _cpp_mark_macro_used (node);
522 /* A possible controlling macro of the form #if !defined ().
523 _cpp_parse_expr checks there was no other junk on the line. */
524 pfile->mi_ind_cmacro = node;
527 pfile->state.prevent_expansion--;
529 result.unsignedp = false;
530 result.high = 0;
531 result.overflow = false;
532 result.low = node && node->type == NT_MACRO;
533 return result;
536 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
537 number or character constant, or the result of the "defined" or "#"
538 operators). */
539 static cpp_num
540 eval_token (cpp_reader *pfile, const cpp_token *token)
542 cpp_num result;
543 unsigned int temp;
544 int unsignedp = 0;
546 result.unsignedp = false;
547 result.overflow = false;
549 switch (token->type)
551 case CPP_NUMBER:
552 temp = cpp_classify_number (pfile, token);
553 switch (temp & CPP_N_CATEGORY)
555 case CPP_N_FLOATING:
556 cpp_error (pfile, CPP_DL_ERROR,
557 "floating constant in preprocessor expression");
558 break;
559 case CPP_N_INTEGER:
560 if (!(temp & CPP_N_IMAGINARY))
561 return cpp_interpret_integer (pfile, token, temp);
562 cpp_error (pfile, CPP_DL_ERROR,
563 "imaginary number in preprocessor expression");
564 break;
566 case CPP_N_INVALID:
567 /* Error already issued. */
568 break;
570 result.high = result.low = 0;
571 break;
573 case CPP_WCHAR:
574 case CPP_CHAR:
576 cppchar_t cc = cpp_interpret_charconst (pfile, token,
577 &temp, &unsignedp);
579 result.high = 0;
580 result.low = cc;
581 /* Sign-extend the result if necessary. */
582 if (!unsignedp && (cppchar_signed_t) cc < 0)
584 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
585 result.low |= ~(~(cpp_num_part) 0
586 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
587 result.high = ~(cpp_num_part) 0;
588 result = num_trim (result, CPP_OPTION (pfile, precision));
591 break;
593 case CPP_NAME:
594 if (token->val.node == pfile->spec_nodes.n_defined)
595 return parse_defined (pfile);
596 else if (CPP_OPTION (pfile, cplusplus)
597 && (token->val.node == pfile->spec_nodes.n_true
598 || token->val.node == pfile->spec_nodes.n_false))
600 result.high = 0;
601 result.low = (token->val.node == pfile->spec_nodes.n_true);
603 else
605 result.high = 0;
606 result.low = 0;
607 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
608 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined",
609 NODE_NAME (token->val.node));
611 break;
613 default: /* CPP_HASH */
614 _cpp_test_assertion (pfile, &temp);
615 result.high = 0;
616 result.low = temp;
619 result.unsignedp = !!unsignedp;
620 return result;
623 /* Operator precedence and flags table.
625 After an operator is returned from the lexer, if it has priority less
626 than the operator on the top of the stack, we reduce the stack by one
627 operator and repeat the test. Since equal priorities do not reduce,
628 this is naturally right-associative.
630 We handle left-associative operators by decrementing the priority of
631 just-lexed operators by one, but retaining the priority of operators
632 already on the stack.
634 The remaining cases are '(' and ')'. We handle '(' by skipping the
635 reduction phase completely. ')' is given lower priority than
636 everything else, including '(', effectively forcing a reduction of the
637 parenthesized expression. If there is a matching '(', the routine
638 reduce() exits immediately. If the normal exit route sees a ')', then
639 there cannot have been a matching '(' and an error message is output.
641 The parser assumes all shifted operators require a left operand unless
642 the flag NO_L_OPERAND is set. These semantics are automatic; any
643 extra semantics need to be handled with operator-specific code. */
645 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
646 operand changes because of integer promotions. */
647 #define NO_L_OPERAND (1 << 0)
648 #define LEFT_ASSOC (1 << 1)
649 #define CHECK_PROMOTION (1 << 2)
651 /* Operator to priority map. Must be in the same order as the first
652 N entries of enum cpp_ttype. */
653 static const struct cpp_operator
655 uchar prio;
656 uchar flags;
657 } optab[] =
659 /* EQ */ {0, 0}, /* Shouldn't happen. */
660 /* NOT */ {16, NO_L_OPERAND},
661 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
662 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
663 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
664 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
665 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
666 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
667 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
668 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
669 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
670 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
671 /* RSHIFT */ {13, LEFT_ASSOC},
672 /* LSHIFT */ {13, LEFT_ASSOC},
674 /* COMPL */ {16, NO_L_OPERAND},
675 /* AND_AND */ {6, LEFT_ASSOC},
676 /* OR_OR */ {5, LEFT_ASSOC},
677 /* QUERY */ {3, 0},
678 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
679 /* COMMA */ {2, LEFT_ASSOC},
680 /* OPEN_PAREN */ {1, NO_L_OPERAND},
681 /* CLOSE_PAREN */ {0, 0},
682 /* EOF */ {0, 0},
683 /* EQ_EQ */ {11, LEFT_ASSOC},
684 /* NOT_EQ */ {11, LEFT_ASSOC},
685 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
686 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
687 /* UPLUS */ {16, NO_L_OPERAND},
688 /* UMINUS */ {16, NO_L_OPERAND}
691 /* Parse and evaluate a C expression, reading from PFILE.
692 Returns the truth value of the expression.
694 The implementation is an operator precedence parser, i.e. a
695 bottom-up parser, using a stack for not-yet-reduced tokens.
697 The stack base is op_stack, and the current stack pointer is 'top'.
698 There is a stack element for each operator (only), and the most
699 recently pushed operator is 'top->op'. An operand (value) is
700 stored in the 'value' field of the stack element of the operator
701 that precedes it. */
702 bool
703 _cpp_parse_expr (cpp_reader *pfile)
705 struct op *top = pfile->op_stack;
706 unsigned int lex_count;
707 bool saw_leading_not, want_value = true;
709 pfile->state.skip_eval = 0;
711 /* Set up detection of #if ! defined(). */
712 pfile->mi_ind_cmacro = 0;
713 saw_leading_not = false;
714 lex_count = 0;
716 /* Lowest priority operator prevents further reductions. */
717 top->op = CPP_EOF;
719 for (;;)
721 struct op op;
723 lex_count++;
724 op.token = cpp_get_token (pfile);
725 op.op = op.token->type;
727 switch (op.op)
729 /* These tokens convert into values. */
730 case CPP_NUMBER:
731 case CPP_CHAR:
732 case CPP_WCHAR:
733 case CPP_NAME:
734 case CPP_HASH:
735 if (!want_value)
736 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
737 cpp_token_as_text (pfile, op.token));
738 want_value = false;
739 top->value = eval_token (pfile, op.token);
740 continue;
742 case CPP_NOT:
743 saw_leading_not = lex_count == 1;
744 break;
745 case CPP_PLUS:
746 if (want_value)
747 op.op = CPP_UPLUS;
748 break;
749 case CPP_MINUS:
750 if (want_value)
751 op.op = CPP_UMINUS;
752 break;
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 /* We want a number (or expression) and haven't got one.
771 Try to emit a specific diagnostic. */
772 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
773 SYNTAX_ERROR ("missing expression between '(' and ')'");
775 if (op.op == CPP_EOF && top->op == CPP_EOF)
776 SYNTAX_ERROR ("#if with no expression");
778 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
779 SYNTAX_ERROR2 ("operator '%s' has no right operand",
780 cpp_token_as_text (pfile, top->token));
781 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
782 /* Complain about missing paren during reduction. */;
783 else
784 SYNTAX_ERROR2 ("operator '%s' has no left operand",
785 cpp_token_as_text (pfile, op.token));
788 top = reduce (pfile, top, op.op);
789 if (!top)
790 goto syntax_error;
792 if (op.op == CPP_EOF)
793 break;
795 switch (op.op)
797 case CPP_CLOSE_PAREN:
798 continue;
799 case CPP_OR_OR:
800 if (!num_zerop (top->value))
801 pfile->state.skip_eval++;
802 break;
803 case CPP_AND_AND:
804 case CPP_QUERY:
805 if (num_zerop (top->value))
806 pfile->state.skip_eval++;
807 break;
808 case CPP_COLON:
809 if (top->op != CPP_QUERY)
810 SYNTAX_ERROR (" ':' without preceding '?'");
811 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
812 pfile->state.skip_eval++;
813 else
814 pfile->state.skip_eval--;
815 default:
816 break;
819 want_value = true;
821 /* Check for and handle stack overflow. */
822 if (++top == pfile->op_limit)
823 top = _cpp_expand_op_stack (pfile);
825 top->op = op.op;
826 top->token = op.token;
829 /* The controlling macro expression is only valid if we called lex 3
830 times: <!> <defined expression> and <EOF>. push_conditional ()
831 checks that we are at top-of-file. */
832 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
833 pfile->mi_ind_cmacro = 0;
835 if (top != pfile->op_stack)
837 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in #if");
838 syntax_error:
839 return false; /* Return false on syntax error. */
842 return !num_zerop (top->value);
845 /* Reduce the operator / value stack if possible, in preparation for
846 pushing operator OP. Returns NULL on error, otherwise the top of
847 the stack. */
848 static struct op *
849 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
851 unsigned int prio;
853 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
855 bad_op:
856 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
857 return 0;
860 if (op == CPP_OPEN_PAREN)
861 return top;
863 /* Decrement the priority of left-associative operators to force a
864 reduction with operators of otherwise equal priority. */
865 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
866 while (prio < optab[top->op].prio)
868 if (CPP_OPTION (pfile, warn_num_sign_change)
869 && optab[top->op].flags & CHECK_PROMOTION)
870 check_promotion (pfile, top);
872 switch (top->op)
874 case CPP_UPLUS:
875 case CPP_UMINUS:
876 case CPP_NOT:
877 case CPP_COMPL:
878 top[-1].value = num_unary_op (pfile, top->value, top->op);
879 break;
881 case CPP_PLUS:
882 case CPP_MINUS:
883 case CPP_RSHIFT:
884 case CPP_LSHIFT:
885 case CPP_COMMA:
886 top[-1].value = num_binary_op (pfile, top[-1].value,
887 top->value, top->op);
888 break;
890 case CPP_GREATER:
891 case CPP_LESS:
892 case CPP_GREATER_EQ:
893 case CPP_LESS_EQ:
894 top[-1].value
895 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
896 break;
898 case CPP_EQ_EQ:
899 case CPP_NOT_EQ:
900 top[-1].value
901 = num_equality_op (pfile, top[-1].value, top->value, top->op);
902 break;
904 case CPP_AND:
905 case CPP_OR:
906 case CPP_XOR:
907 top[-1].value
908 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
909 break;
911 case CPP_MULT:
912 top[-1].value = num_mul (pfile, top[-1].value, top->value);
913 break;
915 case CPP_DIV:
916 case CPP_MOD:
917 top[-1].value = num_div_op (pfile, top[-1].value,
918 top->value, top->op);
919 break;
921 case CPP_OR_OR:
922 top--;
923 if (!num_zerop (top->value))
924 pfile->state.skip_eval--;
925 top->value.low = (!num_zerop (top->value)
926 || !num_zerop (top[1].value));
927 top->value.high = 0;
928 top->value.unsignedp = false;
929 top->value.overflow = false;
930 continue;
932 case CPP_AND_AND:
933 top--;
934 if (num_zerop (top->value))
935 pfile->state.skip_eval--;
936 top->value.low = (!num_zerop (top->value)
937 && !num_zerop (top[1].value));
938 top->value.high = 0;
939 top->value.unsignedp = false;
940 top->value.overflow = false;
941 continue;
943 case CPP_OPEN_PAREN:
944 if (op != CPP_CLOSE_PAREN)
946 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in expression");
947 return 0;
949 top--;
950 top->value = top[1].value;
951 return top;
953 case CPP_COLON:
954 top -= 2;
955 if (!num_zerop (top->value))
957 pfile->state.skip_eval--;
958 top->value = top[1].value;
960 else
961 top->value = top[2].value;
962 top->value.unsignedp = (top[1].value.unsignedp
963 || top[2].value.unsignedp);
964 continue;
966 case CPP_QUERY:
967 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
968 return 0;
970 default:
971 goto bad_op;
974 top--;
975 if (top->value.overflow && !pfile->state.skip_eval)
976 cpp_error (pfile, CPP_DL_PEDWARN,
977 "integer overflow in preprocessor expression");
980 if (op == CPP_CLOSE_PAREN)
982 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
983 return 0;
986 return top;
989 /* Returns the position of the old top of stack after expansion. */
990 struct op *
991 _cpp_expand_op_stack (cpp_reader *pfile)
993 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
994 size_t new_size = old_size * 2 + 20;
996 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
997 pfile->op_limit = pfile->op_stack + new_size;
999 return pfile->op_stack + old_size;
1002 /* Emits a warning if the effective sign of either operand of OP
1003 changes because of integer promotions. */
1004 static void
1005 check_promotion (cpp_reader *pfile, const struct op *op)
1007 if (op->value.unsignedp == op[-1].value.unsignedp)
1008 return;
1010 if (op->value.unsignedp)
1012 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1013 cpp_error (pfile, CPP_DL_WARNING,
1014 "the left operand of \"%s\" changes sign when promoted",
1015 cpp_token_as_text (pfile, op->token));
1017 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1018 cpp_error (pfile, CPP_DL_WARNING,
1019 "the right operand of \"%s\" changes sign when promoted",
1020 cpp_token_as_text (pfile, op->token));
1023 /* Clears the unused high order bits of the number pointed to by PNUM. */
1024 static cpp_num
1025 num_trim (cpp_num num, size_t precision)
1027 if (precision > PART_PRECISION)
1029 precision -= PART_PRECISION;
1030 if (precision < PART_PRECISION)
1031 num.high &= ((cpp_num_part) 1 << precision) - 1;
1033 else
1035 if (precision < PART_PRECISION)
1036 num.low &= ((cpp_num_part) 1 << precision) - 1;
1037 num.high = 0;
1040 return num;
1043 /* True iff A (presumed signed) >= 0. */
1044 static bool
1045 num_positive (cpp_num num, size_t precision)
1047 if (precision > PART_PRECISION)
1049 precision -= PART_PRECISION;
1050 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1053 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1056 /* Sign extend a number, with PRECISION significant bits and all
1057 others assumed clear, to fill out a cpp_num structure. */
1058 cpp_num
1059 cpp_num_sign_extend (cpp_num num, size_t precision)
1061 if (!num.unsignedp)
1063 if (precision > PART_PRECISION)
1065 precision -= PART_PRECISION;
1066 if (precision < PART_PRECISION
1067 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1068 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1070 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1072 if (precision < PART_PRECISION)
1073 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1074 num.high = ~(cpp_num_part) 0;
1078 return num;
1081 /* Returns the negative of NUM. */
1082 static cpp_num
1083 num_negate (cpp_num num, size_t precision)
1085 cpp_num copy;
1087 copy = num;
1088 num.high = ~num.high;
1089 num.low = ~num.low;
1090 if (++num.low == 0)
1091 num.high++;
1092 num = num_trim (num, precision);
1093 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1095 return num;
1098 /* Returns true if A >= B. */
1099 static bool
1100 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1102 bool unsignedp;
1104 unsignedp = pa.unsignedp || pb.unsignedp;
1106 if (!unsignedp)
1108 /* Both numbers have signed type. If they are of different
1109 sign, the answer is the sign of A. */
1110 unsignedp = num_positive (pa, precision);
1112 if (unsignedp != num_positive (pb, precision))
1113 return unsignedp;
1115 /* Otherwise we can do an unsigned comparison. */
1118 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1121 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1122 static cpp_num
1123 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1124 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1126 lhs.overflow = false;
1127 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1129 /* As excess precision is zeroed, there is no need to num_trim () as
1130 these operations cannot introduce a set bit there. */
1131 if (op == CPP_AND)
1133 lhs.low &= rhs.low;
1134 lhs.high &= rhs.high;
1136 else if (op == CPP_OR)
1138 lhs.low |= rhs.low;
1139 lhs.high |= rhs.high;
1141 else
1143 lhs.low ^= rhs.low;
1144 lhs.high ^= rhs.high;
1147 return lhs;
1150 /* Returns LHS OP RHS, where OP is an inequality. */
1151 static cpp_num
1152 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1153 enum cpp_ttype op)
1155 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1157 if (op == CPP_GREATER_EQ)
1158 lhs.low = gte;
1159 else if (op == CPP_LESS)
1160 lhs.low = !gte;
1161 else if (op == CPP_GREATER)
1162 lhs.low = gte && !num_eq (lhs, rhs);
1163 else /* CPP_LESS_EQ. */
1164 lhs.low = !gte || num_eq (lhs, rhs);
1166 lhs.high = 0;
1167 lhs.overflow = false;
1168 lhs.unsignedp = false;
1169 return lhs;
1172 /* Returns LHS OP RHS, where OP is == or !=. */
1173 static cpp_num
1174 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1175 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1177 /* Work around a 3.0.4 bug; see PR 6950. */
1178 bool eq = num_eq (lhs, rhs);
1179 if (op == CPP_NOT_EQ)
1180 eq = !eq;
1181 lhs.low = eq;
1182 lhs.high = 0;
1183 lhs.overflow = false;
1184 lhs.unsignedp = false;
1185 return lhs;
1188 /* Shift NUM, of width PRECISION, right by N bits. */
1189 static cpp_num
1190 num_rshift (cpp_num num, size_t precision, size_t n)
1192 cpp_num_part sign_mask;
1193 bool x = num_positive (num, precision);
1195 if (num.unsignedp || x)
1196 sign_mask = 0;
1197 else
1198 sign_mask = ~(cpp_num_part) 0;
1200 if (n >= precision)
1201 num.high = num.low = sign_mask;
1202 else
1204 /* Sign-extend. */
1205 if (precision < PART_PRECISION)
1206 num.high = sign_mask, num.low |= sign_mask << precision;
1207 else if (precision < 2 * PART_PRECISION)
1208 num.high |= sign_mask << (precision - PART_PRECISION);
1210 if (n >= PART_PRECISION)
1212 n -= PART_PRECISION;
1213 num.low = num.high;
1214 num.high = sign_mask;
1217 if (n)
1219 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1220 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1224 num = num_trim (num, precision);
1225 num.overflow = false;
1226 return num;
1229 /* Shift NUM, of width PRECISION, left by N bits. */
1230 static cpp_num
1231 num_lshift (cpp_num num, size_t precision, size_t n)
1233 if (n >= precision)
1235 num.overflow = !num.unsignedp && !num_zerop (num);
1236 num.high = num.low = 0;
1238 else
1240 cpp_num orig, maybe_orig;
1241 size_t m = n;
1243 orig = num;
1244 if (m >= PART_PRECISION)
1246 m -= PART_PRECISION;
1247 num.high = num.low;
1248 num.low = 0;
1250 if (m)
1252 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1253 num.low <<= m;
1255 num = num_trim (num, precision);
1257 if (num.unsignedp)
1258 num.overflow = false;
1259 else
1261 maybe_orig = num_rshift (num, precision, n);
1262 num.overflow = !num_eq (orig, maybe_orig);
1266 return num;
1269 /* The four unary operators: +, -, ! and ~. */
1270 static cpp_num
1271 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1273 switch (op)
1275 case CPP_UPLUS:
1276 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1277 cpp_error (pfile, CPP_DL_WARNING,
1278 "traditional C rejects the unary plus operator");
1279 num.overflow = false;
1280 break;
1282 case CPP_UMINUS:
1283 num = num_negate (num, CPP_OPTION (pfile, precision));
1284 break;
1286 case CPP_COMPL:
1287 num.high = ~num.high;
1288 num.low = ~num.low;
1289 num = num_trim (num, CPP_OPTION (pfile, precision));
1290 num.overflow = false;
1291 break;
1293 default: /* case CPP_NOT: */
1294 num.low = num_zerop (num);
1295 num.high = 0;
1296 num.overflow = false;
1297 num.unsignedp = false;
1298 break;
1301 return num;
1304 /* The various binary operators. */
1305 static cpp_num
1306 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1308 cpp_num result;
1309 size_t precision = CPP_OPTION (pfile, precision);
1310 size_t n;
1312 switch (op)
1314 /* Shifts. */
1315 case CPP_LSHIFT:
1316 case CPP_RSHIFT:
1317 if (!rhs.unsignedp && !num_positive (rhs, precision))
1319 /* A negative shift is a positive shift the other way. */
1320 if (op == CPP_LSHIFT)
1321 op = CPP_RSHIFT;
1322 else
1323 op = CPP_LSHIFT;
1324 rhs = num_negate (rhs, precision);
1326 if (rhs.high)
1327 n = ~0; /* Maximal. */
1328 else
1329 n = rhs.low;
1330 if (op == CPP_LSHIFT)
1331 lhs = num_lshift (lhs, precision, n);
1332 else
1333 lhs = num_rshift (lhs, precision, n);
1334 break;
1336 /* Arithmetic. */
1337 case CPP_MINUS:
1338 rhs = num_negate (rhs, precision);
1339 case CPP_PLUS:
1340 result.low = lhs.low + rhs.low;
1341 result.high = lhs.high + rhs.high;
1342 if (result.low < lhs.low)
1343 result.high++;
1344 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1345 result.overflow = false;
1347 result = num_trim (result, precision);
1348 if (!result.unsignedp)
1350 bool lhsp = num_positive (lhs, precision);
1351 result.overflow = (lhsp == num_positive (rhs, precision)
1352 && lhsp != num_positive (result, precision));
1354 return result;
1356 /* Comma. */
1357 default: /* case CPP_COMMA: */
1358 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1359 || !pfile->state.skip_eval))
1360 cpp_error (pfile, CPP_DL_PEDWARN,
1361 "comma operator in operand of #if");
1362 lhs = rhs;
1363 break;
1366 return lhs;
1369 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1370 cannot overflow. */
1371 static cpp_num
1372 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1374 cpp_num result;
1375 cpp_num_part middle[2], temp;
1377 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1378 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1380 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1381 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1383 temp = result.low;
1384 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1385 if (result.low < temp)
1386 result.high++;
1388 temp = result.low;
1389 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1390 if (result.low < temp)
1391 result.high++;
1393 result.high += HIGH_PART (middle[0]);
1394 result.high += HIGH_PART (middle[1]);
1395 result.unsignedp = true;
1396 result.overflow = false;
1398 return result;
1401 /* Multiply two preprocessing numbers. */
1402 static cpp_num
1403 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1405 cpp_num result, temp;
1406 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1407 bool overflow, negate = false;
1408 size_t precision = CPP_OPTION (pfile, precision);
1410 /* Prepare for unsigned multiplication. */
1411 if (!unsignedp)
1413 if (!num_positive (lhs, precision))
1414 negate = !negate, lhs = num_negate (lhs, precision);
1415 if (!num_positive (rhs, precision))
1416 negate = !negate, rhs = num_negate (rhs, precision);
1419 overflow = lhs.high && rhs.high;
1420 result = num_part_mul (lhs.low, rhs.low);
1422 temp = num_part_mul (lhs.high, rhs.low);
1423 result.high += temp.low;
1424 if (temp.high)
1425 overflow = true;
1427 temp = num_part_mul (lhs.low, rhs.high);
1428 result.high += temp.low;
1429 if (temp.high)
1430 overflow = true;
1432 temp.low = result.low, temp.high = result.high;
1433 result = num_trim (result, precision);
1434 if (!num_eq (result, temp))
1435 overflow = true;
1437 if (negate)
1438 result = num_negate (result, precision);
1440 if (unsignedp)
1441 result.overflow = false;
1442 else
1443 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1444 && !num_zerop (result));
1445 result.unsignedp = unsignedp;
1447 return result;
1450 /* Divide two preprocessing numbers, returning the answer or the
1451 remainder depending upon OP. */
1452 static cpp_num
1453 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1455 cpp_num result, sub;
1456 cpp_num_part mask;
1457 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1458 bool negate = false, lhs_neg = false;
1459 size_t i, precision = CPP_OPTION (pfile, precision);
1461 /* Prepare for unsigned division. */
1462 if (!unsignedp)
1464 if (!num_positive (lhs, precision))
1465 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1466 if (!num_positive (rhs, precision))
1467 negate = !negate, rhs = num_negate (rhs, precision);
1470 /* Find the high bit. */
1471 if (rhs.high)
1473 i = precision - 1;
1474 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1475 for (; ; i--, mask >>= 1)
1476 if (rhs.high & mask)
1477 break;
1479 else if (rhs.low)
1481 if (precision > PART_PRECISION)
1482 i = precision - PART_PRECISION - 1;
1483 else
1484 i = precision - 1;
1485 mask = (cpp_num_part) 1 << i;
1486 for (; ; i--, mask >>= 1)
1487 if (rhs.low & mask)
1488 break;
1490 else
1492 if (!pfile->state.skip_eval)
1493 cpp_error (pfile, CPP_DL_ERROR, "division by zero in #if");
1494 return lhs;
1497 /* First nonzero bit of RHS is bit I. Do naive division by
1498 shifting the RHS fully left, and subtracting from LHS if LHS is
1499 at least as big, and then repeating but with one less shift.
1500 This is not very efficient, but is easy to understand. */
1502 rhs.unsignedp = true;
1503 lhs.unsignedp = true;
1504 i = precision - i - 1;
1505 sub = num_lshift (rhs, precision, i);
1507 result.high = result.low = 0;
1508 for (;;)
1510 if (num_greater_eq (lhs, sub, precision))
1512 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1513 if (i >= PART_PRECISION)
1514 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1515 else
1516 result.low |= (cpp_num_part) 1 << i;
1518 if (i-- == 0)
1519 break;
1520 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1521 sub.high >>= 1;
1524 /* We divide so that the remainder has the sign of the LHS. */
1525 if (op == CPP_DIV)
1527 result.unsignedp = unsignedp;
1528 result.overflow = false;
1529 if (!unsignedp)
1531 if (negate)
1532 result = num_negate (result, precision);
1533 result.overflow = (num_positive (result, precision) ^ !negate
1534 && !num_zerop (result));
1537 return result;
1540 /* CPP_MOD. */
1541 lhs.unsignedp = unsignedp;
1542 lhs.overflow = false;
1543 if (lhs_neg)
1544 lhs = num_negate (lhs, precision);
1546 return lhs;