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