1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2004, 2008 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, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, 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. */
35 source_location loc
; /* The location of this value. */
39 /* Some simple utility routines on double integers. */
40 #define num_zerop(num) ((num.low | num.high) == 0)
41 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
42 static bool num_positive (cpp_num
, size_t);
43 static bool num_greater_eq (cpp_num
, cpp_num
, size_t);
44 static cpp_num
num_trim (cpp_num
, size_t);
45 static cpp_num
num_part_mul (cpp_num_part
, cpp_num_part
);
47 static cpp_num
num_unary_op (cpp_reader
*, cpp_num
, enum cpp_ttype
);
48 static cpp_num
num_binary_op (cpp_reader
*, cpp_num
, cpp_num
, enum cpp_ttype
);
49 static cpp_num
num_negate (cpp_num
, size_t);
50 static cpp_num
num_bitwise_op (cpp_reader
*, cpp_num
, cpp_num
, enum cpp_ttype
);
51 static cpp_num
num_inequality_op (cpp_reader
*, cpp_num
, cpp_num
,
53 static cpp_num
num_equality_op (cpp_reader
*, cpp_num
, cpp_num
,
55 static cpp_num
num_mul (cpp_reader
*, cpp_num
, cpp_num
);
56 static cpp_num
num_div_op (cpp_reader
*, cpp_num
, cpp_num
, enum cpp_ttype
);
57 static cpp_num
num_lshift (cpp_num
, size_t, size_t);
58 static cpp_num
num_rshift (cpp_num
, size_t, size_t);
60 static cpp_num
append_digit (cpp_num
, int, int, size_t);
61 static cpp_num
parse_defined (cpp_reader
*);
62 static cpp_num
eval_token (cpp_reader
*, const cpp_token
*);
63 static struct op
*reduce (cpp_reader
*, struct op
*, enum cpp_ttype
);
64 static unsigned int interpret_float_suffix (const uchar
*, size_t);
65 static unsigned int interpret_int_suffix (const uchar
*, size_t);
66 static void check_promotion (cpp_reader
*, const struct op
*);
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))
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; } \
80 /* Subroutine of cpp_classify_number. S points to a float suffix of
81 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
82 flag vector describing the suffix. */
84 interpret_float_suffix (const uchar
*s
, size_t len
)
87 size_t f
, d
, l
, w
, q
, i
;
90 f
= d
= l
= w
= q
= i
= 0;
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'))
96 bool uppercase
= (*s
== 'D');
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;
106 /* Additional two-character suffixes beginning with D are not
107 for decimal float constants. */
112 /* Recognize a fixed-point suffix. */
115 case 'k': case 'K': flags
= CPP_N_ACCUM
; break;
116 case 'r': case 'R': flags
= CPP_N_FRACT
; break;
120 /* Continue processing a fixed-point suffix. The suffix is case
121 insensitive except for ll or LL. Order is significant. */
128 if (*s
== 'u' || *s
== 'U')
130 flags
|= CPP_N_UNSIGNED
;
141 return flags
|= CPP_N_SMALL
;
145 return flags
|= CPP_N_MEDIUM
;
146 if (len
== 2 && s
[1] == 'l')
147 return flags
|= CPP_N_LARGE
;
151 return flags
|= CPP_N_MEDIUM
;
152 if (len
== 2 && s
[1] == 'L')
153 return flags
|= CPP_N_LARGE
;
158 /* Anything left at this point is invalid. */
162 /* In any remaining valid suffix, the case and order don't matter. */
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;
172 case 'j': case 'J': i
++; break;
177 if (f
+ d
+ l
+ w
+ q
> 1 || i
> 1)
180 return ((i
? CPP_N_IMAGINARY
: 0)
185 q
? CPP_N_MD_Q
: CPP_N_DEFAULT
));
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. */
192 interpret_int_suffix (const uchar
*s
, size_t len
)
201 case 'u': case 'U': u
++; break;
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])
213 if (l
> 2 || u
> 1 || i
> 1)
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
));
222 /* Categorize numeric constants according to their field (integer,
223 floating point, or invalid), radix (decimal, octal, hexadecimal),
224 and type suffixes. */
226 cpp_classify_number (cpp_reader
*pfile
, const cpp_token
*token
)
228 const uchar
*str
= token
->val
.str
.text
;
230 unsigned int max_digit
, result
, radix
;
231 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
233 /* If the lexer has done its job, length one can only be a single
234 digit. Fast-path this very common case. */
235 if (token
->val
.str
.len
== 1)
236 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
238 limit
= str
+ token
->val
.str
.len
;
239 float_flag
= NOT_FLOAT
;
243 /* First, interpret the radix. */
249 /* Require at least one hex digit to classify it as hex. */
250 if ((*str
== 'x' || *str
== 'X')
251 && (str
[1] == '.' || ISXDIGIT (str
[1])))
256 else if ((*str
== 'b' || *str
== 'B') && (str
[1] == '0' || str
[1] == '1'))
263 /* Now scan for a well-formed integer or float. */
266 unsigned int c
= *str
++;
268 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
276 if (float_flag
== NOT_FLOAT
)
277 float_flag
= AFTER_POINT
;
279 SYNTAX_ERROR ("too many decimal points in number");
281 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
282 || (radix
== 16 && (c
== 'p' || c
== 'P')))
284 float_flag
= AFTER_EXPON
;
289 /* Start of suffix. */
295 /* The suffix may be for decimal fixed-point constants without exponent. */
296 if (radix
!= 16 && float_flag
== NOT_FLOAT
)
298 result
= interpret_float_suffix (str
, limit
- str
);
299 if ((result
& CPP_N_FRACT
) || (result
& CPP_N_ACCUM
))
301 result
|= CPP_N_FLOATING
;
302 /* We need to restore the radix to 10, if the radix is 8. */
306 if (CPP_PEDANTIC (pfile
))
307 cpp_error (pfile
, CPP_DL_PEDWARN
,
308 "fixed-point constants are a GCC extension");
315 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
318 if (max_digit
>= radix
)
321 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit
);
323 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit
);
326 if (float_flag
!= NOT_FLOAT
)
330 cpp_error (pfile
, CPP_DL_ERROR
,
331 "invalid prefix \"0b\" for floating constant");
332 return CPP_N_INVALID
;
335 if (radix
== 16 && CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, c99
))
336 cpp_error (pfile
, CPP_DL_PEDWARN
,
337 "use of C99 hexadecimal floating constant");
339 if (float_flag
== AFTER_EXPON
)
341 if (*str
== '+' || *str
== '-')
344 /* Exponent is decimal, even if string is a hex float. */
346 SYNTAX_ERROR ("exponent has no digits");
350 while (ISDIGIT (*str
));
352 else if (radix
== 16)
353 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
355 result
= interpret_float_suffix (str
, limit
- str
);
358 cpp_error (pfile
, CPP_DL_ERROR
,
359 "invalid suffix \"%.*s\" on floating constant",
360 (int) (limit
- str
), str
);
361 return CPP_N_INVALID
;
364 /* Traditional C didn't accept any floating suffixes. */
366 && CPP_WTRADITIONAL (pfile
)
367 && ! cpp_sys_macro_p (pfile
))
368 cpp_error (pfile
, CPP_DL_WARNING
,
369 "traditional C rejects the \"%.*s\" suffix",
370 (int) (limit
- str
), str
);
372 /* A suffix for double is a GCC extension via decimal float support.
373 If the suffix also specifies an imaginary value we'll catch that
375 if ((result
== CPP_N_MEDIUM
) && CPP_PEDANTIC (pfile
))
376 cpp_error (pfile
, CPP_DL_PEDWARN
,
377 "suffix for double constant is a GCC extension");
379 /* Radix must be 10 for decimal floats. */
380 if ((result
& CPP_N_DFLOAT
) && radix
!= 10)
382 cpp_error (pfile
, CPP_DL_ERROR
,
383 "invalid suffix \"%.*s\" with hexadecimal floating constant",
384 (int) (limit
- str
), str
);
385 return CPP_N_INVALID
;
388 if ((result
& (CPP_N_FRACT
| CPP_N_ACCUM
)) && CPP_PEDANTIC (pfile
))
389 cpp_error (pfile
, CPP_DL_PEDWARN
,
390 "fixed-point constants are a GCC extension");
392 if ((result
& CPP_N_DFLOAT
) && CPP_PEDANTIC (pfile
))
393 cpp_error (pfile
, CPP_DL_PEDWARN
,
394 "decimal float constants are a GCC extension");
396 result
|= CPP_N_FLOATING
;
400 result
= interpret_int_suffix (str
, limit
- str
);
403 cpp_error (pfile
, CPP_DL_ERROR
,
404 "invalid suffix \"%.*s\" on integer constant",
405 (int) (limit
- str
), str
);
406 return CPP_N_INVALID
;
409 /* Traditional C only accepted the 'L' suffix.
410 Suppress warning about 'LL' with -Wno-long-long. */
411 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
413 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
414 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
;
416 if (u_or_i
|| (large
&& CPP_OPTION (pfile
, warn_long_long
)))
417 cpp_error (pfile
, CPP_DL_WARNING
,
418 "traditional C rejects the \"%.*s\" suffix",
419 (int) (limit
- str
), str
);
422 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
423 && ! CPP_OPTION (pfile
, c99
)
424 && CPP_OPTION (pfile
, warn_long_long
))
425 cpp_error (pfile
, CPP_DL_PEDWARN
,
426 "use of C99 long long integer constant");
428 result
|= CPP_N_INTEGER
;
432 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
433 cpp_error (pfile
, CPP_DL_PEDWARN
,
434 "imaginary constants are a GCC extension");
435 if (radix
== 2 && CPP_PEDANTIC (pfile
))
436 cpp_error (pfile
, CPP_DL_PEDWARN
,
437 "binary constants are a GCC extension");
440 result
|= CPP_N_DECIMAL
;
441 else if (radix
== 16)
444 result
|= CPP_N_BINARY
;
446 result
|= CPP_N_OCTAL
;
451 return CPP_N_INVALID
;
454 /* cpp_interpret_integer converts an integer constant into a cpp_num,
455 of precision options->precision.
457 We do not provide any interface for decimal->float conversion,
458 because the preprocessor doesn't need it and we don't want to
459 drag in GCC's floating point emulator. */
461 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
464 const uchar
*p
, *end
;
469 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
470 result
.overflow
= false;
472 p
= token
->val
.str
.text
;
473 end
= p
+ token
->val
.str
.len
;
475 /* Common case of a single digit. */
476 if (token
->val
.str
.len
== 1)
477 result
.low
= p
[0] - '0';
481 size_t precision
= CPP_OPTION (pfile
, precision
);
482 unsigned int base
= 10, c
= 0;
483 bool overflow
= false;
485 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
490 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
495 else if ((type
& CPP_N_RADIX
) == CPP_N_BINARY
)
501 /* We can add a digit to numbers strictly less than this without
502 needing the precision and slowness of double integers. */
503 max
= ~(cpp_num_part
) 0;
504 if (precision
< PART_PRECISION
)
505 max
>>= PART_PRECISION
- precision
;
506 max
= (max
- base
+ 1) / base
+ 1;
512 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
517 /* Strict inequality for when max is set to zero. */
518 if (result
.low
< max
)
519 result
.low
= result
.low
* base
+ c
;
522 result
= append_digit (result
, c
, base
, precision
);
523 overflow
|= result
.overflow
;
529 cpp_error (pfile
, CPP_DL_PEDWARN
,
530 "integer constant is too large for its type");
531 /* If too big to be signed, consider it unsigned. Only warn for
532 decimal numbers. Traditional numbers were always signed (but
533 we still honor an explicit U suffix); but we only have
534 traditional semantics in directives. */
535 else if (!result
.unsignedp
536 && !(CPP_OPTION (pfile
, traditional
)
537 && pfile
->state
.in_directive
)
538 && !num_positive (result
, precision
))
541 cpp_error (pfile
, CPP_DL_WARNING
,
542 "integer constant is so large that it is unsigned");
543 result
.unsignedp
= true;
550 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
552 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
557 cpp_num_part add_high
, add_low
;
559 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
560 need to worry about add_high overflowing. */
574 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
575 result
.high
= num
.high
<< shift
;
576 result
.low
= num
.low
<< shift
;
577 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
578 result
.unsignedp
= num
.unsignedp
;
582 add_low
= num
.low
<< 1;
583 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
586 add_high
= add_low
= 0;
588 if (add_low
+ digit
< add_low
)
592 if (result
.low
+ add_low
< result
.low
)
594 if (result
.high
+ add_high
< result
.high
)
597 result
.low
+= add_low
;
598 result
.high
+= add_high
;
599 result
.overflow
= overflow
;
601 /* The above code catches overflow of a cpp_num type. This catches
602 overflow of the (possibly shorter) target precision. */
603 num
.low
= result
.low
;
604 num
.high
= result
.high
;
605 result
= num_trim (result
, precision
);
606 if (!num_eq (result
, num
))
607 result
.overflow
= true;
612 /* Handle meeting "defined" in a preprocessor expression. */
614 parse_defined (cpp_reader
*pfile
)
618 cpp_hashnode
*node
= 0;
619 const cpp_token
*token
;
620 cpp_context
*initial_context
= pfile
->context
;
622 /* Don't expand macros. */
623 pfile
->state
.prevent_expansion
++;
625 token
= cpp_get_token (pfile
);
626 if (token
->type
== CPP_OPEN_PAREN
)
629 token
= cpp_get_token (pfile
);
632 if (token
->type
== CPP_NAME
)
634 node
= token
->val
.node
;
635 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
637 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' after \"defined\"");
643 cpp_error (pfile
, CPP_DL_ERROR
,
644 "operator \"defined\" requires an identifier");
645 if (token
->flags
& NAMED_OP
)
650 op
.type
= token
->type
;
651 cpp_error (pfile
, CPP_DL_ERROR
,
652 "(\"%s\" is an alternative token for \"%s\" in C++)",
653 cpp_token_as_text (pfile
, token
),
654 cpp_token_as_text (pfile
, &op
));
660 if (pfile
->context
!= initial_context
&& CPP_PEDANTIC (pfile
))
661 cpp_error (pfile
, CPP_DL_WARNING
,
662 "this use of \"defined\" may not be portable");
664 _cpp_mark_macro_used (node
);
665 if (!(node
->flags
& NODE_USED
))
667 node
->flags
|= NODE_USED
;
668 if (node
->type
== NT_MACRO
)
670 if (pfile
->cb
.used_define
)
671 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
675 if (pfile
->cb
.used_undef
)
676 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
680 /* A possible controlling macro of the form #if !defined ().
681 _cpp_parse_expr checks there was no other junk on the line. */
682 pfile
->mi_ind_cmacro
= node
;
685 pfile
->state
.prevent_expansion
--;
687 result
.unsignedp
= false;
689 result
.overflow
= false;
690 result
.low
= node
&& node
->type
== NT_MACRO
;
694 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
695 number or character constant, or the result of the "defined" or "#"
698 eval_token (cpp_reader
*pfile
, const cpp_token
*token
)
704 result
.unsignedp
= false;
705 result
.overflow
= false;
710 temp
= cpp_classify_number (pfile
, token
);
711 switch (temp
& CPP_N_CATEGORY
)
714 cpp_error (pfile
, CPP_DL_ERROR
,
715 "floating constant in preprocessor expression");
718 if (!(temp
& CPP_N_IMAGINARY
))
719 return cpp_interpret_integer (pfile
, token
, temp
);
720 cpp_error (pfile
, CPP_DL_ERROR
,
721 "imaginary number in preprocessor expression");
725 /* Error already issued. */
728 result
.high
= result
.low
= 0;
736 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
741 /* Sign-extend the result if necessary. */
742 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
744 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
745 result
.low
|= ~(~(cpp_num_part
) 0
746 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
747 result
.high
= ~(cpp_num_part
) 0;
748 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
754 if (token
->val
.node
== pfile
->spec_nodes
.n_defined
)
755 return parse_defined (pfile
);
756 else if (CPP_OPTION (pfile
, cplusplus
)
757 && (token
->val
.node
== pfile
->spec_nodes
.n_true
758 || token
->val
.node
== pfile
->spec_nodes
.n_false
))
761 result
.low
= (token
->val
.node
== pfile
->spec_nodes
.n_true
);
767 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
768 cpp_error (pfile
, CPP_DL_WARNING
, "\"%s\" is not defined",
769 NODE_NAME (token
->val
.node
));
774 if (!pfile
->state
.skipping
)
776 /* A pedantic warning takes precedence over a deprecated
778 if (CPP_PEDANTIC (pfile
))
779 cpp_error (pfile
, CPP_DL_PEDWARN
,
780 "assertions are a GCC extension");
781 else if (CPP_OPTION (pfile
, warn_deprecated
))
782 cpp_error (pfile
, CPP_DL_WARNING
,
783 "assertions are a deprecated extension");
785 _cpp_test_assertion (pfile
, &temp
);
794 result
.unsignedp
= !!unsignedp
;
798 /* Operator precedence and flags table.
800 After an operator is returned from the lexer, if it has priority less
801 than the operator on the top of the stack, we reduce the stack by one
802 operator and repeat the test. Since equal priorities do not reduce,
803 this is naturally right-associative.
805 We handle left-associative operators by decrementing the priority of
806 just-lexed operators by one, but retaining the priority of operators
807 already on the stack.
809 The remaining cases are '(' and ')'. We handle '(' by skipping the
810 reduction phase completely. ')' is given lower priority than
811 everything else, including '(', effectively forcing a reduction of the
812 parenthesized expression. If there is a matching '(', the routine
813 reduce() exits immediately. If the normal exit route sees a ')', then
814 there cannot have been a matching '(' and an error message is output.
816 The parser assumes all shifted operators require a left operand unless
817 the flag NO_L_OPERAND is set. These semantics are automatic; any
818 extra semantics need to be handled with operator-specific code. */
820 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
821 operand changes because of integer promotions. */
822 #define NO_L_OPERAND (1 << 0)
823 #define LEFT_ASSOC (1 << 1)
824 #define CHECK_PROMOTION (1 << 2)
826 /* Operator to priority map. Must be in the same order as the first
827 N entries of enum cpp_ttype. */
828 static const struct cpp_operator
834 /* EQ */ {0, 0}, /* Shouldn't happen. */
835 /* NOT */ {16, NO_L_OPERAND
},
836 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
837 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
838 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
839 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
840 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
841 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
842 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
843 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
844 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
845 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
846 /* RSHIFT */ {13, LEFT_ASSOC
},
847 /* LSHIFT */ {13, LEFT_ASSOC
},
849 /* COMPL */ {16, NO_L_OPERAND
},
850 /* AND_AND */ {6, LEFT_ASSOC
},
851 /* OR_OR */ {5, LEFT_ASSOC
},
852 /* Note that QUERY, COLON, and COMMA must have the same precedence.
853 However, there are some special cases for these in reduce(). */
855 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
856 /* COMMA */ {4, LEFT_ASSOC
},
857 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
858 /* CLOSE_PAREN */ {0, 0},
860 /* EQ_EQ */ {11, LEFT_ASSOC
},
861 /* NOT_EQ */ {11, LEFT_ASSOC
},
862 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
863 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
864 /* UPLUS */ {16, NO_L_OPERAND
},
865 /* UMINUS */ {16, NO_L_OPERAND
}
868 /* Parse and evaluate a C expression, reading from PFILE.
869 Returns the truth value of the expression.
871 The implementation is an operator precedence parser, i.e. a
872 bottom-up parser, using a stack for not-yet-reduced tokens.
874 The stack base is op_stack, and the current stack pointer is 'top'.
875 There is a stack element for each operator (only), and the most
876 recently pushed operator is 'top->op'. An operand (value) is
877 stored in the 'value' field of the stack element of the operator
880 _cpp_parse_expr (cpp_reader
*pfile
, bool is_if
)
882 struct op
*top
= pfile
->op_stack
;
883 unsigned int lex_count
;
884 bool saw_leading_not
, want_value
= true;
886 pfile
->state
.skip_eval
= 0;
888 /* Set up detection of #if ! defined(). */
889 pfile
->mi_ind_cmacro
= 0;
890 saw_leading_not
= false;
893 /* Lowest priority operator prevents further reductions. */
901 op
.token
= cpp_get_token (pfile
);
902 op
.op
= op
.token
->type
;
903 op
.loc
= op
.token
->src_loc
;
907 /* These tokens convert into values. */
916 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
917 cpp_token_as_text (pfile
, op
.token
));
919 top
->value
= eval_token (pfile
, op
.token
);
923 saw_leading_not
= lex_count
== 1;
935 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
936 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
937 cpp_token_as_text (pfile
, op
.token
));
941 /* Check we have a value or operator as appropriate. */
942 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
945 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
946 cpp_token_as_text (pfile
, op
.token
));
950 /* We want a number (or expression) and haven't got one.
951 Try to emit a specific diagnostic. */
952 if (op
.op
== CPP_CLOSE_PAREN
&& top
->op
== CPP_OPEN_PAREN
)
953 SYNTAX_ERROR ("missing expression between '(' and ')'");
955 if (op
.op
== CPP_EOF
&& top
->op
== CPP_EOF
)
956 SYNTAX_ERROR2 ("%s with no expression", is_if
? "#if" : "#elif");
958 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
959 SYNTAX_ERROR2 ("operator '%s' has no right operand",
960 cpp_token_as_text (pfile
, top
->token
));
961 else if (op
.op
== CPP_CLOSE_PAREN
|| op
.op
== CPP_EOF
)
962 /* Complain about missing paren during reduction. */;
964 SYNTAX_ERROR2 ("operator '%s' has no left operand",
965 cpp_token_as_text (pfile
, op
.token
));
968 top
= reduce (pfile
, top
, op
.op
);
972 if (op
.op
== CPP_EOF
)
977 case CPP_CLOSE_PAREN
:
980 if (!num_zerop (top
->value
))
981 pfile
->state
.skip_eval
++;
985 if (num_zerop (top
->value
))
986 pfile
->state
.skip_eval
++;
989 if (top
->op
!= CPP_QUERY
)
990 SYNTAX_ERROR (" ':' without preceding '?'");
991 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
992 pfile
->state
.skip_eval
++;
994 pfile
->state
.skip_eval
--;
1001 /* Check for and handle stack overflow. */
1002 if (++top
== pfile
->op_limit
)
1003 top
= _cpp_expand_op_stack (pfile
);
1006 top
->token
= op
.token
;
1007 top
->loc
= op
.token
->src_loc
;
1010 /* The controlling macro expression is only valid if we called lex 3
1011 times: <!> <defined expression> and <EOF>. push_conditional ()
1012 checks that we are at top-of-file. */
1013 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
1014 pfile
->mi_ind_cmacro
= 0;
1016 if (top
!= pfile
->op_stack
)
1018 cpp_error (pfile
, CPP_DL_ICE
, "unbalanced stack in %s",
1019 is_if
? "#if" : "#elif");
1021 return false; /* Return false on syntax error. */
1024 return !num_zerop (top
->value
);
1027 /* Reduce the operator / value stack if possible, in preparation for
1028 pushing operator OP. Returns NULL on error, otherwise the top of
1031 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
1035 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
1038 cpp_error (pfile
, CPP_DL_ICE
, "impossible operator '%u'", top
->op
);
1042 if (op
== CPP_OPEN_PAREN
)
1045 /* Decrement the priority of left-associative operators to force a
1046 reduction with operators of otherwise equal priority. */
1047 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
1048 while (prio
< optab
[top
->op
].prio
)
1050 if (CPP_OPTION (pfile
, warn_num_sign_change
)
1051 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
1052 check_promotion (pfile
, top
);
1060 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
1061 top
[-1].loc
= top
->loc
;
1069 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
1070 top
->value
, top
->op
);
1071 top
[-1].loc
= top
->loc
;
1076 case CPP_GREATER_EQ
:
1079 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1080 top
[-1].loc
= top
->loc
;
1086 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1087 top
[-1].loc
= top
->loc
;
1094 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1095 top
[-1].loc
= top
->loc
;
1099 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
1100 top
[-1].loc
= top
->loc
;
1105 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
1106 top
->value
, top
->op
);
1107 top
[-1].loc
= top
->loc
;
1112 if (!num_zerop (top
->value
))
1113 pfile
->state
.skip_eval
--;
1114 top
->value
.low
= (!num_zerop (top
->value
)
1115 || !num_zerop (top
[1].value
));
1116 top
->value
.high
= 0;
1117 top
->value
.unsignedp
= false;
1118 top
->value
.overflow
= false;
1119 top
->loc
= top
[1].loc
;
1124 if (num_zerop (top
->value
))
1125 pfile
->state
.skip_eval
--;
1126 top
->value
.low
= (!num_zerop (top
->value
)
1127 && !num_zerop (top
[1].value
));
1128 top
->value
.high
= 0;
1129 top
->value
.unsignedp
= false;
1130 top
->value
.overflow
= false;
1131 top
->loc
= top
[1].loc
;
1134 case CPP_OPEN_PAREN
:
1135 if (op
!= CPP_CLOSE_PAREN
)
1137 cpp_error_with_line (pfile
, CPP_DL_ERROR
,
1138 top
->token
->src_loc
,
1139 0, "missing ')' in expression");
1143 top
->value
= top
[1].value
;
1144 top
->loc
= top
[1].loc
;
1149 if (!num_zerop (top
->value
))
1151 pfile
->state
.skip_eval
--;
1152 top
->value
= top
[1].value
;
1153 top
->loc
= top
[1].loc
;
1157 top
->value
= top
[2].value
;
1158 top
->loc
= top
[2].loc
;
1160 top
->value
.unsignedp
= (top
[1].value
.unsignedp
1161 || top
[2].value
.unsignedp
);
1165 /* COMMA and COLON should not reduce a QUERY operator. */
1166 if (op
== CPP_COMMA
|| op
== CPP_COLON
)
1168 cpp_error (pfile
, CPP_DL_ERROR
, "'?' without following ':'");
1176 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
1177 cpp_error (pfile
, CPP_DL_PEDWARN
,
1178 "integer overflow in preprocessor expression");
1181 if (op
== CPP_CLOSE_PAREN
)
1183 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' in expression");
1190 /* Returns the position of the old top of stack after expansion. */
1192 _cpp_expand_op_stack (cpp_reader
*pfile
)
1194 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
1195 size_t new_size
= old_size
* 2 + 20;
1197 pfile
->op_stack
= XRESIZEVEC (struct op
, pfile
->op_stack
, new_size
);
1198 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
1200 return pfile
->op_stack
+ old_size
;
1203 /* Emits a warning if the effective sign of either operand of OP
1204 changes because of integer promotions. */
1206 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
1208 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1211 if (op
->value
.unsignedp
)
1213 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1214 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
[-1].loc
, 0,
1215 "the left operand of \"%s\" changes sign when promoted",
1216 cpp_token_as_text (pfile
, op
->token
));
1218 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1219 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
->loc
, 0,
1220 "the right operand of \"%s\" changes sign when promoted",
1221 cpp_token_as_text (pfile
, op
->token
));
1224 /* Clears the unused high order bits of the number pointed to by PNUM. */
1226 num_trim (cpp_num num
, size_t precision
)
1228 if (precision
> PART_PRECISION
)
1230 precision
-= PART_PRECISION
;
1231 if (precision
< PART_PRECISION
)
1232 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1236 if (precision
< PART_PRECISION
)
1237 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1244 /* True iff A (presumed signed) >= 0. */
1246 num_positive (cpp_num num
, size_t precision
)
1248 if (precision
> PART_PRECISION
)
1250 precision
-= PART_PRECISION
;
1251 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1254 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1257 /* Sign extend a number, with PRECISION significant bits and all
1258 others assumed clear, to fill out a cpp_num structure. */
1260 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1264 if (precision
> PART_PRECISION
)
1266 precision
-= PART_PRECISION
;
1267 if (precision
< PART_PRECISION
1268 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1269 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1271 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1273 if (precision
< PART_PRECISION
)
1274 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1275 num
.high
= ~(cpp_num_part
) 0;
1282 /* Returns the negative of NUM. */
1284 num_negate (cpp_num num
, size_t precision
)
1289 num
.high
= ~num
.high
;
1293 num
= num_trim (num
, precision
);
1294 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1299 /* Returns true if A >= B. */
1301 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1305 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1309 /* Both numbers have signed type. If they are of different
1310 sign, the answer is the sign of A. */
1311 unsignedp
= num_positive (pa
, precision
);
1313 if (unsignedp
!= num_positive (pb
, precision
))
1316 /* Otherwise we can do an unsigned comparison. */
1319 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1322 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1324 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1325 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1327 lhs
.overflow
= false;
1328 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1330 /* As excess precision is zeroed, there is no need to num_trim () as
1331 these operations cannot introduce a set bit there. */
1335 lhs
.high
&= rhs
.high
;
1337 else if (op
== CPP_OR
)
1340 lhs
.high
|= rhs
.high
;
1345 lhs
.high
^= rhs
.high
;
1351 /* Returns LHS OP RHS, where OP is an inequality. */
1353 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1356 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1358 if (op
== CPP_GREATER_EQ
)
1360 else if (op
== CPP_LESS
)
1362 else if (op
== CPP_GREATER
)
1363 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1364 else /* CPP_LESS_EQ. */
1365 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1368 lhs
.overflow
= false;
1369 lhs
.unsignedp
= false;
1373 /* Returns LHS OP RHS, where OP is == or !=. */
1375 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1376 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1378 /* Work around a 3.0.4 bug; see PR 6950. */
1379 bool eq
= num_eq (lhs
, rhs
);
1380 if (op
== CPP_NOT_EQ
)
1384 lhs
.overflow
= false;
1385 lhs
.unsignedp
= false;
1389 /* Shift NUM, of width PRECISION, right by N bits. */
1391 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1393 cpp_num_part sign_mask
;
1394 bool x
= num_positive (num
, precision
);
1396 if (num
.unsignedp
|| x
)
1399 sign_mask
= ~(cpp_num_part
) 0;
1402 num
.high
= num
.low
= sign_mask
;
1406 if (precision
< PART_PRECISION
)
1407 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1408 else if (precision
< 2 * PART_PRECISION
)
1409 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1411 if (n
>= PART_PRECISION
)
1413 n
-= PART_PRECISION
;
1415 num
.high
= sign_mask
;
1420 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1421 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1425 num
= num_trim (num
, precision
);
1426 num
.overflow
= false;
1430 /* Shift NUM, of width PRECISION, left by N bits. */
1432 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1436 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1437 num
.high
= num
.low
= 0;
1441 cpp_num orig
, maybe_orig
;
1445 if (m
>= PART_PRECISION
)
1447 m
-= PART_PRECISION
;
1453 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1456 num
= num_trim (num
, precision
);
1459 num
.overflow
= false;
1462 maybe_orig
= num_rshift (num
, precision
, n
);
1463 num
.overflow
= !num_eq (orig
, maybe_orig
);
1470 /* The four unary operators: +, -, ! and ~. */
1472 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1477 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1478 cpp_error (pfile
, CPP_DL_WARNING
,
1479 "traditional C rejects the unary plus operator");
1480 num
.overflow
= false;
1484 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1488 num
.high
= ~num
.high
;
1490 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1491 num
.overflow
= false;
1494 default: /* case CPP_NOT: */
1495 num
.low
= num_zerop (num
);
1497 num
.overflow
= false;
1498 num
.unsignedp
= false;
1505 /* The various binary operators. */
1507 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1510 size_t precision
= CPP_OPTION (pfile
, precision
);
1518 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1520 /* A negative shift is a positive shift the other way. */
1521 if (op
== CPP_LSHIFT
)
1525 rhs
= num_negate (rhs
, precision
);
1528 n
= ~0; /* Maximal. */
1531 if (op
== CPP_LSHIFT
)
1532 lhs
= num_lshift (lhs
, precision
, n
);
1534 lhs
= num_rshift (lhs
, precision
, n
);
1539 rhs
= num_negate (rhs
, precision
);
1541 result
.low
= lhs
.low
+ rhs
.low
;
1542 result
.high
= lhs
.high
+ rhs
.high
;
1543 if (result
.low
< lhs
.low
)
1545 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1546 result
.overflow
= false;
1548 result
= num_trim (result
, precision
);
1549 if (!result
.unsignedp
)
1551 bool lhsp
= num_positive (lhs
, precision
);
1552 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1553 && lhsp
!= num_positive (result
, precision
));
1558 default: /* case CPP_COMMA: */
1559 if (CPP_PEDANTIC (pfile
) && (!CPP_OPTION (pfile
, c99
)
1560 || !pfile
->state
.skip_eval
))
1561 cpp_error (pfile
, CPP_DL_PEDWARN
,
1562 "comma operator in operand of #if");
1570 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1573 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
1576 cpp_num_part middle
[2], temp
;
1578 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1579 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1581 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1582 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1585 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1586 if (result
.low
< temp
)
1590 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1591 if (result
.low
< temp
)
1594 result
.high
+= HIGH_PART (middle
[0]);
1595 result
.high
+= HIGH_PART (middle
[1]);
1596 result
.unsignedp
= true;
1597 result
.overflow
= false;
1602 /* Multiply two preprocessing numbers. */
1604 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
1606 cpp_num result
, temp
;
1607 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1608 bool overflow
, negate
= false;
1609 size_t precision
= CPP_OPTION (pfile
, precision
);
1611 /* Prepare for unsigned multiplication. */
1614 if (!num_positive (lhs
, precision
))
1615 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1616 if (!num_positive (rhs
, precision
))
1617 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1620 overflow
= lhs
.high
&& rhs
.high
;
1621 result
= num_part_mul (lhs
.low
, rhs
.low
);
1623 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1624 result
.high
+= temp
.low
;
1628 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1629 result
.high
+= temp
.low
;
1633 temp
.low
= result
.low
, temp
.high
= result
.high
;
1634 result
= num_trim (result
, precision
);
1635 if (!num_eq (result
, temp
))
1639 result
= num_negate (result
, precision
);
1642 result
.overflow
= false;
1644 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1645 && !num_zerop (result
));
1646 result
.unsignedp
= unsignedp
;
1651 /* Divide two preprocessing numbers, returning the answer or the
1652 remainder depending upon OP. */
1654 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1656 cpp_num result
, sub
;
1658 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1659 bool negate
= false, lhs_neg
= false;
1660 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1662 /* Prepare for unsigned division. */
1665 if (!num_positive (lhs
, precision
))
1666 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
1667 if (!num_positive (rhs
, precision
))
1668 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1671 /* Find the high bit. */
1675 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1676 for (; ; i
--, mask
>>= 1)
1677 if (rhs
.high
& mask
)
1682 if (precision
> PART_PRECISION
)
1683 i
= precision
- PART_PRECISION
- 1;
1686 mask
= (cpp_num_part
) 1 << i
;
1687 for (; ; i
--, mask
>>= 1)
1693 if (!pfile
->state
.skip_eval
)
1694 cpp_error (pfile
, CPP_DL_ERROR
, "division by zero in #if");
1698 /* First nonzero bit of RHS is bit I. Do naive division by
1699 shifting the RHS fully left, and subtracting from LHS if LHS is
1700 at least as big, and then repeating but with one less shift.
1701 This is not very efficient, but is easy to understand. */
1703 rhs
.unsignedp
= true;
1704 lhs
.unsignedp
= true;
1705 i
= precision
- i
- 1;
1706 sub
= num_lshift (rhs
, precision
, i
);
1708 result
.high
= result
.low
= 0;
1711 if (num_greater_eq (lhs
, sub
, precision
))
1713 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
1714 if (i
>= PART_PRECISION
)
1715 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1717 result
.low
|= (cpp_num_part
) 1 << i
;
1721 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
1725 /* We divide so that the remainder has the sign of the LHS. */
1728 result
.unsignedp
= unsignedp
;
1729 result
.overflow
= false;
1733 result
= num_negate (result
, precision
);
1734 result
.overflow
= (num_positive (result
, precision
) ^ !negate
1735 && !num_zerop (result
));
1742 lhs
.unsignedp
= unsignedp
;
1743 lhs
.overflow
= false;
1745 lhs
= num_negate (lhs
, precision
);