Reapply part of r237975, "Fix Clang -Wmissing-override warning", except for DIContext...
[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     aarch64,    // AArch64 (little endian): aarch64
52     aarch64_be, // AArch64 (big endian): aarch64_be
53     bpf,        // eBPF or extended BPF or 64-bit BPF (little endian)
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     amdgcn,     // AMDGCN: AMD GCN GPUs
65     sparc,      // Sparc: sparc
66     sparcv9,    // Sparcv9: Sparcv9
67     sparcel,    // Sparc: (endianness = little). NB: 'Sparcle' is a CPU variant
68     systemz,    // SystemZ: s390x
69     tce,        // TCE (http://tce.cs.tut.fi/): tce
70     thumb,      // Thumb (little endian): thumb, thumbv.*
71     thumbeb,    // Thumb (big endian): thumbeb
72     x86,        // X86: i[3-9]86
73     x86_64,     // X86-64: amd64, x86_64
74     xcore,      // XCore: xcore
75     nvptx,      // NVPTX: 32-bit
76     nvptx64,    // NVPTX: 64-bit
77     le32,       // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
78     le64,       // le64: generic little-endian 64-bit CPU (PNaCl / Emscripten)
79     amdil,      // AMDIL
80     amdil64,    // AMDIL with 64-bit pointers
81     hsail,      // AMD HSAIL
82     hsail64,    // AMD HSAIL with 64-bit pointers
83     spir,       // SPIR: standard portable IR for OpenCL 32-bit version
84     spir64,     // SPIR: standard portable IR for OpenCL 64-bit version
85     kalimba,    // Kalimba: generic kalimba
86     LastArchType = kalimba
87   };
88   enum SubArchType {
89     NoSubArch,
90
91     ARMSubArch_v8_1a,
92     ARMSubArch_v8,
93     ARMSubArch_v7,
94     ARMSubArch_v7em,
95     ARMSubArch_v7m,
96     ARMSubArch_v7s,
97     ARMSubArch_v6,
98     ARMSubArch_v6m,
99     ARMSubArch_v6k,
100     ARMSubArch_v6t2,
101     ARMSubArch_v5,
102     ARMSubArch_v5te,
103     ARMSubArch_v4t,
104
105     KalimbaSubArch_v3,
106     KalimbaSubArch_v4,
107     KalimbaSubArch_v5
108   };
109   enum VendorType {
110     UnknownVendor,
111
112     Apple,
113     PC,
114     SCEI,
115     BGP,
116     BGQ,
117     Freescale,
118     IBM,
119     ImaginationTechnologies,
120     MipsTechnologies,
121     NVIDIA,
122     CSR,
123     LastVendorType = CSR
124   };
125   enum OSType {
126     UnknownOS,
127
128     CloudABI,
129     Darwin,
130     DragonFly,
131     FreeBSD,
132     IOS,
133     KFreeBSD,
134     Linux,
135     Lv2,        // PS3
136     MacOSX,
137     NetBSD,
138     OpenBSD,
139     Solaris,
140     Win32,
141     Haiku,
142     Minix,
143     RTEMS,
144     NaCl,       // Native Client
145     CNK,        // BG/P Compute-Node Kernel
146     Bitrig,
147     AIX,
148     CUDA,       // NVIDIA CUDA
149     NVCL,       // NVIDIA OpenCL
150     AMDHSA,     // AMD HSA Runtime
151     PS4,
152     LastOSType = PS4
153   };
154   enum EnvironmentType {
155     UnknownEnvironment,
156
157     GNU,
158     GNUEABI,
159     GNUEABIHF,
160     GNUX32,
161     CODE16,
162     EABI,
163     EABIHF,
164     Android,
165
166     MSVC,
167     Itanium,
168     Cygnus,
169     LastEnvironmentType = Cygnus
170   };
171   enum ObjectFormatType {
172     UnknownObjectFormat,
173
174     COFF,
175     ELF,
176     MachO,
177   };
178
179 private:
180   std::string Data;
181
182   /// The parsed arch type.
183   ArchType Arch;
184
185   /// The parsed subarchitecture type.
186   SubArchType SubArch;
187
188   /// The parsed vendor type.
189   VendorType Vendor;
190
191   /// The parsed OS type.
192   OSType OS;
193
194   /// The parsed Environment type.
195   EnvironmentType Environment;
196
197   /// The object format type.
198   ObjectFormatType ObjectFormat;
199
200 public:
201   /// @name Constructors
202   /// @{
203
204   /// \brief Default constructor is the same as an empty string and leaves all
205   /// triple fields unknown.
206   Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
207
208   explicit Triple(const Twine &Str);
209   Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
210   Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
211          const Twine &EnvironmentStr);
212
213   bool operator==(const Triple &Other) const {
214     return Arch == Other.Arch && SubArch == Other.SubArch &&
215            Vendor == Other.Vendor && OS == Other.OS &&
216            Environment == Other.Environment &&
217            ObjectFormat == Other.ObjectFormat;
218   }
219
220   /// @}
221   /// @name Normalization
222   /// @{
223
224   /// normalize - Turn an arbitrary machine specification into the canonical
225   /// triple form (or something sensible that the Triple class understands if
226   /// nothing better can reasonably be done).  In particular, it handles the
227   /// common case in which otherwise valid components are in the wrong order.
228   static std::string normalize(StringRef Str);
229
230   /// \brief Return the normalized form of this triple's string.
231   std::string normalize() const { return normalize(Data); }
232
233   /// @}
234   /// @name Typed Component Access
235   /// @{
236
237   /// getArch - Get the parsed architecture type of this triple.
238   ArchType getArch() const { return Arch; }
239
240   /// getSubArch - get the parsed subarchitecture type for this triple.
241   SubArchType getSubArch() const { return SubArch; }
242
243   /// getVendor - Get the parsed vendor type of this triple.
244   VendorType getVendor() const { return Vendor; }
245
246   /// getOS - Get the parsed operating system type of this triple.
247   OSType getOS() const { return OS; }
248
249   /// hasEnvironment - Does this triple have the optional environment
250   /// (fourth) component?
251   bool hasEnvironment() const {
252     return getEnvironmentName() != "";
253   }
254
255   /// getEnvironment - Get the parsed environment type of this triple.
256   EnvironmentType getEnvironment() const { return Environment; }
257
258   /// getFormat - Get the object format for this triple.
259   ObjectFormatType getObjectFormat() const { return ObjectFormat; }
260
261   /// getOSVersion - Parse the version number from the OS name component of the
262   /// triple, if present.
263   ///
264   /// For example, "fooos1.2.3" would return (1, 2, 3).
265   ///
266   /// If an entry is not defined, it will be returned as 0.
267   void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
268
269   /// getOSMajorVersion - Return just the major version number, this is
270   /// specialized because it is a common query.
271   unsigned getOSMajorVersion() const {
272     unsigned Maj, Min, Micro;
273     getOSVersion(Maj, Min, Micro);
274     return Maj;
275   }
276
277   /// getMacOSXVersion - Parse the version number as with getOSVersion and then
278   /// translate generic "darwin" versions to the corresponding OS X versions.
279   /// This may also be called with IOS triples but the OS X version number is
280   /// just set to a constant 10.4.0 in that case.  Returns true if successful.
281   bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
282                         unsigned &Micro) const;
283
284   /// getiOSVersion - Parse the version number as with getOSVersion.  This should
285   /// only be called with IOS triples.
286   void getiOSVersion(unsigned &Major, unsigned &Minor,
287                      unsigned &Micro) const;
288
289   /// @}
290   /// @name Direct Component Access
291   /// @{
292
293   const std::string &str() const { return Data; }
294
295   const std::string &getTriple() const { return Data; }
296
297   /// getArchName - Get the architecture (first) component of the
298   /// triple.
299   StringRef getArchName() const;
300
301   /// getVendorName - Get the vendor (second) component of the triple.
302   StringRef getVendorName() const;
303
304   /// getOSName - Get the operating system (third) component of the
305   /// triple.
306   StringRef getOSName() const;
307
308   /// getEnvironmentName - Get the optional environment (fourth)
309   /// component of the triple, or "" if empty.
310   StringRef getEnvironmentName() const;
311
312   /// getOSAndEnvironmentName - Get the operating system and optional
313   /// environment components as a single string (separated by a '-'
314   /// if the environment component is present).
315   StringRef getOSAndEnvironmentName() const;
316
317   /// @}
318   /// @name Convenience Predicates
319   /// @{
320
321   /// \brief Test whether the architecture is 64-bit
322   ///
323   /// Note that this tests for 64-bit pointer width, and nothing else. Note
324   /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
325   /// 16-bit. The inner details of pointer width for particular architectures
326   /// is not summed up in the triple, and so only a coarse grained predicate
327   /// system is provided.
328   bool isArch64Bit() const;
329
330   /// \brief Test whether the architecture is 32-bit
331   ///
332   /// Note that this tests for 32-bit pointer width, and nothing else.
333   bool isArch32Bit() const;
334
335   /// \brief Test whether the architecture is 16-bit
336   ///
337   /// Note that this tests for 16-bit pointer width, and nothing else.
338   bool isArch16Bit() const;
339
340   /// isOSVersionLT - Helper function for doing comparisons against version
341   /// numbers included in the target triple.
342   bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
343                      unsigned Micro = 0) const {
344     unsigned LHS[3];
345     getOSVersion(LHS[0], LHS[1], LHS[2]);
346
347     if (LHS[0] != Major)
348       return LHS[0] < Major;
349     if (LHS[1] != Minor)
350       return LHS[1] < Minor;
351     if (LHS[2] != Micro)
352       return LHS[1] < Micro;
353
354     return false;
355   }
356
357   bool isOSVersionLT(const Triple &Other) const {
358     unsigned RHS[3];
359     Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
360     return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
361   }
362
363   /// isMacOSXVersionLT - Comparison function for checking OS X version
364   /// compatibility, which handles supporting skewed version numbering schemes
365   /// used by the "darwin" triples.
366   unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
367                              unsigned Micro = 0) const {
368     assert(isMacOSX() && "Not an OS X triple!");
369
370     // If this is OS X, expect a sane version number.
371     if (getOS() == Triple::MacOSX)
372       return isOSVersionLT(Major, Minor, Micro);
373
374     // Otherwise, compare to the "Darwin" number.
375     assert(Major == 10 && "Unexpected major version");
376     return isOSVersionLT(Minor + 4, Micro, 0);
377   }
378
379   /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
380   /// "darwin" and "osx" as OS X triples.
381   bool isMacOSX() const {
382     return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
383   }
384
385   /// Is this an iOS triple.
386   bool isiOS() const {
387     return getOS() == Triple::IOS;
388   }
389
390   /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
391   bool isOSDarwin() const {
392     return isMacOSX() || isiOS();
393   }
394
395   bool isOSNetBSD() const {
396     return getOS() == Triple::NetBSD;
397   }
398
399   bool isOSOpenBSD() const {
400     return getOS() == Triple::OpenBSD;
401   }
402
403   bool isOSFreeBSD() const {
404     return getOS() == Triple::FreeBSD;
405   }
406
407   bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
408
409   bool isOSSolaris() const {
410     return getOS() == Triple::Solaris;
411   }
412
413   bool isOSBitrig() const {
414     return getOS() == Triple::Bitrig;
415   }
416
417   bool isWindowsMSVCEnvironment() const {
418     return getOS() == Triple::Win32 &&
419            (getEnvironment() == Triple::UnknownEnvironment ||
420             getEnvironment() == Triple::MSVC);
421   }
422
423   bool isKnownWindowsMSVCEnvironment() const {
424     return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
425   }
426
427   bool isWindowsItaniumEnvironment() const {
428     return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
429   }
430
431   bool isWindowsCygwinEnvironment() const {
432     return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
433   }
434
435   bool isWindowsGNUEnvironment() const {
436     return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
437   }
438
439   /// \brief Tests for either Cygwin or MinGW OS
440   bool isOSCygMing() const {
441     return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
442   }
443
444   /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
445   bool isOSMSVCRT() const {
446     return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
447            isWindowsItaniumEnvironment();
448   }
449
450   /// \brief Tests whether the OS is Windows.
451   bool isOSWindows() const {
452     return getOS() == Triple::Win32;
453   }
454
455   /// \brief Tests whether the OS is NaCl (Native Client)
456   bool isOSNaCl() const {
457     return getOS() == Triple::NaCl;
458   }
459
460   /// \brief Tests whether the OS is Linux.
461   bool isOSLinux() const {
462     return getOS() == Triple::Linux;
463   }
464
465   /// \brief Tests whether the OS uses the ELF binary format.
466   bool isOSBinFormatELF() const {
467     return getObjectFormat() == Triple::ELF;
468   }
469
470   /// \brief Tests whether the OS uses the COFF binary format.
471   bool isOSBinFormatCOFF() const {
472     return getObjectFormat() == Triple::COFF;
473   }
474
475   /// \brief Tests whether the environment is MachO.
476   bool isOSBinFormatMachO() const {
477     return getObjectFormat() == Triple::MachO;
478   }
479
480   /// \brief Tests whether the target is the PS4 CPU
481   bool isPS4CPU() const {
482     return getArch() == Triple::x86_64 &&
483            getVendor() == Triple::SCEI &&
484            getOS() == Triple::PS4;
485   }
486
487   /// \brief Tests whether the target is the PS4 platform
488   bool isPS4() const {
489     return getVendor() == Triple::SCEI &&
490            getOS() == Triple::PS4;
491   }
492
493   /// @}
494   /// @name Mutators
495   /// @{
496
497   /// setArch - Set the architecture (first) component of the triple
498   /// to a known type.
499   void setArch(ArchType Kind);
500
501   /// setVendor - Set the vendor (second) component of the triple to a
502   /// known type.
503   void setVendor(VendorType Kind);
504
505   /// setOS - Set the operating system (third) component of the triple
506   /// to a known type.
507   void setOS(OSType Kind);
508
509   /// setEnvironment - Set the environment (fourth) component of the triple
510   /// to a known type.
511   void setEnvironment(EnvironmentType Kind);
512
513   /// setObjectFormat - Set the object file format
514   void setObjectFormat(ObjectFormatType Kind);
515
516   /// setTriple - Set all components to the new triple \p Str.
517   void setTriple(const Twine &Str);
518
519   /// setArchName - Set the architecture (first) component of the
520   /// triple by name.
521   void setArchName(StringRef Str);
522
523   /// setVendorName - Set the vendor (second) component of the triple
524   /// by name.
525   void setVendorName(StringRef Str);
526
527   /// setOSName - Set the operating system (third) component of the
528   /// triple by name.
529   void setOSName(StringRef Str);
530
531   /// setEnvironmentName - Set the optional environment (fourth)
532   /// component of the triple by name.
533   void setEnvironmentName(StringRef Str);
534
535   /// setOSAndEnvironmentName - Set the operating system and optional
536   /// environment components with a single string.
537   void setOSAndEnvironmentName(StringRef Str);
538
539   /// @}
540   /// @name Helpers to build variants of a particular triple.
541   /// @{
542
543   /// \brief Form a triple with a 32-bit variant of the current architecture.
544   ///
545   /// This can be used to move across "families" of architectures where useful.
546   ///
547   /// \returns A new triple with a 32-bit architecture or an unknown
548   ///          architecture if no such variant can be found.
549   llvm::Triple get32BitArchVariant() const;
550
551   /// \brief Form a triple with a 64-bit variant of the current architecture.
552   ///
553   /// This can be used to move across "families" of architectures where useful.
554   ///
555   /// \returns A new triple with a 64-bit architecture or an unknown
556   ///          architecture if no such variant can be found.
557   llvm::Triple get64BitArchVariant() const;
558
559   /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
560   ///
561   /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
562   /// string then the triple's arch name is used.
563   const char* getARMCPUForArch(StringRef Arch = StringRef()) const;
564
565   /// @}
566   /// @name Static helpers for IDs.
567   /// @{
568
569   /// getArchTypeName - Get the canonical name for the \p Kind architecture.
570   static const char *getArchTypeName(ArchType Kind);
571
572   /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
573   /// architecture. This is the prefix used by the architecture specific
574   /// builtins, and is suitable for passing to \see
575   /// Intrinsic::getIntrinsicForGCCBuiltin().
576   ///
577   /// \return - The architecture prefix, or 0 if none is defined.
578   static const char *getArchTypePrefix(ArchType Kind);
579
580   /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
581   static const char *getVendorTypeName(VendorType Kind);
582
583   /// getOSTypeName - Get the canonical name for the \p Kind operating system.
584   static const char *getOSTypeName(OSType Kind);
585
586   /// getEnvironmentTypeName - Get the canonical name for the \p Kind
587   /// environment.
588   static const char *getEnvironmentTypeName(EnvironmentType Kind);
589
590   /// @}
591   /// @name Static helpers for converting alternate architecture names.
592   /// @{
593
594   /// getArchTypeForLLVMName - The canonical type for the given LLVM
595   /// architecture name (e.g., "x86").
596   static ArchType getArchTypeForLLVMName(StringRef Str);
597
598   /// @}
599 };
600
601 } // End llvm namespace
602
603
604 #endif