Triple.h revision 203954
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
67199989Srdivacky    ppc64,   // PPC64: powerpc64, ppu
68198090Srdivacky    sparc,   // Sparc: sparc
69203954Srdivacky    sparcv9, // Sparcv9: Sparcv9
70198090Srdivacky    systemz, // SystemZ: s390x
71198090Srdivacky    tce,     // TCE (http://tce.cs.tut.fi/): tce
72198090Srdivacky    thumb,   // Thumb: thumb, thumbv.*
73198090Srdivacky    x86,     // X86: i[3-9]86
74198090Srdivacky    x86_64,  // X86-64: amd64, x86_64
75198090Srdivacky    xcore,   // XCore: xcore
76193323Sed
77193323Sed    InvalidArch
78193323Sed  };
79193323Sed  enum VendorType {
80193323Sed    UnknownVendor,
81193323Sed
82193323Sed    Apple,
83193323Sed    PC
84193323Sed  };
85193323Sed  enum OSType {
86193323Sed    UnknownOS,
87193323Sed
88194612Sed    AuroraUX,
89198090Srdivacky    Cygwin,
90193323Sed    Darwin,
91193323Sed    DragonFly,
92193323Sed    FreeBSD,
93195340Sed    Linux,
94199989Srdivacky    Lv2,        // PS3
95198090Srdivacky    MinGW32,
96198090Srdivacky    MinGW64,
97198090Srdivacky    NetBSD,
98198090Srdivacky    OpenBSD,
99199481Srdivacky    Psp,
100198090Srdivacky    Solaris,
101198396Srdivacky    Win32,
102198396Srdivacky    Haiku
103193323Sed  };
104193323Sed
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
117193323Sed  bool isInitialized() const { return Arch != InvalidArch; }
118193323Sed  void Parse() const;
119193323Sed
120193323Sedpublic:
121193323Sed  /// @name Constructors
122193323Sed  /// @{
123193323Sed
124198090Srdivacky  Triple() : Data(), Arch(InvalidArch) {}
125198090Srdivacky  explicit Triple(StringRef Str) : Data(Str), Arch(InvalidArch) {}
126198090Srdivacky  explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr)
127193323Sed    : Data(ArchStr), Arch(InvalidArch) {
128193323Sed    Data += '-';
129193323Sed    Data += VendorStr;
130193323Sed    Data += '-';
131193323Sed    Data += OSStr;
132193323Sed  }
133193323Sed
134193323Sed  /// @}
135193323Sed  /// @name Typed Component Access
136193323Sed  /// @{
137193323Sed
138193323Sed  /// getArch - Get the parsed architecture type of this triple.
139193323Sed  ArchType getArch() const {
140193323Sed    if (!isInitialized()) Parse();
141193323Sed    return Arch;
142193323Sed  }
143193323Sed
144193323Sed  /// getVendor - Get the parsed vendor type of this triple.
145193323Sed  VendorType getVendor() const {
146193323Sed    if (!isInitialized()) Parse();
147193323Sed    return Vendor;
148193323Sed  }
149193323Sed
150193323Sed  /// getOS - Get the parsed operating system type of this triple.
151193323Sed  OSType getOS() const {
152193323Sed    if (!isInitialized()) Parse();
153193323Sed    return OS;
154193323Sed  }
155193323Sed
156193323Sed  /// hasEnvironment - Does this triple have the optional environment
157193323Sed  /// (fourth) component?
158193323Sed  bool hasEnvironment() const {
159193323Sed    return getEnvironmentName() != "";
160193323Sed  }
161193323Sed
162193323Sed  /// @}
163193323Sed  /// @name Direct Component Access
164193323Sed  /// @{
165193323Sed
166199481Srdivacky  const std::string &str() const { return Data; }
167199481Srdivacky
168193323Sed  const std::string &getTriple() const { return Data; }
169193323Sed
170193323Sed  /// getArchName - Get the architecture (first) component of the
171193323Sed  /// triple.
172198090Srdivacky  StringRef getArchName() const;
173193323Sed
174193323Sed  /// getVendorName - Get the vendor (second) component of the triple.
175198090Srdivacky  StringRef getVendorName() const;
176193323Sed
177193323Sed  /// getOSName - Get the operating system (third) component of the
178193323Sed  /// triple.
179198090Srdivacky  StringRef getOSName() const;
180193323Sed
181193323Sed  /// getEnvironmentName - Get the optional environment (fourth)
182193323Sed  /// component of the triple, or "" if empty.
183198090Srdivacky  StringRef getEnvironmentName() const;
184193323Sed
185193323Sed  /// getOSAndEnvironmentName - Get the operating system and optional
186193323Sed  /// environment components as a single string (separated by a '-'
187193323Sed  /// if the environment component is present).
188198090Srdivacky  StringRef getOSAndEnvironmentName() const;
189193323Sed
190198090Srdivacky
191198090Srdivacky  /// getDarwinNumber - Parse the 'darwin number' out of the specific target
192198090Srdivacky  /// triple.  For example, if we have darwin8.5 return 8,5,0.  If any entry is
193198090Srdivacky  /// not defined, return 0's.  This requires that the triple have an OSType of
194198090Srdivacky  /// darwin before it is called.
195198090Srdivacky  void getDarwinNumber(unsigned &Maj, unsigned &Min, unsigned &Revision) const;
196198090Srdivacky
197198090Srdivacky  /// getDarwinMajorNumber - Return just the major version number, this is
198198090Srdivacky  /// specialized because it is a common query.
199198090Srdivacky  unsigned getDarwinMajorNumber() const {
200198090Srdivacky    unsigned Maj, Min, Rev;
201198090Srdivacky    getDarwinNumber(Maj, Min, Rev);
202198090Srdivacky    return Maj;
203198090Srdivacky  }
204198090Srdivacky
205193323Sed  /// @}
206193323Sed  /// @name Mutators
207193323Sed  /// @{
208193323Sed
209193323Sed  /// setArch - Set the architecture (first) component of the triple
210193323Sed  /// to a known type.
211193323Sed  void setArch(ArchType Kind);
212193323Sed
213193323Sed  /// setVendor - Set the vendor (second) component of the triple to a
214193323Sed  /// known type.
215193323Sed  void setVendor(VendorType Kind);
216193323Sed
217193323Sed  /// setOS - Set the operating system (third) component of the triple
218193323Sed  /// to a known type.
219193323Sed  void setOS(OSType Kind);
220193323Sed
221193323Sed  /// setTriple - Set all components to the new triple \arg Str.
222198090Srdivacky  void setTriple(const Twine &Str);
223193323Sed
224193323Sed  /// setArchName - Set the architecture (first) component of the
225193323Sed  /// triple by name.
226199481Srdivacky  void setArchName(StringRef Str);
227193323Sed
228193323Sed  /// setVendorName - Set the vendor (second) component of the triple
229193323Sed  /// by name.
230199481Srdivacky  void setVendorName(StringRef Str);
231193323Sed
232193323Sed  /// setOSName - Set the operating system (third) component of the
233193323Sed  /// triple by name.
234199481Srdivacky  void setOSName(StringRef Str);
235193323Sed
236193323Sed  /// setEnvironmentName - Set the optional environment (fourth)
237193323Sed  /// component of the triple by name.
238199481Srdivacky  void setEnvironmentName(StringRef Str);
239193323Sed
240193323Sed  /// setOSAndEnvironmentName - Set the operating system and optional
241193323Sed  /// environment components with a single string.
242199481Srdivacky  void setOSAndEnvironmentName(StringRef Str);
243193323Sed
244199481Srdivacky  /// getArchNameForAssembler - Get an architecture name that is understood by the
245199481Srdivacky  /// target assembler.
246199481Srdivacky  const char *getArchNameForAssembler();
247199481Srdivacky
248193323Sed  /// @}
249193323Sed  /// @name Static helpers for IDs.
250193323Sed  /// @{
251193323Sed
252193323Sed  /// getArchTypeName - Get the canonical name for the \arg Kind
253193323Sed  /// architecture.
254193323Sed  static const char *getArchTypeName(ArchType Kind);
255193323Sed
256198090Srdivacky  /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
257198090Srdivacky  /// architecture. This is the prefix used by the architecture specific
258198090Srdivacky  /// builtins, and is suitable for passing to \see
259198090Srdivacky  /// Intrinsic::getIntrinsicForGCCBuiltin().
260198090Srdivacky  ///
261198090Srdivacky  /// \return - The architecture prefix, or 0 if none is defined.
262198090Srdivacky  static const char *getArchTypePrefix(ArchType Kind);
263198090Srdivacky
264193323Sed  /// getVendorTypeName - Get the canonical name for the \arg Kind
265193323Sed  /// vendor.
266193323Sed  static const char *getVendorTypeName(VendorType Kind);
267193323Sed
268193323Sed  /// getOSTypeName - Get the canonical name for the \arg Kind vendor.
269193323Sed  static const char *getOSTypeName(OSType Kind);
270193323Sed
271193323Sed  /// @}
272198090Srdivacky  /// @name Static helpers for converting alternate architecture names.
273198090Srdivacky  /// @{
274198090Srdivacky
275198090Srdivacky  /// getArchTypeForLLVMName - The canonical type for the given LLVM
276198090Srdivacky  /// architecture name (e.g., "x86").
277199481Srdivacky  static ArchType getArchTypeForLLVMName(StringRef Str);
278198090Srdivacky
279198090Srdivacky  /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
280198090Srdivacky  /// architecture name, for example as accepted by "gcc -arch" (see also
281198090Srdivacky  /// arch(3)).
282199481Srdivacky  static ArchType getArchTypeForDarwinArchName(StringRef Str);
283198090Srdivacky
284198090Srdivacky  /// @}
285193323Sed};
286193323Sed
287193323Sed} // End llvm namespace
288193323Sed
289193323Sed
290193323Sed#endif
291