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