e50cec1f5e8035a10b4624fcb610bd3457d83ec6
[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   /// 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   /// 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   /// 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 or generic triples.
307   void getiOSVersion(unsigned &Major, unsigned &Minor,
308                      unsigned &Micro) const;
309
310   /// getWatchOSVersion - Parse the version number as with getOSVersion.  This
311   /// should only be called with WatchOS or generic triples.
312   void getWatchOSVersion(unsigned &Major, unsigned &Minor,
313                          unsigned &Micro) const;
314
315   /// @}
316   /// @name Direct Component Access
317   /// @{
318
319   const std::string &str() const { return Data; }
320
321   const std::string &getTriple() const { return Data; }
322
323   /// getArchName - Get the architecture (first) component of the
324   /// triple.
325   StringRef getArchName() const;
326
327   /// getVendorName - Get the vendor (second) component of the triple.
328   StringRef getVendorName() const;
329
330   /// getOSName - Get the operating system (third) component of the
331   /// triple.
332   StringRef getOSName() const;
333
334   /// getEnvironmentName - Get the optional environment (fourth)
335   /// component of the triple, or "" if empty.
336   StringRef getEnvironmentName() const;
337
338   /// getOSAndEnvironmentName - Get the operating system and optional
339   /// environment components as a single string (separated by a '-'
340   /// if the environment component is present).
341   StringRef getOSAndEnvironmentName() const;
342
343   /// @}
344   /// @name Convenience Predicates
345   /// @{
346
347   /// Test whether the architecture is 64-bit
348   ///
349   /// Note that this tests for 64-bit pointer width, and nothing else. Note
350   /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
351   /// 16-bit. The inner details of pointer width for particular architectures
352   /// is not summed up in the triple, and so only a coarse grained predicate
353   /// system is provided.
354   bool isArch64Bit() const;
355
356   /// Test whether the architecture is 32-bit
357   ///
358   /// Note that this tests for 32-bit pointer width, and nothing else.
359   bool isArch32Bit() const;
360
361   /// Test whether the architecture is 16-bit
362   ///
363   /// Note that this tests for 16-bit pointer width, and nothing else.
364   bool isArch16Bit() const;
365
366   /// isOSVersionLT - Helper function for doing comparisons against version
367   /// numbers included in the target triple.
368   bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
369                      unsigned Micro = 0) const {
370     unsigned LHS[3];
371     getOSVersion(LHS[0], LHS[1], LHS[2]);
372
373     if (LHS[0] != Major)
374       return LHS[0] < Major;
375     if (LHS[1] != Minor)
376       return LHS[1] < Minor;
377     if (LHS[2] != Micro)
378       return LHS[1] < Micro;
379
380     return false;
381   }
382
383   bool isOSVersionLT(const Triple &Other) const {
384     unsigned RHS[3];
385     Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
386     return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
387   }
388
389   /// isMacOSXVersionLT - Comparison function for checking OS X version
390   /// compatibility, which handles supporting skewed version numbering schemes
391   /// used by the "darwin" triples.
392   unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
393                              unsigned Micro = 0) const {
394     assert(isMacOSX() && "Not an OS X triple!");
395
396     // If this is OS X, expect a sane version number.
397     if (getOS() == Triple::MacOSX)
398       return isOSVersionLT(Major, Minor, Micro);
399
400     // Otherwise, compare to the "Darwin" number.
401     assert(Major == 10 && "Unexpected major version");
402     return isOSVersionLT(Minor + 4, Micro, 0);
403   }
404
405   /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
406   /// "darwin" and "osx" as OS X triples.
407   bool isMacOSX() const {
408     return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
409   }
410
411   /// Is this an iOS triple.
412   /// Note: This identifies tvOS as a variant of iOS. If that ever
413   /// changes, i.e., if the two operating systems diverge or their version
414   /// numbers get out of sync, that will need to be changed.
415   /// watchOS has completely different version numbers so it is not included.
416   bool isiOS() const {
417     return getOS() == Triple::IOS || isTvOS();
418   }
419
420   /// Is this an Apple tvOS triple.
421   bool isTvOS() const {
422     return getOS() == Triple::TvOS;
423   }
424
425   /// Is this an Apple watchOS triple.
426   bool isWatchOS() const {
427     return getOS() == Triple::WatchOS;
428   }
429
430   /// isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
431   bool isOSDarwin() const {
432     return isMacOSX() || isiOS() || isWatchOS();
433   }
434
435   bool isOSNetBSD() const {
436     return getOS() == Triple::NetBSD;
437   }
438
439   bool isOSOpenBSD() const {
440     return getOS() == Triple::OpenBSD;
441   }
442
443   bool isOSFreeBSD() const {
444     return getOS() == Triple::FreeBSD;
445   }
446
447   bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
448
449   bool isOSSolaris() const {
450     return getOS() == Triple::Solaris;
451   }
452
453   bool isOSBitrig() const {
454     return getOS() == Triple::Bitrig;
455   }
456
457   bool isOSIAMCU() const {
458     return getOS() == Triple::ELFIAMCU;
459   }
460
461   /// Checks if the environment could be MSVC.
462   bool isWindowsMSVCEnvironment() const {
463     return getOS() == Triple::Win32 &&
464            (getEnvironment() == Triple::UnknownEnvironment ||
465             getEnvironment() == Triple::MSVC);
466   }
467
468   /// Checks if the environment is MSVC.
469   bool isKnownWindowsMSVCEnvironment() const {
470     return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
471   }
472
473   bool isWindowsCoreCLREnvironment() const {
474     return getOS() == Triple::Win32 && getEnvironment() == Triple::CoreCLR;
475   }
476
477   bool isWindowsItaniumEnvironment() const {
478     return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
479   }
480
481   bool isWindowsCygwinEnvironment() const {
482     return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
483   }
484
485   bool isWindowsGNUEnvironment() const {
486     return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
487   }
488
489   /// Tests for either Cygwin or MinGW OS
490   bool isOSCygMing() const {
491     return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
492   }
493
494   /// Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
495   bool isOSMSVCRT() const {
496     return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
497            isWindowsItaniumEnvironment();
498   }
499
500   /// Tests whether the OS is Windows.
501   bool isOSWindows() const {
502     return getOS() == Triple::Win32;
503   }
504
505   /// Tests whether the OS is NaCl (Native Client)
506   bool isOSNaCl() const {
507     return getOS() == Triple::NaCl;
508   }
509
510   /// Tests whether the OS is Linux.
511   bool isOSLinux() const {
512     return getOS() == Triple::Linux;
513   }
514
515   /// Tests whether the OS uses the ELF binary format.
516   bool isOSBinFormatELF() const {
517     return getObjectFormat() == Triple::ELF;
518   }
519
520   /// Tests whether the OS uses the COFF binary format.
521   bool isOSBinFormatCOFF() const {
522     return getObjectFormat() == Triple::COFF;
523   }
524
525   /// Tests whether the environment is MachO.
526   bool isOSBinFormatMachO() const {
527     return getObjectFormat() == Triple::MachO;
528   }
529
530   /// Tests whether the target is the PS4 CPU
531   bool isPS4CPU() const {
532     return getArch() == Triple::x86_64 &&
533            getVendor() == Triple::SCEI &&
534            getOS() == Triple::PS4;
535   }
536
537   /// Tests whether the target is the PS4 platform
538   bool isPS4() const {
539     return getVendor() == Triple::SCEI &&
540            getOS() == Triple::PS4;
541   }
542
543   /// Tests whether the target is Android
544   bool isAndroid() const { return getEnvironment() == Triple::Android; }
545
546   /// @}
547   /// @name Mutators
548   /// @{
549
550   /// setArch - Set the architecture (first) component of the triple
551   /// to a known type.
552   void setArch(ArchType Kind);
553
554   /// setVendor - Set the vendor (second) component of the triple to a
555   /// known type.
556   void setVendor(VendorType Kind);
557
558   /// setOS - Set the operating system (third) component of the triple
559   /// to a known type.
560   void setOS(OSType Kind);
561
562   /// setEnvironment - Set the environment (fourth) component of the triple
563   /// to a known type.
564   void setEnvironment(EnvironmentType Kind);
565
566   /// setObjectFormat - Set the object file format
567   void setObjectFormat(ObjectFormatType Kind);
568
569   /// setTriple - Set all components to the new triple \p Str.
570   void setTriple(const Twine &Str);
571
572   /// setArchName - Set the architecture (first) component of the
573   /// triple by name.
574   void setArchName(StringRef Str);
575
576   /// setVendorName - Set the vendor (second) component of the triple
577   /// by name.
578   void setVendorName(StringRef Str);
579
580   /// setOSName - Set the operating system (third) component of the
581   /// triple by name.
582   void setOSName(StringRef Str);
583
584   /// setEnvironmentName - Set the optional environment (fourth)
585   /// component of the triple by name.
586   void setEnvironmentName(StringRef Str);
587
588   /// setOSAndEnvironmentName - Set the operating system and optional
589   /// environment components with a single string.
590   void setOSAndEnvironmentName(StringRef Str);
591
592   /// @}
593   /// @name Helpers to build variants of a particular triple.
594   /// @{
595
596   /// Form a triple with a 32-bit variant of the current architecture.
597   ///
598   /// This can be used to move across "families" of architectures where useful.
599   ///
600   /// \returns A new triple with a 32-bit architecture or an unknown
601   ///          architecture if no such variant can be found.
602   llvm::Triple get32BitArchVariant() const;
603
604   /// Form a triple with a 64-bit variant of the current architecture.
605   ///
606   /// This can be used to move across "families" of architectures where useful.
607   ///
608   /// \returns A new triple with a 64-bit architecture or an unknown
609   ///          architecture if no such variant can be found.
610   llvm::Triple get64BitArchVariant() const;
611
612   /// Form a triple with a big endian variant of the current architecture.
613   ///
614   /// This can be used to move across "families" of architectures where useful.
615   ///
616   /// \returns A new triple with a big endian architecture or an unknown
617   ///          architecture if no such variant can be found.
618   llvm::Triple getBigEndianArchVariant() const;
619
620   /// Form a triple with a little endian variant of the current architecture.
621   ///
622   /// This can be used to move across "families" of architectures where useful.
623   ///
624   /// \returns A new triple with a little endian architecture or an unknown
625   ///          architecture if no such variant can be found.
626   llvm::Triple getLittleEndianArchVariant() const;
627
628   /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
629   ///
630   /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
631   /// string then the triple's arch name is used.
632   StringRef getARMCPUForArch(StringRef Arch = StringRef()) const;
633
634   /// @}
635   /// @name Static helpers for IDs.
636   /// @{
637
638   /// getArchTypeName - Get the canonical name for the \p Kind architecture.
639   static const char *getArchTypeName(ArchType Kind);
640
641   /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
642   /// architecture. This is the prefix used by the architecture specific
643   /// builtins, and is suitable for passing to \see
644   /// Intrinsic::getIntrinsicForGCCBuiltin().
645   ///
646   /// \return - The architecture prefix, or 0 if none is defined.
647   static const char *getArchTypePrefix(ArchType Kind);
648
649   /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
650   static const char *getVendorTypeName(VendorType Kind);
651
652   /// getOSTypeName - Get the canonical name for the \p Kind operating system.
653   static const char *getOSTypeName(OSType Kind);
654
655   /// getEnvironmentTypeName - Get the canonical name for the \p Kind
656   /// environment.
657   static const char *getEnvironmentTypeName(EnvironmentType Kind);
658
659   /// @}
660   /// @name Static helpers for converting alternate architecture names.
661   /// @{
662
663   /// getArchTypeForLLVMName - The canonical type for the given LLVM
664   /// architecture name (e.g., "x86").
665   static ArchType getArchTypeForLLVMName(StringRef Str);
666
667   /// @}
668 };
669
670 } // End llvm namespace
671
672
673 #endif