1 //===--- Targets.cpp - Implement target feature support -------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements construction of a TargetInfo object from a 10 // target triple. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "Targets.h" 15 16 #include "Targets/AArch64.h" 17 #include "Targets/AMDGPU.h" 18 #include "Targets/ARC.h" 19 #include "Targets/ARM.h" 20 #include "Targets/AVR.h" 21 #include "Targets/BPF.h" 22 #include "Targets/Hexagon.h" 23 #include "Targets/Lanai.h" 24 #include "Targets/Le64.h" 25 #include "Targets/MSP430.h" 26 #include "Targets/Mips.h" 27 #include "Targets/NVPTX.h" 28 #include "Targets/OSTargets.h" 29 #include "Targets/PNaCl.h" 30 #include "Targets/PPC.h" 31 #include "Targets/RISCV.h" 32 #include "Targets/SPIR.h" 33 #include "Targets/Sparc.h" 34 #include "Targets/SystemZ.h" 35 #include "Targets/TCE.h" 36 #include "Targets/WebAssembly.h" 37 #include "Targets/X86.h" 38 #include "Targets/XCore.h" 39 #include "clang/Basic/Diagnostic.h" 40 #include "llvm/ADT/StringExtras.h" 41 #include "llvm/ADT/Triple.h" 42 43 using namespace clang; 44 45 namespace clang { 46 namespace targets { 47 //===----------------------------------------------------------------------===// 48 // Common code shared among targets. 49 //===----------------------------------------------------------------------===// 50 51 /// DefineStd - Define a macro name and standard variants. For example if 52 /// MacroName is "unix", then this will define "__unix", "__unix__", and "unix" 53 /// when in GNU mode. 54 void DefineStd(MacroBuilder &Builder, StringRef MacroName, 55 const LangOptions &Opts) { 56 assert(MacroName[0] != '_' && "Identifier should be in the user's namespace"); 57 58 // If in GNU mode (e.g. -std=gnu99 but not -std=c99) define the raw identifier 59 // in the user's namespace. 60 if (Opts.GNUMode) 61 Builder.defineMacro(MacroName); 62 63 // Define __unix. 64 Builder.defineMacro("__" + MacroName); 65 66 // Define __unix__. 67 Builder.defineMacro("__" + MacroName + "__"); 68 } 69 70 void defineCPUMacros(MacroBuilder &Builder, StringRef CPUName, bool Tuning) { 71 Builder.defineMacro("__" + CPUName); 72 Builder.defineMacro("__" + CPUName + "__"); 73 if (Tuning) 74 Builder.defineMacro("__tune_" + CPUName + "__"); 75 } 76 77 void addCygMingDefines(const LangOptions &Opts, MacroBuilder &Builder) { 78 // Mingw and cygwin define __declspec(a) to __attribute__((a)). Clang 79 // supports __declspec natively under -fms-extensions, but we define a no-op 80 // __declspec macro anyway for pre-processor compatibility. 81 if (Opts.MicrosoftExt) 82 Builder.defineMacro("__declspec", "__declspec"); 83 else 84 Builder.defineMacro("__declspec(a)", "__attribute__((a))"); 85 86 if (!Opts.MicrosoftExt) { 87 // Provide macros for all the calling convention keywords. Provide both 88 // single and double underscore prefixed variants. These are available on 89 // x64 as well as x86, even though they have no effect. 90 const char *CCs[] = {"cdecl", "stdcall", "fastcall", "thiscall", "pascal"}; 91 for (const char *CC : CCs) { 92 std::string GCCSpelling = "__attribute__((__"; 93 GCCSpelling += CC; 94 GCCSpelling += "__))"; 95 Builder.defineMacro(Twine("_") + CC, GCCSpelling); 96 Builder.defineMacro(Twine("__") + CC, GCCSpelling); 97 } 98 } 99 } 100 101 //===----------------------------------------------------------------------===// 102 // Driver code 103 //===----------------------------------------------------------------------===// 104 105 TargetInfo *AllocateTarget(const llvm::Triple &Triple, 106 const TargetOptions &Opts) { 107 llvm::Triple::OSType os = Triple.getOS(); 108 109 switch (Triple.getArch()) { 110 default: 111 return nullptr; 112 113 case llvm::Triple::arc: 114 return new ARCTargetInfo(Triple, Opts); 115 116 case llvm::Triple::xcore: 117 return new XCoreTargetInfo(Triple, Opts); 118 119 case llvm::Triple::hexagon: 120 return new HexagonTargetInfo(Triple, Opts); 121 122 case llvm::Triple::lanai: 123 return new LanaiTargetInfo(Triple, Opts); 124 125 case llvm::Triple::aarch64: 126 if (Triple.isOSDarwin()) 127 return new DarwinAArch64TargetInfo(Triple, Opts); 128 129 switch (os) { 130 case llvm::Triple::CloudABI: 131 return new CloudABITargetInfo<AArch64leTargetInfo>(Triple, Opts); 132 case llvm::Triple::FreeBSD: 133 return new FreeBSDTargetInfo<AArch64leTargetInfo>(Triple, Opts); 134 case llvm::Triple::Fuchsia: 135 return new FuchsiaTargetInfo<AArch64leTargetInfo>(Triple, Opts); 136 case llvm::Triple::Linux: 137 return new LinuxTargetInfo<AArch64leTargetInfo>(Triple, Opts); 138 case llvm::Triple::NetBSD: 139 return new NetBSDTargetInfo<AArch64leTargetInfo>(Triple, Opts); 140 case llvm::Triple::OpenBSD: 141 return new OpenBSDTargetInfo<AArch64leTargetInfo>(Triple, Opts); 142 case llvm::Triple::Win32: 143 switch (Triple.getEnvironment()) { 144 case llvm::Triple::GNU: 145 return new MinGWARM64TargetInfo(Triple, Opts); 146 case llvm::Triple::MSVC: 147 default: // Assume MSVC for unknown environments 148 return new MicrosoftARM64TargetInfo(Triple, Opts); 149 } 150 default: 151 return new AArch64leTargetInfo(Triple, Opts); 152 } 153 154 case llvm::Triple::aarch64_be: 155 switch (os) { 156 case llvm::Triple::FreeBSD: 157 return new FreeBSDTargetInfo<AArch64beTargetInfo>(Triple, Opts); 158 case llvm::Triple::Fuchsia: 159 return new FuchsiaTargetInfo<AArch64beTargetInfo>(Triple, Opts); 160 case llvm::Triple::Linux: 161 return new LinuxTargetInfo<AArch64beTargetInfo>(Triple, Opts); 162 case llvm::Triple::NetBSD: 163 return new NetBSDTargetInfo<AArch64beTargetInfo>(Triple, Opts); 164 default: 165 return new AArch64beTargetInfo(Triple, Opts); 166 } 167 168 case llvm::Triple::arm: 169 case llvm::Triple::thumb: 170 if (Triple.isOSBinFormatMachO()) 171 return new DarwinARMTargetInfo(Triple, Opts); 172 173 switch (os) { 174 case llvm::Triple::CloudABI: 175 return new CloudABITargetInfo<ARMleTargetInfo>(Triple, Opts); 176 case llvm::Triple::Linux: 177 return new LinuxTargetInfo<ARMleTargetInfo>(Triple, Opts); 178 case llvm::Triple::FreeBSD: 179 return new FreeBSDTargetInfo<ARMleTargetInfo>(Triple, Opts); 180 case llvm::Triple::NetBSD: 181 return new NetBSDTargetInfo<ARMleTargetInfo>(Triple, Opts); 182 case llvm::Triple::OpenBSD: 183 return new OpenBSDTargetInfo<ARMleTargetInfo>(Triple, Opts); 184 case llvm::Triple::RTEMS: 185 return new RTEMSTargetInfo<ARMleTargetInfo>(Triple, Opts); 186 case llvm::Triple::NaCl: 187 return new NaClTargetInfo<ARMleTargetInfo>(Triple, Opts); 188 case llvm::Triple::Win32: 189 switch (Triple.getEnvironment()) { 190 case llvm::Triple::Cygnus: 191 return new CygwinARMTargetInfo(Triple, Opts); 192 case llvm::Triple::GNU: 193 return new MinGWARMTargetInfo(Triple, Opts); 194 case llvm::Triple::Itanium: 195 return new ItaniumWindowsARMleTargetInfo(Triple, Opts); 196 case llvm::Triple::MSVC: 197 default: // Assume MSVC for unknown environments 198 return new MicrosoftARMleTargetInfo(Triple, Opts); 199 } 200 default: 201 return new ARMleTargetInfo(Triple, Opts); 202 } 203 204 case llvm::Triple::armeb: 205 case llvm::Triple::thumbeb: 206 if (Triple.isOSDarwin()) 207 return new DarwinARMTargetInfo(Triple, Opts); 208 209 switch (os) { 210 case llvm::Triple::Linux: 211 return new LinuxTargetInfo<ARMbeTargetInfo>(Triple, Opts); 212 case llvm::Triple::FreeBSD: 213 return new FreeBSDTargetInfo<ARMbeTargetInfo>(Triple, Opts); 214 case llvm::Triple::NetBSD: 215 return new NetBSDTargetInfo<ARMbeTargetInfo>(Triple, Opts); 216 case llvm::Triple::OpenBSD: 217 return new OpenBSDTargetInfo<ARMbeTargetInfo>(Triple, Opts); 218 case llvm::Triple::RTEMS: 219 return new RTEMSTargetInfo<ARMbeTargetInfo>(Triple, Opts); 220 case llvm::Triple::NaCl: 221 return new NaClTargetInfo<ARMbeTargetInfo>(Triple, Opts); 222 default: 223 return new ARMbeTargetInfo(Triple, Opts); 224 } 225 226 case llvm::Triple::avr: 227 return new AVRTargetInfo(Triple, Opts); 228 case llvm::Triple::bpfeb: 229 case llvm::Triple::bpfel: 230 return new BPFTargetInfo(Triple, Opts); 231 232 case llvm::Triple::msp430: 233 return new MSP430TargetInfo(Triple, Opts); 234 235 case llvm::Triple::mips: 236 switch (os) { 237 case llvm::Triple::Linux: 238 return new LinuxTargetInfo<MipsTargetInfo>(Triple, Opts); 239 case llvm::Triple::RTEMS: 240 return new RTEMSTargetInfo<MipsTargetInfo>(Triple, Opts); 241 case llvm::Triple::FreeBSD: 242 return new FreeBSDTargetInfo<MipsTargetInfo>(Triple, Opts); 243 case llvm::Triple::NetBSD: 244 return new NetBSDTargetInfo<MipsTargetInfo>(Triple, Opts); 245 default: 246 return new MipsTargetInfo(Triple, Opts); 247 } 248 249 case llvm::Triple::mipsel: 250 switch (os) { 251 case llvm::Triple::Linux: 252 return new LinuxTargetInfo<MipsTargetInfo>(Triple, Opts); 253 case llvm::Triple::RTEMS: 254 return new RTEMSTargetInfo<MipsTargetInfo>(Triple, Opts); 255 case llvm::Triple::FreeBSD: 256 return new FreeBSDTargetInfo<MipsTargetInfo>(Triple, Opts); 257 case llvm::Triple::NetBSD: 258 return new NetBSDTargetInfo<MipsTargetInfo>(Triple, Opts); 259 case llvm::Triple::NaCl: 260 return new NaClTargetInfo<NaClMips32TargetInfo>(Triple, Opts); 261 default: 262 return new MipsTargetInfo(Triple, Opts); 263 } 264 265 case llvm::Triple::mips64: 266 switch (os) { 267 case llvm::Triple::Linux: 268 return new LinuxTargetInfo<MipsTargetInfo>(Triple, Opts); 269 case llvm::Triple::RTEMS: 270 return new RTEMSTargetInfo<MipsTargetInfo>(Triple, Opts); 271 case llvm::Triple::FreeBSD: 272 return new FreeBSDTargetInfo<MipsTargetInfo>(Triple, Opts); 273 case llvm::Triple::NetBSD: 274 return new NetBSDTargetInfo<MipsTargetInfo>(Triple, Opts); 275 case llvm::Triple::OpenBSD: 276 return new OpenBSDTargetInfo<MipsTargetInfo>(Triple, Opts); 277 default: 278 return new MipsTargetInfo(Triple, Opts); 279 } 280 281 case llvm::Triple::mips64el: 282 switch (os) { 283 case llvm::Triple::Linux: 284 return new LinuxTargetInfo<MipsTargetInfo>(Triple, Opts); 285 case llvm::Triple::RTEMS: 286 return new RTEMSTargetInfo<MipsTargetInfo>(Triple, Opts); 287 case llvm::Triple::FreeBSD: 288 return new FreeBSDTargetInfo<MipsTargetInfo>(Triple, Opts); 289 case llvm::Triple::NetBSD: 290 return new NetBSDTargetInfo<MipsTargetInfo>(Triple, Opts); 291 case llvm::Triple::OpenBSD: 292 return new OpenBSDTargetInfo<MipsTargetInfo>(Triple, Opts); 293 default: 294 return new MipsTargetInfo(Triple, Opts); 295 } 296 297 case llvm::Triple::le32: 298 switch (os) { 299 case llvm::Triple::NaCl: 300 return new NaClTargetInfo<PNaClTargetInfo>(Triple, Opts); 301 default: 302 return nullptr; 303 } 304 305 case llvm::Triple::le64: 306 return new Le64TargetInfo(Triple, Opts); 307 308 case llvm::Triple::ppc: 309 if (Triple.isOSDarwin()) 310 return new DarwinPPC32TargetInfo(Triple, Opts); 311 switch (os) { 312 case llvm::Triple::Linux: 313 return new LinuxTargetInfo<PPC32TargetInfo>(Triple, Opts); 314 case llvm::Triple::FreeBSD: 315 return new FreeBSDTargetInfo<PPC32TargetInfo>(Triple, Opts); 316 case llvm::Triple::NetBSD: 317 return new NetBSDTargetInfo<PPC32TargetInfo>(Triple, Opts); 318 case llvm::Triple::OpenBSD: 319 return new OpenBSDTargetInfo<PPC32TargetInfo>(Triple, Opts); 320 case llvm::Triple::RTEMS: 321 return new RTEMSTargetInfo<PPC32TargetInfo>(Triple, Opts); 322 case llvm::Triple::AIX: 323 return new AIXPPC32TargetInfo(Triple, Opts); 324 default: 325 return new PPC32TargetInfo(Triple, Opts); 326 } 327 328 case llvm::Triple::ppc64: 329 if (Triple.isOSDarwin()) 330 return new DarwinPPC64TargetInfo(Triple, Opts); 331 switch (os) { 332 case llvm::Triple::Linux: 333 return new LinuxTargetInfo<PPC64TargetInfo>(Triple, Opts); 334 case llvm::Triple::Lv2: 335 return new PS3PPUTargetInfo<PPC64TargetInfo>(Triple, Opts); 336 case llvm::Triple::FreeBSD: 337 return new FreeBSDTargetInfo<PPC64TargetInfo>(Triple, Opts); 338 case llvm::Triple::NetBSD: 339 return new NetBSDTargetInfo<PPC64TargetInfo>(Triple, Opts); 340 case llvm::Triple::AIX: 341 return new AIXPPC64TargetInfo(Triple, Opts); 342 default: 343 return new PPC64TargetInfo(Triple, Opts); 344 } 345 346 case llvm::Triple::ppc64le: 347 switch (os) { 348 case llvm::Triple::Linux: 349 return new LinuxTargetInfo<PPC64TargetInfo>(Triple, Opts); 350 case llvm::Triple::NetBSD: 351 return new NetBSDTargetInfo<PPC64TargetInfo>(Triple, Opts); 352 default: 353 return new PPC64TargetInfo(Triple, Opts); 354 } 355 356 case llvm::Triple::nvptx: 357 return new NVPTXTargetInfo(Triple, Opts, /*TargetPointerWidth=*/32); 358 case llvm::Triple::nvptx64: 359 return new NVPTXTargetInfo(Triple, Opts, /*TargetPointerWidth=*/64); 360 361 case llvm::Triple::amdgcn: 362 case llvm::Triple::r600: 363 return new AMDGPUTargetInfo(Triple, Opts); 364 365 case llvm::Triple::riscv32: 366 // TODO: add cases for NetBSD, RTEMS once tested. 367 switch (os) { 368 case llvm::Triple::FreeBSD: 369 return new FreeBSDTargetInfo<RISCV32TargetInfo>(Triple, Opts); 370 case llvm::Triple::Linux: 371 return new LinuxTargetInfo<RISCV32TargetInfo>(Triple, Opts); 372 default: 373 return new RISCV32TargetInfo(Triple, Opts); 374 } 375 376 case llvm::Triple::riscv64: 377 // TODO: add cases for NetBSD, RTEMS once tested. 378 switch (os) { 379 case llvm::Triple::FreeBSD: 380 return new FreeBSDTargetInfo<RISCV64TargetInfo>(Triple, Opts); 381 case llvm::Triple::Linux: 382 return new LinuxTargetInfo<RISCV64TargetInfo>(Triple, Opts); 383 default: 384 return new RISCV64TargetInfo(Triple, Opts); 385 } 386 387 case llvm::Triple::sparc: 388 switch (os) { 389 case llvm::Triple::Linux: 390 return new LinuxTargetInfo<SparcV8TargetInfo>(Triple, Opts); 391 case llvm::Triple::Solaris: 392 return new SolarisTargetInfo<SparcV8TargetInfo>(Triple, Opts); 393 case llvm::Triple::NetBSD: 394 return new NetBSDTargetInfo<SparcV8TargetInfo>(Triple, Opts); 395 case llvm::Triple::OpenBSD: 396 return new OpenBSDTargetInfo<SparcV8TargetInfo>(Triple, Opts); 397 case llvm::Triple::RTEMS: 398 return new RTEMSTargetInfo<SparcV8TargetInfo>(Triple, Opts); 399 default: 400 return new SparcV8TargetInfo(Triple, Opts); 401 } 402 403 // The 'sparcel' architecture copies all the above cases except for Solaris. 404 case llvm::Triple::sparcel: 405 switch (os) { 406 case llvm::Triple::Linux: 407 return new LinuxTargetInfo<SparcV8elTargetInfo>(Triple, Opts); 408 case llvm::Triple::NetBSD: 409 return new NetBSDTargetInfo<SparcV8elTargetInfo>(Triple, Opts); 410 case llvm::Triple::OpenBSD: 411 return new OpenBSDTargetInfo<SparcV8elTargetInfo>(Triple, Opts); 412 case llvm::Triple::RTEMS: 413 return new RTEMSTargetInfo<SparcV8elTargetInfo>(Triple, Opts); 414 default: 415 return new SparcV8elTargetInfo(Triple, Opts); 416 } 417 418 case llvm::Triple::sparcv9: 419 switch (os) { 420 case llvm::Triple::Linux: 421 return new LinuxTargetInfo<SparcV9TargetInfo>(Triple, Opts); 422 case llvm::Triple::Solaris: 423 return new SolarisTargetInfo<SparcV9TargetInfo>(Triple, Opts); 424 case llvm::Triple::NetBSD: 425 return new NetBSDTargetInfo<SparcV9TargetInfo>(Triple, Opts); 426 case llvm::Triple::OpenBSD: 427 return new OpenBSDTargetInfo<SparcV9TargetInfo>(Triple, Opts); 428 case llvm::Triple::FreeBSD: 429 return new FreeBSDTargetInfo<SparcV9TargetInfo>(Triple, Opts); 430 default: 431 return new SparcV9TargetInfo(Triple, Opts); 432 } 433 434 case llvm::Triple::systemz: 435 switch (os) { 436 case llvm::Triple::Linux: 437 return new LinuxTargetInfo<SystemZTargetInfo>(Triple, Opts); 438 default: 439 return new SystemZTargetInfo(Triple, Opts); 440 } 441 442 case llvm::Triple::tce: 443 return new TCETargetInfo(Triple, Opts); 444 445 case llvm::Triple::tcele: 446 return new TCELETargetInfo(Triple, Opts); 447 448 case llvm::Triple::x86: 449 if (Triple.isOSDarwin()) 450 return new DarwinI386TargetInfo(Triple, Opts); 451 452 switch (os) { 453 case llvm::Triple::Ananas: 454 return new AnanasTargetInfo<X86_32TargetInfo>(Triple, Opts); 455 case llvm::Triple::CloudABI: 456 return new CloudABITargetInfo<X86_32TargetInfo>(Triple, Opts); 457 case llvm::Triple::Linux: { 458 switch (Triple.getEnvironment()) { 459 default: 460 return new LinuxTargetInfo<X86_32TargetInfo>(Triple, Opts); 461 case llvm::Triple::Android: 462 return new AndroidX86_32TargetInfo(Triple, Opts); 463 } 464 } 465 case llvm::Triple::DragonFly: 466 return new DragonFlyBSDTargetInfo<X86_32TargetInfo>(Triple, Opts); 467 case llvm::Triple::NetBSD: 468 return new NetBSDI386TargetInfo(Triple, Opts); 469 case llvm::Triple::OpenBSD: 470 return new OpenBSDI386TargetInfo(Triple, Opts); 471 case llvm::Triple::FreeBSD: 472 return new FreeBSDTargetInfo<X86_32TargetInfo>(Triple, Opts); 473 case llvm::Triple::KFreeBSD: 474 return new KFreeBSDTargetInfo<X86_32TargetInfo>(Triple, Opts); 475 case llvm::Triple::Minix: 476 return new MinixTargetInfo<X86_32TargetInfo>(Triple, Opts); 477 case llvm::Triple::Solaris: 478 return new SolarisTargetInfo<X86_32TargetInfo>(Triple, Opts); 479 case llvm::Triple::Win32: { 480 switch (Triple.getEnvironment()) { 481 case llvm::Triple::Cygnus: 482 return new CygwinX86_32TargetInfo(Triple, Opts); 483 case llvm::Triple::GNU: 484 return new MinGWX86_32TargetInfo(Triple, Opts); 485 case llvm::Triple::Itanium: 486 case llvm::Triple::MSVC: 487 default: // Assume MSVC for unknown environments 488 return new MicrosoftX86_32TargetInfo(Triple, Opts); 489 } 490 } 491 case llvm::Triple::Haiku: 492 return new HaikuX86_32TargetInfo(Triple, Opts); 493 case llvm::Triple::RTEMS: 494 return new RTEMSX86_32TargetInfo(Triple, Opts); 495 case llvm::Triple::NaCl: 496 return new NaClTargetInfo<X86_32TargetInfo>(Triple, Opts); 497 case llvm::Triple::ELFIAMCU: 498 return new MCUX86_32TargetInfo(Triple, Opts); 499 case llvm::Triple::Hurd: 500 return new HurdTargetInfo<X86_32TargetInfo>(Triple, Opts); 501 default: 502 return new X86_32TargetInfo(Triple, Opts); 503 } 504 505 case llvm::Triple::x86_64: 506 if (Triple.isOSDarwin() || Triple.isOSBinFormatMachO()) 507 return new DarwinX86_64TargetInfo(Triple, Opts); 508 509 switch (os) { 510 case llvm::Triple::Ananas: 511 return new AnanasTargetInfo<X86_64TargetInfo>(Triple, Opts); 512 case llvm::Triple::CloudABI: 513 return new CloudABITargetInfo<X86_64TargetInfo>(Triple, Opts); 514 case llvm::Triple::Linux: { 515 switch (Triple.getEnvironment()) { 516 default: 517 return new LinuxTargetInfo<X86_64TargetInfo>(Triple, Opts); 518 case llvm::Triple::Android: 519 return new AndroidX86_64TargetInfo(Triple, Opts); 520 } 521 } 522 case llvm::Triple::DragonFly: 523 return new DragonFlyBSDTargetInfo<X86_64TargetInfo>(Triple, Opts); 524 case llvm::Triple::NetBSD: 525 return new NetBSDTargetInfo<X86_64TargetInfo>(Triple, Opts); 526 case llvm::Triple::OpenBSD: 527 return new OpenBSDX86_64TargetInfo(Triple, Opts); 528 case llvm::Triple::FreeBSD: 529 return new FreeBSDTargetInfo<X86_64TargetInfo>(Triple, Opts); 530 case llvm::Triple::Fuchsia: 531 return new FuchsiaTargetInfo<X86_64TargetInfo>(Triple, Opts); 532 case llvm::Triple::KFreeBSD: 533 return new KFreeBSDTargetInfo<X86_64TargetInfo>(Triple, Opts); 534 case llvm::Triple::Solaris: 535 return new SolarisTargetInfo<X86_64TargetInfo>(Triple, Opts); 536 case llvm::Triple::Win32: { 537 switch (Triple.getEnvironment()) { 538 case llvm::Triple::Cygnus: 539 return new CygwinX86_64TargetInfo(Triple, Opts); 540 case llvm::Triple::GNU: 541 return new MinGWX86_64TargetInfo(Triple, Opts); 542 case llvm::Triple::MSVC: 543 default: // Assume MSVC for unknown environments 544 return new MicrosoftX86_64TargetInfo(Triple, Opts); 545 } 546 } 547 case llvm::Triple::Haiku: 548 return new HaikuTargetInfo<X86_64TargetInfo>(Triple, Opts); 549 case llvm::Triple::NaCl: 550 return new NaClTargetInfo<X86_64TargetInfo>(Triple, Opts); 551 case llvm::Triple::PS4: 552 return new PS4OSTargetInfo<X86_64TargetInfo>(Triple, Opts); 553 default: 554 return new X86_64TargetInfo(Triple, Opts); 555 } 556 557 case llvm::Triple::spir: { 558 if (Triple.getOS() != llvm::Triple::UnknownOS || 559 Triple.getEnvironment() != llvm::Triple::UnknownEnvironment) 560 return nullptr; 561 return new SPIR32TargetInfo(Triple, Opts); 562 } 563 case llvm::Triple::spir64: { 564 if (Triple.getOS() != llvm::Triple::UnknownOS || 565 Triple.getEnvironment() != llvm::Triple::UnknownEnvironment) 566 return nullptr; 567 return new SPIR64TargetInfo(Triple, Opts); 568 } 569 case llvm::Triple::wasm32: 570 if (Triple.getSubArch() != llvm::Triple::NoSubArch || 571 Triple.getVendor() != llvm::Triple::UnknownVendor || 572 !Triple.isOSBinFormatWasm()) 573 return nullptr; 574 switch (Triple.getOS()) { 575 case llvm::Triple::WASI: 576 return new WASITargetInfo<WebAssembly32TargetInfo>(Triple, Opts); 577 case llvm::Triple::Emscripten: 578 return new EmscriptenTargetInfo<WebAssembly32TargetInfo>(Triple, Opts); 579 case llvm::Triple::UnknownOS: 580 return new WebAssemblyOSTargetInfo<WebAssembly32TargetInfo>(Triple, Opts); 581 default: 582 return nullptr; 583 } 584 case llvm::Triple::wasm64: 585 if (Triple.getSubArch() != llvm::Triple::NoSubArch || 586 Triple.getVendor() != llvm::Triple::UnknownVendor || 587 !Triple.isOSBinFormatWasm()) 588 return nullptr; 589 switch (Triple.getOS()) { 590 case llvm::Triple::WASI: 591 return new WASITargetInfo<WebAssembly64TargetInfo>(Triple, Opts); 592 case llvm::Triple::Emscripten: 593 return new EmscriptenTargetInfo<WebAssembly64TargetInfo>(Triple, Opts); 594 case llvm::Triple::UnknownOS: 595 return new WebAssemblyOSTargetInfo<WebAssembly64TargetInfo>(Triple, Opts); 596 default: 597 return nullptr; 598 } 599 600 case llvm::Triple::renderscript32: 601 return new LinuxTargetInfo<RenderScript32TargetInfo>(Triple, Opts); 602 case llvm::Triple::renderscript64: 603 return new LinuxTargetInfo<RenderScript64TargetInfo>(Triple, Opts); 604 } 605 } 606 } // namespace targets 607 } // namespace clang 608 609 using namespace clang::targets; 610 /// CreateTargetInfo - Return the target info object for the specified target 611 /// options. 612 TargetInfo * 613 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags, 614 const std::shared_ptr<TargetOptions> &Opts) { 615 llvm::Triple Triple(Opts->Triple); 616 617 // Construct the target 618 std::unique_ptr<TargetInfo> Target(AllocateTarget(Triple, *Opts)); 619 if (!Target) { 620 Diags.Report(diag::err_target_unknown_triple) << Triple.str(); 621 return nullptr; 622 } 623 Target->TargetOpts = Opts; 624 625 // Set the target CPU if specified. 626 if (!Opts->CPU.empty() && !Target->setCPU(Opts->CPU)) { 627 Diags.Report(diag::err_target_unknown_cpu) << Opts->CPU; 628 SmallVector<StringRef, 32> ValidList; 629 Target->fillValidCPUList(ValidList); 630 if (!ValidList.empty()) 631 Diags.Report(diag::note_valid_options) << llvm::join(ValidList, ", "); 632 return nullptr; 633 } 634 635 // Set the target ABI if specified. 636 if (!Opts->ABI.empty() && !Target->setABI(Opts->ABI)) { 637 Diags.Report(diag::err_target_unknown_abi) << Opts->ABI; 638 return nullptr; 639 } 640 641 // Set the fp math unit. 642 if (!Opts->FPMath.empty() && !Target->setFPMath(Opts->FPMath)) { 643 Diags.Report(diag::err_target_unknown_fpmath) << Opts->FPMath; 644 return nullptr; 645 } 646 647 // Compute the default target features, we need the target to handle this 648 // because features may have dependencies on one another. 649 llvm::StringMap<bool> Features; 650 if (!Target->initFeatureMap(Features, Diags, Opts->CPU, 651 Opts->FeaturesAsWritten)) 652 return nullptr; 653 654 // Add the features to the compile options. 655 Opts->Features.clear(); 656 for (const auto &F : Features) 657 Opts->Features.push_back((F.getValue() ? "+" : "-") + F.getKey().str()); 658 // Sort here, so we handle the features in a predictable order. (This matters 659 // when we're dealing with features that overlap.) 660 llvm::sort(Opts->Features); 661 662 if (!Target->handleTargetFeatures(Opts->Features, Diags)) 663 return nullptr; 664 665 Target->setSupportedOpenCLOpts(); 666 Target->setOpenCLExtensionOpts(); 667 Target->setMaxAtomicWidth(); 668 669 if (!Target->validateTarget(Diags)) 670 return nullptr; 671 672 Target->CheckFixedPointBits(); 673 674 return Target.release(); 675 } 676