1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002 Free Software Foundation.
4 Contributed by Per Bothner, 1994.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 typedef struct cpp_num cpp_num
;
28 #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
29 #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
30 #define LOW_PART(num_part) (num_part & HALF_MASK)
31 #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
33 /* A preprocessing number. Code assumes that any unused high bits of
34 the double integer are set to zero. */
39 bool unsignedp
; /* True if value should be treated as unsigned. */
40 bool overflow
; /* True if the most recent calculation overflowed. */
45 cpp_num value
; /* The value logically "right" of op. */
49 /* Some simple utility routines on double integers. */
50 #define num_zerop(num) ((num.low | num.high) == 0)
51 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
52 static bool num_positive
PARAMS ((cpp_num
, size_t));
53 static bool num_greater_eq
PARAMS ((cpp_num
, cpp_num
, size_t));
54 static cpp_num num_trim
PARAMS ((cpp_num
, size_t));
55 static cpp_num num_part_mul
PARAMS ((cpp_num_part
, cpp_num_part
));
57 static cpp_num num_unary_op
PARAMS ((cpp_reader
*, cpp_num
, enum cpp_ttype
));
58 static cpp_num num_binary_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
60 static cpp_num num_negate
PARAMS ((cpp_num
, size_t));
61 static cpp_num num_bitwise_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
63 static cpp_num num_inequality_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
65 static cpp_num num_equality_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
67 static cpp_num num_mul
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
69 static cpp_num num_div_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
71 static cpp_num num_lshift
PARAMS ((cpp_num
, size_t, size_t));
72 static cpp_num num_rshift
PARAMS ((cpp_num
, size_t, size_t));
74 static cpp_num append_digit
PARAMS ((cpp_num
, int, int, size_t));
75 static cpp_num interpret_number
PARAMS ((cpp_reader
*, const cpp_token
*));
76 static cpp_num parse_defined
PARAMS ((cpp_reader
*));
77 static cpp_num eval_token
PARAMS ((cpp_reader
*, const cpp_token
*));
78 static struct op
*reduce
PARAMS ((cpp_reader
*, struct op
*, enum cpp_ttype
));
80 /* Token type abuse. There is no "error" token, but we can't get
81 comments in #if, so we can abuse that token type. Similarly,
82 create unary plus and minus operators. */
83 #define CPP_ERROR CPP_COMMENT
84 #define CPP_UPLUS (CPP_LAST_CPP_OP + 1)
85 #define CPP_UMINUS (CPP_LAST_CPP_OP + 2)
87 /* With -O2, gcc appears to produce nice code, moving the error
88 message load and subsequent jump completely out of the main path. */
89 #define SYNTAX_ERROR(msgid) \
90 do { cpp_error (pfile, DL_ERROR, msgid); goto syntax_error; } while(0)
91 #define SYNTAX_ERROR2(msgid, arg) \
92 do { cpp_error (pfile, DL_ERROR, msgid, arg); goto syntax_error; } while(0)
96 const unsigned char s
[4];
97 const unsigned char u
;
98 const unsigned char l
;
101 static const struct suffix vsuf_1
[] = {
102 { "u", 1, 0 }, { "U", 1, 0 },
103 { "l", 0, 1 }, { "L", 0, 1 }
106 static const struct suffix vsuf_2
[] = {
107 { "ul", 1, 1 }, { "UL", 1, 1 }, { "uL", 1, 1 }, { "Ul", 1, 1 },
108 { "lu", 1, 1 }, { "LU", 1, 1 }, { "Lu", 1, 1 }, { "lU", 1, 1 },
109 { "ll", 0, 2 }, { "LL", 0, 2 }
112 static const struct suffix vsuf_3
[] = {
113 { "ull", 1, 2 }, { "ULL", 1, 2 }, { "uLL", 1, 2 }, { "Ull", 1, 2 },
114 { "llu", 1, 2 }, { "LLU", 1, 2 }, { "LLu", 1, 2 }, { "llU", 1, 2 }
117 /* Append DIGIT to NUM, a number of PRECISION bits being read in base
120 append_digit (num
, digit
, base
, precision
)
126 unsigned int shift
= 3 + (base
== 16);
128 cpp_num_part add_high
, add_low
;
130 /* Multiply by 8 or 16. Catching this overflow here means we don't
131 need to worry about add_high overflowing. */
132 overflow
= num
.high
>> (PART_PRECISION
- shift
);
133 result
.high
= num
.high
<< shift
;
134 result
.low
= num
.low
<< shift
;
135 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
139 add_low
= num
.low
<< 1;
140 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
143 add_high
= add_low
= 0;
145 if (add_low
+ digit
< add_low
)
149 if (result
.low
+ add_low
< result
.low
)
151 if (result
.high
+ add_high
< result
.high
)
154 result
.low
+= add_low
;
155 result
.high
+= add_high
;
157 /* The above code catches overflow of a cpp_num type. This catches
158 overflow of the (possibly shorter) target precision. */
159 num
.low
= result
.low
;
160 num
.high
= result
.high
;
161 result
= num_trim (result
, precision
);
162 if (!num_eq (result
, num
))
165 result
.unsignedp
= num
.unsignedp
;
166 result
.overflow
= overflow
;
170 /* Parse and convert what is presumably an integer in TOK. Accepts
171 decimal, hex, or octal with or without size suffixes. Returned op
172 is CPP_ERROR on error, otherwise it is a CPP_NUMBER. */
174 interpret_number (pfile
, tok
)
176 const cpp_token
*tok
;
180 const uchar
*p
= tok
->val
.str
.text
;
182 const struct suffix
*sufftab
;
184 unsigned int i
, nsuff
, base
, c
;
185 bool overflow
, big_digit
;
189 result
.unsignedp
= 0;
192 /* Common case of a single digit. */
193 end
= p
+ tok
->val
.str
.len
;
194 if (tok
->val
.str
.len
== 1 && (unsigned int) (p
[0] - '0') <= 9)
196 result
.low
= p
[0] - '0';
203 if (end
- p
>= 3 && (p
[1] == 'x' || p
[1] == 'X'))
216 overflow
= big_digit
= false;
217 precision
= CPP_OPTION (pfile
, precision
);
219 /* We can add a digit to numbers less than this without needing
220 double integers. 9 is the maximum digit for octal and decimal;
221 for hex it is annihilated by the division anyway. */
222 max
= ~(cpp_num_part
) 0;
223 if (precision
< PART_PRECISION
)
224 max
>>= PART_PRECISION
- precision
;
225 max
= (max
- 9) / base
+ 1;
231 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
239 /* Strict inequality for when max is set to zero. */
240 if (result
.low
< max
)
241 result
.low
= result
.low
* base
+ c
;
244 result
= append_digit (result
, c
, base
, precision
);
245 overflow
|= result
.overflow
;
252 /* Check for a floating point constant. Note that float constants
253 with an exponent or suffix but no decimal point are technically
254 invalid (C99 6.4.4.2) but accepted elsewhere. */
255 if ((c
== '.' || c
== 'F' || c
== 'f')
256 || (base
== 10 && (c
== 'E' || c
== 'e')
257 && p
+1 < end
&& (p
[1] == '+' || p
[1] == '-'))
258 || (base
== 16 && (c
== 'P' || c
== 'p')
259 && p
+1 < end
&& (p
[1] == '+' || p
[1] == '-')))
260 SYNTAX_ERROR ("floating point numbers are not valid in #if");
262 /* Determine the suffix. l means long, and u means unsigned.
263 See the suffix tables, above. */
266 case 1: sufftab
= vsuf_1
; nsuff
= ARRAY_SIZE (vsuf_1
); break;
267 case 2: sufftab
= vsuf_2
; nsuff
= ARRAY_SIZE (vsuf_2
); break;
268 case 3: sufftab
= vsuf_3
; nsuff
= ARRAY_SIZE (vsuf_3
); break;
269 default: goto invalid_suffix
;
272 for (i
= 0; i
< nsuff
; i
++)
273 if (memcmp (p
, sufftab
[i
].s
, end
- p
) == 0)
277 result
.unsignedp
= sufftab
[i
].u
;
279 if (CPP_WTRADITIONAL (pfile
)
281 && ! cpp_sys_macro_p (pfile
))
282 cpp_error (pfile
, DL_WARNING
, "traditional C rejects the `U' suffix");
283 if (sufftab
[i
].l
== 2 && CPP_OPTION (pfile
, pedantic
)
284 && ! CPP_OPTION (pfile
, c99
))
285 cpp_error (pfile
, DL_PEDWARN
,
286 "too many 'l' suffixes in integer constant");
290 cpp_error (pfile
, DL_PEDWARN
,
291 "integer constant contains digits beyond the radix");
294 cpp_error (pfile
, DL_PEDWARN
, "integer constant too large for its type");
295 /* If too big to be signed, consider it unsigned. */
296 else if (!result
.unsignedp
&& !num_positive (result
, precision
))
299 cpp_error (pfile
, DL_WARNING
,
300 "integer constant is so large that it is unsigned");
301 result
.unsignedp
= 1;
307 cpp_error (pfile
, DL_ERROR
, "invalid suffix '%.*s' on integer constant",
313 /* Handle meeting "defined" in a preprocessor expression. */
315 parse_defined (pfile
)
320 cpp_hashnode
*node
= 0;
321 const cpp_token
*token
;
322 cpp_context
*initial_context
= pfile
->context
;
324 /* Don't expand macros. */
325 pfile
->state
.prevent_expansion
++;
327 token
= cpp_get_token (pfile
);
328 if (token
->type
== CPP_OPEN_PAREN
)
331 token
= cpp_get_token (pfile
);
334 if (token
->type
== CPP_NAME
)
336 node
= token
->val
.node
;
337 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
339 cpp_error (pfile
, DL_ERROR
, "missing ')' after \"defined\"");
345 cpp_error (pfile
, DL_ERROR
,
346 "operator \"defined\" requires an identifier");
347 if (token
->flags
& NAMED_OP
)
352 op
.type
= token
->type
;
353 cpp_error (pfile
, DL_ERROR
,
354 "(\"%s\" is an alternative token for \"%s\" in C++)",
355 cpp_token_as_text (pfile
, token
),
356 cpp_token_as_text (pfile
, &op
));
362 if (pfile
->context
!= initial_context
)
363 cpp_error (pfile
, DL_WARNING
,
364 "this use of \"defined\" may not be portable");
366 /* A possible controlling macro of the form #if !defined ().
367 _cpp_parse_expr checks there was no other junk on the line. */
368 pfile
->mi_ind_cmacro
= node
;
371 pfile
->state
.prevent_expansion
--;
373 result
.unsignedp
= 0;
376 result
.low
= node
&& node
->type
== NT_MACRO
;
380 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
381 number or character constant, or the result of the "defined" or "#"
382 operators), or CPP_ERROR on error. */
384 eval_token (pfile
, token
)
386 const cpp_token
*token
;
395 return interpret_number (pfile
, token
);
400 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
405 /* Sign-extend the result if necessary. */
406 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
408 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
409 result
.low
|= ~(~(cpp_num_part
) 0
410 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
411 result
.high
= ~(cpp_num_part
) 0;
412 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
418 if (token
->val
.node
== pfile
->spec_nodes
.n_defined
)
419 return parse_defined (pfile
);
420 else if (CPP_OPTION (pfile
, cplusplus
)
421 && (token
->val
.node
== pfile
->spec_nodes
.n_true
422 || token
->val
.node
== pfile
->spec_nodes
.n_false
))
425 result
.low
= (token
->val
.node
== pfile
->spec_nodes
.n_true
);
427 /* Warn about use of true or false in #if when pedantic
428 and stdbool.h has not been included. */
429 if (CPP_PEDANTIC (pfile
)
430 && ! cpp_defined (pfile
, DSC("__bool_true_false_are_defined")))
431 cpp_error (pfile
, DL_PEDWARN
,
432 "ISO C++ does not permit \"%s\" in #if",
433 NODE_NAME (token
->val
.node
));
439 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
440 cpp_error (pfile
, DL_WARNING
, "\"%s\" is not defined",
441 NODE_NAME (token
->val
.node
));
445 default: /* CPP_HASH */
446 _cpp_test_assertion (pfile
, &temp
);
451 result
.unsignedp
= unsignedp
;
456 /* Operator precedence and flags table.
458 After an operator is returned from the lexer, if it has priority less
459 than the operator on the top of the stack, we reduce the stack by one
460 operator and repeat the test. Since equal priorities do not reduce,
461 this is naturally right-associative.
463 We handle left-associative operators by decrementing the priority of
464 just-lexed operators by one, but retaining the priority of operators
465 already on the stack.
467 The remaining cases are '(' and ')'. We handle '(' by skipping the
468 reduction phase completely. ')' is given lower priority than
469 everything else, including '(', effectively forcing a reduction of the
470 parenthesised expression. If there is a matching '(', the routine
471 reduce() exits immediately. If the normal exit route sees a ')', then
472 there cannot have been a matching '(' and an error message is output.
474 The parser assumes all shifted operators require a left operand unless
475 the flag NO_L_OPERAND is set. These semantics are automatic; any
476 extra semantics need to be handled with operator-specific code. */
479 #define NO_L_OPERAND (1 << 0)
480 #define LEFT_ASSOC (1 << 1)
483 #define UNARY (1 << 0)
484 #define BINARY (1 << 1)
485 #define OTHER (1 << 2)
487 typedef cpp_num (*binary_handler
) PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
489 /* Operator to priority map. Must be in the same order as the first
490 N entries of enum cpp_ttype. */
491 static const struct operator
496 binary_handler handler
;
499 /* EQ */ {0, 0, OTHER
, NULL
}, /* Shouldn't happen. */
500 /* NOT */ {16, NO_L_OPERAND
, UNARY
, NULL
},
501 /* GREATER */ {12, LEFT_ASSOC
, BINARY
, num_inequality_op
},
502 /* LESS */ {12, LEFT_ASSOC
, BINARY
, num_inequality_op
},
503 /* PLUS */ {14, LEFT_ASSOC
, BINARY
, num_binary_op
},
504 /* MINUS */ {14, LEFT_ASSOC
, BINARY
, num_binary_op
},
505 /* MULT */ {15, LEFT_ASSOC
, BINARY
, num_mul
},
506 /* DIV */ {15, LEFT_ASSOC
, BINARY
, num_div_op
},
507 /* MOD */ {15, LEFT_ASSOC
, BINARY
, num_div_op
},
508 /* AND */ {9, LEFT_ASSOC
, BINARY
, num_bitwise_op
},
509 /* OR */ {7, LEFT_ASSOC
, BINARY
, num_bitwise_op
},
510 /* XOR */ {8, LEFT_ASSOC
, BINARY
, num_bitwise_op
},
511 /* RSHIFT */ {13, LEFT_ASSOC
, BINARY
, num_binary_op
},
512 /* LSHIFT */ {13, LEFT_ASSOC
, BINARY
, num_binary_op
},
514 /* MIN */ {10, LEFT_ASSOC
, BINARY
, num_binary_op
},
515 /* MAX */ {10, LEFT_ASSOC
, BINARY
, num_binary_op
},
517 /* COMPL */ {16, NO_L_OPERAND
, UNARY
, NULL
},
518 /* AND_AND */ {6, LEFT_ASSOC
, OTHER
, NULL
},
519 /* OR_OR */ {5, LEFT_ASSOC
, OTHER
, NULL
},
520 /* QUERY */ {3, 0, OTHER
, NULL
},
521 /* COLON */ {4, LEFT_ASSOC
, OTHER
, NULL
},
522 /* COMMA */ {2, LEFT_ASSOC
, BINARY
, num_binary_op
},
523 /* OPEN_PAREN */ {1, NO_L_OPERAND
, OTHER
, NULL
},
524 /* CLOSE_PAREN */ {0, 0, OTHER
, NULL
},
525 /* EOF */ {0, 0, OTHER
, NULL
},
526 /* EQ_EQ */ {11, LEFT_ASSOC
, BINARY
, num_equality_op
},
527 /* NOT_EQ */ {11, LEFT_ASSOC
, BINARY
, num_equality_op
},
528 /* GREATER_EQ */ {12, LEFT_ASSOC
, BINARY
, num_inequality_op
},
529 /* LESS_EQ */ {12, LEFT_ASSOC
, BINARY
, num_inequality_op
},
530 /* UPLUS */ {16, NO_L_OPERAND
, UNARY
, NULL
},
531 /* UMINUS */ {16, NO_L_OPERAND
, UNARY
, NULL
}
534 /* Parse and evaluate a C expression, reading from PFILE.
535 Returns the truth value of the expression.
537 The implementation is an operator precedence parser, i.e. a
538 bottom-up parser, using a stack for not-yet-reduced tokens.
540 The stack base is op_stack, and the current stack pointer is 'top'.
541 There is a stack element for each operator (only), and the most
542 recently pushed operator is 'top->op'. An operand (value) is
543 stored in the 'value' field of the stack element of the operator
546 _cpp_parse_expr (pfile
)
549 struct op
*top
= pfile
->op_stack
;
550 const cpp_token
*token
= NULL
, *prev_token
;
551 unsigned int lex_count
;
552 bool saw_leading_not
, want_value
= true;
554 pfile
->state
.skip_eval
= 0;
556 /* Set up detection of #if ! defined(). */
557 pfile
->mi_ind_cmacro
= 0;
558 saw_leading_not
= false;
561 /* Lowest priority operator prevents further reductions. */
569 token
= cpp_get_token (pfile
);
575 /* These tokens convert into values. */
582 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
583 cpp_token_as_text (pfile
, token
));
585 top
->value
= eval_token (pfile
, token
);
589 saw_leading_not
= lex_count
== 1;
600 if (ISGRAPH (token
->val
.c
))
601 SYNTAX_ERROR2 ("invalid character '%c' in #if", token
->val
.c
);
603 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token
->val
.c
);
606 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
607 SYNTAX_ERROR2 ("token \"%s\" is not valid in #if expressions",
608 cpp_token_as_text (pfile
, token
));
612 /* Check we have a value or operator as appropriate. */
613 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
616 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
617 cpp_token_as_text (pfile
, token
));
621 /* Ordering here is subtle and intended to favour the
622 missing parenthesis diagnostics over alternatives. */
623 if (op
.op
== CPP_CLOSE_PAREN
)
625 if (top
->op
== CPP_OPEN_PAREN
)
626 SYNTAX_ERROR ("void expression between '(' and ')'");
628 else if (top
->op
== CPP_EOF
)
629 SYNTAX_ERROR ("#if with no expression");
630 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
631 SYNTAX_ERROR2 ("operator '%s' has no right operand",
632 cpp_token_as_text (pfile
, prev_token
));
635 top
= reduce (pfile
, top
, op
.op
);
639 if (op
.op
== CPP_EOF
)
644 case CPP_CLOSE_PAREN
:
647 if (!num_zerop (top
->value
))
648 pfile
->state
.skip_eval
++;
652 if (num_zerop (top
->value
))
653 pfile
->state
.skip_eval
++;
656 if (top
->op
!= CPP_QUERY
)
657 SYNTAX_ERROR (" ':' without preceding '?'");
658 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
659 pfile
->state
.skip_eval
++;
661 pfile
->state
.skip_eval
--;
668 /* Check for and handle stack overflow. */
669 if (++top
== pfile
->op_limit
)
670 top
= _cpp_expand_op_stack (pfile
);
675 /* The controlling macro expression is only valid if we called lex 3
676 times: <!> <defined expression> and <EOF>. push_conditional ()
677 checks that we are at top-of-file. */
678 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
679 pfile
->mi_ind_cmacro
= 0;
681 if (top
!= pfile
->op_stack
)
683 cpp_error (pfile
, DL_ICE
, "unbalanced stack in #if");
685 return false; /* Return false on syntax error. */
688 return !num_zerop (top
->value
);
691 /* Reduce the operator / value stack if possible, in preparation for
692 pushing operator OP. Returns NULL on error, otherwise the top of
695 reduce (pfile
, top
, op
)
702 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
705 cpp_error (pfile
, DL_ICE
, "impossible operator '%u'", top
->op
);
709 if (op
== CPP_OPEN_PAREN
)
712 /* Decrement the priority of left-associative operators to force a
713 reduction with operators of otherwise equal priority. */
714 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
715 while (prio
< optab
[top
->op
].prio
)
717 if (optab
[top
->op
].arity
== UNARY
)
719 if (!pfile
->state
.skip_eval
)
720 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
723 else if (optab
[top
->op
].arity
== BINARY
)
725 if (!pfile
->state
.skip_eval
)
726 top
[-1].value
= (* (binary_handler
) optab
[top
->op
].handler
)
727 (pfile
, top
[-1].value
, top
->value
, top
->op
);
730 /* Anything changing skip_eval has to be handled here. */
731 else switch (top
--->op
)
734 if (!num_zerop (top
->value
))
735 pfile
->state
.skip_eval
--;
736 top
->value
.low
= !num_zerop (top
->value
) || !num_zerop (top
[1].value
);
738 top
->value
.unsignedp
= false;
739 top
->value
.overflow
= false;
743 if (num_zerop (top
->value
))
744 pfile
->state
.skip_eval
--;
745 top
->value
.low
= !num_zerop (top
->value
) && !num_zerop (top
[1].value
);
747 top
->value
.unsignedp
= false;
748 top
->value
.overflow
= false;
752 if (op
!= CPP_CLOSE_PAREN
)
754 cpp_error (pfile
, DL_ERROR
, "missing ')' in expression");
757 top
->value
= top
[1].value
;
762 if (!num_zerop (top
->value
))
764 pfile
->state
.skip_eval
--;
765 top
->value
= top
[1].value
;
768 top
->value
= top
[2].value
;
769 top
->value
.unsignedp
= (top
[1].value
.unsignedp
770 || top
[2].value
.unsignedp
);
774 cpp_error (pfile
, DL_ERROR
, "'?' without following ':'");
781 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
782 cpp_error (pfile
, DL_PEDWARN
,
783 "integer overflow in preprocessor expression");
786 if (op
== CPP_CLOSE_PAREN
)
788 cpp_error (pfile
, DL_ERROR
, "missing '(' in expression");
795 /* Returns the position of the old top of stack after expansion. */
797 _cpp_expand_op_stack (pfile
)
800 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
801 size_t new_size
= old_size
* 2 + 20;
803 pfile
->op_stack
= (struct op
*) xrealloc (pfile
->op_stack
,
804 new_size
* sizeof (struct op
));
805 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
807 return pfile
->op_stack
+ old_size
;
810 /* Clears the unused high order bits of the number pointed to by PNUM. */
812 num_trim (num
, precision
)
816 if (precision
> PART_PRECISION
)
818 precision
-= PART_PRECISION
;
819 if (precision
< PART_PRECISION
)
820 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
824 if (precision
< PART_PRECISION
)
825 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
832 /* True iff A (presumed signed) >= 0. */
834 num_positive (num
, precision
)
838 if (precision
> PART_PRECISION
)
840 precision
-= PART_PRECISION
;
841 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
844 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
847 /* Returns the negative of NUM. */
849 num_negate (num
, precision
)
856 num
.high
= ~num
.high
;
860 num
= num_trim (num
, precision
);
861 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
866 /* Returns true if A >= B. */
868 num_greater_eq (pa
, pb
, precision
)
874 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
878 /* Both numbers have signed type. If they are of different
879 sign, the answer is the sign of A. */
880 unsignedp
= num_positive (pa
, precision
);
882 if (unsignedp
!= num_positive (pb
, precision
))
885 /* Otherwise we can do an unsigned comparison. */
888 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
891 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
893 num_bitwise_op (pfile
, lhs
, rhs
, op
)
894 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
898 lhs
.overflow
= false;
899 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
901 /* As excess precision is zeroed, there is no need to num_trim () as
902 these operations cannot introduce a set bit there. */
906 lhs
.high
&= rhs
.high
;
908 else if (op
== CPP_OR
)
911 lhs
.high
|= rhs
.high
;
916 lhs
.high
^= rhs
.high
;
922 /* Returns LHS OP RHS, where OP is an inequality. */
924 num_inequality_op (pfile
, lhs
, rhs
, op
)
929 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
931 if (op
== CPP_GREATER_EQ
)
933 else if (op
== CPP_LESS
)
935 else if (op
== CPP_GREATER
)
936 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
937 else /* CPP_LESS_EQ. */
938 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
941 lhs
.overflow
= false;
942 lhs
.unsignedp
= false;
946 /* Returns LHS OP RHS, where OP is == or !=. */
948 num_equality_op (pfile
, lhs
, rhs
, op
)
949 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
953 lhs
.low
= num_eq (lhs
, rhs
);
954 if (op
== CPP_NOT_EQ
)
957 lhs
.overflow
= false;
958 lhs
.unsignedp
= false;
962 /* Shift NUM, of width PRECISION, right by N bits. */
964 num_rshift (num
, precision
, n
)
968 cpp_num_part sign_mask
;
970 if (num
.unsignedp
|| num_positive (num
, precision
))
973 sign_mask
= ~(cpp_num_part
) 0;
976 num
.high
= num
.low
= sign_mask
;
980 if (precision
< PART_PRECISION
)
981 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
982 else if (precision
< 2 * PART_PRECISION
)
983 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
985 if (n
>= PART_PRECISION
)
989 num
.high
= sign_mask
;
994 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
995 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
999 num
= num_trim (num
, precision
);
1000 num
.overflow
= false;
1004 /* Shift NUM, of width PRECISION, left by N bits. */
1006 num_lshift (num
, precision
, n
)
1008 size_t precision
, n
;
1012 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1013 num
.high
= num
.low
= 0;
1017 cpp_num orig
, maybe_orig
;
1021 if (m
>= PART_PRECISION
)
1023 m
-= PART_PRECISION
;
1029 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1032 num
= num_trim (num
, precision
);
1035 num
.overflow
= false;
1038 maybe_orig
= num_rshift (num
, precision
, n
);
1039 num
.overflow
= !num_eq (orig
, maybe_orig
);
1046 /* The four unary operators: +, -, ! and ~. */
1048 num_unary_op (pfile
, num
, op
)
1056 if (CPP_WTRADITIONAL (pfile
))
1057 cpp_error (pfile
, DL_WARNING
,
1058 "traditional C rejects the unary plus operator");
1059 num
.overflow
= false;
1063 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1067 num
.high
= ~num
.high
;
1069 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1070 num
.overflow
= false;
1073 default: /* case CPP_NOT: */
1074 num
.low
= num_zerop (num
);
1076 num
.overflow
= false;
1077 num
.unsignedp
= false;
1084 /* The various binary operators. */
1086 num_binary_op (pfile
, lhs
, rhs
, op
)
1092 size_t precision
= CPP_OPTION (pfile
, precision
);
1101 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1103 /* A negative shift is a positive shift the other way. */
1104 if (op
== CPP_LSHIFT
)
1108 rhs
= num_negate (rhs
, precision
);
1111 n
= ~0; /* Maximal. */
1114 if (op
== CPP_LSHIFT
)
1115 lhs
= num_lshift (lhs
, precision
, n
);
1117 lhs
= num_rshift (lhs
, precision
, n
);
1124 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1126 gte
= num_greater_eq (lhs
, rhs
, precision
);
1131 lhs
.unsignedp
= unsignedp
;
1137 rhs
= num_negate (rhs
, precision
);
1139 result
.low
= lhs
.low
+ rhs
.low
;
1140 result
.high
= lhs
.high
+ rhs
.high
;
1141 if (result
.low
< lhs
.low
)
1144 result
= num_trim (result
, precision
);
1145 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1146 if (result
.unsignedp
)
1147 result
.overflow
= false;
1150 bool lhsp
= num_positive (lhs
, precision
);
1151 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1152 && lhsp
!= num_positive (result
, precision
));
1157 default: /* case CPP_COMMA: */
1158 if (CPP_PEDANTIC (pfile
))
1159 cpp_error (pfile
, DL_PEDWARN
,
1160 "comma operator in operand of #if");
1168 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1171 num_part_mul (lhs
, rhs
)
1172 cpp_num_part lhs
, rhs
;
1175 cpp_num_part middle
[2], temp
;
1177 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1178 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1180 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1181 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1184 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1185 if (result
.low
< temp
)
1189 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1190 if (result
.low
< temp
)
1193 result
.high
+= HIGH_PART (middle
[0]);
1194 result
.high
+= HIGH_PART (middle
[1]);
1199 /* Multiply two preprocessing numbers. */
1201 num_mul (pfile
, lhs
, rhs
, op
)
1204 enum cpp_ttype op ATTRIBUTE_UNUSED
;
1206 cpp_num result
, temp
;
1207 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1208 bool overflow
, negate
= false;
1209 size_t precision
= CPP_OPTION (pfile
, precision
);
1211 /* Prepare for unsigned multiplication. */
1214 if (!num_positive (lhs
, precision
))
1215 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1216 if (!num_positive (rhs
, precision
))
1217 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1220 overflow
= lhs
.high
&& rhs
.high
;
1221 result
= num_part_mul (lhs
.low
, rhs
.low
);
1223 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1224 result
.high
+= temp
.low
;
1228 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1229 result
.high
+= temp
.low
;
1233 temp
.low
= result
.low
, temp
.high
= result
.high
;
1234 result
= num_trim (result
, precision
);
1235 if (!num_eq (result
, temp
))
1239 result
= num_negate (result
, precision
);
1242 result
.overflow
= false;
1244 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1245 && !num_zerop (result
));
1246 result
.unsignedp
= unsignedp
;
1251 /* Divide two preprocessing numbers, returning the answer or the
1252 remainder depending upon OP. */
1254 num_div_op (pfile
, lhs
, rhs
, op
)
1259 cpp_num result
, sub
;
1261 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1262 bool negate
= false, lhs_neg
= false;
1263 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1265 /* Prepare for unsigned division. */
1268 if (!num_positive (lhs
, precision
))
1269 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
1270 if (!num_positive (rhs
, precision
))
1271 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1274 /* Find the high bit. */
1278 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1279 for (; ; i
--, mask
>>= 1)
1280 if (rhs
.high
& mask
)
1285 if (precision
> PART_PRECISION
)
1286 i
= precision
- PART_PRECISION
- 1;
1289 mask
= (cpp_num_part
) 1 << i
;
1290 for (; ; i
--, mask
>>= 1)
1296 cpp_error (pfile
, DL_ERROR
, "division by zero in #if");
1300 /* First non-zero bit of RHS is bit I. Do naive division by
1301 shifting the RHS fully left, and subtracting from LHS if LHS is
1302 at least as big, and then repeating but with one less shift.
1303 This is not very efficient, but is easy to understand. */
1305 rhs
.unsignedp
= true;
1306 lhs
.unsignedp
= true;
1307 i
= precision
- i
- 1;
1308 sub
= num_lshift (rhs
, precision
, i
);
1310 result
.high
= result
.low
= 0;
1313 if (num_greater_eq (lhs
, sub
, precision
))
1315 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
1316 if (i
>= PART_PRECISION
)
1317 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1319 result
.low
|= (cpp_num_part
) 1 << i
;
1323 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
1327 /* We divide so that the remainder has the sign of the LHS. */
1330 result
.unsignedp
= unsignedp
;
1332 result
.overflow
= false;
1336 result
= num_negate (result
, precision
);
1337 result
.overflow
= num_positive (result
, precision
) ^ !negate
;
1344 lhs
.unsignedp
= unsignedp
;
1345 lhs
.overflow
= false;
1347 lhs
= num_negate (lhs
, precision
);