cppbuiltin.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* Define builtin-in macros for all front ends that perform preprocessing
  2. Copyright (C) 2010-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "hash-set.h"
  20. #include "machmode.h"
  21. #include "vec.h"
  22. #include "double-int.h"
  23. #include "input.h"
  24. #include "alias.h"
  25. #include "symtab.h"
  26. #include "wide-int.h"
  27. #include "inchash.h"
  28. #include "tree.h"
  29. #include "version.h"
  30. #include "flags.h"
  31. #include "cpp-id-data.h"
  32. #include "cppbuiltin.h"
  33. #include "target.h"
  34. /* Parse a BASEVER version string of the format "major.minor.patchlevel"
  35. or "major.minor" to extract its components. */
  36. void
  37. parse_basever (int *major, int *minor, int *patchlevel)
  38. {
  39. static int s_major = -1, s_minor, s_patchlevel;
  40. if (s_major == -1)
  41. if (sscanf (BASEVER, "%d.%d.%d", &s_major, &s_minor, &s_patchlevel) != 3)
  42. {
  43. sscanf (BASEVER, "%d.%d", &s_major, &s_minor);
  44. s_patchlevel = 0;
  45. }
  46. if (major)
  47. *major = s_major;
  48. if (minor)
  49. *minor = s_minor;
  50. if (patchlevel)
  51. *patchlevel = s_patchlevel;
  52. }
  53. /* Define __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__ and __VERSION__. */
  54. static void
  55. define__GNUC__ (cpp_reader *pfile)
  56. {
  57. int major, minor, patchlevel;
  58. parse_basever (&major, &minor, &patchlevel);
  59. cpp_define_formatted (pfile, "__GNUC__=%d", major);
  60. cpp_define_formatted (pfile, "__GNUC_MINOR__=%d", minor);
  61. cpp_define_formatted (pfile, "__GNUC_PATCHLEVEL__=%d", patchlevel);
  62. cpp_define_formatted (pfile, "__VERSION__=\"%s\"", version_string);
  63. cpp_define_formatted (pfile, "__ATOMIC_RELAXED=%d", MEMMODEL_RELAXED);
  64. cpp_define_formatted (pfile, "__ATOMIC_SEQ_CST=%d", MEMMODEL_SEQ_CST);
  65. cpp_define_formatted (pfile, "__ATOMIC_ACQUIRE=%d", MEMMODEL_ACQUIRE);
  66. cpp_define_formatted (pfile, "__ATOMIC_RELEASE=%d", MEMMODEL_RELEASE);
  67. cpp_define_formatted (pfile, "__ATOMIC_ACQ_REL=%d", MEMMODEL_ACQ_REL);
  68. cpp_define_formatted (pfile, "__ATOMIC_CONSUME=%d", MEMMODEL_CONSUME);
  69. }
  70. /* Define various built-in CPP macros that depend on language-independent
  71. compilation flags. */
  72. static void
  73. define_builtin_macros_for_compilation_flags (cpp_reader *pfile)
  74. {
  75. if (flag_pic)
  76. {
  77. cpp_define_formatted (pfile, "__pic__=%d", flag_pic);
  78. cpp_define_formatted (pfile, "__PIC__=%d", flag_pic);
  79. }
  80. if (flag_pie)
  81. {
  82. cpp_define_formatted (pfile, "__pie__=%d", flag_pie);
  83. cpp_define_formatted (pfile, "__PIE__=%d", flag_pie);
  84. }
  85. if (flag_sanitize & SANITIZE_ADDRESS)
  86. cpp_define (pfile, "__SANITIZE_ADDRESS__");
  87. if (optimize_size)
  88. cpp_define (pfile, "__OPTIMIZE_SIZE__");
  89. if (optimize)
  90. cpp_define (pfile, "__OPTIMIZE__");
  91. if (fast_math_flags_set_p (&global_options))
  92. cpp_define (pfile, "__FAST_MATH__");
  93. if (flag_signaling_nans)
  94. cpp_define (pfile, "__SUPPORT_SNAN__");
  95. if (!flag_errno_math)
  96. cpp_define (pfile, "__NO_MATH_ERRNO__");
  97. cpp_define_formatted (pfile, "__FINITE_MATH_ONLY__=%d",
  98. flag_finite_math_only);
  99. if (flag_cilkplus)
  100. cpp_define (pfile, "__cilk=200");
  101. if (flag_check_pointer_bounds)
  102. cpp_define (pfile, "__CHKP__");
  103. }
  104. /* Define built-in macros for LP64 targets. */
  105. static void
  106. define_builtin_macros_for_lp64 (cpp_reader *pfile)
  107. {
  108. if (TYPE_PRECISION (long_integer_type_node) == 64
  109. && POINTER_SIZE == 64
  110. && TYPE_PRECISION (integer_type_node) == 32)
  111. {
  112. cpp_define (pfile, "_LP64");
  113. cpp_define (pfile, "__LP64__");
  114. }
  115. }
  116. /* Define macros for size of basic C types. */
  117. static void
  118. define_builtin_macros_for_type_sizes (cpp_reader *pfile)
  119. {
  120. #define define_type_sizeof(NAME, TYPE) \
  121. cpp_define_formatted (pfile, NAME"="HOST_WIDE_INT_PRINT_DEC, \
  122. tree_to_uhwi (TYPE_SIZE_UNIT (TYPE)))
  123. define_type_sizeof ("__SIZEOF_INT__", integer_type_node);
  124. define_type_sizeof ("__SIZEOF_LONG__", long_integer_type_node);
  125. define_type_sizeof ("__SIZEOF_LONG_LONG__", long_long_integer_type_node);
  126. define_type_sizeof ("__SIZEOF_SHORT__", short_integer_type_node);
  127. define_type_sizeof ("__SIZEOF_FLOAT__", float_type_node);
  128. define_type_sizeof ("__SIZEOF_DOUBLE__", double_type_node);
  129. define_type_sizeof ("__SIZEOF_LONG_DOUBLE__", long_double_type_node);
  130. define_type_sizeof ("__SIZEOF_SIZE_T__", size_type_node);
  131. #undef define_type_sizeof
  132. cpp_define_formatted (pfile, "__CHAR_BIT__=%u",
  133. TYPE_PRECISION (char_type_node));
  134. cpp_define_formatted (pfile, "__BIGGEST_ALIGNMENT__=%d",
  135. BIGGEST_ALIGNMENT / BITS_PER_UNIT);
  136. /* Define constants useful for implementing endian.h. */
  137. cpp_define (pfile, "__ORDER_LITTLE_ENDIAN__=1234");
  138. cpp_define (pfile, "__ORDER_BIG_ENDIAN__=4321");
  139. cpp_define (pfile, "__ORDER_PDP_ENDIAN__=3412");
  140. if (WORDS_BIG_ENDIAN == BYTES_BIG_ENDIAN)
  141. cpp_define_formatted (pfile, "__BYTE_ORDER__=%s",
  142. (WORDS_BIG_ENDIAN
  143. ? "__ORDER_BIG_ENDIAN__"
  144. : "__ORDER_LITTLE_ENDIAN__"));
  145. else
  146. {
  147. /* Assert that we're only dealing with the PDP11 case. */
  148. gcc_assert (!BYTES_BIG_ENDIAN);
  149. gcc_assert (WORDS_BIG_ENDIAN);
  150. cpp_define (pfile, "__BYTE_ORDER__=__ORDER_PDP_ENDIAN__");
  151. }
  152. cpp_define_formatted (pfile, "__FLOAT_WORD_ORDER__=%s",
  153. (targetm.float_words_big_endian ()
  154. ? "__ORDER_BIG_ENDIAN__"
  155. : "__ORDER_LITTLE_ENDIAN__"));
  156. /* ptr_type_node can't be used here since ptr_mode is only set when
  157. toplev calls backend_init which is not done with -E switch. */
  158. cpp_define_formatted (pfile, "__SIZEOF_POINTER__=%d",
  159. 1 << ceil_log2 ((POINTER_SIZE + BITS_PER_UNIT - 1) / BITS_PER_UNIT));
  160. }
  161. /* Define macros builtins common to all language performing CPP
  162. preprocessing. */
  163. void
  164. define_language_independent_builtin_macros (cpp_reader *pfile)
  165. {
  166. define__GNUC__ (pfile);
  167. define_builtin_macros_for_compilation_flags (pfile);
  168. define_builtin_macros_for_lp64 (pfile);
  169. define_builtin_macros_for_type_sizes (pfile);
  170. }