* config/mips/elf.h (ASM_DECLARE_OBJECT_NAME): Use
[official-gcc.git] / gcc / cppexp.c
blobb558b3e8b8aa4b147ad842d7e95b580108878b9c
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002 Free Software Foundation.
4 Contributed by Per Bothner, 1994.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "cpplib.h"
24 #include "cpphash.h"
26 #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
27 #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
28 #define LOW_PART(num_part) (num_part & HALF_MASK)
29 #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
31 struct op
33 const cpp_token *token; /* The token forming op (for diagnostics). */
34 cpp_num value; /* The value logically "right" of op. */
35 enum cpp_ttype op;
38 /* Some simple utility routines on double integers. */
39 #define num_zerop(num) ((num.low | num.high) == 0)
40 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
41 static bool num_positive (cpp_num, size_t);
42 static bool num_greater_eq (cpp_num, cpp_num, size_t);
43 static cpp_num num_trim (cpp_num, size_t);
44 static cpp_num num_part_mul (cpp_num_part, cpp_num_part);
46 static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype);
47 static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
48 static cpp_num num_negate (cpp_num, size_t);
49 static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
50 static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num,
51 enum cpp_ttype);
52 static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num,
53 enum cpp_ttype);
54 static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num);
55 static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
56 static cpp_num num_lshift (cpp_num, size_t, size_t);
57 static cpp_num num_rshift (cpp_num, size_t, size_t);
59 static cpp_num append_digit (cpp_num, int, int, size_t);
60 static cpp_num parse_defined (cpp_reader *);
61 static cpp_num eval_token (cpp_reader *, const cpp_token *);
62 static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
63 static unsigned int interpret_float_suffix (const uchar *, size_t);
64 static unsigned int interpret_int_suffix (const uchar *, size_t);
65 static void check_promotion (cpp_reader *, const struct op *);
67 /* Token type abuse to create unary plus and minus operators. */
68 #define CPP_UPLUS (CPP_LAST_CPP_OP + 1)
69 #define CPP_UMINUS (CPP_LAST_CPP_OP + 2)
71 /* With -O2, gcc appears to produce nice code, moving the error
72 message load and subsequent jump completely out of the main path. */
73 #define SYNTAX_ERROR(msgid) \
74 do { cpp_error (pfile, 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. */
81 static unsigned int
82 interpret_float_suffix (const uchar *s, size_t len)
84 size_t f = 0, l = 0, i = 0;
86 while (len--)
87 switch (s[len])
89 case 'f': case 'F': f++; break;
90 case 'l': case 'L': l++; break;
91 case 'i': case 'I':
92 case 'j': case 'J': i++; break;
93 default:
94 return 0;
97 if (f + l > 1 || i > 1)
98 return 0;
100 return ((i ? CPP_N_IMAGINARY : 0)
101 | (f ? CPP_N_SMALL :
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. */
108 static unsigned int
109 interpret_int_suffix (const uchar *s, size_t len)
111 size_t u, l, i;
113 u = l = i = 0;
115 while (len--)
116 switch (s[len])
118 case 'u': case 'U': u++; break;
119 case 'i': case 'I':
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])
124 return 0;
125 break;
126 default:
127 return 0;
130 if (l > 2 || u > 1 || i > 1)
131 return 0;
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. */
142 unsigned int
143 cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
145 const uchar *str = token->val.str.text;
146 const uchar *limit;
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;
157 max_digit = 0;
158 radix = 10;
160 /* First, interpret the radix. */
161 if (*str == '0')
163 radix = 8;
164 str++;
166 /* Require at least one hex digit to classify it as hex. */
167 if ((*str == 'x' || *str == 'X')
168 && (str[1] == '.' || ISXDIGIT (str[1])))
170 radix = 16;
171 str++;
175 /* Now scan for a well-formed integer or float. */
176 for (;;)
178 unsigned int c = *str++;
180 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
182 c = hex_value (c);
183 if (c > max_digit)
184 max_digit = c;
186 else if (c == '.')
188 if (float_flag == NOT_FLOAT)
189 float_flag = AFTER_POINT;
190 else
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;
197 break;
199 else
201 /* Start of suffix. */
202 str--;
203 break;
207 if (float_flag != NOT_FLOAT && radix == 8)
208 radix = 10;
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 == '-')
222 str++;
224 /* Exponent is decimal, even if string is a hex float. */
225 if (!ISDIGIT (*str))
226 SYNTAX_ERROR ("exponent has no digits");
229 str++;
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);
236 if (result == 0)
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. */
245 if (limit != str
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;
254 else
256 result = interpret_int_suffix (str, limit - str);
257 if (result == 0)
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");
289 if (radix == 10)
290 result |= CPP_N_DECIMAL;
291 else if (radix == 16)
292 result |= CPP_N_HEX;
293 else
294 result |= CPP_N_OCTAL;
296 return result;
298 syntax_error:
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. */
308 cpp_num
309 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
310 unsigned int type)
312 const uchar *p, *end;
313 cpp_num result;
315 result.low = 0;
316 result.high = 0;
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';
326 else
328 cpp_num_part max;
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)
335 base = 8;
336 p++;
338 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
340 base = 16;
341 p += 2;
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;
351 for (; p < end; p++)
353 c = *p;
355 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
356 c = hex_value (c);
357 else
358 break;
360 /* Strict inequality for when max is set to zero. */
361 if (result.low < max)
362 result.low = result.low * base + c;
363 else
365 result = append_digit (result, c, base, precision);
366 overflow |= result.overflow;
367 max = 0;
371 if (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))
383 if (base == 10)
384 cpp_error (pfile, DL_WARNING,
385 "integer constant is so large that it is unsigned");
386 result.unsignedp = true;
390 return result;
393 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
394 static cpp_num
395 append_digit (cpp_num num, int digit, int base, size_t precision)
397 cpp_num result;
398 unsigned int shift = 3 + (base == 16);
399 bool overflow;
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);
409 if (base == 10)
411 add_low = num.low << 1;
412 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
414 else
415 add_high = add_low = 0;
417 if (add_low + digit < add_low)
418 add_high++;
419 add_low += digit;
421 if (result.low + add_low < result.low)
422 add_high++;
423 if (result.high + add_high < result.high)
424 overflow = true;
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))
435 overflow = true;
437 result.unsignedp = num.unsignedp;
438 result.overflow = overflow;
439 return result;
442 /* Handle meeting "defined" in a preprocessor expression. */
443 static cpp_num
444 parse_defined (cpp_reader *pfile)
446 cpp_num result;
447 int paren = 0;
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)
458 paren = 1;
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\"");
468 node = 0;
471 else
473 cpp_error (pfile, DL_ERROR,
474 "operator \"defined\" requires an identifier");
475 if (token->flags & NAMED_OP)
477 cpp_token op;
479 op.flags = 0;
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));
488 if (node)
490 if (pfile->context != initial_context)
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;
504 result.high = 0;
505 result.overflow = false;
506 result.low = node && node->type == NT_MACRO;
507 return result;
510 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
511 number or character constant, or the result of the "defined" or "#"
512 operators). */
513 static cpp_num
514 eval_token (cpp_reader *pfile, const cpp_token *token)
516 cpp_num result;
517 unsigned int temp;
518 int unsignedp = 0;
520 switch (token->type)
522 case CPP_NUMBER:
523 temp = cpp_classify_number (pfile, token);
524 switch (temp & CPP_N_CATEGORY)
526 case CPP_N_FLOATING:
527 cpp_error (pfile, DL_ERROR,
528 "floating constant in preprocessor expression");
529 break;
530 case CPP_N_INTEGER:
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");
535 break;
537 case CPP_N_INVALID:
538 /* Error already issued. */
539 break;
541 result.high = result.low = 0;
542 break;
544 case CPP_WCHAR:
545 case CPP_CHAR:
547 cppchar_t cc = cpp_interpret_charconst (pfile, token,
548 &temp, &unsignedp);
550 result.high = 0;
551 result.low = cc;
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));
562 break;
564 case CPP_NAME:
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))
571 result.high = 0;
572 result.low = (token->val.node == pfile->spec_nodes.n_true);
574 else
576 result.high = 0;
577 result.low = 0;
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));
582 break;
584 default: /* CPP_HASH */
585 _cpp_test_assertion (pfile, &temp);
586 result.high = 0;
587 result.low = temp;
590 result.unsignedp = !!unsignedp;
591 result.overflow = false;
592 return result;
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
627 uchar prio;
628 uchar flags;
629 } optab[] =
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},
652 /* QUERY */ {3, 0},
653 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
654 /* COMMA */ {2, LEFT_ASSOC},
655 /* OPEN_PAREN */ {1, NO_L_OPERAND},
656 /* CLOSE_PAREN */ {0, 0},
657 /* EOF */ {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
676 that precedes it. */
677 bool
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;
689 lex_count = 0;
691 /* Lowest priority operator prevents further reductions. */
692 top->op = CPP_EOF;
694 for (;;)
696 struct op op;
698 lex_count++;
699 op.token = cpp_get_token (pfile);
700 op.op = op.token->type;
702 switch (op.op)
704 /* These tokens convert into values. */
705 case CPP_NUMBER:
706 case CPP_CHAR:
707 case CPP_WCHAR:
708 case CPP_NAME:
709 case CPP_HASH:
710 if (!want_value)
711 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
712 cpp_token_as_text (pfile, op.token));
713 want_value = false;
714 top->value = eval_token (pfile, op.token);
715 continue;
717 case CPP_NOT:
718 saw_leading_not = lex_count == 1;
719 break;
720 case CPP_PLUS:
721 if (want_value)
722 op.op = CPP_UPLUS;
723 break;
724 case CPP_MINUS:
725 if (want_value)
726 op.op = CPP_UMINUS;
727 break;
729 default:
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));
733 break;
736 /* Check we have a value or operator as appropriate. */
737 if (optab[op.op].flags & NO_L_OPERAND)
739 if (!want_value)
740 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
741 cpp_token_as_text (pfile, op.token));
743 else if (want_value)
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);
760 if (!top)
761 goto syntax_error;
763 if (op.op == CPP_EOF)
764 break;
766 switch (op.op)
768 case CPP_CLOSE_PAREN:
769 continue;
770 case CPP_OR_OR:
771 if (!num_zerop (top->value))
772 pfile->state.skip_eval++;
773 break;
774 case CPP_AND_AND:
775 case CPP_QUERY:
776 if (num_zerop (top->value))
777 pfile->state.skip_eval++;
778 break;
779 case CPP_COLON:
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++;
784 else
785 pfile->state.skip_eval--;
786 default:
787 break;
790 want_value = true;
792 /* Check for and handle stack overflow. */
793 if (++top == pfile->op_limit)
794 top = _cpp_expand_op_stack (pfile);
796 top->op = op.op;
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");
809 syntax_error:
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
818 the stack. */
819 static struct op *
820 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
822 unsigned int prio;
824 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
826 bad_op:
827 cpp_error (pfile, DL_ICE, "impossible operator '%u'", top->op);
828 return 0;
831 if (op == CPP_OPEN_PAREN)
832 return top;
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);
843 switch (top->op)
845 case CPP_UPLUS:
846 case CPP_UMINUS:
847 case CPP_NOT:
848 case CPP_COMPL:
849 top[-1].value = num_unary_op (pfile, top->value, top->op);
850 break;
852 case CPP_PLUS:
853 case CPP_MINUS:
854 case CPP_RSHIFT:
855 case CPP_LSHIFT:
856 case CPP_MIN:
857 case CPP_MAX:
858 case CPP_COMMA:
859 top[-1].value = num_binary_op (pfile, top[-1].value,
860 top->value, top->op);
861 break;
863 case CPP_GREATER:
864 case CPP_LESS:
865 case CPP_GREATER_EQ:
866 case CPP_LESS_EQ:
867 top[-1].value
868 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
869 break;
871 case CPP_EQ_EQ:
872 case CPP_NOT_EQ:
873 top[-1].value
874 = num_equality_op (pfile, top[-1].value, top->value, top->op);
875 break;
877 case CPP_AND:
878 case CPP_OR:
879 case CPP_XOR:
880 top[-1].value
881 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
882 break;
884 case CPP_MULT:
885 top[-1].value = num_mul (pfile, top[-1].value, top->value);
886 break;
888 case CPP_DIV:
889 case CPP_MOD:
890 top[-1].value = num_div_op (pfile, top[-1].value,
891 top->value, top->op);
892 break;
894 case CPP_OR_OR:
895 top--;
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));
900 top->value.high = 0;
901 top->value.unsignedp = false;
902 top->value.overflow = false;
903 continue;
905 case CPP_AND_AND:
906 top--;
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));
911 top->value.high = 0;
912 top->value.unsignedp = false;
913 top->value.overflow = false;
914 continue;
916 case CPP_OPEN_PAREN:
917 if (op != CPP_CLOSE_PAREN)
919 cpp_error (pfile, DL_ERROR, "missing ')' in expression");
920 return 0;
922 top--;
923 top->value = top[1].value;
924 return top;
926 case CPP_COLON:
927 top -= 2;
928 if (!num_zerop (top->value))
930 pfile->state.skip_eval--;
931 top->value = top[1].value;
933 else
934 top->value = top[2].value;
935 top->value.unsignedp = (top[1].value.unsignedp
936 || top[2].value.unsignedp);
937 continue;
939 case CPP_QUERY:
940 cpp_error (pfile, DL_ERROR, "'?' without following ':'");
941 return 0;
943 default:
944 goto bad_op;
947 top--;
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");
956 return 0;
959 return top;
962 /* Returns the position of the old top of stack after expansion. */
963 struct op *
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 = (struct op *) xrealloc (pfile->op_stack,
970 new_size * sizeof (struct op));
971 pfile->op_limit = pfile->op_stack + new_size;
973 return pfile->op_stack + old_size;
976 /* Emits a warning if the effective sign of either operand of OP
977 changes because of integer promotions. */
978 static void
979 check_promotion (cpp_reader *pfile, const struct op *op)
981 if (op->value.unsignedp == op[-1].value.unsignedp)
982 return;
984 if (op->value.unsignedp)
986 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
987 cpp_error (pfile, DL_WARNING,
988 "the left operand of \"%s\" changes sign when promoted",
989 cpp_token_as_text (pfile, op->token));
991 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
992 cpp_error (pfile, DL_WARNING,
993 "the right operand of \"%s\" changes sign when promoted",
994 cpp_token_as_text (pfile, op->token));
997 /* Clears the unused high order bits of the number pointed to by PNUM. */
998 static cpp_num
999 num_trim (cpp_num num, size_t precision)
1001 if (precision > PART_PRECISION)
1003 precision -= PART_PRECISION;
1004 if (precision < PART_PRECISION)
1005 num.high &= ((cpp_num_part) 1 << precision) - 1;
1007 else
1009 if (precision < PART_PRECISION)
1010 num.low &= ((cpp_num_part) 1 << precision) - 1;
1011 num.high = 0;
1014 return num;
1017 /* True iff A (presumed signed) >= 0. */
1018 static bool
1019 num_positive (cpp_num num, size_t precision)
1021 if (precision > PART_PRECISION)
1023 precision -= PART_PRECISION;
1024 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1027 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1030 /* Sign extend a number, with PRECISION significant bits and all
1031 others assumed clear, to fill out a cpp_num structure. */
1032 cpp_num
1033 cpp_num_sign_extend (cpp_num num, size_t precision)
1035 if (!num.unsignedp)
1037 if (precision > PART_PRECISION)
1039 precision -= PART_PRECISION;
1040 if (precision < PART_PRECISION
1041 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1042 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1044 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1046 if (precision < PART_PRECISION)
1047 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1048 num.high = ~(cpp_num_part) 0;
1052 return num;
1055 /* Returns the negative of NUM. */
1056 static cpp_num
1057 num_negate (cpp_num num, size_t precision)
1059 cpp_num copy;
1061 copy = num;
1062 num.high = ~num.high;
1063 num.low = ~num.low;
1064 if (++num.low == 0)
1065 num.high++;
1066 num = num_trim (num, precision);
1067 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1069 return num;
1072 /* Returns true if A >= B. */
1073 static bool
1074 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1076 bool unsignedp;
1078 unsignedp = pa.unsignedp || pb.unsignedp;
1080 if (!unsignedp)
1082 /* Both numbers have signed type. If they are of different
1083 sign, the answer is the sign of A. */
1084 unsignedp = num_positive (pa, precision);
1086 if (unsignedp != num_positive (pb, precision))
1087 return unsignedp;
1089 /* Otherwise we can do an unsigned comparison. */
1092 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1095 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1096 static cpp_num
1097 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1098 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1100 lhs.overflow = false;
1101 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1103 /* As excess precision is zeroed, there is no need to num_trim () as
1104 these operations cannot introduce a set bit there. */
1105 if (op == CPP_AND)
1107 lhs.low &= rhs.low;
1108 lhs.high &= rhs.high;
1110 else if (op == CPP_OR)
1112 lhs.low |= rhs.low;
1113 lhs.high |= rhs.high;
1115 else
1117 lhs.low ^= rhs.low;
1118 lhs.high ^= rhs.high;
1121 return lhs;
1124 /* Returns LHS OP RHS, where OP is an inequality. */
1125 static cpp_num
1126 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1127 enum cpp_ttype op)
1129 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1131 if (op == CPP_GREATER_EQ)
1132 lhs.low = gte;
1133 else if (op == CPP_LESS)
1134 lhs.low = !gte;
1135 else if (op == CPP_GREATER)
1136 lhs.low = gte && !num_eq (lhs, rhs);
1137 else /* CPP_LESS_EQ. */
1138 lhs.low = !gte || num_eq (lhs, rhs);
1140 lhs.high = 0;
1141 lhs.overflow = false;
1142 lhs.unsignedp = false;
1143 return lhs;
1146 /* Returns LHS OP RHS, where OP is == or !=. */
1147 static cpp_num
1148 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1149 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1151 /* Work around a 3.0.4 bug; see PR 6950. */
1152 bool eq = num_eq (lhs, rhs);
1153 if (op == CPP_NOT_EQ)
1154 eq = !eq;
1155 lhs.low = eq;
1156 lhs.high = 0;
1157 lhs.overflow = false;
1158 lhs.unsignedp = false;
1159 return lhs;
1162 /* Shift NUM, of width PRECISION, right by N bits. */
1163 static cpp_num
1164 num_rshift (cpp_num num, size_t precision, size_t n)
1166 cpp_num_part sign_mask;
1168 if (num.unsignedp || num_positive (num, precision))
1169 sign_mask = 0;
1170 else
1171 sign_mask = ~(cpp_num_part) 0;
1173 if (n >= precision)
1174 num.high = num.low = sign_mask;
1175 else
1177 /* Sign-extend. */
1178 if (precision < PART_PRECISION)
1179 num.high = sign_mask, num.low |= sign_mask << precision;
1180 else if (precision < 2 * PART_PRECISION)
1181 num.high |= sign_mask << (precision - PART_PRECISION);
1183 if (n >= PART_PRECISION)
1185 n -= PART_PRECISION;
1186 num.low = num.high;
1187 num.high = sign_mask;
1190 if (n)
1192 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1193 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1197 num = num_trim (num, precision);
1198 num.overflow = false;
1199 return num;
1202 /* Shift NUM, of width PRECISION, left by N bits. */
1203 static cpp_num
1204 num_lshift (cpp_num num, size_t precision, size_t n)
1206 if (n >= precision)
1208 num.overflow = !num.unsignedp && !num_zerop (num);
1209 num.high = num.low = 0;
1211 else
1213 cpp_num orig, maybe_orig;
1214 size_t m = n;
1216 orig = num;
1217 if (m >= PART_PRECISION)
1219 m -= PART_PRECISION;
1220 num.high = num.low;
1221 num.low = 0;
1223 if (m)
1225 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1226 num.low <<= m;
1228 num = num_trim (num, precision);
1230 if (num.unsignedp)
1231 num.overflow = false;
1232 else
1234 maybe_orig = num_rshift (num, precision, n);
1235 num.overflow = !num_eq (orig, maybe_orig);
1239 return num;
1242 /* The four unary operators: +, -, ! and ~. */
1243 static cpp_num
1244 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1246 switch (op)
1248 case CPP_UPLUS:
1249 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1250 cpp_error (pfile, DL_WARNING,
1251 "traditional C rejects the unary plus operator");
1252 num.overflow = false;
1253 break;
1255 case CPP_UMINUS:
1256 num = num_negate (num, CPP_OPTION (pfile, precision));
1257 break;
1259 case CPP_COMPL:
1260 num.high = ~num.high;
1261 num.low = ~num.low;
1262 num = num_trim (num, CPP_OPTION (pfile, precision));
1263 num.overflow = false;
1264 break;
1266 default: /* case CPP_NOT: */
1267 num.low = num_zerop (num);
1268 num.high = 0;
1269 num.overflow = false;
1270 num.unsignedp = false;
1271 break;
1274 return num;
1277 /* The various binary operators. */
1278 static cpp_num
1279 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1281 cpp_num result;
1282 size_t precision = CPP_OPTION (pfile, precision);
1283 bool gte;
1284 size_t n;
1286 switch (op)
1288 /* Shifts. */
1289 case CPP_LSHIFT:
1290 case CPP_RSHIFT:
1291 if (!rhs.unsignedp && !num_positive (rhs, precision))
1293 /* A negative shift is a positive shift the other way. */
1294 if (op == CPP_LSHIFT)
1295 op = CPP_RSHIFT;
1296 else
1297 op = CPP_LSHIFT;
1298 rhs = num_negate (rhs, precision);
1300 if (rhs.high)
1301 n = ~0; /* Maximal. */
1302 else
1303 n = rhs.low;
1304 if (op == CPP_LSHIFT)
1305 lhs = num_lshift (lhs, precision, n);
1306 else
1307 lhs = num_rshift (lhs, precision, n);
1308 break;
1310 /* Min / Max. */
1311 case CPP_MIN:
1312 case CPP_MAX:
1314 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1316 gte = num_greater_eq (lhs, rhs, precision);
1317 if (op == CPP_MIN)
1318 gte = !gte;
1319 if (!gte)
1320 lhs = rhs;
1321 lhs.unsignedp = unsignedp;
1323 break;
1325 /* Arithmetic. */
1326 case CPP_MINUS:
1327 rhs = num_negate (rhs, precision);
1328 case CPP_PLUS:
1329 result.low = lhs.low + rhs.low;
1330 result.high = lhs.high + rhs.high;
1331 if (result.low < lhs.low)
1332 result.high++;
1334 result = num_trim (result, precision);
1335 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1336 if (result.unsignedp)
1337 result.overflow = false;
1338 else
1340 bool lhsp = num_positive (lhs, precision);
1341 result.overflow = (lhsp == num_positive (rhs, precision)
1342 && lhsp != num_positive (result, precision));
1344 return result;
1346 /* Comma. */
1347 default: /* case CPP_COMMA: */
1348 if (CPP_PEDANTIC (pfile) && !pfile->state.skip_eval)
1349 cpp_error (pfile, DL_PEDWARN,
1350 "comma operator in operand of #if");
1351 lhs = rhs;
1352 break;
1355 return lhs;
1358 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1359 cannot overflow. */
1360 static cpp_num
1361 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1363 cpp_num result;
1364 cpp_num_part middle[2], temp;
1366 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1367 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1369 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1370 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1372 temp = result.low;
1373 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1374 if (result.low < temp)
1375 result.high++;
1377 temp = result.low;
1378 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1379 if (result.low < temp)
1380 result.high++;
1382 result.high += HIGH_PART (middle[0]);
1383 result.high += HIGH_PART (middle[1]);
1384 result.unsignedp = 1;
1386 return result;
1389 /* Multiply two preprocessing numbers. */
1390 static cpp_num
1391 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1393 cpp_num result, temp;
1394 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1395 bool overflow, negate = false;
1396 size_t precision = CPP_OPTION (pfile, precision);
1398 /* Prepare for unsigned multiplication. */
1399 if (!unsignedp)
1401 if (!num_positive (lhs, precision))
1402 negate = !negate, lhs = num_negate (lhs, precision);
1403 if (!num_positive (rhs, precision))
1404 negate = !negate, rhs = num_negate (rhs, precision);
1407 overflow = lhs.high && rhs.high;
1408 result = num_part_mul (lhs.low, rhs.low);
1410 temp = num_part_mul (lhs.high, rhs.low);
1411 result.high += temp.low;
1412 if (temp.high)
1413 overflow = true;
1415 temp = num_part_mul (lhs.low, rhs.high);
1416 result.high += temp.low;
1417 if (temp.high)
1418 overflow = true;
1420 temp.low = result.low, temp.high = result.high;
1421 result = num_trim (result, precision);
1422 if (!num_eq (result, temp))
1423 overflow = true;
1425 if (negate)
1426 result = num_negate (result, precision);
1428 if (unsignedp)
1429 result.overflow = false;
1430 else
1431 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1432 && !num_zerop (result));
1433 result.unsignedp = unsignedp;
1435 return result;
1438 /* Divide two preprocessing numbers, returning the answer or the
1439 remainder depending upon OP. */
1440 static cpp_num
1441 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1443 cpp_num result, sub;
1444 cpp_num_part mask;
1445 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1446 bool negate = false, lhs_neg = false;
1447 size_t i, precision = CPP_OPTION (pfile, precision);
1449 /* Prepare for unsigned division. */
1450 if (!unsignedp)
1452 if (!num_positive (lhs, precision))
1453 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1454 if (!num_positive (rhs, precision))
1455 negate = !negate, rhs = num_negate (rhs, precision);
1458 /* Find the high bit. */
1459 if (rhs.high)
1461 i = precision - 1;
1462 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1463 for (; ; i--, mask >>= 1)
1464 if (rhs.high & mask)
1465 break;
1467 else if (rhs.low)
1469 if (precision > PART_PRECISION)
1470 i = precision - PART_PRECISION - 1;
1471 else
1472 i = precision - 1;
1473 mask = (cpp_num_part) 1 << i;
1474 for (; ; i--, mask >>= 1)
1475 if (rhs.low & mask)
1476 break;
1478 else
1480 if (!pfile->state.skip_eval)
1481 cpp_error (pfile, DL_ERROR, "division by zero in #if");
1482 return lhs;
1485 /* First nonzero 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;
1496 for (;;)
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);
1503 else
1504 result.low |= (cpp_num_part) 1 << i;
1506 if (i-- == 0)
1507 break;
1508 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1509 sub.high >>= 1;
1512 /* We divide so that the remainder has the sign of the LHS. */
1513 if (op == CPP_DIV)
1515 result.unsignedp = unsignedp;
1516 if (unsignedp)
1517 result.overflow = false;
1518 else
1520 if (negate)
1521 result = num_negate (result, precision);
1522 result.overflow = num_positive (result, precision) ^ !negate;
1525 return result;
1528 /* CPP_MOD. */
1529 lhs.unsignedp = unsignedp;
1530 lhs.overflow = false;
1531 if (lhs_neg)
1532 lhs = num_negate (lhs, precision);
1534 return lhs;