Triple.h revision 7bbd6e366b39157445cc921024a987e61ea68c00
1//===-- llvm/ADT/Triple.h - Target triple helper class ----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_ADT_TRIPLE_H
11#define LLVM_ADT_TRIPLE_H
12
13#include "llvm/ADT/Twine.h"
14
15// Some system headers or GCC predefined macros conflict with identifiers in
16// this file.  Undefine them here.
17#undef mips
18#undef sparc
19
20namespace llvm {
21
22/// Triple - Helper class for working with target triples.
23///
24/// Target triples are strings in the canonical form:
25///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM
26/// or
27///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
28///
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.
33///
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.
38///
39/// See autoconf/config.guess for a glimpse into what triples look like in
40/// practice.
41class Triple {
42public:
43  enum ArchType {
44    UnknownArch,
45
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
51    mips64,  // MIPS64: mips64
52    mips64el,// MIPS64EL: mips64el
53    msp430,  // MSP430: msp430
54    ppc,     // PPC: powerpc
55    ppc64,   // PPC64: powerpc64, ppu
56    r600,    // R600: AMD GPUs HD2XXX - HD6XXX
57    sparc,   // Sparc: sparc
58    sparcv9, // Sparcv9: Sparcv9
59    tce,     // TCE (http://tce.cs.tut.fi/): tce
60    thumb,   // Thumb: thumb, thumbv.*
61    x86,     // X86: i[3-9]86
62    x86_64,  // X86-64: amd64, x86_64
63    xcore,   // XCore: xcore
64    mblaze,  // MBlaze: mblaze
65    nvptx,   // NVPTX: 32-bit
66    nvptx64, // NVPTX: 64-bit
67    le32,    // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
68    amdil,   // amdil: amd IL
69    spir     // SPIR: standard portable IR for OpenCL
70  };
71  enum VendorType {
72    UnknownVendor,
73
74    Apple,
75    PC,
76    SCEI,
77    BGP,
78    BGQ,
79    Freescale
80  };
81  enum OSType {
82    UnknownOS,
83
84    AuroraUX,
85    Cygwin,
86    Darwin,
87    DragonFly,
88    FreeBSD,
89    IOS,
90    KFreeBSD,
91    Linux,
92    Lv2,        // PS3
93    MacOSX,
94    MinGW32,    // i*86-pc-mingw32, *-w64-mingw32
95    NetBSD,
96    OpenBSD,
97    Solaris,
98    Win32,
99    Haiku,
100    Minix,
101    RTEMS,
102    NativeClient,
103    CNK,         // BG/P Compute-Node Kernel
104    Bitrig
105  };
106  enum EnvironmentType {
107    UnknownEnvironment,
108
109    GNU,
110    GNUEABI,
111    GNUEABIHF,
112    EABI,
113    MachO,
114    Android,
115    ELF
116  };
117
118private:
119  std::string Data;
120
121  /// The parsed arch type.
122  ArchType Arch;
123
124  /// The parsed vendor type.
125  VendorType Vendor;
126
127  /// The parsed OS type.
128  OSType OS;
129
130  /// The parsed Environment type.
131  EnvironmentType Environment;
132
133public:
134  /// @name Constructors
135  /// @{
136
137  /// \brief Default constructor is the same as an empty string and leaves all
138  /// triple fields unknown.
139  Triple() : Data(), Arch(), Vendor(), OS(), Environment() {}
140
141  explicit Triple(const Twine &Str);
142  Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
143  Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
144         const Twine &EnvironmentStr);
145
146  /// @}
147  /// @name Normalization
148  /// @{
149
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);
155
156  /// @}
157  /// @name Typed Component Access
158  /// @{
159
160  /// getArch - Get the parsed architecture type of this triple.
161  ArchType getArch() const { return Arch; }
162
163  /// getVendor - Get the parsed vendor type of this triple.
164  VendorType getVendor() const { return Vendor; }
165
166  /// getOS - Get the parsed operating system type of this triple.
167  OSType getOS() const { return OS; }
168
169  /// hasEnvironment - Does this triple have the optional environment
170  /// (fourth) component?
171  bool hasEnvironment() const {
172    return getEnvironmentName() != "";
173  }
174
175  /// getEnvironment - Get the parsed environment type of this triple.
176  EnvironmentType getEnvironment() const { return Environment; }
177
178  /// getOSVersion - Parse the version number from the OS name component of the
179  /// triple, if present.
180  ///
181  /// For example, "fooos1.2.3" would return (1, 2, 3).
182  ///
183  /// If an entry is not defined, it will be returned as 0.
184  void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
185
186  /// getOSMajorVersion - Return just the major version number, this is
187  /// specialized because it is a common query.
188  unsigned getOSMajorVersion() const {
189    unsigned Maj, Min, Micro;
190    getOSVersion(Maj, Min, Micro);
191    return Maj;
192  }
193
194  /// getMacOSXVersion - Parse the version number as with getOSVersion and then
195  /// translate generic "darwin" versions to the corresponding OS X versions.
196  /// This may also be called with IOS triples but the OS X version number is
197  /// just set to a constant 10.4.0 in that case.  Returns true if successful.
198  bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
199                        unsigned &Micro) const;
200
201  /// getiOSVersion - Parse the version number as with getOSVersion.  This should
202  /// only be called with IOS triples.
203  void getiOSVersion(unsigned &Major, unsigned &Minor,
204                     unsigned &Micro) const;
205
206  /// @}
207  /// @name Direct Component Access
208  /// @{
209
210  const std::string &str() const { return Data; }
211
212  const std::string &getTriple() const { return Data; }
213
214  /// getArchName - Get the architecture (first) component of the
215  /// triple.
216  StringRef getArchName() const;
217
218  /// getVendorName - Get the vendor (second) component of the triple.
219  StringRef getVendorName() const;
220
221  /// getOSName - Get the operating system (third) component of the
222  /// triple.
223  StringRef getOSName() const;
224
225  /// getEnvironmentName - Get the optional environment (fourth)
226  /// component of the triple, or "" if empty.
227  StringRef getEnvironmentName() const;
228
229  /// getOSAndEnvironmentName - Get the operating system and optional
230  /// environment components as a single string (separated by a '-'
231  /// if the environment component is present).
232  StringRef getOSAndEnvironmentName() const;
233
234  /// @}
235  /// @name Convenience Predicates
236  /// @{
237
238  /// \brief Test whether the architecture is 64-bit
239  ///
240  /// Note that this tests for 64-bit pointer width, and nothing else. Note
241  /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
242  /// 16-bit. The inner details of pointer width for particular architectures
243  /// is not summed up in the triple, and so only a coarse grained predicate
244  /// system is provided.
245  bool isArch64Bit() const;
246
247  /// \brief Test whether the architecture is 32-bit
248  ///
249  /// Note that this tests for 32-bit pointer width, and nothing else.
250  bool isArch32Bit() const;
251
252  /// \brief Test whether the architecture is 16-bit
253  ///
254  /// Note that this tests for 16-bit pointer width, and nothing else.
255  bool isArch16Bit() const;
256
257  /// isOSVersionLT - Helper function for doing comparisons against version
258  /// numbers included in the target triple.
259  bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
260                     unsigned Micro = 0) const {
261    unsigned LHS[3];
262    getOSVersion(LHS[0], LHS[1], LHS[2]);
263
264    if (LHS[0] != Major)
265      return LHS[0] < Major;
266    if (LHS[1] != Minor)
267      return LHS[1] < Minor;
268    if (LHS[2] != Micro)
269      return LHS[1] < Micro;
270
271    return false;
272  }
273
274  /// isMacOSXVersionLT - Comparison function for checking OS X version
275  /// compatibility, which handles supporting skewed version numbering schemes
276  /// used by the "darwin" triples.
277  unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
278                             unsigned Micro = 0) const {
279    assert(isMacOSX() && "Not an OS X triple!");
280
281    // If this is OS X, expect a sane version number.
282    if (getOS() == Triple::MacOSX)
283      return isOSVersionLT(Major, Minor, Micro);
284
285    // Otherwise, compare to the "Darwin" number.
286    assert(Major == 10 && "Unexpected major version");
287    return isOSVersionLT(Minor + 4, Micro, 0);
288  }
289
290  /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
291  /// "darwin" and "osx" as OS X triples.
292  bool isMacOSX() const {
293    return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
294  }
295
296  /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
297  bool isOSDarwin() const {
298    return isMacOSX() || getOS() == Triple::IOS;
299  }
300
301  /// \brief Tests for either Cygwin or MinGW OS
302  bool isOSCygMing() const {
303    return getOS() == Triple::Cygwin || getOS() == Triple::MinGW32;
304  }
305
306  /// isOSWindows - Is this a "Windows" OS.
307  bool isOSWindows() const {
308    return getOS() == Triple::Win32 || isOSCygMing();
309  }
310
311  /// \brief Tests whether the OS uses the ELF binary format.
312  bool isOSBinFormatELF() const {
313    return !isOSDarwin() && !isOSWindows();
314  }
315
316  /// \brief Tests whether the OS uses the COFF binary format.
317  bool isOSBinFormatCOFF() const {
318    return isOSWindows();
319  }
320
321  /// \brief Tests whether the environment is MachO.
322  // FIXME: Should this be an OSBinFormat predicate?
323  bool isEnvironmentMachO() const {
324    return getEnvironment() == Triple::MachO || isOSDarwin();
325  }
326
327  /// @}
328  /// @name Mutators
329  /// @{
330
331  /// setArch - Set the architecture (first) component of the triple
332  /// to a known type.
333  void setArch(ArchType Kind);
334
335  /// setVendor - Set the vendor (second) component of the triple to a
336  /// known type.
337  void setVendor(VendorType Kind);
338
339  /// setOS - Set the operating system (third) component of the triple
340  /// to a known type.
341  void setOS(OSType Kind);
342
343  /// setEnvironment - Set the environment (fourth) component of the triple
344  /// to a known type.
345  void setEnvironment(EnvironmentType Kind);
346
347  /// setTriple - Set all components to the new triple \p Str.
348  void setTriple(const Twine &Str);
349
350  /// setArchName - Set the architecture (first) component of the
351  /// triple by name.
352  void setArchName(StringRef Str);
353
354  /// setVendorName - Set the vendor (second) component of the triple
355  /// by name.
356  void setVendorName(StringRef Str);
357
358  /// setOSName - Set the operating system (third) component of the
359  /// triple by name.
360  void setOSName(StringRef Str);
361
362  /// setEnvironmentName - Set the optional environment (fourth)
363  /// component of the triple by name.
364  void setEnvironmentName(StringRef Str);
365
366  /// setOSAndEnvironmentName - Set the operating system and optional
367  /// environment components with a single string.
368  void setOSAndEnvironmentName(StringRef Str);
369
370  /// getArchNameForAssembler - Get an architecture name that is understood by
371  /// the target assembler.
372  const char *getArchNameForAssembler();
373
374  /// @}
375  /// @name Helpers to build variants of a particular triple.
376  /// @{
377
378  /// \brief Form a triple with a 32-bit variant of the current architecture.
379  ///
380  /// This can be used to move across "families" of architectures where useful.
381  ///
382  /// \returns A new triple with a 32-bit architecture or an unknown
383  ///          architecture if no such variant can be found.
384  llvm::Triple get32BitArchVariant() const;
385
386  /// \brief Form a triple with a 64-bit variant of the current architecture.
387  ///
388  /// This can be used to move across "families" of architectures where useful.
389  ///
390  /// \returns A new triple with a 64-bit architecture or an unknown
391  ///          architecture if no such variant can be found.
392  llvm::Triple get64BitArchVariant() const;
393
394  /// @}
395  /// @name Static helpers for IDs.
396  /// @{
397
398  /// getArchTypeName - Get the canonical name for the \p Kind architecture.
399  static const char *getArchTypeName(ArchType Kind);
400
401  /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
402  /// architecture. This is the prefix used by the architecture specific
403  /// builtins, and is suitable for passing to \see
404  /// Intrinsic::getIntrinsicForGCCBuiltin().
405  ///
406  /// \return - The architecture prefix, or 0 if none is defined.
407  static const char *getArchTypePrefix(ArchType Kind);
408
409  /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
410  static const char *getVendorTypeName(VendorType Kind);
411
412  /// getOSTypeName - Get the canonical name for the \p Kind operating system.
413  static const char *getOSTypeName(OSType Kind);
414
415  /// getEnvironmentTypeName - Get the canonical name for the \p Kind
416  /// environment.
417  static const char *getEnvironmentTypeName(EnvironmentType Kind);
418
419  /// @}
420  /// @name Static helpers for converting alternate architecture names.
421  /// @{
422
423  /// getArchTypeForLLVMName - The canonical type for the given LLVM
424  /// architecture name (e.g., "x86").
425  static ArchType getArchTypeForLLVMName(StringRef Str);
426
427  /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
428  /// architecture name, for example as accepted by "gcc -arch" (see also
429  /// arch(3)).
430  static ArchType getArchTypeForDarwinArchName(StringRef Str);
431
432  /// @}
433};
434
435} // End llvm namespace
436
437
438#endif
439