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: arm, armv.*, xscale
50 aarch64, // AArch64 (little endian): aarch64
51 aarch64_be, // AArch64 (big endian): aarch64_be
52 hexagon, // Hexagon: hexagon
53 mips, // MIPS: mips, mipsallegrex
54 mipsel, // MIPSEL: mipsel, mipsallegrexel
55 mips64, // MIPS64: mips64
56 mips64el, // MIPS64EL: mips64el
57 msp430, // MSP430: msp430
59 ppc64, // PPC64: powerpc64, ppu
60 ppc64le, // PPC64LE: powerpc64le
61 r600, // R600: AMD GPUs HD2XXX - HD6XXX
62 sparc, // Sparc: sparc
63 sparcv9, // Sparcv9: Sparcv9
64 systemz, // SystemZ: s390x
65 tce, // TCE (http://tce.cs.tut.fi/): tce
66 thumb, // Thumb: thumb, thumbv.*
68 x86_64, // X86-64: amd64, x86_64
69 xcore, // XCore: xcore
70 nvptx, // NVPTX: 32-bit
71 nvptx64, // NVPTX: 64-bit
72 le32, // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
73 amdil, // amdil: amd IL
74 spir, // SPIR: standard portable IR for OpenCL 32-bit version
75 spir64 // SPIR: standard portable IR for OpenCL 64-bit version
102 MinGW32, // i*86-pc-mingw32, *-w64-mingw32
110 NaCl, // Native Client
111 CNK, // BG/P Compute-Node Kernel
115 NVCL // NVIDIA OpenCL
117 enum EnvironmentType {
129 enum ObjectFormatType {
140 /// The parsed arch type.
143 /// The parsed vendor type.
146 /// The parsed OS type.
149 /// The parsed Environment type.
150 EnvironmentType Environment;
152 /// The object format type.
153 ObjectFormatType ObjectFormat;
156 /// @name Constructors
159 /// \brief Default constructor is the same as an empty string and leaves all
160 /// triple fields unknown.
161 Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
163 explicit Triple(const Twine &Str);
164 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
165 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
166 const Twine &EnvironmentStr);
169 /// @name Normalization
172 /// normalize - Turn an arbitrary machine specification into the canonical
173 /// triple form (or something sensible that the Triple class understands if
174 /// nothing better can reasonably be done). In particular, it handles the
175 /// common case in which otherwise valid components are in the wrong order.
176 static std::string normalize(StringRef Str);
179 /// @name Typed Component Access
182 /// getArch - Get the parsed architecture type of this triple.
183 ArchType getArch() const { return Arch; }
185 /// getVendor - Get the parsed vendor type of this triple.
186 VendorType getVendor() const { return Vendor; }
188 /// getOS - Get the parsed operating system type of this triple.
189 OSType getOS() const { return OS; }
191 /// hasEnvironment - Does this triple have the optional environment
192 /// (fourth) component?
193 bool hasEnvironment() const {
194 return getEnvironmentName() != "";
197 /// getEnvironment - Get the parsed environment type of this triple.
198 EnvironmentType getEnvironment() const { return Environment; }
200 /// getFormat - Get the object format for this triple.
201 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
203 /// getOSVersion - Parse the version number from the OS name component of the
204 /// triple, if present.
206 /// For example, "fooos1.2.3" would return (1, 2, 3).
208 /// If an entry is not defined, it will be returned as 0.
209 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
211 /// getOSMajorVersion - Return just the major version number, this is
212 /// specialized because it is a common query.
213 unsigned getOSMajorVersion() const {
214 unsigned Maj, Min, Micro;
215 getOSVersion(Maj, Min, Micro);
219 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
220 /// translate generic "darwin" versions to the corresponding OS X versions.
221 /// This may also be called with IOS triples but the OS X version number is
222 /// just set to a constant 10.4.0 in that case. Returns true if successful.
223 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
224 unsigned &Micro) const;
226 /// getiOSVersion - Parse the version number as with getOSVersion. This should
227 /// only be called with IOS triples.
228 void getiOSVersion(unsigned &Major, unsigned &Minor,
229 unsigned &Micro) const;
232 /// @name Direct Component Access
235 const std::string &str() const { return Data; }
237 const std::string &getTriple() const { return Data; }
239 /// getArchName - Get the architecture (first) component of the
241 StringRef getArchName() const;
243 /// getVendorName - Get the vendor (second) component of the triple.
244 StringRef getVendorName() const;
246 /// getOSName - Get the operating system (third) component of the
248 StringRef getOSName() const;
250 /// getEnvironmentName - Get the optional environment (fourth)
251 /// component of the triple, or "" if empty.
252 StringRef getEnvironmentName() const;
254 /// getOSAndEnvironmentName - Get the operating system and optional
255 /// environment components as a single string (separated by a '-'
256 /// if the environment component is present).
257 StringRef getOSAndEnvironmentName() const;
260 /// @name Convenience Predicates
263 /// \brief Test whether the architecture is 64-bit
265 /// Note that this tests for 64-bit pointer width, and nothing else. Note
266 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
267 /// 16-bit. The inner details of pointer width for particular architectures
268 /// is not summed up in the triple, and so only a coarse grained predicate
269 /// system is provided.
270 bool isArch64Bit() const;
272 /// \brief Test whether the architecture is 32-bit
274 /// Note that this tests for 32-bit pointer width, and nothing else.
275 bool isArch32Bit() const;
277 /// \brief Test whether the architecture is 16-bit
279 /// Note that this tests for 16-bit pointer width, and nothing else.
280 bool isArch16Bit() const;
282 /// isOSVersionLT - Helper function for doing comparisons against version
283 /// numbers included in the target triple.
284 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
285 unsigned Micro = 0) const {
287 getOSVersion(LHS[0], LHS[1], LHS[2]);
290 return LHS[0] < Major;
292 return LHS[1] < Minor;
294 return LHS[1] < Micro;
299 /// isMacOSXVersionLT - Comparison function for checking OS X version
300 /// compatibility, which handles supporting skewed version numbering schemes
301 /// used by the "darwin" triples.
302 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
303 unsigned Micro = 0) const {
304 assert(isMacOSX() && "Not an OS X triple!");
306 // If this is OS X, expect a sane version number.
307 if (getOS() == Triple::MacOSX)
308 return isOSVersionLT(Major, Minor, Micro);
310 // Otherwise, compare to the "Darwin" number.
311 assert(Major == 10 && "Unexpected major version");
312 return isOSVersionLT(Minor + 4, Micro, 0);
315 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
316 /// "darwin" and "osx" as OS X triples.
317 bool isMacOSX() const {
318 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
321 /// Is this an iOS triple.
323 return getOS() == Triple::IOS;
326 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
327 bool isOSDarwin() const {
328 return isMacOSX() || isiOS();
331 /// \brief Tests for either Cygwin or MinGW OS
332 bool isOSCygMing() const {
333 return getOS() == Triple::Cygwin || getOS() == Triple::MinGW32;
336 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
337 bool isOSMSVCRT() const {
338 return getOS() == Triple::Win32 || getOS() == Triple::MinGW32;
341 /// \brief Tests whether the OS is Windows.
342 bool isOSWindows() const {
343 return getOS() == Triple::Win32 || isOSCygMing();
346 /// \brief Tests whether the OS is NaCl (Native Client)
347 bool isOSNaCl() const {
348 return getOS() == Triple::NaCl;
351 /// \brief Tests whether the OS is Linux.
352 bool isOSLinux() const {
353 return getOS() == Triple::Linux;
356 /// \brief Tests whether the OS uses the ELF binary format.
357 bool isOSBinFormatELF() const {
358 return getObjectFormat() == Triple::ELF;
361 /// \brief Tests whether the OS uses the COFF binary format.
362 bool isOSBinFormatCOFF() const {
363 return getObjectFormat() == Triple::COFF;
366 /// \brief Tests whether the environment is MachO.
367 bool isOSBinFormatMachO() const {
368 return getObjectFormat() == Triple::MachO;
375 /// setArch - Set the architecture (first) component of the triple
377 void setArch(ArchType Kind);
379 /// setVendor - Set the vendor (second) component of the triple to a
381 void setVendor(VendorType Kind);
383 /// setOS - Set the operating system (third) component of the triple
385 void setOS(OSType Kind);
387 /// setEnvironment - Set the environment (fourth) component of the triple
389 void setEnvironment(EnvironmentType Kind);
391 /// setObjectFormat - Set the object file format
392 void setObjectFormat(ObjectFormatType Kind);
394 /// setTriple - Set all components to the new triple \p Str.
395 void setTriple(const Twine &Str);
397 /// setArchName - Set the architecture (first) component of the
399 void setArchName(StringRef Str);
401 /// setVendorName - Set the vendor (second) component of the triple
403 void setVendorName(StringRef Str);
405 /// setOSName - Set the operating system (third) component of the
407 void setOSName(StringRef Str);
409 /// setEnvironmentName - Set the optional environment (fourth)
410 /// component of the triple by name.
411 void setEnvironmentName(StringRef Str);
413 /// setOSAndEnvironmentName - Set the operating system and optional
414 /// environment components with a single string.
415 void setOSAndEnvironmentName(StringRef Str);
417 /// getArchNameForAssembler - Get an architecture name that is understood by
418 /// the target assembler.
419 const char *getArchNameForAssembler();
422 /// @name Helpers to build variants of a particular triple.
425 /// \brief Form a triple with a 32-bit variant of the current architecture.
427 /// This can be used to move across "families" of architectures where useful.
429 /// \returns A new triple with a 32-bit architecture or an unknown
430 /// architecture if no such variant can be found.
431 llvm::Triple get32BitArchVariant() const;
433 /// \brief Form a triple with a 64-bit variant of the current architecture.
435 /// This can be used to move across "families" of architectures where useful.
437 /// \returns A new triple with a 64-bit architecture or an unknown
438 /// architecture if no such variant can be found.
439 llvm::Triple get64BitArchVariant() const;
442 /// @name Static helpers for IDs.
445 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
446 static const char *getArchTypeName(ArchType Kind);
448 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
449 /// architecture. This is the prefix used by the architecture specific
450 /// builtins, and is suitable for passing to \see
451 /// Intrinsic::getIntrinsicForGCCBuiltin().
453 /// \return - The architecture prefix, or 0 if none is defined.
454 static const char *getArchTypePrefix(ArchType Kind);
456 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
457 static const char *getVendorTypeName(VendorType Kind);
459 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
460 static const char *getOSTypeName(OSType Kind);
462 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
464 static const char *getEnvironmentTypeName(EnvironmentType Kind);
467 /// @name Static helpers for converting alternate architecture names.
470 /// getArchTypeForLLVMName - The canonical type for the given LLVM
471 /// architecture name (e.g., "x86").
472 static ArchType getArchTypeForLLVMName(StringRef Str);
477 } // End llvm namespace