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