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