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