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
124 ImaginationTechnologies,
129 LastVendorType = Myriad
150 NaCl, // Native Client
151 CNK, // BG/P Compute-Node Kernel
155 NVCL, // NVIDIA OpenCL
156 AMDHSA, // AMD HSA Runtime
159 LastOSType = ELFIAMCU
161 enum EnvironmentType {
178 LastEnvironmentType = CoreCLR
180 enum ObjectFormatType {
191 /// The parsed arch type.
194 /// The parsed subarchitecture type.
197 /// The parsed vendor type.
200 /// The parsed OS type.
203 /// The parsed Environment type.
204 EnvironmentType Environment;
206 /// The object format type.
207 ObjectFormatType ObjectFormat;
210 /// @name Constructors
213 /// \brief Default constructor is the same as an empty string and leaves all
214 /// triple fields unknown.
215 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
217 explicit Triple(const Twine &Str);
218 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
219 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
220 const Twine &EnvironmentStr);
222 bool operator==(const Triple &Other) const {
223 return Arch == Other.Arch && SubArch == Other.SubArch &&
224 Vendor == Other.Vendor && OS == Other.OS &&
225 Environment == Other.Environment &&
226 ObjectFormat == Other.ObjectFormat;
230 /// @name Normalization
233 /// normalize - Turn an arbitrary machine specification into the canonical
234 /// triple form (or something sensible that the Triple class understands if
235 /// nothing better can reasonably be done). In particular, it handles the
236 /// common case in which otherwise valid components are in the wrong order.
237 static std::string normalize(StringRef Str);
239 /// \brief Return the normalized form of this triple's string.
240 std::string normalize() const { return normalize(Data); }
243 /// @name Typed Component Access
246 /// getArch - Get the parsed architecture type of this triple.
247 ArchType getArch() const { return Arch; }
249 /// getSubArch - get the parsed subarchitecture type for this triple.
250 SubArchType getSubArch() const { return SubArch; }
252 /// getVendor - Get the parsed vendor type of this triple.
253 VendorType getVendor() const { return Vendor; }
255 /// getOS - Get the parsed operating system type of this triple.
256 OSType getOS() const { return OS; }
258 /// hasEnvironment - Does this triple have the optional environment
259 /// (fourth) component?
260 bool hasEnvironment() const {
261 return getEnvironmentName() != "";
264 /// getEnvironment - Get the parsed environment type of this triple.
265 EnvironmentType getEnvironment() const { return Environment; }
267 /// \brief Parse the version number from the OS name component of the
268 /// triple, if present.
270 /// For example, "fooos1.2.3" would return (1, 2, 3).
272 /// If an entry is not defined, it will be returned as 0.
273 void getEnvironmentVersion(unsigned &Major, unsigned &Minor,
274 unsigned &Micro) const;
276 /// getFormat - Get the object format for this triple.
277 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
279 /// getOSVersion - Parse the version number from the OS name component of the
280 /// triple, if present.
282 /// For example, "fooos1.2.3" would return (1, 2, 3).
284 /// If an entry is not defined, it will be returned as 0.
285 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
287 /// getOSMajorVersion - Return just the major version number, this is
288 /// specialized because it is a common query.
289 unsigned getOSMajorVersion() const {
290 unsigned Maj, Min, Micro;
291 getOSVersion(Maj, Min, Micro);
295 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
296 /// translate generic "darwin" versions to the corresponding OS X versions.
297 /// This may also be called with IOS triples but the OS X version number is
298 /// just set to a constant 10.4.0 in that case. Returns true if successful.
299 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
300 unsigned &Micro) const;
302 /// getiOSVersion - Parse the version number as with getOSVersion. This should
303 /// only be called with IOS triples.
304 void getiOSVersion(unsigned &Major, unsigned &Minor,
305 unsigned &Micro) const;
308 /// @name Direct Component Access
311 const std::string &str() const { return Data; }
313 const std::string &getTriple() const { return Data; }
315 /// getArchName - Get the architecture (first) component of the
317 StringRef getArchName() const;
319 /// getVendorName - Get the vendor (second) component of the triple.
320 StringRef getVendorName() const;
322 /// getOSName - Get the operating system (third) component of the
324 StringRef getOSName() const;
326 /// getEnvironmentName - Get the optional environment (fourth)
327 /// component of the triple, or "" if empty.
328 StringRef getEnvironmentName() const;
330 /// getOSAndEnvironmentName - Get the operating system and optional
331 /// environment components as a single string (separated by a '-'
332 /// if the environment component is present).
333 StringRef getOSAndEnvironmentName() const;
336 /// @name Convenience Predicates
339 /// \brief Test whether the architecture is 64-bit
341 /// Note that this tests for 64-bit pointer width, and nothing else. Note
342 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
343 /// 16-bit. The inner details of pointer width for particular architectures
344 /// is not summed up in the triple, and so only a coarse grained predicate
345 /// system is provided.
346 bool isArch64Bit() const;
348 /// \brief Test whether the architecture is 32-bit
350 /// Note that this tests for 32-bit pointer width, and nothing else.
351 bool isArch32Bit() const;
353 /// \brief Test whether the architecture is 16-bit
355 /// Note that this tests for 16-bit pointer width, and nothing else.
356 bool isArch16Bit() const;
358 /// isOSVersionLT - Helper function for doing comparisons against version
359 /// numbers included in the target triple.
360 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
361 unsigned Micro = 0) const {
363 getOSVersion(LHS[0], LHS[1], LHS[2]);
366 return LHS[0] < Major;
368 return LHS[1] < Minor;
370 return LHS[1] < Micro;
375 bool isOSVersionLT(const Triple &Other) const {
377 Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
378 return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
381 /// isMacOSXVersionLT - Comparison function for checking OS X version
382 /// compatibility, which handles supporting skewed version numbering schemes
383 /// used by the "darwin" triples.
384 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
385 unsigned Micro = 0) const {
386 assert(isMacOSX() && "Not an OS X triple!");
388 // If this is OS X, expect a sane version number.
389 if (getOS() == Triple::MacOSX)
390 return isOSVersionLT(Major, Minor, Micro);
392 // Otherwise, compare to the "Darwin" number.
393 assert(Major == 10 && "Unexpected major version");
394 return isOSVersionLT(Minor + 4, Micro, 0);
397 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
398 /// "darwin" and "osx" as OS X triples.
399 bool isMacOSX() const {
400 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
403 /// Is this an iOS triple.
405 return getOS() == Triple::IOS;
408 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
409 bool isOSDarwin() const {
410 return isMacOSX() || isiOS();
413 bool isOSNetBSD() const {
414 return getOS() == Triple::NetBSD;
417 bool isOSOpenBSD() const {
418 return getOS() == Triple::OpenBSD;
421 bool isOSFreeBSD() const {
422 return getOS() == Triple::FreeBSD;
425 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
427 bool isOSSolaris() const {
428 return getOS() == Triple::Solaris;
431 bool isOSBitrig() const {
432 return getOS() == Triple::Bitrig;
435 bool isOSIAMCU() const {
436 return getOS() == Triple::ELFIAMCU;
439 bool isWindowsMSVCEnvironment() const {
440 return getOS() == Triple::Win32 &&
441 (getEnvironment() == Triple::UnknownEnvironment ||
442 getEnvironment() == Triple::MSVC);
445 bool isKnownWindowsMSVCEnvironment() const {
446 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
449 bool isWindowsCoreCLREnvironment() const {
450 return getOS() == Triple::Win32 && getEnvironment() == Triple::CoreCLR;
453 bool isWindowsItaniumEnvironment() const {
454 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
457 bool isWindowsCygwinEnvironment() const {
458 return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
461 bool isWindowsGNUEnvironment() const {
462 return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
465 /// \brief Tests for either Cygwin or MinGW OS
466 bool isOSCygMing() const {
467 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
470 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
471 bool isOSMSVCRT() const {
472 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
473 isWindowsItaniumEnvironment();
476 /// \brief Tests whether the OS is Windows.
477 bool isOSWindows() const {
478 return getOS() == Triple::Win32;
481 /// \brief Tests whether the OS is NaCl (Native Client)
482 bool isOSNaCl() const {
483 return getOS() == Triple::NaCl;
486 /// \brief Tests whether the OS is Linux.
487 bool isOSLinux() const {
488 return getOS() == Triple::Linux;
491 /// \brief Tests whether the OS uses the ELF binary format.
492 bool isOSBinFormatELF() const {
493 return getObjectFormat() == Triple::ELF;
496 /// \brief Tests whether the OS uses the COFF binary format.
497 bool isOSBinFormatCOFF() const {
498 return getObjectFormat() == Triple::COFF;
501 /// \brief Tests whether the environment is MachO.
502 bool isOSBinFormatMachO() const {
503 return getObjectFormat() == Triple::MachO;
506 /// \brief Tests whether the target is the PS4 CPU
507 bool isPS4CPU() const {
508 return getArch() == Triple::x86_64 &&
509 getVendor() == Triple::SCEI &&
510 getOS() == Triple::PS4;
513 /// \brief Tests whether the target is the PS4 platform
515 return getVendor() == Triple::SCEI &&
516 getOS() == Triple::PS4;
519 /// \brief Tests whether the target is Android
520 bool isAndroid() const { return getEnvironment() == Triple::Android; }
526 /// setArch - Set the architecture (first) component of the triple
528 void setArch(ArchType Kind);
530 /// setVendor - Set the vendor (second) component of the triple to a
532 void setVendor(VendorType Kind);
534 /// setOS - Set the operating system (third) component of the triple
536 void setOS(OSType Kind);
538 /// setEnvironment - Set the environment (fourth) component of the triple
540 void setEnvironment(EnvironmentType Kind);
542 /// setObjectFormat - Set the object file format
543 void setObjectFormat(ObjectFormatType Kind);
545 /// setTriple - Set all components to the new triple \p Str.
546 void setTriple(const Twine &Str);
548 /// setArchName - Set the architecture (first) component of the
550 void setArchName(StringRef Str);
552 /// setVendorName - Set the vendor (second) component of the triple
554 void setVendorName(StringRef Str);
556 /// setOSName - Set the operating system (third) component of the
558 void setOSName(StringRef Str);
560 /// setEnvironmentName - Set the optional environment (fourth)
561 /// component of the triple by name.
562 void setEnvironmentName(StringRef Str);
564 /// setOSAndEnvironmentName - Set the operating system and optional
565 /// environment components with a single string.
566 void setOSAndEnvironmentName(StringRef Str);
569 /// @name Helpers to build variants of a particular triple.
572 /// \brief Form a triple with a 32-bit variant of the current architecture.
574 /// This can be used to move across "families" of architectures where useful.
576 /// \returns A new triple with a 32-bit architecture or an unknown
577 /// architecture if no such variant can be found.
578 llvm::Triple get32BitArchVariant() const;
580 /// \brief Form a triple with a 64-bit variant of the current architecture.
582 /// This can be used to move across "families" of architectures where useful.
584 /// \returns A new triple with a 64-bit architecture or an unknown
585 /// architecture if no such variant can be found.
586 llvm::Triple get64BitArchVariant() const;
588 /// Form a triple with a big endian variant of the current architecture.
590 /// This can be used to move across "families" of architectures where useful.
592 /// \returns A new triple with a big endian architecture or an unknown
593 /// architecture if no such variant can be found.
594 llvm::Triple getBigEndianArchVariant() const;
596 /// Form a triple with a little endian 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 little endian architecture or an unknown
601 /// architecture if no such variant can be found.
602 llvm::Triple getLittleEndianArchVariant() const;
604 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
606 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
607 /// string then the triple's arch name is used.
608 StringRef getARMCPUForArch(StringRef Arch = StringRef()) const;
611 /// @name Static helpers for IDs.
614 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
615 static const char *getArchTypeName(ArchType Kind);
617 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
618 /// architecture. This is the prefix used by the architecture specific
619 /// builtins, and is suitable for passing to \see
620 /// Intrinsic::getIntrinsicForGCCBuiltin().
622 /// \return - The architecture prefix, or 0 if none is defined.
623 static const char *getArchTypePrefix(ArchType Kind);
625 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
626 static const char *getVendorTypeName(VendorType Kind);
628 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
629 static const char *getOSTypeName(OSType Kind);
631 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
633 static const char *getEnvironmentTypeName(EnvironmentType Kind);
636 /// @name Static helpers for converting alternate architecture names.
639 /// getArchTypeForLLVMName - The canonical type for the given LLVM
640 /// architecture name (e.g., "x86").
641 static ArchType getArchTypeForLLVMName(StringRef Str);
646 } // End llvm namespace