Triple.h revision 87c06d617917f4a388fbe9db81198e13cde3e431
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 format of:
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; it does not normally change or normalize the triple string, instead
39/// it provides additional APIs to parse normalized parts out of the triple.
40///
41/// One curiosity this implies is that for some odd triples the results of,
42/// e.g., getOSName() can be very different from the result of getOS().  For
43/// example, for 'i386-mingw32', getOS() will return MinGW32, but since
44/// getOSName() is purely based on the string structure that will return the
45/// empty string.
46///
47/// Clients should generally avoid using getOSName() and related APIs unless
48/// they are familiar with the triple format (this is particularly true when
49/// rewriting a triple).
50///
51/// See autoconf/config.guess for a glimpse into what they look like in
52/// practice.
53class Triple {
54public:
55  enum ArchType {
56    UnknownArch,
57
58    alpha,   // Alpha: alpha
59    arm,     // ARM; arm, armv.*, xscale
60    bfin,    // Blackfin: bfin
61    cellspu, // CellSPU: spu, cellspu
62    mips,    // MIPS: mips, mipsallegrex
63    mipsel,  // MIPSEL: mipsel, mipsallegrexel, psp
64    msp430,  // MSP430: msp430
65    pic16,   // PIC16: pic16
66    ppc,     // PPC: powerpc
67    ppc64,   // PPC64: powerpc64, ppu
68    sparc,   // Sparc: sparc
69    sparcv9, // Sparcv9: Sparcv9
70    systemz, // SystemZ: s390x
71    tce,     // TCE (http://tce.cs.tut.fi/): tce
72    thumb,   // Thumb: thumb, thumbv.*
73    x86,     // X86: i[3-9]86
74    x86_64,  // X86-64: amd64, x86_64
75    xcore,   // XCore: xcore
76
77    InvalidArch
78  };
79  enum VendorType {
80    UnknownVendor,
81
82    Apple,
83    PC
84  };
85  enum OSType {
86    UnknownOS,
87
88    AuroraUX,
89    Cygwin,
90    Darwin,
91    DragonFly,
92    FreeBSD,
93    Linux,
94    Lv2,        // PS3
95    MinGW32,
96    MinGW64,
97    NetBSD,
98    OpenBSD,
99    Psp,
100    Solaris,
101    Win32,
102    Haiku
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  bool isInitialized() const { return Arch != InvalidArch; }
118  void Parse() const;
119
120public:
121  /// @name Constructors
122  /// @{
123
124  Triple() : Data(), Arch(InvalidArch) {}
125  explicit Triple(StringRef Str) : Data(Str), Arch(InvalidArch) {}
126  explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr)
127    : Data(ArchStr), Arch(InvalidArch) {
128    Data += '-';
129    Data += VendorStr;
130    Data += '-';
131    Data += OSStr;
132  }
133
134  /// @}
135  /// @name Typed Component Access
136  /// @{
137
138  /// getArch - Get the parsed architecture type of this triple.
139  ArchType getArch() const {
140    if (!isInitialized()) Parse();
141    return Arch;
142  }
143
144  /// getVendor - Get the parsed vendor type of this triple.
145  VendorType getVendor() const {
146    if (!isInitialized()) Parse();
147    return Vendor;
148  }
149
150  /// getOS - Get the parsed operating system type of this triple.
151  OSType getOS() const {
152    if (!isInitialized()) Parse();
153    return OS;
154  }
155
156  /// hasEnvironment - Does this triple have the optional environment
157  /// (fourth) component?
158  bool hasEnvironment() const {
159    return getEnvironmentName() != "";
160  }
161
162  /// @}
163  /// @name Direct Component Access
164  /// @{
165
166  const std::string &str() const { return Data; }
167
168  const std::string &getTriple() const { return Data; }
169
170  /// getArchName - Get the architecture (first) component of the
171  /// triple.
172  StringRef getArchName() const;
173
174  /// getVendorName - Get the vendor (second) component of the triple.
175  StringRef getVendorName() const;
176
177  /// getOSName - Get the operating system (third) component of the
178  /// triple.
179  StringRef getOSName() const;
180
181  /// getEnvironmentName - Get the optional environment (fourth)
182  /// component of the triple, or "" if empty.
183  StringRef getEnvironmentName() const;
184
185  /// getOSAndEnvironmentName - Get the operating system and optional
186  /// environment components as a single string (separated by a '-'
187  /// if the environment component is present).
188  StringRef getOSAndEnvironmentName() const;
189
190
191  /// getDarwinNumber - Parse the 'darwin number' out of the specific target
192  /// triple.  For example, if we have darwin8.5 return 8,5,0.  If any entry is
193  /// not defined, return 0's.  This requires that the triple have an OSType of
194  /// darwin before it is called.
195  void getDarwinNumber(unsigned &Maj, unsigned &Min, unsigned &Revision) const;
196
197  /// getDarwinMajorNumber - Return just the major version number, this is
198  /// specialized because it is a common query.
199  unsigned getDarwinMajorNumber() const {
200    unsigned Maj, Min, Rev;
201    getDarwinNumber(Maj, Min, Rev);
202    return Maj;
203  }
204
205  /// @}
206  /// @name Mutators
207  /// @{
208
209  /// setArch - Set the architecture (first) component of the triple
210  /// to a known type.
211  void setArch(ArchType Kind);
212
213  /// setVendor - Set the vendor (second) component of the triple to a
214  /// known type.
215  void setVendor(VendorType Kind);
216
217  /// setOS - Set the operating system (third) component of the triple
218  /// to a known type.
219  void setOS(OSType Kind);
220
221  /// setTriple - Set all components to the new triple \arg Str.
222  void setTriple(const Twine &Str);
223
224  /// setArchName - Set the architecture (first) component of the
225  /// triple by name.
226  void setArchName(StringRef Str);
227
228  /// setVendorName - Set the vendor (second) component of the triple
229  /// by name.
230  void setVendorName(StringRef Str);
231
232  /// setOSName - Set the operating system (third) component of the
233  /// triple by name.
234  void setOSName(StringRef Str);
235
236  /// setEnvironmentName - Set the optional environment (fourth)
237  /// component of the triple by name.
238  void setEnvironmentName(StringRef Str);
239
240  /// setOSAndEnvironmentName - Set the operating system and optional
241  /// environment components with a single string.
242  void setOSAndEnvironmentName(StringRef Str);
243
244  /// getArchNameForAssembler - Get an architecture name that is understood by the
245  /// target assembler.
246  const char *getArchNameForAssembler();
247
248  /// @}
249  /// @name Static helpers for IDs.
250  /// @{
251
252  /// getArchTypeName - Get the canonical name for the \arg Kind
253  /// architecture.
254  static const char *getArchTypeName(ArchType Kind);
255
256  /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
257  /// architecture. This is the prefix used by the architecture specific
258  /// builtins, and is suitable for passing to \see
259  /// Intrinsic::getIntrinsicForGCCBuiltin().
260  ///
261  /// \return - The architecture prefix, or 0 if none is defined.
262  static const char *getArchTypePrefix(ArchType Kind);
263
264  /// getVendorTypeName - Get the canonical name for the \arg Kind
265  /// vendor.
266  static const char *getVendorTypeName(VendorType Kind);
267
268  /// getOSTypeName - Get the canonical name for the \arg Kind vendor.
269  static const char *getOSTypeName(OSType Kind);
270
271  /// @}
272  /// @name Static helpers for converting alternate architecture names.
273  /// @{
274
275  /// getArchTypeForLLVMName - The canonical type for the given LLVM
276  /// architecture name (e.g., "x86").
277  static ArchType getArchTypeForLLVMName(StringRef Str);
278
279  /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
280  /// architecture name, for example as accepted by "gcc -arch" (see also
281  /// arch(3)).
282  static ArchType getArchTypeForDarwinArchName(StringRef Str);
283
284  /// @}
285};
286
287} // End llvm namespace
288
289
290#endif
291