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