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 cellspu, // CellSPU: spu, cellspu
48 hexagon, // Hexagon: hexagon
49 mips, // MIPS: mips, mipsallegrex
50 mipsel, // MIPSEL: mipsel, mipsallegrexel, psp
51 mips64, // MIPS64: mips64
52 mips64el,// MIPS64EL: mips64el
53 msp430, // MSP430: msp430
55 ppc64, // PPC64: powerpc64, ppu
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 ptx32, // PTX: ptx (32-bit)
65 ptx64, // PTX: ptx (64-bit)
66 le32, // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
67 amdil, // amdil: amd IL
91 MinGW32, // i*86-pc-mingw32, *-w64-mingw32
102 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();
196 /// getOSVersion - Parse the version number from the OS name component of the
197 /// triple, if present.
199 /// For example, "fooos1.2.3" would return (1, 2, 3).
201 /// If an entry is not defined, it will be returned as 0.
202 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
204 /// getOSMajorVersion - Return just the major version number, this is
205 /// specialized because it is a common query.
206 unsigned getOSMajorVersion() const {
207 unsigned Maj, Min, Micro;
208 getOSVersion(Maj, Min, Micro);
213 /// @name Direct Component Access
216 const std::string &str() const { return Data; }
218 const std::string &getTriple() const { return Data; }
220 /// getArchName - Get the architecture (first) component of the
222 StringRef getArchName() const;
224 /// getVendorName - Get the vendor (second) component of the triple.
225 StringRef getVendorName() const;
227 /// getOSName - Get the operating system (third) component of the
229 StringRef getOSName() const;
231 /// getEnvironmentName - Get the optional environment (fourth)
232 /// component of the triple, or "" if empty.
233 StringRef getEnvironmentName() const;
235 /// getOSAndEnvironmentName - Get the operating system and optional
236 /// environment components as a single string (separated by a '-'
237 /// if the environment component is present).
238 StringRef getOSAndEnvironmentName() const;
241 /// @name Convenience Predicates
244 /// \brief Test whether the architecture is 64-bit
246 /// Note that this tests for 64-bit pointer width, and nothing else. Note
247 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
248 /// 16-bit. The inner details of pointer width for particular architectures
249 /// is not summed up in the triple, and so only a coarse grained predicate
250 /// system is provided.
251 bool isArch64Bit() const;
253 /// \brief Test whether the architecture is 32-bit
255 /// Note that this tests for 32-bit pointer width, and nothing else.
256 bool isArch32Bit() const;
258 /// \brief Test whether the architecture is 16-bit
260 /// Note that this tests for 16-bit pointer width, and nothing else.
261 bool isArch16Bit() const;
263 /// isOSVersionLT - Helper function for doing comparisons against version
264 /// numbers included in the target triple.
265 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
266 unsigned Micro = 0) const {
268 getOSVersion(LHS[0], LHS[1], LHS[2]);
271 return LHS[0] < Major;
273 return LHS[1] < Minor;
275 return LHS[1] < Micro;
280 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
281 /// "darwin" and "osx" as OS X triples.
282 bool isMacOSX() const {
283 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
286 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
287 bool isOSDarwin() const {
288 return isMacOSX() || getOS() == Triple::IOS;
291 /// isOSWindows - Is this a "Windows" OS.
292 bool isOSWindows() const {
293 return getOS() == Triple::Win32 || getOS() == Triple::Cygwin ||
294 getOS() == Triple::MinGW32;
297 /// isMacOSXVersionLT - Comparison function for checking OS X version
298 /// compatibility, which handles supporting skewed version numbering schemes
299 /// used by the "darwin" triples.
300 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
301 unsigned Micro = 0) const {
302 assert(isMacOSX() && "Not an OS X triple!");
304 // If this is OS X, expect a sane version number.
305 if (getOS() == Triple::MacOSX)
306 return isOSVersionLT(Major, Minor, Micro);
308 // Otherwise, compare to the "Darwin" number.
309 assert(Major == 10 && "Unexpected major version");
310 return isOSVersionLT(Minor + 4, Micro, 0);
317 /// setArch - Set the architecture (first) component of the triple
319 void setArch(ArchType Kind);
321 /// setVendor - Set the vendor (second) component of the triple to a
323 void setVendor(VendorType Kind);
325 /// setOS - Set the operating system (third) component of the triple
327 void setOS(OSType Kind);
329 /// setEnvironment - Set the environment (fourth) component of the triple
331 void setEnvironment(EnvironmentType Kind);
333 /// setTriple - Set all components to the new triple \arg Str.
334 void setTriple(const Twine &Str);
336 /// setArchName - Set the architecture (first) component of the
338 void setArchName(StringRef Str);
340 /// setVendorName - Set the vendor (second) component of the triple
342 void setVendorName(StringRef Str);
344 /// setOSName - Set the operating system (third) component of the
346 void setOSName(StringRef Str);
348 /// setEnvironmentName - Set the optional environment (fourth)
349 /// component of the triple by name.
350 void setEnvironmentName(StringRef Str);
352 /// setOSAndEnvironmentName - Set the operating system and optional
353 /// environment components with a single string.
354 void setOSAndEnvironmentName(StringRef Str);
356 /// getArchNameForAssembler - Get an architecture name that is understood by
357 /// the target assembler.
358 const char *getArchNameForAssembler();
361 /// @name Static helpers for IDs.
364 /// getArchTypeName - Get the canonical name for the \arg Kind
366 static const char *getArchTypeName(ArchType Kind);
368 /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
369 /// architecture. This is the prefix used by the architecture specific
370 /// builtins, and is suitable for passing to \see
371 /// Intrinsic::getIntrinsicForGCCBuiltin().
373 /// \return - The architecture prefix, or 0 if none is defined.
374 static const char *getArchTypePrefix(ArchType Kind);
376 /// getVendorTypeName - Get the canonical name for the \arg Kind
378 static const char *getVendorTypeName(VendorType Kind);
380 /// getOSTypeName - Get the canonical name for the \arg Kind operating
382 static const char *getOSTypeName(OSType Kind);
384 /// getEnvironmentTypeName - Get the canonical name for the \arg Kind
386 static const char *getEnvironmentTypeName(EnvironmentType Kind);
389 /// @name Static helpers for converting alternate architecture names.
392 /// getArchTypeForLLVMName - The canonical type for the given LLVM
393 /// architecture name (e.g., "x86").
394 static ArchType getArchTypeForLLVMName(StringRef Str);
396 /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
397 /// architecture name, for example as accepted by "gcc -arch" (see also
399 static ArchType getArchTypeForDarwinArchName(StringRef Str);
404 } // End llvm namespace