Triple.h revision 239462
1193323Sed//===-- llvm/ADT/Triple.h - Target triple helper class ----------*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed
10193323Sed#ifndef LLVM_ADT_TRIPLE_H
11193323Sed#define LLVM_ADT_TRIPLE_H
12193323Sed
13226633Sdim#include "llvm/ADT/Twine.h"
14193323Sed
15198090Srdivacky// Some system headers or GCC predefined macros conflict with identifiers in
16198090Srdivacky// this file.  Undefine them here.
17198090Srdivacky#undef mips
18198090Srdivacky#undef sparc
19198090Srdivacky
20193323Sednamespace llvm {
21193323Sed
22193323Sed/// Triple - Helper class for working with target triples.
23193323Sed///
24212904Sdim/// Target triples are strings in the canonical form:
25193323Sed///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM
26193323Sed/// or
27193323Sed///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
28193323Sed///
29193323Sed/// This class is used for clients which want to support arbitrary
30193323Sed/// target triples, but also want to implement certain special
31193323Sed/// behavior for particular targets. This class isolates the mapping
32193323Sed/// from the components of the target triple to well known IDs.
33193323Sed///
34198090Srdivacky/// At its core the Triple class is designed to be a wrapper for a triple
35212904Sdim/// string; the constructor does not change or normalize the triple string.
36212904Sdim/// Clients that need to handle the non-canonical triples that users often
37212904Sdim/// specify should use the normalize method.
38198090Srdivacky///
39212904Sdim/// See autoconf/config.guess for a glimpse into what triples look like in
40198090Srdivacky/// practice.
41193323Sedclass Triple {
42193323Sedpublic:
43193323Sed  enum ArchType {
44193323Sed    UnknownArch,
45218893Sdim
46198090Srdivacky    arm,     // ARM; arm, armv.*, xscale
47198090Srdivacky    cellspu, // CellSPU: spu, cellspu
48234353Sdim    hexagon, // Hexagon: hexagon
49198090Srdivacky    mips,    // MIPS: mips, mipsallegrex
50234353Sdim    mipsel,  // MIPSEL: mipsel, mipsallegrexel
51226633Sdim    mips64,  // MIPS64: mips64
52226633Sdim    mips64el,// MIPS64EL: mips64el
53198090Srdivacky    msp430,  // MSP430: msp430
54198090Srdivacky    ppc,     // PPC: powerpc
55199989Srdivacky    ppc64,   // PPC64: powerpc64, ppu
56234353Sdim    r600,    // R600: AMD GPUs HD2XXX - HD6XXX
57198090Srdivacky    sparc,   // Sparc: sparc
58203954Srdivacky    sparcv9, // Sparcv9: Sparcv9
59198090Srdivacky    tce,     // TCE (http://tce.cs.tut.fi/): tce
60198090Srdivacky    thumb,   // Thumb: thumb, thumbv.*
61198090Srdivacky    x86,     // X86: i[3-9]86
62198090Srdivacky    x86_64,  // X86-64: amd64, x86_64
63198090Srdivacky    xcore,   // XCore: xcore
64204642Srdivacky    mblaze,  // MBlaze: mblaze
65239462Sdim    nvptx,   // NVPTX: 32-bit
66239462Sdim    nvptx64, // NVPTX: 64-bit
67226633Sdim    le32,    // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
68234353Sdim    amdil   // amdil: amd IL
69193323Sed  };
70193323Sed  enum VendorType {
71193323Sed    UnknownVendor,
72193323Sed
73218893Sdim    Apple,
74221345Sdim    PC,
75234353Sdim    SCEI,
76234353Sdim    BGP,
77234353Sdim    BGQ
78193323Sed  };
79193323Sed  enum OSType {
80193323Sed    UnknownOS,
81193323Sed
82194612Sed    AuroraUX,
83198090Srdivacky    Cygwin,
84193323Sed    Darwin,
85193323Sed    DragonFly,
86193323Sed    FreeBSD,
87221345Sdim    IOS,
88226633Sdim    KFreeBSD,
89195340Sed    Linux,
90199989Srdivacky    Lv2,        // PS3
91221345Sdim    MacOSX,
92218893Sdim    MinGW32,    // i*86-pc-mingw32, *-w64-mingw32
93198090Srdivacky    NetBSD,
94198090Srdivacky    OpenBSD,
95198090Srdivacky    Solaris,
96198396Srdivacky    Win32,
97210299Sed    Haiku,
98224145Sdim    Minix,
99226633Sdim    RTEMS,
100234353Sdim    NativeClient,
101239462Sdim    CNK,         // BG/P Compute-Node Kernel
102239462Sdim    Bitrig
103193323Sed  };
104218893Sdim  enum EnvironmentType {
105218893Sdim    UnknownEnvironment,
106218893Sdim
107218893Sdim    GNU,
108218893Sdim    GNUEABI,
109234353Sdim    GNUEABIHF,
110218893Sdim    EABI,
111234353Sdim    MachO,
112234353Sdim    ANDROIDEABI
113218893Sdim  };
114218893Sdim
115193323Sedprivate:
116193323Sed  std::string Data;
117193323Sed
118234353Sdim  /// The parsed arch type.
119234353Sdim  ArchType Arch;
120193323Sed
121193323Sed  /// The parsed vendor type.
122234353Sdim  VendorType Vendor;
123193323Sed
124193323Sed  /// The parsed OS type.
125234353Sdim  OSType OS;
126193323Sed
127218893Sdim  /// The parsed Environment type.
128234353Sdim  EnvironmentType Environment;
129218893Sdim
130193323Sedpublic:
131193323Sed  /// @name Constructors
132193323Sed  /// @{
133218893Sdim
134234353Sdim  /// \brief Default constructor is the same as an empty string and leaves all
135234353Sdim  /// triple fields unknown.
136234353Sdim  Triple() : Data(), Arch(), Vendor(), OS(), Environment() {}
137193323Sed
138234353Sdim  explicit Triple(const Twine &Str);
139234353Sdim  Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
140226633Sdim  Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
141234353Sdim         const Twine &EnvironmentStr);
142218893Sdim
143193323Sed  /// @}
144212904Sdim  /// @name Normalization
145212904Sdim  /// @{
146212904Sdim
147212904Sdim  /// normalize - Turn an arbitrary machine specification into the canonical
148212904Sdim  /// triple form (or something sensible that the Triple class understands if
149212904Sdim  /// nothing better can reasonably be done).  In particular, it handles the
150212904Sdim  /// common case in which otherwise valid components are in the wrong order.
151212904Sdim  static std::string normalize(StringRef Str);
152212904Sdim
153212904Sdim  /// @}
154193323Sed  /// @name Typed Component Access
155193323Sed  /// @{
156218893Sdim
157193323Sed  /// getArch - Get the parsed architecture type of this triple.
158234353Sdim  ArchType getArch() const { return Arch; }
159218893Sdim
160193323Sed  /// getVendor - Get the parsed vendor type of this triple.
161234353Sdim  VendorType getVendor() const { return Vendor; }
162218893Sdim
163193323Sed  /// getOS - Get the parsed operating system type of this triple.
164234353Sdim  OSType getOS() const { return OS; }
165193323Sed
166193323Sed  /// hasEnvironment - Does this triple have the optional environment
167193323Sed  /// (fourth) component?
168193323Sed  bool hasEnvironment() const {
169193323Sed    return getEnvironmentName() != "";
170193323Sed  }
171193323Sed
172218893Sdim  /// getEnvironment - Get the parsed environment type of this triple.
173234353Sdim  EnvironmentType getEnvironment() const { return Environment; }
174234353Sdim
175234353Sdim  /// getOSVersion - Parse the version number from the OS name component of the
176234353Sdim  /// triple, if present.
177234353Sdim  ///
178234353Sdim  /// For example, "fooos1.2.3" would return (1, 2, 3).
179234353Sdim  ///
180234353Sdim  /// If an entry is not defined, it will be returned as 0.
181234353Sdim  void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
182234353Sdim
183234353Sdim  /// getOSMajorVersion - Return just the major version number, this is
184234353Sdim  /// specialized because it is a common query.
185234353Sdim  unsigned getOSMajorVersion() const {
186234353Sdim    unsigned Maj, Min, Micro;
187234353Sdim    getOSVersion(Maj, Min, Micro);
188234353Sdim    return Maj;
189218893Sdim  }
190218893Sdim
191234353Sdim  /// getMacOSXVersion - Parse the version number as with getOSVersion and then
192234353Sdim  /// translate generic "darwin" versions to the corresponding OS X versions.
193234353Sdim  /// This may also be called with IOS triples but the OS X version number is
194234353Sdim  /// just set to a constant 10.4.0 in that case.  Returns true if successful.
195234353Sdim  bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
196234353Sdim                        unsigned &Micro) const;
197234353Sdim
198239462Sdim  /// getiOSVersion - Parse the version number as with getOSVersion.  This should
199239462Sdim  /// only be called with IOS triples.
200239462Sdim  void getiOSVersion(unsigned &Major, unsigned &Minor,
201239462Sdim                     unsigned &Micro) const;
202239462Sdim
203193323Sed  /// @}
204193323Sed  /// @name Direct Component Access
205193323Sed  /// @{
206193323Sed
207199481Srdivacky  const std::string &str() const { return Data; }
208199481Srdivacky
209193323Sed  const std::string &getTriple() const { return Data; }
210193323Sed
211193323Sed  /// getArchName - Get the architecture (first) component of the
212193323Sed  /// triple.
213198090Srdivacky  StringRef getArchName() const;
214193323Sed
215193323Sed  /// getVendorName - Get the vendor (second) component of the triple.
216198090Srdivacky  StringRef getVendorName() const;
217193323Sed
218193323Sed  /// getOSName - Get the operating system (third) component of the
219193323Sed  /// triple.
220198090Srdivacky  StringRef getOSName() const;
221193323Sed
222193323Sed  /// getEnvironmentName - Get the optional environment (fourth)
223193323Sed  /// component of the triple, or "" if empty.
224198090Srdivacky  StringRef getEnvironmentName() const;
225193323Sed
226193323Sed  /// getOSAndEnvironmentName - Get the operating system and optional
227193323Sed  /// environment components as a single string (separated by a '-'
228193323Sed  /// if the environment component is present).
229198090Srdivacky  StringRef getOSAndEnvironmentName() const;
230193323Sed
231234353Sdim  /// @}
232234353Sdim  /// @name Convenience Predicates
233234353Sdim  /// @{
234234353Sdim
235234353Sdim  /// \brief Test whether the architecture is 64-bit
236221345Sdim  ///
237234353Sdim  /// Note that this tests for 64-bit pointer width, and nothing else. Note
238234353Sdim  /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
239234353Sdim  /// 16-bit. The inner details of pointer width for particular architectures
240234353Sdim  /// is not summed up in the triple, and so only a coarse grained predicate
241234353Sdim  /// system is provided.
242234353Sdim  bool isArch64Bit() const;
243234353Sdim
244234353Sdim  /// \brief Test whether the architecture is 32-bit
245221345Sdim  ///
246234353Sdim  /// Note that this tests for 32-bit pointer width, and nothing else.
247234353Sdim  bool isArch32Bit() const;
248218893Sdim
249234353Sdim  /// \brief Test whether the architecture is 16-bit
250234353Sdim  ///
251234353Sdim  /// Note that this tests for 16-bit pointer width, and nothing else.
252234353Sdim  bool isArch16Bit() const;
253218893Sdim
254221345Sdim  /// isOSVersionLT - Helper function for doing comparisons against version
255221345Sdim  /// numbers included in the target triple.
256221345Sdim  bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
257221345Sdim                     unsigned Micro = 0) const {
258221345Sdim    unsigned LHS[3];
259221345Sdim    getOSVersion(LHS[0], LHS[1], LHS[2]);
260221345Sdim
261221345Sdim    if (LHS[0] != Major)
262221345Sdim      return LHS[0] < Major;
263221345Sdim    if (LHS[1] != Minor)
264221345Sdim      return LHS[1] < Minor;
265221345Sdim    if (LHS[2] != Micro)
266221345Sdim      return LHS[1] < Micro;
267221345Sdim
268221345Sdim    return false;
269221345Sdim  }
270221345Sdim
271234353Sdim  /// isMacOSXVersionLT - Comparison function for checking OS X version
272234353Sdim  /// compatibility, which handles supporting skewed version numbering schemes
273234353Sdim  /// used by the "darwin" triples.
274234353Sdim  unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
275239462Sdim                             unsigned Micro = 0) const {
276234353Sdim    assert(isMacOSX() && "Not an OS X triple!");
277234353Sdim
278234353Sdim    // If this is OS X, expect a sane version number.
279234353Sdim    if (getOS() == Triple::MacOSX)
280234353Sdim      return isOSVersionLT(Major, Minor, Micro);
281234353Sdim
282234353Sdim    // Otherwise, compare to the "Darwin" number.
283234353Sdim    assert(Major == 10 && "Unexpected major version");
284234353Sdim    return isOSVersionLT(Minor + 4, Micro, 0);
285234353Sdim  }
286234353Sdim
287221345Sdim  /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
288221345Sdim  /// "darwin" and "osx" as OS X triples.
289221345Sdim  bool isMacOSX() const {
290221345Sdim    return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
291221345Sdim  }
292221345Sdim
293221345Sdim  /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
294221345Sdim  bool isOSDarwin() const {
295224145Sdim    return isMacOSX() || getOS() == Triple::IOS;
296221345Sdim  }
297221345Sdim
298234353Sdim  /// \brief Tests for either Cygwin or MinGW OS
299234353Sdim  bool isOSCygMing() const {
300234353Sdim    return getOS() == Triple::Cygwin || getOS() == Triple::MinGW32;
301234353Sdim  }
302234353Sdim
303221345Sdim  /// isOSWindows - Is this a "Windows" OS.
304221345Sdim  bool isOSWindows() const {
305234353Sdim    return getOS() == Triple::Win32 || isOSCygMing();
306221345Sdim  }
307221345Sdim
308234353Sdim  /// \brief Tests whether the OS uses the ELF binary format.
309234353Sdim  bool isOSBinFormatELF() const {
310234353Sdim    return !isOSDarwin() && !isOSWindows();
311234353Sdim  }
312221345Sdim
313234353Sdim  /// \brief Tests whether the OS uses the COFF binary format.
314234353Sdim  bool isOSBinFormatCOFF() const {
315234353Sdim    return isOSWindows();
316234353Sdim  }
317221345Sdim
318234353Sdim  /// \brief Tests whether the environment is MachO.
319234353Sdim  // FIXME: Should this be an OSBinFormat predicate?
320234353Sdim  bool isEnvironmentMachO() const {
321234353Sdim    return getEnvironment() == Triple::MachO || isOSDarwin();
322221345Sdim  }
323224145Sdim
324193323Sed  /// @}
325193323Sed  /// @name Mutators
326193323Sed  /// @{
327193323Sed
328193323Sed  /// setArch - Set the architecture (first) component of the triple
329193323Sed  /// to a known type.
330193323Sed  void setArch(ArchType Kind);
331193323Sed
332193323Sed  /// setVendor - Set the vendor (second) component of the triple to a
333193323Sed  /// known type.
334193323Sed  void setVendor(VendorType Kind);
335193323Sed
336193323Sed  /// setOS - Set the operating system (third) component of the triple
337193323Sed  /// to a known type.
338193323Sed  void setOS(OSType Kind);
339193323Sed
340218893Sdim  /// setEnvironment - Set the environment (fourth) component of the triple
341218893Sdim  /// to a known type.
342218893Sdim  void setEnvironment(EnvironmentType Kind);
343218893Sdim
344193323Sed  /// setTriple - Set all components to the new triple \arg Str.
345198090Srdivacky  void setTriple(const Twine &Str);
346193323Sed
347193323Sed  /// setArchName - Set the architecture (first) component of the
348193323Sed  /// triple by name.
349199481Srdivacky  void setArchName(StringRef Str);
350193323Sed
351193323Sed  /// setVendorName - Set the vendor (second) component of the triple
352193323Sed  /// by name.
353199481Srdivacky  void setVendorName(StringRef Str);
354193323Sed
355193323Sed  /// setOSName - Set the operating system (third) component of the
356193323Sed  /// triple by name.
357199481Srdivacky  void setOSName(StringRef Str);
358193323Sed
359193323Sed  /// setEnvironmentName - Set the optional environment (fourth)
360193323Sed  /// component of the triple by name.
361199481Srdivacky  void setEnvironmentName(StringRef Str);
362193323Sed
363193323Sed  /// setOSAndEnvironmentName - Set the operating system and optional
364193323Sed  /// environment components with a single string.
365199481Srdivacky  void setOSAndEnvironmentName(StringRef Str);
366193323Sed
367210299Sed  /// getArchNameForAssembler - Get an architecture name that is understood by
368210299Sed  /// the target assembler.
369199481Srdivacky  const char *getArchNameForAssembler();
370199481Srdivacky
371193323Sed  /// @}
372234353Sdim  /// @name Helpers to build variants of a particular triple.
373234353Sdim  /// @{
374234353Sdim
375234353Sdim  /// \brief Form a triple with a 32-bit variant of the current architecture.
376234353Sdim  ///
377234353Sdim  /// This can be used to move across "families" of architectures where useful.
378234353Sdim  ///
379234353Sdim  /// \returns A new triple with a 32-bit architecture or an unknown
380234353Sdim  ///          architecture if no such variant can be found.
381234353Sdim  llvm::Triple get32BitArchVariant() const;
382234353Sdim
383234353Sdim  /// \brief Form a triple with a 64-bit variant of the current architecture.
384234353Sdim  ///
385234353Sdim  /// This can be used to move across "families" of architectures where useful.
386234353Sdim  ///
387234353Sdim  /// \returns A new triple with a 64-bit architecture or an unknown
388234353Sdim  ///          architecture if no such variant can be found.
389234353Sdim  llvm::Triple get64BitArchVariant() const;
390234353Sdim
391234353Sdim  /// @}
392193323Sed  /// @name Static helpers for IDs.
393193323Sed  /// @{
394193323Sed
395193323Sed  /// getArchTypeName - Get the canonical name for the \arg Kind
396193323Sed  /// architecture.
397193323Sed  static const char *getArchTypeName(ArchType Kind);
398193323Sed
399198090Srdivacky  /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
400198090Srdivacky  /// architecture. This is the prefix used by the architecture specific
401198090Srdivacky  /// builtins, and is suitable for passing to \see
402198090Srdivacky  /// Intrinsic::getIntrinsicForGCCBuiltin().
403198090Srdivacky  ///
404198090Srdivacky  /// \return - The architecture prefix, or 0 if none is defined.
405198090Srdivacky  static const char *getArchTypePrefix(ArchType Kind);
406198090Srdivacky
407193323Sed  /// getVendorTypeName - Get the canonical name for the \arg Kind
408193323Sed  /// vendor.
409193323Sed  static const char *getVendorTypeName(VendorType Kind);
410193323Sed
411218893Sdim  /// getOSTypeName - Get the canonical name for the \arg Kind operating
412218893Sdim  /// system.
413193323Sed  static const char *getOSTypeName(OSType Kind);
414193323Sed
415218893Sdim  /// getEnvironmentTypeName - Get the canonical name for the \arg Kind
416218893Sdim  /// environment.
417218893Sdim  static const char *getEnvironmentTypeName(EnvironmentType Kind);
418218893Sdim
419193323Sed  /// @}
420198090Srdivacky  /// @name Static helpers for converting alternate architecture names.
421198090Srdivacky  /// @{
422198090Srdivacky
423198090Srdivacky  /// getArchTypeForLLVMName - The canonical type for the given LLVM
424198090Srdivacky  /// architecture name (e.g., "x86").
425199481Srdivacky  static ArchType getArchTypeForLLVMName(StringRef Str);
426198090Srdivacky
427198090Srdivacky  /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
428198090Srdivacky  /// architecture name, for example as accepted by "gcc -arch" (see also
429198090Srdivacky  /// arch(3)).
430199481Srdivacky  static ArchType getArchTypeForDarwinArchName(StringRef Str);
431198090Srdivacky
432198090Srdivacky  /// @}
433193323Sed};
434193323Sed
435193323Sed} // End llvm namespace
436193323Sed
437193323Sed
438193323Sed#endif
439