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