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
62 nios2, // NIOSII: nios2
64 ppc64, // PPC64: powerpc64, ppu
65 ppc64le, // PPC64LE: powerpc64le
66 r600, // R600: AMD GPUs HD2XXX - HD6XXX
67 amdgcn, // AMDGCN: AMD GCN GPUs
68 riscv32, // RISC-V (32-bit): riscv32
69 riscv64, // RISC-V (64-bit): riscv64
70 sparc, // Sparc: sparc
71 sparcv9, // Sparcv9: Sparcv9
72 sparcel, // Sparc: (endianness = little). NB: 'Sparcle' is a CPU variant
73 systemz, // SystemZ: s390x
74 tce, // TCE (http://tce.cs.tut.fi/): tce
75 tcele, // TCE little endian (http://tce.cs.tut.fi/): tcele
76 thumb, // Thumb (little endian): thumb, thumbv.*
77 thumbeb, // Thumb (big endian): thumbeb
79 x86_64, // X86-64: amd64, x86_64
80 xcore, // XCore: xcore
81 nvptx, // NVPTX: 32-bit
82 nvptx64, // NVPTX: 64-bit
83 le32, // le32: generic little-endian 32-bit CPU (PNaCl)
84 le64, // le64: generic little-endian 64-bit CPU (PNaCl)
86 amdil64, // AMDIL with 64-bit pointers
88 hsail64, // AMD HSAIL with 64-bit pointers
89 spir, // SPIR: standard portable IR for OpenCL 32-bit version
90 spir64, // SPIR: standard portable IR for OpenCL 64-bit version
91 kalimba, // Kalimba: generic kalimba
92 shave, // SHAVE: Movidius vector VLIW processors
93 lanai, // Lanai: Lanai 32-bit
94 wasm32, // WebAssembly with 32-bit pointers
95 wasm64, // WebAssembly with 64-bit pointers
96 renderscript32, // 32-bit RenderScript
97 renderscript64, // 64-bit RenderScript
98 LastArchType = renderscript64
107 ARMSubArch_v8m_baseline,
108 ARMSubArch_v8m_mainline,
137 ImaginationTechnologies,
145 LastVendorType = SUSE
167 NaCl, // Native Client
168 CNK, // BG/P Compute-Node Kernel
172 NVCL, // NVIDIA OpenCL
173 AMDHSA, // AMD HSA Runtime
177 WatchOS, // Apple watchOS
182 enum EnvironmentType {
204 LastEnvironmentType = OpenCL
206 enum ObjectFormatType {
218 /// The parsed arch type.
221 /// The parsed subarchitecture type.
224 /// The parsed vendor type.
227 /// The parsed OS type.
230 /// The parsed Environment type.
231 EnvironmentType Environment;
233 /// The object format type.
234 ObjectFormatType ObjectFormat;
237 /// @name Constructors
240 /// Default constructor is the same as an empty string and leaves all
241 /// triple fields unknown.
242 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
244 explicit Triple(const Twine &Str);
245 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
246 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
247 const Twine &EnvironmentStr);
249 bool operator==(const Triple &Other) const {
250 return Arch == Other.Arch && SubArch == Other.SubArch &&
251 Vendor == Other.Vendor && OS == Other.OS &&
252 Environment == Other.Environment &&
253 ObjectFormat == Other.ObjectFormat;
256 bool operator!=(const Triple &Other) const {
257 return !(*this == Other);
261 /// @name Normalization
264 /// normalize - Turn an arbitrary machine specification into the canonical
265 /// triple form (or something sensible that the Triple class understands if
266 /// nothing better can reasonably be done). In particular, it handles the
267 /// common case in which otherwise valid components are in the wrong order.
268 static std::string normalize(StringRef Str);
270 /// Return the normalized form of this triple's string.
271 std::string normalize() const { return normalize(Data); }
274 /// @name Typed Component Access
277 /// getArch - Get the parsed architecture type of this triple.
278 ArchType getArch() const { return Arch; }
280 /// getSubArch - get the parsed subarchitecture type for this triple.
281 SubArchType getSubArch() const { return SubArch; }
283 /// getVendor - Get the parsed vendor type of this triple.
284 VendorType getVendor() const { return Vendor; }
286 /// getOS - Get the parsed operating system type of this triple.
287 OSType getOS() const { return OS; }
289 /// hasEnvironment - Does this triple have the optional environment
290 /// (fourth) component?
291 bool hasEnvironment() const {
292 return getEnvironmentName() != "";
295 /// getEnvironment - Get the parsed environment type of this triple.
296 EnvironmentType getEnvironment() const { return Environment; }
298 /// Parse the version number from the OS name component of the
299 /// triple, if present.
301 /// For example, "fooos1.2.3" would return (1, 2, 3).
303 /// If an entry is not defined, it will be returned as 0.
304 void getEnvironmentVersion(unsigned &Major, unsigned &Minor,
305 unsigned &Micro) const;
307 /// getFormat - Get the object format for this triple.
308 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
310 /// getOSVersion - Parse the version number from the OS name component of the
311 /// triple, if present.
313 /// For example, "fooos1.2.3" would return (1, 2, 3).
315 /// If an entry is not defined, it will be returned as 0.
316 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
318 /// getOSMajorVersion - Return just the major version number, this is
319 /// specialized because it is a common query.
320 unsigned getOSMajorVersion() const {
321 unsigned Maj, Min, Micro;
322 getOSVersion(Maj, Min, Micro);
326 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
327 /// translate generic "darwin" versions to the corresponding OS X versions.
328 /// This may also be called with IOS triples but the OS X version number is
329 /// just set to a constant 10.4.0 in that case. Returns true if successful.
330 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
331 unsigned &Micro) const;
333 /// getiOSVersion - Parse the version number as with getOSVersion. This should
334 /// only be called with IOS or generic triples.
335 void getiOSVersion(unsigned &Major, unsigned &Minor,
336 unsigned &Micro) const;
338 /// getWatchOSVersion - Parse the version number as with getOSVersion. This
339 /// should only be called with WatchOS or generic triples.
340 void getWatchOSVersion(unsigned &Major, unsigned &Minor,
341 unsigned &Micro) const;
344 /// @name Direct Component Access
347 const std::string &str() const { return Data; }
349 const std::string &getTriple() const { return Data; }
351 /// getArchName - Get the architecture (first) component of the
353 StringRef getArchName() const;
355 /// getVendorName - Get the vendor (second) component of the triple.
356 StringRef getVendorName() const;
358 /// getOSName - Get the operating system (third) component of the
360 StringRef getOSName() const;
362 /// getEnvironmentName - Get the optional environment (fourth)
363 /// component of the triple, or "" if empty.
364 StringRef getEnvironmentName() const;
366 /// getOSAndEnvironmentName - Get the operating system and optional
367 /// environment components as a single string (separated by a '-'
368 /// if the environment component is present).
369 StringRef getOSAndEnvironmentName() const;
372 /// @name Convenience Predicates
375 /// Test whether the architecture is 64-bit
377 /// Note that this tests for 64-bit pointer width, and nothing else. Note
378 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
379 /// 16-bit. The inner details of pointer width for particular architectures
380 /// is not summed up in the triple, and so only a coarse grained predicate
381 /// system is provided.
382 bool isArch64Bit() const;
384 /// Test whether the architecture is 32-bit
386 /// Note that this tests for 32-bit pointer width, and nothing else.
387 bool isArch32Bit() const;
389 /// Test whether the architecture is 16-bit
391 /// Note that this tests for 16-bit pointer width, and nothing else.
392 bool isArch16Bit() const;
394 /// isOSVersionLT - Helper function for doing comparisons against version
395 /// numbers included in the target triple.
396 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
397 unsigned Micro = 0) const {
399 getOSVersion(LHS[0], LHS[1], LHS[2]);
402 return LHS[0] < Major;
404 return LHS[1] < Minor;
406 return LHS[1] < Micro;
411 bool isOSVersionLT(const Triple &Other) const {
413 Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
414 return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
417 /// isMacOSXVersionLT - Comparison function for checking OS X version
418 /// compatibility, which handles supporting skewed version numbering schemes
419 /// used by the "darwin" triples.
420 bool isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
421 unsigned Micro = 0) const {
422 assert(isMacOSX() && "Not an OS X triple!");
424 // If this is OS X, expect a sane version number.
425 if (getOS() == Triple::MacOSX)
426 return isOSVersionLT(Major, Minor, Micro);
428 // Otherwise, compare to the "Darwin" number.
429 assert(Major == 10 && "Unexpected major version");
430 return isOSVersionLT(Minor + 4, Micro, 0);
433 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
434 /// "darwin" and "osx" as OS X triples.
435 bool isMacOSX() const {
436 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
439 /// Is this an iOS triple.
440 /// Note: This identifies tvOS as a variant of iOS. If that ever
441 /// changes, i.e., if the two operating systems diverge or their version
442 /// numbers get out of sync, that will need to be changed.
443 /// watchOS has completely different version numbers so it is not included.
445 return getOS() == Triple::IOS || isTvOS();
448 /// Is this an Apple tvOS triple.
449 bool isTvOS() const {
450 return getOS() == Triple::TvOS;
453 /// Is this an Apple watchOS triple.
454 bool isWatchOS() const {
455 return getOS() == Triple::WatchOS;
458 bool isWatchABI() const {
459 return getSubArch() == Triple::ARMSubArch_v7k;
462 /// isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
463 bool isOSDarwin() const {
464 return isMacOSX() || isiOS() || isWatchOS();
467 bool isOSNetBSD() const {
468 return getOS() == Triple::NetBSD;
471 bool isOSOpenBSD() const {
472 return getOS() == Triple::OpenBSD;
475 bool isOSFreeBSD() const {
476 return getOS() == Triple::FreeBSD;
479 bool isOSFuchsia() const {
480 return getOS() == Triple::Fuchsia;
483 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
485 bool isOSSolaris() const {
486 return getOS() == Triple::Solaris;
489 bool isOSBitrig() const {
490 return getOS() == Triple::Bitrig;
493 bool isOSIAMCU() const {
494 return getOS() == Triple::ELFIAMCU;
497 bool isGNUEnvironment() const {
498 EnvironmentType Env = getEnvironment();
499 return Env == Triple::GNU || Env == Triple::GNUABI64 ||
500 Env == Triple::GNUEABI || Env == Triple::GNUEABIHF ||
501 Env == Triple::GNUX32;
504 bool isOSContiki() const {
505 return getOS() == Triple::Contiki;
508 /// Checks if the environment could be MSVC.
509 bool isWindowsMSVCEnvironment() const {
510 return getOS() == Triple::Win32 &&
511 (getEnvironment() == Triple::UnknownEnvironment ||
512 getEnvironment() == Triple::MSVC);
515 /// Checks if the environment is MSVC.
516 bool isKnownWindowsMSVCEnvironment() const {
517 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
520 bool isWindowsCoreCLREnvironment() const {
521 return getOS() == Triple::Win32 && getEnvironment() == Triple::CoreCLR;
524 bool isWindowsItaniumEnvironment() const {
525 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
528 bool isWindowsCygwinEnvironment() const {
529 return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
532 bool isWindowsGNUEnvironment() const {
533 return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
536 /// Tests for either Cygwin or MinGW OS
537 bool isOSCygMing() const {
538 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
541 /// Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
542 bool isOSMSVCRT() const {
543 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
544 isWindowsItaniumEnvironment();
547 /// Tests whether the OS is Windows.
548 bool isOSWindows() const {
549 return getOS() == Triple::Win32;
552 /// Tests whether the OS is NaCl (Native Client)
553 bool isOSNaCl() const {
554 return getOS() == Triple::NaCl;
557 /// Tests whether the OS is Linux.
558 bool isOSLinux() const {
559 return getOS() == Triple::Linux;
562 /// Tests whether the OS is kFreeBSD.
563 bool isOSKFreeBSD() const {
564 return getOS() == Triple::KFreeBSD;
567 /// Tests whether the OS uses glibc.
568 bool isOSGlibc() const {
569 return (getOS() == Triple::Linux || getOS() == Triple::KFreeBSD) &&
573 /// Tests whether the OS uses the ELF binary format.
574 bool isOSBinFormatELF() const {
575 return getObjectFormat() == Triple::ELF;
578 /// Tests whether the OS uses the COFF binary format.
579 bool isOSBinFormatCOFF() const {
580 return getObjectFormat() == Triple::COFF;
583 /// Tests whether the environment is MachO.
584 bool isOSBinFormatMachO() const {
585 return getObjectFormat() == Triple::MachO;
588 /// Tests whether the OS uses the Wasm binary format.
589 bool isOSBinFormatWasm() const {
590 return getObjectFormat() == Triple::Wasm;
593 /// Tests whether the target is the PS4 CPU
594 bool isPS4CPU() const {
595 return getArch() == Triple::x86_64 &&
596 getVendor() == Triple::SCEI &&
597 getOS() == Triple::PS4;
600 /// Tests whether the target is the PS4 platform
602 return getVendor() == Triple::SCEI &&
603 getOS() == Triple::PS4;
606 /// Tests whether the target is Android
607 bool isAndroid() const { return getEnvironment() == Triple::Android; }
609 bool isAndroidVersionLT(unsigned Major) const {
610 assert(isAndroid() && "Not an Android triple!");
613 getEnvironmentVersion(Env[0], Env[1], Env[2]);
615 // 64-bit targets did not exist before API level 21 (Lollipop).
616 if (isArch64Bit() && Env[0] < 21)
619 return Env[0] < Major;
622 /// Tests whether the environment is musl-libc
623 bool isMusl() const {
624 return getEnvironment() == Triple::Musl ||
625 getEnvironment() == Triple::MuslEABI ||
626 getEnvironment() == Triple::MuslEABIHF;
629 /// Tests whether the target is NVPTX (32- or 64-bit).
630 bool isNVPTX() const {
631 return getArch() == Triple::nvptx || getArch() == Triple::nvptx64;
634 /// Tests wether the target supports comdat
635 bool supportsCOMDAT() const { return !isOSBinFormatMachO(); }
641 /// setArch - Set the architecture (first) component of the triple
643 void setArch(ArchType Kind);
645 /// setVendor - Set the vendor (second) component of the triple to a
647 void setVendor(VendorType Kind);
649 /// setOS - Set the operating system (third) component of the triple
651 void setOS(OSType Kind);
653 /// setEnvironment - Set the environment (fourth) component of the triple
655 void setEnvironment(EnvironmentType Kind);
657 /// setObjectFormat - Set the object file format
658 void setObjectFormat(ObjectFormatType Kind);
660 /// setTriple - Set all components to the new triple \p Str.
661 void setTriple(const Twine &Str);
663 /// setArchName - Set the architecture (first) component of the
665 void setArchName(StringRef Str);
667 /// setVendorName - Set the vendor (second) component of the triple
669 void setVendorName(StringRef Str);
671 /// setOSName - Set the operating system (third) component of the
673 void setOSName(StringRef Str);
675 /// setEnvironmentName - Set the optional environment (fourth)
676 /// component of the triple by name.
677 void setEnvironmentName(StringRef Str);
679 /// setOSAndEnvironmentName - Set the operating system and optional
680 /// environment components with a single string.
681 void setOSAndEnvironmentName(StringRef Str);
684 /// @name Helpers to build variants of a particular triple.
687 /// Form a triple with a 32-bit variant of the current architecture.
689 /// This can be used to move across "families" of architectures where useful.
691 /// \returns A new triple with a 32-bit architecture or an unknown
692 /// architecture if no such variant can be found.
693 llvm::Triple get32BitArchVariant() const;
695 /// Form a triple with a 64-bit variant of the current architecture.
697 /// This can be used to move across "families" of architectures where useful.
699 /// \returns A new triple with a 64-bit architecture or an unknown
700 /// architecture if no such variant can be found.
701 llvm::Triple get64BitArchVariant() const;
703 /// Form a triple with a big endian variant of the current architecture.
705 /// This can be used to move across "families" of architectures where useful.
707 /// \returns A new triple with a big endian architecture or an unknown
708 /// architecture if no such variant can be found.
709 llvm::Triple getBigEndianArchVariant() const;
711 /// Form a triple with a little endian variant of the current architecture.
713 /// This can be used to move across "families" of architectures where useful.
715 /// \returns A new triple with a little endian architecture or an unknown
716 /// architecture if no such variant can be found.
717 llvm::Triple getLittleEndianArchVariant() const;
719 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
721 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
722 /// string then the triple's arch name is used.
723 StringRef getARMCPUForArch(StringRef Arch = StringRef()) const;
725 /// Tests whether the target triple is little endian.
727 /// \returns true if the triple is little endian, false otherwise.
728 bool isLittleEndian() const;
730 /// Test whether target triples are compatible.
731 bool isCompatibleWith(const Triple &Other) const;
733 /// Merge target triples.
734 std::string merge(const Triple &Other) const;
737 /// @name Static helpers for IDs.
740 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
741 static StringRef getArchTypeName(ArchType Kind);
743 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
744 /// architecture. This is the prefix used by the architecture specific
745 /// builtins, and is suitable for passing to \see
746 /// Intrinsic::getIntrinsicForGCCBuiltin().
748 /// \return - The architecture prefix, or 0 if none is defined.
749 static StringRef getArchTypePrefix(ArchType Kind);
751 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
752 static StringRef getVendorTypeName(VendorType Kind);
754 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
755 static StringRef getOSTypeName(OSType Kind);
757 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
759 static StringRef getEnvironmentTypeName(EnvironmentType Kind);
762 /// @name Static helpers for converting alternate architecture names.
765 /// getArchTypeForLLVMName - The canonical type for the given LLVM
766 /// architecture name (e.g., "x86").
767 static ArchType getArchTypeForLLVMName(StringRef Str);
772 } // End llvm namespace