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