LLVM API Documentation
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/StringRef.h" 00014 #include <string> 00015 00016 namespace llvm { 00017 class StringRef; 00018 class Twine; 00019 00020 /// Triple - Helper class for working with target triples. 00021 /// 00022 /// Target triples are strings in the format of: 00023 /// ARCHITECTURE-VENDOR-OPERATING_SYSTEM 00024 /// or 00025 /// ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT 00026 /// 00027 /// This class is used for clients which want to support arbitrary 00028 /// target triples, but also want to implement certain special 00029 /// behavior for particular targets. This class isolates the mapping 00030 /// from the components of the target triple to well known IDs. 00031 /// 00032 /// At its core the Triple class is designed to be a wrapper for a triple 00033 /// string; it does not normally change or normalize the triple string, instead 00034 /// it provides additional APIs to parse normalized parts out of the triple. 00035 /// 00036 /// One curiosity this implies is that for some odd triples the results of, 00037 /// e.g., getOSName() can be very different from the result of getOS(). For 00038 /// example, for 'i386-mingw32', getOS() will return MinGW32, but since 00039 /// getOSName() is purely based on the string structure that will return the 00040 /// empty string. 00041 /// 00042 /// Clients should generally avoid using getOSName() and related APIs unless 00043 /// they are familiar with the triple format (this is particularly true when 00044 /// rewriting a triple). 00045 /// 00046 /// See autoconf/config.guess for a glimpse into what they look like in 00047 /// practice. 00048 class Triple { 00049 public: 00050 enum ArchType { 00051 UnknownArch, 00052 00053 alpha, // Alpha: alpha 00054 arm, // ARM; arm, armv.*, xscale 00055 bfin, // Blackfin: bfin 00056 cellspu, // CellSPU: spu, cellspu 00057 mips, // MIPS: mips, mipsallegrex 00058 mipsel, // MIPSEL: mipsel, mipsallegrexel, psp 00059 msp430, // MSP430: msp430 00060 pic16, // PIC16: pic16 00061 ppc, // PPC: powerpc 00062 ppc64, // PPC64: powerpc64 00063 sparc, // Sparc: sparc 00064 systemz, // SystemZ: s390x 00065 tce, // TCE (http://tce.cs.tut.fi/): tce 00066 thumb, // Thumb: thumb, thumbv.* 00067 x86, // X86: i[3-9]86 00068 x86_64, // X86-64: amd64, x86_64 00069 xcore, // XCore: xcore 00070 00071 InvalidArch 00072 }; 00073 enum VendorType { 00074 UnknownVendor, 00075 00076 Apple, 00077 PC 00078 }; 00079 enum OSType { 00080 UnknownOS, 00081 00082 AuroraUX, 00083 Cygwin, 00084 Darwin, 00085 DragonFly, 00086 FreeBSD, 00087 Linux, 00088 MinGW32, 00089 MinGW64, 00090 NetBSD, 00091 OpenBSD, 00092 Solaris, 00093 Win32 00094 }; 00095 00096 private: 00097 std::string Data; 00098 00099 /// The parsed arch type (or InvalidArch if uninitialized). 00100 mutable ArchType Arch; 00101 00102 /// The parsed vendor type. 00103 mutable VendorType Vendor; 00104 00105 /// The parsed OS type. 00106 mutable OSType OS; 00107 00108 bool isInitialized() const { return Arch != InvalidArch; } 00109 void Parse() const; 00110 00111 public: 00112 /// @name Constructors 00113 /// @{ 00114 00115 Triple() : Data(), Arch(InvalidArch) {} 00116 explicit Triple(const StringRef &Str) : Data(Str), Arch(InvalidArch) {} 00117 explicit Triple(const char *ArchStr, const char *VendorStr, const char *OSStr) 00118 : Data(ArchStr), Arch(InvalidArch) { 00119 Data += '-'; 00120 Data += VendorStr; 00121 Data += '-'; 00122 Data += OSStr; 00123 } 00124 00125 /// @} 00126 /// @name Typed Component Access 00127 /// @{ 00128 00129 /// getArch - Get the parsed architecture type of this triple. 00130 ArchType getArch() const { 00131 if (!isInitialized()) Parse(); 00132 return Arch; 00133 } 00134 00135 /// getVendor - Get the parsed vendor type of this triple. 00136 VendorType getVendor() const { 00137 if (!isInitialized()) Parse(); 00138 return Vendor; 00139 } 00140 00141 /// getOS - Get the parsed operating system type of this triple. 00142 OSType getOS() const { 00143 if (!isInitialized()) Parse(); 00144 return OS; 00145 } 00146 00147 /// hasEnvironment - Does this triple have the optional environment 00148 /// (fourth) component? 00149 bool hasEnvironment() const { 00150 return getEnvironmentName() != ""; 00151 } 00152 00153 /// @} 00154 /// @name Direct Component Access 00155 /// @{ 00156 00157 const std::string &getTriple() const { return Data; } 00158 00159 /// getArchName - Get the architecture (first) component of the 00160 /// triple. 00161 StringRef getArchName() const; 00162 00163 /// getVendorName - Get the vendor (second) component of the triple. 00164 StringRef getVendorName() const; 00165 00166 /// getOSName - Get the operating system (third) component of the 00167 /// triple. 00168 StringRef getOSName() const; 00169 00170 /// getEnvironmentName - Get the optional environment (fourth) 00171 /// component of the triple, or "" if empty. 00172 StringRef getEnvironmentName() const; 00173 00174 /// getOSAndEnvironmentName - Get the operating system and optional 00175 /// environment components as a single string (separated by a '-' 00176 /// if the environment component is present). 00177 StringRef getOSAndEnvironmentName() const; 00178 00179 00180 /// getDarwinNumber - Parse the 'darwin number' out of the specific target 00181 /// triple. For example, if we have darwin8.5 return 8,5,0. If any entry is 00182 /// not defined, return 0's. This requires that the triple have an OSType of 00183 /// darwin before it is called. 00184 void getDarwinNumber(unsigned &Maj, unsigned &Min, unsigned &Revision) const; 00185 00186 /// getDarwinMajorNumber - Return just the major version number, this is 00187 /// specialized because it is a common query. 00188 unsigned getDarwinMajorNumber() const { 00189 unsigned Maj, Min, Rev; 00190 getDarwinNumber(Maj, Min, Rev); 00191 return Maj; 00192 } 00193 00194 /// @} 00195 /// @name Mutators 00196 /// @{ 00197 00198 /// setArch - Set the architecture (first) component of the triple 00199 /// to a known type. 00200 void setArch(ArchType Kind); 00201 00202 /// setVendor - Set the vendor (second) component of the triple to a 00203 /// known type. 00204 void setVendor(VendorType Kind); 00205 00206 /// setOS - Set the operating system (third) component of the triple 00207 /// to a known type. 00208 void setOS(OSType Kind); 00209 00210 /// setTriple - Set all components to the new triple \arg Str. 00211 void setTriple(const Twine &Str); 00212 00213 /// setArchName - Set the architecture (first) component of the 00214 /// triple by name. 00215 void setArchName(const StringRef &Str); 00216 00217 /// setVendorName - Set the vendor (second) component of the triple 00218 /// by name. 00219 void setVendorName(const StringRef &Str); 00220 00221 /// setOSName - Set the operating system (third) component of the 00222 /// triple by name. 00223 void setOSName(const StringRef &Str); 00224 00225 /// setEnvironmentName - Set the optional environment (fourth) 00226 /// component of the triple by name. 00227 void setEnvironmentName(const StringRef &Str); 00228 00229 /// setOSAndEnvironmentName - Set the operating system and optional 00230 /// environment components with a single string. 00231 void setOSAndEnvironmentName(const StringRef &Str); 00232 00233 /// @} 00234 /// @name Static helpers for IDs. 00235 /// @{ 00236 00237 /// getArchTypeName - Get the canonical name for the \arg Kind 00238 /// architecture. 00239 static const char *getArchTypeName(ArchType Kind); 00240 00241 /// getVendorTypeName - Get the canonical name for the \arg Kind 00242 /// vendor. 00243 static const char *getVendorTypeName(VendorType Kind); 00244 00245 /// getOSTypeName - Get the canonical name for the \arg Kind vendor. 00246 static const char *getOSTypeName(OSType Kind); 00247 00248 /// getArchTypeForLLVMName - The canonical type for the given LLVM 00249 /// architecture name (e.g., "x86"). 00250 static ArchType getArchTypeForLLVMName(const StringRef &Str); 00251 00252 /// @} 00253 }; 00254 00255 } // End llvm namespace 00256 00257 00258 #endif