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 pic16, // PIC16: pic16 57 ppc, // PPC: powerpc 58 ppc64, // PPC64: powerpc64, ppu 59 sparc, // Sparc: sparc 60 sparcv9, // Sparcv9: Sparcv9 61 systemz, // SystemZ: s390x 62 tce, // TCE (http://tce.cs.tut.fi/): tce 63 thumb, // Thumb: thumb, thumbv.* 64 x86, // X86: i[3-9]86 65 x86_64, // X86-64: amd64, x86_64 66 xcore, // XCore: xcore 67 mblaze, // MBlaze: mblaze 68 69 InvalidArch 70 }; 71 enum VendorType { 72 UnknownVendor, 73 74 Apple, 75 PC 76 }; 77 enum OSType { 78 UnknownOS, 79 80 AuroraUX, 81 Cygwin, 82 Darwin, 83 DragonFly, 84 FreeBSD, 85 Linux, 86 Lv2, // PS3 87 MinGW32, 88 MinGW64, 89 NetBSD, 90 OpenBSD, 91 Psp, 92 Solaris, 93 Win32, 94 Haiku, 95 Minix 96 }; 97 98 private: 99 std::string Data; 100 101 /// The parsed arch type (or InvalidArch if uninitialized). 102 mutable ArchType Arch; 103 104 /// The parsed vendor type. 105 mutable VendorType Vendor; 106 107 /// The parsed OS type. 108 mutable OSType OS; 109 isInitialized()110 bool isInitialized() const { return Arch != InvalidArch; } 111 static ArchType ParseArch(StringRef ArchName); 112 static VendorType ParseVendor(StringRef VendorName); 113 static OSType ParseOS(StringRef OSName); 114 void Parse() const; 115 116 public: 117 /// @name Constructors 118 /// @{ 119 Triple()120 Triple() : Data(), Arch(InvalidArch) {} Triple(StringRef Str)121 explicit Triple(StringRef Str) : Data(Str), Arch(InvalidArch) {} Triple(StringRef ArchStr,StringRef VendorStr,StringRef OSStr)122 explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr) 123 : Data(ArchStr), Arch(InvalidArch) { 124 Data += '-'; 125 Data += VendorStr; 126 Data += '-'; 127 Data += OSStr; 128 } 129 130 /// @} 131 /// @name Normalization 132 /// @{ 133 134 /// normalize - Turn an arbitrary machine specification into the canonical 135 /// triple form (or something sensible that the Triple class understands if 136 /// nothing better can reasonably be done). In particular, it handles the 137 /// common case in which otherwise valid components are in the wrong order. 138 static std::string normalize(StringRef Str); 139 140 /// @} 141 /// @name Typed Component Access 142 /// @{ 143 144 /// getArch - Get the parsed architecture type of this triple. getArch()145 ArchType getArch() const { 146 if (!isInitialized()) Parse(); 147 return Arch; 148 } 149 150 /// getVendor - Get the parsed vendor type of this triple. getVendor()151 VendorType getVendor() const { 152 if (!isInitialized()) Parse(); 153 return Vendor; 154 } 155 156 /// getOS - Get the parsed operating system type of this triple. getOS()157 OSType getOS() const { 158 if (!isInitialized()) Parse(); 159 return OS; 160 } 161 162 /// hasEnvironment - Does this triple have the optional environment 163 /// (fourth) component? hasEnvironment()164 bool hasEnvironment() const { 165 return getEnvironmentName() != ""; 166 } 167 168 /// @} 169 /// @name Direct Component Access 170 /// @{ 171 str()172 const std::string &str() const { return Data; } 173 getTriple()174 const std::string &getTriple() const { return Data; } 175 176 /// getArchName - Get the architecture (first) component of the 177 /// triple. 178 StringRef getArchName() const; 179 180 /// getVendorName - Get the vendor (second) component of the triple. 181 StringRef getVendorName() const; 182 183 /// getOSName - Get the operating system (third) component of the 184 /// triple. 185 StringRef getOSName() const; 186 187 /// getEnvironmentName - Get the optional environment (fourth) 188 /// component of the triple, or "" if empty. 189 StringRef getEnvironmentName() const; 190 191 /// getOSAndEnvironmentName - Get the operating system and optional 192 /// environment components as a single string (separated by a '-' 193 /// if the environment component is present). 194 StringRef getOSAndEnvironmentName() const; 195 196 197 /// getDarwinNumber - Parse the 'darwin number' out of the specific target 198 /// triple. For example, if we have darwin8.5 return 8,5,0. If any entry is 199 /// not defined, return 0's. This requires that the triple have an OSType of 200 /// darwin before it is called. 201 void getDarwinNumber(unsigned &Maj, unsigned &Min, unsigned &Revision) const; 202 203 /// getDarwinMajorNumber - Return just the major version number, this is 204 /// specialized because it is a common query. getDarwinMajorNumber()205 unsigned getDarwinMajorNumber() const { 206 unsigned Maj, Min, Rev; 207 getDarwinNumber(Maj, Min, Rev); 208 return Maj; 209 } 210 211 /// @} 212 /// @name Mutators 213 /// @{ 214 215 /// setArch - Set the architecture (first) component of the triple 216 /// to a known type. 217 void setArch(ArchType Kind); 218 219 /// setVendor - Set the vendor (second) component of the triple to a 220 /// known type. 221 void setVendor(VendorType Kind); 222 223 /// setOS - Set the operating system (third) component of the triple 224 /// to a known type. 225 void setOS(OSType Kind); 226 227 /// setTriple - Set all components to the new triple \arg Str. 228 void setTriple(const Twine &Str); 229 230 /// setArchName - Set the architecture (first) component of the 231 /// triple by name. 232 void setArchName(StringRef Str); 233 234 /// setVendorName - Set the vendor (second) component of the triple 235 /// by name. 236 void setVendorName(StringRef Str); 237 238 /// setOSName - Set the operating system (third) component of the 239 /// triple by name. 240 void setOSName(StringRef Str); 241 242 /// setEnvironmentName - Set the optional environment (fourth) 243 /// component of the triple by name. 244 void setEnvironmentName(StringRef Str); 245 246 /// setOSAndEnvironmentName - Set the operating system and optional 247 /// environment components with a single string. 248 void setOSAndEnvironmentName(StringRef Str); 249 250 /// getArchNameForAssembler - Get an architecture name that is understood by 251 /// the target assembler. 252 const char *getArchNameForAssembler(); 253 254 /// @} 255 /// @name Static helpers for IDs. 256 /// @{ 257 258 /// getArchTypeName - Get the canonical name for the \arg Kind 259 /// architecture. 260 static const char *getArchTypeName(ArchType Kind); 261 262 /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind 263 /// architecture. This is the prefix used by the architecture specific 264 /// builtins, and is suitable for passing to \see 265 /// Intrinsic::getIntrinsicForGCCBuiltin(). 266 /// 267 /// \return - The architecture prefix, or 0 if none is defined. 268 static const char *getArchTypePrefix(ArchType Kind); 269 270 /// getVendorTypeName - Get the canonical name for the \arg Kind 271 /// vendor. 272 static const char *getVendorTypeName(VendorType Kind); 273 274 /// getOSTypeName - Get the canonical name for the \arg Kind vendor. 275 static const char *getOSTypeName(OSType Kind); 276 277 /// @} 278 /// @name Static helpers for converting alternate architecture names. 279 /// @{ 280 281 /// getArchTypeForLLVMName - The canonical type for the given LLVM 282 /// architecture name (e.g., "x86"). 283 static ArchType getArchTypeForLLVMName(StringRef Str); 284 285 /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin" 286 /// architecture name, for example as accepted by "gcc -arch" (see also 287 /// arch(3)). 288 static ArchType getArchTypeForDarwinArchName(StringRef Str); 289 290 /// @} 291 }; 292 293 } // End llvm namespace 294 295 296 #endif 297