xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/c-family/c-cppbuiltin.c (revision 154bfe8e089c1a0a4e9ed8414f08d3da90949162)
1 /* Define builtin-in macros for the C family front ends.
2    Copyright (C) 2002-2018 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10 
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19 
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "target.h"
24 #include "c-common.h"
25 #include "memmodel.h"
26 #include "tm_p.h"		/* For TARGET_CPU_CPP_BUILTINS & friends.  */
27 #include "stringpool.h"
28 #include "stor-layout.h"
29 #include "flags.h"
30 #include "c-pragma.h"
31 #include "output.h"		/* For user_label_prefix.  */
32 #include "debug.h"		/* For dwarf2out_do_cfi_asm.  */
33 #include "common/common-target.h"
34 #include "cpp-id-data.h"
35 #include "cppbuiltin.h"
36 
37 #ifndef TARGET_OS_CPP_BUILTINS
38 # define TARGET_OS_CPP_BUILTINS()
39 #endif
40 
41 #ifndef TARGET_OBJFMT_CPP_BUILTINS
42 # define TARGET_OBJFMT_CPP_BUILTINS()
43 #endif
44 
45 #ifndef REGISTER_PREFIX
46 #define REGISTER_PREFIX ""
47 #endif
48 
49 /* Non-static as some targets don't use it.  */
50 static void builtin_define_with_hex_fp_value (const char *, tree,
51 					      int, const char *,
52 					      const char *,
53 					      const char *);
54 static void builtin_define_stdint_macros (void);
55 static void builtin_define_constants (const char *, tree);
56 static void builtin_define_type_max (const char *, tree);
57 static void builtin_define_type_minmax (const char *, const char *, tree);
58 static void builtin_define_type_width (const char *, tree, tree);
59 static void builtin_define_float_constants (const char *,
60 					    const char *,
61 					    const char *,
62 					    const char *,
63 					    tree);
64 
65 /* Return true if MODE provides a fast multiply/add (FMA) builtin function.
66    Originally this function used the fma optab, but that doesn't work with
67    -save-temps, so just rely on the HAVE_fma macros for the standard floating
68    point types.  */
69 
70 static bool
71 mode_has_fma (machine_mode mode)
72 {
73   switch (mode)
74     {
75 #ifdef HAVE_fmasf4
76     case E_SFmode:
77       return !!HAVE_fmasf4;
78 #endif
79 
80 #ifdef HAVE_fmadf4
81     case E_DFmode:
82       return !!HAVE_fmadf4;
83 #endif
84 
85 #ifdef HAVE_fmakf4	/* PowerPC if long double != __float128.  */
86     case E_KFmode:
87       return !!HAVE_fmakf4;
88 #endif
89 
90 #ifdef HAVE_fmaxf4
91     case E_XFmode:
92       return !!HAVE_fmaxf4;
93 #endif
94 
95 #ifdef HAVE_fmatf4
96     case E_TFmode:
97       return !!HAVE_fmatf4;
98 #endif
99 
100     default:
101       break;
102     }
103 
104   return false;
105 }
106 
107 /* Define NAME with value TYPE size_unit.  */
108 void
109 builtin_define_type_sizeof (const char *name, tree type)
110 {
111   builtin_define_with_int_value (name,
112 				 tree_to_uhwi (TYPE_SIZE_UNIT (type)));
113 }
114 
115 /* Define the float.h constants for TYPE using NAME_PREFIX, FP_SUFFIX,
116    and FP_CAST. */
117 static void
118 builtin_define_float_constants (const char *name_prefix,
119 		                const char *fp_suffix,
120 				const char *fp_cast,
121 				const char *fma_suffix,
122 				tree type)
123 {
124   /* Used to convert radix-based values to base 10 values in several cases.
125 
126      In the max_exp -> max_10_exp conversion for 128-bit IEEE, we need at
127      least 6 significant digits for correct results.  Using the fraction
128      formed by (log(2)*1e6)/(log(10)*1e6) overflows a 32-bit integer as an
129      intermediate; perhaps someone can find a better approximation, in the
130      mean time, I suspect using doubles won't harm the bootstrap here.  */
131 
132   const double log10_2 = .30102999566398119521;
133   double log10_b;
134   const struct real_format *fmt;
135   const struct real_format *widefmt;
136 
137   char name[64], buf[128];
138   int dig, min_10_exp, max_10_exp;
139   int decimal_dig;
140   int type_decimal_dig;
141 
142   fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
143   gcc_assert (fmt->b != 10);
144   widefmt = REAL_MODE_FORMAT (TYPE_MODE (long_double_type_node));
145   gcc_assert (widefmt->b != 10);
146   for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
147     {
148       tree wtype = FLOATN_NX_TYPE_NODE (i);
149       if (wtype != NULL_TREE)
150 	{
151 	  const struct real_format *wfmt
152 	    = REAL_MODE_FORMAT (TYPE_MODE (wtype));
153 	  gcc_assert (wfmt->b != 10);
154 	  if (wfmt->p > widefmt->p)
155 	    widefmt = wfmt;
156 	}
157     }
158 
159   /* The radix of the exponent representation.  */
160   if (type == float_type_node)
161     builtin_define_with_int_value ("__FLT_RADIX__", fmt->b);
162   log10_b = log10_2;
163 
164   /* The number of radix digits, p, in the floating-point significand.  */
165   sprintf (name, "__%s_MANT_DIG__", name_prefix);
166   builtin_define_with_int_value (name, fmt->p);
167 
168   /* The number of decimal digits, q, such that any floating-point number
169      with q decimal digits can be rounded into a floating-point number with
170      p radix b digits and back again without change to the q decimal digits,
171 
172 	p log10 b			if b is a power of 10
173 	floor((p - 1) log10 b)		otherwise
174   */
175   dig = (fmt->p - 1) * log10_b;
176   sprintf (name, "__%s_DIG__", name_prefix);
177   builtin_define_with_int_value (name, dig);
178 
179   /* The minimum negative int x such that b**(x-1) is a normalized float.  */
180   sprintf (name, "__%s_MIN_EXP__", name_prefix);
181   sprintf (buf, "(%d)", fmt->emin);
182   builtin_define_with_value (name, buf, 0);
183 
184   /* The minimum negative int x such that 10**x is a normalized float,
185 
186 	  ceil (log10 (b ** (emin - 1)))
187 	= ceil (log10 (b) * (emin - 1))
188 
189      Recall that emin is negative, so the integer truncation calculates
190      the ceiling, not the floor, in this case.  */
191   min_10_exp = (fmt->emin - 1) * log10_b;
192   sprintf (name, "__%s_MIN_10_EXP__", name_prefix);
193   sprintf (buf, "(%d)", min_10_exp);
194   builtin_define_with_value (name, buf, 0);
195 
196   /* The maximum int x such that b**(x-1) is a representable float.  */
197   sprintf (name, "__%s_MAX_EXP__", name_prefix);
198   builtin_define_with_int_value (name, fmt->emax);
199 
200   /* The maximum int x such that 10**x is in the range of representable
201      finite floating-point numbers,
202 
203 	  floor (log10((1 - b**-p) * b**emax))
204 	= floor (log10(1 - b**-p) + log10(b**emax))
205 	= floor (log10(1 - b**-p) + log10(b)*emax)
206 
207      The safest thing to do here is to just compute this number.  But since
208      we don't link cc1 with libm, we cannot.  We could implement log10 here
209      a series expansion, but that seems too much effort because:
210 
211      Note that the first term, for all extant p, is a number exceedingly close
212      to zero, but slightly negative.  Note that the second term is an integer
213      scaling an irrational number, and that because of the floor we are only
214      interested in its integral portion.
215 
216      In order for the first term to have any effect on the integral portion
217      of the second term, the second term has to be exceedingly close to an
218      integer itself (e.g. 123.000000000001 or something).  Getting a result
219      that close to an integer requires that the irrational multiplicand have
220      a long series of zeros in its expansion, which doesn't occur in the
221      first 20 digits or so of log10(b).
222 
223      Hand-waving aside, crunching all of the sets of constants above by hand
224      does not yield a case for which the first term is significant, which
225      in the end is all that matters.  */
226   max_10_exp = fmt->emax * log10_b;
227   sprintf (name, "__%s_MAX_10_EXP__", name_prefix);
228   builtin_define_with_int_value (name, max_10_exp);
229 
230   /* The number of decimal digits, n, such that any floating-point number
231      can be rounded to n decimal digits and back again without change to
232      the value.
233 
234 	p * log10(b)			if b is a power of 10
235 	ceil(1 + p * log10(b))		otherwise
236 
237      The only macro we care about is this number for the widest supported
238      floating type, but we want this value for rendering constants below.  */
239   {
240     double d_decimal_dig
241       = 1 + (fmt->p < widefmt->p ? widefmt->p : fmt->p) * log10_b;
242     decimal_dig = d_decimal_dig;
243     if (decimal_dig < d_decimal_dig)
244       decimal_dig++;
245   }
246   /* Similar, for this type rather than long double.  */
247   {
248     double type_d_decimal_dig = 1 + fmt->p * log10_b;
249     type_decimal_dig = type_d_decimal_dig;
250     if (type_decimal_dig < type_d_decimal_dig)
251       type_decimal_dig++;
252   }
253   /* Define __DECIMAL_DIG__ to the value for long double to be
254      compatible with C99 and C11; see DR#501 and N2108.  */
255   if (type == long_double_type_node)
256     builtin_define_with_int_value ("__DECIMAL_DIG__", type_decimal_dig);
257   sprintf (name, "__%s_DECIMAL_DIG__", name_prefix);
258   builtin_define_with_int_value (name, type_decimal_dig);
259 
260   /* Since, for the supported formats, B is always a power of 2, we
261      construct the following numbers directly as a hexadecimal
262      constants.  */
263   get_max_float (fmt, buf, sizeof (buf));
264 
265   sprintf (name, "__%s_MAX__", name_prefix);
266   builtin_define_with_hex_fp_value (name, type, decimal_dig, buf, fp_suffix, fp_cast);
267 
268   /* The minimum normalized positive floating-point number,
269      b**(emin-1).  */
270   sprintf (name, "__%s_MIN__", name_prefix);
271   sprintf (buf, "0x1p%d", fmt->emin - 1);
272   builtin_define_with_hex_fp_value (name, type, decimal_dig, buf, fp_suffix, fp_cast);
273 
274   /* The difference between 1 and the least value greater than 1 that is
275      representable in the given floating point type, b**(1-p).  */
276   sprintf (name, "__%s_EPSILON__", name_prefix);
277   if (fmt->pnan < fmt->p)
278     /* This is an IBM extended double format, so 1.0 + any double is
279        representable precisely.  */
280       sprintf (buf, "0x1p%d", fmt->emin - fmt->p);
281     else
282       sprintf (buf, "0x1p%d", 1 - fmt->p);
283   builtin_define_with_hex_fp_value (name, type, decimal_dig, buf, fp_suffix, fp_cast);
284 
285   /* For C++ std::numeric_limits<T>::denorm_min and C11 *_TRUE_MIN.
286      The minimum denormalized positive floating-point number, b**(emin-p).
287      The minimum normalized positive floating-point number for formats
288      that don't support denormals.  */
289   sprintf (name, "__%s_DENORM_MIN__", name_prefix);
290   sprintf (buf, "0x1p%d", fmt->emin - (fmt->has_denorm ? fmt->p : 1));
291   builtin_define_with_hex_fp_value (name, type, decimal_dig,
292 				    buf, fp_suffix, fp_cast);
293 
294   sprintf (name, "__%s_HAS_DENORM__", name_prefix);
295   builtin_define_with_value (name, fmt->has_denorm ? "1" : "0", 0);
296 
297   /* For C++ std::numeric_limits<T>::has_infinity.  */
298   sprintf (name, "__%s_HAS_INFINITY__", name_prefix);
299   builtin_define_with_int_value (name,
300 				 MODE_HAS_INFINITIES (TYPE_MODE (type)));
301   /* For C++ std::numeric_limits<T>::has_quiet_NaN.  We do not have a
302      predicate to distinguish a target that has both quiet and
303      signalling NaNs from a target that has only quiet NaNs or only
304      signalling NaNs, so we assume that a target that has any kind of
305      NaN has quiet NaNs.  */
306   sprintf (name, "__%s_HAS_QUIET_NAN__", name_prefix);
307   builtin_define_with_int_value (name, MODE_HAS_NANS (TYPE_MODE (type)));
308 
309   /* Note whether we have fast FMA.  */
310   if (mode_has_fma (TYPE_MODE (type)) && fma_suffix != NULL)
311     {
312       sprintf (name, "__FP_FAST_FMA%s", fma_suffix);
313       builtin_define_with_int_value (name, 1);
314     }
315 }
316 
317 /* Define __DECx__ constants for TYPE using NAME_PREFIX and SUFFIX. */
318 static void
319 builtin_define_decimal_float_constants (const char *name_prefix,
320 					const char *suffix,
321 					tree type)
322 {
323   const struct real_format *fmt;
324   char name[64], buf[128], *p;
325   int digits;
326 
327   fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
328 
329   /* The number of radix digits, p, in the significand.  */
330   sprintf (name, "__%s_MANT_DIG__", name_prefix);
331   builtin_define_with_int_value (name, fmt->p);
332 
333   /* The minimum negative int x such that b**(x-1) is a normalized float.  */
334   sprintf (name, "__%s_MIN_EXP__", name_prefix);
335   sprintf (buf, "(%d)", fmt->emin);
336   builtin_define_with_value (name, buf, 0);
337 
338   /* The maximum int x such that b**(x-1) is a representable float.  */
339   sprintf (name, "__%s_MAX_EXP__", name_prefix);
340   builtin_define_with_int_value (name, fmt->emax);
341 
342   /* Compute the minimum representable value.  */
343   sprintf (name, "__%s_MIN__", name_prefix);
344   sprintf (buf, "1E%d%s", fmt->emin - 1, suffix);
345   builtin_define_with_value (name, buf, 0);
346 
347   /* Compute the maximum representable value.  */
348   sprintf (name, "__%s_MAX__", name_prefix);
349   p = buf;
350   for (digits = fmt->p; digits; digits--)
351     {
352       *p++ = '9';
353       if (digits == fmt->p)
354 	*p++ = '.';
355     }
356   *p = 0;
357   /* fmt->p plus 1, to account for the decimal point and fmt->emax
358      minus 1 because the digits are nines, not 1.0.  */
359   sprintf (&buf[fmt->p + 1], "E%d%s", fmt->emax - 1, suffix);
360   builtin_define_with_value (name, buf, 0);
361 
362   /* Compute epsilon (the difference between 1 and least value greater
363      than 1 representable).  */
364   sprintf (name, "__%s_EPSILON__", name_prefix);
365   sprintf (buf, "1E-%d%s", fmt->p - 1, suffix);
366   builtin_define_with_value (name, buf, 0);
367 
368   /* Minimum subnormal positive decimal value.  */
369   sprintf (name, "__%s_SUBNORMAL_MIN__", name_prefix);
370   p = buf;
371   for (digits = fmt->p; digits > 1; digits--)
372     {
373       *p++ = '0';
374       if (digits == fmt->p)
375 	*p++ = '.';
376     }
377   *p = 0;
378   sprintf (&buf[fmt->p], "1E%d%s", fmt->emin - 1, suffix);
379   builtin_define_with_value (name, buf, 0);
380 }
381 
382 /* Define fixed-point constants for TYPE using NAME_PREFIX and SUFFIX.  */
383 
384 static void
385 builtin_define_fixed_point_constants (const char *name_prefix,
386 				      const char *suffix,
387 				      tree type)
388 {
389   char name[64], buf[256], *new_buf;
390   int i, mod;
391 
392   sprintf (name, "__%s_FBIT__", name_prefix);
393   builtin_define_with_int_value (name, TYPE_FBIT (type));
394 
395   sprintf (name, "__%s_IBIT__", name_prefix);
396   builtin_define_with_int_value (name, TYPE_IBIT (type));
397 
398   /* If there is no suffix, defines are for fixed-point modes.
399      We just return.  */
400   if (strcmp (suffix, "") == 0)
401     return;
402 
403   if (TYPE_UNSIGNED (type))
404     {
405       sprintf (name, "__%s_MIN__", name_prefix);
406       sprintf (buf, "0.0%s", suffix);
407       builtin_define_with_value (name, buf, 0);
408     }
409   else
410     {
411       sprintf (name, "__%s_MIN__", name_prefix);
412       if (ALL_ACCUM_MODE_P (TYPE_MODE (type)))
413 	sprintf (buf, "(-0X1P%d%s-0X1P%d%s)", TYPE_IBIT (type) - 1, suffix,
414 		 TYPE_IBIT (type) - 1, suffix);
415       else
416 	sprintf (buf, "(-0.5%s-0.5%s)", suffix, suffix);
417       builtin_define_with_value (name, buf, 0);
418     }
419 
420   sprintf (name, "__%s_MAX__", name_prefix);
421   sprintf (buf, "0X");
422   new_buf = buf + 2;
423   mod = (TYPE_FBIT (type) + TYPE_IBIT (type)) % 4;
424   if (mod)
425     sprintf (new_buf++, "%x", (1 << mod) - 1);
426   for (i = 0; i < (TYPE_FBIT (type) + TYPE_IBIT (type)) / 4; i++)
427     sprintf (new_buf++, "F");
428   sprintf (new_buf, "P-%d%s", TYPE_FBIT (type), suffix);
429   builtin_define_with_value (name, buf, 0);
430 
431   sprintf (name, "__%s_EPSILON__", name_prefix);
432   sprintf (buf, "0x1P-%d%s", TYPE_FBIT (type), suffix);
433   builtin_define_with_value (name, buf, 0);
434 }
435 
436 /* Define macros used by <stdint.h>.  */
437 static void
438 builtin_define_stdint_macros (void)
439 {
440   builtin_define_type_max ("__INTMAX_MAX__", intmax_type_node);
441   builtin_define_constants ("__INTMAX_C", intmax_type_node);
442   builtin_define_type_max ("__UINTMAX_MAX__", uintmax_type_node);
443   builtin_define_constants ("__UINTMAX_C", uintmax_type_node);
444   builtin_define_type_width ("__INTMAX_WIDTH__", intmax_type_node,
445 			     uintmax_type_node);
446   if (sig_atomic_type_node)
447     {
448       builtin_define_type_minmax ("__SIG_ATOMIC_MIN__", "__SIG_ATOMIC_MAX__",
449 				  sig_atomic_type_node);
450       builtin_define_type_width ("__SIG_ATOMIC_WIDTH__", sig_atomic_type_node,
451 				 NULL_TREE);
452     }
453   if (int8_type_node)
454     builtin_define_type_max ("__INT8_MAX__", int8_type_node);
455   if (int16_type_node)
456     builtin_define_type_max ("__INT16_MAX__", int16_type_node);
457   if (int32_type_node)
458     builtin_define_type_max ("__INT32_MAX__", int32_type_node);
459   if (int64_type_node)
460     builtin_define_type_max ("__INT64_MAX__", int64_type_node);
461   if (uint8_type_node)
462     builtin_define_type_max ("__UINT8_MAX__", uint8_type_node);
463   if (c_uint16_type_node)
464     builtin_define_type_max ("__UINT16_MAX__", c_uint16_type_node);
465   if (c_uint32_type_node)
466     builtin_define_type_max ("__UINT32_MAX__", c_uint32_type_node);
467   if (c_uint64_type_node)
468     builtin_define_type_max ("__UINT64_MAX__", c_uint64_type_node);
469   if (int_least8_type_node)
470     {
471       builtin_define_type_max ("__INT_LEAST8_MAX__", int_least8_type_node);
472       builtin_define_constants ("__INT8_C", int_least8_type_node);
473       builtin_define_type_width ("__INT_LEAST8_WIDTH__", int_least8_type_node,
474 				 uint_least8_type_node);
475     }
476   if (int_least16_type_node)
477     {
478       builtin_define_type_max ("__INT_LEAST16_MAX__", int_least16_type_node);
479       builtin_define_constants ("__INT16_C", int_least16_type_node);
480       builtin_define_type_width ("__INT_LEAST16_WIDTH__",
481 				 int_least16_type_node,
482 				 uint_least16_type_node);
483     }
484   if (int_least32_type_node)
485     {
486       builtin_define_type_max ("__INT_LEAST32_MAX__", int_least32_type_node);
487       builtin_define_constants ("__INT32_C", int_least32_type_node);
488       builtin_define_type_width ("__INT_LEAST32_WIDTH__",
489 				 int_least32_type_node,
490 				 uint_least32_type_node);
491     }
492   if (int_least64_type_node)
493     {
494       builtin_define_type_max ("__INT_LEAST64_MAX__", int_least64_type_node);
495       builtin_define_constants ("__INT64_C", int_least64_type_node);
496       builtin_define_type_width ("__INT_LEAST64_WIDTH__",
497 				 int_least64_type_node,
498 				 uint_least64_type_node);
499     }
500   if (uint_least8_type_node)
501     {
502       builtin_define_type_max ("__UINT_LEAST8_MAX__", uint_least8_type_node);
503       builtin_define_constants ("__UINT8_C", uint_least8_type_node);
504     }
505   if (uint_least16_type_node)
506     {
507       builtin_define_type_max ("__UINT_LEAST16_MAX__", uint_least16_type_node);
508       builtin_define_constants ("__UINT16_C", uint_least16_type_node);
509     }
510   if (uint_least32_type_node)
511     {
512       builtin_define_type_max ("__UINT_LEAST32_MAX__", uint_least32_type_node);
513       builtin_define_constants ("__UINT32_C", uint_least32_type_node);
514     }
515   if (uint_least64_type_node)
516     {
517       builtin_define_type_max ("__UINT_LEAST64_MAX__", uint_least64_type_node);
518       builtin_define_constants ("__UINT64_C", uint_least64_type_node);
519     }
520 /*
521  * NetBSD/sparc64 long ago defined signed and unsigned fast{8,16,32} to be
522  * different to the common sparc64 definitions, and they are not the same
523  * size for the same bitsize.  GCC 7 introduced checks that they are the
524  * same size below that trigger here.
525  *
526  * NETBSD_TOOLS/NETBSD_NATIVE is wrong for this, but it will do for now.
527  */
528 #if defined(NETBSD_TOOLS) || defined(NETBSD_NATIVE)
529 #define builtin_define_type_width_nb(a,b,c)	builtin_define_type_width(a,b,NULL_TREE)
530 #else
531 #define builtin_define_type_width_nb(a,b,c)	builtin_define_type_width(a,b,c)
532 #endif
533   if (int_fast8_type_node)
534     {
535       builtin_define_type_max ("__INT_FAST8_MAX__", int_fast8_type_node);
536       builtin_define_type_width_nb ("__INT_FAST8_WIDTH__", int_fast8_type_node,
537 				 uint_fast8_type_node);
538     }
539   if (int_fast16_type_node)
540     {
541       builtin_define_type_max ("__INT_FAST16_MAX__", int_fast16_type_node);
542       builtin_define_type_width_nb ("__INT_FAST16_WIDTH__", int_fast16_type_node,
543 				 uint_fast16_type_node);
544     }
545   if (int_fast32_type_node)
546     {
547       builtin_define_type_max ("__INT_FAST32_MAX__", int_fast32_type_node);
548       builtin_define_type_width_nb ("__INT_FAST32_WIDTH__", int_fast32_type_node,
549 				 uint_fast32_type_node);
550     }
551   if (int_fast64_type_node)
552     {
553       builtin_define_type_max ("__INT_FAST64_MAX__", int_fast64_type_node);
554       builtin_define_type_width ("__INT_FAST64_WIDTH__", int_fast64_type_node,
555 				 uint_fast64_type_node);
556     }
557 #undef builtin_define_type_width_nb
558   if (uint_fast8_type_node)
559     builtin_define_type_max ("__UINT_FAST8_MAX__", uint_fast8_type_node);
560   if (uint_fast16_type_node)
561     builtin_define_type_max ("__UINT_FAST16_MAX__", uint_fast16_type_node);
562   if (uint_fast32_type_node)
563     builtin_define_type_max ("__UINT_FAST32_MAX__", uint_fast32_type_node);
564   if (uint_fast64_type_node)
565     builtin_define_type_max ("__UINT_FAST64_MAX__", uint_fast64_type_node);
566   if (intptr_type_node)
567     {
568       builtin_define_type_max ("__INTPTR_MAX__", intptr_type_node);
569       builtin_define_type_width ("__INTPTR_WIDTH__", intptr_type_node,
570 				 uintptr_type_node);
571     }
572   if (uintptr_type_node)
573     builtin_define_type_max ("__UINTPTR_MAX__", uintptr_type_node);
574 }
575 
576 /* Adjust the optimization macros when a #pragma GCC optimization is done to
577    reflect the current level.  */
578 void
579 c_cpp_builtins_optimize_pragma (cpp_reader *pfile, tree prev_tree,
580 				tree cur_tree)
581 {
582   struct cl_optimization *prev = TREE_OPTIMIZATION (prev_tree);
583   struct cl_optimization *cur  = TREE_OPTIMIZATION (cur_tree);
584   bool prev_fast_math;
585   bool cur_fast_math;
586 
587   /* -undef turns off target-specific built-ins.  */
588   if (flag_undef)
589     return;
590 
591   /* Other target-independent built-ins determined by command-line
592      options.  */
593   if (!prev->x_optimize_size && cur->x_optimize_size)
594     cpp_define (pfile, "__OPTIMIZE_SIZE__");
595   else if (prev->x_optimize_size && !cur->x_optimize_size)
596     cpp_undef (pfile, "__OPTIMIZE_SIZE__");
597 
598   if (!prev->x_optimize && cur->x_optimize)
599     cpp_define (pfile, "__OPTIMIZE__");
600   else if (prev->x_optimize && !cur->x_optimize)
601     cpp_undef (pfile, "__OPTIMIZE__");
602 
603   prev_fast_math = fast_math_flags_struct_set_p (prev);
604   cur_fast_math  = fast_math_flags_struct_set_p (cur);
605   if (!prev_fast_math && cur_fast_math)
606     cpp_define (pfile, "__FAST_MATH__");
607   else if (prev_fast_math && !cur_fast_math)
608     cpp_undef (pfile, "__FAST_MATH__");
609 
610   if (!prev->x_flag_signaling_nans && cur->x_flag_signaling_nans)
611     cpp_define (pfile, "__SUPPORT_SNAN__");
612   else if (prev->x_flag_signaling_nans && !cur->x_flag_signaling_nans)
613     cpp_undef (pfile, "__SUPPORT_SNAN__");
614 
615   if (!prev->x_flag_errno_math && cur->x_flag_errno_math)
616     cpp_undef (pfile, "__NO_MATH_ERRNO__");
617   else if (prev->x_flag_errno_math && !cur->x_flag_errno_math)
618     cpp_define (pfile, "__NO_MATH_ERRNO__");
619 
620   if (!prev->x_flag_finite_math_only && cur->x_flag_finite_math_only)
621     {
622       cpp_undef (pfile, "__FINITE_MATH_ONLY__");
623       cpp_define (pfile, "__FINITE_MATH_ONLY__=1");
624     }
625   else if (prev->x_flag_finite_math_only && !cur->x_flag_finite_math_only)
626     {
627       cpp_undef (pfile, "__FINITE_MATH_ONLY__");
628       cpp_define (pfile, "__FINITE_MATH_ONLY__=0");
629     }
630 }
631 
632 
633 /* This function will emit cpp macros to indicate the presence of various lock
634    free atomic operations.  */
635 
636 static void
637 cpp_atomic_builtins (cpp_reader *pfile)
638 {
639   /* Set a flag for each size of object that compare and swap exists for up to
640      a 16 byte object.  */
641 #define SWAP_LIMIT  17
642   bool have_swap[SWAP_LIMIT];
643   unsigned int psize;
644 
645   /* Clear the map of sizes compare_and swap exists for.  */
646   memset (have_swap, 0, sizeof (have_swap));
647 
648   /* Tell source code if the compiler makes sync_compare_and_swap
649      builtins available.  */
650 #ifndef HAVE_sync_compare_and_swapqi
651 #define HAVE_sync_compare_and_swapqi 0
652 #endif
653 #ifndef HAVE_atomic_compare_and_swapqi
654 #define HAVE_atomic_compare_and_swapqi 0
655 #endif
656 
657   if (HAVE_sync_compare_and_swapqi || HAVE_atomic_compare_and_swapqi)
658     {
659       cpp_define (pfile, "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
660       have_swap[1] = true;
661     }
662 
663 #ifndef HAVE_sync_compare_and_swaphi
664 #define HAVE_sync_compare_and_swaphi 0
665 #endif
666 #ifndef HAVE_atomic_compare_and_swaphi
667 #define HAVE_atomic_compare_and_swaphi 0
668 #endif
669   if (HAVE_sync_compare_and_swaphi || HAVE_atomic_compare_and_swaphi)
670     {
671       cpp_define (pfile, "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
672       have_swap[2] = true;
673     }
674 
675 #ifndef HAVE_sync_compare_and_swapsi
676 #define HAVE_sync_compare_and_swapsi 0
677 #endif
678 #ifndef HAVE_atomic_compare_and_swapsi
679 #define HAVE_atomic_compare_and_swapsi 0
680 #endif
681   if (HAVE_sync_compare_and_swapsi || HAVE_atomic_compare_and_swapsi)
682     {
683       cpp_define (pfile, "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
684       have_swap[4] = true;
685     }
686 
687 #ifndef HAVE_sync_compare_and_swapdi
688 #define HAVE_sync_compare_and_swapdi 0
689 #endif
690 #ifndef HAVE_atomic_compare_and_swapdi
691 #define HAVE_atomic_compare_and_swapdi 0
692 #endif
693   if (HAVE_sync_compare_and_swapdi || HAVE_atomic_compare_and_swapdi)
694     {
695       cpp_define (pfile, "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
696       have_swap[8] = true;
697     }
698 
699 #ifndef HAVE_sync_compare_and_swapti
700 #define HAVE_sync_compare_and_swapti 0
701 #endif
702 #ifndef HAVE_atomic_compare_and_swapti
703 #define HAVE_atomic_compare_and_swapti 0
704 #endif
705   if (HAVE_sync_compare_and_swapti || HAVE_atomic_compare_and_swapti)
706     {
707       cpp_define (pfile, "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_16");
708       have_swap[16] = true;
709     }
710 
711   /* Tell the source code about various types.  These map to the C++11 and C11
712      macros where 2 indicates lock-free always, and 1 indicates sometimes
713      lock free.  */
714 #define SIZEOF_NODE(T) (tree_to_uhwi (TYPE_SIZE_UNIT (T)))
715 #define SWAP_INDEX(T) ((SIZEOF_NODE (T) < SWAP_LIMIT) ? SIZEOF_NODE (T) : 0)
716   builtin_define_with_int_value ("__GCC_ATOMIC_BOOL_LOCK_FREE",
717 			(have_swap[SWAP_INDEX (boolean_type_node)]? 2 : 1));
718   builtin_define_with_int_value ("__GCC_ATOMIC_CHAR_LOCK_FREE",
719 			(have_swap[SWAP_INDEX (signed_char_type_node)]? 2 : 1));
720   builtin_define_with_int_value ("__GCC_ATOMIC_CHAR16_T_LOCK_FREE",
721 			(have_swap[SWAP_INDEX (char16_type_node)]? 2 : 1));
722   builtin_define_with_int_value ("__GCC_ATOMIC_CHAR32_T_LOCK_FREE",
723 			(have_swap[SWAP_INDEX (char32_type_node)]? 2 : 1));
724   builtin_define_with_int_value ("__GCC_ATOMIC_WCHAR_T_LOCK_FREE",
725 			(have_swap[SWAP_INDEX (wchar_type_node)]? 2 : 1));
726   builtin_define_with_int_value ("__GCC_ATOMIC_SHORT_LOCK_FREE",
727 		      (have_swap[SWAP_INDEX (short_integer_type_node)]? 2 : 1));
728   builtin_define_with_int_value ("__GCC_ATOMIC_INT_LOCK_FREE",
729 			(have_swap[SWAP_INDEX (integer_type_node)]? 2 : 1));
730   builtin_define_with_int_value ("__GCC_ATOMIC_LONG_LOCK_FREE",
731 		      (have_swap[SWAP_INDEX (long_integer_type_node)]? 2 : 1));
732   builtin_define_with_int_value ("__GCC_ATOMIC_LLONG_LOCK_FREE",
733 		(have_swap[SWAP_INDEX (long_long_integer_type_node)]? 2 : 1));
734 
735   /* If we're dealing with a "set" value that doesn't exactly correspond
736      to a boolean truth value, let the library work around that.  */
737   builtin_define_with_int_value ("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL",
738 				 targetm.atomic_test_and_set_trueval);
739 
740   /* ptr_type_node can't be used here since ptr_mode is only set when
741      toplev calls backend_init which is not done with -E  or pch.  */
742   psize = POINTER_SIZE_UNITS;
743   if (psize >= SWAP_LIMIT)
744     psize = 0;
745   builtin_define_with_int_value ("__GCC_ATOMIC_POINTER_LOCK_FREE",
746 			(have_swap[psize]? 2 : 1));
747 }
748 
749 /* Return TRUE if the implicit excess precision in which the back-end will
750    compute floating-point calculations is not more than the explicit
751    excess precision that the front-end will apply under
752    -fexcess-precision=[standard|fast].
753 
754    More intuitively, return TRUE if the excess precision proposed by the
755    front-end is the excess precision that will actually be used.  */
756 
757 static bool
758 c_cpp_flt_eval_method_iec_559 (void)
759 {
760   enum excess_precision_type front_end_ept
761     = (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD
762        ? EXCESS_PRECISION_TYPE_STANDARD
763        : EXCESS_PRECISION_TYPE_FAST);
764 
765   enum flt_eval_method back_end
766     = targetm.c.excess_precision (EXCESS_PRECISION_TYPE_IMPLICIT);
767 
768   enum flt_eval_method front_end
769     = targetm.c.excess_precision (front_end_ept);
770 
771   return excess_precision_mode_join (front_end, back_end) == front_end;
772 }
773 
774 /* Return the value for __GCC_IEC_559.  */
775 static int
776 cpp_iec_559_value (void)
777 {
778   /* The default is support for IEEE 754-2008.  */
779   int ret = 2;
780 
781   /* float and double must be binary32 and binary64.  If they are but
782      with reversed NaN convention, at most IEEE 754-1985 is
783      supported.  */
784   const struct real_format *ffmt
785     = REAL_MODE_FORMAT (TYPE_MODE (float_type_node));
786   const struct real_format *dfmt
787     = REAL_MODE_FORMAT (TYPE_MODE (double_type_node));
788   if (!ffmt->qnan_msb_set || !dfmt->qnan_msb_set)
789     ret = 1;
790   if (ffmt->b != 2
791       || ffmt->p != 24
792       || ffmt->pnan != 24
793       || ffmt->emin != -125
794       || ffmt->emax != 128
795       || ffmt->signbit_rw != 31
796       || ffmt->round_towards_zero
797       || !ffmt->has_sign_dependent_rounding
798       || !ffmt->has_nans
799       || !ffmt->has_inf
800       || !ffmt->has_denorm
801       || !ffmt->has_signed_zero
802       || dfmt->b != 2
803       || dfmt->p != 53
804       || dfmt->pnan != 53
805       || dfmt->emin != -1021
806       || dfmt->emax != 1024
807       || dfmt->signbit_rw != 63
808       || dfmt->round_towards_zero
809       || !dfmt->has_sign_dependent_rounding
810       || !dfmt->has_nans
811       || !dfmt->has_inf
812       || !dfmt->has_denorm
813       || !dfmt->has_signed_zero)
814     ret = 0;
815 
816   /* In strict C standards conformance mode, consider a back-end providing
817      more implicit excess precision than the explicit excess precision
818      the front-end options would require to mean a lack of IEEE 754
819      support.  For C++, and outside strict conformance mode, do not consider
820      this to mean a lack of IEEE 754 support.  */
821 
822   if (flag_iso
823       && !c_dialect_cxx ()
824       && !c_cpp_flt_eval_method_iec_559 ())
825     ret = 0;
826 
827   if (flag_iso
828       && !c_dialect_cxx ()
829       && flag_fp_contract_mode == FP_CONTRACT_FAST)
830     ret = 0;
831 
832   /* Various options are contrary to IEEE 754 semantics.  */
833   if (flag_unsafe_math_optimizations
834       || flag_associative_math
835       || flag_reciprocal_math
836       || flag_finite_math_only
837       || !flag_signed_zeros
838       || flag_single_precision_constant)
839     ret = 0;
840 
841   /* If the target does not support IEEE 754 exceptions and rounding
842      modes, consider IEEE 754 support to be absent.  */
843   if (!targetm.float_exceptions_rounding_supported_p ())
844     ret = 0;
845 
846   return ret;
847 }
848 
849 /* Return the value for __GCC_IEC_559_COMPLEX.  */
850 static int
851 cpp_iec_559_complex_value (void)
852 {
853   /* The value is no bigger than that of __GCC_IEC_559.  */
854   int ret = cpp_iec_559_value ();
855 
856   /* Some options are contrary to the required default state of the
857      CX_LIMITED_RANGE pragma.  */
858   if (flag_complex_method != 2)
859     ret = 0;
860 
861   return ret;
862 }
863 
864 /* Hook that registers front end and target-specific built-ins.  */
865 void
866 c_cpp_builtins (cpp_reader *pfile)
867 {
868   int i;
869 
870   /* -undef turns off target-specific built-ins.  */
871   if (flag_undef)
872     return;
873 
874   define_language_independent_builtin_macros (pfile);
875 
876   if (c_dialect_cxx ())
877   {
878     int major;
879     parse_basever (&major, NULL, NULL);
880     cpp_define_formatted (pfile, "__GNUG__=%d", major);
881   }
882 
883   /* For stddef.h.  They require macros defined in c-common.c.  */
884   c_stddef_cpp_builtins ();
885 
886   /* Set include test macros for all C/C++ (not for just C++11 etc.)
887      The builtins __has_include__ and __has_include_next__ are defined
888      in libcpp.  */
889   cpp_define (pfile, "__has_include(STR)=__has_include__(STR)");
890   cpp_define (pfile, "__has_include_next(STR)=__has_include_next__(STR)");
891 
892   if (c_dialect_cxx ())
893     {
894       if (flag_weak && SUPPORTS_ONE_ONLY)
895 	cpp_define (pfile, "__GXX_WEAK__=1");
896       else
897 	cpp_define (pfile, "__GXX_WEAK__=0");
898 
899       if (warn_deprecated)
900 	cpp_define (pfile, "__DEPRECATED");
901 
902       if (flag_rtti)
903 	{
904 	  cpp_define (pfile, "__GXX_RTTI");
905 	  cpp_define (pfile, "__cpp_rtti=199711");
906 	}
907 
908       if (cxx_dialect >= cxx11)
909         cpp_define (pfile, "__GXX_EXPERIMENTAL_CXX0X__");
910 
911       /* Binary literals have been allowed in g++ before C++11
912 	 and were standardized for C++14.  */
913       if (!pedantic || cxx_dialect > cxx11)
914 	cpp_define (pfile, "__cpp_binary_literals=201304");
915 
916       /* Similarly for hexadecimal floating point literals and C++17.  */
917       if (!pedantic || cpp_get_options (parse_in)->extended_numbers)
918 	cpp_define (pfile, "__cpp_hex_float=201603");
919 
920       /* Arrays of runtime bound were removed from C++14, but we still
921 	 support GNU VLAs.  Let's define this macro to a low number
922 	 (corresponding to the initial test release of GNU C++) if we won't
923 	 complain about use of VLAs.  */
924       if (c_dialect_cxx ()
925 	  && (pedantic ? warn_vla == 0 : warn_vla <= 0))
926 	cpp_define (pfile, "__cpp_runtime_arrays=198712");
927 
928       if (cxx_dialect >= cxx11)
929 	{
930 	  /* Set feature test macros for C++11.  */
931 	  if (cxx_dialect <= cxx14)
932 	    cpp_define (pfile, "__cpp_unicode_characters=200704");
933 	  cpp_define (pfile, "__cpp_raw_strings=200710");
934 	  cpp_define (pfile, "__cpp_unicode_literals=200710");
935 	  cpp_define (pfile, "__cpp_user_defined_literals=200809");
936 	  cpp_define (pfile, "__cpp_lambdas=200907");
937 	  if (cxx_dialect == cxx11)
938 	    cpp_define (pfile, "__cpp_constexpr=200704");
939 	  if (cxx_dialect <= cxx14)
940 	    cpp_define (pfile, "__cpp_range_based_for=200907");
941 	  if (cxx_dialect <= cxx14)
942 	    cpp_define (pfile, "__cpp_static_assert=200410");
943 	  cpp_define (pfile, "__cpp_decltype=200707");
944 	  cpp_define (pfile, "__cpp_attributes=200809");
945 	  cpp_define (pfile, "__cpp_rvalue_reference=200610");
946 	  cpp_define (pfile, "__cpp_rvalue_references=200610");
947 	  cpp_define (pfile, "__cpp_variadic_templates=200704");
948 	  cpp_define (pfile, "__cpp_initializer_lists=200806");
949 	  cpp_define (pfile, "__cpp_delegating_constructors=200604");
950 	  cpp_define (pfile, "__cpp_nsdmi=200809");
951 	  if (!flag_new_inheriting_ctors)
952 	    cpp_define (pfile, "__cpp_inheriting_constructors=200802");
953 	  else
954 	    cpp_define (pfile, "__cpp_inheriting_constructors=201511");
955 	  cpp_define (pfile, "__cpp_ref_qualifiers=200710");
956 	  cpp_define (pfile, "__cpp_alias_templates=200704");
957 	}
958       if (cxx_dialect > cxx11)
959 	{
960 	  /* Set feature test macros for C++14.  */
961 	  cpp_define (pfile, "__cpp_return_type_deduction=201304");
962 	  cpp_define (pfile, "__cpp_init_captures=201304");
963 	  cpp_define (pfile, "__cpp_generic_lambdas=201304");
964 	  if (cxx_dialect <= cxx14)
965 	    cpp_define (pfile, "__cpp_constexpr=201304");
966 	  cpp_define (pfile, "__cpp_decltype_auto=201304");
967 	  cpp_define (pfile, "__cpp_aggregate_nsdmi=201304");
968 	  cpp_define (pfile, "__cpp_variable_templates=201304");
969 	  cpp_define (pfile, "__cpp_digit_separators=201309");
970 	}
971       if (cxx_dialect > cxx14)
972 	{
973 	  /* Set feature test macros for C++1z.  */
974 	  cpp_define (pfile, "__cpp_unicode_characters=201411");
975 	  cpp_define (pfile, "__cpp_static_assert=201411");
976 	  cpp_define (pfile, "__cpp_namespace_attributes=201411");
977 	  cpp_define (pfile, "__cpp_enumerator_attributes=201411");
978 	  cpp_define (pfile, "__cpp_nested_namespace_definitions=201411");
979 	  cpp_define (pfile, "__cpp_fold_expressions=201603");
980 	  cpp_define (pfile, "__cpp_nontype_template_args=201411");
981 	  cpp_define (pfile, "__cpp_range_based_for=201603");
982 	  cpp_define (pfile, "__cpp_constexpr=201603");
983 	  cpp_define (pfile, "__cpp_if_constexpr=201606");
984 	  cpp_define (pfile, "__cpp_capture_star_this=201603");
985 	  cpp_define (pfile, "__cpp_inline_variables=201606");
986 	  cpp_define (pfile, "__cpp_aggregate_bases=201603");
987 	  cpp_define (pfile, "__cpp_deduction_guides=201611");
988 	  cpp_define (pfile, "__cpp_noexcept_function_type=201510");
989 	  /* Old macro, superseded by
990 	     __cpp_nontype_template_parameter_auto.  */
991 	  cpp_define (pfile, "__cpp_template_auto=201606");
992 	  cpp_define (pfile, "__cpp_structured_bindings=201606");
993 	  cpp_define (pfile, "__cpp_variadic_using=201611");
994 	  cpp_define (pfile, "__cpp_guaranteed_copy_elision=201606");
995 	  cpp_define (pfile, "__cpp_nontype_template_parameter_auto=201606");
996 	}
997       if (flag_concepts)
998 	cpp_define (pfile, "__cpp_concepts=201507");
999       if (flag_tm)
1000 	/* Use a value smaller than the 201505 specified in
1001 	   the TS, since we don't yet support atomic_cancel.  */
1002 	cpp_define (pfile, "__cpp_transactional_memory=210500");
1003       if (flag_sized_deallocation)
1004 	cpp_define (pfile, "__cpp_sized_deallocation=201309");
1005       if (aligned_new_threshold)
1006 	{
1007 	  cpp_define (pfile, "__cpp_aligned_new=201606");
1008 	  cpp_define_formatted (pfile, "__STDCPP_DEFAULT_NEW_ALIGNMENT__=%d",
1009 				aligned_new_threshold);
1010 	}
1011       if (flag_new_ttp)
1012 	cpp_define (pfile, "__cpp_template_template_args=201611");
1013       if (flag_threadsafe_statics)
1014 	cpp_define (pfile, "__cpp_threadsafe_static_init=200806");
1015     }
1016   /* Note that we define this for C as well, so that we know if
1017      __attribute__((cleanup)) will interface with EH.  */
1018   if (flag_exceptions)
1019     {
1020       cpp_define (pfile, "__EXCEPTIONS");
1021       if (c_dialect_cxx ())
1022 	cpp_define (pfile, "__cpp_exceptions=199711");
1023     }
1024 
1025   /* Represents the C++ ABI version, always defined so it can be used while
1026      preprocessing C and assembler.  */
1027   if (flag_abi_version == 0)
1028     /* We should have set this to something real in c_common_post_options.  */
1029     gcc_unreachable ();
1030   else if (flag_abi_version == 1)
1031     /* Due to a historical accident, this version had the value
1032        "102".  */
1033     builtin_define_with_int_value ("__GXX_ABI_VERSION", 102);
1034   else
1035     /* Newer versions have values 1002, 1003, ....  */
1036     builtin_define_with_int_value ("__GXX_ABI_VERSION",
1037 				   1000 + flag_abi_version);
1038 
1039   /* libgcc needs to know this.  */
1040   if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
1041     cpp_define (pfile, "__USING_SJLJ_EXCEPTIONS__");
1042 
1043   /* limits.h and stdint.h need to know these.  */
1044   builtin_define_type_max ("__SCHAR_MAX__", signed_char_type_node);
1045   builtin_define_type_max ("__SHRT_MAX__", short_integer_type_node);
1046   builtin_define_type_max ("__INT_MAX__", integer_type_node);
1047   builtin_define_type_max ("__LONG_MAX__", long_integer_type_node);
1048   builtin_define_type_max ("__LONG_LONG_MAX__", long_long_integer_type_node);
1049   builtin_define_type_minmax ("__WCHAR_MIN__", "__WCHAR_MAX__",
1050 			      underlying_wchar_type_node);
1051   builtin_define_type_minmax ("__WINT_MIN__", "__WINT_MAX__", wint_type_node);
1052   builtin_define_type_max ("__PTRDIFF_MAX__", ptrdiff_type_node);
1053   builtin_define_type_max ("__SIZE_MAX__", size_type_node);
1054 
1055   /* These are needed for TS 18661-1.  */
1056   builtin_define_type_width ("__SCHAR_WIDTH__", signed_char_type_node,
1057 			     unsigned_char_type_node);
1058   builtin_define_type_width ("__SHRT_WIDTH__", short_integer_type_node,
1059 			     short_unsigned_type_node);
1060   builtin_define_type_width ("__INT_WIDTH__", integer_type_node,
1061 			     unsigned_type_node);
1062   builtin_define_type_width ("__LONG_WIDTH__", long_integer_type_node,
1063 			     long_unsigned_type_node);
1064   builtin_define_type_width ("__LONG_LONG_WIDTH__",
1065 			     long_long_integer_type_node,
1066 			     long_long_unsigned_type_node);
1067   builtin_define_type_width ("__WCHAR_WIDTH__", underlying_wchar_type_node,
1068 			     NULL_TREE);
1069   builtin_define_type_width ("__WINT_WIDTH__", wint_type_node, NULL_TREE);
1070   builtin_define_type_width ("__PTRDIFF_WIDTH__", ptrdiff_type_node, NULL_TREE);
1071   builtin_define_type_width ("__SIZE_WIDTH__", size_type_node, NULL_TREE);
1072 
1073   if (c_dialect_cxx ())
1074     for (i = 0; i < NUM_INT_N_ENTS; i ++)
1075       if (int_n_enabled_p[i])
1076 	{
1077 	  char buf[35+20+20];
1078 
1079 	  /* These are used to configure the C++ library.  */
1080 
1081 	  if (!flag_iso || int_n_data[i].bitsize == POINTER_SIZE)
1082 	    {
1083 	      sprintf (buf, "__GLIBCXX_TYPE_INT_N_%d=__int%d", i, int_n_data[i].bitsize);
1084 	      cpp_define (parse_in, buf);
1085 
1086 	      sprintf (buf, "__GLIBCXX_BITSIZE_INT_N_%d=%d", i, int_n_data[i].bitsize);
1087 	      cpp_define (parse_in, buf);
1088 	    }
1089 	}
1090 
1091   /* stdint.h and the testsuite need to know these.  */
1092   builtin_define_stdint_macros ();
1093 
1094   /* Provide information for library headers to determine whether to
1095      define macros such as __STDC_IEC_559__ and
1096      __STDC_IEC_559_COMPLEX__.  */
1097   builtin_define_with_int_value ("__GCC_IEC_559", cpp_iec_559_value ());
1098   builtin_define_with_int_value ("__GCC_IEC_559_COMPLEX",
1099 				 cpp_iec_559_complex_value ());
1100 
1101   /* float.h needs these to correctly set FLT_EVAL_METHOD
1102 
1103      We define two values:
1104 
1105      __FLT_EVAL_METHOD__
1106        Which, depending on the value given for
1107        -fpermitted-flt-eval-methods, may be limited to only those values
1108        for FLT_EVAL_METHOD defined in C99/C11.
1109 
1110      __FLT_EVAL_METHOD_TS_18661_3__
1111        Which always permits the values for FLT_EVAL_METHOD defined in
1112        ISO/IEC TS 18661-3.  */
1113   builtin_define_with_int_value ("__FLT_EVAL_METHOD__",
1114 				 c_flt_eval_method (true));
1115   builtin_define_with_int_value ("__FLT_EVAL_METHOD_TS_18661_3__",
1116 				 c_flt_eval_method (false));
1117 
1118   /* And decfloat.h needs this.  */
1119   builtin_define_with_int_value ("__DEC_EVAL_METHOD__",
1120                                  TARGET_DEC_EVAL_METHOD);
1121 
1122   builtin_define_float_constants ("FLT", "F", "%s", "F", float_type_node);
1123   /* Cast the double precision constants.  This is needed when single
1124      precision constants are specified or when pragma FLOAT_CONST_DECIMAL64
1125      is used.  The correct result is computed by the compiler when using
1126      macros that include a cast.  We use a different cast for C++ to avoid
1127      problems with -Wold-style-cast.  */
1128   builtin_define_float_constants ("DBL", "L",
1129 				  (c_dialect_cxx ()
1130 				   ? "double(%s)"
1131 				   : "((double)%s)"),
1132 				  "", double_type_node);
1133   builtin_define_float_constants ("LDBL", "L", "%s", "L",
1134 				  long_double_type_node);
1135 
1136   for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
1137     {
1138       if (FLOATN_NX_TYPE_NODE (i) == NULL_TREE)
1139 	continue;
1140       char prefix[20], csuffix[20];
1141       sprintf (prefix, "FLT%d%s", floatn_nx_types[i].n,
1142 	       floatn_nx_types[i].extended ? "X" : "");
1143       sprintf (csuffix, "F%d%s", floatn_nx_types[i].n,
1144 	       floatn_nx_types[i].extended ? "x" : "");
1145       builtin_define_float_constants (prefix, ggc_strdup (csuffix), "%s",
1146 				      csuffix, FLOATN_NX_TYPE_NODE (i));
1147     }
1148 
1149   /* For decfloat.h.  */
1150   builtin_define_decimal_float_constants ("DEC32", "DF", dfloat32_type_node);
1151   builtin_define_decimal_float_constants ("DEC64", "DD", dfloat64_type_node);
1152   builtin_define_decimal_float_constants ("DEC128", "DL", dfloat128_type_node);
1153 
1154   /* For fixed-point fibt, ibit, max, min, and epsilon.  */
1155   if (targetm.fixed_point_supported_p ())
1156     {
1157       builtin_define_fixed_point_constants ("SFRACT", "HR",
1158 					    short_fract_type_node);
1159       builtin_define_fixed_point_constants ("USFRACT", "UHR",
1160 					    unsigned_short_fract_type_node);
1161       builtin_define_fixed_point_constants ("FRACT", "R",
1162 					    fract_type_node);
1163       builtin_define_fixed_point_constants ("UFRACT", "UR",
1164 					    unsigned_fract_type_node);
1165       builtin_define_fixed_point_constants ("LFRACT", "LR",
1166 					    long_fract_type_node);
1167       builtin_define_fixed_point_constants ("ULFRACT", "ULR",
1168 					    unsigned_long_fract_type_node);
1169       builtin_define_fixed_point_constants ("LLFRACT", "LLR",
1170 					    long_long_fract_type_node);
1171       builtin_define_fixed_point_constants ("ULLFRACT", "ULLR",
1172 					    unsigned_long_long_fract_type_node);
1173       builtin_define_fixed_point_constants ("SACCUM", "HK",
1174 					    short_accum_type_node);
1175       builtin_define_fixed_point_constants ("USACCUM", "UHK",
1176 					    unsigned_short_accum_type_node);
1177       builtin_define_fixed_point_constants ("ACCUM", "K",
1178 					    accum_type_node);
1179       builtin_define_fixed_point_constants ("UACCUM", "UK",
1180 					    unsigned_accum_type_node);
1181       builtin_define_fixed_point_constants ("LACCUM", "LK",
1182 					    long_accum_type_node);
1183       builtin_define_fixed_point_constants ("ULACCUM", "ULK",
1184 					    unsigned_long_accum_type_node);
1185       builtin_define_fixed_point_constants ("LLACCUM", "LLK",
1186 					    long_long_accum_type_node);
1187       builtin_define_fixed_point_constants ("ULLACCUM", "ULLK",
1188 					    unsigned_long_long_accum_type_node);
1189 
1190       builtin_define_fixed_point_constants ("QQ", "", qq_type_node);
1191       builtin_define_fixed_point_constants ("HQ", "", hq_type_node);
1192       builtin_define_fixed_point_constants ("SQ", "", sq_type_node);
1193       builtin_define_fixed_point_constants ("DQ", "", dq_type_node);
1194       builtin_define_fixed_point_constants ("TQ", "", tq_type_node);
1195       builtin_define_fixed_point_constants ("UQQ", "", uqq_type_node);
1196       builtin_define_fixed_point_constants ("UHQ", "", uhq_type_node);
1197       builtin_define_fixed_point_constants ("USQ", "", usq_type_node);
1198       builtin_define_fixed_point_constants ("UDQ", "", udq_type_node);
1199       builtin_define_fixed_point_constants ("UTQ", "", utq_type_node);
1200       builtin_define_fixed_point_constants ("HA", "", ha_type_node);
1201       builtin_define_fixed_point_constants ("SA", "", sa_type_node);
1202       builtin_define_fixed_point_constants ("DA", "", da_type_node);
1203       builtin_define_fixed_point_constants ("TA", "", ta_type_node);
1204       builtin_define_fixed_point_constants ("UHA", "", uha_type_node);
1205       builtin_define_fixed_point_constants ("USA", "", usa_type_node);
1206       builtin_define_fixed_point_constants ("UDA", "", uda_type_node);
1207       builtin_define_fixed_point_constants ("UTA", "", uta_type_node);
1208     }
1209 
1210   /* For libgcc-internal use only.  */
1211   if (flag_building_libgcc)
1212     {
1213       /* Properties of floating-point modes for libgcc2.c.  */
1214       opt_scalar_float_mode mode_iter;
1215       FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_FLOAT)
1216 	{
1217 	  scalar_float_mode mode = mode_iter.require ();
1218 	  const char *name = GET_MODE_NAME (mode);
1219 	  char *macro_name
1220 	    = (char *) alloca (strlen (name)
1221 			       + sizeof ("__LIBGCC__MANT_DIG__"));
1222 	  sprintf (macro_name, "__LIBGCC_%s_MANT_DIG__", name);
1223 	  builtin_define_with_int_value (macro_name,
1224 					 REAL_MODE_FORMAT (mode)->p);
1225 	  if (!targetm.scalar_mode_supported_p (mode)
1226 	      || !targetm.libgcc_floating_mode_supported_p (mode))
1227 	    continue;
1228 	  macro_name = (char *) alloca (strlen (name)
1229 					+ sizeof ("__LIBGCC_HAS__MODE__"));
1230 	  sprintf (macro_name, "__LIBGCC_HAS_%s_MODE__", name);
1231 	  cpp_define (pfile, macro_name);
1232 	  macro_name = (char *) alloca (strlen (name)
1233 					+ sizeof ("__LIBGCC__FUNC_EXT__"));
1234 	  sprintf (macro_name, "__LIBGCC_%s_FUNC_EXT__", name);
1235 	  char suffix[20] = "";
1236 	  if (mode == TYPE_MODE (double_type_node))
1237 	    ; /* Empty suffix correct.  */
1238 	  else if (mode == TYPE_MODE (float_type_node))
1239 	    suffix[0] = 'f';
1240 	  else if (mode == TYPE_MODE (long_double_type_node))
1241 	    suffix[0] = 'l';
1242 	  else
1243 	    {
1244 	      bool found_suffix = false;
1245 	      for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
1246 		if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
1247 		    && mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
1248 		  {
1249 		    sprintf (suffix, "f%d%s", floatn_nx_types[i].n,
1250 			     floatn_nx_types[i].extended ? "x" : "");
1251 		    found_suffix = true;
1252 		    break;
1253 		  }
1254 	      gcc_assert (found_suffix);
1255 	    }
1256 	  builtin_define_with_value (macro_name, suffix, 0);
1257 
1258 	  /* The way __LIBGCC_*_EXCESS_PRECISION__ is used is about
1259 	     eliminating excess precision from results assigned to
1260 	     variables - meaning it should be about the implicit excess
1261 	     precision only.  */
1262 	  bool excess_precision = false;
1263 	  machine_mode float16_type_mode = (float16_type_node
1264 					    ? TYPE_MODE (float16_type_node)
1265 					    : VOIDmode);
1266 	  switch (targetm.c.excess_precision
1267 		    (EXCESS_PRECISION_TYPE_IMPLICIT))
1268 	    {
1269 	    case FLT_EVAL_METHOD_UNPREDICTABLE:
1270 	    case FLT_EVAL_METHOD_PROMOTE_TO_LONG_DOUBLE:
1271 	      excess_precision = (mode == float16_type_mode
1272 				  || mode == TYPE_MODE (float_type_node)
1273 				  || mode == TYPE_MODE (double_type_node));
1274 	      break;
1275 
1276 	    case FLT_EVAL_METHOD_PROMOTE_TO_DOUBLE:
1277 	      excess_precision = (mode == float16_type_mode
1278 				  || mode == TYPE_MODE (float_type_node));
1279 	      break;
1280 	    case FLT_EVAL_METHOD_PROMOTE_TO_FLOAT:
1281 	      excess_precision = mode == float16_type_mode;
1282 	      break;
1283 	    case FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16:
1284 	      excess_precision = false;
1285 	      break;
1286 	    default:
1287 	      gcc_unreachable ();
1288 	    }
1289 	  macro_name = (char *) alloca (strlen (name)
1290 					+ sizeof ("__LIBGCC__EXCESS_"
1291 						  "PRECISION__"));
1292 	  sprintf (macro_name, "__LIBGCC_%s_EXCESS_PRECISION__", name);
1293 	  builtin_define_with_int_value (macro_name, excess_precision);
1294 	}
1295 
1296       /* For libgcc crtstuff.c and libgcc2.c.  */
1297       builtin_define_with_int_value ("__LIBGCC_EH_TABLES_CAN_BE_READ_ONLY__",
1298 				     EH_TABLES_CAN_BE_READ_ONLY);
1299 #ifdef EH_FRAME_SECTION_NAME
1300       builtin_define_with_value ("__LIBGCC_EH_FRAME_SECTION_NAME__",
1301 				 EH_FRAME_SECTION_NAME, 1);
1302 #endif
1303 #ifdef CTORS_SECTION_ASM_OP
1304       builtin_define_with_value ("__LIBGCC_CTORS_SECTION_ASM_OP__",
1305 				 CTORS_SECTION_ASM_OP, 1);
1306 #endif
1307 #ifdef DTORS_SECTION_ASM_OP
1308       builtin_define_with_value ("__LIBGCC_DTORS_SECTION_ASM_OP__",
1309 				 DTORS_SECTION_ASM_OP, 1);
1310 #endif
1311 #ifdef TEXT_SECTION_ASM_OP
1312       builtin_define_with_value ("__LIBGCC_TEXT_SECTION_ASM_OP__",
1313 				 TEXT_SECTION_ASM_OP, 1);
1314 #endif
1315 #ifdef INIT_SECTION_ASM_OP
1316       builtin_define_with_value ("__LIBGCC_INIT_SECTION_ASM_OP__",
1317 				 INIT_SECTION_ASM_OP, 1);
1318 #endif
1319 #ifdef INIT_ARRAY_SECTION_ASM_OP
1320       /* Despite the name of this target macro, the expansion is not
1321 	 actually used, and may be empty rather than a string
1322 	 constant.  */
1323       cpp_define (pfile, "__LIBGCC_INIT_ARRAY_SECTION_ASM_OP__");
1324 #endif
1325 
1326       /* For libgcc enable-execute-stack.c.  */
1327       builtin_define_with_int_value ("__LIBGCC_TRAMPOLINE_SIZE__",
1328 				     TRAMPOLINE_SIZE);
1329 
1330       /* For libgcc generic-morestack.c and unwinder code.  */
1331       if (STACK_GROWS_DOWNWARD)
1332 	cpp_define (pfile, "__LIBGCC_STACK_GROWS_DOWNWARD__");
1333 
1334       /* For libgcc unwinder code.  */
1335 #ifdef DONT_USE_BUILTIN_SETJMP
1336       cpp_define (pfile, "__LIBGCC_DONT_USE_BUILTIN_SETJMP__");
1337 #endif
1338 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
1339       builtin_define_with_int_value ("__LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__",
1340 				     DWARF_ALT_FRAME_RETURN_COLUMN);
1341 #endif
1342       builtin_define_with_int_value ("__LIBGCC_DWARF_FRAME_REGISTERS__",
1343 				     DWARF_FRAME_REGISTERS);
1344 #ifdef EH_RETURN_STACKADJ_RTX
1345       cpp_define (pfile, "__LIBGCC_EH_RETURN_STACKADJ_RTX__");
1346 #endif
1347 #ifdef JMP_BUF_SIZE
1348       builtin_define_with_int_value ("__LIBGCC_JMP_BUF_SIZE__",
1349 				     JMP_BUF_SIZE);
1350 #endif
1351       builtin_define_with_int_value ("__LIBGCC_STACK_POINTER_REGNUM__",
1352 				     STACK_POINTER_REGNUM);
1353 
1354       /* For libgcov.  */
1355       builtin_define_with_int_value ("__LIBGCC_VTABLE_USES_DESCRIPTORS__",
1356 				     TARGET_VTABLE_USES_DESCRIPTORS);
1357     }
1358 
1359   /* For use in assembly language.  */
1360   builtin_define_with_value ("__REGISTER_PREFIX__", REGISTER_PREFIX, 0);
1361   builtin_define_with_value ("__USER_LABEL_PREFIX__", user_label_prefix, 0);
1362 
1363   /* Misc.  */
1364   if (flag_gnu89_inline)
1365     cpp_define (pfile, "__GNUC_GNU_INLINE__");
1366   else
1367     cpp_define (pfile, "__GNUC_STDC_INLINE__");
1368 
1369   if (flag_no_inline)
1370     cpp_define (pfile, "__NO_INLINE__");
1371 
1372   if (flag_iso)
1373     cpp_define (pfile, "__STRICT_ANSI__");
1374 
1375   if (!flag_signed_char)
1376     cpp_define (pfile, "__CHAR_UNSIGNED__");
1377 
1378   if (c_dialect_cxx () && TYPE_UNSIGNED (wchar_type_node))
1379     cpp_define (pfile, "__WCHAR_UNSIGNED__");
1380 
1381   cpp_atomic_builtins (pfile);
1382 
1383 #ifdef DWARF2_UNWIND_INFO
1384   if (dwarf2out_do_cfi_asm ())
1385     cpp_define (pfile, "__GCC_HAVE_DWARF2_CFI_ASM");
1386 #endif
1387 
1388   /* Make the choice of ObjC runtime visible to source code.  */
1389   if (c_dialect_objc () && flag_next_runtime)
1390     cpp_define (pfile, "__NEXT_RUNTIME__");
1391 
1392   /* Show the availability of some target pragmas.  */
1393   cpp_define (pfile, "__PRAGMA_REDEFINE_EXTNAME");
1394 
1395   /* Make the choice of the stack protector runtime visible to source code.
1396      The macro names and values here were chosen for compatibility with an
1397      earlier implementation, i.e. ProPolice.  */
1398   if (flag_stack_protect == 4)
1399     cpp_define (pfile, "__SSP_EXPLICIT__=4");
1400   if (flag_stack_protect == 3)
1401     cpp_define (pfile, "__SSP_STRONG__=3");
1402   if (flag_stack_protect == 2)
1403     cpp_define (pfile, "__SSP_ALL__=2");
1404   else if (flag_stack_protect == 1)
1405     cpp_define (pfile, "__SSP__=1");
1406 
1407   if (flag_openacc)
1408     cpp_define (pfile, "_OPENACC=201306");
1409 
1410   if (flag_openmp)
1411     cpp_define (pfile, "_OPENMP=201511");
1412 
1413   for (i = 0; i < NUM_INT_N_ENTS; i ++)
1414     if (int_n_enabled_p[i])
1415       {
1416 	char buf[15+20];
1417 	sprintf(buf, "__SIZEOF_INT%d__", int_n_data[i].bitsize);
1418 	builtin_define_type_sizeof (buf,
1419 				    int_n_trees[i].signed_type);
1420       }
1421   builtin_define_type_sizeof ("__SIZEOF_WCHAR_T__", wchar_type_node);
1422   builtin_define_type_sizeof ("__SIZEOF_WINT_T__", wint_type_node);
1423   builtin_define_type_sizeof ("__SIZEOF_PTRDIFF_T__",
1424 			      unsigned_ptrdiff_type_node);
1425 
1426   /* A straightforward target hook doesn't work, because of problems
1427      linking that hook's body when part of non-C front ends.  */
1428 # define preprocessing_asm_p() (cpp_get_options (pfile)->lang == CLK_ASM)
1429 # define preprocessing_trad_p() (cpp_get_options (pfile)->traditional)
1430 # define builtin_define(TXT) cpp_define (pfile, TXT)
1431 # define builtin_assert(TXT) cpp_assert (pfile, TXT)
1432   TARGET_CPU_CPP_BUILTINS ();
1433   TARGET_OS_CPP_BUILTINS ();
1434   TARGET_OBJFMT_CPP_BUILTINS ();
1435 
1436   /* Support the __declspec keyword by turning them into attributes.
1437      Note that the current way we do this may result in a collision
1438      with predefined attributes later on.  This can be solved by using
1439      one attribute, say __declspec__, and passing args to it.  The
1440      problem with that approach is that args are not accumulated: each
1441      new appearance would clobber any existing args.  */
1442   if (TARGET_DECLSPEC)
1443     builtin_define ("__declspec(x)=__attribute__((x))");
1444 
1445   /* If decimal floating point is supported, tell the user if the
1446      alternate format (BID) is used instead of the standard (DPD)
1447      format.  */
1448   if (ENABLE_DECIMAL_FLOAT && ENABLE_DECIMAL_BID_FORMAT)
1449     cpp_define (pfile, "__DECIMAL_BID_FORMAT__");
1450 }
1451 
1452 /* Pass an object-like macro.  If it doesn't lie in the user's
1453    namespace, defines it unconditionally.  Otherwise define a version
1454    with two leading underscores, and another version with two leading
1455    and trailing underscores, and define the original only if an ISO
1456    standard was not nominated.
1457 
1458    e.g. passing "unix" defines "__unix", "__unix__" and possibly
1459    "unix".  Passing "_mips" defines "__mips", "__mips__" and possibly
1460    "_mips".  */
1461 void
1462 builtin_define_std (const char *macro)
1463 {
1464   size_t len = strlen (macro);
1465   char *buff = (char *) alloca (len + 5);
1466   char *p = buff + 2;
1467   char *q = p + len;
1468 
1469   /* prepend __ (or maybe just _) if in user's namespace.  */
1470   memcpy (p, macro, len + 1);
1471   if (!( *p == '_' && (p[1] == '_' || ISUPPER (p[1]))))
1472     {
1473       if (*p != '_')
1474 	*--p = '_';
1475       if (p[1] != '_')
1476 	*--p = '_';
1477     }
1478   cpp_define (parse_in, p);
1479 
1480   /* If it was in user's namespace...  */
1481   if (p != buff + 2)
1482     {
1483       /* Define the macro with leading and following __.  */
1484       if (q[-1] != '_')
1485 	*q++ = '_';
1486       if (q[-2] != '_')
1487 	*q++ = '_';
1488       *q = '\0';
1489       cpp_define (parse_in, p);
1490 
1491       /* Finally, define the original macro if permitted.  */
1492       if (!flag_iso)
1493 	cpp_define (parse_in, macro);
1494     }
1495 }
1496 
1497 /* Pass an object-like macro and a value to define it to.  The third
1498    parameter says whether or not to turn the value into a string
1499    constant.  */
1500 void
1501 builtin_define_with_value (const char *macro, const char *expansion, int is_str)
1502 {
1503   char *buf;
1504   size_t mlen = strlen (macro);
1505   size_t elen = strlen (expansion);
1506   size_t extra = 2;  /* space for an = and a NUL */
1507 
1508   if (is_str)
1509     {
1510       char *quoted_expansion = (char *) alloca (elen * 4 + 1);
1511       const char *p;
1512       char *q;
1513       extra += 2;  /* space for two quote marks */
1514       for (p = expansion, q = quoted_expansion; *p; p++)
1515 	{
1516 	  switch (*p)
1517 	    {
1518 	    case '\n':
1519 	      *q++ = '\\';
1520 	      *q++ = 'n';
1521 	      break;
1522 
1523 	    case '\t':
1524 	      *q++ = '\\';
1525 	      *q++ = 't';
1526 	      break;
1527 
1528 	    case '\\':
1529 	      *q++ = '\\';
1530 	      *q++ = '\\';
1531 	      break;
1532 
1533 	    case '"':
1534 	      *q++ = '\\';
1535 	      *q++ = '"';
1536 	      break;
1537 
1538 	    default:
1539 	      if (ISPRINT ((unsigned char) *p))
1540 		*q++ = *p;
1541 	      else
1542 		{
1543 		  sprintf (q, "\\%03o", (unsigned char) *p);
1544 		  q += 4;
1545 		}
1546 	    }
1547 	}
1548       *q = '\0';
1549       expansion = quoted_expansion;
1550       elen = q - expansion;
1551     }
1552 
1553   buf = (char *) alloca (mlen + elen + extra);
1554   if (is_str)
1555     sprintf (buf, "%s=\"%s\"", macro, expansion);
1556   else
1557     sprintf (buf, "%s=%s", macro, expansion);
1558 
1559   cpp_define (parse_in, buf);
1560 }
1561 
1562 
1563 /* Pass an object-like macro and an integer value to define it to.  */
1564 void
1565 builtin_define_with_int_value (const char *macro, HOST_WIDE_INT value)
1566 {
1567   char *buf;
1568   size_t mlen = strlen (macro);
1569   size_t vlen = 18;
1570   size_t extra = 2; /* space for = and NUL.  */
1571 
1572   buf = (char *) alloca (mlen + vlen + extra);
1573   memcpy (buf, macro, mlen);
1574   buf[mlen] = '=';
1575   sprintf (buf + mlen + 1, HOST_WIDE_INT_PRINT_DEC, value);
1576 
1577   cpp_define (parse_in, buf);
1578 }
1579 
1580 /* builtin_define_with_hex_fp_value is very expensive, so the following
1581    array and function allows it to be done lazily when __DBL_MAX__
1582    etc. is first used.  */
1583 
1584 struct GTY(()) lazy_hex_fp_value_struct
1585 {
1586   const char *hex_str;
1587   cpp_macro *macro;
1588   machine_mode mode;
1589   int digits;
1590   const char *fp_suffix;
1591 };
1592 /* Number of the expensive to compute macros we should evaluate lazily.
1593    Each builtin_define_float_constants invocation calls
1594    builtin_define_with_hex_fp_value 4 times and builtin_define_float_constants
1595    is called for FLT, DBL, LDBL and up to NUM_FLOATN_NX_TYPES times for
1596    FLTNN*.  */
1597 #define LAZY_HEX_FP_VALUES_CNT (4 * (3 + NUM_FLOATN_NX_TYPES))
1598 static GTY(()) struct lazy_hex_fp_value_struct
1599   lazy_hex_fp_values[LAZY_HEX_FP_VALUES_CNT];
1600 static GTY(()) int lazy_hex_fp_value_count;
1601 
1602 static bool
1603 lazy_hex_fp_value (cpp_reader *pfile ATTRIBUTE_UNUSED,
1604 		   cpp_hashnode *node)
1605 {
1606   REAL_VALUE_TYPE real;
1607   char dec_str[64], buf1[256];
1608   unsigned int idx;
1609   if (node->value.builtin < BT_FIRST_USER
1610       || (int) node->value.builtin >= BT_FIRST_USER + lazy_hex_fp_value_count)
1611     return false;
1612 
1613   idx = node->value.builtin - BT_FIRST_USER;
1614   real_from_string (&real, lazy_hex_fp_values[idx].hex_str);
1615   real_to_decimal_for_mode (dec_str, &real, sizeof (dec_str),
1616 			    lazy_hex_fp_values[idx].digits, 0,
1617 			    lazy_hex_fp_values[idx].mode);
1618 
1619   sprintf (buf1, "%s%s", dec_str, lazy_hex_fp_values[idx].fp_suffix);
1620   node->flags &= ~(NODE_BUILTIN | NODE_USED);
1621   node->value.macro = lazy_hex_fp_values[idx].macro;
1622   for (idx = 0; idx < node->value.macro->count; idx++)
1623     if (node->value.macro->exp.tokens[idx].type == CPP_NUMBER)
1624       break;
1625   gcc_assert (idx < node->value.macro->count);
1626   node->value.macro->exp.tokens[idx].val.str.len = strlen (buf1);
1627   node->value.macro->exp.tokens[idx].val.str.text
1628     = (const unsigned char *) ggc_strdup (buf1);
1629   return true;
1630 }
1631 
1632 /* Pass an object-like macro a hexadecimal floating-point value.  */
1633 static void
1634 builtin_define_with_hex_fp_value (const char *macro,
1635 				  tree type, int digits,
1636 				  const char *hex_str,
1637 				  const char *fp_suffix,
1638 				  const char *fp_cast)
1639 {
1640   REAL_VALUE_TYPE real;
1641   char dec_str[64], buf[256], buf1[128], buf2[64];
1642 
1643   /* This is very expensive, so if possible expand them lazily.  */
1644   if (lazy_hex_fp_value_count < LAZY_HEX_FP_VALUES_CNT
1645       && flag_dump_macros == 0
1646       && !cpp_get_options (parse_in)->traditional)
1647     {
1648       struct cpp_hashnode *node;
1649       if (lazy_hex_fp_value_count == 0)
1650 	cpp_get_callbacks (parse_in)->user_builtin_macro = lazy_hex_fp_value;
1651       sprintf (buf2, fp_cast, "1.1");
1652       sprintf (buf1, "%s=%s", macro, buf2);
1653       cpp_define (parse_in, buf1);
1654       node = C_CPP_HASHNODE (get_identifier (macro));
1655       lazy_hex_fp_values[lazy_hex_fp_value_count].hex_str
1656 	= ggc_strdup (hex_str);
1657       lazy_hex_fp_values[lazy_hex_fp_value_count].mode = TYPE_MODE (type);
1658       lazy_hex_fp_values[lazy_hex_fp_value_count].digits = digits;
1659       lazy_hex_fp_values[lazy_hex_fp_value_count].fp_suffix = fp_suffix;
1660       lazy_hex_fp_values[lazy_hex_fp_value_count].macro = node->value.macro;
1661       node->flags |= NODE_BUILTIN;
1662       node->value.builtin
1663 	= (enum cpp_builtin_type) (BT_FIRST_USER + lazy_hex_fp_value_count);
1664       lazy_hex_fp_value_count++;
1665       return;
1666     }
1667 
1668   /* Hex values are really cool and convenient, except that they're
1669      not supported in strict ISO C90 mode.  First, the "p-" sequence
1670      is not valid as part of a preprocessor number.  Second, we get a
1671      pedwarn from the preprocessor, which has no context, so we can't
1672      suppress the warning with __extension__.
1673 
1674      So instead what we do is construct the number in hex (because
1675      it's easy to get the exact correct value), parse it as a real,
1676      then print it back out as decimal.  */
1677 
1678   real_from_string (&real, hex_str);
1679   real_to_decimal_for_mode (dec_str, &real, sizeof (dec_str), digits, 0,
1680 			    TYPE_MODE (type));
1681 
1682   /* Assemble the macro in the following fashion
1683      macro = fp_cast [dec_str fp_suffix] */
1684   sprintf (buf2, "%s%s", dec_str, fp_suffix);
1685   sprintf (buf1, fp_cast, buf2);
1686   sprintf (buf, "%s=%s", macro, buf1);
1687 
1688   cpp_define (parse_in, buf);
1689 }
1690 
1691 /* Return a string constant for the suffix for a value of type TYPE
1692    promoted according to the integer promotions.  The type must be one
1693    of the standard integer type nodes.  */
1694 
1695 static const char *
1696 type_suffix (tree type)
1697 {
1698   static const char *const suffixes[] = { "", "U", "L", "UL", "LL", "ULL" };
1699   int unsigned_suffix;
1700   int is_long;
1701   int tp = TYPE_PRECISION (type);
1702 
1703   if (type == long_long_integer_type_node
1704       || type == long_long_unsigned_type_node
1705       || tp > TYPE_PRECISION (long_integer_type_node))
1706     is_long = 2;
1707   else if (type == long_integer_type_node
1708 	   || type == long_unsigned_type_node
1709 	   || tp > TYPE_PRECISION (integer_type_node))
1710     is_long = 1;
1711   else if (type == integer_type_node
1712 	   || type == unsigned_type_node
1713 	   || type == short_integer_type_node
1714 	   || type == short_unsigned_type_node
1715 	   || type == signed_char_type_node
1716 	   || type == unsigned_char_type_node
1717 	   /* ??? "char" is not a signed or unsigned integer type and
1718 	      so is not permitted for the standard typedefs, but some
1719 	      systems use it anyway.  */
1720 	   || type == char_type_node)
1721     is_long = 0;
1722   else
1723     gcc_unreachable ();
1724 
1725   unsigned_suffix = TYPE_UNSIGNED (type);
1726   if (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
1727     unsigned_suffix = 0;
1728   return suffixes[is_long * 2 + unsigned_suffix];
1729 }
1730 
1731 /* Define MACRO as a <stdint.h> constant-suffix macro for TYPE.  */
1732 static void
1733 builtin_define_constants (const char *macro, tree type)
1734 {
1735   const char *suffix;
1736   char *buf;
1737 
1738   suffix = type_suffix (type);
1739 
1740   if (suffix[0] == 0)
1741     {
1742       buf = (char *) alloca (strlen (macro) + 6);
1743       sprintf (buf, "%s(c)=c", macro);
1744     }
1745   else
1746     {
1747       buf = (char *) alloca (strlen (macro) + 9 + strlen (suffix) + 1);
1748       sprintf (buf, "%s(c)=c ## %s", macro, suffix);
1749     }
1750 
1751   cpp_define (parse_in, buf);
1752 }
1753 
1754 /* Define MAX for TYPE based on the precision of the type.  */
1755 
1756 static void
1757 builtin_define_type_max (const char *macro, tree type)
1758 {
1759   builtin_define_type_minmax (NULL, macro, type);
1760 }
1761 
1762 /* Given a value with COUNT LSBs set, fill BUF with a hexidecimal
1763    representation of that value.  For example, a COUNT of 10 would
1764    return "0x3ff".  */
1765 
1766 static void
1767 print_bits_of_hex (char *buf, int bufsz, int count)
1768 {
1769   gcc_assert (bufsz > 3);
1770   *buf++ = '0';
1771   *buf++ = 'x';
1772   bufsz -= 2;
1773 
1774   gcc_assert (count > 0);
1775 
1776   switch (count % 4) {
1777   case 0:
1778     break;
1779   case 1:
1780     *buf++ = '1';
1781     bufsz --;
1782     count -= 1;
1783     break;
1784   case 2:
1785     *buf++ = '3';
1786     bufsz --;
1787     count -= 2;
1788     break;
1789   case 3:
1790     *buf++ = '7';
1791     bufsz --;
1792     count -= 3;
1793     break;
1794   }
1795   while (count >= 4)
1796     {
1797       gcc_assert (bufsz > 1);
1798       *buf++ = 'f';
1799       bufsz --;
1800       count -= 4;
1801     }
1802   gcc_assert (bufsz > 0);
1803   *buf++ = 0;
1804 }
1805 
1806 /* Define MIN_MACRO (if not NULL) and MAX_MACRO for TYPE based on the
1807    precision of the type.  */
1808 
1809 static void
1810 builtin_define_type_minmax (const char *min_macro, const char *max_macro,
1811 			    tree type)
1812 {
1813 #define PBOH_SZ (MAX_BITSIZE_MODE_ANY_INT/4+4)
1814   char value[PBOH_SZ];
1815 
1816   const char *suffix;
1817   char *buf;
1818   int bits;
1819 
1820   bits = TYPE_PRECISION (type) + (TYPE_UNSIGNED (type) ? 0 : -1);
1821 
1822   print_bits_of_hex (value, PBOH_SZ, bits);
1823 
1824   suffix = type_suffix (type);
1825 
1826   buf = (char *) alloca (strlen (max_macro) + 1 + strlen (value)
1827                          + strlen (suffix) + 1);
1828   sprintf (buf, "%s=%s%s", max_macro, value, suffix);
1829 
1830   cpp_define (parse_in, buf);
1831 
1832   if (min_macro)
1833     {
1834       if (TYPE_UNSIGNED (type))
1835 	{
1836 	  buf = (char *) alloca (strlen (min_macro) + 2 + strlen (suffix) + 1);
1837 	  sprintf (buf, "%s=0%s", min_macro, suffix);
1838 	}
1839       else
1840 	{
1841 	  buf = (char *) alloca (strlen (min_macro) + 3
1842 				 + strlen (max_macro) + 6);
1843 	  sprintf (buf, "%s=(-%s - 1)", min_macro, max_macro);
1844 	}
1845       cpp_define (parse_in, buf);
1846     }
1847 }
1848 
1849 /* Define WIDTH_MACRO for the width of TYPE.  If TYPE2 is not NULL,
1850    both types must have the same width.  */
1851 
1852 static void
1853 builtin_define_type_width (const char *width_macro, tree type, tree type2)
1854 {
1855   if (type2 != NULL_TREE)
1856     gcc_assert (TYPE_PRECISION (type) == TYPE_PRECISION (type2));
1857   builtin_define_with_int_value (width_macro, TYPE_PRECISION (type));
1858 }
1859 
1860 #include "gt-c-family-c-cppbuiltin.h"
1861