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
168 NaCl, // Native Client
169 CNK, // BG/P Compute-Node Kernel
173 NVCL, // NVIDIA OpenCL
174 AMDHSA, // AMD HSA Runtime
178 WatchOS, // Apple watchOS
183 enum EnvironmentType {
205 LastEnvironmentType = OpenCL
207 enum ObjectFormatType {
219 /// The parsed arch type.
222 /// The parsed subarchitecture type.
225 /// The parsed vendor type.
228 /// The parsed OS type.
231 /// The parsed Environment type.
232 EnvironmentType Environment;
234 /// The object format type.
235 ObjectFormatType ObjectFormat;
238 /// @name Constructors
241 /// Default constructor is the same as an empty string and leaves all
242 /// triple fields unknown.
244 : Data(), Arch(), SubArch(), Vendor(), OS(), Environment(),
247 explicit Triple(const Twine &Str);
248 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
249 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
250 const Twine &EnvironmentStr);
252 bool operator==(const Triple &Other) const {
253 return Arch == Other.Arch && SubArch == Other.SubArch &&
254 Vendor == Other.Vendor && OS == Other.OS &&
255 Environment == Other.Environment &&
256 ObjectFormat == Other.ObjectFormat;
259 bool operator!=(const Triple &Other) const {
260 return !(*this == Other);
264 /// @name Normalization
267 /// normalize - Turn an arbitrary machine specification into the canonical
268 /// triple form (or something sensible that the Triple class understands if
269 /// nothing better can reasonably be done). In particular, it handles the
270 /// common case in which otherwise valid components are in the wrong order.
271 static std::string normalize(StringRef Str);
273 /// Return the normalized form of this triple's string.
274 std::string normalize() const { return normalize(Data); }
277 /// @name Typed Component Access
280 /// getArch - Get the parsed architecture type of this triple.
281 ArchType getArch() const { return Arch; }
283 /// getSubArch - get the parsed subarchitecture type for this triple.
284 SubArchType getSubArch() const { return SubArch; }
286 /// getVendor - Get the parsed vendor type of this triple.
287 VendorType getVendor() const { return Vendor; }
289 /// getOS - Get the parsed operating system type of this triple.
290 OSType getOS() const { return OS; }
292 /// hasEnvironment - Does this triple have the optional environment
293 /// (fourth) component?
294 bool hasEnvironment() const {
295 return getEnvironmentName() != "";
298 /// getEnvironment - Get the parsed environment type of this triple.
299 EnvironmentType getEnvironment() const { return Environment; }
301 /// Parse the version number from the OS name component of the
302 /// triple, if present.
304 /// For example, "fooos1.2.3" would return (1, 2, 3).
306 /// If an entry is not defined, it will be returned as 0.
307 void getEnvironmentVersion(unsigned &Major, unsigned &Minor,
308 unsigned &Micro) const;
310 /// getFormat - Get the object format for this triple.
311 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
313 /// getOSVersion - Parse the version number from the OS name component of the
314 /// triple, if present.
316 /// For example, "fooos1.2.3" would return (1, 2, 3).
318 /// If an entry is not defined, it will be returned as 0.
319 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
321 /// getOSMajorVersion - Return just the major version number, this is
322 /// specialized because it is a common query.
323 unsigned getOSMajorVersion() const {
324 unsigned Maj, Min, Micro;
325 getOSVersion(Maj, Min, Micro);
329 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
330 /// translate generic "darwin" versions to the corresponding OS X versions.
331 /// This may also be called with IOS triples but the OS X version number is
332 /// just set to a constant 10.4.0 in that case. Returns true if successful.
333 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
334 unsigned &Micro) const;
336 /// getiOSVersion - Parse the version number as with getOSVersion. This should
337 /// only be called with IOS or generic triples.
338 void getiOSVersion(unsigned &Major, unsigned &Minor,
339 unsigned &Micro) const;
341 /// getWatchOSVersion - Parse the version number as with getOSVersion. This
342 /// should only be called with WatchOS or generic triples.
343 void getWatchOSVersion(unsigned &Major, unsigned &Minor,
344 unsigned &Micro) const;
347 /// @name Direct Component Access
350 const std::string &str() const { return Data; }
352 const std::string &getTriple() const { return Data; }
354 /// getArchName - Get the architecture (first) component of the
356 StringRef getArchName() const;
358 /// getVendorName - Get the vendor (second) component of the triple.
359 StringRef getVendorName() const;
361 /// getOSName - Get the operating system (third) component of the
363 StringRef getOSName() const;
365 /// getEnvironmentName - Get the optional environment (fourth)
366 /// component of the triple, or "" if empty.
367 StringRef getEnvironmentName() const;
369 /// getOSAndEnvironmentName - Get the operating system and optional
370 /// environment components as a single string (separated by a '-'
371 /// if the environment component is present).
372 StringRef getOSAndEnvironmentName() const;
375 /// @name Convenience Predicates
378 /// Test whether the architecture is 64-bit
380 /// Note that this tests for 64-bit pointer width, and nothing else. Note
381 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
382 /// 16-bit. The inner details of pointer width for particular architectures
383 /// is not summed up in the triple, and so only a coarse grained predicate
384 /// system is provided.
385 bool isArch64Bit() const;
387 /// Test whether the architecture is 32-bit
389 /// Note that this tests for 32-bit pointer width, and nothing else.
390 bool isArch32Bit() const;
392 /// Test whether the architecture is 16-bit
394 /// Note that this tests for 16-bit pointer width, and nothing else.
395 bool isArch16Bit() const;
397 /// isOSVersionLT - Helper function for doing comparisons against version
398 /// numbers included in the target triple.
399 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
400 unsigned Micro = 0) const {
402 getOSVersion(LHS[0], LHS[1], LHS[2]);
405 return LHS[0] < Major;
407 return LHS[1] < Minor;
409 return LHS[1] < Micro;
414 bool isOSVersionLT(const Triple &Other) const {
416 Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
417 return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
420 /// isMacOSXVersionLT - Comparison function for checking OS X version
421 /// compatibility, which handles supporting skewed version numbering schemes
422 /// used by the "darwin" triples.
423 bool isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
424 unsigned Micro = 0) const {
425 assert(isMacOSX() && "Not an OS X triple!");
427 // If this is OS X, expect a sane version number.
428 if (getOS() == Triple::MacOSX)
429 return isOSVersionLT(Major, Minor, Micro);
431 // Otherwise, compare to the "Darwin" number.
432 assert(Major == 10 && "Unexpected major version");
433 return isOSVersionLT(Minor + 4, Micro, 0);
436 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
437 /// "darwin" and "osx" as OS X triples.
438 bool isMacOSX() const {
439 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
442 /// Is this an iOS triple.
443 /// Note: This identifies tvOS as a variant of iOS. If that ever
444 /// changes, i.e., if the two operating systems diverge or their version
445 /// numbers get out of sync, that will need to be changed.
446 /// watchOS has completely different version numbers so it is not included.
448 return getOS() == Triple::IOS || isTvOS();
451 /// Is this an Apple tvOS triple.
452 bool isTvOS() const {
453 return getOS() == Triple::TvOS;
456 /// Is this an Apple watchOS triple.
457 bool isWatchOS() const {
458 return getOS() == Triple::WatchOS;
461 bool isWatchABI() const {
462 return getSubArch() == Triple::ARMSubArch_v7k;
465 /// isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
466 bool isOSDarwin() const {
467 return isMacOSX() || isiOS() || isWatchOS();
470 bool isOSNetBSD() const {
471 return getOS() == Triple::NetBSD;
474 bool isOSOpenBSD() const {
475 return getOS() == Triple::OpenBSD;
478 bool isOSFreeBSD() const {
479 return getOS() == Triple::FreeBSD;
482 bool isOSFuchsia() const {
483 return getOS() == Triple::Fuchsia;
486 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
488 bool isOSSolaris() const {
489 return getOS() == Triple::Solaris;
492 bool isOSBitrig() const {
493 return getOS() == Triple::Bitrig;
496 bool isOSIAMCU() const {
497 return getOS() == Triple::ELFIAMCU;
500 bool isGNUEnvironment() const {
501 EnvironmentType Env = getEnvironment();
502 return Env == Triple::GNU || Env == Triple::GNUABI64 ||
503 Env == Triple::GNUEABI || Env == Triple::GNUEABIHF ||
504 Env == Triple::GNUX32;
507 bool isOSContiki() const {
508 return getOS() == Triple::Contiki;
511 /// Checks if the environment could be MSVC.
512 bool isWindowsMSVCEnvironment() const {
513 return getOS() == Triple::Win32 &&
514 (getEnvironment() == Triple::UnknownEnvironment ||
515 getEnvironment() == Triple::MSVC);
518 /// Checks if the environment is MSVC.
519 bool isKnownWindowsMSVCEnvironment() const {
520 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
523 bool isWindowsCoreCLREnvironment() const {
524 return getOS() == Triple::Win32 && getEnvironment() == Triple::CoreCLR;
527 bool isWindowsItaniumEnvironment() const {
528 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
531 bool isWindowsCygwinEnvironment() const {
532 return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
535 bool isWindowsGNUEnvironment() const {
536 return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
539 /// Tests for either Cygwin or MinGW OS
540 bool isOSCygMing() const {
541 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
544 /// Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
545 bool isOSMSVCRT() const {
546 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
547 isWindowsItaniumEnvironment();
550 /// Tests whether the OS is Windows.
551 bool isOSWindows() const {
552 return getOS() == Triple::Win32;
555 /// Tests whether the OS is NaCl (Native Client)
556 bool isOSNaCl() const {
557 return getOS() == Triple::NaCl;
560 /// Tests whether the OS is Linux.
561 bool isOSLinux() const {
562 return getOS() == Triple::Linux;
565 /// Tests whether the OS is kFreeBSD.
566 bool isOSKFreeBSD() const {
567 return getOS() == Triple::KFreeBSD;
570 /// Tests whether the OS uses glibc.
571 bool isOSGlibc() const {
572 return (getOS() == Triple::Linux || getOS() == Triple::KFreeBSD) &&
576 /// Tests whether the OS uses the ELF binary format.
577 bool isOSBinFormatELF() const {
578 return getObjectFormat() == Triple::ELF;
581 /// Tests whether the OS uses the COFF binary format.
582 bool isOSBinFormatCOFF() const {
583 return getObjectFormat() == Triple::COFF;
586 /// Tests whether the environment is MachO.
587 bool isOSBinFormatMachO() const {
588 return getObjectFormat() == Triple::MachO;
591 /// Tests whether the OS uses the Wasm binary format.
592 bool isOSBinFormatWasm() const {
593 return getObjectFormat() == Triple::Wasm;
596 /// Tests whether the target is the PS4 CPU
597 bool isPS4CPU() const {
598 return getArch() == Triple::x86_64 &&
599 getVendor() == Triple::SCEI &&
600 getOS() == Triple::PS4;
603 /// Tests whether the target is the PS4 platform
605 return getVendor() == Triple::SCEI &&
606 getOS() == Triple::PS4;
609 /// Tests whether the target is Android
610 bool isAndroid() const { return getEnvironment() == Triple::Android; }
612 bool isAndroidVersionLT(unsigned Major) const {
613 assert(isAndroid() && "Not an Android triple!");
616 getEnvironmentVersion(Env[0], Env[1], Env[2]);
618 // 64-bit targets did not exist before API level 21 (Lollipop).
619 if (isArch64Bit() && Env[0] < 21)
622 return Env[0] < Major;
625 /// Tests whether the environment is musl-libc
626 bool isMusl() const {
627 return getEnvironment() == Triple::Musl ||
628 getEnvironment() == Triple::MuslEABI ||
629 getEnvironment() == Triple::MuslEABIHF;
632 /// Tests whether the target is NVPTX (32- or 64-bit).
633 bool isNVPTX() const {
634 return getArch() == Triple::nvptx || getArch() == Triple::nvptx64;
637 /// Tests wether the target supports comdat
638 bool supportsCOMDAT() const { return !isOSBinFormatMachO(); }
644 /// setArch - Set the architecture (first) component of the triple
646 void setArch(ArchType Kind);
648 /// setVendor - Set the vendor (second) component of the triple to a
650 void setVendor(VendorType Kind);
652 /// setOS - Set the operating system (third) component of the triple
654 void setOS(OSType Kind);
656 /// setEnvironment - Set the environment (fourth) component of the triple
658 void setEnvironment(EnvironmentType Kind);
660 /// setObjectFormat - Set the object file format
661 void setObjectFormat(ObjectFormatType Kind);
663 /// setTriple - Set all components to the new triple \p Str.
664 void setTriple(const Twine &Str);
666 /// setArchName - Set the architecture (first) component of the
668 void setArchName(StringRef Str);
670 /// setVendorName - Set the vendor (second) component of the triple
672 void setVendorName(StringRef Str);
674 /// setOSName - Set the operating system (third) component of the
676 void setOSName(StringRef Str);
678 /// setEnvironmentName - Set the optional environment (fourth)
679 /// component of the triple by name.
680 void setEnvironmentName(StringRef Str);
682 /// setOSAndEnvironmentName - Set the operating system and optional
683 /// environment components with a single string.
684 void setOSAndEnvironmentName(StringRef Str);
687 /// @name Helpers to build variants of a particular triple.
690 /// Form a triple with a 32-bit variant of the current architecture.
692 /// This can be used to move across "families" of architectures where useful.
694 /// \returns A new triple with a 32-bit architecture or an unknown
695 /// architecture if no such variant can be found.
696 llvm::Triple get32BitArchVariant() const;
698 /// Form a triple with a 64-bit variant of the current architecture.
700 /// This can be used to move across "families" of architectures where useful.
702 /// \returns A new triple with a 64-bit architecture or an unknown
703 /// architecture if no such variant can be found.
704 llvm::Triple get64BitArchVariant() const;
706 /// Form a triple with a big endian variant of the current architecture.
708 /// This can be used to move across "families" of architectures where useful.
710 /// \returns A new triple with a big endian architecture or an unknown
711 /// architecture if no such variant can be found.
712 llvm::Triple getBigEndianArchVariant() const;
714 /// Form a triple with a little endian variant of the current architecture.
716 /// This can be used to move across "families" of architectures where useful.
718 /// \returns A new triple with a little endian architecture or an unknown
719 /// architecture if no such variant can be found.
720 llvm::Triple getLittleEndianArchVariant() const;
722 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
724 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
725 /// string then the triple's arch name is used.
726 StringRef getARMCPUForArch(StringRef Arch = StringRef()) const;
728 /// Tests whether the target triple is little endian.
730 /// \returns true if the triple is little endian, false otherwise.
731 bool isLittleEndian() const;
733 /// Test whether target triples are compatible.
734 bool isCompatibleWith(const Triple &Other) const;
736 /// Merge target triples.
737 std::string merge(const Triple &Other) const;
740 /// @name Static helpers for IDs.
743 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
744 static StringRef getArchTypeName(ArchType Kind);
746 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
747 /// architecture. This is the prefix used by the architecture specific
748 /// builtins, and is suitable for passing to \see
749 /// Intrinsic::getIntrinsicForGCCBuiltin().
751 /// \return - The architecture prefix, or 0 if none is defined.
752 static StringRef getArchTypePrefix(ArchType Kind);
754 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
755 static StringRef getVendorTypeName(VendorType Kind);
757 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
758 static StringRef getOSTypeName(OSType Kind);
760 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
762 static StringRef getEnvironmentTypeName(EnvironmentType Kind);
765 /// @name Static helpers for converting alternate architecture names.
768 /// getArchTypeForLLVMName - The canonical type for the given LLVM
769 /// architecture name (e.g., "x86").
770 static ArchType getArchTypeForLLVMName(StringRef Str);
775 } // End llvm namespace