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
80 kalimba // Kalimba: generic kalimba
107 ImaginationTechnologies,
124 MinGW32, // i*86-pc-mingw32, *-w64-mingw32
132 NaCl, // Native Client
133 CNK, // BG/P Compute-Node Kernel
137 NVCL // NVIDIA OpenCL
139 enum EnvironmentType {
155 enum ObjectFormatType {
166 /// The parsed arch type.
169 /// The parsed subarchitecture type.
172 /// The parsed vendor type.
175 /// The parsed OS type.
178 /// The parsed Environment type.
179 EnvironmentType Environment;
181 /// The object format type.
182 ObjectFormatType ObjectFormat;
185 /// @name Constructors
188 /// \brief Default constructor is the same as an empty string and leaves all
189 /// triple fields unknown.
190 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
192 explicit Triple(const Twine &Str);
193 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
194 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
195 const Twine &EnvironmentStr);
198 /// @name Normalization
201 /// normalize - Turn an arbitrary machine specification into the canonical
202 /// triple form (or something sensible that the Triple class understands if
203 /// nothing better can reasonably be done). In particular, it handles the
204 /// common case in which otherwise valid components are in the wrong order.
205 static std::string normalize(StringRef Str);
208 /// @name Typed Component Access
211 /// getArch - Get the parsed architecture type of this triple.
212 ArchType getArch() const { return Arch; }
214 /// getSubArch - get the parsed subarchitecture type for this triple.
215 SubArchType getSubArch() const { return SubArch; }
217 /// getVendor - Get the parsed vendor type of this triple.
218 VendorType getVendor() const { return Vendor; }
220 /// getOS - Get the parsed operating system type of this triple.
221 OSType getOS() const { return OS; }
223 /// hasEnvironment - Does this triple have the optional environment
224 /// (fourth) component?
225 bool hasEnvironment() const {
226 return getEnvironmentName() != "";
229 /// getEnvironment - Get the parsed environment type of this triple.
230 EnvironmentType getEnvironment() const { return Environment; }
232 /// getFormat - Get the object format for this triple.
233 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
235 /// getOSVersion - Parse the version number from the OS name component of the
236 /// triple, if present.
238 /// For example, "fooos1.2.3" would return (1, 2, 3).
240 /// If an entry is not defined, it will be returned as 0.
241 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
243 /// getOSMajorVersion - Return just the major version number, this is
244 /// specialized because it is a common query.
245 unsigned getOSMajorVersion() const {
246 unsigned Maj, Min, Micro;
247 getOSVersion(Maj, Min, Micro);
251 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
252 /// translate generic "darwin" versions to the corresponding OS X versions.
253 /// This may also be called with IOS triples but the OS X version number is
254 /// just set to a constant 10.4.0 in that case. Returns true if successful.
255 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
256 unsigned &Micro) const;
258 /// getiOSVersion - Parse the version number as with getOSVersion. This should
259 /// only be called with IOS triples.
260 void getiOSVersion(unsigned &Major, unsigned &Minor,
261 unsigned &Micro) const;
264 /// @name Direct Component Access
267 const std::string &str() const { return Data; }
269 const std::string &getTriple() const { return Data; }
271 /// getArchName - Get the architecture (first) component of the
273 StringRef getArchName() const;
275 /// getVendorName - Get the vendor (second) component of the triple.
276 StringRef getVendorName() const;
278 /// getOSName - Get the operating system (third) component of the
280 StringRef getOSName() const;
282 /// getEnvironmentName - Get the optional environment (fourth)
283 /// component of the triple, or "" if empty.
284 StringRef getEnvironmentName() const;
286 /// getOSAndEnvironmentName - Get the operating system and optional
287 /// environment components as a single string (separated by a '-'
288 /// if the environment component is present).
289 StringRef getOSAndEnvironmentName() const;
292 /// @name Convenience Predicates
295 /// \brief Test whether the architecture is 64-bit
297 /// Note that this tests for 64-bit pointer width, and nothing else. Note
298 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
299 /// 16-bit. The inner details of pointer width for particular architectures
300 /// is not summed up in the triple, and so only a coarse grained predicate
301 /// system is provided.
302 bool isArch64Bit() const;
304 /// \brief Test whether the architecture is 32-bit
306 /// Note that this tests for 32-bit pointer width, and nothing else.
307 bool isArch32Bit() const;
309 /// \brief Test whether the architecture is 16-bit
311 /// Note that this tests for 16-bit pointer width, and nothing else.
312 bool isArch16Bit() const;
314 /// isOSVersionLT - Helper function for doing comparisons against version
315 /// numbers included in the target triple.
316 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
317 unsigned Micro = 0) const {
319 getOSVersion(LHS[0], LHS[1], LHS[2]);
322 return LHS[0] < Major;
324 return LHS[1] < Minor;
326 return LHS[1] < Micro;
331 /// isMacOSXVersionLT - Comparison function for checking OS X version
332 /// compatibility, which handles supporting skewed version numbering schemes
333 /// used by the "darwin" triples.
334 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
335 unsigned Micro = 0) const {
336 assert(isMacOSX() && "Not an OS X triple!");
338 // If this is OS X, expect a sane version number.
339 if (getOS() == Triple::MacOSX)
340 return isOSVersionLT(Major, Minor, Micro);
342 // Otherwise, compare to the "Darwin" number.
343 assert(Major == 10 && "Unexpected major version");
344 return isOSVersionLT(Minor + 4, Micro, 0);
347 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
348 /// "darwin" and "osx" as OS X triples.
349 bool isMacOSX() const {
350 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
353 /// Is this an iOS triple.
355 return getOS() == Triple::IOS;
358 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
359 bool isOSDarwin() const {
360 return isMacOSX() || isiOS();
363 bool isOSFreeBSD() const {
364 return getOS() == Triple::FreeBSD;
367 bool isWindowsMSVCEnvironment() const {
368 return getOS() == Triple::Win32 &&
369 (getEnvironment() == Triple::UnknownEnvironment ||
370 getEnvironment() == Triple::MSVC);
373 bool isKnownWindowsMSVCEnvironment() const {
374 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
377 bool isWindowsItaniumEnvironment() const {
378 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
381 bool isWindowsCygwinEnvironment() const {
382 return getOS() == Triple::Cygwin ||
383 (getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus);
386 bool isWindowsGNUEnvironment() const {
387 return getOS() == Triple::MinGW32 ||
388 (getOS() == Triple::Win32 && getEnvironment() == Triple::GNU);
391 /// \brief Tests for either Cygwin or MinGW OS
392 bool isOSCygMing() const {
393 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
396 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
397 bool isOSMSVCRT() const {
398 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment();
401 /// \brief Tests whether the OS is Windows.
402 bool isOSWindows() const {
403 return getOS() == Triple::Win32 || isOSCygMing();
406 /// \brief Tests whether the OS is NaCl (Native Client)
407 bool isOSNaCl() const {
408 return getOS() == Triple::NaCl;
411 /// \brief Tests whether the OS is Linux.
412 bool isOSLinux() const {
413 return getOS() == Triple::Linux;
416 /// \brief Tests whether the OS uses the ELF binary format.
417 bool isOSBinFormatELF() const {
418 return getObjectFormat() == Triple::ELF;
421 /// \brief Tests whether the OS uses the COFF binary format.
422 bool isOSBinFormatCOFF() const {
423 return getObjectFormat() == Triple::COFF;
426 /// \brief Tests whether the environment is MachO.
427 bool isOSBinFormatMachO() const {
428 return getObjectFormat() == Triple::MachO;
435 /// setArch - Set the architecture (first) component of the triple
437 void setArch(ArchType Kind);
439 /// setVendor - Set the vendor (second) component of the triple to a
441 void setVendor(VendorType Kind);
443 /// setOS - Set the operating system (third) component of the triple
445 void setOS(OSType Kind);
447 /// setEnvironment - Set the environment (fourth) component of the triple
449 void setEnvironment(EnvironmentType Kind);
451 /// setObjectFormat - Set the object file format
452 void setObjectFormat(ObjectFormatType Kind);
454 /// setTriple - Set all components to the new triple \p Str.
455 void setTriple(const Twine &Str);
457 /// setArchName - Set the architecture (first) component of the
459 void setArchName(StringRef Str);
461 /// setVendorName - Set the vendor (second) component of the triple
463 void setVendorName(StringRef Str);
465 /// setOSName - Set the operating system (third) component of the
467 void setOSName(StringRef Str);
469 /// setEnvironmentName - Set the optional environment (fourth)
470 /// component of the triple by name.
471 void setEnvironmentName(StringRef Str);
473 /// setOSAndEnvironmentName - Set the operating system and optional
474 /// environment components with a single string.
475 void setOSAndEnvironmentName(StringRef Str);
477 /// getArchNameForAssembler - Get an architecture name that is understood by
478 /// the target assembler.
479 const char *getArchNameForAssembler();
482 /// @name Helpers to build variants of a particular triple.
485 /// \brief Form a triple with a 32-bit variant of the current architecture.
487 /// This can be used to move across "families" of architectures where useful.
489 /// \returns A new triple with a 32-bit architecture or an unknown
490 /// architecture if no such variant can be found.
491 llvm::Triple get32BitArchVariant() const;
493 /// \brief Form a triple with a 64-bit variant of the current architecture.
495 /// This can be used to move across "families" of architectures where useful.
497 /// \returns A new triple with a 64-bit architecture or an unknown
498 /// architecture if no such variant can be found.
499 llvm::Triple get64BitArchVariant() const;
501 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
503 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
504 /// string then the triple's arch name is used.
505 const char* getARMCPUForArch(StringRef Arch = StringRef()) const;
508 /// @name Static helpers for IDs.
511 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
512 static const char *getArchTypeName(ArchType Kind);
514 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
515 /// architecture. This is the prefix used by the architecture specific
516 /// builtins, and is suitable for passing to \see
517 /// Intrinsic::getIntrinsicForGCCBuiltin().
519 /// \return - The architecture prefix, or 0 if none is defined.
520 static const char *getArchTypePrefix(ArchType Kind);
522 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
523 static const char *getVendorTypeName(VendorType Kind);
525 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
526 static const char *getOSTypeName(OSType Kind);
528 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
530 static const char *getEnvironmentTypeName(EnvironmentType Kind);
533 /// @name Static helpers for converting alternate architecture names.
536 /// getArchTypeForLLVMName - The canonical type for the given LLVM
537 /// architecture name (e.g., "x86").
538 static ArchType getArchTypeForLLVMName(StringRef Str);
543 } // End llvm namespace