Triple.h revision 199481
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///
27193323Sed/// Target triples are strings in the format of:
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
38198090Srdivacky/// string; it does not normally change or normalize the triple string, instead
39198090Srdivacky/// it provides additional APIs to parse normalized parts out of the triple.
40198090Srdivacky///
41198090Srdivacky/// One curiosity this implies is that for some odd triples the results of,
42198090Srdivacky/// e.g., getOSName() can be very different from the result of getOS().  For
43198090Srdivacky/// example, for 'i386-mingw32', getOS() will return MinGW32, but since
44198090Srdivacky/// getOSName() is purely based on the string structure that will return the
45198090Srdivacky/// empty string.
46198090Srdivacky///
47198090Srdivacky/// Clients should generally avoid using getOSName() and related APIs unless
48198090Srdivacky/// they are familiar with the triple format (this is particularly true when
49198090Srdivacky/// rewriting a triple).
50198090Srdivacky///
51198090Srdivacky/// See autoconf/config.guess for a glimpse into what they look like in
52198090Srdivacky/// practice.
53193323Sedclass Triple {
54193323Sedpublic:
55193323Sed  enum ArchType {
56193323Sed    UnknownArch,
57193323Sed
58198090Srdivacky    alpha,   // Alpha: alpha
59198090Srdivacky    arm,     // ARM; arm, armv.*, xscale
60198090Srdivacky    bfin,    // Blackfin: bfin
61198090Srdivacky    cellspu, // CellSPU: spu, cellspu
62198090Srdivacky    mips,    // MIPS: mips, mipsallegrex
63198090Srdivacky    mipsel,  // MIPSEL: mipsel, mipsallegrexel, psp
64198090Srdivacky    msp430,  // MSP430: msp430
65198090Srdivacky    pic16,   // PIC16: pic16
66198090Srdivacky    ppc,     // PPC: powerpc
67198090Srdivacky    ppc64,   // PPC64: powerpc64
68198090Srdivacky    sparc,   // Sparc: sparc
69198090Srdivacky    systemz, // SystemZ: s390x
70198090Srdivacky    tce,     // TCE (http://tce.cs.tut.fi/): tce
71198090Srdivacky    thumb,   // Thumb: thumb, thumbv.*
72198090Srdivacky    x86,     // X86: i[3-9]86
73198090Srdivacky    x86_64,  // X86-64: amd64, x86_64
74198090Srdivacky    xcore,   // XCore: xcore
75193323Sed
76193323Sed    InvalidArch
77193323Sed  };
78193323Sed  enum VendorType {
79193323Sed    UnknownVendor,
80193323Sed
81193323Sed    Apple,
82193323Sed    PC
83193323Sed  };
84193323Sed  enum OSType {
85193323Sed    UnknownOS,
86193323Sed
87194612Sed    AuroraUX,
88198090Srdivacky    Cygwin,
89193323Sed    Darwin,
90193323Sed    DragonFly,
91193323Sed    FreeBSD,
92195340Sed    Linux,
93198090Srdivacky    MinGW32,
94198090Srdivacky    MinGW64,
95198090Srdivacky    NetBSD,
96198090Srdivacky    OpenBSD,
97199481Srdivacky    Psp,
98198090Srdivacky    Solaris,
99198396Srdivacky    Win32,
100198396Srdivacky    Haiku
101193323Sed  };
102193323Sed
103193323Sedprivate:
104193323Sed  std::string Data;
105193323Sed
106193323Sed  /// The parsed arch type (or InvalidArch if uninitialized).
107193323Sed  mutable ArchType Arch;
108193323Sed
109193323Sed  /// The parsed vendor type.
110193323Sed  mutable VendorType Vendor;
111193323Sed
112193323Sed  /// The parsed OS type.
113193323Sed  mutable OSType OS;
114193323Sed
115193323Sed  bool isInitialized() const { return Arch != InvalidArch; }
116193323Sed  void Parse() const;
117193323Sed
118193323Sedpublic:
119193323Sed  /// @name Constructors
120193323Sed  /// @{
121193323Sed
122198090Srdivacky  Triple() : Data(), Arch(InvalidArch) {}
123198090Srdivacky  explicit Triple(StringRef Str) : Data(Str), Arch(InvalidArch) {}
124198090Srdivacky  explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr)
125193323Sed    : Data(ArchStr), Arch(InvalidArch) {
126193323Sed    Data += '-';
127193323Sed    Data += VendorStr;
128193323Sed    Data += '-';
129193323Sed    Data += OSStr;
130193323Sed  }
131193323Sed
132193323Sed  /// @}
133193323Sed  /// @name Typed Component Access
134193323Sed  /// @{
135193323Sed
136193323Sed  /// getArch - Get the parsed architecture type of this triple.
137193323Sed  ArchType getArch() const {
138193323Sed    if (!isInitialized()) Parse();
139193323Sed    return Arch;
140193323Sed  }
141193323Sed
142193323Sed  /// getVendor - Get the parsed vendor type of this triple.
143193323Sed  VendorType getVendor() const {
144193323Sed    if (!isInitialized()) Parse();
145193323Sed    return Vendor;
146193323Sed  }
147193323Sed
148193323Sed  /// getOS - Get the parsed operating system type of this triple.
149193323Sed  OSType getOS() const {
150193323Sed    if (!isInitialized()) Parse();
151193323Sed    return OS;
152193323Sed  }
153193323Sed
154193323Sed  /// hasEnvironment - Does this triple have the optional environment
155193323Sed  /// (fourth) component?
156193323Sed  bool hasEnvironment() const {
157193323Sed    return getEnvironmentName() != "";
158193323Sed  }
159193323Sed
160193323Sed  /// @}
161193323Sed  /// @name Direct Component Access
162193323Sed  /// @{
163193323Sed
164199481Srdivacky  const std::string &str() const { return Data; }
165199481Srdivacky
166193323Sed  const std::string &getTriple() const { return Data; }
167193323Sed
168193323Sed  /// getArchName - Get the architecture (first) component of the
169193323Sed  /// triple.
170198090Srdivacky  StringRef getArchName() const;
171193323Sed
172193323Sed  /// getVendorName - Get the vendor (second) component of the triple.
173198090Srdivacky  StringRef getVendorName() const;
174193323Sed
175193323Sed  /// getOSName - Get the operating system (third) component of the
176193323Sed  /// triple.
177198090Srdivacky  StringRef getOSName() const;
178193323Sed
179193323Sed  /// getEnvironmentName - Get the optional environment (fourth)
180193323Sed  /// component of the triple, or "" if empty.
181198090Srdivacky  StringRef getEnvironmentName() const;
182193323Sed
183193323Sed  /// getOSAndEnvironmentName - Get the operating system and optional
184193323Sed  /// environment components as a single string (separated by a '-'
185193323Sed  /// if the environment component is present).
186198090Srdivacky  StringRef getOSAndEnvironmentName() const;
187193323Sed
188198090Srdivacky
189198090Srdivacky  /// getDarwinNumber - Parse the 'darwin number' out of the specific target
190198090Srdivacky  /// triple.  For example, if we have darwin8.5 return 8,5,0.  If any entry is
191198090Srdivacky  /// not defined, return 0's.  This requires that the triple have an OSType of
192198090Srdivacky  /// darwin before it is called.
193198090Srdivacky  void getDarwinNumber(unsigned &Maj, unsigned &Min, unsigned &Revision) const;
194198090Srdivacky
195198090Srdivacky  /// getDarwinMajorNumber - Return just the major version number, this is
196198090Srdivacky  /// specialized because it is a common query.
197198090Srdivacky  unsigned getDarwinMajorNumber() const {
198198090Srdivacky    unsigned Maj, Min, Rev;
199198090Srdivacky    getDarwinNumber(Maj, Min, Rev);
200198090Srdivacky    return Maj;
201198090Srdivacky  }
202198090Srdivacky
203193323Sed  /// @}
204193323Sed  /// @name Mutators
205193323Sed  /// @{
206193323Sed
207193323Sed  /// setArch - Set the architecture (first) component of the triple
208193323Sed  /// to a known type.
209193323Sed  void setArch(ArchType Kind);
210193323Sed
211193323Sed  /// setVendor - Set the vendor (second) component of the triple to a
212193323Sed  /// known type.
213193323Sed  void setVendor(VendorType Kind);
214193323Sed
215193323Sed  /// setOS - Set the operating system (third) component of the triple
216193323Sed  /// to a known type.
217193323Sed  void setOS(OSType Kind);
218193323Sed
219193323Sed  /// setTriple - Set all components to the new triple \arg Str.
220198090Srdivacky  void setTriple(const Twine &Str);
221193323Sed
222193323Sed  /// setArchName - Set the architecture (first) component of the
223193323Sed  /// triple by name.
224199481Srdivacky  void setArchName(StringRef Str);
225193323Sed
226193323Sed  /// setVendorName - Set the vendor (second) component of the triple
227193323Sed  /// by name.
228199481Srdivacky  void setVendorName(StringRef Str);
229193323Sed
230193323Sed  /// setOSName - Set the operating system (third) component of the
231193323Sed  /// triple by name.
232199481Srdivacky  void setOSName(StringRef Str);
233193323Sed
234193323Sed  /// setEnvironmentName - Set the optional environment (fourth)
235193323Sed  /// component of the triple by name.
236199481Srdivacky  void setEnvironmentName(StringRef Str);
237193323Sed
238193323Sed  /// setOSAndEnvironmentName - Set the operating system and optional
239193323Sed  /// environment components with a single string.
240199481Srdivacky  void setOSAndEnvironmentName(StringRef Str);
241193323Sed
242199481Srdivacky  /// getArchNameForAssembler - Get an architecture name that is understood by the
243199481Srdivacky  /// target assembler.
244199481Srdivacky  const char *getArchNameForAssembler();
245199481Srdivacky
246193323Sed  /// @}
247193323Sed  /// @name Static helpers for IDs.
248193323Sed  /// @{
249193323Sed
250193323Sed  /// getArchTypeName - Get the canonical name for the \arg Kind
251193323Sed  /// architecture.
252193323Sed  static const char *getArchTypeName(ArchType Kind);
253193323Sed
254198090Srdivacky  /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
255198090Srdivacky  /// architecture. This is the prefix used by the architecture specific
256198090Srdivacky  /// builtins, and is suitable for passing to \see
257198090Srdivacky  /// Intrinsic::getIntrinsicForGCCBuiltin().
258198090Srdivacky  ///
259198090Srdivacky  /// \return - The architecture prefix, or 0 if none is defined.
260198090Srdivacky  static const char *getArchTypePrefix(ArchType Kind);
261198090Srdivacky
262193323Sed  /// getVendorTypeName - Get the canonical name for the \arg Kind
263193323Sed  /// vendor.
264193323Sed  static const char *getVendorTypeName(VendorType Kind);
265193323Sed
266193323Sed  /// getOSTypeName - Get the canonical name for the \arg Kind vendor.
267193323Sed  static const char *getOSTypeName(OSType Kind);
268193323Sed
269193323Sed  /// @}
270198090Srdivacky  /// @name Static helpers for converting alternate architecture names.
271198090Srdivacky  /// @{
272198090Srdivacky
273198090Srdivacky  /// getArchTypeForLLVMName - The canonical type for the given LLVM
274198090Srdivacky  /// architecture name (e.g., "x86").
275199481Srdivacky  static ArchType getArchTypeForLLVMName(StringRef Str);
276198090Srdivacky
277198090Srdivacky  /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
278198090Srdivacky  /// architecture name, for example as accepted by "gcc -arch" (see also
279198090Srdivacky  /// arch(3)).
280199481Srdivacky  static ArchType getArchTypeForDarwinArchName(StringRef Str);
281198090Srdivacky
282198090Srdivacky  /// @}
283193323Sed};
284193323Sed
285193323Sed} // End llvm namespace
286193323Sed
287193323Sed
288193323Sed#endif
289