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