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