OSDN Git Service

2010-10-07 Martin Jambor <mjambor@suse.cz>
[pf3gnuchains/gcc-fork.git] / gcc / cppbuiltin.c
1 /* Define builtin-in macros for all front ends that perform preprocessing
2    Copyright (C) 2010
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "version.h"
27 #include "flags.h"
28 #include "toplev.h"
29 #include "cpp-id-data.h"
30 #include "cppbuiltin.h"
31
32
33 /* Parse a BASEVER version string of the format "major.minor.patchlevel"
34    or "major.minor" to extract its components.  */
35 void
36 parse_basever (int *major, int *minor, int *patchlevel)
37 {
38   static int s_major = -1, s_minor, s_patchlevel;
39
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
47   if (major)
48     *major = s_major;
49
50   if (minor)
51     *minor = s_minor;
52
53   if (patchlevel)
54     *patchlevel = s_patchlevel;
55 }
56
57
58 /* Define __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__ and __VERSION__.  */
59 static void
60 define__GNUC__ (cpp_reader *pfile)
61 {
62   int major, minor, patchlevel;
63
64   parse_basever (&major, &minor, &patchlevel);
65   cpp_define_formatted (pfile, "__GNUC__=%d", major);
66   cpp_define_formatted (pfile, "__GNUC_MINOR__=%d", minor);
67   cpp_define_formatted (pfile, "__GNUC_PATCHLEVEL__=%d", patchlevel);
68   cpp_define_formatted (pfile, "__VERSION__=\"%s\"", version_string);
69 }
70
71
72 /* Define various built-in CPP macros that depend on language-independent
73    compilation flags.  */
74 static void
75 define_builtin_macros_for_compilation_flags (cpp_reader *pfile)
76 {
77   if (flag_pic)
78     {
79       cpp_define_formatted (pfile, "__pic__=%d", flag_pic);
80       cpp_define_formatted (pfile, "__PIC__=%d", flag_pic);
81     }
82   if (flag_pie)
83     {
84       cpp_define_formatted (pfile, "__pie__=%d", flag_pie);
85       cpp_define_formatted (pfile, "__PIE__=%d", flag_pie);
86     }
87
88   if (optimize_size)
89     cpp_define (pfile, "__OPTIMIZE_SIZE__");
90   if (optimize)
91     cpp_define (pfile, "__OPTIMIZE__");
92
93   if (fast_math_flags_set_p ())
94     cpp_define (pfile, "__FAST_MATH__");
95   if (flag_signaling_nans)
96     cpp_define (pfile, "__SUPPORT_SNAN__");
97
98   cpp_define_formatted (pfile, "__FINITE_MATH_ONLY__=%d",
99                         flag_finite_math_only);
100 }
101
102
103 /* Define built-in macros for LP64 targets. */
104 static void
105 define_builtin_macros_for_lp64 (cpp_reader *pfile)
106 {
107   if (TYPE_PRECISION (long_integer_type_node) == 64
108       && POINTER_SIZE == 64
109       && TYPE_PRECISION (integer_type_node) == 32)
110     {
111       cpp_define (pfile, "_LP64");
112       cpp_define (pfile, "__LP64__");
113     }
114 }
115
116
117 /* Define macros for size of basic C types.  */
118 static void
119 define_builtin_macros_for_type_sizes (cpp_reader *pfile)
120 {
121 #define define_type_sizeof(NAME, TYPE)                             \
122     cpp_define_formatted (pfile, NAME"="HOST_WIDE_INT_PRINT_DEC,   \
123                           tree_low_cst (TYPE_SIZE_UNIT (TYPE), 1))
124
125   define_type_sizeof ("__SIZEOF_INT__", integer_type_node);
126   define_type_sizeof ("__SIZEOF_LONG__", long_integer_type_node);
127   define_type_sizeof ("__SIZEOF_LONG_LONG__", long_long_integer_type_node);
128   define_type_sizeof ("__SIZEOF_SHORT__", short_integer_type_node);
129   define_type_sizeof ("__SIZEOF_FLOAT__", float_type_node);
130   define_type_sizeof ("__SIZEOF_DOUBLE__", double_type_node);
131   define_type_sizeof ("__SIZEOF_LONG_DOUBLE__", long_double_type_node);
132   define_type_sizeof ("__SIZEOF_SIZE_T__", size_type_node);
133
134 #undef define_type_sizeof
135
136   cpp_define_formatted (pfile, "__CHAR_BIT__=%u",
137                         TYPE_PRECISION (char_type_node));
138   cpp_define_formatted (pfile, "__BIGGEST_ALIGNMENT__=%d",
139                         BIGGEST_ALIGNMENT / BITS_PER_UNIT);
140
141   /* ptr_type_node can't be used here since ptr_mode is only set when
142      toplev calls backend_init which is not done with -E switch.  */
143   cpp_define_formatted (pfile, "__SIZEOF_POINTER__=%d",
144                         POINTER_SIZE / BITS_PER_UNIT);
145 }
146
147
148 /* Define macros builtins common to all language performing CPP
149    preprocessing.  */
150 void
151 define_language_independent_builtin_macros (cpp_reader *pfile)
152 {
153   define__GNUC__ (pfile);
154   define_builtin_macros_for_compilation_flags (pfile);
155   define_builtin_macros_for_lp64 (pfile);
156   define_builtin_macros_for_type_sizes (pfile);
157 }