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