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
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. */
26 typedef struct cpp_num cpp_num;
28 #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
29 #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
30 #define LOW_PART(num_part) (num_part & HALF_MASK)
31 #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
33 /* A preprocessing number. Code assumes that any unused high bits of
34 the double integer are set to zero. */
39 bool unsignedp; /* True if value should be treated as unsigned. */
40 bool overflow; /* True if the most recent calculation overflowed. */
45 cpp_num value; /* The value logically "right" of op. */
49 /* Some simple utility routines on double integers. */
50 #define num_zerop(num) ((num.low | num.high) == 0)
51 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
52 static bool num_positive PARAMS ((cpp_num, size_t));
53 static bool num_greater_eq PARAMS ((cpp_num, cpp_num, size_t));
54 static cpp_num num_trim PARAMS ((cpp_num, size_t));
55 static cpp_num num_part_mul PARAMS ((cpp_num_part, cpp_num_part));
57 static cpp_num num_unary_op PARAMS ((cpp_reader *, cpp_num, enum cpp_ttype));
58 static cpp_num num_binary_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
60 static cpp_num num_negate PARAMS ((cpp_num, size_t));
61 static cpp_num num_bitwise_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
63 static cpp_num num_inequality_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
65 static cpp_num num_equality_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
67 static cpp_num num_mul PARAMS ((cpp_reader *, cpp_num, cpp_num,
69 static cpp_num num_div_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
71 static cpp_num num_lshift PARAMS ((cpp_num, size_t, size_t));
72 static cpp_num num_rshift PARAMS ((cpp_num, size_t, size_t));
74 static cpp_num append_digit PARAMS ((cpp_num, int, int, size_t));
75 static cpp_num interpret_number PARAMS ((cpp_reader *, const cpp_token *));
76 static cpp_num parse_defined PARAMS ((cpp_reader *));
77 static cpp_num eval_token PARAMS ((cpp_reader *, const cpp_token *));
78 static struct op *reduce PARAMS ((cpp_reader *, struct op *, enum cpp_ttype));
80 /* Token type abuse. There is no "error" token, but we can't get
81 comments in #if, so we can abuse that token type. Similarly,
82 create unary plus and minus operators. */
83 #define CPP_ERROR CPP_COMMENT
84 #define CPP_UPLUS (CPP_LAST_CPP_OP + 1)
85 #define CPP_UMINUS (CPP_LAST_CPP_OP + 2)
87 /* With -O2, gcc appears to produce nice code, moving the error
88 message load and subsequent jump completely out of the main path. */
89 #define SYNTAX_ERROR(msgid) \
90 do { cpp_error (pfile, DL_ERROR, msgid); goto syntax_error; } while(0)
91 #define SYNTAX_ERROR2(msgid, arg) \
92 do { cpp_error (pfile, DL_ERROR, msgid, arg); goto syntax_error; } while(0)
96 const unsigned char s[4];
97 const unsigned char u;
98 const unsigned char l;
101 static const struct suffix vsuf_1[] = {
102 { "u", 1, 0 }, { "U", 1, 0 },
103 { "l", 0, 1 }, { "L", 0, 1 }
106 static const struct suffix vsuf_2[] = {
107 { "ul", 1, 1 }, { "UL", 1, 1 }, { "uL", 1, 1 }, { "Ul", 1, 1 },
108 { "lu", 1, 1 }, { "LU", 1, 1 }, { "Lu", 1, 1 }, { "lU", 1, 1 },
109 { "ll", 0, 2 }, { "LL", 0, 2 }
112 static const struct suffix vsuf_3[] = {
113 { "ull", 1, 2 }, { "ULL", 1, 2 }, { "uLL", 1, 2 }, { "Ull", 1, 2 },
114 { "llu", 1, 2 }, { "LLU", 1, 2 }, { "LLu", 1, 2 }, { "llU", 1, 2 }
117 /* Append DIGIT to NUM, a number of PRECISION bits being read in base
120 append_digit (num, digit, base, precision)
126 unsigned int shift = 3 + (base == 16);
128 cpp_num_part add_high, add_low;
130 /* Multiply by 8 or 16. Catching this overflow here means we don't
131 need to worry about add_high overflowing. */
132 overflow = num.high >> (PART_PRECISION - shift);
133 result.high = num.high << shift;
134 result.low = num.low << shift;
135 result.high |= num.low >> (PART_PRECISION - shift);
139 add_low = num.low << 1;
140 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
143 add_high = add_low = 0;
145 if (add_low + digit < add_low)
149 if (result.low + add_low < result.low)
151 if (result.high + add_high < result.high)
154 result.low += add_low;
155 result.high += add_high;
157 /* The above code catches overflow of a cpp_num type. This catches
158 overflow of the (possibly shorter) target precision. */
159 num.low = result.low;
160 num.high = result.high;
161 result = num_trim (result, precision);
162 if (!num_eq (result, num))
165 result.unsignedp = num.unsignedp;
166 result.overflow = overflow;
170 /* Parse and convert what is presumably an integer in TOK. Accepts
171 decimal, hex, or octal with or without size suffixes. Returned op
172 is CPP_ERROR on error, otherwise it is a CPP_NUMBER. */
174 interpret_number (pfile, tok)
176 const cpp_token *tok;
179 const uchar *start = tok->val.str.text;
180 const uchar *end = start + tok->val.str.len;
181 const uchar *p = start;
182 const struct suffix *sufftab;
183 size_t precision = CPP_OPTION (pfile, precision);
184 unsigned int i, nsuff, base = 10, c = 0, largest_digit = 0;
185 bool overflow = false;
187 result.low = result.high = 0;
188 result.unsignedp = 0;
193 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
209 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
214 result = append_digit (result, c, base, precision);
215 overflow |= result.overflow;
216 if (largest_digit < c)
222 /* Check for a floating point constant. Note that float constants
223 with an exponent or suffix but no decimal point are technically
224 invalid (C99 6.4.4.2) but accepted elsewhere. */
225 if ((c == '.' || c == 'F' || c == 'f')
226 || (base == 10 && (c == 'E' || c == 'e')
227 && p+1 < end && (p[1] == '+' || p[1] == '-'))
228 || (base == 16 && (c == 'P' || c == 'p')
229 && p+1 < end && (p[1] == '+' || p[1] == '-')))
230 SYNTAX_ERROR ("floating point numbers are not valid in #if");
232 /* Determine the suffix. l means long, and u means unsigned.
233 See the suffix tables, above. */
236 case 1: sufftab = vsuf_1; nsuff = ARRAY_SIZE (vsuf_1); break;
237 case 2: sufftab = vsuf_2; nsuff = ARRAY_SIZE (vsuf_2); break;
238 case 3: sufftab = vsuf_3; nsuff = ARRAY_SIZE (vsuf_3); break;
239 default: goto invalid_suffix;
242 for (i = 0; i < nsuff; i++)
243 if (memcmp (p, sufftab[i].s, end - p) == 0)
247 result.unsignedp = sufftab[i].u;
249 if (CPP_WTRADITIONAL (pfile)
251 && ! cpp_sys_macro_p (pfile))
252 cpp_error (pfile, DL_WARNING, "traditional C rejects the `U' suffix");
253 if (sufftab[i].l == 2 && CPP_OPTION (pfile, pedantic)
254 && ! CPP_OPTION (pfile, c99))
255 cpp_error (pfile, DL_PEDWARN,
256 "too many 'l' suffixes in integer constant");
259 if (base <= largest_digit)
260 cpp_error (pfile, DL_PEDWARN,
261 "integer constant contains digits beyond the radix");
264 cpp_error (pfile, DL_PEDWARN, "integer constant too large for its type");
265 /* If too big to be signed, consider it unsigned. */
266 else if (!result.unsignedp && !num_positive (result, precision))
269 cpp_error (pfile, DL_WARNING,
270 "integer constant is so large that it is unsigned");
271 result.unsignedp = 1;
277 cpp_error (pfile, DL_ERROR, "invalid suffix '%.*s' on integer constant",
283 /* Handle meeting "defined" in a preprocessor expression. */
285 parse_defined (pfile)
290 cpp_hashnode *node = 0;
291 const cpp_token *token;
292 cpp_context *initial_context = pfile->context;
294 /* Don't expand macros. */
295 pfile->state.prevent_expansion++;
297 token = cpp_get_token (pfile);
298 if (token->type == CPP_OPEN_PAREN)
301 token = cpp_get_token (pfile);
304 if (token->type == CPP_NAME)
306 node = token->val.node;
307 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
309 cpp_error (pfile, DL_ERROR, "missing ')' after \"defined\"");
315 cpp_error (pfile, DL_ERROR,
316 "operator \"defined\" requires an identifier");
317 if (token->flags & NAMED_OP)
322 op.type = token->type;
323 cpp_error (pfile, DL_ERROR,
324 "(\"%s\" is an alternative token for \"%s\" in C++)",
325 cpp_token_as_text (pfile, token),
326 cpp_token_as_text (pfile, &op));
332 if (pfile->context != initial_context)
333 cpp_error (pfile, DL_WARNING,
334 "this use of \"defined\" may not be portable");
336 /* A possible controlling macro of the form #if !defined ().
337 _cpp_parse_expr checks there was no other junk on the line. */
338 pfile->mi_ind_cmacro = node;
341 pfile->state.prevent_expansion--;
343 result.unsignedp = 0;
346 result.low = node && node->type == NT_MACRO;
350 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
351 number or character constant, or the result of the "defined" or "#"
352 operators), or CPP_ERROR on error. */
354 eval_token (pfile, token)
356 const cpp_token *token;
365 return interpret_number (pfile, token);
370 cppchar_t cc = cpp_interpret_charconst (pfile, token,
375 /* Sign-extend the result if necessary. */
376 if (!unsignedp && (cppchar_signed_t) cc < 0)
378 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
379 result.low |= ~(~(cpp_num_part) 0
380 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
381 result.high = ~(cpp_num_part) 0;
382 result = num_trim (result, CPP_OPTION (pfile, precision));
388 if (token->val.node == pfile->spec_nodes.n_defined)
389 return parse_defined (pfile);
390 else if (CPP_OPTION (pfile, cplusplus)
391 && (token->val.node == pfile->spec_nodes.n_true
392 || token->val.node == pfile->spec_nodes.n_false))
395 result.low = (token->val.node == pfile->spec_nodes.n_true);
397 /* Warn about use of true or false in #if when pedantic
398 and stdbool.h has not been included. */
399 if (CPP_PEDANTIC (pfile)
400 && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
401 cpp_error (pfile, DL_PEDWARN,
402 "ISO C++ does not permit \"%s\" in #if",
403 NODE_NAME (token->val.node));
409 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
410 cpp_error (pfile, DL_WARNING, "\"%s\" is not defined",
411 NODE_NAME (token->val.node));
415 default: /* CPP_HASH */
416 _cpp_test_assertion (pfile, &temp);
421 result.unsignedp = unsignedp;
426 /* Operator precedence and flags table.
428 After an operator is returned from the lexer, if it has priority less
429 than the operator on the top of the stack, we reduce the stack by one
430 operator and repeat the test. Since equal priorities do not reduce,
431 this is naturally right-associative.
433 We handle left-associative operators by decrementing the priority of
434 just-lexed operators by one, but retaining the priority of operators
435 already on the stack.
437 The remaining cases are '(' and ')'. We handle '(' by skipping the
438 reduction phase completely. ')' is given lower priority than
439 everything else, including '(', effectively forcing a reduction of the
440 parenthesised expression. If there is a matching '(', the routine
441 reduce() exits immediately. If the normal exit route sees a ')', then
442 there cannot have been a matching '(' and an error message is output.
444 The parser assumes all shifted operators require a left operand unless
445 the flag NO_L_OPERAND is set. These semantics are automatic; any
446 extra semantics need to be handled with operator-specific code. */
449 #define NO_L_OPERAND (1 << 0)
450 #define LEFT_ASSOC (1 << 1)
453 #define UNARY (1 << 0)
454 #define BINARY (1 << 1)
455 #define OTHER (1 << 2)
457 typedef cpp_num (*binary_handler) PARAMS ((cpp_reader *, cpp_num, cpp_num,
459 /* Operator to priority map. Must be in the same order as the first
460 N entries of enum cpp_ttype. */
461 static const struct operator
466 binary_handler handler;
469 /* EQ */ {0, 0, OTHER, NULL}, /* Shouldn't happen. */
470 /* NOT */ {16, NO_L_OPERAND, UNARY, NULL},
471 /* GREATER */ {12, LEFT_ASSOC, BINARY, num_inequality_op},
472 /* LESS */ {12, LEFT_ASSOC, BINARY, num_inequality_op},
473 /* PLUS */ {14, LEFT_ASSOC, BINARY, num_binary_op},
474 /* MINUS */ {14, LEFT_ASSOC, BINARY, num_binary_op},
475 /* MULT */ {15, LEFT_ASSOC, BINARY, num_mul},
476 /* DIV */ {15, LEFT_ASSOC, BINARY, num_div_op},
477 /* MOD */ {15, LEFT_ASSOC, BINARY, num_div_op},
478 /* AND */ {9, LEFT_ASSOC, BINARY, num_bitwise_op},
479 /* OR */ {7, LEFT_ASSOC, BINARY, num_bitwise_op},
480 /* XOR */ {8, LEFT_ASSOC, BINARY, num_bitwise_op},
481 /* RSHIFT */ {13, LEFT_ASSOC, BINARY, num_binary_op},
482 /* LSHIFT */ {13, LEFT_ASSOC, BINARY, num_binary_op},
484 /* MIN */ {10, LEFT_ASSOC, BINARY, num_binary_op},
485 /* MAX */ {10, LEFT_ASSOC, BINARY, num_binary_op},
487 /* COMPL */ {16, NO_L_OPERAND, UNARY, NULL},
488 /* AND_AND */ {6, LEFT_ASSOC, OTHER, NULL},
489 /* OR_OR */ {5, LEFT_ASSOC, OTHER, NULL},
490 /* QUERY */ {3, 0, OTHER, NULL},
491 /* COLON */ {4, LEFT_ASSOC, OTHER, NULL},
492 /* COMMA */ {2, LEFT_ASSOC, BINARY, num_binary_op},
493 /* OPEN_PAREN */ {1, NO_L_OPERAND, OTHER, NULL},
494 /* CLOSE_PAREN */ {0, 0, OTHER, NULL},
495 /* EOF */ {0, 0, OTHER, NULL},
496 /* EQ_EQ */ {11, LEFT_ASSOC, BINARY, num_equality_op},
497 /* NOT_EQ */ {11, LEFT_ASSOC, BINARY, num_equality_op},
498 /* GREATER_EQ */ {12, LEFT_ASSOC, BINARY, num_inequality_op},
499 /* LESS_EQ */ {12, LEFT_ASSOC, BINARY, num_inequality_op},
500 /* UPLUS */ {16, NO_L_OPERAND, UNARY, NULL},
501 /* UMINUS */ {16, NO_L_OPERAND, UNARY, NULL}
504 /* Parse and evaluate a C expression, reading from PFILE.
505 Returns the truth value of the expression.
507 The implementation is an operator precedence parser, i.e. a
508 bottom-up parser, using a stack for not-yet-reduced tokens.
510 The stack base is op_stack, and the current stack pointer is 'top'.
511 There is a stack element for each operator (only), and the most
512 recently pushed operator is 'top->op'. An operand (value) is
513 stored in the 'value' field of the stack element of the operator
516 _cpp_parse_expr (pfile)
519 struct op *top = pfile->op_stack;
520 const cpp_token *token = NULL, *prev_token;
521 unsigned int lex_count;
522 bool saw_leading_not, want_value = true;
524 pfile->state.skip_eval = 0;
526 /* Set up detection of #if ! defined(). */
527 pfile->mi_ind_cmacro = 0;
528 saw_leading_not = false;
531 /* Lowest priority operator prevents further reductions. */
539 token = cpp_get_token (pfile);
545 /* These tokens convert into values. */
552 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
553 cpp_token_as_text (pfile, token));
555 top->value = eval_token (pfile, token);
559 saw_leading_not = lex_count == 1;
570 if (ISGRAPH (token->val.c))
571 SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
573 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
576 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
577 SYNTAX_ERROR2 ("token \"%s\" is not valid in #if expressions",
578 cpp_token_as_text (pfile, token));
582 /* Check we have a value or operator as appropriate. */
583 if (optab[op.op].flags & NO_L_OPERAND)
586 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
587 cpp_token_as_text (pfile, token));
591 /* Ordering here is subtle and intended to favour the
592 missing parenthesis diagnostics over alternatives. */
593 if (op.op == CPP_CLOSE_PAREN)
595 if (top->op == CPP_OPEN_PAREN)
596 SYNTAX_ERROR ("void expression between '(' and ')'");
598 else if (top->op == CPP_EOF)
599 SYNTAX_ERROR ("#if with no expression");
600 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
601 SYNTAX_ERROR2 ("operator '%s' has no right operand",
602 cpp_token_as_text (pfile, prev_token));
605 top = reduce (pfile, top, op.op);
609 if (op.op == CPP_EOF)
614 case CPP_CLOSE_PAREN:
617 if (!num_zerop (top->value))
618 pfile->state.skip_eval++;
622 if (num_zerop (top->value))
623 pfile->state.skip_eval++;
626 if (top->op != CPP_QUERY)
627 SYNTAX_ERROR (" ':' without preceding '?'");
628 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
629 pfile->state.skip_eval++;
631 pfile->state.skip_eval--;
638 /* Check for and handle stack overflow. */
639 if (++top == pfile->op_limit)
640 top = _cpp_expand_op_stack (pfile);
645 /* The controlling macro expression is only valid if we called lex 3
646 times: <!> <defined expression> and <EOF>. push_conditional ()
647 checks that we are at top-of-file. */
648 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
649 pfile->mi_ind_cmacro = 0;
651 if (top != pfile->op_stack)
653 cpp_error (pfile, DL_ICE, "unbalanced stack in #if");
655 return false; /* Return false on syntax error. */
658 return !num_zerop (top->value);
661 /* Reduce the operator / value stack if possible, in preparation for
662 pushing operator OP. Returns NULL on error, otherwise the top of
665 reduce (pfile, top, op)
672 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
675 cpp_error (pfile, DL_ICE, "impossible operator '%u'", top->op);
679 if (op == CPP_OPEN_PAREN)
682 /* Decrement the priority of left-associative operators to force a
683 reduction with operators of otherwise equal priority. */
684 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
685 while (prio < optab[top->op].prio)
687 if (optab[top->op].arity == UNARY)
689 if (!pfile->state.skip_eval)
690 top[-1].value = num_unary_op (pfile, top->value, top->op);
693 else if (optab[top->op].arity == BINARY)
695 if (!pfile->state.skip_eval)
696 top[-1].value = (* (binary_handler) optab[top->op].handler)
697 (pfile, top[-1].value, top->value, top->op);
700 /* Anything changing skip_eval has to be handled here. */
701 else switch (top--->op)
704 if (!num_zerop (top->value))
705 pfile->state.skip_eval--;
706 top->value.low = !num_zerop (top->value) || !num_zerop (top[1].value);
708 top->value.unsignedp = false;
709 top->value.overflow = false;
713 if (num_zerop (top->value))
714 pfile->state.skip_eval--;
715 top->value.low = !num_zerop (top->value) && !num_zerop (top[1].value);
717 top->value.unsignedp = false;
718 top->value.overflow = false;
722 if (op != CPP_CLOSE_PAREN)
724 cpp_error (pfile, DL_ERROR, "missing ')' in expression");
727 top->value = top[1].value;
732 if (!num_zerop (top->value))
734 pfile->state.skip_eval--;
735 top->value = top[1].value;
738 top->value = top[2].value;
739 top->value.unsignedp = (top[1].value.unsignedp
740 || top[2].value.unsignedp);
744 cpp_error (pfile, DL_ERROR, "'?' without following ':'");
751 if (top->value.overflow && !pfile->state.skip_eval)
752 cpp_error (pfile, DL_PEDWARN,
753 "integer overflow in preprocessor expression");
756 if (op == CPP_CLOSE_PAREN)
758 cpp_error (pfile, DL_ERROR, "missing '(' in expression");
765 /* Returns the position of the old top of stack after expansion. */
767 _cpp_expand_op_stack (pfile)
770 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
771 size_t new_size = old_size * 2 + 20;
773 pfile->op_stack = (struct op *) xrealloc (pfile->op_stack,
774 new_size * sizeof (struct op));
775 pfile->op_limit = pfile->op_stack + new_size;
777 return pfile->op_stack + old_size;
780 /* Clears the unused high order bits of the number pointed to by PNUM. */
782 num_trim (num, precision)
786 if (precision > PART_PRECISION)
788 precision -= PART_PRECISION;
789 if (precision < PART_PRECISION)
790 num.high &= (1UL << precision) - 1;
794 if (precision < PART_PRECISION)
795 num.low &= (1UL << precision) - 1;
802 /* True iff A (presumed signed) >= 0. */
804 num_positive (num, precision)
808 if (precision > PART_PRECISION)
810 precision -= PART_PRECISION;
811 return (num.high & (1UL << (precision - 1))) == 0;
814 return (num.low & (1UL << (precision - 1))) == 0;
817 /* Returns the negative of NUM. */
819 num_negate (num, precision)
826 num.high = ~num.high;
830 num = num_trim (num, precision);
831 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
836 /* Returns true if A >= B. */
838 num_greater_eq (pa, pb, precision)
844 unsignedp = pa.unsignedp || pb.unsignedp;
848 /* Both numbers have signed type. If they are of different
849 sign, the answer is the sign of A. */
850 unsignedp = num_positive (pa, precision);
852 if (unsignedp != num_positive (pb, precision))
855 /* Otherwise we can do an unsigned comparison. */
858 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
861 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
863 num_bitwise_op (pfile, lhs, rhs, op)
864 cpp_reader *pfile ATTRIBUTE_UNUSED;
868 lhs.overflow = false;
869 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
871 /* As excess precision is zeroed, there is no need to num_trim () as
872 these operations cannot introduce a set bit there. */
876 lhs.high &= rhs.high;
878 else if (op == CPP_OR)
881 lhs.high |= rhs.high;
886 lhs.high ^= rhs.high;
892 /* Returns LHS OP RHS, where OP is an inequality. */
894 num_inequality_op (pfile, lhs, rhs, op)
899 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
901 if (op == CPP_GREATER_EQ)
903 else if (op == CPP_LESS)
905 else if (op == CPP_GREATER)
906 lhs.low = gte && !num_eq (lhs, rhs);
907 else /* CPP_LESS_EQ. */
908 lhs.low = !gte || num_eq (lhs, rhs);
911 lhs.overflow = false;
912 lhs.unsignedp = false;
916 /* Returns LHS OP RHS, where OP is == or !=. */
918 num_equality_op (pfile, lhs, rhs, op)
919 cpp_reader *pfile ATTRIBUTE_UNUSED;
923 lhs.low = num_eq (lhs, rhs);
924 if (op == CPP_NOT_EQ)
927 lhs.overflow = false;
928 lhs.unsignedp = false;
932 /* Shift NUM, of width PRECISION, right by N bits. */
934 num_rshift (num, precision, n)
938 cpp_num_part sign_mask;
940 if (num.unsignedp || num_positive (num, precision))
943 sign_mask = ~(cpp_num_part) 0;
946 num.high = num.low = sign_mask;
950 if (precision < PART_PRECISION)
951 num.high = sign_mask, num.low |= sign_mask << precision;
952 else if (precision < 2 * PART_PRECISION)
953 num.high |= sign_mask << (precision - PART_PRECISION);
955 if (n >= PART_PRECISION)
959 num.high = sign_mask;
964 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
965 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
969 num = num_trim (num, precision);
970 num.overflow = false;
974 /* Shift NUM, of width PRECISION, left by N bits. */
976 num_lshift (num, precision, n)
982 num.overflow = !num.unsignedp && !num_zerop (num);
983 num.high = num.low = 0;
987 cpp_num orig, maybe_orig;
991 if (m >= PART_PRECISION)
999 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1002 num = num_trim (num, precision);
1005 num.overflow = false;
1008 maybe_orig = num_rshift (num, precision, n);
1009 num.overflow = !num_eq (orig, maybe_orig);
1016 /* The four unary operators: +, -, ! and ~. */
1018 num_unary_op (pfile, num, op)
1026 if (CPP_WTRADITIONAL (pfile))
1027 cpp_error (pfile, DL_WARNING,
1028 "traditional C rejects the unary plus operator");
1029 num.overflow = false;
1033 num = num_negate (num, CPP_OPTION (pfile, precision));
1037 num.high = ~num.high;
1039 num = num_trim (num, CPP_OPTION (pfile, precision));
1040 num.overflow = false;
1043 default: /* case CPP_NOT: */
1044 num.low = num_zerop (num);
1046 num.overflow = false;
1047 num.unsignedp = false;
1054 /* The various binary operators. */
1056 num_binary_op (pfile, lhs, rhs, op)
1062 size_t precision = CPP_OPTION (pfile, precision);
1071 if (!rhs.unsignedp && !num_positive (rhs, precision))
1073 /* A negative shift is a positive shift the other way. */
1074 if (op == CPP_LSHIFT)
1078 rhs = num_negate (rhs, precision);
1081 n = ~0; /* Maximal. */
1084 if (op == CPP_LSHIFT)
1085 lhs = num_lshift (lhs, precision, n);
1087 lhs = num_rshift (lhs, precision, n);
1094 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1096 gte = num_greater_eq (lhs, rhs, precision);
1101 lhs.unsignedp = unsignedp;
1107 rhs = num_negate (rhs, precision);
1109 result.low = lhs.low + rhs.low;
1110 result.high = lhs.high + rhs.high;
1111 if (result.low < lhs.low)
1114 result = num_trim (result, precision);
1115 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1116 if (result.unsignedp)
1117 result.overflow = false;
1120 bool lhsp = num_positive (lhs, precision);
1121 result.overflow = (lhsp == num_positive (rhs, precision)
1122 && lhsp != num_positive (result, precision));
1127 default: /* case CPP_COMMA: */
1128 if (CPP_PEDANTIC (pfile))
1129 cpp_error (pfile, DL_PEDWARN,
1130 "comma operator in operand of #if");
1138 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1141 num_part_mul (lhs, rhs)
1142 cpp_num_part lhs, rhs;
1145 cpp_num_part middle[2], temp;
1147 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1148 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1150 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1151 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1154 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1155 if (result.low < temp)
1159 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1160 if (result.low < temp)
1163 result.high += HIGH_PART (middle[0]);
1164 result.high += HIGH_PART (middle[1]);
1169 /* Multiply two preprocessing numbers. */
1171 num_mul (pfile, lhs, rhs, op)
1174 enum cpp_ttype op ATTRIBUTE_UNUSED;
1176 cpp_num result, temp;
1177 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1178 bool overflow, negate = false;
1179 size_t precision = CPP_OPTION (pfile, precision);
1181 /* Prepare for unsigned multiplication. */
1184 if (!num_positive (lhs, precision))
1185 negate = !negate, lhs = num_negate (lhs, precision);
1186 if (!num_positive (rhs, precision))
1187 negate = !negate, rhs = num_negate (rhs, precision);
1190 overflow = lhs.high && rhs.high;
1191 result = num_part_mul (lhs.low, rhs.low);
1193 temp = num_part_mul (lhs.high, rhs.low);
1194 result.high += temp.low;
1198 temp = num_part_mul (lhs.low, rhs.high);
1199 result.high += temp.low;
1203 temp.low = result.low, temp.high = result.high;
1204 result = num_trim (result, precision);
1205 if (!num_eq (result, temp))
1209 result = num_negate (result, precision);
1212 result.overflow = false;
1214 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1215 && !num_zerop (result));
1216 result.unsignedp = unsignedp;
1221 /* Divide two preprocessing numbers, returning the answer or the
1222 remainder depending upon OP. */
1224 num_div_op (pfile, lhs, rhs, op)
1229 cpp_num result, sub;
1231 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1232 bool negate = false, lhs_neg = false;
1233 size_t i, precision = CPP_OPTION (pfile, precision);
1235 /* Prepare for unsigned division. */
1238 if (!num_positive (lhs, precision))
1239 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1240 if (!num_positive (rhs, precision))
1241 negate = !negate, rhs = num_negate (rhs, precision);
1244 /* Find the high bit. */
1248 mask = 1UL << (i - PART_PRECISION);
1249 for (; ; i--, mask >>= 1)
1250 if (rhs.high & mask)
1255 if (precision > PART_PRECISION)
1256 i = precision - PART_PRECISION - 1;
1260 for (; ; i--, mask >>= 1)
1266 cpp_error (pfile, DL_ERROR, "division by zero in #if");
1270 /* First non-zero bit of RHS is bit I. Do naive division by
1271 shifting the RHS fully left, and subtracting from LHS if LHS is
1272 at least as big, and then repeating but with one less shift.
1273 This is not very efficient, but is easy to understand. */
1275 rhs.unsignedp = true;
1276 lhs.unsignedp = true;
1277 i = precision - i - 1;
1278 sub = num_lshift (rhs, precision, i);
1280 result.high = result.low = 0;
1283 if (num_greater_eq (lhs, sub, precision))
1285 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1286 if (i >= PART_PRECISION)
1287 result.high |= 1UL << (i - PART_PRECISION);
1289 result.low |= 1UL << i;
1293 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1297 /* We divide so that the remainder has the sign of the LHS. */
1300 result.unsignedp = unsignedp;
1302 result.overflow = false;
1306 result = num_negate (result, precision);
1307 result.overflow = num_positive (result, precision) ^ !negate;
1314 lhs.unsignedp = unsignedp;
1315 lhs.overflow = false;
1317 lhs = num_negate (lhs, precision);