1 /* Subroutines used for macro/preprocessor support on the ia-32. 2 Copyright (C) 2008, 2009, 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 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 "tree.h" 26 #include "tm_p.h" 27 #include "flags.h" 28 #include "c-family/c-common.h" 29 #include "ggc.h" 30 #include "target.h" 31 #include "target-def.h" 32 #include "cpplib.h" 33 #include "c-family/c-pragma.h" 34 35 static bool ix86_pragma_target_parse (tree, tree); 36 static void ix86_target_macros_internal 37 (HOST_WIDE_INT, enum processor_type, enum processor_type, enum fpmath_unit, 38 void (*def_or_undef) (cpp_reader *, const char *)); 39 40 41 /* Internal function to either define or undef the appropriate system 42 macros. */ 43 static void 44 ix86_target_macros_internal (HOST_WIDE_INT isa_flag, 45 enum processor_type arch, 46 enum processor_type tune, 47 enum fpmath_unit fpmath, 48 void (*def_or_undef) (cpp_reader *, 49 const char *)) 50 { 51 /* For some of the k6/pentium varients there weren't seperate ISA bits to 52 identify which tune/arch flag was passed, so figure it out here. */ 53 size_t arch_len = strlen (ix86_arch_string); 54 size_t tune_len = strlen (ix86_tune_string); 55 int last_arch_char = ix86_arch_string[arch_len - 1]; 56 int last_tune_char = ix86_tune_string[tune_len - 1]; 57 58 /* Built-ins based on -march=. */ 59 switch (arch) 60 { 61 case PROCESSOR_I386: 62 break; 63 case PROCESSOR_I486: 64 def_or_undef (parse_in, "__i486"); 65 def_or_undef (parse_in, "__i486__"); 66 break; 67 case PROCESSOR_PENTIUM: 68 def_or_undef (parse_in, "__i586"); 69 def_or_undef (parse_in, "__i586__"); 70 def_or_undef (parse_in, "__pentium"); 71 def_or_undef (parse_in, "__pentium__"); 72 if (isa_flag & OPTION_MASK_ISA_MMX) 73 def_or_undef (parse_in, "__pentium_mmx__"); 74 break; 75 case PROCESSOR_PENTIUMPRO: 76 def_or_undef (parse_in, "__i686"); 77 def_or_undef (parse_in, "__i686__"); 78 def_or_undef (parse_in, "__pentiumpro"); 79 def_or_undef (parse_in, "__pentiumpro__"); 80 break; 81 case PROCESSOR_GEODE: 82 def_or_undef (parse_in, "__geode"); 83 def_or_undef (parse_in, "__geode__"); 84 break; 85 case PROCESSOR_K6: 86 def_or_undef (parse_in, "__k6"); 87 def_or_undef (parse_in, "__k6__"); 88 if (last_arch_char == '2') 89 def_or_undef (parse_in, "__k6_2__"); 90 else if (last_arch_char == '3') 91 def_or_undef (parse_in, "__k6_3__"); 92 else if (isa_flag & OPTION_MASK_ISA_3DNOW) 93 def_or_undef (parse_in, "__k6_3__"); 94 break; 95 case PROCESSOR_ATHLON: 96 def_or_undef (parse_in, "__athlon"); 97 def_or_undef (parse_in, "__athlon__"); 98 if (isa_flag & OPTION_MASK_ISA_SSE) 99 def_or_undef (parse_in, "__athlon_sse__"); 100 break; 101 case PROCESSOR_K8: 102 def_or_undef (parse_in, "__k8"); 103 def_or_undef (parse_in, "__k8__"); 104 break; 105 case PROCESSOR_AMDFAM10: 106 def_or_undef (parse_in, "__amdfam10"); 107 def_or_undef (parse_in, "__amdfam10__"); 108 break; 109 case PROCESSOR_BDVER1: 110 def_or_undef (parse_in, "__bdver1"); 111 def_or_undef (parse_in, "__bdver1__"); 112 break; 113 case PROCESSOR_BDVER2: 114 def_or_undef (parse_in, "__bdver2"); 115 def_or_undef (parse_in, "__bdver2__"); 116 break; 117 case PROCESSOR_BTVER1: 118 def_or_undef (parse_in, "__btver1"); 119 def_or_undef (parse_in, "__btver1__"); 120 break; 121 case PROCESSOR_PENTIUM4: 122 def_or_undef (parse_in, "__pentium4"); 123 def_or_undef (parse_in, "__pentium4__"); 124 break; 125 case PROCESSOR_NOCONA: 126 def_or_undef (parse_in, "__nocona"); 127 def_or_undef (parse_in, "__nocona__"); 128 break; 129 case PROCESSOR_CORE2_32: 130 case PROCESSOR_CORE2_64: 131 def_or_undef (parse_in, "__core2"); 132 def_or_undef (parse_in, "__core2__"); 133 break; 134 case PROCESSOR_COREI7_32: 135 case PROCESSOR_COREI7_64: 136 def_or_undef (parse_in, "__corei7"); 137 def_or_undef (parse_in, "__corei7__"); 138 break; 139 case PROCESSOR_ATOM: 140 def_or_undef (parse_in, "__atom"); 141 def_or_undef (parse_in, "__atom__"); 142 break; 143 /* use PROCESSOR_max to not set/unset the arch macro. */ 144 case PROCESSOR_max: 145 break; 146 case PROCESSOR_GENERIC32: 147 case PROCESSOR_GENERIC64: 148 gcc_unreachable (); 149 } 150 151 /* Built-ins based on -mtune=. */ 152 switch (tune) 153 { 154 case PROCESSOR_I386: 155 def_or_undef (parse_in, "__tune_i386__"); 156 break; 157 case PROCESSOR_I486: 158 def_or_undef (parse_in, "__tune_i486__"); 159 break; 160 case PROCESSOR_PENTIUM: 161 def_or_undef (parse_in, "__tune_i586__"); 162 def_or_undef (parse_in, "__tune_pentium__"); 163 if (last_tune_char == 'x') 164 def_or_undef (parse_in, "__tune_pentium_mmx__"); 165 break; 166 case PROCESSOR_PENTIUMPRO: 167 def_or_undef (parse_in, "__tune_i686__"); 168 def_or_undef (parse_in, "__tune_pentiumpro__"); 169 switch (last_tune_char) 170 { 171 case '3': 172 def_or_undef (parse_in, "__tune_pentium3__"); 173 /* FALLTHRU */ 174 case '2': 175 def_or_undef (parse_in, "__tune_pentium2__"); 176 break; 177 } 178 break; 179 case PROCESSOR_GEODE: 180 def_or_undef (parse_in, "__tune_geode__"); 181 break; 182 case PROCESSOR_K6: 183 def_or_undef (parse_in, "__tune_k6__"); 184 if (last_tune_char == '2') 185 def_or_undef (parse_in, "__tune_k6_2__"); 186 else if (last_tune_char == '3') 187 def_or_undef (parse_in, "__tune_k6_3__"); 188 else if (isa_flag & OPTION_MASK_ISA_3DNOW) 189 def_or_undef (parse_in, "__tune_k6_3__"); 190 break; 191 case PROCESSOR_ATHLON: 192 def_or_undef (parse_in, "__tune_athlon__"); 193 if (isa_flag & OPTION_MASK_ISA_SSE) 194 def_or_undef (parse_in, "__tune_athlon_sse__"); 195 break; 196 case PROCESSOR_K8: 197 def_or_undef (parse_in, "__tune_k8__"); 198 break; 199 case PROCESSOR_AMDFAM10: 200 def_or_undef (parse_in, "__tune_amdfam10__"); 201 break; 202 case PROCESSOR_BDVER1: 203 def_or_undef (parse_in, "__tune_bdver1__"); 204 break; 205 case PROCESSOR_BDVER2: 206 def_or_undef (parse_in, "__tune_bdver2__"); 207 break; 208 case PROCESSOR_BTVER1: 209 def_or_undef (parse_in, "__tune_btver1__"); 210 break; 211 case PROCESSOR_PENTIUM4: 212 def_or_undef (parse_in, "__tune_pentium4__"); 213 break; 214 case PROCESSOR_NOCONA: 215 def_or_undef (parse_in, "__tune_nocona__"); 216 break; 217 case PROCESSOR_CORE2_32: 218 case PROCESSOR_CORE2_64: 219 def_or_undef (parse_in, "__tune_core2__"); 220 break; 221 case PROCESSOR_COREI7_32: 222 case PROCESSOR_COREI7_64: 223 def_or_undef (parse_in, "__tune_corei7__"); 224 break; 225 case PROCESSOR_ATOM: 226 def_or_undef (parse_in, "__tune_atom__"); 227 break; 228 case PROCESSOR_GENERIC32: 229 case PROCESSOR_GENERIC64: 230 break; 231 /* use PROCESSOR_max to not set/unset the tune macro. */ 232 case PROCESSOR_max: 233 break; 234 } 235 236 switch (ix86_cmodel) 237 { 238 case CM_SMALL: 239 case CM_SMALL_PIC: 240 def_or_undef (parse_in, "__code_model_small__"); 241 break; 242 case CM_MEDIUM: 243 case CM_MEDIUM_PIC: 244 def_or_undef (parse_in, "__code_model_medium__"); 245 break; 246 case CM_LARGE: 247 case CM_LARGE_PIC: 248 def_or_undef (parse_in, "__code_model_large__"); 249 break; 250 case CM_32: 251 def_or_undef (parse_in, "__code_model_32__"); 252 break; 253 case CM_KERNEL: 254 def_or_undef (parse_in, "__code_model_kernel__"); 255 break; 256 default: 257 ; 258 } 259 260 if (isa_flag & OPTION_MASK_ISA_MMX) 261 def_or_undef (parse_in, "__MMX__"); 262 if (isa_flag & OPTION_MASK_ISA_3DNOW) 263 def_or_undef (parse_in, "__3dNOW__"); 264 if (isa_flag & OPTION_MASK_ISA_3DNOW_A) 265 def_or_undef (parse_in, "__3dNOW_A__"); 266 if (isa_flag & OPTION_MASK_ISA_SSE) 267 def_or_undef (parse_in, "__SSE__"); 268 if (isa_flag & OPTION_MASK_ISA_SSE2) 269 def_or_undef (parse_in, "__SSE2__"); 270 if (isa_flag & OPTION_MASK_ISA_SSE3) 271 def_or_undef (parse_in, "__SSE3__"); 272 if (isa_flag & OPTION_MASK_ISA_SSSE3) 273 def_or_undef (parse_in, "__SSSE3__"); 274 if (isa_flag & OPTION_MASK_ISA_SSE4_1) 275 def_or_undef (parse_in, "__SSE4_1__"); 276 if (isa_flag & OPTION_MASK_ISA_SSE4_2) 277 def_or_undef (parse_in, "__SSE4_2__"); 278 if (isa_flag & OPTION_MASK_ISA_AES) 279 def_or_undef (parse_in, "__AES__"); 280 if (isa_flag & OPTION_MASK_ISA_PCLMUL) 281 def_or_undef (parse_in, "__PCLMUL__"); 282 if (isa_flag & OPTION_MASK_ISA_AVX) 283 def_or_undef (parse_in, "__AVX__"); 284 if (isa_flag & OPTION_MASK_ISA_AVX2) 285 def_or_undef (parse_in, "__AVX2__"); 286 if (isa_flag & OPTION_MASK_ISA_FMA) 287 def_or_undef (parse_in, "__FMA__"); 288 if (isa_flag & OPTION_MASK_ISA_SSE4A) 289 def_or_undef (parse_in, "__SSE4A__"); 290 if (isa_flag & OPTION_MASK_ISA_FMA4) 291 def_or_undef (parse_in, "__FMA4__"); 292 if (isa_flag & OPTION_MASK_ISA_XOP) 293 def_or_undef (parse_in, "__XOP__"); 294 if (isa_flag & OPTION_MASK_ISA_LWP) 295 def_or_undef (parse_in, "__LWP__"); 296 if (isa_flag & OPTION_MASK_ISA_ABM) 297 def_or_undef (parse_in, "__ABM__"); 298 if (isa_flag & OPTION_MASK_ISA_BMI) 299 def_or_undef (parse_in, "__BMI__"); 300 if (isa_flag & OPTION_MASK_ISA_BMI2) 301 def_or_undef (parse_in, "__BMI2__"); 302 if (isa_flag & OPTION_MASK_ISA_LZCNT) 303 def_or_undef (parse_in, "__LZCNT__"); 304 if (isa_flag & OPTION_MASK_ISA_TBM) 305 def_or_undef (parse_in, "__TBM__"); 306 if (isa_flag & OPTION_MASK_ISA_POPCNT) 307 def_or_undef (parse_in, "__POPCNT__"); 308 if (isa_flag & OPTION_MASK_ISA_FSGSBASE) 309 def_or_undef (parse_in, "__FSGSBASE__"); 310 if (isa_flag & OPTION_MASK_ISA_RDRND) 311 def_or_undef (parse_in, "__RDRND__"); 312 if (isa_flag & OPTION_MASK_ISA_F16C) 313 def_or_undef (parse_in, "__F16C__"); 314 if ((fpmath & FPMATH_SSE) && (isa_flag & OPTION_MASK_ISA_SSE)) 315 def_or_undef (parse_in, "__SSE_MATH__"); 316 if ((fpmath & FPMATH_SSE) && (isa_flag & OPTION_MASK_ISA_SSE2)) 317 def_or_undef (parse_in, "__SSE2_MATH__"); 318 } 319 320 321 /* Hook to validate the current #pragma GCC target and set the state, and 322 update the macros based on what was changed. If ARGS is NULL, then 323 POP_TARGET is used to reset the options. */ 324 325 static bool 326 ix86_pragma_target_parse (tree args, tree pop_target) 327 { 328 tree prev_tree = build_target_option_node (); 329 tree cur_tree; 330 struct cl_target_option *prev_opt; 331 struct cl_target_option *cur_opt; 332 HOST_WIDE_INT prev_isa; 333 HOST_WIDE_INT cur_isa; 334 HOST_WIDE_INT diff_isa; 335 enum processor_type prev_arch; 336 enum processor_type prev_tune; 337 enum processor_type cur_arch; 338 enum processor_type cur_tune; 339 340 if (! args) 341 { 342 cur_tree = ((pop_target) 343 ? pop_target 344 : target_option_default_node); 345 cl_target_option_restore (&global_options, 346 TREE_TARGET_OPTION (cur_tree)); 347 } 348 else 349 { 350 cur_tree = ix86_valid_target_attribute_tree (args); 351 if (!cur_tree) 352 return false; 353 } 354 355 target_option_current_node = cur_tree; 356 357 /* Figure out the previous/current isa, arch, tune and the differences. */ 358 prev_opt = TREE_TARGET_OPTION (prev_tree); 359 cur_opt = TREE_TARGET_OPTION (cur_tree); 360 prev_isa = prev_opt->x_ix86_isa_flags; 361 cur_isa = cur_opt->x_ix86_isa_flags; 362 diff_isa = (prev_isa ^ cur_isa); 363 prev_arch = (enum processor_type) prev_opt->arch; 364 prev_tune = (enum processor_type) prev_opt->tune; 365 cur_arch = (enum processor_type) cur_opt->arch; 366 cur_tune = (enum processor_type) cur_opt->tune; 367 368 /* If the same processor is used for both previous and current options, don't 369 change the macros. */ 370 if (cur_arch == prev_arch) 371 cur_arch = prev_arch = PROCESSOR_max; 372 373 if (cur_tune == prev_tune) 374 cur_tune = prev_tune = PROCESSOR_max; 375 376 /* Undef all of the macros for that are no longer current. */ 377 ix86_target_macros_internal (prev_isa & diff_isa, 378 prev_arch, 379 prev_tune, 380 (enum fpmath_unit) prev_opt->x_ix86_fpmath, 381 cpp_undef); 382 383 /* Define all of the macros for new options that were just turned on. */ 384 ix86_target_macros_internal (cur_isa & diff_isa, 385 cur_arch, 386 cur_tune, 387 (enum fpmath_unit) cur_opt->x_ix86_fpmath, 388 cpp_define); 389 390 return true; 391 } 392 393 /* Function to tell the preprocessor about the defines for the current target. */ 394 395 void 396 ix86_target_macros (void) 397 { 398 /* 32/64-bit won't change with target specific options, so do the assert and 399 builtin_define_std calls here. */ 400 if (TARGET_64BIT) 401 { 402 cpp_assert (parse_in, "cpu=x86_64"); 403 cpp_assert (parse_in, "machine=x86_64"); 404 cpp_define (parse_in, "__amd64"); 405 cpp_define (parse_in, "__amd64__"); 406 cpp_define (parse_in, "__x86_64"); 407 cpp_define (parse_in, "__x86_64__"); 408 if (TARGET_X32) 409 { 410 cpp_define (parse_in, "_ILP32"); 411 cpp_define (parse_in, "__ILP32__"); 412 } 413 } 414 else 415 { 416 cpp_assert (parse_in, "cpu=i386"); 417 cpp_assert (parse_in, "machine=i386"); 418 builtin_define_std ("i386"); 419 } 420 421 ix86_target_macros_internal (ix86_isa_flags, 422 ix86_arch, 423 ix86_tune, 424 ix86_fpmath, 425 cpp_define); 426 } 427 428 429 /* Register target pragmas. We need to add the hook for parsing #pragma GCC 430 option here rather than in i386.c since it will pull in various preprocessor 431 functions, and those are not present in languages like fortran without a 432 preprocessor. */ 433 434 void 435 ix86_register_pragmas (void) 436 { 437 /* Update pragma hook to allow parsing #pragma GCC target. */ 438 targetm.target_option.pragma_parse = ix86_pragma_target_parse; 439 440 #ifdef REGISTER_SUBTARGET_PRAGMAS 441 REGISTER_SUBTARGET_PRAGMAS (); 442 #endif 443 } 444