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 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
106 ARMSubArch_v8m_baseline,
107 ARMSubArch_v8m_mainline,
136 ImaginationTechnologies,
144 LastVendorType = SUSE
166 NaCl, // Native Client
167 CNK, // BG/P Compute-Node Kernel
171 NVCL, // NVIDIA OpenCL
172 AMDHSA, // AMD HSA Runtime
176 WatchOS, // Apple watchOS
181 enum EnvironmentType {
203 LastEnvironmentType = OpenCL
205 enum ObjectFormatType {
217 /// The parsed arch type.
220 /// The parsed subarchitecture type.
223 /// The parsed vendor type.
226 /// The parsed OS type.
229 /// The parsed Environment type.
230 EnvironmentType Environment;
232 /// The object format type.
233 ObjectFormatType ObjectFormat;
236 /// @name Constructors
239 /// Default constructor is the same as an empty string and leaves all
240 /// triple fields unknown.
241 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
243 explicit Triple(const Twine &Str);
244 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
245 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
246 const Twine &EnvironmentStr);
248 bool operator==(const Triple &Other) const {
249 return Arch == Other.Arch && SubArch == Other.SubArch &&
250 Vendor == Other.Vendor && OS == Other.OS &&
251 Environment == Other.Environment &&
252 ObjectFormat == Other.ObjectFormat;
256 /// @name Normalization
259 /// normalize - Turn an arbitrary machine specification into the canonical
260 /// triple form (or something sensible that the Triple class understands if
261 /// nothing better can reasonably be done). In particular, it handles the
262 /// common case in which otherwise valid components are in the wrong order.
263 static std::string normalize(StringRef Str);
265 /// Return the normalized form of this triple's string.
266 std::string normalize() const { return normalize(Data); }
269 /// @name Typed Component Access
272 /// getArch - Get the parsed architecture type of this triple.
273 ArchType getArch() const { return Arch; }
275 /// getSubArch - get the parsed subarchitecture type for this triple.
276 SubArchType getSubArch() const { return SubArch; }
278 /// getVendor - Get the parsed vendor type of this triple.
279 VendorType getVendor() const { return Vendor; }
281 /// getOS - Get the parsed operating system type of this triple.
282 OSType getOS() const { return OS; }
284 /// hasEnvironment - Does this triple have the optional environment
285 /// (fourth) component?
286 bool hasEnvironment() const {
287 return getEnvironmentName() != "";
290 /// getEnvironment - Get the parsed environment type of this triple.
291 EnvironmentType getEnvironment() const { return Environment; }
293 /// Parse the version number from the OS name component of the
294 /// triple, if present.
296 /// For example, "fooos1.2.3" would return (1, 2, 3).
298 /// If an entry is not defined, it will be returned as 0.
299 void getEnvironmentVersion(unsigned &Major, unsigned &Minor,
300 unsigned &Micro) const;
302 /// getFormat - Get the object format for this triple.
303 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
305 /// getOSVersion - Parse the version number from the OS name component of the
306 /// triple, if present.
308 /// For example, "fooos1.2.3" would return (1, 2, 3).
310 /// If an entry is not defined, it will be returned as 0.
311 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
313 /// getOSMajorVersion - Return just the major version number, this is
314 /// specialized because it is a common query.
315 unsigned getOSMajorVersion() const {
316 unsigned Maj, Min, Micro;
317 getOSVersion(Maj, Min, Micro);
321 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
322 /// translate generic "darwin" versions to the corresponding OS X versions.
323 /// This may also be called with IOS triples but the OS X version number is
324 /// just set to a constant 10.4.0 in that case. Returns true if successful.
325 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
326 unsigned &Micro) const;
328 /// getiOSVersion - Parse the version number as with getOSVersion. This should
329 /// only be called with IOS or generic triples.
330 void getiOSVersion(unsigned &Major, unsigned &Minor,
331 unsigned &Micro) const;
333 /// getWatchOSVersion - Parse the version number as with getOSVersion. This
334 /// should only be called with WatchOS or generic triples.
335 void getWatchOSVersion(unsigned &Major, unsigned &Minor,
336 unsigned &Micro) const;
339 /// @name Direct Component Access
342 const std::string &str() const { return Data; }
344 const std::string &getTriple() const { return Data; }
346 /// getArchName - Get the architecture (first) component of the
348 StringRef getArchName() const;
350 /// getVendorName - Get the vendor (second) component of the triple.
351 StringRef getVendorName() const;
353 /// getOSName - Get the operating system (third) component of the
355 StringRef getOSName() const;
357 /// getEnvironmentName - Get the optional environment (fourth)
358 /// component of the triple, or "" if empty.
359 StringRef getEnvironmentName() const;
361 /// getOSAndEnvironmentName - Get the operating system and optional
362 /// environment components as a single string (separated by a '-'
363 /// if the environment component is present).
364 StringRef getOSAndEnvironmentName() const;
367 /// @name Convenience Predicates
370 /// Test whether the architecture is 64-bit
372 /// Note that this tests for 64-bit pointer width, and nothing else. Note
373 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
374 /// 16-bit. The inner details of pointer width for particular architectures
375 /// is not summed up in the triple, and so only a coarse grained predicate
376 /// system is provided.
377 bool isArch64Bit() const;
379 /// Test whether the architecture is 32-bit
381 /// Note that this tests for 32-bit pointer width, and nothing else.
382 bool isArch32Bit() const;
384 /// Test whether the architecture is 16-bit
386 /// Note that this tests for 16-bit pointer width, and nothing else.
387 bool isArch16Bit() const;
389 /// isOSVersionLT - Helper function for doing comparisons against version
390 /// numbers included in the target triple.
391 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
392 unsigned Micro = 0) const {
394 getOSVersion(LHS[0], LHS[1], LHS[2]);
397 return LHS[0] < Major;
399 return LHS[1] < Minor;
401 return LHS[1] < Micro;
406 bool isOSVersionLT(const Triple &Other) const {
408 Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
409 return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
412 /// isMacOSXVersionLT - Comparison function for checking OS X version
413 /// compatibility, which handles supporting skewed version numbering schemes
414 /// used by the "darwin" triples.
415 bool isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
416 unsigned Micro = 0) const {
417 assert(isMacOSX() && "Not an OS X triple!");
419 // If this is OS X, expect a sane version number.
420 if (getOS() == Triple::MacOSX)
421 return isOSVersionLT(Major, Minor, Micro);
423 // Otherwise, compare to the "Darwin" number.
424 assert(Major == 10 && "Unexpected major version");
425 return isOSVersionLT(Minor + 4, Micro, 0);
428 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
429 /// "darwin" and "osx" as OS X triples.
430 bool isMacOSX() const {
431 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
434 /// Is this an iOS triple.
435 /// Note: This identifies tvOS as a variant of iOS. If that ever
436 /// changes, i.e., if the two operating systems diverge or their version
437 /// numbers get out of sync, that will need to be changed.
438 /// watchOS has completely different version numbers so it is not included.
440 return getOS() == Triple::IOS || isTvOS();
443 /// Is this an Apple tvOS triple.
444 bool isTvOS() const {
445 return getOS() == Triple::TvOS;
448 /// Is this an Apple watchOS triple.
449 bool isWatchOS() const {
450 return getOS() == Triple::WatchOS;
453 bool isWatchABI() const {
454 return getSubArch() == Triple::ARMSubArch_v7k;
457 /// isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
458 bool isOSDarwin() const {
459 return isMacOSX() || isiOS() || isWatchOS();
462 bool isOSNetBSD() const {
463 return getOS() == Triple::NetBSD;
466 bool isOSOpenBSD() const {
467 return getOS() == Triple::OpenBSD;
470 bool isOSFreeBSD() const {
471 return getOS() == Triple::FreeBSD;
474 bool isOSFuchsia() const {
475 return getOS() == Triple::Fuchsia;
478 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
480 bool isOSSolaris() const {
481 return getOS() == Triple::Solaris;
484 bool isOSBitrig() const {
485 return getOS() == Triple::Bitrig;
488 bool isOSIAMCU() const {
489 return getOS() == Triple::ELFIAMCU;
492 bool isGNUEnvironment() const {
493 EnvironmentType Env = getEnvironment();
494 return Env == Triple::GNU || Env == Triple::GNUABI64 ||
495 Env == Triple::GNUEABI || Env == Triple::GNUEABIHF ||
496 Env == Triple::GNUX32;
499 bool isOSContiki() const {
500 return getOS() == Triple::Contiki;
503 /// Checks if the environment could be MSVC.
504 bool isWindowsMSVCEnvironment() const {
505 return getOS() == Triple::Win32 &&
506 (getEnvironment() == Triple::UnknownEnvironment ||
507 getEnvironment() == Triple::MSVC);
510 /// Checks if the environment is MSVC.
511 bool isKnownWindowsMSVCEnvironment() const {
512 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
515 bool isWindowsCoreCLREnvironment() const {
516 return getOS() == Triple::Win32 && getEnvironment() == Triple::CoreCLR;
519 bool isWindowsItaniumEnvironment() const {
520 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
523 bool isWindowsCygwinEnvironment() const {
524 return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
527 bool isWindowsGNUEnvironment() const {
528 return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
531 /// Tests for either Cygwin or MinGW OS
532 bool isOSCygMing() const {
533 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
536 /// Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
537 bool isOSMSVCRT() const {
538 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
539 isWindowsItaniumEnvironment();
542 /// Tests whether the OS is Windows.
543 bool isOSWindows() const {
544 return getOS() == Triple::Win32;
547 /// Tests whether the OS is NaCl (Native Client)
548 bool isOSNaCl() const {
549 return getOS() == Triple::NaCl;
552 /// Tests whether the OS is Linux.
553 bool isOSLinux() const {
554 return getOS() == Triple::Linux;
557 /// Tests whether the OS is kFreeBSD.
558 bool isOSKFreeBSD() const {
559 return getOS() == Triple::KFreeBSD;
562 /// Tests whether the OS uses glibc.
563 bool isOSGlibc() const {
564 return (getOS() == Triple::Linux || getOS() == Triple::KFreeBSD) &&
568 /// Tests whether the OS uses the ELF binary format.
569 bool isOSBinFormatELF() const {
570 return getObjectFormat() == Triple::ELF;
573 /// Tests whether the OS uses the COFF binary format.
574 bool isOSBinFormatCOFF() const {
575 return getObjectFormat() == Triple::COFF;
578 /// Tests whether the environment is MachO.
579 bool isOSBinFormatMachO() const {
580 return getObjectFormat() == Triple::MachO;
583 /// Tests whether the OS uses the Wasm binary format.
584 bool isOSBinFormatWasm() const {
585 return getObjectFormat() == Triple::Wasm;
588 /// Tests whether the target is the PS4 CPU
589 bool isPS4CPU() const {
590 return getArch() == Triple::x86_64 &&
591 getVendor() == Triple::SCEI &&
592 getOS() == Triple::PS4;
595 /// Tests whether the target is the PS4 platform
597 return getVendor() == Triple::SCEI &&
598 getOS() == Triple::PS4;
601 /// Tests whether the target is Android
602 bool isAndroid() const { return getEnvironment() == Triple::Android; }
604 bool isAndroidVersionLT(unsigned Major) const {
605 assert(isAndroid() && "Not an Android triple!");
608 getEnvironmentVersion(Env[0], Env[1], Env[2]);
610 // 64-bit targets did not exist before API level 21 (Lollipop).
611 if (isArch64Bit() && Env[0] < 21)
614 return Env[0] < Major;
617 /// Tests whether the environment is musl-libc
618 bool isMusl() const {
619 return getEnvironment() == Triple::Musl ||
620 getEnvironment() == Triple::MuslEABI ||
621 getEnvironment() == Triple::MuslEABIHF;
624 /// Tests whether the target is NVPTX (32- or 64-bit).
625 bool isNVPTX() const {
626 return getArch() == Triple::nvptx || getArch() == Triple::nvptx64;
629 /// Tests wether the target supports comdat
630 bool supportsCOMDAT() const { return !isOSBinFormatMachO(); }
636 /// setArch - Set the architecture (first) component of the triple
638 void setArch(ArchType Kind);
640 /// setVendor - Set the vendor (second) component of the triple to a
642 void setVendor(VendorType Kind);
644 /// setOS - Set the operating system (third) component of the triple
646 void setOS(OSType Kind);
648 /// setEnvironment - Set the environment (fourth) component of the triple
650 void setEnvironment(EnvironmentType Kind);
652 /// setObjectFormat - Set the object file format
653 void setObjectFormat(ObjectFormatType Kind);
655 /// setTriple - Set all components to the new triple \p Str.
656 void setTriple(const Twine &Str);
658 /// setArchName - Set the architecture (first) component of the
660 void setArchName(StringRef Str);
662 /// setVendorName - Set the vendor (second) component of the triple
664 void setVendorName(StringRef Str);
666 /// setOSName - Set the operating system (third) component of the
668 void setOSName(StringRef Str);
670 /// setEnvironmentName - Set the optional environment (fourth)
671 /// component of the triple by name.
672 void setEnvironmentName(StringRef Str);
674 /// setOSAndEnvironmentName - Set the operating system and optional
675 /// environment components with a single string.
676 void setOSAndEnvironmentName(StringRef Str);
679 /// @name Helpers to build variants of a particular triple.
682 /// Form a triple with a 32-bit variant of the current architecture.
684 /// This can be used to move across "families" of architectures where useful.
686 /// \returns A new triple with a 32-bit architecture or an unknown
687 /// architecture if no such variant can be found.
688 llvm::Triple get32BitArchVariant() const;
690 /// Form a triple with a 64-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 64-bit architecture or an unknown
695 /// architecture if no such variant can be found.
696 llvm::Triple get64BitArchVariant() const;
698 /// Form a triple with a big endian 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 big endian architecture or an unknown
703 /// architecture if no such variant can be found.
704 llvm::Triple getBigEndianArchVariant() const;
706 /// Form a triple with a little 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 little endian architecture or an unknown
711 /// architecture if no such variant can be found.
712 llvm::Triple getLittleEndianArchVariant() const;
714 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
716 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
717 /// string then the triple's arch name is used.
718 StringRef getARMCPUForArch(StringRef Arch = StringRef()) const;
720 /// Tests whether the target triple is little endian.
722 /// \returns true if the triple is little endian, false otherwise.
723 bool isLittleEndian() const;
726 /// @name Static helpers for IDs.
729 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
730 static StringRef getArchTypeName(ArchType Kind);
732 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
733 /// architecture. This is the prefix used by the architecture specific
734 /// builtins, and is suitable for passing to \see
735 /// Intrinsic::getIntrinsicForGCCBuiltin().
737 /// \return - The architecture prefix, or 0 if none is defined.
738 static StringRef getArchTypePrefix(ArchType Kind);
740 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
741 static StringRef getVendorTypeName(VendorType Kind);
743 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
744 static StringRef getOSTypeName(OSType Kind);
746 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
748 static StringRef getEnvironmentTypeName(EnvironmentType Kind);
751 /// @name Static helpers for converting alternate architecture names.
754 /// getArchTypeForLLVMName - The canonical type for the given LLVM
755 /// architecture name (e.g., "x86").
756 static ArchType getArchTypeForLLVMName(StringRef Str);
761 } // End llvm namespace