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