cppbuiltin.c: New file.
[platform/upstream/gcc.git] / gcc / c-family / c-cppbuiltin.c
1 /* Define builtin-in macros for the C family front ends.
2    Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "version.h"
27 #include "flags.h"
28 #include "c-common.h"
29 #include "c-pragma.h"
30 #include "output.h"
31 #include "debug.h"              /* For dwarf2out_do_cfi_asm.  */
32 #include "toplev.h"
33 #include "tm_p.h"               /* For TARGET_CPU_CPP_BUILTINS & friends.  */
34 #include "target.h"
35 #include "cpp-id-data.h"
36 #include "cppbuiltin.h"
37
38 #ifndef TARGET_OS_CPP_BUILTINS
39 # define TARGET_OS_CPP_BUILTINS()
40 #endif
41
42 #ifndef TARGET_OBJFMT_CPP_BUILTINS
43 # define TARGET_OBJFMT_CPP_BUILTINS()
44 #endif
45
46 #ifndef REGISTER_PREFIX
47 #define REGISTER_PREFIX ""
48 #endif
49
50 /* Non-static as some targets don't use it.  */
51 void builtin_define_std (const char *) ATTRIBUTE_UNUSED;
52 static void builtin_define_with_int_value (const char *, HOST_WIDE_INT);
53 static void builtin_define_with_hex_fp_value (const char *, tree,
54                                               int, const char *,
55                                               const char *,
56                                               const char *);
57 static void builtin_define_stdint_macros (void);
58 static void builtin_define_constants (const char *, tree);
59 static void builtin_define_type_max (const char *, tree);
60 static void builtin_define_type_minmax (const char *, const char *, tree);
61 static void builtin_define_type_sizeof (const char *, tree);
62 static void builtin_define_float_constants (const char *,
63                                             const char *,
64                                             const char *,
65                                             tree);
66
67 /* Define NAME with value TYPE size_unit.  */
68 static void
69 builtin_define_type_sizeof (const char *name, tree type)
70 {
71   builtin_define_with_int_value (name,
72                                  tree_low_cst (TYPE_SIZE_UNIT (type), 1));
73 }
74
75 /* Define the float.h constants for TYPE using NAME_PREFIX, FP_SUFFIX,
76    and FP_CAST. */
77 static void
78 builtin_define_float_constants (const char *name_prefix,
79                                 const char *fp_suffix,
80                                 const char *fp_cast,
81                                 tree type)
82 {
83   /* Used to convert radix-based values to base 10 values in several cases.
84
85      In the max_exp -> max_10_exp conversion for 128-bit IEEE, we need at
86      least 6 significant digits for correct results.  Using the fraction
87      formed by (log(2)*1e6)/(log(10)*1e6) overflows a 32-bit integer as an
88      intermediate; perhaps someone can find a better approximation, in the
89      mean time, I suspect using doubles won't harm the bootstrap here.  */
90
91   const double log10_2 = .30102999566398119521;
92   double log10_b;
93   const struct real_format *fmt;
94   const struct real_format *ldfmt;
95
96   char name[64], buf[128];
97   int dig, min_10_exp, max_10_exp;
98   int decimal_dig;
99   int type_decimal_dig;
100
101   fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
102   gcc_assert (fmt->b != 10);
103   ldfmt = REAL_MODE_FORMAT (TYPE_MODE (long_double_type_node));
104   gcc_assert (ldfmt->b != 10);
105
106   /* The radix of the exponent representation.  */
107   if (type == float_type_node)
108     builtin_define_with_int_value ("__FLT_RADIX__", fmt->b);
109   log10_b = log10_2;
110
111   /* The number of radix digits, p, in the floating-point significand.  */
112   sprintf (name, "__%s_MANT_DIG__", name_prefix);
113   builtin_define_with_int_value (name, fmt->p);
114
115   /* The number of decimal digits, q, such that any floating-point number
116      with q decimal digits can be rounded into a floating-point number with
117      p radix b digits and back again without change to the q decimal digits,
118
119         p log10 b                       if b is a power of 10
120         floor((p - 1) log10 b)          otherwise
121   */
122   dig = (fmt->p - 1) * log10_b;
123   sprintf (name, "__%s_DIG__", name_prefix);
124   builtin_define_with_int_value (name, dig);
125
126   /* The minimum negative int x such that b**(x-1) is a normalized float.  */
127   sprintf (name, "__%s_MIN_EXP__", name_prefix);
128   sprintf (buf, "(%d)", fmt->emin);
129   builtin_define_with_value (name, buf, 0);
130
131   /* The minimum negative int x such that 10**x is a normalized float,
132
133           ceil (log10 (b ** (emin - 1)))
134         = ceil (log10 (b) * (emin - 1))
135
136      Recall that emin is negative, so the integer truncation calculates
137      the ceiling, not the floor, in this case.  */
138   min_10_exp = (fmt->emin - 1) * log10_b;
139   sprintf (name, "__%s_MIN_10_EXP__", name_prefix);
140   sprintf (buf, "(%d)", min_10_exp);
141   builtin_define_with_value (name, buf, 0);
142
143   /* The maximum int x such that b**(x-1) is a representable float.  */
144   sprintf (name, "__%s_MAX_EXP__", name_prefix);
145   builtin_define_with_int_value (name, fmt->emax);
146
147   /* The maximum int x such that 10**x is in the range of representable
148      finite floating-point numbers,
149
150           floor (log10((1 - b**-p) * b**emax))
151         = floor (log10(1 - b**-p) + log10(b**emax))
152         = floor (log10(1 - b**-p) + log10(b)*emax)
153
154      The safest thing to do here is to just compute this number.  But since
155      we don't link cc1 with libm, we cannot.  We could implement log10 here
156      a series expansion, but that seems too much effort because:
157
158      Note that the first term, for all extant p, is a number exceedingly close
159      to zero, but slightly negative.  Note that the second term is an integer
160      scaling an irrational number, and that because of the floor we are only
161      interested in its integral portion.
162
163      In order for the first term to have any effect on the integral portion
164      of the second term, the second term has to be exceedingly close to an
165      integer itself (e.g. 123.000000000001 or something).  Getting a result
166      that close to an integer requires that the irrational multiplicand have
167      a long series of zeros in its expansion, which doesn't occur in the
168      first 20 digits or so of log10(b).
169
170      Hand-waving aside, crunching all of the sets of constants above by hand
171      does not yield a case for which the first term is significant, which
172      in the end is all that matters.  */
173   max_10_exp = fmt->emax * log10_b;
174   sprintf (name, "__%s_MAX_10_EXP__", name_prefix);
175   builtin_define_with_int_value (name, max_10_exp);
176
177   /* The number of decimal digits, n, such that any floating-point number
178      can be rounded to n decimal digits and back again without change to
179      the value.
180
181         p * log10(b)                    if b is a power of 10
182         ceil(1 + p * log10(b))          otherwise
183
184      The only macro we care about is this number for the widest supported
185      floating type, but we want this value for rendering constants below.  */
186   {
187     double d_decimal_dig
188       = 1 + (fmt->p < ldfmt->p ? ldfmt->p : fmt->p) * log10_b;
189     decimal_dig = d_decimal_dig;
190     if (decimal_dig < d_decimal_dig)
191       decimal_dig++;
192   }
193   /* Similar, for this type rather than long double.  */
194   {
195     double type_d_decimal_dig = 1 + fmt->p * log10_b;
196     type_decimal_dig = type_d_decimal_dig;
197     if (type_decimal_dig < type_d_decimal_dig)
198       type_decimal_dig++;
199   }
200   if (type == long_double_type_node)
201     builtin_define_with_int_value ("__DECIMAL_DIG__", decimal_dig);
202   else
203     {
204       sprintf (name, "__%s_DECIMAL_DIG__", name_prefix);
205       builtin_define_with_int_value (name, type_decimal_dig);
206     }
207
208   /* Since, for the supported formats, B is always a power of 2, we
209      construct the following numbers directly as a hexadecimal
210      constants.  */
211   get_max_float (fmt, buf, sizeof (buf));
212
213   sprintf (name, "__%s_MAX__", name_prefix);
214   builtin_define_with_hex_fp_value (name, type, decimal_dig, buf, fp_suffix, fp_cast);
215
216   /* The minimum normalized positive floating-point number,
217      b**(emin-1).  */
218   sprintf (name, "__%s_MIN__", name_prefix);
219   sprintf (buf, "0x1p%d", fmt->emin - 1);
220   builtin_define_with_hex_fp_value (name, type, decimal_dig, buf, fp_suffix, fp_cast);
221
222   /* The difference between 1 and the least value greater than 1 that is
223      representable in the given floating point type, b**(1-p).  */
224   sprintf (name, "__%s_EPSILON__", name_prefix);
225   if (fmt->pnan < fmt->p)
226     /* This is an IBM extended double format, so 1.0 + any double is
227        representable precisely.  */
228       sprintf (buf, "0x1p%d", fmt->emin - fmt->p);
229     else
230       sprintf (buf, "0x1p%d", 1 - fmt->p);
231   builtin_define_with_hex_fp_value (name, type, decimal_dig, buf, fp_suffix, fp_cast);
232
233   /* For C++ std::numeric_limits<T>::denorm_min.  The minimum denormalized
234      positive floating-point number, b**(emin-p).  Zero for formats that
235      don't support denormals.  */
236   sprintf (name, "__%s_DENORM_MIN__", name_prefix);
237   if (fmt->has_denorm)
238     {
239       sprintf (buf, "0x1p%d", fmt->emin - fmt->p);
240       builtin_define_with_hex_fp_value (name, type, decimal_dig,
241                                         buf, fp_suffix, fp_cast);
242     }
243   else
244     {
245       sprintf (buf, "0.0%s", fp_suffix);
246       builtin_define_with_value (name, buf, 0);
247     }
248
249   sprintf (name, "__%s_HAS_DENORM__", name_prefix);
250   builtin_define_with_value (name, fmt->has_denorm ? "1" : "0", 0);
251
252   /* For C++ std::numeric_limits<T>::has_infinity.  */
253   sprintf (name, "__%s_HAS_INFINITY__", name_prefix);
254   builtin_define_with_int_value (name,
255                                  MODE_HAS_INFINITIES (TYPE_MODE (type)));
256   /* For C++ std::numeric_limits<T>::has_quiet_NaN.  We do not have a
257      predicate to distinguish a target that has both quiet and
258      signalling NaNs from a target that has only quiet NaNs or only
259      signalling NaNs, so we assume that a target that has any kind of
260      NaN has quiet NaNs.  */
261   sprintf (name, "__%s_HAS_QUIET_NAN__", name_prefix);
262   builtin_define_with_int_value (name, MODE_HAS_NANS (TYPE_MODE (type)));
263 }
264
265 /* Define __DECx__ constants for TYPE using NAME_PREFIX and SUFFIX. */
266 static void
267 builtin_define_decimal_float_constants (const char *name_prefix,
268                                         const char *suffix,
269                                         tree type)
270 {
271   const struct real_format *fmt;
272   char name[64], buf[128], *p;
273   int digits;
274
275   fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
276
277   /* The number of radix digits, p, in the significand.  */
278   sprintf (name, "__%s_MANT_DIG__", name_prefix);
279   builtin_define_with_int_value (name, fmt->p);
280
281   /* The minimum negative int x such that b**(x-1) is a normalized float.  */
282   sprintf (name, "__%s_MIN_EXP__", name_prefix);
283   sprintf (buf, "(%d)", fmt->emin);
284   builtin_define_with_value (name, buf, 0);
285
286   /* The maximum int x such that b**(x-1) is a representable float.  */
287   sprintf (name, "__%s_MAX_EXP__", name_prefix);
288   builtin_define_with_int_value (name, fmt->emax);
289
290   /* Compute the minimum representable value.  */
291   sprintf (name, "__%s_MIN__", name_prefix);
292   sprintf (buf, "1E%d%s", fmt->emin - 1, suffix);
293   builtin_define_with_value (name, buf, 0);
294
295   /* Compute the maximum representable value.  */
296   sprintf (name, "__%s_MAX__", name_prefix);
297   p = buf;
298   for (digits = fmt->p; digits; digits--)
299     {
300       *p++ = '9';
301       if (digits == fmt->p)
302         *p++ = '.';
303     }
304   *p = 0;
305   /* fmt->p plus 1, to account for the decimal point and fmt->emax
306      minus 1 because the digits are nines, not 1.0.  */
307   sprintf (&buf[fmt->p + 1], "E%d%s", fmt->emax - 1, suffix);
308   builtin_define_with_value (name, buf, 0);
309
310   /* Compute epsilon (the difference between 1 and least value greater
311      than 1 representable).  */
312   sprintf (name, "__%s_EPSILON__", name_prefix);
313   sprintf (buf, "1E-%d%s", fmt->p - 1, suffix);
314   builtin_define_with_value (name, buf, 0);
315
316   /* Minimum subnormal positive decimal value.  */
317   sprintf (name, "__%s_SUBNORMAL_MIN__", name_prefix);
318   p = buf;
319   for (digits = fmt->p; digits > 1; digits--)
320     {
321       *p++ = '0';
322       if (digits == fmt->p)
323         *p++ = '.';
324     }
325   *p = 0;
326   sprintf (&buf[fmt->p], "1E%d%s", fmt->emin - 1, suffix);
327   builtin_define_with_value (name, buf, 0);
328 }
329
330 /* Define fixed-point constants for TYPE using NAME_PREFIX and SUFFIX.  */
331
332 static void
333 builtin_define_fixed_point_constants (const char *name_prefix,
334                                       const char *suffix,
335                                       tree type)
336 {
337   char name[64], buf[256], *new_buf;
338   int i, mod;
339
340   sprintf (name, "__%s_FBIT__", name_prefix);
341   builtin_define_with_int_value (name, TYPE_FBIT (type));
342
343   sprintf (name, "__%s_IBIT__", name_prefix);
344   builtin_define_with_int_value (name, TYPE_IBIT (type));
345
346   /* If there is no suffix, defines are for fixed-point modes.
347      We just return.  */
348   if (strcmp (suffix, "") == 0)
349     return;
350
351   if (TYPE_UNSIGNED (type))
352     {
353       sprintf (name, "__%s_MIN__", name_prefix);
354       sprintf (buf, "0.0%s", suffix);
355       builtin_define_with_value (name, buf, 0);
356     }
357   else
358     {
359       sprintf (name, "__%s_MIN__", name_prefix);
360       if (ALL_ACCUM_MODE_P (TYPE_MODE (type)))
361         sprintf (buf, "(-0X1P%d%s-0X1P%d%s)", TYPE_IBIT (type) - 1, suffix,
362                  TYPE_IBIT (type) - 1, suffix);
363       else
364         sprintf (buf, "(-0.5%s-0.5%s)", suffix, suffix);
365       builtin_define_with_value (name, buf, 0);
366     }
367
368   sprintf (name, "__%s_MAX__", name_prefix);
369   sprintf (buf, "0X");
370   new_buf = buf + 2;
371   mod = (TYPE_FBIT (type) + TYPE_IBIT (type)) % 4;
372   if (mod)
373     sprintf (new_buf++, "%x", (1 << mod) - 1);
374   for (i = 0; i < (TYPE_FBIT (type) + TYPE_IBIT (type)) / 4; i++)
375     sprintf (new_buf++, "F");
376   sprintf (new_buf, "P-%d%s", TYPE_FBIT (type), suffix);
377   builtin_define_with_value (name, buf, 0);
378
379   sprintf (name, "__%s_EPSILON__", name_prefix);
380   sprintf (buf, "0x1P-%d%s", TYPE_FBIT (type), suffix);
381   builtin_define_with_value (name, buf, 0);
382 }
383
384 /* Define macros used by <stdint.h>.  */
385 static void
386 builtin_define_stdint_macros (void)
387 {
388   builtin_define_type_max ("__INTMAX_MAX__", intmax_type_node);
389   builtin_define_constants ("__INTMAX_C", intmax_type_node);
390   builtin_define_type_max ("__UINTMAX_MAX__", uintmax_type_node);
391   builtin_define_constants ("__UINTMAX_C", uintmax_type_node);
392   if (sig_atomic_type_node)
393     builtin_define_type_minmax ("__SIG_ATOMIC_MIN__", "__SIG_ATOMIC_MAX__",
394                                 sig_atomic_type_node);
395   if (int8_type_node)
396     builtin_define_type_max ("__INT8_MAX__", int8_type_node);
397   if (int16_type_node)
398     builtin_define_type_max ("__INT16_MAX__", int16_type_node);
399   if (int32_type_node)
400     builtin_define_type_max ("__INT32_MAX__", int32_type_node);
401   if (int64_type_node)
402     builtin_define_type_max ("__INT64_MAX__", int64_type_node);
403   if (uint8_type_node)
404     builtin_define_type_max ("__UINT8_MAX__", uint8_type_node);
405   if (uint16_type_node)
406     builtin_define_type_max ("__UINT16_MAX__", uint16_type_node);
407   if (c_uint32_type_node)
408     builtin_define_type_max ("__UINT32_MAX__", c_uint32_type_node);
409   if (c_uint64_type_node)
410     builtin_define_type_max ("__UINT64_MAX__", c_uint64_type_node);
411   if (int_least8_type_node)
412     {
413       builtin_define_type_max ("__INT_LEAST8_MAX__", int_least8_type_node);
414       builtin_define_constants ("__INT8_C", int_least8_type_node);
415     }
416   if (int_least16_type_node)
417     {
418       builtin_define_type_max ("__INT_LEAST16_MAX__", int_least16_type_node);
419       builtin_define_constants ("__INT16_C", int_least16_type_node);
420     }
421   if (int_least32_type_node)
422     {
423       builtin_define_type_max ("__INT_LEAST32_MAX__", int_least32_type_node);
424       builtin_define_constants ("__INT32_C", int_least32_type_node);
425     }
426   if (int_least64_type_node)
427     {
428       builtin_define_type_max ("__INT_LEAST64_MAX__", int_least64_type_node);
429       builtin_define_constants ("__INT64_C", int_least64_type_node);
430     }
431   if (uint_least8_type_node)
432     {
433       builtin_define_type_max ("__UINT_LEAST8_MAX__", uint_least8_type_node);
434       builtin_define_constants ("__UINT8_C", uint_least8_type_node);
435     }
436   if (uint_least16_type_node)
437     {
438       builtin_define_type_max ("__UINT_LEAST16_MAX__", uint_least16_type_node);
439       builtin_define_constants ("__UINT16_C", uint_least16_type_node);
440     }
441   if (uint_least32_type_node)
442     {
443       builtin_define_type_max ("__UINT_LEAST32_MAX__", uint_least32_type_node);
444       builtin_define_constants ("__UINT32_C", uint_least32_type_node);
445     }
446   if (uint_least64_type_node)
447     {
448       builtin_define_type_max ("__UINT_LEAST64_MAX__", uint_least64_type_node);
449       builtin_define_constants ("__UINT64_C", uint_least64_type_node);
450     }
451   if (int_fast8_type_node)
452     builtin_define_type_max ("__INT_FAST8_MAX__", int_fast8_type_node);
453   if (int_fast16_type_node)
454     builtin_define_type_max ("__INT_FAST16_MAX__", int_fast16_type_node);
455   if (int_fast32_type_node)
456     builtin_define_type_max ("__INT_FAST32_MAX__", int_fast32_type_node);
457   if (int_fast64_type_node)
458     builtin_define_type_max ("__INT_FAST64_MAX__", int_fast64_type_node);
459   if (uint_fast8_type_node)
460     builtin_define_type_max ("__UINT_FAST8_MAX__", uint_fast8_type_node);
461   if (uint_fast16_type_node)
462     builtin_define_type_max ("__UINT_FAST16_MAX__", uint_fast16_type_node);
463   if (uint_fast32_type_node)
464     builtin_define_type_max ("__UINT_FAST32_MAX__", uint_fast32_type_node);
465   if (uint_fast64_type_node)
466     builtin_define_type_max ("__UINT_FAST64_MAX__", uint_fast64_type_node);
467   if (intptr_type_node)
468     builtin_define_type_max ("__INTPTR_MAX__", intptr_type_node);
469   if (uintptr_type_node)
470     builtin_define_type_max ("__UINTPTR_MAX__", uintptr_type_node);
471 }
472
473 /* Adjust the optimization macros when a #pragma GCC optimization is done to
474    reflect the current level.  */
475 void
476 c_cpp_builtins_optimize_pragma (cpp_reader *pfile, tree prev_tree,
477                                 tree cur_tree)
478 {
479   struct cl_optimization *prev = TREE_OPTIMIZATION (prev_tree);
480   struct cl_optimization *cur  = TREE_OPTIMIZATION (cur_tree);
481   bool prev_fast_math;
482   bool cur_fast_math;
483
484   /* -undef turns off target-specific built-ins.  */
485   if (flag_undef)
486     return;
487
488   /* Other target-independent built-ins determined by command-line
489      options.  */
490   if (!prev->x_optimize_size && cur->x_optimize_size)
491     cpp_define (pfile, "__OPTIMIZE_SIZE__");
492   else if (prev->x_optimize_size && !cur->x_optimize_size)
493     cpp_undef (pfile, "__OPTIMIZE_SIZE__");
494
495   if (!prev->x_optimize && cur->x_optimize)
496     cpp_define (pfile, "__OPTIMIZE__");
497   else if (prev->x_optimize && !cur->x_optimize)
498     cpp_undef (pfile, "__OPTIMIZE__");
499
500   prev_fast_math = fast_math_flags_struct_set_p (prev);
501   cur_fast_math  = fast_math_flags_struct_set_p (cur);
502   if (!prev_fast_math && cur_fast_math)
503     cpp_define (pfile, "__FAST_MATH__");
504   else if (prev_fast_math && !cur_fast_math)
505     cpp_undef (pfile, "__FAST_MATH__");
506
507   if (!prev->x_flag_signaling_nans && cur->x_flag_signaling_nans)
508     cpp_define (pfile, "__SUPPORT_SNAN__");
509   else if (prev->x_flag_signaling_nans && !cur->x_flag_signaling_nans)
510     cpp_undef (pfile, "__SUPPORT_SNAN__");
511
512   if (!prev->x_flag_finite_math_only && cur->x_flag_finite_math_only)
513     {
514       cpp_undef (pfile, "__FINITE_MATH_ONLY__");
515       cpp_define (pfile, "__FINITE_MATH_ONLY__=1");
516     }
517   else if (!prev->x_flag_finite_math_only && cur->x_flag_finite_math_only)
518     {
519       cpp_undef (pfile, "__FINITE_MATH_ONLY__");
520       cpp_define (pfile, "__FINITE_MATH_ONLY__=0");
521     }
522 }
523
524
525 /* Hook that registers front end and target-specific built-ins.  */
526 void
527 c_cpp_builtins (cpp_reader *pfile)
528 {
529   /* -undef turns off target-specific built-ins.  */
530   if (flag_undef)
531     return;
532
533   define_language_independent_builtin_macros (pfile);
534
535   if (c_dialect_cxx ())
536   {
537     int major;
538     parse_basever (&major, NULL, NULL);
539     cpp_define_formatted (pfile, "__GNUG__=%d", major);
540   }
541
542   /* For stddef.h.  They require macros defined in c-common.c.  */
543   c_stddef_cpp_builtins ();
544
545   if (c_dialect_cxx ())
546     {
547       if (flag_weak && SUPPORTS_ONE_ONLY)
548         cpp_define (pfile, "__GXX_WEAK__=1");
549       else
550         cpp_define (pfile, "__GXX_WEAK__=0");
551       if (warn_deprecated)
552         cpp_define (pfile, "__DEPRECATED");
553       if (flag_rtti)
554         cpp_define (pfile, "__GXX_RTTI");
555       if (cxx_dialect == cxx0x)
556         cpp_define (pfile, "__GXX_EXPERIMENTAL_CXX0X__");
557     }
558   /* Note that we define this for C as well, so that we know if
559      __attribute__((cleanup)) will interface with EH.  */
560   if (flag_exceptions)
561     cpp_define (pfile, "__EXCEPTIONS");
562
563   /* Represents the C++ ABI version, always defined so it can be used while
564      preprocessing C and assembler.  */
565   if (flag_abi_version == 0)
566     /* Use a very large value so that:
567
568          #if __GXX_ABI_VERSION >= <value for version X>
569
570        will work whether the user explicitly says "-fabi-version=x" or
571        "-fabi-version=0".  Do not use INT_MAX because that will be
572        different from system to system.  */
573     builtin_define_with_int_value ("__GXX_ABI_VERSION", 999999);
574   else if (flag_abi_version == 1)
575     /* Due to a historical accident, this version had the value
576        "102".  */
577     builtin_define_with_int_value ("__GXX_ABI_VERSION", 102);
578   else
579     /* Newer versions have values 1002, 1003, ....  */
580     builtin_define_with_int_value ("__GXX_ABI_VERSION",
581                                    1000 + flag_abi_version);
582
583   /* libgcc needs to know this.  */
584   if (targetm.except_unwind_info () == UI_SJLJ)
585     cpp_define (pfile, "__USING_SJLJ_EXCEPTIONS__");
586
587   /* limits.h and stdint.h need to know these.  */
588   builtin_define_type_max ("__SCHAR_MAX__", signed_char_type_node);
589   builtin_define_type_max ("__SHRT_MAX__", short_integer_type_node);
590   builtin_define_type_max ("__INT_MAX__", integer_type_node);
591   builtin_define_type_max ("__LONG_MAX__", long_integer_type_node);
592   builtin_define_type_max ("__LONG_LONG_MAX__", long_long_integer_type_node);
593   builtin_define_type_minmax ("__WCHAR_MIN__", "__WCHAR_MAX__",
594                               underlying_wchar_type_node);
595   builtin_define_type_minmax ("__WINT_MIN__", "__WINT_MAX__", wint_type_node);
596   builtin_define_type_max ("__PTRDIFF_MAX__", ptrdiff_type_node);
597   builtin_define_type_max ("__SIZE_MAX__", size_type_node);
598
599   /* stdint.h and the testsuite need to know these.  */
600   builtin_define_stdint_macros ();
601
602   /* float.h needs to know this.  */
603   builtin_define_with_int_value ("__FLT_EVAL_METHOD__",
604                                  TARGET_FLT_EVAL_METHOD);
605
606   /* And decfloat.h needs this.  */
607   builtin_define_with_int_value ("__DEC_EVAL_METHOD__",
608                                  TARGET_DEC_EVAL_METHOD);
609
610   builtin_define_float_constants ("FLT", "F", "%s", float_type_node);
611   /* Cast the double precision constants.  This is needed when single
612      precision constants are specified or when pragma FLOAT_CONST_DECIMAL64
613      is used.  The correct result is computed by the compiler when using
614      macros that include a cast.  */
615   builtin_define_float_constants ("DBL", "L", "((double)%s)", double_type_node);
616   builtin_define_float_constants ("LDBL", "L", "%s", long_double_type_node);
617
618   /* For decfloat.h.  */
619   builtin_define_decimal_float_constants ("DEC32", "DF", dfloat32_type_node);
620   builtin_define_decimal_float_constants ("DEC64", "DD", dfloat64_type_node);
621   builtin_define_decimal_float_constants ("DEC128", "DL", dfloat128_type_node);
622
623   /* For fixed-point fibt, ibit, max, min, and epsilon.  */
624   if (targetm.fixed_point_supported_p ())
625     {
626       builtin_define_fixed_point_constants ("SFRACT", "HR",
627                                             short_fract_type_node);
628       builtin_define_fixed_point_constants ("USFRACT", "UHR",
629                                             unsigned_short_fract_type_node);
630       builtin_define_fixed_point_constants ("FRACT", "R",
631                                             fract_type_node);
632       builtin_define_fixed_point_constants ("UFRACT", "UR",
633                                             unsigned_fract_type_node);
634       builtin_define_fixed_point_constants ("LFRACT", "LR",
635                                             long_fract_type_node);
636       builtin_define_fixed_point_constants ("ULFRACT", "ULR",
637                                             unsigned_long_fract_type_node);
638       builtin_define_fixed_point_constants ("LLFRACT", "LLR",
639                                             long_long_fract_type_node);
640       builtin_define_fixed_point_constants ("ULLFRACT", "ULLR",
641                                             unsigned_long_long_fract_type_node);
642       builtin_define_fixed_point_constants ("SACCUM", "HK",
643                                             short_accum_type_node);
644       builtin_define_fixed_point_constants ("USACCUM", "UHK",
645                                             unsigned_short_accum_type_node);
646       builtin_define_fixed_point_constants ("ACCUM", "K",
647                                             accum_type_node);
648       builtin_define_fixed_point_constants ("UACCUM", "UK",
649                                             unsigned_accum_type_node);
650       builtin_define_fixed_point_constants ("LACCUM", "LK",
651                                             long_accum_type_node);
652       builtin_define_fixed_point_constants ("ULACCUM", "ULK",
653                                             unsigned_long_accum_type_node);
654       builtin_define_fixed_point_constants ("LLACCUM", "LLK",
655                                             long_long_accum_type_node);
656       builtin_define_fixed_point_constants ("ULLACCUM", "ULLK",
657                                             unsigned_long_long_accum_type_node);
658
659       builtin_define_fixed_point_constants ("QQ", "", qq_type_node);
660       builtin_define_fixed_point_constants ("HQ", "", hq_type_node);
661       builtin_define_fixed_point_constants ("SQ", "", sq_type_node);
662       builtin_define_fixed_point_constants ("DQ", "", dq_type_node);
663       builtin_define_fixed_point_constants ("TQ", "", tq_type_node);
664       builtin_define_fixed_point_constants ("UQQ", "", uqq_type_node);
665       builtin_define_fixed_point_constants ("UHQ", "", uhq_type_node);
666       builtin_define_fixed_point_constants ("USQ", "", usq_type_node);
667       builtin_define_fixed_point_constants ("UDQ", "", udq_type_node);
668       builtin_define_fixed_point_constants ("UTQ", "", utq_type_node);
669       builtin_define_fixed_point_constants ("HA", "", ha_type_node);
670       builtin_define_fixed_point_constants ("SA", "", sa_type_node);
671       builtin_define_fixed_point_constants ("DA", "", da_type_node);
672       builtin_define_fixed_point_constants ("TA", "", ta_type_node);
673       builtin_define_fixed_point_constants ("UHA", "", uha_type_node);
674       builtin_define_fixed_point_constants ("USA", "", usa_type_node);
675       builtin_define_fixed_point_constants ("UDA", "", uda_type_node);
676       builtin_define_fixed_point_constants ("UTA", "", uta_type_node);
677     }
678
679   /* For use in assembly language.  */
680   builtin_define_with_value ("__REGISTER_PREFIX__", REGISTER_PREFIX, 0);
681   builtin_define_with_value ("__USER_LABEL_PREFIX__", user_label_prefix, 0);
682
683   /* Misc.  */
684   if (flag_gnu89_inline)
685     cpp_define (pfile, "__GNUC_GNU_INLINE__");
686   else
687     cpp_define (pfile, "__GNUC_STDC_INLINE__");
688
689   if (flag_no_inline)
690     cpp_define (pfile, "__NO_INLINE__");
691
692   if (flag_iso)
693     cpp_define (pfile, "__STRICT_ANSI__");
694
695   if (!flag_signed_char)
696     cpp_define (pfile, "__CHAR_UNSIGNED__");
697
698   if (c_dialect_cxx () && TYPE_UNSIGNED (wchar_type_node))
699     cpp_define (pfile, "__WCHAR_UNSIGNED__");
700
701   /* Tell source code if the compiler makes sync_compare_and_swap
702      builtins available.  */
703 #ifdef HAVE_sync_compare_and_swapqi
704   if (HAVE_sync_compare_and_swapqi)
705     cpp_define (pfile, "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
706 #endif
707
708 #ifdef HAVE_sync_compare_and_swaphi
709   if (HAVE_sync_compare_and_swaphi)
710     cpp_define (pfile, "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
711 #endif
712
713 #ifdef HAVE_sync_compare_and_swapsi
714   if (HAVE_sync_compare_and_swapsi)
715     cpp_define (pfile, "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
716 #endif
717
718 #ifdef HAVE_sync_compare_and_swapdi
719   if (HAVE_sync_compare_and_swapdi)
720     cpp_define (pfile, "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
721 #endif
722
723 #ifdef HAVE_sync_compare_and_swapti
724   if (HAVE_sync_compare_and_swapti)
725     cpp_define (pfile, "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_16");
726 #endif
727
728 #ifdef DWARF2_UNWIND_INFO
729   if (dwarf2out_do_cfi_asm ())
730     cpp_define (pfile, "__GCC_HAVE_DWARF2_CFI_ASM");
731 #endif
732
733   /* Make the choice of ObjC runtime visible to source code.  */
734   if (c_dialect_objc () && flag_next_runtime)
735     cpp_define (pfile, "__NEXT_RUNTIME__");
736
737   /* Show the availability of some target pragmas.  */
738   cpp_define (pfile, "__PRAGMA_REDEFINE_EXTNAME");
739
740   if (targetm.handle_pragma_extern_prefix)
741     cpp_define (pfile, "__PRAGMA_EXTERN_PREFIX");
742
743   /* Make the choice of the stack protector runtime visible to source code.
744      The macro names and values here were chosen for compatibility with an
745      earlier implementation, i.e. ProPolice.  */
746   if (flag_stack_protect == 2)
747     cpp_define (pfile, "__SSP_ALL__=2");
748   else if (flag_stack_protect == 1)
749     cpp_define (pfile, "__SSP__=1");
750
751   if (flag_openmp)
752     cpp_define (pfile, "_OPENMP=200805");
753
754   if (int128_integer_type_node != NULL_TREE)
755     builtin_define_type_sizeof ("__SIZEOF_INT128__",
756                                 int128_integer_type_node);
757   builtin_define_type_sizeof ("__SIZEOF_WCHAR_T__", wchar_type_node);
758   builtin_define_type_sizeof ("__SIZEOF_WINT_T__", wint_type_node);
759   builtin_define_type_sizeof ("__SIZEOF_PTRDIFF_T__",
760                               unsigned_ptrdiff_type_node);
761
762   /* A straightforward target hook doesn't work, because of problems
763      linking that hook's body when part of non-C front ends.  */
764 # define preprocessing_asm_p() (cpp_get_options (pfile)->lang == CLK_ASM)
765 # define preprocessing_trad_p() (cpp_get_options (pfile)->traditional)
766 # define builtin_define(TXT) cpp_define (pfile, TXT)
767 # define builtin_assert(TXT) cpp_assert (pfile, TXT)
768   TARGET_CPU_CPP_BUILTINS ();
769   TARGET_OS_CPP_BUILTINS ();
770   TARGET_OBJFMT_CPP_BUILTINS ();
771
772   /* Support the __declspec keyword by turning them into attributes.
773      Note that the current way we do this may result in a collision
774      with predefined attributes later on.  This can be solved by using
775      one attribute, say __declspec__, and passing args to it.  The
776      problem with that approach is that args are not accumulated: each
777      new appearance would clobber any existing args.  */
778   if (TARGET_DECLSPEC)
779     builtin_define ("__declspec(x)=__attribute__((x))");
780
781   /* If decimal floating point is supported, tell the user if the
782      alternate format (BID) is used instead of the standard (DPD)
783      format.  */
784   if (ENABLE_DECIMAL_FLOAT && ENABLE_DECIMAL_BID_FORMAT)
785     cpp_define (pfile, "__DECIMAL_BID_FORMAT__");
786 }
787
788 /* Pass an object-like macro.  If it doesn't lie in the user's
789    namespace, defines it unconditionally.  Otherwise define a version
790    with two leading underscores, and another version with two leading
791    and trailing underscores, and define the original only if an ISO
792    standard was not nominated.
793
794    e.g. passing "unix" defines "__unix", "__unix__" and possibly
795    "unix".  Passing "_mips" defines "__mips", "__mips__" and possibly
796    "_mips".  */
797 void
798 builtin_define_std (const char *macro)
799 {
800   size_t len = strlen (macro);
801   char *buff = (char *) alloca (len + 5);
802   char *p = buff + 2;
803   char *q = p + len;
804
805   /* prepend __ (or maybe just _) if in user's namespace.  */
806   memcpy (p, macro, len + 1);
807   if (!( *p == '_' && (p[1] == '_' || ISUPPER (p[1]))))
808     {
809       if (*p != '_')
810         *--p = '_';
811       if (p[1] != '_')
812         *--p = '_';
813     }
814   cpp_define (parse_in, p);
815
816   /* If it was in user's namespace...  */
817   if (p != buff + 2)
818     {
819       /* Define the macro with leading and following __.  */
820       if (q[-1] != '_')
821         *q++ = '_';
822       if (q[-2] != '_')
823         *q++ = '_';
824       *q = '\0';
825       cpp_define (parse_in, p);
826
827       /* Finally, define the original macro if permitted.  */
828       if (!flag_iso)
829         cpp_define (parse_in, macro);
830     }
831 }
832
833 /* Pass an object-like macro and a value to define it to.  The third
834    parameter says whether or not to turn the value into a string
835    constant.  */
836 void
837 builtin_define_with_value (const char *macro, const char *expansion, int is_str)
838 {
839   char *buf;
840   size_t mlen = strlen (macro);
841   size_t elen = strlen (expansion);
842   size_t extra = 2;  /* space for an = and a NUL */
843
844   if (is_str)
845     extra += 2;  /* space for two quote marks */
846
847   buf = (char *) alloca (mlen + elen + extra);
848   if (is_str)
849     sprintf (buf, "%s=\"%s\"", macro, expansion);
850   else
851     sprintf (buf, "%s=%s", macro, expansion);
852
853   cpp_define (parse_in, buf);
854 }
855
856
857 /* Pass an object-like macro and an integer value to define it to.  */
858 static void
859 builtin_define_with_int_value (const char *macro, HOST_WIDE_INT value)
860 {
861   char *buf;
862   size_t mlen = strlen (macro);
863   size_t vlen = 18;
864   size_t extra = 2; /* space for = and NUL.  */
865
866   buf = (char *) alloca (mlen + vlen + extra);
867   memcpy (buf, macro, mlen);
868   buf[mlen] = '=';
869   sprintf (buf + mlen + 1, HOST_WIDE_INT_PRINT_DEC, value);
870
871   cpp_define (parse_in, buf);
872 }
873
874 /* builtin_define_with_hex_fp_value is very expensive, so the following
875    array and function allows it to be done lazily when __DBL_MAX__
876    etc. is first used.  */
877
878 struct GTY(()) lazy_hex_fp_value_struct
879 {
880   const char *hex_str;
881   cpp_macro *macro;
882   enum machine_mode mode;
883   int digits;
884   const char *fp_suffix;
885 };
886 static GTY(()) struct lazy_hex_fp_value_struct lazy_hex_fp_values[12];
887 static GTY(()) int lazy_hex_fp_value_count;
888
889 static bool
890 lazy_hex_fp_value (cpp_reader *pfile ATTRIBUTE_UNUSED,
891                    cpp_hashnode *node)
892 {
893   REAL_VALUE_TYPE real;
894   char dec_str[64], buf1[256];
895   unsigned int idx;
896   if (node->value.builtin < BT_FIRST_USER
897       || (int) node->value.builtin >= BT_FIRST_USER + lazy_hex_fp_value_count)
898     return false;
899
900   idx = node->value.builtin - BT_FIRST_USER;
901   real_from_string (&real, lazy_hex_fp_values[idx].hex_str);
902   real_to_decimal_for_mode (dec_str, &real, sizeof (dec_str),
903                             lazy_hex_fp_values[idx].digits, 0,
904                             lazy_hex_fp_values[idx].mode);
905
906   sprintf (buf1, "%s%s", dec_str, lazy_hex_fp_values[idx].fp_suffix);
907   node->flags &= ~(NODE_BUILTIN | NODE_USED);
908   node->value.macro = lazy_hex_fp_values[idx].macro;
909   for (idx = 0; idx < node->value.macro->count; idx++)
910     if (node->value.macro->exp.tokens[idx].type == CPP_NUMBER)
911       break;
912   gcc_assert (idx < node->value.macro->count);
913   node->value.macro->exp.tokens[idx].val.str.len = strlen (buf1);
914   node->value.macro->exp.tokens[idx].val.str.text
915     = (const unsigned char *) ggc_strdup (buf1);
916   return true;
917 }
918
919 /* Pass an object-like macro a hexadecimal floating-point value.  */
920 static void
921 builtin_define_with_hex_fp_value (const char *macro,
922                                   tree type, int digits,
923                                   const char *hex_str,
924                                   const char *fp_suffix,
925                                   const char *fp_cast)
926 {
927   REAL_VALUE_TYPE real;
928   char dec_str[64], buf1[256], buf2[256];
929
930   /* This is very expensive, so if possible expand them lazily.  */
931   if (lazy_hex_fp_value_count < 12
932       && flag_dump_macros == 0
933       && !cpp_get_options (parse_in)->traditional)
934     {
935       struct cpp_hashnode *node;
936       if (lazy_hex_fp_value_count == 0)
937         cpp_get_callbacks (parse_in)->user_builtin_macro = lazy_hex_fp_value;
938       sprintf (buf2, fp_cast, "1.1");
939       sprintf (buf1, "%s=%s", macro, buf2);
940       cpp_define (parse_in, buf1);
941       node = C_CPP_HASHNODE (get_identifier (macro));
942       lazy_hex_fp_values[lazy_hex_fp_value_count].hex_str
943         = ggc_strdup (hex_str);
944       lazy_hex_fp_values[lazy_hex_fp_value_count].mode = TYPE_MODE (type);
945       lazy_hex_fp_values[lazy_hex_fp_value_count].digits = digits;
946       lazy_hex_fp_values[lazy_hex_fp_value_count].fp_suffix = fp_suffix;
947       lazy_hex_fp_values[lazy_hex_fp_value_count].macro = node->value.macro;
948       node->flags |= NODE_BUILTIN;
949       node->value.builtin
950         = (enum cpp_builtin_type) (BT_FIRST_USER + lazy_hex_fp_value_count);
951       lazy_hex_fp_value_count++;
952       return;
953     }
954
955   /* Hex values are really cool and convenient, except that they're
956      not supported in strict ISO C90 mode.  First, the "p-" sequence
957      is not valid as part of a preprocessor number.  Second, we get a
958      pedwarn from the preprocessor, which has no context, so we can't
959      suppress the warning with __extension__.
960
961      So instead what we do is construct the number in hex (because
962      it's easy to get the exact correct value), parse it as a real,
963      then print it back out as decimal.  */
964
965   real_from_string (&real, hex_str);
966   real_to_decimal_for_mode (dec_str, &real, sizeof (dec_str), digits, 0,
967                             TYPE_MODE (type));
968
969   /* Assemble the macro in the following fashion
970      macro = fp_cast [dec_str fp_suffix] */
971   sprintf (buf1, "%s%s", dec_str, fp_suffix);
972   sprintf (buf2, fp_cast, buf1);
973   sprintf (buf1, "%s=%s", macro, buf2);
974
975   cpp_define (parse_in, buf1);
976 }
977
978 /* Return a string constant for the suffix for a value of type TYPE
979    promoted according to the integer promotions.  The type must be one
980    of the standard integer type nodes.  */
981
982 static const char *
983 type_suffix (tree type)
984 {
985   static const char *const suffixes[] = { "", "U", "L", "UL", "LL", "ULL" };
986   int unsigned_suffix;
987   int is_long;
988
989   if (type == long_long_integer_type_node
990       || type == long_long_unsigned_type_node)
991     is_long = 2;
992   else if (type == long_integer_type_node
993            || type == long_unsigned_type_node)
994     is_long = 1;
995   else if (type == integer_type_node
996            || type == unsigned_type_node
997            || type == short_integer_type_node
998            || type == short_unsigned_type_node
999            || type == signed_char_type_node
1000            || type == unsigned_char_type_node
1001            /* ??? "char" is not a signed or unsigned integer type and
1002               so is not permitted for the standard typedefs, but some
1003               systems use it anyway.  */
1004            || type == char_type_node)
1005     is_long = 0;
1006   else
1007     gcc_unreachable ();
1008
1009   unsigned_suffix = TYPE_UNSIGNED (type);
1010   if (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
1011     unsigned_suffix = 0;
1012   return suffixes[is_long * 2 + unsigned_suffix];
1013 }
1014
1015 /* Define MACRO as a <stdint.h> constant-suffix macro for TYPE.  */
1016 static void
1017 builtin_define_constants (const char *macro, tree type)
1018 {
1019   const char *suffix;
1020   char *buf;
1021
1022   suffix = type_suffix (type);
1023
1024   if (suffix[0] == 0)
1025     {
1026       buf = (char *) alloca (strlen (macro) + 6);
1027       sprintf (buf, "%s(c)=c", macro);
1028     }
1029   else
1030     {
1031       buf = (char *) alloca (strlen (macro) + 9 + strlen (suffix) + 1);
1032       sprintf (buf, "%s(c)=c ## %s", macro, suffix);
1033     }
1034
1035   cpp_define (parse_in, buf);
1036 }
1037
1038 /* Define MAX for TYPE based on the precision of the type.  */
1039
1040 static void
1041 builtin_define_type_max (const char *macro, tree type)
1042 {
1043   builtin_define_type_minmax (NULL, macro, type);
1044 }
1045
1046 /* Define MIN_MACRO (if not NULL) and MAX_MACRO for TYPE based on the
1047    precision of the type.  */
1048
1049 static void
1050 builtin_define_type_minmax (const char *min_macro, const char *max_macro,
1051                             tree type)
1052 {
1053   static const char *const values[]
1054     = { "127", "255",
1055         "32767", "65535",
1056         "2147483647", "4294967295",
1057         "9223372036854775807", "18446744073709551615",
1058         "170141183460469231731687303715884105727",
1059         "340282366920938463463374607431768211455" };
1060
1061   const char *value, *suffix;
1062   char *buf;
1063   size_t idx;
1064
1065   /* Pre-rendering the values mean we don't have to futz with printing a
1066      multi-word decimal value.  There are also a very limited number of
1067      precisions that we support, so it's really a waste of time.  */
1068   switch (TYPE_PRECISION (type))
1069     {
1070     case 8:     idx = 0; break;
1071     case 16:    idx = 2; break;
1072     case 32:    idx = 4; break;
1073     case 64:    idx = 6; break;
1074     case 128:   idx = 8; break;
1075     default:    gcc_unreachable ();
1076     }
1077
1078   value = values[idx + TYPE_UNSIGNED (type)];
1079   suffix = type_suffix (type);
1080
1081   buf = (char *) alloca (strlen (max_macro) + 1 + strlen (value)
1082                          + strlen (suffix) + 1);
1083   sprintf (buf, "%s=%s%s", max_macro, value, suffix);
1084
1085   cpp_define (parse_in, buf);
1086
1087   if (min_macro)
1088     {
1089       if (TYPE_UNSIGNED (type))
1090         {
1091           buf = (char *) alloca (strlen (min_macro) + 2 + strlen (suffix) + 1);
1092           sprintf (buf, "%s=0%s", min_macro, suffix);
1093         }
1094       else
1095         {
1096           buf = (char *) alloca (strlen (min_macro) + 3
1097                                  + strlen (max_macro) + 6);
1098           sprintf (buf, "%s=(-%s - 1)", min_macro, max_macro);
1099         }
1100       cpp_define (parse_in, buf);
1101     }
1102 }
1103
1104 #include "gt-c-family-c-cppbuiltin.h"