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