Triple.h revision ae200c60c31666da81a8550172de03a9f417ba1b
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/StringRef.h"
14#include <string>
15
16// Some system headers or GCC predefined macros conflict with identifiers in
17// this file.  Undefine them here.
18#undef mips
19#undef sparc
20
21namespace llvm {
22class StringRef;
23class Twine;
24
25/// Triple - Helper class for working with target triples.
26///
27/// Target triples are strings in the canonical form:
28///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM
29/// or
30///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
31///
32/// This class is used for clients which want to support arbitrary
33/// target triples, but also want to implement certain special
34/// behavior for particular targets. This class isolates the mapping
35/// from the components of the target triple to well known IDs.
36///
37/// At its core the Triple class is designed to be a wrapper for a triple
38/// string; the constructor does not change or normalize the triple string.
39/// Clients that need to handle the non-canonical triples that users often
40/// specify should use the normalize method.
41///
42/// See autoconf/config.guess for a glimpse into what triples look like in
43/// practice.
44class Triple {
45public:
46  enum ArchType {
47    UnknownArch,
48
49    alpha,   // Alpha: alpha
50    arm,     // ARM; arm, armv.*, xscale
51    bfin,    // Blackfin: bfin
52    cellspu, // CellSPU: spu, cellspu
53    mips,    // MIPS: mips, mipsallegrex
54    mipsel,  // MIPSEL: mipsel, mipsallegrexel, psp
55    msp430,  // MSP430: msp430
56    ppc,     // PPC: powerpc
57    ppc64,   // PPC64: powerpc64, ppu
58    sparc,   // Sparc: sparc
59    sparcv9, // Sparcv9: Sparcv9
60    systemz, // SystemZ: s390x
61    tce,     // TCE (http://tce.cs.tut.fi/): tce
62    thumb,   // Thumb: thumb, thumbv.*
63    x86,     // X86: i[3-9]86
64    x86_64,  // X86-64: amd64, x86_64
65    xcore,   // XCore: xcore
66    mblaze,  // MBlaze: mblaze
67    ptx,     // PTX: ptx
68
69    InvalidArch
70  };
71  enum VendorType {
72    UnknownVendor,
73
74    Apple,
75    PC
76  };
77  enum OSType {
78    UnknownOS,
79
80    AuroraUX,
81    Cygwin,
82    Darwin,
83    DragonFly,
84    FreeBSD,
85    Linux,
86    Lv2,        // PS3
87    MinGW32,
88    MinGW64,
89    NetBSD,
90    OpenBSD,
91    Psp,
92    Solaris,
93    Win32,
94    Haiku,
95    Minix
96  };
97  enum EnvironmentType {
98    UnknownEnvironment,
99
100    GNU,
101    GNUEABI,
102    EABI,
103    MachO
104  };
105
106private:
107  std::string Data;
108
109  /// The parsed arch type (or InvalidArch if uninitialized).
110  mutable ArchType Arch;
111
112  /// The parsed vendor type.
113  mutable VendorType Vendor;
114
115  /// The parsed OS type.
116  mutable OSType OS;
117
118  /// The parsed Environment type.
119  mutable EnvironmentType Environment;
120
121  bool isInitialized() const { return Arch != InvalidArch; }
122  static ArchType ParseArch(StringRef ArchName);
123  static VendorType ParseVendor(StringRef VendorName);
124  static OSType ParseOS(StringRef OSName);
125  static EnvironmentType ParseEnvironment(StringRef EnvironmentName);
126  void Parse() const;
127
128public:
129  /// @name Constructors
130  /// @{
131
132  Triple() : Data(), Arch(InvalidArch) {}
133  explicit Triple(StringRef Str) : Data(Str), Arch(InvalidArch) {}
134  explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr)
135    : Data(ArchStr), Arch(InvalidArch) {
136    Data += '-';
137    Data += VendorStr;
138    Data += '-';
139    Data += OSStr;
140  }
141
142  explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr,
143    StringRef EnvironmentStr)
144    : Data(ArchStr), Arch(InvalidArch) {
145    Data += '-';
146    Data += VendorStr;
147    Data += '-';
148    Data += OSStr;
149    Data += '-';
150    Data += EnvironmentStr;
151  }
152
153  /// @}
154  /// @name Normalization
155  /// @{
156
157  /// normalize - Turn an arbitrary machine specification into the canonical
158  /// triple form (or something sensible that the Triple class understands if
159  /// nothing better can reasonably be done).  In particular, it handles the
160  /// common case in which otherwise valid components are in the wrong order.
161  static std::string normalize(StringRef Str);
162
163  /// @}
164  /// @name Typed Component Access
165  /// @{
166
167  /// getArch - Get the parsed architecture type of this triple.
168  ArchType getArch() const {
169    if (!isInitialized()) Parse();
170    return Arch;
171  }
172
173  /// getVendor - Get the parsed vendor type of this triple.
174  VendorType getVendor() const {
175    if (!isInitialized()) Parse();
176    return Vendor;
177  }
178
179  /// getOS - Get the parsed operating system type of this triple.
180  OSType getOS() const {
181    if (!isInitialized()) Parse();
182    return OS;
183  }
184
185  /// hasEnvironment - Does this triple have the optional environment
186  /// (fourth) component?
187  bool hasEnvironment() const {
188    return getEnvironmentName() != "";
189  }
190
191  /// getEnvironment - Get the parsed environment type of this triple.
192  EnvironmentType getEnvironment() const {
193    if (!isInitialized()) Parse();
194    return Environment;
195  }
196
197  /// @}
198  /// @name Direct Component Access
199  /// @{
200
201  const std::string &str() const { return Data; }
202
203  const std::string &getTriple() const { return Data; }
204
205  /// getArchName - Get the architecture (first) component of the
206  /// triple.
207  StringRef getArchName() const;
208
209  /// getVendorName - Get the vendor (second) component of the triple.
210  StringRef getVendorName() const;
211
212  /// getOSName - Get the operating system (third) component of the
213  /// triple.
214  StringRef getOSName() const;
215
216  /// getEnvironmentName - Get the optional environment (fourth)
217  /// component of the triple, or "" if empty.
218  StringRef getEnvironmentName() const;
219
220  /// getOSAndEnvironmentName - Get the operating system and optional
221  /// environment components as a single string (separated by a '-'
222  /// if the environment component is present).
223  StringRef getOSAndEnvironmentName() const;
224
225
226  /// getDarwinNumber - Parse the 'darwin number' out of the specific target
227  /// triple.  For example, if we have darwin8.5 return 8,5,0.  If any entry is
228  /// not defined, return 0's.  This requires that the triple have an OSType of
229  /// darwin before it is called.
230  void getDarwinNumber(unsigned &Maj, unsigned &Min, unsigned &Revision) const;
231
232  /// getDarwinMajorNumber - Return just the major version number, this is
233  /// specialized because it is a common query.
234  unsigned getDarwinMajorNumber() const {
235    unsigned Maj, Min, Rev;
236    getDarwinNumber(Maj, Min, Rev);
237    return Maj;
238  }
239
240  /// @}
241  /// @name Mutators
242  /// @{
243
244  /// setArch - Set the architecture (first) component of the triple
245  /// to a known type.
246  void setArch(ArchType Kind);
247
248  /// setVendor - Set the vendor (second) component of the triple to a
249  /// known type.
250  void setVendor(VendorType Kind);
251
252  /// setOS - Set the operating system (third) component of the triple
253  /// to a known type.
254  void setOS(OSType Kind);
255
256  /// setEnvironment - Set the environment (fourth) component of the triple
257  /// to a known type.
258  void setEnvironment(EnvironmentType Kind);
259
260  /// setTriple - Set all components to the new triple \arg Str.
261  void setTriple(const Twine &Str);
262
263  /// setArchName - Set the architecture (first) component of the
264  /// triple by name.
265  void setArchName(StringRef Str);
266
267  /// setVendorName - Set the vendor (second) component of the triple
268  /// by name.
269  void setVendorName(StringRef Str);
270
271  /// setOSName - Set the operating system (third) component of the
272  /// triple by name.
273  void setOSName(StringRef Str);
274
275  /// setEnvironmentName - Set the optional environment (fourth)
276  /// component of the triple by name.
277  void setEnvironmentName(StringRef Str);
278
279  /// setOSAndEnvironmentName - Set the operating system and optional
280  /// environment components with a single string.
281  void setOSAndEnvironmentName(StringRef Str);
282
283  /// getArchNameForAssembler - Get an architecture name that is understood by
284  /// the target assembler.
285  const char *getArchNameForAssembler();
286
287  /// @}
288  /// @name Static helpers for IDs.
289  /// @{
290
291  /// getArchTypeName - Get the canonical name for the \arg Kind
292  /// architecture.
293  static const char *getArchTypeName(ArchType Kind);
294
295  /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
296  /// architecture. This is the prefix used by the architecture specific
297  /// builtins, and is suitable for passing to \see
298  /// Intrinsic::getIntrinsicForGCCBuiltin().
299  ///
300  /// \return - The architecture prefix, or 0 if none is defined.
301  static const char *getArchTypePrefix(ArchType Kind);
302
303  /// getVendorTypeName - Get the canonical name for the \arg Kind
304  /// vendor.
305  static const char *getVendorTypeName(VendorType Kind);
306
307  /// getOSTypeName - Get the canonical name for the \arg Kind operating
308  /// system.
309  static const char *getOSTypeName(OSType Kind);
310
311  /// getEnvironmentTypeName - Get the canonical name for the \arg Kind
312  /// environment.
313  static const char *getEnvironmentTypeName(EnvironmentType Kind);
314
315  /// @}
316  /// @name Static helpers for converting alternate architecture names.
317  /// @{
318
319  /// getArchTypeForLLVMName - The canonical type for the given LLVM
320  /// architecture name (e.g., "x86").
321  static ArchType getArchTypeForLLVMName(StringRef Str);
322
323  /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
324  /// architecture name, for example as accepted by "gcc -arch" (see also
325  /// arch(3)).
326  static ArchType getArchTypeForDarwinArchName(StringRef Str);
327
328  /// @}
329};
330
331} // End llvm namespace
332
333
334#endif
335