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