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