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 msp430, // MSP430: msp430
54 ppc64, // PPC64: powerpc64, ppu
55 sparc, // Sparc: sparc
56 sparcv9, // Sparcv9: Sparcv9
57 systemz, // SystemZ: s390x
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)
90 MinGW32, // i*86-pc-mingw32, *-w64-mingw32
101 enum EnvironmentType {
113 /// The parsed arch type (or InvalidArch if uninitialized).
114 mutable ArchType Arch;
116 /// The parsed vendor type.
117 mutable VendorType Vendor;
119 /// The parsed OS type.
122 /// The parsed Environment type.
123 mutable EnvironmentType Environment;
125 bool isInitialized() const { return Arch != InvalidArch; }
126 static ArchType ParseArch(StringRef ArchName);
127 static VendorType ParseVendor(StringRef VendorName);
128 static OSType ParseOS(StringRef OSName);
129 static EnvironmentType ParseEnvironment(StringRef EnvironmentName);
133 /// @name Constructors
136 Triple() : Data(), Arch(InvalidArch) {}
137 explicit Triple(const Twine &Str) : Data(Str.str()), Arch(InvalidArch) {}
138 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
139 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
143 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
144 const Twine &EnvironmentStr)
145 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
146 EnvironmentStr).str()), Arch(InvalidArch) {
150 /// @name Normalization
153 /// normalize - Turn an arbitrary machine specification into the canonical
154 /// triple form (or something sensible that the Triple class understands if
155 /// nothing better can reasonably be done). In particular, it handles the
156 /// common case in which otherwise valid components are in the wrong order.
157 static std::string normalize(StringRef Str);
160 /// @name Typed Component Access
163 /// getArch - Get the parsed architecture type of this triple.
164 ArchType getArch() const {
165 if (!isInitialized()) Parse();
169 /// getVendor - Get the parsed vendor type of this triple.
170 VendorType getVendor() const {
171 if (!isInitialized()) Parse();
175 /// getOS - Get the parsed operating system type of this triple.
176 OSType getOS() const {
177 if (!isInitialized()) Parse();
181 /// hasEnvironment - Does this triple have the optional environment
182 /// (fourth) component?
183 bool hasEnvironment() const {
184 return getEnvironmentName() != "";
187 /// getEnvironment - Get the parsed environment type of this triple.
188 EnvironmentType getEnvironment() const {
189 if (!isInitialized()) Parse();
194 /// @name Direct Component Access
197 const std::string &str() const { return Data; }
199 const std::string &getTriple() const { return Data; }
201 /// getArchName - Get the architecture (first) component of the
203 StringRef getArchName() const;
205 /// getVendorName - Get the vendor (second) component of the triple.
206 StringRef getVendorName() const;
208 /// getOSName - Get the operating system (third) component of the
210 StringRef getOSName() const;
212 /// getEnvironmentName - Get the optional environment (fourth)
213 /// component of the triple, or "" if empty.
214 StringRef getEnvironmentName() const;
216 /// getOSAndEnvironmentName - Get the operating system and optional
217 /// environment components as a single string (separated by a '-'
218 /// if the environment component is present).
219 StringRef getOSAndEnvironmentName() const;
221 /// getOSVersion - Parse the version number from the OS name component of the
222 /// triple, if present.
224 /// For example, "fooos1.2.3" would return (1, 2, 3).
226 /// If an entry is not defined, it will be returned as 0.
227 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
229 /// getOSMajorVersion - Return just the major version number, this is
230 /// specialized because it is a common query.
231 unsigned getOSMajorVersion() const {
232 unsigned Maj, Min, Micro;
233 getOSVersion(Maj, Min, Micro);
237 /// isOSVersionLT - Helper function for doing comparisons against version
238 /// numbers included in the target triple.
239 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
240 unsigned Micro = 0) const {
242 getOSVersion(LHS[0], LHS[1], LHS[2]);
245 return LHS[0] < Major;
247 return LHS[1] < Minor;
249 return LHS[1] < Micro;
254 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
255 /// "darwin" and "osx" as OS X triples.
256 bool isMacOSX() const {
257 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
260 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
261 bool isOSDarwin() const {
262 return isMacOSX() || getOS() == Triple::IOS;
265 /// isOSWindows - Is this a "Windows" OS.
266 bool isOSWindows() const {
267 return getOS() == Triple::Win32 || getOS() == Triple::Cygwin ||
268 getOS() == Triple::MinGW32;
271 /// isMacOSXVersionLT - Comparison function for checking OS X version
272 /// compatibility, which handles supporting skewed version numbering schemes
273 /// used by the "darwin" triples.
274 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
275 unsigned Micro = 0) const {
276 assert(isMacOSX() && "Not an OS X triple!");
278 // If this is OS X, expect a sane version number.
279 if (getOS() == Triple::MacOSX)
280 return isOSVersionLT(Major, Minor, Micro);
282 // Otherwise, compare to the "Darwin" number.
283 assert(Major == 10 && "Unexpected major version");
284 return isOSVersionLT(Minor + 4, Micro, 0);
291 /// setArch - Set the architecture (first) component of the triple
293 void setArch(ArchType Kind);
295 /// setVendor - Set the vendor (second) component of the triple to a
297 void setVendor(VendorType Kind);
299 /// setOS - Set the operating system (third) component of the triple
301 void setOS(OSType Kind);
303 /// setEnvironment - Set the environment (fourth) component of the triple
305 void setEnvironment(EnvironmentType Kind);
307 /// setTriple - Set all components to the new triple \arg Str.
308 void setTriple(const Twine &Str);
310 /// setArchName - Set the architecture (first) component of the
312 void setArchName(StringRef Str);
314 /// setVendorName - Set the vendor (second) component of the triple
316 void setVendorName(StringRef Str);
318 /// setOSName - Set the operating system (third) component of the
320 void setOSName(StringRef Str);
322 /// setEnvironmentName - Set the optional environment (fourth)
323 /// component of the triple by name.
324 void setEnvironmentName(StringRef Str);
326 /// setOSAndEnvironmentName - Set the operating system and optional
327 /// environment components with a single string.
328 void setOSAndEnvironmentName(StringRef Str);
330 /// getArchNameForAssembler - Get an architecture name that is understood by
331 /// the target assembler.
332 const char *getArchNameForAssembler();
335 /// @name Static helpers for IDs.
338 /// getArchTypeName - Get the canonical name for the \arg Kind
340 static const char *getArchTypeName(ArchType Kind);
342 /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
343 /// architecture. This is the prefix used by the architecture specific
344 /// builtins, and is suitable for passing to \see
345 /// Intrinsic::getIntrinsicForGCCBuiltin().
347 /// \return - The architecture prefix, or 0 if none is defined.
348 static const char *getArchTypePrefix(ArchType Kind);
350 /// getVendorTypeName - Get the canonical name for the \arg Kind
352 static const char *getVendorTypeName(VendorType Kind);
354 /// getOSTypeName - Get the canonical name for the \arg Kind operating
356 static const char *getOSTypeName(OSType Kind);
358 /// getEnvironmentTypeName - Get the canonical name for the \arg Kind
360 static const char *getEnvironmentTypeName(EnvironmentType Kind);
363 /// @name Static helpers for converting alternate architecture names.
366 /// getArchTypeForLLVMName - The canonical type for the given LLVM
367 /// architecture name (e.g., "x86").
368 static ArchType getArchTypeForLLVMName(StringRef Str);
370 /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
371 /// architecture name, for example as accepted by "gcc -arch" (see also
373 static ArchType getArchTypeForDarwinArchName(StringRef Str);
378 } // End llvm namespace