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