Triple.h revision 251662
1139749Simp//===-- llvm/ADT/Triple.h - Target triple helper class ----------*- C++ -*-===// 2135048Swpaul// 3135048Swpaul// The LLVM Compiler Infrastructure 4135048Swpaul// 5135048Swpaul// This file is distributed under the University of Illinois Open Source 6135048Swpaul// License. See LICENSE.TXT for details. 7135048Swpaul// 8135048Swpaul//===----------------------------------------------------------------------===// 9135048Swpaul 10135048Swpaul#ifndef LLVM_ADT_TRIPLE_H 11135048Swpaul#define LLVM_ADT_TRIPLE_H 12135048Swpaul 13135048Swpaul#include "llvm/ADT/Twine.h" 14135048Swpaul 15135048Swpaul// Some system headers or GCC predefined macros conflict with identifiers in 16135048Swpaul// this file. Undefine them here. 17135048Swpaul#undef mips 18135048Swpaul#undef sparc 19135048Swpaul 20135048Swpaulnamespace llvm { 21135048Swpaul 22135048Swpaul/// Triple - Helper class for working with target triples. 23135048Swpaul/// 24135048Swpaul/// Target triples are strings in the canonical form: 25135048Swpaul/// ARCHITECTURE-VENDOR-OPERATING_SYSTEM 26135048Swpaul/// or 27135048Swpaul/// ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT 28135048Swpaul/// 29135048Swpaul/// This class is used for clients which want to support arbitrary 30135048Swpaul/// target triples, but also want to implement certain special 31135048Swpaul/// behavior for particular targets. This class isolates the mapping 32135048Swpaul/// from the components of the target triple to well known IDs. 33135048Swpaul/// 34135048Swpaul/// At its core the Triple class is designed to be a wrapper for a triple 35135048Swpaul/// string; the constructor does not change or normalize the triple string. 36135048Swpaul/// Clients that need to handle the non-canonical triples that users often 37135048Swpaul/// specify should use the normalize method. 38135048Swpaul/// 39135048Swpaul/// See autoconf/config.guess for a glimpse into what triples look like in 40135048Swpaul/// practice. 41135048Swpaulclass Triple { 42135048Swpaulpublic: 43135048Swpaul enum ArchType { 44135048Swpaul UnknownArch, 45135048Swpaul 46135048Swpaul arm, // ARM: arm, armv.*, xscale 47135048Swpaul aarch64, // AArch64: aarch64 48135048Swpaul hexagon, // Hexagon: hexagon 49135048Swpaul mips, // MIPS: mips, mipsallegrex 50135048Swpaul mipsel, // MIPSEL: mipsel, mipsallegrexel 51135048Swpaul mips64, // MIPS64: mips64 52135048Swpaul mips64el,// MIPS64EL: mips64el 53135048Swpaul msp430, // MSP430: msp430 54135048Swpaul ppc, // PPC: powerpc 55135048Swpaul ppc64, // PPC64: powerpc64, ppu 56135048Swpaul r600, // R600: AMD GPUs HD2XXX - HD6XXX 57135048Swpaul sparc, // Sparc: sparc 58135048Swpaul sparcv9, // Sparcv9: Sparcv9 59135048Swpaul systemz, // SystemZ: s390x 60135048Swpaul tce, // TCE (http://tce.cs.tut.fi/): tce 61135048Swpaul thumb, // Thumb: thumb, thumbv.* 62135048Swpaul x86, // X86: i[3-9]86 63135048Swpaul x86_64, // X86-64: amd64, x86_64 64135048Swpaul xcore, // XCore: xcore 65135048Swpaul mblaze, // MBlaze: mblaze 66135048Swpaul nvptx, // NVPTX: 32-bit 67135048Swpaul nvptx64, // NVPTX: 64-bit 68135048Swpaul le32, // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten) 69135048Swpaul amdil, // amdil: amd IL 70135048Swpaul spir, // SPIR: standard portable IR for OpenCL 32-bit version 71220938Smarius spir64 // SPIR: standard portable IR for OpenCL 64-bit version 72135048Swpaul }; 73135048Swpaul enum VendorType { 74135048Swpaul UnknownVendor, 75135048Swpaul 76135048Swpaul Apple, 77135048Swpaul PC, 78135048Swpaul SCEI, 79135048Swpaul BGP, 80135048Swpaul BGQ, 81135048Swpaul Freescale, 82135048Swpaul IBM 83135048Swpaul }; 84135048Swpaul enum OSType { 85135048Swpaul UnknownOS, 86135048Swpaul 87135048Swpaul AuroraUX, 88135048Swpaul Cygwin, 89135048Swpaul Darwin, 90135048Swpaul DragonFly, 91135048Swpaul FreeBSD, 92135048Swpaul IOS, 93135048Swpaul KFreeBSD, 94135048Swpaul Linux, 95135048Swpaul Lv2, // PS3 96135048Swpaul MacOSX, 97135048Swpaul MinGW32, // i*86-pc-mingw32, *-w64-mingw32 98135048Swpaul NetBSD, 99135048Swpaul OpenBSD, 100135048Swpaul Solaris, 101135048Swpaul Win32, 102135048Swpaul Haiku, 103135048Swpaul Minix, 104135048Swpaul RTEMS, 105135048Swpaul NaCl, // Native Client 106135048Swpaul CNK, // BG/P Compute-Node Kernel 107135048Swpaul Bitrig, 108135048Swpaul AIX 109135048Swpaul }; 110135048Swpaul enum EnvironmentType { 111135048Swpaul UnknownEnvironment, 112135048Swpaul 113135048Swpaul GNU, 114135048Swpaul GNUEABI, 115135048Swpaul GNUEABIHF, 116135048Swpaul GNUX32, 117135048Swpaul EABI, 118135048Swpaul MachO, 119135048Swpaul Android, 120135048Swpaul ELF 121135048Swpaul }; 122135048Swpaul 123135048Swpaulprivate: 124135048Swpaul std::string Data; 125135048Swpaul 126135048Swpaul /// The parsed arch type. 127135048Swpaul ArchType Arch; 128135048Swpaul 129135048Swpaul /// The parsed vendor type. 130135048Swpaul VendorType Vendor; 131135048Swpaul 132135048Swpaul /// The parsed OS type. 133135048Swpaul OSType OS; 134135048Swpaul 135135048Swpaul /// The parsed Environment type. 136135048Swpaul EnvironmentType Environment; 137135048Swpaul 138135048Swpaulpublic: 139135048Swpaul /// @name Constructors 140135048Swpaul /// @{ 141135048Swpaul 142135048Swpaul /// \brief Default constructor is the same as an empty string and leaves all 143135048Swpaul /// triple fields unknown. 144135048Swpaul Triple() : Data(), Arch(), Vendor(), OS(), Environment() {} 145135048Swpaul 146135048Swpaul explicit Triple(const Twine &Str); 147135048Swpaul Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr); 148135048Swpaul Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr, 149135048Swpaul const Twine &EnvironmentStr); 150135048Swpaul 151135048Swpaul /// @} 152135048Swpaul /// @name Normalization 153135048Swpaul /// @{ 154135048Swpaul 155135048Swpaul /// normalize - Turn an arbitrary machine specification into the canonical 156135048Swpaul /// triple form (or something sensible that the Triple class understands if 157135048Swpaul /// nothing better can reasonably be done). In particular, it handles the 158135048Swpaul /// common case in which otherwise valid components are in the wrong order. 159135048Swpaul static std::string normalize(StringRef Str); 160135048Swpaul 161135048Swpaul /// @} 162135048Swpaul /// @name Typed Component Access 163135048Swpaul /// @{ 164135048Swpaul 165135048Swpaul /// getArch - Get the parsed architecture type of this triple. 166135048Swpaul ArchType getArch() const { return Arch; } 167135048Swpaul 168135048Swpaul /// getVendor - Get the parsed vendor type of this triple. 169135048Swpaul VendorType getVendor() const { return Vendor; } 170135048Swpaul 171135048Swpaul /// getOS - Get the parsed operating system type of this triple. 172135048Swpaul OSType getOS() const { return OS; } 173220938Smarius 174135048Swpaul /// hasEnvironment - Does this triple have the optional environment 175135048Swpaul /// (fourth) component? 176135048Swpaul bool hasEnvironment() const { 177135048Swpaul return getEnvironmentName() != ""; 178135048Swpaul } 179135048Swpaul 180135048Swpaul /// getEnvironment - Get the parsed environment type of this triple. 181135048Swpaul EnvironmentType getEnvironment() const { return Environment; } 182135048Swpaul 183135048Swpaul /// getOSVersion - Parse the version number from the OS name component of the 184220938Smarius /// triple, if present. 185135048Swpaul /// 186135048Swpaul /// For example, "fooos1.2.3" would return (1, 2, 3). 187135048Swpaul /// 188135048Swpaul /// If an entry is not defined, it will be returned as 0. 189135048Swpaul void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const; 190135048Swpaul 191135048Swpaul /// getOSMajorVersion - Return just the major version number, this is 192135048Swpaul /// specialized because it is a common query. 193135048Swpaul unsigned getOSMajorVersion() const { 194220938Smarius unsigned Maj, Min, Micro; 195135048Swpaul getOSVersion(Maj, Min, Micro); 196135048Swpaul return Maj; 197135048Swpaul } 198135048Swpaul 199135048Swpaul /// getMacOSXVersion - Parse the version number as with getOSVersion and then 200135048Swpaul /// translate generic "darwin" versions to the corresponding OS X versions. 201135048Swpaul /// This may also be called with IOS triples but the OS X version number is 202135048Swpaul /// just set to a constant 10.4.0 in that case. Returns true if successful. 203135048Swpaul bool getMacOSXVersion(unsigned &Major, unsigned &Minor, 204135048Swpaul unsigned &Micro) const; 205135048Swpaul 206135048Swpaul /// getiOSVersion - Parse the version number as with getOSVersion. This should 207135048Swpaul /// only be called with IOS triples. 208135048Swpaul void getiOSVersion(unsigned &Major, unsigned &Minor, 209135048Swpaul unsigned &Micro) const; 210135048Swpaul 211135048Swpaul /// @} 212135048Swpaul /// @name Direct Component Access 213135048Swpaul /// @{ 214135048Swpaul 215135048Swpaul const std::string &str() const { return Data; } 216135048Swpaul 217135048Swpaul const std::string &getTriple() const { return Data; } 218135048Swpaul 219135048Swpaul /// getArchName - Get the architecture (first) component of the 220135048Swpaul /// triple. 221135048Swpaul StringRef getArchName() const; 222135048Swpaul 223135048Swpaul /// getVendorName - Get the vendor (second) component of the triple. 224135048Swpaul StringRef getVendorName() const; 225135048Swpaul 226135048Swpaul /// getOSName - Get the operating system (third) component of the 227135048Swpaul /// triple. 228135048Swpaul StringRef getOSName() const; 229135048Swpaul 230135048Swpaul /// getEnvironmentName - Get the optional environment (fourth) 231135048Swpaul /// component of the triple, or "" if empty. 232135048Swpaul StringRef getEnvironmentName() const; 233135048Swpaul 234135048Swpaul /// getOSAndEnvironmentName - Get the operating system and optional 235135048Swpaul /// environment components as a single string (separated by a '-' 236135048Swpaul /// if the environment component is present). 237135048Swpaul StringRef getOSAndEnvironmentName() const; 238135048Swpaul 239135048Swpaul /// @} 240135048Swpaul /// @name Convenience Predicates 241135048Swpaul /// @{ 242135048Swpaul 243135048Swpaul /// \brief Test whether the architecture is 64-bit 244135048Swpaul /// 245135048Swpaul /// Note that this tests for 64-bit pointer width, and nothing else. Note 246135048Swpaul /// that we intentionally expose only three predicates, 64-bit, 32-bit, and 247135048Swpaul /// 16-bit. The inner details of pointer width for particular architectures 248135048Swpaul /// is not summed up in the triple, and so only a coarse grained predicate 249135048Swpaul /// system is provided. 250135048Swpaul bool isArch64Bit() const; 251135048Swpaul 252135048Swpaul /// \brief Test whether the architecture is 32-bit 253135048Swpaul /// 254170365Syongari /// Note that this tests for 32-bit pointer width, and nothing else. 255170365Syongari bool isArch32Bit() const; 256135048Swpaul 257170365Syongari /// \brief Test whether the architecture is 16-bit 258170365Syongari /// 259170365Syongari /// Note that this tests for 16-bit pointer width, and nothing else. 260170365Syongari bool isArch16Bit() const; 261170365Syongari 262170365Syongari /// isOSVersionLT - Helper function for doing comparisons against version 263170365Syongari /// numbers included in the target triple. 264170365Syongari bool isOSVersionLT(unsigned Major, unsigned Minor = 0, 265135048Swpaul unsigned Micro = 0) const { 266135048Swpaul unsigned LHS[3]; 267135048Swpaul getOSVersion(LHS[0], LHS[1], LHS[2]); 268135048Swpaul 269135048Swpaul if (LHS[0] != Major) 270135048Swpaul return LHS[0] < Major; 271135048Swpaul if (LHS[1] != Minor) 272135048Swpaul return LHS[1] < Minor; 273135048Swpaul if (LHS[2] != Micro) 274135048Swpaul return LHS[1] < Micro; 275135048Swpaul 276135048Swpaul return false; 277135048Swpaul } 278135048Swpaul 279135048Swpaul /// isMacOSXVersionLT - Comparison function for checking OS X version 280135048Swpaul /// compatibility, which handles supporting skewed version numbering schemes 281135048Swpaul /// used by the "darwin" triples. 282135048Swpaul unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0, 283135048Swpaul unsigned Micro = 0) const { 284135048Swpaul assert(isMacOSX() && "Not an OS X triple!"); 285135048Swpaul 286135048Swpaul // If this is OS X, expect a sane version number. 287135048Swpaul if (getOS() == Triple::MacOSX) 288135048Swpaul return isOSVersionLT(Major, Minor, Micro); 289135048Swpaul 290135048Swpaul // Otherwise, compare to the "Darwin" number. 291135048Swpaul assert(Major == 10 && "Unexpected major version"); 292135048Swpaul return isOSVersionLT(Minor + 4, Micro, 0); 293135048Swpaul } 294135048Swpaul 295135048Swpaul /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both 296135048Swpaul /// "darwin" and "osx" as OS X triples. 297135048Swpaul bool isMacOSX() const { 298135048Swpaul return getOS() == Triple::Darwin || getOS() == Triple::MacOSX; 299135048Swpaul } 300135048Swpaul 301135048Swpaul /// Is this an iOS triple. 302135048Swpaul bool isiOS() const { 303135048Swpaul return getOS() == Triple::IOS; 304135048Swpaul } 305135048Swpaul 306135048Swpaul /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS). 307135048Swpaul bool isOSDarwin() const { 308135048Swpaul return isMacOSX() || isiOS(); 309135048Swpaul } 310135048Swpaul 311135048Swpaul /// \brief Tests for either Cygwin or MinGW OS 312135048Swpaul bool isOSCygMing() const { 313135048Swpaul return getOS() == Triple::Cygwin || getOS() == Triple::MinGW32; 314135048Swpaul } 315135048Swpaul 316135048Swpaul /// isOSWindows - Is this a "Windows" OS. 317135048Swpaul bool isOSWindows() const { 318135048Swpaul return getOS() == Triple::Win32 || isOSCygMing(); 319135048Swpaul } 320135048Swpaul 321135048Swpaul /// \brief Tests whether the OS is NaCl (Native Client) 322135048Swpaul bool isOSNaCl() const { 323135048Swpaul return getOS() == Triple::NaCl; 324135048Swpaul } 325135048Swpaul 326135048Swpaul /// \brief Tests whether the OS uses the ELF binary format. 327135048Swpaul bool isOSBinFormatELF() const { 328135048Swpaul return !isOSDarwin() && !isOSWindows(); 329135048Swpaul } 330135048Swpaul 331135048Swpaul /// \brief Tests whether the OS uses the COFF binary format. 332135048Swpaul bool isOSBinFormatCOFF() const { 333135048Swpaul return isOSWindows(); 334135048Swpaul } 335220938Smarius 336135048Swpaul /// \brief Tests whether the environment is MachO. 337135048Swpaul // FIXME: Should this be an OSBinFormat predicate? 338135048Swpaul bool isEnvironmentMachO() const { 339135048Swpaul return getEnvironment() == Triple::MachO || isOSDarwin(); 340135048Swpaul } 341135048Swpaul 342135048Swpaul /// @} 343135048Swpaul /// @name Mutators 344135048Swpaul /// @{ 345135048Swpaul 346135048Swpaul /// setArch - Set the architecture (first) component of the triple 347135048Swpaul /// to a known type. 348135048Swpaul void setArch(ArchType Kind); 349135048Swpaul 350135048Swpaul /// setVendor - Set the vendor (second) component of the triple to a 351135048Swpaul /// known type. 352135048Swpaul void setVendor(VendorType Kind); 353135048Swpaul 354135048Swpaul /// setOS - Set the operating system (third) component of the triple 355135048Swpaul /// to a known type. 356135048Swpaul void setOS(OSType Kind); 357135048Swpaul 358135048Swpaul /// setEnvironment - Set the environment (fourth) component of the triple 359135048Swpaul /// to a known type. 360135048Swpaul void setEnvironment(EnvironmentType Kind); 361135048Swpaul 362 /// setTriple - Set all components to the new triple \p Str. 363 void setTriple(const Twine &Str); 364 365 /// setArchName - Set the architecture (first) component of the 366 /// triple by name. 367 void setArchName(StringRef Str); 368 369 /// setVendorName - Set the vendor (second) component of the triple 370 /// by name. 371 void setVendorName(StringRef Str); 372 373 /// setOSName - Set the operating system (third) component of the 374 /// triple by name. 375 void setOSName(StringRef Str); 376 377 /// setEnvironmentName - Set the optional environment (fourth) 378 /// component of the triple by name. 379 void setEnvironmentName(StringRef Str); 380 381 /// setOSAndEnvironmentName - Set the operating system and optional 382 /// environment components with a single string. 383 void setOSAndEnvironmentName(StringRef Str); 384 385 /// getArchNameForAssembler - Get an architecture name that is understood by 386 /// the target assembler. 387 const char *getArchNameForAssembler(); 388 389 /// @} 390 /// @name Helpers to build variants of a particular triple. 391 /// @{ 392 393 /// \brief Form a triple with a 32-bit variant of the current architecture. 394 /// 395 /// This can be used to move across "families" of architectures where useful. 396 /// 397 /// \returns A new triple with a 32-bit architecture or an unknown 398 /// architecture if no such variant can be found. 399 llvm::Triple get32BitArchVariant() const; 400 401 /// \brief Form a triple with a 64-bit variant of the current architecture. 402 /// 403 /// This can be used to move across "families" of architectures where useful. 404 /// 405 /// \returns A new triple with a 64-bit architecture or an unknown 406 /// architecture if no such variant can be found. 407 llvm::Triple get64BitArchVariant() const; 408 409 /// @} 410 /// @name Static helpers for IDs. 411 /// @{ 412 413 /// getArchTypeName - Get the canonical name for the \p Kind architecture. 414 static const char *getArchTypeName(ArchType Kind); 415 416 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind 417 /// architecture. This is the prefix used by the architecture specific 418 /// builtins, and is suitable for passing to \see 419 /// Intrinsic::getIntrinsicForGCCBuiltin(). 420 /// 421 /// \return - The architecture prefix, or 0 if none is defined. 422 static const char *getArchTypePrefix(ArchType Kind); 423 424 /// getVendorTypeName - Get the canonical name for the \p Kind vendor. 425 static const char *getVendorTypeName(VendorType Kind); 426 427 /// getOSTypeName - Get the canonical name for the \p Kind operating system. 428 static const char *getOSTypeName(OSType Kind); 429 430 /// getEnvironmentTypeName - Get the canonical name for the \p Kind 431 /// environment. 432 static const char *getEnvironmentTypeName(EnvironmentType Kind); 433 434 /// @} 435 /// @name Static helpers for converting alternate architecture names. 436 /// @{ 437 438 /// getArchTypeForLLVMName - The canonical type for the given LLVM 439 /// architecture name (e.g., "x86"). 440 static ArchType getArchTypeForLLVMName(StringRef Str); 441 442 /// @} 443}; 444 445} // End llvm namespace 446 447 448#endif 449