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