Triple.h revision 651aa689cc37d5932fac0e096aa8b536f282aa21
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
16namespace llvm {
17class StringRef;
18class Twine;
19
20/// Triple - Helper class for working with target triples.
21///
22/// Target triples are strings in the format of:
23///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM
24/// or
25///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
26///
27/// This class is used for clients which want to support arbitrary
28/// target triples, but also want to implement certain special
29/// behavior for particular targets. This class isolates the mapping
30/// from the components of the target triple to well known IDs.
31///
32/// At its core the Triple class is designed to be a wrapper for a triple
33/// string; it does not normally change or normalize the triple string, instead
34/// it provides additional APIs to parse normalized parts out of the triple.
35///
36/// One curiosity this implies is that for some odd triples the results of,
37/// e.g., getOSName() can be very different from the result of getOS().  For
38/// example, for 'i386-mingw32', getOS() will return MinGW32, but since
39/// getOSName() is purely based on the string structure that will return the
40/// empty string.
41///
42/// Clients should generally avoid using getOSName() and related APIs unless
43/// they are familiar with the triple format (this is particularly true when
44/// rewriting a triple).
45///
46/// See autoconf/config.guess for a glimpse into what they look like in
47/// practice.
48class Triple {
49public:
50  enum ArchType {
51    UnknownArch,
52
53    alpha,   // Alpha: alpha
54    arm,     // ARM; arm, armv.*, xscale
55    bfin,    // Blackfin: bfin
56    cellspu, // CellSPU: spu, cellspu
57    mips,    // MIPS: mips, mipsallegrex
58    mipsel,  // MIPSEL: mipsel, mipsallegrexel, psp
59    msp430,  // MSP430: msp430
60    pic16,   // PIC16: pic16
61    ppc,     // PPC: powerpc
62    ppc64,   // PPC64: powerpc64
63    sparc,   // Sparc: sparc
64    systemz, // SystemZ: s390x
65    thumb,   // Thumb: thumb, thumbv.*
66    x86,     // X86: i[3-9]86
67    x86_64,  // X86-64: amd64, x86_64
68    xcore,   // XCore: xcore
69
70    InvalidArch
71  };
72  enum VendorType {
73    UnknownVendor,
74
75    Apple,
76    PC
77  };
78  enum OSType {
79    UnknownOS,
80
81    AuroraUX,
82    Cygwin,
83    Darwin,
84    DragonFly,
85    FreeBSD,
86    Linux,
87    MinGW32,
88    MinGW64,
89    NetBSD,
90    OpenBSD,
91    Solaris,
92    Win32
93  };
94
95private:
96  std::string Data;
97
98  /// The parsed arch type (or InvalidArch if uninitialized).
99  mutable ArchType Arch;
100
101  /// The parsed vendor type.
102  mutable VendorType Vendor;
103
104  /// The parsed OS type.
105  mutable OSType OS;
106
107  bool isInitialized() const { return Arch != InvalidArch; }
108  void Parse() const;
109
110public:
111  /// @name Constructors
112  /// @{
113
114  Triple() : Data(), Arch(InvalidArch) {}
115  explicit Triple(const StringRef &Str) : Data(Str), Arch(InvalidArch) {}
116  explicit Triple(const char *ArchStr, const char *VendorStr, const char *OSStr)
117    : Data(ArchStr), Arch(InvalidArch) {
118    Data += '-';
119    Data += VendorStr;
120    Data += '-';
121    Data += OSStr;
122  }
123
124  /// @}
125  /// @name Typed Component Access
126  /// @{
127
128  /// getArch - Get the parsed architecture type of this triple.
129  ArchType getArch() const {
130    if (!isInitialized()) Parse();
131    return Arch;
132  }
133
134  /// getVendor - Get the parsed vendor type of this triple.
135  VendorType getVendor() const {
136    if (!isInitialized()) Parse();
137    return Vendor;
138  }
139
140  /// getOS - Get the parsed operating system type of this triple.
141  OSType getOS() const {
142    if (!isInitialized()) Parse();
143    return OS;
144  }
145
146  /// hasEnvironment - Does this triple have the optional environment
147  /// (fourth) component?
148  bool hasEnvironment() const {
149    return getEnvironmentName() != "";
150  }
151
152  /// @}
153  /// @name Direct Component Access
154  /// @{
155
156  const std::string &getTriple() const { return Data; }
157
158  /// getArchName - Get the architecture (first) component of the
159  /// triple.
160  StringRef getArchName() const;
161
162  /// getVendorName - Get the vendor (second) component of the triple.
163  StringRef getVendorName() const;
164
165  /// getOSName - Get the operating system (third) component of the
166  /// triple.
167  StringRef getOSName() const;
168
169  /// getEnvironmentName - Get the optional environment (fourth)
170  /// component of the triple, or "" if empty.
171  StringRef getEnvironmentName() const;
172
173  /// getOSAndEnvironmentName - Get the operating system and optional
174  /// environment components as a single string (separated by a '-'
175  /// if the environment component is present).
176  StringRef getOSAndEnvironmentName() const;
177
178
179  /// getDarwinNumber - Parse the 'darwin number' out of the specific target
180  /// triple.  For example, if we have darwin8.5 return 8,5,0.  If any entry is
181  /// not defined, return 0's.  This requires that the triple have an OSType of
182  /// darwin before it is called.
183  void getDarwinNumber(unsigned &Maj, unsigned &Min, unsigned &Revision) const;
184
185  /// getDarwinMajorNumber - Return just the major version number, this is
186  /// specialized because it is a common query.
187  unsigned getDarwinMajorNumber() const {
188    unsigned Maj, Min, Rev;
189    getDarwinNumber(Maj, Min, Rev);
190    return Maj;
191  }
192
193  /// @}
194  /// @name Mutators
195  /// @{
196
197  /// setArch - Set the architecture (first) component of the triple
198  /// to a known type.
199  void setArch(ArchType Kind);
200
201  /// setVendor - Set the vendor (second) component of the triple to a
202  /// known type.
203  void setVendor(VendorType Kind);
204
205  /// setOS - Set the operating system (third) component of the triple
206  /// to a known type.
207  void setOS(OSType Kind);
208
209  /// setTriple - Set all components to the new triple \arg Str.
210  void setTriple(const Twine &Str);
211
212  /// setArchName - Set the architecture (first) component of the
213  /// triple by name.
214  void setArchName(const StringRef &Str);
215
216  /// setVendorName - Set the vendor (second) component of the triple
217  /// by name.
218  void setVendorName(const StringRef &Str);
219
220  /// setOSName - Set the operating system (third) component of the
221  /// triple by name.
222  void setOSName(const StringRef &Str);
223
224  /// setEnvironmentName - Set the optional environment (fourth)
225  /// component of the triple by name.
226  void setEnvironmentName(const StringRef &Str);
227
228  /// setOSAndEnvironmentName - Set the operating system and optional
229  /// environment components with a single string.
230  void setOSAndEnvironmentName(const StringRef &Str);
231
232  /// @}
233  /// @name Static helpers for IDs.
234  /// @{
235
236  /// getArchTypeName - Get the canonical name for the \arg Kind
237  /// architecture.
238  static const char *getArchTypeName(ArchType Kind);
239
240  /// getVendorTypeName - Get the canonical name for the \arg Kind
241  /// vendor.
242  static const char *getVendorTypeName(VendorType Kind);
243
244  /// getOSTypeName - Get the canonical name for the \arg Kind vendor.
245  static const char *getOSTypeName(OSType Kind);
246
247  /// getArchTypeForLLVMName - The canonical type for the given LLVM
248  /// architecture name (e.g., "x86").
249  static ArchType getArchTypeForLLVMName(const StringRef &Str);
250
251  /// @}
252};
253
254} // End llvm namespace
255
256
257#endif
258