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: aarch64
51 hexagon, // Hexagon: hexagon
52 mips, // MIPS: mips, mipsallegrex
53 mipsel, // MIPSEL: mipsel, mipsallegrexel
54 mips64, // MIPS64: mips64
55 mips64el,// MIPS64EL: mips64el
56 msp430, // MSP430: msp430
58 ppc64, // PPC64: powerpc64, ppu
59 ppc64le, // PPC64LE: powerpc64le
60 r600, // R600: AMD GPUs HD2XXX - HD6XXX
61 sparc, // Sparc: sparc
62 sparcv9, // Sparcv9: Sparcv9
63 systemz, // SystemZ: s390x
64 tce, // TCE (http://tce.cs.tut.fi/): tce
65 thumb, // Thumb: thumb, thumbv.*
67 x86_64, // X86-64: amd64, x86_64
68 xcore, // XCore: xcore
69 nvptx, // NVPTX: 32-bit
70 nvptx64, // NVPTX: 64-bit
71 le32, // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
72 amdil, // amdil: amd IL
73 spir, // SPIR: standard portable IR for OpenCL 32-bit version
74 spir64 // SPIR: standard portable IR for OpenCL 64-bit version
101 MinGW32, // i*86-pc-mingw32, *-w64-mingw32
109 NaCl, // Native Client
110 CNK, // BG/P Compute-Node Kernel
114 NVCL // NVIDIA OpenCL
116 enum EnvironmentType {
132 /// The parsed arch type.
135 /// The parsed vendor type.
138 /// The parsed OS type.
141 /// The parsed Environment type.
142 EnvironmentType Environment;
145 /// @name Constructors
148 /// \brief Default constructor is the same as an empty string and leaves all
149 /// triple fields unknown.
150 Triple() : Data(), Arch(), Vendor(), OS(), Environment() {}
152 explicit Triple(const Twine &Str);
153 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
154 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
155 const Twine &EnvironmentStr);
158 /// @name Normalization
161 /// normalize - Turn an arbitrary machine specification into the canonical
162 /// triple form (or something sensible that the Triple class understands if
163 /// nothing better can reasonably be done). In particular, it handles the
164 /// common case in which otherwise valid components are in the wrong order.
165 static std::string normalize(StringRef Str);
168 /// @name Typed Component Access
171 /// getArch - Get the parsed architecture type of this triple.
172 ArchType getArch() const { return Arch; }
174 /// getVendor - Get the parsed vendor type of this triple.
175 VendorType getVendor() const { return Vendor; }
177 /// getOS - Get the parsed operating system type of this triple.
178 OSType getOS() const { return OS; }
180 /// hasEnvironment - Does this triple have the optional environment
181 /// (fourth) component?
182 bool hasEnvironment() const {
183 return getEnvironmentName() != "";
186 /// getEnvironment - Get the parsed environment type of this triple.
187 EnvironmentType getEnvironment() const { return Environment; }
189 /// getOSVersion - Parse the version number from the OS name component of the
190 /// triple, if present.
192 /// For example, "fooos1.2.3" would return (1, 2, 3).
194 /// If an entry is not defined, it will be returned as 0.
195 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
197 /// getOSMajorVersion - Return just the major version number, this is
198 /// specialized because it is a common query.
199 unsigned getOSMajorVersion() const {
200 unsigned Maj, Min, Micro;
201 getOSVersion(Maj, Min, Micro);
205 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
206 /// translate generic "darwin" versions to the corresponding OS X versions.
207 /// This may also be called with IOS triples but the OS X version number is
208 /// just set to a constant 10.4.0 in that case. Returns true if successful.
209 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
210 unsigned &Micro) const;
212 /// getiOSVersion - Parse the version number as with getOSVersion. This should
213 /// only be called with IOS triples.
214 void getiOSVersion(unsigned &Major, unsigned &Minor,
215 unsigned &Micro) const;
218 /// @name Direct Component Access
221 const std::string &str() const { return Data; }
223 const std::string &getTriple() const { return Data; }
225 /// getArchName - Get the architecture (first) component of the
227 StringRef getArchName() const;
229 /// getVendorName - Get the vendor (second) component of the triple.
230 StringRef getVendorName() const;
232 /// getOSName - Get the operating system (third) component of the
234 StringRef getOSName() const;
236 /// getEnvironmentName - Get the optional environment (fourth)
237 /// component of the triple, or "" if empty.
238 StringRef getEnvironmentName() const;
240 /// getOSAndEnvironmentName - Get the operating system and optional
241 /// environment components as a single string (separated by a '-'
242 /// if the environment component is present).
243 StringRef getOSAndEnvironmentName() const;
246 /// @name Convenience Predicates
249 /// \brief Test whether the architecture is 64-bit
251 /// Note that this tests for 64-bit pointer width, and nothing else. Note
252 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
253 /// 16-bit. The inner details of pointer width for particular architectures
254 /// is not summed up in the triple, and so only a coarse grained predicate
255 /// system is provided.
256 bool isArch64Bit() const;
258 /// \brief Test whether the architecture is 32-bit
260 /// Note that this tests for 32-bit pointer width, and nothing else.
261 bool isArch32Bit() const;
263 /// \brief Test whether the architecture is 16-bit
265 /// Note that this tests for 16-bit pointer width, and nothing else.
266 bool isArch16Bit() const;
268 /// isOSVersionLT - Helper function for doing comparisons against version
269 /// numbers included in the target triple.
270 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
271 unsigned Micro = 0) const {
273 getOSVersion(LHS[0], LHS[1], LHS[2]);
276 return LHS[0] < Major;
278 return LHS[1] < Minor;
280 return LHS[1] < Micro;
285 /// isMacOSXVersionLT - Comparison function for checking OS X version
286 /// compatibility, which handles supporting skewed version numbering schemes
287 /// used by the "darwin" triples.
288 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
289 unsigned Micro = 0) const {
290 assert(isMacOSX() && "Not an OS X triple!");
292 // If this is OS X, expect a sane version number.
293 if (getOS() == Triple::MacOSX)
294 return isOSVersionLT(Major, Minor, Micro);
296 // Otherwise, compare to the "Darwin" number.
297 assert(Major == 10 && "Unexpected major version");
298 return isOSVersionLT(Minor + 4, Micro, 0);
301 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
302 /// "darwin" and "osx" as OS X triples.
303 bool isMacOSX() const {
304 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
307 /// Is this an iOS triple.
309 return getOS() == Triple::IOS;
312 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
313 bool isOSDarwin() const {
314 return isMacOSX() || isiOS();
317 /// \brief Tests for either Cygwin or MinGW OS
318 bool isOSCygMing() const {
319 return getOS() == Triple::Cygwin || getOS() == Triple::MinGW32;
322 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
323 bool isOSMSVCRT() const {
324 return getOS() == Triple::Win32 || getOS() == Triple::MinGW32;
327 /// \brief Tests whether the OS is Windows.
328 bool isOSWindows() const {
329 return getOS() == Triple::Win32 || isOSCygMing();
332 /// \brief Tests whether the OS is NaCl (Native Client)
333 bool isOSNaCl() const {
334 return getOS() == Triple::NaCl;
337 /// \brief Tests whether the OS is Linux.
338 bool isOSLinux() const {
339 return getOS() == Triple::Linux;
342 /// \brief Tests whether the OS uses the ELF binary format.
343 bool isOSBinFormatELF() const {
344 return !isOSDarwin() && !isOSWindows();
347 /// \brief Tests whether the OS uses the COFF binary format.
348 bool isOSBinFormatCOFF() const {
349 return isOSWindows();
352 /// \brief Tests whether the environment is MachO.
353 // FIXME: Should this be an OSBinFormat predicate?
354 bool isEnvironmentMachO() const {
355 return getEnvironment() == Triple::MachO || isOSDarwin();
362 /// setArch - Set the architecture (first) component of the triple
364 void setArch(ArchType Kind);
366 /// setVendor - Set the vendor (second) component of the triple to a
368 void setVendor(VendorType Kind);
370 /// setOS - Set the operating system (third) component of the triple
372 void setOS(OSType Kind);
374 /// setEnvironment - Set the environment (fourth) component of the triple
376 void setEnvironment(EnvironmentType Kind);
378 /// setTriple - Set all components to the new triple \p Str.
379 void setTriple(const Twine &Str);
381 /// setArchName - Set the architecture (first) component of the
383 void setArchName(StringRef Str);
385 /// setVendorName - Set the vendor (second) component of the triple
387 void setVendorName(StringRef Str);
389 /// setOSName - Set the operating system (third) component of the
391 void setOSName(StringRef Str);
393 /// setEnvironmentName - Set the optional environment (fourth)
394 /// component of the triple by name.
395 void setEnvironmentName(StringRef Str);
397 /// setOSAndEnvironmentName - Set the operating system and optional
398 /// environment components with a single string.
399 void setOSAndEnvironmentName(StringRef Str);
401 /// getArchNameForAssembler - Get an architecture name that is understood by
402 /// the target assembler.
403 const char *getArchNameForAssembler();
406 /// @name Helpers to build variants of a particular triple.
409 /// \brief Form a triple with a 32-bit variant of the current architecture.
411 /// This can be used to move across "families" of architectures where useful.
413 /// \returns A new triple with a 32-bit architecture or an unknown
414 /// architecture if no such variant can be found.
415 llvm::Triple get32BitArchVariant() const;
417 /// \brief Form a triple with a 64-bit variant of the current architecture.
419 /// This can be used to move across "families" of architectures where useful.
421 /// \returns A new triple with a 64-bit architecture or an unknown
422 /// architecture if no such variant can be found.
423 llvm::Triple get64BitArchVariant() const;
426 /// @name Static helpers for IDs.
429 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
430 static const char *getArchTypeName(ArchType Kind);
432 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
433 /// architecture. This is the prefix used by the architecture specific
434 /// builtins, and is suitable for passing to \see
435 /// Intrinsic::getIntrinsicForGCCBuiltin().
437 /// \return - The architecture prefix, or 0 if none is defined.
438 static const char *getArchTypePrefix(ArchType Kind);
440 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
441 static const char *getVendorTypeName(VendorType Kind);
443 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
444 static const char *getOSTypeName(OSType Kind);
446 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
448 static const char *getEnvironmentTypeName(EnvironmentType Kind);
451 /// @name Static helpers for converting alternate architecture names.
454 /// getArchTypeForLLVMName - The canonical type for the given LLVM
455 /// architecture name (e.g., "x86").
456 static ArchType getArchTypeForLLVMName(StringRef Str);
461 } // End llvm namespace