Convert a std::vector to a SmallVector for another 5.4% speedup on domtree.
[oota-llvm.git] / include / llvm / ADT / Triple.h
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
21 namespace llvm {
22 class StringRef;
23 class Twine;
24
25 /// Triple - Helper class for working with target triples.
26 ///
27 /// Target triples are strings in the canonical form:
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; the constructor does not change or normalize the triple string.
39 /// Clients that need to handle the non-canonical triples that users often
40 /// specify should use the normalize method.
41 ///
42 /// See autoconf/config.guess for a glimpse into what triples look like in
43 /// practice.
44 class Triple {
45 public:
46   enum ArchType {
47     UnknownArch,
48
49     alpha,   // Alpha: alpha
50     arm,     // ARM; arm, armv.*, xscale
51     bfin,    // Blackfin: bfin
52     cellspu, // CellSPU: spu, cellspu
53     mips,    // MIPS: mips, mipsallegrex
54     mipsel,  // MIPSEL: mipsel, mipsallegrexel, psp
55     msp430,  // MSP430: msp430
56     ppc,     // PPC: powerpc
57     ppc64,   // PPC64: powerpc64, ppu
58     sparc,   // Sparc: sparc
59     sparcv9, // Sparcv9: Sparcv9
60     systemz, // SystemZ: s390x
61     tce,     // TCE (http://tce.cs.tut.fi/): tce
62     thumb,   // Thumb: thumb, thumbv.*
63     x86,     // X86: i[3-9]86
64     x86_64,  // X86-64: amd64, x86_64
65     xcore,   // XCore: xcore
66     mblaze,  // MBlaze: mblaze
67     ptx,     // PTX: ptx
68
69     InvalidArch
70   };
71   enum VendorType {
72     UnknownVendor,
73
74     Apple,
75     PC,
76     NoVendor
77   };
78   enum OSType {
79     UnknownOS,
80
81     AuroraUX,
82     Cygwin,
83     Darwin,
84     DragonFly,
85     FreeBSD,
86     Linux,
87     Lv2,        // PS3
88     MinGW32,
89     MinGW64,
90     NetBSD,
91     OpenBSD,
92     Psp,
93     Solaris,
94     Win32,
95     Haiku,
96     Minix,
97     NoOS
98   };
99   enum EnvironmentType {
100     UnknownEnvironment,
101
102     GNU,
103     GNUEABI,
104     EABI
105   };
106
107 private:
108   std::string Data;
109
110   /// The parsed arch type (or InvalidArch if uninitialized).
111   mutable ArchType Arch;
112
113   /// The parsed vendor type.
114   mutable VendorType Vendor;
115
116   /// The parsed OS type.
117   mutable OSType OS;
118
119   /// The parsed Environment type.
120   mutable EnvironmentType Environment;
121
122   bool isInitialized() const { return Arch != InvalidArch; }
123   static ArchType ParseArch(StringRef ArchName);
124   static VendorType ParseVendor(StringRef VendorName);
125   static OSType ParseOS(StringRef OSName);
126   static EnvironmentType ParseEnvironment(StringRef EnvironmentName);
127   void Parse() const;
128
129 public:
130   /// @name Constructors
131   /// @{
132
133   Triple() : Data(), Arch(InvalidArch) {}
134   explicit Triple(StringRef Str) : Data(Str), Arch(InvalidArch) {}
135   explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr)
136     : Data(ArchStr), Arch(InvalidArch) {
137     Data += '-';
138     Data += VendorStr;
139     Data += '-';
140     Data += OSStr;
141   }
142
143   explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr,
144     StringRef EnvironmentStr)
145     : Data(ArchStr), Arch(InvalidArch) {
146     Data += '-';
147     Data += VendorStr;
148     Data += '-';
149     Data += OSStr;
150     Data += '-';
151     Data += EnvironmentStr;
152   }
153
154   /// @}
155   /// @name Normalization
156   /// @{
157
158   /// normalize - Turn an arbitrary machine specification into the canonical
159   /// triple form (or something sensible that the Triple class understands if
160   /// nothing better can reasonably be done).  In particular, it handles the
161   /// common case in which otherwise valid components are in the wrong order.
162   static std::string normalize(StringRef Str);
163
164   /// @}
165   /// @name Typed Component Access
166   /// @{
167
168   /// getArch - Get the parsed architecture type of this triple.
169   ArchType getArch() const {
170     if (!isInitialized()) Parse();
171     return Arch;
172   }
173
174   /// getVendor - Get the parsed vendor type of this triple.
175   VendorType getVendor() const {
176     if (!isInitialized()) Parse();
177     return Vendor;
178   }
179
180   /// getOS - Get the parsed operating system type of this triple.
181   OSType getOS() const {
182     if (!isInitialized()) Parse();
183     return OS;
184   }
185
186   /// hasEnvironment - Does this triple have the optional environment
187   /// (fourth) component?
188   bool hasEnvironment() const {
189     return getEnvironmentName() != "";
190   }
191
192   /// getEnvironment - Get the parsed environment type of this triple.
193   EnvironmentType getEnvironment() const {
194     if (!isInitialized()) Parse();
195     return Environment;
196   }
197
198   /// @}
199   /// @name Direct Component Access
200   /// @{
201
202   const std::string &str() const { return Data; }
203
204   const std::string &getTriple() const { return Data; }
205
206   /// getArchName - Get the architecture (first) component of the
207   /// triple.
208   StringRef getArchName() const;
209
210   /// getVendorName - Get the vendor (second) component of the triple.
211   StringRef getVendorName() const;
212
213   /// getOSName - Get the operating system (third) component of the
214   /// triple.
215   StringRef getOSName() const;
216
217   /// getEnvironmentName - Get the optional environment (fourth)
218   /// component of the triple, or "" if empty.
219   StringRef getEnvironmentName() const;
220
221   /// getOSAndEnvironmentName - Get the operating system and optional
222   /// environment components as a single string (separated by a '-'
223   /// if the environment component is present).
224   StringRef getOSAndEnvironmentName() const;
225
226
227   /// getDarwinNumber - Parse the 'darwin number' out of the specific target
228   /// triple.  For example, if we have darwin8.5 return 8,5,0.  If any entry is
229   /// not defined, return 0's.  This requires that the triple have an OSType of
230   /// darwin before it is called.
231   void getDarwinNumber(unsigned &Maj, unsigned &Min, unsigned &Revision) const;
232
233   /// getDarwinMajorNumber - Return just the major version number, this is
234   /// specialized because it is a common query.
235   unsigned getDarwinMajorNumber() const {
236     unsigned Maj, Min, Rev;
237     getDarwinNumber(Maj, Min, Rev);
238     return Maj;
239   }
240
241   /// @}
242   /// @name Mutators
243   /// @{
244
245   /// setArch - Set the architecture (first) component of the triple
246   /// to a known type.
247   void setArch(ArchType Kind);
248
249   /// setVendor - Set the vendor (second) component of the triple to a
250   /// known type.
251   void setVendor(VendorType Kind);
252
253   /// setOS - Set the operating system (third) component of the triple
254   /// to a known type.
255   void setOS(OSType Kind);
256
257   /// setEnvironment - Set the environment (fourth) component of the triple
258   /// to a known type.
259   void setEnvironment(EnvironmentType Kind);
260
261   /// setTriple - Set all components to the new triple \arg Str.
262   void setTriple(const Twine &Str);
263
264   /// setArchName - Set the architecture (first) component of the
265   /// triple by name.
266   void setArchName(StringRef Str);
267
268   /// setVendorName - Set the vendor (second) component of the triple
269   /// by name.
270   void setVendorName(StringRef Str);
271
272   /// setOSName - Set the operating system (third) component of the
273   /// triple by name.
274   void setOSName(StringRef Str);
275
276   /// setEnvironmentName - Set the optional environment (fourth)
277   /// component of the triple by name.
278   void setEnvironmentName(StringRef Str);
279
280   /// setOSAndEnvironmentName - Set the operating system and optional
281   /// environment components with a single string.
282   void setOSAndEnvironmentName(StringRef Str);
283
284   /// getArchNameForAssembler - Get an architecture name that is understood by
285   /// the target assembler.
286   const char *getArchNameForAssembler();
287
288   /// @}
289   /// @name Static helpers for IDs.
290   /// @{
291
292   /// getArchTypeName - Get the canonical name for the \arg Kind
293   /// architecture.
294   static const char *getArchTypeName(ArchType Kind);
295
296   /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
297   /// architecture. This is the prefix used by the architecture specific
298   /// builtins, and is suitable for passing to \see
299   /// Intrinsic::getIntrinsicForGCCBuiltin().
300   ///
301   /// \return - The architecture prefix, or 0 if none is defined.
302   static const char *getArchTypePrefix(ArchType Kind);
303
304   /// getVendorTypeName - Get the canonical name for the \arg Kind
305   /// vendor.
306   static const char *getVendorTypeName(VendorType Kind);
307
308   /// getOSTypeName - Get the canonical name for the \arg Kind operating
309   /// system.
310   static const char *getOSTypeName(OSType Kind);
311
312   /// getEnvironmentTypeName - Get the canonical name for the \arg Kind
313   /// environment.
314   static const char *getEnvironmentTypeName(EnvironmentType Kind);
315
316   /// @}
317   /// @name Static helpers for converting alternate architecture names.
318   /// @{
319
320   /// getArchTypeForLLVMName - The canonical type for the given LLVM
321   /// architecture name (e.g., "x86").
322   static ArchType getArchTypeForLLVMName(StringRef Str);
323
324   /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
325   /// architecture name, for example as accepted by "gcc -arch" (see also
326   /// arch(3)).
327   static ArchType getArchTypeForDarwinArchName(StringRef Str);
328
329   /// @}
330 };
331
332 } // End llvm namespace
333
334
335 #endif