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 autoconf configuration names. For
23 /// historical reasons, we also call these 'triples' (they used to contain
24 /// exactly three fields).
26 /// Configuration names are strings in the canonical form:
27 /// ARCHITECTURE-VENDOR-OPERATING_SYSTEM
29 /// ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
31 /// This class is used for clients which want to support arbitrary
32 /// configuration names, but also want to implement certain special
33 /// behavior for particular configurations. This class isolates the mapping
34 /// from the components of the configuration name to well known IDs.
36 /// At its core the Triple class is designed to be a wrapper for a triple
37 /// string; the constructor does not change or normalize the triple string.
38 /// Clients that need to handle the non-canonical triples that users often
39 /// specify should use the normalize method.
41 /// See autoconf/config.guess for a glimpse into what configuration names
42 /// look like in practice.
48 arm, // ARM: arm, armv.*, xscale
49 aarch64, // AArch64: aarch64
50 hexagon, // Hexagon: hexagon
51 mips, // MIPS: mips, mipsallegrex
52 mipsel, // MIPSEL: mipsel, mipsallegrexel
53 mips64, // MIPS64: mips64
54 mips64el,// MIPS64EL: mips64el
55 msp430, // MSP430: msp430
57 ppc64, // PPC64: powerpc64, ppu
58 r600, // R600: AMD GPUs HD2XXX - HD6XXX
59 sparc, // Sparc: sparc
60 sparcv9, // Sparcv9: Sparcv9
61 systemz, // SystemZ: s390x
62 tce, // TCE (http://tce.cs.tut.fi/): tce
63 thumb, // Thumb: thumb, thumbv.*
65 x86_64, // X86-64: amd64, x86_64
66 xcore, // XCore: xcore
67 mblaze, // MBlaze: mblaze
68 nvptx, // NVPTX: 32-bit
69 nvptx64, // NVPTX: 64-bit
70 le32, // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
71 amdil, // amdil: amd IL
72 spir, // SPIR: standard portable IR for OpenCL 32-bit version
73 spir64 // SPIR: standard portable IR for OpenCL 64-bit version
100 MinGW32, // i*86-pc-mingw32, *-w64-mingw32
108 NaCl, // Native Client
109 CNK, // BG/P Compute-Node Kernel
113 NVCL // NVIDIA OpenCL
115 enum EnvironmentType {
131 /// The parsed arch type.
134 /// The parsed vendor type.
137 /// The parsed OS type.
140 /// The parsed Environment type.
141 EnvironmentType Environment;
144 /// @name Constructors
147 /// \brief Default constructor is the same as an empty string and leaves all
148 /// triple fields unknown.
149 Triple() : Data(), Arch(), Vendor(), OS(), Environment() {}
151 explicit Triple(const Twine &Str);
152 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
153 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
154 const Twine &EnvironmentStr);
157 /// @name Normalization
160 /// normalize - Turn an arbitrary machine specification into the canonical
161 /// triple form (or something sensible that the Triple class understands if
162 /// nothing better can reasonably be done). In particular, it handles the
163 /// common case in which otherwise valid components are in the wrong order.
164 static std::string normalize(StringRef Str);
167 /// @name Typed Component Access
170 /// getArch - Get the parsed architecture type of this triple.
171 ArchType getArch() const { return Arch; }
173 /// getVendor - Get the parsed vendor type of this triple.
174 VendorType getVendor() const { return Vendor; }
176 /// getOS - Get the parsed operating system type of this triple.
177 OSType getOS() const { return OS; }
179 /// hasEnvironment - Does this triple have the optional environment
180 /// (fourth) component?
181 bool hasEnvironment() const {
182 return getEnvironmentName() != "";
185 /// getEnvironment - Get the parsed environment type of this triple.
186 EnvironmentType getEnvironment() const { return Environment; }
188 /// getOSVersion - Parse the version number from the OS name component of the
189 /// triple, if present.
191 /// For example, "fooos1.2.3" would return (1, 2, 3).
193 /// If an entry is not defined, it will be returned as 0.
194 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
196 /// getOSMajorVersion - Return just the major version number, this is
197 /// specialized because it is a common query.
198 unsigned getOSMajorVersion() const {
199 unsigned Maj, Min, Micro;
200 getOSVersion(Maj, Min, Micro);
204 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
205 /// translate generic "darwin" versions to the corresponding OS X versions.
206 /// This may also be called with IOS triples but the OS X version number is
207 /// just set to a constant 10.4.0 in that case. Returns true if successful.
208 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
209 unsigned &Micro) const;
211 /// getiOSVersion - Parse the version number as with getOSVersion. This should
212 /// only be called with IOS triples.
213 void getiOSVersion(unsigned &Major, unsigned &Minor,
214 unsigned &Micro) const;
217 /// @name Direct Component Access
220 const std::string &str() const { return Data; }
222 const std::string &getTriple() const { return Data; }
224 /// getArchName - Get the architecture (first) component of the
226 StringRef getArchName() const;
228 /// getVendorName - Get the vendor (second) component of the triple.
229 StringRef getVendorName() const;
231 /// getOSName - Get the operating system (third) component of the
233 StringRef getOSName() const;
235 /// getEnvironmentName - Get the optional environment (fourth)
236 /// component of the triple, or "" if empty.
237 StringRef getEnvironmentName() const;
239 /// getOSAndEnvironmentName - Get the operating system and optional
240 /// environment components as a single string (separated by a '-'
241 /// if the environment component is present).
242 StringRef getOSAndEnvironmentName() const;
245 /// @name Convenience Predicates
248 /// \brief Test whether the architecture is 64-bit
250 /// Note that this tests for 64-bit pointer width, and nothing else. Note
251 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
252 /// 16-bit. The inner details of pointer width for particular architectures
253 /// is not summed up in the triple, and so only a coarse grained predicate
254 /// system is provided.
255 bool isArch64Bit() const;
257 /// \brief Test whether the architecture is 32-bit
259 /// Note that this tests for 32-bit pointer width, and nothing else.
260 bool isArch32Bit() const;
262 /// \brief Test whether the architecture is 16-bit
264 /// Note that this tests for 16-bit pointer width, and nothing else.
265 bool isArch16Bit() const;
267 /// isOSVersionLT - Helper function for doing comparisons against version
268 /// numbers included in the target triple.
269 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
270 unsigned Micro = 0) const {
272 getOSVersion(LHS[0], LHS[1], LHS[2]);
275 return LHS[0] < Major;
277 return LHS[1] < Minor;
279 return LHS[1] < Micro;
284 /// isMacOSXVersionLT - Comparison function for checking OS X version
285 /// compatibility, which handles supporting skewed version numbering schemes
286 /// used by the "darwin" triples.
287 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
288 unsigned Micro = 0) const {
289 assert(isMacOSX() && "Not an OS X triple!");
291 // If this is OS X, expect a sane version number.
292 if (getOS() == Triple::MacOSX)
293 return isOSVersionLT(Major, Minor, Micro);
295 // Otherwise, compare to the "Darwin" number.
296 assert(Major == 10 && "Unexpected major version");
297 return isOSVersionLT(Minor + 4, Micro, 0);
300 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
301 /// "darwin" and "osx" as OS X triples.
302 bool isMacOSX() const {
303 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
306 /// Is this an iOS triple.
308 return getOS() == Triple::IOS;
311 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
312 bool isOSDarwin() const {
313 return isMacOSX() || isiOS();
316 /// \brief Tests for either Cygwin or MinGW OS
317 bool isOSCygMing() const {
318 return getOS() == Triple::Cygwin || getOS() == Triple::MinGW32;
321 /// isOSWindows - Is this a "Windows" OS.
322 bool isOSWindows() const {
323 return getOS() == Triple::Win32 || isOSCygMing();
326 /// \brief Tests whether the OS is NaCl (Native Client)
327 bool isOSNaCl() const {
328 return getOS() == Triple::NaCl;
331 /// \brief Tests whether the OS uses the ELF binary format.
332 bool isOSBinFormatELF() const {
333 return !isOSDarwin() && !isOSWindows();
336 /// \brief Tests whether the OS uses the COFF binary format.
337 bool isOSBinFormatCOFF() const {
338 return isOSWindows();
341 /// \brief Tests whether the environment is MachO.
342 // FIXME: Should this be an OSBinFormat predicate?
343 bool isEnvironmentMachO() const {
344 return getEnvironment() == Triple::MachO || isOSDarwin();
351 /// setArch - Set the architecture (first) component of the triple
353 void setArch(ArchType Kind);
355 /// setVendor - Set the vendor (second) component of the triple to a
357 void setVendor(VendorType Kind);
359 /// setOS - Set the operating system (third) component of the triple
361 void setOS(OSType Kind);
363 /// setEnvironment - Set the environment (fourth) component of the triple
365 void setEnvironment(EnvironmentType Kind);
367 /// setTriple - Set all components to the new triple \p Str.
368 void setTriple(const Twine &Str);
370 /// setArchName - Set the architecture (first) component of the
372 void setArchName(StringRef Str);
374 /// setVendorName - Set the vendor (second) component of the triple
376 void setVendorName(StringRef Str);
378 /// setOSName - Set the operating system (third) component of the
380 void setOSName(StringRef Str);
382 /// setEnvironmentName - Set the optional environment (fourth)
383 /// component of the triple by name.
384 void setEnvironmentName(StringRef Str);
386 /// setOSAndEnvironmentName - Set the operating system and optional
387 /// environment components with a single string.
388 void setOSAndEnvironmentName(StringRef Str);
390 /// getArchNameForAssembler - Get an architecture name that is understood by
391 /// the target assembler.
392 const char *getArchNameForAssembler();
395 /// @name Helpers to build variants of a particular triple.
398 /// \brief Form a triple with a 32-bit variant of the current architecture.
400 /// This can be used to move across "families" of architectures where useful.
402 /// \returns A new triple with a 32-bit architecture or an unknown
403 /// architecture if no such variant can be found.
404 llvm::Triple get32BitArchVariant() const;
406 /// \brief Form a triple with a 64-bit variant of the current architecture.
408 /// This can be used to move across "families" of architectures where useful.
410 /// \returns A new triple with a 64-bit architecture or an unknown
411 /// architecture if no such variant can be found.
412 llvm::Triple get64BitArchVariant() const;
415 /// @name Static helpers for IDs.
418 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
419 static const char *getArchTypeName(ArchType Kind);
421 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
422 /// architecture. This is the prefix used by the architecture specific
423 /// builtins, and is suitable for passing to \see
424 /// Intrinsic::getIntrinsicForGCCBuiltin().
426 /// \return - The architecture prefix, or 0 if none is defined.
427 static const char *getArchTypePrefix(ArchType Kind);
429 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
430 static const char *getVendorTypeName(VendorType Kind);
432 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
433 static const char *getOSTypeName(OSType Kind);
435 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
437 static const char *getEnvironmentTypeName(EnvironmentType Kind);
440 /// @name Static helpers for converting alternate architecture names.
443 /// getArchTypeForLLVMName - The canonical type for the given LLVM
444 /// architecture name (e.g., "x86").
445 static ArchType getArchTypeForLLVMName(StringRef Str);
450 } // End llvm namespace