Triple.h revision 6904f05e607b6bbdfa96a2ebb628ebf3a1f21455
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,    // i*86-pc-mingw32, *-w64-mingw32
88    NetBSD,
89    OpenBSD,
90    Psp,
91    Solaris,
92    Win32,
93    Haiku,
94    Minix
95  };
96  enum EnvironmentType {
97    UnknownEnvironment,
98
99    GNU,
100    GNUEABI,
101    EABI,
102    MachO
103  };
104
105private:
106  std::string Data;
107
108  /// The parsed arch type (or InvalidArch if uninitialized).
109  mutable ArchType Arch;
110
111  /// The parsed vendor type.
112  mutable VendorType Vendor;
113
114  /// The parsed OS type.
115  mutable OSType OS;
116
117  /// The parsed Environment type.
118  mutable EnvironmentType Environment;
119
120  bool isInitialized() const { return Arch != InvalidArch; }
121  static ArchType ParseArch(StringRef ArchName);
122  static VendorType ParseVendor(StringRef VendorName);
123  static OSType ParseOS(StringRef OSName);
124  static EnvironmentType ParseEnvironment(StringRef EnvironmentName);
125  void Parse() const;
126
127public:
128  /// @name Constructors
129  /// @{
130
131  Triple() : Data(), Arch(InvalidArch) {}
132  explicit Triple(StringRef Str) : Data(Str), Arch(InvalidArch) {}
133  explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr)
134    : Data(ArchStr), Arch(InvalidArch) {
135    Data += '-';
136    Data += VendorStr;
137    Data += '-';
138    Data += OSStr;
139  }
140
141  explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr,
142    StringRef EnvironmentStr)
143    : Data(ArchStr), Arch(InvalidArch) {
144    Data += '-';
145    Data += VendorStr;
146    Data += '-';
147    Data += OSStr;
148    Data += '-';
149    Data += EnvironmentStr;
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  /// @}
197  /// @name Direct Component Access
198  /// @{
199
200  const std::string &str() const { return Data; }
201
202  const std::string &getTriple() const { return Data; }
203
204  /// getArchName - Get the architecture (first) component of the
205  /// triple.
206  StringRef getArchName() const;
207
208  /// getVendorName - Get the vendor (second) component of the triple.
209  StringRef getVendorName() const;
210
211  /// getOSName - Get the operating system (third) component of the
212  /// triple.
213  StringRef getOSName() const;
214
215  /// getEnvironmentName - Get the optional environment (fourth)
216  /// component of the triple, or "" if empty.
217  StringRef getEnvironmentName() const;
218
219  /// getOSAndEnvironmentName - Get the operating system and optional
220  /// environment components as a single string (separated by a '-'
221  /// if the environment component is present).
222  StringRef getOSAndEnvironmentName() const;
223
224
225  /// getDarwinNumber - Parse the 'darwin number' out of the specific target
226  /// triple.  For example, if we have darwin8.5 return 8,5,0.  If any entry is
227  /// not defined, return 0's.  This requires that the triple have an OSType of
228  /// darwin before it is called.
229  void getDarwinNumber(unsigned &Maj, unsigned &Min, unsigned &Revision) const;
230
231  /// getDarwinMajorNumber - Return just the major version number, this is
232  /// specialized because it is a common query.
233  unsigned getDarwinMajorNumber() const {
234    unsigned Maj, Min, Rev;
235    getDarwinNumber(Maj, Min, Rev);
236    return Maj;
237  }
238
239  /// @}
240  /// @name Mutators
241  /// @{
242
243  /// setArch - Set the architecture (first) component of the triple
244  /// to a known type.
245  void setArch(ArchType Kind);
246
247  /// setVendor - Set the vendor (second) component of the triple to a
248  /// known type.
249  void setVendor(VendorType Kind);
250
251  /// setOS - Set the operating system (third) component of the triple
252  /// to a known type.
253  void setOS(OSType Kind);
254
255  /// setEnvironment - Set the environment (fourth) component of the triple
256  /// to a known type.
257  void setEnvironment(EnvironmentType Kind);
258
259  /// setTriple - Set all components to the new triple \arg Str.
260  void setTriple(const Twine &Str);
261
262  /// setArchName - Set the architecture (first) component of the
263  /// triple by name.
264  void setArchName(StringRef Str);
265
266  /// setVendorName - Set the vendor (second) component of the triple
267  /// by name.
268  void setVendorName(StringRef Str);
269
270  /// setOSName - Set the operating system (third) component of the
271  /// triple by name.
272  void setOSName(StringRef Str);
273
274  /// setEnvironmentName - Set the optional environment (fourth)
275  /// component of the triple by name.
276  void setEnvironmentName(StringRef Str);
277
278  /// setOSAndEnvironmentName - Set the operating system and optional
279  /// environment components with a single string.
280  void setOSAndEnvironmentName(StringRef Str);
281
282  /// getArchNameForAssembler - Get an architecture name that is understood by
283  /// the target assembler.
284  const char *getArchNameForAssembler();
285
286  /// @}
287  /// @name Static helpers for IDs.
288  /// @{
289
290  /// getArchTypeName - Get the canonical name for the \arg Kind
291  /// architecture.
292  static const char *getArchTypeName(ArchType Kind);
293
294  /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
295  /// architecture. This is the prefix used by the architecture specific
296  /// builtins, and is suitable for passing to \see
297  /// Intrinsic::getIntrinsicForGCCBuiltin().
298  ///
299  /// \return - The architecture prefix, or 0 if none is defined.
300  static const char *getArchTypePrefix(ArchType Kind);
301
302  /// getVendorTypeName - Get the canonical name for the \arg Kind
303  /// vendor.
304  static const char *getVendorTypeName(VendorType Kind);
305
306  /// getOSTypeName - Get the canonical name for the \arg Kind operating
307  /// system.
308  static const char *getOSTypeName(OSType Kind);
309
310  /// getEnvironmentTypeName - Get the canonical name for the \arg Kind
311  /// environment.
312  static const char *getEnvironmentTypeName(EnvironmentType Kind);
313
314  /// @}
315  /// @name Static helpers for converting alternate architecture names.
316  /// @{
317
318  /// getArchTypeForLLVMName - The canonical type for the given LLVM
319  /// architecture name (e.g., "x86").
320  static ArchType getArchTypeForLLVMName(StringRef Str);
321
322  /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
323  /// architecture name, for example as accepted by "gcc -arch" (see also
324  /// arch(3)).
325  static ArchType getArchTypeForDarwinArchName(StringRef Str);
326
327  /// @}
328};
329
330} // End llvm namespace
331
332
333#endif
334