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