Home | History | Annotate | Download | only in ADT
      1 //===-- llvm/ADT/Triple.h - Target triple helper class ----------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #ifndef LLVM_ADT_TRIPLE_H
     11 #define LLVM_ADT_TRIPLE_H
     12 
     13 #include "llvm/ADT/StringRef.h"
     14 #include <string>
     15 
     16 // Some system headers or GCC predefined macros conflict with identifiers in
     17 // this file.  Undefine them here.
     18 #undef mips
     19 #undef sparc
     20 
     21 namespace llvm {
     22 class StringRef;
     23 class Twine;
     24 
     25 /// Triple - Helper class for working with target triples.
     26 ///
     27 /// Target triples are strings in the canonical form:
     28 ///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM
     29 /// or
     30 ///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
     31 ///
     32 /// This class is used for clients which want to support arbitrary
     33 /// target triples, but also want to implement certain special
     34 /// behavior for particular targets. This class isolates the mapping
     35 /// from the components of the target triple to well known IDs.
     36 ///
     37 /// At its core the Triple class is designed to be a wrapper for a triple
     38 /// string; the constructor does not change or normalize the triple string.
     39 /// Clients that need to handle the non-canonical triples that users often
     40 /// specify should use the normalize method.
     41 ///
     42 /// See autoconf/config.guess for a glimpse into what triples look like in
     43 /// practice.
     44 class Triple {
     45 public:
     46   enum ArchType {
     47     UnknownArch,
     48 
     49     alpha,   // Alpha: alpha
     50     arm,     // ARM; arm, armv.*, xscale
     51     bfin,    // Blackfin: bfin
     52     cellspu, // CellSPU: spu, cellspu
     53     mips,    // MIPS: mips, mipsallegrex
     54     mipsel,  // MIPSEL: mipsel, mipsallegrexel, psp
     55     msp430,  // MSP430: msp430
     56     ppc,     // PPC: powerpc
     57     ppc64,   // PPC64: powerpc64, ppu
     58     sparc,   // Sparc: sparc
     59     sparcv9, // Sparcv9: Sparcv9
     60     systemz, // SystemZ: s390x
     61     tce,     // TCE (http://tce.cs.tut.fi/): tce
     62     thumb,   // Thumb: thumb, thumbv.*
     63     x86,     // X86: i[3-9]86
     64     x86_64,  // X86-64: amd64, x86_64
     65     xcore,   // XCore: xcore
     66     mblaze,  // MBlaze: mblaze
     67     ptx32,   // PTX: ptx (32-bit)
     68     ptx64,   // PTX: ptx (64-bit)
     69 
     70     InvalidArch
     71   };
     72   enum VendorType {
     73     UnknownVendor,
     74 
     75     Apple,
     76     PC,
     77     SCEI
     78   };
     79   enum OSType {
     80     UnknownOS,
     81 
     82     AuroraUX,
     83     Cygwin,
     84     Darwin,
     85     DragonFly,
     86     FreeBSD,
     87     IOS,
     88     Linux,
     89     Lv2,        // PS3
     90     MacOSX,
     91     MinGW32,    // i*86-pc-mingw32, *-w64-mingw32
     92     NetBSD,
     93     OpenBSD,
     94     Psp,
     95     Solaris,
     96     Win32,
     97     Haiku,
     98     Minix,
     99     RTEMS
    100   };
    101   enum EnvironmentType {
    102     UnknownEnvironment,
    103 
    104     GNU,
    105     GNUEABI,
    106     EABI,
    107     MachO
    108   };
    109 
    110 private:
    111   std::string Data;
    112 
    113   /// The parsed arch type (or InvalidArch if uninitialized).
    114   mutable ArchType Arch;
    115 
    116   /// The parsed vendor type.
    117   mutable VendorType Vendor;
    118 
    119   /// The parsed OS type.
    120   mutable OSType OS;
    121 
    122   /// The parsed Environment type.
    123   mutable EnvironmentType Environment;
    124 
    125   bool isInitialized() const { return Arch != InvalidArch; }
    126   static ArchType ParseArch(StringRef ArchName);
    127   static VendorType ParseVendor(StringRef VendorName);
    128   static OSType ParseOS(StringRef OSName);
    129   static EnvironmentType ParseEnvironment(StringRef EnvironmentName);
    130   void Parse() const;
    131 
    132 public:
    133   /// @name Constructors
    134   /// @{
    135 
    136   Triple() : Data(), Arch(InvalidArch) {}
    137   explicit Triple(StringRef Str) : Data(Str), Arch(InvalidArch) {}
    138   explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr)
    139     : Data(ArchStr), Arch(InvalidArch) {
    140     Data += '-';
    141     Data += VendorStr;
    142     Data += '-';
    143     Data += OSStr;
    144   }
    145 
    146   explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr,
    147     StringRef EnvironmentStr)
    148     : Data(ArchStr), Arch(InvalidArch) {
    149     Data += '-';
    150     Data += VendorStr;
    151     Data += '-';
    152     Data += OSStr;
    153     Data += '-';
    154     Data += EnvironmentStr;
    155   }
    156 
    157   /// @}
    158   /// @name Normalization
    159   /// @{
    160 
    161   /// normalize - Turn an arbitrary machine specification into the canonical
    162   /// triple form (or something sensible that the Triple class understands if
    163   /// nothing better can reasonably be done).  In particular, it handles the
    164   /// common case in which otherwise valid components are in the wrong order.
    165   static std::string normalize(StringRef Str);
    166 
    167   /// @}
    168   /// @name Typed Component Access
    169   /// @{
    170 
    171   /// getArch - Get the parsed architecture type of this triple.
    172   ArchType getArch() const {
    173     if (!isInitialized()) Parse();
    174     return Arch;
    175   }
    176 
    177   /// getVendor - Get the parsed vendor type of this triple.
    178   VendorType getVendor() const {
    179     if (!isInitialized()) Parse();
    180     return Vendor;
    181   }
    182 
    183   /// getOS - Get the parsed operating system type of this triple.
    184   OSType getOS() const {
    185     if (!isInitialized()) Parse();
    186     return OS;
    187   }
    188 
    189   /// hasEnvironment - Does this triple have the optional environment
    190   /// (fourth) component?
    191   bool hasEnvironment() const {
    192     return getEnvironmentName() != "";
    193   }
    194 
    195   /// getEnvironment - Get the parsed environment type of this triple.
    196   EnvironmentType getEnvironment() const {
    197     if (!isInitialized()) Parse();
    198     return Environment;
    199   }
    200 
    201   /// @}
    202   /// @name Direct Component Access
    203   /// @{
    204 
    205   const std::string &str() const { return Data; }
    206 
    207   const std::string &getTriple() const { return Data; }
    208 
    209   /// getArchName - Get the architecture (first) component of the
    210   /// triple.
    211   StringRef getArchName() const;
    212 
    213   /// getVendorName - Get the vendor (second) component of the triple.
    214   StringRef getVendorName() const;
    215 
    216   /// getOSName - Get the operating system (third) component of the
    217   /// triple.
    218   StringRef getOSName() const;
    219 
    220   /// getEnvironmentName - Get the optional environment (fourth)
    221   /// component of the triple, or "" if empty.
    222   StringRef getEnvironmentName() const;
    223 
    224   /// getOSAndEnvironmentName - Get the operating system and optional
    225   /// environment components as a single string (separated by a '-'
    226   /// if the environment component is present).
    227   StringRef getOSAndEnvironmentName() const;
    228 
    229   /// getOSVersion - Parse the version number from the OS name component of the
    230   /// triple, if present.
    231   ///
    232   /// For example, "fooos1.2.3" would return (1, 2, 3).
    233   ///
    234   /// If an entry is not defined, it will be returned as 0.
    235   void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
    236 
    237   /// getOSMajorVersion - Return just the major version number, this is
    238   /// specialized because it is a common query.
    239   unsigned getOSMajorVersion() const {
    240     unsigned Maj, Min, Micro;
    241     getOSVersion(Maj, Min, Micro);
    242     return Maj;
    243   }
    244 
    245   /// isOSVersionLT - Helper function for doing comparisons against version
    246   /// numbers included in the target triple.
    247   bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
    248                      unsigned Micro = 0) const {
    249     unsigned LHS[3];
    250     getOSVersion(LHS[0], LHS[1], LHS[2]);
    251 
    252     if (LHS[0] != Major)
    253       return LHS[0] < Major;
    254     if (LHS[1] != Minor)
    255       return LHS[1] < Minor;
    256     if (LHS[2] != Micro)
    257       return LHS[1] < Micro;
    258 
    259     return false;
    260   }
    261 
    262   /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
    263   /// "darwin" and "osx" as OS X triples.
    264   bool isMacOSX() const {
    265     return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
    266   }
    267 
    268   /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
    269   bool isOSDarwin() const {
    270     return isMacOSX() || getOS() == Triple::IOS;
    271   }
    272 
    273   /// isOSWindows - Is this a "Windows" OS.
    274   bool isOSWindows() const {
    275     return getOS() == Triple::Win32 || getOS() == Triple::Cygwin ||
    276       getOS() == Triple::MinGW32;
    277   }
    278 
    279   /// isMacOSXVersionLT - Comparison function for checking OS X version
    280   /// compatibility, which handles supporting skewed version numbering schemes
    281   /// used by the "darwin" triples.
    282   unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
    283 			     unsigned Micro = 0) const {
    284     assert(isMacOSX() && "Not an OS X triple!");
    285 
    286     // If this is OS X, expect a sane version number.
    287     if (getOS() == Triple::MacOSX)
    288       return isOSVersionLT(Major, Minor, Micro);
    289 
    290     // Otherwise, compare to the "Darwin" number.
    291     assert(Major == 10 && "Unexpected major version");
    292     return isOSVersionLT(Minor + 4, Micro, 0);
    293   }
    294 
    295   /// @}
    296   /// @name Mutators
    297   /// @{
    298 
    299   /// setArch - Set the architecture (first) component of the triple
    300   /// to a known type.
    301   void setArch(ArchType Kind);
    302 
    303   /// setVendor - Set the vendor (second) component of the triple to a
    304   /// known type.
    305   void setVendor(VendorType Kind);
    306 
    307   /// setOS - Set the operating system (third) component of the triple
    308   /// to a known type.
    309   void setOS(OSType Kind);
    310 
    311   /// setEnvironment - Set the environment (fourth) component of the triple
    312   /// to a known type.
    313   void setEnvironment(EnvironmentType Kind);
    314 
    315   /// setTriple - Set all components to the new triple \arg Str.
    316   void setTriple(const Twine &Str);
    317 
    318   /// setArchName - Set the architecture (first) component of the
    319   /// triple by name.
    320   void setArchName(StringRef Str);
    321 
    322   /// setVendorName - Set the vendor (second) component of the triple
    323   /// by name.
    324   void setVendorName(StringRef Str);
    325 
    326   /// setOSName - Set the operating system (third) component of the
    327   /// triple by name.
    328   void setOSName(StringRef Str);
    329 
    330   /// setEnvironmentName - Set the optional environment (fourth)
    331   /// component of the triple by name.
    332   void setEnvironmentName(StringRef Str);
    333 
    334   /// setOSAndEnvironmentName - Set the operating system and optional
    335   /// environment components with a single string.
    336   void setOSAndEnvironmentName(StringRef Str);
    337 
    338   /// getArchNameForAssembler - Get an architecture name that is understood by
    339   /// the target assembler.
    340   const char *getArchNameForAssembler();
    341 
    342   /// @}
    343   /// @name Static helpers for IDs.
    344   /// @{
    345 
    346   /// getArchTypeName - Get the canonical name for the \arg Kind
    347   /// architecture.
    348   static const char *getArchTypeName(ArchType Kind);
    349 
    350   /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
    351   /// architecture. This is the prefix used by the architecture specific
    352   /// builtins, and is suitable for passing to \see
    353   /// Intrinsic::getIntrinsicForGCCBuiltin().
    354   ///
    355   /// \return - The architecture prefix, or 0 if none is defined.
    356   static const char *getArchTypePrefix(ArchType Kind);
    357 
    358   /// getVendorTypeName - Get the canonical name for the \arg Kind
    359   /// vendor.
    360   static const char *getVendorTypeName(VendorType Kind);
    361 
    362   /// getOSTypeName - Get the canonical name for the \arg Kind operating
    363   /// system.
    364   static const char *getOSTypeName(OSType Kind);
    365 
    366   /// getEnvironmentTypeName - Get the canonical name for the \arg Kind
    367   /// environment.
    368   static const char *getEnvironmentTypeName(EnvironmentType Kind);
    369 
    370   /// @}
    371   /// @name Static helpers for converting alternate architecture names.
    372   /// @{
    373 
    374   /// getArchTypeForLLVMName - The canonical type for the given LLVM
    375   /// architecture name (e.g., "x86").
    376   static ArchType getArchTypeForLLVMName(StringRef Str);
    377 
    378   /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
    379   /// architecture name, for example as accepted by "gcc -arch" (see also
    380   /// arch(3)).
    381   static ArchType getArchTypeForDarwinArchName(StringRef Str);
    382 
    383   /// @}
    384 };
    385 
    386 } // End llvm namespace
    387 
    388 
    389 #endif
    390