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