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 arm64, // ARM64 (little endian): arm64
52 arm64_be, // ARM64 (big endian): arm64_be
53 aarch64, // AArch64 (little endian): aarch64
54 aarch64_be, // AArch64 (big endian): aarch64_be
55 hexagon, // Hexagon: hexagon
56 mips, // MIPS: mips, mipsallegrex
57 mipsel, // MIPSEL: mipsel, mipsallegrexel
58 mips64, // MIPS64: mips64
59 mips64el, // MIPS64EL: mips64el
60 msp430, // MSP430: msp430
62 ppc64, // PPC64: powerpc64, ppu
63 ppc64le, // PPC64LE: powerpc64le
64 r600, // R600: AMD GPUs HD2XXX - HD6XXX
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 amdil, // amdil: amd IL
78 spir, // SPIR: standard portable IR for OpenCL 32-bit version
79 spir64 // SPIR: standard portable IR for OpenCL 64-bit version
122 MinGW32, // i*86-pc-mingw32, *-w64-mingw32
130 NaCl, // Native Client
131 CNK, // BG/P Compute-Node Kernel
135 NVCL // NVIDIA OpenCL
137 enum EnvironmentType {
153 enum ObjectFormatType {
164 /// The parsed arch type.
167 /// The parsed subarchitecture type.
170 /// The parsed vendor type.
173 /// The parsed OS type.
176 /// The parsed Environment type.
177 EnvironmentType Environment;
179 /// The object format type.
180 ObjectFormatType ObjectFormat;
183 /// @name Constructors
186 /// \brief Default constructor is the same as an empty string and leaves all
187 /// triple fields unknown.
188 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
190 explicit Triple(const Twine &Str);
191 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
192 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
193 const Twine &EnvironmentStr);
196 /// @name Normalization
199 /// normalize - Turn an arbitrary machine specification into the canonical
200 /// triple form (or something sensible that the Triple class understands if
201 /// nothing better can reasonably be done). In particular, it handles the
202 /// common case in which otherwise valid components are in the wrong order.
203 static std::string normalize(StringRef Str);
206 /// @name Typed Component Access
209 /// getArch - Get the parsed architecture type of this triple.
210 ArchType getArch() const { return Arch; }
212 /// getSubArch - get the parsed subarchitecture type for this triple.
213 SubArchType getSubArch() const { return SubArch; }
215 /// getVendor - Get the parsed vendor type of this triple.
216 VendorType getVendor() const { return Vendor; }
218 /// getOS - Get the parsed operating system type of this triple.
219 OSType getOS() const { return OS; }
221 /// hasEnvironment - Does this triple have the optional environment
222 /// (fourth) component?
223 bool hasEnvironment() const {
224 return getEnvironmentName() != "";
227 /// getEnvironment - Get the parsed environment type of this triple.
228 EnvironmentType getEnvironment() const { return Environment; }
230 /// getFormat - Get the object format for this triple.
231 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
233 /// getOSVersion - Parse the version number from the OS name component of the
234 /// triple, if present.
236 /// For example, "fooos1.2.3" would return (1, 2, 3).
238 /// If an entry is not defined, it will be returned as 0.
239 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
241 /// getOSMajorVersion - Return just the major version number, this is
242 /// specialized because it is a common query.
243 unsigned getOSMajorVersion() const {
244 unsigned Maj, Min, Micro;
245 getOSVersion(Maj, Min, Micro);
249 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
250 /// translate generic "darwin" versions to the corresponding OS X versions.
251 /// This may also be called with IOS triples but the OS X version number is
252 /// just set to a constant 10.4.0 in that case. Returns true if successful.
253 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
254 unsigned &Micro) const;
256 /// getiOSVersion - Parse the version number as with getOSVersion. This should
257 /// only be called with IOS triples.
258 void getiOSVersion(unsigned &Major, unsigned &Minor,
259 unsigned &Micro) const;
262 /// @name Direct Component Access
265 const std::string &str() const { return Data; }
267 const std::string &getTriple() const { return Data; }
269 /// getArchName - Get the architecture (first) component of the
271 StringRef getArchName() const;
273 /// getVendorName - Get the vendor (second) component of the triple.
274 StringRef getVendorName() const;
276 /// getOSName - Get the operating system (third) component of the
278 StringRef getOSName() const;
280 /// getEnvironmentName - Get the optional environment (fourth)
281 /// component of the triple, or "" if empty.
282 StringRef getEnvironmentName() const;
284 /// getOSAndEnvironmentName - Get the operating system and optional
285 /// environment components as a single string (separated by a '-'
286 /// if the environment component is present).
287 StringRef getOSAndEnvironmentName() const;
290 /// @name Convenience Predicates
293 /// \brief Test whether the architecture is 64-bit
295 /// Note that this tests for 64-bit pointer width, and nothing else. Note
296 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
297 /// 16-bit. The inner details of pointer width for particular architectures
298 /// is not summed up in the triple, and so only a coarse grained predicate
299 /// system is provided.
300 bool isArch64Bit() const;
302 /// \brief Test whether the architecture is 32-bit
304 /// Note that this tests for 32-bit pointer width, and nothing else.
305 bool isArch32Bit() const;
307 /// \brief Test whether the architecture is 16-bit
309 /// Note that this tests for 16-bit pointer width, and nothing else.
310 bool isArch16Bit() const;
312 /// isOSVersionLT - Helper function for doing comparisons against version
313 /// numbers included in the target triple.
314 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
315 unsigned Micro = 0) const {
317 getOSVersion(LHS[0], LHS[1], LHS[2]);
320 return LHS[0] < Major;
322 return LHS[1] < Minor;
324 return LHS[1] < Micro;
329 /// isMacOSXVersionLT - Comparison function for checking OS X version
330 /// compatibility, which handles supporting skewed version numbering schemes
331 /// used by the "darwin" triples.
332 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
333 unsigned Micro = 0) const {
334 assert(isMacOSX() && "Not an OS X triple!");
336 // If this is OS X, expect a sane version number.
337 if (getOS() == Triple::MacOSX)
338 return isOSVersionLT(Major, Minor, Micro);
340 // Otherwise, compare to the "Darwin" number.
341 assert(Major == 10 && "Unexpected major version");
342 return isOSVersionLT(Minor + 4, Micro, 0);
345 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
346 /// "darwin" and "osx" as OS X triples.
347 bool isMacOSX() const {
348 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
351 /// Is this an iOS triple.
353 return getOS() == Triple::IOS;
356 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
357 bool isOSDarwin() const {
358 return isMacOSX() || isiOS();
361 bool isOSFreeBSD() const {
362 return getOS() == Triple::FreeBSD;
365 bool isWindowsMSVCEnvironment() const {
366 return getOS() == Triple::Win32 &&
367 (getEnvironment() == Triple::UnknownEnvironment ||
368 getEnvironment() == Triple::MSVC);
371 bool isKnownWindowsMSVCEnvironment() const {
372 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
375 bool isWindowsItaniumEnvironment() const {
376 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
379 bool isWindowsCygwinEnvironment() const {
380 return getOS() == Triple::Cygwin ||
381 (getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus);
384 bool isWindowsGNUEnvironment() const {
385 return getOS() == Triple::MinGW32 ||
386 (getOS() == Triple::Win32 && getEnvironment() == Triple::GNU);
389 /// \brief Tests for either Cygwin or MinGW OS
390 bool isOSCygMing() const {
391 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
394 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
395 bool isOSMSVCRT() const {
396 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment();
399 /// \brief Tests whether the OS is Windows.
400 bool isOSWindows() const {
401 return getOS() == Triple::Win32 || isOSCygMing();
404 /// \brief Tests whether the OS is NaCl (Native Client)
405 bool isOSNaCl() const {
406 return getOS() == Triple::NaCl;
409 /// \brief Tests whether the OS is Linux.
410 bool isOSLinux() const {
411 return getOS() == Triple::Linux;
414 /// \brief Tests whether the OS uses the ELF binary format.
415 bool isOSBinFormatELF() const {
416 return getObjectFormat() == Triple::ELF;
419 /// \brief Tests whether the OS uses the COFF binary format.
420 bool isOSBinFormatCOFF() const {
421 return getObjectFormat() == Triple::COFF;
424 /// \brief Tests whether the environment is MachO.
425 bool isOSBinFormatMachO() const {
426 return getObjectFormat() == Triple::MachO;
433 /// setArch - Set the architecture (first) component of the triple
435 void setArch(ArchType Kind);
437 /// setVendor - Set the vendor (second) component of the triple to a
439 void setVendor(VendorType Kind);
441 /// setOS - Set the operating system (third) component of the triple
443 void setOS(OSType Kind);
445 /// setEnvironment - Set the environment (fourth) component of the triple
447 void setEnvironment(EnvironmentType Kind);
449 /// setObjectFormat - Set the object file format
450 void setObjectFormat(ObjectFormatType Kind);
452 /// setTriple - Set all components to the new triple \p Str.
453 void setTriple(const Twine &Str);
455 /// setArchName - Set the architecture (first) component of the
457 void setArchName(StringRef Str);
459 /// setVendorName - Set the vendor (second) component of the triple
461 void setVendorName(StringRef Str);
463 /// setOSName - Set the operating system (third) component of the
465 void setOSName(StringRef Str);
467 /// setEnvironmentName - Set the optional environment (fourth)
468 /// component of the triple by name.
469 void setEnvironmentName(StringRef Str);
471 /// setOSAndEnvironmentName - Set the operating system and optional
472 /// environment components with a single string.
473 void setOSAndEnvironmentName(StringRef Str);
475 /// getArchNameForAssembler - Get an architecture name that is understood by
476 /// the target assembler.
477 const char *getArchNameForAssembler();
480 /// @name Helpers to build variants of a particular triple.
483 /// \brief Form a triple with a 32-bit variant of the current architecture.
485 /// This can be used to move across "families" of architectures where useful.
487 /// \returns A new triple with a 32-bit architecture or an unknown
488 /// architecture if no such variant can be found.
489 llvm::Triple get32BitArchVariant() const;
491 /// \brief Form a triple with a 64-bit variant of the current architecture.
493 /// This can be used to move across "families" of architectures where useful.
495 /// \returns A new triple with a 64-bit architecture or an unknown
496 /// architecture if no such variant can be found.
497 llvm::Triple get64BitArchVariant() const;
500 /// @name Static helpers for IDs.
503 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
504 static const char *getArchTypeName(ArchType Kind);
506 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
507 /// architecture. This is the prefix used by the architecture specific
508 /// builtins, and is suitable for passing to \see
509 /// Intrinsic::getIntrinsicForGCCBuiltin().
511 /// \return - The architecture prefix, or 0 if none is defined.
512 static const char *getArchTypePrefix(ArchType Kind);
514 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
515 static const char *getVendorTypeName(VendorType Kind);
517 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
518 static const char *getOSTypeName(OSType Kind);
520 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
522 static const char *getEnvironmentTypeName(EnvironmentType Kind);
525 /// @name Static helpers for converting alternate architecture names.
528 /// getArchTypeForLLVMName - The canonical type for the given LLVM
529 /// architecture name (e.g., "x86").
530 static ArchType getArchTypeForLLVMName(StringRef Str);
535 } // End llvm namespace