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