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