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 hexagon, // Hexagon: hexagon
48 mips, // MIPS: mips, mipsallegrex
49 mipsel, // MIPSEL: mipsel, mipsallegrexel
50 mips64, // MIPS64: mips64
51 mips64el,// MIPS64EL: mips64el
52 msp430, // MSP430: msp430
54 ppc64, // PPC64: powerpc64, ppu
55 r600, // R600: AMD GPUs HD2XXX - HD6XXX
56 sparc, // Sparc: sparc
57 sparcv9, // Sparcv9: Sparcv9
58 tce, // TCE (http://tce.cs.tut.fi/): tce
59 thumb, // Thumb: thumb, thumbv.*
61 x86_64, // X86-64: amd64, x86_64
62 xcore, // XCore: xcore
63 mblaze, // MBlaze: mblaze
64 nvptx, // NVPTX: 32-bit
65 nvptx64, // NVPTX: 64-bit
66 le32, // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
67 amdil, // amdil: amd IL
68 spir, // SPIR: standard portable IR for OpenCL 32-bit version
69 spir64 // SPIR: standard portable IR for OpenCL 64-bit version
95 MinGW32, // i*86-pc-mingw32, *-w64-mingw32
103 NaCl, // Native Client
104 CNK, // BG/P Compute-Node Kernel
108 enum EnvironmentType {
123 /// The parsed arch type.
126 /// The parsed vendor type.
129 /// The parsed OS type.
132 /// The parsed Environment type.
133 EnvironmentType Environment;
136 /// @name Constructors
139 /// \brief Default constructor is the same as an empty string and leaves all
140 /// triple fields unknown.
141 Triple() : Data(), Arch(), Vendor(), OS(), Environment() {}
143 explicit Triple(const Twine &Str);
144 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
145 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
146 const Twine &EnvironmentStr);
149 /// @name Normalization
152 /// normalize - Turn an arbitrary machine specification into the canonical
153 /// triple form (or something sensible that the Triple class understands if
154 /// nothing better can reasonably be done). In particular, it handles the
155 /// common case in which otherwise valid components are in the wrong order.
156 static std::string normalize(StringRef Str);
159 /// @name Typed Component Access
162 /// getArch - Get the parsed architecture type of this triple.
163 ArchType getArch() const { return Arch; }
165 /// getVendor - Get the parsed vendor type of this triple.
166 VendorType getVendor() const { return Vendor; }
168 /// getOS - Get the parsed operating system type of this triple.
169 OSType getOS() const { return OS; }
171 /// hasEnvironment - Does this triple have the optional environment
172 /// (fourth) component?
173 bool hasEnvironment() const {
174 return getEnvironmentName() != "";
177 /// getEnvironment - Get the parsed environment type of this triple.
178 EnvironmentType getEnvironment() const { return Environment; }
180 /// getOSVersion - Parse the version number from the OS name component of the
181 /// triple, if present.
183 /// For example, "fooos1.2.3" would return (1, 2, 3).
185 /// If an entry is not defined, it will be returned as 0.
186 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
188 /// getOSMajorVersion - Return just the major version number, this is
189 /// specialized because it is a common query.
190 unsigned getOSMajorVersion() const {
191 unsigned Maj, Min, Micro;
192 getOSVersion(Maj, Min, Micro);
196 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
197 /// translate generic "darwin" versions to the corresponding OS X versions.
198 /// This may also be called with IOS triples but the OS X version number is
199 /// just set to a constant 10.4.0 in that case. Returns true if successful.
200 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
201 unsigned &Micro) const;
203 /// getiOSVersion - Parse the version number as with getOSVersion. This should
204 /// only be called with IOS triples.
205 void getiOSVersion(unsigned &Major, unsigned &Minor,
206 unsigned &Micro) const;
209 /// @name Direct Component Access
212 const std::string &str() const { return Data; }
214 const std::string &getTriple() const { return Data; }
216 /// getArchName - Get the architecture (first) component of the
218 StringRef getArchName() const;
220 /// getVendorName - Get the vendor (second) component of the triple.
221 StringRef getVendorName() const;
223 /// getOSName - Get the operating system (third) component of the
225 StringRef getOSName() const;
227 /// getEnvironmentName - Get the optional environment (fourth)
228 /// component of the triple, or "" if empty.
229 StringRef getEnvironmentName() const;
231 /// getOSAndEnvironmentName - Get the operating system and optional
232 /// environment components as a single string (separated by a '-'
233 /// if the environment component is present).
234 StringRef getOSAndEnvironmentName() const;
237 /// @name Convenience Predicates
240 /// \brief Test whether the architecture is 64-bit
242 /// Note that this tests for 64-bit pointer width, and nothing else. Note
243 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
244 /// 16-bit. The inner details of pointer width for particular architectures
245 /// is not summed up in the triple, and so only a coarse grained predicate
246 /// system is provided.
247 bool isArch64Bit() const;
249 /// \brief Test whether the architecture is 32-bit
251 /// Note that this tests for 32-bit pointer width, and nothing else.
252 bool isArch32Bit() const;
254 /// \brief Test whether the architecture is 16-bit
256 /// Note that this tests for 16-bit pointer width, and nothing else.
257 bool isArch16Bit() const;
259 /// isOSVersionLT - Helper function for doing comparisons against version
260 /// numbers included in the target triple.
261 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
262 unsigned Micro = 0) const {
264 getOSVersion(LHS[0], LHS[1], LHS[2]);
267 return LHS[0] < Major;
269 return LHS[1] < Minor;
271 return LHS[1] < Micro;
276 /// isMacOSXVersionLT - Comparison function for checking OS X version
277 /// compatibility, which handles supporting skewed version numbering schemes
278 /// used by the "darwin" triples.
279 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
280 unsigned Micro = 0) const {
281 assert(isMacOSX() && "Not an OS X triple!");
283 // If this is OS X, expect a sane version number.
284 if (getOS() == Triple::MacOSX)
285 return isOSVersionLT(Major, Minor, Micro);
287 // Otherwise, compare to the "Darwin" number.
288 assert(Major == 10 && "Unexpected major version");
289 return isOSVersionLT(Minor + 4, Micro, 0);
292 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
293 /// "darwin" and "osx" as OS X triples.
294 bool isMacOSX() const {
295 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
298 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
299 bool isOSDarwin() const {
300 return isMacOSX() || getOS() == Triple::IOS;
303 /// \brief Tests for either Cygwin or MinGW OS
304 bool isOSCygMing() const {
305 return getOS() == Triple::Cygwin || getOS() == Triple::MinGW32;
308 /// isOSWindows - Is this a "Windows" OS.
309 bool isOSWindows() const {
310 return getOS() == Triple::Win32 || isOSCygMing();
313 /// \brief Tests whether the OS is NaCl (Native Client)
314 bool isOSNaCl() const {
315 return getOS() == Triple::NaCl;
318 /// \brief Tests whether the OS uses the ELF binary format.
319 bool isOSBinFormatELF() const {
320 return !isOSDarwin() && !isOSWindows();
323 /// \brief Tests whether the OS uses the COFF binary format.
324 bool isOSBinFormatCOFF() const {
325 return isOSWindows();
328 /// \brief Tests whether the environment is MachO.
329 // FIXME: Should this be an OSBinFormat predicate?
330 bool isEnvironmentMachO() const {
331 return getEnvironment() == Triple::MachO || isOSDarwin();
338 /// setArch - Set the architecture (first) component of the triple
340 void setArch(ArchType Kind);
342 /// setVendor - Set the vendor (second) component of the triple to a
344 void setVendor(VendorType Kind);
346 /// setOS - Set the operating system (third) component of the triple
348 void setOS(OSType Kind);
350 /// setEnvironment - Set the environment (fourth) component of the triple
352 void setEnvironment(EnvironmentType Kind);
354 /// setTriple - Set all components to the new triple \p Str.
355 void setTriple(const Twine &Str);
357 /// setArchName - Set the architecture (first) component of the
359 void setArchName(StringRef Str);
361 /// setVendorName - Set the vendor (second) component of the triple
363 void setVendorName(StringRef Str);
365 /// setOSName - Set the operating system (third) component of the
367 void setOSName(StringRef Str);
369 /// setEnvironmentName - Set the optional environment (fourth)
370 /// component of the triple by name.
371 void setEnvironmentName(StringRef Str);
373 /// setOSAndEnvironmentName - Set the operating system and optional
374 /// environment components with a single string.
375 void setOSAndEnvironmentName(StringRef Str);
377 /// getArchNameForAssembler - Get an architecture name that is understood by
378 /// the target assembler.
379 const char *getArchNameForAssembler();
382 /// @name Helpers to build variants of a particular triple.
385 /// \brief Form a triple with a 32-bit variant of the current architecture.
387 /// This can be used to move across "families" of architectures where useful.
389 /// \returns A new triple with a 32-bit architecture or an unknown
390 /// architecture if no such variant can be found.
391 llvm::Triple get32BitArchVariant() const;
393 /// \brief Form a triple with a 64-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 64-bit architecture or an unknown
398 /// architecture if no such variant can be found.
399 llvm::Triple get64BitArchVariant() const;
402 /// @name Static helpers for IDs.
405 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
406 static const char *getArchTypeName(ArchType Kind);
408 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
409 /// architecture. This is the prefix used by the architecture specific
410 /// builtins, and is suitable for passing to \see
411 /// Intrinsic::getIntrinsicForGCCBuiltin().
413 /// \return - The architecture prefix, or 0 if none is defined.
414 static const char *getArchTypePrefix(ArchType Kind);
416 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
417 static const char *getVendorTypeName(VendorType Kind);
419 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
420 static const char *getOSTypeName(OSType Kind);
422 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
424 static const char *getEnvironmentTypeName(EnvironmentType Kind);
427 /// @name Static helpers for converting alternate architecture names.
430 /// getArchTypeForLLVMName - The canonical type for the given LLVM
431 /// architecture name (e.g., "x86").
432 static ArchType getArchTypeForLLVMName(StringRef Str);
437 } // End llvm namespace