Triple.h revision cceb8f44a0ca3a9c57ab3a7f4947b2abda531cef
1//===-- llvm/ADT/Triple.h - Target triple helper class ----------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef LLVM_ADT_TRIPLE_H 11#define LLVM_ADT_TRIPLE_H 12 13#include "llvm/ADT/Twine.h" 14 15// Some system headers or GCC predefined macros conflict with identifiers in 16// this file. Undefine them here. 17#undef mips 18#undef sparc 19 20namespace llvm { 21 22/// Triple - Helper class for working with target triples. 23/// 24/// Target triples are strings in the canonical form: 25/// ARCHITECTURE-VENDOR-OPERATING_SYSTEM 26/// or 27/// ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT 28/// 29/// This class is used for clients which want to support arbitrary 30/// target triples, but also want to implement certain special 31/// behavior for particular targets. This class isolates the mapping 32/// from the components of the target triple to well known IDs. 33/// 34/// At its core the Triple class is designed to be a wrapper for a triple 35/// string; the constructor does not change or normalize the triple string. 36/// Clients that need to handle the non-canonical triples that users often 37/// specify should use the normalize method. 38/// 39/// See autoconf/config.guess for a glimpse into what triples look like in 40/// practice. 41class Triple { 42public: 43 enum ArchType { 44 UnknownArch, 45 46 arm, // ARM; arm, armv.*, xscale 47 cellspu, // CellSPU: spu, cellspu 48 hexagon, // Hexagon: hexagon 49 mips, // MIPS: mips, mipsallegrex 50 mipsel, // MIPSEL: mipsel, mipsallegrexel, psp 51 mips64, // MIPS64: mips64 52 mips64el,// MIPS64EL: mips64el 53 msp430, // MSP430: msp430 54 ppc, // PPC: powerpc 55 ppc64, // PPC64: powerpc64, ppu 56 sparc, // Sparc: sparc 57 sparcv9, // Sparcv9: Sparcv9 58 tce, // TCE (http://tce.cs.tut.fi/): tce 59 thumb, // Thumb: thumb, thumbv.* 60 x86, // X86: i[3-9]86 61 x86_64, // X86-64: amd64, x86_64 62 xcore, // XCore: xcore 63 mblaze, // MBlaze: mblaze 64 ptx32, // PTX: ptx (32-bit) 65 ptx64, // PTX: ptx (64-bit) 66 le32, // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten) 67 amdil, // amdil: amd IL 68 69 InvalidArch 70 }; 71 enum VendorType { 72 UnknownVendor, 73 74 Apple, 75 PC, 76 SCEI 77 }; 78 enum OSType { 79 UnknownOS, 80 81 AuroraUX, 82 Cygwin, 83 Darwin, 84 DragonFly, 85 FreeBSD, 86 IOS, 87 KFreeBSD, 88 Linux, 89 Lv2, // PS3 90 MacOSX, 91 MinGW32, // i*86-pc-mingw32, *-w64-mingw32 92 NetBSD, 93 OpenBSD, 94 Psp, 95 Solaris, 96 Win32, 97 Haiku, 98 Minix, 99 RTEMS, 100 NativeClient 101 }; 102 enum EnvironmentType { 103 UnknownEnvironment, 104 105 GNU, 106 GNUEABI, 107 GNUEABIHF, 108 EABI, 109 MachO, 110 ANDROIDEABI 111 }; 112 113private: 114 std::string Data; 115 116 /// The parsed arch type (or InvalidArch if uninitialized). 117 mutable ArchType Arch; 118 119 /// The parsed vendor type. 120 mutable VendorType Vendor; 121 122 /// The parsed OS type. 123 mutable OSType OS; 124 125 /// The parsed Environment type. 126 mutable EnvironmentType Environment; 127 128 bool isInitialized() const { return Arch != InvalidArch; } 129 static ArchType ParseArch(StringRef ArchName); 130 static VendorType ParseVendor(StringRef VendorName); 131 static OSType ParseOS(StringRef OSName); 132 static EnvironmentType ParseEnvironment(StringRef EnvironmentName); 133 void Parse() const; 134 135public: 136 /// @name Constructors 137 /// @{ 138 139 /// \brief Default constructor produces an empty, invalid triple. 140 Triple() : Data(), Arch(InvalidArch) {} 141 142 explicit Triple(const Twine &Str); 143 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr); 144 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr, 145 const Twine &EnvironmentStr); 146 147 /// @} 148 /// @name Normalization 149 /// @{ 150 151 /// normalize - Turn an arbitrary machine specification into the canonical 152 /// triple form (or something sensible that the Triple class understands if 153 /// nothing better can reasonably be done). In particular, it handles the 154 /// common case in which otherwise valid components are in the wrong order. 155 static std::string normalize(StringRef Str); 156 157 /// @} 158 /// @name Typed Component Access 159 /// @{ 160 161 /// getArch - Get the parsed architecture type of this triple. 162 ArchType getArch() const { 163 if (!isInitialized()) Parse(); 164 return Arch; 165 } 166 167 /// getVendor - Get the parsed vendor type of this triple. 168 VendorType getVendor() const { 169 if (!isInitialized()) Parse(); 170 return Vendor; 171 } 172 173 /// getOS - Get the parsed operating system type of this triple. 174 OSType getOS() const { 175 if (!isInitialized()) Parse(); 176 return OS; 177 } 178 179 /// hasEnvironment - Does this triple have the optional environment 180 /// (fourth) component? 181 bool hasEnvironment() const { 182 return getEnvironmentName() != ""; 183 } 184 185 /// getEnvironment - Get the parsed environment type of this triple. 186 EnvironmentType getEnvironment() const { 187 if (!isInitialized()) Parse(); 188 return Environment; 189 } 190 191 /// getOSVersion - Parse the version number from the OS name component of the 192 /// triple, if present. 193 /// 194 /// For example, "fooos1.2.3" would return (1, 2, 3). 195 /// 196 /// If an entry is not defined, it will be returned as 0. 197 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const; 198 199 /// getOSMajorVersion - Return just the major version number, this is 200 /// specialized because it is a common query. 201 unsigned getOSMajorVersion() const { 202 unsigned Maj, Min, Micro; 203 getOSVersion(Maj, Min, Micro); 204 return Maj; 205 } 206 207 /// getMacOSXVersion - Parse the version number as with getOSVersion and then 208 /// translate generic "darwin" versions to the corresponding OS X versions. 209 /// This may also be called with IOS triples but the OS X version number is 210 /// just set to a constant 10.4.0 in that case. Returns true if successful. 211 bool getMacOSXVersion(unsigned &Major, unsigned &Minor, 212 unsigned &Micro) const; 213 214 /// @} 215 /// @name Direct Component Access 216 /// @{ 217 218 const std::string &str() const { return Data; } 219 220 const std::string &getTriple() const { return Data; } 221 222 /// getArchName - Get the architecture (first) component of the 223 /// triple. 224 StringRef getArchName() const; 225 226 /// getVendorName - Get the vendor (second) component of the triple. 227 StringRef getVendorName() const; 228 229 /// getOSName - Get the operating system (third) component of the 230 /// triple. 231 StringRef getOSName() const; 232 233 /// getEnvironmentName - Get the optional environment (fourth) 234 /// component of the triple, or "" if empty. 235 StringRef getEnvironmentName() const; 236 237 /// getOSAndEnvironmentName - Get the operating system and optional 238 /// environment components as a single string (separated by a '-' 239 /// if the environment component is present). 240 StringRef getOSAndEnvironmentName() const; 241 242 /// @} 243 /// @name Convenience Predicates 244 /// @{ 245 246 /// \brief Test whether the architecture is 64-bit 247 /// 248 /// Note that this tests for 64-bit pointer width, and nothing else. Note 249 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and 250 /// 16-bit. The inner details of pointer width for particular architectures 251 /// is not summed up in the triple, and so only a coarse grained predicate 252 /// system is provided. 253 bool isArch64Bit() const; 254 255 /// \brief Test whether the architecture is 32-bit 256 /// 257 /// Note that this tests for 32-bit pointer width, and nothing else. 258 bool isArch32Bit() const; 259 260 /// \brief Test whether the architecture is 16-bit 261 /// 262 /// Note that this tests for 16-bit pointer width, and nothing else. 263 bool isArch16Bit() const; 264 265 /// isOSVersionLT - Helper function for doing comparisons against version 266 /// numbers included in the target triple. 267 bool isOSVersionLT(unsigned Major, unsigned Minor = 0, 268 unsigned Micro = 0) const { 269 unsigned LHS[3]; 270 getOSVersion(LHS[0], LHS[1], LHS[2]); 271 272 if (LHS[0] != Major) 273 return LHS[0] < Major; 274 if (LHS[1] != Minor) 275 return LHS[1] < Minor; 276 if (LHS[2] != Micro) 277 return LHS[1] < Micro; 278 279 return false; 280 } 281 282 /// isMacOSXVersionLT - Comparison function for checking OS X version 283 /// compatibility, which handles supporting skewed version numbering schemes 284 /// used by the "darwin" triples. 285 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0, 286 unsigned Micro = 0) const { 287 assert(isMacOSX() && "Not an OS X triple!"); 288 289 // If this is OS X, expect a sane version number. 290 if (getOS() == Triple::MacOSX) 291 return isOSVersionLT(Major, Minor, Micro); 292 293 // Otherwise, compare to the "Darwin" number. 294 assert(Major == 10 && "Unexpected major version"); 295 return isOSVersionLT(Minor + 4, Micro, 0); 296 } 297 298 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both 299 /// "darwin" and "osx" as OS X triples. 300 bool isMacOSX() const { 301 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX; 302 } 303 304 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS). 305 bool isOSDarwin() const { 306 return isMacOSX() || getOS() == Triple::IOS; 307 } 308 309 /// \brief Tests for either Cygwin or MinGW OS 310 bool isOSCygMing() const { 311 return getOS() == Triple::Cygwin || getOS() == Triple::MinGW32; 312 } 313 314 /// isOSWindows - Is this a "Windows" OS. 315 bool isOSWindows() const { 316 return getOS() == Triple::Win32 || isOSCygMing(); 317 } 318 319 /// \brief Tests whether the OS uses the ELF binary format. 320 bool isOSBinFormatELF() const { 321 return !isOSDarwin() && !isOSWindows(); 322 } 323 324 /// \brief Tests whether the OS uses the COFF binary format. 325 bool isOSBinFormatCOFF() const { 326 return isOSWindows(); 327 } 328 329 /// \brief Tests whether the environment is MachO. 330 // FIXME: Should this be an OSBinFormat predicate? 331 bool isEnvironmentMachO() const { 332 return getEnvironment() == Triple::MachO || isOSDarwin(); 333 } 334 335 /// @} 336 /// @name Mutators 337 /// @{ 338 339 /// setArch - Set the architecture (first) component of the triple 340 /// to a known type. 341 void setArch(ArchType Kind); 342 343 /// setVendor - Set the vendor (second) component of the triple to a 344 /// known type. 345 void setVendor(VendorType Kind); 346 347 /// setOS - Set the operating system (third) component of the triple 348 /// to a known type. 349 void setOS(OSType Kind); 350 351 /// setEnvironment - Set the environment (fourth) component of the triple 352 /// to a known type. 353 void setEnvironment(EnvironmentType Kind); 354 355 /// setTriple - Set all components to the new triple \arg Str. 356 void setTriple(const Twine &Str); 357 358 /// setArchName - Set the architecture (first) component of the 359 /// triple by name. 360 void setArchName(StringRef Str); 361 362 /// setVendorName - Set the vendor (second) component of the triple 363 /// by name. 364 void setVendorName(StringRef Str); 365 366 /// setOSName - Set the operating system (third) component of the 367 /// triple by name. 368 void setOSName(StringRef Str); 369 370 /// setEnvironmentName - Set the optional environment (fourth) 371 /// component of the triple by name. 372 void setEnvironmentName(StringRef Str); 373 374 /// setOSAndEnvironmentName - Set the operating system and optional 375 /// environment components with a single string. 376 void setOSAndEnvironmentName(StringRef Str); 377 378 /// getArchNameForAssembler - Get an architecture name that is understood by 379 /// the target assembler. 380 const char *getArchNameForAssembler(); 381 382 /// @} 383 /// @name Helpers to build variants of a particular triple. 384 /// @{ 385 386 /// \brief Form a triple with a 32-bit variant of the current architecture. 387 /// 388 /// This can be used to move across "families" of architectures where useful. 389 /// 390 /// \returns A new triple with a 32-bit architecture or an unknown 391 /// architecture if no such variant can be found. 392 llvm::Triple get32BitArchVariant() const; 393 394 /// \brief Form a triple with a 64-bit variant of the current architecture. 395 /// 396 /// This can be used to move across "families" of architectures where useful. 397 /// 398 /// \returns A new triple with a 64-bit architecture or an unknown 399 /// architecture if no such variant can be found. 400 llvm::Triple get64BitArchVariant() const; 401 402 /// @} 403 /// @name Static helpers for IDs. 404 /// @{ 405 406 /// getArchTypeName - Get the canonical name for the \arg Kind 407 /// architecture. 408 static const char *getArchTypeName(ArchType Kind); 409 410 /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind 411 /// architecture. This is the prefix used by the architecture specific 412 /// builtins, and is suitable for passing to \see 413 /// Intrinsic::getIntrinsicForGCCBuiltin(). 414 /// 415 /// \return - The architecture prefix, or 0 if none is defined. 416 static const char *getArchTypePrefix(ArchType Kind); 417 418 /// getVendorTypeName - Get the canonical name for the \arg Kind 419 /// vendor. 420 static const char *getVendorTypeName(VendorType Kind); 421 422 /// getOSTypeName - Get the canonical name for the \arg Kind operating 423 /// system. 424 static const char *getOSTypeName(OSType Kind); 425 426 /// getEnvironmentTypeName - Get the canonical name for the \arg Kind 427 /// environment. 428 static const char *getEnvironmentTypeName(EnvironmentType Kind); 429 430 /// @} 431 /// @name Static helpers for converting alternate architecture names. 432 /// @{ 433 434 /// getArchTypeForLLVMName - The canonical type for the given LLVM 435 /// architecture name (e.g., "x86"). 436 static ArchType getArchTypeForLLVMName(StringRef Str); 437 438 /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin" 439 /// architecture name, for example as accepted by "gcc -arch" (see also 440 /// arch(3)). 441 static ArchType getArchTypeForDarwinArchName(StringRef Str); 442 443 /// @} 444}; 445 446} // End llvm namespace 447 448 449#endif 450