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