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