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