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