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
99 enum EnvironmentType {
111 /// The parsed arch type (or InvalidArch if uninitialized).
112 mutable ArchType Arch;
114 /// The parsed vendor type.
115 mutable VendorType Vendor;
117 /// The parsed OS type.
120 /// The parsed Environment type.
121 mutable EnvironmentType Environment;
123 bool isInitialized() const { return Arch != InvalidArch; }
124 static ArchType ParseArch(StringRef ArchName);
125 static VendorType ParseVendor(StringRef VendorName);
126 static OSType ParseOS(StringRef OSName);
127 static EnvironmentType ParseEnvironment(StringRef EnvironmentName);
131 /// @name Constructors
134 Triple() : Data(), Arch(InvalidArch) {}
135 explicit Triple(const Twine &Str) : Data(Str.str()), Arch(InvalidArch) {}
136 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
137 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
141 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
142 const Twine &EnvironmentStr)
143 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
144 EnvironmentStr).str()), Arch(InvalidArch) {
148 /// @name Normalization
151 /// normalize - Turn an arbitrary machine specification into the canonical
152 /// triple form (or something sensible that the Triple class understands if
153 /// nothing better can reasonably be done). In particular, it handles the
154 /// common case in which otherwise valid components are in the wrong order.
155 static std::string normalize(StringRef Str);
158 /// @name Typed Component Access
161 /// getArch - Get the parsed architecture type of this triple.
162 ArchType getArch() const {
163 if (!isInitialized()) Parse();
167 /// getVendor - Get the parsed vendor type of this triple.
168 VendorType getVendor() const {
169 if (!isInitialized()) Parse();
173 /// getOS - Get the parsed operating system type of this triple.
174 OSType getOS() const {
175 if (!isInitialized()) Parse();
179 /// hasEnvironment - Does this triple have the optional environment
180 /// (fourth) component?
181 bool hasEnvironment() const {
182 return getEnvironmentName() != "";
185 /// getEnvironment - Get the parsed environment type of this triple.
186 EnvironmentType getEnvironment() const {
187 if (!isInitialized()) Parse();
192 /// @name Direct Component Access
195 const std::string &str() const { return Data; }
197 const std::string &getTriple() const { return Data; }
199 /// getArchName - Get the architecture (first) component of the
201 StringRef getArchName() const;
203 /// getVendorName - Get the vendor (second) component of the triple.
204 StringRef getVendorName() const;
206 /// getOSName - Get the operating system (third) component of the
208 StringRef getOSName() const;
210 /// getEnvironmentName - Get the optional environment (fourth)
211 /// component of the triple, or "" if empty.
212 StringRef getEnvironmentName() const;
214 /// getOSAndEnvironmentName - Get the operating system and optional
215 /// environment components as a single string (separated by a '-'
216 /// if the environment component is present).
217 StringRef getOSAndEnvironmentName() const;
219 /// getOSVersion - Parse the version number from the OS name component of the
220 /// triple, if present.
222 /// For example, "fooos1.2.3" would return (1, 2, 3).
224 /// If an entry is not defined, it will be returned as 0.
225 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
227 /// getOSMajorVersion - Return just the major version number, this is
228 /// specialized because it is a common query.
229 unsigned getOSMajorVersion() const {
230 unsigned Maj, Min, Micro;
231 getOSVersion(Maj, Min, Micro);
235 /// isOSVersionLT - Helper function for doing comparisons against version
236 /// numbers included in the target triple.
237 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
238 unsigned Micro = 0) const {
240 getOSVersion(LHS[0], LHS[1], LHS[2]);
243 return LHS[0] < Major;
245 return LHS[1] < Minor;
247 return LHS[1] < Micro;
252 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
253 /// "darwin" and "osx" as OS X triples.
254 bool isMacOSX() const {
255 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
258 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
259 bool isOSDarwin() const {
260 return isMacOSX() || getOS() == Triple::IOS;
263 /// isOSWindows - Is this a "Windows" OS.
264 bool isOSWindows() const {
265 return getOS() == Triple::Win32 || getOS() == Triple::Cygwin ||
266 getOS() == Triple::MinGW32;
269 /// isMacOSXVersionLT - Comparison function for checking OS X version
270 /// compatibility, which handles supporting skewed version numbering schemes
271 /// used by the "darwin" triples.
272 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
273 unsigned Micro = 0) const {
274 assert(isMacOSX() && "Not an OS X triple!");
276 // If this is OS X, expect a sane version number.
277 if (getOS() == Triple::MacOSX)
278 return isOSVersionLT(Major, Minor, Micro);
280 // Otherwise, compare to the "Darwin" number.
281 assert(Major == 10 && "Unexpected major version");
282 return isOSVersionLT(Minor + 4, Micro, 0);
289 /// setArch - Set the architecture (first) component of the triple
291 void setArch(ArchType Kind);
293 /// setVendor - Set the vendor (second) component of the triple to a
295 void setVendor(VendorType Kind);
297 /// setOS - Set the operating system (third) component of the triple
299 void setOS(OSType Kind);
301 /// setEnvironment - Set the environment (fourth) component of the triple
303 void setEnvironment(EnvironmentType Kind);
305 /// setTriple - Set all components to the new triple \arg Str.
306 void setTriple(const Twine &Str);
308 /// setArchName - Set the architecture (first) component of the
310 void setArchName(StringRef Str);
312 /// setVendorName - Set the vendor (second) component of the triple
314 void setVendorName(StringRef Str);
316 /// setOSName - Set the operating system (third) component of the
318 void setOSName(StringRef Str);
320 /// setEnvironmentName - Set the optional environment (fourth)
321 /// component of the triple by name.
322 void setEnvironmentName(StringRef Str);
324 /// setOSAndEnvironmentName - Set the operating system and optional
325 /// environment components with a single string.
326 void setOSAndEnvironmentName(StringRef Str);
328 /// getArchNameForAssembler - Get an architecture name that is understood by
329 /// the target assembler.
330 const char *getArchNameForAssembler();
333 /// @name Static helpers for IDs.
336 /// getArchTypeName - Get the canonical name for the \arg Kind
338 static const char *getArchTypeName(ArchType Kind);
340 /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
341 /// architecture. This is the prefix used by the architecture specific
342 /// builtins, and is suitable for passing to \see
343 /// Intrinsic::getIntrinsicForGCCBuiltin().
345 /// \return - The architecture prefix, or 0 if none is defined.
346 static const char *getArchTypePrefix(ArchType Kind);
348 /// getVendorTypeName - Get the canonical name for the \arg Kind
350 static const char *getVendorTypeName(VendorType Kind);
352 /// getOSTypeName - Get the canonical name for the \arg Kind operating
354 static const char *getOSTypeName(OSType Kind);
356 /// getEnvironmentTypeName - Get the canonical name for the \arg Kind
358 static const char *getEnvironmentTypeName(EnvironmentType Kind);
361 /// @name Static helpers for converting alternate architecture names.
364 /// getArchTypeForLLVMName - The canonical type for the given LLVM
365 /// architecture name (e.g., "x86").
366 static ArchType getArchTypeForLLVMName(StringRef Str);
368 /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
369 /// architecture name, for example as accepted by "gcc -arch" (see also
371 static ArchType getArchTypeForDarwinArchName(StringRef Str);
376 } // End llvm namespace