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