blob: 9e89dd9574af1fe47fb2d05a131ca1099ee8ceff [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{
Uros Bizjakc77cd3d2007-07-03 07:53:58 +020085 size_t f, l, w, q, i, d;
Chao-ying Fuac6b1c62007-08-30 23:05:17 +000086 size_t r, k, u, h;
Uros Bizjakc77cd3d2007-07-03 07:53:58 +020087
88 f = l = w = q = i = d = 0;
Chao-ying Fuac6b1c62007-08-30 23:05:17 +000089 r = k = u = h = 0;
Zack Weinbergcf00a882000-07-08 02:33:00 +000090
Neil Boothcd7ab832002-05-29 17:15:42 +000091 while (len--)
92 switch (s[len])
93 {
Chao-ying Fuac6b1c62007-08-30 23:05:17 +000094 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;
Janis Johnson30e04922007-05-14 23:43:07 +000098 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++;
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000107 /* 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;
Janis Johnson30e04922007-05-14 23:43:07 +0000110 break;
Uros Bizjakc77cd3d2007-07-03 07:53:58 +0200111 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;
Neil Boothcd7ab832002-05-29 17:15:42 +0000121 case 'i': case 'I':
122 case 'j': case 'J': i++; break;
Janis Johnson30e04922007-05-14 23:43:07 +0000123 case 'd': case 'D': d++; break;
Neil Boothcd7ab832002-05-29 17:15:42 +0000124 default:
125 return 0;
126 }
Zack Weinbergcf00a882000-07-08 02:33:00 +0000127
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000128 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)
Neil Boothcd7ab832002-05-29 17:15:42 +0000156 return 0;
Zack Weinbergcf00a882000-07-08 02:33:00 +0000157
Jon Grimmad6ed772005-12-06 23:13:15 +0000158 /* Allow dd, df, dl suffixes for decimal float constants. */
159 if (d && ((d + f + l != 2) || i))
160 return 0;
161
Neil Boothcd7ab832002-05-29 17:15:42 +0000162 return ((i ? CPP_N_IMAGINARY : 0)
163 | (f ? CPP_N_SMALL :
Uros Bizjakc77cd3d2007-07-03 07:53:58 +0200164 l ? CPP_N_LARGE :
165 w ? CPP_N_MD_W :
166 q ? CPP_N_MD_Q : CPP_N_MEDIUM)
Jon Grimmad6ed772005-12-06 23:13:15 +0000167 | (d ? CPP_N_DFLOAT : 0));
Neil Boothcd7ab832002-05-29 17:15:42 +0000168}
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. */
173static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000174interpret_int_suffix (const uchar *s, size_t len)
Neil Boothcd7ab832002-05-29 17:15:42 +0000175{
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. */
207unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000208cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
Neil Boothcd7ab832002-05-29 17:15:42 +0000209{
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. */
Michael Matz7f1fc382003-03-31 15:50:53 +0000232 if ((*str == 'x' || *str == 'X')
233 && (str[1] == '.' || ISXDIGIT (str[1])))
Neil Boothcd7ab832002-05-29 17:15:42 +0000234 {
235 radix = 16;
236 str++;
237 }
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000238 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
239 {
240 radix = 2;
241 str++;
242 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000243 }
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
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000277 /* 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
Neil Boothcd7ab832002-05-29 17:15:42 +0000297 if (float_flag != NOT_FLOAT && radix == 8)
298 radix = 10;
299
300 if (max_digit >= radix)
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000301 {
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 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000307
308 if (float_flag != NOT_FLOAT)
309 {
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000310 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
Neil Boothcd7ab832002-05-29 17:15:42 +0000317 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
John David Anglin0527bc42003-11-01 22:56:54 +0000318 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothcd7ab832002-05-29 17:15:42 +0000319 "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 {
John David Anglin0527bc42003-11-01 22:56:54 +0000340 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000341 "invalid suffix \"%.*s\" on floating constant",
Andreas Jaeger91b12472002-06-01 16:11:45 +0200342 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000343 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))
John David Anglin0527bc42003-11-01 22:56:54 +0000350 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothcd7ab832002-05-29 17:15:42 +0000351 "traditional C rejects the \"%.*s\" suffix",
Andreas Jaeger91b12472002-06-01 16:11:45 +0200352 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000353
Jon Grimmad6ed772005-12-06 23:13:15 +0000354 /* 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
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000363 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
Janis Johnson5a6bb572007-05-14 23:45:40 +0000367 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
368 cpp_error (pfile, CPP_DL_PEDWARN,
369 "decimal float constants are a GCC extension");
370
Neil Boothcd7ab832002-05-29 17:15:42 +0000371 result |= CPP_N_FLOATING;
372 }
373 else
374 {
375 result = interpret_int_suffix (str, limit - str);
376 if (result == 0)
377 {
John David Anglin0527bc42003-11-01 22:56:54 +0000378 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000379 "invalid suffix \"%.*s\" on integer constant",
Andreas Jaeger91b12472002-06-01 16:11:45 +0200380 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000381 return CPP_N_INVALID;
382 }
383
Zack Weinberg56da7202002-08-02 04:18:16 +0000384 /* 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)))
John David Anglin0527bc42003-11-01 22:56:54 +0000392 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinberg56da7202002-08-02 04:18:16 +0000393 "traditional C rejects the \"%.*s\" suffix",
394 (int) (limit - str), str);
395 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000396
397 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
398 && ! CPP_OPTION (pfile, c99)
399 && CPP_OPTION (pfile, warn_long_long))
John David Anglin0527bc42003-11-01 22:56:54 +0000400 cpp_error (pfile, CPP_DL_PEDWARN,
401 "use of C99 long long integer constant");
Neil Boothcd7ab832002-05-29 17:15:42 +0000402
403 result |= CPP_N_INTEGER;
404 }
405
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000406 syntax_ok:
Neil Boothcd7ab832002-05-29 17:15:42 +0000407 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000408 cpp_error (pfile, CPP_DL_PEDWARN,
409 "imaginary constants are a GCC extension");
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000410 if (radix == 2 && CPP_PEDANTIC (pfile))
411 cpp_error (pfile, CPP_DL_PEDWARN,
412 "binary constants are a GCC extension");
Neil Boothcd7ab832002-05-29 17:15:42 +0000413
414 if (radix == 10)
415 result |= CPP_N_DECIMAL;
416 else if (radix == 16)
417 result |= CPP_N_HEX;
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000418 else if (radix == 2)
419 result |= CPP_N_BINARY;
Neil Boothcd7ab832002-05-29 17:15:42 +0000420 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,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000433 because the preprocessor doesn't need it and we don't want to
434 drag in GCC's floating point emulator. */
Neil Boothcd7ab832002-05-29 17:15:42 +0000435cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000436cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
437 unsigned int type)
Neil Boothcd7ab832002-05-29 17:15:42 +0000438{
439 const uchar *p, *end;
440 cpp_num result;
441
442 result.low = 0;
443 result.high = 0;
Neil Booth23ff0222002-07-17 17:27:14 +0000444 result.unsignedp = !!(type & CPP_N_UNSIGNED);
445 result.overflow = false;
Neil Boothcd7ab832002-05-29 17:15:42 +0000446
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 }
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000470 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
471 {
472 base = 2;
473 p += 2;
474 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000475
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)
John David Anglin0527bc42003-11-01 22:56:54 +0000504 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothcd7ab832002-05-29 17:15:42 +0000505 "integer constant is too large for its type");
Neil Booth017acb42002-06-20 20:34:19 +0000506 /* If too big to be signed, consider it unsigned. Only warn for
507 decimal numbers. Traditional numbers were always signed (but
Kazu Hirata8d9afc4e2002-09-16 11:42:00 +0000508 we still honor an explicit U suffix); but we only have
Neil Boothcd98faa2002-07-09 22:21:37 +0000509 traditional semantics in directives. */
Neil Booth017acb42002-06-20 20:34:19 +0000510 else if (!result.unsignedp
Neil Boothcd98faa2002-07-09 22:21:37 +0000511 && !(CPP_OPTION (pfile, traditional)
512 && pfile->state.in_directive)
Neil Booth017acb42002-06-20 20:34:19 +0000513 && !num_positive (result, precision))
Neil Boothcd7ab832002-05-29 17:15:42 +0000514 {
Neil Boothcd7ab832002-05-29 17:15:42 +0000515 if (base == 10)
John David Anglin0527bc42003-11-01 22:56:54 +0000516 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothcd7ab832002-05-29 17:15:42 +0000517 "integer constant is so large that it is unsigned");
Neil Booth23ff0222002-07-17 17:27:14 +0000518 result.unsignedp = true;
Neil Boothcd7ab832002-05-29 17:15:42 +0000519 }
520 }
521
522 return result;
523}
Zack Weinbergcf00a882000-07-08 02:33:00 +0000524
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000525/* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
Neil Booth91318902002-05-26 18:42:21 +0000526static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000527append_digit (cpp_num num, int digit, int base, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +0000528{
529 cpp_num result;
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000530 unsigned int shift;
Neil Booth91318902002-05-26 18:42:21 +0000531 bool overflow;
532 cpp_num_part add_high, add_low;
533
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000534 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
Neil Booth91318902002-05-26 18:42:21 +0000535 need to worry about add_high overflowing. */
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000536 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 }
Neil Booth23ff0222002-07-17 17:27:14 +0000549 overflow = !!(num.high >> (PART_PRECISION - shift));
Neil Booth91318902002-05-26 18:42:21 +0000550 result.high = num.high << shift;
551 result.low = num.low << shift;
552 result.high |= num.low >> (PART_PRECISION - shift);
Diego Novillo6de9cd92004-05-13 02:41:07 -0400553 result.unsignedp = num.unsignedp;
Neil Booth91318902002-05-26 18:42:21 +0000554
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;
Eric Christopher22a8a522007-05-02 21:57:50 +0000566
Neil Booth91318902002-05-26 18:42:21 +0000567 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;
Diego Novillo6de9cd92004-05-13 02:41:07 -0400574 result.overflow = overflow;
Neil Booth91318902002-05-26 18:42:21 +0000575
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))
Diego Novillo6de9cd92004-05-13 02:41:07 -0400582 result.overflow = true;
Neil Booth91318902002-05-26 18:42:21 +0000583
Neil Booth91318902002-05-26 18:42:21 +0000584 return result;
585}
586
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000587/* Handle meeting "defined" in a preprocessor expression. */
Neil Booth91318902002-05-26 18:42:21 +0000588static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000589parse_defined (cpp_reader *pfile)
Zack Weinbergba412f12000-03-01 00:57:09 +0000590{
Neil Booth91318902002-05-26 18:42:21 +0000591 cpp_num result;
Neil Booth93c803682000-10-28 17:59:06 +0000592 int paren = 0;
593 cpp_hashnode *node = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000594 const cpp_token *token;
Neil Booth63d75002001-11-05 22:26:13 +0000595 cpp_context *initial_context = pfile->context;
Zack Weinbergcf00a882000-07-08 02:33:00 +0000596
Neil Booth93c803682000-10-28 17:59:06 +0000597 /* Don't expand macros. */
598 pfile->state.prevent_expansion++;
599
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000600 token = cpp_get_token (pfile);
601 if (token->type == CPP_OPEN_PAREN)
Zack Weinbergcf00a882000-07-08 02:33:00 +0000602 {
603 paren = 1;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000604 token = cpp_get_token (pfile);
Zack Weinbergcf00a882000-07-08 02:33:00 +0000605 }
606
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000607 if (token->type == CPP_NAME)
Zack Weinberg041c3192000-07-04 01:58:21 +0000608 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000609 node = token->val.node;
610 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
Neil Booth93c803682000-10-28 17:59:06 +0000611 {
John David Anglin0527bc42003-11-01 22:56:54 +0000612 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000613 node = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000614 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000615 }
Neil Booth93c803682000-10-28 17:59:06 +0000616 else
Neil Booth3c8465d2001-02-06 20:07:07 +0000617 {
John David Anglin0527bc42003-11-01 22:56:54 +0000618 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000619 "operator \"defined\" requires an identifier");
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000620 if (token->flags & NAMED_OP)
Neil Booth3c8465d2001-02-06 20:07:07 +0000621 {
622 cpp_token op;
623
624 op.flags = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000625 op.type = token->type;
John David Anglin0527bc42003-11-01 22:56:54 +0000626 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth3c8465d2001-02-06 20:07:07 +0000627 "(\"%s\" is an alternative token for \"%s\" in C++)",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000628 cpp_token_as_text (pfile, token),
Neil Booth3c8465d2001-02-06 20:07:07 +0000629 cpp_token_as_text (pfile, &op));
630 }
631 }
Neil Booth93c803682000-10-28 17:59:06 +0000632
Neil Booth91318902002-05-26 18:42:21 +0000633 if (node)
Neil Booth93c803682000-10-28 17:59:06 +0000634 {
Neil Booth335d03e2003-08-03 12:23:46 +0000635 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000636 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000637 "this use of \"defined\" may not be portable");
Neil Booth63d75002001-11-05 22:26:13 +0000638
Neil Bootha69cbaa2002-07-23 22:57:49 +0000639 _cpp_mark_macro_used (node);
Joseph Myers93d45d92008-04-02 20:42:53 +0100640 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 }
Neil Bootha69cbaa2002-07-23 22:57:49 +0000654
Neil Booth6d18adb2001-07-29 17:27:57 +0000655 /* 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;
Neil Booth93c803682000-10-28 17:59:06 +0000658 }
659
660 pfile->state.prevent_expansion--;
Neil Booth91318902002-05-26 18:42:21 +0000661
Neil Booth23ff0222002-07-17 17:27:14 +0000662 result.unsignedp = false;
Neil Booth91318902002-05-26 18:42:21 +0000663 result.high = 0;
Neil Booth23ff0222002-07-17 17:27:14 +0000664 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +0000665 result.low = node && node->type == NT_MACRO;
666 return result;
Zack Weinberg15dad1d2000-05-18 15:55:46 +0000667}
668
Neil Booth60284a52002-04-28 23:14:56 +0000669/* Convert a token into a CPP_NUMBER (an interpreted preprocessing
670 number or character constant, or the result of the "defined" or "#"
Neil Boothcd7ab832002-05-29 17:15:42 +0000671 operators). */
Neil Booth91318902002-05-26 18:42:21 +0000672static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000673eval_token (cpp_reader *pfile, const cpp_token *token)
Per Bothner7f2935c1995-03-16 13:59:07 -0800674{
Neil Booth91318902002-05-26 18:42:21 +0000675 cpp_num result;
Neil Booth60284a52002-04-28 23:14:56 +0000676 unsigned int temp;
Neil Booth4268e8b2002-05-04 07:30:32 +0000677 int unsignedp = 0;
Zack Weinberg041c3192000-07-04 01:58:21 +0000678
Diego Novillo6de9cd92004-05-13 02:41:07 -0400679 result.unsignedp = false;
680 result.overflow = false;
681
Neil Booth93c803682000-10-28 17:59:06 +0000682 switch (token->type)
Zack Weinbergba412f12000-03-01 00:57:09 +0000683 {
Per Bothner7f2935c1995-03-16 13:59:07 -0800684 case CPP_NUMBER:
Neil Boothcd7ab832002-05-29 17:15:42 +0000685 temp = cpp_classify_number (pfile, token);
686 switch (temp & CPP_N_CATEGORY)
687 {
688 case CPP_N_FLOATING:
John David Anglin0527bc42003-11-01 22:56:54 +0000689 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000690 "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);
John David Anglin0527bc42003-11-01 22:56:54 +0000695 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000696 "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;
Neil Booth7f2f1a62000-11-14 18:32:06 +0000705
Alexandre Oliva525bc952000-02-23 19:21:07 +0000706 case CPP_WCHAR:
Neil Booth4268e8b2002-05-04 07:30:32 +0000707 case CPP_CHAR:
Neil Bootha5a49442002-05-06 22:53:10 +0000708 {
Neil Booth91318902002-05-26 18:42:21 +0000709 cppchar_t cc = cpp_interpret_charconst (pfile, token,
710 &temp, &unsignedp);
711
712 result.high = 0;
713 result.low = cc;
Neil Bootha5a49442002-05-06 22:53:10 +0000714 /* Sign-extend the result if necessary. */
Neil Booth91318902002-05-26 18:42:21 +0000715 if (!unsignedp && (cppchar_signed_t) cc < 0)
716 {
717 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
718 result.low |= ~(~(cpp_num_part) 0
719 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
720 result.high = ~(cpp_num_part) 0;
721 result = num_trim (result, CPP_OPTION (pfile, precision));
722 }
Neil Bootha5a49442002-05-06 22:53:10 +0000723 }
Neil Booth60284a52002-04-28 23:14:56 +0000724 break;
Zack Weinbergba412f12000-03-01 00:57:09 +0000725
Zack Weinberg92936ec2000-07-19 20:18:08 +0000726 case CPP_NAME:
Neil Booth93c803682000-10-28 17:59:06 +0000727 if (token->val.node == pfile->spec_nodes.n_defined)
Neil Booth63d75002001-11-05 22:26:13 +0000728 return parse_defined (pfile);
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000729 else if (CPP_OPTION (pfile, cplusplus)
730 && (token->val.node == pfile->spec_nodes.n_true
731 || token->val.node == pfile->spec_nodes.n_false))
732 {
Neil Booth91318902002-05-26 18:42:21 +0000733 result.high = 0;
734 result.low = (token->val.node == pfile->spec_nodes.n_true);
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000735 }
736 else
737 {
Neil Booth91318902002-05-26 18:42:21 +0000738 result.high = 0;
739 result.low = 0;
Neil Booth87ed1092002-04-28 19:42:54 +0000740 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +0000741 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined",
Neil Boothebef4e82002-04-14 18:42:47 +0000742 NODE_NAME (token->val.node));
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000743 }
Neil Booth60284a52002-04-28 23:14:56 +0000744 break;
Zack Weinberg5dfa4da1999-02-08 20:27:27 +0000745
Neil Booth60284a52002-04-28 23:14:56 +0000746 default: /* CPP_HASH */
Neil Booth91318902002-05-26 18:42:21 +0000747 _cpp_test_assertion (pfile, &temp);
748 result.high = 0;
749 result.low = temp;
Neil Booth93c803682000-10-28 17:59:06 +0000750 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800751
Neil Booth23ff0222002-07-17 17:27:14 +0000752 result.unsignedp = !!unsignedp;
Neil Booth91318902002-05-26 18:42:21 +0000753 return result;
Per Bothner7f2935c1995-03-16 13:59:07 -0800754}
755
Neil Booth4063b942000-04-02 08:27:23 +0000756/* Operator precedence and flags table.
Neil Boothdbac4af2000-04-01 07:48:59 +0000757
758After an operator is returned from the lexer, if it has priority less
Neil Booth87ed1092002-04-28 19:42:54 +0000759than the operator on the top of the stack, we reduce the stack by one
760operator and repeat the test. Since equal priorities do not reduce,
761this is naturally right-associative.
Neil Boothdbac4af2000-04-01 07:48:59 +0000762
Neil Booth87ed1092002-04-28 19:42:54 +0000763We handle left-associative operators by decrementing the priority of
764just-lexed operators by one, but retaining the priority of operators
765already on the stack.
Neil Boothdbac4af2000-04-01 07:48:59 +0000766
767The remaining cases are '(' and ')'. We handle '(' by skipping the
768reduction phase completely. ')' is given lower priority than
769everything else, including '(', effectively forcing a reduction of the
Kazu Hirata272d0be2002-12-19 05:18:13 +0000770parenthesized expression. If there is a matching '(', the routine
Neil Booth87ed1092002-04-28 19:42:54 +0000771reduce() exits immediately. If the normal exit route sees a ')', then
772there cannot have been a matching '(' and an error message is output.
Neil Boothdbac4af2000-04-01 07:48:59 +0000773
Neil Boothf8b954f2002-04-26 06:32:50 +0000774The parser assumes all shifted operators require a left operand unless
775the flag NO_L_OPERAND is set. These semantics are automatic; any
776extra semantics need to be handled with operator-specific code. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800777
Neil Booth68e652752002-07-20 13:31:56 +0000778/* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
779 operand changes because of integer promotions. */
Neil Booth87ed1092002-04-28 19:42:54 +0000780#define NO_L_OPERAND (1 << 0)
781#define LEFT_ASSOC (1 << 1)
Neil Booth68e652752002-07-20 13:31:56 +0000782#define CHECK_PROMOTION (1 << 2)
Neil Bootheba30522000-03-31 22:23:59 +0000783
Zack Weinbergcf00a882000-07-08 02:33:00 +0000784/* Operator to priority map. Must be in the same order as the first
785 N entries of enum cpp_ttype. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000786static const struct cpp_operator
Zack Weinbergcf00a882000-07-08 02:33:00 +0000787{
Neil Booth60284a52002-04-28 23:14:56 +0000788 uchar prio;
Neil Booth87ed1092002-04-28 19:42:54 +0000789 uchar flags;
790} optab[] =
791{
Neil Boothad28cff2002-07-18 22:08:35 +0000792 /* EQ */ {0, 0}, /* Shouldn't happen. */
793 /* NOT */ {16, NO_L_OPERAND},
Neil Booth68e652752002-07-20 13:31:56 +0000794 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
795 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
796 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
797 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
798 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
799 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
800 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
801 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
802 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
803 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
Neil Boothad28cff2002-07-18 22:08:35 +0000804 /* RSHIFT */ {13, LEFT_ASSOC},
805 /* LSHIFT */ {13, LEFT_ASSOC},
Zack Weinbergcf00a882000-07-08 02:33:00 +0000806
Neil Boothad28cff2002-07-18 22:08:35 +0000807 /* COMPL */ {16, NO_L_OPERAND},
Neil Booth75aef482002-07-19 19:24:43 +0000808 /* AND_AND */ {6, LEFT_ASSOC},
809 /* OR_OR */ {5, LEFT_ASSOC},
810 /* QUERY */ {3, 0},
Neil Booth68e652752002-07-20 13:31:56 +0000811 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
Neil Boothad28cff2002-07-18 22:08:35 +0000812 /* COMMA */ {2, LEFT_ASSOC},
Neil Booth75aef482002-07-19 19:24:43 +0000813 /* OPEN_PAREN */ {1, NO_L_OPERAND},
Neil Boothad28cff2002-07-18 22:08:35 +0000814 /* CLOSE_PAREN */ {0, 0},
815 /* EOF */ {0, 0},
816 /* EQ_EQ */ {11, LEFT_ASSOC},
817 /* NOT_EQ */ {11, LEFT_ASSOC},
Neil Booth68e652752002-07-20 13:31:56 +0000818 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
819 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
Neil Boothad28cff2002-07-18 22:08:35 +0000820 /* UPLUS */ {16, NO_L_OPERAND},
821 /* UMINUS */ {16, NO_L_OPERAND}
Zack Weinbergcf00a882000-07-08 02:33:00 +0000822};
823
Per Bothner7f2935c1995-03-16 13:59:07 -0800824/* Parse and evaluate a C expression, reading from PFILE.
Kazu Hiratadf383482002-05-22 22:02:16 +0000825 Returns the truth value of the expression.
Neil Booth87ed1092002-04-28 19:42:54 +0000826
827 The implementation is an operator precedence parser, i.e. a
828 bottom-up parser, using a stack for not-yet-reduced tokens.
829
830 The stack base is op_stack, and the current stack pointer is 'top'.
831 There is a stack element for each operator (only), and the most
832 recently pushed operator is 'top->op'. An operand (value) is
833 stored in the 'value' field of the stack element of the operator
834 that precedes it. */
835bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000836_cpp_parse_expr (cpp_reader *pfile)
Per Bothner7f2935c1995-03-16 13:59:07 -0800837{
Neil Booth87ed1092002-04-28 19:42:54 +0000838 struct op *top = pfile->op_stack;
839 unsigned int lex_count;
840 bool saw_leading_not, want_value = true;
Per Bothner7f2935c1995-03-16 13:59:07 -0800841
Neil Booth87ed1092002-04-28 19:42:54 +0000842 pfile->state.skip_eval = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800843
Neil Booth93c803682000-10-28 17:59:06 +0000844 /* Set up detection of #if ! defined(). */
Neil Booth6d18adb2001-07-29 17:27:57 +0000845 pfile->mi_ind_cmacro = 0;
Neil Booth87ed1092002-04-28 19:42:54 +0000846 saw_leading_not = false;
Neil Booth6d18adb2001-07-29 17:27:57 +0000847 lex_count = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000848
Neil Booth87ed1092002-04-28 19:42:54 +0000849 /* Lowest priority operator prevents further reductions. */
Zack Weinbergcf00a882000-07-08 02:33:00 +0000850 top->op = CPP_EOF;
Neil Booth4063b942000-04-02 08:27:23 +0000851
Per Bothner7f2935c1995-03-16 13:59:07 -0800852 for (;;)
853 {
Zack Weinbergcf00a882000-07-08 02:33:00 +0000854 struct op op;
Per Bothner7f2935c1995-03-16 13:59:07 -0800855
Neil Booth6d18adb2001-07-29 17:27:57 +0000856 lex_count++;
Neil Booth68e652752002-07-20 13:31:56 +0000857 op.token = cpp_get_token (pfile);
858 op.op = op.token->type;
Per Bothner7f2935c1995-03-16 13:59:07 -0800859
Per Bothner7f2935c1995-03-16 13:59:07 -0800860 switch (op.op)
861 {
Neil Booth60284a52002-04-28 23:14:56 +0000862 /* These tokens convert into values. */
Neil Boothc60e94a2001-07-19 06:12:50 +0000863 case CPP_NUMBER:
Neil Booth60284a52002-04-28 23:14:56 +0000864 case CPP_CHAR:
865 case CPP_WCHAR:
866 case CPP_NAME:
867 case CPP_HASH:
Neil Boothf8b954f2002-04-26 06:32:50 +0000868 if (!want_value)
Neil Booth60284a52002-04-28 23:14:56 +0000869 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
Neil Booth68e652752002-07-20 13:31:56 +0000870 cpp_token_as_text (pfile, op.token));
Neil Boothf8b954f2002-04-26 06:32:50 +0000871 want_value = false;
Neil Booth68e652752002-07-20 13:31:56 +0000872 top->value = eval_token (pfile, op.token);
Neil Booth9ee703132000-04-01 07:42:37 +0000873 continue;
874
Neil Booth6d18adb2001-07-29 17:27:57 +0000875 case CPP_NOT:
876 saw_leading_not = lex_count == 1;
Neil Booth6d18adb2001-07-29 17:27:57 +0000877 break;
Zack Weinbergcf00a882000-07-08 02:33:00 +0000878 case CPP_PLUS:
Neil Boothf8b954f2002-04-26 06:32:50 +0000879 if (want_value)
880 op.op = CPP_UPLUS;
881 break;
882 case CPP_MINUS:
883 if (want_value)
884 op.op = CPP_UMINUS;
885 break;
Neil Booth60284a52002-04-28 23:14:56 +0000886
Neil Boothf8b954f2002-04-26 06:32:50 +0000887 default:
Neil Booth60284a52002-04-28 23:14:56 +0000888 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
Neil Boothcd7ab832002-05-29 17:15:42 +0000889 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
Neil Booth68e652752002-07-20 13:31:56 +0000890 cpp_token_as_text (pfile, op.token));
Neil Boothf8b954f2002-04-26 06:32:50 +0000891 break;
Per Bothner7f2935c1995-03-16 13:59:07 -0800892 }
893
Neil Booth87ed1092002-04-28 19:42:54 +0000894 /* Check we have a value or operator as appropriate. */
895 if (optab[op.op].flags & NO_L_OPERAND)
Neil Booth4063b942000-04-02 08:27:23 +0000896 {
Neil Boothf8b954f2002-04-26 06:32:50 +0000897 if (!want_value)
Neil Booth60284a52002-04-28 23:14:56 +0000898 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
Neil Booth68e652752002-07-20 13:31:56 +0000899 cpp_token_as_text (pfile, op.token));
Neil Boothb22ef132000-04-03 22:33:12 +0000900 }
Neil Booth87ed1092002-04-28 19:42:54 +0000901 else if (want_value)
Neil Boothb22ef132000-04-03 22:33:12 +0000902 {
Neil Bootha09d4742004-07-04 12:57:50 +0000903 /* We want a number (or expression) and haven't got one.
904 Try to emit a specific diagnostic. */
905 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
906 SYNTAX_ERROR ("missing expression between '(' and ')'");
907
908 if (op.op == CPP_EOF && top->op == CPP_EOF)
909 SYNTAX_ERROR ("#if with no expression");
910
911 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
912 SYNTAX_ERROR2 ("operator '%s' has no right operand",
913 cpp_token_as_text (pfile, top->token));
914 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
915 /* Complain about missing paren during reduction. */;
916 else
917 SYNTAX_ERROR2 ("operator '%s' has no left operand",
918 cpp_token_as_text (pfile, op.token));
Neil Booth4063b942000-04-02 08:27:23 +0000919 }
Neil Booth87ed1092002-04-28 19:42:54 +0000920
921 top = reduce (pfile, top, op.op);
922 if (!top)
923 goto syntax_error;
924
Neil Booth60284a52002-04-28 23:14:56 +0000925 if (op.op == CPP_EOF)
926 break;
927
Neil Booth87ed1092002-04-28 19:42:54 +0000928 switch (op.op)
929 {
930 case CPP_CLOSE_PAREN:
931 continue;
Neil Booth87ed1092002-04-28 19:42:54 +0000932 case CPP_OR_OR:
Neil Booth91318902002-05-26 18:42:21 +0000933 if (!num_zerop (top->value))
Neil Booth87ed1092002-04-28 19:42:54 +0000934 pfile->state.skip_eval++;
935 break;
936 case CPP_AND_AND:
937 case CPP_QUERY:
Neil Booth91318902002-05-26 18:42:21 +0000938 if (num_zerop (top->value))
Neil Booth87ed1092002-04-28 19:42:54 +0000939 pfile->state.skip_eval++;
940 break;
941 case CPP_COLON:
Neil Booth60284a52002-04-28 23:14:56 +0000942 if (top->op != CPP_QUERY)
943 SYNTAX_ERROR (" ':' without preceding '?'");
Neil Booth91318902002-05-26 18:42:21 +0000944 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
Neil Booth87ed1092002-04-28 19:42:54 +0000945 pfile->state.skip_eval++;
946 else
947 pfile->state.skip_eval--;
948 default:
949 break;
950 }
951
Neil Boothf8b954f2002-04-26 06:32:50 +0000952 want_value = true;
Neil Booth4063b942000-04-02 08:27:23 +0000953
Mike Stump0f413021996-07-03 22:07:53 +0000954 /* Check for and handle stack overflow. */
Neil Booth87ed1092002-04-28 19:42:54 +0000955 if (++top == pfile->op_limit)
956 top = _cpp_expand_op_stack (pfile);
Kazu Hiratadf383482002-05-22 22:02:16 +0000957
Per Bothner7f2935c1995-03-16 13:59:07 -0800958 top->op = op.op;
Neil Booth68e652752002-07-20 13:31:56 +0000959 top->token = op.token;
Per Bothner7f2935c1995-03-16 13:59:07 -0800960 }
Neil Booth9ee703132000-04-01 07:42:37 +0000961
Neil Booth6d18adb2001-07-29 17:27:57 +0000962 /* The controlling macro expression is only valid if we called lex 3
963 times: <!> <defined expression> and <EOF>. push_conditional ()
964 checks that we are at top-of-file. */
965 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
966 pfile->mi_ind_cmacro = 0;
967
Neil Booth87ed1092002-04-28 19:42:54 +0000968 if (top != pfile->op_stack)
Neil Boothebef4e82002-04-14 18:42:47 +0000969 {
John David Anglin0527bc42003-11-01 22:56:54 +0000970 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in #if");
Neil Booth4063b942000-04-02 08:27:23 +0000971 syntax_error:
Neil Booth87ed1092002-04-28 19:42:54 +0000972 return false; /* Return false on syntax error. */
Neil Booth4063b942000-04-02 08:27:23 +0000973 }
Neil Booth9ee703132000-04-01 07:42:37 +0000974
Neil Booth91318902002-05-26 18:42:21 +0000975 return !num_zerop (top->value);
Neil Booth87ed1092002-04-28 19:42:54 +0000976}
977
978/* Reduce the operator / value stack if possible, in preparation for
979 pushing operator OP. Returns NULL on error, otherwise the top of
980 the stack. */
981static struct op *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000982reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
Neil Booth87ed1092002-04-28 19:42:54 +0000983{
984 unsigned int prio;
985
Neil Booth91318902002-05-26 18:42:21 +0000986 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
987 {
988 bad_op:
John David Anglin0527bc42003-11-01 22:56:54 +0000989 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
Neil Booth91318902002-05-26 18:42:21 +0000990 return 0;
991 }
992
Neil Booth87ed1092002-04-28 19:42:54 +0000993 if (op == CPP_OPEN_PAREN)
994 return top;
995
996 /* Decrement the priority of left-associative operators to force a
997 reduction with operators of otherwise equal priority. */
998 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
999 while (prio < optab[top->op].prio)
1000 {
Neil Booth68e652752002-07-20 13:31:56 +00001001 if (CPP_OPTION (pfile, warn_num_sign_change)
1002 && optab[top->op].flags & CHECK_PROMOTION)
1003 check_promotion (pfile, top);
1004
Neil Booth75aef482002-07-19 19:24:43 +00001005 switch (top->op)
1006 {
1007 case CPP_UPLUS:
1008 case CPP_UMINUS:
1009 case CPP_NOT:
1010 case CPP_COMPL:
1011 top[-1].value = num_unary_op (pfile, top->value, top->op);
1012 break;
Neil Booth91318902002-05-26 18:42:21 +00001013
Neil Booth75aef482002-07-19 19:24:43 +00001014 case CPP_PLUS:
1015 case CPP_MINUS:
1016 case CPP_RSHIFT:
1017 case CPP_LSHIFT:
Neil Booth75aef482002-07-19 19:24:43 +00001018 case CPP_COMMA:
1019 top[-1].value = num_binary_op (pfile, top[-1].value,
1020 top->value, top->op);
1021 break;
Neil Booth91318902002-05-26 18:42:21 +00001022
Neil Booth75aef482002-07-19 19:24:43 +00001023 case CPP_GREATER:
1024 case CPP_LESS:
1025 case CPP_GREATER_EQ:
1026 case CPP_LESS_EQ:
1027 top[-1].value
1028 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1029 break;
Neil Booth91318902002-05-26 18:42:21 +00001030
Neil Booth75aef482002-07-19 19:24:43 +00001031 case CPP_EQ_EQ:
1032 case CPP_NOT_EQ:
1033 top[-1].value
1034 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1035 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001036
Neil Booth75aef482002-07-19 19:24:43 +00001037 case CPP_AND:
1038 case CPP_OR:
1039 case CPP_XOR:
1040 top[-1].value
1041 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1042 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001043
Neil Booth75aef482002-07-19 19:24:43 +00001044 case CPP_MULT:
1045 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1046 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001047
Neil Booth75aef482002-07-19 19:24:43 +00001048 case CPP_DIV:
1049 case CPP_MOD:
1050 top[-1].value = num_div_op (pfile, top[-1].value,
1051 top->value, top->op);
1052 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001053
Neil Booth75aef482002-07-19 19:24:43 +00001054 case CPP_OR_OR:
1055 top--;
1056 if (!num_zerop (top->value))
1057 pfile->state.skip_eval--;
1058 top->value.low = (!num_zerop (top->value)
1059 || !num_zerop (top[1].value));
1060 top->value.high = 0;
1061 top->value.unsignedp = false;
1062 top->value.overflow = false;
1063 continue;
1064
1065 case CPP_AND_AND:
1066 top--;
1067 if (num_zerop (top->value))
1068 pfile->state.skip_eval--;
1069 top->value.low = (!num_zerop (top->value)
1070 && !num_zerop (top[1].value));
1071 top->value.high = 0;
1072 top->value.unsignedp = false;
1073 top->value.overflow = false;
1074 continue;
1075
1076 case CPP_OPEN_PAREN:
1077 if (op != CPP_CLOSE_PAREN)
1078 {
John David Anglin0527bc42003-11-01 22:56:54 +00001079 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in expression");
Neil Booth75aef482002-07-19 19:24:43 +00001080 return 0;
1081 }
1082 top--;
1083 top->value = top[1].value;
1084 return top;
1085
1086 case CPP_COLON:
1087 top -= 2;
1088 if (!num_zerop (top->value))
1089 {
Neil Booth91318902002-05-26 18:42:21 +00001090 pfile->state.skip_eval--;
Neil Booth75aef482002-07-19 19:24:43 +00001091 top->value = top[1].value;
1092 }
1093 else
1094 top->value = top[2].value;
1095 top->value.unsignedp = (top[1].value.unsignedp
1096 || top[2].value.unsignedp);
1097 continue;
Neil Booth91318902002-05-26 18:42:21 +00001098
Neil Booth75aef482002-07-19 19:24:43 +00001099 case CPP_QUERY:
John David Anglin0527bc42003-11-01 22:56:54 +00001100 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
Neil Booth75aef482002-07-19 19:24:43 +00001101 return 0;
Neil Booth91318902002-05-26 18:42:21 +00001102
Neil Booth75aef482002-07-19 19:24:43 +00001103 default:
1104 goto bad_op;
1105 }
Neil Boothad28cff2002-07-18 22:08:35 +00001106
1107 top--;
Neil Booth91318902002-05-26 18:42:21 +00001108 if (top->value.overflow && !pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +00001109 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth91318902002-05-26 18:42:21 +00001110 "integer overflow in preprocessor expression");
Neil Booth87ed1092002-04-28 19:42:54 +00001111 }
1112
1113 if (op == CPP_CLOSE_PAREN)
1114 {
John David Anglin0527bc42003-11-01 22:56:54 +00001115 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
Neil Booth87ed1092002-04-28 19:42:54 +00001116 return 0;
1117 }
1118
1119 return top;
1120}
1121
1122/* Returns the position of the old top of stack after expansion. */
1123struct op *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001124_cpp_expand_op_stack (cpp_reader *pfile)
Neil Booth87ed1092002-04-28 19:42:54 +00001125{
Neil Booth32fa4562002-05-09 22:27:31 +00001126 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1127 size_t new_size = old_size * 2 + 20;
Neil Booth87ed1092002-04-28 19:42:54 +00001128
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001129 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
Neil Booth32fa4562002-05-09 22:27:31 +00001130 pfile->op_limit = pfile->op_stack + new_size;
Neil Booth87ed1092002-04-28 19:42:54 +00001131
Neil Booth32fa4562002-05-09 22:27:31 +00001132 return pfile->op_stack + old_size;
Per Bothner7f2935c1995-03-16 13:59:07 -08001133}
Neil Booth91318902002-05-26 18:42:21 +00001134
Neil Booth68e652752002-07-20 13:31:56 +00001135/* Emits a warning if the effective sign of either operand of OP
1136 changes because of integer promotions. */
1137static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001138check_promotion (cpp_reader *pfile, const struct op *op)
Neil Booth68e652752002-07-20 13:31:56 +00001139{
1140 if (op->value.unsignedp == op[-1].value.unsignedp)
1141 return;
1142
1143 if (op->value.unsignedp)
1144 {
1145 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
John David Anglin0527bc42003-11-01 22:56:54 +00001146 cpp_error (pfile, CPP_DL_WARNING,
Neil Booth68e652752002-07-20 13:31:56 +00001147 "the left operand of \"%s\" changes sign when promoted",
1148 cpp_token_as_text (pfile, op->token));
1149 }
1150 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
John David Anglin0527bc42003-11-01 22:56:54 +00001151 cpp_error (pfile, CPP_DL_WARNING,
Neil Booth68e652752002-07-20 13:31:56 +00001152 "the right operand of \"%s\" changes sign when promoted",
1153 cpp_token_as_text (pfile, op->token));
1154}
1155
Neil Booth91318902002-05-26 18:42:21 +00001156/* Clears the unused high order bits of the number pointed to by PNUM. */
1157static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001158num_trim (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001159{
1160 if (precision > PART_PRECISION)
1161 {
1162 precision -= PART_PRECISION;
1163 if (precision < PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001164 num.high &= ((cpp_num_part) 1 << precision) - 1;
Neil Booth91318902002-05-26 18:42:21 +00001165 }
1166 else
1167 {
1168 if (precision < PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001169 num.low &= ((cpp_num_part) 1 << precision) - 1;
Neil Booth91318902002-05-26 18:42:21 +00001170 num.high = 0;
1171 }
1172
1173 return num;
1174}
1175
1176/* True iff A (presumed signed) >= 0. */
1177static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001178num_positive (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001179{
1180 if (precision > PART_PRECISION)
1181 {
1182 precision -= PART_PRECISION;
Neil Booth359b0be2002-05-28 05:44:06 +00001183 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
Neil Booth91318902002-05-26 18:42:21 +00001184 }
1185
Neil Booth359b0be2002-05-28 05:44:06 +00001186 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
Neil Booth91318902002-05-26 18:42:21 +00001187}
1188
Neil Boothceeedfc2002-06-02 19:37:34 +00001189/* Sign extend a number, with PRECISION significant bits and all
1190 others assumed clear, to fill out a cpp_num structure. */
1191cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001192cpp_num_sign_extend (cpp_num num, size_t precision)
Neil Boothceeedfc2002-06-02 19:37:34 +00001193{
1194 if (!num.unsignedp)
1195 {
1196 if (precision > PART_PRECISION)
1197 {
1198 precision -= PART_PRECISION;
1199 if (precision < PART_PRECISION
1200 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1201 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1202 }
1203 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1204 {
1205 if (precision < PART_PRECISION)
1206 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1207 num.high = ~(cpp_num_part) 0;
1208 }
1209 }
1210
1211 return num;
1212}
1213
Neil Booth91318902002-05-26 18:42:21 +00001214/* Returns the negative of NUM. */
1215static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001216num_negate (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001217{
1218 cpp_num copy;
1219
1220 copy = num;
1221 num.high = ~num.high;
1222 num.low = ~num.low;
1223 if (++num.low == 0)
1224 num.high++;
1225 num = num_trim (num, precision);
1226 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1227
1228 return num;
1229}
1230
1231/* Returns true if A >= B. */
1232static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001233num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001234{
1235 bool unsignedp;
1236
1237 unsignedp = pa.unsignedp || pb.unsignedp;
1238
1239 if (!unsignedp)
1240 {
1241 /* Both numbers have signed type. If they are of different
1242 sign, the answer is the sign of A. */
1243 unsignedp = num_positive (pa, precision);
1244
1245 if (unsignedp != num_positive (pb, precision))
1246 return unsignedp;
1247
1248 /* Otherwise we can do an unsigned comparison. */
1249 }
1250
1251 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1252}
1253
1254/* Returns LHS OP RHS, where OP is a bit-wise operation. */
1255static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001256num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1257 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001258{
1259 lhs.overflow = false;
1260 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1261
1262 /* As excess precision is zeroed, there is no need to num_trim () as
1263 these operations cannot introduce a set bit there. */
1264 if (op == CPP_AND)
1265 {
1266 lhs.low &= rhs.low;
1267 lhs.high &= rhs.high;
1268 }
1269 else if (op == CPP_OR)
1270 {
1271 lhs.low |= rhs.low;
1272 lhs.high |= rhs.high;
1273 }
1274 else
1275 {
1276 lhs.low ^= rhs.low;
1277 lhs.high ^= rhs.high;
1278 }
1279
1280 return lhs;
1281}
1282
1283/* Returns LHS OP RHS, where OP is an inequality. */
1284static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001285num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1286 enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001287{
1288 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1289
1290 if (op == CPP_GREATER_EQ)
1291 lhs.low = gte;
1292 else if (op == CPP_LESS)
1293 lhs.low = !gte;
1294 else if (op == CPP_GREATER)
1295 lhs.low = gte && !num_eq (lhs, rhs);
1296 else /* CPP_LESS_EQ. */
1297 lhs.low = !gte || num_eq (lhs, rhs);
1298
1299 lhs.high = 0;
1300 lhs.overflow = false;
1301 lhs.unsignedp = false;
1302 return lhs;
1303}
1304
1305/* Returns LHS OP RHS, where OP is == or !=. */
1306static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001307num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1308 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001309{
Jason Merrill97459792002-06-07 09:29:17 -04001310 /* Work around a 3.0.4 bug; see PR 6950. */
1311 bool eq = num_eq (lhs, rhs);
Neil Booth91318902002-05-26 18:42:21 +00001312 if (op == CPP_NOT_EQ)
Jason Merrill97459792002-06-07 09:29:17 -04001313 eq = !eq;
1314 lhs.low = eq;
Neil Booth91318902002-05-26 18:42:21 +00001315 lhs.high = 0;
1316 lhs.overflow = false;
1317 lhs.unsignedp = false;
1318 return lhs;
1319}
1320
1321/* Shift NUM, of width PRECISION, right by N bits. */
1322static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001323num_rshift (cpp_num num, size_t precision, size_t n)
Neil Booth91318902002-05-26 18:42:21 +00001324{
1325 cpp_num_part sign_mask;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001326 bool x = num_positive (num, precision);
Neil Booth91318902002-05-26 18:42:21 +00001327
Diego Novillo6de9cd92004-05-13 02:41:07 -04001328 if (num.unsignedp || x)
Neil Booth91318902002-05-26 18:42:21 +00001329 sign_mask = 0;
1330 else
1331 sign_mask = ~(cpp_num_part) 0;
1332
1333 if (n >= precision)
1334 num.high = num.low = sign_mask;
1335 else
1336 {
1337 /* Sign-extend. */
1338 if (precision < PART_PRECISION)
1339 num.high = sign_mask, num.low |= sign_mask << precision;
1340 else if (precision < 2 * PART_PRECISION)
1341 num.high |= sign_mask << (precision - PART_PRECISION);
1342
1343 if (n >= PART_PRECISION)
1344 {
1345 n -= PART_PRECISION;
1346 num.low = num.high;
1347 num.high = sign_mask;
1348 }
1349
1350 if (n)
1351 {
1352 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1353 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1354 }
1355 }
1356
1357 num = num_trim (num, precision);
1358 num.overflow = false;
1359 return num;
1360}
1361
1362/* Shift NUM, of width PRECISION, left by N bits. */
1363static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001364num_lshift (cpp_num num, size_t precision, size_t n)
Neil Booth91318902002-05-26 18:42:21 +00001365{
1366 if (n >= precision)
1367 {
1368 num.overflow = !num.unsignedp && !num_zerop (num);
1369 num.high = num.low = 0;
1370 }
1371 else
1372 {
1373 cpp_num orig, maybe_orig;
1374 size_t m = n;
1375
1376 orig = num;
1377 if (m >= PART_PRECISION)
1378 {
1379 m -= PART_PRECISION;
1380 num.high = num.low;
1381 num.low = 0;
1382 }
1383 if (m)
1384 {
1385 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1386 num.low <<= m;
1387 }
1388 num = num_trim (num, precision);
1389
1390 if (num.unsignedp)
1391 num.overflow = false;
1392 else
1393 {
1394 maybe_orig = num_rshift (num, precision, n);
1395 num.overflow = !num_eq (orig, maybe_orig);
1396 }
1397 }
1398
1399 return num;
1400}
1401
1402/* The four unary operators: +, -, ! and ~. */
1403static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001404num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001405{
1406 switch (op)
1407 {
1408 case CPP_UPLUS:
Neil Booth75aef482002-07-19 19:24:43 +00001409 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +00001410 cpp_error (pfile, CPP_DL_WARNING,
Neil Booth91318902002-05-26 18:42:21 +00001411 "traditional C rejects the unary plus operator");
1412 num.overflow = false;
1413 break;
1414
1415 case CPP_UMINUS:
1416 num = num_negate (num, CPP_OPTION (pfile, precision));
1417 break;
1418
1419 case CPP_COMPL:
1420 num.high = ~num.high;
1421 num.low = ~num.low;
1422 num = num_trim (num, CPP_OPTION (pfile, precision));
1423 num.overflow = false;
1424 break;
1425
1426 default: /* case CPP_NOT: */
1427 num.low = num_zerop (num);
1428 num.high = 0;
1429 num.overflow = false;
1430 num.unsignedp = false;
1431 break;
1432 }
1433
1434 return num;
1435}
1436
1437/* The various binary operators. */
1438static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001439num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001440{
1441 cpp_num result;
1442 size_t precision = CPP_OPTION (pfile, precision);
Neil Booth91318902002-05-26 18:42:21 +00001443 size_t n;
1444
1445 switch (op)
1446 {
1447 /* Shifts. */
1448 case CPP_LSHIFT:
1449 case CPP_RSHIFT:
1450 if (!rhs.unsignedp && !num_positive (rhs, precision))
1451 {
1452 /* A negative shift is a positive shift the other way. */
1453 if (op == CPP_LSHIFT)
1454 op = CPP_RSHIFT;
1455 else
1456 op = CPP_LSHIFT;
1457 rhs = num_negate (rhs, precision);
1458 }
1459 if (rhs.high)
1460 n = ~0; /* Maximal. */
1461 else
1462 n = rhs.low;
1463 if (op == CPP_LSHIFT)
1464 lhs = num_lshift (lhs, precision, n);
1465 else
1466 lhs = num_rshift (lhs, precision, n);
1467 break;
1468
Neil Booth91318902002-05-26 18:42:21 +00001469 /* Arithmetic. */
1470 case CPP_MINUS:
1471 rhs = num_negate (rhs, precision);
1472 case CPP_PLUS:
1473 result.low = lhs.low + rhs.low;
1474 result.high = lhs.high + rhs.high;
1475 if (result.low < lhs.low)
1476 result.high++;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001477 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1478 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +00001479
1480 result = num_trim (result, precision);
Diego Novillo6de9cd92004-05-13 02:41:07 -04001481 if (!result.unsignedp)
Neil Booth91318902002-05-26 18:42:21 +00001482 {
1483 bool lhsp = num_positive (lhs, precision);
1484 result.overflow = (lhsp == num_positive (rhs, precision)
1485 && lhsp != num_positive (result, precision));
1486 }
1487 return result;
1488
1489 /* Comma. */
1490 default: /* case CPP_COMMA: */
Joseph Myers32e8aa92004-02-11 23:50:45 +00001491 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1492 || !pfile->state.skip_eval))
John David Anglin0527bc42003-11-01 22:56:54 +00001493 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth91318902002-05-26 18:42:21 +00001494 "comma operator in operand of #if");
1495 lhs = rhs;
1496 break;
1497 }
1498
1499 return lhs;
1500}
1501
1502/* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1503 cannot overflow. */
1504static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001505num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
Neil Booth91318902002-05-26 18:42:21 +00001506{
1507 cpp_num result;
1508 cpp_num_part middle[2], temp;
1509
1510 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1511 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1512
1513 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1514 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1515
1516 temp = result.low;
1517 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1518 if (result.low < temp)
1519 result.high++;
1520
1521 temp = result.low;
1522 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1523 if (result.low < temp)
1524 result.high++;
1525
1526 result.high += HIGH_PART (middle[0]);
1527 result.high += HIGH_PART (middle[1]);
Diego Novillo6de9cd92004-05-13 02:41:07 -04001528 result.unsignedp = true;
1529 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +00001530
1531 return result;
1532}
1533
1534/* Multiply two preprocessing numbers. */
1535static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001536num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
Neil Booth91318902002-05-26 18:42:21 +00001537{
1538 cpp_num result, temp;
1539 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1540 bool overflow, negate = false;
1541 size_t precision = CPP_OPTION (pfile, precision);
1542
1543 /* Prepare for unsigned multiplication. */
1544 if (!unsignedp)
1545 {
1546 if (!num_positive (lhs, precision))
1547 negate = !negate, lhs = num_negate (lhs, precision);
1548 if (!num_positive (rhs, precision))
1549 negate = !negate, rhs = num_negate (rhs, precision);
1550 }
1551
1552 overflow = lhs.high && rhs.high;
1553 result = num_part_mul (lhs.low, rhs.low);
1554
1555 temp = num_part_mul (lhs.high, rhs.low);
1556 result.high += temp.low;
1557 if (temp.high)
1558 overflow = true;
1559
1560 temp = num_part_mul (lhs.low, rhs.high);
1561 result.high += temp.low;
1562 if (temp.high)
1563 overflow = true;
1564
1565 temp.low = result.low, temp.high = result.high;
1566 result = num_trim (result, precision);
1567 if (!num_eq (result, temp))
1568 overflow = true;
1569
1570 if (negate)
1571 result = num_negate (result, precision);
1572
1573 if (unsignedp)
1574 result.overflow = false;
1575 else
1576 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1577 && !num_zerop (result));
1578 result.unsignedp = unsignedp;
1579
1580 return result;
1581}
1582
1583/* Divide two preprocessing numbers, returning the answer or the
1584 remainder depending upon OP. */
1585static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001586num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001587{
1588 cpp_num result, sub;
1589 cpp_num_part mask;
1590 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1591 bool negate = false, lhs_neg = false;
1592 size_t i, precision = CPP_OPTION (pfile, precision);
1593
1594 /* Prepare for unsigned division. */
1595 if (!unsignedp)
1596 {
1597 if (!num_positive (lhs, precision))
1598 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1599 if (!num_positive (rhs, precision))
1600 negate = !negate, rhs = num_negate (rhs, precision);
1601 }
1602
1603 /* Find the high bit. */
1604 if (rhs.high)
1605 {
1606 i = precision - 1;
Neil Booth359b0be2002-05-28 05:44:06 +00001607 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
Neil Booth91318902002-05-26 18:42:21 +00001608 for (; ; i--, mask >>= 1)
1609 if (rhs.high & mask)
1610 break;
1611 }
1612 else if (rhs.low)
1613 {
1614 if (precision > PART_PRECISION)
1615 i = precision - PART_PRECISION - 1;
1616 else
1617 i = precision - 1;
Neil Booth359b0be2002-05-28 05:44:06 +00001618 mask = (cpp_num_part) 1 << i;
Neil Booth91318902002-05-26 18:42:21 +00001619 for (; ; i--, mask >>= 1)
1620 if (rhs.low & mask)
1621 break;
1622 }
1623 else
1624 {
Neil Booth75aef482002-07-19 19:24:43 +00001625 if (!pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +00001626 cpp_error (pfile, CPP_DL_ERROR, "division by zero in #if");
Neil Booth91318902002-05-26 18:42:21 +00001627 return lhs;
1628 }
1629
Kazu Hiratada7d8302002-09-22 02:03:17 +00001630 /* First nonzero bit of RHS is bit I. Do naive division by
Neil Booth91318902002-05-26 18:42:21 +00001631 shifting the RHS fully left, and subtracting from LHS if LHS is
1632 at least as big, and then repeating but with one less shift.
1633 This is not very efficient, but is easy to understand. */
1634
1635 rhs.unsignedp = true;
1636 lhs.unsignedp = true;
1637 i = precision - i - 1;
1638 sub = num_lshift (rhs, precision, i);
1639
1640 result.high = result.low = 0;
1641 for (;;)
1642 {
1643 if (num_greater_eq (lhs, sub, precision))
1644 {
1645 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1646 if (i >= PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001647 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
Neil Booth91318902002-05-26 18:42:21 +00001648 else
Neil Booth359b0be2002-05-28 05:44:06 +00001649 result.low |= (cpp_num_part) 1 << i;
Neil Booth91318902002-05-26 18:42:21 +00001650 }
1651 if (i-- == 0)
1652 break;
1653 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1654 sub.high >>= 1;
1655 }
1656
1657 /* We divide so that the remainder has the sign of the LHS. */
1658 if (op == CPP_DIV)
1659 {
1660 result.unsignedp = unsignedp;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001661 result.overflow = false;
1662 if (!unsignedp)
Neil Booth91318902002-05-26 18:42:21 +00001663 {
1664 if (negate)
1665 result = num_negate (result, precision);
Eric Christopher22a8a522007-05-02 21:57:50 +00001666 result.overflow = (num_positive (result, precision) ^ !negate
1667 && !num_zerop (result));
Neil Booth91318902002-05-26 18:42:21 +00001668 }
1669
1670 return result;
1671 }
1672
1673 /* CPP_MOD. */
1674 lhs.unsignedp = unsignedp;
1675 lhs.overflow = false;
1676 if (lhs_neg)
1677 lhs = num_negate (lhs, precision);
1678
1679 return lhs;
1680}