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 sparc, // Sparc: sparc
64 sparcv9, // Sparcv9: Sparcv9
65 systemz, // SystemZ: s390x
66 tce, // TCE (http://tce.cs.tut.fi/): tce
67 thumb, // Thumb (little endian): thumb, thumbv.*
68 thumbeb, // Thumb (big endian): thumbeb
70 x86_64, // X86-64: amd64, x86_64
71 xcore, // XCore: xcore
72 nvptx, // NVPTX: 32-bit
73 nvptx64, // NVPTX: 64-bit
74 le32, // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
75 le64, // le64: generic little-endian 64-bit CPU (PNaCl / Emscripten)
77 amdil64, // AMDIL with 64-bit pointers
79 hsail64, // AMD HSAIL with 64-bit pointers
80 spir, // SPIR: standard portable IR for OpenCL 32-bit version
81 spir64, // SPIR: standard portable IR for OpenCL 64-bit version
82 kalimba // Kalimba: generic kalimba
113 ImaginationTechnologies,
136 NaCl, // Native Client
137 CNK, // BG/P Compute-Node Kernel
141 NVCL, // NVIDIA OpenCL
142 AMDHSA // AMD HSA Runtime
144 enum EnvironmentType {
160 enum ObjectFormatType {
171 /// The parsed arch type.
174 /// The parsed subarchitecture type.
177 /// The parsed vendor type.
180 /// The parsed OS type.
183 /// The parsed Environment type.
184 EnvironmentType Environment;
186 /// The object format type.
187 ObjectFormatType ObjectFormat;
190 /// @name Constructors
193 /// \brief Default constructor is the same as an empty string and leaves all
194 /// triple fields unknown.
195 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
197 explicit Triple(const Twine &Str);
198 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
199 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
200 const Twine &EnvironmentStr);
203 /// @name Normalization
206 /// normalize - Turn an arbitrary machine specification into the canonical
207 /// triple form (or something sensible that the Triple class understands if
208 /// nothing better can reasonably be done). In particular, it handles the
209 /// common case in which otherwise valid components are in the wrong order.
210 static std::string normalize(StringRef Str);
213 /// @name Typed Component Access
216 /// getArch - Get the parsed architecture type of this triple.
217 ArchType getArch() const { return Arch; }
219 /// getSubArch - get the parsed subarchitecture type for this triple.
220 SubArchType getSubArch() const { return SubArch; }
222 /// getVendor - Get the parsed vendor type of this triple.
223 VendorType getVendor() const { return Vendor; }
225 /// getOS - Get the parsed operating system type of this triple.
226 OSType getOS() const { return OS; }
228 /// hasEnvironment - Does this triple have the optional environment
229 /// (fourth) component?
230 bool hasEnvironment() const {
231 return getEnvironmentName() != "";
234 /// getEnvironment - Get the parsed environment type of this triple.
235 EnvironmentType getEnvironment() const { return Environment; }
237 /// getFormat - Get the object format for this triple.
238 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
240 /// getOSVersion - Parse the version number from the OS name component of the
241 /// triple, if present.
243 /// For example, "fooos1.2.3" would return (1, 2, 3).
245 /// If an entry is not defined, it will be returned as 0.
246 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
248 /// getOSMajorVersion - Return just the major version number, this is
249 /// specialized because it is a common query.
250 unsigned getOSMajorVersion() const {
251 unsigned Maj, Min, Micro;
252 getOSVersion(Maj, Min, Micro);
256 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
257 /// translate generic "darwin" versions to the corresponding OS X versions.
258 /// This may also be called with IOS triples but the OS X version number is
259 /// just set to a constant 10.4.0 in that case. Returns true if successful.
260 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
261 unsigned &Micro) const;
263 /// getiOSVersion - Parse the version number as with getOSVersion. This should
264 /// only be called with IOS triples.
265 void getiOSVersion(unsigned &Major, unsigned &Minor,
266 unsigned &Micro) const;
269 /// @name Direct Component Access
272 const std::string &str() const { return Data; }
274 const std::string &getTriple() const { return Data; }
276 /// getArchName - Get the architecture (first) component of the
278 StringRef getArchName() const;
280 /// getVendorName - Get the vendor (second) component of the triple.
281 StringRef getVendorName() const;
283 /// getOSName - Get the operating system (third) component of the
285 StringRef getOSName() const;
287 /// getEnvironmentName - Get the optional environment (fourth)
288 /// component of the triple, or "" if empty.
289 StringRef getEnvironmentName() const;
291 /// getOSAndEnvironmentName - Get the operating system and optional
292 /// environment components as a single string (separated by a '-'
293 /// if the environment component is present).
294 StringRef getOSAndEnvironmentName() const;
297 /// @name Convenience Predicates
300 /// \brief Test whether the architecture is 64-bit
302 /// Note that this tests for 64-bit pointer width, and nothing else. Note
303 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
304 /// 16-bit. The inner details of pointer width for particular architectures
305 /// is not summed up in the triple, and so only a coarse grained predicate
306 /// system is provided.
307 bool isArch64Bit() const;
309 /// \brief Test whether the architecture is 32-bit
311 /// Note that this tests for 32-bit pointer width, and nothing else.
312 bool isArch32Bit() const;
314 /// \brief Test whether the architecture is 16-bit
316 /// Note that this tests for 16-bit pointer width, and nothing else.
317 bool isArch16Bit() const;
319 /// isOSVersionLT - Helper function for doing comparisons against version
320 /// numbers included in the target triple.
321 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
322 unsigned Micro = 0) const {
324 getOSVersion(LHS[0], LHS[1], LHS[2]);
327 return LHS[0] < Major;
329 return LHS[1] < Minor;
331 return LHS[1] < Micro;
336 /// isMacOSXVersionLT - Comparison function for checking OS X version
337 /// compatibility, which handles supporting skewed version numbering schemes
338 /// used by the "darwin" triples.
339 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
340 unsigned Micro = 0) const {
341 assert(isMacOSX() && "Not an OS X triple!");
343 // If this is OS X, expect a sane version number.
344 if (getOS() == Triple::MacOSX)
345 return isOSVersionLT(Major, Minor, Micro);
347 // Otherwise, compare to the "Darwin" number.
348 assert(Major == 10 && "Unexpected major version");
349 return isOSVersionLT(Minor + 4, Micro, 0);
352 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
353 /// "darwin" and "osx" as OS X triples.
354 bool isMacOSX() const {
355 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
358 /// Is this an iOS triple.
360 return getOS() == Triple::IOS;
363 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
364 bool isOSDarwin() const {
365 return isMacOSX() || isiOS();
368 bool isOSNetBSD() const {
369 return getOS() == Triple::NetBSD;
372 bool isOSOpenBSD() const {
373 return getOS() == Triple::OpenBSD;
376 bool isOSFreeBSD() const {
377 return getOS() == Triple::FreeBSD;
380 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
382 bool isOSSolaris() const {
383 return getOS() == Triple::Solaris;
386 bool isOSBitrig() const {
387 return getOS() == Triple::Bitrig;
390 bool isWindowsMSVCEnvironment() const {
391 return getOS() == Triple::Win32 &&
392 (getEnvironment() == Triple::UnknownEnvironment ||
393 getEnvironment() == Triple::MSVC);
396 bool isKnownWindowsMSVCEnvironment() const {
397 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
400 bool isWindowsItaniumEnvironment() const {
401 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
404 bool isWindowsCygwinEnvironment() const {
405 return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
408 bool isWindowsGNUEnvironment() const {
409 return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
412 /// \brief Tests for either Cygwin or MinGW OS
413 bool isOSCygMing() const {
414 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
417 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
418 bool isOSMSVCRT() const {
419 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
420 isWindowsItaniumEnvironment();
423 /// \brief Tests whether the OS is Windows.
424 bool isOSWindows() const {
425 return getOS() == Triple::Win32 || isOSCygMing();
428 /// \brief Tests whether the OS is NaCl (Native Client)
429 bool isOSNaCl() const {
430 return getOS() == Triple::NaCl;
433 /// \brief Tests whether the OS is Linux.
434 bool isOSLinux() const {
435 return getOS() == Triple::Linux;
438 /// \brief Tests whether the OS uses the ELF binary format.
439 bool isOSBinFormatELF() const {
440 return getObjectFormat() == Triple::ELF;
443 /// \brief Tests whether the OS uses the COFF binary format.
444 bool isOSBinFormatCOFF() const {
445 return getObjectFormat() == Triple::COFF;
448 /// \brief Tests whether the environment is MachO.
449 bool isOSBinFormatMachO() const {
450 return getObjectFormat() == Triple::MachO;
457 /// setArch - Set the architecture (first) component of the triple
459 void setArch(ArchType Kind);
461 /// setVendor - Set the vendor (second) component of the triple to a
463 void setVendor(VendorType Kind);
465 /// setOS - Set the operating system (third) component of the triple
467 void setOS(OSType Kind);
469 /// setEnvironment - Set the environment (fourth) component of the triple
471 void setEnvironment(EnvironmentType Kind);
473 /// setObjectFormat - Set the object file format
474 void setObjectFormat(ObjectFormatType Kind);
476 /// setTriple - Set all components to the new triple \p Str.
477 void setTriple(const Twine &Str);
479 /// setArchName - Set the architecture (first) component of the
481 void setArchName(StringRef Str);
483 /// setVendorName - Set the vendor (second) component of the triple
485 void setVendorName(StringRef Str);
487 /// setOSName - Set the operating system (third) component of the
489 void setOSName(StringRef Str);
491 /// setEnvironmentName - Set the optional environment (fourth)
492 /// component of the triple by name.
493 void setEnvironmentName(StringRef Str);
495 /// setOSAndEnvironmentName - Set the operating system and optional
496 /// environment components with a single string.
497 void setOSAndEnvironmentName(StringRef Str);
500 /// @name Helpers to build variants of a particular triple.
503 /// \brief Form a triple with a 32-bit variant of the current architecture.
505 /// This can be used to move across "families" of architectures where useful.
507 /// \returns A new triple with a 32-bit architecture or an unknown
508 /// architecture if no such variant can be found.
509 llvm::Triple get32BitArchVariant() const;
511 /// \brief Form a triple with a 64-bit variant of the current architecture.
513 /// This can be used to move across "families" of architectures where useful.
515 /// \returns A new triple with a 64-bit architecture or an unknown
516 /// architecture if no such variant can be found.
517 llvm::Triple get64BitArchVariant() const;
519 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
521 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
522 /// string then the triple's arch name is used.
523 const char* getARMCPUForArch(StringRef Arch = StringRef()) const;
526 /// @name Static helpers for IDs.
529 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
530 static const char *getArchTypeName(ArchType Kind);
532 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
533 /// architecture. This is the prefix used by the architecture specific
534 /// builtins, and is suitable for passing to \see
535 /// Intrinsic::getIntrinsicForGCCBuiltin().
537 /// \return - The architecture prefix, or 0 if none is defined.
538 static const char *getArchTypePrefix(ArchType Kind);
540 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
541 static const char *getVendorTypeName(VendorType Kind);
543 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
544 static const char *getOSTypeName(OSType Kind);
546 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
548 static const char *getEnvironmentTypeName(EnvironmentType Kind);
551 /// @name Static helpers for converting alternate architecture names.
554 /// getArchTypeForLLVMName - The canonical type for the given LLVM
555 /// architecture name (e.g., "x86").
556 static ArchType getArchTypeForLLVMName(StringRef Str);
561 } // End llvm namespace