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 LastArchType = kalimba
120 ImaginationTechnologies,
145 NaCl, // Native Client
146 CNK, // BG/P Compute-Node Kernel
150 NVCL, // NVIDIA OpenCL
151 AMDHSA, // AMD HSA Runtime
155 enum EnvironmentType {
170 LastEnvironmentType = Cygnus
172 enum ObjectFormatType {
183 /// The parsed arch type.
186 /// The parsed subarchitecture type.
189 /// The parsed vendor type.
192 /// The parsed OS type.
195 /// The parsed Environment type.
196 EnvironmentType Environment;
198 /// The object format type.
199 ObjectFormatType ObjectFormat;
202 /// @name Constructors
205 /// \brief Default constructor is the same as an empty string and leaves all
206 /// triple fields unknown.
207 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
209 explicit Triple(const Twine &Str);
210 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
211 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
212 const Twine &EnvironmentStr);
214 bool operator==(const Triple &Other) const {
215 return Arch == Other.Arch && SubArch == Other.SubArch &&
216 Vendor == Other.Vendor && OS == Other.OS &&
217 Environment == Other.Environment &&
218 ObjectFormat == Other.ObjectFormat;
222 /// @name Normalization
225 /// normalize - Turn an arbitrary machine specification into the canonical
226 /// triple form (or something sensible that the Triple class understands if
227 /// nothing better can reasonably be done). In particular, it handles the
228 /// common case in which otherwise valid components are in the wrong order.
229 static std::string normalize(StringRef Str);
231 /// \brief Return the normalized form of this triple's string.
232 std::string normalize() const { return normalize(Data); }
235 /// @name Typed Component Access
238 /// getArch - Get the parsed architecture type of this triple.
239 ArchType getArch() const { return Arch; }
241 /// getSubArch - get the parsed subarchitecture type for this triple.
242 SubArchType getSubArch() const { return SubArch; }
244 /// getVendor - Get the parsed vendor type of this triple.
245 VendorType getVendor() const { return Vendor; }
247 /// getOS - Get the parsed operating system type of this triple.
248 OSType getOS() const { return OS; }
250 /// hasEnvironment - Does this triple have the optional environment
251 /// (fourth) component?
252 bool hasEnvironment() const {
253 return getEnvironmentName() != "";
256 /// getEnvironment - Get the parsed environment type of this triple.
257 EnvironmentType getEnvironment() const { return Environment; }
259 /// \brief Parse the version number from the OS name component of the
260 /// triple, if present.
262 /// For example, "fooos1.2.3" would return (1, 2, 3).
264 /// If an entry is not defined, it will be returned as 0.
265 void getEnvironmentVersion(unsigned &Major, unsigned &Minor,
266 unsigned &Micro) const;
268 /// getFormat - Get the object format for this triple.
269 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
271 /// getOSVersion - Parse the version number from the OS name component of the
272 /// triple, if present.
274 /// For example, "fooos1.2.3" would return (1, 2, 3).
276 /// If an entry is not defined, it will be returned as 0.
277 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
279 /// getOSMajorVersion - Return just the major version number, this is
280 /// specialized because it is a common query.
281 unsigned getOSMajorVersion() const {
282 unsigned Maj, Min, Micro;
283 getOSVersion(Maj, Min, Micro);
287 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
288 /// translate generic "darwin" versions to the corresponding OS X versions.
289 /// This may also be called with IOS triples but the OS X version number is
290 /// just set to a constant 10.4.0 in that case. Returns true if successful.
291 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
292 unsigned &Micro) const;
294 /// getiOSVersion - Parse the version number as with getOSVersion. This should
295 /// only be called with IOS triples.
296 void getiOSVersion(unsigned &Major, unsigned &Minor,
297 unsigned &Micro) const;
300 /// @name Direct Component Access
303 const std::string &str() const { return Data; }
305 const std::string &getTriple() const { return Data; }
307 /// getArchName - Get the architecture (first) component of the
309 StringRef getArchName() const;
311 /// getVendorName - Get the vendor (second) component of the triple.
312 StringRef getVendorName() const;
314 /// getOSName - Get the operating system (third) component of the
316 StringRef getOSName() const;
318 /// getEnvironmentName - Get the optional environment (fourth)
319 /// component of the triple, or "" if empty.
320 StringRef getEnvironmentName() const;
322 /// getOSAndEnvironmentName - Get the operating system and optional
323 /// environment components as a single string (separated by a '-'
324 /// if the environment component is present).
325 StringRef getOSAndEnvironmentName() const;
328 /// @name Convenience Predicates
331 /// \brief Test whether the architecture is 64-bit
333 /// Note that this tests for 64-bit pointer width, and nothing else. Note
334 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
335 /// 16-bit. The inner details of pointer width for particular architectures
336 /// is not summed up in the triple, and so only a coarse grained predicate
337 /// system is provided.
338 bool isArch64Bit() const;
340 /// \brief Test whether the architecture is 32-bit
342 /// Note that this tests for 32-bit pointer width, and nothing else.
343 bool isArch32Bit() const;
345 /// \brief Test whether the architecture is 16-bit
347 /// Note that this tests for 16-bit pointer width, and nothing else.
348 bool isArch16Bit() const;
350 /// isOSVersionLT - Helper function for doing comparisons against version
351 /// numbers included in the target triple.
352 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
353 unsigned Micro = 0) const {
355 getOSVersion(LHS[0], LHS[1], LHS[2]);
358 return LHS[0] < Major;
360 return LHS[1] < Minor;
362 return LHS[1] < Micro;
367 bool isOSVersionLT(const Triple &Other) const {
369 Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
370 return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
373 /// isMacOSXVersionLT - Comparison function for checking OS X version
374 /// compatibility, which handles supporting skewed version numbering schemes
375 /// used by the "darwin" triples.
376 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
377 unsigned Micro = 0) const {
378 assert(isMacOSX() && "Not an OS X triple!");
380 // If this is OS X, expect a sane version number.
381 if (getOS() == Triple::MacOSX)
382 return isOSVersionLT(Major, Minor, Micro);
384 // Otherwise, compare to the "Darwin" number.
385 assert(Major == 10 && "Unexpected major version");
386 return isOSVersionLT(Minor + 4, Micro, 0);
389 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
390 /// "darwin" and "osx" as OS X triples.
391 bool isMacOSX() const {
392 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
395 /// Is this an iOS triple.
397 return getOS() == Triple::IOS;
400 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
401 bool isOSDarwin() const {
402 return isMacOSX() || isiOS();
405 bool isOSNetBSD() const {
406 return getOS() == Triple::NetBSD;
409 bool isOSOpenBSD() const {
410 return getOS() == Triple::OpenBSD;
413 bool isOSFreeBSD() const {
414 return getOS() == Triple::FreeBSD;
417 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
419 bool isOSSolaris() const {
420 return getOS() == Triple::Solaris;
423 bool isOSBitrig() const {
424 return getOS() == Triple::Bitrig;
427 bool isWindowsMSVCEnvironment() const {
428 return getOS() == Triple::Win32 &&
429 (getEnvironment() == Triple::UnknownEnvironment ||
430 getEnvironment() == Triple::MSVC);
433 bool isKnownWindowsMSVCEnvironment() const {
434 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
437 bool isWindowsItaniumEnvironment() const {
438 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
441 bool isWindowsCygwinEnvironment() const {
442 return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
445 bool isWindowsGNUEnvironment() const {
446 return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
449 /// \brief Tests for either Cygwin or MinGW OS
450 bool isOSCygMing() const {
451 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
454 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
455 bool isOSMSVCRT() const {
456 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
457 isWindowsItaniumEnvironment();
460 /// \brief Tests whether the OS is Windows.
461 bool isOSWindows() const {
462 return getOS() == Triple::Win32;
465 /// \brief Tests whether the OS is NaCl (Native Client)
466 bool isOSNaCl() const {
467 return getOS() == Triple::NaCl;
470 /// \brief Tests whether the OS is Linux.
471 bool isOSLinux() const {
472 return getOS() == Triple::Linux;
475 /// \brief Tests whether the OS uses the ELF binary format.
476 bool isOSBinFormatELF() const {
477 return getObjectFormat() == Triple::ELF;
480 /// \brief Tests whether the OS uses the COFF binary format.
481 bool isOSBinFormatCOFF() const {
482 return getObjectFormat() == Triple::COFF;
485 /// \brief Tests whether the environment is MachO.
486 bool isOSBinFormatMachO() const {
487 return getObjectFormat() == Triple::MachO;
490 /// \brief Tests whether the target is the PS4 CPU
491 bool isPS4CPU() const {
492 return getArch() == Triple::x86_64 &&
493 getVendor() == Triple::SCEI &&
494 getOS() == Triple::PS4;
497 /// \brief Tests whether the target is the PS4 platform
499 return getVendor() == Triple::SCEI &&
500 getOS() == Triple::PS4;
507 /// setArch - Set the architecture (first) component of the triple
509 void setArch(ArchType Kind);
511 /// setVendor - Set the vendor (second) component of the triple to a
513 void setVendor(VendorType Kind);
515 /// setOS - Set the operating system (third) component of the triple
517 void setOS(OSType Kind);
519 /// setEnvironment - Set the environment (fourth) component of the triple
521 void setEnvironment(EnvironmentType Kind);
523 /// setObjectFormat - Set the object file format
524 void setObjectFormat(ObjectFormatType Kind);
526 /// setTriple - Set all components to the new triple \p Str.
527 void setTriple(const Twine &Str);
529 /// setArchName - Set the architecture (first) component of the
531 void setArchName(StringRef Str);
533 /// setVendorName - Set the vendor (second) component of the triple
535 void setVendorName(StringRef Str);
537 /// setOSName - Set the operating system (third) component of the
539 void setOSName(StringRef Str);
541 /// setEnvironmentName - Set the optional environment (fourth)
542 /// component of the triple by name.
543 void setEnvironmentName(StringRef Str);
545 /// setOSAndEnvironmentName - Set the operating system and optional
546 /// environment components with a single string.
547 void setOSAndEnvironmentName(StringRef Str);
550 /// @name Helpers to build variants of a particular triple.
553 /// \brief Form a triple with a 32-bit variant of the current architecture.
555 /// This can be used to move across "families" of architectures where useful.
557 /// \returns A new triple with a 32-bit architecture or an unknown
558 /// architecture if no such variant can be found.
559 llvm::Triple get32BitArchVariant() const;
561 /// \brief Form a triple with a 64-bit variant of the current architecture.
563 /// This can be used to move across "families" of architectures where useful.
565 /// \returns A new triple with a 64-bit architecture or an unknown
566 /// architecture if no such variant can be found.
567 llvm::Triple get64BitArchVariant() const;
569 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
571 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
572 /// string then the triple's arch name is used.
573 const char* getARMCPUForArch(StringRef Arch = StringRef()) const;
576 /// @name Static helpers for IDs.
579 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
580 static const char *getArchTypeName(ArchType Kind);
582 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
583 /// architecture. This is the prefix used by the architecture specific
584 /// builtins, and is suitable for passing to \see
585 /// Intrinsic::getIntrinsicForGCCBuiltin().
587 /// \return - The architecture prefix, or 0 if none is defined.
588 static const char *getArchTypePrefix(ArchType Kind);
590 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
591 static const char *getVendorTypeName(VendorType Kind);
593 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
594 static const char *getOSTypeName(OSType Kind);
596 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
598 static const char *getEnvironmentTypeName(EnvironmentType Kind);
601 /// @name Static helpers for converting alternate architecture names.
604 /// getArchTypeForLLVMName - The canonical type for the given LLVM
605 /// architecture name (e.g., "x86").
606 static ArchType getArchTypeForLLVMName(StringRef Str);
611 } // End llvm namespace