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