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 hexagon, // Hexagon: hexagon
54 mips, // MIPS: mips, mipsallegrex
55 mipsel, // MIPSEL: mipsel, mipsallegrexel
56 mips64, // MIPS64: mips64
57 mips64el, // MIPS64EL: mips64el
58 msp430, // MSP430: msp430
60 ppc64, // PPC64: powerpc64, ppu
61 ppc64le, // PPC64LE: powerpc64le
62 r600, // R600: AMD GPUs HD2XXX - HD6XXX
63 amdgcn, // AMDGCN: AMD GCN GPUs
64 sparc, // Sparc: sparc
65 sparcv9, // Sparcv9: Sparcv9
66 systemz, // SystemZ: s390x
67 tce, // TCE (http://tce.cs.tut.fi/): tce
68 thumb, // Thumb (little endian): thumb, thumbv.*
69 thumbeb, // Thumb (big endian): thumbeb
71 x86_64, // X86-64: amd64, x86_64
72 xcore, // XCore: xcore
73 nvptx, // NVPTX: 32-bit
74 nvptx64, // NVPTX: 64-bit
75 le32, // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
76 le64, // le64: generic little-endian 64-bit CPU (PNaCl / Emscripten)
78 amdil64, // AMDIL with 64-bit pointers
80 hsail64, // AMD HSAIL with 64-bit pointers
81 spir, // SPIR: standard portable IR for OpenCL 32-bit version
82 spir64, // SPIR: standard portable IR for OpenCL 64-bit version
83 kalimba // Kalimba: generic kalimba
114 ImaginationTechnologies,
137 NaCl, // Native Client
138 CNK, // BG/P Compute-Node Kernel
142 NVCL, // NVIDIA OpenCL
143 AMDHSA // AMD HSA Runtime
145 enum EnvironmentType {
161 enum ObjectFormatType {
172 /// The parsed arch type.
175 /// The parsed subarchitecture type.
178 /// The parsed vendor type.
181 /// The parsed OS type.
184 /// The parsed Environment type.
185 EnvironmentType Environment;
187 /// The object format type.
188 ObjectFormatType ObjectFormat;
191 /// @name Constructors
194 /// \brief Default constructor is the same as an empty string and leaves all
195 /// triple fields unknown.
196 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
198 explicit Triple(const Twine &Str);
199 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
200 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
201 const Twine &EnvironmentStr);
204 /// @name Normalization
207 /// normalize - Turn an arbitrary machine specification into the canonical
208 /// triple form (or something sensible that the Triple class understands if
209 /// nothing better can reasonably be done). In particular, it handles the
210 /// common case in which otherwise valid components are in the wrong order.
211 static std::string normalize(StringRef Str);
214 /// @name Typed Component Access
217 /// getArch - Get the parsed architecture type of this triple.
218 ArchType getArch() const { return Arch; }
220 /// getSubArch - get the parsed subarchitecture type for this triple.
221 SubArchType getSubArch() const { return SubArch; }
223 /// getVendor - Get the parsed vendor type of this triple.
224 VendorType getVendor() const { return Vendor; }
226 /// getOS - Get the parsed operating system type of this triple.
227 OSType getOS() const { return OS; }
229 /// hasEnvironment - Does this triple have the optional environment
230 /// (fourth) component?
231 bool hasEnvironment() const {
232 return getEnvironmentName() != "";
235 /// getEnvironment - Get the parsed environment type of this triple.
236 EnvironmentType getEnvironment() const { return Environment; }
238 /// getFormat - Get the object format for this triple.
239 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
241 /// getOSVersion - Parse the version number from the OS name component of the
242 /// triple, if present.
244 /// For example, "fooos1.2.3" would return (1, 2, 3).
246 /// If an entry is not defined, it will be returned as 0.
247 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
249 /// getOSMajorVersion - Return just the major version number, this is
250 /// specialized because it is a common query.
251 unsigned getOSMajorVersion() const {
252 unsigned Maj, Min, Micro;
253 getOSVersion(Maj, Min, Micro);
257 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
258 /// translate generic "darwin" versions to the corresponding OS X versions.
259 /// This may also be called with IOS triples but the OS X version number is
260 /// just set to a constant 10.4.0 in that case. Returns true if successful.
261 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
262 unsigned &Micro) const;
264 /// getiOSVersion - Parse the version number as with getOSVersion. This should
265 /// only be called with IOS triples.
266 void getiOSVersion(unsigned &Major, unsigned &Minor,
267 unsigned &Micro) const;
270 /// @name Direct Component Access
273 const std::string &str() const { return Data; }
275 const std::string &getTriple() const { return Data; }
277 /// getArchName - Get the architecture (first) component of the
279 StringRef getArchName() const;
281 /// getVendorName - Get the vendor (second) component of the triple.
282 StringRef getVendorName() const;
284 /// getOSName - Get the operating system (third) component of the
286 StringRef getOSName() const;
288 /// getEnvironmentName - Get the optional environment (fourth)
289 /// component of the triple, or "" if empty.
290 StringRef getEnvironmentName() const;
292 /// getOSAndEnvironmentName - Get the operating system and optional
293 /// environment components as a single string (separated by a '-'
294 /// if the environment component is present).
295 StringRef getOSAndEnvironmentName() const;
298 /// @name Convenience Predicates
301 /// \brief Test whether the architecture is 64-bit
303 /// Note that this tests for 64-bit pointer width, and nothing else. Note
304 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
305 /// 16-bit. The inner details of pointer width for particular architectures
306 /// is not summed up in the triple, and so only a coarse grained predicate
307 /// system is provided.
308 bool isArch64Bit() const;
310 /// \brief Test whether the architecture is 32-bit
312 /// Note that this tests for 32-bit pointer width, and nothing else.
313 bool isArch32Bit() const;
315 /// \brief Test whether the architecture is 16-bit
317 /// Note that this tests for 16-bit pointer width, and nothing else.
318 bool isArch16Bit() const;
320 /// isOSVersionLT - Helper function for doing comparisons against version
321 /// numbers included in the target triple.
322 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
323 unsigned Micro = 0) const {
325 getOSVersion(LHS[0], LHS[1], LHS[2]);
328 return LHS[0] < Major;
330 return LHS[1] < Minor;
332 return LHS[1] < Micro;
337 /// isMacOSXVersionLT - Comparison function for checking OS X version
338 /// compatibility, which handles supporting skewed version numbering schemes
339 /// used by the "darwin" triples.
340 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
341 unsigned Micro = 0) const {
342 assert(isMacOSX() && "Not an OS X triple!");
344 // If this is OS X, expect a sane version number.
345 if (getOS() == Triple::MacOSX)
346 return isOSVersionLT(Major, Minor, Micro);
348 // Otherwise, compare to the "Darwin" number.
349 assert(Major == 10 && "Unexpected major version");
350 return isOSVersionLT(Minor + 4, Micro, 0);
353 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
354 /// "darwin" and "osx" as OS X triples.
355 bool isMacOSX() const {
356 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
359 /// Is this an iOS triple.
361 return getOS() == Triple::IOS;
364 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
365 bool isOSDarwin() const {
366 return isMacOSX() || isiOS();
369 bool isOSNetBSD() const {
370 return getOS() == Triple::NetBSD;
373 bool isOSOpenBSD() const {
374 return getOS() == Triple::OpenBSD;
377 bool isOSFreeBSD() const {
378 return getOS() == Triple::FreeBSD;
381 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
383 bool isOSSolaris() const {
384 return getOS() == Triple::Solaris;
387 bool isOSBitrig() const {
388 return getOS() == Triple::Bitrig;
391 bool isWindowsMSVCEnvironment() const {
392 return getOS() == Triple::Win32 &&
393 (getEnvironment() == Triple::UnknownEnvironment ||
394 getEnvironment() == Triple::MSVC);
397 bool isKnownWindowsMSVCEnvironment() const {
398 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
401 bool isWindowsItaniumEnvironment() const {
402 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
405 bool isWindowsCygwinEnvironment() const {
406 return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
409 bool isWindowsGNUEnvironment() const {
410 return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
413 /// \brief Tests for either Cygwin or MinGW OS
414 bool isOSCygMing() const {
415 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
418 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
419 bool isOSMSVCRT() const {
420 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
421 isWindowsItaniumEnvironment();
424 /// \brief Tests whether the OS is Windows.
425 bool isOSWindows() const {
426 return getOS() == Triple::Win32 || isOSCygMing();
429 /// \brief Tests whether the OS is NaCl (Native Client)
430 bool isOSNaCl() const {
431 return getOS() == Triple::NaCl;
434 /// \brief Tests whether the OS is Linux.
435 bool isOSLinux() const {
436 return getOS() == Triple::Linux;
439 /// \brief Tests whether the OS uses the ELF binary format.
440 bool isOSBinFormatELF() const {
441 return getObjectFormat() == Triple::ELF;
444 /// \brief Tests whether the OS uses the COFF binary format.
445 bool isOSBinFormatCOFF() const {
446 return getObjectFormat() == Triple::COFF;
449 /// \brief Tests whether the environment is MachO.
450 bool isOSBinFormatMachO() const {
451 return getObjectFormat() == Triple::MachO;
458 /// setArch - Set the architecture (first) component of the triple
460 void setArch(ArchType Kind);
462 /// setVendor - Set the vendor (second) component of the triple to a
464 void setVendor(VendorType Kind);
466 /// setOS - Set the operating system (third) component of the triple
468 void setOS(OSType Kind);
470 /// setEnvironment - Set the environment (fourth) component of the triple
472 void setEnvironment(EnvironmentType Kind);
474 /// setObjectFormat - Set the object file format
475 void setObjectFormat(ObjectFormatType Kind);
477 /// setTriple - Set all components to the new triple \p Str.
478 void setTriple(const Twine &Str);
480 /// setArchName - Set the architecture (first) component of the
482 void setArchName(StringRef Str);
484 /// setVendorName - Set the vendor (second) component of the triple
486 void setVendorName(StringRef Str);
488 /// setOSName - Set the operating system (third) component of the
490 void setOSName(StringRef Str);
492 /// setEnvironmentName - Set the optional environment (fourth)
493 /// component of the triple by name.
494 void setEnvironmentName(StringRef Str);
496 /// setOSAndEnvironmentName - Set the operating system and optional
497 /// environment components with a single string.
498 void setOSAndEnvironmentName(StringRef Str);
501 /// @name Helpers to build variants of a particular triple.
504 /// \brief Form a triple with a 32-bit variant of the current architecture.
506 /// This can be used to move across "families" of architectures where useful.
508 /// \returns A new triple with a 32-bit architecture or an unknown
509 /// architecture if no such variant can be found.
510 llvm::Triple get32BitArchVariant() const;
512 /// \brief Form a triple with a 64-bit variant of the current architecture.
514 /// This can be used to move across "families" of architectures where useful.
516 /// \returns A new triple with a 64-bit architecture or an unknown
517 /// architecture if no such variant can be found.
518 llvm::Triple get64BitArchVariant() const;
520 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
522 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
523 /// string then the triple's arch name is used.
524 const char* getARMCPUForArch(StringRef Arch = StringRef()) const;
527 /// @name Static helpers for IDs.
530 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
531 static const char *getArchTypeName(ArchType Kind);
533 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
534 /// architecture. This is the prefix used by the architecture specific
535 /// builtins, and is suitable for passing to \see
536 /// Intrinsic::getIntrinsicForGCCBuiltin().
538 /// \return - The architecture prefix, or 0 if none is defined.
539 static const char *getArchTypePrefix(ArchType Kind);
541 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
542 static const char *getVendorTypeName(VendorType Kind);
544 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
545 static const char *getOSTypeName(OSType Kind);
547 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
549 static const char *getEnvironmentTypeName(EnvironmentType Kind);
552 /// @name Static helpers for converting alternate architecture names.
555 /// getArchTypeForLLVMName - The canonical type for the given LLVM
556 /// architecture name (e.g., "x86").
557 static ArchType getArchTypeForLLVMName(StringRef Str);
562 } // End llvm namespace