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