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