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