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