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