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