1/* Parse C expressions for cpplib. 2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001, 3 2002, 2004, 2008, 2009, 2010 Free Software Foundation. 4 Contributed by Per Bothner, 1994. 5 6This program is free software; you can redistribute it and/or modify it 7under the terms of the GNU General Public License as published by the 8Free Software Foundation; either version 3, or (at your option) any 9later version. 10 11This program is distributed in the hope that it will be useful, 12but WITHOUT ANY WARRANTY; without even the implied warranty of 13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14GNU General Public License for more details. 15 16You should have received a copy of the GNU General Public License 17along with this program; see the file COPYING3. If not see 18<http://www.gnu.org/licenses/>. */ 19 20#include "config.h" 21#include "system.h" 22#include "cpplib.h" 23#include "internal.h" 24 25#define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT) 26#define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2)) 27#define LOW_PART(num_part) (num_part & HALF_MASK) 28#define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2)) 29 30struct op 31{ 32 const cpp_token *token; /* The token forming op (for diagnostics). */ 33 cpp_num value; /* The value logically "right" of op. */ 34 source_location loc; /* The location of this value. */ 35 enum cpp_ttype op; 36}; 37 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) 41static bool num_positive (cpp_num, size_t); 42static bool num_greater_eq (cpp_num, cpp_num, size_t); 43static cpp_num num_trim (cpp_num, size_t); 44static cpp_num num_part_mul (cpp_num_part, cpp_num_part); 45 46static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype); 47static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype); 48static cpp_num num_negate (cpp_num, size_t); 49static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype); 50static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num, 51 enum cpp_ttype); 52static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num, 53 enum cpp_ttype); 54static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num); 55static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype, 56 source_location); 57static cpp_num num_lshift (cpp_num, size_t, size_t); 58static cpp_num num_rshift (cpp_num, size_t, size_t); 59 60static cpp_num append_digit (cpp_num, int, int, size_t); 61static cpp_num parse_defined (cpp_reader *); 62static cpp_num eval_token (cpp_reader *, const cpp_token *); 63static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype); 64static unsigned int interpret_float_suffix (const uchar *, size_t); 65static unsigned int interpret_int_suffix (const uchar *, size_t); 66static void check_promotion (cpp_reader *, const struct op *); 67 68/* Token type abuse to create unary plus and minus operators. */ 69#define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1)) 70#define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2)) 71 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, CPP_DL_ERROR, msgid); goto syntax_error; } while(0) 76#define SYNTAX_ERROR2(msgid, arg) \ 77 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \ 78 while(0) 79 80/* Subroutine of cpp_classify_number. S points to a float suffix of 81 length LEN, possibly zero. Returns 0 for an invalid suffix, or a 82 flag vector describing the suffix. */ 83static unsigned int 84interpret_float_suffix (const uchar *s, size_t len) 85{ 86 size_t flags; 87 size_t f, d, l, w, q, i; 88 89 flags = 0; 90 f = d = l = w = q = i = 0; 91 92 /* Process decimal float suffixes, which are two letters starting 93 with d or D. Order and case are significant. */ 94 if (len == 2 && (*s == 'd' || *s == 'D')) 95 { 96 bool uppercase = (*s == 'D'); 97 switch (s[1]) 98 { 99 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break; 100 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break; 101 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break; 102 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break; 103 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break; 104 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break; 105 default: 106 /* Additional two-character suffixes beginning with D are not 107 for decimal float constants. */ 108 break; 109 } 110 } 111 112 /* Recognize a fixed-point suffix. */ 113 switch (s[len-1]) 114 { 115 case 'k': case 'K': flags = CPP_N_ACCUM; break; 116 case 'r': case 'R': flags = CPP_N_FRACT; break; 117 default: break; 118 } 119 120 /* Continue processing a fixed-point suffix. The suffix is case 121 insensitive except for ll or LL. Order is significant. */ 122 if (flags) 123 { 124 if (len == 1) 125 return flags; 126 len--; 127 128 if (*s == 'u' || *s == 'U') 129 { 130 flags |= CPP_N_UNSIGNED; 131 if (len == 1) 132 return flags; 133 len--; 134 s++; 135 } 136 137 switch (*s) 138 { 139 case 'h': case 'H': 140 if (len == 1) 141 return flags |= CPP_N_SMALL; 142 break; 143 case 'l': 144 if (len == 1) 145 return flags |= CPP_N_MEDIUM; 146 if (len == 2 && s[1] == 'l') 147 return flags |= CPP_N_LARGE; 148 break; 149 case 'L': 150 if (len == 1) 151 return flags |= CPP_N_MEDIUM; 152 if (len == 2 && s[1] == 'L') 153 return flags |= CPP_N_LARGE; 154 break; 155 default: 156 break; 157 } 158 /* Anything left at this point is invalid. */ 159 return 0; 160 } 161 162 /* In any remaining valid suffix, the case and order don't matter. */ 163 while (len--) 164 switch (s[len]) 165 { 166 case 'f': case 'F': f++; break; 167 case 'd': case 'D': d++; break; 168 case 'l': case 'L': l++; break; 169 case 'w': case 'W': w++; break; 170 case 'q': case 'Q': q++; break; 171 case 'i': case 'I': 172 case 'j': case 'J': i++; break; 173 default: 174 return 0; 175 } 176 177 if (f + d + l + w + q > 1 || i > 1) 178 return 0; 179 180 return ((i ? CPP_N_IMAGINARY : 0) 181 | (f ? CPP_N_SMALL : 182 d ? CPP_N_MEDIUM : 183 l ? CPP_N_LARGE : 184 w ? CPP_N_MD_W : 185 q ? CPP_N_MD_Q : CPP_N_DEFAULT)); 186} 187 188/* Subroutine of cpp_classify_number. S points to an integer suffix 189 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a 190 flag vector describing the suffix. */ 191static unsigned int 192interpret_int_suffix (const uchar *s, size_t len) 193{ 194 size_t u, l, i; 195 196 u = l = i = 0; 197 198 while (len--) 199 switch (s[len]) 200 { 201 case 'u': case 'U': u++; break; 202 case 'i': case 'I': 203 case 'j': case 'J': i++; break; 204 case 'l': case 'L': l++; 205 /* If there are two Ls, they must be adjacent and the same case. */ 206 if (l == 2 && s[len] != s[len + 1]) 207 return 0; 208 break; 209 default: 210 return 0; 211 } 212 213 if (l > 2 || u > 1 || i > 1) 214 return 0; 215 216 return ((i ? CPP_N_IMAGINARY : 0) 217 | (u ? CPP_N_UNSIGNED : 0) 218 | ((l == 0) ? CPP_N_SMALL 219 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE)); 220} 221 222/* Categorize numeric constants according to their field (integer, 223 floating point, or invalid), radix (decimal, octal, hexadecimal), 224 and type suffixes. */ 225unsigned int 226cpp_classify_number (cpp_reader *pfile, const cpp_token *token) 227{ 228 const uchar *str = token->val.str.text; 229 const uchar *limit; 230 unsigned int max_digit, result, radix; 231 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag; 232 bool seen_digit; 233 234 /* If the lexer has done its job, length one can only be a single 235 digit. Fast-path this very common case. */ 236 if (token->val.str.len == 1) 237 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL; 238 239 limit = str + token->val.str.len; 240 float_flag = NOT_FLOAT; 241 max_digit = 0; 242 radix = 10; 243 seen_digit = false; 244 245 /* First, interpret the radix. */ 246 if (*str == '0') 247 { 248 radix = 8; 249 str++; 250 251 /* Require at least one hex digit to classify it as hex. */ 252 if ((*str == 'x' || *str == 'X') 253 && (str[1] == '.' || ISXDIGIT (str[1]))) 254 { 255 radix = 16; 256 str++; 257 } 258 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1')) 259 { 260 radix = 2; 261 str++; 262 } 263 } 264 265 /* Now scan for a well-formed integer or float. */ 266 for (;;) 267 { 268 unsigned int c = *str++; 269 270 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16)) 271 { 272 seen_digit = true; 273 c = hex_value (c); 274 if (c > max_digit) 275 max_digit = c; 276 } 277 else if (c == '.') 278 { 279 if (float_flag == NOT_FLOAT) 280 float_flag = AFTER_POINT; 281 else 282 SYNTAX_ERROR ("too many decimal points in number"); 283 } 284 else if ((radix <= 10 && (c == 'e' || c == 'E')) 285 || (radix == 16 && (c == 'p' || c == 'P'))) 286 { 287 float_flag = AFTER_EXPON; 288 break; 289 } 290 else 291 { 292 /* Start of suffix. */ 293 str--; 294 break; 295 } 296 } 297 298 /* The suffix may be for decimal fixed-point constants without exponent. */ 299 if (radix != 16 && float_flag == NOT_FLOAT) 300 { 301 result = interpret_float_suffix (str, limit - str); 302 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM)) 303 { 304 result |= CPP_N_FLOATING; 305 /* We need to restore the radix to 10, if the radix is 8. */ 306 if (radix == 8) 307 radix = 10; 308 309 if (CPP_PEDANTIC (pfile)) 310 cpp_error (pfile, CPP_DL_PEDWARN, 311 "fixed-point constants are a GCC extension"); 312 goto syntax_ok; 313 } 314 else 315 result = 0; 316 } 317 318 if (float_flag != NOT_FLOAT && radix == 8) 319 radix = 10; 320 321 if (max_digit >= radix) 322 { 323 if (radix == 2) 324 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit); 325 else 326 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit); 327 } 328 329 if (float_flag != NOT_FLOAT) 330 { 331 if (radix == 2) 332 { 333 cpp_error (pfile, CPP_DL_ERROR, 334 "invalid prefix \"0b\" for floating constant"); 335 return CPP_N_INVALID; 336 } 337 338 if (radix == 16 && !seen_digit) 339 SYNTAX_ERROR ("no digits in hexadecimal floating constant"); 340 341 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99)) 342 cpp_error (pfile, CPP_DL_PEDWARN, 343 "use of C99 hexadecimal floating constant"); 344 345 if (float_flag == AFTER_EXPON) 346 { 347 if (*str == '+' || *str == '-') 348 str++; 349 350 /* Exponent is decimal, even if string is a hex float. */ 351 if (!ISDIGIT (*str)) 352 SYNTAX_ERROR ("exponent has no digits"); 353 354 do 355 str++; 356 while (ISDIGIT (*str)); 357 } 358 else if (radix == 16) 359 SYNTAX_ERROR ("hexadecimal floating constants require an exponent"); 360 361 result = interpret_float_suffix (str, limit - str); 362 if (result == 0) 363 { 364 cpp_error (pfile, CPP_DL_ERROR, 365 "invalid suffix \"%.*s\" on floating constant", 366 (int) (limit - str), str); 367 return CPP_N_INVALID; 368 } 369 370 /* Traditional C didn't accept any floating suffixes. */ 371 if (limit != str 372 && CPP_WTRADITIONAL (pfile) 373 && ! cpp_sys_macro_p (pfile)) 374 cpp_error (pfile, CPP_DL_WARNING, 375 "traditional C rejects the \"%.*s\" suffix", 376 (int) (limit - str), str); 377 378 /* A suffix for double is a GCC extension via decimal float support. 379 If the suffix also specifies an imaginary value we'll catch that 380 later. */ 381 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile)) 382 cpp_error (pfile, CPP_DL_PEDWARN, 383 "suffix for double constant is a GCC extension"); 384 385 /* Radix must be 10 for decimal floats. */ 386 if ((result & CPP_N_DFLOAT) && radix != 10) 387 { 388 cpp_error (pfile, CPP_DL_ERROR, 389 "invalid suffix \"%.*s\" with hexadecimal floating constant", 390 (int) (limit - str), str); 391 return CPP_N_INVALID; 392 } 393 394 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile)) 395 cpp_error (pfile, CPP_DL_PEDWARN, 396 "fixed-point constants are a GCC extension"); 397 398 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile)) 399 cpp_error (pfile, CPP_DL_PEDWARN, 400 "decimal float constants are a GCC extension"); 401 402 result |= CPP_N_FLOATING; 403 } 404 else 405 { 406 result = interpret_int_suffix (str, limit - str); 407 if (result == 0) 408 { 409 cpp_error (pfile, CPP_DL_ERROR, 410 "invalid suffix \"%.*s\" on integer constant", 411 (int) (limit - str), str); 412 return CPP_N_INVALID; 413 } 414 415 /* Traditional C only accepted the 'L' suffix. 416 Suppress warning about 'LL' with -Wno-long-long. */ 417 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile)) 418 { 419 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY)); 420 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE; 421 422 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long))) 423 cpp_error (pfile, CPP_DL_WARNING, 424 "traditional C rejects the \"%.*s\" suffix", 425 (int) (limit - str), str); 426 } 427 428 if ((result & CPP_N_WIDTH) == CPP_N_LARGE 429 && CPP_OPTION (pfile, warn_long_long)) 430 cpp_error (pfile, 431 CPP_OPTION (pfile, c99) ? CPP_DL_WARNING : CPP_DL_PEDWARN, 432 CPP_OPTION (pfile, cplusplus) 433 ? "use of C++0x long long integer constant" 434 : "use of C99 long long integer constant"); 435 436 result |= CPP_N_INTEGER; 437 } 438 439 syntax_ok: 440 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile)) 441 cpp_error (pfile, CPP_DL_PEDWARN, 442 "imaginary constants are a GCC extension"); 443 if (radix == 2 && CPP_PEDANTIC (pfile)) 444 cpp_error (pfile, CPP_DL_PEDWARN, 445 "binary constants are a GCC extension"); 446 447 if (radix == 10) 448 result |= CPP_N_DECIMAL; 449 else if (radix == 16) 450 result |= CPP_N_HEX; 451 else if (radix == 2) 452 result |= CPP_N_BINARY; 453 else 454 result |= CPP_N_OCTAL; 455 456 return result; 457 458 syntax_error: 459 return CPP_N_INVALID; 460} 461 462/* cpp_interpret_integer converts an integer constant into a cpp_num, 463 of precision options->precision. 464 465 We do not provide any interface for decimal->float conversion, 466 because the preprocessor doesn't need it and we don't want to 467 drag in GCC's floating point emulator. */ 468cpp_num 469cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token, 470 unsigned int type) 471{ 472 const uchar *p, *end; 473 cpp_num result; 474 475 result.low = 0; 476 result.high = 0; 477 result.unsignedp = !!(type & CPP_N_UNSIGNED); 478 result.overflow = false; 479 480 p = token->val.str.text; 481 end = p + token->val.str.len; 482 483 /* Common case of a single digit. */ 484 if (token->val.str.len == 1) 485 result.low = p[0] - '0'; 486 else 487 { 488 cpp_num_part max; 489 size_t precision = CPP_OPTION (pfile, precision); 490 unsigned int base = 10, c = 0; 491 bool overflow = false; 492 493 if ((type & CPP_N_RADIX) == CPP_N_OCTAL) 494 { 495 base = 8; 496 p++; 497 } 498 else if ((type & CPP_N_RADIX) == CPP_N_HEX) 499 { 500 base = 16; 501 p += 2; 502 } 503 else if ((type & CPP_N_RADIX) == CPP_N_BINARY) 504 { 505 base = 2; 506 p += 2; 507 } 508 509 /* We can add a digit to numbers strictly less than this without 510 needing the precision and slowness of double integers. */ 511 max = ~(cpp_num_part) 0; 512 if (precision < PART_PRECISION) 513 max >>= PART_PRECISION - precision; 514 max = (max - base + 1) / base + 1; 515 516 for (; p < end; p++) 517 { 518 c = *p; 519 520 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c))) 521 c = hex_value (c); 522 else 523 break; 524 525 /* Strict inequality for when max is set to zero. */ 526 if (result.low < max) 527 result.low = result.low * base + c; 528 else 529 { 530 result = append_digit (result, c, base, precision); 531 overflow |= result.overflow; 532 max = 0; 533 } 534 } 535 536 if (overflow) 537 cpp_error (pfile, CPP_DL_PEDWARN, 538 "integer constant is too large for its type"); 539 /* If too big to be signed, consider it unsigned. Only warn for 540 decimal numbers. Traditional numbers were always signed (but 541 we still honor an explicit U suffix); but we only have 542 traditional semantics in directives. */ 543 else if (!result.unsignedp 544 && !(CPP_OPTION (pfile, traditional) 545 && pfile->state.in_directive) 546 && !num_positive (result, precision)) 547 { 548 /* This is for constants within the range of uintmax_t but 549 not that of intmax_t. For such decimal constants, a 550 diagnostic is required for C99 as the selected type must 551 be signed and not having a type is a constraint violation 552 (DR#298, TC3), so this must be a pedwarn. For C90, 553 unsigned long is specified to be used for a constant that 554 does not fit in signed long; if uintmax_t has the same 555 range as unsigned long this means only a warning is 556 appropriate here. C90 permits the preprocessor to use a 557 wider range than unsigned long in the compiler, so if 558 uintmax_t is wider than unsigned long no diagnostic is 559 required for such constants in preprocessor #if 560 expressions and the compiler will pedwarn for such 561 constants outside the range of unsigned long that reach 562 the compiler so a diagnostic is not required there 563 either; thus, pedwarn for C99 but use a plain warning for 564 C90. */ 565 if (base == 10) 566 cpp_error (pfile, (CPP_OPTION (pfile, c99) 567 ? CPP_DL_PEDWARN 568 : CPP_DL_WARNING), 569 "integer constant is so large that it is unsigned"); 570 result.unsignedp = true; 571 } 572 } 573 574 return result; 575} 576 577/* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */ 578static cpp_num 579append_digit (cpp_num num, int digit, int base, size_t precision) 580{ 581 cpp_num result; 582 unsigned int shift; 583 bool overflow; 584 cpp_num_part add_high, add_low; 585 586 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't 587 need to worry about add_high overflowing. */ 588 switch (base) 589 { 590 case 2: 591 shift = 1; 592 break; 593 594 case 16: 595 shift = 4; 596 break; 597 598 default: 599 shift = 3; 600 } 601 overflow = !!(num.high >> (PART_PRECISION - shift)); 602 result.high = num.high << shift; 603 result.low = num.low << shift; 604 result.high |= num.low >> (PART_PRECISION - shift); 605 result.unsignedp = num.unsignedp; 606 607 if (base == 10) 608 { 609 add_low = num.low << 1; 610 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1)); 611 } 612 else 613 add_high = add_low = 0; 614 615 if (add_low + digit < add_low) 616 add_high++; 617 add_low += digit; 618 619 if (result.low + add_low < result.low) 620 add_high++; 621 if (result.high + add_high < result.high) 622 overflow = true; 623 624 result.low += add_low; 625 result.high += add_high; 626 result.overflow = overflow; 627 628 /* The above code catches overflow of a cpp_num type. This catches 629 overflow of the (possibly shorter) target precision. */ 630 num.low = result.low; 631 num.high = result.high; 632 result = num_trim (result, precision); 633 if (!num_eq (result, num)) 634 result.overflow = true; 635 636 return result; 637} 638 639/* Handle meeting "defined" in a preprocessor expression. */ 640static cpp_num 641parse_defined (cpp_reader *pfile) 642{ 643 cpp_num result; 644 int paren = 0; 645 cpp_hashnode *node = 0; 646 const cpp_token *token; 647 cpp_context *initial_context = pfile->context; 648 649 /* Don't expand macros. */ 650 pfile->state.prevent_expansion++; 651 652 token = cpp_get_token (pfile); 653 if (token->type == CPP_OPEN_PAREN) 654 { 655 paren = 1; 656 token = cpp_get_token (pfile); 657 } 658 659 if (token->type == CPP_NAME) 660 { 661 node = token->val.node.node; 662 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN) 663 { 664 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\""); 665 node = 0; 666 } 667 } 668 else 669 { 670 cpp_error (pfile, CPP_DL_ERROR, 671 "operator \"defined\" requires an identifier"); 672 if (token->flags & NAMED_OP) 673 { 674 cpp_token op; 675 676 op.flags = 0; 677 op.type = token->type; 678 cpp_error (pfile, CPP_DL_ERROR, 679 "(\"%s\" is an alternative token for \"%s\" in C++)", 680 cpp_token_as_text (pfile, token), 681 cpp_token_as_text (pfile, &op)); 682 } 683 } 684 685 if (node) 686 { 687 if (pfile->context != initial_context && CPP_PEDANTIC (pfile)) 688 cpp_error (pfile, CPP_DL_WARNING, 689 "this use of \"defined\" may not be portable"); 690 691 _cpp_mark_macro_used (node); 692 if (!(node->flags & NODE_USED)) 693 { 694 node->flags |= NODE_USED; 695 if (node->type == NT_MACRO) 696 { 697 if (pfile->cb.used_define) 698 pfile->cb.used_define (pfile, pfile->directive_line, node); 699 } 700 else 701 { 702 if (pfile->cb.used_undef) 703 pfile->cb.used_undef (pfile, pfile->directive_line, node); 704 } 705 } 706 707 /* A possible controlling macro of the form #if !defined (). 708 _cpp_parse_expr checks there was no other junk on the line. */ 709 pfile->mi_ind_cmacro = node; 710 } 711 712 pfile->state.prevent_expansion--; 713 714 /* Do not treat conditional macros as being defined. This is due to the 715 powerpc and spu ports using conditional macros for 'vector', 'bool', and 716 'pixel' to act as conditional keywords. This messes up tests like #ifndef 717 bool. */ 718 result.unsignedp = false; 719 result.high = 0; 720 result.overflow = false; 721 result.low = (node && node->type == NT_MACRO 722 && (node->flags & NODE_CONDITIONAL) == 0); 723 return result; 724} 725 726/* Convert a token into a CPP_NUMBER (an interpreted preprocessing 727 number or character constant, or the result of the "defined" or "#" 728 operators). */ 729static cpp_num 730eval_token (cpp_reader *pfile, const cpp_token *token) 731{ 732 cpp_num result; 733 unsigned int temp; 734 int unsignedp = 0; 735 736 result.unsignedp = false; 737 result.overflow = false; 738 739 switch (token->type) 740 { 741 case CPP_NUMBER: 742 temp = cpp_classify_number (pfile, token); 743 switch (temp & CPP_N_CATEGORY) 744 { 745 case CPP_N_FLOATING: 746 cpp_error (pfile, CPP_DL_ERROR, 747 "floating constant in preprocessor expression"); 748 break; 749 case CPP_N_INTEGER: 750 if (!(temp & CPP_N_IMAGINARY)) 751 return cpp_interpret_integer (pfile, token, temp); 752 cpp_error (pfile, CPP_DL_ERROR, 753 "imaginary number in preprocessor expression"); 754 break; 755 756 case CPP_N_INVALID: 757 /* Error already issued. */ 758 break; 759 } 760 result.high = result.low = 0; 761 break; 762 763 case CPP_WCHAR: 764 case CPP_CHAR: 765 case CPP_CHAR16: 766 case CPP_CHAR32: 767 { 768 cppchar_t cc = cpp_interpret_charconst (pfile, token, 769 &temp, &unsignedp); 770 771 result.high = 0; 772 result.low = cc; 773 /* Sign-extend the result if necessary. */ 774 if (!unsignedp && (cppchar_signed_t) cc < 0) 775 { 776 if (PART_PRECISION > BITS_PER_CPPCHAR_T) 777 result.low |= ~(~(cpp_num_part) 0 778 >> (PART_PRECISION - BITS_PER_CPPCHAR_T)); 779 result.high = ~(cpp_num_part) 0; 780 result = num_trim (result, CPP_OPTION (pfile, precision)); 781 } 782 } 783 break; 784 785 case CPP_NAME: 786 if (token->val.node.node == pfile->spec_nodes.n_defined) 787 return parse_defined (pfile); 788 else if (CPP_OPTION (pfile, cplusplus) 789 && (token->val.node.node == pfile->spec_nodes.n_true 790 || token->val.node.node == pfile->spec_nodes.n_false)) 791 { 792 result.high = 0; 793 result.low = (token->val.node.node == pfile->spec_nodes.n_true); 794 } 795 else 796 { 797 result.high = 0; 798 result.low = 0; 799 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval) 800 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined", 801 NODE_NAME (token->val.node.node)); 802 } 803 break; 804 805 case CPP_HASH: 806 if (!pfile->state.skipping) 807 { 808 /* A pedantic warning takes precedence over a deprecated 809 warning here. */ 810 if (CPP_PEDANTIC (pfile)) 811 cpp_error (pfile, CPP_DL_PEDWARN, 812 "assertions are a GCC extension"); 813 else if (CPP_OPTION (pfile, warn_deprecated)) 814 cpp_error (pfile, CPP_DL_WARNING, 815 "assertions are a deprecated extension"); 816 } 817 _cpp_test_assertion (pfile, &temp); 818 result.high = 0; 819 result.low = temp; 820 break; 821 822 default: 823 abort (); 824 } 825 826 result.unsignedp = !!unsignedp; 827 return result; 828} 829 830/* Operator precedence and flags table. 831 832After an operator is returned from the lexer, if it has priority less 833than the operator on the top of the stack, we reduce the stack by one 834operator and repeat the test. Since equal priorities do not reduce, 835this is naturally right-associative. 836 837We handle left-associative operators by decrementing the priority of 838just-lexed operators by one, but retaining the priority of operators 839already on the stack. 840 841The remaining cases are '(' and ')'. We handle '(' by skipping the 842reduction phase completely. ')' is given lower priority than 843everything else, including '(', effectively forcing a reduction of the 844parenthesized expression. If there is a matching '(', the routine 845reduce() exits immediately. If the normal exit route sees a ')', then 846there cannot have been a matching '(' and an error message is output. 847 848The parser assumes all shifted operators require a left operand unless 849the flag NO_L_OPERAND is set. These semantics are automatic; any 850extra semantics need to be handled with operator-specific code. */ 851 852/* Flags. If CHECK_PROMOTION, we warn if the effective sign of an 853 operand changes because of integer promotions. */ 854#define NO_L_OPERAND (1 << 0) 855#define LEFT_ASSOC (1 << 1) 856#define CHECK_PROMOTION (1 << 2) 857 858/* Operator to priority map. Must be in the same order as the first 859 N entries of enum cpp_ttype. */ 860static const struct cpp_operator 861{ 862 uchar prio; 863 uchar flags; 864} optab[] = 865{ 866 /* EQ */ {0, 0}, /* Shouldn't happen. */ 867 /* NOT */ {16, NO_L_OPERAND}, 868 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION}, 869 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION}, 870 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION}, 871 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION}, 872 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION}, 873 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION}, 874 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION}, 875 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION}, 876 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION}, 877 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION}, 878 /* RSHIFT */ {13, LEFT_ASSOC}, 879 /* LSHIFT */ {13, LEFT_ASSOC}, 880 881 /* COMPL */ {16, NO_L_OPERAND}, 882 /* AND_AND */ {6, LEFT_ASSOC}, 883 /* OR_OR */ {5, LEFT_ASSOC}, 884 /* Note that QUERY, COLON, and COMMA must have the same precedence. 885 However, there are some special cases for these in reduce(). */ 886 /* QUERY */ {4, 0}, 887 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION}, 888 /* COMMA */ {4, LEFT_ASSOC}, 889 /* OPEN_PAREN */ {1, NO_L_OPERAND}, 890 /* CLOSE_PAREN */ {0, 0}, 891 /* EOF */ {0, 0}, 892 /* EQ_EQ */ {11, LEFT_ASSOC}, 893 /* NOT_EQ */ {11, LEFT_ASSOC}, 894 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION}, 895 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION}, 896 /* UPLUS */ {16, NO_L_OPERAND}, 897 /* UMINUS */ {16, NO_L_OPERAND} 898}; 899 900/* Parse and evaluate a C expression, reading from PFILE. 901 Returns the truth value of the expression. 902 903 The implementation is an operator precedence parser, i.e. a 904 bottom-up parser, using a stack for not-yet-reduced tokens. 905 906 The stack base is op_stack, and the current stack pointer is 'top'. 907 There is a stack element for each operator (only), and the most 908 recently pushed operator is 'top->op'. An operand (value) is 909 stored in the 'value' field of the stack element of the operator 910 that precedes it. */ 911bool 912_cpp_parse_expr (cpp_reader *pfile, bool is_if) 913{ 914 struct op *top = pfile->op_stack; 915 unsigned int lex_count; 916 bool saw_leading_not, want_value = true; 917 918 pfile->state.skip_eval = 0; 919 920 /* Set up detection of #if ! defined(). */ 921 pfile->mi_ind_cmacro = 0; 922 saw_leading_not = false; 923 lex_count = 0; 924 925 /* Lowest priority operator prevents further reductions. */ 926 top->op = CPP_EOF; 927 928 for (;;) 929 { 930 struct op op; 931 932 lex_count++; 933 op.token = cpp_get_token (pfile); 934 op.op = op.token->type; 935 op.loc = op.token->src_loc; 936 937 switch (op.op) 938 { 939 /* These tokens convert into values. */ 940 case CPP_NUMBER: 941 case CPP_CHAR: 942 case CPP_WCHAR: 943 case CPP_CHAR16: 944 case CPP_CHAR32: 945 case CPP_NAME: 946 case CPP_HASH: 947 if (!want_value) 948 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"", 949 cpp_token_as_text (pfile, op.token)); 950 want_value = false; 951 top->value = eval_token (pfile, op.token); 952 continue; 953 954 case CPP_NOT: 955 saw_leading_not = lex_count == 1; 956 break; 957 case CPP_PLUS: 958 if (want_value) 959 op.op = CPP_UPLUS; 960 break; 961 case CPP_MINUS: 962 if (want_value) 963 op.op = CPP_UMINUS; 964 break; 965 966 default: 967 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ) 968 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions", 969 cpp_token_as_text (pfile, op.token)); 970 break; 971 } 972 973 /* Check we have a value or operator as appropriate. */ 974 if (optab[op.op].flags & NO_L_OPERAND) 975 { 976 if (!want_value) 977 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"", 978 cpp_token_as_text (pfile, op.token)); 979 } 980 else if (want_value) 981 { 982 /* We want a number (or expression) and haven't got one. 983 Try to emit a specific diagnostic. */ 984 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN) 985 SYNTAX_ERROR ("missing expression between '(' and ')'"); 986 987 if (op.op == CPP_EOF && top->op == CPP_EOF) 988 SYNTAX_ERROR2 ("%s with no expression", is_if ? "#if" : "#elif"); 989 990 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN) 991 SYNTAX_ERROR2 ("operator '%s' has no right operand", 992 cpp_token_as_text (pfile, top->token)); 993 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF) 994 /* Complain about missing paren during reduction. */; 995 else 996 SYNTAX_ERROR2 ("operator '%s' has no left operand", 997 cpp_token_as_text (pfile, op.token)); 998 } 999 1000 top = reduce (pfile, top, op.op); 1001 if (!top) 1002 goto syntax_error; 1003 1004 if (op.op == CPP_EOF) 1005 break; 1006 1007 switch (op.op) 1008 { 1009 case CPP_CLOSE_PAREN: 1010 continue; 1011 case CPP_OR_OR: 1012 if (!num_zerop (top->value)) 1013 pfile->state.skip_eval++; 1014 break; 1015 case CPP_AND_AND: 1016 case CPP_QUERY: 1017 if (num_zerop (top->value)) 1018 pfile->state.skip_eval++; 1019 break; 1020 case CPP_COLON: 1021 if (top->op != CPP_QUERY) 1022 SYNTAX_ERROR (" ':' without preceding '?'"); 1023 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */ 1024 pfile->state.skip_eval++; 1025 else 1026 pfile->state.skip_eval--; 1027 default: 1028 break; 1029 } 1030 1031 want_value = true; 1032 1033 /* Check for and handle stack overflow. */ 1034 if (++top == pfile->op_limit) 1035 top = _cpp_expand_op_stack (pfile); 1036 1037 top->op = op.op; 1038 top->token = op.token; 1039 top->loc = op.token->src_loc; 1040 } 1041 1042 /* The controlling macro expression is only valid if we called lex 3 1043 times: <!> <defined expression> and <EOF>. push_conditional () 1044 checks that we are at top-of-file. */ 1045 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3)) 1046 pfile->mi_ind_cmacro = 0; 1047 1048 if (top != pfile->op_stack) 1049 { 1050 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in %s", 1051 is_if ? "#if" : "#elif"); 1052 syntax_error: 1053 return false; /* Return false on syntax error. */ 1054 } 1055 1056 return !num_zerop (top->value); 1057} 1058 1059/* Reduce the operator / value stack if possible, in preparation for 1060 pushing operator OP. Returns NULL on error, otherwise the top of 1061 the stack. */ 1062static struct op * 1063reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op) 1064{ 1065 unsigned int prio; 1066 1067 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2) 1068 { 1069 bad_op: 1070 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op); 1071 return 0; 1072 } 1073 1074 if (op == CPP_OPEN_PAREN) 1075 return top; 1076 1077 /* Decrement the priority of left-associative operators to force a 1078 reduction with operators of otherwise equal priority. */ 1079 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0); 1080 while (prio < optab[top->op].prio) 1081 { 1082 if (CPP_OPTION (pfile, warn_num_sign_change) 1083 && optab[top->op].flags & CHECK_PROMOTION) 1084 check_promotion (pfile, top); 1085 1086 switch (top->op) 1087 { 1088 case CPP_UPLUS: 1089 case CPP_UMINUS: 1090 case CPP_NOT: 1091 case CPP_COMPL: 1092 top[-1].value = num_unary_op (pfile, top->value, top->op); 1093 top[-1].loc = top->loc; 1094 break; 1095 1096 case CPP_PLUS: 1097 case CPP_MINUS: 1098 case CPP_RSHIFT: 1099 case CPP_LSHIFT: 1100 case CPP_COMMA: 1101 top[-1].value = num_binary_op (pfile, top[-1].value, 1102 top->value, top->op); 1103 top[-1].loc = top->loc; 1104 break; 1105 1106 case CPP_GREATER: 1107 case CPP_LESS: 1108 case CPP_GREATER_EQ: 1109 case CPP_LESS_EQ: 1110 top[-1].value 1111 = num_inequality_op (pfile, top[-1].value, top->value, top->op); 1112 top[-1].loc = top->loc; 1113 break; 1114 1115 case CPP_EQ_EQ: 1116 case CPP_NOT_EQ: 1117 top[-1].value 1118 = num_equality_op (pfile, top[-1].value, top->value, top->op); 1119 top[-1].loc = top->loc; 1120 break; 1121 1122 case CPP_AND: 1123 case CPP_OR: 1124 case CPP_XOR: 1125 top[-1].value 1126 = num_bitwise_op (pfile, top[-1].value, top->value, top->op); 1127 top[-1].loc = top->loc; 1128 break; 1129 1130 case CPP_MULT: 1131 top[-1].value = num_mul (pfile, top[-1].value, top->value); 1132 top[-1].loc = top->loc; 1133 break; 1134 1135 case CPP_DIV: 1136 case CPP_MOD: 1137 top[-1].value = num_div_op (pfile, top[-1].value, 1138 top->value, top->op, top->loc); 1139 top[-1].loc = top->loc; 1140 break; 1141 1142 case CPP_OR_OR: 1143 top--; 1144 if (!num_zerop (top->value)) 1145 pfile->state.skip_eval--; 1146 top->value.low = (!num_zerop (top->value) 1147 || !num_zerop (top[1].value)); 1148 top->value.high = 0; 1149 top->value.unsignedp = false; 1150 top->value.overflow = false; 1151 top->loc = top[1].loc; 1152 continue; 1153 1154 case CPP_AND_AND: 1155 top--; 1156 if (num_zerop (top->value)) 1157 pfile->state.skip_eval--; 1158 top->value.low = (!num_zerop (top->value) 1159 && !num_zerop (top[1].value)); 1160 top->value.high = 0; 1161 top->value.unsignedp = false; 1162 top->value.overflow = false; 1163 top->loc = top[1].loc; 1164 continue; 1165 1166 case CPP_OPEN_PAREN: 1167 if (op != CPP_CLOSE_PAREN) 1168 { 1169 cpp_error_with_line (pfile, CPP_DL_ERROR, 1170 top->token->src_loc, 1171 0, "missing ')' in expression"); 1172 return 0; 1173 } 1174 top--; 1175 top->value = top[1].value; 1176 top->loc = top[1].loc; 1177 return top; 1178 1179 case CPP_COLON: 1180 top -= 2; 1181 if (!num_zerop (top->value)) 1182 { 1183 pfile->state.skip_eval--; 1184 top->value = top[1].value; 1185 top->loc = top[1].loc; 1186 } 1187 else 1188 { 1189 top->value = top[2].value; 1190 top->loc = top[2].loc; 1191 } 1192 top->value.unsignedp = (top[1].value.unsignedp 1193 || top[2].value.unsignedp); 1194 continue; 1195 1196 case CPP_QUERY: 1197 /* COMMA and COLON should not reduce a QUERY operator. */ 1198 if (op == CPP_COMMA || op == CPP_COLON) 1199 return top; 1200 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'"); 1201 return 0; 1202 1203 default: 1204 goto bad_op; 1205 } 1206 1207 top--; 1208 if (top->value.overflow && !pfile->state.skip_eval) 1209 cpp_error (pfile, CPP_DL_PEDWARN, 1210 "integer overflow in preprocessor expression"); 1211 } 1212 1213 if (op == CPP_CLOSE_PAREN) 1214 { 1215 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression"); 1216 return 0; 1217 } 1218 1219 return top; 1220} 1221 1222/* Returns the position of the old top of stack after expansion. */ 1223struct op * 1224_cpp_expand_op_stack (cpp_reader *pfile) 1225{ 1226 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack); 1227 size_t new_size = old_size * 2 + 20; 1228 1229 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size); 1230 pfile->op_limit = pfile->op_stack + new_size; 1231 1232 return pfile->op_stack + old_size; 1233} 1234 1235/* Emits a warning if the effective sign of either operand of OP 1236 changes because of integer promotions. */ 1237static void 1238check_promotion (cpp_reader *pfile, const struct op *op) 1239{ 1240 if (op->value.unsignedp == op[-1].value.unsignedp) 1241 return; 1242 1243 if (op->value.unsignedp) 1244 { 1245 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision))) 1246 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0, 1247 "the left operand of \"%s\" changes sign when promoted", 1248 cpp_token_as_text (pfile, op->token)); 1249 } 1250 else if (!num_positive (op->value, CPP_OPTION (pfile, precision))) 1251 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0, 1252 "the right operand of \"%s\" changes sign when promoted", 1253 cpp_token_as_text (pfile, op->token)); 1254} 1255 1256/* Clears the unused high order bits of the number pointed to by PNUM. */ 1257static cpp_num 1258num_trim (cpp_num num, size_t precision) 1259{ 1260 if (precision > PART_PRECISION) 1261 { 1262 precision -= PART_PRECISION; 1263 if (precision < PART_PRECISION) 1264 num.high &= ((cpp_num_part) 1 << precision) - 1; 1265 } 1266 else 1267 { 1268 if (precision < PART_PRECISION) 1269 num.low &= ((cpp_num_part) 1 << precision) - 1; 1270 num.high = 0; 1271 } 1272 1273 return num; 1274} 1275 1276/* True iff A (presumed signed) >= 0. */ 1277static bool 1278num_positive (cpp_num num, size_t precision) 1279{ 1280 if (precision > PART_PRECISION) 1281 { 1282 precision -= PART_PRECISION; 1283 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0; 1284 } 1285 1286 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0; 1287} 1288 1289/* Sign extend a number, with PRECISION significant bits and all 1290 others assumed clear, to fill out a cpp_num structure. */ 1291cpp_num 1292cpp_num_sign_extend (cpp_num num, size_t precision) 1293{ 1294 if (!num.unsignedp) 1295 { 1296 if (precision > PART_PRECISION) 1297 { 1298 precision -= PART_PRECISION; 1299 if (precision < PART_PRECISION 1300 && (num.high & (cpp_num_part) 1 << (precision - 1))) 1301 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision)); 1302 } 1303 else if (num.low & (cpp_num_part) 1 << (precision - 1)) 1304 { 1305 if (precision < PART_PRECISION) 1306 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision)); 1307 num.high = ~(cpp_num_part) 0; 1308 } 1309 } 1310 1311 return num; 1312} 1313 1314/* Returns the negative of NUM. */ 1315static cpp_num 1316num_negate (cpp_num num, size_t precision) 1317{ 1318 cpp_num copy; 1319 1320 copy = num; 1321 num.high = ~num.high; 1322 num.low = ~num.low; 1323 if (++num.low == 0) 1324 num.high++; 1325 num = num_trim (num, precision); 1326 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num)); 1327 1328 return num; 1329} 1330 1331/* Returns true if A >= B. */ 1332static bool 1333num_greater_eq (cpp_num pa, cpp_num pb, size_t precision) 1334{ 1335 bool unsignedp; 1336 1337 unsignedp = pa.unsignedp || pb.unsignedp; 1338 1339 if (!unsignedp) 1340 { 1341 /* Both numbers have signed type. If they are of different 1342 sign, the answer is the sign of A. */ 1343 unsignedp = num_positive (pa, precision); 1344 1345 if (unsignedp != num_positive (pb, precision)) 1346 return unsignedp; 1347 1348 /* Otherwise we can do an unsigned comparison. */ 1349 } 1350 1351 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low); 1352} 1353 1354/* Returns LHS OP RHS, where OP is a bit-wise operation. */ 1355static cpp_num 1356num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED, 1357 cpp_num lhs, cpp_num rhs, enum cpp_ttype op) 1358{ 1359 lhs.overflow = false; 1360 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp; 1361 1362 /* As excess precision is zeroed, there is no need to num_trim () as 1363 these operations cannot introduce a set bit there. */ 1364 if (op == CPP_AND) 1365 { 1366 lhs.low &= rhs.low; 1367 lhs.high &= rhs.high; 1368 } 1369 else if (op == CPP_OR) 1370 { 1371 lhs.low |= rhs.low; 1372 lhs.high |= rhs.high; 1373 } 1374 else 1375 { 1376 lhs.low ^= rhs.low; 1377 lhs.high ^= rhs.high; 1378 } 1379 1380 return lhs; 1381} 1382 1383/* Returns LHS OP RHS, where OP is an inequality. */ 1384static cpp_num 1385num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, 1386 enum cpp_ttype op) 1387{ 1388 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision)); 1389 1390 if (op == CPP_GREATER_EQ) 1391 lhs.low = gte; 1392 else if (op == CPP_LESS) 1393 lhs.low = !gte; 1394 else if (op == CPP_GREATER) 1395 lhs.low = gte && !num_eq (lhs, rhs); 1396 else /* CPP_LESS_EQ. */ 1397 lhs.low = !gte || num_eq (lhs, rhs); 1398 1399 lhs.high = 0; 1400 lhs.overflow = false; 1401 lhs.unsignedp = false; 1402 return lhs; 1403} 1404 1405/* Returns LHS OP RHS, where OP is == or !=. */ 1406static cpp_num 1407num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED, 1408 cpp_num lhs, cpp_num rhs, enum cpp_ttype op) 1409{ 1410 /* Work around a 3.0.4 bug; see PR 6950. */ 1411 bool eq = num_eq (lhs, rhs); 1412 if (op == CPP_NOT_EQ) 1413 eq = !eq; 1414 lhs.low = eq; 1415 lhs.high = 0; 1416 lhs.overflow = false; 1417 lhs.unsignedp = false; 1418 return lhs; 1419} 1420 1421/* Shift NUM, of width PRECISION, right by N bits. */ 1422static cpp_num 1423num_rshift (cpp_num num, size_t precision, size_t n) 1424{ 1425 cpp_num_part sign_mask; 1426 bool x = num_positive (num, precision); 1427 1428 if (num.unsignedp || x) 1429 sign_mask = 0; 1430 else 1431 sign_mask = ~(cpp_num_part) 0; 1432 1433 if (n >= precision) 1434 num.high = num.low = sign_mask; 1435 else 1436 { 1437 /* Sign-extend. */ 1438 if (precision < PART_PRECISION) 1439 num.high = sign_mask, num.low |= sign_mask << precision; 1440 else if (precision < 2 * PART_PRECISION) 1441 num.high |= sign_mask << (precision - PART_PRECISION); 1442 1443 if (n >= PART_PRECISION) 1444 { 1445 n -= PART_PRECISION; 1446 num.low = num.high; 1447 num.high = sign_mask; 1448 } 1449 1450 if (n) 1451 { 1452 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n)); 1453 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n)); 1454 } 1455 } 1456 1457 num = num_trim (num, precision); 1458 num.overflow = false; 1459 return num; 1460} 1461 1462/* Shift NUM, of width PRECISION, left by N bits. */ 1463static cpp_num 1464num_lshift (cpp_num num, size_t precision, size_t n) 1465{ 1466 if (n >= precision) 1467 { 1468 num.overflow = !num.unsignedp && !num_zerop (num); 1469 num.high = num.low = 0; 1470 } 1471 else 1472 { 1473 cpp_num orig, maybe_orig; 1474 size_t m = n; 1475 1476 orig = num; 1477 if (m >= PART_PRECISION) 1478 { 1479 m -= PART_PRECISION; 1480 num.high = num.low; 1481 num.low = 0; 1482 } 1483 if (m) 1484 { 1485 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m)); 1486 num.low <<= m; 1487 } 1488 num = num_trim (num, precision); 1489 1490 if (num.unsignedp) 1491 num.overflow = false; 1492 else 1493 { 1494 maybe_orig = num_rshift (num, precision, n); 1495 num.overflow = !num_eq (orig, maybe_orig); 1496 } 1497 } 1498 1499 return num; 1500} 1501 1502/* The four unary operators: +, -, ! and ~. */ 1503static cpp_num 1504num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op) 1505{ 1506 switch (op) 1507 { 1508 case CPP_UPLUS: 1509 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval) 1510 cpp_error (pfile, CPP_DL_WARNING, 1511 "traditional C rejects the unary plus operator"); 1512 num.overflow = false; 1513 break; 1514 1515 case CPP_UMINUS: 1516 num = num_negate (num, CPP_OPTION (pfile, precision)); 1517 break; 1518 1519 case CPP_COMPL: 1520 num.high = ~num.high; 1521 num.low = ~num.low; 1522 num = num_trim (num, CPP_OPTION (pfile, precision)); 1523 num.overflow = false; 1524 break; 1525 1526 default: /* case CPP_NOT: */ 1527 num.low = num_zerop (num); 1528 num.high = 0; 1529 num.overflow = false; 1530 num.unsignedp = false; 1531 break; 1532 } 1533 1534 return num; 1535} 1536 1537/* The various binary operators. */ 1538static cpp_num 1539num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op) 1540{ 1541 cpp_num result; 1542 size_t precision = CPP_OPTION (pfile, precision); 1543 size_t n; 1544 1545 switch (op) 1546 { 1547 /* Shifts. */ 1548 case CPP_LSHIFT: 1549 case CPP_RSHIFT: 1550 if (!rhs.unsignedp && !num_positive (rhs, precision)) 1551 { 1552 /* A negative shift is a positive shift the other way. */ 1553 if (op == CPP_LSHIFT) 1554 op = CPP_RSHIFT; 1555 else 1556 op = CPP_LSHIFT; 1557 rhs = num_negate (rhs, precision); 1558 } 1559 if (rhs.high) 1560 n = ~0; /* Maximal. */ 1561 else 1562 n = rhs.low; 1563 if (op == CPP_LSHIFT) 1564 lhs = num_lshift (lhs, precision, n); 1565 else 1566 lhs = num_rshift (lhs, precision, n); 1567 break; 1568 1569 /* Arithmetic. */ 1570 case CPP_MINUS: 1571 rhs = num_negate (rhs, precision); 1572 case CPP_PLUS: 1573 result.low = lhs.low + rhs.low; 1574 result.high = lhs.high + rhs.high; 1575 if (result.low < lhs.low) 1576 result.high++; 1577 result.unsignedp = lhs.unsignedp || rhs.unsignedp; 1578 result.overflow = false; 1579 1580 result = num_trim (result, precision); 1581 if (!result.unsignedp) 1582 { 1583 bool lhsp = num_positive (lhs, precision); 1584 result.overflow = (lhsp == num_positive (rhs, precision) 1585 && lhsp != num_positive (result, precision)); 1586 } 1587 return result; 1588 1589 /* Comma. */ 1590 default: /* case CPP_COMMA: */ 1591 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99) 1592 || !pfile->state.skip_eval)) 1593 cpp_error (pfile, CPP_DL_PEDWARN, 1594 "comma operator in operand of #if"); 1595 lhs = rhs; 1596 break; 1597 } 1598 1599 return lhs; 1600} 1601 1602/* Multiplies two unsigned cpp_num_parts to give a cpp_num. This 1603 cannot overflow. */ 1604static cpp_num 1605num_part_mul (cpp_num_part lhs, cpp_num_part rhs) 1606{ 1607 cpp_num result; 1608 cpp_num_part middle[2], temp; 1609 1610 result.low = LOW_PART (lhs) * LOW_PART (rhs); 1611 result.high = HIGH_PART (lhs) * HIGH_PART (rhs); 1612 1613 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs); 1614 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs); 1615 1616 temp = result.low; 1617 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2); 1618 if (result.low < temp) 1619 result.high++; 1620 1621 temp = result.low; 1622 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2); 1623 if (result.low < temp) 1624 result.high++; 1625 1626 result.high += HIGH_PART (middle[0]); 1627 result.high += HIGH_PART (middle[1]); 1628 result.unsignedp = true; 1629 result.overflow = false; 1630 1631 return result; 1632} 1633 1634/* Multiply two preprocessing numbers. */ 1635static cpp_num 1636num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs) 1637{ 1638 cpp_num result, temp; 1639 bool unsignedp = lhs.unsignedp || rhs.unsignedp; 1640 bool overflow, negate = false; 1641 size_t precision = CPP_OPTION (pfile, precision); 1642 1643 /* Prepare for unsigned multiplication. */ 1644 if (!unsignedp) 1645 { 1646 if (!num_positive (lhs, precision)) 1647 negate = !negate, lhs = num_negate (lhs, precision); 1648 if (!num_positive (rhs, precision)) 1649 negate = !negate, rhs = num_negate (rhs, precision); 1650 } 1651 1652 overflow = lhs.high && rhs.high; 1653 result = num_part_mul (lhs.low, rhs.low); 1654 1655 temp = num_part_mul (lhs.high, rhs.low); 1656 result.high += temp.low; 1657 if (temp.high) 1658 overflow = true; 1659 1660 temp = num_part_mul (lhs.low, rhs.high); 1661 result.high += temp.low; 1662 if (temp.high) 1663 overflow = true; 1664 1665 temp.low = result.low, temp.high = result.high; 1666 result = num_trim (result, precision); 1667 if (!num_eq (result, temp)) 1668 overflow = true; 1669 1670 if (negate) 1671 result = num_negate (result, precision); 1672 1673 if (unsignedp) 1674 result.overflow = false; 1675 else 1676 result.overflow = overflow || (num_positive (result, precision) ^ !negate 1677 && !num_zerop (result)); 1678 result.unsignedp = unsignedp; 1679 1680 return result; 1681} 1682 1683/* Divide two preprocessing numbers, LHS and RHS, returning the answer 1684 or the remainder depending upon OP. LOCATION is the source location 1685 of this operator (for diagnostics). */ 1686 1687static cpp_num 1688num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op, 1689 source_location location) 1690{ 1691 cpp_num result, sub; 1692 cpp_num_part mask; 1693 bool unsignedp = lhs.unsignedp || rhs.unsignedp; 1694 bool negate = false, lhs_neg = false; 1695 size_t i, precision = CPP_OPTION (pfile, precision); 1696 1697 /* Prepare for unsigned division. */ 1698 if (!unsignedp) 1699 { 1700 if (!num_positive (lhs, precision)) 1701 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision); 1702 if (!num_positive (rhs, precision)) 1703 negate = !negate, rhs = num_negate (rhs, precision); 1704 } 1705 1706 /* Find the high bit. */ 1707 if (rhs.high) 1708 { 1709 i = precision - 1; 1710 mask = (cpp_num_part) 1 << (i - PART_PRECISION); 1711 for (; ; i--, mask >>= 1) 1712 if (rhs.high & mask) 1713 break; 1714 } 1715 else if (rhs.low) 1716 { 1717 if (precision > PART_PRECISION) 1718 i = precision - PART_PRECISION - 1; 1719 else 1720 i = precision - 1; 1721 mask = (cpp_num_part) 1 << i; 1722 for (; ; i--, mask >>= 1) 1723 if (rhs.low & mask) 1724 break; 1725 } 1726 else 1727 { 1728 if (!pfile->state.skip_eval) 1729 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0, 1730 "division by zero in #if"); 1731 return lhs; 1732 } 1733 1734 /* First nonzero bit of RHS is bit I. Do naive division by 1735 shifting the RHS fully left, and subtracting from LHS if LHS is 1736 at least as big, and then repeating but with one less shift. 1737 This is not very efficient, but is easy to understand. */ 1738 1739 rhs.unsignedp = true; 1740 lhs.unsignedp = true; 1741 i = precision - i - 1; 1742 sub = num_lshift (rhs, precision, i); 1743 1744 result.high = result.low = 0; 1745 for (;;) 1746 { 1747 if (num_greater_eq (lhs, sub, precision)) 1748 { 1749 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS); 1750 if (i >= PART_PRECISION) 1751 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION); 1752 else 1753 result.low |= (cpp_num_part) 1 << i; 1754 } 1755 if (i-- == 0) 1756 break; 1757 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1)); 1758 sub.high >>= 1; 1759 } 1760 1761 /* We divide so that the remainder has the sign of the LHS. */ 1762 if (op == CPP_DIV) 1763 { 1764 result.unsignedp = unsignedp; 1765 result.overflow = false; 1766 if (!unsignedp) 1767 { 1768 if (negate) 1769 result = num_negate (result, precision); 1770 result.overflow = (num_positive (result, precision) ^ !negate 1771 && !num_zerop (result)); 1772 } 1773 1774 return result; 1775 } 1776 1777 /* CPP_MOD. */ 1778 lhs.unsignedp = unsignedp; 1779 lhs.overflow = false; 1780 if (lhs_neg) 1781 lhs = num_negate (lhs, precision); 1782 1783 return lhs; 1784} 1785