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