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