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 / Emscripten)
79 le64, // le64: generic little-endian 64-bit CPU (PNaCl / Emscripten)
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
121 ImaginationTechnologies,
146 NaCl, // Native Client
147 CNK, // BG/P Compute-Node Kernel
151 NVCL, // NVIDIA OpenCL
152 AMDHSA, // AMD HSA Runtime
156 enum EnvironmentType {
171 LastEnvironmentType = Cygnus
173 enum ObjectFormatType {
184 /// The parsed arch type.
187 /// The parsed subarchitecture type.
190 /// The parsed vendor type.
193 /// The parsed OS type.
196 /// The parsed Environment type.
197 EnvironmentType Environment;
199 /// The object format type.
200 ObjectFormatType ObjectFormat;
203 /// @name Constructors
206 /// \brief Default constructor is the same as an empty string and leaves all
207 /// triple fields unknown.
208 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
210 explicit Triple(const Twine &Str);
211 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
212 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
213 const Twine &EnvironmentStr);
215 bool operator==(const Triple &Other) const {
216 return Arch == Other.Arch && SubArch == Other.SubArch &&
217 Vendor == Other.Vendor && OS == Other.OS &&
218 Environment == Other.Environment &&
219 ObjectFormat == Other.ObjectFormat;
223 /// @name Normalization
226 /// normalize - Turn an arbitrary machine specification into the canonical
227 /// triple form (or something sensible that the Triple class understands if
228 /// nothing better can reasonably be done). In particular, it handles the
229 /// common case in which otherwise valid components are in the wrong order.
230 static std::string normalize(StringRef Str);
232 /// \brief Return the normalized form of this triple's string.
233 std::string normalize() const { return normalize(Data); }
236 /// @name Typed Component Access
239 /// getArch - Get the parsed architecture type of this triple.
240 ArchType getArch() const { return Arch; }
242 /// getSubArch - get the parsed subarchitecture type for this triple.
243 SubArchType getSubArch() const { return SubArch; }
245 /// getVendor - Get the parsed vendor type of this triple.
246 VendorType getVendor() const { return Vendor; }
248 /// getOS - Get the parsed operating system type of this triple.
249 OSType getOS() const { return OS; }
251 /// hasEnvironment - Does this triple have the optional environment
252 /// (fourth) component?
253 bool hasEnvironment() const {
254 return getEnvironmentName() != "";
257 /// getEnvironment - Get the parsed environment type of this triple.
258 EnvironmentType getEnvironment() const { return Environment; }
260 /// \brief Parse the version number from the OS name component of the
261 /// triple, if present.
263 /// For example, "fooos1.2.3" would return (1, 2, 3).
265 /// If an entry is not defined, it will be returned as 0.
266 void getEnvironmentVersion(unsigned &Major, unsigned &Minor,
267 unsigned &Micro) const;
269 /// getFormat - Get the object format for this triple.
270 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
272 /// getOSVersion - Parse the version number from the OS name component of the
273 /// triple, if present.
275 /// For example, "fooos1.2.3" would return (1, 2, 3).
277 /// If an entry is not defined, it will be returned as 0.
278 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
280 /// getOSMajorVersion - Return just the major version number, this is
281 /// specialized because it is a common query.
282 unsigned getOSMajorVersion() const {
283 unsigned Maj, Min, Micro;
284 getOSVersion(Maj, Min, Micro);
288 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
289 /// translate generic "darwin" versions to the corresponding OS X versions.
290 /// This may also be called with IOS triples but the OS X version number is
291 /// just set to a constant 10.4.0 in that case. Returns true if successful.
292 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
293 unsigned &Micro) const;
295 /// getiOSVersion - Parse the version number as with getOSVersion. This should
296 /// only be called with IOS triples.
297 void getiOSVersion(unsigned &Major, unsigned &Minor,
298 unsigned &Micro) const;
301 /// @name Direct Component Access
304 const std::string &str() const { return Data; }
306 const std::string &getTriple() const { return Data; }
308 /// getArchName - Get the architecture (first) component of the
310 StringRef getArchName() const;
312 /// getVendorName - Get the vendor (second) component of the triple.
313 StringRef getVendorName() const;
315 /// getOSName - Get the operating system (third) component of the
317 StringRef getOSName() const;
319 /// getEnvironmentName - Get the optional environment (fourth)
320 /// component of the triple, or "" if empty.
321 StringRef getEnvironmentName() const;
323 /// getOSAndEnvironmentName - Get the operating system and optional
324 /// environment components as a single string (separated by a '-'
325 /// if the environment component is present).
326 StringRef getOSAndEnvironmentName() const;
329 /// @name Convenience Predicates
332 /// \brief Test whether the architecture is 64-bit
334 /// Note that this tests for 64-bit pointer width, and nothing else. Note
335 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
336 /// 16-bit. The inner details of pointer width for particular architectures
337 /// is not summed up in the triple, and so only a coarse grained predicate
338 /// system is provided.
339 bool isArch64Bit() const;
341 /// \brief Test whether the architecture is 32-bit
343 /// Note that this tests for 32-bit pointer width, and nothing else.
344 bool isArch32Bit() const;
346 /// \brief Test whether the architecture is 16-bit
348 /// Note that this tests for 16-bit pointer width, and nothing else.
349 bool isArch16Bit() const;
351 /// isOSVersionLT - Helper function for doing comparisons against version
352 /// numbers included in the target triple.
353 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
354 unsigned Micro = 0) const {
356 getOSVersion(LHS[0], LHS[1], LHS[2]);
359 return LHS[0] < Major;
361 return LHS[1] < Minor;
363 return LHS[1] < Micro;
368 bool isOSVersionLT(const Triple &Other) const {
370 Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
371 return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
374 /// isMacOSXVersionLT - Comparison function for checking OS X version
375 /// compatibility, which handles supporting skewed version numbering schemes
376 /// used by the "darwin" triples.
377 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
378 unsigned Micro = 0) const {
379 assert(isMacOSX() && "Not an OS X triple!");
381 // If this is OS X, expect a sane version number.
382 if (getOS() == Triple::MacOSX)
383 return isOSVersionLT(Major, Minor, Micro);
385 // Otherwise, compare to the "Darwin" number.
386 assert(Major == 10 && "Unexpected major version");
387 return isOSVersionLT(Minor + 4, Micro, 0);
390 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
391 /// "darwin" and "osx" as OS X triples.
392 bool isMacOSX() const {
393 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
396 /// Is this an iOS triple.
398 return getOS() == Triple::IOS;
401 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
402 bool isOSDarwin() const {
403 return isMacOSX() || isiOS();
406 bool isOSNetBSD() const {
407 return getOS() == Triple::NetBSD;
410 bool isOSOpenBSD() const {
411 return getOS() == Triple::OpenBSD;
414 bool isOSFreeBSD() const {
415 return getOS() == Triple::FreeBSD;
418 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
420 bool isOSSolaris() const {
421 return getOS() == Triple::Solaris;
424 bool isOSBitrig() const {
425 return getOS() == Triple::Bitrig;
428 bool isWindowsMSVCEnvironment() const {
429 return getOS() == Triple::Win32 &&
430 (getEnvironment() == Triple::UnknownEnvironment ||
431 getEnvironment() == Triple::MSVC);
434 bool isKnownWindowsMSVCEnvironment() const {
435 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
438 bool isWindowsItaniumEnvironment() const {
439 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
442 bool isWindowsCygwinEnvironment() const {
443 return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
446 bool isWindowsGNUEnvironment() const {
447 return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
450 /// \brief Tests for either Cygwin or MinGW OS
451 bool isOSCygMing() const {
452 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
455 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
456 bool isOSMSVCRT() const {
457 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
458 isWindowsItaniumEnvironment();
461 /// \brief Tests whether the OS is Windows.
462 bool isOSWindows() const {
463 return getOS() == Triple::Win32;
466 /// \brief Tests whether the OS is NaCl (Native Client)
467 bool isOSNaCl() const {
468 return getOS() == Triple::NaCl;
471 /// \brief Tests whether the OS is Linux.
472 bool isOSLinux() const {
473 return getOS() == Triple::Linux;
476 /// \brief Tests whether the OS uses the ELF binary format.
477 bool isOSBinFormatELF() const {
478 return getObjectFormat() == Triple::ELF;
481 /// \brief Tests whether the OS uses the COFF binary format.
482 bool isOSBinFormatCOFF() const {
483 return getObjectFormat() == Triple::COFF;
486 /// \brief Tests whether the environment is MachO.
487 bool isOSBinFormatMachO() const {
488 return getObjectFormat() == Triple::MachO;
491 /// \brief Tests whether the target is the PS4 CPU
492 bool isPS4CPU() const {
493 return getArch() == Triple::x86_64 &&
494 getVendor() == Triple::SCEI &&
495 getOS() == Triple::PS4;
498 /// \brief Tests whether the target is the PS4 platform
500 return getVendor() == Triple::SCEI &&
501 getOS() == Triple::PS4;
508 /// setArch - Set the architecture (first) component of the triple
510 void setArch(ArchType Kind);
512 /// setVendor - Set the vendor (second) component of the triple to a
514 void setVendor(VendorType Kind);
516 /// setOS - Set the operating system (third) component of the triple
518 void setOS(OSType Kind);
520 /// setEnvironment - Set the environment (fourth) component of the triple
522 void setEnvironment(EnvironmentType Kind);
524 /// setObjectFormat - Set the object file format
525 void setObjectFormat(ObjectFormatType Kind);
527 /// setTriple - Set all components to the new triple \p Str.
528 void setTriple(const Twine &Str);
530 /// setArchName - Set the architecture (first) component of the
532 void setArchName(StringRef Str);
534 /// setVendorName - Set the vendor (second) component of the triple
536 void setVendorName(StringRef Str);
538 /// setOSName - Set the operating system (third) component of the
540 void setOSName(StringRef Str);
542 /// setEnvironmentName - Set the optional environment (fourth)
543 /// component of the triple by name.
544 void setEnvironmentName(StringRef Str);
546 /// setOSAndEnvironmentName - Set the operating system and optional
547 /// environment components with a single string.
548 void setOSAndEnvironmentName(StringRef Str);
551 /// @name Helpers to build variants of a particular triple.
554 /// \brief Form a triple with a 32-bit variant of the current architecture.
556 /// This can be used to move across "families" of architectures where useful.
558 /// \returns A new triple with a 32-bit architecture or an unknown
559 /// architecture if no such variant can be found.
560 llvm::Triple get32BitArchVariant() const;
562 /// \brief Form a triple with a 64-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 64-bit architecture or an unknown
567 /// architecture if no such variant can be found.
568 llvm::Triple get64BitArchVariant() const;
570 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
572 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
573 /// string then the triple's arch name is used.
574 const char* getARMCPUForArch(StringRef Arch = StringRef()) const;
577 /// @name Static helpers for IDs.
580 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
581 static const char *getArchTypeName(ArchType Kind);
583 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
584 /// architecture. This is the prefix used by the architecture specific
585 /// builtins, and is suitable for passing to \see
586 /// Intrinsic::getIntrinsicForGCCBuiltin().
588 /// \return - The architecture prefix, or 0 if none is defined.
589 static const char *getArchTypePrefix(ArchType Kind);
591 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
592 static const char *getVendorTypeName(VendorType Kind);
594 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
595 static const char *getOSTypeName(OSType Kind);
597 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
599 static const char *getEnvironmentTypeName(EnvironmentType Kind);
602 /// @name Static helpers for converting alternate architecture names.
605 /// getArchTypeForLLVMName - The canonical type for the given LLVM
606 /// architecture name (e.g., "x86").
607 static ArchType getArchTypeForLLVMName(StringRef Str);