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 alpha, // Alpha: alpha
47 arm, // ARM; arm, armv.*, xscale
48 bfin, // Blackfin: bfin
49 cellspu, // CellSPU: spu, cellspu
50 mips, // MIPS: mips, mipsallegrex
51 mipsel, // MIPSEL: mipsel, mipsallegrexel, psp
52 mips64, // MIPS64: mips64
53 mips64el,// MIPS64EL: mips64el
54 msp430, // MSP430: msp430
56 ppc64, // PPC64: powerpc64, ppu
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 ptx32, // PTX: ptx (32-bit)
67 ptx64, // PTX: ptx (64-bit)
68 le32, // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
69 amdil, // amdil: amd IL
93 MinGW32, // i*86-pc-mingw32, *-w64-mingw32
104 enum EnvironmentType {
116 /// The parsed arch type (or InvalidArch if uninitialized).
117 mutable ArchType Arch;
119 /// The parsed vendor type.
120 mutable VendorType Vendor;
122 /// The parsed OS type.
125 /// The parsed Environment type.
126 mutable EnvironmentType Environment;
128 bool isInitialized() const { return Arch != InvalidArch; }
129 static ArchType ParseArch(StringRef ArchName);
130 static VendorType ParseVendor(StringRef VendorName);
131 static OSType ParseOS(StringRef OSName);
132 static EnvironmentType ParseEnvironment(StringRef EnvironmentName);
136 /// @name Constructors
139 Triple() : Data(), Arch(InvalidArch) {}
140 explicit Triple(const Twine &Str) : Data(Str.str()), Arch(InvalidArch) {}
141 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
142 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
146 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
147 const Twine &EnvironmentStr)
148 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
149 EnvironmentStr).str()), Arch(InvalidArch) {
153 /// @name Normalization
156 /// normalize - Turn an arbitrary machine specification into the canonical
157 /// triple form (or something sensible that the Triple class understands if
158 /// nothing better can reasonably be done). In particular, it handles the
159 /// common case in which otherwise valid components are in the wrong order.
160 static std::string normalize(StringRef Str);
163 /// @name Typed Component Access
166 /// getArch - Get the parsed architecture type of this triple.
167 ArchType getArch() const {
168 if (!isInitialized()) Parse();
172 /// getVendor - Get the parsed vendor type of this triple.
173 VendorType getVendor() const {
174 if (!isInitialized()) Parse();
178 /// getOS - Get the parsed operating system type of this triple.
179 OSType getOS() const {
180 if (!isInitialized()) Parse();
184 /// hasEnvironment - Does this triple have the optional environment
185 /// (fourth) component?
186 bool hasEnvironment() const {
187 return getEnvironmentName() != "";
190 /// getEnvironment - Get the parsed environment type of this triple.
191 EnvironmentType getEnvironment() const {
192 if (!isInitialized()) Parse();
197 /// @name Direct Component Access
200 const std::string &str() const { return Data; }
202 const std::string &getTriple() const { return Data; }
204 /// getArchName - Get the architecture (first) component of the
206 StringRef getArchName() const;
208 /// getVendorName - Get the vendor (second) component of the triple.
209 StringRef getVendorName() const;
211 /// getOSName - Get the operating system (third) component of the
213 StringRef getOSName() const;
215 /// getEnvironmentName - Get the optional environment (fourth)
216 /// component of the triple, or "" if empty.
217 StringRef getEnvironmentName() const;
219 /// getOSAndEnvironmentName - Get the operating system and optional
220 /// environment components as a single string (separated by a '-'
221 /// if the environment component is present).
222 StringRef getOSAndEnvironmentName() const;
224 /// getOSVersion - Parse the version number from the OS name component of the
225 /// triple, if present.
227 /// For example, "fooos1.2.3" would return (1, 2, 3).
229 /// If an entry is not defined, it will be returned as 0.
230 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
232 /// getOSMajorVersion - Return just the major version number, this is
233 /// specialized because it is a common query.
234 unsigned getOSMajorVersion() const {
235 unsigned Maj, Min, Micro;
236 getOSVersion(Maj, Min, Micro);
240 /// isOSVersionLT - Helper function for doing comparisons against version
241 /// numbers included in the target triple.
242 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
243 unsigned Micro = 0) const {
245 getOSVersion(LHS[0], LHS[1], LHS[2]);
248 return LHS[0] < Major;
250 return LHS[1] < Minor;
252 return LHS[1] < Micro;
257 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
258 /// "darwin" and "osx" as OS X triples.
259 bool isMacOSX() const {
260 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
263 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
264 bool isOSDarwin() const {
265 return isMacOSX() || getOS() == Triple::IOS;
268 /// isOSWindows - Is this a "Windows" OS.
269 bool isOSWindows() const {
270 return getOS() == Triple::Win32 || getOS() == Triple::Cygwin ||
271 getOS() == Triple::MinGW32;
274 /// isMacOSXVersionLT - Comparison function for checking OS X version
275 /// compatibility, which handles supporting skewed version numbering schemes
276 /// used by the "darwin" triples.
277 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
278 unsigned Micro = 0) const {
279 assert(isMacOSX() && "Not an OS X triple!");
281 // If this is OS X, expect a sane version number.
282 if (getOS() == Triple::MacOSX)
283 return isOSVersionLT(Major, Minor, Micro);
285 // Otherwise, compare to the "Darwin" number.
286 assert(Major == 10 && "Unexpected major version");
287 return isOSVersionLT(Minor + 4, Micro, 0);
294 /// setArch - Set the architecture (first) component of the triple
296 void setArch(ArchType Kind);
298 /// setVendor - Set the vendor (second) component of the triple to a
300 void setVendor(VendorType Kind);
302 /// setOS - Set the operating system (third) component of the triple
304 void setOS(OSType Kind);
306 /// setEnvironment - Set the environment (fourth) component of the triple
308 void setEnvironment(EnvironmentType Kind);
310 /// setTriple - Set all components to the new triple \arg Str.
311 void setTriple(const Twine &Str);
313 /// setArchName - Set the architecture (first) component of the
315 void setArchName(StringRef Str);
317 /// setVendorName - Set the vendor (second) component of the triple
319 void setVendorName(StringRef Str);
321 /// setOSName - Set the operating system (third) component of the
323 void setOSName(StringRef Str);
325 /// setEnvironmentName - Set the optional environment (fourth)
326 /// component of the triple by name.
327 void setEnvironmentName(StringRef Str);
329 /// setOSAndEnvironmentName - Set the operating system and optional
330 /// environment components with a single string.
331 void setOSAndEnvironmentName(StringRef Str);
333 /// getArchNameForAssembler - Get an architecture name that is understood by
334 /// the target assembler.
335 const char *getArchNameForAssembler();
338 /// @name Static helpers for IDs.
341 /// getArchTypeName - Get the canonical name for the \arg Kind
343 static const char *getArchTypeName(ArchType Kind);
345 /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
346 /// architecture. This is the prefix used by the architecture specific
347 /// builtins, and is suitable for passing to \see
348 /// Intrinsic::getIntrinsicForGCCBuiltin().
350 /// \return - The architecture prefix, or 0 if none is defined.
351 static const char *getArchTypePrefix(ArchType Kind);
353 /// getVendorTypeName - Get the canonical name for the \arg Kind
355 static const char *getVendorTypeName(VendorType Kind);
357 /// getOSTypeName - Get the canonical name for the \arg Kind operating
359 static const char *getOSTypeName(OSType Kind);
361 /// getEnvironmentTypeName - Get the canonical name for the \arg Kind
363 static const char *getEnvironmentTypeName(EnvironmentType Kind);
366 /// @name Static helpers for converting alternate architecture names.
369 /// getArchTypeForLLVMName - The canonical type for the given LLVM
370 /// architecture name (e.g., "x86").
371 static ArchType getArchTypeForLLVMName(StringRef Str);
373 /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
374 /// architecture name, for example as accepted by "gcc -arch" (see also
376 static ArchType getArchTypeForDarwinArchName(StringRef Str);
381 } // End llvm namespace