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 20 namespace 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. 43 class Triple { 44 public: 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 128 private: 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 143 public: 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 /// isOSWindows - Is this a "Windows" OS. 322 bool isOSWindows() const { 323 return getOS() == Triple::Win32 || isOSCygMing(); 324 } 325 326 /// \brief Tests whether the OS is NaCl (Native Client) 327 bool isOSNaCl() const { 328 return getOS() == Triple::NaCl; 329 } 330 331 /// \brief Tests whether the OS uses the ELF binary format. 332 bool isOSBinFormatELF() const { 333 return !isOSDarwin() && !isOSWindows(); 334 } 335 336 /// \brief Tests whether the OS uses the COFF binary format. 337 bool isOSBinFormatCOFF() const { 338 return isOSWindows(); 339 } 340 341 /// \brief Tests whether the environment is MachO. 342 // FIXME: Should this be an OSBinFormat predicate? 343 bool isEnvironmentMachO() const { 344 return getEnvironment() == Triple::MachO || isOSDarwin(); 345 } 346 347 /// @} 348 /// @name Mutators 349 /// @{ 350 351 /// setArch - Set the architecture (first) component of the triple 352 /// to a known type. 353 void setArch(ArchType Kind); 354 355 /// setVendor - Set the vendor (second) component of the triple to a 356 /// known type. 357 void setVendor(VendorType Kind); 358 359 /// setOS - Set the operating system (third) component of the triple 360 /// to a known type. 361 void setOS(OSType Kind); 362 363 /// setEnvironment - Set the environment (fourth) component of the triple 364 /// to a known type. 365 void setEnvironment(EnvironmentType Kind); 366 367 /// setTriple - Set all components to the new triple \p Str. 368 void setTriple(const Twine &Str); 369 370 /// setArchName - Set the architecture (first) component of the 371 /// triple by name. 372 void setArchName(StringRef Str); 373 374 /// setVendorName - Set the vendor (second) component of the triple 375 /// by name. 376 void setVendorName(StringRef Str); 377 378 /// setOSName - Set the operating system (third) component of the 379 /// triple by name. 380 void setOSName(StringRef Str); 381 382 /// setEnvironmentName - Set the optional environment (fourth) 383 /// component of the triple by name. 384 void setEnvironmentName(StringRef Str); 385 386 /// setOSAndEnvironmentName - Set the operating system and optional 387 /// environment components with a single string. 388 void setOSAndEnvironmentName(StringRef Str); 389 390 /// getArchNameForAssembler - Get an architecture name that is understood by 391 /// the target assembler. 392 const char *getArchNameForAssembler(); 393 394 /// @} 395 /// @name Helpers to build variants of a particular triple. 396 /// @{ 397 398 /// \brief Form a triple with a 32-bit variant of the current architecture. 399 /// 400 /// This can be used to move across "families" of architectures where useful. 401 /// 402 /// \returns A new triple with a 32-bit architecture or an unknown 403 /// architecture if no such variant can be found. 404 llvm::Triple get32BitArchVariant() const; 405 406 /// \brief Form a triple with a 64-bit variant of the current architecture. 407 /// 408 /// This can be used to move across "families" of architectures where useful. 409 /// 410 /// \returns A new triple with a 64-bit architecture or an unknown 411 /// architecture if no such variant can be found. 412 llvm::Triple get64BitArchVariant() const; 413 414 /// @} 415 /// @name Static helpers for IDs. 416 /// @{ 417 418 /// getArchTypeName - Get the canonical name for the \p Kind architecture. 419 static const char *getArchTypeName(ArchType Kind); 420 421 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind 422 /// architecture. This is the prefix used by the architecture specific 423 /// builtins, and is suitable for passing to \see 424 /// Intrinsic::getIntrinsicForGCCBuiltin(). 425 /// 426 /// \return - The architecture prefix, or 0 if none is defined. 427 static const char *getArchTypePrefix(ArchType Kind); 428 429 /// getVendorTypeName - Get the canonical name for the \p Kind vendor. 430 static const char *getVendorTypeName(VendorType Kind); 431 432 /// getOSTypeName - Get the canonical name for the \p Kind operating system. 433 static const char *getOSTypeName(OSType Kind); 434 435 /// getEnvironmentTypeName - Get the canonical name for the \p Kind 436 /// environment. 437 static const char *getEnvironmentTypeName(EnvironmentType Kind); 438 439 /// @} 440 /// @name Static helpers for converting alternate architecture names. 441 /// @{ 442 443 /// getArchTypeForLLVMName - The canonical type for the given LLVM 444 /// architecture name (e.g., "x86"). 445 static ArchType getArchTypeForLLVMName(StringRef Str); 446 447 /// @} 448 }; 449 450 } // End llvm namespace 451 452 453 #endif 454