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
115 ImaginationTechnologies,
139 NaCl, // Native Client
140 CNK, // BG/P Compute-Node Kernel
144 NVCL, // NVIDIA OpenCL
145 AMDHSA, // AMD HSA Runtime
148 enum EnvironmentType {
164 enum ObjectFormatType {
175 /// The parsed arch type.
178 /// The parsed subarchitecture type.
181 /// The parsed vendor type.
184 /// The parsed OS type.
187 /// The parsed Environment type.
188 EnvironmentType Environment;
190 /// The object format type.
191 ObjectFormatType ObjectFormat;
194 /// @name Constructors
197 /// \brief Default constructor is the same as an empty string and leaves all
198 /// triple fields unknown.
199 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
201 explicit Triple(const Twine &Str);
202 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
203 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
204 const Twine &EnvironmentStr);
206 bool operator==(const Triple &Other) const {
207 return Arch == Other.Arch && SubArch == Other.SubArch &&
208 Vendor == Other.Vendor && OS == Other.OS &&
209 Environment == Other.Environment &&
210 ObjectFormat == Other.ObjectFormat;
214 /// @name Normalization
217 /// normalize - Turn an arbitrary machine specification into the canonical
218 /// triple form (or something sensible that the Triple class understands if
219 /// nothing better can reasonably be done). In particular, it handles the
220 /// common case in which otherwise valid components are in the wrong order.
221 static std::string normalize(StringRef Str);
223 /// \brief Return the normalized form of this triple's string.
224 std::string normalize() const { return normalize(Data); }
227 /// @name Typed Component Access
230 /// getArch - Get the parsed architecture type of this triple.
231 ArchType getArch() const { return Arch; }
233 /// getSubArch - get the parsed subarchitecture type for this triple.
234 SubArchType getSubArch() const { return SubArch; }
236 /// getVendor - Get the parsed vendor type of this triple.
237 VendorType getVendor() const { return Vendor; }
239 /// getOS - Get the parsed operating system type of this triple.
240 OSType getOS() const { return OS; }
242 /// hasEnvironment - Does this triple have the optional environment
243 /// (fourth) component?
244 bool hasEnvironment() const {
245 return getEnvironmentName() != "";
248 /// getEnvironment - Get the parsed environment type of this triple.
249 EnvironmentType getEnvironment() const { return Environment; }
251 /// getFormat - Get the object format for this triple.
252 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
254 /// getOSVersion - Parse the version number from the OS name component of the
255 /// triple, if present.
257 /// For example, "fooos1.2.3" would return (1, 2, 3).
259 /// If an entry is not defined, it will be returned as 0.
260 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
262 /// getOSMajorVersion - Return just the major version number, this is
263 /// specialized because it is a common query.
264 unsigned getOSMajorVersion() const {
265 unsigned Maj, Min, Micro;
266 getOSVersion(Maj, Min, Micro);
270 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
271 /// translate generic "darwin" versions to the corresponding OS X versions.
272 /// This may also be called with IOS triples but the OS X version number is
273 /// just set to a constant 10.4.0 in that case. Returns true if successful.
274 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
275 unsigned &Micro) const;
277 /// getiOSVersion - Parse the version number as with getOSVersion. This should
278 /// only be called with IOS triples.
279 void getiOSVersion(unsigned &Major, unsigned &Minor,
280 unsigned &Micro) const;
283 /// @name Direct Component Access
286 const std::string &str() const { return Data; }
288 const std::string &getTriple() const { return Data; }
290 /// getArchName - Get the architecture (first) component of the
292 StringRef getArchName() const;
294 /// getVendorName - Get the vendor (second) component of the triple.
295 StringRef getVendorName() const;
297 /// getOSName - Get the operating system (third) component of the
299 StringRef getOSName() const;
301 /// getEnvironmentName - Get the optional environment (fourth)
302 /// component of the triple, or "" if empty.
303 StringRef getEnvironmentName() const;
305 /// getOSAndEnvironmentName - Get the operating system and optional
306 /// environment components as a single string (separated by a '-'
307 /// if the environment component is present).
308 StringRef getOSAndEnvironmentName() const;
311 /// @name Convenience Predicates
314 /// \brief Test whether the architecture is 64-bit
316 /// Note that this tests for 64-bit pointer width, and nothing else. Note
317 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
318 /// 16-bit. The inner details of pointer width for particular architectures
319 /// is not summed up in the triple, and so only a coarse grained predicate
320 /// system is provided.
321 bool isArch64Bit() const;
323 /// \brief Test whether the architecture is 32-bit
325 /// Note that this tests for 32-bit pointer width, and nothing else.
326 bool isArch32Bit() const;
328 /// \brief Test whether the architecture is 16-bit
330 /// Note that this tests for 16-bit pointer width, and nothing else.
331 bool isArch16Bit() const;
333 /// isOSVersionLT - Helper function for doing comparisons against version
334 /// numbers included in the target triple.
335 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
336 unsigned Micro = 0) const {
338 getOSVersion(LHS[0], LHS[1], LHS[2]);
341 return LHS[0] < Major;
343 return LHS[1] < Minor;
345 return LHS[1] < Micro;
350 bool isOSVersionLT(const Triple &Other) const {
352 Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
353 return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
356 /// isMacOSXVersionLT - Comparison function for checking OS X version
357 /// compatibility, which handles supporting skewed version numbering schemes
358 /// used by the "darwin" triples.
359 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
360 unsigned Micro = 0) const {
361 assert(isMacOSX() && "Not an OS X triple!");
363 // If this is OS X, expect a sane version number.
364 if (getOS() == Triple::MacOSX)
365 return isOSVersionLT(Major, Minor, Micro);
367 // Otherwise, compare to the "Darwin" number.
368 assert(Major == 10 && "Unexpected major version");
369 return isOSVersionLT(Minor + 4, Micro, 0);
372 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
373 /// "darwin" and "osx" as OS X triples.
374 bool isMacOSX() const {
375 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
378 /// Is this an iOS triple.
380 return getOS() == Triple::IOS;
383 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
384 bool isOSDarwin() const {
385 return isMacOSX() || isiOS();
388 bool isOSNetBSD() const {
389 return getOS() == Triple::NetBSD;
392 bool isOSOpenBSD() const {
393 return getOS() == Triple::OpenBSD;
396 bool isOSFreeBSD() const {
397 return getOS() == Triple::FreeBSD;
400 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
402 bool isOSSolaris() const {
403 return getOS() == Triple::Solaris;
406 bool isOSBitrig() const {
407 return getOS() == Triple::Bitrig;
410 bool isWindowsMSVCEnvironment() const {
411 return getOS() == Triple::Win32 &&
412 (getEnvironment() == Triple::UnknownEnvironment ||
413 getEnvironment() == Triple::MSVC);
416 bool isKnownWindowsMSVCEnvironment() const {
417 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
420 bool isWindowsItaniumEnvironment() const {
421 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
424 bool isWindowsCygwinEnvironment() const {
425 return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
428 bool isWindowsGNUEnvironment() const {
429 return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
432 /// \brief Tests for either Cygwin or MinGW OS
433 bool isOSCygMing() const {
434 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
437 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
438 bool isOSMSVCRT() const {
439 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
440 isWindowsItaniumEnvironment();
443 /// \brief Tests whether the OS is Windows.
444 bool isOSWindows() const {
445 return getOS() == Triple::Win32;
448 /// \brief Tests whether the OS is NaCl (Native Client)
449 bool isOSNaCl() const {
450 return getOS() == Triple::NaCl;
453 /// \brief Tests whether the OS is Linux.
454 bool isOSLinux() const {
455 return getOS() == Triple::Linux;
458 /// \brief Tests whether the OS uses the ELF binary format.
459 bool isOSBinFormatELF() const {
460 return getObjectFormat() == Triple::ELF;
463 /// \brief Tests whether the OS uses the COFF binary format.
464 bool isOSBinFormatCOFF() const {
465 return getObjectFormat() == Triple::COFF;
468 /// \brief Tests whether the environment is MachO.
469 bool isOSBinFormatMachO() const {
470 return getObjectFormat() == Triple::MachO;
473 /// \brief Tests whether the target is the PS4 CPU
474 bool isPS4CPU() const {
475 return getArch() == Triple::x86_64 &&
476 getVendor() == Triple::SCEI &&
477 getOS() == Triple::PS4;
480 /// \brief Tests whether the target is the PS4 platform
482 return getVendor() == Triple::SCEI &&
483 getOS() == Triple::PS4;
490 /// setArch - Set the architecture (first) component of the triple
492 void setArch(ArchType Kind);
494 /// setVendor - Set the vendor (second) component of the triple to a
496 void setVendor(VendorType Kind);
498 /// setOS - Set the operating system (third) component of the triple
500 void setOS(OSType Kind);
502 /// setEnvironment - Set the environment (fourth) component of the triple
504 void setEnvironment(EnvironmentType Kind);
506 /// setObjectFormat - Set the object file format
507 void setObjectFormat(ObjectFormatType Kind);
509 /// setTriple - Set all components to the new triple \p Str.
510 void setTriple(const Twine &Str);
512 /// setArchName - Set the architecture (first) component of the
514 void setArchName(StringRef Str);
516 /// setVendorName - Set the vendor (second) component of the triple
518 void setVendorName(StringRef Str);
520 /// setOSName - Set the operating system (third) component of the
522 void setOSName(StringRef Str);
524 /// setEnvironmentName - Set the optional environment (fourth)
525 /// component of the triple by name.
526 void setEnvironmentName(StringRef Str);
528 /// setOSAndEnvironmentName - Set the operating system and optional
529 /// environment components with a single string.
530 void setOSAndEnvironmentName(StringRef Str);
533 /// @name Helpers to build variants of a particular triple.
536 /// \brief Form a triple with a 32-bit variant of the current architecture.
538 /// This can be used to move across "families" of architectures where useful.
540 /// \returns A new triple with a 32-bit architecture or an unknown
541 /// architecture if no such variant can be found.
542 llvm::Triple get32BitArchVariant() const;
544 /// \brief Form a triple with a 64-bit variant of the current architecture.
546 /// This can be used to move across "families" of architectures where useful.
548 /// \returns A new triple with a 64-bit architecture or an unknown
549 /// architecture if no such variant can be found.
550 llvm::Triple get64BitArchVariant() const;
552 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
554 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
555 /// string then the triple's arch name is used.
556 const char* getARMCPUForArch(StringRef Arch = StringRef()) const;
559 /// @name Static helpers for IDs.
562 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
563 static const char *getArchTypeName(ArchType Kind);
565 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
566 /// architecture. This is the prefix used by the architecture specific
567 /// builtins, and is suitable for passing to \see
568 /// Intrinsic::getIntrinsicForGCCBuiltin().
570 /// \return - The architecture prefix, or 0 if none is defined.
571 static const char *getArchTypePrefix(ArchType Kind);
573 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
574 static const char *getVendorTypeName(VendorType Kind);
576 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
577 static const char *getOSTypeName(OSType Kind);
579 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
581 static const char *getEnvironmentTypeName(EnvironmentType Kind);
584 /// @name Static helpers for converting alternate architecture names.
587 /// getArchTypeForLLVMName - The canonical type for the given LLVM
588 /// architecture name (e.g., "x86").
589 static ArchType getArchTypeForLLVMName(StringRef Str);
594 } // End llvm namespace