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