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