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 avr, // AVR: Atmel AVR microcontroller
54 bpfel, // eBPF or extended BPF or 64-bit BPF (little endian)
55 bpfeb, // eBPF or extended BPF or 64-bit BPF (big endian)
56 hexagon, // Hexagon: hexagon
57 mips, // MIPS: mips, mipsallegrex
58 mipsel, // MIPSEL: mipsel, mipsallegrexel
59 mips64, // MIPS64: mips64
60 mips64el, // MIPS64EL: mips64el
61 msp430, // MSP430: msp430
63 ppc64, // PPC64: powerpc64, ppu
64 ppc64le, // PPC64LE: powerpc64le
65 r600, // R600: AMD GPUs HD2XXX - HD6XXX
66 amdgcn, // AMDGCN: AMD GCN GPUs
67 sparc, // Sparc: sparc
68 sparcv9, // Sparcv9: Sparcv9
69 sparcel, // Sparc: (endianness = little). NB: 'Sparcle' is a CPU variant
70 systemz, // SystemZ: s390x
71 tce, // TCE (http://tce.cs.tut.fi/): tce
72 thumb, // Thumb (little endian): thumb, thumbv.*
73 thumbeb, // Thumb (big endian): thumbeb
75 x86_64, // X86-64: amd64, x86_64
76 xcore, // XCore: xcore
77 nvptx, // NVPTX: 32-bit
78 nvptx64, // NVPTX: 64-bit
79 le32, // le32: generic little-endian 32-bit CPU (PNaCl)
80 le64, // le64: generic little-endian 64-bit CPU (PNaCl)
82 amdil64, // AMDIL with 64-bit pointers
84 hsail64, // AMD HSAIL with 64-bit pointers
85 spir, // SPIR: standard portable IR for OpenCL 32-bit version
86 spir64, // SPIR: standard portable IR for OpenCL 64-bit version
87 kalimba, // Kalimba: generic kalimba
88 shave, // SHAVE: Movidius vector VLIW processors
89 wasm32, // WebAssembly with 32-bit pointers
90 wasm64, // WebAssembly with 64-bit pointers
124 ImaginationTechnologies,
129 LastVendorType = Myriad
150 NaCl, // Native Client
151 CNK, // BG/P Compute-Node Kernel
155 NVCL, // NVIDIA OpenCL
156 AMDHSA, // AMD HSA Runtime
160 enum EnvironmentType {
177 LastEnvironmentType = CoreCLR
179 enum ObjectFormatType {
190 /// The parsed arch type.
193 /// The parsed subarchitecture type.
196 /// The parsed vendor type.
199 /// The parsed OS type.
202 /// The parsed Environment type.
203 EnvironmentType Environment;
205 /// The object format type.
206 ObjectFormatType ObjectFormat;
209 /// @name Constructors
212 /// \brief Default constructor is the same as an empty string and leaves all
213 /// triple fields unknown.
214 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
216 explicit Triple(const Twine &Str);
217 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
218 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
219 const Twine &EnvironmentStr);
221 bool operator==(const Triple &Other) const {
222 return Arch == Other.Arch && SubArch == Other.SubArch &&
223 Vendor == Other.Vendor && OS == Other.OS &&
224 Environment == Other.Environment &&
225 ObjectFormat == Other.ObjectFormat;
229 /// @name Normalization
232 /// normalize - Turn an arbitrary machine specification into the canonical
233 /// triple form (or something sensible that the Triple class understands if
234 /// nothing better can reasonably be done). In particular, it handles the
235 /// common case in which otherwise valid components are in the wrong order.
236 static std::string normalize(StringRef Str);
238 /// \brief Return the normalized form of this triple's string.
239 std::string normalize() const { return normalize(Data); }
242 /// @name Typed Component Access
245 /// getArch - Get the parsed architecture type of this triple.
246 ArchType getArch() const { return Arch; }
248 /// getSubArch - get the parsed subarchitecture type for this triple.
249 SubArchType getSubArch() const { return SubArch; }
251 /// getVendor - Get the parsed vendor type of this triple.
252 VendorType getVendor() const { return Vendor; }
254 /// getOS - Get the parsed operating system type of this triple.
255 OSType getOS() const { return OS; }
257 /// hasEnvironment - Does this triple have the optional environment
258 /// (fourth) component?
259 bool hasEnvironment() const {
260 return getEnvironmentName() != "";
263 /// getEnvironment - Get the parsed environment type of this triple.
264 EnvironmentType getEnvironment() const { return Environment; }
266 /// \brief Parse the version number from the OS name component of the
267 /// triple, if present.
269 /// For example, "fooos1.2.3" would return (1, 2, 3).
271 /// If an entry is not defined, it will be returned as 0.
272 void getEnvironmentVersion(unsigned &Major, unsigned &Minor,
273 unsigned &Micro) const;
275 /// getFormat - Get the object format for this triple.
276 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
278 /// getOSVersion - Parse the version number from the OS name component of the
279 /// triple, if present.
281 /// For example, "fooos1.2.3" would return (1, 2, 3).
283 /// If an entry is not defined, it will be returned as 0.
284 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
286 /// getOSMajorVersion - Return just the major version number, this is
287 /// specialized because it is a common query.
288 unsigned getOSMajorVersion() const {
289 unsigned Maj, Min, Micro;
290 getOSVersion(Maj, Min, Micro);
294 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
295 /// translate generic "darwin" versions to the corresponding OS X versions.
296 /// This may also be called with IOS triples but the OS X version number is
297 /// just set to a constant 10.4.0 in that case. Returns true if successful.
298 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
299 unsigned &Micro) const;
301 /// getiOSVersion - Parse the version number as with getOSVersion. This should
302 /// only be called with IOS triples.
303 void getiOSVersion(unsigned &Major, unsigned &Minor,
304 unsigned &Micro) const;
307 /// @name Direct Component Access
310 const std::string &str() const { return Data; }
312 const std::string &getTriple() const { return Data; }
314 /// getArchName - Get the architecture (first) component of the
316 StringRef getArchName() const;
318 /// getVendorName - Get the vendor (second) component of the triple.
319 StringRef getVendorName() const;
321 /// getOSName - Get the operating system (third) component of the
323 StringRef getOSName() const;
325 /// getEnvironmentName - Get the optional environment (fourth)
326 /// component of the triple, or "" if empty.
327 StringRef getEnvironmentName() const;
329 /// getOSAndEnvironmentName - Get the operating system and optional
330 /// environment components as a single string (separated by a '-'
331 /// if the environment component is present).
332 StringRef getOSAndEnvironmentName() const;
335 /// @name Convenience Predicates
338 /// \brief Test whether the architecture is 64-bit
340 /// Note that this tests for 64-bit pointer width, and nothing else. Note
341 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
342 /// 16-bit. The inner details of pointer width for particular architectures
343 /// is not summed up in the triple, and so only a coarse grained predicate
344 /// system is provided.
345 bool isArch64Bit() const;
347 /// \brief Test whether the architecture is 32-bit
349 /// Note that this tests for 32-bit pointer width, and nothing else.
350 bool isArch32Bit() const;
352 /// \brief Test whether the architecture is 16-bit
354 /// Note that this tests for 16-bit pointer width, and nothing else.
355 bool isArch16Bit() const;
357 /// isOSVersionLT - Helper function for doing comparisons against version
358 /// numbers included in the target triple.
359 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
360 unsigned Micro = 0) const {
362 getOSVersion(LHS[0], LHS[1], LHS[2]);
365 return LHS[0] < Major;
367 return LHS[1] < Minor;
369 return LHS[1] < Micro;
374 bool isOSVersionLT(const Triple &Other) const {
376 Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
377 return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
380 /// isMacOSXVersionLT - Comparison function for checking OS X version
381 /// compatibility, which handles supporting skewed version numbering schemes
382 /// used by the "darwin" triples.
383 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
384 unsigned Micro = 0) const {
385 assert(isMacOSX() && "Not an OS X triple!");
387 // If this is OS X, expect a sane version number.
388 if (getOS() == Triple::MacOSX)
389 return isOSVersionLT(Major, Minor, Micro);
391 // Otherwise, compare to the "Darwin" number.
392 assert(Major == 10 && "Unexpected major version");
393 return isOSVersionLT(Minor + 4, Micro, 0);
396 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
397 /// "darwin" and "osx" as OS X triples.
398 bool isMacOSX() const {
399 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
402 /// Is this an iOS triple.
404 return getOS() == Triple::IOS;
407 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
408 bool isOSDarwin() const {
409 return isMacOSX() || isiOS();
412 bool isOSNetBSD() const {
413 return getOS() == Triple::NetBSD;
416 bool isOSOpenBSD() const {
417 return getOS() == Triple::OpenBSD;
420 bool isOSFreeBSD() const {
421 return getOS() == Triple::FreeBSD;
424 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
426 bool isOSSolaris() const {
427 return getOS() == Triple::Solaris;
430 bool isOSBitrig() const {
431 return getOS() == Triple::Bitrig;
434 bool isWindowsMSVCEnvironment() const {
435 return getOS() == Triple::Win32 &&
436 (getEnvironment() == Triple::UnknownEnvironment ||
437 getEnvironment() == Triple::MSVC);
440 bool isKnownWindowsMSVCEnvironment() const {
441 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
444 bool isWindowsCoreCLREnvironment() const {
445 return getOS() == Triple::Win32 && getEnvironment() == Triple::CoreCLR;
448 bool isWindowsItaniumEnvironment() const {
449 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
452 bool isWindowsCygwinEnvironment() const {
453 return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
456 bool isWindowsGNUEnvironment() const {
457 return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
460 /// \brief Tests for either Cygwin or MinGW OS
461 bool isOSCygMing() const {
462 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
465 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
466 bool isOSMSVCRT() const {
467 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
468 isWindowsItaniumEnvironment();
471 /// \brief Tests whether the OS is Windows.
472 bool isOSWindows() const {
473 return getOS() == Triple::Win32;
476 /// \brief Tests whether the OS is NaCl (Native Client)
477 bool isOSNaCl() const {
478 return getOS() == Triple::NaCl;
481 /// \brief Tests whether the OS is Linux.
482 bool isOSLinux() const {
483 return getOS() == Triple::Linux;
486 /// \brief Tests whether the OS uses the ELF binary format.
487 bool isOSBinFormatELF() const {
488 return getObjectFormat() == Triple::ELF;
491 /// \brief Tests whether the OS uses the COFF binary format.
492 bool isOSBinFormatCOFF() const {
493 return getObjectFormat() == Triple::COFF;
496 /// \brief Tests whether the environment is MachO.
497 bool isOSBinFormatMachO() const {
498 return getObjectFormat() == Triple::MachO;
501 /// \brief Tests whether the target is the PS4 CPU
502 bool isPS4CPU() const {
503 return getArch() == Triple::x86_64 &&
504 getVendor() == Triple::SCEI &&
505 getOS() == Triple::PS4;
508 /// \brief Tests whether the target is the PS4 platform
510 return getVendor() == Triple::SCEI &&
511 getOS() == Triple::PS4;
514 /// \brief Tests whether the target is Android
515 bool isAndroid() const { return getEnvironment() == Triple::Android; }
521 /// setArch - Set the architecture (first) component of the triple
523 void setArch(ArchType Kind);
525 /// setVendor - Set the vendor (second) component of the triple to a
527 void setVendor(VendorType Kind);
529 /// setOS - Set the operating system (third) component of the triple
531 void setOS(OSType Kind);
533 /// setEnvironment - Set the environment (fourth) component of the triple
535 void setEnvironment(EnvironmentType Kind);
537 /// setObjectFormat - Set the object file format
538 void setObjectFormat(ObjectFormatType Kind);
540 /// setTriple - Set all components to the new triple \p Str.
541 void setTriple(const Twine &Str);
543 /// setArchName - Set the architecture (first) component of the
545 void setArchName(StringRef Str);
547 /// setVendorName - Set the vendor (second) component of the triple
549 void setVendorName(StringRef Str);
551 /// setOSName - Set the operating system (third) component of the
553 void setOSName(StringRef Str);
555 /// setEnvironmentName - Set the optional environment (fourth)
556 /// component of the triple by name.
557 void setEnvironmentName(StringRef Str);
559 /// setOSAndEnvironmentName - Set the operating system and optional
560 /// environment components with a single string.
561 void setOSAndEnvironmentName(StringRef Str);
564 /// @name Helpers to build variants of a particular triple.
567 /// \brief Form a triple with a 32-bit variant of the current architecture.
569 /// This can be used to move across "families" of architectures where useful.
571 /// \returns A new triple with a 32-bit architecture or an unknown
572 /// architecture if no such variant can be found.
573 llvm::Triple get32BitArchVariant() const;
575 /// \brief Form a triple with a 64-bit variant of the current architecture.
577 /// This can be used to move across "families" of architectures where useful.
579 /// \returns A new triple with a 64-bit architecture or an unknown
580 /// architecture if no such variant can be found.
581 llvm::Triple get64BitArchVariant() const;
583 /// Form a triple with a big endian variant of the current architecture.
585 /// This can be used to move across "families" of architectures where useful.
587 /// \returns A new triple with a big endian architecture or an unknown
588 /// architecture if no such variant can be found.
589 llvm::Triple getBigEndianArchVariant() const;
591 /// Form a triple with a little endian variant of the current architecture.
593 /// This can be used to move across "families" of architectures where useful.
595 /// \returns A new triple with a little endian architecture or an unknown
596 /// architecture if no such variant can be found.
597 llvm::Triple getLittleEndianArchVariant() const;
599 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
601 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
602 /// string then the triple's arch name is used.
603 StringRef getARMCPUForArch(StringRef Arch = StringRef()) const;
606 /// @name Static helpers for IDs.
609 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
610 static const char *getArchTypeName(ArchType Kind);
612 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
613 /// architecture. This is the prefix used by the architecture specific
614 /// builtins, and is suitable for passing to \see
615 /// Intrinsic::getIntrinsicForGCCBuiltin().
617 /// \return - The architecture prefix, or 0 if none is defined.
618 static const char *getArchTypePrefix(ArchType Kind);
620 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
621 static const char *getVendorTypeName(VendorType Kind);
623 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
624 static const char *getOSTypeName(OSType Kind);
626 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
628 static const char *getEnvironmentTypeName(EnvironmentType Kind);
631 /// @name Static helpers for converting alternate architecture names.
634 /// getArchTypeForLLVMName - The canonical type for the given LLVM
635 /// architecture name (e.g., "x86").
636 static ArchType getArchTypeForLLVMName(StringRef Str);
641 } // End llvm namespace