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