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