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