Triple.h revision 198396
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
68    sparc,   // Sparc: sparc
69    systemz, // SystemZ: s390x
70    tce,     // TCE (http://tce.cs.tut.fi/): tce
71    thumb,   // Thumb: thumb, thumbv.*
72    x86,     // X86: i[3-9]86
73    x86_64,  // X86-64: amd64, x86_64
74    xcore,   // XCore: xcore
75
76    InvalidArch
77  };
78  enum VendorType {
79    UnknownVendor,
80
81    Apple,
82    PC
83  };
84  enum OSType {
85    UnknownOS,
86
87    AuroraUX,
88    Cygwin,
89    Darwin,
90    DragonFly,
91    FreeBSD,
92    Linux,
93    MinGW32,
94    MinGW64,
95    NetBSD,
96    OpenBSD,
97    Solaris,
98    Win32,
99    Haiku
100  };
101
102private:
103  std::string Data;
104
105  /// The parsed arch type (or InvalidArch if uninitialized).
106  mutable ArchType Arch;
107
108  /// The parsed vendor type.
109  mutable VendorType Vendor;
110
111  /// The parsed OS type.
112  mutable OSType OS;
113
114  bool isInitialized() const { return Arch != InvalidArch; }
115  void Parse() const;
116
117public:
118  /// @name Constructors
119  /// @{
120
121  Triple() : Data(), Arch(InvalidArch) {}
122  explicit Triple(StringRef Str) : Data(Str), Arch(InvalidArch) {}
123  explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr)
124    : Data(ArchStr), Arch(InvalidArch) {
125    Data += '-';
126    Data += VendorStr;
127    Data += '-';
128    Data += OSStr;
129  }
130
131  /// @}
132  /// @name Typed Component Access
133  /// @{
134
135  /// getArch - Get the parsed architecture type of this triple.
136  ArchType getArch() const {
137    if (!isInitialized()) Parse();
138    return Arch;
139  }
140
141  /// getVendor - Get the parsed vendor type of this triple.
142  VendorType getVendor() const {
143    if (!isInitialized()) Parse();
144    return Vendor;
145  }
146
147  /// getOS - Get the parsed operating system type of this triple.
148  OSType getOS() const {
149    if (!isInitialized()) Parse();
150    return OS;
151  }
152
153  /// hasEnvironment - Does this triple have the optional environment
154  /// (fourth) component?
155  bool hasEnvironment() const {
156    return getEnvironmentName() != "";
157  }
158
159  /// @}
160  /// @name Direct Component Access
161  /// @{
162
163  const std::string &getTriple() const { return Data; }
164
165  /// getArchName - Get the architecture (first) component of the
166  /// triple.
167  StringRef getArchName() const;
168
169  /// getVendorName - Get the vendor (second) component of the triple.
170  StringRef getVendorName() const;
171
172  /// getOSName - Get the operating system (third) component of the
173  /// triple.
174  StringRef getOSName() const;
175
176  /// getEnvironmentName - Get the optional environment (fourth)
177  /// component of the triple, or "" if empty.
178  StringRef getEnvironmentName() const;
179
180  /// getOSAndEnvironmentName - Get the operating system and optional
181  /// environment components as a single string (separated by a '-'
182  /// if the environment component is present).
183  StringRef getOSAndEnvironmentName() const;
184
185
186  /// getDarwinNumber - Parse the 'darwin number' out of the specific target
187  /// triple.  For example, if we have darwin8.5 return 8,5,0.  If any entry is
188  /// not defined, return 0's.  This requires that the triple have an OSType of
189  /// darwin before it is called.
190  void getDarwinNumber(unsigned &Maj, unsigned &Min, unsigned &Revision) const;
191
192  /// getDarwinMajorNumber - Return just the major version number, this is
193  /// specialized because it is a common query.
194  unsigned getDarwinMajorNumber() const {
195    unsigned Maj, Min, Rev;
196    getDarwinNumber(Maj, Min, Rev);
197    return Maj;
198  }
199
200  /// @}
201  /// @name Mutators
202  /// @{
203
204  /// setArch - Set the architecture (first) component of the triple
205  /// to a known type.
206  void setArch(ArchType Kind);
207
208  /// setVendor - Set the vendor (second) component of the triple to a
209  /// known type.
210  void setVendor(VendorType Kind);
211
212  /// setOS - Set the operating system (third) component of the triple
213  /// to a known type.
214  void setOS(OSType Kind);
215
216  /// setTriple - Set all components to the new triple \arg Str.
217  void setTriple(const Twine &Str);
218
219  /// setArchName - Set the architecture (first) component of the
220  /// triple by name.
221  void setArchName(const StringRef &Str);
222
223  /// setVendorName - Set the vendor (second) component of the triple
224  /// by name.
225  void setVendorName(const StringRef &Str);
226
227  /// setOSName - Set the operating system (third) component of the
228  /// triple by name.
229  void setOSName(const StringRef &Str);
230
231  /// setEnvironmentName - Set the optional environment (fourth)
232  /// component of the triple by name.
233  void setEnvironmentName(const StringRef &Str);
234
235  /// setOSAndEnvironmentName - Set the operating system and optional
236  /// environment components with a single string.
237  void setOSAndEnvironmentName(const StringRef &Str);
238
239  /// @}
240  /// @name Static helpers for IDs.
241  /// @{
242
243  /// getArchTypeName - Get the canonical name for the \arg Kind
244  /// architecture.
245  static const char *getArchTypeName(ArchType Kind);
246
247  /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
248  /// architecture. This is the prefix used by the architecture specific
249  /// builtins, and is suitable for passing to \see
250  /// Intrinsic::getIntrinsicForGCCBuiltin().
251  ///
252  /// \return - The architecture prefix, or 0 if none is defined.
253  static const char *getArchTypePrefix(ArchType Kind);
254
255  /// getVendorTypeName - Get the canonical name for the \arg Kind
256  /// vendor.
257  static const char *getVendorTypeName(VendorType Kind);
258
259  /// getOSTypeName - Get the canonical name for the \arg Kind vendor.
260  static const char *getOSTypeName(OSType Kind);
261
262  /// @}
263  /// @name Static helpers for converting alternate architecture names.
264  /// @{
265
266  /// getArchTypeForLLVMName - The canonical type for the given LLVM
267  /// architecture name (e.g., "x86").
268  static ArchType getArchTypeForLLVMName(const StringRef &Str);
269
270  /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
271  /// architecture name, for example as accepted by "gcc -arch" (see also
272  /// arch(3)).
273  static ArchType getArchTypeForDarwinArchName(const StringRef &Str);
274
275  /// @}
276};
277
278} // End llvm namespace
279
280
281#endif
282