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 bpfel, // eBPF or extended BPF or 64-bit BPF (little endian)
54 bpfeb, // eBPF or extended BPF or 64-bit BPF (big endian)
55 hexagon, // Hexagon: hexagon
56 mips, // MIPS: mips, mipsallegrex
57 mipsel, // MIPSEL: mipsel, mipsallegrexel
58 mips64, // MIPS64: mips64
59 mips64el, // MIPS64EL: mips64el
60 msp430, // MSP430: msp430
62 ppc64, // PPC64: powerpc64, ppu
63 ppc64le, // PPC64LE: powerpc64le
64 r600, // R600: AMD GPUs HD2XXX - HD6XXX
65 amdgcn, // AMDGCN: AMD GCN GPUs
66 sparc, // Sparc: sparc
67 sparcv9, // Sparcv9: Sparcv9
68 sparcel, // Sparc: (endianness = little). NB: 'Sparcle' is a CPU variant
69 systemz, // SystemZ: s390x
70 tce, // TCE (http://tce.cs.tut.fi/): tce
71 thumb, // Thumb (little endian): thumb, thumbv.*
72 thumbeb, // Thumb (big endian): thumbeb
74 x86_64, // X86-64: amd64, x86_64
75 xcore, // XCore: xcore
76 nvptx, // NVPTX: 32-bit
77 nvptx64, // NVPTX: 64-bit
78 le32, // le32: generic little-endian 32-bit CPU (PNaCl)
79 le64, // le64: generic little-endian 64-bit CPU (PNaCl)
81 amdil64, // AMDIL with 64-bit pointers
83 hsail64, // AMD HSAIL with 64-bit pointers
84 spir, // SPIR: standard portable IR for OpenCL 32-bit version
85 spir64, // SPIR: standard portable IR for OpenCL 64-bit version
86 kalimba, // Kalimba: generic kalimba
87 shave, // SHAVE: Movidius vector VLIW processors
88 wasm32, // WebAssembly with 32-bit pointers
89 wasm64, // WebAssembly with 64-bit pointers
123 ImaginationTechnologies,
128 LastVendorType = Myriad
149 NaCl, // Native Client
150 CNK, // BG/P Compute-Node Kernel
154 NVCL, // NVIDIA OpenCL
155 AMDHSA, // AMD HSA Runtime
159 enum EnvironmentType {
176 LastEnvironmentType = CoreCLR
178 enum ObjectFormatType {
189 /// The parsed arch type.
192 /// The parsed subarchitecture type.
195 /// The parsed vendor type.
198 /// The parsed OS type.
201 /// The parsed Environment type.
202 EnvironmentType Environment;
204 /// The object format type.
205 ObjectFormatType ObjectFormat;
208 /// @name Constructors
211 /// \brief Default constructor is the same as an empty string and leaves all
212 /// triple fields unknown.
213 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
215 explicit Triple(const Twine &Str);
216 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
217 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
218 const Twine &EnvironmentStr);
220 bool operator==(const Triple &Other) const {
221 return Arch == Other.Arch && SubArch == Other.SubArch &&
222 Vendor == Other.Vendor && OS == Other.OS &&
223 Environment == Other.Environment &&
224 ObjectFormat == Other.ObjectFormat;
228 /// @name Normalization
231 /// normalize - Turn an arbitrary machine specification into the canonical
232 /// triple form (or something sensible that the Triple class understands if
233 /// nothing better can reasonably be done). In particular, it handles the
234 /// common case in which otherwise valid components are in the wrong order.
235 static std::string normalize(StringRef Str);
237 /// \brief Return the normalized form of this triple's string.
238 std::string normalize() const { return normalize(Data); }
241 /// @name Typed Component Access
244 /// getArch - Get the parsed architecture type of this triple.
245 ArchType getArch() const { return Arch; }
247 /// getSubArch - get the parsed subarchitecture type for this triple.
248 SubArchType getSubArch() const { return SubArch; }
250 /// getVendor - Get the parsed vendor type of this triple.
251 VendorType getVendor() const { return Vendor; }
253 /// getOS - Get the parsed operating system type of this triple.
254 OSType getOS() const { return OS; }
256 /// hasEnvironment - Does this triple have the optional environment
257 /// (fourth) component?
258 bool hasEnvironment() const {
259 return getEnvironmentName() != "";
262 /// getEnvironment - Get the parsed environment type of this triple.
263 EnvironmentType getEnvironment() const { return Environment; }
265 /// \brief Parse the version number from the OS name component of the
266 /// triple, if present.
268 /// For example, "fooos1.2.3" would return (1, 2, 3).
270 /// If an entry is not defined, it will be returned as 0.
271 void getEnvironmentVersion(unsigned &Major, unsigned &Minor,
272 unsigned &Micro) const;
274 /// getFormat - Get the object format for this triple.
275 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
277 /// getOSVersion - Parse the version number from the OS name component of the
278 /// triple, if present.
280 /// For example, "fooos1.2.3" would return (1, 2, 3).
282 /// If an entry is not defined, it will be returned as 0.
283 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
285 /// getOSMajorVersion - Return just the major version number, this is
286 /// specialized because it is a common query.
287 unsigned getOSMajorVersion() const {
288 unsigned Maj, Min, Micro;
289 getOSVersion(Maj, Min, Micro);
293 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
294 /// translate generic "darwin" versions to the corresponding OS X versions.
295 /// This may also be called with IOS triples but the OS X version number is
296 /// just set to a constant 10.4.0 in that case. Returns true if successful.
297 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
298 unsigned &Micro) const;
300 /// getiOSVersion - Parse the version number as with getOSVersion. This should
301 /// only be called with IOS triples.
302 void getiOSVersion(unsigned &Major, unsigned &Minor,
303 unsigned &Micro) const;
306 /// @name Direct Component Access
309 const std::string &str() const { return Data; }
311 const std::string &getTriple() const { return Data; }
313 /// getArchName - Get the architecture (first) component of the
315 StringRef getArchName() const;
317 /// getVendorName - Get the vendor (second) component of the triple.
318 StringRef getVendorName() const;
320 /// getOSName - Get the operating system (third) component of the
322 StringRef getOSName() const;
324 /// getEnvironmentName - Get the optional environment (fourth)
325 /// component of the triple, or "" if empty.
326 StringRef getEnvironmentName() const;
328 /// getOSAndEnvironmentName - Get the operating system and optional
329 /// environment components as a single string (separated by a '-'
330 /// if the environment component is present).
331 StringRef getOSAndEnvironmentName() const;
334 /// @name Convenience Predicates
337 /// \brief Test whether the architecture is 64-bit
339 /// Note that this tests for 64-bit pointer width, and nothing else. Note
340 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
341 /// 16-bit. The inner details of pointer width for particular architectures
342 /// is not summed up in the triple, and so only a coarse grained predicate
343 /// system is provided.
344 bool isArch64Bit() const;
346 /// \brief Test whether the architecture is 32-bit
348 /// Note that this tests for 32-bit pointer width, and nothing else.
349 bool isArch32Bit() const;
351 /// \brief Test whether the architecture is 16-bit
353 /// Note that this tests for 16-bit pointer width, and nothing else.
354 bool isArch16Bit() const;
356 /// isOSVersionLT - Helper function for doing comparisons against version
357 /// numbers included in the target triple.
358 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
359 unsigned Micro = 0) const {
361 getOSVersion(LHS[0], LHS[1], LHS[2]);
364 return LHS[0] < Major;
366 return LHS[1] < Minor;
368 return LHS[1] < Micro;
373 bool isOSVersionLT(const Triple &Other) const {
375 Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
376 return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
379 /// isMacOSXVersionLT - Comparison function for checking OS X version
380 /// compatibility, which handles supporting skewed version numbering schemes
381 /// used by the "darwin" triples.
382 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
383 unsigned Micro = 0) const {
384 assert(isMacOSX() && "Not an OS X triple!");
386 // If this is OS X, expect a sane version number.
387 if (getOS() == Triple::MacOSX)
388 return isOSVersionLT(Major, Minor, Micro);
390 // Otherwise, compare to the "Darwin" number.
391 assert(Major == 10 && "Unexpected major version");
392 return isOSVersionLT(Minor + 4, Micro, 0);
395 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
396 /// "darwin" and "osx" as OS X triples.
397 bool isMacOSX() const {
398 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
401 /// Is this an iOS triple.
403 return getOS() == Triple::IOS;
406 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
407 bool isOSDarwin() const {
408 return isMacOSX() || isiOS();
411 bool isOSNetBSD() const {
412 return getOS() == Triple::NetBSD;
415 bool isOSOpenBSD() const {
416 return getOS() == Triple::OpenBSD;
419 bool isOSFreeBSD() const {
420 return getOS() == Triple::FreeBSD;
423 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
425 bool isOSSolaris() const {
426 return getOS() == Triple::Solaris;
429 bool isOSBitrig() const {
430 return getOS() == Triple::Bitrig;
433 bool isWindowsMSVCEnvironment() const {
434 return getOS() == Triple::Win32 &&
435 (getEnvironment() == Triple::UnknownEnvironment ||
436 getEnvironment() == Triple::MSVC);
439 bool isKnownWindowsMSVCEnvironment() const {
440 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
443 bool isWindowsCoreCLREnvironment() const {
444 return getOS() == Triple::Win32 && getEnvironment() == Triple::CoreCLR;
447 bool isWindowsItaniumEnvironment() const {
448 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
451 bool isWindowsCygwinEnvironment() const {
452 return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
455 bool isWindowsGNUEnvironment() const {
456 return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
459 /// \brief Tests for either Cygwin or MinGW OS
460 bool isOSCygMing() const {
461 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
464 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
465 bool isOSMSVCRT() const {
466 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
467 isWindowsItaniumEnvironment();
470 /// \brief Tests whether the OS is Windows.
471 bool isOSWindows() const {
472 return getOS() == Triple::Win32;
475 /// \brief Tests whether the OS is NaCl (Native Client)
476 bool isOSNaCl() const {
477 return getOS() == Triple::NaCl;
480 /// \brief Tests whether the OS is Linux.
481 bool isOSLinux() const {
482 return getOS() == Triple::Linux;
485 /// \brief Tests whether the OS uses the ELF binary format.
486 bool isOSBinFormatELF() const {
487 return getObjectFormat() == Triple::ELF;
490 /// \brief Tests whether the OS uses the COFF binary format.
491 bool isOSBinFormatCOFF() const {
492 return getObjectFormat() == Triple::COFF;
495 /// \brief Tests whether the environment is MachO.
496 bool isOSBinFormatMachO() const {
497 return getObjectFormat() == Triple::MachO;
500 /// \brief Tests whether the target is the PS4 CPU
501 bool isPS4CPU() const {
502 return getArch() == Triple::x86_64 &&
503 getVendor() == Triple::SCEI &&
504 getOS() == Triple::PS4;
507 /// \brief Tests whether the target is the PS4 platform
509 return getVendor() == Triple::SCEI &&
510 getOS() == Triple::PS4;
517 /// setArch - Set the architecture (first) component of the triple
519 void setArch(ArchType Kind);
521 /// setVendor - Set the vendor (second) component of the triple to a
523 void setVendor(VendorType Kind);
525 /// setOS - Set the operating system (third) component of the triple
527 void setOS(OSType Kind);
529 /// setEnvironment - Set the environment (fourth) component of the triple
531 void setEnvironment(EnvironmentType Kind);
533 /// setObjectFormat - Set the object file format
534 void setObjectFormat(ObjectFormatType Kind);
536 /// setTriple - Set all components to the new triple \p Str.
537 void setTriple(const Twine &Str);
539 /// setArchName - Set the architecture (first) component of the
541 void setArchName(StringRef Str);
543 /// setVendorName - Set the vendor (second) component of the triple
545 void setVendorName(StringRef Str);
547 /// setOSName - Set the operating system (third) component of the
549 void setOSName(StringRef Str);
551 /// setEnvironmentName - Set the optional environment (fourth)
552 /// component of the triple by name.
553 void setEnvironmentName(StringRef Str);
555 /// setOSAndEnvironmentName - Set the operating system and optional
556 /// environment components with a single string.
557 void setOSAndEnvironmentName(StringRef Str);
560 /// @name Helpers to build variants of a particular triple.
563 /// \brief Form a triple with a 32-bit variant of the current architecture.
565 /// This can be used to move across "families" of architectures where useful.
567 /// \returns A new triple with a 32-bit architecture or an unknown
568 /// architecture if no such variant can be found.
569 llvm::Triple get32BitArchVariant() const;
571 /// \brief Form a triple with a 64-bit variant of the current architecture.
573 /// This can be used to move across "families" of architectures where useful.
575 /// \returns A new triple with a 64-bit architecture or an unknown
576 /// architecture if no such variant can be found.
577 llvm::Triple get64BitArchVariant() const;
579 /// Form a triple with a big endian variant of the current architecture.
581 /// This can be used to move across "families" of architectures where useful.
583 /// \returns A new triple with a big endian architecture or an unknown
584 /// architecture if no such variant can be found.
585 llvm::Triple getBigEndianArchVariant() const;
587 /// Form a triple with a little endian variant of the current architecture.
589 /// This can be used to move across "families" of architectures where useful.
591 /// \returns A new triple with a little endian architecture or an unknown
592 /// architecture if no such variant can be found.
593 llvm::Triple getLittleEndianArchVariant() const;
595 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
597 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
598 /// string then the triple's arch name is used.
599 StringRef getARMCPUForArch(StringRef Arch = StringRef()) const;
602 /// @name Static helpers for IDs.
605 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
606 static const char *getArchTypeName(ArchType Kind);
608 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
609 /// architecture. This is the prefix used by the architecture specific
610 /// builtins, and is suitable for passing to \see
611 /// Intrinsic::getIntrinsicForGCCBuiltin().
613 /// \return - The architecture prefix, or 0 if none is defined.
614 static const char *getArchTypePrefix(ArchType Kind);
616 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
617 static const char *getVendorTypeName(VendorType Kind);
619 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
620 static const char *getOSTypeName(OSType Kind);
622 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
624 static const char *getEnvironmentTypeName(EnvironmentType Kind);
627 /// @name Static helpers for converting alternate architecture names.
630 /// getArchTypeForLLVMName - The canonical type for the given LLVM
631 /// architecture name (e.g., "x86").
632 static ArchType getArchTypeForLLVMName(StringRef Str);
637 } // End llvm namespace