Create PTX backend. Patch by Che-Liang Chiou!
[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     pic16,   // PIC16: pic16
57     ppc,     // PPC: powerpc
58     ppc64,   // PPC64: powerpc64, ppu
59     sparc,   // Sparc: sparc
60     sparcv9, // Sparcv9: Sparcv9
61     systemz, // SystemZ: s390x
62     tce,     // TCE (http://tce.cs.tut.fi/): tce
63     thumb,   // Thumb: thumb, thumbv.*
64     x86,     // X86: i[3-9]86
65     x86_64,  // X86-64: amd64, x86_64
66     xcore,   // XCore: xcore
67     mblaze,  // MBlaze: mblaze
68     ptx,     // PTX: ptx
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     Lv2,        // PS3
88     MinGW32,
89     MinGW64,
90     NetBSD,
91     OpenBSD,
92     Psp,
93     Solaris,
94     Win32,
95     Haiku,
96     Minix
97   };
98   
99 private:
100   std::string Data;
101
102   /// The parsed arch type (or InvalidArch if uninitialized).
103   mutable ArchType Arch;
104
105   /// The parsed vendor type.
106   mutable VendorType Vendor;
107
108   /// The parsed OS type.
109   mutable OSType OS;
110
111   bool isInitialized() const { return Arch != InvalidArch; }
112   static ArchType ParseArch(StringRef ArchName);
113   static VendorType ParseVendor(StringRef VendorName);
114   static OSType ParseOS(StringRef OSName);
115   void Parse() const;
116
117 public:
118   /// @name Constructors
119   /// @{
120   
121   Triple() : Data(), Arch(InvalidArch) {}
122   explicit Triple(StringRef Str) : Data(Str), Arch(InvalidArch) {}
123   explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr)
124     : Data(ArchStr), Arch(InvalidArch) {
125     Data += '-';
126     Data += VendorStr;
127     Data += '-';
128     Data += OSStr;
129   }
130
131   /// @}
132   /// @name Normalization
133   /// @{
134
135   /// normalize - Turn an arbitrary machine specification into the canonical
136   /// triple form (or something sensible that the Triple class understands if
137   /// nothing better can reasonably be done).  In particular, it handles the
138   /// common case in which otherwise valid components are in the wrong order.
139   static std::string normalize(StringRef Str);
140
141   /// @}
142   /// @name Typed Component Access
143   /// @{
144   
145   /// getArch - Get the parsed architecture type of this triple.
146   ArchType getArch() const { 
147     if (!isInitialized()) Parse(); 
148     return Arch;
149   }
150   
151   /// getVendor - Get the parsed vendor type of this triple.
152   VendorType getVendor() const { 
153     if (!isInitialized()) Parse(); 
154     return Vendor;
155   }
156   
157   /// getOS - Get the parsed operating system type of this triple.
158   OSType getOS() const { 
159     if (!isInitialized()) Parse(); 
160     return OS;
161   }
162
163   /// hasEnvironment - Does this triple have the optional environment
164   /// (fourth) component?
165   bool hasEnvironment() const {
166     return getEnvironmentName() != "";
167   }
168
169   /// @}
170   /// @name Direct Component Access
171   /// @{
172
173   const std::string &str() const { return Data; }
174
175   const std::string &getTriple() const { return Data; }
176
177   /// getArchName - Get the architecture (first) component of the
178   /// triple.
179   StringRef getArchName() const;
180
181   /// getVendorName - Get the vendor (second) component of the triple.
182   StringRef getVendorName() const;
183
184   /// getOSName - Get the operating system (third) component of the
185   /// triple.
186   StringRef getOSName() const;
187
188   /// getEnvironmentName - Get the optional environment (fourth)
189   /// component of the triple, or "" if empty.
190   StringRef getEnvironmentName() const;
191
192   /// getOSAndEnvironmentName - Get the operating system and optional
193   /// environment components as a single string (separated by a '-'
194   /// if the environment component is present).
195   StringRef getOSAndEnvironmentName() const;
196
197   
198   /// getDarwinNumber - Parse the 'darwin number' out of the specific target
199   /// triple.  For example, if we have darwin8.5 return 8,5,0.  If any entry is
200   /// not defined, return 0's.  This requires that the triple have an OSType of
201   /// darwin before it is called.
202   void getDarwinNumber(unsigned &Maj, unsigned &Min, unsigned &Revision) const;
203   
204   /// getDarwinMajorNumber - Return just the major version number, this is
205   /// specialized because it is a common query.
206   unsigned getDarwinMajorNumber() const {
207     unsigned Maj, Min, Rev;
208     getDarwinNumber(Maj, Min, Rev);
209     return Maj;
210   }
211   
212   /// @}
213   /// @name Mutators
214   /// @{
215
216   /// setArch - Set the architecture (first) component of the triple
217   /// to a known type.
218   void setArch(ArchType Kind);
219
220   /// setVendor - Set the vendor (second) component of the triple to a
221   /// known type.
222   void setVendor(VendorType Kind);
223
224   /// setOS - Set the operating system (third) component of the triple
225   /// to a known type.
226   void setOS(OSType Kind);
227
228   /// setTriple - Set all components to the new triple \arg Str.
229   void setTriple(const Twine &Str);
230
231   /// setArchName - Set the architecture (first) component of the
232   /// triple by name.
233   void setArchName(StringRef Str);
234
235   /// setVendorName - Set the vendor (second) component of the triple
236   /// by name.
237   void setVendorName(StringRef Str);
238
239   /// setOSName - Set the operating system (third) component of the
240   /// triple by name.
241   void setOSName(StringRef Str);
242
243   /// setEnvironmentName - Set the optional environment (fourth)
244   /// component of the triple by name.
245   void setEnvironmentName(StringRef Str);
246
247   /// setOSAndEnvironmentName - Set the operating system and optional
248   /// environment components with a single string.
249   void setOSAndEnvironmentName(StringRef Str);
250
251   /// getArchNameForAssembler - Get an architecture name that is understood by
252   /// the target assembler.
253   const char *getArchNameForAssembler();
254
255   /// @}
256   /// @name Static helpers for IDs.
257   /// @{
258
259   /// getArchTypeName - Get the canonical name for the \arg Kind
260   /// architecture.
261   static const char *getArchTypeName(ArchType Kind);
262
263   /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
264   /// architecture. This is the prefix used by the architecture specific
265   /// builtins, and is suitable for passing to \see
266   /// Intrinsic::getIntrinsicForGCCBuiltin().
267   ///
268   /// \return - The architecture prefix, or 0 if none is defined.
269   static const char *getArchTypePrefix(ArchType Kind);
270
271   /// getVendorTypeName - Get the canonical name for the \arg Kind
272   /// vendor.
273   static const char *getVendorTypeName(VendorType Kind);
274
275   /// getOSTypeName - Get the canonical name for the \arg Kind vendor.
276   static const char *getOSTypeName(OSType Kind);
277
278   /// @}
279   /// @name Static helpers for converting alternate architecture names.
280   /// @{
281
282   /// getArchTypeForLLVMName - The canonical type for the given LLVM
283   /// architecture name (e.g., "x86").
284   static ArchType getArchTypeForLLVMName(StringRef Str);
285
286   /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
287   /// architecture name, for example as accepted by "gcc -arch" (see also
288   /// arch(3)).
289   static ArchType getArchTypeForDarwinArchName(StringRef Str);
290
291   /// @}
292 };
293
294 } // End llvm namespace
295
296
297 #endif