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