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