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