Triple.h revision 8181827d1b04f7bc6939ab099acc09ef2b247061
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 uses the ELF binary format.
314  bool isOSBinFormatELF() const {
315    return !isOSDarwin() && !isOSWindows();
316  }
317
318  /// \brief Tests whether the OS uses the COFF binary format.
319  bool isOSBinFormatCOFF() const {
320    return isOSWindows();
321  }
322
323  /// \brief Tests whether the environment is MachO.
324  // FIXME: Should this be an OSBinFormat predicate?
325  bool isEnvironmentMachO() const {
326    return getEnvironment() == Triple::MachO || isOSDarwin();
327  }
328
329  /// @}
330  /// @name Mutators
331  /// @{
332
333  /// setArch - Set the architecture (first) component of the triple
334  /// to a known type.
335  void setArch(ArchType Kind);
336
337  /// setVendor - Set the vendor (second) component of the triple to a
338  /// known type.
339  void setVendor(VendorType Kind);
340
341  /// setOS - Set the operating system (third) component of the triple
342  /// to a known type.
343  void setOS(OSType Kind);
344
345  /// setEnvironment - Set the environment (fourth) component of the triple
346  /// to a known type.
347  void setEnvironment(EnvironmentType Kind);
348
349  /// setTriple - Set all components to the new triple \p Str.
350  void setTriple(const Twine &Str);
351
352  /// setArchName - Set the architecture (first) component of the
353  /// triple by name.
354  void setArchName(StringRef Str);
355
356  /// setVendorName - Set the vendor (second) component of the triple
357  /// by name.
358  void setVendorName(StringRef Str);
359
360  /// setOSName - Set the operating system (third) component of the
361  /// triple by name.
362  void setOSName(StringRef Str);
363
364  /// setEnvironmentName - Set the optional environment (fourth)
365  /// component of the triple by name.
366  void setEnvironmentName(StringRef Str);
367
368  /// setOSAndEnvironmentName - Set the operating system and optional
369  /// environment components with a single string.
370  void setOSAndEnvironmentName(StringRef Str);
371
372  /// getArchNameForAssembler - Get an architecture name that is understood by
373  /// the target assembler.
374  const char *getArchNameForAssembler();
375
376  /// @}
377  /// @name Helpers to build variants of a particular triple.
378  /// @{
379
380  /// \brief Form a triple with a 32-bit variant of the current architecture.
381  ///
382  /// This can be used to move across "families" of architectures where useful.
383  ///
384  /// \returns A new triple with a 32-bit architecture or an unknown
385  ///          architecture if no such variant can be found.
386  llvm::Triple get32BitArchVariant() const;
387
388  /// \brief Form a triple with a 64-bit variant of the current architecture.
389  ///
390  /// This can be used to move across "families" of architectures where useful.
391  ///
392  /// \returns A new triple with a 64-bit architecture or an unknown
393  ///          architecture if no such variant can be found.
394  llvm::Triple get64BitArchVariant() const;
395
396  /// @}
397  /// @name Static helpers for IDs.
398  /// @{
399
400  /// getArchTypeName - Get the canonical name for the \p Kind architecture.
401  static const char *getArchTypeName(ArchType Kind);
402
403  /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
404  /// architecture. This is the prefix used by the architecture specific
405  /// builtins, and is suitable for passing to \see
406  /// Intrinsic::getIntrinsicForGCCBuiltin().
407  ///
408  /// \return - The architecture prefix, or 0 if none is defined.
409  static const char *getArchTypePrefix(ArchType Kind);
410
411  /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
412  static const char *getVendorTypeName(VendorType Kind);
413
414  /// getOSTypeName - Get the canonical name for the \p Kind operating system.
415  static const char *getOSTypeName(OSType Kind);
416
417  /// getEnvironmentTypeName - Get the canonical name for the \p Kind
418  /// environment.
419  static const char *getEnvironmentTypeName(EnvironmentType Kind);
420
421  /// @}
422  /// @name Static helpers for converting alternate architecture names.
423  /// @{
424
425  /// getArchTypeForLLVMName - The canonical type for the given LLVM
426  /// architecture name (e.g., "x86").
427  static ArchType getArchTypeForLLVMName(StringRef Str);
428
429  /// @}
430};
431
432} // End llvm namespace
433
434
435#endif
436