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 wasm32, // WebAssembly with 32-bit pointers
90 wasm64, // WebAssembly with 64-bit pointers
125 ImaginationTechnologies,
130 LastVendorType = Myriad
151 NaCl, // Native Client
152 CNK, // BG/P Compute-Node Kernel
156 NVCL, // NVIDIA OpenCL
157 AMDHSA, // AMD HSA Runtime
161 WatchOS, // Apple watchOS
164 enum EnvironmentType {
181 LastEnvironmentType = CoreCLR
183 enum ObjectFormatType {
194 /// The parsed arch type.
197 /// The parsed subarchitecture type.
200 /// The parsed vendor type.
203 /// The parsed OS type.
206 /// The parsed Environment type.
207 EnvironmentType Environment;
209 /// The object format type.
210 ObjectFormatType ObjectFormat;
213 /// @name Constructors
216 /// Default constructor is the same as an empty string and leaves all
217 /// triple fields unknown.
218 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
220 explicit Triple(const Twine &Str);
221 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
222 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
223 const Twine &EnvironmentStr);
225 bool operator==(const Triple &Other) const {
226 return Arch == Other.Arch && SubArch == Other.SubArch &&
227 Vendor == Other.Vendor && OS == Other.OS &&
228 Environment == Other.Environment &&
229 ObjectFormat == Other.ObjectFormat;
233 /// @name Normalization
236 /// normalize - Turn an arbitrary machine specification into the canonical
237 /// triple form (or something sensible that the Triple class understands if
238 /// nothing better can reasonably be done). In particular, it handles the
239 /// common case in which otherwise valid components are in the wrong order.
240 static std::string normalize(StringRef Str);
242 /// Return the normalized form of this triple's string.
243 std::string normalize() const { return normalize(Data); }
246 /// @name Typed Component Access
249 /// getArch - Get the parsed architecture type of this triple.
250 ArchType getArch() const { return Arch; }
252 /// getSubArch - get the parsed subarchitecture type for this triple.
253 SubArchType getSubArch() const { return SubArch; }
255 /// getVendor - Get the parsed vendor type of this triple.
256 VendorType getVendor() const { return Vendor; }
258 /// getOS - Get the parsed operating system type of this triple.
259 OSType getOS() const { return OS; }
261 /// hasEnvironment - Does this triple have the optional environment
262 /// (fourth) component?
263 bool hasEnvironment() const {
264 return getEnvironmentName() != "";
267 /// getEnvironment - Get the parsed environment type of this triple.
268 EnvironmentType getEnvironment() const { return Environment; }
270 /// Parse the version number from the OS name component of the
271 /// triple, if present.
273 /// For example, "fooos1.2.3" would return (1, 2, 3).
275 /// If an entry is not defined, it will be returned as 0.
276 void getEnvironmentVersion(unsigned &Major, unsigned &Minor,
277 unsigned &Micro) const;
279 /// getFormat - Get the object format for this triple.
280 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
282 /// getOSVersion - Parse the version number from the OS name component of the
283 /// triple, if present.
285 /// For example, "fooos1.2.3" would return (1, 2, 3).
287 /// If an entry is not defined, it will be returned as 0.
288 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
290 /// getOSMajorVersion - Return just the major version number, this is
291 /// specialized because it is a common query.
292 unsigned getOSMajorVersion() const {
293 unsigned Maj, Min, Micro;
294 getOSVersion(Maj, Min, Micro);
298 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
299 /// translate generic "darwin" versions to the corresponding OS X versions.
300 /// This may also be called with IOS triples but the OS X version number is
301 /// just set to a constant 10.4.0 in that case. Returns true if successful.
302 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
303 unsigned &Micro) const;
305 /// getiOSVersion - Parse the version number as with getOSVersion. This should
306 /// only be called with IOS or generic triples.
307 void getiOSVersion(unsigned &Major, unsigned &Minor,
308 unsigned &Micro) const;
310 /// getWatchOSVersion - Parse the version number as with getOSVersion. This
311 /// should only be called with WatchOS or generic triples.
312 void getWatchOSVersion(unsigned &Major, unsigned &Minor,
313 unsigned &Micro) const;
316 /// @name Direct Component Access
319 const std::string &str() const { return Data; }
321 const std::string &getTriple() const { return Data; }
323 /// getArchName - Get the architecture (first) component of the
325 StringRef getArchName() const;
327 /// getVendorName - Get the vendor (second) component of the triple.
328 StringRef getVendorName() const;
330 /// getOSName - Get the operating system (third) component of the
332 StringRef getOSName() const;
334 /// getEnvironmentName - Get the optional environment (fourth)
335 /// component of the triple, or "" if empty.
336 StringRef getEnvironmentName() const;
338 /// getOSAndEnvironmentName - Get the operating system and optional
339 /// environment components as a single string (separated by a '-'
340 /// if the environment component is present).
341 StringRef getOSAndEnvironmentName() const;
344 /// @name Convenience Predicates
347 /// Test whether the architecture is 64-bit
349 /// Note that this tests for 64-bit pointer width, and nothing else. Note
350 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
351 /// 16-bit. The inner details of pointer width for particular architectures
352 /// is not summed up in the triple, and so only a coarse grained predicate
353 /// system is provided.
354 bool isArch64Bit() const;
356 /// Test whether the architecture is 32-bit
358 /// Note that this tests for 32-bit pointer width, and nothing else.
359 bool isArch32Bit() const;
361 /// Test whether the architecture is 16-bit
363 /// Note that this tests for 16-bit pointer width, and nothing else.
364 bool isArch16Bit() const;
366 /// isOSVersionLT - Helper function for doing comparisons against version
367 /// numbers included in the target triple.
368 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
369 unsigned Micro = 0) const {
371 getOSVersion(LHS[0], LHS[1], LHS[2]);
374 return LHS[0] < Major;
376 return LHS[1] < Minor;
378 return LHS[1] < Micro;
383 bool isOSVersionLT(const Triple &Other) const {
385 Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
386 return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
389 /// isMacOSXVersionLT - Comparison function for checking OS X version
390 /// compatibility, which handles supporting skewed version numbering schemes
391 /// used by the "darwin" triples.
392 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
393 unsigned Micro = 0) const {
394 assert(isMacOSX() && "Not an OS X triple!");
396 // If this is OS X, expect a sane version number.
397 if (getOS() == Triple::MacOSX)
398 return isOSVersionLT(Major, Minor, Micro);
400 // Otherwise, compare to the "Darwin" number.
401 assert(Major == 10 && "Unexpected major version");
402 return isOSVersionLT(Minor + 4, Micro, 0);
405 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
406 /// "darwin" and "osx" as OS X triples.
407 bool isMacOSX() const {
408 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
411 /// Is this an iOS triple.
412 /// Note: This identifies tvOS as a variant of iOS. If that ever
413 /// changes, i.e., if the two operating systems diverge or their version
414 /// numbers get out of sync, that will need to be changed.
415 /// watchOS has completely different version numbers so it is not included.
417 return getOS() == Triple::IOS || isTvOS();
420 /// Is this an Apple tvOS triple.
421 bool isTvOS() const {
422 return getOS() == Triple::TvOS;
425 /// Is this an Apple watchOS triple.
426 bool isWatchOS() const {
427 return getOS() == Triple::WatchOS;
430 /// isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
431 bool isOSDarwin() const {
432 return isMacOSX() || isiOS() || isWatchOS();
435 bool isOSNetBSD() const {
436 return getOS() == Triple::NetBSD;
439 bool isOSOpenBSD() const {
440 return getOS() == Triple::OpenBSD;
443 bool isOSFreeBSD() const {
444 return getOS() == Triple::FreeBSD;
447 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
449 bool isOSSolaris() const {
450 return getOS() == Triple::Solaris;
453 bool isOSBitrig() const {
454 return getOS() == Triple::Bitrig;
457 bool isOSIAMCU() const {
458 return getOS() == Triple::ELFIAMCU;
461 /// Checks if the environment could be MSVC.
462 bool isWindowsMSVCEnvironment() const {
463 return getOS() == Triple::Win32 &&
464 (getEnvironment() == Triple::UnknownEnvironment ||
465 getEnvironment() == Triple::MSVC);
468 /// Checks if the environment is MSVC.
469 bool isKnownWindowsMSVCEnvironment() const {
470 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
473 bool isWindowsCoreCLREnvironment() const {
474 return getOS() == Triple::Win32 && getEnvironment() == Triple::CoreCLR;
477 bool isWindowsItaniumEnvironment() const {
478 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
481 bool isWindowsCygwinEnvironment() const {
482 return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
485 bool isWindowsGNUEnvironment() const {
486 return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
489 /// Tests for either Cygwin or MinGW OS
490 bool isOSCygMing() const {
491 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
494 /// Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
495 bool isOSMSVCRT() const {
496 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
497 isWindowsItaniumEnvironment();
500 /// Tests whether the OS is Windows.
501 bool isOSWindows() const {
502 return getOS() == Triple::Win32;
505 /// Tests whether the OS is NaCl (Native Client)
506 bool isOSNaCl() const {
507 return getOS() == Triple::NaCl;
510 /// Tests whether the OS is Linux.
511 bool isOSLinux() const {
512 return getOS() == Triple::Linux;
515 /// Tests whether the OS uses the ELF binary format.
516 bool isOSBinFormatELF() const {
517 return getObjectFormat() == Triple::ELF;
520 /// Tests whether the OS uses the COFF binary format.
521 bool isOSBinFormatCOFF() const {
522 return getObjectFormat() == Triple::COFF;
525 /// Tests whether the environment is MachO.
526 bool isOSBinFormatMachO() const {
527 return getObjectFormat() == Triple::MachO;
530 /// Tests whether the target is the PS4 CPU
531 bool isPS4CPU() const {
532 return getArch() == Triple::x86_64 &&
533 getVendor() == Triple::SCEI &&
534 getOS() == Triple::PS4;
537 /// Tests whether the target is the PS4 platform
539 return getVendor() == Triple::SCEI &&
540 getOS() == Triple::PS4;
543 /// Tests whether the target is Android
544 bool isAndroid() const { return getEnvironment() == Triple::Android; }
550 /// setArch - Set the architecture (first) component of the triple
552 void setArch(ArchType Kind);
554 /// setVendor - Set the vendor (second) component of the triple to a
556 void setVendor(VendorType Kind);
558 /// setOS - Set the operating system (third) component of the triple
560 void setOS(OSType Kind);
562 /// setEnvironment - Set the environment (fourth) component of the triple
564 void setEnvironment(EnvironmentType Kind);
566 /// setObjectFormat - Set the object file format
567 void setObjectFormat(ObjectFormatType Kind);
569 /// setTriple - Set all components to the new triple \p Str.
570 void setTriple(const Twine &Str);
572 /// setArchName - Set the architecture (first) component of the
574 void setArchName(StringRef Str);
576 /// setVendorName - Set the vendor (second) component of the triple
578 void setVendorName(StringRef Str);
580 /// setOSName - Set the operating system (third) component of the
582 void setOSName(StringRef Str);
584 /// setEnvironmentName - Set the optional environment (fourth)
585 /// component of the triple by name.
586 void setEnvironmentName(StringRef Str);
588 /// setOSAndEnvironmentName - Set the operating system and optional
589 /// environment components with a single string.
590 void setOSAndEnvironmentName(StringRef Str);
593 /// @name Helpers to build variants of a particular triple.
596 /// Form a triple with a 32-bit variant of the current architecture.
598 /// This can be used to move across "families" of architectures where useful.
600 /// \returns A new triple with a 32-bit architecture or an unknown
601 /// architecture if no such variant can be found.
602 llvm::Triple get32BitArchVariant() const;
604 /// Form a triple with a 64-bit variant of the current architecture.
606 /// This can be used to move across "families" of architectures where useful.
608 /// \returns A new triple with a 64-bit architecture or an unknown
609 /// architecture if no such variant can be found.
610 llvm::Triple get64BitArchVariant() const;
612 /// Form a triple with a big endian variant of the current architecture.
614 /// This can be used to move across "families" of architectures where useful.
616 /// \returns A new triple with a big endian architecture or an unknown
617 /// architecture if no such variant can be found.
618 llvm::Triple getBigEndianArchVariant() const;
620 /// Form a triple with a little endian variant of the current architecture.
622 /// This can be used to move across "families" of architectures where useful.
624 /// \returns A new triple with a little endian architecture or an unknown
625 /// architecture if no such variant can be found.
626 llvm::Triple getLittleEndianArchVariant() const;
628 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
630 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
631 /// string then the triple's arch name is used.
632 StringRef getARMCPUForArch(StringRef Arch = StringRef()) const;
635 /// @name Static helpers for IDs.
638 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
639 static const char *getArchTypeName(ArchType Kind);
641 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
642 /// architecture. This is the prefix used by the architecture specific
643 /// builtins, and is suitable for passing to \see
644 /// Intrinsic::getIntrinsicForGCCBuiltin().
646 /// \return - The architecture prefix, or 0 if none is defined.
647 static const char *getArchTypePrefix(ArchType Kind);
649 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
650 static const char *getVendorTypeName(VendorType Kind);
652 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
653 static const char *getOSTypeName(OSType Kind);
655 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
657 static const char *getEnvironmentTypeName(EnvironmentType Kind);
660 /// @name Static helpers for converting alternate architecture names.
663 /// getArchTypeForLLVMName - The canonical type for the given LLVM
664 /// architecture name (e.g., "x86").
665 static ArchType getArchTypeForLLVMName(StringRef Str);
670 } // End llvm namespace