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 const char *);
00058 static void builtin_define_stdint_macros (void);
00059 static void builtin_define_type_max (const char *, tree, int);
00060 static void builtin_define_type_precision (const char *, tree);
00061 static void builtin_define_float_constants (const char *,
00062 const char *,
00063 const char *,
00064 tree);
00065 static void define__GNUC__ (void);
00066
00067
00068 static void
00069 builtin_define_type_precision (const char *name, tree type)
00070 {
00071 builtin_define_with_int_value (name, TYPE_PRECISION (type));
00072 }
00073
00074
00075
00076 static void
00077 builtin_define_float_constants (const char *name_prefix,
00078 const char *fp_suffix,
00079 const char *fp_cast,
00080 tree type)
00081 {
00082
00083
00084
00085
00086
00087
00088
00089
00090 const double log10_2 = .30102999566398119521;
00091 double log10_b;
00092 const struct real_format *fmt;
00093
00094 char name[64], buf[128];
00095 int dig, min_10_exp, max_10_exp;
00096 int decimal_dig;
00097
00098 fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
00099 gcc_assert (fmt->b != 10);
00100
00101
00102 if (type == float_type_node)
00103 builtin_define_with_int_value ("__FLT_RADIX__", fmt->b);
00104 log10_b = log10_2 * fmt->log2_b;
00105
00106
00107 sprintf (name, "__%s_MANT_DIG__", name_prefix);
00108 builtin_define_with_int_value (name, fmt->p);
00109
00110
00111
00112
00113
00114
00115
00116
00117 dig = (fmt->p - 1) * log10_b;
00118 sprintf (name, "__%s_DIG__", name_prefix);
00119 builtin_define_with_int_value (name, dig);
00120
00121
00122 sprintf (name, "__%s_MIN_EXP__", name_prefix);
00123 sprintf (buf, "(%d)", fmt->emin);
00124 builtin_define_with_value (name, buf, 0);
00125
00126
00127
00128
00129
00130
00131
00132
00133 min_10_exp = (fmt->emin - 1) * log10_b;
00134 sprintf (name, "__%s_MIN_10_EXP__", name_prefix);
00135 sprintf (buf, "(%d)", min_10_exp);
00136 builtin_define_with_value (name, buf, 0);
00137
00138
00139 sprintf (name, "__%s_MAX_EXP__", name_prefix);
00140 builtin_define_with_int_value (name, fmt->emax);
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 max_10_exp = fmt->emax * log10_b;
00169 sprintf (name, "__%s_MAX_10_EXP__", name_prefix);
00170 builtin_define_with_int_value (name, max_10_exp);
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 {
00182 double d_decimal_dig = 1 + fmt->p * log10_b;
00183 decimal_dig = d_decimal_dig;
00184 if (decimal_dig < d_decimal_dig)
00185 decimal_dig++;
00186 }
00187 if (type == long_double_type_node)
00188 builtin_define_with_int_value ("__DECIMAL_DIG__", decimal_dig);
00189
00190
00191
00192
00193
00194
00195
00196 {
00197 int i, n;
00198 char *p;
00199
00200 strcpy (buf, "0x0.");
00201 n = fmt->p * fmt->log2_b;
00202 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
00203 *p++ = 'f';
00204 if (i < n)
00205 *p++ = "08ce"[n - i];
00206 sprintf (p, "p%d", fmt->emax * fmt->log2_b);
00207 if (fmt->pnan < fmt->p)
00208 {
00209
00210
00211
00212
00213
00214
00215 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
00216 }
00217 }
00218 sprintf (name, "__%s_MAX__", name_prefix);
00219 builtin_define_with_hex_fp_value (name, type, decimal_dig, buf, fp_suffix, fp_cast);
00220
00221
00222
00223 sprintf (name, "__%s_MIN__", name_prefix);
00224 sprintf (buf, "0x1p%d", (fmt->emin - 1) * fmt->log2_b);
00225 builtin_define_with_hex_fp_value (name, type, decimal_dig, buf, fp_suffix, fp_cast);
00226
00227
00228
00229 sprintf (name, "__%s_EPSILON__", name_prefix);
00230 if (fmt->pnan < fmt->p)
00231
00232
00233 sprintf (buf, "0x1p%d", (fmt->emin - fmt->p) * fmt->log2_b);
00234 else
00235 sprintf (buf, "0x1p%d", (1 - fmt->p) * fmt->log2_b);
00236 builtin_define_with_hex_fp_value (name, type, decimal_dig, buf, fp_suffix, fp_cast);
00237
00238
00239
00240
00241 sprintf (name, "__%s_DENORM_MIN__", name_prefix);
00242 if (fmt->has_denorm)
00243 {
00244 sprintf (buf, "0x1p%d", (fmt->emin - fmt->p) * fmt->log2_b);
00245 builtin_define_with_hex_fp_value (name, type, decimal_dig,
00246 buf, fp_suffix, fp_cast);
00247 }
00248 else
00249 {
00250 sprintf (buf, "0.0%s", fp_suffix);
00251 builtin_define_with_value (name, buf, 0);
00252 }
00253
00254 sprintf (name, "__%s_HAS_DENORM__", name_prefix);
00255 builtin_define_with_value (name, fmt->has_denorm ? "1" : "0", 0);
00256
00257
00258 sprintf (name, "__%s_HAS_INFINITY__", name_prefix);
00259 builtin_define_with_int_value (name,
00260 MODE_HAS_INFINITIES (TYPE_MODE (type)));
00261
00262
00263
00264
00265
00266 sprintf (name, "__%s_HAS_QUIET_NAN__", name_prefix);
00267 builtin_define_with_int_value (name, MODE_HAS_NANS (TYPE_MODE (type)));
00268 }
00269
00270
00271 static void
00272 builtin_define_decimal_float_constants (const char *name_prefix,
00273 const char *suffix,
00274 tree type)
00275 {
00276 const struct real_format *fmt;
00277 char name[64], buf[128], *p;
00278 int digits;
00279
00280 fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
00281
00282
00283 sprintf (name, "__%s_MANT_DIG__", name_prefix);
00284 builtin_define_with_int_value (name, fmt->p);
00285
00286
00287 sprintf (name, "__%s_MIN_EXP__", name_prefix);
00288 sprintf (buf, "(%d)", fmt->emin);
00289 builtin_define_with_value (name, buf, 0);
00290
00291
00292 sprintf (name, "__%s_MAX_EXP__", name_prefix);
00293 builtin_define_with_int_value (name, fmt->emax);
00294
00295
00296 sprintf (name, "__%s_MIN__", name_prefix);
00297 sprintf (buf, "1E%d%s", fmt->emin, suffix);
00298 builtin_define_with_value (name, buf, 0);
00299
00300
00301 sprintf (name, "__%s_MAX__", name_prefix);
00302 p = buf;
00303 for (digits = fmt->p; digits; digits--)
00304 {
00305 *p++ = '9';
00306 if (digits == fmt->p)
00307 *p++ = '.';
00308 }
00309 *p = 0;
00310
00311 sprintf (&buf[fmt->p + 1], "E%d%s", fmt->emax, suffix);
00312 builtin_define_with_value (name, buf, 0);
00313
00314
00315
00316 sprintf (name, "__%s_EPSILON__", name_prefix);
00317 sprintf (buf, "1E-%d%s", fmt->p - 1, suffix);
00318 builtin_define_with_value (name, buf, 0);
00319
00320
00321 sprintf (name, "__%s_DEN__", name_prefix);
00322 p = buf;
00323 for (digits = fmt->p; digits > 1; digits--)
00324 {
00325 *p++ = '0';
00326 if (digits == fmt->p)
00327 *p++ = '.';
00328 }
00329 *p = 0;
00330 sprintf (&buf[fmt->p], "1E%d%s", fmt->emin, suffix);
00331 builtin_define_with_value (name, buf, 0);
00332 }
00333
00334
00335 static void
00336 define__GNUC__ (void)
00337 {
00338
00339
00340 const char *q, *v = version_string;
00341
00342 while (*v && !ISDIGIT (*v))
00343 v++;
00344 gcc_assert (*v && (v <= version_string || v[-1] == '-'));
00345
00346 q = v;
00347 while (ISDIGIT (*v))
00348 v++;
00349 builtin_define_with_value_n ("__GNUC__", q, v - q);
00350 if (c_dialect_cxx ())
00351 builtin_define_with_value_n ("__GNUG__", q, v - q);
00352
00353 gcc_assert (*v == '.' && ISDIGIT (v[1]));
00354
00355 q = ++v;
00356 while (ISDIGIT (*v))
00357 v++;
00358 builtin_define_with_value_n ("__GNUC_MINOR__", q, v - q);
00359
00360 if (*v == '.')
00361 {
00362 gcc_assert (ISDIGIT (v[1]));
00363 q = ++v;
00364 while (ISDIGIT (*v))
00365 v++;
00366 builtin_define_with_value_n ("__GNUC_PATCHLEVEL__", q, v - q);
00367 }
00368 else
00369 builtin_define_with_value_n ("__GNUC_PATCHLEVEL__", "0", 1);
00370
00371 gcc_assert (!*v || *v == ' ' || *v == '-');
00372 }
00373
00374
00375
00376 static void
00377 builtin_define_stdint_macros (void)
00378 {
00379 int intmax_long;
00380 if (intmax_type_node == long_long_integer_type_node)
00381 intmax_long = 2;
00382 else if (intmax_type_node == long_integer_type_node)
00383 intmax_long = 1;
00384 else if (intmax_type_node == integer_type_node)
00385 intmax_long = 0;
00386 else
00387 gcc_unreachable ();
00388 builtin_define_type_max ("__INTMAX_MAX__", intmax_type_node, intmax_long);
00389 }
00390
00391
00392 void
00393 c_cpp_builtins (cpp_reader *pfile)
00394 {
00395
00396 if (flag_undef)
00397 return;
00398
00399 define__GNUC__ ();
00400
00401
00402 c_stddef_cpp_builtins ();
00403
00404 if (c_dialect_cxx ())
00405 {
00406 if (flag_weak && SUPPORTS_ONE_ONLY)
00407 cpp_define (pfile, "__GXX_WEAK__=1");
00408 else
00409 cpp_define (pfile, "__GXX_WEAK__=0");
00410 if (warn_deprecated)
00411 cpp_define (pfile, "__DEPRECATED");
00412 }
00413
00414
00415 if (flag_exceptions)
00416 cpp_define (pfile, "__EXCEPTIONS");
00417
00418
00419
00420 if (flag_abi_version == 0)
00421
00422
00423
00424
00425
00426
00427
00428 builtin_define_with_int_value ("__GXX_ABI_VERSION", 999999);
00429 else if (flag_abi_version == 1)
00430
00431
00432 builtin_define_with_int_value ("__GXX_ABI_VERSION", 102);
00433 else
00434
00435 builtin_define_with_int_value ("__GXX_ABI_VERSION",
00436 1000 + flag_abi_version);
00437
00438
00439 if (USING_SJLJ_EXCEPTIONS)
00440 cpp_define (pfile, "__USING_SJLJ_EXCEPTIONS__");
00441
00442
00443 builtin_define_type_max ("__SCHAR_MAX__", signed_char_type_node, 0);
00444 builtin_define_type_max ("__SHRT_MAX__", short_integer_type_node, 0);
00445 builtin_define_type_max ("__INT_MAX__", integer_type_node, 0);
00446 builtin_define_type_max ("__LONG_MAX__", long_integer_type_node, 1);
00447 builtin_define_type_max ("__LONG_LONG_MAX__", long_long_integer_type_node, 2);
00448 builtin_define_type_max ("__WCHAR_MAX__", wchar_type_node, 0);
00449
00450 builtin_define_type_precision ("__CHAR_BIT__", char_type_node);
00451
00452
00453 builtin_define_stdint_macros ();
00454
00455
00456
00457 builtin_define_with_int_value ("__FLT_EVAL_METHOD__",
00458 TARGET_FLT_EVAL_METHOD);
00459
00460
00461 builtin_define_with_int_value ("__DEC_EVAL_METHOD__",
00462 TARGET_DEC_EVAL_METHOD);
00463
00464 builtin_define_float_constants ("FLT", "F", "%s", float_type_node);
00465
00466
00467
00468
00469 if (flag_single_precision_constant)
00470 builtin_define_float_constants ("DBL", "L", "((double)%s)", double_type_node);
00471 else
00472 builtin_define_float_constants ("DBL", "", "%s", double_type_node);
00473 builtin_define_float_constants ("LDBL", "L", "%s", long_double_type_node);
00474
00475
00476 builtin_define_decimal_float_constants ("DEC32", "DF", dfloat32_type_node);
00477 builtin_define_decimal_float_constants ("DEC64", "DD", dfloat64_type_node);
00478 builtin_define_decimal_float_constants ("DEC128", "DL", dfloat128_type_node);
00479
00480
00481 builtin_define_with_value ("__REGISTER_PREFIX__", REGISTER_PREFIX, 0);
00482 builtin_define_with_value ("__USER_LABEL_PREFIX__", user_label_prefix, 0);
00483
00484
00485 builtin_define_with_value ("__VERSION__", version_string, 1);
00486
00487 cpp_define (pfile, "__GNUC_GNU_INLINE__");
00488
00489
00490 if (TYPE_PRECISION (long_integer_type_node) == 64
00491 && POINTER_SIZE == 64
00492 && TYPE_PRECISION (integer_type_node) == 32)
00493 {
00494 cpp_define (pfile, "_LP64");
00495 cpp_define (pfile, "__LP64__");
00496 }
00497
00498
00499
00500 if (optimize_size)
00501 cpp_define (pfile, "__OPTIMIZE_SIZE__");
00502 if (optimize)
00503 cpp_define (pfile, "__OPTIMIZE__");
00504
00505 if (fast_math_flags_set_p ())
00506 cpp_define (pfile, "__FAST_MATH__");
00507 if (flag_really_no_inline)
00508 cpp_define (pfile, "__NO_INLINE__");
00509 if (flag_signaling_nans)
00510 cpp_define (pfile, "__SUPPORT_SNAN__");
00511 if (flag_finite_math_only)
00512 cpp_define (pfile, "__FINITE_MATH_ONLY__=1");
00513 else
00514 cpp_define (pfile, "__FINITE_MATH_ONLY__=0");
00515 if (flag_pic)
00516 {
00517 builtin_define_with_int_value ("__pic__", flag_pic);
00518 builtin_define_with_int_value ("__PIC__", flag_pic);
00519 }
00520
00521 if (flag_iso)
00522 cpp_define (pfile, "__STRICT_ANSI__");
00523
00524 if (!flag_signed_char)
00525 cpp_define (pfile, "__CHAR_UNSIGNED__");
00526
00527 if (c_dialect_cxx () && TYPE_UNSIGNED (wchar_type_node))
00528 cpp_define (pfile, "__WCHAR_UNSIGNED__");
00529
00530
00531 if (c_dialect_objc () && flag_next_runtime)
00532 cpp_define (pfile, "__NEXT_RUNTIME__");
00533
00534
00535 if (flag_mudflap || targetm.handle_pragma_redefine_extname)
00536 cpp_define (pfile, "__PRAGMA_REDEFINE_EXTNAME");
00537
00538 if (targetm.handle_pragma_extern_prefix)
00539 cpp_define (pfile, "__PRAGMA_EXTERN_PREFIX");
00540
00541
00542
00543
00544 if (flag_stack_protect == 2)
00545 cpp_define (pfile, "__SSP_ALL__=2");
00546 else if (flag_stack_protect == 1)
00547 cpp_define (pfile, "__SSP__=1");
00548
00549 if (flag_openmp)
00550 cpp_define (pfile, "_OPENMP=200505");
00551
00552
00553
00554 # define preprocessing_asm_p() (cpp_get_options (pfile)->lang == CLK_ASM)
00555 # define preprocessing_trad_p() (cpp_get_options (pfile)->traditional)
00556 # define builtin_define(TXT) cpp_define (pfile, TXT)
00557 # define builtin_assert(TXT) cpp_assert (pfile, TXT)
00558 TARGET_CPU_CPP_BUILTINS ();
00559 TARGET_OS_CPP_BUILTINS ();
00560 TARGET_OBJFMT_CPP_BUILTINS ();
00561
00562
00563
00564
00565
00566
00567
00568 if (TARGET_DECLSPEC)
00569 builtin_define ("__declspec(x)=__attribute__((x))");
00570 }
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581 void
00582 builtin_define_std (const char *macro)
00583 {
00584 size_t len = strlen (macro);
00585 char *buff = (char *) alloca (len + 5);
00586 char *p = buff + 2;
00587 char *q = p + len;
00588
00589
00590 memcpy (p, macro, len + 1);
00591 if (!( *p == '_' && (p[1] == '_' || ISUPPER (p[1]))))
00592 {
00593 if (*p != '_')
00594 *--p = '_';
00595 if (p[1] != '_')
00596 *--p = '_';
00597 }
00598 cpp_define (parse_in, p);
00599
00600
00601 if (p != buff + 2)
00602 {
00603
00604 if (q[-1] != '_')
00605 *q++ = '_';
00606 if (q[-2] != '_')
00607 *q++ = '_';
00608 *q = '\0';
00609 cpp_define (parse_in, p);
00610
00611
00612 if (!flag_iso)
00613 cpp_define (parse_in, macro);
00614 }
00615 }
00616
00617
00618
00619
00620 void
00621 builtin_define_with_value (const char *macro, const char *expansion, int is_str)
00622 {
00623 char *buf;
00624 size_t mlen = strlen (macro);
00625 size_t elen = strlen (expansion);
00626 size_t extra = 2;
00627
00628 if (is_str)
00629 extra += 2;
00630
00631 buf = (char *) alloca (mlen + elen + extra);
00632 if (is_str)
00633 sprintf (buf, "%s=\"%s\"", macro, expansion);
00634 else
00635 sprintf (buf, "%s=%s", macro, expansion);
00636
00637 cpp_define (parse_in, buf);
00638 }
00639
00640
00641
00642 static void
00643 builtin_define_with_value_n (const char *macro, const char *expansion, size_t elen)
00644 {
00645 char *buf;
00646 size_t mlen = strlen (macro);
00647
00648
00649 buf = (char *) alloca (mlen + elen + 2);
00650 memcpy (buf, macro, mlen);
00651 buf[mlen] = '=';
00652 memcpy (buf + mlen + 1, expansion, elen);
00653 buf[mlen + elen + 1] = '\0';
00654
00655 cpp_define (parse_in, buf);
00656 }
00657
00658
00659 static void
00660 builtin_define_with_int_value (const char *macro, HOST_WIDE_INT value)
00661 {
00662 char *buf;
00663 size_t mlen = strlen (macro);
00664 size_t vlen = 18;
00665 size_t extra = 2;
00666
00667 buf = (char *) alloca (mlen + vlen + extra);
00668 memcpy (buf, macro, mlen);
00669 buf[mlen] = '=';
00670 sprintf (buf + mlen + 1, HOST_WIDE_INT_PRINT_DEC, value);
00671
00672 cpp_define (parse_in, buf);
00673 }
00674
00675
00676 static void
00677 builtin_define_with_hex_fp_value (const char *macro,
00678 tree type ATTRIBUTE_UNUSED, int digits,
00679 const char *hex_str,
00680 const char *fp_suffix,
00681 const char *fp_cast)
00682 {
00683 REAL_VALUE_TYPE real;
00684 char dec_str[64], buf1[256], buf2[256];
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696 real_from_string (&real, hex_str);
00697 real_to_decimal (dec_str, &real, sizeof (dec_str), digits, 0);
00698
00699
00700
00701 sprintf (buf1, "%s%s", dec_str, fp_suffix);
00702 sprintf (buf2, fp_cast, buf1);
00703 sprintf (buf1, "%s=%s", macro, buf2);
00704
00705 cpp_define (parse_in, buf1);
00706 }
00707
00708
00709
00710
00711
00712 static void
00713 builtin_define_type_max (const char *macro, tree type, int is_long)
00714 {
00715 static const char *const values[]
00716 = { "127", "255",
00717 "32767", "65535",
00718 "2147483647", "4294967295",
00719 "9223372036854775807", "18446744073709551615",
00720 "170141183460469231731687303715884105727",
00721 "340282366920938463463374607431768211455" };
00722 static const char *const suffixes[] = { "", "U", "L", "UL", "LL", "ULL" };
00723
00724 const char *value, *suffix;
00725 char *buf;
00726 size_t idx;
00727
00728
00729
00730
00731 switch (TYPE_PRECISION (type))
00732 {
00733 case 8: idx = 0; break;
00734 case 16: idx = 2; break;
00735 case 32: idx = 4; break;
00736 case 64: idx = 6; break;
00737 case 128: idx = 8; break;
00738 default: gcc_unreachable ();
00739 }
00740
00741 value = values[idx + TYPE_UNSIGNED (type)];
00742 suffix = suffixes[is_long * 2 + TYPE_UNSIGNED (type)];
00743
00744 buf = (char *) alloca (strlen (macro) + 1 + strlen (value)
00745 + strlen (suffix) + 1);
00746 sprintf (buf, "%s=%s%s", macro, value, suffix);
00747
00748 cpp_define (parse_in, buf);
00749 }