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,
138 NaCl, // Native Client
139 CNK, // BG/P Compute-Node Kernel
143 NVCL, // NVIDIA OpenCL
144 AMDHSA, // AMD HSA Runtime
147 enum EnvironmentType {
163 enum ObjectFormatType {
174 /// The parsed arch type.
177 /// The parsed subarchitecture type.
180 /// The parsed vendor type.
183 /// The parsed OS type.
186 /// The parsed Environment type.
187 EnvironmentType Environment;
189 /// The object format type.
190 ObjectFormatType ObjectFormat;
193 /// @name Constructors
196 /// \brief Default constructor is the same as an empty string and leaves all
197 /// triple fields unknown.
198 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
200 explicit Triple(const Twine &Str);
201 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
202 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
203 const Twine &EnvironmentStr);
206 /// @name Normalization
209 /// normalize - Turn an arbitrary machine specification into the canonical
210 /// triple form (or something sensible that the Triple class understands if
211 /// nothing better can reasonably be done). In particular, it handles the
212 /// common case in which otherwise valid components are in the wrong order.
213 static std::string normalize(StringRef Str);
215 /// \brief Return the normalized form of this triple's string.
216 std::string normalize() const { return normalize(Data); }
219 /// @name Typed Component Access
222 /// getArch - Get the parsed architecture type of this triple.
223 ArchType getArch() const { return Arch; }
225 /// getSubArch - get the parsed subarchitecture type for this triple.
226 SubArchType getSubArch() const { return SubArch; }
228 /// getVendor - Get the parsed vendor type of this triple.
229 VendorType getVendor() const { return Vendor; }
231 /// getOS - Get the parsed operating system type of this triple.
232 OSType getOS() const { return OS; }
234 /// hasEnvironment - Does this triple have the optional environment
235 /// (fourth) component?
236 bool hasEnvironment() const {
237 return getEnvironmentName() != "";
240 /// getEnvironment - Get the parsed environment type of this triple.
241 EnvironmentType getEnvironment() const { return Environment; }
243 /// getFormat - Get the object format for this triple.
244 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
246 /// getOSVersion - Parse the version number from the OS name component of the
247 /// triple, if present.
249 /// For example, "fooos1.2.3" would return (1, 2, 3).
251 /// If an entry is not defined, it will be returned as 0.
252 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
254 /// getOSMajorVersion - Return just the major version number, this is
255 /// specialized because it is a common query.
256 unsigned getOSMajorVersion() const {
257 unsigned Maj, Min, Micro;
258 getOSVersion(Maj, Min, Micro);
262 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
263 /// translate generic "darwin" versions to the corresponding OS X versions.
264 /// This may also be called with IOS triples but the OS X version number is
265 /// just set to a constant 10.4.0 in that case. Returns true if successful.
266 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
267 unsigned &Micro) const;
269 /// getiOSVersion - Parse the version number as with getOSVersion. This should
270 /// only be called with IOS triples.
271 void getiOSVersion(unsigned &Major, unsigned &Minor,
272 unsigned &Micro) const;
275 /// @name Direct Component Access
278 const std::string &str() const { return Data; }
280 const std::string &getTriple() const { return Data; }
282 /// getArchName - Get the architecture (first) component of the
284 StringRef getArchName() const;
286 /// getVendorName - Get the vendor (second) component of the triple.
287 StringRef getVendorName() const;
289 /// getOSName - Get the operating system (third) component of the
291 StringRef getOSName() const;
293 /// getEnvironmentName - Get the optional environment (fourth)
294 /// component of the triple, or "" if empty.
295 StringRef getEnvironmentName() const;
297 /// getOSAndEnvironmentName - Get the operating system and optional
298 /// environment components as a single string (separated by a '-'
299 /// if the environment component is present).
300 StringRef getOSAndEnvironmentName() const;
303 /// @name Convenience Predicates
306 /// \brief Test whether the architecture is 64-bit
308 /// Note that this tests for 64-bit pointer width, and nothing else. Note
309 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
310 /// 16-bit. The inner details of pointer width for particular architectures
311 /// is not summed up in the triple, and so only a coarse grained predicate
312 /// system is provided.
313 bool isArch64Bit() const;
315 /// \brief Test whether the architecture is 32-bit
317 /// Note that this tests for 32-bit pointer width, and nothing else.
318 bool isArch32Bit() const;
320 /// \brief Test whether the architecture is 16-bit
322 /// Note that this tests for 16-bit pointer width, and nothing else.
323 bool isArch16Bit() const;
325 /// isOSVersionLT - Helper function for doing comparisons against version
326 /// numbers included in the target triple.
327 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
328 unsigned Micro = 0) const {
330 getOSVersion(LHS[0], LHS[1], LHS[2]);
333 return LHS[0] < Major;
335 return LHS[1] < Minor;
337 return LHS[1] < Micro;
342 /// isMacOSXVersionLT - Comparison function for checking OS X version
343 /// compatibility, which handles supporting skewed version numbering schemes
344 /// used by the "darwin" triples.
345 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
346 unsigned Micro = 0) const {
347 assert(isMacOSX() && "Not an OS X triple!");
349 // If this is OS X, expect a sane version number.
350 if (getOS() == Triple::MacOSX)
351 return isOSVersionLT(Major, Minor, Micro);
353 // Otherwise, compare to the "Darwin" number.
354 assert(Major == 10 && "Unexpected major version");
355 return isOSVersionLT(Minor + 4, Micro, 0);
358 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
359 /// "darwin" and "osx" as OS X triples.
360 bool isMacOSX() const {
361 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
364 /// Is this an iOS triple.
366 return getOS() == Triple::IOS;
369 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
370 bool isOSDarwin() const {
371 return isMacOSX() || isiOS();
374 bool isOSNetBSD() const {
375 return getOS() == Triple::NetBSD;
378 bool isOSOpenBSD() const {
379 return getOS() == Triple::OpenBSD;
382 bool isOSFreeBSD() const {
383 return getOS() == Triple::FreeBSD;
386 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
388 bool isOSSolaris() const {
389 return getOS() == Triple::Solaris;
392 bool isOSBitrig() const {
393 return getOS() == Triple::Bitrig;
396 bool isWindowsMSVCEnvironment() const {
397 return getOS() == Triple::Win32 &&
398 (getEnvironment() == Triple::UnknownEnvironment ||
399 getEnvironment() == Triple::MSVC);
402 bool isKnownWindowsMSVCEnvironment() const {
403 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
406 bool isWindowsItaniumEnvironment() const {
407 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
410 bool isWindowsCygwinEnvironment() const {
411 return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
414 bool isWindowsGNUEnvironment() const {
415 return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
418 /// \brief Tests for either Cygwin or MinGW OS
419 bool isOSCygMing() const {
420 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
423 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
424 bool isOSMSVCRT() const {
425 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
426 isWindowsItaniumEnvironment();
429 /// \brief Tests whether the OS is Windows.
430 bool isOSWindows() const {
431 return getOS() == Triple::Win32 || isOSCygMing();
434 /// \brief Tests whether the OS is NaCl (Native Client)
435 bool isOSNaCl() const {
436 return getOS() == Triple::NaCl;
439 /// \brief Tests whether the OS is Linux.
440 bool isOSLinux() const {
441 return getOS() == Triple::Linux;
444 /// \brief Tests whether the OS uses the ELF binary format.
445 bool isOSBinFormatELF() const {
446 return getObjectFormat() == Triple::ELF;
449 /// \brief Tests whether the OS uses the COFF binary format.
450 bool isOSBinFormatCOFF() const {
451 return getObjectFormat() == Triple::COFF;
454 /// \brief Tests whether the environment is MachO.
455 bool isOSBinFormatMachO() const {
456 return getObjectFormat() == Triple::MachO;
459 /// \brief Tests whether the target is the PS4 CPU
460 bool isPS4CPU() const {
461 return getArch() == Triple::x86_64 &&
462 getVendor() == Triple::SCEI &&
463 getOS() == Triple::PS4;
466 /// \brief Tests whether the target is the PS4 platform
468 return getVendor() == Triple::SCEI &&
469 getOS() == Triple::PS4;
476 /// setArch - Set the architecture (first) component of the triple
478 void setArch(ArchType Kind);
480 /// setVendor - Set the vendor (second) component of the triple to a
482 void setVendor(VendorType Kind);
484 /// setOS - Set the operating system (third) component of the triple
486 void setOS(OSType Kind);
488 /// setEnvironment - Set the environment (fourth) component of the triple
490 void setEnvironment(EnvironmentType Kind);
492 /// setObjectFormat - Set the object file format
493 void setObjectFormat(ObjectFormatType Kind);
495 /// setTriple - Set all components to the new triple \p Str.
496 void setTriple(const Twine &Str);
498 /// setArchName - Set the architecture (first) component of the
500 void setArchName(StringRef Str);
502 /// setVendorName - Set the vendor (second) component of the triple
504 void setVendorName(StringRef Str);
506 /// setOSName - Set the operating system (third) component of the
508 void setOSName(StringRef Str);
510 /// setEnvironmentName - Set the optional environment (fourth)
511 /// component of the triple by name.
512 void setEnvironmentName(StringRef Str);
514 /// setOSAndEnvironmentName - Set the operating system and optional
515 /// environment components with a single string.
516 void setOSAndEnvironmentName(StringRef Str);
519 /// @name Helpers to build variants of a particular triple.
522 /// \brief Form a triple with a 32-bit variant of the current architecture.
524 /// This can be used to move across "families" of architectures where useful.
526 /// \returns A new triple with a 32-bit architecture or an unknown
527 /// architecture if no such variant can be found.
528 llvm::Triple get32BitArchVariant() const;
530 /// \brief Form a triple with a 64-bit variant of the current architecture.
532 /// This can be used to move across "families" of architectures where useful.
534 /// \returns A new triple with a 64-bit architecture or an unknown
535 /// architecture if no such variant can be found.
536 llvm::Triple get64BitArchVariant() const;
538 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
540 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
541 /// string then the triple's arch name is used.
542 const char* getARMCPUForArch(StringRef Arch = StringRef()) const;
545 /// @name Static helpers for IDs.
548 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
549 static const char *getArchTypeName(ArchType Kind);
551 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
552 /// architecture. This is the prefix used by the architecture specific
553 /// builtins, and is suitable for passing to \see
554 /// Intrinsic::getIntrinsicForGCCBuiltin().
556 /// \return - The architecture prefix, or 0 if none is defined.
557 static const char *getArchTypePrefix(ArchType Kind);
559 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
560 static const char *getVendorTypeName(VendorType Kind);
562 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
563 static const char *getOSTypeName(OSType Kind);
565 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
567 static const char *getEnvironmentTypeName(EnvironmentType Kind);
570 /// @name Static helpers for converting alternate architecture names.
573 /// getArchTypeForLLVMName - The canonical type for the given LLVM
574 /// architecture name (e.g., "x86").
575 static ArchType getArchTypeForLLVMName(StringRef Str);
580 } // End llvm namespace