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