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)
88 MinGW32, // i*86-pc-mingw32, *-w64-mingw32
98 enum EnvironmentType {
110 /// The parsed arch type (or InvalidArch if uninitialized).
111 mutable ArchType Arch;
113 /// The parsed vendor type.
114 mutable VendorType Vendor;
116 /// The parsed OS type.
119 /// The parsed Environment type.
120 mutable EnvironmentType Environment;
122 bool isInitialized() const { return Arch != InvalidArch; }
123 static ArchType ParseArch(StringRef ArchName);
124 static VendorType ParseVendor(StringRef VendorName);
125 static OSType ParseOS(StringRef OSName);
126 static EnvironmentType ParseEnvironment(StringRef EnvironmentName);
130 /// @name Constructors
133 Triple() : Data(), Arch(InvalidArch) {}
134 explicit Triple(const Twine &Str) : Data(Str.str()), Arch(InvalidArch) {}
135 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
136 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
140 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
141 const Twine &EnvironmentStr)
142 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
143 EnvironmentStr).str()), Arch(InvalidArch) {
147 /// @name Normalization
150 /// normalize - Turn an arbitrary machine specification into the canonical
151 /// triple form (or something sensible that the Triple class understands if
152 /// nothing better can reasonably be done). In particular, it handles the
153 /// common case in which otherwise valid components are in the wrong order.
154 static std::string normalize(StringRef Str);
157 /// @name Typed Component Access
160 /// getArch - Get the parsed architecture type of this triple.
161 ArchType getArch() const {
162 if (!isInitialized()) Parse();
166 /// getVendor - Get the parsed vendor type of this triple.
167 VendorType getVendor() const {
168 if (!isInitialized()) Parse();
172 /// getOS - Get the parsed operating system type of this triple.
173 OSType getOS() const {
174 if (!isInitialized()) Parse();
178 /// hasEnvironment - Does this triple have the optional environment
179 /// (fourth) component?
180 bool hasEnvironment() const {
181 return getEnvironmentName() != "";
184 /// getEnvironment - Get the parsed environment type of this triple.
185 EnvironmentType getEnvironment() const {
186 if (!isInitialized()) Parse();
191 /// @name Direct Component Access
194 const std::string &str() const { return Data; }
196 const std::string &getTriple() const { return Data; }
198 /// getArchName - Get the architecture (first) component of the
200 StringRef getArchName() const;
202 /// getVendorName - Get the vendor (second) component of the triple.
203 StringRef getVendorName() const;
205 /// getOSName - Get the operating system (third) component of the
207 StringRef getOSName() const;
209 /// getEnvironmentName - Get the optional environment (fourth)
210 /// component of the triple, or "" if empty.
211 StringRef getEnvironmentName() const;
213 /// getOSAndEnvironmentName - Get the operating system and optional
214 /// environment components as a single string (separated by a '-'
215 /// if the environment component is present).
216 StringRef getOSAndEnvironmentName() const;
218 /// getOSVersion - Parse the version number from the OS name component of the
219 /// triple, if present.
221 /// For example, "fooos1.2.3" would return (1, 2, 3).
223 /// If an entry is not defined, it will be returned as 0.
224 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
226 /// getOSMajorVersion - Return just the major version number, this is
227 /// specialized because it is a common query.
228 unsigned getOSMajorVersion() const {
229 unsigned Maj, Min, Micro;
230 getOSVersion(Maj, Min, Micro);
234 /// isOSVersionLT - Helper function for doing comparisons against version
235 /// numbers included in the target triple.
236 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
237 unsigned Micro = 0) const {
239 getOSVersion(LHS[0], LHS[1], LHS[2]);
242 return LHS[0] < Major;
244 return LHS[1] < Minor;
246 return LHS[1] < Micro;
251 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
252 /// "darwin" and "osx" as OS X triples.
253 bool isMacOSX() const {
254 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
257 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
258 bool isOSDarwin() const {
259 return isMacOSX() || getOS() == Triple::IOS;
262 /// isOSWindows - Is this a "Windows" OS.
263 bool isOSWindows() const {
264 return getOS() == Triple::Win32 || getOS() == Triple::Cygwin ||
265 getOS() == Triple::MinGW32;
268 /// isMacOSXVersionLT - Comparison function for checking OS X version
269 /// compatibility, which handles supporting skewed version numbering schemes
270 /// used by the "darwin" triples.
271 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
272 unsigned Micro = 0) const {
273 assert(isMacOSX() && "Not an OS X triple!");
275 // If this is OS X, expect a sane version number.
276 if (getOS() == Triple::MacOSX)
277 return isOSVersionLT(Major, Minor, Micro);
279 // Otherwise, compare to the "Darwin" number.
280 assert(Major == 10 && "Unexpected major version");
281 return isOSVersionLT(Minor + 4, Micro, 0);
288 /// setArch - Set the architecture (first) component of the triple
290 void setArch(ArchType Kind);
292 /// setVendor - Set the vendor (second) component of the triple to a
294 void setVendor(VendorType Kind);
296 /// setOS - Set the operating system (third) component of the triple
298 void setOS(OSType Kind);
300 /// setEnvironment - Set the environment (fourth) component of the triple
302 void setEnvironment(EnvironmentType Kind);
304 /// setTriple - Set all components to the new triple \arg Str.
305 void setTriple(const Twine &Str);
307 /// setArchName - Set the architecture (first) component of the
309 void setArchName(StringRef Str);
311 /// setVendorName - Set the vendor (second) component of the triple
313 void setVendorName(StringRef Str);
315 /// setOSName - Set the operating system (third) component of the
317 void setOSName(StringRef Str);
319 /// setEnvironmentName - Set the optional environment (fourth)
320 /// component of the triple by name.
321 void setEnvironmentName(StringRef Str);
323 /// setOSAndEnvironmentName - Set the operating system and optional
324 /// environment components with a single string.
325 void setOSAndEnvironmentName(StringRef Str);
327 /// getArchNameForAssembler - Get an architecture name that is understood by
328 /// the target assembler.
329 const char *getArchNameForAssembler();
332 /// @name Static helpers for IDs.
335 /// getArchTypeName - Get the canonical name for the \arg Kind
337 static const char *getArchTypeName(ArchType Kind);
339 /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
340 /// architecture. This is the prefix used by the architecture specific
341 /// builtins, and is suitable for passing to \see
342 /// Intrinsic::getIntrinsicForGCCBuiltin().
344 /// \return - The architecture prefix, or 0 if none is defined.
345 static const char *getArchTypePrefix(ArchType Kind);
347 /// getVendorTypeName - Get the canonical name for the \arg Kind
349 static const char *getVendorTypeName(VendorType Kind);
351 /// getOSTypeName - Get the canonical name for the \arg Kind operating
353 static const char *getOSTypeName(OSType Kind);
355 /// getEnvironmentTypeName - Get the canonical name for the \arg Kind
357 static const char *getEnvironmentTypeName(EnvironmentType Kind);
360 /// @name Static helpers for converting alternate architecture names.
363 /// getArchTypeForLLVMName - The canonical type for the given LLVM
364 /// architecture name (e.g., "x86").
365 static ArchType getArchTypeForLLVMName(StringRef Str);
367 /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
368 /// architecture name, for example as accepted by "gcc -arch" (see also
370 static ArchType getArchTypeForDarwinArchName(StringRef Str);
375 } // End llvm namespace