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
89 MinGW32, // i*86-pc-mingw32, *-w64-mingw32
99 enum EnvironmentType {
111 /// The parsed arch type (or InvalidArch if uninitialized).
112 mutable ArchType Arch;
114 /// The parsed vendor type.
115 mutable VendorType Vendor;
117 /// The parsed OS type.
120 /// The parsed Environment type.
121 mutable EnvironmentType Environment;
123 bool isInitialized() const { return Arch != InvalidArch; }
124 static ArchType ParseArch(StringRef ArchName);
125 static VendorType ParseVendor(StringRef VendorName);
126 static OSType ParseOS(StringRef OSName);
127 static EnvironmentType ParseEnvironment(StringRef EnvironmentName);
131 /// @name Constructors
134 Triple() : Data(), Arch(InvalidArch) {}
135 explicit Triple(StringRef Str) : Data(Str), Arch(InvalidArch) {}
136 explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr)
137 : Data(ArchStr), Arch(InvalidArch) {
144 explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr,
145 StringRef EnvironmentStr)
146 : Data(ArchStr), Arch(InvalidArch) {
152 Data += EnvironmentStr;
156 /// @name Normalization
159 /// normalize - Turn an arbitrary machine specification into the canonical
160 /// triple form (or something sensible that the Triple class understands if
161 /// nothing better can reasonably be done). In particular, it handles the
162 /// common case in which otherwise valid components are in the wrong order.
163 static std::string normalize(StringRef Str);
166 /// @name Typed Component Access
169 /// getArch - Get the parsed architecture type of this triple.
170 ArchType getArch() const {
171 if (!isInitialized()) Parse();
175 /// getVendor - Get the parsed vendor type of this triple.
176 VendorType getVendor() const {
177 if (!isInitialized()) Parse();
181 /// getOS - Get the parsed operating system type of this triple.
182 OSType getOS() const {
183 if (!isInitialized()) Parse();
187 /// hasEnvironment - Does this triple have the optional environment
188 /// (fourth) component?
189 bool hasEnvironment() const {
190 return getEnvironmentName() != "";
193 /// getEnvironment - Get the parsed environment type of this triple.
194 EnvironmentType getEnvironment() const {
195 if (!isInitialized()) Parse();
200 /// @name Direct Component Access
203 const std::string &str() const { return Data; }
205 const std::string &getTriple() const { return Data; }
207 /// getArchName - Get the architecture (first) component of the
209 StringRef getArchName() const;
211 /// getVendorName - Get the vendor (second) component of the triple.
212 StringRef getVendorName() const;
214 /// getOSName - Get the operating system (third) component of the
216 StringRef getOSName() const;
218 /// getEnvironmentName - Get the optional environment (fourth)
219 /// component of the triple, or "" if empty.
220 StringRef getEnvironmentName() const;
222 /// getOSAndEnvironmentName - Get the operating system and optional
223 /// environment components as a single string (separated by a '-'
224 /// if the environment component is present).
225 StringRef getOSAndEnvironmentName() const;
227 /// getOSNumber - Parse the version number from the OS name component of the
228 /// triple, if present.
230 /// For example, "fooos1.2.3" would return (1, 2, 3).
232 /// If an entry is not defined, it will be returned as 0.
233 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
235 /// getOSMajorVersion - Return just the major version number, this is
236 /// specialized because it is a common query.
237 unsigned getOSMajorVersion() const {
238 unsigned Maj, Min, Micro;
239 getDarwinNumber(Maj, Min, Micro);
243 void getDarwinNumber(unsigned &Major, unsigned &Minor,
244 unsigned &Micro) const {
245 return getOSVersion(Major, Minor, Micro);
248 unsigned getDarwinMajorNumber() const {
249 return getOSMajorVersion();
252 bool isOSVersionLT(unsigned Major, unsigned B_Minor, unsigned Micro) {
254 getOSVersion(LHS[0], LHS[1], LHS[2]);
257 return LHS[0] < Major;
259 return LHS[1] < Minor;
261 return LHS[1] < Micro;
270 /// setArch - Set the architecture (first) component of the triple
272 void setArch(ArchType Kind);
274 /// setVendor - Set the vendor (second) component of the triple to a
276 void setVendor(VendorType Kind);
278 /// setOS - Set the operating system (third) component of the triple
280 void setOS(OSType Kind);
282 /// setEnvironment - Set the environment (fourth) component of the triple
284 void setEnvironment(EnvironmentType Kind);
286 /// setTriple - Set all components to the new triple \arg Str.
287 void setTriple(const Twine &Str);
289 /// setArchName - Set the architecture (first) component of the
291 void setArchName(StringRef Str);
293 /// setVendorName - Set the vendor (second) component of the triple
295 void setVendorName(StringRef Str);
297 /// setOSName - Set the operating system (third) component of the
299 void setOSName(StringRef Str);
301 /// setEnvironmentName - Set the optional environment (fourth)
302 /// component of the triple by name.
303 void setEnvironmentName(StringRef Str);
305 /// setOSAndEnvironmentName - Set the operating system and optional
306 /// environment components with a single string.
307 void setOSAndEnvironmentName(StringRef Str);
309 /// getArchNameForAssembler - Get an architecture name that is understood by
310 /// the target assembler.
311 const char *getArchNameForAssembler();
314 /// @name Static helpers for IDs.
317 /// getArchTypeName - Get the canonical name for the \arg Kind
319 static const char *getArchTypeName(ArchType Kind);
321 /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
322 /// architecture. This is the prefix used by the architecture specific
323 /// builtins, and is suitable for passing to \see
324 /// Intrinsic::getIntrinsicForGCCBuiltin().
326 /// \return - The architecture prefix, or 0 if none is defined.
327 static const char *getArchTypePrefix(ArchType Kind);
329 /// getVendorTypeName - Get the canonical name for the \arg Kind
331 static const char *getVendorTypeName(VendorType Kind);
333 /// getOSTypeName - Get the canonical name for the \arg Kind operating
335 static const char *getOSTypeName(OSType Kind);
337 /// getEnvironmentTypeName - Get the canonical name for the \arg Kind
339 static const char *getEnvironmentTypeName(EnvironmentType Kind);
342 /// @name Static helpers for converting alternate architecture names.
345 /// getArchTypeForLLVMName - The canonical type for the given LLVM
346 /// architecture name (e.g., "x86").
347 static ArchType getArchTypeForLLVMName(StringRef Str);
349 /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
350 /// architecture name, for example as accepted by "gcc -arch" (see also
352 static ArchType getArchTypeForDarwinArchName(StringRef Str);
357 } // End llvm namespace