1193323Sed//===-- llvm/ADT/Triple.h - Target triple helper class ----------*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed 10193323Sed#ifndef LLVM_ADT_TRIPLE_H 11193323Sed#define LLVM_ADT_TRIPLE_H 12193323Sed 13226633Sdim#include "llvm/ADT/Twine.h" 14193323Sed 15198090Srdivacky// Some system headers or GCC predefined macros conflict with identifiers in 16198090Srdivacky// this file. Undefine them here. 17198090Srdivacky#undef mips 18198090Srdivacky#undef sparc 19198090Srdivacky 20193323Sednamespace llvm { 21193323Sed 22193323Sed/// Triple - Helper class for working with target triples. 23193323Sed/// 24212904Sdim/// Target triples are strings in the canonical form: 25193323Sed/// ARCHITECTURE-VENDOR-OPERATING_SYSTEM 26193323Sed/// or 27193323Sed/// ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT 28193323Sed/// 29193323Sed/// This class is used for clients which want to support arbitrary 30193323Sed/// target triples, but also want to implement certain special 31193323Sed/// behavior for particular targets. This class isolates the mapping 32193323Sed/// from the components of the target triple to well known IDs. 33193323Sed/// 34198090Srdivacky/// At its core the Triple class is designed to be a wrapper for a triple 35212904Sdim/// string; the constructor does not change or normalize the triple string. 36212904Sdim/// Clients that need to handle the non-canonical triples that users often 37212904Sdim/// specify should use the normalize method. 38198090Srdivacky/// 39212904Sdim/// See autoconf/config.guess for a glimpse into what triples look like in 40198090Srdivacky/// practice. 41193323Sedclass Triple { 42193323Sedpublic: 43193323Sed enum ArchType { 44193323Sed UnknownArch, 45218893Sdim 46251662Sdim arm, // ARM: arm, armv.*, xscale 47249423Sdim aarch64, // AArch64: aarch64 48234353Sdim hexagon, // Hexagon: hexagon 49198090Srdivacky mips, // MIPS: mips, mipsallegrex 50234353Sdim mipsel, // MIPSEL: mipsel, mipsallegrexel 51226633Sdim mips64, // MIPS64: mips64 52226633Sdim mips64el,// MIPS64EL: mips64el 53198090Srdivacky msp430, // MSP430: msp430 54198090Srdivacky ppc, // PPC: powerpc 55199989Srdivacky ppc64, // PPC64: powerpc64, ppu 56234353Sdim r600, // R600: AMD GPUs HD2XXX - HD6XXX 57198090Srdivacky sparc, // Sparc: sparc 58203954Srdivacky sparcv9, // Sparcv9: Sparcv9 59251662Sdim systemz, // SystemZ: s390x 60198090Srdivacky tce, // TCE (http://tce.cs.tut.fi/): tce 61198090Srdivacky thumb, // Thumb: thumb, thumbv.* 62198090Srdivacky x86, // X86: i[3-9]86 63198090Srdivacky x86_64, // X86-64: amd64, x86_64 64198090Srdivacky xcore, // XCore: xcore 65204642Srdivacky mblaze, // MBlaze: mblaze 66239462Sdim nvptx, // NVPTX: 32-bit 67239462Sdim nvptx64, // NVPTX: 64-bit 68226633Sdim le32, // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten) 69243830Sdim amdil, // amdil: amd IL 70243830Sdim spir, // SPIR: standard portable IR for OpenCL 32-bit version 71243830Sdim spir64 // SPIR: standard portable IR for OpenCL 64-bit version 72193323Sed }; 73193323Sed enum VendorType { 74193323Sed UnknownVendor, 75193323Sed 76218893Sdim Apple, 77221345Sdim PC, 78234353Sdim SCEI, 79234353Sdim BGP, 80243830Sdim BGQ, 81243830Sdim Freescale, 82243830Sdim IBM 83193323Sed }; 84193323Sed enum OSType { 85193323Sed UnknownOS, 86193323Sed 87194612Sed AuroraUX, 88198090Srdivacky Cygwin, 89193323Sed Darwin, 90193323Sed DragonFly, 91193323Sed FreeBSD, 92221345Sdim IOS, 93226633Sdim KFreeBSD, 94195340Sed Linux, 95199989Srdivacky Lv2, // PS3 96221345Sdim MacOSX, 97218893Sdim MinGW32, // i*86-pc-mingw32, *-w64-mingw32 98198090Srdivacky NetBSD, 99198090Srdivacky OpenBSD, 100198090Srdivacky Solaris, 101198396Srdivacky Win32, 102210299Sed Haiku, 103224145Sdim Minix, 104226633Sdim RTEMS, 105249423Sdim NaCl, // Native Client 106249423Sdim CNK, // BG/P Compute-Node Kernel 107243830Sdim Bitrig, 108243830Sdim AIX 109193323Sed }; 110218893Sdim enum EnvironmentType { 111218893Sdim UnknownEnvironment, 112218893Sdim 113218893Sdim GNU, 114218893Sdim GNUEABI, 115234353Sdim GNUEABIHF, 116249423Sdim GNUX32, 117218893Sdim EABI, 118234353Sdim MachO, 119243830Sdim Android, 120243830Sdim ELF 121218893Sdim }; 122218893Sdim 123193323Sedprivate: 124193323Sed std::string Data; 125193323Sed 126234353Sdim /// The parsed arch type. 127234353Sdim ArchType Arch; 128193323Sed 129193323Sed /// The parsed vendor type. 130234353Sdim VendorType Vendor; 131193323Sed 132193323Sed /// The parsed OS type. 133234353Sdim OSType OS; 134193323Sed 135218893Sdim /// The parsed Environment type. 136234353Sdim EnvironmentType Environment; 137218893Sdim 138193323Sedpublic: 139193323Sed /// @name Constructors 140193323Sed /// @{ 141218893Sdim 142234353Sdim /// \brief Default constructor is the same as an empty string and leaves all 143234353Sdim /// triple fields unknown. 144234353Sdim Triple() : Data(), Arch(), Vendor(), OS(), Environment() {} 145193323Sed 146234353Sdim explicit Triple(const Twine &Str); 147234353Sdim Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr); 148226633Sdim Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr, 149234353Sdim const Twine &EnvironmentStr); 150218893Sdim 151193323Sed /// @} 152212904Sdim /// @name Normalization 153212904Sdim /// @{ 154212904Sdim 155212904Sdim /// normalize - Turn an arbitrary machine specification into the canonical 156212904Sdim /// triple form (or something sensible that the Triple class understands if 157212904Sdim /// nothing better can reasonably be done). In particular, it handles the 158212904Sdim /// common case in which otherwise valid components are in the wrong order. 159212904Sdim static std::string normalize(StringRef Str); 160212904Sdim 161212904Sdim /// @} 162193323Sed /// @name Typed Component Access 163193323Sed /// @{ 164218893Sdim 165193323Sed /// getArch - Get the parsed architecture type of this triple. 166234353Sdim ArchType getArch() const { return Arch; } 167218893Sdim 168193323Sed /// getVendor - Get the parsed vendor type of this triple. 169234353Sdim VendorType getVendor() const { return Vendor; } 170218893Sdim 171193323Sed /// getOS - Get the parsed operating system type of this triple. 172234353Sdim OSType getOS() const { return OS; } 173193323Sed 174193323Sed /// hasEnvironment - Does this triple have the optional environment 175193323Sed /// (fourth) component? 176193323Sed bool hasEnvironment() const { 177193323Sed return getEnvironmentName() != ""; 178193323Sed } 179193323Sed 180218893Sdim /// getEnvironment - Get the parsed environment type of this triple. 181234353Sdim EnvironmentType getEnvironment() const { return Environment; } 182234353Sdim 183234353Sdim /// getOSVersion - Parse the version number from the OS name component of the 184234353Sdim /// triple, if present. 185234353Sdim /// 186234353Sdim /// For example, "fooos1.2.3" would return (1, 2, 3). 187234353Sdim /// 188234353Sdim /// If an entry is not defined, it will be returned as 0. 189234353Sdim void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const; 190234353Sdim 191234353Sdim /// getOSMajorVersion - Return just the major version number, this is 192234353Sdim /// specialized because it is a common query. 193234353Sdim unsigned getOSMajorVersion() const { 194234353Sdim unsigned Maj, Min, Micro; 195234353Sdim getOSVersion(Maj, Min, Micro); 196234353Sdim return Maj; 197218893Sdim } 198218893Sdim 199234353Sdim /// getMacOSXVersion - Parse the version number as with getOSVersion and then 200234353Sdim /// translate generic "darwin" versions to the corresponding OS X versions. 201234353Sdim /// This may also be called with IOS triples but the OS X version number is 202234353Sdim /// just set to a constant 10.4.0 in that case. Returns true if successful. 203234353Sdim bool getMacOSXVersion(unsigned &Major, unsigned &Minor, 204234353Sdim unsigned &Micro) const; 205234353Sdim 206239462Sdim /// getiOSVersion - Parse the version number as with getOSVersion. This should 207239462Sdim /// only be called with IOS triples. 208239462Sdim void getiOSVersion(unsigned &Major, unsigned &Minor, 209239462Sdim unsigned &Micro) const; 210239462Sdim 211193323Sed /// @} 212193323Sed /// @name Direct Component Access 213193323Sed /// @{ 214193323Sed 215199481Srdivacky const std::string &str() const { return Data; } 216199481Srdivacky 217193323Sed const std::string &getTriple() const { return Data; } 218193323Sed 219193323Sed /// getArchName - Get the architecture (first) component of the 220193323Sed /// triple. 221198090Srdivacky StringRef getArchName() const; 222193323Sed 223193323Sed /// getVendorName - Get the vendor (second) component of the triple. 224198090Srdivacky StringRef getVendorName() const; 225193323Sed 226193323Sed /// getOSName - Get the operating system (third) component of the 227193323Sed /// triple. 228198090Srdivacky StringRef getOSName() const; 229193323Sed 230193323Sed /// getEnvironmentName - Get the optional environment (fourth) 231193323Sed /// component of the triple, or "" if empty. 232198090Srdivacky StringRef getEnvironmentName() const; 233193323Sed 234193323Sed /// getOSAndEnvironmentName - Get the operating system and optional 235193323Sed /// environment components as a single string (separated by a '-' 236193323Sed /// if the environment component is present). 237198090Srdivacky StringRef getOSAndEnvironmentName() const; 238193323Sed 239234353Sdim /// @} 240234353Sdim /// @name Convenience Predicates 241234353Sdim /// @{ 242234353Sdim 243234353Sdim /// \brief Test whether the architecture is 64-bit 244221345Sdim /// 245234353Sdim /// Note that this tests for 64-bit pointer width, and nothing else. Note 246234353Sdim /// that we intentionally expose only three predicates, 64-bit, 32-bit, and 247234353Sdim /// 16-bit. The inner details of pointer width for particular architectures 248234353Sdim /// is not summed up in the triple, and so only a coarse grained predicate 249234353Sdim /// system is provided. 250234353Sdim bool isArch64Bit() const; 251234353Sdim 252234353Sdim /// \brief Test whether the architecture is 32-bit 253221345Sdim /// 254234353Sdim /// Note that this tests for 32-bit pointer width, and nothing else. 255234353Sdim bool isArch32Bit() const; 256218893Sdim 257234353Sdim /// \brief Test whether the architecture is 16-bit 258234353Sdim /// 259234353Sdim /// Note that this tests for 16-bit pointer width, and nothing else. 260234353Sdim bool isArch16Bit() const; 261218893Sdim 262221345Sdim /// isOSVersionLT - Helper function for doing comparisons against version 263221345Sdim /// numbers included in the target triple. 264221345Sdim bool isOSVersionLT(unsigned Major, unsigned Minor = 0, 265221345Sdim unsigned Micro = 0) const { 266221345Sdim unsigned LHS[3]; 267221345Sdim getOSVersion(LHS[0], LHS[1], LHS[2]); 268221345Sdim 269221345Sdim if (LHS[0] != Major) 270221345Sdim return LHS[0] < Major; 271221345Sdim if (LHS[1] != Minor) 272221345Sdim return LHS[1] < Minor; 273221345Sdim if (LHS[2] != Micro) 274221345Sdim return LHS[1] < Micro; 275221345Sdim 276221345Sdim return false; 277221345Sdim } 278221345Sdim 279234353Sdim /// isMacOSXVersionLT - Comparison function for checking OS X version 280234353Sdim /// compatibility, which handles supporting skewed version numbering schemes 281234353Sdim /// used by the "darwin" triples. 282234353Sdim unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0, 283239462Sdim unsigned Micro = 0) const { 284234353Sdim assert(isMacOSX() && "Not an OS X triple!"); 285234353Sdim 286234353Sdim // If this is OS X, expect a sane version number. 287234353Sdim if (getOS() == Triple::MacOSX) 288234353Sdim return isOSVersionLT(Major, Minor, Micro); 289234353Sdim 290234353Sdim // Otherwise, compare to the "Darwin" number. 291234353Sdim assert(Major == 10 && "Unexpected major version"); 292234353Sdim return isOSVersionLT(Minor + 4, Micro, 0); 293234353Sdim } 294234353Sdim 295221345Sdim /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both 296221345Sdim /// "darwin" and "osx" as OS X triples. 297221345Sdim bool isMacOSX() const { 298221345Sdim return getOS() == Triple::Darwin || getOS() == Triple::MacOSX; 299221345Sdim } 300221345Sdim 301249423Sdim /// Is this an iOS triple. 302249423Sdim bool isiOS() const { 303249423Sdim return getOS() == Triple::IOS; 304249423Sdim } 305249423Sdim 306221345Sdim /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS). 307221345Sdim bool isOSDarwin() const { 308249423Sdim return isMacOSX() || isiOS(); 309221345Sdim } 310221345Sdim 311234353Sdim /// \brief Tests for either Cygwin or MinGW OS 312234353Sdim bool isOSCygMing() const { 313234353Sdim return getOS() == Triple::Cygwin || getOS() == Triple::MinGW32; 314234353Sdim } 315234353Sdim 316221345Sdim /// isOSWindows - Is this a "Windows" OS. 317221345Sdim bool isOSWindows() const { 318234353Sdim return getOS() == Triple::Win32 || isOSCygMing(); 319221345Sdim } 320221345Sdim 321249423Sdim /// \brief Tests whether the OS is NaCl (Native Client) 322249423Sdim bool isOSNaCl() const { 323249423Sdim return getOS() == Triple::NaCl; 324249423Sdim } 325249423Sdim 326234353Sdim /// \brief Tests whether the OS uses the ELF binary format. 327234353Sdim bool isOSBinFormatELF() const { 328234353Sdim return !isOSDarwin() && !isOSWindows(); 329234353Sdim } 330221345Sdim 331234353Sdim /// \brief Tests whether the OS uses the COFF binary format. 332234353Sdim bool isOSBinFormatCOFF() const { 333234353Sdim return isOSWindows(); 334234353Sdim } 335221345Sdim 336234353Sdim /// \brief Tests whether the environment is MachO. 337234353Sdim // FIXME: Should this be an OSBinFormat predicate? 338234353Sdim bool isEnvironmentMachO() const { 339234353Sdim return getEnvironment() == Triple::MachO || isOSDarwin(); 340221345Sdim } 341224145Sdim 342193323Sed /// @} 343193323Sed /// @name Mutators 344193323Sed /// @{ 345193323Sed 346193323Sed /// setArch - Set the architecture (first) component of the triple 347193323Sed /// to a known type. 348193323Sed void setArch(ArchType Kind); 349193323Sed 350193323Sed /// setVendor - Set the vendor (second) component of the triple to a 351193323Sed /// known type. 352193323Sed void setVendor(VendorType Kind); 353193323Sed 354193323Sed /// setOS - Set the operating system (third) component of the triple 355193323Sed /// to a known type. 356193323Sed void setOS(OSType Kind); 357193323Sed 358218893Sdim /// setEnvironment - Set the environment (fourth) component of the triple 359218893Sdim /// to a known type. 360218893Sdim void setEnvironment(EnvironmentType Kind); 361218893Sdim 362243830Sdim /// setTriple - Set all components to the new triple \p Str. 363198090Srdivacky void setTriple(const Twine &Str); 364193323Sed 365193323Sed /// setArchName - Set the architecture (first) component of the 366193323Sed /// triple by name. 367199481Srdivacky void setArchName(StringRef Str); 368193323Sed 369193323Sed /// setVendorName - Set the vendor (second) component of the triple 370193323Sed /// by name. 371199481Srdivacky void setVendorName(StringRef Str); 372193323Sed 373193323Sed /// setOSName - Set the operating system (third) component of the 374193323Sed /// triple by name. 375199481Srdivacky void setOSName(StringRef Str); 376193323Sed 377193323Sed /// setEnvironmentName - Set the optional environment (fourth) 378193323Sed /// component of the triple by name. 379199481Srdivacky void setEnvironmentName(StringRef Str); 380193323Sed 381193323Sed /// setOSAndEnvironmentName - Set the operating system and optional 382193323Sed /// environment components with a single string. 383199481Srdivacky void setOSAndEnvironmentName(StringRef Str); 384193323Sed 385210299Sed /// getArchNameForAssembler - Get an architecture name that is understood by 386210299Sed /// the target assembler. 387199481Srdivacky const char *getArchNameForAssembler(); 388199481Srdivacky 389193323Sed /// @} 390234353Sdim /// @name Helpers to build variants of a particular triple. 391234353Sdim /// @{ 392234353Sdim 393234353Sdim /// \brief Form a triple with a 32-bit variant of the current architecture. 394234353Sdim /// 395234353Sdim /// This can be used to move across "families" of architectures where useful. 396234353Sdim /// 397234353Sdim /// \returns A new triple with a 32-bit architecture or an unknown 398234353Sdim /// architecture if no such variant can be found. 399234353Sdim llvm::Triple get32BitArchVariant() const; 400234353Sdim 401234353Sdim /// \brief Form a triple with a 64-bit variant of the current architecture. 402234353Sdim /// 403234353Sdim /// This can be used to move across "families" of architectures where useful. 404234353Sdim /// 405234353Sdim /// \returns A new triple with a 64-bit architecture or an unknown 406234353Sdim /// architecture if no such variant can be found. 407234353Sdim llvm::Triple get64BitArchVariant() const; 408234353Sdim 409234353Sdim /// @} 410193323Sed /// @name Static helpers for IDs. 411193323Sed /// @{ 412193323Sed 413243830Sdim /// getArchTypeName - Get the canonical name for the \p Kind architecture. 414193323Sed static const char *getArchTypeName(ArchType Kind); 415193323Sed 416243830Sdim /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind 417198090Srdivacky /// architecture. This is the prefix used by the architecture specific 418198090Srdivacky /// builtins, and is suitable for passing to \see 419198090Srdivacky /// Intrinsic::getIntrinsicForGCCBuiltin(). 420198090Srdivacky /// 421198090Srdivacky /// \return - The architecture prefix, or 0 if none is defined. 422198090Srdivacky static const char *getArchTypePrefix(ArchType Kind); 423198090Srdivacky 424243830Sdim /// getVendorTypeName - Get the canonical name for the \p Kind vendor. 425193323Sed static const char *getVendorTypeName(VendorType Kind); 426193323Sed 427243830Sdim /// getOSTypeName - Get the canonical name for the \p Kind operating system. 428193323Sed static const char *getOSTypeName(OSType Kind); 429193323Sed 430243830Sdim /// getEnvironmentTypeName - Get the canonical name for the \p Kind 431218893Sdim /// environment. 432218893Sdim static const char *getEnvironmentTypeName(EnvironmentType Kind); 433218893Sdim 434193323Sed /// @} 435198090Srdivacky /// @name Static helpers for converting alternate architecture names. 436198090Srdivacky /// @{ 437198090Srdivacky 438198090Srdivacky /// getArchTypeForLLVMName - The canonical type for the given LLVM 439198090Srdivacky /// architecture name (e.g., "x86"). 440199481Srdivacky static ArchType getArchTypeForLLVMName(StringRef Str); 441198090Srdivacky 442198090Srdivacky /// @} 443193323Sed}; 444193323Sed 445193323Sed} // End llvm namespace 446193323Sed 447193323Sed 448193323Sed#endif 449