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