OSDN Git Service

2009-05-06 H.J. Lu <hongjiu.lu@intel.com>
[pf3gnuchains/gcc-fork.git] / gcc / config / i386 / i386-c.c
1 /* Subroutines used for macro/preprocessor support on the ia-32.
2    Copyright (C) 2008, 2009
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
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License 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 "rtl.h"
26 #include "tree.h"
27 #include "tm_p.h"
28 #include "flags.h"
29 #include "c-common.h"
30 #include "ggc.h"
31 #include "target.h"
32 #include "target-def.h"
33 #include "cpplib.h"
34 #include "c-pragma.h"
35
36 static bool ix86_pragma_target_parse (tree, tree);
37 static void ix86_target_macros_internal
38   (int, enum processor_type, enum processor_type, enum fpmath_unit,
39    void (*def_or_undef) (cpp_reader *, const char *));
40
41 \f
42 /* Internal function to either define or undef the appropriate system
43    macros.  */
44 static void
45 ix86_target_macros_internal (int isa_flag,
46                              enum processor_type arch,
47                              enum processor_type tune,
48                              enum fpmath_unit fpmath,
49                              void (*def_or_undef) (cpp_reader *,
50                                                    const char *))
51 {
52   /* For some of the k6/pentium varients there weren't seperate ISA bits to
53      identify which tune/arch flag was passed, so figure it out here.  */
54   size_t arch_len = strlen (ix86_arch_string);
55   size_t tune_len = strlen (ix86_tune_string);
56   int last_arch_char = ix86_arch_string[arch_len - 1];
57   int last_tune_char = ix86_tune_string[tune_len - 1];
58
59   /* Built-ins based on -march=.  */
60   switch (arch)
61     {
62     case PROCESSOR_I386:
63       break;
64     case PROCESSOR_I486:
65       def_or_undef (parse_in, "__i486");
66       def_or_undef (parse_in, "__i486__");
67       break;
68     case PROCESSOR_PENTIUM:
69       def_or_undef (parse_in, "__i586");
70       def_or_undef (parse_in, "__i586__");
71       def_or_undef (parse_in, "__pentium");
72       def_or_undef (parse_in, "__pentium__");
73       if (isa_flag & OPTION_MASK_ISA_MMX)
74         def_or_undef (parse_in, "__pentium_mmx__");
75       break;
76     case PROCESSOR_PENTIUMPRO:
77       def_or_undef (parse_in, "__i686");
78       def_or_undef (parse_in, "__i686__");
79       def_or_undef (parse_in, "__pentiumpro");
80       def_or_undef (parse_in, "__pentiumpro__");
81       break;
82     case PROCESSOR_GEODE:
83       def_or_undef (parse_in, "__geode");
84       def_or_undef (parse_in, "__geode__");
85       break;
86     case PROCESSOR_K6:
87       def_or_undef (parse_in, "__k6");
88       def_or_undef (parse_in, "__k6__");
89       if (last_arch_char == '2')
90         def_or_undef (parse_in, "__k6_2__");
91       else if (last_arch_char == '3')
92         def_or_undef (parse_in, "__k6_3__");
93       else if (isa_flag & OPTION_MASK_ISA_3DNOW)
94         def_or_undef (parse_in, "__k6_3__");
95       break;
96     case PROCESSOR_ATHLON:
97       def_or_undef (parse_in, "__athlon");
98       def_or_undef (parse_in, "__athlon__");
99       if (isa_flag & OPTION_MASK_ISA_SSE)
100         def_or_undef (parse_in, "__athlon_sse__");
101       break;
102     case PROCESSOR_K8:
103       def_or_undef (parse_in, "__k8");
104       def_or_undef (parse_in, "__k8__");
105       break;
106     case PROCESSOR_AMDFAM10:
107       def_or_undef (parse_in, "__amdfam10");
108       def_or_undef (parse_in, "__amdfam10__");
109       break;
110     case PROCESSOR_PENTIUM4:
111       def_or_undef (parse_in, "__pentium4");
112       def_or_undef (parse_in, "__pentium4__");
113       break;
114     case PROCESSOR_NOCONA:
115       def_or_undef (parse_in, "__nocona");
116       def_or_undef (parse_in, "__nocona__");
117       break;
118     case PROCESSOR_CORE2:
119       def_or_undef (parse_in, "__core2");
120       def_or_undef (parse_in, "__core2__");
121       break;
122     case PROCESSOR_ATOM:
123       def_or_undef (parse_in, "__atom");
124       def_or_undef (parse_in, "__atom__");
125       break;
126     /* use PROCESSOR_max to not set/unset the arch macro.  */
127     case PROCESSOR_max:
128       break;
129     case PROCESSOR_GENERIC32:
130     case PROCESSOR_GENERIC64:
131       gcc_unreachable ();
132     }
133
134   /* Built-ins based on -mtune=.  */
135   switch (tune)
136     {
137     case PROCESSOR_I386:
138       def_or_undef (parse_in, "__tune_i386__");
139       break;
140     case PROCESSOR_I486:
141       def_or_undef (parse_in, "__tune_i486__");
142       break;
143     case PROCESSOR_PENTIUM:
144       def_or_undef (parse_in, "__tune_i586__");
145       def_or_undef (parse_in, "__tune_pentium__");
146       if (last_tune_char == 'x')
147         def_or_undef (parse_in, "__tune_pentium_mmx__");
148       break;
149     case PROCESSOR_PENTIUMPRO:
150       def_or_undef (parse_in, "__tune_i686__");
151       def_or_undef (parse_in, "__tune_pentiumpro__");
152       switch (last_tune_char)
153         {
154         case '3':
155           def_or_undef (parse_in, "__tune_pentium3__");
156           /* FALLTHRU */
157         case '2':
158           def_or_undef (parse_in, "__tune_pentium2__");
159           break;
160         }
161       break;
162     case PROCESSOR_GEODE:
163       def_or_undef (parse_in, "__tune_geode__");
164       break;
165     case PROCESSOR_K6:
166       def_or_undef (parse_in, "__tune_k6__");
167       if (last_tune_char == '2')
168         def_or_undef (parse_in, "__tune_k6_2__");
169       else if (last_tune_char == '3')
170         def_or_undef (parse_in, "__tune_k6_3__");
171       else if (isa_flag & OPTION_MASK_ISA_3DNOW)
172         def_or_undef (parse_in, "__tune_k6_3__");
173       break;
174     case PROCESSOR_ATHLON:
175       def_or_undef (parse_in, "__tune_athlon__");
176       if (isa_flag & OPTION_MASK_ISA_SSE)
177         def_or_undef (parse_in, "__tune_athlon_sse__");
178       break;
179     case PROCESSOR_K8:
180       def_or_undef (parse_in, "__tune_k8__");
181       break;
182     case PROCESSOR_AMDFAM10:
183       def_or_undef (parse_in, "__tune_amdfam10__");
184       break;
185     case PROCESSOR_PENTIUM4:
186       def_or_undef (parse_in, "__tune_pentium4__");
187       break;
188     case PROCESSOR_NOCONA:
189       def_or_undef (parse_in, "__tune_nocona__");
190       break;
191     case PROCESSOR_CORE2:
192       def_or_undef (parse_in, "__tune_core2__");
193       break;
194     case PROCESSOR_ATOM:
195       def_or_undef (parse_in, "__tune_atom__");
196       break;
197     case PROCESSOR_GENERIC32:
198     case PROCESSOR_GENERIC64:
199       break;
200     /* use PROCESSOR_max to not set/unset the tune macro.  */
201     case PROCESSOR_max:
202       break;
203     }
204
205   if (isa_flag & OPTION_MASK_ISA_MMX)
206     def_or_undef (parse_in, "__MMX__");
207   if (isa_flag & OPTION_MASK_ISA_3DNOW)
208     def_or_undef (parse_in, "__3dNOW__");
209   if (isa_flag & OPTION_MASK_ISA_3DNOW_A)
210     def_or_undef (parse_in, "__3dNOW_A__");
211   if (isa_flag & OPTION_MASK_ISA_SSE)
212     def_or_undef (parse_in, "__SSE__");
213   if (isa_flag & OPTION_MASK_ISA_SSE2)
214     def_or_undef (parse_in, "__SSE2__");
215   if (isa_flag & OPTION_MASK_ISA_SSE3)
216     def_or_undef (parse_in, "__SSE3__");
217   if (isa_flag & OPTION_MASK_ISA_SSSE3)
218     def_or_undef (parse_in, "__SSSE3__");
219   if (isa_flag & OPTION_MASK_ISA_SSE4_1)
220     def_or_undef (parse_in, "__SSE4_1__");
221   if (isa_flag & OPTION_MASK_ISA_SSE4_2)
222     def_or_undef (parse_in, "__SSE4_2__");
223   if (isa_flag & OPTION_MASK_ISA_AES)
224     def_or_undef (parse_in, "__AES__");
225   if (isa_flag & OPTION_MASK_ISA_PCLMUL)
226     def_or_undef (parse_in, "__PCLMUL__");
227   if (isa_flag & OPTION_MASK_ISA_AVX)
228     def_or_undef (parse_in, "__AVX__");
229   if (isa_flag & OPTION_MASK_ISA_FMA)
230     def_or_undef (parse_in, "__FMA__");
231   if (isa_flag & OPTION_MASK_ISA_SSE4A)
232     def_or_undef (parse_in, "__SSE4A__");
233   if (isa_flag & OPTION_MASK_ISA_SSE5)
234     def_or_undef (parse_in, "__SSE5__");
235   if ((fpmath & FPMATH_SSE) && (isa_flag & OPTION_MASK_ISA_SSE))
236     def_or_undef (parse_in, "__SSE_MATH__");
237   if ((fpmath & FPMATH_SSE) && (isa_flag & OPTION_MASK_ISA_SSE2))
238     def_or_undef (parse_in, "__SSE2_MATH__");
239 }
240
241 \f
242 /* Hook to validate the current #pragma GCC target and set the state, and
243    update the macros based on what was changed.  If ARGS is NULL, then
244    POP_TARGET is used to reset the options.  */
245
246 static bool
247 ix86_pragma_target_parse (tree args, tree pop_target)
248 {
249   tree prev_tree = build_target_option_node ();
250   tree cur_tree;
251   struct cl_target_option *prev_opt;
252   struct cl_target_option *cur_opt;
253   int prev_isa;
254   int cur_isa;
255   int diff_isa;
256   enum processor_type prev_arch;
257   enum processor_type prev_tune;
258   enum processor_type cur_arch;
259   enum processor_type cur_tune;
260
261   if (! args)
262     {
263       cur_tree = ((pop_target)
264                   ? pop_target
265                   : target_option_default_node);
266       cl_target_option_restore (TREE_TARGET_OPTION (cur_tree));
267     }
268   else
269     {
270       cur_tree = ix86_valid_target_attribute_tree (args);
271       if (!cur_tree)
272         return false;
273     }
274
275   target_option_current_node = cur_tree;
276
277   /* Figure out the previous/current isa, arch, tune and the differences.  */
278   prev_opt  = TREE_TARGET_OPTION (prev_tree);
279   cur_opt   = TREE_TARGET_OPTION (cur_tree);
280   prev_isa  = prev_opt->ix86_isa_flags;
281   cur_isa   = cur_opt->ix86_isa_flags;
282   diff_isa  = (prev_isa ^ cur_isa);
283   prev_arch = (enum processor_type) prev_opt->arch;
284   prev_tune = (enum processor_type) prev_opt->tune;
285   cur_arch  = (enum processor_type) cur_opt->arch;
286   cur_tune  = (enum processor_type) cur_opt->tune;
287
288   /* If the same processor is used for both previous and current options, don't
289      change the macros.  */
290   if (cur_arch == prev_arch)
291     cur_arch = prev_arch = PROCESSOR_max;
292
293   if (cur_tune == prev_tune)
294     cur_tune = prev_tune = PROCESSOR_max;
295
296   /* Undef all of the macros for that are no longer current.  */
297   ix86_target_macros_internal (prev_isa & diff_isa,
298                                prev_arch,
299                                prev_tune,
300                                (enum fpmath_unit) prev_opt->fpmath,
301                                cpp_undef);
302
303   /* Define all of the macros for new options that were just turned on.  */
304   ix86_target_macros_internal (cur_isa & diff_isa,
305                                cur_arch,
306                                cur_tune,
307                                (enum fpmath_unit) cur_opt->fpmath,
308                                cpp_define);
309
310   return true;
311 }
312 \f
313 /* Function to tell the preprocessor about the defines for the current target.  */
314
315 void
316 ix86_target_macros (void)
317 {
318   /* 32/64-bit won't change with target specific options, so do the assert and
319      builtin_define_std calls here.  */
320   if (TARGET_64BIT)
321     {
322       cpp_assert (parse_in, "cpu=x86_64");
323       cpp_assert (parse_in, "machine=x86_64");
324       cpp_define (parse_in, "__amd64");
325       cpp_define (parse_in, "__amd64__");
326       cpp_define (parse_in, "__x86_64");
327       cpp_define (parse_in, "__x86_64__");
328     }
329   else
330     {
331       cpp_assert (parse_in, "cpu=i386");
332       cpp_assert (parse_in, "machine=i386");
333       builtin_define_std ("i386");
334     }
335
336   ix86_target_macros_internal (ix86_isa_flags,
337                                ix86_arch,
338                                ix86_tune,
339                                ix86_fpmath,
340                                cpp_define);
341 }
342
343 \f
344 /* Register target pragmas.  We need to add the hook for parsing #pragma GCC
345    option here rather than in i386.c since it will pull in various preprocessor
346    functions, and those are not present in languages like fortran without a
347    preprocessor.  */
348
349 void
350 ix86_register_pragmas (void)
351 {
352   /* Update pragma hook to allow parsing #pragma GCC target.  */
353   targetm.target_option.pragma_parse = ix86_pragma_target_parse;
354
355 #ifdef REGISTER_SUBTARGET_PRAGMAS
356   REGISTER_SUBTARGET_PRAGMAS ();
357 #endif
358 }