1 /* Define builtin-in macros for all front ends that perform preprocessing
2    Copyright (C) 2010-2016 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 "target.h"
24 #include "tree.h"
25 #include "version.h"
26 #include "flags.h"
27 #include "cpp-id-data.h"
28 #include "cppbuiltin.h"
29 
30 
31 /* Parse a BASEVER version string of the format "major.minor.patchlevel"
32    or "major.minor" to extract its components.  */
33 void
parse_basever(int * major,int * minor,int * patchlevel)34 parse_basever (int *major, int *minor, int *patchlevel)
35 {
36   static int s_major = -1, s_minor, s_patchlevel;
37 
38   if (s_major == -1)
39     if (sscanf (BASEVER, "%d.%d.%d", &s_major, &s_minor, &s_patchlevel) != 3)
40       {
41 	sscanf (BASEVER, "%d.%d", &s_major, &s_minor);
42 	s_patchlevel = 0;
43       }
44 
45   if (major)
46     *major = s_major;
47 
48   if (minor)
49     *minor = s_minor;
50 
51   if (patchlevel)
52     *patchlevel = s_patchlevel;
53 }
54 
55 
56 /* Define __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__ and __VERSION__.  */
57 static void
define__GNUC__(cpp_reader * pfile)58 define__GNUC__ (cpp_reader *pfile)
59 {
60   int major, minor, patchlevel;
61 
62   parse_basever (&major, &minor, &patchlevel);
63   cpp_define_formatted (pfile, "__GNUC__=%d", major);
64   cpp_define_formatted (pfile, "__GNUC_MINOR__=%d", minor);
65   cpp_define_formatted (pfile, "__GNUC_PATCHLEVEL__=%d", patchlevel);
66   cpp_define_formatted (pfile, "__VERSION__=\"%s\"", version_string);
67   cpp_define_formatted (pfile, "__ATOMIC_RELAXED=%d", MEMMODEL_RELAXED);
68   cpp_define_formatted (pfile, "__ATOMIC_SEQ_CST=%d", MEMMODEL_SEQ_CST);
69   cpp_define_formatted (pfile, "__ATOMIC_ACQUIRE=%d", MEMMODEL_ACQUIRE);
70   cpp_define_formatted (pfile, "__ATOMIC_RELEASE=%d", MEMMODEL_RELEASE);
71   cpp_define_formatted (pfile, "__ATOMIC_ACQ_REL=%d", MEMMODEL_ACQ_REL);
72   cpp_define_formatted (pfile, "__ATOMIC_CONSUME=%d", MEMMODEL_CONSUME);
73 }
74 
75 
76 /* Define various built-in CPP macros that depend on language-independent
77    compilation flags.  */
78 static void
define_builtin_macros_for_compilation_flags(cpp_reader * pfile)79 define_builtin_macros_for_compilation_flags (cpp_reader *pfile)
80 {
81   if (flag_pic)
82     {
83       cpp_define_formatted (pfile, "__pic__=%d", flag_pic);
84       cpp_define_formatted (pfile, "__PIC__=%d", flag_pic);
85     }
86   if (flag_pie)
87     {
88       cpp_define_formatted (pfile, "__pie__=%d", flag_pie);
89       cpp_define_formatted (pfile, "__PIE__=%d", flag_pie);
90     }
91 
92   if (flag_sanitize & SANITIZE_ADDRESS)
93     cpp_define (pfile, "__SANITIZE_ADDRESS__");
94 
95   if (optimize_size)
96     cpp_define (pfile, "__OPTIMIZE_SIZE__");
97   if (optimize)
98     cpp_define (pfile, "__OPTIMIZE__");
99 
100   if (fast_math_flags_set_p (&global_options))
101     cpp_define (pfile, "__FAST_MATH__");
102   if (flag_signaling_nans)
103     cpp_define (pfile, "__SUPPORT_SNAN__");
104   if (!flag_errno_math)
105     cpp_define (pfile, "__NO_MATH_ERRNO__");
106 
107   cpp_define_formatted (pfile, "__FINITE_MATH_ONLY__=%d",
108 			flag_finite_math_only);
109   if (flag_cilkplus)
110     cpp_define (pfile, "__cilk=200");
111 
112   if (flag_check_pointer_bounds)
113     cpp_define (pfile, "__CHKP__");
114 }
115 
116 
117 /* Define built-in macros for LP64 targets. */
118 static void
define_builtin_macros_for_lp64(cpp_reader * pfile)119 define_builtin_macros_for_lp64 (cpp_reader *pfile)
120 {
121   if (TYPE_PRECISION (long_integer_type_node) == 64
122       && POINTER_SIZE == 64
123       && TYPE_PRECISION (integer_type_node) == 32)
124     {
125       cpp_define (pfile, "_LP64");
126       cpp_define (pfile, "__LP64__");
127     }
128 }
129 
130 
131 /* Define macros for size of basic C types.  */
132 static void
define_builtin_macros_for_type_sizes(cpp_reader * pfile)133 define_builtin_macros_for_type_sizes (cpp_reader *pfile)
134 {
135 #define define_type_sizeof(NAME, TYPE)                             \
136     cpp_define_formatted (pfile, NAME"=" HOST_WIDE_INT_PRINT_DEC,   \
137                           tree_to_uhwi (TYPE_SIZE_UNIT (TYPE)))
138 
139   define_type_sizeof ("__SIZEOF_INT__", integer_type_node);
140   define_type_sizeof ("__SIZEOF_LONG__", long_integer_type_node);
141   define_type_sizeof ("__SIZEOF_LONG_LONG__", long_long_integer_type_node);
142   define_type_sizeof ("__SIZEOF_SHORT__", short_integer_type_node);
143   define_type_sizeof ("__SIZEOF_FLOAT__", float_type_node);
144   define_type_sizeof ("__SIZEOF_DOUBLE__", double_type_node);
145   define_type_sizeof ("__SIZEOF_LONG_DOUBLE__", long_double_type_node);
146   define_type_sizeof ("__SIZEOF_SIZE_T__", size_type_node);
147 
148 #undef define_type_sizeof
149 
150   cpp_define_formatted (pfile, "__CHAR_BIT__=%u",
151 			TYPE_PRECISION (char_type_node));
152   cpp_define_formatted (pfile, "__BIGGEST_ALIGNMENT__=%d",
153 			BIGGEST_ALIGNMENT / BITS_PER_UNIT);
154 
155   /* Define constants useful for implementing endian.h.  */
156   cpp_define (pfile, "__ORDER_LITTLE_ENDIAN__=1234");
157   cpp_define (pfile, "__ORDER_BIG_ENDIAN__=4321");
158   cpp_define (pfile, "__ORDER_PDP_ENDIAN__=3412");
159 
160   if (WORDS_BIG_ENDIAN == BYTES_BIG_ENDIAN)
161     cpp_define_formatted (pfile, "__BYTE_ORDER__=%s",
162 			  (WORDS_BIG_ENDIAN
163 			   ? "__ORDER_BIG_ENDIAN__"
164 			   : "__ORDER_LITTLE_ENDIAN__"));
165   else
166     {
167       /* Assert that we're only dealing with the PDP11 case.  */
168       gcc_assert (!BYTES_BIG_ENDIAN);
169       gcc_assert (WORDS_BIG_ENDIAN);
170 
171       cpp_define (pfile, "__BYTE_ORDER__=__ORDER_PDP_ENDIAN__");
172     }
173 
174   cpp_define_formatted (pfile, "__FLOAT_WORD_ORDER__=%s",
175                         (targetm.float_words_big_endian ()
176                          ? "__ORDER_BIG_ENDIAN__"
177                          : "__ORDER_LITTLE_ENDIAN__"));
178 
179   /* ptr_type_node can't be used here since ptr_mode is only set when
180      toplev calls backend_init which is not done with -E switch.  */
181   cpp_define_formatted (pfile, "__SIZEOF_POINTER__=%d",
182 			1 << ceil_log2 ((POINTER_SIZE + BITS_PER_UNIT - 1) / BITS_PER_UNIT));
183 }
184 
185 
186 /* Define macros builtins common to all language performing CPP
187    preprocessing.  */
188 void
define_language_independent_builtin_macros(cpp_reader * pfile)189 define_language_independent_builtin_macros (cpp_reader *pfile)
190 {
191   define__GNUC__ (pfile);
192   define_builtin_macros_for_compilation_flags (pfile);
193   define_builtin_macros_for_lp64 (pfile);
194   define_builtin_macros_for_type_sizes (pfile);
195 }
196