Revert r199791.
[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/Twine.h"
14
15 // Some system headers or GCC predefined macros conflict with identifiers in
16 // this file.  Undefine them here.
17 #undef NetBSD
18 #undef mips
19 #undef sparc
20
21 namespace llvm {
22
23 /// Triple - Helper class for working with autoconf configuration names. For
24 /// historical reasons, we also call these 'triples' (they used to contain
25 /// exactly three fields).
26 ///
27 /// Configuration names 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 /// configuration names, but also want to implement certain special
34 /// behavior for particular configurations. This class isolates the mapping
35 /// from the components of the configuration name 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 configuration names
43 /// look like in practice.
44 class Triple {
45 public:
46   enum ArchType {
47     UnknownArch,
48
49     arm,     // ARM: arm, armv.*, xscale
50     aarch64, // AArch64: aarch64
51     hexagon, // Hexagon: hexagon
52     mips,    // MIPS: mips, mipsallegrex
53     mipsel,  // MIPSEL: mipsel, mipsallegrexel
54     mips64,  // MIPS64: mips64
55     mips64el,// MIPS64EL: mips64el
56     msp430,  // MSP430: msp430
57     ppc,     // PPC: powerpc
58     ppc64,   // PPC64: powerpc64, ppu
59     ppc64le, // PPC64LE: powerpc64le
60     r600,    // R600: AMD GPUs HD2XXX - HD6XXX
61     sparc,   // Sparc: sparc
62     sparcv9, // Sparcv9: Sparcv9
63     systemz, // SystemZ: s390x
64     tce,     // TCE (http://tce.cs.tut.fi/): tce
65     thumb,   // Thumb: thumb, thumbv.*
66     x86,     // X86: i[3-9]86
67     x86_64,  // X86-64: amd64, x86_64
68     xcore,   // XCore: xcore
69     nvptx,   // NVPTX: 32-bit
70     nvptx64, // NVPTX: 64-bit
71     le32,    // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
72     amdil,   // amdil: amd IL
73     spir,    // SPIR: standard portable IR for OpenCL 32-bit version
74     spir64   // SPIR: standard portable IR for OpenCL 64-bit version
75   };
76   enum VendorType {
77     UnknownVendor,
78
79     Apple,
80     PC,
81     SCEI,
82     BGP,
83     BGQ,
84     Freescale,
85     IBM,
86     NVIDIA
87   };
88   enum OSType {
89     UnknownOS,
90
91     AuroraUX,
92     Cygwin,
93     Darwin,
94     DragonFly,
95     FreeBSD,
96     IOS,
97     KFreeBSD,
98     Linux,
99     Lv2,        // PS3
100     MacOSX,
101     MinGW32,    // i*86-pc-mingw32, *-w64-mingw32
102     NetBSD,
103     OpenBSD,
104     Solaris,
105     Win32,
106     Haiku,
107     Minix,
108     RTEMS,
109     NaCl,       // Native Client
110     CNK,        // BG/P Compute-Node Kernel
111     Bitrig,
112     AIX,
113     CUDA,       // NVIDIA CUDA
114     NVCL        // NVIDIA OpenCL
115   };
116   enum EnvironmentType {
117     UnknownEnvironment,
118
119     GNU,
120     GNUEABI,
121     GNUEABIHF,
122     GNUX32,
123     CODE16,
124     EABI,
125     EABIHF,
126     MachO,
127     Android,
128     ELF
129   };
130
131 private:
132   std::string Data;
133
134   /// The parsed arch type.
135   ArchType Arch;
136
137   /// The parsed vendor type.
138   VendorType Vendor;
139
140   /// The parsed OS type.
141   OSType OS;
142
143   /// The parsed Environment type.
144   EnvironmentType Environment;
145
146 public:
147   /// @name Constructors
148   /// @{
149
150   /// \brief Default constructor is the same as an empty string and leaves all
151   /// triple fields unknown.
152   Triple() : Data(), Arch(), Vendor(), OS(), Environment() {}
153
154   explicit Triple(const Twine &Str);
155   Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
156   Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
157          const Twine &EnvironmentStr);
158
159   /// @}
160   /// @name Normalization
161   /// @{
162
163   /// normalize - Turn an arbitrary machine specification into the canonical
164   /// triple form (or something sensible that the Triple class understands if
165   /// nothing better can reasonably be done).  In particular, it handles the
166   /// common case in which otherwise valid components are in the wrong order.
167   static std::string normalize(StringRef Str);
168
169   /// @}
170   /// @name Typed Component Access
171   /// @{
172
173   /// getArch - Get the parsed architecture type of this triple.
174   ArchType getArch() const { return Arch; }
175
176   /// getVendor - Get the parsed vendor type of this triple.
177   VendorType getVendor() const { return Vendor; }
178
179   /// getOS - Get the parsed operating system type of this triple.
180   OSType getOS() const { return OS; }
181
182   /// hasEnvironment - Does this triple have the optional environment
183   /// (fourth) component?
184   bool hasEnvironment() const {
185     return getEnvironmentName() != "";
186   }
187
188   /// getEnvironment - Get the parsed environment type of this triple.
189   EnvironmentType getEnvironment() const { return Environment; }
190
191   /// getOSVersion - Parse the version number from the OS name component of the
192   /// triple, if present.
193   ///
194   /// For example, "fooos1.2.3" would return (1, 2, 3).
195   ///
196   /// If an entry is not defined, it will be returned as 0.
197   void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
198
199   /// getOSMajorVersion - Return just the major version number, this is
200   /// specialized because it is a common query.
201   unsigned getOSMajorVersion() const {
202     unsigned Maj, Min, Micro;
203     getOSVersion(Maj, Min, Micro);
204     return Maj;
205   }
206
207   /// getMacOSXVersion - Parse the version number as with getOSVersion and then
208   /// translate generic "darwin" versions to the corresponding OS X versions.
209   /// This may also be called with IOS triples but the OS X version number is
210   /// just set to a constant 10.4.0 in that case.  Returns true if successful.
211   bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
212                         unsigned &Micro) const;
213
214   /// getiOSVersion - Parse the version number as with getOSVersion.  This should
215   /// only be called with IOS triples.
216   void getiOSVersion(unsigned &Major, unsigned &Minor,
217                      unsigned &Micro) const;
218
219   /// @}
220   /// @name Direct Component Access
221   /// @{
222
223   const std::string &str() const { return Data; }
224
225   const std::string &getTriple() const { return Data; }
226
227   /// getArchName - Get the architecture (first) component of the
228   /// triple.
229   StringRef getArchName() const;
230
231   /// getVendorName - Get the vendor (second) component of the triple.
232   StringRef getVendorName() const;
233
234   /// getOSName - Get the operating system (third) component of the
235   /// triple.
236   StringRef getOSName() const;
237
238   /// getEnvironmentName - Get the optional environment (fourth)
239   /// component of the triple, or "" if empty.
240   StringRef getEnvironmentName() const;
241
242   /// getOSAndEnvironmentName - Get the operating system and optional
243   /// environment components as a single string (separated by a '-'
244   /// if the environment component is present).
245   StringRef getOSAndEnvironmentName() const;
246
247   /// @}
248   /// @name Convenience Predicates
249   /// @{
250
251   /// \brief Test whether the architecture is 64-bit
252   ///
253   /// Note that this tests for 64-bit pointer width, and nothing else. Note
254   /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
255   /// 16-bit. The inner details of pointer width for particular architectures
256   /// is not summed up in the triple, and so only a coarse grained predicate
257   /// system is provided.
258   bool isArch64Bit() const;
259
260   /// \brief Test whether the architecture is 32-bit
261   ///
262   /// Note that this tests for 32-bit pointer width, and nothing else.
263   bool isArch32Bit() const;
264
265   /// \brief Test whether the architecture is 16-bit
266   ///
267   /// Note that this tests for 16-bit pointer width, and nothing else.
268   bool isArch16Bit() const;
269
270   /// isOSVersionLT - Helper function for doing comparisons against version
271   /// numbers included in the target triple.
272   bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
273                      unsigned Micro = 0) const {
274     unsigned LHS[3];
275     getOSVersion(LHS[0], LHS[1], LHS[2]);
276
277     if (LHS[0] != Major)
278       return LHS[0] < Major;
279     if (LHS[1] != Minor)
280       return LHS[1] < Minor;
281     if (LHS[2] != Micro)
282       return LHS[1] < Micro;
283
284     return false;
285   }
286
287   /// isMacOSXVersionLT - Comparison function for checking OS X version
288   /// compatibility, which handles supporting skewed version numbering schemes
289   /// used by the "darwin" triples.
290   unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
291                              unsigned Micro = 0) const {
292     assert(isMacOSX() && "Not an OS X triple!");
293
294     // If this is OS X, expect a sane version number.
295     if (getOS() == Triple::MacOSX)
296       return isOSVersionLT(Major, Minor, Micro);
297
298     // Otherwise, compare to the "Darwin" number.
299     assert(Major == 10 && "Unexpected major version");
300     return isOSVersionLT(Minor + 4, Micro, 0);
301   }
302
303   /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
304   /// "darwin" and "osx" as OS X triples.
305   bool isMacOSX() const {
306     return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
307   }
308
309   /// Is this an iOS triple.
310   bool isiOS() const {
311     return getOS() == Triple::IOS;
312   }
313
314   /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
315   bool isOSDarwin() const {
316     return isMacOSX() || isiOS();
317   }
318
319   /// \brief Tests for either Cygwin or MinGW OS
320   bool isOSCygMing() const {
321     return getOS() == Triple::Cygwin || getOS() == Triple::MinGW32;
322   }
323
324   /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
325   bool isOSMSVCRT() const {
326     return getOS() == Triple::Win32 || getOS() == Triple::MinGW32;
327   }
328
329   /// \brief Tests whether the OS is Windows.
330   bool isOSWindows() const {
331     return getOS() == Triple::Win32 || isOSCygMing();
332   }
333
334   /// \brief Tests whether the OS is NaCl (Native Client)
335   bool isOSNaCl() const {
336     return getOS() == Triple::NaCl;
337   }
338
339   /// \brief Tests whether the OS is Linux.
340   bool isOSLinux() const {
341     return getOS() == Triple::Linux;
342   }
343
344   /// \brief Tests whether the OS uses the ELF binary format.
345   bool isOSBinFormatELF() const {
346     return !isOSBinFormatMachO() && !isOSBinFormatCOFF();
347   }
348
349   /// \brief Tests whether the OS uses the COFF binary format.
350   bool isOSBinFormatCOFF() const {
351     return getEnvironment() != Triple::ELF &&
352            getEnvironment() != Triple::MachO && isOSWindows();
353   }
354
355   /// \brief Tests whether the environment is MachO.
356   bool isOSBinFormatMachO() const {
357     return getEnvironment() == Triple::MachO || isOSDarwin();
358   }
359
360   /// @}
361   /// @name Mutators
362   /// @{
363
364   /// setArch - Set the architecture (first) component of the triple
365   /// to a known type.
366   void setArch(ArchType Kind);
367
368   /// setVendor - Set the vendor (second) component of the triple to a
369   /// known type.
370   void setVendor(VendorType Kind);
371
372   /// setOS - Set the operating system (third) component of the triple
373   /// to a known type.
374   void setOS(OSType Kind);
375
376   /// setEnvironment - Set the environment (fourth) component of the triple
377   /// to a known type.
378   void setEnvironment(EnvironmentType Kind);
379
380   /// setTriple - Set all components to the new triple \p Str.
381   void setTriple(const Twine &Str);
382
383   /// setArchName - Set the architecture (first) component of the
384   /// triple by name.
385   void setArchName(StringRef Str);
386
387   /// setVendorName - Set the vendor (second) component of the triple
388   /// by name.
389   void setVendorName(StringRef Str);
390
391   /// setOSName - Set the operating system (third) component of the
392   /// triple by name.
393   void setOSName(StringRef Str);
394
395   /// setEnvironmentName - Set the optional environment (fourth)
396   /// component of the triple by name.
397   void setEnvironmentName(StringRef Str);
398
399   /// setOSAndEnvironmentName - Set the operating system and optional
400   /// environment components with a single string.
401   void setOSAndEnvironmentName(StringRef Str);
402
403   /// getArchNameForAssembler - Get an architecture name that is understood by
404   /// the target assembler.
405   const char *getArchNameForAssembler();
406
407   /// @}
408   /// @name Helpers to build variants of a particular triple.
409   /// @{
410
411   /// \brief Form a triple with a 32-bit variant of the current architecture.
412   ///
413   /// This can be used to move across "families" of architectures where useful.
414   ///
415   /// \returns A new triple with a 32-bit architecture or an unknown
416   ///          architecture if no such variant can be found.
417   llvm::Triple get32BitArchVariant() const;
418
419   /// \brief Form a triple with a 64-bit variant of the current architecture.
420   ///
421   /// This can be used to move across "families" of architectures where useful.
422   ///
423   /// \returns A new triple with a 64-bit architecture or an unknown
424   ///          architecture if no such variant can be found.
425   llvm::Triple get64BitArchVariant() const;
426
427   /// @}
428   /// @name Static helpers for IDs.
429   /// @{
430
431   /// getArchTypeName - Get the canonical name for the \p Kind architecture.
432   static const char *getArchTypeName(ArchType Kind);
433
434   /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
435   /// architecture. This is the prefix used by the architecture specific
436   /// builtins, and is suitable for passing to \see
437   /// Intrinsic::getIntrinsicForGCCBuiltin().
438   ///
439   /// \return - The architecture prefix, or 0 if none is defined.
440   static const char *getArchTypePrefix(ArchType Kind);
441
442   /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
443   static const char *getVendorTypeName(VendorType Kind);
444
445   /// getOSTypeName - Get the canonical name for the \p Kind operating system.
446   static const char *getOSTypeName(OSType Kind);
447
448   /// getEnvironmentTypeName - Get the canonical name for the \p Kind
449   /// environment.
450   static const char *getEnvironmentTypeName(EnvironmentType Kind);
451
452   /// @}
453   /// @name Static helpers for converting alternate architecture names.
454   /// @{
455
456   /// getArchTypeForLLVMName - The canonical type for the given LLVM
457   /// architecture name (e.g., "x86").
458   static ArchType getArchTypeForLLVMName(StringRef Str);
459
460   /// @}
461 };
462
463 } // End llvm namespace
464
465
466 #endif