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