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