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