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