Triple.h revision 218893
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
13198090Srdivacky#include "llvm/ADT/StringRef.h"
14193323Sed#include <string>
15193323Sed
16198090Srdivacky// Some system headers or GCC predefined macros conflict with identifiers in
17198090Srdivacky// this file.  Undefine them here.
18198090Srdivacky#undef mips
19198090Srdivacky#undef sparc
20198090Srdivacky
21193323Sednamespace llvm {
22198090Srdivackyclass StringRef;
23198090Srdivackyclass Twine;
24193323Sed
25193323Sed/// Triple - Helper class for working with target triples.
26193323Sed///
27212904Sdim/// Target triples are strings in the canonical form:
28193323Sed///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM
29193323Sed/// or
30193323Sed///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
31193323Sed///
32193323Sed/// This class is used for clients which want to support arbitrary
33193323Sed/// target triples, but also want to implement certain special
34193323Sed/// behavior for particular targets. This class isolates the mapping
35193323Sed/// from the components of the target triple to well known IDs.
36193323Sed///
37198090Srdivacky/// At its core the Triple class is designed to be a wrapper for a triple
38212904Sdim/// string; the constructor does not change or normalize the triple string.
39212904Sdim/// Clients that need to handle the non-canonical triples that users often
40212904Sdim/// specify should use the normalize method.
41198090Srdivacky///
42212904Sdim/// See autoconf/config.guess for a glimpse into what triples look like in
43198090Srdivacky/// practice.
44193323Sedclass Triple {
45193323Sedpublic:
46193323Sed  enum ArchType {
47193323Sed    UnknownArch,
48218893Sdim
49198090Srdivacky    alpha,   // Alpha: alpha
50198090Srdivacky    arm,     // ARM; arm, armv.*, xscale
51198090Srdivacky    bfin,    // Blackfin: bfin
52198090Srdivacky    cellspu, // CellSPU: spu, cellspu
53198090Srdivacky    mips,    // MIPS: mips, mipsallegrex
54198090Srdivacky    mipsel,  // MIPSEL: mipsel, mipsallegrexel, psp
55198090Srdivacky    msp430,  // MSP430: msp430
56198090Srdivacky    ppc,     // PPC: powerpc
57199989Srdivacky    ppc64,   // PPC64: powerpc64, ppu
58198090Srdivacky    sparc,   // Sparc: sparc
59203954Srdivacky    sparcv9, // Sparcv9: Sparcv9
60198090Srdivacky    systemz, // SystemZ: s390x
61198090Srdivacky    tce,     // TCE (http://tce.cs.tut.fi/): tce
62198090Srdivacky    thumb,   // Thumb: thumb, thumbv.*
63198090Srdivacky    x86,     // X86: i[3-9]86
64198090Srdivacky    x86_64,  // X86-64: amd64, x86_64
65198090Srdivacky    xcore,   // XCore: xcore
66204642Srdivacky    mblaze,  // MBlaze: mblaze
67218893Sdim    ptx,     // PTX: ptx
68193323Sed
69193323Sed    InvalidArch
70193323Sed  };
71193323Sed  enum VendorType {
72193323Sed    UnknownVendor,
73193323Sed
74218893Sdim    Apple,
75193323Sed    PC
76193323Sed  };
77193323Sed  enum OSType {
78193323Sed    UnknownOS,
79193323Sed
80194612Sed    AuroraUX,
81198090Srdivacky    Cygwin,
82193323Sed    Darwin,
83193323Sed    DragonFly,
84193323Sed    FreeBSD,
85195340Sed    Linux,
86199989Srdivacky    Lv2,        // PS3
87218893Sdim    MinGW32,    // i*86-pc-mingw32, *-w64-mingw32
88198090Srdivacky    NetBSD,
89198090Srdivacky    OpenBSD,
90199481Srdivacky    Psp,
91198090Srdivacky    Solaris,
92198396Srdivacky    Win32,
93210299Sed    Haiku,
94210299Sed    Minix
95193323Sed  };
96218893Sdim  enum EnvironmentType {
97218893Sdim    UnknownEnvironment,
98218893Sdim
99218893Sdim    GNU,
100218893Sdim    GNUEABI,
101218893Sdim    EABI,
102218893Sdim    MachO
103218893Sdim  };
104218893Sdim
105193323Sedprivate:
106193323Sed  std::string Data;
107193323Sed
108193323Sed  /// The parsed arch type (or InvalidArch if uninitialized).
109193323Sed  mutable ArchType Arch;
110193323Sed
111193323Sed  /// The parsed vendor type.
112193323Sed  mutable VendorType Vendor;
113193323Sed
114193323Sed  /// The parsed OS type.
115193323Sed  mutable OSType OS;
116193323Sed
117218893Sdim  /// The parsed Environment type.
118218893Sdim  mutable EnvironmentType Environment;
119218893Sdim
120193323Sed  bool isInitialized() const { return Arch != InvalidArch; }
121212904Sdim  static ArchType ParseArch(StringRef ArchName);
122212904Sdim  static VendorType ParseVendor(StringRef VendorName);
123212904Sdim  static OSType ParseOS(StringRef OSName);
124218893Sdim  static EnvironmentType ParseEnvironment(StringRef EnvironmentName);
125193323Sed  void Parse() const;
126193323Sed
127193323Sedpublic:
128193323Sed  /// @name Constructors
129193323Sed  /// @{
130218893Sdim
131198090Srdivacky  Triple() : Data(), Arch(InvalidArch) {}
132198090Srdivacky  explicit Triple(StringRef Str) : Data(Str), Arch(InvalidArch) {}
133198090Srdivacky  explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr)
134193323Sed    : Data(ArchStr), Arch(InvalidArch) {
135193323Sed    Data += '-';
136193323Sed    Data += VendorStr;
137193323Sed    Data += '-';
138193323Sed    Data += OSStr;
139193323Sed  }
140193323Sed
141218893Sdim  explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr,
142218893Sdim    StringRef EnvironmentStr)
143218893Sdim    : Data(ArchStr), Arch(InvalidArch) {
144218893Sdim    Data += '-';
145218893Sdim    Data += VendorStr;
146218893Sdim    Data += '-';
147218893Sdim    Data += OSStr;
148218893Sdim    Data += '-';
149218893Sdim    Data += EnvironmentStr;
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
224218893Sdim
225198090Srdivacky  /// getDarwinNumber - Parse the 'darwin number' out of the specific target
226198090Srdivacky  /// triple.  For example, if we have darwin8.5 return 8,5,0.  If any entry is
227198090Srdivacky  /// not defined, return 0's.  This requires that the triple have an OSType of
228198090Srdivacky  /// darwin before it is called.
229198090Srdivacky  void getDarwinNumber(unsigned &Maj, unsigned &Min, unsigned &Revision) const;
230218893Sdim
231198090Srdivacky  /// getDarwinMajorNumber - Return just the major version number, this is
232198090Srdivacky  /// specialized because it is a common query.
233198090Srdivacky  unsigned getDarwinMajorNumber() const {
234198090Srdivacky    unsigned Maj, Min, Rev;
235198090Srdivacky    getDarwinNumber(Maj, Min, Rev);
236198090Srdivacky    return Maj;
237198090Srdivacky  }
238218893Sdim
239193323Sed  /// @}
240193323Sed  /// @name Mutators
241193323Sed  /// @{
242193323Sed
243193323Sed  /// setArch - Set the architecture (first) component of the triple
244193323Sed  /// to a known type.
245193323Sed  void setArch(ArchType Kind);
246193323Sed
247193323Sed  /// setVendor - Set the vendor (second) component of the triple to a
248193323Sed  /// known type.
249193323Sed  void setVendor(VendorType Kind);
250193323Sed
251193323Sed  /// setOS - Set the operating system (third) component of the triple
252193323Sed  /// to a known type.
253193323Sed  void setOS(OSType Kind);
254193323Sed
255218893Sdim  /// setEnvironment - Set the environment (fourth) component of the triple
256218893Sdim  /// to a known type.
257218893Sdim  void setEnvironment(EnvironmentType Kind);
258218893Sdim
259193323Sed  /// setTriple - Set all components to the new triple \arg Str.
260198090Srdivacky  void setTriple(const Twine &Str);
261193323Sed
262193323Sed  /// setArchName - Set the architecture (first) component of the
263193323Sed  /// triple by name.
264199481Srdivacky  void setArchName(StringRef Str);
265193323Sed
266193323Sed  /// setVendorName - Set the vendor (second) component of the triple
267193323Sed  /// by name.
268199481Srdivacky  void setVendorName(StringRef Str);
269193323Sed
270193323Sed  /// setOSName - Set the operating system (third) component of the
271193323Sed  /// triple by name.
272199481Srdivacky  void setOSName(StringRef Str);
273193323Sed
274193323Sed  /// setEnvironmentName - Set the optional environment (fourth)
275193323Sed  /// component of the triple by name.
276199481Srdivacky  void setEnvironmentName(StringRef Str);
277193323Sed
278193323Sed  /// setOSAndEnvironmentName - Set the operating system and optional
279193323Sed  /// environment components with a single string.
280199481Srdivacky  void setOSAndEnvironmentName(StringRef Str);
281193323Sed
282210299Sed  /// getArchNameForAssembler - Get an architecture name that is understood by
283210299Sed  /// the target assembler.
284199481Srdivacky  const char *getArchNameForAssembler();
285199481Srdivacky
286193323Sed  /// @}
287193323Sed  /// @name Static helpers for IDs.
288193323Sed  /// @{
289193323Sed
290193323Sed  /// getArchTypeName - Get the canonical name for the \arg Kind
291193323Sed  /// architecture.
292193323Sed  static const char *getArchTypeName(ArchType Kind);
293193323Sed
294198090Srdivacky  /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
295198090Srdivacky  /// architecture. This is the prefix used by the architecture specific
296198090Srdivacky  /// builtins, and is suitable for passing to \see
297198090Srdivacky  /// Intrinsic::getIntrinsicForGCCBuiltin().
298198090Srdivacky  ///
299198090Srdivacky  /// \return - The architecture prefix, or 0 if none is defined.
300198090Srdivacky  static const char *getArchTypePrefix(ArchType Kind);
301198090Srdivacky
302193323Sed  /// getVendorTypeName - Get the canonical name for the \arg Kind
303193323Sed  /// vendor.
304193323Sed  static const char *getVendorTypeName(VendorType Kind);
305193323Sed
306218893Sdim  /// getOSTypeName - Get the canonical name for the \arg Kind operating
307218893Sdim  /// system.
308193323Sed  static const char *getOSTypeName(OSType Kind);
309193323Sed
310218893Sdim  /// getEnvironmentTypeName - Get the canonical name for the \arg Kind
311218893Sdim  /// environment.
312218893Sdim  static const char *getEnvironmentTypeName(EnvironmentType Kind);
313218893Sdim
314193323Sed  /// @}
315198090Srdivacky  /// @name Static helpers for converting alternate architecture names.
316198090Srdivacky  /// @{
317198090Srdivacky
318198090Srdivacky  /// getArchTypeForLLVMName - The canonical type for the given LLVM
319198090Srdivacky  /// architecture name (e.g., "x86").
320199481Srdivacky  static ArchType getArchTypeForLLVMName(StringRef Str);
321198090Srdivacky
322198090Srdivacky  /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
323198090Srdivacky  /// architecture name, for example as accepted by "gcc -arch" (see also
324198090Srdivacky  /// arch(3)).
325199481Srdivacky  static ArchType getArchTypeForDarwinArchName(StringRef Str);
326198090Srdivacky
327198090Srdivacky  /// @}
328193323Sed};
329193323Sed
330193323Sed} // End llvm namespace
331193323Sed
332193323Sed
333193323Sed#endif
334