1 //===-- llvm/ADT/Triple.h - Target triple helper class ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ADT_TRIPLE_H
11 #define LLVM_ADT_TRIPLE_H
13 #include "llvm/ADT/Twine.h"
15 // Some system headers or GCC predefined macros conflict with identifiers in
16 // this file. Undefine them here.
23 /// Triple - Helper class for working with autoconf configuration names. For
24 /// historical reasons, we also call these 'triples' (they used to contain
25 /// exactly three fields).
27 /// Configuration names are strings in the canonical form:
28 /// ARCHITECTURE-VENDOR-OPERATING_SYSTEM
30 /// ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
32 /// This class is used for clients which want to support arbitrary
33 /// configuration names, but also want to implement certain special
34 /// behavior for particular configurations. This class isolates the mapping
35 /// from the components of the configuration name to well known IDs.
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.
42 /// See autoconf/config.guess for a glimpse into what configuration names
43 /// look like in practice.
49 arm, // ARM (little endian): arm, armv.*, xscale
50 armeb, // ARM (big endian): armeb
51 aarch64, // AArch64 (little endian): aarch64
52 aarch64_be, // AArch64 (big endian): aarch64_be
53 avr, // AVR: Atmel AVR microcontroller
54 bpfel, // eBPF or extended BPF or 64-bit BPF (little endian)
55 bpfeb, // eBPF or extended BPF or 64-bit BPF (big endian)
56 hexagon, // Hexagon: hexagon
57 mips, // MIPS: mips, mipsallegrex
58 mipsel, // MIPSEL: mipsel, mipsallegrexel
59 mips64, // MIPS64: mips64
60 mips64el, // MIPS64EL: mips64el
61 msp430, // MSP430: msp430
63 ppc64, // PPC64: powerpc64, ppu
64 ppc64le, // PPC64LE: powerpc64le
65 r600, // R600: AMD GPUs HD2XXX - HD6XXX
66 amdgcn, // AMDGCN: AMD GCN GPUs
67 sparc, // Sparc: sparc
68 sparcv9, // Sparcv9: Sparcv9
69 sparcel, // Sparc: (endianness = little). NB: 'Sparcle' is a CPU variant
70 systemz, // SystemZ: s390x
71 tce, // TCE (http://tce.cs.tut.fi/): tce
72 thumb, // Thumb (little endian): thumb, thumbv.*
73 thumbeb, // Thumb (big endian): thumbeb
75 x86_64, // X86-64: amd64, x86_64
76 xcore, // XCore: xcore
77 nvptx, // NVPTX: 32-bit
78 nvptx64, // NVPTX: 64-bit
79 le32, // le32: generic little-endian 32-bit CPU (PNaCl)
80 le64, // le64: generic little-endian 64-bit CPU (PNaCl)
82 amdil64, // AMDIL with 64-bit pointers
84 hsail64, // AMD HSAIL with 64-bit pointers
85 spir, // SPIR: standard portable IR for OpenCL 32-bit version
86 spir64, // SPIR: standard portable IR for OpenCL 64-bit version
87 kalimba, // Kalimba: generic kalimba
88 shave, // SHAVE: Movidius vector VLIW processors
89 wasm32, // WebAssembly with 32-bit pointers
90 wasm64, // WebAssembly with 64-bit pointers
126 ImaginationTechnologies,
131 LastVendorType = Myriad
152 NaCl, // Native Client
153 CNK, // BG/P Compute-Node Kernel
157 NVCL, // NVIDIA OpenCL
158 AMDHSA, // AMD HSA Runtime
162 WatchOS, // Apple watchOS
165 enum EnvironmentType {
182 LastEnvironmentType = CoreCLR
184 enum ObjectFormatType {
195 /// The parsed arch type.
198 /// The parsed subarchitecture type.
201 /// The parsed vendor type.
204 /// The parsed OS type.
207 /// The parsed Environment type.
208 EnvironmentType Environment;
210 /// The object format type.
211 ObjectFormatType ObjectFormat;
214 /// @name Constructors
217 /// Default constructor is the same as an empty string and leaves all
218 /// triple fields unknown.
219 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
221 explicit Triple(const Twine &Str);
222 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
223 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
224 const Twine &EnvironmentStr);
226 bool operator==(const Triple &Other) const {
227 return Arch == Other.Arch && SubArch == Other.SubArch &&
228 Vendor == Other.Vendor && OS == Other.OS &&
229 Environment == Other.Environment &&
230 ObjectFormat == Other.ObjectFormat;
234 /// @name Normalization
237 /// normalize - Turn an arbitrary machine specification into the canonical
238 /// triple form (or something sensible that the Triple class understands if
239 /// nothing better can reasonably be done). In particular, it handles the
240 /// common case in which otherwise valid components are in the wrong order.
241 static std::string normalize(StringRef Str);
243 /// Return the normalized form of this triple's string.
244 std::string normalize() const { return normalize(Data); }
247 /// @name Typed Component Access
250 /// getArch - Get the parsed architecture type of this triple.
251 ArchType getArch() const { return Arch; }
253 /// getSubArch - get the parsed subarchitecture type for this triple.
254 SubArchType getSubArch() const { return SubArch; }
256 /// getVendor - Get the parsed vendor type of this triple.
257 VendorType getVendor() const { return Vendor; }
259 /// getOS - Get the parsed operating system type of this triple.
260 OSType getOS() const { return OS; }
262 /// hasEnvironment - Does this triple have the optional environment
263 /// (fourth) component?
264 bool hasEnvironment() const {
265 return getEnvironmentName() != "";
268 /// getEnvironment - Get the parsed environment type of this triple.
269 EnvironmentType getEnvironment() const { return Environment; }
271 /// Parse the version number from the OS name component of the
272 /// triple, if present.
274 /// For example, "fooos1.2.3" would return (1, 2, 3).
276 /// If an entry is not defined, it will be returned as 0.
277 void getEnvironmentVersion(unsigned &Major, unsigned &Minor,
278 unsigned &Micro) const;
280 /// getFormat - Get the object format for this triple.
281 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
283 /// getOSVersion - Parse the version number from the OS name component of the
284 /// triple, if present.
286 /// For example, "fooos1.2.3" would return (1, 2, 3).
288 /// If an entry is not defined, it will be returned as 0.
289 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
291 /// getOSMajorVersion - Return just the major version number, this is
292 /// specialized because it is a common query.
293 unsigned getOSMajorVersion() const {
294 unsigned Maj, Min, Micro;
295 getOSVersion(Maj, Min, Micro);
299 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
300 /// translate generic "darwin" versions to the corresponding OS X versions.
301 /// This may also be called with IOS triples but the OS X version number is
302 /// just set to a constant 10.4.0 in that case. Returns true if successful.
303 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
304 unsigned &Micro) const;
306 /// getiOSVersion - Parse the version number as with getOSVersion. This should
307 /// only be called with IOS or generic triples.
308 void getiOSVersion(unsigned &Major, unsigned &Minor,
309 unsigned &Micro) const;
311 /// getWatchOSVersion - Parse the version number as with getOSVersion. This
312 /// should only be called with WatchOS or generic triples.
313 void getWatchOSVersion(unsigned &Major, unsigned &Minor,
314 unsigned &Micro) const;
317 /// @name Direct Component Access
320 const std::string &str() const { return Data; }
322 const std::string &getTriple() const { return Data; }
324 /// getArchName - Get the architecture (first) component of the
326 StringRef getArchName() const;
328 /// getVendorName - Get the vendor (second) component of the triple.
329 StringRef getVendorName() const;
331 /// getOSName - Get the operating system (third) component of the
333 StringRef getOSName() const;
335 /// getEnvironmentName - Get the optional environment (fourth)
336 /// component of the triple, or "" if empty.
337 StringRef getEnvironmentName() const;
339 /// getOSAndEnvironmentName - Get the operating system and optional
340 /// environment components as a single string (separated by a '-'
341 /// if the environment component is present).
342 StringRef getOSAndEnvironmentName() const;
345 /// @name Convenience Predicates
348 /// Test whether the architecture is 64-bit
350 /// Note that this tests for 64-bit pointer width, and nothing else. Note
351 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
352 /// 16-bit. The inner details of pointer width for particular architectures
353 /// is not summed up in the triple, and so only a coarse grained predicate
354 /// system is provided.
355 bool isArch64Bit() const;
357 /// Test whether the architecture is 32-bit
359 /// Note that this tests for 32-bit pointer width, and nothing else.
360 bool isArch32Bit() const;
362 /// Test whether the architecture is 16-bit
364 /// Note that this tests for 16-bit pointer width, and nothing else.
365 bool isArch16Bit() const;
367 /// isOSVersionLT - Helper function for doing comparisons against version
368 /// numbers included in the target triple.
369 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
370 unsigned Micro = 0) const {
372 getOSVersion(LHS[0], LHS[1], LHS[2]);
375 return LHS[0] < Major;
377 return LHS[1] < Minor;
379 return LHS[1] < Micro;
384 bool isOSVersionLT(const Triple &Other) const {
386 Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
387 return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
390 /// isMacOSXVersionLT - Comparison function for checking OS X version
391 /// compatibility, which handles supporting skewed version numbering schemes
392 /// used by the "darwin" triples.
393 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
394 unsigned Micro = 0) const {
395 assert(isMacOSX() && "Not an OS X triple!");
397 // If this is OS X, expect a sane version number.
398 if (getOS() == Triple::MacOSX)
399 return isOSVersionLT(Major, Minor, Micro);
401 // Otherwise, compare to the "Darwin" number.
402 assert(Major == 10 && "Unexpected major version");
403 return isOSVersionLT(Minor + 4, Micro, 0);
406 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
407 /// "darwin" and "osx" as OS X triples.
408 bool isMacOSX() const {
409 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
412 /// Is this an iOS triple.
413 /// Note: This identifies tvOS as a variant of iOS. If that ever
414 /// changes, i.e., if the two operating systems diverge or their version
415 /// numbers get out of sync, that will need to be changed.
416 /// watchOS has completely different version numbers so it is not included.
418 return getOS() == Triple::IOS || isTvOS();
421 /// Is this an Apple tvOS triple.
422 bool isTvOS() const {
423 return getOS() == Triple::TvOS;
426 /// Is this an Apple watchOS triple.
427 bool isWatchOS() const {
428 return getOS() == Triple::WatchOS;
431 /// isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
432 bool isOSDarwin() const {
433 return isMacOSX() || isiOS() || isWatchOS();
436 bool isOSNetBSD() const {
437 return getOS() == Triple::NetBSD;
440 bool isOSOpenBSD() const {
441 return getOS() == Triple::OpenBSD;
444 bool isOSFreeBSD() const {
445 return getOS() == Triple::FreeBSD;
448 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
450 bool isOSSolaris() const {
451 return getOS() == Triple::Solaris;
454 bool isOSBitrig() const {
455 return getOS() == Triple::Bitrig;
458 bool isOSIAMCU() const {
459 return getOS() == Triple::ELFIAMCU;
462 /// Checks if the environment could be MSVC.
463 bool isWindowsMSVCEnvironment() const {
464 return getOS() == Triple::Win32 &&
465 (getEnvironment() == Triple::UnknownEnvironment ||
466 getEnvironment() == Triple::MSVC);
469 /// Checks if the environment is MSVC.
470 bool isKnownWindowsMSVCEnvironment() const {
471 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
474 bool isWindowsCoreCLREnvironment() const {
475 return getOS() == Triple::Win32 && getEnvironment() == Triple::CoreCLR;
478 bool isWindowsItaniumEnvironment() const {
479 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
482 bool isWindowsCygwinEnvironment() const {
483 return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
486 bool isWindowsGNUEnvironment() const {
487 return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
490 /// Tests for either Cygwin or MinGW OS
491 bool isOSCygMing() const {
492 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
495 /// Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
496 bool isOSMSVCRT() const {
497 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
498 isWindowsItaniumEnvironment();
501 /// Tests whether the OS is Windows.
502 bool isOSWindows() const {
503 return getOS() == Triple::Win32;
506 /// Tests whether the OS is NaCl (Native Client)
507 bool isOSNaCl() const {
508 return getOS() == Triple::NaCl;
511 /// Tests whether the OS is Linux.
512 bool isOSLinux() const {
513 return getOS() == Triple::Linux;
516 /// Tests whether the OS uses the ELF binary format.
517 bool isOSBinFormatELF() const {
518 return getObjectFormat() == Triple::ELF;
521 /// Tests whether the OS uses the COFF binary format.
522 bool isOSBinFormatCOFF() const {
523 return getObjectFormat() == Triple::COFF;
526 /// Tests whether the environment is MachO.
527 bool isOSBinFormatMachO() const {
528 return getObjectFormat() == Triple::MachO;
531 /// Tests whether the target is the PS4 CPU
532 bool isPS4CPU() const {
533 return getArch() == Triple::x86_64 &&
534 getVendor() == Triple::SCEI &&
535 getOS() == Triple::PS4;
538 /// Tests whether the target is the PS4 platform
540 return getVendor() == Triple::SCEI &&
541 getOS() == Triple::PS4;
544 /// Tests whether the target is Android
545 bool isAndroid() const { return getEnvironment() == Triple::Android; }
551 /// setArch - Set the architecture (first) component of the triple
553 void setArch(ArchType Kind);
555 /// setVendor - Set the vendor (second) component of the triple to a
557 void setVendor(VendorType Kind);
559 /// setOS - Set the operating system (third) component of the triple
561 void setOS(OSType Kind);
563 /// setEnvironment - Set the environment (fourth) component of the triple
565 void setEnvironment(EnvironmentType Kind);
567 /// setObjectFormat - Set the object file format
568 void setObjectFormat(ObjectFormatType Kind);
570 /// setTriple - Set all components to the new triple \p Str.
571 void setTriple(const Twine &Str);
573 /// setArchName - Set the architecture (first) component of the
575 void setArchName(StringRef Str);
577 /// setVendorName - Set the vendor (second) component of the triple
579 void setVendorName(StringRef Str);
581 /// setOSName - Set the operating system (third) component of the
583 void setOSName(StringRef Str);
585 /// setEnvironmentName - Set the optional environment (fourth)
586 /// component of the triple by name.
587 void setEnvironmentName(StringRef Str);
589 /// setOSAndEnvironmentName - Set the operating system and optional
590 /// environment components with a single string.
591 void setOSAndEnvironmentName(StringRef Str);
594 /// @name Helpers to build variants of a particular triple.
597 /// Form a triple with a 32-bit variant of the current architecture.
599 /// This can be used to move across "families" of architectures where useful.
601 /// \returns A new triple with a 32-bit architecture or an unknown
602 /// architecture if no such variant can be found.
603 llvm::Triple get32BitArchVariant() const;
605 /// Form a triple with a 64-bit variant of the current architecture.
607 /// This can be used to move across "families" of architectures where useful.
609 /// \returns A new triple with a 64-bit architecture or an unknown
610 /// architecture if no such variant can be found.
611 llvm::Triple get64BitArchVariant() const;
613 /// Form a triple with a big endian variant of the current architecture.
615 /// This can be used to move across "families" of architectures where useful.
617 /// \returns A new triple with a big endian architecture or an unknown
618 /// architecture if no such variant can be found.
619 llvm::Triple getBigEndianArchVariant() const;
621 /// Form a triple with a little endian variant of the current architecture.
623 /// This can be used to move across "families" of architectures where useful.
625 /// \returns A new triple with a little endian architecture or an unknown
626 /// architecture if no such variant can be found.
627 llvm::Triple getLittleEndianArchVariant() const;
629 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
631 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
632 /// string then the triple's arch name is used.
633 StringRef getARMCPUForArch(StringRef Arch = StringRef()) const;
636 /// @name Static helpers for IDs.
639 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
640 static const char *getArchTypeName(ArchType Kind);
642 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
643 /// architecture. This is the prefix used by the architecture specific
644 /// builtins, and is suitable for passing to \see
645 /// Intrinsic::getIntrinsicForGCCBuiltin().
647 /// \return - The architecture prefix, or 0 if none is defined.
648 static const char *getArchTypePrefix(ArchType Kind);
650 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
651 static const char *getVendorTypeName(VendorType Kind);
653 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
654 static const char *getOSTypeName(OSType Kind);
656 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
658 static const char *getEnvironmentTypeName(EnvironmentType Kind);
661 /// @name Static helpers for converting alternate architecture names.
664 /// getArchTypeForLLVMName - The canonical type for the given LLVM
665 /// architecture name (e.g., "x86").
666 static ArchType getArchTypeForLLVMName(StringRef Str);
671 } // End llvm namespace