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 bpf, // eBPF or extended BPF or 64-bit BPF (little endian)
54 hexagon, // Hexagon: hexagon
55 mips, // MIPS: mips, mipsallegrex
56 mipsel, // MIPSEL: mipsel, mipsallegrexel
57 mips64, // MIPS64: mips64
58 mips64el, // MIPS64EL: mips64el
59 msp430, // MSP430: msp430
61 ppc64, // PPC64: powerpc64, ppu
62 ppc64le, // PPC64LE: powerpc64le
63 r600, // R600: AMD GPUs HD2XXX - HD6XXX
64 amdgcn, // AMDGCN: AMD GCN GPUs
65 sparc, // Sparc: sparc
66 sparcv9, // Sparcv9: Sparcv9
67 systemz, // SystemZ: s390x
68 tce, // TCE (http://tce.cs.tut.fi/): tce
69 thumb, // Thumb (little endian): thumb, thumbv.*
70 thumbeb, // Thumb (big endian): thumbeb
72 x86_64, // X86-64: amd64, x86_64
73 xcore, // XCore: xcore
74 nvptx, // NVPTX: 32-bit
75 nvptx64, // NVPTX: 64-bit
76 le32, // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
77 le64, // le64: generic little-endian 64-bit CPU (PNaCl / Emscripten)
79 amdil64, // AMDIL with 64-bit pointers
81 hsail64, // AMD HSAIL with 64-bit pointers
82 spir, // SPIR: standard portable IR for OpenCL 32-bit version
83 spir64, // SPIR: standard portable IR for OpenCL 64-bit version
84 kalimba // Kalimba: generic kalimba
116 ImaginationTechnologies,
140 NaCl, // Native Client
141 CNK, // BG/P Compute-Node Kernel
145 NVCL, // NVIDIA OpenCL
146 AMDHSA, // AMD HSA Runtime
149 enum EnvironmentType {
165 enum ObjectFormatType {
176 /// The parsed arch type.
179 /// The parsed subarchitecture type.
182 /// The parsed vendor type.
185 /// The parsed OS type.
188 /// The parsed Environment type.
189 EnvironmentType Environment;
191 /// The object format type.
192 ObjectFormatType ObjectFormat;
195 /// @name Constructors
198 /// \brief Default constructor is the same as an empty string and leaves all
199 /// triple fields unknown.
200 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
202 explicit Triple(const Twine &Str);
203 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
204 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
205 const Twine &EnvironmentStr);
207 bool operator==(const Triple &Other) const {
208 return Arch == Other.Arch && SubArch == Other.SubArch &&
209 Vendor == Other.Vendor && OS == Other.OS &&
210 Environment == Other.Environment &&
211 ObjectFormat == Other.ObjectFormat;
215 /// @name Normalization
218 /// normalize - Turn an arbitrary machine specification into the canonical
219 /// triple form (or something sensible that the Triple class understands if
220 /// nothing better can reasonably be done). In particular, it handles the
221 /// common case in which otherwise valid components are in the wrong order.
222 static std::string normalize(StringRef Str);
224 /// \brief Return the normalized form of this triple's string.
225 std::string normalize() const { return normalize(Data); }
228 /// @name Typed Component Access
231 /// getArch - Get the parsed architecture type of this triple.
232 ArchType getArch() const { return Arch; }
234 /// getSubArch - get the parsed subarchitecture type for this triple.
235 SubArchType getSubArch() const { return SubArch; }
237 /// getVendor - Get the parsed vendor type of this triple.
238 VendorType getVendor() const { return Vendor; }
240 /// getOS - Get the parsed operating system type of this triple.
241 OSType getOS() const { return OS; }
243 /// hasEnvironment - Does this triple have the optional environment
244 /// (fourth) component?
245 bool hasEnvironment() const {
246 return getEnvironmentName() != "";
249 /// getEnvironment - Get the parsed environment type of this triple.
250 EnvironmentType getEnvironment() const { return Environment; }
252 /// getFormat - Get the object format for this triple.
253 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
255 /// getOSVersion - Parse the version number from the OS name component of the
256 /// triple, if present.
258 /// For example, "fooos1.2.3" would return (1, 2, 3).
260 /// If an entry is not defined, it will be returned as 0.
261 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
263 /// getOSMajorVersion - Return just the major version number, this is
264 /// specialized because it is a common query.
265 unsigned getOSMajorVersion() const {
266 unsigned Maj, Min, Micro;
267 getOSVersion(Maj, Min, Micro);
271 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
272 /// translate generic "darwin" versions to the corresponding OS X versions.
273 /// This may also be called with IOS triples but the OS X version number is
274 /// just set to a constant 10.4.0 in that case. Returns true if successful.
275 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
276 unsigned &Micro) const;
278 /// getiOSVersion - Parse the version number as with getOSVersion. This should
279 /// only be called with IOS triples.
280 void getiOSVersion(unsigned &Major, unsigned &Minor,
281 unsigned &Micro) const;
284 /// @name Direct Component Access
287 const std::string &str() const { return Data; }
289 const std::string &getTriple() const { return Data; }
291 /// getArchName - Get the architecture (first) component of the
293 StringRef getArchName() const;
295 /// getVendorName - Get the vendor (second) component of the triple.
296 StringRef getVendorName() const;
298 /// getOSName - Get the operating system (third) component of the
300 StringRef getOSName() const;
302 /// getEnvironmentName - Get the optional environment (fourth)
303 /// component of the triple, or "" if empty.
304 StringRef getEnvironmentName() const;
306 /// getOSAndEnvironmentName - Get the operating system and optional
307 /// environment components as a single string (separated by a '-'
308 /// if the environment component is present).
309 StringRef getOSAndEnvironmentName() const;
312 /// @name Convenience Predicates
315 /// \brief Test whether the architecture is 64-bit
317 /// Note that this tests for 64-bit pointer width, and nothing else. Note
318 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
319 /// 16-bit. The inner details of pointer width for particular architectures
320 /// is not summed up in the triple, and so only a coarse grained predicate
321 /// system is provided.
322 bool isArch64Bit() const;
324 /// \brief Test whether the architecture is 32-bit
326 /// Note that this tests for 32-bit pointer width, and nothing else.
327 bool isArch32Bit() const;
329 /// \brief Test whether the architecture is 16-bit
331 /// Note that this tests for 16-bit pointer width, and nothing else.
332 bool isArch16Bit() const;
334 /// isOSVersionLT - Helper function for doing comparisons against version
335 /// numbers included in the target triple.
336 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
337 unsigned Micro = 0) const {
339 getOSVersion(LHS[0], LHS[1], LHS[2]);
342 return LHS[0] < Major;
344 return LHS[1] < Minor;
346 return LHS[1] < Micro;
351 bool isOSVersionLT(const Triple &Other) const {
353 Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
354 return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
357 /// isMacOSXVersionLT - Comparison function for checking OS X version
358 /// compatibility, which handles supporting skewed version numbering schemes
359 /// used by the "darwin" triples.
360 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
361 unsigned Micro = 0) const {
362 assert(isMacOSX() && "Not an OS X triple!");
364 // If this is OS X, expect a sane version number.
365 if (getOS() == Triple::MacOSX)
366 return isOSVersionLT(Major, Minor, Micro);
368 // Otherwise, compare to the "Darwin" number.
369 assert(Major == 10 && "Unexpected major version");
370 return isOSVersionLT(Minor + 4, Micro, 0);
373 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
374 /// "darwin" and "osx" as OS X triples.
375 bool isMacOSX() const {
376 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
379 /// Is this an iOS triple.
381 return getOS() == Triple::IOS;
384 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
385 bool isOSDarwin() const {
386 return isMacOSX() || isiOS();
389 bool isOSNetBSD() const {
390 return getOS() == Triple::NetBSD;
393 bool isOSOpenBSD() const {
394 return getOS() == Triple::OpenBSD;
397 bool isOSFreeBSD() const {
398 return getOS() == Triple::FreeBSD;
401 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
403 bool isOSSolaris() const {
404 return getOS() == Triple::Solaris;
407 bool isOSBitrig() const {
408 return getOS() == Triple::Bitrig;
411 bool isWindowsMSVCEnvironment() const {
412 return getOS() == Triple::Win32 &&
413 (getEnvironment() == Triple::UnknownEnvironment ||
414 getEnvironment() == Triple::MSVC);
417 bool isKnownWindowsMSVCEnvironment() const {
418 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
421 bool isWindowsItaniumEnvironment() const {
422 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
425 bool isWindowsCygwinEnvironment() const {
426 return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
429 bool isWindowsGNUEnvironment() const {
430 return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
433 /// \brief Tests for either Cygwin or MinGW OS
434 bool isOSCygMing() const {
435 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
438 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
439 bool isOSMSVCRT() const {
440 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
441 isWindowsItaniumEnvironment();
444 /// \brief Tests whether the OS is Windows.
445 bool isOSWindows() const {
446 return getOS() == Triple::Win32;
449 /// \brief Tests whether the OS is NaCl (Native Client)
450 bool isOSNaCl() const {
451 return getOS() == Triple::NaCl;
454 /// \brief Tests whether the OS is Linux.
455 bool isOSLinux() const {
456 return getOS() == Triple::Linux;
459 /// \brief Tests whether the OS uses the ELF binary format.
460 bool isOSBinFormatELF() const {
461 return getObjectFormat() == Triple::ELF;
464 /// \brief Tests whether the OS uses the COFF binary format.
465 bool isOSBinFormatCOFF() const {
466 return getObjectFormat() == Triple::COFF;
469 /// \brief Tests whether the environment is MachO.
470 bool isOSBinFormatMachO() const {
471 return getObjectFormat() == Triple::MachO;
474 /// \brief Tests whether the target is the PS4 CPU
475 bool isPS4CPU() const {
476 return getArch() == Triple::x86_64 &&
477 getVendor() == Triple::SCEI &&
478 getOS() == Triple::PS4;
481 /// \brief Tests whether the target is the PS4 platform
483 return getVendor() == Triple::SCEI &&
484 getOS() == Triple::PS4;
491 /// setArch - Set the architecture (first) component of the triple
493 void setArch(ArchType Kind);
495 /// setVendor - Set the vendor (second) component of the triple to a
497 void setVendor(VendorType Kind);
499 /// setOS - Set the operating system (third) component of the triple
501 void setOS(OSType Kind);
503 /// setEnvironment - Set the environment (fourth) component of the triple
505 void setEnvironment(EnvironmentType Kind);
507 /// setObjectFormat - Set the object file format
508 void setObjectFormat(ObjectFormatType Kind);
510 /// setTriple - Set all components to the new triple \p Str.
511 void setTriple(const Twine &Str);
513 /// setArchName - Set the architecture (first) component of the
515 void setArchName(StringRef Str);
517 /// setVendorName - Set the vendor (second) component of the triple
519 void setVendorName(StringRef Str);
521 /// setOSName - Set the operating system (third) component of the
523 void setOSName(StringRef Str);
525 /// setEnvironmentName - Set the optional environment (fourth)
526 /// component of the triple by name.
527 void setEnvironmentName(StringRef Str);
529 /// setOSAndEnvironmentName - Set the operating system and optional
530 /// environment components with a single string.
531 void setOSAndEnvironmentName(StringRef Str);
534 /// @name Helpers to build variants of a particular triple.
537 /// \brief Form a triple with a 32-bit variant of the current architecture.
539 /// This can be used to move across "families" of architectures where useful.
541 /// \returns A new triple with a 32-bit architecture or an unknown
542 /// architecture if no such variant can be found.
543 llvm::Triple get32BitArchVariant() const;
545 /// \brief Form a triple with a 64-bit variant of the current architecture.
547 /// This can be used to move across "families" of architectures where useful.
549 /// \returns A new triple with a 64-bit architecture or an unknown
550 /// architecture if no such variant can be found.
551 llvm::Triple get64BitArchVariant() const;
553 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
555 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
556 /// string then the triple's arch name is used.
557 const char* getARMCPUForArch(StringRef Arch = StringRef()) const;
560 /// @name Static helpers for IDs.
563 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
564 static const char *getArchTypeName(ArchType Kind);
566 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
567 /// architecture. This is the prefix used by the architecture specific
568 /// builtins, and is suitable for passing to \see
569 /// Intrinsic::getIntrinsicForGCCBuiltin().
571 /// \return - The architecture prefix, or 0 if none is defined.
572 static const char *getArchTypePrefix(ArchType Kind);
574 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
575 static const char *getVendorTypeName(VendorType Kind);
577 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
578 static const char *getOSTypeName(OSType Kind);
580 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
582 static const char *getEnvironmentTypeName(EnvironmentType Kind);
585 /// @name Static helpers for converting alternate architecture names.
588 /// getArchTypeForLLVMName - The canonical type for the given LLVM
589 /// architecture name (e.g., "x86").
590 static ArchType getArchTypeForLLVMName(StringRef Str);
595 } // End llvm namespace