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
89 MinGW32, // i*86-pc-mingw32, *-w64-mingw32
100 enum EnvironmentType {
114 /// The parsed arch type.
117 /// The parsed vendor type.
120 /// The parsed OS type.
123 /// The parsed Environment type.
124 EnvironmentType Environment;
126 static ArchType ParseArch(StringRef ArchName);
127 static VendorType ParseVendor(StringRef VendorName);
128 static OSType ParseOS(StringRef OSName);
129 static EnvironmentType ParseEnvironment(StringRef EnvironmentName);
132 /// @name Constructors
135 /// \brief Default constructor is the same as an empty string and leaves all
136 /// triple fields unknown.
137 Triple() : Data(), Arch(), Vendor(), OS(), Environment() {}
139 explicit Triple(const Twine &Str);
140 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
141 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
142 const Twine &EnvironmentStr);
145 /// @name Normalization
148 /// normalize - Turn an arbitrary machine specification into the canonical
149 /// triple form (or something sensible that the Triple class understands if
150 /// nothing better can reasonably be done). In particular, it handles the
151 /// common case in which otherwise valid components are in the wrong order.
152 static std::string normalize(StringRef Str);
155 /// @name Typed Component Access
158 /// getArch - Get the parsed architecture type of this triple.
159 ArchType getArch() const { return Arch; }
161 /// getVendor - Get the parsed vendor type of this triple.
162 VendorType getVendor() const { return Vendor; }
164 /// getOS - Get the parsed operating system type of this triple.
165 OSType getOS() const { return OS; }
167 /// hasEnvironment - Does this triple have the optional environment
168 /// (fourth) component?
169 bool hasEnvironment() const {
170 return getEnvironmentName() != "";
173 /// getEnvironment - Get the parsed environment type of this triple.
174 EnvironmentType getEnvironment() const { return Environment; }
176 /// getOSVersion - Parse the version number from the OS name component of the
177 /// triple, if present.
179 /// For example, "fooos1.2.3" would return (1, 2, 3).
181 /// If an entry is not defined, it will be returned as 0.
182 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
184 /// getOSMajorVersion - Return just the major version number, this is
185 /// specialized because it is a common query.
186 unsigned getOSMajorVersion() const {
187 unsigned Maj, Min, Micro;
188 getOSVersion(Maj, Min, Micro);
192 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
193 /// translate generic "darwin" versions to the corresponding OS X versions.
194 /// This may also be called with IOS triples but the OS X version number is
195 /// just set to a constant 10.4.0 in that case. Returns true if successful.
196 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
197 unsigned &Micro) const;
200 /// @name Direct Component Access
203 const std::string &str() const { return Data; }
205 const std::string &getTriple() const { return Data; }
207 /// getArchName - Get the architecture (first) component of the
209 StringRef getArchName() const;
211 /// getVendorName - Get the vendor (second) component of the triple.
212 StringRef getVendorName() const;
214 /// getOSName - Get the operating system (third) component of the
216 StringRef getOSName() const;
218 /// getEnvironmentName - Get the optional environment (fourth)
219 /// component of the triple, or "" if empty.
220 StringRef getEnvironmentName() const;
222 /// getOSAndEnvironmentName - Get the operating system and optional
223 /// environment components as a single string (separated by a '-'
224 /// if the environment component is present).
225 StringRef getOSAndEnvironmentName() const;
228 /// @name Convenience Predicates
231 /// \brief Test whether the architecture is 64-bit
233 /// Note that this tests for 64-bit pointer width, and nothing else. Note
234 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
235 /// 16-bit. The inner details of pointer width for particular architectures
236 /// is not summed up in the triple, and so only a coarse grained predicate
237 /// system is provided.
238 bool isArch64Bit() const;
240 /// \brief Test whether the architecture is 32-bit
242 /// Note that this tests for 32-bit pointer width, and nothing else.
243 bool isArch32Bit() const;
245 /// \brief Test whether the architecture is 16-bit
247 /// Note that this tests for 16-bit pointer width, and nothing else.
248 bool isArch16Bit() const;
250 /// isOSVersionLT - Helper function for doing comparisons against version
251 /// numbers included in the target triple.
252 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
253 unsigned Micro = 0) const {
255 getOSVersion(LHS[0], LHS[1], LHS[2]);
258 return LHS[0] < Major;
260 return LHS[1] < Minor;
262 return LHS[1] < Micro;
267 /// isMacOSXVersionLT - Comparison function for checking OS X version
268 /// compatibility, which handles supporting skewed version numbering schemes
269 /// used by the "darwin" triples.
270 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
271 unsigned Micro = 0) const {
272 assert(isMacOSX() && "Not an OS X triple!");
274 // If this is OS X, expect a sane version number.
275 if (getOS() == Triple::MacOSX)
276 return isOSVersionLT(Major, Minor, Micro);
278 // Otherwise, compare to the "Darwin" number.
279 assert(Major == 10 && "Unexpected major version");
280 return isOSVersionLT(Minor + 4, Micro, 0);
283 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
284 /// "darwin" and "osx" as OS X triples.
285 bool isMacOSX() const {
286 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
289 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
290 bool isOSDarwin() const {
291 return isMacOSX() || getOS() == Triple::IOS;
294 /// \brief Tests for either Cygwin or MinGW OS
295 bool isOSCygMing() const {
296 return getOS() == Triple::Cygwin || getOS() == Triple::MinGW32;
299 /// isOSWindows - Is this a "Windows" OS.
300 bool isOSWindows() const {
301 return getOS() == Triple::Win32 || isOSCygMing();
304 /// \brief Tests whether the OS uses the ELF binary format.
305 bool isOSBinFormatELF() const {
306 return !isOSDarwin() && !isOSWindows();
309 /// \brief Tests whether the OS uses the COFF binary format.
310 bool isOSBinFormatCOFF() const {
311 return isOSWindows();
314 /// \brief Tests whether the environment is MachO.
315 // FIXME: Should this be an OSBinFormat predicate?
316 bool isEnvironmentMachO() const {
317 return getEnvironment() == Triple::MachO || isOSDarwin();
324 /// setArch - Set the architecture (first) component of the triple
326 void setArch(ArchType Kind);
328 /// setVendor - Set the vendor (second) component of the triple to a
330 void setVendor(VendorType Kind);
332 /// setOS - Set the operating system (third) component of the triple
334 void setOS(OSType Kind);
336 /// setEnvironment - Set the environment (fourth) component of the triple
338 void setEnvironment(EnvironmentType Kind);
340 /// setTriple - Set all components to the new triple \arg Str.
341 void setTriple(const Twine &Str);
343 /// setArchName - Set the architecture (first) component of the
345 void setArchName(StringRef Str);
347 /// setVendorName - Set the vendor (second) component of the triple
349 void setVendorName(StringRef Str);
351 /// setOSName - Set the operating system (third) component of the
353 void setOSName(StringRef Str);
355 /// setEnvironmentName - Set the optional environment (fourth)
356 /// component of the triple by name.
357 void setEnvironmentName(StringRef Str);
359 /// setOSAndEnvironmentName - Set the operating system and optional
360 /// environment components with a single string.
361 void setOSAndEnvironmentName(StringRef Str);
363 /// getArchNameForAssembler - Get an architecture name that is understood by
364 /// the target assembler.
365 const char *getArchNameForAssembler();
368 /// @name Helpers to build variants of a particular triple.
371 /// \brief Form a triple with a 32-bit variant of the current architecture.
373 /// This can be used to move across "families" of architectures where useful.
375 /// \returns A new triple with a 32-bit architecture or an unknown
376 /// architecture if no such variant can be found.
377 llvm::Triple get32BitArchVariant() const;
379 /// \brief Form a triple with a 64-bit variant of the current architecture.
381 /// This can be used to move across "families" of architectures where useful.
383 /// \returns A new triple with a 64-bit architecture or an unknown
384 /// architecture if no such variant can be found.
385 llvm::Triple get64BitArchVariant() const;
388 /// @name Static helpers for IDs.
391 /// getArchTypeName - Get the canonical name for the \arg Kind
393 static const char *getArchTypeName(ArchType Kind);
395 /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
396 /// architecture. This is the prefix used by the architecture specific
397 /// builtins, and is suitable for passing to \see
398 /// Intrinsic::getIntrinsicForGCCBuiltin().
400 /// \return - The architecture prefix, or 0 if none is defined.
401 static const char *getArchTypePrefix(ArchType Kind);
403 /// getVendorTypeName - Get the canonical name for the \arg Kind
405 static const char *getVendorTypeName(VendorType Kind);
407 /// getOSTypeName - Get the canonical name for the \arg Kind operating
409 static const char *getOSTypeName(OSType Kind);
411 /// getEnvironmentTypeName - Get the canonical name for the \arg Kind
413 static const char *getEnvironmentTypeName(EnvironmentType Kind);
416 /// @name Static helpers for converting alternate architecture names.
419 /// getArchTypeForLLVMName - The canonical type for the given LLVM
420 /// architecture name (e.g., "x86").
421 static ArchType getArchTypeForLLVMName(StringRef Str);
423 /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
424 /// architecture name, for example as accepted by "gcc -arch" (see also
426 static ArchType getArchTypeForDarwinArchName(StringRef Str);
431 } // End llvm namespace