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