Triple.h revision 353358
1193323Sed//===-- llvm/ADT/Triple.h - Target triple helper class ----------*- C++ -*-===// 2193323Sed// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6193323Sed// 7193323Sed//===----------------------------------------------------------------------===// 8193323Sed 9193323Sed#ifndef LLVM_ADT_TRIPLE_H 10193323Sed#define LLVM_ADT_TRIPLE_H 11193323Sed 12226633Sdim#include "llvm/ADT/Twine.h" 13193323Sed 14198090Srdivacky// Some system headers or GCC predefined macros conflict with identifiers in 15198090Srdivacky// this file. Undefine them here. 16261991Sdim#undef NetBSD 17198090Srdivacky#undef mips 18198090Srdivacky#undef sparc 19198090Srdivacky 20193323Sednamespace llvm { 21193323Sed 22261991Sdim/// Triple - Helper class for working with autoconf configuration names. For 23261991Sdim/// historical reasons, we also call these 'triples' (they used to contain 24261991Sdim/// exactly three fields). 25193323Sed/// 26261991Sdim/// Configuration names are strings in the canonical form: 27193323Sed/// ARCHITECTURE-VENDOR-OPERATING_SYSTEM 28193323Sed/// or 29193323Sed/// ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT 30193323Sed/// 31193323Sed/// This class is used for clients which want to support arbitrary 32261991Sdim/// configuration names, but also want to implement certain special 33261991Sdim/// behavior for particular configurations. This class isolates the mapping 34261991Sdim/// from the components of the configuration name to well known IDs. 35193323Sed/// 36198090Srdivacky/// At its core the Triple class is designed to be a wrapper for a triple 37212904Sdim/// string; the constructor does not change or normalize the triple string. 38212904Sdim/// Clients that need to handle the non-canonical triples that users often 39212904Sdim/// specify should use the normalize method. 40198090Srdivacky/// 41261991Sdim/// See autoconf/config.guess for a glimpse into what configuration names 42261991Sdim/// look like in practice. 43193323Sedclass Triple { 44193323Sedpublic: 45193323Sed enum ArchType { 46193323Sed UnknownArch, 47218893Sdim 48309124Sdim arm, // ARM (little endian): arm, armv.*, xscale 49309124Sdim armeb, // ARM (big endian): armeb 50309124Sdim aarch64, // AArch64 (little endian): aarch64 51309124Sdim aarch64_be, // AArch64 (big endian): aarch64_be 52353358Sdim aarch64_32, // AArch64 (little endian) ILP32: aarch64_32 53327952Sdim arc, // ARC: Synopsys ARC 54309124Sdim avr, // AVR: Atmel AVR microcontroller 55309124Sdim bpfel, // eBPF or extended BPF or 64-bit BPF (little endian) 56309124Sdim bpfeb, // eBPF or extended BPF or 64-bit BPF (big endian) 57309124Sdim hexagon, // Hexagon: hexagon 58344779Sdim mips, // MIPS: mips, mipsallegrex, mipsr6 59344779Sdim mipsel, // MIPSEL: mipsel, mipsallegrexe, mipsr6el 60344779Sdim mips64, // MIPS64: mips64, mips64r6, mipsn32, mipsn32r6 61344779Sdim mips64el, // MIPS64EL: mips64el, mips64r6el, mipsn32el, mipsn32r6el 62309124Sdim msp430, // MSP430: msp430 63309124Sdim ppc, // PPC: powerpc 64309124Sdim ppc64, // PPC64: powerpc64, ppu 65309124Sdim ppc64le, // PPC64LE: powerpc64le 66309124Sdim r600, // R600: AMD GPUs HD2XXX - HD6XXX 67309124Sdim amdgcn, // AMDGCN: AMD GCN GPUs 68314564Sdim riscv32, // RISC-V (32-bit): riscv32 69314564Sdim riscv64, // RISC-V (64-bit): riscv64 70309124Sdim sparc, // Sparc: sparc 71309124Sdim sparcv9, // Sparcv9: Sparcv9 72309124Sdim sparcel, // Sparc: (endianness = little). NB: 'Sparcle' is a CPU variant 73309124Sdim systemz, // SystemZ: s390x 74309124Sdim tce, // TCE (http://tce.cs.tut.fi/): tce 75314564Sdim tcele, // TCE little endian (http://tce.cs.tut.fi/): tcele 76309124Sdim thumb, // Thumb (little endian): thumb, thumbv.* 77309124Sdim thumbeb, // Thumb (big endian): thumbeb 78309124Sdim x86, // X86: i[3-9]86 79309124Sdim x86_64, // X86-64: amd64, x86_64 80309124Sdim xcore, // XCore: xcore 81309124Sdim nvptx, // NVPTX: 32-bit 82309124Sdim nvptx64, // NVPTX: 64-bit 83309124Sdim le32, // le32: generic little-endian 32-bit CPU (PNaCl) 84309124Sdim le64, // le64: generic little-endian 64-bit CPU (PNaCl) 85309124Sdim amdil, // AMDIL 86309124Sdim amdil64, // AMDIL with 64-bit pointers 87309124Sdim hsail, // AMD HSAIL 88309124Sdim hsail64, // AMD HSAIL with 64-bit pointers 89309124Sdim spir, // SPIR: standard portable IR for OpenCL 32-bit version 90309124Sdim spir64, // SPIR: standard portable IR for OpenCL 64-bit version 91309124Sdim kalimba, // Kalimba: generic kalimba 92309124Sdim shave, // SHAVE: Movidius vector VLIW processors 93309124Sdim lanai, // Lanai: Lanai 32-bit 94309124Sdim wasm32, // WebAssembly with 32-bit pointers 95309124Sdim wasm64, // WebAssembly with 64-bit pointers 96309124Sdim renderscript32, // 32-bit RenderScript 97309124Sdim renderscript64, // 64-bit RenderScript 98309124Sdim LastArchType = renderscript64 99193323Sed }; 100276479Sdim enum SubArchType { 101276479Sdim NoSubArch, 102276479Sdim 103344779Sdim ARMSubArch_v8_5a, 104341825Sdim ARMSubArch_v8_4a, 105327952Sdim ARMSubArch_v8_3a, 106296417Sdim ARMSubArch_v8_2a, 107288943Sdim ARMSubArch_v8_1a, 108276479Sdim ARMSubArch_v8, 109314564Sdim ARMSubArch_v8r, 110309124Sdim ARMSubArch_v8m_baseline, 111309124Sdim ARMSubArch_v8m_mainline, 112353358Sdim ARMSubArch_v8_1m_mainline, 113276479Sdim ARMSubArch_v7, 114276479Sdim ARMSubArch_v7em, 115276479Sdim ARMSubArch_v7m, 116276479Sdim ARMSubArch_v7s, 117296417Sdim ARMSubArch_v7k, 118321369Sdim ARMSubArch_v7ve, 119276479Sdim ARMSubArch_v6, 120276479Sdim ARMSubArch_v6m, 121288943Sdim ARMSubArch_v6k, 122276479Sdim ARMSubArch_v6t2, 123276479Sdim ARMSubArch_v5, 124276479Sdim ARMSubArch_v5te, 125280031Sdim ARMSubArch_v4t, 126280031Sdim 127280031Sdim KalimbaSubArch_v3, 128280031Sdim KalimbaSubArch_v4, 129344779Sdim KalimbaSubArch_v5, 130344779Sdim 131344779Sdim MipsSubArch_r6 132276479Sdim }; 133193323Sed enum VendorType { 134193323Sed UnknownVendor, 135193323Sed 136218893Sdim Apple, 137221345Sdim PC, 138234353Sdim SCEI, 139234353Sdim BGP, 140243830Sdim BGQ, 141243830Sdim Freescale, 142261991Sdim IBM, 143276479Sdim ImaginationTechnologies, 144276479Sdim MipsTechnologies, 145276479Sdim NVIDIA, 146288943Sdim CSR, 147296417Sdim Myriad, 148309124Sdim AMD, 149309124Sdim Mesa, 150321369Sdim SUSE, 151341825Sdim OpenEmbedded, 152341825Sdim LastVendorType = OpenEmbedded 153193323Sed }; 154193323Sed enum OSType { 155193323Sed UnknownOS, 156193323Sed 157321369Sdim Ananas, 158288943Sdim CloudABI, 159193323Sed Darwin, 160193323Sed DragonFly, 161193323Sed FreeBSD, 162314564Sdim Fuchsia, 163221345Sdim IOS, 164226633Sdim KFreeBSD, 165195340Sed Linux, 166199989Srdivacky Lv2, // PS3 167221345Sdim MacOSX, 168198090Srdivacky NetBSD, 169198090Srdivacky OpenBSD, 170198090Srdivacky Solaris, 171198396Srdivacky Win32, 172210299Sed Haiku, 173224145Sdim Minix, 174226633Sdim RTEMS, 175249423Sdim NaCl, // Native Client 176249423Sdim CNK, // BG/P Compute-Node Kernel 177261991Sdim AIX, 178261991Sdim CUDA, // NVIDIA CUDA 179280031Sdim NVCL, // NVIDIA OpenCL 180288943Sdim AMDHSA, // AMD HSA Runtime 181288943Sdim PS4, 182296417Sdim ELFIAMCU, 183296417Sdim TvOS, // Apple tvOS 184296417Sdim WatchOS, // Apple watchOS 185309124Sdim Mesa3D, 186314564Sdim Contiki, 187327952Sdim AMDPAL, // AMD PAL Runtime 188344779Sdim HermitCore, // HermitCore Unikernel/Multikernel 189344779Sdim Hurd, // GNU/Hurd 190344779Sdim WASI, // Experimental WebAssembly OS 191353358Sdim Emscripten, 192353358Sdim LastOSType = Emscripten 193193323Sed }; 194218893Sdim enum EnvironmentType { 195218893Sdim UnknownEnvironment, 196218893Sdim 197218893Sdim GNU, 198327952Sdim GNUABIN32, 199309124Sdim GNUABI64, 200218893Sdim GNUEABI, 201234353Sdim GNUEABIHF, 202249423Sdim GNUX32, 203276479Sdim CODE16, 204218893Sdim EABI, 205276479Sdim EABIHF, 206353358Sdim ELFv1, 207353358Sdim ELFv2, 208243830Sdim Android, 209309124Sdim Musl, 210309124Sdim MuslEABI, 211309124Sdim MuslEABIHF, 212276479Sdim 213276479Sdim MSVC, 214276479Sdim Itanium, 215276479Sdim Cygnus, 216296417Sdim CoreCLR, 217353358Sdim Simulator, // Simulator variants of other systems, e.g., Apple's iOS 218353358Sdim MacABI, // Mac Catalyst variant of Apple's iOS deployment target. 219353358Sdim LastEnvironmentType = MacABI 220218893Sdim }; 221276479Sdim enum ObjectFormatType { 222276479Sdim UnknownObjectFormat, 223218893Sdim 224276479Sdim COFF, 225276479Sdim ELF, 226276479Sdim MachO, 227321369Sdim Wasm, 228353358Sdim XCOFF, 229276479Sdim }; 230276479Sdim 231193323Sedprivate: 232193323Sed std::string Data; 233193323Sed 234234353Sdim /// The parsed arch type. 235234353Sdim ArchType Arch; 236193323Sed 237276479Sdim /// The parsed subarchitecture type. 238276479Sdim SubArchType SubArch; 239276479Sdim 240193323Sed /// The parsed vendor type. 241234353Sdim VendorType Vendor; 242193323Sed 243193323Sed /// The parsed OS type. 244234353Sdim OSType OS; 245193323Sed 246218893Sdim /// The parsed Environment type. 247234353Sdim EnvironmentType Environment; 248218893Sdim 249276479Sdim /// The object format type. 250276479Sdim ObjectFormatType ObjectFormat; 251276479Sdim 252193323Sedpublic: 253193323Sed /// @name Constructors 254193323Sed /// @{ 255218893Sdim 256296417Sdim /// Default constructor is the same as an empty string and leaves all 257234353Sdim /// triple fields unknown. 258321369Sdim Triple() 259321369Sdim : Data(), Arch(), SubArch(), Vendor(), OS(), Environment(), 260321369Sdim ObjectFormat() {} 261193323Sed 262234353Sdim explicit Triple(const Twine &Str); 263234353Sdim Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr); 264226633Sdim Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr, 265234353Sdim const Twine &EnvironmentStr); 266218893Sdim 267288943Sdim bool operator==(const Triple &Other) const { 268288943Sdim return Arch == Other.Arch && SubArch == Other.SubArch && 269288943Sdim Vendor == Other.Vendor && OS == Other.OS && 270288943Sdim Environment == Other.Environment && 271288943Sdim ObjectFormat == Other.ObjectFormat; 272288943Sdim } 273288943Sdim 274321369Sdim bool operator!=(const Triple &Other) const { 275321369Sdim return !(*this == Other); 276321369Sdim } 277321369Sdim 278193323Sed /// @} 279212904Sdim /// @name Normalization 280212904Sdim /// @{ 281212904Sdim 282212904Sdim /// normalize - Turn an arbitrary machine specification into the canonical 283212904Sdim /// triple form (or something sensible that the Triple class understands if 284212904Sdim /// nothing better can reasonably be done). In particular, it handles the 285212904Sdim /// common case in which otherwise valid components are in the wrong order. 286212904Sdim static std::string normalize(StringRef Str); 287212904Sdim 288296417Sdim /// Return the normalized form of this triple's string. 289288943Sdim std::string normalize() const { return normalize(Data); } 290288943Sdim 291212904Sdim /// @} 292193323Sed /// @name Typed Component Access 293193323Sed /// @{ 294218893Sdim 295193323Sed /// getArch - Get the parsed architecture type of this triple. 296234353Sdim ArchType getArch() const { return Arch; } 297218893Sdim 298276479Sdim /// getSubArch - get the parsed subarchitecture type for this triple. 299276479Sdim SubArchType getSubArch() const { return SubArch; } 300276479Sdim 301193323Sed /// getVendor - Get the parsed vendor type of this triple. 302234353Sdim VendorType getVendor() const { return Vendor; } 303218893Sdim 304193323Sed /// getOS - Get the parsed operating system type of this triple. 305234353Sdim OSType getOS() const { return OS; } 306193323Sed 307193323Sed /// hasEnvironment - Does this triple have the optional environment 308193323Sed /// (fourth) component? 309193323Sed bool hasEnvironment() const { 310193323Sed return getEnvironmentName() != ""; 311193323Sed } 312193323Sed 313218893Sdim /// getEnvironment - Get the parsed environment type of this triple. 314234353Sdim EnvironmentType getEnvironment() const { return Environment; } 315234353Sdim 316296417Sdim /// Parse the version number from the OS name component of the 317288943Sdim /// triple, if present. 318288943Sdim /// 319288943Sdim /// For example, "fooos1.2.3" would return (1, 2, 3). 320288943Sdim /// 321288943Sdim /// If an entry is not defined, it will be returned as 0. 322288943Sdim void getEnvironmentVersion(unsigned &Major, unsigned &Minor, 323288943Sdim unsigned &Micro) const; 324288943Sdim 325276479Sdim /// getFormat - Get the object format for this triple. 326276479Sdim ObjectFormatType getObjectFormat() const { return ObjectFormat; } 327276479Sdim 328234353Sdim /// getOSVersion - Parse the version number from the OS name component of the 329234353Sdim /// triple, if present. 330234353Sdim /// 331234353Sdim /// For example, "fooos1.2.3" would return (1, 2, 3). 332234353Sdim /// 333234353Sdim /// If an entry is not defined, it will be returned as 0. 334234353Sdim void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const; 335234353Sdim 336234353Sdim /// getOSMajorVersion - Return just the major version number, this is 337234353Sdim /// specialized because it is a common query. 338234353Sdim unsigned getOSMajorVersion() const { 339234353Sdim unsigned Maj, Min, Micro; 340234353Sdim getOSVersion(Maj, Min, Micro); 341234353Sdim return Maj; 342218893Sdim } 343218893Sdim 344234353Sdim /// getMacOSXVersion - Parse the version number as with getOSVersion and then 345234353Sdim /// translate generic "darwin" versions to the corresponding OS X versions. 346234353Sdim /// This may also be called with IOS triples but the OS X version number is 347234353Sdim /// just set to a constant 10.4.0 in that case. Returns true if successful. 348234353Sdim bool getMacOSXVersion(unsigned &Major, unsigned &Minor, 349234353Sdim unsigned &Micro) const; 350234353Sdim 351239462Sdim /// getiOSVersion - Parse the version number as with getOSVersion. This should 352296417Sdim /// only be called with IOS or generic triples. 353239462Sdim void getiOSVersion(unsigned &Major, unsigned &Minor, 354239462Sdim unsigned &Micro) const; 355239462Sdim 356296417Sdim /// getWatchOSVersion - Parse the version number as with getOSVersion. This 357296417Sdim /// should only be called with WatchOS or generic triples. 358296417Sdim void getWatchOSVersion(unsigned &Major, unsigned &Minor, 359296417Sdim unsigned &Micro) const; 360296417Sdim 361193323Sed /// @} 362193323Sed /// @name Direct Component Access 363193323Sed /// @{ 364193323Sed 365199481Srdivacky const std::string &str() const { return Data; } 366199481Srdivacky 367193323Sed const std::string &getTriple() const { return Data; } 368193323Sed 369193323Sed /// getArchName - Get the architecture (first) component of the 370193323Sed /// triple. 371198090Srdivacky StringRef getArchName() const; 372193323Sed 373193323Sed /// getVendorName - Get the vendor (second) component of the triple. 374198090Srdivacky StringRef getVendorName() const; 375193323Sed 376193323Sed /// getOSName - Get the operating system (third) component of the 377193323Sed /// triple. 378198090Srdivacky StringRef getOSName() const; 379193323Sed 380193323Sed /// getEnvironmentName - Get the optional environment (fourth) 381193323Sed /// component of the triple, or "" if empty. 382198090Srdivacky StringRef getEnvironmentName() const; 383193323Sed 384193323Sed /// getOSAndEnvironmentName - Get the operating system and optional 385193323Sed /// environment components as a single string (separated by a '-' 386193323Sed /// if the environment component is present). 387198090Srdivacky StringRef getOSAndEnvironmentName() const; 388193323Sed 389234353Sdim /// @} 390234353Sdim /// @name Convenience Predicates 391234353Sdim /// @{ 392234353Sdim 393296417Sdim /// Test whether the architecture is 64-bit 394221345Sdim /// 395234353Sdim /// Note that this tests for 64-bit pointer width, and nothing else. Note 396234353Sdim /// that we intentionally expose only three predicates, 64-bit, 32-bit, and 397234353Sdim /// 16-bit. The inner details of pointer width for particular architectures 398234353Sdim /// is not summed up in the triple, and so only a coarse grained predicate 399234353Sdim /// system is provided. 400234353Sdim bool isArch64Bit() const; 401234353Sdim 402296417Sdim /// Test whether the architecture is 32-bit 403221345Sdim /// 404234353Sdim /// Note that this tests for 32-bit pointer width, and nothing else. 405234353Sdim bool isArch32Bit() const; 406218893Sdim 407296417Sdim /// Test whether the architecture is 16-bit 408234353Sdim /// 409234353Sdim /// Note that this tests for 16-bit pointer width, and nothing else. 410234353Sdim bool isArch16Bit() const; 411218893Sdim 412221345Sdim /// isOSVersionLT - Helper function for doing comparisons against version 413221345Sdim /// numbers included in the target triple. 414221345Sdim bool isOSVersionLT(unsigned Major, unsigned Minor = 0, 415221345Sdim unsigned Micro = 0) const { 416221345Sdim unsigned LHS[3]; 417221345Sdim getOSVersion(LHS[0], LHS[1], LHS[2]); 418221345Sdim 419221345Sdim if (LHS[0] != Major) 420221345Sdim return LHS[0] < Major; 421221345Sdim if (LHS[1] != Minor) 422221345Sdim return LHS[1] < Minor; 423221345Sdim if (LHS[2] != Micro) 424353358Sdim return LHS[2] < Micro; 425221345Sdim 426221345Sdim return false; 427221345Sdim } 428221345Sdim 429288943Sdim bool isOSVersionLT(const Triple &Other) const { 430288943Sdim unsigned RHS[3]; 431288943Sdim Other.getOSVersion(RHS[0], RHS[1], RHS[2]); 432288943Sdim return isOSVersionLT(RHS[0], RHS[1], RHS[2]); 433288943Sdim } 434288943Sdim 435234353Sdim /// isMacOSXVersionLT - Comparison function for checking OS X version 436234353Sdim /// compatibility, which handles supporting skewed version numbering schemes 437234353Sdim /// used by the "darwin" triples. 438309124Sdim bool isMacOSXVersionLT(unsigned Major, unsigned Minor = 0, 439309124Sdim unsigned Micro = 0) const { 440234353Sdim assert(isMacOSX() && "Not an OS X triple!"); 441234353Sdim 442234353Sdim // If this is OS X, expect a sane version number. 443234353Sdim if (getOS() == Triple::MacOSX) 444234353Sdim return isOSVersionLT(Major, Minor, Micro); 445234353Sdim 446234353Sdim // Otherwise, compare to the "Darwin" number. 447234353Sdim assert(Major == 10 && "Unexpected major version"); 448234353Sdim return isOSVersionLT(Minor + 4, Micro, 0); 449234353Sdim } 450234353Sdim 451221345Sdim /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both 452221345Sdim /// "darwin" and "osx" as OS X triples. 453221345Sdim bool isMacOSX() const { 454221345Sdim return getOS() == Triple::Darwin || getOS() == Triple::MacOSX; 455221345Sdim } 456221345Sdim 457249423Sdim /// Is this an iOS triple. 458296417Sdim /// Note: This identifies tvOS as a variant of iOS. If that ever 459296417Sdim /// changes, i.e., if the two operating systems diverge or their version 460296417Sdim /// numbers get out of sync, that will need to be changed. 461296417Sdim /// watchOS has completely different version numbers so it is not included. 462249423Sdim bool isiOS() const { 463296417Sdim return getOS() == Triple::IOS || isTvOS(); 464249423Sdim } 465249423Sdim 466296417Sdim /// Is this an Apple tvOS triple. 467296417Sdim bool isTvOS() const { 468296417Sdim return getOS() == Triple::TvOS; 469296417Sdim } 470296417Sdim 471296417Sdim /// Is this an Apple watchOS triple. 472296417Sdim bool isWatchOS() const { 473296417Sdim return getOS() == Triple::WatchOS; 474296417Sdim } 475296417Sdim 476309124Sdim bool isWatchABI() const { 477309124Sdim return getSubArch() == Triple::ARMSubArch_v7k; 478309124Sdim } 479309124Sdim 480296417Sdim /// isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS). 481221345Sdim bool isOSDarwin() const { 482296417Sdim return isMacOSX() || isiOS() || isWatchOS(); 483221345Sdim } 484221345Sdim 485327952Sdim bool isSimulatorEnvironment() const { 486327952Sdim return getEnvironment() == Triple::Simulator; 487327952Sdim } 488327952Sdim 489353358Sdim bool isMacCatalystEnvironment() const { 490353358Sdim return getEnvironment() == Triple::MacABI; 491353358Sdim } 492353358Sdim 493280031Sdim bool isOSNetBSD() const { 494280031Sdim return getOS() == Triple::NetBSD; 495280031Sdim } 496280031Sdim 497280031Sdim bool isOSOpenBSD() const { 498280031Sdim return getOS() == Triple::OpenBSD; 499280031Sdim } 500280031Sdim 501276479Sdim bool isOSFreeBSD() const { 502276479Sdim return getOS() == Triple::FreeBSD; 503276479Sdim } 504276479Sdim 505314564Sdim bool isOSFuchsia() const { 506314564Sdim return getOS() == Triple::Fuchsia; 507314564Sdim } 508314564Sdim 509280031Sdim bool isOSDragonFly() const { return getOS() == Triple::DragonFly; } 510280031Sdim 511280031Sdim bool isOSSolaris() const { 512280031Sdim return getOS() == Triple::Solaris; 513280031Sdim } 514280031Sdim 515296417Sdim bool isOSIAMCU() const { 516296417Sdim return getOS() == Triple::ELFIAMCU; 517296417Sdim } 518296417Sdim 519327952Sdim bool isOSUnknown() const { return getOS() == Triple::UnknownOS; } 520327952Sdim 521309124Sdim bool isGNUEnvironment() const { 522309124Sdim EnvironmentType Env = getEnvironment(); 523327952Sdim return Env == Triple::GNU || Env == Triple::GNUABIN32 || 524327952Sdim Env == Triple::GNUABI64 || Env == Triple::GNUEABI || 525327952Sdim Env == Triple::GNUEABIHF || Env == Triple::GNUX32; 526309124Sdim } 527309124Sdim 528314564Sdim bool isOSContiki() const { 529314564Sdim return getOS() == Triple::Contiki; 530314564Sdim } 531314564Sdim 532327952Sdim /// Tests whether the OS is Haiku. 533327952Sdim bool isOSHaiku() const { 534327952Sdim return getOS() == Triple::Haiku; 535327952Sdim } 536327952Sdim 537353358Sdim /// Tests whether the OS is Windows. 538353358Sdim bool isOSWindows() const { 539353358Sdim return getOS() == Triple::Win32; 540276479Sdim } 541276479Sdim 542296417Sdim /// Checks if the environment is MSVC. 543276479Sdim bool isKnownWindowsMSVCEnvironment() const { 544353358Sdim return isOSWindows() && getEnvironment() == Triple::MSVC; 545276479Sdim } 546276479Sdim 547353358Sdim /// Checks if the environment could be MSVC. 548353358Sdim bool isWindowsMSVCEnvironment() const { 549353358Sdim return isKnownWindowsMSVCEnvironment() || 550353358Sdim (isOSWindows() && getEnvironment() == Triple::UnknownEnvironment); 551353358Sdim } 552353358Sdim 553296417Sdim bool isWindowsCoreCLREnvironment() const { 554353358Sdim return isOSWindows() && getEnvironment() == Triple::CoreCLR; 555296417Sdim } 556296417Sdim 557276479Sdim bool isWindowsItaniumEnvironment() const { 558353358Sdim return isOSWindows() && getEnvironment() == Triple::Itanium; 559276479Sdim } 560276479Sdim 561276479Sdim bool isWindowsCygwinEnvironment() const { 562353358Sdim return isOSWindows() && getEnvironment() == Triple::Cygnus; 563276479Sdim } 564276479Sdim 565276479Sdim bool isWindowsGNUEnvironment() const { 566353358Sdim return isOSWindows() && getEnvironment() == Triple::GNU; 567276479Sdim } 568276479Sdim 569296417Sdim /// Tests for either Cygwin or MinGW OS 570234353Sdim bool isOSCygMing() const { 571276479Sdim return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment(); 572234353Sdim } 573234353Sdim 574296417Sdim /// Is this a "Windows" OS targeting a "MSVCRT.dll" environment. 575261991Sdim bool isOSMSVCRT() const { 576280031Sdim return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() || 577280031Sdim isWindowsItaniumEnvironment(); 578261991Sdim } 579261991Sdim 580296417Sdim /// Tests whether the OS is NaCl (Native Client) 581249423Sdim bool isOSNaCl() const { 582249423Sdim return getOS() == Triple::NaCl; 583249423Sdim } 584249423Sdim 585296417Sdim /// Tests whether the OS is Linux. 586261991Sdim bool isOSLinux() const { 587261991Sdim return getOS() == Triple::Linux; 588261991Sdim } 589261991Sdim 590309124Sdim /// Tests whether the OS is kFreeBSD. 591309124Sdim bool isOSKFreeBSD() const { 592309124Sdim return getOS() == Triple::KFreeBSD; 593309124Sdim } 594309124Sdim 595344779Sdim /// Tests whether the OS is Hurd. 596344779Sdim bool isOSHurd() const { 597344779Sdim return getOS() == Triple::Hurd; 598344779Sdim } 599344779Sdim 600344779Sdim /// Tests whether the OS is WASI. 601344779Sdim bool isOSWASI() const { 602344779Sdim return getOS() == Triple::WASI; 603344779Sdim } 604344779Sdim 605353358Sdim /// Tests whether the OS is Emscripten. 606353358Sdim bool isOSEmscripten() const { 607353358Sdim return getOS() == Triple::Emscripten; 608353358Sdim } 609353358Sdim 610309124Sdim /// Tests whether the OS uses glibc. 611309124Sdim bool isOSGlibc() const { 612344779Sdim return (getOS() == Triple::Linux || getOS() == Triple::KFreeBSD || 613344779Sdim getOS() == Triple::Hurd) && 614321369Sdim !isAndroid(); 615309124Sdim } 616309124Sdim 617353358Sdim /// Tests whether the OS is AIX. 618353358Sdim bool isOSAIX() const { 619353358Sdim return getOS() == Triple::AIX; 620353358Sdim } 621353358Sdim 622296417Sdim /// Tests whether the OS uses the ELF binary format. 623234353Sdim bool isOSBinFormatELF() const { 624276479Sdim return getObjectFormat() == Triple::ELF; 625234353Sdim } 626221345Sdim 627296417Sdim /// Tests whether the OS uses the COFF binary format. 628234353Sdim bool isOSBinFormatCOFF() const { 629276479Sdim return getObjectFormat() == Triple::COFF; 630234353Sdim } 631221345Sdim 632296417Sdim /// Tests whether the environment is MachO. 633276479Sdim bool isOSBinFormatMachO() const { 634276479Sdim return getObjectFormat() == Triple::MachO; 635221345Sdim } 636224145Sdim 637321369Sdim /// Tests whether the OS uses the Wasm binary format. 638321369Sdim bool isOSBinFormatWasm() const { 639321369Sdim return getObjectFormat() == Triple::Wasm; 640321369Sdim } 641321369Sdim 642353358Sdim /// Tests whether the OS uses the XCOFF binary format. 643353358Sdim bool isOSBinFormatXCOFF() const { 644353358Sdim return getObjectFormat() == Triple::XCOFF; 645353358Sdim } 646353358Sdim 647296417Sdim /// Tests whether the target is the PS4 CPU 648288943Sdim bool isPS4CPU() const { 649288943Sdim return getArch() == Triple::x86_64 && 650288943Sdim getVendor() == Triple::SCEI && 651288943Sdim getOS() == Triple::PS4; 652288943Sdim } 653288943Sdim 654296417Sdim /// Tests whether the target is the PS4 platform 655288943Sdim bool isPS4() const { 656288943Sdim return getVendor() == Triple::SCEI && 657288943Sdim getOS() == Triple::PS4; 658288943Sdim } 659288943Sdim 660296417Sdim /// Tests whether the target is Android 661296417Sdim bool isAndroid() const { return getEnvironment() == Triple::Android; } 662296417Sdim 663321369Sdim bool isAndroidVersionLT(unsigned Major) const { 664321369Sdim assert(isAndroid() && "Not an Android triple!"); 665321369Sdim 666321369Sdim unsigned Env[3]; 667321369Sdim getEnvironmentVersion(Env[0], Env[1], Env[2]); 668321369Sdim 669321369Sdim // 64-bit targets did not exist before API level 21 (Lollipop). 670321369Sdim if (isArch64Bit() && Env[0] < 21) 671321369Sdim Env[0] = 21; 672321369Sdim 673321369Sdim return Env[0] < Major; 674321369Sdim } 675321369Sdim 676309124Sdim /// Tests whether the environment is musl-libc 677309124Sdim bool isMusl() const { 678309124Sdim return getEnvironment() == Triple::Musl || 679309124Sdim getEnvironment() == Triple::MuslEABI || 680309124Sdim getEnvironment() == Triple::MuslEABIHF; 681309124Sdim } 682309124Sdim 683353358Sdim /// Tests whether the target is SPIR (32- or 64-bit). 684353358Sdim bool isSPIR() const { 685353358Sdim return getArch() == Triple::spir || getArch() == Triple::spir64; 686353358Sdim } 687353358Sdim 688309124Sdim /// Tests whether the target is NVPTX (32- or 64-bit). 689309124Sdim bool isNVPTX() const { 690309124Sdim return getArch() == Triple::nvptx || getArch() == Triple::nvptx64; 691309124Sdim } 692309124Sdim 693327952Sdim /// Tests whether the target is Thumb (little and big endian). 694327952Sdim bool isThumb() const { 695327952Sdim return getArch() == Triple::thumb || getArch() == Triple::thumbeb; 696327952Sdim } 697327952Sdim 698327952Sdim /// Tests whether the target is ARM (little and big endian). 699327952Sdim bool isARM() const { 700327952Sdim return getArch() == Triple::arm || getArch() == Triple::armeb; 701327952Sdim } 702327952Sdim 703327952Sdim /// Tests whether the target is AArch64 (little and big endian). 704327952Sdim bool isAArch64() const { 705327952Sdim return getArch() == Triple::aarch64 || getArch() == Triple::aarch64_be; 706327952Sdim } 707327952Sdim 708341825Sdim /// Tests whether the target is MIPS 32-bit (little and big endian). 709341825Sdim bool isMIPS32() const { 710341825Sdim return getArch() == Triple::mips || getArch() == Triple::mipsel; 711341825Sdim } 712341825Sdim 713341825Sdim /// Tests whether the target is MIPS 64-bit (little and big endian). 714341825Sdim bool isMIPS64() const { 715341825Sdim return getArch() == Triple::mips64 || getArch() == Triple::mips64el; 716341825Sdim } 717341825Sdim 718341825Sdim /// Tests whether the target is MIPS (little and big endian, 32- or 64-bit). 719341825Sdim bool isMIPS() const { 720341825Sdim return isMIPS32() || isMIPS64(); 721341825Sdim } 722341825Sdim 723353358Sdim /// Tests whether the target is 64-bit PowerPC (little and big endian). 724353358Sdim bool isPPC64() const { 725353358Sdim return getArch() == Triple::ppc64 || getArch() == Triple::ppc64le; 726353358Sdim } 727353358Sdim 728353358Sdim /// Tests whether the target is RISC-V (32- and 64-bit). 729353358Sdim bool isRISCV() const { 730353358Sdim return getArch() == Triple::riscv32 || getArch() == Triple::riscv64; 731353358Sdim } 732353358Sdim 733341825Sdim /// Tests whether the target supports comdat 734327952Sdim bool supportsCOMDAT() const { 735341825Sdim return !isOSBinFormatMachO(); 736327952Sdim } 737309124Sdim 738341825Sdim /// Tests whether the target uses emulated TLS as default. 739341825Sdim bool hasDefaultEmulatedTLS() const { 740341825Sdim return isAndroid() || isOSOpenBSD() || isWindowsCygwinEnvironment(); 741341825Sdim } 742341825Sdim 743193323Sed /// @} 744193323Sed /// @name Mutators 745193323Sed /// @{ 746193323Sed 747193323Sed /// setArch - Set the architecture (first) component of the triple 748193323Sed /// to a known type. 749193323Sed void setArch(ArchType Kind); 750193323Sed 751193323Sed /// setVendor - Set the vendor (second) component of the triple to a 752193323Sed /// known type. 753193323Sed void setVendor(VendorType Kind); 754193323Sed 755193323Sed /// setOS - Set the operating system (third) component of the triple 756193323Sed /// to a known type. 757193323Sed void setOS(OSType Kind); 758193323Sed 759218893Sdim /// setEnvironment - Set the environment (fourth) component of the triple 760218893Sdim /// to a known type. 761218893Sdim void setEnvironment(EnvironmentType Kind); 762218893Sdim 763276479Sdim /// setObjectFormat - Set the object file format 764276479Sdim void setObjectFormat(ObjectFormatType Kind); 765276479Sdim 766243830Sdim /// setTriple - Set all components to the new triple \p Str. 767198090Srdivacky void setTriple(const Twine &Str); 768193323Sed 769193323Sed /// setArchName - Set the architecture (first) component of the 770193323Sed /// triple by name. 771199481Srdivacky void setArchName(StringRef Str); 772193323Sed 773193323Sed /// setVendorName - Set the vendor (second) component of the triple 774193323Sed /// by name. 775199481Srdivacky void setVendorName(StringRef Str); 776193323Sed 777193323Sed /// setOSName - Set the operating system (third) component of the 778193323Sed /// triple by name. 779199481Srdivacky void setOSName(StringRef Str); 780193323Sed 781193323Sed /// setEnvironmentName - Set the optional environment (fourth) 782193323Sed /// component of the triple by name. 783199481Srdivacky void setEnvironmentName(StringRef Str); 784193323Sed 785193323Sed /// setOSAndEnvironmentName - Set the operating system and optional 786193323Sed /// environment components with a single string. 787199481Srdivacky void setOSAndEnvironmentName(StringRef Str); 788193323Sed 789193323Sed /// @} 790234353Sdim /// @name Helpers to build variants of a particular triple. 791234353Sdim /// @{ 792234353Sdim 793296417Sdim /// Form a triple with a 32-bit variant of the current architecture. 794234353Sdim /// 795234353Sdim /// This can be used to move across "families" of architectures where useful. 796234353Sdim /// 797234353Sdim /// \returns A new triple with a 32-bit architecture or an unknown 798234353Sdim /// architecture if no such variant can be found. 799234353Sdim llvm::Triple get32BitArchVariant() const; 800234353Sdim 801296417Sdim /// Form a triple with a 64-bit variant of the current architecture. 802234353Sdim /// 803234353Sdim /// This can be used to move across "families" of architectures where useful. 804234353Sdim /// 805234353Sdim /// \returns A new triple with a 64-bit architecture or an unknown 806234353Sdim /// architecture if no such variant can be found. 807234353Sdim llvm::Triple get64BitArchVariant() const; 808234353Sdim 809288943Sdim /// Form a triple with a big endian variant of the current architecture. 810288943Sdim /// 811288943Sdim /// This can be used to move across "families" of architectures where useful. 812288943Sdim /// 813288943Sdim /// \returns A new triple with a big endian architecture or an unknown 814288943Sdim /// architecture if no such variant can be found. 815288943Sdim llvm::Triple getBigEndianArchVariant() const; 816288943Sdim 817288943Sdim /// Form a triple with a little endian variant of the current architecture. 818288943Sdim /// 819288943Sdim /// This can be used to move across "families" of architectures where useful. 820288943Sdim /// 821288943Sdim /// \returns A new triple with a little endian architecture or an unknown 822288943Sdim /// architecture if no such variant can be found. 823288943Sdim llvm::Triple getLittleEndianArchVariant() const; 824288943Sdim 825276479Sdim /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting. 826276479Sdim /// 827276479Sdim /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty 828276479Sdim /// string then the triple's arch name is used. 829296417Sdim StringRef getARMCPUForArch(StringRef Arch = StringRef()) const; 830276479Sdim 831309124Sdim /// Tests whether the target triple is little endian. 832309124Sdim /// 833309124Sdim /// \returns true if the triple is little endian, false otherwise. 834309124Sdim bool isLittleEndian() const; 835309124Sdim 836321369Sdim /// Test whether target triples are compatible. 837321369Sdim bool isCompatibleWith(const Triple &Other) const; 838321369Sdim 839321369Sdim /// Merge target triples. 840321369Sdim std::string merge(const Triple &Other) const; 841321369Sdim 842234353Sdim /// @} 843193323Sed /// @name Static helpers for IDs. 844193323Sed /// @{ 845193323Sed 846243830Sdim /// getArchTypeName - Get the canonical name for the \p Kind architecture. 847314564Sdim static StringRef getArchTypeName(ArchType Kind); 848193323Sed 849243830Sdim /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind 850198090Srdivacky /// architecture. This is the prefix used by the architecture specific 851198090Srdivacky /// builtins, and is suitable for passing to \see 852198090Srdivacky /// Intrinsic::getIntrinsicForGCCBuiltin(). 853198090Srdivacky /// 854198090Srdivacky /// \return - The architecture prefix, or 0 if none is defined. 855314564Sdim static StringRef getArchTypePrefix(ArchType Kind); 856198090Srdivacky 857243830Sdim /// getVendorTypeName - Get the canonical name for the \p Kind vendor. 858314564Sdim static StringRef getVendorTypeName(VendorType Kind); 859193323Sed 860243830Sdim /// getOSTypeName - Get the canonical name for the \p Kind operating system. 861314564Sdim static StringRef getOSTypeName(OSType Kind); 862193323Sed 863243830Sdim /// getEnvironmentTypeName - Get the canonical name for the \p Kind 864218893Sdim /// environment. 865314564Sdim static StringRef getEnvironmentTypeName(EnvironmentType Kind); 866218893Sdim 867193323Sed /// @} 868198090Srdivacky /// @name Static helpers for converting alternate architecture names. 869198090Srdivacky /// @{ 870198090Srdivacky 871198090Srdivacky /// getArchTypeForLLVMName - The canonical type for the given LLVM 872198090Srdivacky /// architecture name (e.g., "x86"). 873199481Srdivacky static ArchType getArchTypeForLLVMName(StringRef Str); 874198090Srdivacky 875198090Srdivacky /// @} 876193323Sed}; 877193323Sed 878193323Sed} // End llvm namespace 879193323Sed 880193323Sed 881193323Sed#endif 882