Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.git] / contrib / gcc-3.4 / gcc / cppexp.c
blobcb35b6c782fd3bdbda6a765ee8117d8427ebcbbe
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 (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 (CPP_LAST_CPP_OP + 1)
69 #define CPP_UMINUS (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;
87 while (len--)
88 switch (s[len])
90 case 'f': case 'F': f++; break;
91 case 'l': case 'L': l++; break;
92 case 'i': case 'I':
93 case 'j': case 'J': i++; break;
94 default:
95 return 0;
98 if (f + l > 1 || i > 1)
99 return 0;
101 return ((i ? CPP_N_IMAGINARY : 0)
102 | (f ? CPP_N_SMALL :
103 l ? CPP_N_LARGE : CPP_N_MEDIUM));
106 /* Subroutine of cpp_classify_number. S points to an integer suffix
107 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
108 flag vector describing the suffix. */
109 static unsigned int
110 interpret_int_suffix (const uchar *s, size_t len)
112 size_t u, l, i;
114 u = l = i = 0;
116 while (len--)
117 switch (s[len])
119 case 'u': case 'U': u++; break;
120 case 'i': case 'I':
121 case 'j': case 'J': i++; break;
122 case 'l': case 'L': l++;
123 /* If there are two Ls, they must be adjacent and the same case. */
124 if (l == 2 && s[len] != s[len + 1])
125 return 0;
126 break;
127 default:
128 return 0;
131 if (l > 2 || u > 1 || i > 1)
132 return 0;
134 return ((i ? CPP_N_IMAGINARY : 0)
135 | (u ? CPP_N_UNSIGNED : 0)
136 | ((l == 0) ? CPP_N_SMALL
137 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
140 /* Categorize numeric constants according to their field (integer,
141 floating point, or invalid), radix (decimal, octal, hexadecimal),
142 and type suffixes. */
143 unsigned int
144 cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
146 const uchar *str = token->val.str.text;
147 const uchar *limit;
148 unsigned int max_digit, result, radix;
149 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
151 /* If the lexer has done its job, length one can only be a single
152 digit. Fast-path this very common case. */
153 if (token->val.str.len == 1)
154 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
156 limit = str + token->val.str.len;
157 float_flag = NOT_FLOAT;
158 max_digit = 0;
159 radix = 10;
161 /* First, interpret the radix. */
162 if (*str == '0')
164 radix = 8;
165 str++;
167 /* Require at least one hex digit to classify it as hex. */
168 if ((*str == 'x' || *str == 'X')
169 && (str[1] == '.' || ISXDIGIT (str[1])))
171 radix = 16;
172 str++;
176 /* Now scan for a well-formed integer or float. */
177 for (;;)
179 unsigned int c = *str++;
181 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
183 c = hex_value (c);
184 if (c > max_digit)
185 max_digit = c;
187 else if (c == '.')
189 if (float_flag == NOT_FLOAT)
190 float_flag = AFTER_POINT;
191 else
192 SYNTAX_ERROR ("too many decimal points in number");
194 else if ((radix <= 10 && (c == 'e' || c == 'E'))
195 || (radix == 16 && (c == 'p' || c == 'P')))
197 float_flag = AFTER_EXPON;
198 break;
200 else
202 /* Start of suffix. */
203 str--;
204 break;
208 if (float_flag != NOT_FLOAT && radix == 8)
209 radix = 10;
211 if (max_digit >= radix)
212 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
214 if (float_flag != NOT_FLOAT)
216 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
217 cpp_error (pfile, CPP_DL_PEDWARN,
218 "use of C99 hexadecimal floating constant");
220 if (float_flag == AFTER_EXPON)
222 if (*str == '+' || *str == '-')
223 str++;
225 /* Exponent is decimal, even if string is a hex float. */
226 if (!ISDIGIT (*str))
227 SYNTAX_ERROR ("exponent has no digits");
230 str++;
231 while (ISDIGIT (*str));
233 else if (radix == 16)
234 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
236 result = interpret_float_suffix (str, limit - str);
237 if (result == 0)
239 cpp_error (pfile, CPP_DL_ERROR,
240 "invalid suffix \"%.*s\" on floating constant",
241 (int) (limit - str), str);
242 return CPP_N_INVALID;
245 /* Traditional C didn't accept any floating suffixes. */
246 if (limit != str
247 && CPP_WTRADITIONAL (pfile)
248 && ! cpp_sys_macro_p (pfile))
249 cpp_error (pfile, CPP_DL_WARNING,
250 "traditional C rejects the \"%.*s\" suffix",
251 (int) (limit - str), str);
253 result |= CPP_N_FLOATING;
255 else
257 result = interpret_int_suffix (str, limit - str);
258 if (result == 0)
260 cpp_error (pfile, CPP_DL_ERROR,
261 "invalid suffix \"%.*s\" on integer constant",
262 (int) (limit - str), str);
263 return CPP_N_INVALID;
266 /* Traditional C only accepted the 'L' suffix.
267 Suppress warning about 'LL' with -Wno-long-long. */
268 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
270 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
271 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
273 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
274 cpp_error (pfile, CPP_DL_WARNING,
275 "traditional C rejects the \"%.*s\" suffix",
276 (int) (limit - str), str);
279 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
280 && ! CPP_OPTION (pfile, c99)
281 && CPP_OPTION (pfile, warn_long_long))
282 cpp_error (pfile, CPP_DL_PEDWARN,
283 "use of C99 long long integer constant");
285 result |= CPP_N_INTEGER;
288 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
289 cpp_error (pfile, CPP_DL_PEDWARN,
290 "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 we don't want to
310 drag in GCC's floating point emulator. */
311 cpp_num
312 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
313 unsigned int type)
315 const uchar *p, *end;
316 cpp_num result;
318 result.low = 0;
319 result.high = 0;
320 result.unsignedp = !!(type & CPP_N_UNSIGNED);
321 result.overflow = false;
323 p = token->val.str.text;
324 end = p + token->val.str.len;
326 /* Common case of a single digit. */
327 if (token->val.str.len == 1)
328 result.low = p[0] - '0';
329 else
331 cpp_num_part max;
332 size_t precision = CPP_OPTION (pfile, precision);
333 unsigned int base = 10, c = 0;
334 bool overflow = false;
336 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
338 base = 8;
339 p++;
341 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
343 base = 16;
344 p += 2;
347 /* We can add a digit to numbers strictly less than this without
348 needing the precision and slowness of double integers. */
349 max = ~(cpp_num_part) 0;
350 if (precision < PART_PRECISION)
351 max >>= PART_PRECISION - precision;
352 max = (max - base + 1) / base + 1;
354 for (; p < end; p++)
356 c = *p;
358 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
359 c = hex_value (c);
360 else
361 break;
363 /* Strict inequality for when max is set to zero. */
364 if (result.low < max)
365 result.low = result.low * base + c;
366 else
368 result = append_digit (result, c, base, precision);
369 overflow |= result.overflow;
370 max = 0;
374 if (overflow)
375 cpp_error (pfile, CPP_DL_PEDWARN,
376 "integer constant is too large for its type");
377 /* If too big to be signed, consider it unsigned. Only warn for
378 decimal numbers. Traditional numbers were always signed (but
379 we still honor an explicit U suffix); but we only have
380 traditional semantics in directives. */
381 else if (!result.unsignedp
382 && !(CPP_OPTION (pfile, traditional)
383 && pfile->state.in_directive)
384 && !num_positive (result, precision))
386 if (base == 10)
387 cpp_error (pfile, CPP_DL_WARNING,
388 "integer constant is so large that it is unsigned");
389 result.unsignedp = true;
393 return result;
396 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
397 static cpp_num
398 append_digit (cpp_num num, int digit, int base, size_t precision)
400 cpp_num result;
401 unsigned int shift = 3 + (base == 16);
402 bool overflow;
403 cpp_num_part add_high, add_low;
405 /* Multiply by 8 or 16. Catching this overflow here means we don't
406 need to worry about add_high overflowing. */
407 overflow = !!(num.high >> (PART_PRECISION - shift));
408 result.high = num.high << shift;
409 result.low = num.low << shift;
410 result.high |= num.low >> (PART_PRECISION - shift);
412 if (base == 10)
414 add_low = num.low << 1;
415 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
417 else
418 add_high = add_low = 0;
420 if (add_low + digit < add_low)
421 add_high++;
422 add_low += digit;
424 if (result.low + add_low < result.low)
425 add_high++;
426 if (result.high + add_high < result.high)
427 overflow = true;
429 result.low += add_low;
430 result.high += add_high;
432 /* The above code catches overflow of a cpp_num type. This catches
433 overflow of the (possibly shorter) target precision. */
434 num.low = result.low;
435 num.high = result.high;
436 result = num_trim (result, precision);
437 if (!num_eq (result, num))
438 overflow = true;
440 result.unsignedp = num.unsignedp;
441 result.overflow = overflow;
442 return result;
445 /* Handle meeting "defined" in a preprocessor expression. */
446 static cpp_num
447 parse_defined (cpp_reader *pfile)
449 cpp_num result;
450 int paren = 0;
451 cpp_hashnode *node = 0;
452 const cpp_token *token;
453 cpp_context *initial_context = pfile->context;
455 /* Don't expand macros. */
456 pfile->state.prevent_expansion++;
458 token = cpp_get_token (pfile);
459 if (token->type == CPP_OPEN_PAREN)
461 paren = 1;
462 token = cpp_get_token (pfile);
465 if (token->type == CPP_NAME)
467 node = token->val.node;
468 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
470 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
471 node = 0;
474 else
476 cpp_error (pfile, CPP_DL_ERROR,
477 "operator \"defined\" requires an identifier");
478 if (token->flags & NAMED_OP)
480 cpp_token op;
482 op.flags = 0;
483 op.type = token->type;
484 cpp_error (pfile, CPP_DL_ERROR,
485 "(\"%s\" is an alternative token for \"%s\" in C++)",
486 cpp_token_as_text (pfile, token),
487 cpp_token_as_text (pfile, &op));
491 if (node)
493 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
494 cpp_error (pfile, CPP_DL_WARNING,
495 "this use of \"defined\" may not be portable");
497 _cpp_mark_macro_used (node);
499 /* A possible controlling macro of the form #if !defined ().
500 _cpp_parse_expr checks there was no other junk on the line. */
501 pfile->mi_ind_cmacro = node;
504 pfile->state.prevent_expansion--;
506 result.unsignedp = false;
507 result.high = 0;
508 result.overflow = false;
509 result.low = node && node->type == NT_MACRO;
510 return result;
513 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
514 number or character constant, or the result of the "defined" or "#"
515 operators). */
516 static cpp_num
517 eval_token (cpp_reader *pfile, const cpp_token *token)
519 cpp_num result;
520 unsigned int temp;
521 int unsignedp = 0;
523 switch (token->type)
525 case CPP_NUMBER:
526 temp = cpp_classify_number (pfile, token);
527 switch (temp & CPP_N_CATEGORY)
529 case CPP_N_FLOATING:
530 cpp_error (pfile, CPP_DL_ERROR,
531 "floating constant in preprocessor expression");
532 break;
533 case CPP_N_INTEGER:
534 if (!(temp & CPP_N_IMAGINARY))
535 return cpp_interpret_integer (pfile, token, temp);
536 cpp_error (pfile, CPP_DL_ERROR,
537 "imaginary number in preprocessor expression");
538 break;
540 case CPP_N_INVALID:
541 /* Error already issued. */
542 break;
544 result.high = result.low = 0;
545 break;
547 case CPP_WCHAR:
548 case CPP_CHAR:
550 cppchar_t cc = cpp_interpret_charconst (pfile, token,
551 &temp, &unsignedp);
553 result.high = 0;
554 result.low = cc;
555 /* Sign-extend the result if necessary. */
556 if (!unsignedp && (cppchar_signed_t) cc < 0)
558 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
559 result.low |= ~(~(cpp_num_part) 0
560 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
561 result.high = ~(cpp_num_part) 0;
562 result = num_trim (result, CPP_OPTION (pfile, precision));
565 break;
567 case CPP_NAME:
568 if (token->val.node == pfile->spec_nodes.n_defined)
569 return parse_defined (pfile);
570 else if (CPP_OPTION (pfile, cplusplus)
571 && (token->val.node == pfile->spec_nodes.n_true
572 || token->val.node == pfile->spec_nodes.n_false))
574 result.high = 0;
575 result.low = (token->val.node == pfile->spec_nodes.n_true);
577 else
579 result.high = 0;
580 result.low = 0;
581 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
582 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined",
583 NODE_NAME (token->val.node));
585 break;
587 default: /* CPP_HASH */
588 _cpp_test_assertion (pfile, &temp);
589 result.high = 0;
590 result.low = temp;
593 result.unsignedp = !!unsignedp;
594 result.overflow = false;
595 return result;
598 /* Operator precedence and flags table.
600 After an operator is returned from the lexer, if it has priority less
601 than the operator on the top of the stack, we reduce the stack by one
602 operator and repeat the test. Since equal priorities do not reduce,
603 this is naturally right-associative.
605 We handle left-associative operators by decrementing the priority of
606 just-lexed operators by one, but retaining the priority of operators
607 already on the stack.
609 The remaining cases are '(' and ')'. We handle '(' by skipping the
610 reduction phase completely. ')' is given lower priority than
611 everything else, including '(', effectively forcing a reduction of the
612 parenthesized expression. If there is a matching '(', the routine
613 reduce() exits immediately. If the normal exit route sees a ')', then
614 there cannot have been a matching '(' and an error message is output.
616 The parser assumes all shifted operators require a left operand unless
617 the flag NO_L_OPERAND is set. These semantics are automatic; any
618 extra semantics need to be handled with operator-specific code. */
620 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
621 operand changes because of integer promotions. */
622 #define NO_L_OPERAND (1 << 0)
623 #define LEFT_ASSOC (1 << 1)
624 #define CHECK_PROMOTION (1 << 2)
626 /* Operator to priority map. Must be in the same order as the first
627 N entries of enum cpp_ttype. */
628 static const struct operator
630 uchar prio;
631 uchar flags;
632 } optab[] =
634 /* EQ */ {0, 0}, /* Shouldn't happen. */
635 /* NOT */ {16, NO_L_OPERAND},
636 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
637 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
638 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
639 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
640 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
641 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
642 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
643 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
644 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
645 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
646 /* RSHIFT */ {13, LEFT_ASSOC},
647 /* LSHIFT */ {13, LEFT_ASSOC},
649 /* MIN */ {10, LEFT_ASSOC | CHECK_PROMOTION},
650 /* MAX */ {10, LEFT_ASSOC | CHECK_PROMOTION},
652 /* COMPL */ {16, NO_L_OPERAND},
653 /* AND_AND */ {6, LEFT_ASSOC},
654 /* OR_OR */ {5, LEFT_ASSOC},
655 /* QUERY */ {3, 0},
656 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
657 /* COMMA */ {2, LEFT_ASSOC},
658 /* OPEN_PAREN */ {1, NO_L_OPERAND},
659 /* CLOSE_PAREN */ {0, 0},
660 /* EOF */ {0, 0},
661 /* EQ_EQ */ {11, LEFT_ASSOC},
662 /* NOT_EQ */ {11, LEFT_ASSOC},
663 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
664 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
665 /* UPLUS */ {16, NO_L_OPERAND},
666 /* UMINUS */ {16, NO_L_OPERAND}
669 /* Parse and evaluate a C expression, reading from PFILE.
670 Returns the truth value of the expression.
672 The implementation is an operator precedence parser, i.e. a
673 bottom-up parser, using a stack for not-yet-reduced tokens.
675 The stack base is op_stack, and the current stack pointer is 'top'.
676 There is a stack element for each operator (only), and the most
677 recently pushed operator is 'top->op'. An operand (value) is
678 stored in the 'value' field of the stack element of the operator
679 that precedes it. */
680 bool
681 _cpp_parse_expr (cpp_reader *pfile)
683 struct op *top = pfile->op_stack;
684 unsigned int lex_count;
685 bool saw_leading_not, want_value = true;
687 pfile->state.skip_eval = 0;
689 /* Set up detection of #if ! defined(). */
690 pfile->mi_ind_cmacro = 0;
691 saw_leading_not = false;
692 lex_count = 0;
694 /* Lowest priority operator prevents further reductions. */
695 top->op = CPP_EOF;
697 for (;;)
699 struct op op;
701 lex_count++;
702 op.token = cpp_get_token (pfile);
703 op.op = op.token->type;
705 switch (op.op)
707 /* These tokens convert into values. */
708 case CPP_NUMBER:
709 case CPP_CHAR:
710 case CPP_WCHAR:
711 case CPP_NAME:
712 case CPP_HASH:
713 if (!want_value)
714 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
715 cpp_token_as_text (pfile, op.token));
716 want_value = false;
717 top->value = eval_token (pfile, op.token);
718 continue;
720 case CPP_NOT:
721 saw_leading_not = lex_count == 1;
722 break;
723 case CPP_PLUS:
724 if (want_value)
725 op.op = CPP_UPLUS;
726 break;
727 case CPP_MINUS:
728 if (want_value)
729 op.op = CPP_UMINUS;
730 break;
732 default:
733 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
734 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
735 cpp_token_as_text (pfile, op.token));
736 break;
739 /* Check we have a value or operator as appropriate. */
740 if (optab[op.op].flags & NO_L_OPERAND)
742 if (!want_value)
743 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
744 cpp_token_as_text (pfile, op.token));
746 else if (want_value)
748 /* We want a number (or expression) and haven't got one.
749 Try to emit a specific diagnostic. */
750 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
751 SYNTAX_ERROR ("missing expression between '(' and ')'");
753 if (op.op == CPP_EOF && top->op == CPP_EOF)
754 SYNTAX_ERROR ("#if with no expression");
756 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
757 SYNTAX_ERROR2 ("operator '%s' has no right operand",
758 cpp_token_as_text (pfile, top->token));
759 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
760 /* Complain about missing paren during reduction. */;
761 else
762 SYNTAX_ERROR2 ("operator '%s' has no left operand",
763 cpp_token_as_text (pfile, op.token));
766 top = reduce (pfile, top, op.op);
767 if (!top)
768 goto syntax_error;
770 if (op.op == CPP_EOF)
771 break;
773 switch (op.op)
775 case CPP_CLOSE_PAREN:
776 continue;
777 case CPP_OR_OR:
778 if (!num_zerop (top->value))
779 pfile->state.skip_eval++;
780 break;
781 case CPP_AND_AND:
782 case CPP_QUERY:
783 if (num_zerop (top->value))
784 pfile->state.skip_eval++;
785 break;
786 case CPP_COLON:
787 if (top->op != CPP_QUERY)
788 SYNTAX_ERROR (" ':' without preceding '?'");
789 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
790 pfile->state.skip_eval++;
791 else
792 pfile->state.skip_eval--;
793 default:
794 break;
797 want_value = true;
799 /* Check for and handle stack overflow. */
800 if (++top == pfile->op_limit)
801 top = _cpp_expand_op_stack (pfile);
803 top->op = op.op;
804 top->token = op.token;
807 /* The controlling macro expression is only valid if we called lex 3
808 times: <!> <defined expression> and <EOF>. push_conditional ()
809 checks that we are at top-of-file. */
810 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
811 pfile->mi_ind_cmacro = 0;
813 if (top != pfile->op_stack)
815 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in #if");
816 syntax_error:
817 return false; /* Return false on syntax error. */
820 return !num_zerop (top->value);
823 /* Reduce the operator / value stack if possible, in preparation for
824 pushing operator OP. Returns NULL on error, otherwise the top of
825 the stack. */
826 static struct op *
827 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
829 unsigned int prio;
831 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
833 bad_op:
834 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
835 return 0;
838 if (op == CPP_OPEN_PAREN)
839 return top;
841 /* Decrement the priority of left-associative operators to force a
842 reduction with operators of otherwise equal priority. */
843 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
844 while (prio < optab[top->op].prio)
846 if (CPP_OPTION (pfile, warn_num_sign_change)
847 && optab[top->op].flags & CHECK_PROMOTION)
848 check_promotion (pfile, top);
850 switch (top->op)
852 case CPP_UPLUS:
853 case CPP_UMINUS:
854 case CPP_NOT:
855 case CPP_COMPL:
856 top[-1].value = num_unary_op (pfile, top->value, top->op);
857 break;
859 case CPP_PLUS:
860 case CPP_MINUS:
861 case CPP_RSHIFT:
862 case CPP_LSHIFT:
863 case CPP_MIN:
864 case CPP_MAX:
865 case CPP_COMMA:
866 top[-1].value = num_binary_op (pfile, top[-1].value,
867 top->value, top->op);
868 break;
870 case CPP_GREATER:
871 case CPP_LESS:
872 case CPP_GREATER_EQ:
873 case CPP_LESS_EQ:
874 top[-1].value
875 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
876 break;
878 case CPP_EQ_EQ:
879 case CPP_NOT_EQ:
880 top[-1].value
881 = num_equality_op (pfile, top[-1].value, top->value, top->op);
882 break;
884 case CPP_AND:
885 case CPP_OR:
886 case CPP_XOR:
887 top[-1].value
888 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
889 break;
891 case CPP_MULT:
892 top[-1].value = num_mul (pfile, top[-1].value, top->value);
893 break;
895 case CPP_DIV:
896 case CPP_MOD:
897 top[-1].value = num_div_op (pfile, top[-1].value,
898 top->value, top->op);
899 break;
901 case CPP_OR_OR:
902 top--;
903 if (!num_zerop (top->value))
904 pfile->state.skip_eval--;
905 top->value.low = (!num_zerop (top->value)
906 || !num_zerop (top[1].value));
907 top->value.high = 0;
908 top->value.unsignedp = false;
909 top->value.overflow = false;
910 continue;
912 case CPP_AND_AND:
913 top--;
914 if (num_zerop (top->value))
915 pfile->state.skip_eval--;
916 top->value.low = (!num_zerop (top->value)
917 && !num_zerop (top[1].value));
918 top->value.high = 0;
919 top->value.unsignedp = false;
920 top->value.overflow = false;
921 continue;
923 case CPP_OPEN_PAREN:
924 if (op != CPP_CLOSE_PAREN)
926 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in expression");
927 return 0;
929 top--;
930 top->value = top[1].value;
931 return top;
933 case CPP_COLON:
934 top -= 2;
935 if (!num_zerop (top->value))
937 pfile->state.skip_eval--;
938 top->value = top[1].value;
940 else
941 top->value = top[2].value;
942 top->value.unsignedp = (top[1].value.unsignedp
943 || top[2].value.unsignedp);
944 continue;
946 case CPP_QUERY:
947 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
948 return 0;
950 default:
951 goto bad_op;
954 top--;
955 if (top->value.overflow && !pfile->state.skip_eval)
956 cpp_error (pfile, CPP_DL_PEDWARN,
957 "integer overflow in preprocessor expression");
960 if (op == CPP_CLOSE_PAREN)
962 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
963 return 0;
966 return top;
969 /* Returns the position of the old top of stack after expansion. */
970 struct op *
971 _cpp_expand_op_stack (cpp_reader *pfile)
973 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
974 size_t new_size = old_size * 2 + 20;
976 pfile->op_stack = xrealloc (pfile->op_stack, new_size * sizeof (struct op));
977 pfile->op_limit = pfile->op_stack + new_size;
979 return pfile->op_stack + old_size;
982 /* Emits a warning if the effective sign of either operand of OP
983 changes because of integer promotions. */
984 static void
985 check_promotion (cpp_reader *pfile, const struct op *op)
987 if (op->value.unsignedp == op[-1].value.unsignedp)
988 return;
990 if (op->value.unsignedp)
992 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
993 cpp_error (pfile, CPP_DL_WARNING,
994 "the left operand of \"%s\" changes sign when promoted",
995 cpp_token_as_text (pfile, op->token));
997 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
998 cpp_error (pfile, CPP_DL_WARNING,
999 "the right operand of \"%s\" changes sign when promoted",
1000 cpp_token_as_text (pfile, op->token));
1003 /* Clears the unused high order bits of the number pointed to by PNUM. */
1004 static cpp_num
1005 num_trim (cpp_num num, size_t precision)
1007 if (precision > PART_PRECISION)
1009 precision -= PART_PRECISION;
1010 if (precision < PART_PRECISION)
1011 num.high &= ((cpp_num_part) 1 << precision) - 1;
1013 else
1015 if (precision < PART_PRECISION)
1016 num.low &= ((cpp_num_part) 1 << precision) - 1;
1017 num.high = 0;
1020 return num;
1023 /* True iff A (presumed signed) >= 0. */
1024 static bool
1025 num_positive (cpp_num num, size_t precision)
1027 if (precision > PART_PRECISION)
1029 precision -= PART_PRECISION;
1030 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1033 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1036 /* Sign extend a number, with PRECISION significant bits and all
1037 others assumed clear, to fill out a cpp_num structure. */
1038 cpp_num
1039 cpp_num_sign_extend (cpp_num num, size_t precision)
1041 if (!num.unsignedp)
1043 if (precision > PART_PRECISION)
1045 precision -= PART_PRECISION;
1046 if (precision < PART_PRECISION
1047 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1048 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1050 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1052 if (precision < PART_PRECISION)
1053 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1054 num.high = ~(cpp_num_part) 0;
1058 return num;
1061 /* Returns the negative of NUM. */
1062 static cpp_num
1063 num_negate (cpp_num num, size_t precision)
1065 cpp_num copy;
1067 copy = num;
1068 num.high = ~num.high;
1069 num.low = ~num.low;
1070 if (++num.low == 0)
1071 num.high++;
1072 num = num_trim (num, precision);
1073 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1075 return num;
1078 /* Returns true if A >= B. */
1079 static bool
1080 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1082 bool unsignedp;
1084 unsignedp = pa.unsignedp || pb.unsignedp;
1086 if (!unsignedp)
1088 /* Both numbers have signed type. If they are of different
1089 sign, the answer is the sign of A. */
1090 unsignedp = num_positive (pa, precision);
1092 if (unsignedp != num_positive (pb, precision))
1093 return unsignedp;
1095 /* Otherwise we can do an unsigned comparison. */
1098 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1101 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1102 static cpp_num
1103 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1104 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1106 lhs.overflow = false;
1107 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1109 /* As excess precision is zeroed, there is no need to num_trim () as
1110 these operations cannot introduce a set bit there. */
1111 if (op == CPP_AND)
1113 lhs.low &= rhs.low;
1114 lhs.high &= rhs.high;
1116 else if (op == CPP_OR)
1118 lhs.low |= rhs.low;
1119 lhs.high |= rhs.high;
1121 else
1123 lhs.low ^= rhs.low;
1124 lhs.high ^= rhs.high;
1127 return lhs;
1130 /* Returns LHS OP RHS, where OP is an inequality. */
1131 static cpp_num
1132 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1133 enum cpp_ttype op)
1135 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1137 if (op == CPP_GREATER_EQ)
1138 lhs.low = gte;
1139 else if (op == CPP_LESS)
1140 lhs.low = !gte;
1141 else if (op == CPP_GREATER)
1142 lhs.low = gte && !num_eq (lhs, rhs);
1143 else /* CPP_LESS_EQ. */
1144 lhs.low = !gte || num_eq (lhs, rhs);
1146 lhs.high = 0;
1147 lhs.overflow = false;
1148 lhs.unsignedp = false;
1149 return lhs;
1152 /* Returns LHS OP RHS, where OP is == or !=. */
1153 static cpp_num
1154 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1155 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1157 /* Work around a 3.0.4 bug; see PR 6950. */
1158 bool eq = num_eq (lhs, rhs);
1159 if (op == CPP_NOT_EQ)
1160 eq = !eq;
1161 lhs.low = eq;
1162 lhs.high = 0;
1163 lhs.overflow = false;
1164 lhs.unsignedp = false;
1165 return lhs;
1168 /* Shift NUM, of width PRECISION, right by N bits. */
1169 static cpp_num
1170 num_rshift (cpp_num num, size_t precision, size_t n)
1172 cpp_num_part sign_mask;
1174 if (num.unsignedp || num_positive (num, precision))
1175 sign_mask = 0;
1176 else
1177 sign_mask = ~(cpp_num_part) 0;
1179 if (n >= precision)
1180 num.high = num.low = sign_mask;
1181 else
1183 /* Sign-extend. */
1184 if (precision < PART_PRECISION)
1185 num.high = sign_mask, num.low |= sign_mask << precision;
1186 else if (precision < 2 * PART_PRECISION)
1187 num.high |= sign_mask << (precision - PART_PRECISION);
1189 if (n >= PART_PRECISION)
1191 n -= PART_PRECISION;
1192 num.low = num.high;
1193 num.high = sign_mask;
1196 if (n)
1198 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1199 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1203 num = num_trim (num, precision);
1204 num.overflow = false;
1205 return num;
1208 /* Shift NUM, of width PRECISION, left by N bits. */
1209 static cpp_num
1210 num_lshift (cpp_num num, size_t precision, size_t n)
1212 if (n >= precision)
1214 num.overflow = !num.unsignedp && !num_zerop (num);
1215 num.high = num.low = 0;
1217 else
1219 cpp_num orig, maybe_orig;
1220 size_t m = n;
1222 orig = num;
1223 if (m >= PART_PRECISION)
1225 m -= PART_PRECISION;
1226 num.high = num.low;
1227 num.low = 0;
1229 if (m)
1231 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1232 num.low <<= m;
1234 num = num_trim (num, precision);
1236 if (num.unsignedp)
1237 num.overflow = false;
1238 else
1240 maybe_orig = num_rshift (num, precision, n);
1241 num.overflow = !num_eq (orig, maybe_orig);
1245 return num;
1248 /* The four unary operators: +, -, ! and ~. */
1249 static cpp_num
1250 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1252 switch (op)
1254 case CPP_UPLUS:
1255 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1256 cpp_error (pfile, CPP_DL_WARNING,
1257 "traditional C rejects the unary plus operator");
1258 num.overflow = false;
1259 break;
1261 case CPP_UMINUS:
1262 num = num_negate (num, CPP_OPTION (pfile, precision));
1263 break;
1265 case CPP_COMPL:
1266 num.high = ~num.high;
1267 num.low = ~num.low;
1268 num = num_trim (num, CPP_OPTION (pfile, precision));
1269 num.overflow = false;
1270 break;
1272 default: /* case CPP_NOT: */
1273 num.low = num_zerop (num);
1274 num.high = 0;
1275 num.overflow = false;
1276 num.unsignedp = false;
1277 break;
1280 return num;
1283 /* The various binary operators. */
1284 static cpp_num
1285 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1287 cpp_num result;
1288 size_t precision = CPP_OPTION (pfile, precision);
1289 bool gte;
1290 size_t n;
1292 switch (op)
1294 /* Shifts. */
1295 case CPP_LSHIFT:
1296 case CPP_RSHIFT:
1297 if (!rhs.unsignedp && !num_positive (rhs, precision))
1299 /* A negative shift is a positive shift the other way. */
1300 if (op == CPP_LSHIFT)
1301 op = CPP_RSHIFT;
1302 else
1303 op = CPP_LSHIFT;
1304 rhs = num_negate (rhs, precision);
1306 if (rhs.high)
1307 n = ~0; /* Maximal. */
1308 else
1309 n = rhs.low;
1310 if (op == CPP_LSHIFT)
1311 lhs = num_lshift (lhs, precision, n);
1312 else
1313 lhs = num_rshift (lhs, precision, n);
1314 break;
1316 /* Min / Max. */
1317 case CPP_MIN:
1318 case CPP_MAX:
1320 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1322 gte = num_greater_eq (lhs, rhs, precision);
1323 if (op == CPP_MIN)
1324 gte = !gte;
1325 if (!gte)
1326 lhs = rhs;
1327 lhs.unsignedp = unsignedp;
1329 break;
1331 /* Arithmetic. */
1332 case CPP_MINUS:
1333 rhs = num_negate (rhs, precision);
1334 case CPP_PLUS:
1335 result.low = lhs.low + rhs.low;
1336 result.high = lhs.high + rhs.high;
1337 if (result.low < lhs.low)
1338 result.high++;
1340 result = num_trim (result, precision);
1341 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1342 if (result.unsignedp)
1343 result.overflow = false;
1344 else
1346 bool lhsp = num_positive (lhs, precision);
1347 result.overflow = (lhsp == num_positive (rhs, precision)
1348 && lhsp != num_positive (result, precision));
1350 return result;
1352 /* Comma. */
1353 default: /* case CPP_COMMA: */
1354 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1355 || !pfile->state.skip_eval))
1356 cpp_error (pfile, CPP_DL_PEDWARN,
1357 "comma operator in operand of #if");
1358 lhs = rhs;
1359 break;
1362 return lhs;
1365 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1366 cannot overflow. */
1367 static cpp_num
1368 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1370 cpp_num result;
1371 cpp_num_part middle[2], temp;
1373 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1374 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1376 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1377 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1379 temp = result.low;
1380 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1381 if (result.low < temp)
1382 result.high++;
1384 temp = result.low;
1385 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1386 if (result.low < temp)
1387 result.high++;
1389 result.high += HIGH_PART (middle[0]);
1390 result.high += HIGH_PART (middle[1]);
1391 result.unsignedp = 1;
1393 return result;
1396 /* Multiply two preprocessing numbers. */
1397 static cpp_num
1398 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1400 cpp_num result, temp;
1401 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1402 bool overflow, negate = false;
1403 size_t precision = CPP_OPTION (pfile, precision);
1405 /* Prepare for unsigned multiplication. */
1406 if (!unsignedp)
1408 if (!num_positive (lhs, precision))
1409 negate = !negate, lhs = num_negate (lhs, precision);
1410 if (!num_positive (rhs, precision))
1411 negate = !negate, rhs = num_negate (rhs, precision);
1414 overflow = lhs.high && rhs.high;
1415 result = num_part_mul (lhs.low, rhs.low);
1417 temp = num_part_mul (lhs.high, rhs.low);
1418 result.high += temp.low;
1419 if (temp.high)
1420 overflow = true;
1422 temp = num_part_mul (lhs.low, rhs.high);
1423 result.high += temp.low;
1424 if (temp.high)
1425 overflow = true;
1427 temp.low = result.low, temp.high = result.high;
1428 result = num_trim (result, precision);
1429 if (!num_eq (result, temp))
1430 overflow = true;
1432 if (negate)
1433 result = num_negate (result, precision);
1435 if (unsignedp)
1436 result.overflow = false;
1437 else
1438 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1439 && !num_zerop (result));
1440 result.unsignedp = unsignedp;
1442 return result;
1445 /* Divide two preprocessing numbers, returning the answer or the
1446 remainder depending upon OP. */
1447 static cpp_num
1448 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1450 cpp_num result, sub;
1451 cpp_num_part mask;
1452 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1453 bool negate = false, lhs_neg = false;
1454 size_t i, precision = CPP_OPTION (pfile, precision);
1456 /* Prepare for unsigned division. */
1457 if (!unsignedp)
1459 if (!num_positive (lhs, precision))
1460 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1461 if (!num_positive (rhs, precision))
1462 negate = !negate, rhs = num_negate (rhs, precision);
1465 /* Find the high bit. */
1466 if (rhs.high)
1468 i = precision - 1;
1469 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1470 for (; ; i--, mask >>= 1)
1471 if (rhs.high & mask)
1472 break;
1474 else if (rhs.low)
1476 if (precision > PART_PRECISION)
1477 i = precision - PART_PRECISION - 1;
1478 else
1479 i = precision - 1;
1480 mask = (cpp_num_part) 1 << i;
1481 for (; ; i--, mask >>= 1)
1482 if (rhs.low & mask)
1483 break;
1485 else
1487 if (!pfile->state.skip_eval)
1488 cpp_error (pfile, CPP_DL_ERROR, "division by zero in #if");
1489 return lhs;
1492 /* First nonzero bit of RHS is bit I. Do naive division by
1493 shifting the RHS fully left, and subtracting from LHS if LHS is
1494 at least as big, and then repeating but with one less shift.
1495 This is not very efficient, but is easy to understand. */
1497 rhs.unsignedp = true;
1498 lhs.unsignedp = true;
1499 i = precision - i - 1;
1500 sub = num_lshift (rhs, precision, i);
1502 result.high = result.low = 0;
1503 for (;;)
1505 if (num_greater_eq (lhs, sub, precision))
1507 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1508 if (i >= PART_PRECISION)
1509 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1510 else
1511 result.low |= (cpp_num_part) 1 << i;
1513 if (i-- == 0)
1514 break;
1515 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1516 sub.high >>= 1;
1519 /* We divide so that the remainder has the sign of the LHS. */
1520 if (op == CPP_DIV)
1522 result.unsignedp = unsignedp;
1523 if (unsignedp)
1524 result.overflow = false;
1525 else
1527 if (negate)
1528 result = num_negate (result, precision);
1529 result.overflow = num_positive (result, precision) ^ !negate;
1532 return result;
1535 /* CPP_MOD. */
1536 lhs.unsignedp = unsignedp;
1537 lhs.overflow = false;
1538 if (lhs_neg)
1539 lhs = num_negate (lhs, precision);
1541 return lhs;