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