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,
148 NaCl, // Native Client
149 CNK, // BG/P Compute-Node Kernel
153 NVCL, // NVIDIA OpenCL
154 AMDHSA, // AMD HSA Runtime
158 enum EnvironmentType {
175 LastEnvironmentType = CoreCLR
177 enum ObjectFormatType {
188 /// The parsed arch type.
191 /// The parsed subarchitecture type.
194 /// The parsed vendor type.
197 /// The parsed OS type.
200 /// The parsed Environment type.
201 EnvironmentType Environment;
203 /// The object format type.
204 ObjectFormatType ObjectFormat;
207 /// @name Constructors
210 /// \brief Default constructor is the same as an empty string and leaves all
211 /// triple fields unknown.
212 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
214 explicit Triple(const Twine &Str);
215 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
216 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
217 const Twine &EnvironmentStr);
219 bool operator==(const Triple &Other) const {
220 return Arch == Other.Arch && SubArch == Other.SubArch &&
221 Vendor == Other.Vendor && OS == Other.OS &&
222 Environment == Other.Environment &&
223 ObjectFormat == Other.ObjectFormat;
227 /// @name Normalization
230 /// normalize - Turn an arbitrary machine specification into the canonical
231 /// triple form (or something sensible that the Triple class understands if
232 /// nothing better can reasonably be done). In particular, it handles the
233 /// common case in which otherwise valid components are in the wrong order.
234 static std::string normalize(StringRef Str);
236 /// \brief Return the normalized form of this triple's string.
237 std::string normalize() const { return normalize(Data); }
240 /// @name Typed Component Access
243 /// getArch - Get the parsed architecture type of this triple.
244 ArchType getArch() const { return Arch; }
246 /// getSubArch - get the parsed subarchitecture type for this triple.
247 SubArchType getSubArch() const { return SubArch; }
249 /// getVendor - Get the parsed vendor type of this triple.
250 VendorType getVendor() const { return Vendor; }
252 /// getOS - Get the parsed operating system type of this triple.
253 OSType getOS() const { return OS; }
255 /// hasEnvironment - Does this triple have the optional environment
256 /// (fourth) component?
257 bool hasEnvironment() const {
258 return getEnvironmentName() != "";
261 /// getEnvironment - Get the parsed environment type of this triple.
262 EnvironmentType getEnvironment() const { return Environment; }
264 /// \brief Parse the version number from the OS name component of the
265 /// triple, if present.
267 /// For example, "fooos1.2.3" would return (1, 2, 3).
269 /// If an entry is not defined, it will be returned as 0.
270 void getEnvironmentVersion(unsigned &Major, unsigned &Minor,
271 unsigned &Micro) const;
273 /// getFormat - Get the object format for this triple.
274 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
276 /// getOSVersion - Parse the version number from the OS name component of the
277 /// triple, if present.
279 /// For example, "fooos1.2.3" would return (1, 2, 3).
281 /// If an entry is not defined, it will be returned as 0.
282 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
284 /// getOSMajorVersion - Return just the major version number, this is
285 /// specialized because it is a common query.
286 unsigned getOSMajorVersion() const {
287 unsigned Maj, Min, Micro;
288 getOSVersion(Maj, Min, Micro);
292 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
293 /// translate generic "darwin" versions to the corresponding OS X versions.
294 /// This may also be called with IOS triples but the OS X version number is
295 /// just set to a constant 10.4.0 in that case. Returns true if successful.
296 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
297 unsigned &Micro) const;
299 /// getiOSVersion - Parse the version number as with getOSVersion. This should
300 /// only be called with IOS triples.
301 void getiOSVersion(unsigned &Major, unsigned &Minor,
302 unsigned &Micro) const;
305 /// @name Direct Component Access
308 const std::string &str() const { return Data; }
310 const std::string &getTriple() const { return Data; }
312 /// getArchName - Get the architecture (first) component of the
314 StringRef getArchName() const;
316 /// getVendorName - Get the vendor (second) component of the triple.
317 StringRef getVendorName() const;
319 /// getOSName - Get the operating system (third) component of the
321 StringRef getOSName() const;
323 /// getEnvironmentName - Get the optional environment (fourth)
324 /// component of the triple, or "" if empty.
325 StringRef getEnvironmentName() const;
327 /// getOSAndEnvironmentName - Get the operating system and optional
328 /// environment components as a single string (separated by a '-'
329 /// if the environment component is present).
330 StringRef getOSAndEnvironmentName() const;
333 /// @name Convenience Predicates
336 /// \brief Test whether the architecture is 64-bit
338 /// Note that this tests for 64-bit pointer width, and nothing else. Note
339 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
340 /// 16-bit. The inner details of pointer width for particular architectures
341 /// is not summed up in the triple, and so only a coarse grained predicate
342 /// system is provided.
343 bool isArch64Bit() const;
345 /// \brief Test whether the architecture is 32-bit
347 /// Note that this tests for 32-bit pointer width, and nothing else.
348 bool isArch32Bit() const;
350 /// \brief Test whether the architecture is 16-bit
352 /// Note that this tests for 16-bit pointer width, and nothing else.
353 bool isArch16Bit() const;
355 /// isOSVersionLT - Helper function for doing comparisons against version
356 /// numbers included in the target triple.
357 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
358 unsigned Micro = 0) const {
360 getOSVersion(LHS[0], LHS[1], LHS[2]);
363 return LHS[0] < Major;
365 return LHS[1] < Minor;
367 return LHS[1] < Micro;
372 bool isOSVersionLT(const Triple &Other) const {
374 Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
375 return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
378 /// isMacOSXVersionLT - Comparison function for checking OS X version
379 /// compatibility, which handles supporting skewed version numbering schemes
380 /// used by the "darwin" triples.
381 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
382 unsigned Micro = 0) const {
383 assert(isMacOSX() && "Not an OS X triple!");
385 // If this is OS X, expect a sane version number.
386 if (getOS() == Triple::MacOSX)
387 return isOSVersionLT(Major, Minor, Micro);
389 // Otherwise, compare to the "Darwin" number.
390 assert(Major == 10 && "Unexpected major version");
391 return isOSVersionLT(Minor + 4, Micro, 0);
394 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
395 /// "darwin" and "osx" as OS X triples.
396 bool isMacOSX() const {
397 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
400 /// Is this an iOS triple.
402 return getOS() == Triple::IOS;
405 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
406 bool isOSDarwin() const {
407 return isMacOSX() || isiOS();
410 bool isOSNetBSD() const {
411 return getOS() == Triple::NetBSD;
414 bool isOSOpenBSD() const {
415 return getOS() == Triple::OpenBSD;
418 bool isOSFreeBSD() const {
419 return getOS() == Triple::FreeBSD;
422 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
424 bool isOSSolaris() const {
425 return getOS() == Triple::Solaris;
428 bool isOSBitrig() const {
429 return getOS() == Triple::Bitrig;
432 bool isWindowsMSVCEnvironment() const {
433 return getOS() == Triple::Win32 &&
434 (getEnvironment() == Triple::UnknownEnvironment ||
435 getEnvironment() == Triple::MSVC);
438 bool isKnownWindowsMSVCEnvironment() const {
439 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
442 bool isWindowsCoreCLREnvironment() const {
443 return getOS() == Triple::Win32 && getEnvironment() == Triple::CoreCLR;
446 bool isWindowsItaniumEnvironment() const {
447 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
450 bool isWindowsCygwinEnvironment() const {
451 return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
454 bool isWindowsGNUEnvironment() const {
455 return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
458 /// \brief Tests for either Cygwin or MinGW OS
459 bool isOSCygMing() const {
460 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
463 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
464 bool isOSMSVCRT() const {
465 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
466 isWindowsItaniumEnvironment();
469 /// \brief Tests whether the OS is Windows.
470 bool isOSWindows() const {
471 return getOS() == Triple::Win32;
474 /// \brief Tests whether the OS is NaCl (Native Client)
475 bool isOSNaCl() const {
476 return getOS() == Triple::NaCl;
479 /// \brief Tests whether the OS is Linux.
480 bool isOSLinux() const {
481 return getOS() == Triple::Linux;
484 /// \brief Tests whether the OS uses the ELF binary format.
485 bool isOSBinFormatELF() const {
486 return getObjectFormat() == Triple::ELF;
489 /// \brief Tests whether the OS uses the COFF binary format.
490 bool isOSBinFormatCOFF() const {
491 return getObjectFormat() == Triple::COFF;
494 /// \brief Tests whether the environment is MachO.
495 bool isOSBinFormatMachO() const {
496 return getObjectFormat() == Triple::MachO;
499 /// \brief Tests whether the target is the PS4 CPU
500 bool isPS4CPU() const {
501 return getArch() == Triple::x86_64 &&
502 getVendor() == Triple::SCEI &&
503 getOS() == Triple::PS4;
506 /// \brief Tests whether the target is the PS4 platform
508 return getVendor() == Triple::SCEI &&
509 getOS() == Triple::PS4;
516 /// setArch - Set the architecture (first) component of the triple
518 void setArch(ArchType Kind);
520 /// setVendor - Set the vendor (second) component of the triple to a
522 void setVendor(VendorType Kind);
524 /// setOS - Set the operating system (third) component of the triple
526 void setOS(OSType Kind);
528 /// setEnvironment - Set the environment (fourth) component of the triple
530 void setEnvironment(EnvironmentType Kind);
532 /// setObjectFormat - Set the object file format
533 void setObjectFormat(ObjectFormatType Kind);
535 /// setTriple - Set all components to the new triple \p Str.
536 void setTriple(const Twine &Str);
538 /// setArchName - Set the architecture (first) component of the
540 void setArchName(StringRef Str);
542 /// setVendorName - Set the vendor (second) component of the triple
544 void setVendorName(StringRef Str);
546 /// setOSName - Set the operating system (third) component of the
548 void setOSName(StringRef Str);
550 /// setEnvironmentName - Set the optional environment (fourth)
551 /// component of the triple by name.
552 void setEnvironmentName(StringRef Str);
554 /// setOSAndEnvironmentName - Set the operating system and optional
555 /// environment components with a single string.
556 void setOSAndEnvironmentName(StringRef Str);
559 /// @name Helpers to build variants of a particular triple.
562 /// \brief Form a triple with a 32-bit variant of the current architecture.
564 /// This can be used to move across "families" of architectures where useful.
566 /// \returns A new triple with a 32-bit architecture or an unknown
567 /// architecture if no such variant can be found.
568 llvm::Triple get32BitArchVariant() const;
570 /// \brief Form a triple with a 64-bit variant of the current architecture.
572 /// This can be used to move across "families" of architectures where useful.
574 /// \returns A new triple with a 64-bit architecture or an unknown
575 /// architecture if no such variant can be found.
576 llvm::Triple get64BitArchVariant() const;
578 /// Form a triple with a big endian variant of the current architecture.
580 /// This can be used to move across "families" of architectures where useful.
582 /// \returns A new triple with a big endian architecture or an unknown
583 /// architecture if no such variant can be found.
584 llvm::Triple getBigEndianArchVariant() const;
586 /// Form a triple with a little endian variant of the current architecture.
588 /// This can be used to move across "families" of architectures where useful.
590 /// \returns A new triple with a little endian architecture or an unknown
591 /// architecture if no such variant can be found.
592 llvm::Triple getLittleEndianArchVariant() const;
594 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
596 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
597 /// string then the triple's arch name is used.
598 const char* getARMCPUForArch(StringRef Arch = StringRef()) const;
601 /// @name Static helpers for IDs.
604 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
605 static const char *getArchTypeName(ArchType Kind);
607 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
608 /// architecture. This is the prefix used by the architecture specific
609 /// builtins, and is suitable for passing to \see
610 /// Intrinsic::getIntrinsicForGCCBuiltin().
612 /// \return - The architecture prefix, or 0 if none is defined.
613 static const char *getArchTypePrefix(ArchType Kind);
615 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
616 static const char *getVendorTypeName(VendorType Kind);
618 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
619 static const char *getOSTypeName(OSType Kind);
621 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
623 static const char *getEnvironmentTypeName(EnvironmentType Kind);
626 /// @name Static helpers for converting alternate architecture names.
629 /// getArchTypeForLLVMName - The canonical type for the given LLVM
630 /// architecture name (e.g., "x86").
631 static ArchType getArchTypeForLLVMName(StringRef Str);
636 } // End llvm namespace