1 //===-- llvm/ADT/Triple.h - Target triple helper class ----------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_ADT_TRIPLE_H
10 #define LLVM_ADT_TRIPLE_H
12 #include "llvm/ADT/Twine.h"
14 // Some system headers or GCC predefined macros conflict with identifiers in
15 // this file. Undefine them here.
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).
26 /// Configuration names are strings in the canonical form:
27 /// ARCHITECTURE-VENDOR-OPERATING_SYSTEM
29 /// ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
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.
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.
41 /// See autoconf/config.guess for a glimpse into what configuration names
42 /// look like in practice.
48 arm, // ARM (little endian): arm, armv.*, xscale
49 armeb, // ARM (big endian): armeb
50 aarch64, // AArch64 (little endian): aarch64
51 aarch64_be, // AArch64 (big endian): aarch64_be
52 arc, // ARC: Synopsys ARC
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, mipsr6
58 mipsel, // MIPSEL: mipsel, mipsallegrexe, mipsr6el
59 mips64, // MIPS64: mips64, mips64r6, mipsn32, mipsn32r6
60 mips64el, // MIPS64EL: mips64el, mips64r6el, mipsn32el, mipsn32r6el
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 riscv32, // RISC-V (32-bit): riscv32
68 riscv64, // RISC-V (64-bit): riscv64
69 sparc, // Sparc: sparc
70 sparcv9, // Sparcv9: Sparcv9
71 sparcel, // Sparc: (endianness = little). NB: 'Sparcle' is a CPU variant
72 systemz, // SystemZ: s390x
73 tce, // TCE (http://tce.cs.tut.fi/): tce
74 tcele, // TCE little endian (http://tce.cs.tut.fi/): tcele
75 thumb, // Thumb (little endian): thumb, thumbv.*
76 thumbeb, // Thumb (big endian): thumbeb
78 x86_64, // X86-64: amd64, x86_64
79 xcore, // XCore: xcore
80 nvptx, // NVPTX: 32-bit
81 nvptx64, // NVPTX: 64-bit
82 le32, // le32: generic little-endian 32-bit CPU (PNaCl)
83 le64, // le64: generic little-endian 64-bit CPU (PNaCl)
85 amdil64, // AMDIL with 64-bit pointers
87 hsail64, // AMD HSAIL with 64-bit pointers
88 spir, // SPIR: standard portable IR for OpenCL 32-bit version
89 spir64, // SPIR: standard portable IR for OpenCL 64-bit version
90 kalimba, // Kalimba: generic kalimba
91 shave, // SHAVE: Movidius vector VLIW processors
92 lanai, // Lanai: Lanai 32-bit
93 wasm32, // WebAssembly with 32-bit pointers
94 wasm64, // WebAssembly with 64-bit pointers
95 renderscript32, // 32-bit RenderScript
96 renderscript64, // 64-bit RenderScript
97 LastArchType = renderscript64
109 ARMSubArch_v8m_baseline,
110 ARMSubArch_v8m_mainline,
141 ImaginationTechnologies,
150 LastVendorType = OpenEmbedded
173 NaCl, // Native Client
174 CNK, // BG/P Compute-Node Kernel
177 NVCL, // NVIDIA OpenCL
178 AMDHSA, // AMD HSA Runtime
182 WatchOS, // Apple watchOS
185 AMDPAL, // AMD PAL Runtime
186 HermitCore, // HermitCore Unikernel/Multikernel
188 WASI, // Experimental WebAssembly OS
190 LastOSType = Emscripten
192 enum EnvironmentType {
213 Simulator, // Simulator variants of other systems, e.g., Apple's iOS
214 LastEnvironmentType = Simulator
216 enum ObjectFormatType {
229 /// The parsed arch type.
232 /// The parsed subarchitecture type.
235 /// The parsed vendor type.
238 /// The parsed OS type.
241 /// The parsed Environment type.
242 EnvironmentType Environment;
244 /// The object format type.
245 ObjectFormatType ObjectFormat;
248 /// @name Constructors
251 /// Default constructor is the same as an empty string and leaves all
252 /// triple fields unknown.
254 : Data(), Arch(), SubArch(), Vendor(), OS(), Environment(),
257 explicit Triple(const Twine &Str);
258 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
259 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
260 const Twine &EnvironmentStr);
262 bool operator==(const Triple &Other) const {
263 return Arch == Other.Arch && SubArch == Other.SubArch &&
264 Vendor == Other.Vendor && OS == Other.OS &&
265 Environment == Other.Environment &&
266 ObjectFormat == Other.ObjectFormat;
269 bool operator!=(const Triple &Other) const {
270 return !(*this == Other);
274 /// @name Normalization
277 /// normalize - Turn an arbitrary machine specification into the canonical
278 /// triple form (or something sensible that the Triple class understands if
279 /// nothing better can reasonably be done). In particular, it handles the
280 /// common case in which otherwise valid components are in the wrong order.
281 static std::string normalize(StringRef Str);
283 /// Return the normalized form of this triple's string.
284 std::string normalize() const { return normalize(Data); }
287 /// @name Typed Component Access
290 /// getArch - Get the parsed architecture type of this triple.
291 ArchType getArch() const { return Arch; }
293 /// getSubArch - get the parsed subarchitecture type for this triple.
294 SubArchType getSubArch() const { return SubArch; }
296 /// getVendor - Get the parsed vendor type of this triple.
297 VendorType getVendor() const { return Vendor; }
299 /// getOS - Get the parsed operating system type of this triple.
300 OSType getOS() const { return OS; }
302 /// hasEnvironment - Does this triple have the optional environment
303 /// (fourth) component?
304 bool hasEnvironment() const {
305 return getEnvironmentName() != "";
308 /// getEnvironment - Get the parsed environment type of this triple.
309 EnvironmentType getEnvironment() const { return Environment; }
311 /// Parse the version number from the OS name component of the
312 /// triple, if present.
314 /// For example, "fooos1.2.3" would return (1, 2, 3).
316 /// If an entry is not defined, it will be returned as 0.
317 void getEnvironmentVersion(unsigned &Major, unsigned &Minor,
318 unsigned &Micro) const;
320 /// getFormat - Get the object format for this triple.
321 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
323 /// getOSVersion - Parse the version number from the OS name component of the
324 /// triple, if present.
326 /// For example, "fooos1.2.3" would return (1, 2, 3).
328 /// If an entry is not defined, it will be returned as 0.
329 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
331 /// getOSMajorVersion - Return just the major version number, this is
332 /// specialized because it is a common query.
333 unsigned getOSMajorVersion() const {
334 unsigned Maj, Min, Micro;
335 getOSVersion(Maj, Min, Micro);
339 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
340 /// translate generic "darwin" versions to the corresponding OS X versions.
341 /// This may also be called with IOS triples but the OS X version number is
342 /// just set to a constant 10.4.0 in that case. Returns true if successful.
343 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
344 unsigned &Micro) const;
346 /// getiOSVersion - Parse the version number as with getOSVersion. This should
347 /// only be called with IOS or generic triples.
348 void getiOSVersion(unsigned &Major, unsigned &Minor,
349 unsigned &Micro) const;
351 /// getWatchOSVersion - Parse the version number as with getOSVersion. This
352 /// should only be called with WatchOS or generic triples.
353 void getWatchOSVersion(unsigned &Major, unsigned &Minor,
354 unsigned &Micro) const;
357 /// @name Direct Component Access
360 const std::string &str() const { return Data; }
362 const std::string &getTriple() const { return Data; }
364 /// getArchName - Get the architecture (first) component of the
366 StringRef getArchName() const;
368 /// getVendorName - Get the vendor (second) component of the triple.
369 StringRef getVendorName() const;
371 /// getOSName - Get the operating system (third) component of the
373 StringRef getOSName() const;
375 /// getEnvironmentName - Get the optional environment (fourth)
376 /// component of the triple, or "" if empty.
377 StringRef getEnvironmentName() const;
379 /// getOSAndEnvironmentName - Get the operating system and optional
380 /// environment components as a single string (separated by a '-'
381 /// if the environment component is present).
382 StringRef getOSAndEnvironmentName() const;
385 /// @name Convenience Predicates
388 /// Test whether the architecture is 64-bit
390 /// Note that this tests for 64-bit pointer width, and nothing else. Note
391 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
392 /// 16-bit. The inner details of pointer width for particular architectures
393 /// is not summed up in the triple, and so only a coarse grained predicate
394 /// system is provided.
395 bool isArch64Bit() const;
397 /// Test whether the architecture is 32-bit
399 /// Note that this tests for 32-bit pointer width, and nothing else.
400 bool isArch32Bit() const;
402 /// Test whether the architecture is 16-bit
404 /// Note that this tests for 16-bit pointer width, and nothing else.
405 bool isArch16Bit() const;
407 /// isOSVersionLT - Helper function for doing comparisons against version
408 /// numbers included in the target triple.
409 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
410 unsigned Micro = 0) const {
412 getOSVersion(LHS[0], LHS[1], LHS[2]);
415 return LHS[0] < Major;
417 return LHS[1] < Minor;
419 return LHS[2] < Micro;
424 bool isOSVersionLT(const Triple &Other) const {
426 Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
427 return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
430 /// isMacOSXVersionLT - Comparison function for checking OS X version
431 /// compatibility, which handles supporting skewed version numbering schemes
432 /// used by the "darwin" triples.
433 bool isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
434 unsigned Micro = 0) const {
435 assert(isMacOSX() && "Not an OS X triple!");
437 // If this is OS X, expect a sane version number.
438 if (getOS() == Triple::MacOSX)
439 return isOSVersionLT(Major, Minor, Micro);
441 // Otherwise, compare to the "Darwin" number.
442 assert(Major == 10 && "Unexpected major version");
443 return isOSVersionLT(Minor + 4, Micro, 0);
446 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
447 /// "darwin" and "osx" as OS X triples.
448 bool isMacOSX() const {
449 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
452 /// Is this an iOS triple.
453 /// Note: This identifies tvOS as a variant of iOS. If that ever
454 /// changes, i.e., if the two operating systems diverge or their version
455 /// numbers get out of sync, that will need to be changed.
456 /// watchOS has completely different version numbers so it is not included.
458 return getOS() == Triple::IOS || isTvOS();
461 /// Is this an Apple tvOS triple.
462 bool isTvOS() const {
463 return getOS() == Triple::TvOS;
466 /// Is this an Apple watchOS triple.
467 bool isWatchOS() const {
468 return getOS() == Triple::WatchOS;
471 bool isWatchABI() const {
472 return getSubArch() == Triple::ARMSubArch_v7k;
475 /// isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
476 bool isOSDarwin() const {
477 return isMacOSX() || isiOS() || isWatchOS();
480 bool isSimulatorEnvironment() const {
481 return getEnvironment() == Triple::Simulator;
484 bool isOSNetBSD() const {
485 return getOS() == Triple::NetBSD;
488 bool isOSOpenBSD() const {
489 return getOS() == Triple::OpenBSD;
492 bool isOSFreeBSD() const {
493 return getOS() == Triple::FreeBSD;
496 bool isOSFuchsia() const {
497 return getOS() == Triple::Fuchsia;
500 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
502 bool isOSSolaris() const {
503 return getOS() == Triple::Solaris;
506 bool isOSIAMCU() const {
507 return getOS() == Triple::ELFIAMCU;
510 bool isOSUnknown() const { return getOS() == Triple::UnknownOS; }
512 bool isGNUEnvironment() const {
513 EnvironmentType Env = getEnvironment();
514 return Env == Triple::GNU || Env == Triple::GNUABIN32 ||
515 Env == Triple::GNUABI64 || Env == Triple::GNUEABI ||
516 Env == Triple::GNUEABIHF || Env == Triple::GNUX32;
519 bool isOSContiki() const {
520 return getOS() == Triple::Contiki;
523 /// Tests whether the OS is Haiku.
524 bool isOSHaiku() const {
525 return getOS() == Triple::Haiku;
528 /// Tests whether the OS is Windows.
529 bool isOSWindows() const {
530 return getOS() == Triple::Win32;
533 /// Checks if the environment is MSVC.
534 bool isKnownWindowsMSVCEnvironment() const {
535 return isOSWindows() && getEnvironment() == Triple::MSVC;
538 /// Checks if the environment could be MSVC.
539 bool isWindowsMSVCEnvironment() const {
540 return isKnownWindowsMSVCEnvironment() ||
541 (isOSWindows() && getEnvironment() == Triple::UnknownEnvironment);
544 bool isWindowsCoreCLREnvironment() const {
545 return isOSWindows() && getEnvironment() == Triple::CoreCLR;
548 bool isWindowsItaniumEnvironment() const {
549 return isOSWindows() && getEnvironment() == Triple::Itanium;
552 bool isWindowsCygwinEnvironment() const {
553 return isOSWindows() && getEnvironment() == Triple::Cygnus;
556 bool isWindowsGNUEnvironment() const {
557 return isOSWindows() && getEnvironment() == Triple::GNU;
560 /// Tests for either Cygwin or MinGW OS
561 bool isOSCygMing() const {
562 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
565 /// Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
566 bool isOSMSVCRT() const {
567 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
568 isWindowsItaniumEnvironment();
571 /// Tests whether the OS is NaCl (Native Client)
572 bool isOSNaCl() const {
573 return getOS() == Triple::NaCl;
576 /// Tests whether the OS is Linux.
577 bool isOSLinux() const {
578 return getOS() == Triple::Linux;
581 /// Tests whether the OS is kFreeBSD.
582 bool isOSKFreeBSD() const {
583 return getOS() == Triple::KFreeBSD;
586 /// Tests whether the OS is Hurd.
587 bool isOSHurd() const {
588 return getOS() == Triple::Hurd;
591 /// Tests whether the OS is WASI.
592 bool isOSWASI() const {
593 return getOS() == Triple::WASI;
596 /// Tests whether the OS is Emscripten.
597 bool isOSEmscripten() const {
598 return getOS() == Triple::Emscripten;
601 /// Tests whether the OS uses glibc.
602 bool isOSGlibc() const {
603 return (getOS() == Triple::Linux || getOS() == Triple::KFreeBSD ||
604 getOS() == Triple::Hurd) &&
608 /// Tests whether the OS is AIX.
609 bool isOSAIX() const {
610 return getOS() == Triple::AIX;
613 /// Tests whether the OS uses the ELF binary format.
614 bool isOSBinFormatELF() const {
615 return getObjectFormat() == Triple::ELF;
618 /// Tests whether the OS uses the COFF binary format.
619 bool isOSBinFormatCOFF() const {
620 return getObjectFormat() == Triple::COFF;
623 /// Tests whether the environment is MachO.
624 bool isOSBinFormatMachO() const {
625 return getObjectFormat() == Triple::MachO;
628 /// Tests whether the OS uses the Wasm binary format.
629 bool isOSBinFormatWasm() const {
630 return getObjectFormat() == Triple::Wasm;
633 /// Tests whether the OS uses the XCOFF binary format.
634 bool isOSBinFormatXCOFF() const {
635 return getObjectFormat() == Triple::XCOFF;
638 /// Tests whether the target is the PS4 CPU
639 bool isPS4CPU() const {
640 return getArch() == Triple::x86_64 &&
641 getVendor() == Triple::SCEI &&
642 getOS() == Triple::PS4;
645 /// Tests whether the target is the PS4 platform
647 return getVendor() == Triple::SCEI &&
648 getOS() == Triple::PS4;
651 /// Tests whether the target is Android
652 bool isAndroid() const { return getEnvironment() == Triple::Android; }
654 bool isAndroidVersionLT(unsigned Major) const {
655 assert(isAndroid() && "Not an Android triple!");
658 getEnvironmentVersion(Env[0], Env[1], Env[2]);
660 // 64-bit targets did not exist before API level 21 (Lollipop).
661 if (isArch64Bit() && Env[0] < 21)
664 return Env[0] < Major;
667 /// Tests whether the environment is musl-libc
668 bool isMusl() const {
669 return getEnvironment() == Triple::Musl ||
670 getEnvironment() == Triple::MuslEABI ||
671 getEnvironment() == Triple::MuslEABIHF;
674 /// Tests whether the target is NVPTX (32- or 64-bit).
675 bool isNVPTX() const {
676 return getArch() == Triple::nvptx || getArch() == Triple::nvptx64;
679 /// Tests whether the target is Thumb (little and big endian).
680 bool isThumb() const {
681 return getArch() == Triple::thumb || getArch() == Triple::thumbeb;
684 /// Tests whether the target is ARM (little and big endian).
686 return getArch() == Triple::arm || getArch() == Triple::armeb;
689 /// Tests whether the target is AArch64 (little and big endian).
690 bool isAArch64() const {
691 return getArch() == Triple::aarch64 || getArch() == Triple::aarch64_be;
694 /// Tests whether the target is MIPS 32-bit (little and big endian).
695 bool isMIPS32() const {
696 return getArch() == Triple::mips || getArch() == Triple::mipsel;
699 /// Tests whether the target is MIPS 64-bit (little and big endian).
700 bool isMIPS64() const {
701 return getArch() == Triple::mips64 || getArch() == Triple::mips64el;
704 /// Tests whether the target is MIPS (little and big endian, 32- or 64-bit).
705 bool isMIPS() const {
706 return isMIPS32() || isMIPS64();
709 /// Tests whether the target supports comdat
710 bool supportsCOMDAT() const {
711 return !isOSBinFormatMachO();
714 /// Tests whether the target uses emulated TLS as default.
715 bool hasDefaultEmulatedTLS() const {
716 return isAndroid() || isOSOpenBSD() || isWindowsCygwinEnvironment();
723 /// setArch - Set the architecture (first) component of the triple
725 void setArch(ArchType Kind);
727 /// setVendor - Set the vendor (second) component of the triple to a
729 void setVendor(VendorType Kind);
731 /// setOS - Set the operating system (third) component of the triple
733 void setOS(OSType Kind);
735 /// setEnvironment - Set the environment (fourth) component of the triple
737 void setEnvironment(EnvironmentType Kind);
739 /// setObjectFormat - Set the object file format
740 void setObjectFormat(ObjectFormatType Kind);
742 /// setTriple - Set all components to the new triple \p Str.
743 void setTriple(const Twine &Str);
745 /// setArchName - Set the architecture (first) component of the
747 void setArchName(StringRef Str);
749 /// setVendorName - Set the vendor (second) component of the triple
751 void setVendorName(StringRef Str);
753 /// setOSName - Set the operating system (third) component of the
755 void setOSName(StringRef Str);
757 /// setEnvironmentName - Set the optional environment (fourth)
758 /// component of the triple by name.
759 void setEnvironmentName(StringRef Str);
761 /// setOSAndEnvironmentName - Set the operating system and optional
762 /// environment components with a single string.
763 void setOSAndEnvironmentName(StringRef Str);
766 /// @name Helpers to build variants of a particular triple.
769 /// Form a triple with a 32-bit variant of the current architecture.
771 /// This can be used to move across "families" of architectures where useful.
773 /// \returns A new triple with a 32-bit architecture or an unknown
774 /// architecture if no such variant can be found.
775 llvm::Triple get32BitArchVariant() const;
777 /// Form a triple with a 64-bit variant of the current architecture.
779 /// This can be used to move across "families" of architectures where useful.
781 /// \returns A new triple with a 64-bit architecture or an unknown
782 /// architecture if no such variant can be found.
783 llvm::Triple get64BitArchVariant() const;
785 /// Form a triple with a big endian variant of the current architecture.
787 /// This can be used to move across "families" of architectures where useful.
789 /// \returns A new triple with a big endian architecture or an unknown
790 /// architecture if no such variant can be found.
791 llvm::Triple getBigEndianArchVariant() const;
793 /// Form a triple with a little endian variant of the current architecture.
795 /// This can be used to move across "families" of architectures where useful.
797 /// \returns A new triple with a little endian architecture or an unknown
798 /// architecture if no such variant can be found.
799 llvm::Triple getLittleEndianArchVariant() const;
801 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
803 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
804 /// string then the triple's arch name is used.
805 StringRef getARMCPUForArch(StringRef Arch = StringRef()) const;
807 /// Tests whether the target triple is little endian.
809 /// \returns true if the triple is little endian, false otherwise.
810 bool isLittleEndian() const;
812 /// Test whether target triples are compatible.
813 bool isCompatibleWith(const Triple &Other) const;
815 /// Merge target triples.
816 std::string merge(const Triple &Other) const;
819 /// @name Static helpers for IDs.
822 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
823 static StringRef getArchTypeName(ArchType Kind);
825 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
826 /// architecture. This is the prefix used by the architecture specific
827 /// builtins, and is suitable for passing to \see
828 /// Intrinsic::getIntrinsicForGCCBuiltin().
830 /// \return - The architecture prefix, or 0 if none is defined.
831 static StringRef getArchTypePrefix(ArchType Kind);
833 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
834 static StringRef getVendorTypeName(VendorType Kind);
836 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
837 static StringRef getOSTypeName(OSType Kind);
839 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
841 static StringRef getEnvironmentTypeName(EnvironmentType Kind);
844 /// @name Static helpers for converting alternate architecture names.
847 /// getArchTypeForLLVMName - The canonical type for the given LLVM
848 /// architecture name (e.g., "x86").
849 static ArchType getArchTypeForLLVMName(StringRef Str);
854 } // End llvm namespace