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