Triple.h revision 226633
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    alpha,   // Alpha: alpha
47198090Srdivacky    arm,     // ARM; arm, armv.*, xscale
48198090Srdivacky    bfin,    // Blackfin: bfin
49198090Srdivacky    cellspu, // CellSPU: spu, cellspu
50198090Srdivacky    mips,    // MIPS: mips, mipsallegrex
51198090Srdivacky    mipsel,  // MIPSEL: mipsel, mipsallegrexel, psp
52226633Sdim    mips64,  // MIPS64: mips64
53226633Sdim    mips64el,// MIPS64EL: mips64el
54198090Srdivacky    msp430,  // MSP430: msp430
55198090Srdivacky    ppc,     // PPC: powerpc
56199989Srdivacky    ppc64,   // PPC64: powerpc64, ppu
57198090Srdivacky    sparc,   // Sparc: sparc
58203954Srdivacky    sparcv9, // Sparcv9: Sparcv9
59198090Srdivacky    systemz, // SystemZ: s390x
60198090Srdivacky    tce,     // TCE (http://tce.cs.tut.fi/): tce
61198090Srdivacky    thumb,   // Thumb: thumb, thumbv.*
62198090Srdivacky    x86,     // X86: i[3-9]86
63198090Srdivacky    x86_64,  // X86-64: amd64, x86_64
64198090Srdivacky    xcore,   // XCore: xcore
65204642Srdivacky    mblaze,  // MBlaze: mblaze
66221345Sdim    ptx32,   // PTX: ptx (32-bit)
67221345Sdim    ptx64,   // PTX: ptx (64-bit)
68226633Sdim    le32,    // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
69226633Sdim    amdil,   // amdil: amd IL
70193323Sed
71193323Sed    InvalidArch
72193323Sed  };
73193323Sed  enum VendorType {
74193323Sed    UnknownVendor,
75193323Sed
76218893Sdim    Apple,
77221345Sdim    PC,
78221345Sdim    SCEI
79193323Sed  };
80193323Sed  enum OSType {
81193323Sed    UnknownOS,
82193323Sed
83194612Sed    AuroraUX,
84198090Srdivacky    Cygwin,
85193323Sed    Darwin,
86193323Sed    DragonFly,
87193323Sed    FreeBSD,
88221345Sdim    IOS,
89226633Sdim    KFreeBSD,
90195340Sed    Linux,
91199989Srdivacky    Lv2,        // PS3
92221345Sdim    MacOSX,
93218893Sdim    MinGW32,    // i*86-pc-mingw32, *-w64-mingw32
94198090Srdivacky    NetBSD,
95198090Srdivacky    OpenBSD,
96199481Srdivacky    Psp,
97198090Srdivacky    Solaris,
98198396Srdivacky    Win32,
99210299Sed    Haiku,
100224145Sdim    Minix,
101226633Sdim    RTEMS,
102226633Sdim    NativeClient
103193323Sed  };
104218893Sdim  enum EnvironmentType {
105218893Sdim    UnknownEnvironment,
106218893Sdim
107218893Sdim    GNU,
108218893Sdim    GNUEABI,
109218893Sdim    EABI,
110218893Sdim    MachO
111218893Sdim  };
112218893Sdim
113193323Sedprivate:
114193323Sed  std::string Data;
115193323Sed
116193323Sed  /// The parsed arch type (or InvalidArch if uninitialized).
117193323Sed  mutable ArchType Arch;
118193323Sed
119193323Sed  /// The parsed vendor type.
120193323Sed  mutable VendorType Vendor;
121193323Sed
122193323Sed  /// The parsed OS type.
123193323Sed  mutable OSType OS;
124193323Sed
125218893Sdim  /// The parsed Environment type.
126218893Sdim  mutable EnvironmentType Environment;
127218893Sdim
128193323Sed  bool isInitialized() const { return Arch != InvalidArch; }
129212904Sdim  static ArchType ParseArch(StringRef ArchName);
130212904Sdim  static VendorType ParseVendor(StringRef VendorName);
131212904Sdim  static OSType ParseOS(StringRef OSName);
132218893Sdim  static EnvironmentType ParseEnvironment(StringRef EnvironmentName);
133193323Sed  void Parse() const;
134193323Sed
135193323Sedpublic:
136193323Sed  /// @name Constructors
137193323Sed  /// @{
138218893Sdim
139198090Srdivacky  Triple() : Data(), Arch(InvalidArch) {}
140226633Sdim  explicit Triple(const Twine &Str) : Data(Str.str()), Arch(InvalidArch) {}
141226633Sdim  Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
142226633Sdim    : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
143226633Sdim      Arch(InvalidArch) {
144193323Sed  }
145193323Sed
146226633Sdim  Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
147226633Sdim         const Twine &EnvironmentStr)
148226633Sdim    : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
149226633Sdim            EnvironmentStr).str()), Arch(InvalidArch) {
150218893Sdim  }
151218893Sdim
152193323Sed  /// @}
153212904Sdim  /// @name Normalization
154212904Sdim  /// @{
155212904Sdim
156212904Sdim  /// normalize - Turn an arbitrary machine specification into the canonical
157212904Sdim  /// triple form (or something sensible that the Triple class understands if
158212904Sdim  /// nothing better can reasonably be done).  In particular, it handles the
159212904Sdim  /// common case in which otherwise valid components are in the wrong order.
160212904Sdim  static std::string normalize(StringRef Str);
161212904Sdim
162212904Sdim  /// @}
163193323Sed  /// @name Typed Component Access
164193323Sed  /// @{
165218893Sdim
166193323Sed  /// getArch - Get the parsed architecture type of this triple.
167218893Sdim  ArchType getArch() const {
168218893Sdim    if (!isInitialized()) Parse();
169193323Sed    return Arch;
170193323Sed  }
171218893Sdim
172193323Sed  /// getVendor - Get the parsed vendor type of this triple.
173218893Sdim  VendorType getVendor() const {
174218893Sdim    if (!isInitialized()) Parse();
175193323Sed    return Vendor;
176193323Sed  }
177218893Sdim
178193323Sed  /// getOS - Get the parsed operating system type of this triple.
179218893Sdim  OSType getOS() const {
180218893Sdim    if (!isInitialized()) Parse();
181193323Sed    return OS;
182193323Sed  }
183193323Sed
184193323Sed  /// hasEnvironment - Does this triple have the optional environment
185193323Sed  /// (fourth) component?
186193323Sed  bool hasEnvironment() const {
187193323Sed    return getEnvironmentName() != "";
188193323Sed  }
189193323Sed
190218893Sdim  /// getEnvironment - Get the parsed environment type of this triple.
191218893Sdim  EnvironmentType getEnvironment() const {
192218893Sdim    if (!isInitialized()) Parse();
193218893Sdim    return Environment;
194218893Sdim  }
195218893Sdim
196193323Sed  /// @}
197193323Sed  /// @name Direct Component Access
198193323Sed  /// @{
199193323Sed
200199481Srdivacky  const std::string &str() const { return Data; }
201199481Srdivacky
202193323Sed  const std::string &getTriple() const { return Data; }
203193323Sed
204193323Sed  /// getArchName - Get the architecture (first) component of the
205193323Sed  /// triple.
206198090Srdivacky  StringRef getArchName() const;
207193323Sed
208193323Sed  /// getVendorName - Get the vendor (second) component of the triple.
209198090Srdivacky  StringRef getVendorName() const;
210193323Sed
211193323Sed  /// getOSName - Get the operating system (third) component of the
212193323Sed  /// triple.
213198090Srdivacky  StringRef getOSName() const;
214193323Sed
215193323Sed  /// getEnvironmentName - Get the optional environment (fourth)
216193323Sed  /// component of the triple, or "" if empty.
217198090Srdivacky  StringRef getEnvironmentName() const;
218193323Sed
219193323Sed  /// getOSAndEnvironmentName - Get the operating system and optional
220193323Sed  /// environment components as a single string (separated by a '-'
221193323Sed  /// if the environment component is present).
222198090Srdivacky  StringRef getOSAndEnvironmentName() const;
223193323Sed
224223017Sdim  /// getOSVersion - Parse the version number from the OS name component of the
225221345Sdim  /// triple, if present.
226221345Sdim  ///
227221345Sdim  /// For example, "fooos1.2.3" would return (1, 2, 3).
228221345Sdim  ///
229221345Sdim  /// If an entry is not defined, it will be returned as 0.
230221345Sdim  void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
231218893Sdim
232221345Sdim  /// getOSMajorVersion - Return just the major version number, this is
233198090Srdivacky  /// specialized because it is a common query.
234221345Sdim  unsigned getOSMajorVersion() const {
235221345Sdim    unsigned Maj, Min, Micro;
236224145Sdim    getOSVersion(Maj, Min, Micro);
237198090Srdivacky    return Maj;
238198090Srdivacky  }
239218893Sdim
240221345Sdim  /// isOSVersionLT - Helper function for doing comparisons against version
241221345Sdim  /// numbers included in the target triple.
242221345Sdim  bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
243221345Sdim                     unsigned Micro = 0) const {
244221345Sdim    unsigned LHS[3];
245221345Sdim    getOSVersion(LHS[0], LHS[1], LHS[2]);
246221345Sdim
247221345Sdim    if (LHS[0] != Major)
248221345Sdim      return LHS[0] < Major;
249221345Sdim    if (LHS[1] != Minor)
250221345Sdim      return LHS[1] < Minor;
251221345Sdim    if (LHS[2] != Micro)
252221345Sdim      return LHS[1] < Micro;
253221345Sdim
254221345Sdim    return false;
255221345Sdim  }
256221345Sdim
257221345Sdim  /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
258221345Sdim  /// "darwin" and "osx" as OS X triples.
259221345Sdim  bool isMacOSX() const {
260221345Sdim    return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
261221345Sdim  }
262221345Sdim
263221345Sdim  /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
264221345Sdim  bool isOSDarwin() const {
265224145Sdim    return isMacOSX() || getOS() == Triple::IOS;
266221345Sdim  }
267221345Sdim
268221345Sdim  /// isOSWindows - Is this a "Windows" OS.
269221345Sdim  bool isOSWindows() const {
270221345Sdim    return getOS() == Triple::Win32 || getOS() == Triple::Cygwin ||
271221345Sdim      getOS() == Triple::MinGW32;
272221345Sdim  }
273221345Sdim
274221345Sdim  /// isMacOSXVersionLT - Comparison function for checking OS X version
275221345Sdim  /// compatibility, which handles supporting skewed version numbering schemes
276221345Sdim  /// used by the "darwin" triples.
277221345Sdim  unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
278224145Sdim			     unsigned Micro = 0) const {
279221345Sdim    assert(isMacOSX() && "Not an OS X triple!");
280221345Sdim
281221345Sdim    // If this is OS X, expect a sane version number.
282221345Sdim    if (getOS() == Triple::MacOSX)
283221345Sdim      return isOSVersionLT(Major, Minor, Micro);
284221345Sdim
285221345Sdim    // Otherwise, compare to the "Darwin" number.
286221345Sdim    assert(Major == 10 && "Unexpected major version");
287221345Sdim    return isOSVersionLT(Minor + 4, Micro, 0);
288221345Sdim  }
289224145Sdim
290193323Sed  /// @}
291193323Sed  /// @name Mutators
292193323Sed  /// @{
293193323Sed
294193323Sed  /// setArch - Set the architecture (first) component of the triple
295193323Sed  /// to a known type.
296193323Sed  void setArch(ArchType Kind);
297193323Sed
298193323Sed  /// setVendor - Set the vendor (second) component of the triple to a
299193323Sed  /// known type.
300193323Sed  void setVendor(VendorType Kind);
301193323Sed
302193323Sed  /// setOS - Set the operating system (third) component of the triple
303193323Sed  /// to a known type.
304193323Sed  void setOS(OSType Kind);
305193323Sed
306218893Sdim  /// setEnvironment - Set the environment (fourth) component of the triple
307218893Sdim  /// to a known type.
308218893Sdim  void setEnvironment(EnvironmentType Kind);
309218893Sdim
310193323Sed  /// setTriple - Set all components to the new triple \arg Str.
311198090Srdivacky  void setTriple(const Twine &Str);
312193323Sed
313193323Sed  /// setArchName - Set the architecture (first) component of the
314193323Sed  /// triple by name.
315199481Srdivacky  void setArchName(StringRef Str);
316193323Sed
317193323Sed  /// setVendorName - Set the vendor (second) component of the triple
318193323Sed  /// by name.
319199481Srdivacky  void setVendorName(StringRef Str);
320193323Sed
321193323Sed  /// setOSName - Set the operating system (third) component of the
322193323Sed  /// triple by name.
323199481Srdivacky  void setOSName(StringRef Str);
324193323Sed
325193323Sed  /// setEnvironmentName - Set the optional environment (fourth)
326193323Sed  /// component of the triple by name.
327199481Srdivacky  void setEnvironmentName(StringRef Str);
328193323Sed
329193323Sed  /// setOSAndEnvironmentName - Set the operating system and optional
330193323Sed  /// environment components with a single string.
331199481Srdivacky  void setOSAndEnvironmentName(StringRef Str);
332193323Sed
333210299Sed  /// getArchNameForAssembler - Get an architecture name that is understood by
334210299Sed  /// the target assembler.
335199481Srdivacky  const char *getArchNameForAssembler();
336199481Srdivacky
337193323Sed  /// @}
338193323Sed  /// @name Static helpers for IDs.
339193323Sed  /// @{
340193323Sed
341193323Sed  /// getArchTypeName - Get the canonical name for the \arg Kind
342193323Sed  /// architecture.
343193323Sed  static const char *getArchTypeName(ArchType Kind);
344193323Sed
345198090Srdivacky  /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
346198090Srdivacky  /// architecture. This is the prefix used by the architecture specific
347198090Srdivacky  /// builtins, and is suitable for passing to \see
348198090Srdivacky  /// Intrinsic::getIntrinsicForGCCBuiltin().
349198090Srdivacky  ///
350198090Srdivacky  /// \return - The architecture prefix, or 0 if none is defined.
351198090Srdivacky  static const char *getArchTypePrefix(ArchType Kind);
352198090Srdivacky
353193323Sed  /// getVendorTypeName - Get the canonical name for the \arg Kind
354193323Sed  /// vendor.
355193323Sed  static const char *getVendorTypeName(VendorType Kind);
356193323Sed
357218893Sdim  /// getOSTypeName - Get the canonical name for the \arg Kind operating
358218893Sdim  /// system.
359193323Sed  static const char *getOSTypeName(OSType Kind);
360193323Sed
361218893Sdim  /// getEnvironmentTypeName - Get the canonical name for the \arg Kind
362218893Sdim  /// environment.
363218893Sdim  static const char *getEnvironmentTypeName(EnvironmentType Kind);
364218893Sdim
365193323Sed  /// @}
366198090Srdivacky  /// @name Static helpers for converting alternate architecture names.
367198090Srdivacky  /// @{
368198090Srdivacky
369198090Srdivacky  /// getArchTypeForLLVMName - The canonical type for the given LLVM
370198090Srdivacky  /// architecture name (e.g., "x86").
371199481Srdivacky  static ArchType getArchTypeForLLVMName(StringRef Str);
372198090Srdivacky
373198090Srdivacky  /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
374198090Srdivacky  /// architecture name, for example as accepted by "gcc -arch" (see also
375198090Srdivacky  /// arch(3)).
376199481Srdivacky  static ArchType getArchTypeForDarwinArchName(StringRef Str);
377198090Srdivacky
378198090Srdivacky  /// @}
379193323Sed};
380193323Sed
381193323Sed} // End llvm namespace
382193323Sed
383193323Sed
384193323Sed#endif
385