1 //===-- llvm/ADT/Triple.h - Target triple helper class ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ADT_TRIPLE_H
11 #define LLVM_ADT_TRIPLE_H
13 #include "llvm/ADT/StringRef.h"
16 // Some system headers or GCC predefined macros conflict with identifiers in
17 // this file. Undefine them here.
25 /// Triple - Helper class for working with target triples.
27 /// Target triples are strings in the canonical form:
28 /// ARCHITECTURE-VENDOR-OPERATING_SYSTEM
30 /// ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
32 /// This class is used for clients which want to support arbitrary
33 /// target triples, but also want to implement certain special
34 /// behavior for particular targets. This class isolates the mapping
35 /// from the components of the target triple to well known IDs.
37 /// At its core the Triple class is designed to be a wrapper for a triple
38 /// string; the constructor does not change or normalize the triple string.
39 /// Clients that need to handle the non-canonical triples that users often
40 /// specify should use the normalize method.
42 /// See autoconf/config.guess for a glimpse into what triples look like in
49 alpha, // Alpha: alpha
50 arm, // ARM; arm, armv.*, xscale
51 bfin, // Blackfin: bfin
52 cellspu, // CellSPU: spu, cellspu
53 mips, // MIPS: mips, mipsallegrex
54 mipsel, // MIPSEL: mipsel, mipsallegrexel, psp
55 msp430, // MSP430: msp430
57 ppc64, // PPC64: powerpc64, ppu
58 sparc, // Sparc: sparc
59 sparcv9, // Sparcv9: Sparcv9
60 systemz, // SystemZ: s390x
61 tce, // TCE (http://tce.cs.tut.fi/): tce
62 thumb, // Thumb: thumb, thumbv.*
64 x86_64, // X86-64: amd64, x86_64
65 xcore, // XCore: xcore
66 mblaze, // MBlaze: mblaze
99 enum EnvironmentType {
110 /// The parsed arch type (or InvalidArch if uninitialized).
111 mutable ArchType Arch;
113 /// The parsed vendor type.
114 mutable VendorType Vendor;
116 /// The parsed OS type.
119 /// The parsed Environment type.
120 mutable EnvironmentType Environment;
122 bool isInitialized() const { return Arch != InvalidArch; }
123 static ArchType ParseArch(StringRef ArchName);
124 static VendorType ParseVendor(StringRef VendorName);
125 static OSType ParseOS(StringRef OSName);
126 static EnvironmentType ParseEnvironment(StringRef EnvironmentName);
130 /// @name Constructors
133 Triple() : Data(), Arch(InvalidArch) {}
134 explicit Triple(StringRef Str) : Data(Str), Arch(InvalidArch) {}
135 explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr)
136 : Data(ArchStr), Arch(InvalidArch) {
143 explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr,
144 StringRef EnvironmentStr)
145 : Data(ArchStr), Arch(InvalidArch) {
151 Data += EnvironmentStr;
155 /// @name Normalization
158 /// normalize - Turn an arbitrary machine specification into the canonical
159 /// triple form (or something sensible that the Triple class understands if
160 /// nothing better can reasonably be done). In particular, it handles the
161 /// common case in which otherwise valid components are in the wrong order.
162 static std::string normalize(StringRef Str);
165 /// @name Typed Component Access
168 /// getArch - Get the parsed architecture type of this triple.
169 ArchType getArch() const {
170 if (!isInitialized()) Parse();
174 /// getVendor - Get the parsed vendor type of this triple.
175 VendorType getVendor() const {
176 if (!isInitialized()) Parse();
180 /// getOS - Get the parsed operating system type of this triple.
181 OSType getOS() const {
182 if (!isInitialized()) Parse();
186 /// hasEnvironment - Does this triple have the optional environment
187 /// (fourth) component?
188 bool hasEnvironment() const {
189 return getEnvironmentName() != "";
192 /// getEnvironment - Get the parsed environment type of this triple.
193 EnvironmentType getEnvironment() const {
194 if (!isInitialized()) Parse();
199 /// @name Direct Component Access
202 const std::string &str() const { return Data; }
204 const std::string &getTriple() const { return Data; }
206 /// getArchName - Get the architecture (first) component of the
208 StringRef getArchName() const;
210 /// getVendorName - Get the vendor (second) component of the triple.
211 StringRef getVendorName() const;
213 /// getOSName - Get the operating system (third) component of the
215 StringRef getOSName() const;
217 /// getEnvironmentName - Get the optional environment (fourth)
218 /// component of the triple, or "" if empty.
219 StringRef getEnvironmentName() const;
221 /// getOSAndEnvironmentName - Get the operating system and optional
222 /// environment components as a single string (separated by a '-'
223 /// if the environment component is present).
224 StringRef getOSAndEnvironmentName() const;
227 /// getDarwinNumber - Parse the 'darwin number' out of the specific target
228 /// triple. For example, if we have darwin8.5 return 8,5,0. If any entry is
229 /// not defined, return 0's. This requires that the triple have an OSType of
230 /// darwin before it is called.
231 void getDarwinNumber(unsigned &Maj, unsigned &Min, unsigned &Revision) const;
233 /// getDarwinMajorNumber - Return just the major version number, this is
234 /// specialized because it is a common query.
235 unsigned getDarwinMajorNumber() const {
236 unsigned Maj, Min, Rev;
237 getDarwinNumber(Maj, Min, Rev);
245 /// setArch - Set the architecture (first) component of the triple
247 void setArch(ArchType Kind);
249 /// setVendor - Set the vendor (second) component of the triple to a
251 void setVendor(VendorType Kind);
253 /// setOS - Set the operating system (third) component of the triple
255 void setOS(OSType Kind);
257 /// setEnvironment - Set the environment (fourth) component of the triple
259 void setEnvironment(EnvironmentType Kind);
261 /// setTriple - Set all components to the new triple \arg Str.
262 void setTriple(const Twine &Str);
264 /// setArchName - Set the architecture (first) component of the
266 void setArchName(StringRef Str);
268 /// setVendorName - Set the vendor (second) component of the triple
270 void setVendorName(StringRef Str);
272 /// setOSName - Set the operating system (third) component of the
274 void setOSName(StringRef Str);
276 /// setEnvironmentName - Set the optional environment (fourth)
277 /// component of the triple by name.
278 void setEnvironmentName(StringRef Str);
280 /// setOSAndEnvironmentName - Set the operating system and optional
281 /// environment components with a single string.
282 void setOSAndEnvironmentName(StringRef Str);
284 /// getArchNameForAssembler - Get an architecture name that is understood by
285 /// the target assembler.
286 const char *getArchNameForAssembler();
289 /// @name Static helpers for IDs.
292 /// getArchTypeName - Get the canonical name for the \arg Kind
294 static const char *getArchTypeName(ArchType Kind);
296 /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
297 /// architecture. This is the prefix used by the architecture specific
298 /// builtins, and is suitable for passing to \see
299 /// Intrinsic::getIntrinsicForGCCBuiltin().
301 /// \return - The architecture prefix, or 0 if none is defined.
302 static const char *getArchTypePrefix(ArchType Kind);
304 /// getVendorTypeName - Get the canonical name for the \arg Kind
306 static const char *getVendorTypeName(VendorType Kind);
308 /// getOSTypeName - Get the canonical name for the \arg Kind operating
310 static const char *getOSTypeName(OSType Kind);
312 /// getEnvironmentTypeName - Get the canonical name for the \arg Kind
314 static const char *getEnvironmentTypeName(EnvironmentType Kind);
317 /// @name Static helpers for converting alternate architecture names.
320 /// getArchTypeForLLVMName - The canonical type for the given LLVM
321 /// architecture name (e.g., "x86").
322 static ArchType getArchTypeForLLVMName(StringRef Str);
324 /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
325 /// architecture name, for example as accepted by "gcc -arch" (see also
327 static ArchType getArchTypeForDarwinArchName(StringRef Str);
332 } // End llvm namespace