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