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