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