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
92 ImaginationTechnologies,
109 MinGW32, // i*86-pc-mingw32, *-w64-mingw32
117 NaCl, // Native Client
118 CNK, // BG/P Compute-Node Kernel
122 NVCL // NVIDIA OpenCL
124 enum EnvironmentType {
140 enum ObjectFormatType {
151 /// The parsed arch type.
154 /// The parsed vendor type.
157 /// The parsed OS type.
160 /// The parsed Environment type.
161 EnvironmentType Environment;
163 /// The object format type.
164 ObjectFormatType ObjectFormat;
167 /// @name Constructors
170 /// \brief Default constructor is the same as an empty string and leaves all
171 /// triple fields unknown.
172 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
174 explicit Triple(const Twine &Str);
175 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
176 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
177 const Twine &EnvironmentStr);
180 /// @name Normalization
183 /// normalize - Turn an arbitrary machine specification into the canonical
184 /// triple form (or something sensible that the Triple class understands if
185 /// nothing better can reasonably be done). In particular, it handles the
186 /// common case in which otherwise valid components are in the wrong order.
187 static std::string normalize(StringRef Str);
190 /// @name Typed Component Access
193 /// getArch - Get the parsed architecture type of this triple.
194 ArchType getArch() const { return Arch; }
196 /// getVendor - Get the parsed vendor type of this triple.
197 VendorType getVendor() const { return Vendor; }
199 /// getOS - Get the parsed operating system type of this triple.
200 OSType getOS() const { return OS; }
202 /// hasEnvironment - Does this triple have the optional environment
203 /// (fourth) component?
204 bool hasEnvironment() const {
205 return getEnvironmentName() != "";
208 /// getEnvironment - Get the parsed environment type of this triple.
209 EnvironmentType getEnvironment() const { return Environment; }
211 /// getFormat - Get the object format for this triple.
212 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
214 /// getOSVersion - Parse the version number from the OS name component of the
215 /// triple, if present.
217 /// For example, "fooos1.2.3" would return (1, 2, 3).
219 /// If an entry is not defined, it will be returned as 0.
220 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
222 /// getOSMajorVersion - Return just the major version number, this is
223 /// specialized because it is a common query.
224 unsigned getOSMajorVersion() const {
225 unsigned Maj, Min, Micro;
226 getOSVersion(Maj, Min, Micro);
230 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
231 /// translate generic "darwin" versions to the corresponding OS X versions.
232 /// This may also be called with IOS triples but the OS X version number is
233 /// just set to a constant 10.4.0 in that case. Returns true if successful.
234 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
235 unsigned &Micro) const;
237 /// getiOSVersion - Parse the version number as with getOSVersion. This should
238 /// only be called with IOS triples.
239 void getiOSVersion(unsigned &Major, unsigned &Minor,
240 unsigned &Micro) const;
243 /// @name Direct Component Access
246 const std::string &str() const { return Data; }
248 const std::string &getTriple() const { return Data; }
250 /// getArchName - Get the architecture (first) component of the
252 StringRef getArchName() const;
254 /// getVendorName - Get the vendor (second) component of the triple.
255 StringRef getVendorName() const;
257 /// getOSName - Get the operating system (third) component of the
259 StringRef getOSName() const;
261 /// getEnvironmentName - Get the optional environment (fourth)
262 /// component of the triple, or "" if empty.
263 StringRef getEnvironmentName() const;
265 /// getOSAndEnvironmentName - Get the operating system and optional
266 /// environment components as a single string (separated by a '-'
267 /// if the environment component is present).
268 StringRef getOSAndEnvironmentName() const;
271 /// @name Convenience Predicates
274 /// \brief Test whether the architecture is 64-bit
276 /// Note that this tests for 64-bit pointer width, and nothing else. Note
277 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
278 /// 16-bit. The inner details of pointer width for particular architectures
279 /// is not summed up in the triple, and so only a coarse grained predicate
280 /// system is provided.
281 bool isArch64Bit() const;
283 /// \brief Test whether the architecture is 32-bit
285 /// Note that this tests for 32-bit pointer width, and nothing else.
286 bool isArch32Bit() const;
288 /// \brief Test whether the architecture is 16-bit
290 /// Note that this tests for 16-bit pointer width, and nothing else.
291 bool isArch16Bit() const;
293 /// isOSVersionLT - Helper function for doing comparisons against version
294 /// numbers included in the target triple.
295 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
296 unsigned Micro = 0) const {
298 getOSVersion(LHS[0], LHS[1], LHS[2]);
301 return LHS[0] < Major;
303 return LHS[1] < Minor;
305 return LHS[1] < Micro;
310 /// isMacOSXVersionLT - Comparison function for checking OS X version
311 /// compatibility, which handles supporting skewed version numbering schemes
312 /// used by the "darwin" triples.
313 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
314 unsigned Micro = 0) const {
315 assert(isMacOSX() && "Not an OS X triple!");
317 // If this is OS X, expect a sane version number.
318 if (getOS() == Triple::MacOSX)
319 return isOSVersionLT(Major, Minor, Micro);
321 // Otherwise, compare to the "Darwin" number.
322 assert(Major == 10 && "Unexpected major version");
323 return isOSVersionLT(Minor + 4, Micro, 0);
326 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
327 /// "darwin" and "osx" as OS X triples.
328 bool isMacOSX() const {
329 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
332 /// Is this an iOS triple.
334 return getOS() == Triple::IOS;
337 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
338 bool isOSDarwin() const {
339 return isMacOSX() || isiOS();
342 bool isOSFreeBSD() const {
343 return getOS() == Triple::FreeBSD;
346 bool isWindowsMSVCEnvironment() const {
347 return getOS() == Triple::Win32 &&
348 (getEnvironment() == Triple::UnknownEnvironment ||
349 getEnvironment() == Triple::MSVC);
352 bool isKnownWindowsMSVCEnvironment() const {
353 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
356 bool isWindowsItaniumEnvironment() const {
357 return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
360 bool isWindowsCygwinEnvironment() const {
361 return getOS() == Triple::Cygwin ||
362 (getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus);
365 bool isWindowsGNUEnvironment() const {
366 return getOS() == Triple::MinGW32 ||
367 (getOS() == Triple::Win32 && getEnvironment() == Triple::GNU);
370 /// \brief Tests for either Cygwin or MinGW OS
371 bool isOSCygMing() const {
372 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
375 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
376 bool isOSMSVCRT() const {
377 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment();
380 /// \brief Tests whether the OS is Windows.
381 bool isOSWindows() const {
382 return getOS() == Triple::Win32 || isOSCygMing();
385 /// \brief Tests whether the OS is NaCl (Native Client)
386 bool isOSNaCl() const {
387 return getOS() == Triple::NaCl;
390 /// \brief Tests whether the OS is Linux.
391 bool isOSLinux() const {
392 return getOS() == Triple::Linux;
395 /// \brief Tests whether the OS uses the ELF binary format.
396 bool isOSBinFormatELF() const {
397 return getObjectFormat() == Triple::ELF;
400 /// \brief Tests whether the OS uses the COFF binary format.
401 bool isOSBinFormatCOFF() const {
402 return getObjectFormat() == Triple::COFF;
405 /// \brief Tests whether the environment is MachO.
406 bool isOSBinFormatMachO() const {
407 return getObjectFormat() == Triple::MachO;
414 /// setArch - Set the architecture (first) component of the triple
416 void setArch(ArchType Kind);
418 /// setVendor - Set the vendor (second) component of the triple to a
420 void setVendor(VendorType Kind);
422 /// setOS - Set the operating system (third) component of the triple
424 void setOS(OSType Kind);
426 /// setEnvironment - Set the environment (fourth) component of the triple
428 void setEnvironment(EnvironmentType Kind);
430 /// setObjectFormat - Set the object file format
431 void setObjectFormat(ObjectFormatType Kind);
433 /// setTriple - Set all components to the new triple \p Str.
434 void setTriple(const Twine &Str);
436 /// setArchName - Set the architecture (first) component of the
438 void setArchName(StringRef Str);
440 /// setVendorName - Set the vendor (second) component of the triple
442 void setVendorName(StringRef Str);
444 /// setOSName - Set the operating system (third) component of the
446 void setOSName(StringRef Str);
448 /// setEnvironmentName - Set the optional environment (fourth)
449 /// component of the triple by name.
450 void setEnvironmentName(StringRef Str);
452 /// setOSAndEnvironmentName - Set the operating system and optional
453 /// environment components with a single string.
454 void setOSAndEnvironmentName(StringRef Str);
456 /// getArchNameForAssembler - Get an architecture name that is understood by
457 /// the target assembler.
458 const char *getArchNameForAssembler();
461 /// @name Helpers to build variants of a particular triple.
464 /// \brief Form a triple with a 32-bit variant of the current architecture.
466 /// This can be used to move across "families" of architectures where useful.
468 /// \returns A new triple with a 32-bit architecture or an unknown
469 /// architecture if no such variant can be found.
470 llvm::Triple get32BitArchVariant() const;
472 /// \brief Form a triple with a 64-bit variant of the current architecture.
474 /// This can be used to move across "families" of architectures where useful.
476 /// \returns A new triple with a 64-bit architecture or an unknown
477 /// architecture if no such variant can be found.
478 llvm::Triple get64BitArchVariant() const;
481 /// @name Static helpers for IDs.
484 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
485 static const char *getArchTypeName(ArchType Kind);
487 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
488 /// architecture. This is the prefix used by the architecture specific
489 /// builtins, and is suitable for passing to \see
490 /// Intrinsic::getIntrinsicForGCCBuiltin().
492 /// \return - The architecture prefix, or 0 if none is defined.
493 static const char *getArchTypePrefix(ArchType Kind);
495 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
496 static const char *getVendorTypeName(VendorType Kind);
498 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
499 static const char *getOSTypeName(OSType Kind);
501 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
503 static const char *getEnvironmentTypeName(EnvironmentType Kind);
506 /// @name Static helpers for converting alternate architecture names.
509 /// getArchTypeForLLVMName - The canonical type for the given LLVM
510 /// architecture name (e.g., "x86").
511 static ArchType getArchTypeForLLVMName(StringRef Str);
516 } // End llvm namespace