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