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