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