1169695Skan/* Parse C expressions for cpplib.
2169695Skan   Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3169695Skan   2002, 2004 Free Software Foundation.
4169695Skan   Contributed by Per Bothner, 1994.
5169695Skan
6169695SkanThis program is free software; you can redistribute it and/or modify it
7169695Skanunder the terms of the GNU General Public License as published by the
8169695SkanFree Software Foundation; either version 2, or (at your option) any
9169695Skanlater version.
10169695Skan
11169695SkanThis program is distributed in the hope that it will be useful,
12169695Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of
13169695SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14169695SkanGNU General Public License for more details.
15169695Skan
16169695SkanYou should have received a copy of the GNU General Public License
17169695Skanalong with this program; if not, write to the Free Software
18169695SkanFoundation, 51 Franklin Street, Fifth Floor,
19169695SkanBoston, MA 02110-1301, USA.  */
20169695Skan
21169695Skan#include "config.h"
22169695Skan#include "system.h"
23169695Skan#include "cpplib.h"
24169695Skan#include "internal.h"
25169695Skan
26169695Skan#define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
27169695Skan#define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
28169695Skan#define LOW_PART(num_part) (num_part & HALF_MASK)
29169695Skan#define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
30169695Skan
31169695Skanstruct op
32169695Skan{
33169695Skan  const cpp_token *token;	/* The token forming op (for diagnostics).  */
34169695Skan  cpp_num value;		/* The value logically "right" of op.  */
35169695Skan  enum cpp_ttype op;
36169695Skan};
37169695Skan
38169695Skan/* Some simple utility routines on double integers.  */
39169695Skan#define num_zerop(num) ((num.low | num.high) == 0)
40169695Skan#define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
41169695Skanstatic bool num_positive (cpp_num, size_t);
42169695Skanstatic bool num_greater_eq (cpp_num, cpp_num, size_t);
43169695Skanstatic cpp_num num_trim (cpp_num, size_t);
44169695Skanstatic cpp_num num_part_mul (cpp_num_part, cpp_num_part);
45169695Skan
46169695Skanstatic cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype);
47169695Skanstatic cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
48169695Skanstatic cpp_num num_negate (cpp_num, size_t);
49169695Skanstatic cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
50169695Skanstatic cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num,
51169695Skan				  enum cpp_ttype);
52169695Skanstatic cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num,
53169695Skan				enum cpp_ttype);
54169695Skanstatic cpp_num num_mul (cpp_reader *, cpp_num, cpp_num);
55169695Skanstatic cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
56169695Skanstatic cpp_num num_lshift (cpp_num, size_t, size_t);
57169695Skanstatic cpp_num num_rshift (cpp_num, size_t, size_t);
58169695Skan
59169695Skanstatic cpp_num append_digit (cpp_num, int, int, size_t);
60169695Skanstatic cpp_num parse_defined (cpp_reader *);
61169695Skanstatic cpp_num eval_token (cpp_reader *, const cpp_token *);
62169695Skanstatic struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
63169695Skanstatic unsigned int interpret_float_suffix (const uchar *, size_t);
64169695Skanstatic unsigned int interpret_int_suffix (const uchar *, size_t);
65169695Skanstatic void check_promotion (cpp_reader *, const struct op *);
66169695Skan
67169695Skan/* Token type abuse to create unary plus and minus operators.  */
68169695Skan#define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
69169695Skan#define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
70169695Skan
71169695Skan/* With -O2, gcc appears to produce nice code, moving the error
72169695Skan   message load and subsequent jump completely out of the main path.  */
73169695Skan#define SYNTAX_ERROR(msgid) \
74169695Skan  do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
75169695Skan#define SYNTAX_ERROR2(msgid, arg) \
76169695Skan  do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
77169695Skan  while(0)
78169695Skan
79169695Skan/* Subroutine of cpp_classify_number.  S points to a float suffix of
80169695Skan   length LEN, possibly zero.  Returns 0 for an invalid suffix, or a
81169695Skan   flag vector describing the suffix.  */
82169695Skanstatic unsigned int
83169695Skaninterpret_float_suffix (const uchar *s, size_t len)
84169695Skan{
85250566Spfg  size_t f = 0, l = 0, i = 0, d = 0, d0 = 0;
86169695Skan
87169695Skan  while (len--)
88169695Skan    switch (s[len])
89169695Skan      {
90259890Spfg      case 'f': case 'F':
91259890Spfg	if (d > 0)
92259890Spfg	  return 0;
93259890Spfg	f++;
94259890Spfg	break;
95259890Spfg      case 'l': case 'L':
96259890Spfg	if (d > 0)
97259890Spfg	  return 0;
98259890Spfg	l++;
99259890Spfg	break;
100169695Skan      case 'i': case 'I':
101169695Skan      case 'j': case 'J': i++; break;
102259890Spfg      case 'd': case 'D': d++; break;
103169695Skan      default:
104169695Skan	return 0;
105169695Skan      }
106169695Skan
107250566Spfg  if (d == 1 && !f && !l) {
108250566Spfg    d = 0;
109250566Spfg    d0 = 1;
110250566Spfg  }
111250566Spfg
112250566Spfg  if (f + d0 + l > 1 || i > 1)
113169695Skan    return 0;
114169695Skan
115169695Skan  /* Allow dd, df, dl suffixes for decimal float constants.  */
116169695Skan  if (d && ((d + f + l != 2) || i))
117169695Skan    return 0;
118169695Skan
119169695Skan  return ((i ? CPP_N_IMAGINARY : 0)
120169695Skan	  | (f ? CPP_N_SMALL :
121250566Spfg	     d0 ? CPP_N_MEDIUM :
122250566Spfg	     l ? CPP_N_LARGE : CPP_N_DEFAULT)
123169695Skan	  | (d ? CPP_N_DFLOAT : 0));
124169695Skan}
125169695Skan
126169695Skan/* Subroutine of cpp_classify_number.  S points to an integer suffix
127169695Skan   of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
128169695Skan   flag vector describing the suffix.  */
129169695Skanstatic unsigned int
130169695Skaninterpret_int_suffix (const uchar *s, size_t len)
131169695Skan{
132169695Skan  size_t u, l, i;
133169695Skan
134169695Skan  u = l = i = 0;
135169695Skan
136169695Skan  while (len--)
137169695Skan    switch (s[len])
138169695Skan      {
139169695Skan      case 'u': case 'U':	u++; break;
140169695Skan      case 'i': case 'I':
141169695Skan      case 'j': case 'J':	i++; break;
142169695Skan      case 'l': case 'L':	l++;
143169695Skan	/* If there are two Ls, they must be adjacent and the same case.  */
144169695Skan	if (l == 2 && s[len] != s[len + 1])
145169695Skan	  return 0;
146169695Skan	break;
147169695Skan      default:
148169695Skan	return 0;
149169695Skan      }
150169695Skan
151169695Skan  if (l > 2 || u > 1 || i > 1)
152169695Skan    return 0;
153169695Skan
154169695Skan  return ((i ? CPP_N_IMAGINARY : 0)
155169695Skan	  | (u ? CPP_N_UNSIGNED : 0)
156169695Skan	  | ((l == 0) ? CPP_N_SMALL
157169695Skan	     : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
158169695Skan}
159169695Skan
160169695Skan/* Categorize numeric constants according to their field (integer,
161169695Skan   floating point, or invalid), radix (decimal, octal, hexadecimal),
162169695Skan   and type suffixes.  */
163169695Skanunsigned int
164169695Skancpp_classify_number (cpp_reader *pfile, const cpp_token *token)
165169695Skan{
166169695Skan  const uchar *str = token->val.str.text;
167169695Skan  const uchar *limit;
168169695Skan  unsigned int max_digit, result, radix;
169169695Skan  enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
170169695Skan
171169695Skan  /* If the lexer has done its job, length one can only be a single
172169695Skan     digit.  Fast-path this very common case.  */
173169695Skan  if (token->val.str.len == 1)
174169695Skan    return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
175169695Skan
176169695Skan  limit = str + token->val.str.len;
177169695Skan  float_flag = NOT_FLOAT;
178169695Skan  max_digit = 0;
179169695Skan  radix = 10;
180169695Skan
181169695Skan  /* First, interpret the radix.  */
182169695Skan  if (*str == '0')
183169695Skan    {
184169695Skan      radix = 8;
185169695Skan      str++;
186169695Skan
187169695Skan      /* Require at least one hex digit to classify it as hex.  */
188169695Skan      if ((*str == 'x' || *str == 'X')
189169695Skan	  && (str[1] == '.' || ISXDIGIT (str[1])))
190169695Skan	{
191169695Skan	  radix = 16;
192169695Skan	  str++;
193169695Skan	}
194255107Spfg      else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
195255107Spfg	{
196255107Spfg	  radix = 2;
197255107Spfg	  str++;
198255107Spfg	}
199169695Skan    }
200169695Skan
201169695Skan  /* Now scan for a well-formed integer or float.  */
202169695Skan  for (;;)
203169695Skan    {
204169695Skan      unsigned int c = *str++;
205169695Skan
206169695Skan      if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
207169695Skan	{
208169695Skan	  c = hex_value (c);
209169695Skan	  if (c > max_digit)
210169695Skan	    max_digit = c;
211169695Skan	}
212169695Skan      else if (c == '.')
213169695Skan	{
214169695Skan	  if (float_flag == NOT_FLOAT)
215169695Skan	    float_flag = AFTER_POINT;
216169695Skan	  else
217169695Skan	    SYNTAX_ERROR ("too many decimal points in number");
218169695Skan	}
219169695Skan      else if ((radix <= 10 && (c == 'e' || c == 'E'))
220169695Skan	       || (radix == 16 && (c == 'p' || c == 'P')))
221169695Skan	{
222169695Skan	  float_flag = AFTER_EXPON;
223169695Skan	  break;
224169695Skan	}
225169695Skan      else
226169695Skan	{
227169695Skan	  /* Start of suffix.  */
228169695Skan	  str--;
229169695Skan	  break;
230169695Skan	}
231169695Skan    }
232169695Skan
233169695Skan  if (float_flag != NOT_FLOAT && radix == 8)
234169695Skan    radix = 10;
235169695Skan
236169695Skan  if (max_digit >= radix)
237255107Spfg    {
238255107Spfg      if (radix == 2)
239255107Spfg	SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
240255107Spfg      else
241255107Spfg	SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
242255107Spfg    }
243169695Skan
244169695Skan  if (float_flag != NOT_FLOAT)
245169695Skan    {
246255107Spfg      if (radix == 2)
247255107Spfg	{
248255107Spfg	  cpp_error (pfile, CPP_DL_ERROR,
249255107Spfg		     "invalid prefix \"0b\" for floating constant");
250255107Spfg	  return CPP_N_INVALID;
251255107Spfg	}
252255107Spfg
253169695Skan      if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
254169695Skan	cpp_error (pfile, CPP_DL_PEDWARN,
255169695Skan		   "use of C99 hexadecimal floating constant");
256169695Skan
257169695Skan      if (float_flag == AFTER_EXPON)
258169695Skan	{
259169695Skan	  if (*str == '+' || *str == '-')
260169695Skan	    str++;
261169695Skan
262169695Skan	  /* Exponent is decimal, even if string is a hex float.  */
263169695Skan	  if (!ISDIGIT (*str))
264169695Skan	    SYNTAX_ERROR ("exponent has no digits");
265169695Skan
266169695Skan	  do
267169695Skan	    str++;
268169695Skan	  while (ISDIGIT (*str));
269169695Skan	}
270169695Skan      else if (radix == 16)
271169695Skan	SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
272169695Skan
273169695Skan      result = interpret_float_suffix (str, limit - str);
274169695Skan      if (result == 0)
275169695Skan	{
276169695Skan	  cpp_error (pfile, CPP_DL_ERROR,
277169695Skan		     "invalid suffix \"%.*s\" on floating constant",
278169695Skan		     (int) (limit - str), str);
279169695Skan	  return CPP_N_INVALID;
280169695Skan	}
281169695Skan
282169695Skan      /* Traditional C didn't accept any floating suffixes.  */
283169695Skan      if (limit != str
284169695Skan	  && CPP_WTRADITIONAL (pfile)
285169695Skan	  && ! cpp_sys_macro_p (pfile))
286169695Skan	cpp_error (pfile, CPP_DL_WARNING,
287169695Skan		   "traditional C rejects the \"%.*s\" suffix",
288169695Skan		   (int) (limit - str), str);
289169695Skan
290250566Spfg      /* A suffix for double is a GCC extension via decimal float support.
291250566Spfg	 If the suffix also specifies an imaginary value we'll catch that
292250566Spfg	 later.  */
293250566Spfg      if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
294250566Spfg	cpp_error (pfile, CPP_DL_PEDWARN,
295250566Spfg		   "suffix for double constant is a GCC extension");
296250566Spfg
297169695Skan      /* Radix must be 10 for decimal floats.  */
298169695Skan      if ((result & CPP_N_DFLOAT) && radix != 10)
299169695Skan        {
300169695Skan          cpp_error (pfile, CPP_DL_ERROR,
301169695Skan                     "invalid suffix \"%.*s\" with hexadecimal floating constant",
302169695Skan                     (int) (limit - str), str);
303169695Skan          return CPP_N_INVALID;
304169695Skan        }
305169695Skan
306169695Skan      result |= CPP_N_FLOATING;
307169695Skan    }
308169695Skan  else
309169695Skan    {
310169695Skan      result = interpret_int_suffix (str, limit - str);
311169695Skan      if (result == 0)
312169695Skan	{
313169695Skan	  cpp_error (pfile, CPP_DL_ERROR,
314169695Skan		     "invalid suffix \"%.*s\" on integer constant",
315169695Skan		     (int) (limit - str), str);
316169695Skan	  return CPP_N_INVALID;
317169695Skan	}
318169695Skan
319169695Skan      /* Traditional C only accepted the 'L' suffix.
320169695Skan         Suppress warning about 'LL' with -Wno-long-long.  */
321169695Skan      if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
322169695Skan	{
323169695Skan	  int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
324169695Skan	  int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
325169695Skan
326169695Skan	  if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
327169695Skan	    cpp_error (pfile, CPP_DL_WARNING,
328169695Skan		       "traditional C rejects the \"%.*s\" suffix",
329169695Skan		       (int) (limit - str), str);
330169695Skan	}
331169695Skan
332169695Skan      if ((result & CPP_N_WIDTH) == CPP_N_LARGE
333169695Skan	  && ! CPP_OPTION (pfile, c99)
334169695Skan	  && CPP_OPTION (pfile, warn_long_long))
335169695Skan	cpp_error (pfile, CPP_DL_PEDWARN,
336169695Skan		   "use of C99 long long integer constant");
337169695Skan
338169695Skan      result |= CPP_N_INTEGER;
339169695Skan    }
340169695Skan
341169695Skan  if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
342169695Skan    cpp_error (pfile, CPP_DL_PEDWARN,
343169695Skan	       "imaginary constants are a GCC extension");
344255107Spfg  if (radix == 2 && CPP_PEDANTIC (pfile))
345255107Spfg    cpp_error (pfile, CPP_DL_PEDWARN,
346255107Spfg	       "binary constants are a GCC extension");
347169695Skan
348169695Skan  if (radix == 10)
349169695Skan    result |= CPP_N_DECIMAL;
350169695Skan  else if (radix == 16)
351169695Skan    result |= CPP_N_HEX;
352255107Spfg  else if (radix == 2)
353255107Spfg    result |= CPP_N_BINARY;
354169695Skan  else
355169695Skan    result |= CPP_N_OCTAL;
356169695Skan
357169695Skan  return result;
358169695Skan
359169695Skan syntax_error:
360169695Skan  return CPP_N_INVALID;
361169695Skan}
362169695Skan
363169695Skan/* cpp_interpret_integer converts an integer constant into a cpp_num,
364169695Skan   of precision options->precision.
365169695Skan
366169695Skan   We do not provide any interface for decimal->float conversion,
367169695Skan   because the preprocessor doesn't need it and we don't want to
368169695Skan   drag in GCC's floating point emulator.  */
369169695Skancpp_num
370169695Skancpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
371169695Skan		       unsigned int type)
372169695Skan{
373169695Skan  const uchar *p, *end;
374169695Skan  cpp_num result;
375169695Skan
376169695Skan  result.low = 0;
377169695Skan  result.high = 0;
378169695Skan  result.unsignedp = !!(type & CPP_N_UNSIGNED);
379169695Skan  result.overflow = false;
380169695Skan
381169695Skan  p = token->val.str.text;
382169695Skan  end = p + token->val.str.len;
383169695Skan
384169695Skan  /* Common case of a single digit.  */
385169695Skan  if (token->val.str.len == 1)
386169695Skan    result.low = p[0] - '0';
387169695Skan  else
388169695Skan    {
389169695Skan      cpp_num_part max;
390169695Skan      size_t precision = CPP_OPTION (pfile, precision);
391169695Skan      unsigned int base = 10, c = 0;
392169695Skan      bool overflow = false;
393169695Skan
394169695Skan      if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
395169695Skan	{
396169695Skan	  base = 8;
397169695Skan	  p++;
398169695Skan	}
399169695Skan      else if ((type & CPP_N_RADIX) == CPP_N_HEX)
400169695Skan	{
401169695Skan	  base = 16;
402169695Skan	  p += 2;
403169695Skan	}
404255107Spfg      else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
405255107Spfg	{
406255107Spfg	  base = 2;
407255107Spfg	  p += 2;
408255107Spfg	}
409169695Skan
410169695Skan      /* We can add a digit to numbers strictly less than this without
411169695Skan	 needing the precision and slowness of double integers.  */
412169695Skan      max = ~(cpp_num_part) 0;
413169695Skan      if (precision < PART_PRECISION)
414169695Skan	max >>= PART_PRECISION - precision;
415169695Skan      max = (max - base + 1) / base + 1;
416169695Skan
417169695Skan      for (; p < end; p++)
418169695Skan	{
419169695Skan	  c = *p;
420169695Skan
421169695Skan	  if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
422169695Skan	    c = hex_value (c);
423169695Skan	  else
424169695Skan	    break;
425169695Skan
426169695Skan	  /* Strict inequality for when max is set to zero.  */
427169695Skan	  if (result.low < max)
428169695Skan	    result.low = result.low * base + c;
429169695Skan	  else
430169695Skan	    {
431169695Skan	      result = append_digit (result, c, base, precision);
432169695Skan	      overflow |= result.overflow;
433169695Skan	      max = 0;
434169695Skan	    }
435169695Skan	}
436169695Skan
437169695Skan      if (overflow)
438169695Skan	cpp_error (pfile, CPP_DL_PEDWARN,
439169695Skan		   "integer constant is too large for its type");
440169695Skan      /* If too big to be signed, consider it unsigned.  Only warn for
441169695Skan	 decimal numbers.  Traditional numbers were always signed (but
442169695Skan	 we still honor an explicit U suffix); but we only have
443169695Skan	 traditional semantics in directives.  */
444169695Skan      else if (!result.unsignedp
445169695Skan	       && !(CPP_OPTION (pfile, traditional)
446169695Skan		    && pfile->state.in_directive)
447169695Skan	       && !num_positive (result, precision))
448169695Skan	{
449169695Skan	  if (base == 10)
450169695Skan	    cpp_error (pfile, CPP_DL_WARNING,
451169695Skan		       "integer constant is so large that it is unsigned");
452169695Skan	  result.unsignedp = true;
453169695Skan	}
454169695Skan    }
455169695Skan
456169695Skan  return result;
457169695Skan}
458169695Skan
459169695Skan/* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE.  */
460169695Skanstatic cpp_num
461169695Skanappend_digit (cpp_num num, int digit, int base, size_t precision)
462169695Skan{
463169695Skan  cpp_num result;
464255107Spfg  unsigned int shift;
465169695Skan  bool overflow;
466169695Skan  cpp_num_part add_high, add_low;
467169695Skan
468255107Spfg  /* Multiply by 2, 8 or 16.  Catching this overflow here means we don't
469169695Skan     need to worry about add_high overflowing.  */
470255107Spfg  switch (base)
471255107Spfg    {
472255107Spfg    case 2:
473255107Spfg      shift = 1;
474255107Spfg      break;
475255107Spfg
476255107Spfg    case 16:
477255107Spfg      shift = 4;
478255107Spfg      break;
479255107Spfg
480255107Spfg    default:
481255107Spfg      shift = 3;
482255107Spfg    }
483169695Skan  overflow = !!(num.high >> (PART_PRECISION - shift));
484169695Skan  result.high = num.high << shift;
485169695Skan  result.low = num.low << shift;
486169695Skan  result.high |= num.low >> (PART_PRECISION - shift);
487169695Skan  result.unsignedp = num.unsignedp;
488169695Skan
489169695Skan  if (base == 10)
490169695Skan    {
491169695Skan      add_low = num.low << 1;
492169695Skan      add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
493169695Skan    }
494169695Skan  else
495169695Skan    add_high = add_low = 0;
496169695Skan
497169695Skan  if (add_low + digit < add_low)
498169695Skan    add_high++;
499169695Skan  add_low += digit;
500259890Spfg
501169695Skan  if (result.low + add_low < result.low)
502169695Skan    add_high++;
503169695Skan  if (result.high + add_high < result.high)
504169695Skan    overflow = true;
505169695Skan
506169695Skan  result.low += add_low;
507169695Skan  result.high += add_high;
508169695Skan  result.overflow = overflow;
509169695Skan
510169695Skan  /* The above code catches overflow of a cpp_num type.  This catches
511169695Skan     overflow of the (possibly shorter) target precision.  */
512169695Skan  num.low = result.low;
513169695Skan  num.high = result.high;
514169695Skan  result = num_trim (result, precision);
515169695Skan  if (!num_eq (result, num))
516169695Skan    result.overflow = true;
517169695Skan
518169695Skan  return result;
519169695Skan}
520169695Skan
521169695Skan/* Handle meeting "defined" in a preprocessor expression.  */
522169695Skanstatic cpp_num
523169695Skanparse_defined (cpp_reader *pfile)
524169695Skan{
525169695Skan  cpp_num result;
526169695Skan  int paren = 0;
527169695Skan  cpp_hashnode *node = 0;
528169695Skan  const cpp_token *token;
529169695Skan  cpp_context *initial_context = pfile->context;
530169695Skan
531169695Skan  /* Don't expand macros.  */
532169695Skan  pfile->state.prevent_expansion++;
533169695Skan
534169695Skan  token = cpp_get_token (pfile);
535169695Skan  if (token->type == CPP_OPEN_PAREN)
536169695Skan    {
537169695Skan      paren = 1;
538169695Skan      token = cpp_get_token (pfile);
539169695Skan    }
540169695Skan
541169695Skan  if (token->type == CPP_NAME)
542169695Skan    {
543169695Skan      node = token->val.node;
544169695Skan      if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
545169695Skan	{
546169695Skan	  cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
547169695Skan	  node = 0;
548169695Skan	}
549169695Skan    }
550169695Skan  else
551169695Skan    {
552169695Skan      cpp_error (pfile, CPP_DL_ERROR,
553169695Skan		 "operator \"defined\" requires an identifier");
554169695Skan      if (token->flags & NAMED_OP)
555169695Skan	{
556169695Skan	  cpp_token op;
557169695Skan
558169695Skan	  op.flags = 0;
559169695Skan	  op.type = token->type;
560169695Skan	  cpp_error (pfile, CPP_DL_ERROR,
561169695Skan		     "(\"%s\" is an alternative token for \"%s\" in C++)",
562169695Skan		     cpp_token_as_text (pfile, token),
563169695Skan		     cpp_token_as_text (pfile, &op));
564169695Skan	}
565169695Skan    }
566169695Skan
567169695Skan  if (node)
568169695Skan    {
569169695Skan      if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
570169695Skan	cpp_error (pfile, CPP_DL_WARNING,
571169695Skan		   "this use of \"defined\" may not be portable");
572169695Skan
573169695Skan      _cpp_mark_macro_used (node);
574169695Skan
575169695Skan      /* A possible controlling macro of the form #if !defined ().
576169695Skan	 _cpp_parse_expr checks there was no other junk on the line.  */
577169695Skan      pfile->mi_ind_cmacro = node;
578169695Skan    }
579169695Skan
580169695Skan  pfile->state.prevent_expansion--;
581169695Skan
582169695Skan  result.unsignedp = false;
583169695Skan  result.high = 0;
584169695Skan  result.overflow = false;
585169695Skan  result.low = node && node->type == NT_MACRO;
586169695Skan  return result;
587169695Skan}
588169695Skan
589169695Skan/* Convert a token into a CPP_NUMBER (an interpreted preprocessing
590169695Skan   number or character constant, or the result of the "defined" or "#"
591169695Skan   operators).  */
592169695Skanstatic cpp_num
593169695Skaneval_token (cpp_reader *pfile, const cpp_token *token)
594169695Skan{
595169695Skan  cpp_num result;
596169695Skan  unsigned int temp;
597169695Skan  int unsignedp = 0;
598169695Skan
599169695Skan  result.unsignedp = false;
600169695Skan  result.overflow = false;
601169695Skan
602169695Skan  switch (token->type)
603169695Skan    {
604169695Skan    case CPP_NUMBER:
605169695Skan      temp = cpp_classify_number (pfile, token);
606169695Skan      switch (temp & CPP_N_CATEGORY)
607169695Skan	{
608169695Skan	case CPP_N_FLOATING:
609169695Skan	  cpp_error (pfile, CPP_DL_ERROR,
610169695Skan		     "floating constant in preprocessor expression");
611169695Skan	  break;
612169695Skan	case CPP_N_INTEGER:
613169695Skan	  if (!(temp & CPP_N_IMAGINARY))
614169695Skan	    return cpp_interpret_integer (pfile, token, temp);
615169695Skan	  cpp_error (pfile, CPP_DL_ERROR,
616169695Skan		     "imaginary number in preprocessor expression");
617169695Skan	  break;
618169695Skan
619169695Skan	case CPP_N_INVALID:
620169695Skan	  /* Error already issued.  */
621169695Skan	  break;
622169695Skan	}
623169695Skan      result.high = result.low = 0;
624169695Skan      break;
625169695Skan
626169695Skan    case CPP_WCHAR:
627169695Skan    case CPP_CHAR:
628169695Skan      {
629169695Skan	cppchar_t cc = cpp_interpret_charconst (pfile, token,
630169695Skan						&temp, &unsignedp);
631169695Skan
632169695Skan	result.high = 0;
633169695Skan	result.low = cc;
634169695Skan	/* Sign-extend the result if necessary.  */
635169695Skan	if (!unsignedp && (cppchar_signed_t) cc < 0)
636169695Skan	  {
637169695Skan	    if (PART_PRECISION > BITS_PER_CPPCHAR_T)
638169695Skan	      result.low |= ~(~(cpp_num_part) 0
639169695Skan			      >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
640169695Skan	    result.high = ~(cpp_num_part) 0;
641169695Skan	    result = num_trim (result, CPP_OPTION (pfile, precision));
642169695Skan	  }
643169695Skan      }
644169695Skan      break;
645169695Skan
646169695Skan    case CPP_NAME:
647169695Skan      if (token->val.node == pfile->spec_nodes.n_defined)
648169695Skan	return parse_defined (pfile);
649169695Skan      else if (CPP_OPTION (pfile, cplusplus)
650169695Skan	       && (token->val.node == pfile->spec_nodes.n_true
651169695Skan		   || token->val.node == pfile->spec_nodes.n_false))
652169695Skan	{
653169695Skan	  result.high = 0;
654169695Skan	  result.low = (token->val.node == pfile->spec_nodes.n_true);
655169695Skan	}
656169695Skan      else
657169695Skan	{
658169695Skan	  result.high = 0;
659169695Skan	  result.low = 0;
660169695Skan	  if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
661169695Skan	    cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined",
662169695Skan		       NODE_NAME (token->val.node));
663169695Skan	}
664169695Skan      break;
665169695Skan
666169695Skan    default: /* CPP_HASH */
667169695Skan      _cpp_test_assertion (pfile, &temp);
668169695Skan      result.high = 0;
669169695Skan      result.low = temp;
670169695Skan    }
671169695Skan
672169695Skan  result.unsignedp = !!unsignedp;
673169695Skan  return result;
674169695Skan}
675169695Skan
676169695Skan/* Operator precedence and flags table.
677169695Skan
678169695SkanAfter an operator is returned from the lexer, if it has priority less
679169695Skanthan the operator on the top of the stack, we reduce the stack by one
680169695Skanoperator and repeat the test.  Since equal priorities do not reduce,
681169695Skanthis is naturally right-associative.
682169695Skan
683169695SkanWe handle left-associative operators by decrementing the priority of
684169695Skanjust-lexed operators by one, but retaining the priority of operators
685169695Skanalready on the stack.
686169695Skan
687169695SkanThe remaining cases are '(' and ')'.  We handle '(' by skipping the
688169695Skanreduction phase completely.  ')' is given lower priority than
689169695Skaneverything else, including '(', effectively forcing a reduction of the
690169695Skanparenthesized expression.  If there is a matching '(', the routine
691169695Skanreduce() exits immediately.  If the normal exit route sees a ')', then
692169695Skanthere cannot have been a matching '(' and an error message is output.
693169695Skan
694169695SkanThe parser assumes all shifted operators require a left operand unless
695169695Skanthe flag NO_L_OPERAND is set.  These semantics are automatic; any
696169695Skanextra semantics need to be handled with operator-specific code.  */
697169695Skan
698169695Skan/* Flags.  If CHECK_PROMOTION, we warn if the effective sign of an
699169695Skan   operand changes because of integer promotions.  */
700169695Skan#define NO_L_OPERAND	(1 << 0)
701169695Skan#define LEFT_ASSOC	(1 << 1)
702169695Skan#define CHECK_PROMOTION	(1 << 2)
703169695Skan
704169695Skan/* Operator to priority map.  Must be in the same order as the first
705169695Skan   N entries of enum cpp_ttype.  */
706169695Skanstatic const struct cpp_operator
707169695Skan{
708169695Skan  uchar prio;
709169695Skan  uchar flags;
710169695Skan} optab[] =
711169695Skan{
712169695Skan  /* EQ */		{0, 0},	/* Shouldn't happen.  */
713169695Skan  /* NOT */		{16, NO_L_OPERAND},
714169695Skan  /* GREATER */		{12, LEFT_ASSOC | CHECK_PROMOTION},
715169695Skan  /* LESS */		{12, LEFT_ASSOC | CHECK_PROMOTION},
716169695Skan  /* PLUS */		{14, LEFT_ASSOC | CHECK_PROMOTION},
717169695Skan  /* MINUS */		{14, LEFT_ASSOC | CHECK_PROMOTION},
718169695Skan  /* MULT */		{15, LEFT_ASSOC | CHECK_PROMOTION},
719169695Skan  /* DIV */		{15, LEFT_ASSOC | CHECK_PROMOTION},
720169695Skan  /* MOD */		{15, LEFT_ASSOC | CHECK_PROMOTION},
721169695Skan  /* AND */		{9, LEFT_ASSOC | CHECK_PROMOTION},
722169695Skan  /* OR */		{7, LEFT_ASSOC | CHECK_PROMOTION},
723169695Skan  /* XOR */		{8, LEFT_ASSOC | CHECK_PROMOTION},
724169695Skan  /* RSHIFT */		{13, LEFT_ASSOC},
725169695Skan  /* LSHIFT */		{13, LEFT_ASSOC},
726169695Skan
727169695Skan  /* COMPL */		{16, NO_L_OPERAND},
728169695Skan  /* AND_AND */		{6, LEFT_ASSOC},
729169695Skan  /* OR_OR */		{5, LEFT_ASSOC},
730169695Skan  /* QUERY */		{3, 0},
731169695Skan  /* COLON */		{4, LEFT_ASSOC | CHECK_PROMOTION},
732169695Skan  /* COMMA */		{2, LEFT_ASSOC},
733169695Skan  /* OPEN_PAREN */	{1, NO_L_OPERAND},
734169695Skan  /* CLOSE_PAREN */	{0, 0},
735169695Skan  /* EOF */		{0, 0},
736169695Skan  /* EQ_EQ */		{11, LEFT_ASSOC},
737169695Skan  /* NOT_EQ */		{11, LEFT_ASSOC},
738169695Skan  /* GREATER_EQ */	{12, LEFT_ASSOC | CHECK_PROMOTION},
739169695Skan  /* LESS_EQ */		{12, LEFT_ASSOC | CHECK_PROMOTION},
740169695Skan  /* UPLUS */		{16, NO_L_OPERAND},
741169695Skan  /* UMINUS */		{16, NO_L_OPERAND}
742169695Skan};
743169695Skan
744169695Skan/* Parse and evaluate a C expression, reading from PFILE.
745169695Skan   Returns the truth value of the expression.
746169695Skan
747169695Skan   The implementation is an operator precedence parser, i.e. a
748169695Skan   bottom-up parser, using a stack for not-yet-reduced tokens.
749169695Skan
750169695Skan   The stack base is op_stack, and the current stack pointer is 'top'.
751169695Skan   There is a stack element for each operator (only), and the most
752169695Skan   recently pushed operator is 'top->op'.  An operand (value) is
753169695Skan   stored in the 'value' field of the stack element of the operator
754169695Skan   that precedes it.  */
755169695Skanbool
756169695Skan_cpp_parse_expr (cpp_reader *pfile)
757169695Skan{
758169695Skan  struct op *top = pfile->op_stack;
759169695Skan  unsigned int lex_count;
760169695Skan  bool saw_leading_not, want_value = true;
761169695Skan
762169695Skan  pfile->state.skip_eval = 0;
763169695Skan
764169695Skan  /* Set up detection of #if ! defined().  */
765169695Skan  pfile->mi_ind_cmacro = 0;
766169695Skan  saw_leading_not = false;
767169695Skan  lex_count = 0;
768169695Skan
769169695Skan  /* Lowest priority operator prevents further reductions.  */
770169695Skan  top->op = CPP_EOF;
771169695Skan
772169695Skan  for (;;)
773169695Skan    {
774169695Skan      struct op op;
775169695Skan
776169695Skan      lex_count++;
777169695Skan      op.token = cpp_get_token (pfile);
778169695Skan      op.op = op.token->type;
779169695Skan
780169695Skan      switch (op.op)
781169695Skan	{
782169695Skan	  /* These tokens convert into values.  */
783169695Skan	case CPP_NUMBER:
784169695Skan	case CPP_CHAR:
785169695Skan	case CPP_WCHAR:
786169695Skan	case CPP_NAME:
787169695Skan	case CPP_HASH:
788169695Skan	  if (!want_value)
789169695Skan	    SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
790169695Skan			   cpp_token_as_text (pfile, op.token));
791169695Skan	  want_value = false;
792169695Skan	  top->value = eval_token (pfile, op.token);
793169695Skan	  continue;
794169695Skan
795169695Skan	case CPP_NOT:
796169695Skan	  saw_leading_not = lex_count == 1;
797169695Skan	  break;
798169695Skan	case CPP_PLUS:
799169695Skan	  if (want_value)
800169695Skan	    op.op = CPP_UPLUS;
801169695Skan	  break;
802169695Skan	case CPP_MINUS:
803169695Skan	  if (want_value)
804169695Skan	    op.op = CPP_UMINUS;
805169695Skan	  break;
806169695Skan
807169695Skan	default:
808169695Skan	  if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
809169695Skan	    SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
810169695Skan			   cpp_token_as_text (pfile, op.token));
811169695Skan	  break;
812169695Skan	}
813169695Skan
814169695Skan      /* Check we have a value or operator as appropriate.  */
815169695Skan      if (optab[op.op].flags & NO_L_OPERAND)
816169695Skan	{
817169695Skan	  if (!want_value)
818169695Skan	    SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
819169695Skan			   cpp_token_as_text (pfile, op.token));
820169695Skan	}
821169695Skan      else if (want_value)
822169695Skan	{
823169695Skan	  /* We want a number (or expression) and haven't got one.
824169695Skan	     Try to emit a specific diagnostic.  */
825169695Skan	  if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
826169695Skan	    SYNTAX_ERROR ("missing expression between '(' and ')'");
827169695Skan
828169695Skan	  if (op.op == CPP_EOF && top->op == CPP_EOF)
829169695Skan 	    SYNTAX_ERROR ("#if with no expression");
830169695Skan
831169695Skan 	  if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
832169695Skan 	    SYNTAX_ERROR2 ("operator '%s' has no right operand",
833169695Skan 			   cpp_token_as_text (pfile, top->token));
834169695Skan	  else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
835169695Skan	    /* Complain about missing paren during reduction.  */;
836169695Skan	  else
837169695Skan	    SYNTAX_ERROR2 ("operator '%s' has no left operand",
838169695Skan			   cpp_token_as_text (pfile, op.token));
839169695Skan	}
840169695Skan
841169695Skan      top = reduce (pfile, top, op.op);
842169695Skan      if (!top)
843169695Skan	goto syntax_error;
844169695Skan
845169695Skan      if (op.op == CPP_EOF)
846169695Skan	break;
847169695Skan
848169695Skan      switch (op.op)
849169695Skan	{
850169695Skan	case CPP_CLOSE_PAREN:
851169695Skan	  continue;
852169695Skan	case CPP_OR_OR:
853169695Skan	  if (!num_zerop (top->value))
854169695Skan	    pfile->state.skip_eval++;
855169695Skan	  break;
856169695Skan	case CPP_AND_AND:
857169695Skan	case CPP_QUERY:
858169695Skan	  if (num_zerop (top->value))
859169695Skan	    pfile->state.skip_eval++;
860169695Skan	  break;
861169695Skan	case CPP_COLON:
862169695Skan	  if (top->op != CPP_QUERY)
863169695Skan	    SYNTAX_ERROR (" ':' without preceding '?'");
864169695Skan	  if (!num_zerop (top[-1].value)) /* Was '?' condition true?  */
865169695Skan	    pfile->state.skip_eval++;
866169695Skan	  else
867169695Skan	    pfile->state.skip_eval--;
868169695Skan	default:
869169695Skan	  break;
870169695Skan	}
871169695Skan
872169695Skan      want_value = true;
873169695Skan
874169695Skan      /* Check for and handle stack overflow.  */
875169695Skan      if (++top == pfile->op_limit)
876169695Skan	top = _cpp_expand_op_stack (pfile);
877169695Skan
878169695Skan      top->op = op.op;
879169695Skan      top->token = op.token;
880169695Skan    }
881169695Skan
882169695Skan  /* The controlling macro expression is only valid if we called lex 3
883169695Skan     times: <!> <defined expression> and <EOF>.  push_conditional ()
884169695Skan     checks that we are at top-of-file.  */
885169695Skan  if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
886169695Skan    pfile->mi_ind_cmacro = 0;
887169695Skan
888169695Skan  if (top != pfile->op_stack)
889169695Skan    {
890169695Skan      cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in #if");
891169695Skan    syntax_error:
892169695Skan      return false;  /* Return false on syntax error.  */
893169695Skan    }
894169695Skan
895169695Skan  return !num_zerop (top->value);
896169695Skan}
897169695Skan
898169695Skan/* Reduce the operator / value stack if possible, in preparation for
899169695Skan   pushing operator OP.  Returns NULL on error, otherwise the top of
900169695Skan   the stack.  */
901169695Skanstatic struct op *
902169695Skanreduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
903169695Skan{
904169695Skan  unsigned int prio;
905169695Skan
906169695Skan  if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
907169695Skan    {
908169695Skan    bad_op:
909169695Skan      cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
910169695Skan      return 0;
911169695Skan    }
912169695Skan
913169695Skan  if (op == CPP_OPEN_PAREN)
914169695Skan    return top;
915169695Skan
916169695Skan  /* Decrement the priority of left-associative operators to force a
917169695Skan     reduction with operators of otherwise equal priority.  */
918169695Skan  prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
919169695Skan  while (prio < optab[top->op].prio)
920169695Skan    {
921169695Skan      if (CPP_OPTION (pfile, warn_num_sign_change)
922169695Skan	  && optab[top->op].flags & CHECK_PROMOTION)
923169695Skan	check_promotion (pfile, top);
924169695Skan
925169695Skan      switch (top->op)
926169695Skan	{
927169695Skan	case CPP_UPLUS:
928169695Skan	case CPP_UMINUS:
929169695Skan	case CPP_NOT:
930169695Skan	case CPP_COMPL:
931169695Skan	  top[-1].value = num_unary_op (pfile, top->value, top->op);
932169695Skan	  break;
933169695Skan
934169695Skan	case CPP_PLUS:
935169695Skan	case CPP_MINUS:
936169695Skan	case CPP_RSHIFT:
937169695Skan	case CPP_LSHIFT:
938169695Skan	case CPP_COMMA:
939169695Skan	  top[-1].value = num_binary_op (pfile, top[-1].value,
940169695Skan					 top->value, top->op);
941169695Skan	  break;
942169695Skan
943169695Skan	case CPP_GREATER:
944169695Skan	case CPP_LESS:
945169695Skan	case CPP_GREATER_EQ:
946169695Skan	case CPP_LESS_EQ:
947169695Skan	  top[-1].value
948169695Skan	    = num_inequality_op (pfile, top[-1].value, top->value, top->op);
949169695Skan	  break;
950169695Skan
951169695Skan	case CPP_EQ_EQ:
952169695Skan	case CPP_NOT_EQ:
953169695Skan	  top[-1].value
954169695Skan	    = num_equality_op (pfile, top[-1].value, top->value, top->op);
955169695Skan	  break;
956169695Skan
957169695Skan	case CPP_AND:
958169695Skan	case CPP_OR:
959169695Skan	case CPP_XOR:
960169695Skan	  top[-1].value
961169695Skan	    = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
962169695Skan	  break;
963169695Skan
964169695Skan	case CPP_MULT:
965169695Skan	  top[-1].value = num_mul (pfile, top[-1].value, top->value);
966169695Skan	  break;
967169695Skan
968169695Skan	case CPP_DIV:
969169695Skan	case CPP_MOD:
970169695Skan	  top[-1].value = num_div_op (pfile, top[-1].value,
971169695Skan				      top->value, top->op);
972169695Skan	  break;
973169695Skan
974169695Skan	case CPP_OR_OR:
975169695Skan	  top--;
976169695Skan	  if (!num_zerop (top->value))
977169695Skan	    pfile->state.skip_eval--;
978169695Skan	  top->value.low = (!num_zerop (top->value)
979169695Skan			    || !num_zerop (top[1].value));
980169695Skan	  top->value.high = 0;
981169695Skan	  top->value.unsignedp = false;
982169695Skan	  top->value.overflow = false;
983169695Skan	  continue;
984169695Skan
985169695Skan	case CPP_AND_AND:
986169695Skan	  top--;
987169695Skan	  if (num_zerop (top->value))
988169695Skan	    pfile->state.skip_eval--;
989169695Skan	  top->value.low = (!num_zerop (top->value)
990169695Skan			    && !num_zerop (top[1].value));
991169695Skan	  top->value.high = 0;
992169695Skan	  top->value.unsignedp = false;
993169695Skan	  top->value.overflow = false;
994169695Skan	  continue;
995169695Skan
996169695Skan	case CPP_OPEN_PAREN:
997169695Skan	  if (op != CPP_CLOSE_PAREN)
998169695Skan	    {
999169695Skan	      cpp_error (pfile, CPP_DL_ERROR, "missing ')' in expression");
1000169695Skan	      return 0;
1001169695Skan	    }
1002169695Skan	  top--;
1003169695Skan	  top->value = top[1].value;
1004169695Skan	  return top;
1005169695Skan
1006169695Skan	case CPP_COLON:
1007169695Skan	  top -= 2;
1008169695Skan	  if (!num_zerop (top->value))
1009169695Skan	    {
1010169695Skan	      pfile->state.skip_eval--;
1011169695Skan	      top->value = top[1].value;
1012169695Skan	    }
1013169695Skan	  else
1014169695Skan	    top->value = top[2].value;
1015169695Skan	  top->value.unsignedp = (top[1].value.unsignedp
1016169695Skan				  || top[2].value.unsignedp);
1017169695Skan	  continue;
1018169695Skan
1019169695Skan	case CPP_QUERY:
1020169695Skan	  cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1021169695Skan	  return 0;
1022169695Skan
1023169695Skan	default:
1024169695Skan	  goto bad_op;
1025169695Skan	}
1026169695Skan
1027169695Skan      top--;
1028169695Skan      if (top->value.overflow && !pfile->state.skip_eval)
1029169695Skan	cpp_error (pfile, CPP_DL_PEDWARN,
1030169695Skan		   "integer overflow in preprocessor expression");
1031169695Skan    }
1032169695Skan
1033169695Skan  if (op == CPP_CLOSE_PAREN)
1034169695Skan    {
1035169695Skan      cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1036169695Skan      return 0;
1037169695Skan    }
1038169695Skan
1039169695Skan  return top;
1040169695Skan}
1041169695Skan
1042169695Skan/* Returns the position of the old top of stack after expansion.  */
1043169695Skanstruct op *
1044169695Skan_cpp_expand_op_stack (cpp_reader *pfile)
1045169695Skan{
1046169695Skan  size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1047169695Skan  size_t new_size = old_size * 2 + 20;
1048169695Skan
1049169695Skan  pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1050169695Skan  pfile->op_limit = pfile->op_stack + new_size;
1051169695Skan
1052169695Skan  return pfile->op_stack + old_size;
1053169695Skan}
1054169695Skan
1055169695Skan/* Emits a warning if the effective sign of either operand of OP
1056169695Skan   changes because of integer promotions.  */
1057169695Skanstatic void
1058169695Skancheck_promotion (cpp_reader *pfile, const struct op *op)
1059169695Skan{
1060169695Skan  if (op->value.unsignedp == op[-1].value.unsignedp)
1061169695Skan    return;
1062169695Skan
1063169695Skan  if (op->value.unsignedp)
1064169695Skan    {
1065169695Skan      if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1066169695Skan	cpp_error (pfile, CPP_DL_WARNING,
1067169695Skan		   "the left operand of \"%s\" changes sign when promoted",
1068169695Skan		   cpp_token_as_text (pfile, op->token));
1069169695Skan    }
1070169695Skan  else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1071169695Skan    cpp_error (pfile, CPP_DL_WARNING,
1072169695Skan	       "the right operand of \"%s\" changes sign when promoted",
1073169695Skan	       cpp_token_as_text (pfile, op->token));
1074169695Skan}
1075169695Skan
1076169695Skan/* Clears the unused high order bits of the number pointed to by PNUM.  */
1077169695Skanstatic cpp_num
1078169695Skannum_trim (cpp_num num, size_t precision)
1079169695Skan{
1080169695Skan  if (precision > PART_PRECISION)
1081169695Skan    {
1082169695Skan      precision -= PART_PRECISION;
1083169695Skan      if (precision < PART_PRECISION)
1084169695Skan	num.high &= ((cpp_num_part) 1 << precision) - 1;
1085169695Skan    }
1086169695Skan  else
1087169695Skan    {
1088169695Skan      if (precision < PART_PRECISION)
1089169695Skan	num.low &= ((cpp_num_part) 1 << precision) - 1;
1090169695Skan      num.high = 0;
1091169695Skan    }
1092169695Skan
1093169695Skan  return num;
1094169695Skan}
1095169695Skan
1096169695Skan/* True iff A (presumed signed) >= 0.  */
1097169695Skanstatic bool
1098169695Skannum_positive (cpp_num num, size_t precision)
1099169695Skan{
1100169695Skan  if (precision > PART_PRECISION)
1101169695Skan    {
1102169695Skan      precision -= PART_PRECISION;
1103169695Skan      return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1104169695Skan    }
1105169695Skan
1106169695Skan  return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1107169695Skan}
1108169695Skan
1109169695Skan/* Sign extend a number, with PRECISION significant bits and all
1110169695Skan   others assumed clear, to fill out a cpp_num structure.  */
1111169695Skancpp_num
1112169695Skancpp_num_sign_extend (cpp_num num, size_t precision)
1113169695Skan{
1114169695Skan  if (!num.unsignedp)
1115169695Skan    {
1116169695Skan      if (precision > PART_PRECISION)
1117169695Skan	{
1118169695Skan	  precision -= PART_PRECISION;
1119169695Skan	  if (precision < PART_PRECISION
1120169695Skan	      && (num.high & (cpp_num_part) 1 << (precision - 1)))
1121169695Skan	    num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1122169695Skan	}
1123169695Skan      else if (num.low & (cpp_num_part) 1 << (precision - 1))
1124169695Skan	{
1125169695Skan	  if (precision < PART_PRECISION)
1126169695Skan	    num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1127169695Skan	  num.high = ~(cpp_num_part) 0;
1128169695Skan	}
1129169695Skan    }
1130169695Skan
1131169695Skan  return num;
1132169695Skan}
1133169695Skan
1134169695Skan/* Returns the negative of NUM.  */
1135169695Skanstatic cpp_num
1136169695Skannum_negate (cpp_num num, size_t precision)
1137169695Skan{
1138169695Skan  cpp_num copy;
1139169695Skan
1140169695Skan  copy = num;
1141169695Skan  num.high = ~num.high;
1142169695Skan  num.low = ~num.low;
1143169695Skan  if (++num.low == 0)
1144169695Skan    num.high++;
1145169695Skan  num = num_trim (num, precision);
1146169695Skan  num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1147169695Skan
1148169695Skan  return num;
1149169695Skan}
1150169695Skan
1151169695Skan/* Returns true if A >= B.  */
1152169695Skanstatic bool
1153169695Skannum_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1154169695Skan{
1155169695Skan  bool unsignedp;
1156169695Skan
1157169695Skan  unsignedp = pa.unsignedp || pb.unsignedp;
1158169695Skan
1159169695Skan  if (!unsignedp)
1160169695Skan    {
1161169695Skan      /* Both numbers have signed type.  If they are of different
1162169695Skan       sign, the answer is the sign of A.  */
1163169695Skan      unsignedp = num_positive (pa, precision);
1164169695Skan
1165169695Skan      if (unsignedp != num_positive (pb, precision))
1166169695Skan	return unsignedp;
1167169695Skan
1168169695Skan      /* Otherwise we can do an unsigned comparison.  */
1169169695Skan    }
1170169695Skan
1171169695Skan  return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1172169695Skan}
1173169695Skan
1174169695Skan/* Returns LHS OP RHS, where OP is a bit-wise operation.  */
1175169695Skanstatic cpp_num
1176169695Skannum_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1177169695Skan		cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1178169695Skan{
1179169695Skan  lhs.overflow = false;
1180169695Skan  lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1181169695Skan
1182169695Skan  /* As excess precision is zeroed, there is no need to num_trim () as
1183169695Skan     these operations cannot introduce a set bit there.  */
1184169695Skan  if (op == CPP_AND)
1185169695Skan    {
1186169695Skan      lhs.low &= rhs.low;
1187169695Skan      lhs.high &= rhs.high;
1188169695Skan    }
1189169695Skan  else if (op == CPP_OR)
1190169695Skan    {
1191169695Skan      lhs.low |= rhs.low;
1192169695Skan      lhs.high |= rhs.high;
1193169695Skan    }
1194169695Skan  else
1195169695Skan    {
1196169695Skan      lhs.low ^= rhs.low;
1197169695Skan      lhs.high ^= rhs.high;
1198169695Skan    }
1199169695Skan
1200169695Skan  return lhs;
1201169695Skan}
1202169695Skan
1203169695Skan/* Returns LHS OP RHS, where OP is an inequality.  */
1204169695Skanstatic cpp_num
1205169695Skannum_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1206169695Skan		   enum cpp_ttype op)
1207169695Skan{
1208169695Skan  bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1209169695Skan
1210169695Skan  if (op == CPP_GREATER_EQ)
1211169695Skan    lhs.low = gte;
1212169695Skan  else if (op == CPP_LESS)
1213169695Skan    lhs.low = !gte;
1214169695Skan  else if (op == CPP_GREATER)
1215169695Skan    lhs.low = gte && !num_eq (lhs, rhs);
1216169695Skan  else /* CPP_LESS_EQ.  */
1217169695Skan    lhs.low = !gte || num_eq (lhs, rhs);
1218169695Skan
1219169695Skan  lhs.high = 0;
1220169695Skan  lhs.overflow = false;
1221169695Skan  lhs.unsignedp = false;
1222169695Skan  return lhs;
1223169695Skan}
1224169695Skan
1225169695Skan/* Returns LHS OP RHS, where OP is == or !=.  */
1226169695Skanstatic cpp_num
1227169695Skannum_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1228169695Skan		 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1229169695Skan{
1230169695Skan  /* Work around a 3.0.4 bug; see PR 6950.  */
1231169695Skan  bool eq = num_eq (lhs, rhs);
1232169695Skan  if (op == CPP_NOT_EQ)
1233169695Skan    eq = !eq;
1234169695Skan  lhs.low = eq;
1235169695Skan  lhs.high = 0;
1236169695Skan  lhs.overflow = false;
1237169695Skan  lhs.unsignedp = false;
1238169695Skan  return lhs;
1239169695Skan}
1240169695Skan
1241169695Skan/* Shift NUM, of width PRECISION, right by N bits.  */
1242169695Skanstatic cpp_num
1243169695Skannum_rshift (cpp_num num, size_t precision, size_t n)
1244169695Skan{
1245169695Skan  cpp_num_part sign_mask;
1246169695Skan  bool x = num_positive (num, precision);
1247169695Skan
1248169695Skan  if (num.unsignedp || x)
1249169695Skan    sign_mask = 0;
1250169695Skan  else
1251169695Skan    sign_mask = ~(cpp_num_part) 0;
1252169695Skan
1253169695Skan  if (n >= precision)
1254169695Skan    num.high = num.low = sign_mask;
1255169695Skan  else
1256169695Skan    {
1257169695Skan      /* Sign-extend.  */
1258169695Skan      if (precision < PART_PRECISION)
1259169695Skan	num.high = sign_mask, num.low |= sign_mask << precision;
1260169695Skan      else if (precision < 2 * PART_PRECISION)
1261169695Skan	num.high |= sign_mask << (precision - PART_PRECISION);
1262169695Skan
1263169695Skan      if (n >= PART_PRECISION)
1264169695Skan	{
1265169695Skan	  n -= PART_PRECISION;
1266169695Skan	  num.low = num.high;
1267169695Skan	  num.high = sign_mask;
1268169695Skan	}
1269169695Skan
1270169695Skan      if (n)
1271169695Skan	{
1272169695Skan	  num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1273169695Skan	  num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1274169695Skan	}
1275169695Skan    }
1276169695Skan
1277169695Skan  num = num_trim (num, precision);
1278169695Skan  num.overflow = false;
1279169695Skan  return num;
1280169695Skan}
1281169695Skan
1282169695Skan/* Shift NUM, of width PRECISION, left by N bits.  */
1283169695Skanstatic cpp_num
1284169695Skannum_lshift (cpp_num num, size_t precision, size_t n)
1285169695Skan{
1286169695Skan  if (n >= precision)
1287169695Skan    {
1288169695Skan      num.overflow = !num.unsignedp && !num_zerop (num);
1289169695Skan      num.high = num.low = 0;
1290169695Skan    }
1291169695Skan  else
1292169695Skan    {
1293169695Skan      cpp_num orig, maybe_orig;
1294169695Skan      size_t m = n;
1295169695Skan
1296169695Skan      orig = num;
1297169695Skan      if (m >= PART_PRECISION)
1298169695Skan	{
1299169695Skan	  m -= PART_PRECISION;
1300169695Skan	  num.high = num.low;
1301169695Skan	  num.low = 0;
1302169695Skan	}
1303169695Skan      if (m)
1304169695Skan	{
1305169695Skan	  num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1306169695Skan	  num.low <<= m;
1307169695Skan	}
1308169695Skan      num = num_trim (num, precision);
1309169695Skan
1310169695Skan      if (num.unsignedp)
1311169695Skan	num.overflow = false;
1312169695Skan      else
1313169695Skan	{
1314169695Skan	  maybe_orig = num_rshift (num, precision, n);
1315169695Skan	  num.overflow = !num_eq (orig, maybe_orig);
1316169695Skan	}
1317169695Skan    }
1318169695Skan
1319169695Skan  return num;
1320169695Skan}
1321169695Skan
1322169695Skan/* The four unary operators: +, -, ! and ~.  */
1323169695Skanstatic cpp_num
1324169695Skannum_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1325169695Skan{
1326169695Skan  switch (op)
1327169695Skan    {
1328169695Skan    case CPP_UPLUS:
1329169695Skan      if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1330169695Skan	cpp_error (pfile, CPP_DL_WARNING,
1331169695Skan		   "traditional C rejects the unary plus operator");
1332169695Skan      num.overflow = false;
1333169695Skan      break;
1334169695Skan
1335169695Skan    case CPP_UMINUS:
1336169695Skan      num = num_negate (num, CPP_OPTION (pfile, precision));
1337169695Skan      break;
1338169695Skan
1339169695Skan    case CPP_COMPL:
1340169695Skan      num.high = ~num.high;
1341169695Skan      num.low = ~num.low;
1342169695Skan      num = num_trim (num, CPP_OPTION (pfile, precision));
1343169695Skan      num.overflow = false;
1344169695Skan      break;
1345169695Skan
1346169695Skan    default: /* case CPP_NOT: */
1347169695Skan      num.low = num_zerop (num);
1348169695Skan      num.high = 0;
1349169695Skan      num.overflow = false;
1350169695Skan      num.unsignedp = false;
1351169695Skan      break;
1352169695Skan    }
1353169695Skan
1354169695Skan  return num;
1355169695Skan}
1356169695Skan
1357169695Skan/* The various binary operators.  */
1358169695Skanstatic cpp_num
1359169695Skannum_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1360169695Skan{
1361169695Skan  cpp_num result;
1362169695Skan  size_t precision = CPP_OPTION (pfile, precision);
1363169695Skan  size_t n;
1364169695Skan
1365169695Skan  switch (op)
1366169695Skan    {
1367169695Skan      /* Shifts.  */
1368169695Skan    case CPP_LSHIFT:
1369169695Skan    case CPP_RSHIFT:
1370169695Skan      if (!rhs.unsignedp && !num_positive (rhs, precision))
1371169695Skan	{
1372169695Skan	  /* A negative shift is a positive shift the other way.  */
1373169695Skan	  if (op == CPP_LSHIFT)
1374169695Skan	    op = CPP_RSHIFT;
1375169695Skan	  else
1376169695Skan	    op = CPP_LSHIFT;
1377169695Skan	  rhs = num_negate (rhs, precision);
1378169695Skan	}
1379169695Skan      if (rhs.high)
1380169695Skan	n = ~0;			/* Maximal.  */
1381169695Skan      else
1382169695Skan	n = rhs.low;
1383169695Skan      if (op == CPP_LSHIFT)
1384169695Skan	lhs = num_lshift (lhs, precision, n);
1385169695Skan      else
1386169695Skan	lhs = num_rshift (lhs, precision, n);
1387169695Skan      break;
1388169695Skan
1389169695Skan      /* Arithmetic.  */
1390169695Skan    case CPP_MINUS:
1391169695Skan      rhs = num_negate (rhs, precision);
1392169695Skan    case CPP_PLUS:
1393169695Skan      result.low = lhs.low + rhs.low;
1394169695Skan      result.high = lhs.high + rhs.high;
1395169695Skan      if (result.low < lhs.low)
1396169695Skan	result.high++;
1397169695Skan      result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1398169695Skan      result.overflow = false;
1399169695Skan
1400169695Skan      result = num_trim (result, precision);
1401169695Skan      if (!result.unsignedp)
1402169695Skan	{
1403169695Skan	  bool lhsp = num_positive (lhs, precision);
1404169695Skan	  result.overflow = (lhsp == num_positive (rhs, precision)
1405169695Skan			     && lhsp != num_positive (result, precision));
1406169695Skan	}
1407169695Skan      return result;
1408169695Skan
1409169695Skan      /* Comma.  */
1410169695Skan    default: /* case CPP_COMMA: */
1411169695Skan      if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1412169695Skan				   || !pfile->state.skip_eval))
1413169695Skan	cpp_error (pfile, CPP_DL_PEDWARN,
1414169695Skan		   "comma operator in operand of #if");
1415169695Skan      lhs = rhs;
1416169695Skan      break;
1417169695Skan    }
1418169695Skan
1419169695Skan  return lhs;
1420169695Skan}
1421169695Skan
1422169695Skan/* Multiplies two unsigned cpp_num_parts to give a cpp_num.  This
1423169695Skan   cannot overflow.  */
1424169695Skanstatic cpp_num
1425169695Skannum_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1426169695Skan{
1427169695Skan  cpp_num result;
1428169695Skan  cpp_num_part middle[2], temp;
1429169695Skan
1430169695Skan  result.low = LOW_PART (lhs) * LOW_PART (rhs);
1431169695Skan  result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1432169695Skan
1433169695Skan  middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1434169695Skan  middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1435169695Skan
1436169695Skan  temp = result.low;
1437169695Skan  result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1438169695Skan  if (result.low < temp)
1439169695Skan    result.high++;
1440169695Skan
1441169695Skan  temp = result.low;
1442169695Skan  result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1443169695Skan  if (result.low < temp)
1444169695Skan    result.high++;
1445169695Skan
1446169695Skan  result.high += HIGH_PART (middle[0]);
1447169695Skan  result.high += HIGH_PART (middle[1]);
1448169695Skan  result.unsignedp = true;
1449169695Skan  result.overflow = false;
1450169695Skan
1451169695Skan  return result;
1452169695Skan}
1453169695Skan
1454169695Skan/* Multiply two preprocessing numbers.  */
1455169695Skanstatic cpp_num
1456169695Skannum_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1457169695Skan{
1458169695Skan  cpp_num result, temp;
1459169695Skan  bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1460169695Skan  bool overflow, negate = false;
1461169695Skan  size_t precision = CPP_OPTION (pfile, precision);
1462169695Skan
1463169695Skan  /* Prepare for unsigned multiplication.  */
1464169695Skan  if (!unsignedp)
1465169695Skan    {
1466169695Skan      if (!num_positive (lhs, precision))
1467169695Skan	negate = !negate, lhs = num_negate (lhs, precision);
1468169695Skan      if (!num_positive (rhs, precision))
1469169695Skan	negate = !negate, rhs = num_negate (rhs, precision);
1470169695Skan    }
1471169695Skan
1472169695Skan  overflow = lhs.high && rhs.high;
1473169695Skan  result = num_part_mul (lhs.low, rhs.low);
1474169695Skan
1475169695Skan  temp = num_part_mul (lhs.high, rhs.low);
1476169695Skan  result.high += temp.low;
1477169695Skan  if (temp.high)
1478169695Skan    overflow = true;
1479169695Skan
1480169695Skan  temp = num_part_mul (lhs.low, rhs.high);
1481169695Skan  result.high += temp.low;
1482169695Skan  if (temp.high)
1483169695Skan    overflow = true;
1484169695Skan
1485169695Skan  temp.low = result.low, temp.high = result.high;
1486169695Skan  result = num_trim (result, precision);
1487169695Skan  if (!num_eq (result, temp))
1488169695Skan    overflow = true;
1489169695Skan
1490169695Skan  if (negate)
1491169695Skan    result = num_negate (result, precision);
1492169695Skan
1493169695Skan  if (unsignedp)
1494169695Skan    result.overflow = false;
1495169695Skan  else
1496169695Skan    result.overflow = overflow || (num_positive (result, precision) ^ !negate
1497169695Skan				   && !num_zerop (result));
1498169695Skan  result.unsignedp = unsignedp;
1499169695Skan
1500169695Skan  return result;
1501169695Skan}
1502169695Skan
1503169695Skan/* Divide two preprocessing numbers, returning the answer or the
1504169695Skan   remainder depending upon OP.  */
1505169695Skanstatic cpp_num
1506169695Skannum_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1507169695Skan{
1508169695Skan  cpp_num result, sub;
1509169695Skan  cpp_num_part mask;
1510169695Skan  bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1511169695Skan  bool negate = false, lhs_neg = false;
1512169695Skan  size_t i, precision = CPP_OPTION (pfile, precision);
1513169695Skan
1514169695Skan  /* Prepare for unsigned division.  */
1515169695Skan  if (!unsignedp)
1516169695Skan    {
1517169695Skan      if (!num_positive (lhs, precision))
1518169695Skan	negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1519169695Skan      if (!num_positive (rhs, precision))
1520169695Skan	negate = !negate, rhs = num_negate (rhs, precision);
1521169695Skan    }
1522169695Skan
1523169695Skan  /* Find the high bit.  */
1524169695Skan  if (rhs.high)
1525169695Skan    {
1526169695Skan      i = precision - 1;
1527169695Skan      mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1528169695Skan      for (; ; i--, mask >>= 1)
1529169695Skan	if (rhs.high & mask)
1530169695Skan	  break;
1531169695Skan    }
1532169695Skan  else if (rhs.low)
1533169695Skan    {
1534169695Skan      if (precision > PART_PRECISION)
1535169695Skan	i = precision - PART_PRECISION - 1;
1536169695Skan      else
1537169695Skan	i = precision - 1;
1538169695Skan      mask = (cpp_num_part) 1 << i;
1539169695Skan      for (; ; i--, mask >>= 1)
1540169695Skan	if (rhs.low & mask)
1541169695Skan	  break;
1542169695Skan    }
1543169695Skan  else
1544169695Skan    {
1545169695Skan      if (!pfile->state.skip_eval)
1546169695Skan	cpp_error (pfile, CPP_DL_ERROR, "division by zero in #if");
1547169695Skan      return lhs;
1548169695Skan    }
1549169695Skan
1550169695Skan  /* First nonzero bit of RHS is bit I.  Do naive division by
1551169695Skan     shifting the RHS fully left, and subtracting from LHS if LHS is
1552169695Skan     at least as big, and then repeating but with one less shift.
1553169695Skan     This is not very efficient, but is easy to understand.  */
1554169695Skan
1555169695Skan  rhs.unsignedp = true;
1556169695Skan  lhs.unsignedp = true;
1557169695Skan  i = precision - i - 1;
1558169695Skan  sub = num_lshift (rhs, precision, i);
1559169695Skan
1560169695Skan  result.high = result.low = 0;
1561169695Skan  for (;;)
1562169695Skan    {
1563169695Skan      if (num_greater_eq (lhs, sub, precision))
1564169695Skan	{
1565169695Skan	  lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1566169695Skan	  if (i >= PART_PRECISION)
1567169695Skan	    result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1568169695Skan	  else
1569169695Skan	    result.low |= (cpp_num_part) 1 << i;
1570169695Skan	}
1571169695Skan      if (i-- == 0)
1572169695Skan	break;
1573169695Skan      sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1574169695Skan      sub.high >>= 1;
1575169695Skan    }
1576169695Skan
1577169695Skan  /* We divide so that the remainder has the sign of the LHS.  */
1578169695Skan  if (op == CPP_DIV)
1579169695Skan    {
1580169695Skan      result.unsignedp = unsignedp;
1581169695Skan      result.overflow = false;
1582169695Skan      if (!unsignedp)
1583169695Skan	{
1584169695Skan	  if (negate)
1585169695Skan	    result = num_negate (result, precision);
1586259890Spfg	  result.overflow = (num_positive (result, precision) ^ !negate
1587259890Spfg			     && !num_zerop (result));
1588169695Skan	}
1589169695Skan
1590169695Skan      return result;
1591169695Skan    }
1592169695Skan
1593169695Skan  /* CPP_MOD.  */
1594169695Skan  lhs.unsignedp = unsignedp;
1595169695Skan  lhs.overflow = false;
1596169695Skan  if (lhs_neg)
1597169695Skan    lhs = num_negate (lhs, precision);
1598169695Skan
1599169695Skan  return lhs;
1600169695Skan}
1601