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