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