1 //===-- llvm/ADT/Triple.h - Target triple helper class ----------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLVM_ADT_TRIPLE_H 11 #define LLVM_ADT_TRIPLE_H 12 13 #include "llvm/ADT/Twine.h" 14 15 // Some system headers or GCC predefined macros conflict with identifiers in 16 // this file. Undefine them here. 17 #undef NetBSD 18 #undef mips 19 #undef sparc 20 21 namespace llvm { 22 23 /// Triple - Helper class for working with autoconf configuration names. For 24 /// historical reasons, we also call these 'triples' (they used to contain 25 /// exactly three fields). 26 /// 27 /// Configuration names are strings in the canonical form: 28 /// ARCHITECTURE-VENDOR-OPERATING_SYSTEM 29 /// or 30 /// ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT 31 /// 32 /// This class is used for clients which want to support arbitrary 33 /// configuration names, but also want to implement certain special 34 /// behavior for particular configurations. This class isolates the mapping 35 /// from the components of the configuration name to well known IDs. 36 /// 37 /// At its core the Triple class is designed to be a wrapper for a triple 38 /// string; the constructor does not change or normalize the triple string. 39 /// Clients that need to handle the non-canonical triples that users often 40 /// specify should use the normalize method. 41 /// 42 /// See autoconf/config.guess for a glimpse into what configuration names 43 /// look like in practice. 44 class Triple { 45 public: 46 enum ArchType { 47 UnknownArch, 48 49 arm, // ARM (little endian): arm, armv.*, xscale 50 armeb, // ARM (big endian): armeb 51 aarch64, // AArch64 (little endian): aarch64 52 aarch64_be, // AArch64 (big endian): aarch64_be 53 avr, // AVR: Atmel AVR microcontroller 54 bpfel, // eBPF or extended BPF or 64-bit BPF (little endian) 55 bpfeb, // eBPF or extended BPF or 64-bit BPF (big endian) 56 hexagon, // Hexagon: hexagon 57 mips, // MIPS: mips, mipsallegrex 58 mipsel, // MIPSEL: mipsel, mipsallegrexel 59 mips64, // MIPS64: mips64 60 mips64el, // MIPS64EL: mips64el 61 msp430, // MSP430: msp430 62 ppc, // PPC: powerpc 63 ppc64, // PPC64: powerpc64, ppu 64 ppc64le, // PPC64LE: powerpc64le 65 r600, // R600: AMD GPUs HD2XXX - HD6XXX 66 amdgcn, // AMDGCN: AMD GCN GPUs 67 sparc, // Sparc: sparc 68 sparcv9, // Sparcv9: Sparcv9 69 sparcel, // Sparc: (endianness = little). NB: 'Sparcle' is a CPU variant 70 systemz, // SystemZ: s390x 71 tce, // TCE (http://tce.cs.tut.fi/): tce 72 thumb, // Thumb (little endian): thumb, thumbv.* 73 thumbeb, // Thumb (big endian): thumbeb 74 x86, // X86: i[3-9]86 75 x86_64, // X86-64: amd64, x86_64 76 xcore, // XCore: xcore 77 nvptx, // NVPTX: 32-bit 78 nvptx64, // NVPTX: 64-bit 79 le32, // le32: generic little-endian 32-bit CPU (PNaCl) 80 le64, // le64: generic little-endian 64-bit CPU (PNaCl) 81 amdil, // AMDIL 82 amdil64, // AMDIL with 64-bit pointers 83 hsail, // AMD HSAIL 84 hsail64, // AMD HSAIL with 64-bit pointers 85 spir, // SPIR: standard portable IR for OpenCL 32-bit version 86 spir64, // SPIR: standard portable IR for OpenCL 64-bit version 87 kalimba, // Kalimba: generic kalimba 88 shave, // SHAVE: Movidius vector VLIW processors 89 wasm32, // WebAssembly with 32-bit pointers 90 wasm64, // WebAssembly with 64-bit pointers 91 LastArchType = wasm64 92 }; 93 enum SubArchType { 94 NoSubArch, 95 96 ARMSubArch_v8_2a, 97 ARMSubArch_v8_1a, 98 ARMSubArch_v8, 99 ARMSubArch_v7, 100 ARMSubArch_v7em, 101 ARMSubArch_v7m, 102 ARMSubArch_v7s, 103 ARMSubArch_v7k, 104 ARMSubArch_v6, 105 ARMSubArch_v6m, 106 ARMSubArch_v6k, 107 ARMSubArch_v6t2, 108 ARMSubArch_v5, 109 ARMSubArch_v5te, 110 ARMSubArch_v4t, 111 112 KalimbaSubArch_v3, 113 KalimbaSubArch_v4, 114 KalimbaSubArch_v5 115 }; 116 enum VendorType { 117 UnknownVendor, 118 119 Apple, 120 PC, 121 SCEI, 122 BGP, 123 BGQ, 124 Freescale, 125 IBM, 126 ImaginationTechnologies, 127 MipsTechnologies, 128 NVIDIA, 129 CSR, 130 Myriad, 131 LastVendorType = Myriad 132 }; 133 enum OSType { 134 UnknownOS, 135 136 CloudABI, 137 Darwin, 138 DragonFly, 139 FreeBSD, 140 IOS, 141 KFreeBSD, 142 Linux, 143 Lv2, // PS3 144 MacOSX, 145 NetBSD, 146 OpenBSD, 147 Solaris, 148 Win32, 149 Haiku, 150 Minix, 151 RTEMS, 152 NaCl, // Native Client 153 CNK, // BG/P Compute-Node Kernel 154 Bitrig, 155 AIX, 156 CUDA, // NVIDIA CUDA 157 NVCL, // NVIDIA OpenCL 158 AMDHSA, // AMD HSA Runtime 159 PS4, 160 ELFIAMCU, 161 TvOS, // Apple tvOS 162 WatchOS, // Apple watchOS 163 LastOSType = WatchOS 164 }; 165 enum EnvironmentType { 166 UnknownEnvironment, 167 168 GNU, 169 GNUEABI, 170 GNUEABIHF, 171 GNUX32, 172 CODE16, 173 EABI, 174 EABIHF, 175 Android, 176 177 MSVC, 178 Itanium, 179 Cygnus, 180 AMDOpenCL, 181 CoreCLR, 182 LastEnvironmentType = CoreCLR 183 }; 184 enum ObjectFormatType { 185 UnknownObjectFormat, 186 187 COFF, 188 ELF, 189 MachO, 190 }; 191 192 private: 193 std::string Data; 194 195 /// The parsed arch type. 196 ArchType Arch; 197 198 /// The parsed subarchitecture type. 199 SubArchType SubArch; 200 201 /// The parsed vendor type. 202 VendorType Vendor; 203 204 /// The parsed OS type. 205 OSType OS; 206 207 /// The parsed Environment type. 208 EnvironmentType Environment; 209 210 /// The object format type. 211 ObjectFormatType ObjectFormat; 212 213 public: 214 /// @name Constructors 215 /// @{ 216 217 /// Default constructor is the same as an empty string and leaves all 218 /// triple fields unknown. Triple()219 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {} 220 221 explicit Triple(const Twine &Str); 222 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr); 223 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr, 224 const Twine &EnvironmentStr); 225 226 bool operator==(const Triple &Other) const { 227 return Arch == Other.Arch && SubArch == Other.SubArch && 228 Vendor == Other.Vendor && OS == Other.OS && 229 Environment == Other.Environment && 230 ObjectFormat == Other.ObjectFormat; 231 } 232 233 /// @} 234 /// @name Normalization 235 /// @{ 236 237 /// normalize - Turn an arbitrary machine specification into the canonical 238 /// triple form (or something sensible that the Triple class understands if 239 /// nothing better can reasonably be done). In particular, it handles the 240 /// common case in which otherwise valid components are in the wrong order. 241 static std::string normalize(StringRef Str); 242 243 /// Return the normalized form of this triple's string. normalize()244 std::string normalize() const { return normalize(Data); } 245 246 /// @} 247 /// @name Typed Component Access 248 /// @{ 249 250 /// getArch - Get the parsed architecture type of this triple. getArch()251 ArchType getArch() const { return Arch; } 252 253 /// getSubArch - get the parsed subarchitecture type for this triple. getSubArch()254 SubArchType getSubArch() const { return SubArch; } 255 256 /// getVendor - Get the parsed vendor type of this triple. getVendor()257 VendorType getVendor() const { return Vendor; } 258 259 /// getOS - Get the parsed operating system type of this triple. getOS()260 OSType getOS() const { return OS; } 261 262 /// hasEnvironment - Does this triple have the optional environment 263 /// (fourth) component? hasEnvironment()264 bool hasEnvironment() const { 265 return getEnvironmentName() != ""; 266 } 267 268 /// getEnvironment - Get the parsed environment type of this triple. getEnvironment()269 EnvironmentType getEnvironment() const { return Environment; } 270 271 /// Parse the version number from the OS name component of the 272 /// triple, if present. 273 /// 274 /// For example, "fooos1.2.3" would return (1, 2, 3). 275 /// 276 /// If an entry is not defined, it will be returned as 0. 277 void getEnvironmentVersion(unsigned &Major, unsigned &Minor, 278 unsigned &Micro) const; 279 280 /// getFormat - Get the object format for this triple. getObjectFormat()281 ObjectFormatType getObjectFormat() const { return ObjectFormat; } 282 283 /// getOSVersion - Parse the version number from the OS name component of the 284 /// triple, if present. 285 /// 286 /// For example, "fooos1.2.3" would return (1, 2, 3). 287 /// 288 /// If an entry is not defined, it will be returned as 0. 289 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const; 290 291 /// getOSMajorVersion - Return just the major version number, this is 292 /// specialized because it is a common query. getOSMajorVersion()293 unsigned getOSMajorVersion() const { 294 unsigned Maj, Min, Micro; 295 getOSVersion(Maj, Min, Micro); 296 return Maj; 297 } 298 299 /// getMacOSXVersion - Parse the version number as with getOSVersion and then 300 /// translate generic "darwin" versions to the corresponding OS X versions. 301 /// This may also be called with IOS triples but the OS X version number is 302 /// just set to a constant 10.4.0 in that case. Returns true if successful. 303 bool getMacOSXVersion(unsigned &Major, unsigned &Minor, 304 unsigned &Micro) const; 305 306 /// getiOSVersion - Parse the version number as with getOSVersion. This should 307 /// only be called with IOS or generic triples. 308 void getiOSVersion(unsigned &Major, unsigned &Minor, 309 unsigned &Micro) const; 310 311 /// getWatchOSVersion - Parse the version number as with getOSVersion. This 312 /// should only be called with WatchOS or generic triples. 313 void getWatchOSVersion(unsigned &Major, unsigned &Minor, 314 unsigned &Micro) const; 315 316 /// @} 317 /// @name Direct Component Access 318 /// @{ 319 str()320 const std::string &str() const { return Data; } 321 getTriple()322 const std::string &getTriple() const { return Data; } 323 324 /// getArchName - Get the architecture (first) component of the 325 /// triple. 326 StringRef getArchName() const; 327 328 /// getVendorName - Get the vendor (second) component of the triple. 329 StringRef getVendorName() const; 330 331 /// getOSName - Get the operating system (third) component of the 332 /// triple. 333 StringRef getOSName() const; 334 335 /// getEnvironmentName - Get the optional environment (fourth) 336 /// component of the triple, or "" if empty. 337 StringRef getEnvironmentName() const; 338 339 /// getOSAndEnvironmentName - Get the operating system and optional 340 /// environment components as a single string (separated by a '-' 341 /// if the environment component is present). 342 StringRef getOSAndEnvironmentName() const; 343 344 /// @} 345 /// @name Convenience Predicates 346 /// @{ 347 348 /// Test whether the architecture is 64-bit 349 /// 350 /// Note that this tests for 64-bit pointer width, and nothing else. Note 351 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and 352 /// 16-bit. The inner details of pointer width for particular architectures 353 /// is not summed up in the triple, and so only a coarse grained predicate 354 /// system is provided. 355 bool isArch64Bit() const; 356 357 /// Test whether the architecture is 32-bit 358 /// 359 /// Note that this tests for 32-bit pointer width, and nothing else. 360 bool isArch32Bit() const; 361 362 /// Test whether the architecture is 16-bit 363 /// 364 /// Note that this tests for 16-bit pointer width, and nothing else. 365 bool isArch16Bit() const; 366 367 /// isOSVersionLT - Helper function for doing comparisons against version 368 /// numbers included in the target triple. 369 bool isOSVersionLT(unsigned Major, unsigned Minor = 0, 370 unsigned Micro = 0) const { 371 unsigned LHS[3]; 372 getOSVersion(LHS[0], LHS[1], LHS[2]); 373 374 if (LHS[0] != Major) 375 return LHS[0] < Major; 376 if (LHS[1] != Minor) 377 return LHS[1] < Minor; 378 if (LHS[2] != Micro) 379 return LHS[1] < Micro; 380 381 return false; 382 } 383 isOSVersionLT(const Triple & Other)384 bool isOSVersionLT(const Triple &Other) const { 385 unsigned RHS[3]; 386 Other.getOSVersion(RHS[0], RHS[1], RHS[2]); 387 return isOSVersionLT(RHS[0], RHS[1], RHS[2]); 388 } 389 390 /// isMacOSXVersionLT - Comparison function for checking OS X version 391 /// compatibility, which handles supporting skewed version numbering schemes 392 /// used by the "darwin" triples. 393 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0, 394 unsigned Micro = 0) const { 395 assert(isMacOSX() && "Not an OS X triple!"); 396 397 // If this is OS X, expect a sane version number. 398 if (getOS() == Triple::MacOSX) 399 return isOSVersionLT(Major, Minor, Micro); 400 401 // Otherwise, compare to the "Darwin" number. 402 assert(Major == 10 && "Unexpected major version"); 403 return isOSVersionLT(Minor + 4, Micro, 0); 404 } 405 406 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both 407 /// "darwin" and "osx" as OS X triples. isMacOSX()408 bool isMacOSX() const { 409 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX; 410 } 411 412 /// Is this an iOS triple. 413 /// Note: This identifies tvOS as a variant of iOS. If that ever 414 /// changes, i.e., if the two operating systems diverge or their version 415 /// numbers get out of sync, that will need to be changed. 416 /// watchOS has completely different version numbers so it is not included. isiOS()417 bool isiOS() const { 418 return getOS() == Triple::IOS || isTvOS(); 419 } 420 421 /// Is this an Apple tvOS triple. isTvOS()422 bool isTvOS() const { 423 return getOS() == Triple::TvOS; 424 } 425 426 /// Is this an Apple watchOS triple. isWatchOS()427 bool isWatchOS() const { 428 return getOS() == Triple::WatchOS; 429 } 430 431 /// isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS). isOSDarwin()432 bool isOSDarwin() const { 433 return isMacOSX() || isiOS() || isWatchOS(); 434 } 435 isOSNetBSD()436 bool isOSNetBSD() const { 437 return getOS() == Triple::NetBSD; 438 } 439 isOSOpenBSD()440 bool isOSOpenBSD() const { 441 return getOS() == Triple::OpenBSD; 442 } 443 isOSFreeBSD()444 bool isOSFreeBSD() const { 445 return getOS() == Triple::FreeBSD; 446 } 447 isOSDragonFly()448 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; } 449 isOSSolaris()450 bool isOSSolaris() const { 451 return getOS() == Triple::Solaris; 452 } 453 isOSBitrig()454 bool isOSBitrig() const { 455 return getOS() == Triple::Bitrig; 456 } 457 isOSIAMCU()458 bool isOSIAMCU() const { 459 return getOS() == Triple::ELFIAMCU; 460 } 461 462 /// Checks if the environment could be MSVC. isWindowsMSVCEnvironment()463 bool isWindowsMSVCEnvironment() const { 464 return getOS() == Triple::Win32 && 465 (getEnvironment() == Triple::UnknownEnvironment || 466 getEnvironment() == Triple::MSVC); 467 } 468 469 /// Checks if the environment is MSVC. isKnownWindowsMSVCEnvironment()470 bool isKnownWindowsMSVCEnvironment() const { 471 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC; 472 } 473 isWindowsCoreCLREnvironment()474 bool isWindowsCoreCLREnvironment() const { 475 return getOS() == Triple::Win32 && getEnvironment() == Triple::CoreCLR; 476 } 477 isWindowsItaniumEnvironment()478 bool isWindowsItaniumEnvironment() const { 479 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium; 480 } 481 isWindowsCygwinEnvironment()482 bool isWindowsCygwinEnvironment() const { 483 return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus; 484 } 485 isWindowsGNUEnvironment()486 bool isWindowsGNUEnvironment() const { 487 return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU; 488 } 489 490 /// Tests for either Cygwin or MinGW OS isOSCygMing()491 bool isOSCygMing() const { 492 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment(); 493 } 494 495 /// Is this a "Windows" OS targeting a "MSVCRT.dll" environment. isOSMSVCRT()496 bool isOSMSVCRT() const { 497 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() || 498 isWindowsItaniumEnvironment(); 499 } 500 501 /// Tests whether the OS is Windows. isOSWindows()502 bool isOSWindows() const { 503 return getOS() == Triple::Win32; 504 } 505 506 /// Tests whether the OS is NaCl (Native Client) isOSNaCl()507 bool isOSNaCl() const { 508 return getOS() == Triple::NaCl; 509 } 510 511 /// Tests whether the OS is Linux. isOSLinux()512 bool isOSLinux() const { 513 return getOS() == Triple::Linux; 514 } 515 516 /// Tests whether the OS uses the ELF binary format. isOSBinFormatELF()517 bool isOSBinFormatELF() const { 518 return getObjectFormat() == Triple::ELF; 519 } 520 521 /// Tests whether the OS uses the COFF binary format. isOSBinFormatCOFF()522 bool isOSBinFormatCOFF() const { 523 return getObjectFormat() == Triple::COFF; 524 } 525 526 /// Tests whether the environment is MachO. isOSBinFormatMachO()527 bool isOSBinFormatMachO() const { 528 return getObjectFormat() == Triple::MachO; 529 } 530 531 /// Tests whether the target is the PS4 CPU isPS4CPU()532 bool isPS4CPU() const { 533 return getArch() == Triple::x86_64 && 534 getVendor() == Triple::SCEI && 535 getOS() == Triple::PS4; 536 } 537 538 /// Tests whether the target is the PS4 platform isPS4()539 bool isPS4() const { 540 return getVendor() == Triple::SCEI && 541 getOS() == Triple::PS4; 542 } 543 544 /// Tests whether the target is Android isAndroid()545 bool isAndroid() const { return getEnvironment() == Triple::Android; } 546 547 /// @} 548 /// @name Mutators 549 /// @{ 550 551 /// setArch - Set the architecture (first) component of the triple 552 /// to a known type. 553 void setArch(ArchType Kind); 554 555 /// setVendor - Set the vendor (second) component of the triple to a 556 /// known type. 557 void setVendor(VendorType Kind); 558 559 /// setOS - Set the operating system (third) component of the triple 560 /// to a known type. 561 void setOS(OSType Kind); 562 563 /// setEnvironment - Set the environment (fourth) component of the triple 564 /// to a known type. 565 void setEnvironment(EnvironmentType Kind); 566 567 /// setObjectFormat - Set the object file format 568 void setObjectFormat(ObjectFormatType Kind); 569 570 /// setTriple - Set all components to the new triple \p Str. 571 void setTriple(const Twine &Str); 572 573 /// setArchName - Set the architecture (first) component of the 574 /// triple by name. 575 void setArchName(StringRef Str); 576 577 /// setVendorName - Set the vendor (second) component of the triple 578 /// by name. 579 void setVendorName(StringRef Str); 580 581 /// setOSName - Set the operating system (third) component of the 582 /// triple by name. 583 void setOSName(StringRef Str); 584 585 /// setEnvironmentName - Set the optional environment (fourth) 586 /// component of the triple by name. 587 void setEnvironmentName(StringRef Str); 588 589 /// setOSAndEnvironmentName - Set the operating system and optional 590 /// environment components with a single string. 591 void setOSAndEnvironmentName(StringRef Str); 592 593 /// @} 594 /// @name Helpers to build variants of a particular triple. 595 /// @{ 596 597 /// Form a triple with a 32-bit variant of the current architecture. 598 /// 599 /// This can be used to move across "families" of architectures where useful. 600 /// 601 /// \returns A new triple with a 32-bit architecture or an unknown 602 /// architecture if no such variant can be found. 603 llvm::Triple get32BitArchVariant() const; 604 605 /// Form a triple with a 64-bit variant of the current architecture. 606 /// 607 /// This can be used to move across "families" of architectures where useful. 608 /// 609 /// \returns A new triple with a 64-bit architecture or an unknown 610 /// architecture if no such variant can be found. 611 llvm::Triple get64BitArchVariant() const; 612 613 /// Form a triple with a big endian variant of the current architecture. 614 /// 615 /// This can be used to move across "families" of architectures where useful. 616 /// 617 /// \returns A new triple with a big endian architecture or an unknown 618 /// architecture if no such variant can be found. 619 llvm::Triple getBigEndianArchVariant() const; 620 621 /// Form a triple with a little endian variant of the current architecture. 622 /// 623 /// This can be used to move across "families" of architectures where useful. 624 /// 625 /// \returns A new triple with a little endian architecture or an unknown 626 /// architecture if no such variant can be found. 627 llvm::Triple getLittleEndianArchVariant() const; 628 629 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting. 630 /// 631 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty 632 /// string then the triple's arch name is used. 633 StringRef getARMCPUForArch(StringRef Arch = StringRef()) const; 634 635 /// @} 636 /// @name Static helpers for IDs. 637 /// @{ 638 639 /// getArchTypeName - Get the canonical name for the \p Kind architecture. 640 static const char *getArchTypeName(ArchType Kind); 641 642 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind 643 /// architecture. This is the prefix used by the architecture specific 644 /// builtins, and is suitable for passing to \see 645 /// Intrinsic::getIntrinsicForGCCBuiltin(). 646 /// 647 /// \return - The architecture prefix, or 0 if none is defined. 648 static const char *getArchTypePrefix(ArchType Kind); 649 650 /// getVendorTypeName - Get the canonical name for the \p Kind vendor. 651 static const char *getVendorTypeName(VendorType Kind); 652 653 /// getOSTypeName - Get the canonical name for the \p Kind operating system. 654 static const char *getOSTypeName(OSType Kind); 655 656 /// getEnvironmentTypeName - Get the canonical name for the \p Kind 657 /// environment. 658 static const char *getEnvironmentTypeName(EnvironmentType Kind); 659 660 /// @} 661 /// @name Static helpers for converting alternate architecture names. 662 /// @{ 663 664 /// getArchTypeForLLVMName - The canonical type for the given LLVM 665 /// architecture name (e.g., "x86"). 666 static ArchType getArchTypeForLLVMName(StringRef Str); 667 668 /// @} 669 }; 670 671 } // End llvm namespace 672 673 674 #endif 675