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.
22 /// Triple - Helper class for working with target triples.
24 /// Target triples are strings in the canonical form:
25 /// ARCHITECTURE-VENDOR-OPERATING_SYSTEM
27 /// ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
29 /// This class is used for clients which want to support arbitrary
30 /// target triples, but also want to implement certain special
31 /// behavior for particular targets. This class isolates the mapping
32 /// from the components of the target triple to well known IDs.
34 /// At its core the Triple class is designed to be a wrapper for a triple
35 /// string; the constructor does not change or normalize the triple string.
36 /// Clients that need to handle the non-canonical triples that users often
37 /// specify should use the normalize method.
39 /// See autoconf/config.guess for a glimpse into what triples look like in
46 arm, // ARM: arm, armv.*, xscale
47 aarch64, // AArch64: aarch64
48 hexagon, // Hexagon: hexagon
49 mips, // MIPS: mips, mipsallegrex
50 mipsel, // MIPSEL: mipsel, mipsallegrexel
51 mips64, // MIPS64: mips64
52 mips64el,// MIPS64EL: mips64el
53 msp430, // MSP430: msp430
55 ppc64, // PPC64: powerpc64, ppu
56 r600, // R600: AMD GPUs HD2XXX - HD6XXX
57 sparc, // Sparc: sparc
58 sparcv9, // Sparcv9: Sparcv9
59 systemz, // SystemZ: s390x
60 tce, // TCE (http://tce.cs.tut.fi/): tce
61 thumb, // Thumb: thumb, thumbv.*
63 x86_64, // X86-64: amd64, x86_64
64 xcore, // XCore: xcore
65 mblaze, // MBlaze: mblaze
66 nvptx, // NVPTX: 32-bit
67 nvptx64, // NVPTX: 64-bit
68 le32, // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
69 amdil, // amdil: amd IL
70 spir, // SPIR: standard portable IR for OpenCL 32-bit version
71 spir64 // SPIR: standard portable IR for OpenCL 64-bit version
97 MinGW32, // i*86-pc-mingw32, *-w64-mingw32
105 NaCl, // Native Client
106 CNK, // BG/P Compute-Node Kernel
110 enum EnvironmentType {
126 /// The parsed arch type.
129 /// The parsed vendor type.
132 /// The parsed OS type.
135 /// The parsed Environment type.
136 EnvironmentType Environment;
139 /// @name Constructors
142 /// \brief Default constructor is the same as an empty string and leaves all
143 /// triple fields unknown.
144 Triple() : Data(), Arch(), Vendor(), OS(), Environment() {}
146 explicit Triple(const Twine &Str);
147 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
148 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
149 const Twine &EnvironmentStr);
152 /// @name Normalization
155 /// normalize - Turn an arbitrary machine specification into the canonical
156 /// triple form (or something sensible that the Triple class understands if
157 /// nothing better can reasonably be done). In particular, it handles the
158 /// common case in which otherwise valid components are in the wrong order.
159 static std::string normalize(StringRef Str);
162 /// @name Typed Component Access
165 /// getArch - Get the parsed architecture type of this triple.
166 ArchType getArch() const { return Arch; }
168 /// getVendor - Get the parsed vendor type of this triple.
169 VendorType getVendor() const { return Vendor; }
171 /// getOS - Get the parsed operating system type of this triple.
172 OSType getOS() const { return OS; }
174 /// hasEnvironment - Does this triple have the optional environment
175 /// (fourth) component?
176 bool hasEnvironment() const {
177 return getEnvironmentName() != "";
180 /// getEnvironment - Get the parsed environment type of this triple.
181 EnvironmentType getEnvironment() const { return Environment; }
183 /// getOSVersion - Parse the version number from the OS name component of the
184 /// triple, if present.
186 /// For example, "fooos1.2.3" would return (1, 2, 3).
188 /// If an entry is not defined, it will be returned as 0.
189 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
191 /// getOSMajorVersion - Return just the major version number, this is
192 /// specialized because it is a common query.
193 unsigned getOSMajorVersion() const {
194 unsigned Maj, Min, Micro;
195 getOSVersion(Maj, Min, Micro);
199 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
200 /// translate generic "darwin" versions to the corresponding OS X versions.
201 /// This may also be called with IOS triples but the OS X version number is
202 /// just set to a constant 10.4.0 in that case. Returns true if successful.
203 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
204 unsigned &Micro) const;
206 /// getiOSVersion - Parse the version number as with getOSVersion. This should
207 /// only be called with IOS triples.
208 void getiOSVersion(unsigned &Major, unsigned &Minor,
209 unsigned &Micro) const;
212 /// @name Direct Component Access
215 const std::string &str() const { return Data; }
217 const std::string &getTriple() const { return Data; }
219 /// getArchName - Get the architecture (first) component of the
221 StringRef getArchName() const;
223 /// getVendorName - Get the vendor (second) component of the triple.
224 StringRef getVendorName() const;
226 /// getOSName - Get the operating system (third) component of the
228 StringRef getOSName() const;
230 /// getEnvironmentName - Get the optional environment (fourth)
231 /// component of the triple, or "" if empty.
232 StringRef getEnvironmentName() const;
234 /// getOSAndEnvironmentName - Get the operating system and optional
235 /// environment components as a single string (separated by a '-'
236 /// if the environment component is present).
237 StringRef getOSAndEnvironmentName() const;
240 /// @name Convenience Predicates
243 /// \brief Test whether the architecture is 64-bit
245 /// Note that this tests for 64-bit pointer width, and nothing else. Note
246 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
247 /// 16-bit. The inner details of pointer width for particular architectures
248 /// is not summed up in the triple, and so only a coarse grained predicate
249 /// system is provided.
250 bool isArch64Bit() const;
252 /// \brief Test whether the architecture is 32-bit
254 /// Note that this tests for 32-bit pointer width, and nothing else.
255 bool isArch32Bit() const;
257 /// \brief Test whether the architecture is 16-bit
259 /// Note that this tests for 16-bit pointer width, and nothing else.
260 bool isArch16Bit() const;
262 /// isOSVersionLT - Helper function for doing comparisons against version
263 /// numbers included in the target triple.
264 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
265 unsigned Micro = 0) const {
267 getOSVersion(LHS[0], LHS[1], LHS[2]);
270 return LHS[0] < Major;
272 return LHS[1] < Minor;
274 return LHS[1] < Micro;
279 /// isMacOSXVersionLT - Comparison function for checking OS X version
280 /// compatibility, which handles supporting skewed version numbering schemes
281 /// used by the "darwin" triples.
282 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
283 unsigned Micro = 0) const {
284 assert(isMacOSX() && "Not an OS X triple!");
286 // If this is OS X, expect a sane version number.
287 if (getOS() == Triple::MacOSX)
288 return isOSVersionLT(Major, Minor, Micro);
290 // Otherwise, compare to the "Darwin" number.
291 assert(Major == 10 && "Unexpected major version");
292 return isOSVersionLT(Minor + 4, Micro, 0);
295 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
296 /// "darwin" and "osx" as OS X triples.
297 bool isMacOSX() const {
298 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
301 /// Is this an iOS triple.
303 return getOS() == Triple::IOS;
306 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
307 bool isOSDarwin() const {
308 return isMacOSX() || isiOS();
311 /// \brief Tests for either Cygwin or MinGW OS
312 bool isOSCygMing() const {
313 return getOS() == Triple::Cygwin || getOS() == Triple::MinGW32;
316 /// isOSWindows - Is this a "Windows" OS.
317 bool isOSWindows() const {
318 return getOS() == Triple::Win32 || isOSCygMing();
321 /// \brief Tests whether the OS is NaCl (Native Client)
322 bool isOSNaCl() const {
323 return getOS() == Triple::NaCl;
326 /// \brief Tests whether the OS uses the ELF binary format.
327 bool isOSBinFormatELF() const {
328 return !isOSDarwin() && !isOSWindows();
331 /// \brief Tests whether the OS uses the COFF binary format.
332 bool isOSBinFormatCOFF() const {
333 return isOSWindows();
336 /// \brief Tests whether the environment is MachO.
337 // FIXME: Should this be an OSBinFormat predicate?
338 bool isEnvironmentMachO() const {
339 return getEnvironment() == Triple::MachO || isOSDarwin();
346 /// setArch - Set the architecture (first) component of the triple
348 void setArch(ArchType Kind);
350 /// setVendor - Set the vendor (second) component of the triple to a
352 void setVendor(VendorType Kind);
354 /// setOS - Set the operating system (third) component of the triple
356 void setOS(OSType Kind);
358 /// setEnvironment - Set the environment (fourth) component of the triple
360 void setEnvironment(EnvironmentType Kind);
362 /// setTriple - Set all components to the new triple \p Str.
363 void setTriple(const Twine &Str);
365 /// setArchName - Set the architecture (first) component of the
367 void setArchName(StringRef Str);
369 /// setVendorName - Set the vendor (second) component of the triple
371 void setVendorName(StringRef Str);
373 /// setOSName - Set the operating system (third) component of the
375 void setOSName(StringRef Str);
377 /// setEnvironmentName - Set the optional environment (fourth)
378 /// component of the triple by name.
379 void setEnvironmentName(StringRef Str);
381 /// setOSAndEnvironmentName - Set the operating system and optional
382 /// environment components with a single string.
383 void setOSAndEnvironmentName(StringRef Str);
385 /// getArchNameForAssembler - Get an architecture name that is understood by
386 /// the target assembler.
387 const char *getArchNameForAssembler();
390 /// @name Helpers to build variants of a particular triple.
393 /// \brief Form a triple with a 32-bit variant of the current architecture.
395 /// This can be used to move across "families" of architectures where useful.
397 /// \returns A new triple with a 32-bit architecture or an unknown
398 /// architecture if no such variant can be found.
399 llvm::Triple get32BitArchVariant() const;
401 /// \brief Form a triple with a 64-bit variant of the current architecture.
403 /// This can be used to move across "families" of architectures where useful.
405 /// \returns A new triple with a 64-bit architecture or an unknown
406 /// architecture if no such variant can be found.
407 llvm::Triple get64BitArchVariant() const;
410 /// @name Static helpers for IDs.
413 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
414 static const char *getArchTypeName(ArchType Kind);
416 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
417 /// architecture. This is the prefix used by the architecture specific
418 /// builtins, and is suitable for passing to \see
419 /// Intrinsic::getIntrinsicForGCCBuiltin().
421 /// \return - The architecture prefix, or 0 if none is defined.
422 static const char *getArchTypePrefix(ArchType Kind);
424 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
425 static const char *getVendorTypeName(VendorType Kind);
427 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
428 static const char *getOSTypeName(OSType Kind);
430 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
432 static const char *getEnvironmentTypeName(EnvironmentType Kind);
435 /// @name Static helpers for converting alternate architecture names.
438 /// getArchTypeForLLVMName - The canonical type for the given LLVM
439 /// architecture name (e.g., "x86").
440 static ArchType getArchTypeForLLVMName(StringRef Str);
445 } // End llvm namespace