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