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 lanai, // Lanai: Lanai 32-bit
90 wasm32, // WebAssembly with 32-bit pointers
91 wasm64, // WebAssembly with 64-bit pointers
92 renderscript32, // 32-bit RenderScript
93 renderscript64, // 64-bit RenderScript
94 LastArchType = renderscript64
102 ARMSubArch_v8m_baseline,
103 ARMSubArch_v8m_mainline,
131 ImaginationTechnologies,
138 LastVendorType = Mesa
159 NaCl, // Native Client
160 CNK, // BG/P Compute-Node Kernel
164 NVCL, // NVIDIA OpenCL
165 AMDHSA, // AMD HSA Runtime
169 WatchOS, // Apple watchOS
173 enum EnvironmentType {
194 LastEnvironmentType = CoreCLR
196 enum ObjectFormatType {
207 /// The parsed arch type.
210 /// The parsed subarchitecture type.
213 /// The parsed vendor type.
216 /// The parsed OS type.
219 /// The parsed Environment type.
220 EnvironmentType Environment;
222 /// The object format type.
223 ObjectFormatType ObjectFormat;
226 /// @name Constructors
229 /// Default constructor is the same as an empty string and leaves all
230 /// triple fields unknown.
231 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
233 explicit Triple(const Twine &Str);
234 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
235 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
236 const Twine &EnvironmentStr);
238 bool operator==(const Triple &Other) const {
239 return Arch == Other.Arch && SubArch == Other.SubArch &&
240 Vendor == Other.Vendor && OS == Other.OS &&
241 Environment == Other.Environment &&
242 ObjectFormat == Other.ObjectFormat;
246 /// @name Normalization
249 /// normalize - Turn an arbitrary machine specification into the canonical
250 /// triple form (or something sensible that the Triple class understands if
251 /// nothing better can reasonably be done). In particular, it handles the
252 /// common case in which otherwise valid components are in the wrong order.
253 static std::string normalize(StringRef Str);
255 /// Return the normalized form of this triple's string.
256 std::string normalize() const { return normalize(Data); }
259 /// @name Typed Component Access
262 /// getArch - Get the parsed architecture type of this triple.
263 ArchType getArch() const { return Arch; }
265 /// getSubArch - get the parsed subarchitecture type for this triple.
266 SubArchType getSubArch() const { return SubArch; }
268 /// getVendor - Get the parsed vendor type of this triple.
269 VendorType getVendor() const { return Vendor; }
271 /// getOS - Get the parsed operating system type of this triple.
272 OSType getOS() const { return OS; }
274 /// hasEnvironment - Does this triple have the optional environment
275 /// (fourth) component?
276 bool hasEnvironment() const {
277 return getEnvironmentName() != "";
280 /// getEnvironment - Get the parsed environment type of this triple.
281 EnvironmentType getEnvironment() const { return Environment; }
283 /// 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 getEnvironmentVersion(unsigned &Major, unsigned &Minor,
290 unsigned &Micro) const;
292 /// getFormat - Get the object format for this triple.
293 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
295 /// getOSVersion - Parse the version number from the OS name component of the
296 /// triple, if present.
298 /// For example, "fooos1.2.3" would return (1, 2, 3).
300 /// If an entry is not defined, it will be returned as 0.
301 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
303 /// getOSMajorVersion - Return just the major version number, this is
304 /// specialized because it is a common query.
305 unsigned getOSMajorVersion() const {
306 unsigned Maj, Min, Micro;
307 getOSVersion(Maj, Min, Micro);
311 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
312 /// translate generic "darwin" versions to the corresponding OS X versions.
313 /// This may also be called with IOS triples but the OS X version number is
314 /// just set to a constant 10.4.0 in that case. Returns true if successful.
315 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
316 unsigned &Micro) const;
318 /// getiOSVersion - Parse the version number as with getOSVersion. This should
319 /// only be called with IOS or generic triples.
320 void getiOSVersion(unsigned &Major, unsigned &Minor,
321 unsigned &Micro) const;
323 /// getWatchOSVersion - Parse the version number as with getOSVersion. This
324 /// should only be called with WatchOS or generic triples.
325 void getWatchOSVersion(unsigned &Major, unsigned &Minor,
326 unsigned &Micro) const;
329 /// @name Direct Component Access
332 const std::string &str() const { return Data; }
334 const std::string &getTriple() const { return Data; }
336 /// getArchName - Get the architecture (first) component of the
338 StringRef getArchName() const;
340 /// getVendorName - Get the vendor (second) component of the triple.
341 StringRef getVendorName() const;
343 /// getOSName - Get the operating system (third) component of the
345 StringRef getOSName() const;
347 /// getEnvironmentName - Get the optional environment (fourth)
348 /// component of the triple, or "" if empty.
349 StringRef getEnvironmentName() const;
351 /// getOSAndEnvironmentName - Get the operating system and optional
352 /// environment components as a single string (separated by a '-'
353 /// if the environment component is present).
354 StringRef getOSAndEnvironmentName() const;
357 /// @name Convenience Predicates
360 /// Test whether the architecture is 64-bit
362 /// Note that this tests for 64-bit pointer width, and nothing else. Note
363 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
364 /// 16-bit. The inner details of pointer width for particular architectures
365 /// is not summed up in the triple, and so only a coarse grained predicate
366 /// system is provided.
367 bool isArch64Bit() const;
369 /// Test whether the architecture is 32-bit
371 /// Note that this tests for 32-bit pointer width, and nothing else.
372 bool isArch32Bit() const;
374 /// Test whether the architecture is 16-bit
376 /// Note that this tests for 16-bit pointer width, and nothing else.
377 bool isArch16Bit() const;
379 /// isOSVersionLT - Helper function for doing comparisons against version
380 /// numbers included in the target triple.
381 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
382 unsigned Micro = 0) const {
384 getOSVersion(LHS[0], LHS[1], LHS[2]);
387 return LHS[0] < Major;
389 return LHS[1] < Minor;
391 return LHS[1] < Micro;
396 bool isOSVersionLT(const Triple &Other) const {
398 Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
399 return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
402 /// isMacOSXVersionLT - Comparison function for checking OS X version
403 /// compatibility, which handles supporting skewed version numbering schemes
404 /// used by the "darwin" triples.
405 bool isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
406 unsigned Micro = 0) const {
407 assert(isMacOSX() && "Not an OS X triple!");
409 // If this is OS X, expect a sane version number.
410 if (getOS() == Triple::MacOSX)
411 return isOSVersionLT(Major, Minor, Micro);
413 // Otherwise, compare to the "Darwin" number.
414 assert(Major == 10 && "Unexpected major version");
415 return isOSVersionLT(Minor + 4, Micro, 0);
418 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
419 /// "darwin" and "osx" as OS X triples.
420 bool isMacOSX() const {
421 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
424 /// Is this an iOS triple.
425 /// Note: This identifies tvOS as a variant of iOS. If that ever
426 /// changes, i.e., if the two operating systems diverge or their version
427 /// numbers get out of sync, that will need to be changed.
428 /// watchOS has completely different version numbers so it is not included.
430 return getOS() == Triple::IOS || isTvOS();
433 /// Is this an Apple tvOS triple.
434 bool isTvOS() const {
435 return getOS() == Triple::TvOS;
438 /// Is this an Apple watchOS triple.
439 bool isWatchOS() const {
440 return getOS() == Triple::WatchOS;
443 bool isWatchABI() const {
444 return getSubArch() == Triple::ARMSubArch_v7k;
447 /// isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
448 bool isOSDarwin() const {
449 return isMacOSX() || isiOS() || isWatchOS();
452 bool isOSNetBSD() const {
453 return getOS() == Triple::NetBSD;
456 bool isOSOpenBSD() const {
457 return getOS() == Triple::OpenBSD;
460 bool isOSFreeBSD() const {
461 return getOS() == Triple::FreeBSD;
464 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
466 bool isOSSolaris() const {
467 return getOS() == Triple::Solaris;
470 bool isOSBitrig() const {
471 return getOS() == Triple::Bitrig;
474 bool isOSIAMCU() const {
475 return getOS() == Triple::ELFIAMCU;
478 bool isGNUEnvironment() const {
479 EnvironmentType Env = getEnvironment();
480 return Env == Triple::GNU || Env == Triple::GNUABI64 ||
481 Env == Triple::GNUEABI || Env == Triple::GNUEABIHF ||
482 Env == Triple::GNUX32;
485 /// Checks if the environment could be MSVC.
486 bool isWindowsMSVCEnvironment() const {
487 return getOS() == Triple::Win32 &&
488 (getEnvironment() == Triple::UnknownEnvironment ||
489 getEnvironment() == Triple::MSVC);
492 /// Checks if the environment is MSVC.
493 bool isKnownWindowsMSVCEnvironment() const {
494 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
497 bool isWindowsCoreCLREnvironment() const {
498 return getOS() == Triple::Win32 && getEnvironment() == Triple::CoreCLR;
501 bool isWindowsItaniumEnvironment() const {
502 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
505 bool isWindowsCygwinEnvironment() const {
506 return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
509 bool isWindowsGNUEnvironment() const {
510 return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
513 /// Tests for either Cygwin or MinGW OS
514 bool isOSCygMing() const {
515 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
518 /// Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
519 bool isOSMSVCRT() const {
520 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
521 isWindowsItaniumEnvironment();
524 /// Tests whether the OS is Windows.
525 bool isOSWindows() const {
526 return getOS() == Triple::Win32;
529 /// Tests whether the OS is NaCl (Native Client)
530 bool isOSNaCl() const {
531 return getOS() == Triple::NaCl;
534 /// Tests whether the OS is Linux.
535 bool isOSLinux() const {
536 return getOS() == Triple::Linux;
539 /// Tests whether the OS is kFreeBSD.
540 bool isOSKFreeBSD() const {
541 return getOS() == Triple::KFreeBSD;
544 /// Tests whether the OS uses glibc.
545 bool isOSGlibc() const {
546 return getOS() == Triple::Linux || getOS() == Triple::KFreeBSD;
549 /// Tests whether the OS uses the ELF binary format.
550 bool isOSBinFormatELF() const {
551 return getObjectFormat() == Triple::ELF;
554 /// Tests whether the OS uses the COFF binary format.
555 bool isOSBinFormatCOFF() const {
556 return getObjectFormat() == Triple::COFF;
559 /// Tests whether the environment is MachO.
560 bool isOSBinFormatMachO() const {
561 return getObjectFormat() == Triple::MachO;
564 /// Tests whether the target is the PS4 CPU
565 bool isPS4CPU() const {
566 return getArch() == Triple::x86_64 &&
567 getVendor() == Triple::SCEI &&
568 getOS() == Triple::PS4;
571 /// Tests whether the target is the PS4 platform
573 return getVendor() == Triple::SCEI &&
574 getOS() == Triple::PS4;
577 /// Tests whether the target is Android
578 bool isAndroid() const { return getEnvironment() == Triple::Android; }
580 /// Tests whether the environment is musl-libc
581 bool isMusl() const {
582 return getEnvironment() == Triple::Musl ||
583 getEnvironment() == Triple::MuslEABI ||
584 getEnvironment() == Triple::MuslEABIHF;
587 /// Tests whether the target is NVPTX (32- or 64-bit).
588 bool isNVPTX() const {
589 return getArch() == Triple::nvptx || getArch() == Triple::nvptx64;
592 /// Tests wether the target supports comdat
593 bool supportsCOMDAT() const { return !isOSBinFormatMachO(); }
599 /// setArch - Set the architecture (first) component of the triple
601 void setArch(ArchType Kind);
603 /// setVendor - Set the vendor (second) component of the triple to a
605 void setVendor(VendorType Kind);
607 /// setOS - Set the operating system (third) component of the triple
609 void setOS(OSType Kind);
611 /// setEnvironment - Set the environment (fourth) component of the triple
613 void setEnvironment(EnvironmentType Kind);
615 /// setObjectFormat - Set the object file format
616 void setObjectFormat(ObjectFormatType Kind);
618 /// setTriple - Set all components to the new triple \p Str.
619 void setTriple(const Twine &Str);
621 /// setArchName - Set the architecture (first) component of the
623 void setArchName(StringRef Str);
625 /// setVendorName - Set the vendor (second) component of the triple
627 void setVendorName(StringRef Str);
629 /// setOSName - Set the operating system (third) component of the
631 void setOSName(StringRef Str);
633 /// setEnvironmentName - Set the optional environment (fourth)
634 /// component of the triple by name.
635 void setEnvironmentName(StringRef Str);
637 /// setOSAndEnvironmentName - Set the operating system and optional
638 /// environment components with a single string.
639 void setOSAndEnvironmentName(StringRef Str);
642 /// @name Helpers to build variants of a particular triple.
645 /// Form a triple with a 32-bit variant of the current architecture.
647 /// This can be used to move across "families" of architectures where useful.
649 /// \returns A new triple with a 32-bit architecture or an unknown
650 /// architecture if no such variant can be found.
651 llvm::Triple get32BitArchVariant() const;
653 /// Form a triple with a 64-bit variant of the current architecture.
655 /// This can be used to move across "families" of architectures where useful.
657 /// \returns A new triple with a 64-bit architecture or an unknown
658 /// architecture if no such variant can be found.
659 llvm::Triple get64BitArchVariant() const;
661 /// Form a triple with a big endian variant of the current architecture.
663 /// This can be used to move across "families" of architectures where useful.
665 /// \returns A new triple with a big endian architecture or an unknown
666 /// architecture if no such variant can be found.
667 llvm::Triple getBigEndianArchVariant() const;
669 /// Form a triple with a little endian variant of the current architecture.
671 /// This can be used to move across "families" of architectures where useful.
673 /// \returns A new triple with a little endian architecture or an unknown
674 /// architecture if no such variant can be found.
675 llvm::Triple getLittleEndianArchVariant() const;
677 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
679 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
680 /// string then the triple's arch name is used.
681 StringRef getARMCPUForArch(StringRef Arch = StringRef()) const;
683 /// Tests whether the target triple is little endian.
685 /// \returns true if the triple is little endian, false otherwise.
686 bool isLittleEndian() const;
689 /// @name Static helpers for IDs.
692 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
693 static const char *getArchTypeName(ArchType Kind);
695 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
696 /// architecture. This is the prefix used by the architecture specific
697 /// builtins, and is suitable for passing to \see
698 /// Intrinsic::getIntrinsicForGCCBuiltin().
700 /// \return - The architecture prefix, or 0 if none is defined.
701 static const char *getArchTypePrefix(ArchType Kind);
703 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
704 static const char *getVendorTypeName(VendorType Kind);
706 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
707 static const char *getOSTypeName(OSType Kind);
709 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
711 static const char *getEnvironmentTypeName(EnvironmentType Kind);
714 /// @name Static helpers for converting alternate architecture names.
717 /// getArchTypeForLLVMName - The canonical type for the given LLVM
718 /// architecture name (e.g., "x86").
719 static ArchType getArchTypeForLLVMName(StringRef Str);
724 } // End llvm namespace