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