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 amdil, // amdil: amd IL
76 spir, // SPIR: standard portable IR for OpenCL 32-bit version
77 spir64 // SPIR: standard portable IR for OpenCL 64-bit version
104 MinGW32, // i*86-pc-mingw32, *-w64-mingw32
112 NaCl, // Native Client
113 CNK, // BG/P Compute-Node Kernel
117 NVCL // NVIDIA OpenCL
119 enum EnvironmentType {
135 enum ObjectFormatType {
146 /// The parsed arch type.
149 /// The parsed vendor type.
152 /// The parsed OS type.
155 /// The parsed Environment type.
156 EnvironmentType Environment;
158 /// The object format type.
159 ObjectFormatType ObjectFormat;
162 /// @name Constructors
165 /// \brief Default constructor is the same as an empty string and leaves all
166 /// triple fields unknown.
167 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
169 explicit Triple(const Twine &Str);
170 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
171 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
172 const Twine &EnvironmentStr);
175 /// @name Normalization
178 /// normalize - Turn an arbitrary machine specification into the canonical
179 /// triple form (or something sensible that the Triple class understands if
180 /// nothing better can reasonably be done). In particular, it handles the
181 /// common case in which otherwise valid components are in the wrong order.
182 static std::string normalize(StringRef Str);
185 /// @name Typed Component Access
188 /// getArch - Get the parsed architecture type of this triple.
189 ArchType getArch() const { return Arch; }
191 /// getVendor - Get the parsed vendor type of this triple.
192 VendorType getVendor() const { return Vendor; }
194 /// getOS - Get the parsed operating system type of this triple.
195 OSType getOS() const { return OS; }
197 /// hasEnvironment - Does this triple have the optional environment
198 /// (fourth) component?
199 bool hasEnvironment() const {
200 return getEnvironmentName() != "";
203 /// getEnvironment - Get the parsed environment type of this triple.
204 EnvironmentType getEnvironment() const { return Environment; }
206 /// getFormat - Get the object format for this triple.
207 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
209 /// getOSVersion - Parse the version number from the OS name component of the
210 /// triple, if present.
212 /// For example, "fooos1.2.3" would return (1, 2, 3).
214 /// If an entry is not defined, it will be returned as 0.
215 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
217 /// getOSMajorVersion - Return just the major version number, this is
218 /// specialized because it is a common query.
219 unsigned getOSMajorVersion() const {
220 unsigned Maj, Min, Micro;
221 getOSVersion(Maj, Min, Micro);
225 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
226 /// translate generic "darwin" versions to the corresponding OS X versions.
227 /// This may also be called with IOS triples but the OS X version number is
228 /// just set to a constant 10.4.0 in that case. Returns true if successful.
229 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
230 unsigned &Micro) const;
232 /// getiOSVersion - Parse the version number as with getOSVersion. This should
233 /// only be called with IOS triples.
234 void getiOSVersion(unsigned &Major, unsigned &Minor,
235 unsigned &Micro) const;
238 /// @name Direct Component Access
241 const std::string &str() const { return Data; }
243 const std::string &getTriple() const { return Data; }
245 /// getArchName - Get the architecture (first) component of the
247 StringRef getArchName() const;
249 /// getVendorName - Get the vendor (second) component of the triple.
250 StringRef getVendorName() const;
252 /// getOSName - Get the operating system (third) component of the
254 StringRef getOSName() const;
256 /// getEnvironmentName - Get the optional environment (fourth)
257 /// component of the triple, or "" if empty.
258 StringRef getEnvironmentName() const;
260 /// getOSAndEnvironmentName - Get the operating system and optional
261 /// environment components as a single string (separated by a '-'
262 /// if the environment component is present).
263 StringRef getOSAndEnvironmentName() const;
266 /// @name Convenience Predicates
269 /// \brief Test whether the architecture is 64-bit
271 /// Note that this tests for 64-bit pointer width, and nothing else. Note
272 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
273 /// 16-bit. The inner details of pointer width for particular architectures
274 /// is not summed up in the triple, and so only a coarse grained predicate
275 /// system is provided.
276 bool isArch64Bit() const;
278 /// \brief Test whether the architecture is 32-bit
280 /// Note that this tests for 32-bit pointer width, and nothing else.
281 bool isArch32Bit() const;
283 /// \brief Test whether the architecture is 16-bit
285 /// Note that this tests for 16-bit pointer width, and nothing else.
286 bool isArch16Bit() const;
288 /// isOSVersionLT - Helper function for doing comparisons against version
289 /// numbers included in the target triple.
290 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
291 unsigned Micro = 0) const {
293 getOSVersion(LHS[0], LHS[1], LHS[2]);
296 return LHS[0] < Major;
298 return LHS[1] < Minor;
300 return LHS[1] < Micro;
305 /// isMacOSXVersionLT - Comparison function for checking OS X version
306 /// compatibility, which handles supporting skewed version numbering schemes
307 /// used by the "darwin" triples.
308 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
309 unsigned Micro = 0) const {
310 assert(isMacOSX() && "Not an OS X triple!");
312 // If this is OS X, expect a sane version number.
313 if (getOS() == Triple::MacOSX)
314 return isOSVersionLT(Major, Minor, Micro);
316 // Otherwise, compare to the "Darwin" number.
317 assert(Major == 10 && "Unexpected major version");
318 return isOSVersionLT(Minor + 4, Micro, 0);
321 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
322 /// "darwin" and "osx" as OS X triples.
323 bool isMacOSX() const {
324 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
327 /// Is this an iOS triple.
329 return getOS() == Triple::IOS;
332 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
333 bool isOSDarwin() const {
334 return isMacOSX() || isiOS();
337 bool isWindowsMSVCEnvironment() const {
338 return getOS() == Triple::Win32 &&
339 (getEnvironment() == Triple::UnknownEnvironment ||
340 getEnvironment() == Triple::MSVC);
343 bool isKnownWindowsMSVCEnvironment() const {
344 return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
347 bool isWindowsCygwinEnvironment() const {
348 return getOS() == Triple::Cygwin ||
349 (getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus);
352 bool isWindowsGNUEnvironment() const {
353 return getOS() == Triple::MinGW32 ||
354 (getOS() == Triple::Win32 && getEnvironment() == Triple::GNU);
357 /// \brief Tests for either Cygwin or MinGW OS
358 bool isOSCygMing() const {
359 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
362 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
363 bool isOSMSVCRT() const {
364 return getOS() == Triple::Win32 || getOS() == Triple::MinGW32;
367 /// \brief Tests whether the OS is Windows.
368 bool isOSWindows() const {
369 return getOS() == Triple::Win32 || isOSCygMing();
372 /// \brief Tests whether the OS is NaCl (Native Client)
373 bool isOSNaCl() const {
374 return getOS() == Triple::NaCl;
377 /// \brief Tests whether the OS is Linux.
378 bool isOSLinux() const {
379 return getOS() == Triple::Linux;
382 /// \brief Tests whether the OS uses the ELF binary format.
383 bool isOSBinFormatELF() const {
384 return getObjectFormat() == Triple::ELF;
387 /// \brief Tests whether the OS uses the COFF binary format.
388 bool isOSBinFormatCOFF() const {
389 return getObjectFormat() == Triple::COFF;
392 /// \brief Tests whether the environment is MachO.
393 bool isOSBinFormatMachO() const {
394 return getObjectFormat() == Triple::MachO;
401 /// setArch - Set the architecture (first) component of the triple
403 void setArch(ArchType Kind);
405 /// setVendor - Set the vendor (second) component of the triple to a
407 void setVendor(VendorType Kind);
409 /// setOS - Set the operating system (third) component of the triple
411 void setOS(OSType Kind);
413 /// setEnvironment - Set the environment (fourth) component of the triple
415 void setEnvironment(EnvironmentType Kind);
417 /// setObjectFormat - Set the object file format
418 void setObjectFormat(ObjectFormatType Kind);
420 /// setTriple - Set all components to the new triple \p Str.
421 void setTriple(const Twine &Str);
423 /// setArchName - Set the architecture (first) component of the
425 void setArchName(StringRef Str);
427 /// setVendorName - Set the vendor (second) component of the triple
429 void setVendorName(StringRef Str);
431 /// setOSName - Set the operating system (third) component of the
433 void setOSName(StringRef Str);
435 /// setEnvironmentName - Set the optional environment (fourth)
436 /// component of the triple by name.
437 void setEnvironmentName(StringRef Str);
439 /// setOSAndEnvironmentName - Set the operating system and optional
440 /// environment components with a single string.
441 void setOSAndEnvironmentName(StringRef Str);
443 /// getArchNameForAssembler - Get an architecture name that is understood by
444 /// the target assembler.
445 const char *getArchNameForAssembler();
448 /// @name Helpers to build variants of a particular triple.
451 /// \brief Form a triple with a 32-bit variant of the current architecture.
453 /// This can be used to move across "families" of architectures where useful.
455 /// \returns A new triple with a 32-bit architecture or an unknown
456 /// architecture if no such variant can be found.
457 llvm::Triple get32BitArchVariant() const;
459 /// \brief Form a triple with a 64-bit variant of the current architecture.
461 /// This can be used to move across "families" of architectures where useful.
463 /// \returns A new triple with a 64-bit architecture or an unknown
464 /// architecture if no such variant can be found.
465 llvm::Triple get64BitArchVariant() const;
468 /// @name Static helpers for IDs.
471 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
472 static const char *getArchTypeName(ArchType Kind);
474 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
475 /// architecture. This is the prefix used by the architecture specific
476 /// builtins, and is suitable for passing to \see
477 /// Intrinsic::getIntrinsicForGCCBuiltin().
479 /// \return - The architecture prefix, or 0 if none is defined.
480 static const char *getArchTypePrefix(ArchType Kind);
482 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
483 static const char *getVendorTypeName(VendorType Kind);
485 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
486 static const char *getOSTypeName(OSType Kind);
488 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
490 static const char *getEnvironmentTypeName(EnvironmentType Kind);
493 /// @name Static helpers for converting alternate architecture names.
496 /// getArchTypeForLLVMName - The canonical type for the given LLVM
497 /// architecture name (e.g., "x86").
498 static ArchType getArchTypeForLLVMName(StringRef Str);
503 } // End llvm namespace