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