Triple.h revision 441c557708b5dbe91f1799baf790ad418c23ea70
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 autoconf configuration names. For 23/// historical reasons, we also call these 'triples' (they used to contain 24/// exactly three fields). 25/// 26/// Configuration names are strings in the canonical form: 27/// ARCHITECTURE-VENDOR-OPERATING_SYSTEM 28/// or 29/// ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT 30/// 31/// This class is used for clients which want to support arbitrary 32/// configuration names, but also want to implement certain special 33/// behavior for particular configurations. This class isolates the mapping 34/// from the components of the configuration name to well known IDs. 35/// 36/// At its core the Triple class is designed to be a wrapper for a triple 37/// string; the constructor does not change or normalize the triple string. 38/// Clients that need to handle the non-canonical triples that users often 39/// specify should use the normalize method. 40/// 41/// See autoconf/config.guess for a glimpse into what configuration names 42/// look like in practice. 43class Triple { 44public: 45 enum ArchType { 46 UnknownArch, 47 48 arm, // ARM: arm, armv.*, xscale 49 aarch64, // AArch64: aarch64 50 hexagon, // Hexagon: hexagon 51 mips, // MIPS: mips, mipsallegrex 52 mipsel, // MIPSEL: mipsel, mipsallegrexel 53 mips64, // MIPS64: mips64 54 mips64el,// MIPS64EL: mips64el 55 msp430, // MSP430: msp430 56 ppc, // PPC: powerpc 57 ppc64, // PPC64: powerpc64, ppu 58 ppc64le, // PPC64LE: powerpc64le 59 r600, // R600: AMD GPUs HD2XXX - HD6XXX 60 sparc, // Sparc: sparc 61 sparcv9, // Sparcv9: Sparcv9 62 systemz, // SystemZ: s390x 63 tce, // TCE (http://tce.cs.tut.fi/): tce 64 thumb, // Thumb: thumb, thumbv.* 65 x86, // X86: i[3-9]86 66 x86_64, // X86-64: amd64, x86_64 67 xcore, // XCore: xcore 68 nvptx, // NVPTX: 32-bit 69 nvptx64, // NVPTX: 64-bit 70 le32, // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten) 71 amdil, // amdil: amd IL 72 spir, // SPIR: standard portable IR for OpenCL 32-bit version 73 spir64 // SPIR: standard portable IR for OpenCL 64-bit version 74 }; 75 enum VendorType { 76 UnknownVendor, 77 78 Apple, 79 PC, 80 SCEI, 81 BGP, 82 BGQ, 83 Freescale, 84 IBM, 85 NVIDIA 86 }; 87 enum OSType { 88 UnknownOS, 89 90 AuroraUX, 91 Cygwin, 92 Darwin, 93 DragonFly, 94 FreeBSD, 95 IOS, 96 KFreeBSD, 97 Linux, 98 Lv2, // PS3 99 MacOSX, 100 MinGW32, // i*86-pc-mingw32, *-w64-mingw32 101 NetBSD, 102 OpenBSD, 103 Solaris, 104 Win32, 105 Haiku, 106 Minix, 107 RTEMS, 108 NaCl, // Native Client 109 CNK, // BG/P Compute-Node Kernel 110 Bitrig, 111 AIX, 112 CUDA, // NVIDIA CUDA 113 NVCL // NVIDIA OpenCL 114 }; 115 enum EnvironmentType { 116 UnknownEnvironment, 117 118 GNU, 119 GNUEABI, 120 GNUEABIHF, 121 GNUX32, 122 EABI, 123 MachO, 124 Android, 125 ELF 126 }; 127 128private: 129 std::string Data; 130 131 /// The parsed arch type. 132 ArchType Arch; 133 134 /// The parsed vendor type. 135 VendorType Vendor; 136 137 /// The parsed OS type. 138 OSType OS; 139 140 /// The parsed Environment type. 141 EnvironmentType Environment; 142 143public: 144 /// @name Constructors 145 /// @{ 146 147 /// \brief Default constructor is the same as an empty string and leaves all 148 /// triple fields unknown. 149 Triple() : Data(), Arch(), Vendor(), OS(), Environment() {} 150 151 explicit Triple(const Twine &Str); 152 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr); 153 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr, 154 const Twine &EnvironmentStr); 155 156 /// @} 157 /// @name Normalization 158 /// @{ 159 160 /// normalize - Turn an arbitrary machine specification into the canonical 161 /// triple form (or something sensible that the Triple class understands if 162 /// nothing better can reasonably be done). In particular, it handles the 163 /// common case in which otherwise valid components are in the wrong order. 164 static std::string normalize(StringRef Str); 165 166 /// @} 167 /// @name Typed Component Access 168 /// @{ 169 170 /// getArch - Get the parsed architecture type of this triple. 171 ArchType getArch() const { return Arch; } 172 173 /// getVendor - Get the parsed vendor type of this triple. 174 VendorType getVendor() const { return Vendor; } 175 176 /// getOS - Get the parsed operating system type of this triple. 177 OSType getOS() const { return OS; } 178 179 /// hasEnvironment - Does this triple have the optional environment 180 /// (fourth) component? 181 bool hasEnvironment() const { 182 return getEnvironmentName() != ""; 183 } 184 185 /// getEnvironment - Get the parsed environment type of this triple. 186 EnvironmentType getEnvironment() const { return Environment; } 187 188 /// getOSVersion - Parse the version number from the OS name component of the 189 /// triple, if present. 190 /// 191 /// For example, "fooos1.2.3" would return (1, 2, 3). 192 /// 193 /// If an entry is not defined, it will be returned as 0. 194 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const; 195 196 /// getOSMajorVersion - Return just the major version number, this is 197 /// specialized because it is a common query. 198 unsigned getOSMajorVersion() const { 199 unsigned Maj, Min, Micro; 200 getOSVersion(Maj, Min, Micro); 201 return Maj; 202 } 203 204 /// getMacOSXVersion - Parse the version number as with getOSVersion and then 205 /// translate generic "darwin" versions to the corresponding OS X versions. 206 /// This may also be called with IOS triples but the OS X version number is 207 /// just set to a constant 10.4.0 in that case. Returns true if successful. 208 bool getMacOSXVersion(unsigned &Major, unsigned &Minor, 209 unsigned &Micro) const; 210 211 /// getiOSVersion - Parse the version number as with getOSVersion. This should 212 /// only be called with IOS triples. 213 void getiOSVersion(unsigned &Major, unsigned &Minor, 214 unsigned &Micro) const; 215 216 /// @} 217 /// @name Direct Component Access 218 /// @{ 219 220 const std::string &str() const { return Data; } 221 222 const std::string &getTriple() const { return Data; } 223 224 /// getArchName - Get the architecture (first) component of the 225 /// triple. 226 StringRef getArchName() const; 227 228 /// getVendorName - Get the vendor (second) component of the triple. 229 StringRef getVendorName() const; 230 231 /// getOSName - Get the operating system (third) component of the 232 /// triple. 233 StringRef getOSName() const; 234 235 /// getEnvironmentName - Get the optional environment (fourth) 236 /// component of the triple, or "" if empty. 237 StringRef getEnvironmentName() const; 238 239 /// getOSAndEnvironmentName - Get the operating system and optional 240 /// environment components as a single string (separated by a '-' 241 /// if the environment component is present). 242 StringRef getOSAndEnvironmentName() const; 243 244 /// @} 245 /// @name Convenience Predicates 246 /// @{ 247 248 /// \brief Test whether the architecture is 64-bit 249 /// 250 /// Note that this tests for 64-bit pointer width, and nothing else. Note 251 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and 252 /// 16-bit. The inner details of pointer width for particular architectures 253 /// is not summed up in the triple, and so only a coarse grained predicate 254 /// system is provided. 255 bool isArch64Bit() const; 256 257 /// \brief Test whether the architecture is 32-bit 258 /// 259 /// Note that this tests for 32-bit pointer width, and nothing else. 260 bool isArch32Bit() const; 261 262 /// \brief Test whether the architecture is 16-bit 263 /// 264 /// Note that this tests for 16-bit pointer width, and nothing else. 265 bool isArch16Bit() const; 266 267 /// isOSVersionLT - Helper function for doing comparisons against version 268 /// numbers included in the target triple. 269 bool isOSVersionLT(unsigned Major, unsigned Minor = 0, 270 unsigned Micro = 0) const { 271 unsigned LHS[3]; 272 getOSVersion(LHS[0], LHS[1], LHS[2]); 273 274 if (LHS[0] != Major) 275 return LHS[0] < Major; 276 if (LHS[1] != Minor) 277 return LHS[1] < Minor; 278 if (LHS[2] != Micro) 279 return LHS[1] < Micro; 280 281 return false; 282 } 283 284 /// isMacOSXVersionLT - Comparison function for checking OS X version 285 /// compatibility, which handles supporting skewed version numbering schemes 286 /// used by the "darwin" triples. 287 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0, 288 unsigned Micro = 0) const { 289 assert(isMacOSX() && "Not an OS X triple!"); 290 291 // If this is OS X, expect a sane version number. 292 if (getOS() == Triple::MacOSX) 293 return isOSVersionLT(Major, Minor, Micro); 294 295 // Otherwise, compare to the "Darwin" number. 296 assert(Major == 10 && "Unexpected major version"); 297 return isOSVersionLT(Minor + 4, Micro, 0); 298 } 299 300 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both 301 /// "darwin" and "osx" as OS X triples. 302 bool isMacOSX() const { 303 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX; 304 } 305 306 /// Is this an iOS triple. 307 bool isiOS() const { 308 return getOS() == Triple::IOS; 309 } 310 311 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS). 312 bool isOSDarwin() const { 313 return isMacOSX() || isiOS(); 314 } 315 316 /// \brief Tests for either Cygwin or MinGW OS 317 bool isOSCygMing() const { 318 return getOS() == Triple::Cygwin || getOS() == Triple::MinGW32; 319 } 320 321 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment. 322 bool isOSMSVCRT() const { 323 return getOS() == Triple::Win32 || getOS() == Triple::MinGW32; 324 } 325 326 /// \brief Tests whether the OS is Windows. 327 bool isOSWindows() const { 328 return getOS() == Triple::Win32 || isOSCygMing(); 329 } 330 331 /// \brief Tests whether the OS is NaCl (Native Client) 332 bool isOSNaCl() const { 333 return getOS() == Triple::NaCl; 334 } 335 336 /// \brief Tests whether the OS is Linux. 337 bool isOSLinux() const { 338 return getOS() == Triple::Linux; 339 } 340 341 /// \brief Tests whether the OS uses the ELF binary format. 342 bool isOSBinFormatELF() const { 343 return !isOSDarwin() && !isOSWindows(); 344 } 345 346 /// \brief Tests whether the OS uses the COFF binary format. 347 bool isOSBinFormatCOFF() const { 348 return isOSWindows(); 349 } 350 351 /// \brief Tests whether the environment is MachO. 352 // FIXME: Should this be an OSBinFormat predicate? 353 bool isEnvironmentMachO() const { 354 return getEnvironment() == Triple::MachO || isOSDarwin(); 355 } 356 357 /// @} 358 /// @name Mutators 359 /// @{ 360 361 /// setArch - Set the architecture (first) component of the triple 362 /// to a known type. 363 void setArch(ArchType Kind); 364 365 /// setVendor - Set the vendor (second) component of the triple to a 366 /// known type. 367 void setVendor(VendorType Kind); 368 369 /// setOS - Set the operating system (third) component of the triple 370 /// to a known type. 371 void setOS(OSType Kind); 372 373 /// setEnvironment - Set the environment (fourth) component of the triple 374 /// to a known type. 375 void setEnvironment(EnvironmentType Kind); 376 377 /// setTriple - Set all components to the new triple \p Str. 378 void setTriple(const Twine &Str); 379 380 /// setArchName - Set the architecture (first) component of the 381 /// triple by name. 382 void setArchName(StringRef Str); 383 384 /// setVendorName - Set the vendor (second) component of the triple 385 /// by name. 386 void setVendorName(StringRef Str); 387 388 /// setOSName - Set the operating system (third) component of the 389 /// triple by name. 390 void setOSName(StringRef Str); 391 392 /// setEnvironmentName - Set the optional environment (fourth) 393 /// component of the triple by name. 394 void setEnvironmentName(StringRef Str); 395 396 /// setOSAndEnvironmentName - Set the operating system and optional 397 /// environment components with a single string. 398 void setOSAndEnvironmentName(StringRef Str); 399 400 /// getArchNameForAssembler - Get an architecture name that is understood by 401 /// the target assembler. 402 const char *getArchNameForAssembler(); 403 404 /// @} 405 /// @name Helpers to build variants of a particular triple. 406 /// @{ 407 408 /// \brief Form a triple with a 32-bit variant of the current architecture. 409 /// 410 /// This can be used to move across "families" of architectures where useful. 411 /// 412 /// \returns A new triple with a 32-bit architecture or an unknown 413 /// architecture if no such variant can be found. 414 llvm::Triple get32BitArchVariant() const; 415 416 /// \brief Form a triple with a 64-bit variant of the current architecture. 417 /// 418 /// This can be used to move across "families" of architectures where useful. 419 /// 420 /// \returns A new triple with a 64-bit architecture or an unknown 421 /// architecture if no such variant can be found. 422 llvm::Triple get64BitArchVariant() const; 423 424 /// @} 425 /// @name Static helpers for IDs. 426 /// @{ 427 428 /// getArchTypeName - Get the canonical name for the \p Kind architecture. 429 static const char *getArchTypeName(ArchType Kind); 430 431 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind 432 /// architecture. This is the prefix used by the architecture specific 433 /// builtins, and is suitable for passing to \see 434 /// Intrinsic::getIntrinsicForGCCBuiltin(). 435 /// 436 /// \return - The architecture prefix, or 0 if none is defined. 437 static const char *getArchTypePrefix(ArchType Kind); 438 439 /// getVendorTypeName - Get the canonical name for the \p Kind vendor. 440 static const char *getVendorTypeName(VendorType Kind); 441 442 /// getOSTypeName - Get the canonical name for the \p Kind operating system. 443 static const char *getOSTypeName(OSType Kind); 444 445 /// getEnvironmentTypeName - Get the canonical name for the \p Kind 446 /// environment. 447 static const char *getEnvironmentTypeName(EnvironmentType Kind); 448 449 /// @} 450 /// @name Static helpers for converting alternate architecture names. 451 /// @{ 452 453 /// getArchTypeForLLVMName - The canonical type for the given LLVM 454 /// architecture name (e.g., "x86"). 455 static ArchType getArchTypeForLLVMName(StringRef Str); 456 457 /// @} 458}; 459 460} // End llvm namespace 461 462 463#endif 464