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