00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "config.h"
00022 #include "system.h"
00023 #include "coretypes.h"
00024 #include "tm.h"
00025 #include "tree.h"
00026 #include "version.h"
00027 #include "flags.h"
00028 #include "real.h"
00029 #include "c-common.h"
00030 #include "c-pragma.h"
00031 #include "output.h"
00032 #include "except.h"
00033 #include "toplev.h"
00034 #include "tm_p.h"
00035 #include "target.h"
00036
00037 #ifndef TARGET_OS_CPP_BUILTINS
00038 # define TARGET_OS_CPP_BUILTINS()
00039 #endif
00040
00041 #ifndef TARGET_OBJFMT_CPP_BUILTINS
00042 # define TARGET_OBJFMT_CPP_BUILTINS()
00043 #endif
00044
00045 #ifndef REGISTER_PREFIX
00046 #define REGISTER_PREFIX ""
00047 #endif
00048
00049
00050 void builtin_define_std (const char *) ATTRIBUTE_UNUSED;
00051 static void builtin_define_with_value_n (const char *, const char *,
00052 size_t);
00053 static void builtin_define_with_int_value (const char *, HOST_WIDE_INT);
00054 static void builtin_define_with_hex_fp_value (const char *, tree,
00055 int, const char *,
00056 const char *);
00057 static void builtin_define_stdint_macros (void);
00058 static void builtin_define_type_max (const char *, tree, int);
00059 static void builtin_define_type_precision (const char *, tree);
00060 static void builtin_define_float_constants (const char *, const char *,
00061 tree);
00062 static void define__GNUC__ (void);
00063
00064
00065 static void
00066 builtin_define_type_precision (const char *name, tree type)
00067 {
00068 builtin_define_with_int_value (name, TYPE_PRECISION (type));
00069 }
00070
00071
00072 static void
00073 builtin_define_float_constants (const char *name_prefix, const char *fp_suffix, tree type)
00074 {
00075
00076
00077
00078
00079
00080
00081
00082
00083 const double log10_2 = .30102999566398119521;
00084 double log10_b;
00085 const struct real_format *fmt;
00086
00087 char name[64], buf[128];
00088 int dig, min_10_exp, max_10_exp;
00089 int decimal_dig;
00090
00091 fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
00092
00093
00094 if (type == float_type_node)
00095 builtin_define_with_int_value ("__FLT_RADIX__", fmt->b);
00096 log10_b = log10_2 * fmt->log2_b;
00097
00098
00099 sprintf (name, "__%s_MANT_DIG__", name_prefix);
00100 builtin_define_with_int_value (name, fmt->p);
00101
00102
00103
00104
00105
00106
00107
00108
00109 dig = (fmt->p - 1) * log10_b;
00110 sprintf (name, "__%s_DIG__", name_prefix);
00111 builtin_define_with_int_value (name, dig);
00112
00113
00114 sprintf (name, "__%s_MIN_EXP__", name_prefix);
00115 sprintf (buf, "(%d)", fmt->emin);
00116 builtin_define_with_value (name, buf, 0);
00117
00118
00119
00120
00121
00122
00123
00124
00125 min_10_exp = (fmt->emin - 1) * log10_b;
00126 sprintf (name, "__%s_MIN_10_EXP__", name_prefix);
00127 sprintf (buf, "(%d)", min_10_exp);
00128 builtin_define_with_value (name, buf, 0);
00129
00130
00131 sprintf (name, "__%s_MAX_EXP__", name_prefix);
00132 builtin_define_with_int_value (name, fmt->emax);
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 max_10_exp = fmt->emax * log10_b;
00161 sprintf (name, "__%s_MAX_10_EXP__", name_prefix);
00162 builtin_define_with_int_value (name, max_10_exp);
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 {
00174 double d_decimal_dig = 1 + fmt->p * log10_b;
00175 decimal_dig = d_decimal_dig;
00176 if (decimal_dig < d_decimal_dig)
00177 decimal_dig++;
00178 }
00179 if (type == long_double_type_node)
00180 builtin_define_with_int_value ("__DECIMAL_DIG__", decimal_dig);
00181
00182
00183
00184
00185
00186
00187
00188 {
00189 int i, n;
00190 char *p;
00191
00192 strcpy (buf, "0x0.");
00193 n = fmt->p * fmt->log2_b;
00194 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
00195 *p++ = 'f';
00196 if (i < n)
00197 *p++ = "08ce"[n - i];
00198 sprintf (p, "p%d", fmt->emax * fmt->log2_b);
00199 if (fmt->pnan < fmt->p)
00200 {
00201
00202
00203
00204
00205
00206
00207 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
00208 }
00209 }
00210 sprintf (name, "__%s_MAX__", name_prefix);
00211 builtin_define_with_hex_fp_value (name, type, decimal_dig, buf, fp_suffix);
00212
00213
00214
00215 sprintf (name, "__%s_MIN__", name_prefix);
00216 sprintf (buf, "0x1p%d", (fmt->emin - 1) * fmt->log2_b);
00217 builtin_define_with_hex_fp_value (name, type, decimal_dig, buf, fp_suffix);
00218
00219
00220
00221 sprintf (name, "__%s_EPSILON__", name_prefix);
00222 if (fmt->pnan < fmt->p)
00223
00224
00225 sprintf (buf, "0x1p%d", (fmt->emin - fmt->p) * fmt->log2_b);
00226 else
00227 sprintf (buf, "0x1p%d", (1 - fmt->p) * fmt->log2_b);
00228 builtin_define_with_hex_fp_value (name, type, decimal_dig, buf, fp_suffix);
00229
00230
00231
00232
00233 sprintf (name, "__%s_DENORM_MIN__", name_prefix);
00234 if (fmt->has_denorm)
00235 {
00236 sprintf (buf, "0x1p%d", (fmt->emin - fmt->p) * fmt->log2_b);
00237 builtin_define_with_hex_fp_value (name, type, decimal_dig,
00238 buf, fp_suffix);
00239 }
00240 else
00241 {
00242 sprintf (buf, "0.0%s", fp_suffix);
00243 builtin_define_with_value (name, buf, 0);
00244 }
00245
00246
00247 sprintf (name, "__%s_HAS_INFINITY__", name_prefix);
00248 builtin_define_with_int_value (name,
00249 MODE_HAS_INFINITIES (TYPE_MODE (type)));
00250
00251
00252
00253
00254
00255 sprintf (name, "__%s_HAS_QUIET_NAN__", name_prefix);
00256 builtin_define_with_int_value (name, MODE_HAS_NANS (TYPE_MODE (type)));
00257 }
00258
00259
00260 static void
00261 define__GNUC__ (void)
00262 {
00263
00264
00265 const char *q, *v = version_string;
00266
00267 while (*v && !ISDIGIT (*v))
00268 v++;
00269 gcc_assert (*v && (v <= version_string || v[-1] == '-'));
00270
00271 q = v;
00272 while (ISDIGIT (*v))
00273 v++;
00274 builtin_define_with_value_n ("__GNUC__", q, v - q);
00275 if (c_dialect_cxx ())
00276 builtin_define_with_value_n ("__GNUG__", q, v - q);
00277
00278 gcc_assert (*v == '.' && ISDIGIT (v[1]));
00279
00280 q = ++v;
00281 while (ISDIGIT (*v))
00282 v++;
00283 builtin_define_with_value_n ("__GNUC_MINOR__", q, v - q);
00284
00285 if (*v == '.')
00286 {
00287 gcc_assert (ISDIGIT (v[1]));
00288 q = ++v;
00289 while (ISDIGIT (*v))
00290 v++;
00291 builtin_define_with_value_n ("__GNUC_PATCHLEVEL__", q, v - q);
00292 }
00293 else
00294 builtin_define_with_value_n ("__GNUC_PATCHLEVEL__", "0", 1);
00295
00296 gcc_assert (!*v || *v == ' ' || *v == '-');
00297 }
00298
00299
00300
00301 static void
00302 builtin_define_stdint_macros (void)
00303 {
00304 int intmax_long;
00305 if (intmax_type_node == long_long_integer_type_node)
00306 intmax_long = 2;
00307 else if (intmax_type_node == long_integer_type_node)
00308 intmax_long = 1;
00309 else if (intmax_type_node == integer_type_node)
00310 intmax_long = 0;
00311 else
00312 gcc_unreachable ();
00313 builtin_define_type_max ("__INTMAX_MAX__", intmax_type_node, intmax_long);
00314 }
00315
00316
00317 void
00318 c_cpp_builtins (cpp_reader *pfile)
00319 {
00320
00321 if (flag_undef)
00322 return;
00323
00324 define__GNUC__ ();
00325
00326
00327 c_stddef_cpp_builtins ();
00328
00329 if (c_dialect_cxx ())
00330 {
00331 if (SUPPORTS_ONE_ONLY)
00332 cpp_define (pfile, "__GXX_WEAK__=1");
00333 else
00334 cpp_define (pfile, "__GXX_WEAK__=0");
00335 if (warn_deprecated)
00336 cpp_define (pfile, "__DEPRECATED");
00337 }
00338
00339
00340 if (flag_exceptions)
00341 cpp_define (pfile, "__EXCEPTIONS");
00342
00343
00344
00345 if (flag_abi_version == 0)
00346
00347
00348
00349
00350
00351
00352
00353 builtin_define_with_int_value ("__GXX_ABI_VERSION", 999999);
00354 else if (flag_abi_version == 1)
00355
00356
00357 builtin_define_with_int_value ("__GXX_ABI_VERSION", 102);
00358 else
00359
00360 builtin_define_with_int_value ("__GXX_ABI_VERSION",
00361 1000 + flag_abi_version);
00362
00363
00364 if (USING_SJLJ_EXCEPTIONS)
00365 cpp_define (pfile, "__USING_SJLJ_EXCEPTIONS__");
00366
00367
00368 builtin_define_type_max ("__SCHAR_MAX__", signed_char_type_node, 0);
00369 builtin_define_type_max ("__SHRT_MAX__", short_integer_type_node, 0);
00370 builtin_define_type_max ("__INT_MAX__", integer_type_node, 0);
00371 builtin_define_type_max ("__LONG_MAX__", long_integer_type_node, 1);
00372 builtin_define_type_max ("__LONG_LONG_MAX__", long_long_integer_type_node, 2);
00373 builtin_define_type_max ("__WCHAR_MAX__", wchar_type_node, 0);
00374
00375 builtin_define_type_precision ("__CHAR_BIT__", char_type_node);
00376
00377
00378 builtin_define_stdint_macros ();
00379
00380
00381
00382 builtin_define_with_int_value ("__FLT_EVAL_METHOD__",
00383 TARGET_FLT_EVAL_METHOD);
00384
00385 builtin_define_float_constants ("FLT", "F", float_type_node);
00386 builtin_define_float_constants ("DBL", "", double_type_node);
00387 builtin_define_float_constants ("LDBL", "L", long_double_type_node);
00388
00389
00390 builtin_define_with_value ("__REGISTER_PREFIX__", REGISTER_PREFIX, 0);
00391 builtin_define_with_value ("__USER_LABEL_PREFIX__", user_label_prefix, 0);
00392
00393
00394 builtin_define_with_value ("__VERSION__", version_string, 1);
00395
00396
00397 if (TYPE_PRECISION (long_integer_type_node) == 64
00398 && POINTER_SIZE == 64
00399 && TYPE_PRECISION (integer_type_node) == 32)
00400 {
00401 cpp_define (pfile, "_LP64");
00402 cpp_define (pfile, "__LP64__");
00403 }
00404
00405
00406
00407 if (optimize_size)
00408 cpp_define (pfile, "__OPTIMIZE_SIZE__");
00409 if (optimize)
00410 cpp_define (pfile, "__OPTIMIZE__");
00411
00412 if (fast_math_flags_set_p ())
00413 cpp_define (pfile, "__FAST_MATH__");
00414 if (flag_really_no_inline)
00415 cpp_define (pfile, "__NO_INLINE__");
00416 if (flag_signaling_nans)
00417 cpp_define (pfile, "__SUPPORT_SNAN__");
00418 if (flag_finite_math_only)
00419 cpp_define (pfile, "__FINITE_MATH_ONLY__=1");
00420 else
00421 cpp_define (pfile, "__FINITE_MATH_ONLY__=0");
00422
00423 if (flag_iso)
00424 cpp_define (pfile, "__STRICT_ANSI__");
00425
00426 if (!flag_signed_char)
00427 cpp_define (pfile, "__CHAR_UNSIGNED__");
00428
00429 if (c_dialect_cxx () && TYPE_UNSIGNED (wchar_type_node))
00430 cpp_define (pfile, "__WCHAR_UNSIGNED__");
00431
00432
00433 if (c_dialect_objc () && flag_next_runtime)
00434 cpp_define (pfile, "__NEXT_RUNTIME__");
00435
00436
00437 if (flag_mudflap || targetm.handle_pragma_redefine_extname)
00438 cpp_define (pfile, "__PRAGMA_REDEFINE_EXTNAME");
00439
00440 if (targetm.handle_pragma_extern_prefix)
00441 cpp_define (pfile, "__PRAGMA_EXTERN_PREFIX");
00442
00443
00444
00445 # define preprocessing_asm_p() (cpp_get_options (pfile)->lang == CLK_ASM)
00446 # define preprocessing_trad_p() (cpp_get_options (pfile)->traditional)
00447 # define builtin_define(TXT) cpp_define (pfile, TXT)
00448 # define builtin_assert(TXT) cpp_assert (pfile, TXT)
00449 TARGET_CPU_CPP_BUILTINS ();
00450 TARGET_OS_CPP_BUILTINS ();
00451 TARGET_OBJFMT_CPP_BUILTINS ();
00452
00453
00454
00455
00456
00457
00458
00459 if (TARGET_DECLSPEC)
00460 builtin_define ("__declspec(x)=__attribute__((x))");
00461 }
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472 void
00473 builtin_define_std (const char *macro)
00474 {
00475 size_t len = strlen (macro);
00476 char *buff = alloca (len + 5);
00477 char *p = buff + 2;
00478 char *q = p + len;
00479
00480
00481 memcpy (p, macro, len + 1);
00482 if (!( *p == '_' && (p[1] == '_' || ISUPPER (p[1]))))
00483 {
00484 if (*p != '_')
00485 *--p = '_';
00486 if (p[1] != '_')
00487 *--p = '_';
00488 }
00489 cpp_define (parse_in, p);
00490
00491
00492 if (p != buff + 2)
00493 {
00494
00495 if (q[-1] != '_')
00496 *q++ = '_';
00497 if (q[-2] != '_')
00498 *q++ = '_';
00499 *q = '\0';
00500 cpp_define (parse_in, p);
00501
00502
00503 if (!flag_iso)
00504 cpp_define (parse_in, macro);
00505 }
00506 }
00507
00508
00509
00510
00511 void
00512 builtin_define_with_value (const char *macro, const char *expansion, int is_str)
00513 {
00514 char *buf;
00515 size_t mlen = strlen (macro);
00516 size_t elen = strlen (expansion);
00517 size_t extra = 2;
00518
00519 if (is_str)
00520 extra += 2;
00521
00522 buf = alloca (mlen + elen + extra);
00523 if (is_str)
00524 sprintf (buf, "%s=\"%s\"", macro, expansion);
00525 else
00526 sprintf (buf, "%s=%s", macro, expansion);
00527
00528 cpp_define (parse_in, buf);
00529 }
00530
00531
00532
00533 static void
00534 builtin_define_with_value_n (const char *macro, const char *expansion, size_t elen)
00535 {
00536 char *buf;
00537 size_t mlen = strlen (macro);
00538
00539
00540 buf = alloca (mlen + elen + 2);
00541 memcpy (buf, macro, mlen);
00542 buf[mlen] = '=';
00543 memcpy (buf + mlen + 1, expansion, elen);
00544 buf[mlen + elen + 1] = '\0';
00545
00546 cpp_define (parse_in, buf);
00547 }
00548
00549
00550 static void
00551 builtin_define_with_int_value (const char *macro, HOST_WIDE_INT value)
00552 {
00553 char *buf;
00554 size_t mlen = strlen (macro);
00555 size_t vlen = 18;
00556 size_t extra = 2;
00557
00558 buf = alloca (mlen + vlen + extra);
00559 memcpy (buf, macro, mlen);
00560 buf[mlen] = '=';
00561 sprintf (buf + mlen + 1, HOST_WIDE_INT_PRINT_DEC, value);
00562
00563 cpp_define (parse_in, buf);
00564 }
00565
00566
00567 static void
00568 builtin_define_with_hex_fp_value (const char *macro,
00569 tree type ATTRIBUTE_UNUSED, int digits,
00570 const char *hex_str, const char *fp_suffix)
00571 {
00572 REAL_VALUE_TYPE real;
00573 char dec_str[64], buf[256];
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585 real_from_string (&real, hex_str);
00586 real_to_decimal (dec_str, &real, sizeof (dec_str), digits, 0);
00587
00588 sprintf (buf, "%s=%s%s", macro, dec_str, fp_suffix);
00589 cpp_define (parse_in, buf);
00590 }
00591
00592
00593
00594
00595
00596 static void
00597 builtin_define_type_max (const char *macro, tree type, int is_long)
00598 {
00599 static const char *const values[]
00600 = { "127", "255",
00601 "32767", "65535",
00602 "2147483647", "4294967295",
00603 "9223372036854775807", "18446744073709551615",
00604 "170141183460469231731687303715884105727",
00605 "340282366920938463463374607431768211455" };
00606 static const char *const suffixes[] = { "", "U", "L", "UL", "LL", "ULL" };
00607
00608 const char *value, *suffix;
00609 char *buf;
00610 size_t idx;
00611
00612
00613
00614
00615 switch (TYPE_PRECISION (type))
00616 {
00617 case 8: idx = 0; break;
00618 case 16: idx = 2; break;
00619 case 32: idx = 4; break;
00620 case 64: idx = 6; break;
00621 case 128: idx = 8; break;
00622 default: gcc_unreachable ();
00623 }
00624
00625 value = values[idx + TYPE_UNSIGNED (type)];
00626 suffix = suffixes[is_long * 2 + TYPE_UNSIGNED (type)];
00627
00628 buf = alloca (strlen (macro) + 1 + strlen (value) + strlen (suffix) + 1);
00629 sprintf (buf, "%s=%s%s", macro, value, suffix);
00630
00631 cpp_define (parse_in, buf);
00632 }