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 {
135 /// The parsed arch type.
138 /// The parsed vendor type.
141 /// The parsed OS type.
144 /// The parsed Environment type.
145 EnvironmentType Environment;
148 /// @name Constructors
151 /// \brief Default constructor is the same as an empty string and leaves all
152 /// triple fields unknown.
153 Triple() : Data(), Arch(), Vendor(), OS(), Environment() {}
155 explicit Triple(const Twine &Str);
156 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
157 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
158 const Twine &EnvironmentStr);
161 /// @name Normalization
164 /// normalize - Turn an arbitrary machine specification into the canonical
165 /// triple form (or something sensible that the Triple class understands if
166 /// nothing better can reasonably be done). In particular, it handles the
167 /// common case in which otherwise valid components are in the wrong order.
168 static std::string normalize(StringRef Str);
171 /// @name Typed Component Access
174 /// getArch - Get the parsed architecture type of this triple.
175 ArchType getArch() const { return Arch; }
177 /// getVendor - Get the parsed vendor type of this triple.
178 VendorType getVendor() const { return Vendor; }
180 /// getOS - Get the parsed operating system type of this triple.
181 OSType getOS() const { return OS; }
183 /// hasEnvironment - Does this triple have the optional environment
184 /// (fourth) component?
185 bool hasEnvironment() const {
186 return getEnvironmentName() != "";
189 /// getEnvironment - Get the parsed environment type of this triple.
190 EnvironmentType getEnvironment() const { return Environment; }
192 /// getOSVersion - Parse the version number from the OS name component of the
193 /// triple, if present.
195 /// For example, "fooos1.2.3" would return (1, 2, 3).
197 /// If an entry is not defined, it will be returned as 0.
198 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
200 /// getOSMajorVersion - Return just the major version number, this is
201 /// specialized because it is a common query.
202 unsigned getOSMajorVersion() const {
203 unsigned Maj, Min, Micro;
204 getOSVersion(Maj, Min, Micro);
208 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
209 /// translate generic "darwin" versions to the corresponding OS X versions.
210 /// This may also be called with IOS triples but the OS X version number is
211 /// just set to a constant 10.4.0 in that case. Returns true if successful.
212 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
213 unsigned &Micro) const;
215 /// getiOSVersion - Parse the version number as with getOSVersion. This should
216 /// only be called with IOS triples.
217 void getiOSVersion(unsigned &Major, unsigned &Minor,
218 unsigned &Micro) const;
221 /// @name Direct Component Access
224 const std::string &str() const { return Data; }
226 const std::string &getTriple() const { return Data; }
228 /// getArchName - Get the architecture (first) component of the
230 StringRef getArchName() const;
232 /// getVendorName - Get the vendor (second) component of the triple.
233 StringRef getVendorName() const;
235 /// getOSName - Get the operating system (third) component of the
237 StringRef getOSName() const;
239 /// getEnvironmentName - Get the optional environment (fourth)
240 /// component of the triple, or "" if empty.
241 StringRef getEnvironmentName() const;
243 /// getOSAndEnvironmentName - Get the operating system and optional
244 /// environment components as a single string (separated by a '-'
245 /// if the environment component is present).
246 StringRef getOSAndEnvironmentName() const;
249 /// @name Convenience Predicates
252 /// \brief Test whether the architecture is 64-bit
254 /// Note that this tests for 64-bit pointer width, and nothing else. Note
255 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
256 /// 16-bit. The inner details of pointer width for particular architectures
257 /// is not summed up in the triple, and so only a coarse grained predicate
258 /// system is provided.
259 bool isArch64Bit() const;
261 /// \brief Test whether the architecture is 32-bit
263 /// Note that this tests for 32-bit pointer width, and nothing else.
264 bool isArch32Bit() const;
266 /// \brief Test whether the architecture is 16-bit
268 /// Note that this tests for 16-bit pointer width, and nothing else.
269 bool isArch16Bit() const;
271 /// isOSVersionLT - Helper function for doing comparisons against version
272 /// numbers included in the target triple.
273 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
274 unsigned Micro = 0) const {
276 getOSVersion(LHS[0], LHS[1], LHS[2]);
279 return LHS[0] < Major;
281 return LHS[1] < Minor;
283 return LHS[1] < Micro;
288 /// isMacOSXVersionLT - Comparison function for checking OS X version
289 /// compatibility, which handles supporting skewed version numbering schemes
290 /// used by the "darwin" triples.
291 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
292 unsigned Micro = 0) const {
293 assert(isMacOSX() && "Not an OS X triple!");
295 // If this is OS X, expect a sane version number.
296 if (getOS() == Triple::MacOSX)
297 return isOSVersionLT(Major, Minor, Micro);
299 // Otherwise, compare to the "Darwin" number.
300 assert(Major == 10 && "Unexpected major version");
301 return isOSVersionLT(Minor + 4, Micro, 0);
304 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
305 /// "darwin" and "osx" as OS X triples.
306 bool isMacOSX() const {
307 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
310 /// Is this an iOS triple.
312 return getOS() == Triple::IOS;
315 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
316 bool isOSDarwin() const {
317 return isMacOSX() || isiOS();
320 /// \brief Tests for either Cygwin or MinGW OS
321 bool isOSCygMing() const {
322 return getOS() == Triple::Cygwin || getOS() == Triple::MinGW32;
325 /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
326 bool isOSMSVCRT() const {
327 return getOS() == Triple::Win32 || getOS() == Triple::MinGW32;
330 /// \brief Tests whether the OS is Windows.
331 bool isOSWindows() const {
332 return getOS() == Triple::Win32 || isOSCygMing();
335 /// \brief Tests whether the OS is NaCl (Native Client)
336 bool isOSNaCl() const {
337 return getOS() == Triple::NaCl;
340 /// \brief Tests whether the OS is Linux.
341 bool isOSLinux() const {
342 return getOS() == Triple::Linux;
345 /// \brief Tests whether the OS uses the ELF binary format.
346 bool isOSBinFormatELF() const {
347 return !isOSBinFormatMachO() && !isOSBinFormatCOFF();
350 /// \brief Tests whether the OS uses the COFF binary format.
351 bool isOSBinFormatCOFF() const {
352 return getEnvironment() != Triple::ELF &&
353 getEnvironment() != Triple::MachO && isOSWindows();
356 /// \brief Tests whether the environment is MachO.
357 bool isOSBinFormatMachO() const {
358 return getEnvironment() == Triple::MachO || isOSDarwin();
365 /// setArch - Set the architecture (first) component of the triple
367 void setArch(ArchType Kind);
369 /// setVendor - Set the vendor (second) component of the triple to a
371 void setVendor(VendorType Kind);
373 /// setOS - Set the operating system (third) component of the triple
375 void setOS(OSType Kind);
377 /// setEnvironment - Set the environment (fourth) component of the triple
379 void setEnvironment(EnvironmentType Kind);
381 /// setTriple - Set all components to the new triple \p Str.
382 void setTriple(const Twine &Str);
384 /// setArchName - Set the architecture (first) component of the
386 void setArchName(StringRef Str);
388 /// setVendorName - Set the vendor (second) component of the triple
390 void setVendorName(StringRef Str);
392 /// setOSName - Set the operating system (third) component of the
394 void setOSName(StringRef Str);
396 /// setEnvironmentName - Set the optional environment (fourth)
397 /// component of the triple by name.
398 void setEnvironmentName(StringRef Str);
400 /// setOSAndEnvironmentName - Set the operating system and optional
401 /// environment components with a single string.
402 void setOSAndEnvironmentName(StringRef Str);
404 /// getArchNameForAssembler - Get an architecture name that is understood by
405 /// the target assembler.
406 const char *getArchNameForAssembler();
409 /// @name Helpers to build variants of a particular triple.
412 /// \brief Form a triple with a 32-bit variant of the current architecture.
414 /// This can be used to move across "families" of architectures where useful.
416 /// \returns A new triple with a 32-bit architecture or an unknown
417 /// architecture if no such variant can be found.
418 llvm::Triple get32BitArchVariant() const;
420 /// \brief Form a triple with a 64-bit variant of the current architecture.
422 /// This can be used to move across "families" of architectures where useful.
424 /// \returns A new triple with a 64-bit architecture or an unknown
425 /// architecture if no such variant can be found.
426 llvm::Triple get64BitArchVariant() const;
429 /// @name Static helpers for IDs.
432 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
433 static const char *getArchTypeName(ArchType Kind);
435 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
436 /// architecture. This is the prefix used by the architecture specific
437 /// builtins, and is suitable for passing to \see
438 /// Intrinsic::getIntrinsicForGCCBuiltin().
440 /// \return - The architecture prefix, or 0 if none is defined.
441 static const char *getArchTypePrefix(ArchType Kind);
443 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
444 static const char *getVendorTypeName(VendorType Kind);
446 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
447 static const char *getOSTypeName(OSType Kind);
449 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
451 static const char *getEnvironmentTypeName(EnvironmentType Kind);
454 /// @name Static helpers for converting alternate architecture names.
457 /// getArchTypeForLLVMName - The canonical type for the given LLVM
458 /// architecture name (e.g., "x86").
459 static ArchType getArchTypeForLLVMName(StringRef Str);
464 } // End llvm namespace