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 #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))
33 cpp_num value; /* The value logically "right" of op. */
37 /* Some simple utility routines on double integers. */
38 #define num_zerop(num) ((num.low | num.high) == 0)
39 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
40 static bool num_positive PARAMS ((cpp_num, size_t));
41 static bool num_greater_eq PARAMS ((cpp_num, cpp_num, size_t));
42 static cpp_num num_trim PARAMS ((cpp_num, size_t));
43 static cpp_num num_part_mul PARAMS ((cpp_num_part, cpp_num_part));
45 static cpp_num num_unary_op PARAMS ((cpp_reader *, cpp_num, enum cpp_ttype));
46 static cpp_num num_binary_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
48 static cpp_num num_negate PARAMS ((cpp_num, size_t));
49 static cpp_num num_bitwise_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
51 static cpp_num num_inequality_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
53 static cpp_num num_equality_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
55 static cpp_num num_mul PARAMS ((cpp_reader *, cpp_num, cpp_num,
57 static cpp_num num_div_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
59 static cpp_num num_lshift PARAMS ((cpp_num, size_t, size_t));
60 static cpp_num num_rshift PARAMS ((cpp_num, size_t, size_t));
62 static cpp_num append_digit PARAMS ((cpp_num, int, int, size_t));
63 static cpp_num parse_defined PARAMS ((cpp_reader *));
64 static cpp_num eval_token PARAMS ((cpp_reader *, const cpp_token *));
65 static struct op *reduce PARAMS ((cpp_reader *, struct op *, enum cpp_ttype));
66 static unsigned int interpret_float_suffix PARAMS ((const uchar *, size_t));
67 static unsigned int interpret_int_suffix PARAMS ((const uchar *, size_t));
69 /* Token type abuse to create unary plus and minus operators. */
70 #define CPP_UPLUS (CPP_LAST_CPP_OP + 1)
71 #define CPP_UMINUS (CPP_LAST_CPP_OP + 2)
73 /* With -O2, gcc appears to produce nice code, moving the error
74 message load and subsequent jump completely out of the main path. */
75 #define SYNTAX_ERROR(msgid) \
76 do { cpp_error (pfile, DL_ERROR, msgid); goto syntax_error; } while(0)
77 #define SYNTAX_ERROR2(msgid, arg) \
78 do { cpp_error (pfile, DL_ERROR, msgid, arg); goto syntax_error; } while(0)
80 /* Subroutine of cpp_classify_number. S points to a float suffix of
81 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
82 flag vector describing the suffix. */
84 interpret_float_suffix (s, len)
88 size_t f = 0, l = 0, i = 0;
93 case 'f': case 'F': f++; break;
94 case 'l': case 'L': l++; break;
96 case 'j': case 'J': i++; break;
101 if (f + l > 1 || i > 1)
104 return ((i ? CPP_N_IMAGINARY : 0)
106 l ? CPP_N_LARGE : CPP_N_MEDIUM));
109 /* Subroutine of cpp_classify_number. S points to an integer suffix
110 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
111 flag vector describing the suffix. */
113 interpret_int_suffix (s, len)
124 case 'u': case 'U': u++; break;
126 case 'j': case 'J': i++; break;
127 case 'l': case 'L': l++;
128 /* If there are two Ls, they must be adjacent and the same case. */
129 if (l == 2 && s[len] != s[len + 1])
136 if (l > 2 || u > 1 || i > 1)
139 return ((i ? CPP_N_IMAGINARY : 0)
140 | (u ? CPP_N_UNSIGNED : 0)
141 | ((l == 0) ? CPP_N_SMALL
142 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
145 /* Categorize numeric constants according to their field (integer,
146 floating point, or invalid), radix (decimal, octal, hexadecimal),
147 and type suffixes. */
149 cpp_classify_number (pfile, token)
151 const cpp_token *token;
153 const uchar *str = token->val.str.text;
155 unsigned int max_digit, result, radix;
156 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
158 /* If the lexer has done its job, length one can only be a single
159 digit. Fast-path this very common case. */
160 if (token->val.str.len == 1)
161 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
163 limit = str + token->val.str.len;
164 float_flag = NOT_FLOAT;
168 /* First, interpret the radix. */
174 /* Require at least one hex digit to classify it as hex. */
175 if ((*str == 'x' || *str == 'X') && ISXDIGIT (str[1]))
182 /* Now scan for a well-formed integer or float. */
185 unsigned int c = *str++;
187 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
195 if (float_flag == NOT_FLOAT)
196 float_flag = AFTER_POINT;
198 SYNTAX_ERROR ("too many decimal points in number");
200 else if ((radix <= 10 && (c == 'e' || c == 'E'))
201 || (radix == 16 && (c == 'p' || c == 'P')))
203 float_flag = AFTER_EXPON;
208 /* Start of suffix. */
214 if (float_flag != NOT_FLOAT && radix == 8)
217 if (max_digit >= radix)
218 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
220 if (float_flag != NOT_FLOAT)
222 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
223 cpp_error (pfile, DL_PEDWARN,
224 "use of C99 hexadecimal floating constant");
226 if (float_flag == AFTER_EXPON)
228 if (*str == '+' || *str == '-')
231 /* Exponent is decimal, even if string is a hex float. */
233 SYNTAX_ERROR ("exponent has no digits");
237 while (ISDIGIT (*str));
239 else if (radix == 16)
240 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
242 result = interpret_float_suffix (str, limit - str);
245 cpp_error (pfile, DL_ERROR,
246 "invalid suffix \"%.*s\" on floating constant",
247 (int) (limit - str), str);
248 return CPP_N_INVALID;
251 /* Traditional C didn't accept any floating suffixes. */
253 && CPP_WTRADITIONAL (pfile)
254 && ! cpp_sys_macro_p (pfile))
255 cpp_error (pfile, DL_WARNING,
256 "traditional C rejects the \"%.*s\" suffix",
257 (int) (limit - str), str);
259 result |= CPP_N_FLOATING;
263 result = interpret_int_suffix (str, limit - str);
266 cpp_error (pfile, DL_ERROR,
267 "invalid suffix \"%.*s\" on integer constant",
268 (int) (limit - str), str);
269 return CPP_N_INVALID;
272 /* Traditional C only accepted the 'L' suffix. */
273 if (result != CPP_N_SMALL && result != CPP_N_MEDIUM
274 && CPP_WTRADITIONAL (pfile)
275 && ! cpp_sys_macro_p (pfile))
276 cpp_error (pfile, DL_WARNING,
277 "traditional C rejects the \"%.*s\" suffix",
278 (int) (limit - str), str);
280 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
281 && ! CPP_OPTION (pfile, c99)
282 && CPP_OPTION (pfile, warn_long_long))
283 cpp_error (pfile, DL_PEDWARN, "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, DL_PEDWARN, "imaginary constants are a GCC extension");
292 result |= CPP_N_DECIMAL;
293 else if (radix == 16)
296 result |= CPP_N_OCTAL;
301 return CPP_N_INVALID;
304 /* cpp_interpret_integer converts an integer constant into a cpp_num,
305 of precision options->precision.
307 We do not provide any interface for decimal->float conversion,
308 because the preprocessor doesn't need it and the floating point
309 handling in GCC proper is too ugly to speak of. */
311 cpp_interpret_integer (pfile, token, type)
313 const cpp_token *token;
316 const uchar *p, *end;
321 result.unsignedp = !!(type & CPP_N_UNSIGNED);
322 result.overflow = false;
324 p = token->val.str.text;
325 end = p + token->val.str.len;
327 /* Common case of a single digit. */
328 if (token->val.str.len == 1)
329 result.low = p[0] - '0';
333 size_t precision = CPP_OPTION (pfile, precision);
334 unsigned int base = 10, c = 0;
335 bool overflow = false;
337 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
342 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
348 /* We can add a digit to numbers strictly less than this without
349 needing the precision and slowness of double integers. */
350 max = ~(cpp_num_part) 0;
351 if (precision < PART_PRECISION)
352 max >>= PART_PRECISION - precision;
353 max = (max - base + 1) / base + 1;
359 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
364 /* Strict inequality for when max is set to zero. */
365 if (result.low < max)
366 result.low = result.low * base + c;
369 result = append_digit (result, c, base, precision);
370 overflow |= result.overflow;
376 cpp_error (pfile, DL_PEDWARN,
377 "integer constant is too large for its type");
378 /* If too big to be signed, consider it unsigned. Only warn for
379 decimal numbers. Traditional numbers were always signed (but
380 we still honour an explicit U suffix); but we only have
381 traditional semantics in directives. */
382 else if (!result.unsignedp
383 && !(CPP_OPTION (pfile, traditional)
384 && pfile->state.in_directive)
385 && !num_positive (result, precision))
388 cpp_error (pfile, DL_WARNING,
389 "integer constant is so large that it is unsigned");
390 result.unsignedp = true;
397 /* Append DIGIT to NUM, a number of PRECISION bits being read in base
400 append_digit (num, digit, base, precision)
406 unsigned int shift = 3 + (base == 16);
408 cpp_num_part add_high, add_low;
410 /* Multiply by 8 or 16. Catching this overflow here means we don't
411 need to worry about add_high overflowing. */
412 overflow = !!(num.high >> (PART_PRECISION - shift));
413 result.high = num.high << shift;
414 result.low = num.low << shift;
415 result.high |= num.low >> (PART_PRECISION - shift);
419 add_low = num.low << 1;
420 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
423 add_high = add_low = 0;
425 if (add_low + digit < add_low)
429 if (result.low + add_low < result.low)
431 if (result.high + add_high < result.high)
434 result.low += add_low;
435 result.high += add_high;
437 /* The above code catches overflow of a cpp_num type. This catches
438 overflow of the (possibly shorter) target precision. */
439 num.low = result.low;
440 num.high = result.high;
441 result = num_trim (result, precision);
442 if (!num_eq (result, num))
445 result.unsignedp = num.unsignedp;
446 result.overflow = overflow;
450 /* Handle meeting "defined" in a preprocessor expression. */
452 parse_defined (pfile)
457 cpp_hashnode *node = 0;
458 const cpp_token *token;
459 cpp_context *initial_context = pfile->context;
461 /* Don't expand macros. */
462 pfile->state.prevent_expansion++;
464 token = cpp_get_token (pfile);
465 if (token->type == CPP_OPEN_PAREN)
468 token = cpp_get_token (pfile);
471 if (token->type == CPP_NAME)
473 node = token->val.node;
474 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
476 cpp_error (pfile, DL_ERROR, "missing ')' after \"defined\"");
482 cpp_error (pfile, DL_ERROR,
483 "operator \"defined\" requires an identifier");
484 if (token->flags & NAMED_OP)
489 op.type = token->type;
490 cpp_error (pfile, DL_ERROR,
491 "(\"%s\" is an alternative token for \"%s\" in C++)",
492 cpp_token_as_text (pfile, token),
493 cpp_token_as_text (pfile, &op));
499 if (pfile->context != initial_context)
500 cpp_error (pfile, DL_WARNING,
501 "this use of \"defined\" may not be portable");
503 /* A possible controlling macro of the form #if !defined ().
504 _cpp_parse_expr checks there was no other junk on the line. */
505 pfile->mi_ind_cmacro = node;
508 pfile->state.prevent_expansion--;
510 result.unsignedp = false;
512 result.overflow = false;
513 result.low = node && node->type == NT_MACRO;
517 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
518 number or character constant, or the result of the "defined" or "#"
521 eval_token (pfile, token)
523 const cpp_token *token;
532 temp = cpp_classify_number (pfile, token);
533 switch (temp & CPP_N_CATEGORY)
536 cpp_error (pfile, DL_ERROR,
537 "floating constant in preprocessor expression");
540 if (!(temp & CPP_N_IMAGINARY))
541 return cpp_interpret_integer (pfile, token, temp);
542 cpp_error (pfile, DL_ERROR,
543 "imaginary number in preprocessor expression");
547 /* Error already issued. */
550 result.high = result.low = 0;
556 cppchar_t cc = cpp_interpret_charconst (pfile, token,
561 /* Sign-extend the result if necessary. */
562 if (!unsignedp && (cppchar_signed_t) cc < 0)
564 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
565 result.low |= ~(~(cpp_num_part) 0
566 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
567 result.high = ~(cpp_num_part) 0;
568 result = num_trim (result, CPP_OPTION (pfile, precision));
574 if (token->val.node == pfile->spec_nodes.n_defined)
575 return parse_defined (pfile);
576 else if (CPP_OPTION (pfile, cplusplus)
577 && (token->val.node == pfile->spec_nodes.n_true
578 || token->val.node == pfile->spec_nodes.n_false))
581 result.low = (token->val.node == pfile->spec_nodes.n_true);
583 /* Warn about use of true or false in #if when pedantic
584 and stdbool.h has not been included. */
585 if (CPP_PEDANTIC (pfile)
586 && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
587 cpp_error (pfile, DL_PEDWARN,
588 "ISO C++ does not permit \"%s\" in #if",
589 NODE_NAME (token->val.node));
595 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
596 cpp_error (pfile, DL_WARNING, "\"%s\" is not defined",
597 NODE_NAME (token->val.node));
601 default: /* CPP_HASH */
602 _cpp_test_assertion (pfile, &temp);
607 result.unsignedp = !!unsignedp;
608 result.overflow = false;
612 /* Operator precedence and flags table.
614 After an operator is returned from the lexer, if it has priority less
615 than the operator on the top of the stack, we reduce the stack by one
616 operator and repeat the test. Since equal priorities do not reduce,
617 this is naturally right-associative.
619 We handle left-associative operators by decrementing the priority of
620 just-lexed operators by one, but retaining the priority of operators
621 already on the stack.
623 The remaining cases are '(' and ')'. We handle '(' by skipping the
624 reduction phase completely. ')' is given lower priority than
625 everything else, including '(', effectively forcing a reduction of the
626 parenthesised expression. If there is a matching '(', the routine
627 reduce() exits immediately. If the normal exit route sees a ')', then
628 there cannot have been a matching '(' and an error message is output.
630 The parser assumes all shifted operators require a left operand unless
631 the flag NO_L_OPERAND is set. These semantics are automatic; any
632 extra semantics need to be handled with operator-specific code. */
635 #define NO_L_OPERAND (1 << 0)
636 #define LEFT_ASSOC (1 << 1)
639 #define UNARY (1 << 0)
640 #define BINARY (1 << 1)
641 #define OTHER (1 << 2)
643 typedef cpp_num (*binary_handler) PARAMS ((cpp_reader *, cpp_num, cpp_num,
645 /* Operator to priority map. Must be in the same order as the first
646 N entries of enum cpp_ttype. */
647 static const struct operator
652 binary_handler handler;
655 /* EQ */ {0, 0, OTHER, NULL}, /* Shouldn't happen. */
656 /* NOT */ {16, NO_L_OPERAND, UNARY, NULL},
657 /* GREATER */ {12, LEFT_ASSOC, BINARY, num_inequality_op},
658 /* LESS */ {12, LEFT_ASSOC, BINARY, num_inequality_op},
659 /* PLUS */ {14, LEFT_ASSOC, BINARY, num_binary_op},
660 /* MINUS */ {14, LEFT_ASSOC, BINARY, num_binary_op},
661 /* MULT */ {15, LEFT_ASSOC, BINARY, num_mul},
662 /* DIV */ {15, LEFT_ASSOC, BINARY, num_div_op},
663 /* MOD */ {15, LEFT_ASSOC, BINARY, num_div_op},
664 /* AND */ {9, LEFT_ASSOC, BINARY, num_bitwise_op},
665 /* OR */ {7, LEFT_ASSOC, BINARY, num_bitwise_op},
666 /* XOR */ {8, LEFT_ASSOC, BINARY, num_bitwise_op},
667 /* RSHIFT */ {13, LEFT_ASSOC, BINARY, num_binary_op},
668 /* LSHIFT */ {13, LEFT_ASSOC, BINARY, num_binary_op},
670 /* MIN */ {10, LEFT_ASSOC, BINARY, num_binary_op},
671 /* MAX */ {10, LEFT_ASSOC, BINARY, num_binary_op},
673 /* COMPL */ {16, NO_L_OPERAND, UNARY, NULL},
674 /* AND_AND */ {6, LEFT_ASSOC, OTHER, NULL},
675 /* OR_OR */ {5, LEFT_ASSOC, OTHER, NULL},
676 /* QUERY */ {3, 0, OTHER, NULL},
677 /* COLON */ {4, LEFT_ASSOC, OTHER, NULL},
678 /* COMMA */ {2, LEFT_ASSOC, BINARY, num_binary_op},
679 /* OPEN_PAREN */ {1, NO_L_OPERAND, OTHER, NULL},
680 /* CLOSE_PAREN */ {0, 0, OTHER, NULL},
681 /* EOF */ {0, 0, OTHER, NULL},
682 /* EQ_EQ */ {11, LEFT_ASSOC, BINARY, num_equality_op},
683 /* NOT_EQ */ {11, LEFT_ASSOC, BINARY, num_equality_op},
684 /* GREATER_EQ */ {12, LEFT_ASSOC, BINARY, num_inequality_op},
685 /* LESS_EQ */ {12, LEFT_ASSOC, BINARY, num_inequality_op},
686 /* UPLUS */ {16, NO_L_OPERAND, UNARY, NULL},
687 /* UMINUS */ {16, NO_L_OPERAND, UNARY, NULL}
690 /* Parse and evaluate a C expression, reading from PFILE.
691 Returns the truth value of the expression.
693 The implementation is an operator precedence parser, i.e. a
694 bottom-up parser, using a stack for not-yet-reduced tokens.
696 The stack base is op_stack, and the current stack pointer is 'top'.
697 There is a stack element for each operator (only), and the most
698 recently pushed operator is 'top->op'. An operand (value) is
699 stored in the 'value' field of the stack element of the operator
702 _cpp_parse_expr (pfile)
705 struct op *top = pfile->op_stack;
706 const cpp_token *token = NULL, *prev_token;
707 unsigned int lex_count;
708 bool saw_leading_not, want_value = true;
710 pfile->state.skip_eval = 0;
712 /* Set up detection of #if ! defined(). */
713 pfile->mi_ind_cmacro = 0;
714 saw_leading_not = false;
717 /* Lowest priority operator prevents further reductions. */
725 token = cpp_get_token (pfile);
731 /* These tokens convert into values. */
738 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
739 cpp_token_as_text (pfile, token));
741 top->value = eval_token (pfile, token);
745 saw_leading_not = lex_count == 1;
756 if (ISGRAPH (token->val.c))
757 SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
759 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
762 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
763 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
764 cpp_token_as_text (pfile, token));
768 /* Check we have a value or operator as appropriate. */
769 if (optab[op.op].flags & NO_L_OPERAND)
772 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
773 cpp_token_as_text (pfile, token));
777 /* Ordering here is subtle and intended to favour the
778 missing parenthesis diagnostics over alternatives. */
779 if (op.op == CPP_CLOSE_PAREN)
781 if (top->op == CPP_OPEN_PAREN)
782 SYNTAX_ERROR ("void expression between '(' and ')'");
784 else if (top->op == CPP_EOF)
785 SYNTAX_ERROR ("#if with no expression");
786 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
787 SYNTAX_ERROR2 ("operator '%s' has no right operand",
788 cpp_token_as_text (pfile, prev_token));
791 top = reduce (pfile, top, op.op);
795 if (op.op == CPP_EOF)
800 case CPP_CLOSE_PAREN:
803 if (!num_zerop (top->value))
804 pfile->state.skip_eval++;
808 if (num_zerop (top->value))
809 pfile->state.skip_eval++;
812 if (top->op != CPP_QUERY)
813 SYNTAX_ERROR (" ':' without preceding '?'");
814 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
815 pfile->state.skip_eval++;
817 pfile->state.skip_eval--;
824 /* Check for and handle stack overflow. */
825 if (++top == pfile->op_limit)
826 top = _cpp_expand_op_stack (pfile);
831 /* The controlling macro expression is only valid if we called lex 3
832 times: <!> <defined expression> and <EOF>. push_conditional ()
833 checks that we are at top-of-file. */
834 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
835 pfile->mi_ind_cmacro = 0;
837 if (top != pfile->op_stack)
839 cpp_error (pfile, DL_ICE, "unbalanced stack in #if");
841 return false; /* Return false on syntax error. */
844 return !num_zerop (top->value);
847 /* Reduce the operator / value stack if possible, in preparation for
848 pushing operator OP. Returns NULL on error, otherwise the top of
851 reduce (pfile, top, op)
858 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
861 cpp_error (pfile, DL_ICE, "impossible operator '%u'", top->op);
865 if (op == CPP_OPEN_PAREN)
868 /* Decrement the priority of left-associative operators to force a
869 reduction with operators of otherwise equal priority. */
870 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
871 while (prio < optab[top->op].prio)
873 if (optab[top->op].arity == UNARY)
875 if (!pfile->state.skip_eval)
876 top[-1].value = num_unary_op (pfile, top->value, top->op);
879 else if (optab[top->op].arity == BINARY)
881 if (!pfile->state.skip_eval)
882 top[-1].value = (* (binary_handler) optab[top->op].handler)
883 (pfile, top[-1].value, top->value, top->op);
886 /* Anything changing skip_eval has to be handled here. */
887 else switch (top--->op)
890 if (!num_zerop (top->value))
891 pfile->state.skip_eval--;
892 top->value.low = !num_zerop (top->value) || !num_zerop (top[1].value);
894 top->value.unsignedp = false;
895 top->value.overflow = false;
899 if (num_zerop (top->value))
900 pfile->state.skip_eval--;
901 top->value.low = !num_zerop (top->value) && !num_zerop (top[1].value);
903 top->value.unsignedp = false;
904 top->value.overflow = false;
908 if (op != CPP_CLOSE_PAREN)
910 cpp_error (pfile, DL_ERROR, "missing ')' in expression");
913 top->value = top[1].value;
918 if (!num_zerop (top->value))
920 pfile->state.skip_eval--;
921 top->value = top[1].value;
924 top->value = top[2].value;
925 top->value.unsignedp = (top[1].value.unsignedp
926 || top[2].value.unsignedp);
930 cpp_error (pfile, DL_ERROR, "'?' without following ':'");
937 if (top->value.overflow && !pfile->state.skip_eval)
938 cpp_error (pfile, DL_PEDWARN,
939 "integer overflow in preprocessor expression");
942 if (op == CPP_CLOSE_PAREN)
944 cpp_error (pfile, DL_ERROR, "missing '(' in expression");
951 /* Returns the position of the old top of stack after expansion. */
953 _cpp_expand_op_stack (pfile)
956 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
957 size_t new_size = old_size * 2 + 20;
959 pfile->op_stack = (struct op *) xrealloc (pfile->op_stack,
960 new_size * sizeof (struct op));
961 pfile->op_limit = pfile->op_stack + new_size;
963 return pfile->op_stack + old_size;
966 /* Clears the unused high order bits of the number pointed to by PNUM. */
968 num_trim (num, precision)
972 if (precision > PART_PRECISION)
974 precision -= PART_PRECISION;
975 if (precision < PART_PRECISION)
976 num.high &= ((cpp_num_part) 1 << precision) - 1;
980 if (precision < PART_PRECISION)
981 num.low &= ((cpp_num_part) 1 << precision) - 1;
988 /* True iff A (presumed signed) >= 0. */
990 num_positive (num, precision)
994 if (precision > PART_PRECISION)
996 precision -= PART_PRECISION;
997 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1000 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1003 /* Sign extend a number, with PRECISION significant bits and all
1004 others assumed clear, to fill out a cpp_num structure. */
1006 cpp_num_sign_extend (num, precision)
1012 if (precision > PART_PRECISION)
1014 precision -= PART_PRECISION;
1015 if (precision < PART_PRECISION
1016 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1017 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1019 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1021 if (precision < PART_PRECISION)
1022 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1023 num.high = ~(cpp_num_part) 0;
1030 /* Returns the negative of NUM. */
1032 num_negate (num, precision)
1039 num.high = ~num.high;
1043 num = num_trim (num, precision);
1044 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1049 /* Returns true if A >= B. */
1051 num_greater_eq (pa, pb, precision)
1057 unsignedp = pa.unsignedp || pb.unsignedp;
1061 /* Both numbers have signed type. If they are of different
1062 sign, the answer is the sign of A. */
1063 unsignedp = num_positive (pa, precision);
1065 if (unsignedp != num_positive (pb, precision))
1068 /* Otherwise we can do an unsigned comparison. */
1071 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1074 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1076 num_bitwise_op (pfile, lhs, rhs, op)
1077 cpp_reader *pfile ATTRIBUTE_UNUSED;
1081 lhs.overflow = false;
1082 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1084 /* As excess precision is zeroed, there is no need to num_trim () as
1085 these operations cannot introduce a set bit there. */
1089 lhs.high &= rhs.high;
1091 else if (op == CPP_OR)
1094 lhs.high |= rhs.high;
1099 lhs.high ^= rhs.high;
1105 /* Returns LHS OP RHS, where OP is an inequality. */
1107 num_inequality_op (pfile, lhs, rhs, op)
1112 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1114 if (op == CPP_GREATER_EQ)
1116 else if (op == CPP_LESS)
1118 else if (op == CPP_GREATER)
1119 lhs.low = gte && !num_eq (lhs, rhs);
1120 else /* CPP_LESS_EQ. */
1121 lhs.low = !gte || num_eq (lhs, rhs);
1124 lhs.overflow = false;
1125 lhs.unsignedp = false;
1129 /* Returns LHS OP RHS, where OP is == or !=. */
1131 num_equality_op (pfile, lhs, rhs, op)
1132 cpp_reader *pfile ATTRIBUTE_UNUSED;
1136 /* Work around a 3.0.4 bug; see PR 6950. */
1137 bool eq = num_eq (lhs, rhs);
1138 if (op == CPP_NOT_EQ)
1142 lhs.overflow = false;
1143 lhs.unsignedp = false;
1147 /* Shift NUM, of width PRECISION, right by N bits. */
1149 num_rshift (num, precision, n)
1151 size_t precision, n;
1153 cpp_num_part sign_mask;
1155 if (num.unsignedp || num_positive (num, precision))
1158 sign_mask = ~(cpp_num_part) 0;
1161 num.high = num.low = sign_mask;
1165 if (precision < PART_PRECISION)
1166 num.high = sign_mask, num.low |= sign_mask << precision;
1167 else if (precision < 2 * PART_PRECISION)
1168 num.high |= sign_mask << (precision - PART_PRECISION);
1170 if (n >= PART_PRECISION)
1172 n -= PART_PRECISION;
1174 num.high = sign_mask;
1179 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1180 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1184 num = num_trim (num, precision);
1185 num.overflow = false;
1189 /* Shift NUM, of width PRECISION, left by N bits. */
1191 num_lshift (num, precision, n)
1193 size_t precision, n;
1197 num.overflow = !num.unsignedp && !num_zerop (num);
1198 num.high = num.low = 0;
1202 cpp_num orig, maybe_orig;
1206 if (m >= PART_PRECISION)
1208 m -= PART_PRECISION;
1214 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1217 num = num_trim (num, precision);
1220 num.overflow = false;
1223 maybe_orig = num_rshift (num, precision, n);
1224 num.overflow = !num_eq (orig, maybe_orig);
1231 /* The four unary operators: +, -, ! and ~. */
1233 num_unary_op (pfile, num, op)
1241 if (CPP_WTRADITIONAL (pfile))
1242 cpp_error (pfile, DL_WARNING,
1243 "traditional C rejects the unary plus operator");
1244 num.overflow = false;
1248 num = num_negate (num, CPP_OPTION (pfile, precision));
1252 num.high = ~num.high;
1254 num = num_trim (num, CPP_OPTION (pfile, precision));
1255 num.overflow = false;
1258 default: /* case CPP_NOT: */
1259 num.low = num_zerop (num);
1261 num.overflow = false;
1262 num.unsignedp = false;
1269 /* The various binary operators. */
1271 num_binary_op (pfile, lhs, rhs, op)
1277 size_t precision = CPP_OPTION (pfile, precision);
1286 if (!rhs.unsignedp && !num_positive (rhs, precision))
1288 /* A negative shift is a positive shift the other way. */
1289 if (op == CPP_LSHIFT)
1293 rhs = num_negate (rhs, precision);
1296 n = ~0; /* Maximal. */
1299 if (op == CPP_LSHIFT)
1300 lhs = num_lshift (lhs, precision, n);
1302 lhs = num_rshift (lhs, precision, n);
1309 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1311 gte = num_greater_eq (lhs, rhs, precision);
1316 lhs.unsignedp = unsignedp;
1322 rhs = num_negate (rhs, precision);
1324 result.low = lhs.low + rhs.low;
1325 result.high = lhs.high + rhs.high;
1326 if (result.low < lhs.low)
1329 result = num_trim (result, precision);
1330 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1331 if (result.unsignedp)
1332 result.overflow = false;
1335 bool lhsp = num_positive (lhs, precision);
1336 result.overflow = (lhsp == num_positive (rhs, precision)
1337 && lhsp != num_positive (result, precision));
1342 default: /* case CPP_COMMA: */
1343 if (CPP_PEDANTIC (pfile))
1344 cpp_error (pfile, DL_PEDWARN,
1345 "comma operator in operand of #if");
1353 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1356 num_part_mul (lhs, rhs)
1357 cpp_num_part lhs, rhs;
1360 cpp_num_part middle[2], temp;
1362 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1363 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1365 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1366 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1369 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1370 if (result.low < temp)
1374 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1375 if (result.low < temp)
1378 result.high += HIGH_PART (middle[0]);
1379 result.high += HIGH_PART (middle[1]);
1384 /* Multiply two preprocessing numbers. */
1386 num_mul (pfile, lhs, rhs, op)
1389 enum cpp_ttype op ATTRIBUTE_UNUSED;
1391 cpp_num result, temp;
1392 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1393 bool overflow, negate = false;
1394 size_t precision = CPP_OPTION (pfile, precision);
1396 /* Prepare for unsigned multiplication. */
1399 if (!num_positive (lhs, precision))
1400 negate = !negate, lhs = num_negate (lhs, precision);
1401 if (!num_positive (rhs, precision))
1402 negate = !negate, rhs = num_negate (rhs, precision);
1405 overflow = lhs.high && rhs.high;
1406 result = num_part_mul (lhs.low, rhs.low);
1408 temp = num_part_mul (lhs.high, rhs.low);
1409 result.high += temp.low;
1413 temp = num_part_mul (lhs.low, rhs.high);
1414 result.high += temp.low;
1418 temp.low = result.low, temp.high = result.high;
1419 result = num_trim (result, precision);
1420 if (!num_eq (result, temp))
1424 result = num_negate (result, precision);
1427 result.overflow = false;
1429 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1430 && !num_zerop (result));
1431 result.unsignedp = unsignedp;
1436 /* Divide two preprocessing numbers, returning the answer or the
1437 remainder depending upon OP. */
1439 num_div_op (pfile, lhs, rhs, op)
1444 cpp_num result, sub;
1446 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1447 bool negate = false, lhs_neg = false;
1448 size_t i, precision = CPP_OPTION (pfile, precision);
1450 /* Prepare for unsigned division. */
1453 if (!num_positive (lhs, precision))
1454 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1455 if (!num_positive (rhs, precision))
1456 negate = !negate, rhs = num_negate (rhs, precision);
1459 /* Find the high bit. */
1463 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1464 for (; ; i--, mask >>= 1)
1465 if (rhs.high & mask)
1470 if (precision > PART_PRECISION)
1471 i = precision - PART_PRECISION - 1;
1474 mask = (cpp_num_part) 1 << i;
1475 for (; ; i--, mask >>= 1)
1481 cpp_error (pfile, DL_ERROR, "division by zero in #if");
1485 /* First non-zero bit of RHS is bit I. Do naive division by
1486 shifting the RHS fully left, and subtracting from LHS if LHS is
1487 at least as big, and then repeating but with one less shift.
1488 This is not very efficient, but is easy to understand. */
1490 rhs.unsignedp = true;
1491 lhs.unsignedp = true;
1492 i = precision - i - 1;
1493 sub = num_lshift (rhs, precision, i);
1495 result.high = result.low = 0;
1498 if (num_greater_eq (lhs, sub, precision))
1500 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1501 if (i >= PART_PRECISION)
1502 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1504 result.low |= (cpp_num_part) 1 << i;
1508 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1512 /* We divide so that the remainder has the sign of the LHS. */
1515 result.unsignedp = unsignedp;
1517 result.overflow = false;
1521 result = num_negate (result, precision);
1522 result.overflow = num_positive (result, precision) ^ !negate;
1529 lhs.unsignedp = unsignedp;
1530 lhs.overflow = false;
1532 lhs = num_negate (lhs, precision);