blob: 8401daee032f24cb655766cc4b92f9948c64ced7 [file] [log] [blame]
Zack Weinbergb0699da2000-03-07 20:58:47 +00001/* Parse C expressions for cpplib.
Neil Booth5d8ebbd2002-01-03 21:43:09 +00002 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
Kazu Hirataca2b05b2004-03-15 18:20:51 +00003 2002, 2004 Free Software Foundation.
Richard Kennere38992e2000-04-18 20:42:00 +00004 Contributed by Per Bothner, 1994.
Per Bothner7f2935c1995-03-16 13:59:07 -08005
6This program is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
Kelley Cook200031d2005-06-29 02:34:39 +000018Foundation, 51 Franklin Street, Fifth Floor,
19Boston, MA 02110-1301, USA. */
Per Bothner7f2935c1995-03-16 13:59:07 -080020
Per Bothner7f2935c1995-03-16 13:59:07 -080021#include "config.h"
Kaveh R. Ghazib04cd5071998-03-30 12:05:54 +000022#include "system.h"
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +000023#include "cpplib.h"
Paolo Bonzini4f4e53dd2004-05-24 10:50:45 +000024#include "internal.h"
Per Bothner7f2935c1995-03-16 13:59:07 -080025
Neil Booth91318902002-05-26 18:42:21 +000026#define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
27#define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
28#define LOW_PART(num_part) (num_part & HALF_MASK)
29#define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
30
Zack Weinbergcf00a882000-07-08 02:33:00 +000031struct op
Zack Weinbergb0699da2000-03-07 20:58:47 +000032{
Neil Booth68e652752002-07-20 13:31:56 +000033 const cpp_token *token; /* The token forming op (for diagnostics). */
Neil Boothad28cff2002-07-18 22:08:35 +000034 cpp_num value; /* The value logically "right" of op. */
Zack Weinbergcf00a882000-07-08 02:33:00 +000035 enum cpp_ttype op;
Per Bothner7f2935c1995-03-16 13:59:07 -080036};
Per Bothner7f2935c1995-03-16 13:59:07 -080037
Neil Booth91318902002-05-26 18:42:21 +000038/* 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)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000041static bool num_positive (cpp_num, size_t);
42static bool num_greater_eq (cpp_num, cpp_num, size_t);
43static cpp_num num_trim (cpp_num, size_t);
44static cpp_num num_part_mul (cpp_num_part, cpp_num_part);
Neil Booth91318902002-05-26 18:42:21 +000045
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000046static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype);
47static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
48static cpp_num num_negate (cpp_num, size_t);
49static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
50static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num,
51 enum cpp_ttype);
52static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num,
53 enum cpp_ttype);
54static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num);
55static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
56static cpp_num num_lshift (cpp_num, size_t, size_t);
57static cpp_num num_rshift (cpp_num, size_t, size_t);
Neil Booth91318902002-05-26 18:42:21 +000058
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000059static cpp_num append_digit (cpp_num, int, int, size_t);
60static cpp_num parse_defined (cpp_reader *);
61static cpp_num eval_token (cpp_reader *, const cpp_token *);
62static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
63static unsigned int interpret_float_suffix (const uchar *, size_t);
64static unsigned int interpret_int_suffix (const uchar *, size_t);
65static void check_promotion (cpp_reader *, const struct op *);
Neil Booth91318902002-05-26 18:42:21 +000066
Neil Boothcd7ab832002-05-29 17:15:42 +000067/* Token type abuse to create unary plus and minus operators. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +000068#define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
69#define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
Zack Weinbergcf00a882000-07-08 02:33:00 +000070
Zack Weinberg15dad1d2000-05-18 15:55:46 +000071/* With -O2, gcc appears to produce nice code, moving the error
72 message load and subsequent jump completely out of the main path. */
Zack Weinberg15dad1d2000-05-18 15:55:46 +000073#define SYNTAX_ERROR(msgid) \
John David Anglin0527bc42003-11-01 22:56:54 +000074 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
Zack Weinberg15dad1d2000-05-18 15:55:46 +000075#define SYNTAX_ERROR2(msgid, arg) \
John David Anglin0527bc42003-11-01 22:56:54 +000076 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
77 while(0)
Zack Weinberg15dad1d2000-05-18 15:55:46 +000078
Neil Boothcd7ab832002-05-29 17:15:42 +000079/* Subroutine of cpp_classify_number. S points to a float suffix of
80 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
81 flag vector describing the suffix. */
82static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000083interpret_float_suffix (const uchar *s, size_t len)
Per Bothner7f2935c1995-03-16 13:59:07 -080084{
Jon Grimmad6ed772005-12-06 23:13:15 +000085 size_t f = 0, l = 0, i = 0, d = 0;
Zack Weinbergcf00a882000-07-08 02:33:00 +000086
Neil Boothcd7ab832002-05-29 17:15:42 +000087 while (len--)
88 switch (s[len])
89 {
Janis Johnson30e04922007-05-14 23:43:07 +000090 case 'f': case 'F':
91 if (d > 0)
92 return 0;
93 f++;
94 break;
95 case 'l': case 'L':
96 if (d > 0)
97 return 0;
98 l++;
99 break;
Neil Boothcd7ab832002-05-29 17:15:42 +0000100 case 'i': case 'I':
101 case 'j': case 'J': i++; break;
Janis Johnson30e04922007-05-14 23:43:07 +0000102 case 'd': case 'D': d++; break;
Neil Boothcd7ab832002-05-29 17:15:42 +0000103 default:
104 return 0;
105 }
Zack Weinbergcf00a882000-07-08 02:33:00 +0000106
Neil Boothcd7ab832002-05-29 17:15:42 +0000107 if (f + l > 1 || i > 1)
108 return 0;
Zack Weinbergcf00a882000-07-08 02:33:00 +0000109
Jon Grimmad6ed772005-12-06 23:13:15 +0000110 /* Allow dd, df, dl suffixes for decimal float constants. */
111 if (d && ((d + f + l != 2) || i))
112 return 0;
113
Neil Boothcd7ab832002-05-29 17:15:42 +0000114 return ((i ? CPP_N_IMAGINARY : 0)
115 | (f ? CPP_N_SMALL :
Jon Grimmad6ed772005-12-06 23:13:15 +0000116 l ? CPP_N_LARGE : CPP_N_MEDIUM)
117 | (d ? CPP_N_DFLOAT : 0));
Neil Boothcd7ab832002-05-29 17:15:42 +0000118}
119
120/* Subroutine of cpp_classify_number. S points to an integer suffix
121 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
122 flag vector describing the suffix. */
123static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000124interpret_int_suffix (const uchar *s, size_t len)
Neil Boothcd7ab832002-05-29 17:15:42 +0000125{
126 size_t u, l, i;
127
128 u = l = i = 0;
129
130 while (len--)
131 switch (s[len])
132 {
133 case 'u': case 'U': u++; break;
134 case 'i': case 'I':
135 case 'j': case 'J': i++; break;
136 case 'l': case 'L': l++;
137 /* If there are two Ls, they must be adjacent and the same case. */
138 if (l == 2 && s[len] != s[len + 1])
139 return 0;
140 break;
141 default:
142 return 0;
143 }
144
145 if (l > 2 || u > 1 || i > 1)
146 return 0;
147
148 return ((i ? CPP_N_IMAGINARY : 0)
149 | (u ? CPP_N_UNSIGNED : 0)
150 | ((l == 0) ? CPP_N_SMALL
151 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
152}
153
154/* Categorize numeric constants according to their field (integer,
155 floating point, or invalid), radix (decimal, octal, hexadecimal),
156 and type suffixes. */
157unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000158cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
Neil Boothcd7ab832002-05-29 17:15:42 +0000159{
160 const uchar *str = token->val.str.text;
161 const uchar *limit;
162 unsigned int max_digit, result, radix;
163 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
164
165 /* If the lexer has done its job, length one can only be a single
166 digit. Fast-path this very common case. */
167 if (token->val.str.len == 1)
168 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
169
170 limit = str + token->val.str.len;
171 float_flag = NOT_FLOAT;
172 max_digit = 0;
173 radix = 10;
174
175 /* First, interpret the radix. */
176 if (*str == '0')
177 {
178 radix = 8;
179 str++;
180
181 /* Require at least one hex digit to classify it as hex. */
Michael Matz7f1fc382003-03-31 15:50:53 +0000182 if ((*str == 'x' || *str == 'X')
183 && (str[1] == '.' || ISXDIGIT (str[1])))
Neil Boothcd7ab832002-05-29 17:15:42 +0000184 {
185 radix = 16;
186 str++;
187 }
188 }
189
190 /* Now scan for a well-formed integer or float. */
191 for (;;)
192 {
193 unsigned int c = *str++;
194
195 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
196 {
197 c = hex_value (c);
198 if (c > max_digit)
199 max_digit = c;
200 }
201 else if (c == '.')
202 {
203 if (float_flag == NOT_FLOAT)
204 float_flag = AFTER_POINT;
205 else
206 SYNTAX_ERROR ("too many decimal points in number");
207 }
208 else if ((radix <= 10 && (c == 'e' || c == 'E'))
209 || (radix == 16 && (c == 'p' || c == 'P')))
210 {
211 float_flag = AFTER_EXPON;
212 break;
213 }
214 else
215 {
216 /* Start of suffix. */
217 str--;
218 break;
219 }
220 }
221
222 if (float_flag != NOT_FLOAT && radix == 8)
223 radix = 10;
224
225 if (max_digit >= radix)
226 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
227
228 if (float_flag != NOT_FLOAT)
229 {
230 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
John David Anglin0527bc42003-11-01 22:56:54 +0000231 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothcd7ab832002-05-29 17:15:42 +0000232 "use of C99 hexadecimal floating constant");
233
234 if (float_flag == AFTER_EXPON)
235 {
236 if (*str == '+' || *str == '-')
237 str++;
238
239 /* Exponent is decimal, even if string is a hex float. */
240 if (!ISDIGIT (*str))
241 SYNTAX_ERROR ("exponent has no digits");
242
243 do
244 str++;
245 while (ISDIGIT (*str));
246 }
247 else if (radix == 16)
248 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
249
250 result = interpret_float_suffix (str, limit - str);
251 if (result == 0)
252 {
John David Anglin0527bc42003-11-01 22:56:54 +0000253 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000254 "invalid suffix \"%.*s\" on floating constant",
Andreas Jaeger91b12472002-06-01 16:11:45 +0200255 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000256 return CPP_N_INVALID;
257 }
258
259 /* Traditional C didn't accept any floating suffixes. */
260 if (limit != str
261 && CPP_WTRADITIONAL (pfile)
262 && ! cpp_sys_macro_p (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000263 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothcd7ab832002-05-29 17:15:42 +0000264 "traditional C rejects the \"%.*s\" suffix",
Andreas Jaeger91b12472002-06-01 16:11:45 +0200265 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000266
Jon Grimmad6ed772005-12-06 23:13:15 +0000267 /* Radix must be 10 for decimal floats. */
268 if ((result & CPP_N_DFLOAT) && radix != 10)
269 {
270 cpp_error (pfile, CPP_DL_ERROR,
271 "invalid suffix \"%.*s\" with hexadecimal floating constant",
272 (int) (limit - str), str);
273 return CPP_N_INVALID;
274 }
275
Neil Boothcd7ab832002-05-29 17:15:42 +0000276 result |= CPP_N_FLOATING;
277 }
278 else
279 {
280 result = interpret_int_suffix (str, limit - str);
281 if (result == 0)
282 {
John David Anglin0527bc42003-11-01 22:56:54 +0000283 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000284 "invalid suffix \"%.*s\" on integer constant",
Andreas Jaeger91b12472002-06-01 16:11:45 +0200285 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000286 return CPP_N_INVALID;
287 }
288
Zack Weinberg56da7202002-08-02 04:18:16 +0000289 /* Traditional C only accepted the 'L' suffix.
290 Suppress warning about 'LL' with -Wno-long-long. */
291 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
292 {
293 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
294 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
295
296 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
John David Anglin0527bc42003-11-01 22:56:54 +0000297 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinberg56da7202002-08-02 04:18:16 +0000298 "traditional C rejects the \"%.*s\" suffix",
299 (int) (limit - str), str);
300 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000301
302 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
303 && ! CPP_OPTION (pfile, c99)
304 && CPP_OPTION (pfile, warn_long_long))
John David Anglin0527bc42003-11-01 22:56:54 +0000305 cpp_error (pfile, CPP_DL_PEDWARN,
306 "use of C99 long long integer constant");
Neil Boothcd7ab832002-05-29 17:15:42 +0000307
308 result |= CPP_N_INTEGER;
309 }
310
311 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000312 cpp_error (pfile, CPP_DL_PEDWARN,
313 "imaginary constants are a GCC extension");
Neil Boothcd7ab832002-05-29 17:15:42 +0000314
315 if (radix == 10)
316 result |= CPP_N_DECIMAL;
317 else if (radix == 16)
318 result |= CPP_N_HEX;
319 else
320 result |= CPP_N_OCTAL;
321
322 return result;
323
324 syntax_error:
325 return CPP_N_INVALID;
326}
327
328/* cpp_interpret_integer converts an integer constant into a cpp_num,
329 of precision options->precision.
330
331 We do not provide any interface for decimal->float conversion,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000332 because the preprocessor doesn't need it and we don't want to
333 drag in GCC's floating point emulator. */
Neil Boothcd7ab832002-05-29 17:15:42 +0000334cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000335cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
336 unsigned int type)
Neil Boothcd7ab832002-05-29 17:15:42 +0000337{
338 const uchar *p, *end;
339 cpp_num result;
340
341 result.low = 0;
342 result.high = 0;
Neil Booth23ff0222002-07-17 17:27:14 +0000343 result.unsignedp = !!(type & CPP_N_UNSIGNED);
344 result.overflow = false;
Neil Boothcd7ab832002-05-29 17:15:42 +0000345
346 p = token->val.str.text;
347 end = p + token->val.str.len;
348
349 /* Common case of a single digit. */
350 if (token->val.str.len == 1)
351 result.low = p[0] - '0';
352 else
353 {
354 cpp_num_part max;
355 size_t precision = CPP_OPTION (pfile, precision);
356 unsigned int base = 10, c = 0;
357 bool overflow = false;
358
359 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
360 {
361 base = 8;
362 p++;
363 }
364 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
365 {
366 base = 16;
367 p += 2;
368 }
369
370 /* We can add a digit to numbers strictly less than this without
371 needing the precision and slowness of double integers. */
372 max = ~(cpp_num_part) 0;
373 if (precision < PART_PRECISION)
374 max >>= PART_PRECISION - precision;
375 max = (max - base + 1) / base + 1;
376
377 for (; p < end; p++)
378 {
379 c = *p;
380
381 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
382 c = hex_value (c);
383 else
384 break;
385
386 /* Strict inequality for when max is set to zero. */
387 if (result.low < max)
388 result.low = result.low * base + c;
389 else
390 {
391 result = append_digit (result, c, base, precision);
392 overflow |= result.overflow;
393 max = 0;
394 }
395 }
396
397 if (overflow)
John David Anglin0527bc42003-11-01 22:56:54 +0000398 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothcd7ab832002-05-29 17:15:42 +0000399 "integer constant is too large for its type");
Neil Booth017acb42002-06-20 20:34:19 +0000400 /* If too big to be signed, consider it unsigned. Only warn for
401 decimal numbers. Traditional numbers were always signed (but
Kazu Hirata8d9afc4e2002-09-16 11:42:00 +0000402 we still honor an explicit U suffix); but we only have
Neil Boothcd98faa2002-07-09 22:21:37 +0000403 traditional semantics in directives. */
Neil Booth017acb42002-06-20 20:34:19 +0000404 else if (!result.unsignedp
Neil Boothcd98faa2002-07-09 22:21:37 +0000405 && !(CPP_OPTION (pfile, traditional)
406 && pfile->state.in_directive)
Neil Booth017acb42002-06-20 20:34:19 +0000407 && !num_positive (result, precision))
Neil Boothcd7ab832002-05-29 17:15:42 +0000408 {
Neil Boothcd7ab832002-05-29 17:15:42 +0000409 if (base == 10)
John David Anglin0527bc42003-11-01 22:56:54 +0000410 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothcd7ab832002-05-29 17:15:42 +0000411 "integer constant is so large that it is unsigned");
Neil Booth23ff0222002-07-17 17:27:14 +0000412 result.unsignedp = true;
Neil Boothcd7ab832002-05-29 17:15:42 +0000413 }
414 }
415
416 return result;
417}
Zack Weinbergcf00a882000-07-08 02:33:00 +0000418
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000419/* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
Neil Booth91318902002-05-26 18:42:21 +0000420static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000421append_digit (cpp_num num, int digit, int base, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +0000422{
423 cpp_num result;
424 unsigned int shift = 3 + (base == 16);
425 bool overflow;
426 cpp_num_part add_high, add_low;
427
428 /* Multiply by 8 or 16. Catching this overflow here means we don't
429 need to worry about add_high overflowing. */
Neil Booth23ff0222002-07-17 17:27:14 +0000430 overflow = !!(num.high >> (PART_PRECISION - shift));
Neil Booth91318902002-05-26 18:42:21 +0000431 result.high = num.high << shift;
432 result.low = num.low << shift;
433 result.high |= num.low >> (PART_PRECISION - shift);
Diego Novillo6de9cd92004-05-13 02:41:07 -0400434 result.unsignedp = num.unsignedp;
Neil Booth91318902002-05-26 18:42:21 +0000435
436 if (base == 10)
437 {
438 add_low = num.low << 1;
439 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
440 }
441 else
442 add_high = add_low = 0;
443
444 if (add_low + digit < add_low)
445 add_high++;
446 add_low += digit;
Eric Christopher22a8a522007-05-02 21:57:50 +0000447
Neil Booth91318902002-05-26 18:42:21 +0000448 if (result.low + add_low < result.low)
449 add_high++;
450 if (result.high + add_high < result.high)
451 overflow = true;
452
453 result.low += add_low;
454 result.high += add_high;
Diego Novillo6de9cd92004-05-13 02:41:07 -0400455 result.overflow = overflow;
Neil Booth91318902002-05-26 18:42:21 +0000456
457 /* The above code catches overflow of a cpp_num type. This catches
458 overflow of the (possibly shorter) target precision. */
459 num.low = result.low;
460 num.high = result.high;
461 result = num_trim (result, precision);
462 if (!num_eq (result, num))
Diego Novillo6de9cd92004-05-13 02:41:07 -0400463 result.overflow = true;
Neil Booth91318902002-05-26 18:42:21 +0000464
Neil Booth91318902002-05-26 18:42:21 +0000465 return result;
466}
467
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000468/* Handle meeting "defined" in a preprocessor expression. */
Neil Booth91318902002-05-26 18:42:21 +0000469static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000470parse_defined (cpp_reader *pfile)
Zack Weinbergba412f12000-03-01 00:57:09 +0000471{
Neil Booth91318902002-05-26 18:42:21 +0000472 cpp_num result;
Neil Booth93c803682000-10-28 17:59:06 +0000473 int paren = 0;
474 cpp_hashnode *node = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000475 const cpp_token *token;
Neil Booth63d75002001-11-05 22:26:13 +0000476 cpp_context *initial_context = pfile->context;
Zack Weinbergcf00a882000-07-08 02:33:00 +0000477
Neil Booth93c803682000-10-28 17:59:06 +0000478 /* Don't expand macros. */
479 pfile->state.prevent_expansion++;
480
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000481 token = cpp_get_token (pfile);
482 if (token->type == CPP_OPEN_PAREN)
Zack Weinbergcf00a882000-07-08 02:33:00 +0000483 {
484 paren = 1;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000485 token = cpp_get_token (pfile);
Zack Weinbergcf00a882000-07-08 02:33:00 +0000486 }
487
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000488 if (token->type == CPP_NAME)
Zack Weinberg041c3192000-07-04 01:58:21 +0000489 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000490 node = token->val.node;
491 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
Neil Booth93c803682000-10-28 17:59:06 +0000492 {
John David Anglin0527bc42003-11-01 22:56:54 +0000493 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000494 node = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000495 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000496 }
Neil Booth93c803682000-10-28 17:59:06 +0000497 else
Neil Booth3c8465d2001-02-06 20:07:07 +0000498 {
John David Anglin0527bc42003-11-01 22:56:54 +0000499 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000500 "operator \"defined\" requires an identifier");
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000501 if (token->flags & NAMED_OP)
Neil Booth3c8465d2001-02-06 20:07:07 +0000502 {
503 cpp_token op;
504
505 op.flags = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000506 op.type = token->type;
John David Anglin0527bc42003-11-01 22:56:54 +0000507 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth3c8465d2001-02-06 20:07:07 +0000508 "(\"%s\" is an alternative token for \"%s\" in C++)",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000509 cpp_token_as_text (pfile, token),
Neil Booth3c8465d2001-02-06 20:07:07 +0000510 cpp_token_as_text (pfile, &op));
511 }
512 }
Neil Booth93c803682000-10-28 17:59:06 +0000513
Neil Booth91318902002-05-26 18:42:21 +0000514 if (node)
Neil Booth93c803682000-10-28 17:59:06 +0000515 {
Neil Booth335d03e2003-08-03 12:23:46 +0000516 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000517 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000518 "this use of \"defined\" may not be portable");
Neil Booth63d75002001-11-05 22:26:13 +0000519
Neil Bootha69cbaa2002-07-23 22:57:49 +0000520 _cpp_mark_macro_used (node);
521
Neil Booth6d18adb2001-07-29 17:27:57 +0000522 /* A possible controlling macro of the form #if !defined ().
523 _cpp_parse_expr checks there was no other junk on the line. */
524 pfile->mi_ind_cmacro = node;
Neil Booth93c803682000-10-28 17:59:06 +0000525 }
526
527 pfile->state.prevent_expansion--;
Neil Booth91318902002-05-26 18:42:21 +0000528
Neil Booth23ff0222002-07-17 17:27:14 +0000529 result.unsignedp = false;
Neil Booth91318902002-05-26 18:42:21 +0000530 result.high = 0;
Neil Booth23ff0222002-07-17 17:27:14 +0000531 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +0000532 result.low = node && node->type == NT_MACRO;
533 return result;
Zack Weinberg15dad1d2000-05-18 15:55:46 +0000534}
535
Neil Booth60284a52002-04-28 23:14:56 +0000536/* Convert a token into a CPP_NUMBER (an interpreted preprocessing
537 number or character constant, or the result of the "defined" or "#"
Neil Boothcd7ab832002-05-29 17:15:42 +0000538 operators). */
Neil Booth91318902002-05-26 18:42:21 +0000539static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000540eval_token (cpp_reader *pfile, const cpp_token *token)
Per Bothner7f2935c1995-03-16 13:59:07 -0800541{
Neil Booth91318902002-05-26 18:42:21 +0000542 cpp_num result;
Neil Booth60284a52002-04-28 23:14:56 +0000543 unsigned int temp;
Neil Booth4268e8b2002-05-04 07:30:32 +0000544 int unsignedp = 0;
Zack Weinberg041c3192000-07-04 01:58:21 +0000545
Diego Novillo6de9cd92004-05-13 02:41:07 -0400546 result.unsignedp = false;
547 result.overflow = false;
548
Neil Booth93c803682000-10-28 17:59:06 +0000549 switch (token->type)
Zack Weinbergba412f12000-03-01 00:57:09 +0000550 {
Per Bothner7f2935c1995-03-16 13:59:07 -0800551 case CPP_NUMBER:
Neil Boothcd7ab832002-05-29 17:15:42 +0000552 temp = cpp_classify_number (pfile, token);
553 switch (temp & CPP_N_CATEGORY)
554 {
555 case CPP_N_FLOATING:
John David Anglin0527bc42003-11-01 22:56:54 +0000556 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000557 "floating constant in preprocessor expression");
558 break;
559 case CPP_N_INTEGER:
560 if (!(temp & CPP_N_IMAGINARY))
561 return cpp_interpret_integer (pfile, token, temp);
John David Anglin0527bc42003-11-01 22:56:54 +0000562 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000563 "imaginary number in preprocessor expression");
564 break;
565
566 case CPP_N_INVALID:
567 /* Error already issued. */
568 break;
569 }
570 result.high = result.low = 0;
571 break;
Neil Booth7f2f1a62000-11-14 18:32:06 +0000572
Alexandre Oliva525bc952000-02-23 19:21:07 +0000573 case CPP_WCHAR:
Neil Booth4268e8b2002-05-04 07:30:32 +0000574 case CPP_CHAR:
Neil Bootha5a49442002-05-06 22:53:10 +0000575 {
Neil Booth91318902002-05-26 18:42:21 +0000576 cppchar_t cc = cpp_interpret_charconst (pfile, token,
577 &temp, &unsignedp);
578
579 result.high = 0;
580 result.low = cc;
Neil Bootha5a49442002-05-06 22:53:10 +0000581 /* Sign-extend the result if necessary. */
Neil Booth91318902002-05-26 18:42:21 +0000582 if (!unsignedp && (cppchar_signed_t) cc < 0)
583 {
584 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
585 result.low |= ~(~(cpp_num_part) 0
586 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
587 result.high = ~(cpp_num_part) 0;
588 result = num_trim (result, CPP_OPTION (pfile, precision));
589 }
Neil Bootha5a49442002-05-06 22:53:10 +0000590 }
Neil Booth60284a52002-04-28 23:14:56 +0000591 break;
Zack Weinbergba412f12000-03-01 00:57:09 +0000592
Zack Weinberg92936ec2000-07-19 20:18:08 +0000593 case CPP_NAME:
Neil Booth93c803682000-10-28 17:59:06 +0000594 if (token->val.node == pfile->spec_nodes.n_defined)
Neil Booth63d75002001-11-05 22:26:13 +0000595 return parse_defined (pfile);
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000596 else if (CPP_OPTION (pfile, cplusplus)
597 && (token->val.node == pfile->spec_nodes.n_true
598 || token->val.node == pfile->spec_nodes.n_false))
599 {
Neil Booth91318902002-05-26 18:42:21 +0000600 result.high = 0;
601 result.low = (token->val.node == pfile->spec_nodes.n_true);
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000602 }
603 else
604 {
Neil Booth91318902002-05-26 18:42:21 +0000605 result.high = 0;
606 result.low = 0;
Neil Booth87ed1092002-04-28 19:42:54 +0000607 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +0000608 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined",
Neil Boothebef4e82002-04-14 18:42:47 +0000609 NODE_NAME (token->val.node));
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000610 }
Neil Booth60284a52002-04-28 23:14:56 +0000611 break;
Zack Weinberg5dfa4da1999-02-08 20:27:27 +0000612
Neil Booth60284a52002-04-28 23:14:56 +0000613 default: /* CPP_HASH */
Neil Booth91318902002-05-26 18:42:21 +0000614 _cpp_test_assertion (pfile, &temp);
615 result.high = 0;
616 result.low = temp;
Neil Booth93c803682000-10-28 17:59:06 +0000617 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800618
Neil Booth23ff0222002-07-17 17:27:14 +0000619 result.unsignedp = !!unsignedp;
Neil Booth91318902002-05-26 18:42:21 +0000620 return result;
Per Bothner7f2935c1995-03-16 13:59:07 -0800621}
622
Neil Booth4063b942000-04-02 08:27:23 +0000623/* Operator precedence and flags table.
Neil Boothdbac4af2000-04-01 07:48:59 +0000624
625After an operator is returned from the lexer, if it has priority less
Neil Booth87ed1092002-04-28 19:42:54 +0000626than the operator on the top of the stack, we reduce the stack by one
627operator and repeat the test. Since equal priorities do not reduce,
628this is naturally right-associative.
Neil Boothdbac4af2000-04-01 07:48:59 +0000629
Neil Booth87ed1092002-04-28 19:42:54 +0000630We handle left-associative operators by decrementing the priority of
631just-lexed operators by one, but retaining the priority of operators
632already on the stack.
Neil Boothdbac4af2000-04-01 07:48:59 +0000633
634The remaining cases are '(' and ')'. We handle '(' by skipping the
635reduction phase completely. ')' is given lower priority than
636everything else, including '(', effectively forcing a reduction of the
Kazu Hirata272d0be2002-12-19 05:18:13 +0000637parenthesized expression. If there is a matching '(', the routine
Neil Booth87ed1092002-04-28 19:42:54 +0000638reduce() exits immediately. If the normal exit route sees a ')', then
639there cannot have been a matching '(' and an error message is output.
Neil Boothdbac4af2000-04-01 07:48:59 +0000640
Neil Boothf8b954f2002-04-26 06:32:50 +0000641The parser assumes all shifted operators require a left operand unless
642the flag NO_L_OPERAND is set. These semantics are automatic; any
643extra semantics need to be handled with operator-specific code. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800644
Neil Booth68e652752002-07-20 13:31:56 +0000645/* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
646 operand changes because of integer promotions. */
Neil Booth87ed1092002-04-28 19:42:54 +0000647#define NO_L_OPERAND (1 << 0)
648#define LEFT_ASSOC (1 << 1)
Neil Booth68e652752002-07-20 13:31:56 +0000649#define CHECK_PROMOTION (1 << 2)
Neil Bootheba30522000-03-31 22:23:59 +0000650
Zack Weinbergcf00a882000-07-08 02:33:00 +0000651/* Operator to priority map. Must be in the same order as the first
652 N entries of enum cpp_ttype. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000653static const struct cpp_operator
Zack Weinbergcf00a882000-07-08 02:33:00 +0000654{
Neil Booth60284a52002-04-28 23:14:56 +0000655 uchar prio;
Neil Booth87ed1092002-04-28 19:42:54 +0000656 uchar flags;
657} optab[] =
658{
Neil Boothad28cff2002-07-18 22:08:35 +0000659 /* EQ */ {0, 0}, /* Shouldn't happen. */
660 /* NOT */ {16, NO_L_OPERAND},
Neil Booth68e652752002-07-20 13:31:56 +0000661 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
662 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
663 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
664 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
665 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
666 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
667 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
668 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
669 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
670 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
Neil Boothad28cff2002-07-18 22:08:35 +0000671 /* RSHIFT */ {13, LEFT_ASSOC},
672 /* LSHIFT */ {13, LEFT_ASSOC},
Zack Weinbergcf00a882000-07-08 02:33:00 +0000673
Neil Boothad28cff2002-07-18 22:08:35 +0000674 /* COMPL */ {16, NO_L_OPERAND},
Neil Booth75aef482002-07-19 19:24:43 +0000675 /* AND_AND */ {6, LEFT_ASSOC},
676 /* OR_OR */ {5, LEFT_ASSOC},
677 /* QUERY */ {3, 0},
Neil Booth68e652752002-07-20 13:31:56 +0000678 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
Neil Boothad28cff2002-07-18 22:08:35 +0000679 /* COMMA */ {2, LEFT_ASSOC},
Neil Booth75aef482002-07-19 19:24:43 +0000680 /* OPEN_PAREN */ {1, NO_L_OPERAND},
Neil Boothad28cff2002-07-18 22:08:35 +0000681 /* CLOSE_PAREN */ {0, 0},
682 /* EOF */ {0, 0},
683 /* EQ_EQ */ {11, LEFT_ASSOC},
684 /* NOT_EQ */ {11, LEFT_ASSOC},
Neil Booth68e652752002-07-20 13:31:56 +0000685 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
686 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
Neil Boothad28cff2002-07-18 22:08:35 +0000687 /* UPLUS */ {16, NO_L_OPERAND},
688 /* UMINUS */ {16, NO_L_OPERAND}
Zack Weinbergcf00a882000-07-08 02:33:00 +0000689};
690
Per Bothner7f2935c1995-03-16 13:59:07 -0800691/* Parse and evaluate a C expression, reading from PFILE.
Kazu Hiratadf383482002-05-22 22:02:16 +0000692 Returns the truth value of the expression.
Neil Booth87ed1092002-04-28 19:42:54 +0000693
694 The implementation is an operator precedence parser, i.e. a
695 bottom-up parser, using a stack for not-yet-reduced tokens.
696
697 The stack base is op_stack, and the current stack pointer is 'top'.
698 There is a stack element for each operator (only), and the most
699 recently pushed operator is 'top->op'. An operand (value) is
700 stored in the 'value' field of the stack element of the operator
701 that precedes it. */
702bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000703_cpp_parse_expr (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800704{
Neil Booth87ed1092002-04-28 19:42:54 +0000705 struct op *top = pfile->op_stack;
706 unsigned int lex_count;
707 bool saw_leading_not, want_value = true;
Per Bothner7f2935c1995-03-16 13:59:07 -0800708
Neil Booth87ed1092002-04-28 19:42:54 +0000709 pfile->state.skip_eval = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800710
Neil Booth93c803682000-10-28 17:59:06 +0000711 /* Set up detection of #if ! defined(). */
Neil Booth6d18adb2001-07-29 17:27:57 +0000712 pfile->mi_ind_cmacro = 0;
Neil Booth87ed1092002-04-28 19:42:54 +0000713 saw_leading_not = false;
Neil Booth6d18adb2001-07-29 17:27:57 +0000714 lex_count = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000715
Neil Booth87ed1092002-04-28 19:42:54 +0000716 /* Lowest priority operator prevents further reductions. */
Zack Weinbergcf00a882000-07-08 02:33:00 +0000717 top->op = CPP_EOF;
Neil Booth4063b942000-04-02 08:27:23 +0000718
Per Bothner7f2935c1995-03-16 13:59:07 -0800719 for (;;)
720 {
Zack Weinbergcf00a882000-07-08 02:33:00 +0000721 struct op op;
Per Bothner7f2935c1995-03-16 13:59:07 -0800722
Neil Booth6d18adb2001-07-29 17:27:57 +0000723 lex_count++;
Neil Booth68e652752002-07-20 13:31:56 +0000724 op.token = cpp_get_token (pfile);
725 op.op = op.token->type;
Per Bothner7f2935c1995-03-16 13:59:07 -0800726
Per Bothner7f2935c1995-03-16 13:59:07 -0800727 switch (op.op)
728 {
Neil Booth60284a52002-04-28 23:14:56 +0000729 /* These tokens convert into values. */
Neil Boothc60e94a2001-07-19 06:12:50 +0000730 case CPP_NUMBER:
Neil Booth60284a52002-04-28 23:14:56 +0000731 case CPP_CHAR:
732 case CPP_WCHAR:
733 case CPP_NAME:
734 case CPP_HASH:
Neil Boothf8b954f2002-04-26 06:32:50 +0000735 if (!want_value)
Neil Booth60284a52002-04-28 23:14:56 +0000736 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
Neil Booth68e652752002-07-20 13:31:56 +0000737 cpp_token_as_text (pfile, op.token));
Neil Boothf8b954f2002-04-26 06:32:50 +0000738 want_value = false;
Neil Booth68e652752002-07-20 13:31:56 +0000739 top->value = eval_token (pfile, op.token);
Neil Booth9ee703132000-04-01 07:42:37 +0000740 continue;
741
Neil Booth6d18adb2001-07-29 17:27:57 +0000742 case CPP_NOT:
743 saw_leading_not = lex_count == 1;
Neil Booth6d18adb2001-07-29 17:27:57 +0000744 break;
Zack Weinbergcf00a882000-07-08 02:33:00 +0000745 case CPP_PLUS:
Neil Boothf8b954f2002-04-26 06:32:50 +0000746 if (want_value)
747 op.op = CPP_UPLUS;
748 break;
749 case CPP_MINUS:
750 if (want_value)
751 op.op = CPP_UMINUS;
752 break;
Neil Booth60284a52002-04-28 23:14:56 +0000753
Neil Boothf8b954f2002-04-26 06:32:50 +0000754 default:
Neil Booth60284a52002-04-28 23:14:56 +0000755 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
Neil Boothcd7ab832002-05-29 17:15:42 +0000756 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
Neil Booth68e652752002-07-20 13:31:56 +0000757 cpp_token_as_text (pfile, op.token));
Neil Boothf8b954f2002-04-26 06:32:50 +0000758 break;
Per Bothner7f2935c1995-03-16 13:59:07 -0800759 }
760
Neil Booth87ed1092002-04-28 19:42:54 +0000761 /* Check we have a value or operator as appropriate. */
762 if (optab[op.op].flags & NO_L_OPERAND)
Neil Booth4063b942000-04-02 08:27:23 +0000763 {
Neil Boothf8b954f2002-04-26 06:32:50 +0000764 if (!want_value)
Neil Booth60284a52002-04-28 23:14:56 +0000765 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
Neil Booth68e652752002-07-20 13:31:56 +0000766 cpp_token_as_text (pfile, op.token));
Neil Boothb22ef132000-04-03 22:33:12 +0000767 }
Neil Booth87ed1092002-04-28 19:42:54 +0000768 else if (want_value)
Neil Boothb22ef132000-04-03 22:33:12 +0000769 {
Neil Bootha09d4742004-07-04 12:57:50 +0000770 /* We want a number (or expression) and haven't got one.
771 Try to emit a specific diagnostic. */
772 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
773 SYNTAX_ERROR ("missing expression between '(' and ')'");
774
775 if (op.op == CPP_EOF && top->op == CPP_EOF)
776 SYNTAX_ERROR ("#if with no expression");
777
778 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
779 SYNTAX_ERROR2 ("operator '%s' has no right operand",
780 cpp_token_as_text (pfile, top->token));
781 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
782 /* Complain about missing paren during reduction. */;
783 else
784 SYNTAX_ERROR2 ("operator '%s' has no left operand",
785 cpp_token_as_text (pfile, op.token));
Neil Booth4063b942000-04-02 08:27:23 +0000786 }
Neil Booth87ed1092002-04-28 19:42:54 +0000787
788 top = reduce (pfile, top, op.op);
789 if (!top)
790 goto syntax_error;
791
Neil Booth60284a52002-04-28 23:14:56 +0000792 if (op.op == CPP_EOF)
793 break;
794
Neil Booth87ed1092002-04-28 19:42:54 +0000795 switch (op.op)
796 {
797 case CPP_CLOSE_PAREN:
798 continue;
Neil Booth87ed1092002-04-28 19:42:54 +0000799 case CPP_OR_OR:
Neil Booth91318902002-05-26 18:42:21 +0000800 if (!num_zerop (top->value))
Neil Booth87ed1092002-04-28 19:42:54 +0000801 pfile->state.skip_eval++;
802 break;
803 case CPP_AND_AND:
804 case CPP_QUERY:
Neil Booth91318902002-05-26 18:42:21 +0000805 if (num_zerop (top->value))
Neil Booth87ed1092002-04-28 19:42:54 +0000806 pfile->state.skip_eval++;
807 break;
808 case CPP_COLON:
Neil Booth60284a52002-04-28 23:14:56 +0000809 if (top->op != CPP_QUERY)
810 SYNTAX_ERROR (" ':' without preceding '?'");
Neil Booth91318902002-05-26 18:42:21 +0000811 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
Neil Booth87ed1092002-04-28 19:42:54 +0000812 pfile->state.skip_eval++;
813 else
814 pfile->state.skip_eval--;
815 default:
816 break;
817 }
818
Neil Boothf8b954f2002-04-26 06:32:50 +0000819 want_value = true;
Neil Booth4063b942000-04-02 08:27:23 +0000820
Mike Stump0f413021996-07-03 22:07:53 +0000821 /* Check for and handle stack overflow. */
Neil Booth87ed1092002-04-28 19:42:54 +0000822 if (++top == pfile->op_limit)
823 top = _cpp_expand_op_stack (pfile);
Kazu Hiratadf383482002-05-22 22:02:16 +0000824
Per Bothner7f2935c1995-03-16 13:59:07 -0800825 top->op = op.op;
Neil Booth68e652752002-07-20 13:31:56 +0000826 top->token = op.token;
Per Bothner7f2935c1995-03-16 13:59:07 -0800827 }
Neil Booth9ee703132000-04-01 07:42:37 +0000828
Neil Booth6d18adb2001-07-29 17:27:57 +0000829 /* The controlling macro expression is only valid if we called lex 3
830 times: <!> <defined expression> and <EOF>. push_conditional ()
831 checks that we are at top-of-file. */
832 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
833 pfile->mi_ind_cmacro = 0;
834
Neil Booth87ed1092002-04-28 19:42:54 +0000835 if (top != pfile->op_stack)
Neil Boothebef4e82002-04-14 18:42:47 +0000836 {
John David Anglin0527bc42003-11-01 22:56:54 +0000837 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in #if");
Neil Booth4063b942000-04-02 08:27:23 +0000838 syntax_error:
Neil Booth87ed1092002-04-28 19:42:54 +0000839 return false; /* Return false on syntax error. */
Neil Booth4063b942000-04-02 08:27:23 +0000840 }
Neil Booth9ee703132000-04-01 07:42:37 +0000841
Neil Booth91318902002-05-26 18:42:21 +0000842 return !num_zerop (top->value);
Neil Booth87ed1092002-04-28 19:42:54 +0000843}
844
845/* Reduce the operator / value stack if possible, in preparation for
846 pushing operator OP. Returns NULL on error, otherwise the top of
847 the stack. */
848static struct op *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000849reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
Neil Booth87ed1092002-04-28 19:42:54 +0000850{
851 unsigned int prio;
852
Neil Booth91318902002-05-26 18:42:21 +0000853 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
854 {
855 bad_op:
John David Anglin0527bc42003-11-01 22:56:54 +0000856 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
Neil Booth91318902002-05-26 18:42:21 +0000857 return 0;
858 }
859
Neil Booth87ed1092002-04-28 19:42:54 +0000860 if (op == CPP_OPEN_PAREN)
861 return top;
862
863 /* Decrement the priority of left-associative operators to force a
864 reduction with operators of otherwise equal priority. */
865 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
866 while (prio < optab[top->op].prio)
867 {
Neil Booth68e652752002-07-20 13:31:56 +0000868 if (CPP_OPTION (pfile, warn_num_sign_change)
869 && optab[top->op].flags & CHECK_PROMOTION)
870 check_promotion (pfile, top);
871
Neil Booth75aef482002-07-19 19:24:43 +0000872 switch (top->op)
873 {
874 case CPP_UPLUS:
875 case CPP_UMINUS:
876 case CPP_NOT:
877 case CPP_COMPL:
878 top[-1].value = num_unary_op (pfile, top->value, top->op);
879 break;
Neil Booth91318902002-05-26 18:42:21 +0000880
Neil Booth75aef482002-07-19 19:24:43 +0000881 case CPP_PLUS:
882 case CPP_MINUS:
883 case CPP_RSHIFT:
884 case CPP_LSHIFT:
Neil Booth75aef482002-07-19 19:24:43 +0000885 case CPP_COMMA:
886 top[-1].value = num_binary_op (pfile, top[-1].value,
887 top->value, top->op);
888 break;
Neil Booth91318902002-05-26 18:42:21 +0000889
Neil Booth75aef482002-07-19 19:24:43 +0000890 case CPP_GREATER:
891 case CPP_LESS:
892 case CPP_GREATER_EQ:
893 case CPP_LESS_EQ:
894 top[-1].value
895 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
896 break;
Neil Booth91318902002-05-26 18:42:21 +0000897
Neil Booth75aef482002-07-19 19:24:43 +0000898 case CPP_EQ_EQ:
899 case CPP_NOT_EQ:
900 top[-1].value
901 = num_equality_op (pfile, top[-1].value, top->value, top->op);
902 break;
Neil Boothad28cff2002-07-18 22:08:35 +0000903
Neil Booth75aef482002-07-19 19:24:43 +0000904 case CPP_AND:
905 case CPP_OR:
906 case CPP_XOR:
907 top[-1].value
908 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
909 break;
Neil Boothad28cff2002-07-18 22:08:35 +0000910
Neil Booth75aef482002-07-19 19:24:43 +0000911 case CPP_MULT:
912 top[-1].value = num_mul (pfile, top[-1].value, top->value);
913 break;
Neil Boothad28cff2002-07-18 22:08:35 +0000914
Neil Booth75aef482002-07-19 19:24:43 +0000915 case CPP_DIV:
916 case CPP_MOD:
917 top[-1].value = num_div_op (pfile, top[-1].value,
918 top->value, top->op);
919 break;
Neil Boothad28cff2002-07-18 22:08:35 +0000920
Neil Booth75aef482002-07-19 19:24:43 +0000921 case CPP_OR_OR:
922 top--;
923 if (!num_zerop (top->value))
924 pfile->state.skip_eval--;
925 top->value.low = (!num_zerop (top->value)
926 || !num_zerop (top[1].value));
927 top->value.high = 0;
928 top->value.unsignedp = false;
929 top->value.overflow = false;
930 continue;
931
932 case CPP_AND_AND:
933 top--;
934 if (num_zerop (top->value))
935 pfile->state.skip_eval--;
936 top->value.low = (!num_zerop (top->value)
937 && !num_zerop (top[1].value));
938 top->value.high = 0;
939 top->value.unsignedp = false;
940 top->value.overflow = false;
941 continue;
942
943 case CPP_OPEN_PAREN:
944 if (op != CPP_CLOSE_PAREN)
945 {
John David Anglin0527bc42003-11-01 22:56:54 +0000946 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in expression");
Neil Booth75aef482002-07-19 19:24:43 +0000947 return 0;
948 }
949 top--;
950 top->value = top[1].value;
951 return top;
952
953 case CPP_COLON:
954 top -= 2;
955 if (!num_zerop (top->value))
956 {
Neil Booth91318902002-05-26 18:42:21 +0000957 pfile->state.skip_eval--;
Neil Booth75aef482002-07-19 19:24:43 +0000958 top->value = top[1].value;
959 }
960 else
961 top->value = top[2].value;
962 top->value.unsignedp = (top[1].value.unsignedp
963 || top[2].value.unsignedp);
964 continue;
Neil Booth91318902002-05-26 18:42:21 +0000965
Neil Booth75aef482002-07-19 19:24:43 +0000966 case CPP_QUERY:
John David Anglin0527bc42003-11-01 22:56:54 +0000967 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
Neil Booth75aef482002-07-19 19:24:43 +0000968 return 0;
Neil Booth91318902002-05-26 18:42:21 +0000969
Neil Booth75aef482002-07-19 19:24:43 +0000970 default:
971 goto bad_op;
972 }
Neil Boothad28cff2002-07-18 22:08:35 +0000973
974 top--;
Neil Booth91318902002-05-26 18:42:21 +0000975 if (top->value.overflow && !pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +0000976 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth91318902002-05-26 18:42:21 +0000977 "integer overflow in preprocessor expression");
Neil Booth87ed1092002-04-28 19:42:54 +0000978 }
979
980 if (op == CPP_CLOSE_PAREN)
981 {
John David Anglin0527bc42003-11-01 22:56:54 +0000982 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
Neil Booth87ed1092002-04-28 19:42:54 +0000983 return 0;
984 }
985
986 return top;
987}
988
989/* Returns the position of the old top of stack after expansion. */
990struct op *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000991_cpp_expand_op_stack (cpp_reader *pfile)
Neil Booth87ed1092002-04-28 19:42:54 +0000992{
Neil Booth32fa4562002-05-09 22:27:31 +0000993 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
994 size_t new_size = old_size * 2 + 20;
Neil Booth87ed1092002-04-28 19:42:54 +0000995
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000996 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
Neil Booth32fa4562002-05-09 22:27:31 +0000997 pfile->op_limit = pfile->op_stack + new_size;
Neil Booth87ed1092002-04-28 19:42:54 +0000998
Neil Booth32fa4562002-05-09 22:27:31 +0000999 return pfile->op_stack + old_size;
Per Bothner7f2935c1995-03-16 13:59:07 -08001000}
Neil Booth91318902002-05-26 18:42:21 +00001001
Neil Booth68e652752002-07-20 13:31:56 +00001002/* Emits a warning if the effective sign of either operand of OP
1003 changes because of integer promotions. */
1004static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001005check_promotion (cpp_reader *pfile, const struct op *op)
Neil Booth68e652752002-07-20 13:31:56 +00001006{
1007 if (op->value.unsignedp == op[-1].value.unsignedp)
1008 return;
1009
1010 if (op->value.unsignedp)
1011 {
1012 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
John David Anglin0527bc42003-11-01 22:56:54 +00001013 cpp_error (pfile, CPP_DL_WARNING,
Neil Booth68e652752002-07-20 13:31:56 +00001014 "the left operand of \"%s\" changes sign when promoted",
1015 cpp_token_as_text (pfile, op->token));
1016 }
1017 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
John David Anglin0527bc42003-11-01 22:56:54 +00001018 cpp_error (pfile, CPP_DL_WARNING,
Neil Booth68e652752002-07-20 13:31:56 +00001019 "the right operand of \"%s\" changes sign when promoted",
1020 cpp_token_as_text (pfile, op->token));
1021}
1022
Neil Booth91318902002-05-26 18:42:21 +00001023/* Clears the unused high order bits of the number pointed to by PNUM. */
1024static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001025num_trim (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001026{
1027 if (precision > PART_PRECISION)
1028 {
1029 precision -= PART_PRECISION;
1030 if (precision < PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001031 num.high &= ((cpp_num_part) 1 << precision) - 1;
Neil Booth91318902002-05-26 18:42:21 +00001032 }
1033 else
1034 {
1035 if (precision < PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001036 num.low &= ((cpp_num_part) 1 << precision) - 1;
Neil Booth91318902002-05-26 18:42:21 +00001037 num.high = 0;
1038 }
1039
1040 return num;
1041}
1042
1043/* True iff A (presumed signed) >= 0. */
1044static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001045num_positive (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001046{
1047 if (precision > PART_PRECISION)
1048 {
1049 precision -= PART_PRECISION;
Neil Booth359b0be2002-05-28 05:44:06 +00001050 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
Neil Booth91318902002-05-26 18:42:21 +00001051 }
1052
Neil Booth359b0be2002-05-28 05:44:06 +00001053 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
Neil Booth91318902002-05-26 18:42:21 +00001054}
1055
Neil Boothceeedfc2002-06-02 19:37:34 +00001056/* Sign extend a number, with PRECISION significant bits and all
1057 others assumed clear, to fill out a cpp_num structure. */
1058cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001059cpp_num_sign_extend (cpp_num num, size_t precision)
Neil Boothceeedfc2002-06-02 19:37:34 +00001060{
1061 if (!num.unsignedp)
1062 {
1063 if (precision > PART_PRECISION)
1064 {
1065 precision -= PART_PRECISION;
1066 if (precision < PART_PRECISION
1067 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1068 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1069 }
1070 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1071 {
1072 if (precision < PART_PRECISION)
1073 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1074 num.high = ~(cpp_num_part) 0;
1075 }
1076 }
1077
1078 return num;
1079}
1080
Neil Booth91318902002-05-26 18:42:21 +00001081/* Returns the negative of NUM. */
1082static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001083num_negate (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001084{
1085 cpp_num copy;
1086
1087 copy = num;
1088 num.high = ~num.high;
1089 num.low = ~num.low;
1090 if (++num.low == 0)
1091 num.high++;
1092 num = num_trim (num, precision);
1093 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1094
1095 return num;
1096}
1097
1098/* Returns true if A >= B. */
1099static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001100num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001101{
1102 bool unsignedp;
1103
1104 unsignedp = pa.unsignedp || pb.unsignedp;
1105
1106 if (!unsignedp)
1107 {
1108 /* Both numbers have signed type. If they are of different
1109 sign, the answer is the sign of A. */
1110 unsignedp = num_positive (pa, precision);
1111
1112 if (unsignedp != num_positive (pb, precision))
1113 return unsignedp;
1114
1115 /* Otherwise we can do an unsigned comparison. */
1116 }
1117
1118 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1119}
1120
1121/* Returns LHS OP RHS, where OP is a bit-wise operation. */
1122static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001123num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1124 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001125{
1126 lhs.overflow = false;
1127 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1128
1129 /* As excess precision is zeroed, there is no need to num_trim () as
1130 these operations cannot introduce a set bit there. */
1131 if (op == CPP_AND)
1132 {
1133 lhs.low &= rhs.low;
1134 lhs.high &= rhs.high;
1135 }
1136 else if (op == CPP_OR)
1137 {
1138 lhs.low |= rhs.low;
1139 lhs.high |= rhs.high;
1140 }
1141 else
1142 {
1143 lhs.low ^= rhs.low;
1144 lhs.high ^= rhs.high;
1145 }
1146
1147 return lhs;
1148}
1149
1150/* Returns LHS OP RHS, where OP is an inequality. */
1151static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001152num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1153 enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001154{
1155 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1156
1157 if (op == CPP_GREATER_EQ)
1158 lhs.low = gte;
1159 else if (op == CPP_LESS)
1160 lhs.low = !gte;
1161 else if (op == CPP_GREATER)
1162 lhs.low = gte && !num_eq (lhs, rhs);
1163 else /* CPP_LESS_EQ. */
1164 lhs.low = !gte || num_eq (lhs, rhs);
1165
1166 lhs.high = 0;
1167 lhs.overflow = false;
1168 lhs.unsignedp = false;
1169 return lhs;
1170}
1171
1172/* Returns LHS OP RHS, where OP is == or !=. */
1173static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001174num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1175 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001176{
Jason Merrill97459792002-06-07 09:29:17 -04001177 /* Work around a 3.0.4 bug; see PR 6950. */
1178 bool eq = num_eq (lhs, rhs);
Neil Booth91318902002-05-26 18:42:21 +00001179 if (op == CPP_NOT_EQ)
Jason Merrill97459792002-06-07 09:29:17 -04001180 eq = !eq;
1181 lhs.low = eq;
Neil Booth91318902002-05-26 18:42:21 +00001182 lhs.high = 0;
1183 lhs.overflow = false;
1184 lhs.unsignedp = false;
1185 return lhs;
1186}
1187
1188/* Shift NUM, of width PRECISION, right by N bits. */
1189static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001190num_rshift (cpp_num num, size_t precision, size_t n)
Neil Booth91318902002-05-26 18:42:21 +00001191{
1192 cpp_num_part sign_mask;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001193 bool x = num_positive (num, precision);
Neil Booth91318902002-05-26 18:42:21 +00001194
Diego Novillo6de9cd92004-05-13 02:41:07 -04001195 if (num.unsignedp || x)
Neil Booth91318902002-05-26 18:42:21 +00001196 sign_mask = 0;
1197 else
1198 sign_mask = ~(cpp_num_part) 0;
1199
1200 if (n >= precision)
1201 num.high = num.low = sign_mask;
1202 else
1203 {
1204 /* Sign-extend. */
1205 if (precision < PART_PRECISION)
1206 num.high = sign_mask, num.low |= sign_mask << precision;
1207 else if (precision < 2 * PART_PRECISION)
1208 num.high |= sign_mask << (precision - PART_PRECISION);
1209
1210 if (n >= PART_PRECISION)
1211 {
1212 n -= PART_PRECISION;
1213 num.low = num.high;
1214 num.high = sign_mask;
1215 }
1216
1217 if (n)
1218 {
1219 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1220 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1221 }
1222 }
1223
1224 num = num_trim (num, precision);
1225 num.overflow = false;
1226 return num;
1227}
1228
1229/* Shift NUM, of width PRECISION, left by N bits. */
1230static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001231num_lshift (cpp_num num, size_t precision, size_t n)
Neil Booth91318902002-05-26 18:42:21 +00001232{
1233 if (n >= precision)
1234 {
1235 num.overflow = !num.unsignedp && !num_zerop (num);
1236 num.high = num.low = 0;
1237 }
1238 else
1239 {
1240 cpp_num orig, maybe_orig;
1241 size_t m = n;
1242
1243 orig = num;
1244 if (m >= PART_PRECISION)
1245 {
1246 m -= PART_PRECISION;
1247 num.high = num.low;
1248 num.low = 0;
1249 }
1250 if (m)
1251 {
1252 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1253 num.low <<= m;
1254 }
1255 num = num_trim (num, precision);
1256
1257 if (num.unsignedp)
1258 num.overflow = false;
1259 else
1260 {
1261 maybe_orig = num_rshift (num, precision, n);
1262 num.overflow = !num_eq (orig, maybe_orig);
1263 }
1264 }
1265
1266 return num;
1267}
1268
1269/* The four unary operators: +, -, ! and ~. */
1270static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001271num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001272{
1273 switch (op)
1274 {
1275 case CPP_UPLUS:
Neil Booth75aef482002-07-19 19:24:43 +00001276 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +00001277 cpp_error (pfile, CPP_DL_WARNING,
Neil Booth91318902002-05-26 18:42:21 +00001278 "traditional C rejects the unary plus operator");
1279 num.overflow = false;
1280 break;
1281
1282 case CPP_UMINUS:
1283 num = num_negate (num, CPP_OPTION (pfile, precision));
1284 break;
1285
1286 case CPP_COMPL:
1287 num.high = ~num.high;
1288 num.low = ~num.low;
1289 num = num_trim (num, CPP_OPTION (pfile, precision));
1290 num.overflow = false;
1291 break;
1292
1293 default: /* case CPP_NOT: */
1294 num.low = num_zerop (num);
1295 num.high = 0;
1296 num.overflow = false;
1297 num.unsignedp = false;
1298 break;
1299 }
1300
1301 return num;
1302}
1303
1304/* The various binary operators. */
1305static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001306num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001307{
1308 cpp_num result;
1309 size_t precision = CPP_OPTION (pfile, precision);
Neil Booth91318902002-05-26 18:42:21 +00001310 size_t n;
1311
1312 switch (op)
1313 {
1314 /* Shifts. */
1315 case CPP_LSHIFT:
1316 case CPP_RSHIFT:
1317 if (!rhs.unsignedp && !num_positive (rhs, precision))
1318 {
1319 /* A negative shift is a positive shift the other way. */
1320 if (op == CPP_LSHIFT)
1321 op = CPP_RSHIFT;
1322 else
1323 op = CPP_LSHIFT;
1324 rhs = num_negate (rhs, precision);
1325 }
1326 if (rhs.high)
1327 n = ~0; /* Maximal. */
1328 else
1329 n = rhs.low;
1330 if (op == CPP_LSHIFT)
1331 lhs = num_lshift (lhs, precision, n);
1332 else
1333 lhs = num_rshift (lhs, precision, n);
1334 break;
1335
Neil Booth91318902002-05-26 18:42:21 +00001336 /* Arithmetic. */
1337 case CPP_MINUS:
1338 rhs = num_negate (rhs, precision);
1339 case CPP_PLUS:
1340 result.low = lhs.low + rhs.low;
1341 result.high = lhs.high + rhs.high;
1342 if (result.low < lhs.low)
1343 result.high++;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001344 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1345 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +00001346
1347 result = num_trim (result, precision);
Diego Novillo6de9cd92004-05-13 02:41:07 -04001348 if (!result.unsignedp)
Neil Booth91318902002-05-26 18:42:21 +00001349 {
1350 bool lhsp = num_positive (lhs, precision);
1351 result.overflow = (lhsp == num_positive (rhs, precision)
1352 && lhsp != num_positive (result, precision));
1353 }
1354 return result;
1355
1356 /* Comma. */
1357 default: /* case CPP_COMMA: */
Joseph Myers32e8aa92004-02-11 23:50:45 +00001358 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1359 || !pfile->state.skip_eval))
John David Anglin0527bc42003-11-01 22:56:54 +00001360 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth91318902002-05-26 18:42:21 +00001361 "comma operator in operand of #if");
1362 lhs = rhs;
1363 break;
1364 }
1365
1366 return lhs;
1367}
1368
1369/* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1370 cannot overflow. */
1371static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001372num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
Neil Booth91318902002-05-26 18:42:21 +00001373{
1374 cpp_num result;
1375 cpp_num_part middle[2], temp;
1376
1377 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1378 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1379
1380 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1381 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1382
1383 temp = result.low;
1384 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1385 if (result.low < temp)
1386 result.high++;
1387
1388 temp = result.low;
1389 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1390 if (result.low < temp)
1391 result.high++;
1392
1393 result.high += HIGH_PART (middle[0]);
1394 result.high += HIGH_PART (middle[1]);
Diego Novillo6de9cd92004-05-13 02:41:07 -04001395 result.unsignedp = true;
1396 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +00001397
1398 return result;
1399}
1400
1401/* Multiply two preprocessing numbers. */
1402static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001403num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
Neil Booth91318902002-05-26 18:42:21 +00001404{
1405 cpp_num result, temp;
1406 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1407 bool overflow, negate = false;
1408 size_t precision = CPP_OPTION (pfile, precision);
1409
1410 /* Prepare for unsigned multiplication. */
1411 if (!unsignedp)
1412 {
1413 if (!num_positive (lhs, precision))
1414 negate = !negate, lhs = num_negate (lhs, precision);
1415 if (!num_positive (rhs, precision))
1416 negate = !negate, rhs = num_negate (rhs, precision);
1417 }
1418
1419 overflow = lhs.high && rhs.high;
1420 result = num_part_mul (lhs.low, rhs.low);
1421
1422 temp = num_part_mul (lhs.high, rhs.low);
1423 result.high += temp.low;
1424 if (temp.high)
1425 overflow = true;
1426
1427 temp = num_part_mul (lhs.low, rhs.high);
1428 result.high += temp.low;
1429 if (temp.high)
1430 overflow = true;
1431
1432 temp.low = result.low, temp.high = result.high;
1433 result = num_trim (result, precision);
1434 if (!num_eq (result, temp))
1435 overflow = true;
1436
1437 if (negate)
1438 result = num_negate (result, precision);
1439
1440 if (unsignedp)
1441 result.overflow = false;
1442 else
1443 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1444 && !num_zerop (result));
1445 result.unsignedp = unsignedp;
1446
1447 return result;
1448}
1449
1450/* Divide two preprocessing numbers, returning the answer or the
1451 remainder depending upon OP. */
1452static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001453num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001454{
1455 cpp_num result, sub;
1456 cpp_num_part mask;
1457 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1458 bool negate = false, lhs_neg = false;
1459 size_t i, precision = CPP_OPTION (pfile, precision);
1460
1461 /* Prepare for unsigned division. */
1462 if (!unsignedp)
1463 {
1464 if (!num_positive (lhs, precision))
1465 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1466 if (!num_positive (rhs, precision))
1467 negate = !negate, rhs = num_negate (rhs, precision);
1468 }
1469
1470 /* Find the high bit. */
1471 if (rhs.high)
1472 {
1473 i = precision - 1;
Neil Booth359b0be2002-05-28 05:44:06 +00001474 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
Neil Booth91318902002-05-26 18:42:21 +00001475 for (; ; i--, mask >>= 1)
1476 if (rhs.high & mask)
1477 break;
1478 }
1479 else if (rhs.low)
1480 {
1481 if (precision > PART_PRECISION)
1482 i = precision - PART_PRECISION - 1;
1483 else
1484 i = precision - 1;
Neil Booth359b0be2002-05-28 05:44:06 +00001485 mask = (cpp_num_part) 1 << i;
Neil Booth91318902002-05-26 18:42:21 +00001486 for (; ; i--, mask >>= 1)
1487 if (rhs.low & mask)
1488 break;
1489 }
1490 else
1491 {
Neil Booth75aef482002-07-19 19:24:43 +00001492 if (!pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +00001493 cpp_error (pfile, CPP_DL_ERROR, "division by zero in #if");
Neil Booth91318902002-05-26 18:42:21 +00001494 return lhs;
1495 }
1496
Kazu Hiratada7d8302002-09-22 02:03:17 +00001497 /* First nonzero bit of RHS is bit I. Do naive division by
Neil Booth91318902002-05-26 18:42:21 +00001498 shifting the RHS fully left, and subtracting from LHS if LHS is
1499 at least as big, and then repeating but with one less shift.
1500 This is not very efficient, but is easy to understand. */
1501
1502 rhs.unsignedp = true;
1503 lhs.unsignedp = true;
1504 i = precision - i - 1;
1505 sub = num_lshift (rhs, precision, i);
1506
1507 result.high = result.low = 0;
1508 for (;;)
1509 {
1510 if (num_greater_eq (lhs, sub, precision))
1511 {
1512 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1513 if (i >= PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001514 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
Neil Booth91318902002-05-26 18:42:21 +00001515 else
Neil Booth359b0be2002-05-28 05:44:06 +00001516 result.low |= (cpp_num_part) 1 << i;
Neil Booth91318902002-05-26 18:42:21 +00001517 }
1518 if (i-- == 0)
1519 break;
1520 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1521 sub.high >>= 1;
1522 }
1523
1524 /* We divide so that the remainder has the sign of the LHS. */
1525 if (op == CPP_DIV)
1526 {
1527 result.unsignedp = unsignedp;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001528 result.overflow = false;
1529 if (!unsignedp)
Neil Booth91318902002-05-26 18:42:21 +00001530 {
1531 if (negate)
1532 result = num_negate (result, precision);
Eric Christopher22a8a522007-05-02 21:57:50 +00001533 result.overflow = (num_positive (result, precision) ^ !negate
1534 && !num_zerop (result));
Neil Booth91318902002-05-26 18:42:21 +00001535 }
1536
1537 return result;
1538 }
1539
1540 /* CPP_MOD. */
1541 lhs.unsignedp = unsignedp;
1542 lhs.overflow = false;
1543 if (lhs_neg)
1544 lhs = num_negate (lhs, precision);
1545
1546 return lhs;
1547}