1132718Skan/* Define builtin-in macros for the C family front ends. 2189824Sdas Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 3189824Sdas Free Software Foundation, Inc. 4132718Skan 5132718SkanThis file is part of GCC. 6132718Skan 7132718SkanGCC is free software; you can redistribute it and/or modify it under 8132718Skanthe terms of the GNU General Public License as published by the Free 9132718SkanSoftware Foundation; either version 2, or (at your option) any later 10132718Skanversion. 11132718Skan 12132718SkanGCC is distributed in the hope that it will be useful, but WITHOUT ANY 13132718SkanWARRANTY; without even the implied warranty of MERCHANTABILITY or 14132718SkanFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15132718Skanfor more details. 16132718Skan 17132718SkanYou should have received a copy of the GNU General Public License 18132718Skanalong with GCC; see the file COPYING. If not, write to the Free 19169689SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 20169689Skan02110-1301, USA. */ 21132718Skan 22132718Skan#include "config.h" 23132718Skan#include "system.h" 24132718Skan#include "coretypes.h" 25132718Skan#include "tm.h" 26132718Skan#include "tree.h" 27169689Skan#include "version.h" 28132718Skan#include "flags.h" 29132718Skan#include "real.h" 30132718Skan#include "c-common.h" 31132718Skan#include "c-pragma.h" 32132718Skan#include "output.h" 33132718Skan#include "except.h" /* For USING_SJLJ_EXCEPTIONS. */ 34132718Skan#include "toplev.h" 35132718Skan#include "tm_p.h" /* Target prototypes. */ 36169689Skan#include "target.h" 37132718Skan 38132718Skan#ifndef TARGET_OS_CPP_BUILTINS 39132718Skan# define TARGET_OS_CPP_BUILTINS() 40132718Skan#endif 41132718Skan 42132718Skan#ifndef TARGET_OBJFMT_CPP_BUILTINS 43132718Skan# define TARGET_OBJFMT_CPP_BUILTINS() 44132718Skan#endif 45132718Skan 46132718Skan#ifndef REGISTER_PREFIX 47132718Skan#define REGISTER_PREFIX "" 48132718Skan#endif 49132718Skan 50132718Skan/* Non-static as some targets don't use it. */ 51132718Skanvoid builtin_define_std (const char *) ATTRIBUTE_UNUSED; 52132718Skanstatic void builtin_define_with_value_n (const char *, const char *, 53132718Skan size_t); 54132718Skanstatic void builtin_define_with_int_value (const char *, HOST_WIDE_INT); 55132718Skanstatic void builtin_define_with_hex_fp_value (const char *, tree, 56132718Skan int, const char *, 57169689Skan const char *, 58132718Skan const char *); 59169689Skanstatic void builtin_define_stdint_macros (void); 60132718Skanstatic void builtin_define_type_max (const char *, tree, int); 61132718Skanstatic void builtin_define_type_precision (const char *, tree); 62169689Skanstatic void builtin_define_float_constants (const char *, 63169689Skan const char *, 64169689Skan const char *, 65132718Skan tree); 66132718Skanstatic void define__GNUC__ (void); 67132718Skan 68132718Skan/* Define NAME with value TYPE precision. */ 69132718Skanstatic void 70132718Skanbuiltin_define_type_precision (const char *name, tree type) 71132718Skan{ 72132718Skan builtin_define_with_int_value (name, TYPE_PRECISION (type)); 73132718Skan} 74132718Skan 75169689Skan/* Define the float.h constants for TYPE using NAME_PREFIX, FP_SUFFIX, 76169689Skan and FP_CAST. */ 77132718Skanstatic void 78169689Skanbuiltin_define_float_constants (const char *name_prefix, 79169689Skan const char *fp_suffix, 80169689Skan const char *fp_cast, 81169689Skan tree type) 82132718Skan{ 83132718Skan /* Used to convert radix-based values to base 10 values in several cases. 84132718Skan 85132718Skan In the max_exp -> max_10_exp conversion for 128-bit IEEE, we need at 86132718Skan least 6 significant digits for correct results. Using the fraction 87132718Skan formed by (log(2)*1e6)/(log(10)*1e6) overflows a 32-bit integer as an 88132718Skan intermediate; perhaps someone can find a better approximation, in the 89132718Skan mean time, I suspect using doubles won't harm the bootstrap here. */ 90132718Skan 91132718Skan const double log10_2 = .30102999566398119521; 92132718Skan double log10_b; 93132718Skan const struct real_format *fmt; 94132718Skan 95132718Skan char name[64], buf[128]; 96132718Skan int dig, min_10_exp, max_10_exp; 97132718Skan int decimal_dig; 98132718Skan 99132718Skan fmt = REAL_MODE_FORMAT (TYPE_MODE (type)); 100169689Skan gcc_assert (fmt->b != 10); 101132718Skan 102132718Skan /* The radix of the exponent representation. */ 103132718Skan if (type == float_type_node) 104132718Skan builtin_define_with_int_value ("__FLT_RADIX__", fmt->b); 105132718Skan log10_b = log10_2 * fmt->log2_b; 106132718Skan 107132718Skan /* The number of radix digits, p, in the floating-point significand. */ 108132718Skan sprintf (name, "__%s_MANT_DIG__", name_prefix); 109132718Skan builtin_define_with_int_value (name, fmt->p); 110132718Skan 111132718Skan /* The number of decimal digits, q, such that any floating-point number 112132718Skan with q decimal digits can be rounded into a floating-point number with 113132718Skan p radix b digits and back again without change to the q decimal digits, 114132718Skan 115132718Skan p log10 b if b is a power of 10 116132718Skan floor((p - 1) log10 b) otherwise 117132718Skan */ 118132718Skan dig = (fmt->p - 1) * log10_b; 119132718Skan sprintf (name, "__%s_DIG__", name_prefix); 120132718Skan builtin_define_with_int_value (name, dig); 121132718Skan 122132718Skan /* The minimum negative int x such that b**(x-1) is a normalized float. */ 123132718Skan sprintf (name, "__%s_MIN_EXP__", name_prefix); 124132718Skan sprintf (buf, "(%d)", fmt->emin); 125132718Skan builtin_define_with_value (name, buf, 0); 126132718Skan 127132718Skan /* The minimum negative int x such that 10**x is a normalized float, 128132718Skan 129132718Skan ceil (log10 (b ** (emin - 1))) 130132718Skan = ceil (log10 (b) * (emin - 1)) 131132718Skan 132132718Skan Recall that emin is negative, so the integer truncation calculates 133132718Skan the ceiling, not the floor, in this case. */ 134132718Skan min_10_exp = (fmt->emin - 1) * log10_b; 135132718Skan sprintf (name, "__%s_MIN_10_EXP__", name_prefix); 136132718Skan sprintf (buf, "(%d)", min_10_exp); 137132718Skan builtin_define_with_value (name, buf, 0); 138132718Skan 139132718Skan /* The maximum int x such that b**(x-1) is a representable float. */ 140132718Skan sprintf (name, "__%s_MAX_EXP__", name_prefix); 141132718Skan builtin_define_with_int_value (name, fmt->emax); 142132718Skan 143132718Skan /* The maximum int x such that 10**x is in the range of representable 144132718Skan finite floating-point numbers, 145132718Skan 146132718Skan floor (log10((1 - b**-p) * b**emax)) 147132718Skan = floor (log10(1 - b**-p) + log10(b**emax)) 148132718Skan = floor (log10(1 - b**-p) + log10(b)*emax) 149132718Skan 150132718Skan The safest thing to do here is to just compute this number. But since 151132718Skan we don't link cc1 with libm, we cannot. We could implement log10 here 152132718Skan a series expansion, but that seems too much effort because: 153132718Skan 154132718Skan Note that the first term, for all extant p, is a number exceedingly close 155132718Skan to zero, but slightly negative. Note that the second term is an integer 156132718Skan scaling an irrational number, and that because of the floor we are only 157132718Skan interested in its integral portion. 158132718Skan 159132718Skan In order for the first term to have any effect on the integral portion 160132718Skan of the second term, the second term has to be exceedingly close to an 161132718Skan integer itself (e.g. 123.000000000001 or something). Getting a result 162132718Skan that close to an integer requires that the irrational multiplicand have 163132718Skan a long series of zeros in its expansion, which doesn't occur in the 164132718Skan first 20 digits or so of log10(b). 165132718Skan 166132718Skan Hand-waving aside, crunching all of the sets of constants above by hand 167132718Skan does not yield a case for which the first term is significant, which 168132718Skan in the end is all that matters. */ 169132718Skan max_10_exp = fmt->emax * log10_b; 170132718Skan sprintf (name, "__%s_MAX_10_EXP__", name_prefix); 171132718Skan builtin_define_with_int_value (name, max_10_exp); 172132718Skan 173132718Skan /* The number of decimal digits, n, such that any floating-point number 174132718Skan can be rounded to n decimal digits and back again without change to 175132718Skan the value. 176132718Skan 177132718Skan p * log10(b) if b is a power of 10 178132718Skan ceil(1 + p * log10(b)) otherwise 179132718Skan 180132718Skan The only macro we care about is this number for the widest supported 181132718Skan floating type, but we want this value for rendering constants below. */ 182132718Skan { 183132718Skan double d_decimal_dig = 1 + fmt->p * log10_b; 184132718Skan decimal_dig = d_decimal_dig; 185132718Skan if (decimal_dig < d_decimal_dig) 186132718Skan decimal_dig++; 187132718Skan } 188132718Skan if (type == long_double_type_node) 189132718Skan builtin_define_with_int_value ("__DECIMAL_DIG__", decimal_dig); 190132718Skan 191132718Skan /* Since, for the supported formats, B is always a power of 2, we 192132718Skan construct the following numbers directly as a hexadecimal 193132718Skan constants. */ 194132718Skan 195132718Skan /* The maximum representable finite floating-point number, 196132718Skan (1 - b**-p) * b**emax */ 197132718Skan { 198132718Skan int i, n; 199132718Skan char *p; 200132718Skan 201132718Skan strcpy (buf, "0x0."); 202132718Skan n = fmt->p * fmt->log2_b; 203132718Skan for (i = 0, p = buf + 4; i + 3 < n; i += 4) 204132718Skan *p++ = 'f'; 205132718Skan if (i < n) 206132718Skan *p++ = "08ce"[n - i]; 207132718Skan sprintf (p, "p%d", fmt->emax * fmt->log2_b); 208132718Skan if (fmt->pnan < fmt->p) 209132718Skan { 210132718Skan /* This is an IBM extended double format made up of two IEEE 211132718Skan doubles. The value of the long double is the sum of the 212132718Skan values of the two parts. The most significant part is 213132718Skan required to be the value of the long double rounded to the 214132718Skan nearest double. Rounding means we need a slightly smaller 215132718Skan value for LDBL_MAX. */ 216132718Skan buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4]; 217132718Skan } 218132718Skan } 219132718Skan sprintf (name, "__%s_MAX__", name_prefix); 220169689Skan builtin_define_with_hex_fp_value (name, type, decimal_dig, buf, fp_suffix, fp_cast); 221132718Skan 222132718Skan /* The minimum normalized positive floating-point number, 223132718Skan b**(emin-1). */ 224132718Skan sprintf (name, "__%s_MIN__", name_prefix); 225132718Skan sprintf (buf, "0x1p%d", (fmt->emin - 1) * fmt->log2_b); 226169689Skan builtin_define_with_hex_fp_value (name, type, decimal_dig, buf, fp_suffix, fp_cast); 227132718Skan 228132718Skan /* The difference between 1 and the least value greater than 1 that is 229132718Skan representable in the given floating point type, b**(1-p). */ 230132718Skan sprintf (name, "__%s_EPSILON__", name_prefix); 231169689Skan if (fmt->pnan < fmt->p) 232169689Skan /* This is an IBM extended double format, so 1.0 + any double is 233169689Skan representable precisely. */ 234169689Skan sprintf (buf, "0x1p%d", (fmt->emin - fmt->p) * fmt->log2_b); 235169689Skan else 236169689Skan sprintf (buf, "0x1p%d", (1 - fmt->p) * fmt->log2_b); 237169689Skan builtin_define_with_hex_fp_value (name, type, decimal_dig, buf, fp_suffix, fp_cast); 238132718Skan 239132718Skan /* For C++ std::numeric_limits<T>::denorm_min. The minimum denormalized 240132718Skan positive floating-point number, b**(emin-p). Zero for formats that 241132718Skan don't support denormals. */ 242132718Skan sprintf (name, "__%s_DENORM_MIN__", name_prefix); 243132718Skan if (fmt->has_denorm) 244132718Skan { 245132718Skan sprintf (buf, "0x1p%d", (fmt->emin - fmt->p) * fmt->log2_b); 246132718Skan builtin_define_with_hex_fp_value (name, type, decimal_dig, 247169689Skan buf, fp_suffix, fp_cast); 248132718Skan } 249132718Skan else 250132718Skan { 251132718Skan sprintf (buf, "0.0%s", fp_suffix); 252132718Skan builtin_define_with_value (name, buf, 0); 253132718Skan } 254132718Skan 255169689Skan sprintf (name, "__%s_HAS_DENORM__", name_prefix); 256169689Skan builtin_define_with_value (name, fmt->has_denorm ? "1" : "0", 0); 257169689Skan 258132718Skan /* For C++ std::numeric_limits<T>::has_infinity. */ 259132718Skan sprintf (name, "__%s_HAS_INFINITY__", name_prefix); 260132718Skan builtin_define_with_int_value (name, 261132718Skan MODE_HAS_INFINITIES (TYPE_MODE (type))); 262132718Skan /* For C++ std::numeric_limits<T>::has_quiet_NaN. We do not have a 263132718Skan predicate to distinguish a target that has both quiet and 264132718Skan signalling NaNs from a target that has only quiet NaNs or only 265132718Skan signalling NaNs, so we assume that a target that has any kind of 266132718Skan NaN has quiet NaNs. */ 267132718Skan sprintf (name, "__%s_HAS_QUIET_NAN__", name_prefix); 268132718Skan builtin_define_with_int_value (name, MODE_HAS_NANS (TYPE_MODE (type))); 269132718Skan} 270132718Skan 271169689Skan/* Define __DECx__ constants for TYPE using NAME_PREFIX and SUFFIX. */ 272169689Skanstatic void 273169689Skanbuiltin_define_decimal_float_constants (const char *name_prefix, 274169689Skan const char *suffix, 275169689Skan tree type) 276169689Skan{ 277169689Skan const struct real_format *fmt; 278169689Skan char name[64], buf[128], *p; 279169689Skan int digits; 280169689Skan 281169689Skan fmt = REAL_MODE_FORMAT (TYPE_MODE (type)); 282169689Skan 283169689Skan /* The number of radix digits, p, in the significand. */ 284169689Skan sprintf (name, "__%s_MANT_DIG__", name_prefix); 285169689Skan builtin_define_with_int_value (name, fmt->p); 286169689Skan 287169689Skan /* The minimum negative int x such that b**(x-1) is a normalized float. */ 288169689Skan sprintf (name, "__%s_MIN_EXP__", name_prefix); 289169689Skan sprintf (buf, "(%d)", fmt->emin); 290169689Skan builtin_define_with_value (name, buf, 0); 291169689Skan 292169689Skan /* The maximum int x such that b**(x-1) is a representable float. */ 293169689Skan sprintf (name, "__%s_MAX_EXP__", name_prefix); 294169689Skan builtin_define_with_int_value (name, fmt->emax); 295169689Skan 296169689Skan /* Compute the minimum representable value. */ 297169689Skan sprintf (name, "__%s_MIN__", name_prefix); 298169689Skan sprintf (buf, "1E%d%s", fmt->emin, suffix); 299169689Skan builtin_define_with_value (name, buf, 0); 300169689Skan 301169689Skan /* Compute the maximum representable value. */ 302169689Skan sprintf (name, "__%s_MAX__", name_prefix); 303169689Skan p = buf; 304169689Skan for (digits = fmt->p; digits; digits--) 305169689Skan { 306169689Skan *p++ = '9'; 307169689Skan if (digits == fmt->p) 308169689Skan *p++ = '.'; 309169689Skan } 310169689Skan *p = 0; 311169689Skan /* fmt->p plus 1, to account for the decimal point. */ 312169689Skan sprintf (&buf[fmt->p + 1], "E%d%s", fmt->emax, suffix); 313169689Skan builtin_define_with_value (name, buf, 0); 314169689Skan 315169689Skan /* Compute epsilon (the difference between 1 and least value greater 316169689Skan than 1 representable). */ 317169689Skan sprintf (name, "__%s_EPSILON__", name_prefix); 318169689Skan sprintf (buf, "1E-%d%s", fmt->p - 1, suffix); 319169689Skan builtin_define_with_value (name, buf, 0); 320169689Skan 321169689Skan /* Minimum denormalized postive decimal value. */ 322169689Skan sprintf (name, "__%s_DEN__", name_prefix); 323169689Skan p = buf; 324169689Skan for (digits = fmt->p; digits > 1; digits--) 325169689Skan { 326169689Skan *p++ = '0'; 327169689Skan if (digits == fmt->p) 328169689Skan *p++ = '.'; 329169689Skan } 330169689Skan *p = 0; 331169689Skan sprintf (&buf[fmt->p], "1E%d%s", fmt->emin, suffix); 332169689Skan builtin_define_with_value (name, buf, 0); 333169689Skan} 334169689Skan 335132718Skan/* Define __GNUC__, __GNUC_MINOR__ and __GNUC_PATCHLEVEL__. */ 336132718Skanstatic void 337132718Skandefine__GNUC__ (void) 338132718Skan{ 339132718Skan /* The format of the version string, enforced below, is 340132718Skan ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */ 341132718Skan const char *q, *v = version_string; 342132718Skan 343169689Skan while (*v && !ISDIGIT (*v)) 344132718Skan v++; 345169689Skan gcc_assert (*v && (v <= version_string || v[-1] == '-')); 346132718Skan 347132718Skan q = v; 348132718Skan while (ISDIGIT (*v)) 349132718Skan v++; 350132718Skan builtin_define_with_value_n ("__GNUC__", q, v - q); 351132718Skan if (c_dialect_cxx ()) 352132718Skan builtin_define_with_value_n ("__GNUG__", q, v - q); 353132718Skan 354169689Skan gcc_assert (*v == '.' && ISDIGIT (v[1])); 355169689Skan 356132718Skan q = ++v; 357132718Skan while (ISDIGIT (*v)) 358132718Skan v++; 359132718Skan builtin_define_with_value_n ("__GNUC_MINOR__", q, v - q); 360132718Skan 361132718Skan if (*v == '.') 362132718Skan { 363169689Skan gcc_assert (ISDIGIT (v[1])); 364132718Skan q = ++v; 365132718Skan while (ISDIGIT (*v)) 366132718Skan v++; 367132718Skan builtin_define_with_value_n ("__GNUC_PATCHLEVEL__", q, v - q); 368132718Skan } 369132718Skan else 370132718Skan builtin_define_with_value_n ("__GNUC_PATCHLEVEL__", "0", 1); 371132718Skan 372169689Skan gcc_assert (!*v || *v == ' ' || *v == '-'); 373132718Skan} 374132718Skan 375169689Skan/* Define macros used by <stdint.h>. Currently only defines limits 376169689Skan for intmax_t, used by the testsuite. */ 377169689Skanstatic void 378169689Skanbuiltin_define_stdint_macros (void) 379169689Skan{ 380169689Skan int intmax_long; 381169689Skan if (intmax_type_node == long_long_integer_type_node) 382169689Skan intmax_long = 2; 383169689Skan else if (intmax_type_node == long_integer_type_node) 384169689Skan intmax_long = 1; 385169689Skan else if (intmax_type_node == integer_type_node) 386169689Skan intmax_long = 0; 387169689Skan else 388169689Skan gcc_unreachable (); 389169689Skan builtin_define_type_max ("__INTMAX_MAX__", intmax_type_node, intmax_long); 390169689Skan} 391169689Skan 392132718Skan/* Hook that registers front end and target-specific built-ins. */ 393132718Skanvoid 394132718Skanc_cpp_builtins (cpp_reader *pfile) 395132718Skan{ 396132718Skan /* -undef turns off target-specific built-ins. */ 397132718Skan if (flag_undef) 398132718Skan return; 399132718Skan 400132718Skan define__GNUC__ (); 401132718Skan 402132718Skan /* For stddef.h. They require macros defined in c-common.c. */ 403132718Skan c_stddef_cpp_builtins (); 404132718Skan 405132718Skan if (c_dialect_cxx ()) 406132718Skan { 407169689Skan if (flag_weak && SUPPORTS_ONE_ONLY) 408132718Skan cpp_define (pfile, "__GXX_WEAK__=1"); 409132718Skan else 410132718Skan cpp_define (pfile, "__GXX_WEAK__=0"); 411132718Skan if (warn_deprecated) 412132718Skan cpp_define (pfile, "__DEPRECATED"); 413132718Skan } 414132718Skan /* Note that we define this for C as well, so that we know if 415132718Skan __attribute__((cleanup)) will interface with EH. */ 416132718Skan if (flag_exceptions) 417132718Skan cpp_define (pfile, "__EXCEPTIONS"); 418132718Skan 419132718Skan /* Represents the C++ ABI version, always defined so it can be used while 420132718Skan preprocessing C and assembler. */ 421132718Skan if (flag_abi_version == 0) 422132718Skan /* Use a very large value so that: 423132718Skan 424169689Skan #if __GXX_ABI_VERSION >= <value for version X> 425132718Skan 426132718Skan will work whether the user explicitly says "-fabi-version=x" or 427132718Skan "-fabi-version=0". Do not use INT_MAX because that will be 428132718Skan different from system to system. */ 429132718Skan builtin_define_with_int_value ("__GXX_ABI_VERSION", 999999); 430132718Skan else if (flag_abi_version == 1) 431169689Skan /* Due to a historical accident, this version had the value 432132718Skan "102". */ 433132718Skan builtin_define_with_int_value ("__GXX_ABI_VERSION", 102); 434132718Skan else 435132718Skan /* Newer versions have values 1002, 1003, .... */ 436169689Skan builtin_define_with_int_value ("__GXX_ABI_VERSION", 437132718Skan 1000 + flag_abi_version); 438132718Skan 439132718Skan /* libgcc needs to know this. */ 440132718Skan if (USING_SJLJ_EXCEPTIONS) 441132718Skan cpp_define (pfile, "__USING_SJLJ_EXCEPTIONS__"); 442132718Skan 443132718Skan /* limits.h needs to know these. */ 444132718Skan builtin_define_type_max ("__SCHAR_MAX__", signed_char_type_node, 0); 445132718Skan builtin_define_type_max ("__SHRT_MAX__", short_integer_type_node, 0); 446132718Skan builtin_define_type_max ("__INT_MAX__", integer_type_node, 0); 447132718Skan builtin_define_type_max ("__LONG_MAX__", long_integer_type_node, 1); 448132718Skan builtin_define_type_max ("__LONG_LONG_MAX__", long_long_integer_type_node, 2); 449132718Skan builtin_define_type_max ("__WCHAR_MAX__", wchar_type_node, 0); 450132718Skan 451132718Skan builtin_define_type_precision ("__CHAR_BIT__", char_type_node); 452132718Skan 453169689Skan /* stdint.h (eventually) and the testsuite need to know these. */ 454169689Skan builtin_define_stdint_macros (); 455169689Skan 456132718Skan /* float.h needs to know these. */ 457132718Skan 458132718Skan builtin_define_with_int_value ("__FLT_EVAL_METHOD__", 459132718Skan TARGET_FLT_EVAL_METHOD); 460132718Skan 461169689Skan /* And decfloat.h needs this. */ 462169689Skan builtin_define_with_int_value ("__DEC_EVAL_METHOD__", 463169689Skan TARGET_DEC_EVAL_METHOD); 464132718Skan 465169689Skan builtin_define_float_constants ("FLT", "F", "%s", float_type_node); 466169689Skan /* Cast the double precision constants when single precision constants are 467169689Skan specified. The correct result is computed by the compiler when using 468169689Skan macros that include a cast. This has the side-effect of making the value 469169689Skan unusable in const expressions. */ 470169689Skan if (flag_single_precision_constant) 471169689Skan builtin_define_float_constants ("DBL", "L", "((double)%s)", double_type_node); 472169689Skan else 473169689Skan builtin_define_float_constants ("DBL", "", "%s", double_type_node); 474169689Skan builtin_define_float_constants ("LDBL", "L", "%s", long_double_type_node); 475169689Skan 476169689Skan /* For decfloat.h. */ 477169689Skan builtin_define_decimal_float_constants ("DEC32", "DF", dfloat32_type_node); 478169689Skan builtin_define_decimal_float_constants ("DEC64", "DD", dfloat64_type_node); 479169689Skan builtin_define_decimal_float_constants ("DEC128", "DL", dfloat128_type_node); 480169689Skan 481132718Skan /* For use in assembly language. */ 482132718Skan builtin_define_with_value ("__REGISTER_PREFIX__", REGISTER_PREFIX, 0); 483132718Skan builtin_define_with_value ("__USER_LABEL_PREFIX__", user_label_prefix, 0); 484132718Skan 485132718Skan /* Misc. */ 486132718Skan builtin_define_with_value ("__VERSION__", version_string, 1); 487132718Skan 488189824Sdas if (flag_gnu89_inline) 489189824Sdas cpp_define (pfile, "__GNUC_GNU_INLINE__"); 490189824Sdas else 491189824Sdas cpp_define (pfile, "__GNUC_STDC_INLINE__"); 492169689Skan 493132718Skan /* Definitions for LP64 model. */ 494132718Skan if (TYPE_PRECISION (long_integer_type_node) == 64 495132718Skan && POINTER_SIZE == 64 496132718Skan && TYPE_PRECISION (integer_type_node) == 32) 497132718Skan { 498132718Skan cpp_define (pfile, "_LP64"); 499132718Skan cpp_define (pfile, "__LP64__"); 500132718Skan } 501132718Skan 502132718Skan /* Other target-independent built-ins determined by command-line 503132718Skan options. */ 504261188Spfg /* APPLE LOCAL begin blocks */ 505261188Spfg /* APPLE LOCAL radar 5868913 */ 506261188Spfg if (flag_blocks) 507264214Spfg { 508264214Spfg cpp_define (pfile, "__block=__attribute__((__blocks__(byref)))"); 509264214Spfg cpp_define (pfile, "__BLOCKS__=1"); 510264214Spfg } 511261188Spfg /* APPLE LOCAL end blocks */ 512132718Skan if (optimize_size) 513132718Skan cpp_define (pfile, "__OPTIMIZE_SIZE__"); 514132718Skan if (optimize) 515132718Skan cpp_define (pfile, "__OPTIMIZE__"); 516132718Skan 517132718Skan if (fast_math_flags_set_p ()) 518132718Skan cpp_define (pfile, "__FAST_MATH__"); 519132718Skan if (flag_really_no_inline) 520132718Skan cpp_define (pfile, "__NO_INLINE__"); 521132718Skan if (flag_signaling_nans) 522132718Skan cpp_define (pfile, "__SUPPORT_SNAN__"); 523132718Skan if (flag_finite_math_only) 524132718Skan cpp_define (pfile, "__FINITE_MATH_ONLY__=1"); 525132718Skan else 526132718Skan cpp_define (pfile, "__FINITE_MATH_ONLY__=0"); 527169689Skan if (flag_pic) 528169689Skan { 529169689Skan builtin_define_with_int_value ("__pic__", flag_pic); 530169689Skan builtin_define_with_int_value ("__PIC__", flag_pic); 531169689Skan } 532132718Skan 533132718Skan if (flag_iso) 534132718Skan cpp_define (pfile, "__STRICT_ANSI__"); 535132718Skan 536132718Skan if (!flag_signed_char) 537132718Skan cpp_define (pfile, "__CHAR_UNSIGNED__"); 538132718Skan 539169689Skan if (c_dialect_cxx () && TYPE_UNSIGNED (wchar_type_node)) 540132718Skan cpp_define (pfile, "__WCHAR_UNSIGNED__"); 541132718Skan 542132718Skan /* Make the choice of ObjC runtime visible to source code. */ 543132718Skan if (c_dialect_objc () && flag_next_runtime) 544132718Skan cpp_define (pfile, "__NEXT_RUNTIME__"); 545132718Skan 546169689Skan /* Show the availability of some target pragmas. */ 547169689Skan if (flag_mudflap || targetm.handle_pragma_redefine_extname) 548169689Skan cpp_define (pfile, "__PRAGMA_REDEFINE_EXTNAME"); 549169689Skan 550169689Skan if (targetm.handle_pragma_extern_prefix) 551169689Skan cpp_define (pfile, "__PRAGMA_EXTERN_PREFIX"); 552169689Skan 553169689Skan /* Make the choice of the stack protector runtime visible to source code. 554169689Skan The macro names and values here were chosen for compatibility with an 555169689Skan earlier implementation, i.e. ProPolice. */ 556169689Skan if (flag_stack_protect == 2) 557169689Skan cpp_define (pfile, "__SSP_ALL__=2"); 558169689Skan else if (flag_stack_protect == 1) 559169689Skan cpp_define (pfile, "__SSP__=1"); 560169689Skan 561169689Skan if (flag_openmp) 562169689Skan cpp_define (pfile, "_OPENMP=200505"); 563169689Skan 564132718Skan /* A straightforward target hook doesn't work, because of problems 565132718Skan linking that hook's body when part of non-C front ends. */ 566132718Skan# define preprocessing_asm_p() (cpp_get_options (pfile)->lang == CLK_ASM) 567132718Skan# define preprocessing_trad_p() (cpp_get_options (pfile)->traditional) 568132718Skan# define builtin_define(TXT) cpp_define (pfile, TXT) 569132718Skan# define builtin_assert(TXT) cpp_assert (pfile, TXT) 570132718Skan TARGET_CPU_CPP_BUILTINS (); 571132718Skan TARGET_OS_CPP_BUILTINS (); 572132718Skan TARGET_OBJFMT_CPP_BUILTINS (); 573169689Skan 574169689Skan /* Support the __declspec keyword by turning them into attributes. 575169689Skan Note that the current way we do this may result in a collision 576169689Skan with predefined attributes later on. This can be solved by using 577169689Skan one attribute, say __declspec__, and passing args to it. The 578169689Skan problem with that approach is that args are not accumulated: each 579169689Skan new appearance would clobber any existing args. */ 580169689Skan if (TARGET_DECLSPEC) 581169689Skan builtin_define ("__declspec(x)=__attribute__((x))"); 582132718Skan} 583132718Skan 584132718Skan/* Pass an object-like macro. If it doesn't lie in the user's 585132718Skan namespace, defines it unconditionally. Otherwise define a version 586132718Skan with two leading underscores, and another version with two leading 587132718Skan and trailing underscores, and define the original only if an ISO 588132718Skan standard was not nominated. 589132718Skan 590132718Skan e.g. passing "unix" defines "__unix", "__unix__" and possibly 591132718Skan "unix". Passing "_mips" defines "__mips", "__mips__" and possibly 592132718Skan "_mips". */ 593132718Skanvoid 594132718Skanbuiltin_define_std (const char *macro) 595132718Skan{ 596132718Skan size_t len = strlen (macro); 597169689Skan char *buff = (char *) alloca (len + 5); 598132718Skan char *p = buff + 2; 599132718Skan char *q = p + len; 600132718Skan 601132718Skan /* prepend __ (or maybe just _) if in user's namespace. */ 602132718Skan memcpy (p, macro, len + 1); 603132718Skan if (!( *p == '_' && (p[1] == '_' || ISUPPER (p[1])))) 604132718Skan { 605132718Skan if (*p != '_') 606132718Skan *--p = '_'; 607132718Skan if (p[1] != '_') 608132718Skan *--p = '_'; 609132718Skan } 610132718Skan cpp_define (parse_in, p); 611132718Skan 612132718Skan /* If it was in user's namespace... */ 613132718Skan if (p != buff + 2) 614132718Skan { 615132718Skan /* Define the macro with leading and following __. */ 616132718Skan if (q[-1] != '_') 617132718Skan *q++ = '_'; 618132718Skan if (q[-2] != '_') 619132718Skan *q++ = '_'; 620132718Skan *q = '\0'; 621132718Skan cpp_define (parse_in, p); 622132718Skan 623132718Skan /* Finally, define the original macro if permitted. */ 624132718Skan if (!flag_iso) 625132718Skan cpp_define (parse_in, macro); 626132718Skan } 627132718Skan} 628132718Skan 629132718Skan/* Pass an object-like macro and a value to define it to. The third 630132718Skan parameter says whether or not to turn the value into a string 631132718Skan constant. */ 632132718Skanvoid 633132718Skanbuiltin_define_with_value (const char *macro, const char *expansion, int is_str) 634132718Skan{ 635132718Skan char *buf; 636132718Skan size_t mlen = strlen (macro); 637132718Skan size_t elen = strlen (expansion); 638132718Skan size_t extra = 2; /* space for an = and a NUL */ 639132718Skan 640132718Skan if (is_str) 641132718Skan extra += 2; /* space for two quote marks */ 642132718Skan 643169689Skan buf = (char *) alloca (mlen + elen + extra); 644132718Skan if (is_str) 645132718Skan sprintf (buf, "%s=\"%s\"", macro, expansion); 646132718Skan else 647132718Skan sprintf (buf, "%s=%s", macro, expansion); 648132718Skan 649132718Skan cpp_define (parse_in, buf); 650132718Skan} 651132718Skan 652132718Skan/* Pass an object-like macro and a value to define it to. The third 653132718Skan parameter is the length of the expansion. */ 654132718Skanstatic void 655132718Skanbuiltin_define_with_value_n (const char *macro, const char *expansion, size_t elen) 656132718Skan{ 657132718Skan char *buf; 658132718Skan size_t mlen = strlen (macro); 659132718Skan 660132718Skan /* Space for an = and a NUL. */ 661169689Skan buf = (char *) alloca (mlen + elen + 2); 662132718Skan memcpy (buf, macro, mlen); 663132718Skan buf[mlen] = '='; 664132718Skan memcpy (buf + mlen + 1, expansion, elen); 665132718Skan buf[mlen + elen + 1] = '\0'; 666132718Skan 667132718Skan cpp_define (parse_in, buf); 668132718Skan} 669132718Skan 670132718Skan/* Pass an object-like macro and an integer value to define it to. */ 671132718Skanstatic void 672132718Skanbuiltin_define_with_int_value (const char *macro, HOST_WIDE_INT value) 673132718Skan{ 674132718Skan char *buf; 675132718Skan size_t mlen = strlen (macro); 676132718Skan size_t vlen = 18; 677132718Skan size_t extra = 2; /* space for = and NUL. */ 678132718Skan 679169689Skan buf = (char *) alloca (mlen + vlen + extra); 680132718Skan memcpy (buf, macro, mlen); 681132718Skan buf[mlen] = '='; 682132718Skan sprintf (buf + mlen + 1, HOST_WIDE_INT_PRINT_DEC, value); 683132718Skan 684132718Skan cpp_define (parse_in, buf); 685132718Skan} 686132718Skan 687132718Skan/* Pass an object-like macro a hexadecimal floating-point value. */ 688132718Skanstatic void 689132718Skanbuiltin_define_with_hex_fp_value (const char *macro, 690132718Skan tree type ATTRIBUTE_UNUSED, int digits, 691169689Skan const char *hex_str, 692169689Skan const char *fp_suffix, 693169689Skan const char *fp_cast) 694132718Skan{ 695132718Skan REAL_VALUE_TYPE real; 696169689Skan char dec_str[64], buf1[256], buf2[256]; 697132718Skan 698132718Skan /* Hex values are really cool and convenient, except that they're 699132718Skan not supported in strict ISO C90 mode. First, the "p-" sequence 700132718Skan is not valid as part of a preprocessor number. Second, we get a 701132718Skan pedwarn from the preprocessor, which has no context, so we can't 702132718Skan suppress the warning with __extension__. 703132718Skan 704132718Skan So instead what we do is construct the number in hex (because 705132718Skan it's easy to get the exact correct value), parse it as a real, 706132718Skan then print it back out as decimal. */ 707132718Skan 708132718Skan real_from_string (&real, hex_str); 709132718Skan real_to_decimal (dec_str, &real, sizeof (dec_str), digits, 0); 710132718Skan 711169689Skan /* Assemble the macro in the following fashion 712169689Skan macro = fp_cast [dec_str fp_suffix] */ 713169689Skan sprintf (buf1, "%s%s", dec_str, fp_suffix); 714169689Skan sprintf (buf2, fp_cast, buf1); 715169689Skan sprintf (buf1, "%s=%s", macro, buf2); 716169689Skan 717169689Skan cpp_define (parse_in, buf1); 718132718Skan} 719132718Skan 720132718Skan/* Define MAX for TYPE based on the precision of the type. IS_LONG is 721132718Skan 1 for type "long" and 2 for "long long". We have to handle 722132718Skan unsigned types, since wchar_t might be unsigned. */ 723132718Skan 724132718Skanstatic void 725132718Skanbuiltin_define_type_max (const char *macro, tree type, int is_long) 726132718Skan{ 727132718Skan static const char *const values[] 728132718Skan = { "127", "255", 729132718Skan "32767", "65535", 730132718Skan "2147483647", "4294967295", 731132718Skan "9223372036854775807", "18446744073709551615", 732132718Skan "170141183460469231731687303715884105727", 733132718Skan "340282366920938463463374607431768211455" }; 734132718Skan static const char *const suffixes[] = { "", "U", "L", "UL", "LL", "ULL" }; 735132718Skan 736132718Skan const char *value, *suffix; 737132718Skan char *buf; 738132718Skan size_t idx; 739132718Skan 740132718Skan /* Pre-rendering the values mean we don't have to futz with printing a 741132718Skan multi-word decimal value. There are also a very limited number of 742132718Skan precisions that we support, so it's really a waste of time. */ 743132718Skan switch (TYPE_PRECISION (type)) 744132718Skan { 745132718Skan case 8: idx = 0; break; 746132718Skan case 16: idx = 2; break; 747132718Skan case 32: idx = 4; break; 748132718Skan case 64: idx = 6; break; 749132718Skan case 128: idx = 8; break; 750169689Skan default: gcc_unreachable (); 751132718Skan } 752132718Skan 753169689Skan value = values[idx + TYPE_UNSIGNED (type)]; 754169689Skan suffix = suffixes[is_long * 2 + TYPE_UNSIGNED (type)]; 755132718Skan 756169689Skan buf = (char *) alloca (strlen (macro) + 1 + strlen (value) 757169689Skan + strlen (suffix) + 1); 758132718Skan sprintf (buf, "%s=%s%s", macro, value, suffix); 759132718Skan 760132718Skan cpp_define (parse_in, buf); 761132718Skan} 762