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 const cpp_token *token; /* The token forming op (for diagnostics). */
34 cpp_num value; /* The value logically "right" of op. */
38 /* Some simple utility routines on double integers. */
39 #define num_zerop(num) ((num.low | num.high) == 0)
40 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
41 static bool num_positive (cpp_num, size_t);
42 static bool num_greater_eq (cpp_num, cpp_num, size_t);
43 static cpp_num num_trim (cpp_num, size_t);
44 static cpp_num num_part_mul (cpp_num_part, cpp_num_part);
46 static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype);
47 static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
48 static cpp_num num_negate (cpp_num, size_t);
49 static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
50 static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num,
52 static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num,
54 static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num);
55 static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
56 static cpp_num num_lshift (cpp_num, size_t, size_t);
57 static cpp_num num_rshift (cpp_num, size_t, size_t);
59 static cpp_num append_digit (cpp_num, int, int, size_t);
60 static cpp_num parse_defined (cpp_reader *);
61 static cpp_num eval_token (cpp_reader *, const cpp_token *);
62 static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
63 static unsigned int interpret_float_suffix (const uchar *, size_t);
64 static unsigned int interpret_int_suffix (const uchar *, size_t);
65 static void check_promotion (cpp_reader *, const struct op *);
67 /* Token type abuse to create unary plus and minus operators. */
68 #define CPP_UPLUS (CPP_LAST_CPP_OP + 1)
69 #define CPP_UMINUS (CPP_LAST_CPP_OP + 2)
71 /* With -O2, gcc appears to produce nice code, moving the error
72 message load and subsequent jump completely out of the main path. */
73 #define SYNTAX_ERROR(msgid) \
74 do { cpp_error (pfile, DL_ERROR, msgid); goto syntax_error; } while(0)
75 #define SYNTAX_ERROR2(msgid, arg) \
76 do { cpp_error (pfile, DL_ERROR, msgid, arg); goto syntax_error; } while(0)
78 /* Subroutine of cpp_classify_number. S points to a float suffix of
79 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
80 flag vector describing the suffix. */
82 interpret_float_suffix (const uchar *s, size_t len)
84 size_t f = 0, l = 0, i = 0;
89 case 'f': case 'F': f++; break;
90 case 'l': case 'L': l++; break;
92 case 'j': case 'J': i++; break;
97 if (f + l > 1 || i > 1)
100 return ((i ? CPP_N_IMAGINARY : 0)
102 l ? CPP_N_LARGE : CPP_N_MEDIUM));
105 /* Subroutine of cpp_classify_number. S points to an integer suffix
106 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
107 flag vector describing the suffix. */
109 interpret_int_suffix (const uchar *s, size_t len)
118 case 'u': case 'U': u++; break;
120 case 'j': case 'J': i++; break;
121 case 'l': case 'L': l++;
122 /* If there are two Ls, they must be adjacent and the same case. */
123 if (l == 2 && s[len] != s[len + 1])
130 if (l > 2 || u > 1 || i > 1)
133 return ((i ? CPP_N_IMAGINARY : 0)
134 | (u ? CPP_N_UNSIGNED : 0)
135 | ((l == 0) ? CPP_N_SMALL
136 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
139 /* Categorize numeric constants according to their field (integer,
140 floating point, or invalid), radix (decimal, octal, hexadecimal),
141 and type suffixes. */
143 cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
145 const uchar *str = token->val.str.text;
147 unsigned int max_digit, result, radix;
148 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
150 /* If the lexer has done its job, length one can only be a single
151 digit. Fast-path this very common case. */
152 if (token->val.str.len == 1)
153 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
155 limit = str + token->val.str.len;
156 float_flag = NOT_FLOAT;
160 /* First, interpret the radix. */
166 /* Require at least one hex digit to classify it as hex. */
167 if ((*str == 'x' || *str == 'X')
168 && (str[1] == '.' || ISXDIGIT (str[1])))
175 /* Now scan for a well-formed integer or float. */
178 unsigned int c = *str++;
180 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
188 if (float_flag == NOT_FLOAT)
189 float_flag = AFTER_POINT;
191 SYNTAX_ERROR ("too many decimal points in number");
193 else if ((radix <= 10 && (c == 'e' || c == 'E'))
194 || (radix == 16 && (c == 'p' || c == 'P')))
196 float_flag = AFTER_EXPON;
201 /* Start of suffix. */
207 if (float_flag != NOT_FLOAT && radix == 8)
210 if (max_digit >= radix)
211 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
213 if (float_flag != NOT_FLOAT)
215 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
216 cpp_error (pfile, DL_PEDWARN,
217 "use of C99 hexadecimal floating constant");
219 if (float_flag == AFTER_EXPON)
221 if (*str == '+' || *str == '-')
224 /* Exponent is decimal, even if string is a hex float. */
226 SYNTAX_ERROR ("exponent has no digits");
230 while (ISDIGIT (*str));
232 else if (radix == 16)
233 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
235 result = interpret_float_suffix (str, limit - str);
238 cpp_error (pfile, DL_ERROR,
239 "invalid suffix \"%.*s\" on floating constant",
240 (int) (limit - str), str);
241 return CPP_N_INVALID;
244 /* Traditional C didn't accept any floating suffixes. */
246 && CPP_WTRADITIONAL (pfile)
247 && ! cpp_sys_macro_p (pfile))
248 cpp_error (pfile, DL_WARNING,
249 "traditional C rejects the \"%.*s\" suffix",
250 (int) (limit - str), str);
252 result |= CPP_N_FLOATING;
256 result = interpret_int_suffix (str, limit - str);
259 cpp_error (pfile, DL_ERROR,
260 "invalid suffix \"%.*s\" on integer constant",
261 (int) (limit - str), str);
262 return CPP_N_INVALID;
265 /* Traditional C only accepted the 'L' suffix.
266 Suppress warning about 'LL' with -Wno-long-long. */
267 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
269 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
270 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
272 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
273 cpp_error (pfile, DL_WARNING,
274 "traditional C rejects the \"%.*s\" suffix",
275 (int) (limit - str), str);
278 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
279 && ! CPP_OPTION (pfile, c99)
280 && CPP_OPTION (pfile, warn_long_long))
281 cpp_error (pfile, DL_PEDWARN, "use of C99 long long integer constant");
283 result |= CPP_N_INTEGER;
286 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
287 cpp_error (pfile, DL_PEDWARN, "imaginary constants are a GCC extension");
290 result |= CPP_N_DECIMAL;
291 else if (radix == 16)
294 result |= CPP_N_OCTAL;
299 return CPP_N_INVALID;
302 /* cpp_interpret_integer converts an integer constant into a cpp_num,
303 of precision options->precision.
305 We do not provide any interface for decimal->float conversion,
306 because the preprocessor doesn't need it and we don't want to
307 drag in GCC's floating point emulator. */
309 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
312 const uchar *p, *end;
317 result.unsignedp = !!(type & CPP_N_UNSIGNED);
318 result.overflow = false;
320 p = token->val.str.text;
321 end = p + token->val.str.len;
323 /* Common case of a single digit. */
324 if (token->val.str.len == 1)
325 result.low = p[0] - '0';
329 size_t precision = CPP_OPTION (pfile, precision);
330 unsigned int base = 10, c = 0;
331 bool overflow = false;
333 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
338 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
344 /* We can add a digit to numbers strictly less than this without
345 needing the precision and slowness of double integers. */
346 max = ~(cpp_num_part) 0;
347 if (precision < PART_PRECISION)
348 max >>= PART_PRECISION - precision;
349 max = (max - base + 1) / base + 1;
355 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
360 /* Strict inequality for when max is set to zero. */
361 if (result.low < max)
362 result.low = result.low * base + c;
365 result = append_digit (result, c, base, precision);
366 overflow |= result.overflow;
372 cpp_error (pfile, DL_PEDWARN,
373 "integer constant is too large for its type");
374 /* If too big to be signed, consider it unsigned. Only warn for
375 decimal numbers. Traditional numbers were always signed (but
376 we still honor an explicit U suffix); but we only have
377 traditional semantics in directives. */
378 else if (!result.unsignedp
379 && !(CPP_OPTION (pfile, traditional)
380 && pfile->state.in_directive)
381 && !num_positive (result, precision))
384 cpp_error (pfile, DL_WARNING,
385 "integer constant is so large that it is unsigned");
386 result.unsignedp = true;
393 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
395 append_digit (cpp_num num, int digit, int base, size_t precision)
398 unsigned int shift = 3 + (base == 16);
400 cpp_num_part add_high, add_low;
402 /* Multiply by 8 or 16. Catching this overflow here means we don't
403 need to worry about add_high overflowing. */
404 overflow = !!(num.high >> (PART_PRECISION - shift));
405 result.high = num.high << shift;
406 result.low = num.low << shift;
407 result.high |= num.low >> (PART_PRECISION - shift);
411 add_low = num.low << 1;
412 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
415 add_high = add_low = 0;
417 if (add_low + digit < add_low)
421 if (result.low + add_low < result.low)
423 if (result.high + add_high < result.high)
426 result.low += add_low;
427 result.high += add_high;
429 /* The above code catches overflow of a cpp_num type. This catches
430 overflow of the (possibly shorter) target precision. */
431 num.low = result.low;
432 num.high = result.high;
433 result = num_trim (result, precision);
434 if (!num_eq (result, num))
437 result.unsignedp = num.unsignedp;
438 result.overflow = overflow;
442 /* Handle meeting "defined" in a preprocessor expression. */
444 parse_defined (cpp_reader *pfile)
448 cpp_hashnode *node = 0;
449 const cpp_token *token;
450 cpp_context *initial_context = pfile->context;
452 /* Don't expand macros. */
453 pfile->state.prevent_expansion++;
455 token = cpp_get_token (pfile);
456 if (token->type == CPP_OPEN_PAREN)
459 token = cpp_get_token (pfile);
462 if (token->type == CPP_NAME)
464 node = token->val.node;
465 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
467 cpp_error (pfile, DL_ERROR, "missing ')' after \"defined\"");
473 cpp_error (pfile, DL_ERROR,
474 "operator \"defined\" requires an identifier");
475 if (token->flags & NAMED_OP)
480 op.type = token->type;
481 cpp_error (pfile, DL_ERROR,
482 "(\"%s\" is an alternative token for \"%s\" in C++)",
483 cpp_token_as_text (pfile, token),
484 cpp_token_as_text (pfile, &op));
490 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
491 cpp_error (pfile, DL_WARNING,
492 "this use of \"defined\" may not be portable");
494 _cpp_mark_macro_used (node);
496 /* A possible controlling macro of the form #if !defined ().
497 _cpp_parse_expr checks there was no other junk on the line. */
498 pfile->mi_ind_cmacro = node;
501 pfile->state.prevent_expansion--;
503 result.unsignedp = false;
505 result.overflow = false;
506 result.low = node && node->type == NT_MACRO;
510 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
511 number or character constant, or the result of the "defined" or "#"
514 eval_token (cpp_reader *pfile, const cpp_token *token)
523 temp = cpp_classify_number (pfile, token);
524 switch (temp & CPP_N_CATEGORY)
527 cpp_error (pfile, DL_ERROR,
528 "floating constant in preprocessor expression");
531 if (!(temp & CPP_N_IMAGINARY))
532 return cpp_interpret_integer (pfile, token, temp);
533 cpp_error (pfile, DL_ERROR,
534 "imaginary number in preprocessor expression");
538 /* Error already issued. */
541 result.high = result.low = 0;
547 cppchar_t cc = cpp_interpret_charconst (pfile, token,
552 /* Sign-extend the result if necessary. */
553 if (!unsignedp && (cppchar_signed_t) cc < 0)
555 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
556 result.low |= ~(~(cpp_num_part) 0
557 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
558 result.high = ~(cpp_num_part) 0;
559 result = num_trim (result, CPP_OPTION (pfile, precision));
565 if (token->val.node == pfile->spec_nodes.n_defined)
566 return parse_defined (pfile);
567 else if (CPP_OPTION (pfile, cplusplus)
568 && (token->val.node == pfile->spec_nodes.n_true
569 || token->val.node == pfile->spec_nodes.n_false))
572 result.low = (token->val.node == pfile->spec_nodes.n_true);
578 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
579 cpp_error (pfile, DL_WARNING, "\"%s\" is not defined",
580 NODE_NAME (token->val.node));
584 default: /* CPP_HASH */
585 _cpp_test_assertion (pfile, &temp);
590 result.unsignedp = !!unsignedp;
591 result.overflow = false;
595 /* Operator precedence and flags table.
597 After an operator is returned from the lexer, if it has priority less
598 than the operator on the top of the stack, we reduce the stack by one
599 operator and repeat the test. Since equal priorities do not reduce,
600 this is naturally right-associative.
602 We handle left-associative operators by decrementing the priority of
603 just-lexed operators by one, but retaining the priority of operators
604 already on the stack.
606 The remaining cases are '(' and ')'. We handle '(' by skipping the
607 reduction phase completely. ')' is given lower priority than
608 everything else, including '(', effectively forcing a reduction of the
609 parenthesized expression. If there is a matching '(', the routine
610 reduce() exits immediately. If the normal exit route sees a ')', then
611 there cannot have been a matching '(' and an error message is output.
613 The parser assumes all shifted operators require a left operand unless
614 the flag NO_L_OPERAND is set. These semantics are automatic; any
615 extra semantics need to be handled with operator-specific code. */
617 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
618 operand changes because of integer promotions. */
619 #define NO_L_OPERAND (1 << 0)
620 #define LEFT_ASSOC (1 << 1)
621 #define CHECK_PROMOTION (1 << 2)
623 /* Operator to priority map. Must be in the same order as the first
624 N entries of enum cpp_ttype. */
625 static const struct operator
631 /* EQ */ {0, 0}, /* Shouldn't happen. */
632 /* NOT */ {16, NO_L_OPERAND},
633 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
634 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
635 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
636 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
637 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
638 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
639 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
640 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
641 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
642 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
643 /* RSHIFT */ {13, LEFT_ASSOC},
644 /* LSHIFT */ {13, LEFT_ASSOC},
646 /* MIN */ {10, LEFT_ASSOC | CHECK_PROMOTION},
647 /* MAX */ {10, LEFT_ASSOC | CHECK_PROMOTION},
649 /* COMPL */ {16, NO_L_OPERAND},
650 /* AND_AND */ {6, LEFT_ASSOC},
651 /* OR_OR */ {5, LEFT_ASSOC},
653 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
654 /* COMMA */ {2, LEFT_ASSOC},
655 /* OPEN_PAREN */ {1, NO_L_OPERAND},
656 /* CLOSE_PAREN */ {0, 0},
658 /* EQ_EQ */ {11, LEFT_ASSOC},
659 /* NOT_EQ */ {11, LEFT_ASSOC},
660 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
661 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
662 /* UPLUS */ {16, NO_L_OPERAND},
663 /* UMINUS */ {16, NO_L_OPERAND}
666 /* Parse and evaluate a C expression, reading from PFILE.
667 Returns the truth value of the expression.
669 The implementation is an operator precedence parser, i.e. a
670 bottom-up parser, using a stack for not-yet-reduced tokens.
672 The stack base is op_stack, and the current stack pointer is 'top'.
673 There is a stack element for each operator (only), and the most
674 recently pushed operator is 'top->op'. An operand (value) is
675 stored in the 'value' field of the stack element of the operator
678 _cpp_parse_expr (cpp_reader *pfile)
680 struct op *top = pfile->op_stack;
681 unsigned int lex_count;
682 bool saw_leading_not, want_value = true;
684 pfile->state.skip_eval = 0;
686 /* Set up detection of #if ! defined(). */
687 pfile->mi_ind_cmacro = 0;
688 saw_leading_not = false;
691 /* Lowest priority operator prevents further reductions. */
699 op.token = cpp_get_token (pfile);
700 op.op = op.token->type;
704 /* These tokens convert into values. */
711 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
712 cpp_token_as_text (pfile, op.token));
714 top->value = eval_token (pfile, op.token);
718 saw_leading_not = lex_count == 1;
730 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
731 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
732 cpp_token_as_text (pfile, op.token));
736 /* Check we have a value or operator as appropriate. */
737 if (optab[op.op].flags & NO_L_OPERAND)
740 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
741 cpp_token_as_text (pfile, op.token));
745 /* Ordering here is subtle and intended to favor the
746 missing parenthesis diagnostics over alternatives. */
747 if (op.op == CPP_CLOSE_PAREN)
749 if (top->op == CPP_OPEN_PAREN)
750 SYNTAX_ERROR ("void expression between '(' and ')'");
752 else if (top->op == CPP_EOF)
753 SYNTAX_ERROR ("#if with no expression");
754 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
755 SYNTAX_ERROR2 ("operator '%s' has no right operand",
756 cpp_token_as_text (pfile, top->token));
759 top = reduce (pfile, top, op.op);
763 if (op.op == CPP_EOF)
768 case CPP_CLOSE_PAREN:
771 if (!num_zerop (top->value))
772 pfile->state.skip_eval++;
776 if (num_zerop (top->value))
777 pfile->state.skip_eval++;
780 if (top->op != CPP_QUERY)
781 SYNTAX_ERROR (" ':' without preceding '?'");
782 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
783 pfile->state.skip_eval++;
785 pfile->state.skip_eval--;
792 /* Check for and handle stack overflow. */
793 if (++top == pfile->op_limit)
794 top = _cpp_expand_op_stack (pfile);
797 top->token = op.token;
800 /* The controlling macro expression is only valid if we called lex 3
801 times: <!> <defined expression> and <EOF>. push_conditional ()
802 checks that we are at top-of-file. */
803 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
804 pfile->mi_ind_cmacro = 0;
806 if (top != pfile->op_stack)
808 cpp_error (pfile, DL_ICE, "unbalanced stack in #if");
810 return false; /* Return false on syntax error. */
813 return !num_zerop (top->value);
816 /* Reduce the operator / value stack if possible, in preparation for
817 pushing operator OP. Returns NULL on error, otherwise the top of
820 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
824 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
827 cpp_error (pfile, DL_ICE, "impossible operator '%u'", top->op);
831 if (op == CPP_OPEN_PAREN)
834 /* Decrement the priority of left-associative operators to force a
835 reduction with operators of otherwise equal priority. */
836 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
837 while (prio < optab[top->op].prio)
839 if (CPP_OPTION (pfile, warn_num_sign_change)
840 && optab[top->op].flags & CHECK_PROMOTION)
841 check_promotion (pfile, top);
849 top[-1].value = num_unary_op (pfile, top->value, top->op);
859 top[-1].value = num_binary_op (pfile, top[-1].value,
860 top->value, top->op);
868 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
874 = num_equality_op (pfile, top[-1].value, top->value, top->op);
881 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
885 top[-1].value = num_mul (pfile, top[-1].value, top->value);
890 top[-1].value = num_div_op (pfile, top[-1].value,
891 top->value, top->op);
896 if (!num_zerop (top->value))
897 pfile->state.skip_eval--;
898 top->value.low = (!num_zerop (top->value)
899 || !num_zerop (top[1].value));
901 top->value.unsignedp = false;
902 top->value.overflow = false;
907 if (num_zerop (top->value))
908 pfile->state.skip_eval--;
909 top->value.low = (!num_zerop (top->value)
910 && !num_zerop (top[1].value));
912 top->value.unsignedp = false;
913 top->value.overflow = false;
917 if (op != CPP_CLOSE_PAREN)
919 cpp_error (pfile, DL_ERROR, "missing ')' in expression");
923 top->value = top[1].value;
928 if (!num_zerop (top->value))
930 pfile->state.skip_eval--;
931 top->value = top[1].value;
934 top->value = top[2].value;
935 top->value.unsignedp = (top[1].value.unsignedp
936 || top[2].value.unsignedp);
940 cpp_error (pfile, DL_ERROR, "'?' without following ':'");
948 if (top->value.overflow && !pfile->state.skip_eval)
949 cpp_error (pfile, DL_PEDWARN,
950 "integer overflow in preprocessor expression");
953 if (op == CPP_CLOSE_PAREN)
955 cpp_error (pfile, DL_ERROR, "missing '(' in expression");
962 /* Returns the position of the old top of stack after expansion. */
964 _cpp_expand_op_stack (cpp_reader *pfile)
966 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
967 size_t new_size = old_size * 2 + 20;
969 pfile->op_stack = xrealloc (pfile->op_stack, new_size * sizeof (struct op));
970 pfile->op_limit = pfile->op_stack + new_size;
972 return pfile->op_stack + old_size;
975 /* Emits a warning if the effective sign of either operand of OP
976 changes because of integer promotions. */
978 check_promotion (cpp_reader *pfile, const struct op *op)
980 if (op->value.unsignedp == op[-1].value.unsignedp)
983 if (op->value.unsignedp)
985 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
986 cpp_error (pfile, DL_WARNING,
987 "the left operand of \"%s\" changes sign when promoted",
988 cpp_token_as_text (pfile, op->token));
990 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
991 cpp_error (pfile, DL_WARNING,
992 "the right operand of \"%s\" changes sign when promoted",
993 cpp_token_as_text (pfile, op->token));
996 /* Clears the unused high order bits of the number pointed to by PNUM. */
998 num_trim (cpp_num num, size_t precision)
1000 if (precision > PART_PRECISION)
1002 precision -= PART_PRECISION;
1003 if (precision < PART_PRECISION)
1004 num.high &= ((cpp_num_part) 1 << precision) - 1;
1008 if (precision < PART_PRECISION)
1009 num.low &= ((cpp_num_part) 1 << precision) - 1;
1016 /* True iff A (presumed signed) >= 0. */
1018 num_positive (cpp_num num, size_t precision)
1020 if (precision > PART_PRECISION)
1022 precision -= PART_PRECISION;
1023 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1026 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1029 /* Sign extend a number, with PRECISION significant bits and all
1030 others assumed clear, to fill out a cpp_num structure. */
1032 cpp_num_sign_extend (cpp_num num, size_t precision)
1036 if (precision > PART_PRECISION)
1038 precision -= PART_PRECISION;
1039 if (precision < PART_PRECISION
1040 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1041 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1043 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1045 if (precision < PART_PRECISION)
1046 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1047 num.high = ~(cpp_num_part) 0;
1054 /* Returns the negative of NUM. */
1056 num_negate (cpp_num num, size_t precision)
1061 num.high = ~num.high;
1065 num = num_trim (num, precision);
1066 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1071 /* Returns true if A >= B. */
1073 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1077 unsignedp = pa.unsignedp || pb.unsignedp;
1081 /* Both numbers have signed type. If they are of different
1082 sign, the answer is the sign of A. */
1083 unsignedp = num_positive (pa, precision);
1085 if (unsignedp != num_positive (pb, precision))
1088 /* Otherwise we can do an unsigned comparison. */
1091 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1094 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1096 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1097 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1099 lhs.overflow = false;
1100 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1102 /* As excess precision is zeroed, there is no need to num_trim () as
1103 these operations cannot introduce a set bit there. */
1107 lhs.high &= rhs.high;
1109 else if (op == CPP_OR)
1112 lhs.high |= rhs.high;
1117 lhs.high ^= rhs.high;
1123 /* Returns LHS OP RHS, where OP is an inequality. */
1125 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1128 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1130 if (op == CPP_GREATER_EQ)
1132 else if (op == CPP_LESS)
1134 else if (op == CPP_GREATER)
1135 lhs.low = gte && !num_eq (lhs, rhs);
1136 else /* CPP_LESS_EQ. */
1137 lhs.low = !gte || num_eq (lhs, rhs);
1140 lhs.overflow = false;
1141 lhs.unsignedp = false;
1145 /* Returns LHS OP RHS, where OP is == or !=. */
1147 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1148 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1150 /* Work around a 3.0.4 bug; see PR 6950. */
1151 bool eq = num_eq (lhs, rhs);
1152 if (op == CPP_NOT_EQ)
1156 lhs.overflow = false;
1157 lhs.unsignedp = false;
1161 /* Shift NUM, of width PRECISION, right by N bits. */
1163 num_rshift (cpp_num num, size_t precision, size_t n)
1165 cpp_num_part sign_mask;
1167 if (num.unsignedp || num_positive (num, precision))
1170 sign_mask = ~(cpp_num_part) 0;
1173 num.high = num.low = sign_mask;
1177 if (precision < PART_PRECISION)
1178 num.high = sign_mask, num.low |= sign_mask << precision;
1179 else if (precision < 2 * PART_PRECISION)
1180 num.high |= sign_mask << (precision - PART_PRECISION);
1182 if (n >= PART_PRECISION)
1184 n -= PART_PRECISION;
1186 num.high = sign_mask;
1191 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1192 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1196 num = num_trim (num, precision);
1197 num.overflow = false;
1201 /* Shift NUM, of width PRECISION, left by N bits. */
1203 num_lshift (cpp_num num, size_t precision, size_t n)
1207 num.overflow = !num.unsignedp && !num_zerop (num);
1208 num.high = num.low = 0;
1212 cpp_num orig, maybe_orig;
1216 if (m >= PART_PRECISION)
1218 m -= PART_PRECISION;
1224 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1227 num = num_trim (num, precision);
1230 num.overflow = false;
1233 maybe_orig = num_rshift (num, precision, n);
1234 num.overflow = !num_eq (orig, maybe_orig);
1241 /* The four unary operators: +, -, ! and ~. */
1243 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1248 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1249 cpp_error (pfile, DL_WARNING,
1250 "traditional C rejects the unary plus operator");
1251 num.overflow = false;
1255 num = num_negate (num, CPP_OPTION (pfile, precision));
1259 num.high = ~num.high;
1261 num = num_trim (num, CPP_OPTION (pfile, precision));
1262 num.overflow = false;
1265 default: /* case CPP_NOT: */
1266 num.low = num_zerop (num);
1268 num.overflow = false;
1269 num.unsignedp = false;
1276 /* The various binary operators. */
1278 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1281 size_t precision = CPP_OPTION (pfile, precision);
1290 if (!rhs.unsignedp && !num_positive (rhs, precision))
1292 /* A negative shift is a positive shift the other way. */
1293 if (op == CPP_LSHIFT)
1297 rhs = num_negate (rhs, precision);
1300 n = ~0; /* Maximal. */
1303 if (op == CPP_LSHIFT)
1304 lhs = num_lshift (lhs, precision, n);
1306 lhs = num_rshift (lhs, precision, n);
1313 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1315 gte = num_greater_eq (lhs, rhs, precision);
1320 lhs.unsignedp = unsignedp;
1326 rhs = num_negate (rhs, precision);
1328 result.low = lhs.low + rhs.low;
1329 result.high = lhs.high + rhs.high;
1330 if (result.low < lhs.low)
1333 result = num_trim (result, precision);
1334 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1335 if (result.unsignedp)
1336 result.overflow = false;
1339 bool lhsp = num_positive (lhs, precision);
1340 result.overflow = (lhsp == num_positive (rhs, precision)
1341 && lhsp != num_positive (result, precision));
1346 default: /* case CPP_COMMA: */
1347 if (CPP_PEDANTIC (pfile) && !pfile->state.skip_eval)
1348 cpp_error (pfile, DL_PEDWARN,
1349 "comma operator in operand of #if");
1357 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1360 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1363 cpp_num_part middle[2], temp;
1365 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1366 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1368 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1369 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1372 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1373 if (result.low < temp)
1377 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1378 if (result.low < temp)
1381 result.high += HIGH_PART (middle[0]);
1382 result.high += HIGH_PART (middle[1]);
1383 result.unsignedp = 1;
1388 /* Multiply two preprocessing numbers. */
1390 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1392 cpp_num result, temp;
1393 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1394 bool overflow, negate = false;
1395 size_t precision = CPP_OPTION (pfile, precision);
1397 /* Prepare for unsigned multiplication. */
1400 if (!num_positive (lhs, precision))
1401 negate = !negate, lhs = num_negate (lhs, precision);
1402 if (!num_positive (rhs, precision))
1403 negate = !negate, rhs = num_negate (rhs, precision);
1406 overflow = lhs.high && rhs.high;
1407 result = num_part_mul (lhs.low, rhs.low);
1409 temp = num_part_mul (lhs.high, rhs.low);
1410 result.high += temp.low;
1414 temp = num_part_mul (lhs.low, rhs.high);
1415 result.high += temp.low;
1419 temp.low = result.low, temp.high = result.high;
1420 result = num_trim (result, precision);
1421 if (!num_eq (result, temp))
1425 result = num_negate (result, precision);
1428 result.overflow = false;
1430 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1431 && !num_zerop (result));
1432 result.unsignedp = unsignedp;
1437 /* Divide two preprocessing numbers, returning the answer or the
1438 remainder depending upon OP. */
1440 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1442 cpp_num result, sub;
1444 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1445 bool negate = false, lhs_neg = false;
1446 size_t i, precision = CPP_OPTION (pfile, precision);
1448 /* Prepare for unsigned division. */
1451 if (!num_positive (lhs, precision))
1452 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1453 if (!num_positive (rhs, precision))
1454 negate = !negate, rhs = num_negate (rhs, precision);
1457 /* Find the high bit. */
1461 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1462 for (; ; i--, mask >>= 1)
1463 if (rhs.high & mask)
1468 if (precision > PART_PRECISION)
1469 i = precision - PART_PRECISION - 1;
1472 mask = (cpp_num_part) 1 << i;
1473 for (; ; i--, mask >>= 1)
1479 if (!pfile->state.skip_eval)
1480 cpp_error (pfile, DL_ERROR, "division by zero in #if");
1484 /* First nonzero bit of RHS is bit I. Do naive division by
1485 shifting the RHS fully left, and subtracting from LHS if LHS is
1486 at least as big, and then repeating but with one less shift.
1487 This is not very efficient, but is easy to understand. */
1489 rhs.unsignedp = true;
1490 lhs.unsignedp = true;
1491 i = precision - i - 1;
1492 sub = num_lshift (rhs, precision, i);
1494 result.high = result.low = 0;
1497 if (num_greater_eq (lhs, sub, precision))
1499 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1500 if (i >= PART_PRECISION)
1501 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1503 result.low |= (cpp_num_part) 1 << i;
1507 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1511 /* We divide so that the remainder has the sign of the LHS. */
1514 result.unsignedp = unsignedp;
1516 result.overflow = false;
1520 result = num_negate (result, precision);
1521 result.overflow = num_positive (result, precision) ^ !negate;
1528 lhs.unsignedp = unsignedp;
1529 lhs.overflow = false;
1531 lhs = num_negate (lhs, precision);