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));
56 static cpp_num num_div_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
58 static cpp_num num_lshift PARAMS ((cpp_num, size_t, size_t));
59 static cpp_num num_rshift PARAMS ((cpp_num, size_t, size_t));
61 static cpp_num append_digit PARAMS ((cpp_num, int, int, size_t));
62 static cpp_num parse_defined PARAMS ((cpp_reader *));
63 static cpp_num eval_token PARAMS ((cpp_reader *, const cpp_token *));
64 static struct op *reduce PARAMS ((cpp_reader *, struct op *, enum cpp_ttype));
65 static unsigned int interpret_float_suffix PARAMS ((const uchar *, size_t));
66 static unsigned int interpret_int_suffix PARAMS ((const uchar *, size_t));
68 /* Token type abuse to create unary plus and minus operators. */
69 #define CPP_UPLUS (CPP_LAST_CPP_OP + 1)
70 #define CPP_UMINUS (CPP_LAST_CPP_OP + 2)
72 /* With -O2, gcc appears to produce nice code, moving the error
73 message load and subsequent jump completely out of the main path. */
74 #define SYNTAX_ERROR(msgid) \
75 do { cpp_error (pfile, DL_ERROR, msgid); goto syntax_error; } while(0)
76 #define SYNTAX_ERROR2(msgid, arg) \
77 do { cpp_error (pfile, DL_ERROR, msgid, arg); goto syntax_error; } 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. */
83 interpret_float_suffix (s, len)
87 size_t f = 0, l = 0, i = 0;
92 case 'f': case 'F': f++; break;
93 case 'l': case 'L': l++; break;
95 case 'j': case 'J': i++; break;
100 if (f + l > 1 || i > 1)
103 return ((i ? CPP_N_IMAGINARY : 0)
105 l ? CPP_N_LARGE : CPP_N_MEDIUM));
108 /* Subroutine of cpp_classify_number. S points to an integer suffix
109 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
110 flag vector describing the suffix. */
112 interpret_int_suffix (s, len)
123 case 'u': case 'U': u++; break;
125 case 'j': case 'J': i++; break;
126 case 'l': case 'L': l++;
127 /* If there are two Ls, they must be adjacent and the same case. */
128 if (l == 2 && s[len] != s[len + 1])
135 if (l > 2 || u > 1 || i > 1)
138 return ((i ? CPP_N_IMAGINARY : 0)
139 | (u ? CPP_N_UNSIGNED : 0)
140 | ((l == 0) ? CPP_N_SMALL
141 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
144 /* Categorize numeric constants according to their field (integer,
145 floating point, or invalid), radix (decimal, octal, hexadecimal),
146 and type suffixes. */
148 cpp_classify_number (pfile, token)
150 const cpp_token *token;
152 const uchar *str = token->val.str.text;
154 unsigned int max_digit, result, radix;
155 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
157 /* If the lexer has done its job, length one can only be a single
158 digit. Fast-path this very common case. */
159 if (token->val.str.len == 1)
160 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
162 limit = str + token->val.str.len;
163 float_flag = NOT_FLOAT;
167 /* First, interpret the radix. */
173 /* Require at least one hex digit to classify it as hex. */
174 if ((*str == 'x' || *str == 'X') && ISXDIGIT (str[1]))
181 /* Now scan for a well-formed integer or float. */
184 unsigned int c = *str++;
186 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
194 if (float_flag == NOT_FLOAT)
195 float_flag = AFTER_POINT;
197 SYNTAX_ERROR ("too many decimal points in number");
199 else if ((radix <= 10 && (c == 'e' || c == 'E'))
200 || (radix == 16 && (c == 'p' || c == 'P')))
202 float_flag = AFTER_EXPON;
207 /* Start of suffix. */
213 if (float_flag != NOT_FLOAT && radix == 8)
216 if (max_digit >= radix)
217 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
219 if (float_flag != NOT_FLOAT)
221 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
222 cpp_error (pfile, DL_PEDWARN,
223 "use of C99 hexadecimal floating constant");
225 if (float_flag == AFTER_EXPON)
227 if (*str == '+' || *str == '-')
230 /* Exponent is decimal, even if string is a hex float. */
232 SYNTAX_ERROR ("exponent has no digits");
236 while (ISDIGIT (*str));
238 else if (radix == 16)
239 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
241 result = interpret_float_suffix (str, limit - str);
244 cpp_error (pfile, DL_ERROR,
245 "invalid suffix \"%.*s\" on floating constant",
246 (int) (limit - str), str);
247 return CPP_N_INVALID;
250 /* Traditional C didn't accept any floating suffixes. */
252 && CPP_WTRADITIONAL (pfile)
253 && ! cpp_sys_macro_p (pfile))
254 cpp_error (pfile, DL_WARNING,
255 "traditional C rejects the \"%.*s\" suffix",
256 (int) (limit - str), str);
258 result |= CPP_N_FLOATING;
262 result = interpret_int_suffix (str, limit - str);
265 cpp_error (pfile, DL_ERROR,
266 "invalid suffix \"%.*s\" on integer constant",
267 (int) (limit - str), str);
268 return CPP_N_INVALID;
271 /* Traditional C only accepted the 'L' suffix. */
272 if (result != CPP_N_SMALL && result != CPP_N_MEDIUM
273 && CPP_WTRADITIONAL (pfile)
274 && ! cpp_sys_macro_p (pfile))
275 cpp_error (pfile, DL_WARNING,
276 "traditional C rejects the \"%.*s\" suffix",
277 (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, DL_PEDWARN, "use of C99 long long integer constant");
284 result |= CPP_N_INTEGER;
287 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
288 cpp_error (pfile, DL_PEDWARN, "imaginary constants are a GCC extension");
291 result |= CPP_N_DECIMAL;
292 else if (radix == 16)
295 result |= CPP_N_OCTAL;
300 return CPP_N_INVALID;
303 /* cpp_interpret_integer converts an integer constant into a cpp_num,
304 of precision options->precision.
306 We do not provide any interface for decimal->float conversion,
307 because the preprocessor doesn't need it and the floating point
308 handling in GCC proper is too ugly to speak of. */
310 cpp_interpret_integer (pfile, token, type)
312 const cpp_token *token;
315 const uchar *p, *end;
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';
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)
341 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
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;
358 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
363 /* Strict inequality for when max is set to zero. */
364 if (result.low < max)
365 result.low = result.low * base + c;
368 result = append_digit (result, c, base, precision);
369 overflow |= result.overflow;
375 cpp_error (pfile, 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 honour 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))
387 cpp_error (pfile, DL_WARNING,
388 "integer constant is so large that it is unsigned");
389 result.unsignedp = true;
396 /* Append DIGIT to NUM, a number of PRECISION bits being read in base
399 append_digit (num, digit, base, precision)
405 unsigned int shift = 3 + (base == 16);
407 cpp_num_part add_high, add_low;
409 /* Multiply by 8 or 16. Catching this overflow here means we don't
410 need to worry about add_high overflowing. */
411 overflow = !!(num.high >> (PART_PRECISION - shift));
412 result.high = num.high << shift;
413 result.low = num.low << shift;
414 result.high |= num.low >> (PART_PRECISION - shift);
418 add_low = num.low << 1;
419 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
422 add_high = add_low = 0;
424 if (add_low + digit < add_low)
428 if (result.low + add_low < result.low)
430 if (result.high + add_high < result.high)
433 result.low += add_low;
434 result.high += add_high;
436 /* The above code catches overflow of a cpp_num type. This catches
437 overflow of the (possibly shorter) target precision. */
438 num.low = result.low;
439 num.high = result.high;
440 result = num_trim (result, precision);
441 if (!num_eq (result, num))
444 result.unsignedp = num.unsignedp;
445 result.overflow = overflow;
449 /* Handle meeting "defined" in a preprocessor expression. */
451 parse_defined (pfile)
456 cpp_hashnode *node = 0;
457 const cpp_token *token;
458 cpp_context *initial_context = pfile->context;
460 /* Don't expand macros. */
461 pfile->state.prevent_expansion++;
463 token = cpp_get_token (pfile);
464 if (token->type == CPP_OPEN_PAREN)
467 token = cpp_get_token (pfile);
470 if (token->type == CPP_NAME)
472 node = token->val.node;
473 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
475 cpp_error (pfile, DL_ERROR, "missing ')' after \"defined\"");
481 cpp_error (pfile, DL_ERROR,
482 "operator \"defined\" requires an identifier");
483 if (token->flags & NAMED_OP)
488 op.type = token->type;
489 cpp_error (pfile, DL_ERROR,
490 "(\"%s\" is an alternative token for \"%s\" in C++)",
491 cpp_token_as_text (pfile, token),
492 cpp_token_as_text (pfile, &op));
498 if (pfile->context != initial_context)
499 cpp_error (pfile, DL_WARNING,
500 "this use of \"defined\" may not be portable");
502 /* A possible controlling macro of the form #if !defined ().
503 _cpp_parse_expr checks there was no other junk on the line. */
504 pfile->mi_ind_cmacro = node;
507 pfile->state.prevent_expansion--;
509 result.unsignedp = false;
511 result.overflow = false;
512 result.low = node && node->type == NT_MACRO;
516 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
517 number or character constant, or the result of the "defined" or "#"
520 eval_token (pfile, token)
522 const cpp_token *token;
531 temp = cpp_classify_number (pfile, token);
532 switch (temp & CPP_N_CATEGORY)
535 cpp_error (pfile, DL_ERROR,
536 "floating constant in preprocessor expression");
539 if (!(temp & CPP_N_IMAGINARY))
540 return cpp_interpret_integer (pfile, token, temp);
541 cpp_error (pfile, DL_ERROR,
542 "imaginary number in preprocessor expression");
546 /* Error already issued. */
549 result.high = result.low = 0;
555 cppchar_t cc = cpp_interpret_charconst (pfile, token,
560 /* Sign-extend the result if necessary. */
561 if (!unsignedp && (cppchar_signed_t) cc < 0)
563 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
564 result.low |= ~(~(cpp_num_part) 0
565 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
566 result.high = ~(cpp_num_part) 0;
567 result = num_trim (result, CPP_OPTION (pfile, precision));
573 if (token->val.node == pfile->spec_nodes.n_defined)
574 return parse_defined (pfile);
575 else if (CPP_OPTION (pfile, cplusplus)
576 && (token->val.node == pfile->spec_nodes.n_true
577 || token->val.node == pfile->spec_nodes.n_false))
580 result.low = (token->val.node == pfile->spec_nodes.n_true);
582 /* Warn about use of true or false in #if when pedantic
583 and stdbool.h has not been included. */
584 if (CPP_PEDANTIC (pfile)
585 && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
586 cpp_error (pfile, DL_PEDWARN,
587 "ISO C++ does not permit \"%s\" in #if",
588 NODE_NAME (token->val.node));
594 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
595 cpp_error (pfile, DL_WARNING, "\"%s\" is not defined",
596 NODE_NAME (token->val.node));
600 default: /* CPP_HASH */
601 _cpp_test_assertion (pfile, &temp);
606 result.unsignedp = !!unsignedp;
607 result.overflow = false;
611 /* Operator precedence and flags table.
613 After an operator is returned from the lexer, if it has priority less
614 than the operator on the top of the stack, we reduce the stack by one
615 operator and repeat the test. Since equal priorities do not reduce,
616 this is naturally right-associative.
618 We handle left-associative operators by decrementing the priority of
619 just-lexed operators by one, but retaining the priority of operators
620 already on the stack.
622 The remaining cases are '(' and ')'. We handle '(' by skipping the
623 reduction phase completely. ')' is given lower priority than
624 everything else, including '(', effectively forcing a reduction of the
625 parenthesised expression. If there is a matching '(', the routine
626 reduce() exits immediately. If the normal exit route sees a ')', then
627 there cannot have been a matching '(' and an error message is output.
629 The parser assumes all shifted operators require a left operand unless
630 the flag NO_L_OPERAND is set. These semantics are automatic; any
631 extra semantics need to be handled with operator-specific code. */
633 /* Flags. ALWAYS_EVAL is for operators that should be evaluated even
634 if skip_eval is true; perhaps they are invalid and require a
635 diagnostic, or they might modify skip_eval. */
636 #define NO_L_OPERAND (1 << 0)
637 #define LEFT_ASSOC (1 << 1)
638 #define ALWAYS_EVAL (1 << 2)
640 /* Operator to priority map. Must be in the same order as the first
641 N entries of enum cpp_ttype. */
642 static const struct operator
648 /* EQ */ {0, 0}, /* Shouldn't happen. */
649 /* NOT */ {16, NO_L_OPERAND},
650 /* GREATER */ {12, LEFT_ASSOC},
651 /* LESS */ {12, LEFT_ASSOC},
652 /* PLUS */ {14, LEFT_ASSOC},
653 /* MINUS */ {14, LEFT_ASSOC},
654 /* MULT */ {15, LEFT_ASSOC},
655 /* DIV */ {15, LEFT_ASSOC},
656 /* MOD */ {15, LEFT_ASSOC},
657 /* AND */ {9, LEFT_ASSOC},
658 /* OR */ {7, LEFT_ASSOC},
659 /* XOR */ {8, LEFT_ASSOC},
660 /* RSHIFT */ {13, LEFT_ASSOC},
661 /* LSHIFT */ {13, LEFT_ASSOC},
663 /* MIN */ {10, LEFT_ASSOC},
664 /* MAX */ {10, LEFT_ASSOC},
666 /* COMPL */ {16, NO_L_OPERAND},
667 /* AND_AND */ {6, LEFT_ASSOC | ALWAYS_EVAL},
668 /* OR_OR */ {5, LEFT_ASSOC | ALWAYS_EVAL},
669 /* QUERY */ {3, ALWAYS_EVAL},
670 /* COLON */ {4, LEFT_ASSOC | ALWAYS_EVAL},
671 /* COMMA */ {2, LEFT_ASSOC},
672 /* OPEN_PAREN */ {1, NO_L_OPERAND | ALWAYS_EVAL},
673 /* CLOSE_PAREN */ {0, 0},
675 /* EQ_EQ */ {11, LEFT_ASSOC},
676 /* NOT_EQ */ {11, LEFT_ASSOC},
677 /* GREATER_EQ */ {12, LEFT_ASSOC},
678 /* LESS_EQ */ {12, LEFT_ASSOC},
679 /* UPLUS */ {16, NO_L_OPERAND},
680 /* UMINUS */ {16, NO_L_OPERAND}
683 /* Parse and evaluate a C expression, reading from PFILE.
684 Returns the truth value of the expression.
686 The implementation is an operator precedence parser, i.e. a
687 bottom-up parser, using a stack for not-yet-reduced tokens.
689 The stack base is op_stack, and the current stack pointer is 'top'.
690 There is a stack element for each operator (only), and the most
691 recently pushed operator is 'top->op'. An operand (value) is
692 stored in the 'value' field of the stack element of the operator
695 _cpp_parse_expr (pfile)
698 struct op *top = pfile->op_stack;
699 const cpp_token *token = NULL, *prev_token;
700 unsigned int lex_count;
701 bool saw_leading_not, want_value = true;
703 pfile->state.skip_eval = 0;
705 /* Set up detection of #if ! defined(). */
706 pfile->mi_ind_cmacro = 0;
707 saw_leading_not = false;
710 /* Lowest priority operator prevents further reductions. */
718 token = cpp_get_token (pfile);
724 /* These tokens convert into values. */
731 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
732 cpp_token_as_text (pfile, token));
734 top->value = eval_token (pfile, token);
738 saw_leading_not = lex_count == 1;
749 if (ISGRAPH (token->val.c))
750 SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
752 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
755 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
756 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
757 cpp_token_as_text (pfile, token));
761 /* Check we have a value or operator as appropriate. */
762 if (optab[op.op].flags & NO_L_OPERAND)
765 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
766 cpp_token_as_text (pfile, token));
770 /* Ordering here is subtle and intended to favour the
771 missing parenthesis diagnostics over alternatives. */
772 if (op.op == CPP_CLOSE_PAREN)
774 if (top->op == CPP_OPEN_PAREN)
775 SYNTAX_ERROR ("void expression between '(' and ')'");
777 else if (top->op == CPP_EOF)
778 SYNTAX_ERROR ("#if with no expression");
779 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
780 SYNTAX_ERROR2 ("operator '%s' has no right operand",
781 cpp_token_as_text (pfile, prev_token));
784 top = reduce (pfile, top, op.op);
788 if (op.op == CPP_EOF)
793 case CPP_CLOSE_PAREN:
796 if (!num_zerop (top->value))
797 pfile->state.skip_eval++;
801 if (num_zerop (top->value))
802 pfile->state.skip_eval++;
805 if (top->op != CPP_QUERY)
806 SYNTAX_ERROR (" ':' without preceding '?'");
807 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
808 pfile->state.skip_eval++;
810 pfile->state.skip_eval--;
817 /* Check for and handle stack overflow. */
818 if (++top == pfile->op_limit)
819 top = _cpp_expand_op_stack (pfile);
824 /* The controlling macro expression is only valid if we called lex 3
825 times: <!> <defined expression> and <EOF>. push_conditional ()
826 checks that we are at top-of-file. */
827 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
828 pfile->mi_ind_cmacro = 0;
830 if (top != pfile->op_stack)
832 cpp_error (pfile, DL_ICE, "unbalanced stack in #if");
834 return false; /* Return false on syntax error. */
837 return !num_zerop (top->value);
840 /* Reduce the operator / value stack if possible, in preparation for
841 pushing operator OP. Returns NULL on error, otherwise the top of
844 reduce (pfile, top, op)
851 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
854 cpp_error (pfile, DL_ICE, "impossible operator '%u'", top->op);
858 if (op == CPP_OPEN_PAREN)
861 /* Decrement the priority of left-associative operators to force a
862 reduction with operators of otherwise equal priority. */
863 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
864 while (prio < optab[top->op].prio)
866 if (!pfile->state.skip_eval || optab[top->op].flags & ALWAYS_EVAL)
873 top[-1].value = num_unary_op (pfile, top->value, top->op);
883 top[-1].value = num_binary_op (pfile, top[-1].value,
884 top->value, top->op);
892 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
898 = num_equality_op (pfile, top[-1].value, top->value, top->op);
905 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
909 top[-1].value = num_mul (pfile, top[-1].value, top->value);
914 top[-1].value = num_div_op (pfile, top[-1].value,
915 top->value, top->op);
920 if (!num_zerop (top->value))
921 pfile->state.skip_eval--;
922 top->value.low = (!num_zerop (top->value)
923 || !num_zerop (top[1].value));
925 top->value.unsignedp = false;
926 top->value.overflow = false;
931 if (num_zerop (top->value))
932 pfile->state.skip_eval--;
933 top->value.low = (!num_zerop (top->value)
934 && !num_zerop (top[1].value));
936 top->value.unsignedp = false;
937 top->value.overflow = false;
941 if (op != CPP_CLOSE_PAREN)
943 cpp_error (pfile, DL_ERROR, "missing ')' in expression");
947 top->value = top[1].value;
952 if (!num_zerop (top->value))
954 pfile->state.skip_eval--;
955 top->value = top[1].value;
958 top->value = top[2].value;
959 top->value.unsignedp = (top[1].value.unsignedp
960 || top[2].value.unsignedp);
964 cpp_error (pfile, DL_ERROR, "'?' without following ':'");
972 if (top->value.overflow && !pfile->state.skip_eval)
973 cpp_error (pfile, DL_PEDWARN,
974 "integer overflow in preprocessor expression");
977 if (op == CPP_CLOSE_PAREN)
979 cpp_error (pfile, DL_ERROR, "missing '(' in expression");
986 /* Returns the position of the old top of stack after expansion. */
988 _cpp_expand_op_stack (pfile)
991 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
992 size_t new_size = old_size * 2 + 20;
994 pfile->op_stack = (struct op *) xrealloc (pfile->op_stack,
995 new_size * sizeof (struct op));
996 pfile->op_limit = pfile->op_stack + new_size;
998 return pfile->op_stack + old_size;
1001 /* Clears the unused high order bits of the number pointed to by PNUM. */
1003 num_trim (num, precision)
1007 if (precision > PART_PRECISION)
1009 precision -= PART_PRECISION;
1010 if (precision < PART_PRECISION)
1011 num.high &= ((cpp_num_part) 1 << precision) - 1;
1015 if (precision < PART_PRECISION)
1016 num.low &= ((cpp_num_part) 1 << precision) - 1;
1023 /* True iff A (presumed signed) >= 0. */
1025 num_positive (num, precision)
1029 if (precision > PART_PRECISION)
1031 precision -= PART_PRECISION;
1032 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1035 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1038 /* Sign extend a number, with PRECISION significant bits and all
1039 others assumed clear, to fill out a cpp_num structure. */
1041 cpp_num_sign_extend (num, precision)
1047 if (precision > PART_PRECISION)
1049 precision -= PART_PRECISION;
1050 if (precision < PART_PRECISION
1051 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1052 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1054 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1056 if (precision < PART_PRECISION)
1057 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1058 num.high = ~(cpp_num_part) 0;
1065 /* Returns the negative of NUM. */
1067 num_negate (num, precision)
1074 num.high = ~num.high;
1078 num = num_trim (num, precision);
1079 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1084 /* Returns true if A >= B. */
1086 num_greater_eq (pa, pb, precision)
1092 unsignedp = pa.unsignedp || pb.unsignedp;
1096 /* Both numbers have signed type. If they are of different
1097 sign, the answer is the sign of A. */
1098 unsignedp = num_positive (pa, precision);
1100 if (unsignedp != num_positive (pb, precision))
1103 /* Otherwise we can do an unsigned comparison. */
1106 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1109 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1111 num_bitwise_op (pfile, lhs, rhs, op)
1112 cpp_reader *pfile ATTRIBUTE_UNUSED;
1116 lhs.overflow = false;
1117 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1119 /* As excess precision is zeroed, there is no need to num_trim () as
1120 these operations cannot introduce a set bit there. */
1124 lhs.high &= rhs.high;
1126 else if (op == CPP_OR)
1129 lhs.high |= rhs.high;
1134 lhs.high ^= rhs.high;
1140 /* Returns LHS OP RHS, where OP is an inequality. */
1142 num_inequality_op (pfile, lhs, rhs, op)
1147 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1149 if (op == CPP_GREATER_EQ)
1151 else if (op == CPP_LESS)
1153 else if (op == CPP_GREATER)
1154 lhs.low = gte && !num_eq (lhs, rhs);
1155 else /* CPP_LESS_EQ. */
1156 lhs.low = !gte || num_eq (lhs, rhs);
1159 lhs.overflow = false;
1160 lhs.unsignedp = false;
1164 /* Returns LHS OP RHS, where OP is == or !=. */
1166 num_equality_op (pfile, lhs, rhs, op)
1167 cpp_reader *pfile ATTRIBUTE_UNUSED;
1171 /* Work around a 3.0.4 bug; see PR 6950. */
1172 bool eq = num_eq (lhs, rhs);
1173 if (op == CPP_NOT_EQ)
1177 lhs.overflow = false;
1178 lhs.unsignedp = false;
1182 /* Shift NUM, of width PRECISION, right by N bits. */
1184 num_rshift (num, precision, n)
1186 size_t precision, n;
1188 cpp_num_part sign_mask;
1190 if (num.unsignedp || num_positive (num, precision))
1193 sign_mask = ~(cpp_num_part) 0;
1196 num.high = num.low = sign_mask;
1200 if (precision < PART_PRECISION)
1201 num.high = sign_mask, num.low |= sign_mask << precision;
1202 else if (precision < 2 * PART_PRECISION)
1203 num.high |= sign_mask << (precision - PART_PRECISION);
1205 if (n >= PART_PRECISION)
1207 n -= PART_PRECISION;
1209 num.high = sign_mask;
1214 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1215 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1219 num = num_trim (num, precision);
1220 num.overflow = false;
1224 /* Shift NUM, of width PRECISION, left by N bits. */
1226 num_lshift (num, precision, n)
1228 size_t precision, n;
1232 num.overflow = !num.unsignedp && !num_zerop (num);
1233 num.high = num.low = 0;
1237 cpp_num orig, maybe_orig;
1241 if (m >= PART_PRECISION)
1243 m -= PART_PRECISION;
1249 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1252 num = num_trim (num, precision);
1255 num.overflow = false;
1258 maybe_orig = num_rshift (num, precision, n);
1259 num.overflow = !num_eq (orig, maybe_orig);
1266 /* The four unary operators: +, -, ! and ~. */
1268 num_unary_op (pfile, num, op)
1276 if (CPP_WTRADITIONAL (pfile))
1277 cpp_error (pfile, DL_WARNING,
1278 "traditional C rejects the unary plus operator");
1279 num.overflow = false;
1283 num = num_negate (num, CPP_OPTION (pfile, precision));
1287 num.high = ~num.high;
1289 num = num_trim (num, CPP_OPTION (pfile, precision));
1290 num.overflow = false;
1293 default: /* case CPP_NOT: */
1294 num.low = num_zerop (num);
1296 num.overflow = false;
1297 num.unsignedp = false;
1304 /* The various binary operators. */
1306 num_binary_op (pfile, lhs, rhs, op)
1312 size_t precision = CPP_OPTION (pfile, precision);
1321 if (!rhs.unsignedp && !num_positive (rhs, precision))
1323 /* A negative shift is a positive shift the other way. */
1324 if (op == CPP_LSHIFT)
1328 rhs = num_negate (rhs, precision);
1331 n = ~0; /* Maximal. */
1334 if (op == CPP_LSHIFT)
1335 lhs = num_lshift (lhs, precision, n);
1337 lhs = num_rshift (lhs, precision, n);
1344 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1346 gte = num_greater_eq (lhs, rhs, precision);
1351 lhs.unsignedp = unsignedp;
1357 rhs = num_negate (rhs, precision);
1359 result.low = lhs.low + rhs.low;
1360 result.high = lhs.high + rhs.high;
1361 if (result.low < lhs.low)
1364 result = num_trim (result, precision);
1365 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1366 if (result.unsignedp)
1367 result.overflow = false;
1370 bool lhsp = num_positive (lhs, precision);
1371 result.overflow = (lhsp == num_positive (rhs, precision)
1372 && lhsp != num_positive (result, precision));
1377 default: /* case CPP_COMMA: */
1378 if (CPP_PEDANTIC (pfile))
1379 cpp_error (pfile, DL_PEDWARN,
1380 "comma operator in operand of #if");
1388 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1391 num_part_mul (lhs, rhs)
1392 cpp_num_part lhs, rhs;
1395 cpp_num_part middle[2], temp;
1397 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1398 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1400 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1401 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1404 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1405 if (result.low < temp)
1409 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1410 if (result.low < temp)
1413 result.high += HIGH_PART (middle[0]);
1414 result.high += HIGH_PART (middle[1]);
1419 /* Multiply two preprocessing numbers. */
1421 num_mul (pfile, lhs, rhs)
1425 cpp_num result, temp;
1426 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1427 bool overflow, negate = false;
1428 size_t precision = CPP_OPTION (pfile, precision);
1430 /* Prepare for unsigned multiplication. */
1433 if (!num_positive (lhs, precision))
1434 negate = !negate, lhs = num_negate (lhs, precision);
1435 if (!num_positive (rhs, precision))
1436 negate = !negate, rhs = num_negate (rhs, precision);
1439 overflow = lhs.high && rhs.high;
1440 result = num_part_mul (lhs.low, rhs.low);
1442 temp = num_part_mul (lhs.high, rhs.low);
1443 result.high += temp.low;
1447 temp = num_part_mul (lhs.low, rhs.high);
1448 result.high += temp.low;
1452 temp.low = result.low, temp.high = result.high;
1453 result = num_trim (result, precision);
1454 if (!num_eq (result, temp))
1458 result = num_negate (result, precision);
1461 result.overflow = false;
1463 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1464 && !num_zerop (result));
1465 result.unsignedp = unsignedp;
1470 /* Divide two preprocessing numbers, returning the answer or the
1471 remainder depending upon OP. */
1473 num_div_op (pfile, lhs, rhs, op)
1478 cpp_num result, sub;
1480 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1481 bool negate = false, lhs_neg = false;
1482 size_t i, precision = CPP_OPTION (pfile, precision);
1484 /* Prepare for unsigned division. */
1487 if (!num_positive (lhs, precision))
1488 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1489 if (!num_positive (rhs, precision))
1490 negate = !negate, rhs = num_negate (rhs, precision);
1493 /* Find the high bit. */
1497 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1498 for (; ; i--, mask >>= 1)
1499 if (rhs.high & mask)
1504 if (precision > PART_PRECISION)
1505 i = precision - PART_PRECISION - 1;
1508 mask = (cpp_num_part) 1 << i;
1509 for (; ; i--, mask >>= 1)
1515 cpp_error (pfile, DL_ERROR, "division by zero in #if");
1519 /* First non-zero bit of RHS is bit I. Do naive division by
1520 shifting the RHS fully left, and subtracting from LHS if LHS is
1521 at least as big, and then repeating but with one less shift.
1522 This is not very efficient, but is easy to understand. */
1524 rhs.unsignedp = true;
1525 lhs.unsignedp = true;
1526 i = precision - i - 1;
1527 sub = num_lshift (rhs, precision, i);
1529 result.high = result.low = 0;
1532 if (num_greater_eq (lhs, sub, precision))
1534 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1535 if (i >= PART_PRECISION)
1536 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1538 result.low |= (cpp_num_part) 1 << i;
1542 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1546 /* We divide so that the remainder has the sign of the LHS. */
1549 result.unsignedp = unsignedp;
1551 result.overflow = false;
1555 result = num_negate (result, precision);
1556 result.overflow = num_positive (result, precision) ^ !negate;
1563 lhs.unsignedp = unsignedp;
1564 lhs.overflow = false;
1566 lhs = num_negate (lhs, precision);