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
90 MinGW32, // i*86-pc-mingw32, *-w64-mingw32
100 enum EnvironmentType {
112 /// The parsed arch type (or InvalidArch if uninitialized).
113 mutable ArchType Arch;
115 /// The parsed vendor type.
116 mutable VendorType Vendor;
118 /// The parsed OS type.
121 /// The parsed Environment type.
122 mutable EnvironmentType Environment;
124 bool isInitialized() const { return Arch != InvalidArch; }
125 static ArchType ParseArch(StringRef ArchName);
126 static VendorType ParseVendor(StringRef VendorName);
127 static OSType ParseOS(StringRef OSName);
128 static EnvironmentType ParseEnvironment(StringRef EnvironmentName);
132 /// @name Constructors
135 Triple() : Data(), Arch(InvalidArch) {}
136 explicit Triple(StringRef Str) : Data(Str), Arch(InvalidArch) {}
137 explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr)
138 : Data(ArchStr), Arch(InvalidArch) {
145 explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr,
146 StringRef EnvironmentStr)
147 : Data(ArchStr), Arch(InvalidArch) {
153 Data += EnvironmentStr;
157 /// @name Normalization
160 /// normalize - Turn an arbitrary machine specification into the canonical
161 /// triple form (or something sensible that the Triple class understands if
162 /// nothing better can reasonably be done). In particular, it handles the
163 /// common case in which otherwise valid components are in the wrong order.
164 static std::string normalize(StringRef Str);
167 /// @name Typed Component Access
170 /// getArch - Get the parsed architecture type of this triple.
171 ArchType getArch() const {
172 if (!isInitialized()) Parse();
176 /// getVendor - Get the parsed vendor type of this triple.
177 VendorType getVendor() const {
178 if (!isInitialized()) Parse();
182 /// getOS - Get the parsed operating system type of this triple.
183 OSType getOS() const {
184 if (!isInitialized()) Parse();
188 /// hasEnvironment - Does this triple have the optional environment
189 /// (fourth) component?
190 bool hasEnvironment() const {
191 return getEnvironmentName() != "";
194 /// getEnvironment - Get the parsed environment type of this triple.
195 EnvironmentType getEnvironment() const {
196 if (!isInitialized()) Parse();
201 /// @name Direct Component Access
204 const std::string &str() const { return Data; }
206 const std::string &getTriple() const { return Data; }
208 /// getArchName - Get the architecture (first) component of the
210 StringRef getArchName() const;
212 /// getVendorName - Get the vendor (second) component of the triple.
213 StringRef getVendorName() const;
215 /// getOSName - Get the operating system (third) component of the
217 StringRef getOSName() const;
219 /// getEnvironmentName - Get the optional environment (fourth)
220 /// component of the triple, or "" if empty.
221 StringRef getEnvironmentName() const;
223 /// getOSAndEnvironmentName - Get the operating system and optional
224 /// environment components as a single string (separated by a '-'
225 /// if the environment component is present).
226 StringRef getOSAndEnvironmentName() const;
228 /// getOSNumber - Parse the version number from the OS name component of the
229 /// triple, if present.
231 /// For example, "fooos1.2.3" would return (1, 2, 3).
233 /// If an entry is not defined, it will be returned as 0.
234 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
236 /// getOSMajorVersion - Return just the major version number, this is
237 /// specialized because it is a common query.
238 unsigned getOSMajorVersion() const {
239 unsigned Maj, Min, Micro;
240 getDarwinNumber(Maj, Min, Micro);
244 void getDarwinNumber(unsigned &Major, unsigned &Minor,
245 unsigned &Micro) const {
246 return getOSVersion(Major, Minor, Micro);
249 unsigned getDarwinMajorNumber() const {
250 return getOSMajorVersion();
253 /// isOSVersionLT - Helper function for doing comparisons against version
254 /// numbers included in the target triple.
255 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
256 unsigned Micro = 0) const {
258 getOSVersion(LHS[0], LHS[1], LHS[2]);
261 return LHS[0] < Major;
263 return LHS[1] < Minor;
265 return LHS[1] < Micro;
270 /// isOSX - Is this an OS X triple. For legacy reasons, we support both
271 /// "darwin" and "osx" as OS X triples.
273 return getOS() == Triple::Darwin || getOS() == Triple::OSX ||
274 getOS() == Triple::MacOSX;
277 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
278 bool isOSDarwin() const {
279 return isOSX() ||getOS() == Triple::IOS;
282 /// isOSWindows - Is this a "Windows" OS.
283 bool isOSWindows() const {
284 return getOS() == Triple::Win32 || getOS() == Triple::Cygwin ||
285 getOS() == Triple::MinGW32;
288 /// isOSXVersionLT - Comparison function for checking OS X version
289 /// compatibility, which handles supporting skewed version numbering schemes
290 /// used by the "darwin" triples.
291 unsigned isOSXVersionLT(unsigned Major, unsigned Minor = 0,
292 unsigned Micro = 0) const {
293 assert(isOSX() && "Not an OS X triple!");
295 // If this is OS X, expect a sane version number.
296 if (getOS() == Triple::OSX || getOS() == Triple::MacOSX)
297 return isOSVersionLT(Major, Minor, Micro);
299 // Otherwise, compare to the "Darwin" number.
300 assert(Major == 10 && "Unexpected major version");
301 return isOSVersionLT(Minor + 4, Micro, 0);
308 /// setArch - Set the architecture (first) component of the triple
310 void setArch(ArchType Kind);
312 /// setVendor - Set the vendor (second) component of the triple to a
314 void setVendor(VendorType Kind);
316 /// setOS - Set the operating system (third) component of the triple
318 void setOS(OSType Kind);
320 /// setEnvironment - Set the environment (fourth) component of the triple
322 void setEnvironment(EnvironmentType Kind);
324 /// setTriple - Set all components to the new triple \arg Str.
325 void setTriple(const Twine &Str);
327 /// setArchName - Set the architecture (first) component of the
329 void setArchName(StringRef Str);
331 /// setVendorName - Set the vendor (second) component of the triple
333 void setVendorName(StringRef Str);
335 /// setOSName - Set the operating system (third) component of the
337 void setOSName(StringRef Str);
339 /// setEnvironmentName - Set the optional environment (fourth)
340 /// component of the triple by name.
341 void setEnvironmentName(StringRef Str);
343 /// setOSAndEnvironmentName - Set the operating system and optional
344 /// environment components with a single string.
345 void setOSAndEnvironmentName(StringRef Str);
347 /// getArchNameForAssembler - Get an architecture name that is understood by
348 /// the target assembler.
349 const char *getArchNameForAssembler();
352 /// @name Static helpers for IDs.
355 /// getArchTypeName - Get the canonical name for the \arg Kind
357 static const char *getArchTypeName(ArchType Kind);
359 /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
360 /// architecture. This is the prefix used by the architecture specific
361 /// builtins, and is suitable for passing to \see
362 /// Intrinsic::getIntrinsicForGCCBuiltin().
364 /// \return - The architecture prefix, or 0 if none is defined.
365 static const char *getArchTypePrefix(ArchType Kind);
367 /// getVendorTypeName - Get the canonical name for the \arg Kind
369 static const char *getVendorTypeName(VendorType Kind);
371 /// getOSTypeName - Get the canonical name for the \arg Kind operating
373 static const char *getOSTypeName(OSType Kind);
375 /// getEnvironmentTypeName - Get the canonical name for the \arg Kind
377 static const char *getEnvironmentTypeName(EnvironmentType Kind);
380 /// @name Static helpers for converting alternate architecture names.
383 /// getArchTypeForLLVMName - The canonical type for the given LLVM
384 /// architecture name (e.g., "x86").
385 static ArchType getArchTypeForLLVMName(StringRef Str);
387 /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
388 /// architecture name, for example as accepted by "gcc -arch" (see also
390 static ArchType getArchTypeForDarwinArchName(StringRef Str);
395 } // End llvm namespace