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