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