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