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