LLVM API Documentation

Triple.h
Go to the documentation of this file.
00001 //===-- llvm/ADT/Triple.h - Target triple helper class ----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_ADT_TRIPLE_H
00011 #define LLVM_ADT_TRIPLE_H
00012 
00013 #include "llvm/ADT/Twine.h"
00014 
00015 // Some system headers or GCC predefined macros conflict with identifiers in
00016 // this file.  Undefine them here.
00017 #undef NetBSD
00018 #undef mips
00019 #undef sparc
00020 
00021 namespace llvm {
00022 
00023 /// Triple - Helper class for working with autoconf configuration names. For
00024 /// historical reasons, we also call these 'triples' (they used to contain
00025 /// exactly three fields).
00026 ///
00027 /// Configuration names are strings in the canonical form:
00028 ///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM
00029 /// or
00030 ///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
00031 ///
00032 /// This class is used for clients which want to support arbitrary
00033 /// configuration names, but also want to implement certain special
00034 /// behavior for particular configurations. This class isolates the mapping
00035 /// from the components of the configuration name to well known IDs.
00036 ///
00037 /// At its core the Triple class is designed to be a wrapper for a triple
00038 /// string; the constructor does not change or normalize the triple string.
00039 /// Clients that need to handle the non-canonical triples that users often
00040 /// specify should use the normalize method.
00041 ///
00042 /// See autoconf/config.guess for a glimpse into what configuration names
00043 /// look like in practice.
00044 class Triple {
00045 public:
00046   enum ArchType {
00047     UnknownArch,
00048 
00049     arm,        // ARM (little endian): arm, armv.*, xscale
00050     armeb,      // ARM (big endian): armeb
00051     aarch64,    // AArch64 (little endian): aarch64
00052     aarch64_be, // AArch64 (big endian): aarch64_be
00053     hexagon,    // Hexagon: hexagon
00054     mips,       // MIPS: mips, mipsallegrex
00055     mipsel,     // MIPSEL: mipsel, mipsallegrexel
00056     mips64,     // MIPS64: mips64
00057     mips64el,   // MIPS64EL: mips64el
00058     msp430,     // MSP430: msp430
00059     ppc,        // PPC: powerpc
00060     ppc64,      // PPC64: powerpc64, ppu
00061     ppc64le,    // PPC64LE: powerpc64le
00062     r600,       // R600: AMD GPUs HD2XXX - HD6XXX
00063     sparc,      // Sparc: sparc
00064     sparcv9,    // Sparcv9: Sparcv9
00065     systemz,    // SystemZ: s390x
00066     tce,        // TCE (http://tce.cs.tut.fi/): tce
00067     thumb,      // Thumb (little endian): thumb, thumbv.*
00068     thumbeb,    // Thumb (big endian): thumbeb
00069     x86,        // X86: i[3-9]86
00070     x86_64,     // X86-64: amd64, x86_64
00071     xcore,      // XCore: xcore
00072     nvptx,      // NVPTX: 32-bit
00073     nvptx64,    // NVPTX: 64-bit
00074     le32,       // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
00075     le64,       // le64: generic little-endian 64-bit CPU (PNaCl / Emscripten)
00076     amdil,      // amdil: amd IL
00077     spir,       // SPIR: standard portable IR for OpenCL 32-bit version
00078     spir64,     // SPIR: standard portable IR for OpenCL 64-bit version
00079     kalimba     // Kalimba: generic kalimba
00080   };
00081   enum SubArchType {
00082     NoSubArch,
00083 
00084     ARMSubArch_v8,
00085     ARMSubArch_v7,
00086     ARMSubArch_v7em,
00087     ARMSubArch_v7m,
00088     ARMSubArch_v7s,
00089     ARMSubArch_v6,
00090     ARMSubArch_v6m,
00091     ARMSubArch_v6t2,
00092     ARMSubArch_v5,
00093     ARMSubArch_v5te,
00094     ARMSubArch_v4t,
00095 
00096     KalimbaSubArch_v3,
00097     KalimbaSubArch_v4,
00098     KalimbaSubArch_v5
00099   };
00100   enum VendorType {
00101     UnknownVendor,
00102 
00103     Apple,
00104     PC,
00105     SCEI,
00106     BGP,
00107     BGQ,
00108     Freescale,
00109     IBM,
00110     ImaginationTechnologies,
00111     MipsTechnologies,
00112     NVIDIA,
00113     CSR
00114   };
00115   enum OSType {
00116     UnknownOS,
00117 
00118     Darwin,
00119     DragonFly,
00120     FreeBSD,
00121     IOS,
00122     KFreeBSD,
00123     Linux,
00124     Lv2,        // PS3
00125     MacOSX,
00126     NetBSD,
00127     OpenBSD,
00128     Solaris,
00129     Win32,
00130     Haiku,
00131     Minix,
00132     RTEMS,
00133     NaCl,       // Native Client
00134     CNK,        // BG/P Compute-Node Kernel
00135     Bitrig,
00136     AIX,
00137     CUDA,       // NVIDIA CUDA
00138     NVCL        // NVIDIA OpenCL
00139   };
00140   enum EnvironmentType {
00141     UnknownEnvironment,
00142 
00143     GNU,
00144     GNUEABI,
00145     GNUEABIHF,
00146     GNUX32,
00147     CODE16,
00148     EABI,
00149     EABIHF,
00150     Android,
00151 
00152     MSVC,
00153     Itanium,
00154     Cygnus,
00155   };
00156   enum ObjectFormatType {
00157     UnknownObjectFormat,
00158 
00159     COFF,
00160     ELF,
00161     MachO,
00162   };
00163 
00164 private:
00165   std::string Data;
00166 
00167   /// The parsed arch type.
00168   ArchType Arch;
00169 
00170   /// The parsed subarchitecture type.
00171   SubArchType SubArch;
00172 
00173   /// The parsed vendor type.
00174   VendorType Vendor;
00175 
00176   /// The parsed OS type.
00177   OSType OS;
00178 
00179   /// The parsed Environment type.
00180   EnvironmentType Environment;
00181 
00182   /// The object format type.
00183   ObjectFormatType ObjectFormat;
00184 
00185 public:
00186   /// @name Constructors
00187   /// @{
00188 
00189   /// \brief Default constructor is the same as an empty string and leaves all
00190   /// triple fields unknown.
00191   Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
00192 
00193   explicit Triple(const Twine &Str);
00194   Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
00195   Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
00196          const Twine &EnvironmentStr);
00197 
00198   /// @}
00199   /// @name Normalization
00200   /// @{
00201 
00202   /// normalize - Turn an arbitrary machine specification into the canonical
00203   /// triple form (or something sensible that the Triple class understands if
00204   /// nothing better can reasonably be done).  In particular, it handles the
00205   /// common case in which otherwise valid components are in the wrong order.
00206   static std::string normalize(StringRef Str);
00207 
00208   /// @}
00209   /// @name Typed Component Access
00210   /// @{
00211 
00212   /// getArch - Get the parsed architecture type of this triple.
00213   ArchType getArch() const { return Arch; }
00214 
00215   /// getSubArch - get the parsed subarchitecture type for this triple.
00216   SubArchType getSubArch() const { return SubArch; }
00217 
00218   /// getVendor - Get the parsed vendor type of this triple.
00219   VendorType getVendor() const { return Vendor; }
00220 
00221   /// getOS - Get the parsed operating system type of this triple.
00222   OSType getOS() const { return OS; }
00223 
00224   /// hasEnvironment - Does this triple have the optional environment
00225   /// (fourth) component?
00226   bool hasEnvironment() const {
00227     return getEnvironmentName() != "";
00228   }
00229 
00230   /// getEnvironment - Get the parsed environment type of this triple.
00231   EnvironmentType getEnvironment() const { return Environment; }
00232 
00233   /// getFormat - Get the object format for this triple.
00234   ObjectFormatType getObjectFormat() const { return ObjectFormat; }
00235 
00236   /// getOSVersion - Parse the version number from the OS name component of the
00237   /// triple, if present.
00238   ///
00239   /// For example, "fooos1.2.3" would return (1, 2, 3).
00240   ///
00241   /// If an entry is not defined, it will be returned as 0.
00242   void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
00243 
00244   /// getOSMajorVersion - Return just the major version number, this is
00245   /// specialized because it is a common query.
00246   unsigned getOSMajorVersion() const {
00247     unsigned Maj, Min, Micro;
00248     getOSVersion(Maj, Min, Micro);
00249     return Maj;
00250   }
00251 
00252   /// getMacOSXVersion - Parse the version number as with getOSVersion and then
00253   /// translate generic "darwin" versions to the corresponding OS X versions.
00254   /// This may also be called with IOS triples but the OS X version number is
00255   /// just set to a constant 10.4.0 in that case.  Returns true if successful.
00256   bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
00257                         unsigned &Micro) const;
00258 
00259   /// getiOSVersion - Parse the version number as with getOSVersion.  This should
00260   /// only be called with IOS triples.
00261   void getiOSVersion(unsigned &Major, unsigned &Minor,
00262                      unsigned &Micro) const;
00263 
00264   /// @}
00265   /// @name Direct Component Access
00266   /// @{
00267 
00268   const std::string &str() const { return Data; }
00269 
00270   const std::string &getTriple() const { return Data; }
00271 
00272   /// getArchName - Get the architecture (first) component of the
00273   /// triple.
00274   StringRef getArchName() const;
00275 
00276   /// getVendorName - Get the vendor (second) component of the triple.
00277   StringRef getVendorName() const;
00278 
00279   /// getOSName - Get the operating system (third) component of the
00280   /// triple.
00281   StringRef getOSName() const;
00282 
00283   /// getEnvironmentName - Get the optional environment (fourth)
00284   /// component of the triple, or "" if empty.
00285   StringRef getEnvironmentName() const;
00286 
00287   /// getOSAndEnvironmentName - Get the operating system and optional
00288   /// environment components as a single string (separated by a '-'
00289   /// if the environment component is present).
00290   StringRef getOSAndEnvironmentName() const;
00291 
00292   /// @}
00293   /// @name Convenience Predicates
00294   /// @{
00295 
00296   /// \brief Test whether the architecture is 64-bit
00297   ///
00298   /// Note that this tests for 64-bit pointer width, and nothing else. Note
00299   /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
00300   /// 16-bit. The inner details of pointer width for particular architectures
00301   /// is not summed up in the triple, and so only a coarse grained predicate
00302   /// system is provided.
00303   bool isArch64Bit() const;
00304 
00305   /// \brief Test whether the architecture is 32-bit
00306   ///
00307   /// Note that this tests for 32-bit pointer width, and nothing else.
00308   bool isArch32Bit() const;
00309 
00310   /// \brief Test whether the architecture is 16-bit
00311   ///
00312   /// Note that this tests for 16-bit pointer width, and nothing else.
00313   bool isArch16Bit() const;
00314 
00315   /// isOSVersionLT - Helper function for doing comparisons against version
00316   /// numbers included in the target triple.
00317   bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
00318                      unsigned Micro = 0) const {
00319     unsigned LHS[3];
00320     getOSVersion(LHS[0], LHS[1], LHS[2]);
00321 
00322     if (LHS[0] != Major)
00323       return LHS[0] < Major;
00324     if (LHS[1] != Minor)
00325       return LHS[1] < Minor;
00326     if (LHS[2] != Micro)
00327       return LHS[1] < Micro;
00328 
00329     return false;
00330   }
00331 
00332   /// isMacOSXVersionLT - Comparison function for checking OS X version
00333   /// compatibility, which handles supporting skewed version numbering schemes
00334   /// used by the "darwin" triples.
00335   unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
00336                              unsigned Micro = 0) const {
00337     assert(isMacOSX() && "Not an OS X triple!");
00338 
00339     // If this is OS X, expect a sane version number.
00340     if (getOS() == Triple::MacOSX)
00341       return isOSVersionLT(Major, Minor, Micro);
00342 
00343     // Otherwise, compare to the "Darwin" number.
00344     assert(Major == 10 && "Unexpected major version");
00345     return isOSVersionLT(Minor + 4, Micro, 0);
00346   }
00347 
00348   /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
00349   /// "darwin" and "osx" as OS X triples.
00350   bool isMacOSX() const {
00351     return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
00352   }
00353 
00354   /// Is this an iOS triple.
00355   bool isiOS() const {
00356     return getOS() == Triple::IOS;
00357   }
00358 
00359   /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
00360   bool isOSDarwin() const {
00361     return isMacOSX() || isiOS();
00362   }
00363 
00364   bool isOSFreeBSD() const {
00365     return getOS() == Triple::FreeBSD;
00366   }
00367 
00368   bool isWindowsMSVCEnvironment() const {
00369     return getOS() == Triple::Win32 &&
00370            (getEnvironment() == Triple::UnknownEnvironment ||
00371             getEnvironment() == Triple::MSVC);
00372   }
00373 
00374   bool isKnownWindowsMSVCEnvironment() const {
00375     return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
00376   }
00377 
00378   bool isWindowsItaniumEnvironment() const {
00379     return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
00380   }
00381 
00382   bool isWindowsCygwinEnvironment() const {
00383     return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
00384   }
00385 
00386   bool isWindowsGNUEnvironment() const {
00387     return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
00388   }
00389 
00390   /// \brief Tests for either Cygwin or MinGW OS
00391   bool isOSCygMing() const {
00392     return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
00393   }
00394 
00395   /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
00396   bool isOSMSVCRT() const {
00397     return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment();
00398   }
00399 
00400   /// \brief Tests whether the OS is Windows.
00401   bool isOSWindows() const {
00402     return getOS() == Triple::Win32 || isOSCygMing();
00403   }
00404 
00405   /// \brief Tests whether the OS is NaCl (Native Client)
00406   bool isOSNaCl() const {
00407     return getOS() == Triple::NaCl;
00408   }
00409 
00410   /// \brief Tests whether the OS is Linux.
00411   bool isOSLinux() const {
00412     return getOS() == Triple::Linux;
00413   }
00414 
00415   /// \brief Tests whether the OS uses the ELF binary format.
00416   bool isOSBinFormatELF() const {
00417     return getObjectFormat() == Triple::ELF;
00418   }
00419 
00420   /// \brief Tests whether the OS uses the COFF binary format.
00421   bool isOSBinFormatCOFF() const {
00422     return getObjectFormat() == Triple::COFF;
00423   }
00424 
00425   /// \brief Tests whether the environment is MachO.
00426   bool isOSBinFormatMachO() const {
00427     return getObjectFormat() == Triple::MachO;
00428   }
00429 
00430   /// @}
00431   /// @name Mutators
00432   /// @{
00433 
00434   /// setArch - Set the architecture (first) component of the triple
00435   /// to a known type.
00436   void setArch(ArchType Kind);
00437 
00438   /// setVendor - Set the vendor (second) component of the triple to a
00439   /// known type.
00440   void setVendor(VendorType Kind);
00441 
00442   /// setOS - Set the operating system (third) component of the triple
00443   /// to a known type.
00444   void setOS(OSType Kind);
00445 
00446   /// setEnvironment - Set the environment (fourth) component of the triple
00447   /// to a known type.
00448   void setEnvironment(EnvironmentType Kind);
00449 
00450   /// setObjectFormat - Set the object file format
00451   void setObjectFormat(ObjectFormatType Kind);
00452 
00453   /// setTriple - Set all components to the new triple \p Str.
00454   void setTriple(const Twine &Str);
00455 
00456   /// setArchName - Set the architecture (first) component of the
00457   /// triple by name.
00458   void setArchName(StringRef Str);
00459 
00460   /// setVendorName - Set the vendor (second) component of the triple
00461   /// by name.
00462   void setVendorName(StringRef Str);
00463 
00464   /// setOSName - Set the operating system (third) component of the
00465   /// triple by name.
00466   void setOSName(StringRef Str);
00467 
00468   /// setEnvironmentName - Set the optional environment (fourth)
00469   /// component of the triple by name.
00470   void setEnvironmentName(StringRef Str);
00471 
00472   /// setOSAndEnvironmentName - Set the operating system and optional
00473   /// environment components with a single string.
00474   void setOSAndEnvironmentName(StringRef Str);
00475 
00476   /// @}
00477   /// @name Helpers to build variants of a particular triple.
00478   /// @{
00479 
00480   /// \brief Form a triple with a 32-bit variant of the current architecture.
00481   ///
00482   /// This can be used to move across "families" of architectures where useful.
00483   ///
00484   /// \returns A new triple with a 32-bit architecture or an unknown
00485   ///          architecture if no such variant can be found.
00486   llvm::Triple get32BitArchVariant() const;
00487 
00488   /// \brief Form a triple with a 64-bit variant of the current architecture.
00489   ///
00490   /// This can be used to move across "families" of architectures where useful.
00491   ///
00492   /// \returns A new triple with a 64-bit architecture or an unknown
00493   ///          architecture if no such variant can be found.
00494   llvm::Triple get64BitArchVariant() const;
00495 
00496   /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
00497   ///
00498   /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
00499   /// string then the triple's arch name is used.
00500   const char* getARMCPUForArch(StringRef Arch = StringRef()) const;
00501 
00502   /// @}
00503   /// @name Static helpers for IDs.
00504   /// @{
00505 
00506   /// getArchTypeName - Get the canonical name for the \p Kind architecture.
00507   static const char *getArchTypeName(ArchType Kind);
00508 
00509   /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
00510   /// architecture. This is the prefix used by the architecture specific
00511   /// builtins, and is suitable for passing to \see
00512   /// Intrinsic::getIntrinsicForGCCBuiltin().
00513   ///
00514   /// \return - The architecture prefix, or 0 if none is defined.
00515   static const char *getArchTypePrefix(ArchType Kind);
00516 
00517   /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
00518   static const char *getVendorTypeName(VendorType Kind);
00519 
00520   /// getOSTypeName - Get the canonical name for the \p Kind operating system.
00521   static const char *getOSTypeName(OSType Kind);
00522 
00523   /// getEnvironmentTypeName - Get the canonical name for the \p Kind
00524   /// environment.
00525   static const char *getEnvironmentTypeName(EnvironmentType Kind);
00526 
00527   /// @}
00528   /// @name Static helpers for converting alternate architecture names.
00529   /// @{
00530 
00531   /// getArchTypeForLLVMName - The canonical type for the given LLVM
00532   /// architecture name (e.g., "x86").
00533   static ArchType getArchTypeForLLVMName(StringRef Str);
00534 
00535   /// @}
00536 };
00537 
00538 } // End llvm namespace
00539 
00540 
00541 #endif