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)
89 MinGW32, // i*86-pc-mingw32, *-w64-mingw32
100 enum EnvironmentType {
112 /// The parsed arch type (or InvalidArch if uninitialized).
113 mutable ArchType Arch;
115 /// The parsed vendor type.
116 mutable VendorType Vendor;
118 /// The parsed OS type.
121 /// The parsed Environment type.
122 mutable EnvironmentType Environment;
124 bool isInitialized() const { return Arch != InvalidArch; }
125 static ArchType ParseArch(StringRef ArchName);
126 static VendorType ParseVendor(StringRef VendorName);
127 static OSType ParseOS(StringRef OSName);
128 static EnvironmentType ParseEnvironment(StringRef EnvironmentName);
132 /// @name Constructors
135 Triple() : Data(), Arch(InvalidArch) {}
136 explicit Triple(const Twine &Str) : Data(Str.str()), Arch(InvalidArch) {}
137 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
138 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
142 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
143 const Twine &EnvironmentStr)
144 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
145 EnvironmentStr).str()), Arch(InvalidArch) {
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 {
164 if (!isInitialized()) Parse();
168 /// getVendor - Get the parsed vendor type of this triple.
169 VendorType getVendor() const {
170 if (!isInitialized()) Parse();
174 /// getOS - Get the parsed operating system type of this triple.
175 OSType getOS() const {
176 if (!isInitialized()) Parse();
180 /// hasEnvironment - Does this triple have the optional environment
181 /// (fourth) component?
182 bool hasEnvironment() const {
183 return getEnvironmentName() != "";
186 /// getEnvironment - Get the parsed environment type of this triple.
187 EnvironmentType getEnvironment() const {
188 if (!isInitialized()) Parse();
193 /// @name Direct Component Access
196 const std::string &str() const { return Data; }
198 const std::string &getTriple() const { return Data; }
200 /// getArchName - Get the architecture (first) component of the
202 StringRef getArchName() const;
204 /// getVendorName - Get the vendor (second) component of the triple.
205 StringRef getVendorName() const;
207 /// getOSName - Get the operating system (third) component of the
209 StringRef getOSName() const;
211 /// getEnvironmentName - Get the optional environment (fourth)
212 /// component of the triple, or "" if empty.
213 StringRef getEnvironmentName() const;
215 /// getOSAndEnvironmentName - Get the operating system and optional
216 /// environment components as a single string (separated by a '-'
217 /// if the environment component is present).
218 StringRef getOSAndEnvironmentName() const;
220 /// getOSVersion - Parse the version number from the OS name component of the
221 /// triple, if present.
223 /// For example, "fooos1.2.3" would return (1, 2, 3).
225 /// If an entry is not defined, it will be returned as 0.
226 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
228 /// getOSMajorVersion - Return just the major version number, this is
229 /// specialized because it is a common query.
230 unsigned getOSMajorVersion() const {
231 unsigned Maj, Min, Micro;
232 getOSVersion(Maj, Min, Micro);
236 /// isOSVersionLT - Helper function for doing comparisons against version
237 /// numbers included in the target triple.
238 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
239 unsigned Micro = 0) const {
241 getOSVersion(LHS[0], LHS[1], LHS[2]);
244 return LHS[0] < Major;
246 return LHS[1] < Minor;
248 return LHS[1] < Micro;
253 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
254 /// "darwin" and "osx" as OS X triples.
255 bool isMacOSX() const {
256 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
259 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
260 bool isOSDarwin() const {
261 return isMacOSX() || getOS() == Triple::IOS;
264 /// isOSWindows - Is this a "Windows" OS.
265 bool isOSWindows() const {
266 return getOS() == Triple::Win32 || getOS() == Triple::Cygwin ||
267 getOS() == Triple::MinGW32;
270 /// isMacOSXVersionLT - Comparison function for checking OS X version
271 /// compatibility, which handles supporting skewed version numbering schemes
272 /// used by the "darwin" triples.
273 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
274 unsigned Micro = 0) const {
275 assert(isMacOSX() && "Not an OS X triple!");
277 // If this is OS X, expect a sane version number.
278 if (getOS() == Triple::MacOSX)
279 return isOSVersionLT(Major, Minor, Micro);
281 // Otherwise, compare to the "Darwin" number.
282 assert(Major == 10 && "Unexpected major version");
283 return isOSVersionLT(Minor + 4, Micro, 0);
290 /// setArch - Set the architecture (first) component of the triple
292 void setArch(ArchType Kind);
294 /// setVendor - Set the vendor (second) component of the triple to a
296 void setVendor(VendorType Kind);
298 /// setOS - Set the operating system (third) component of the triple
300 void setOS(OSType Kind);
302 /// setEnvironment - Set the environment (fourth) component of the triple
304 void setEnvironment(EnvironmentType Kind);
306 /// setTriple - Set all components to the new triple \arg Str.
307 void setTriple(const Twine &Str);
309 /// setArchName - Set the architecture (first) component of the
311 void setArchName(StringRef Str);
313 /// setVendorName - Set the vendor (second) component of the triple
315 void setVendorName(StringRef Str);
317 /// setOSName - Set the operating system (third) component of the
319 void setOSName(StringRef Str);
321 /// setEnvironmentName - Set the optional environment (fourth)
322 /// component of the triple by name.
323 void setEnvironmentName(StringRef Str);
325 /// setOSAndEnvironmentName - Set the operating system and optional
326 /// environment components with a single string.
327 void setOSAndEnvironmentName(StringRef Str);
329 /// getArchNameForAssembler - Get an architecture name that is understood by
330 /// the target assembler.
331 const char *getArchNameForAssembler();
334 /// @name Static helpers for IDs.
337 /// getArchTypeName - Get the canonical name for the \arg Kind
339 static const char *getArchTypeName(ArchType Kind);
341 /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
342 /// architecture. This is the prefix used by the architecture specific
343 /// builtins, and is suitable for passing to \see
344 /// Intrinsic::getIntrinsicForGCCBuiltin().
346 /// \return - The architecture prefix, or 0 if none is defined.
347 static const char *getArchTypePrefix(ArchType Kind);
349 /// getVendorTypeName - Get the canonical name for the \arg Kind
351 static const char *getVendorTypeName(VendorType Kind);
353 /// getOSTypeName - Get the canonical name for the \arg Kind operating
355 static const char *getOSTypeName(OSType Kind);
357 /// getEnvironmentTypeName - Get the canonical name for the \arg Kind
359 static const char *getEnvironmentTypeName(EnvironmentType Kind);
362 /// @name Static helpers for converting alternate architecture names.
365 /// getArchTypeForLLVMName - The canonical type for the given LLVM
366 /// architecture name (e.g., "x86").
367 static ArchType getArchTypeForLLVMName(StringRef Str);
369 /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
370 /// architecture name, for example as accepted by "gcc -arch" (see also
372 static ArchType getArchTypeForDarwinArchName(StringRef Str);
377 } // End llvm namespace