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