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