Triple.h revision fdf0dc9e0bb5c0b848286e6c96bfc9a390d26775
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 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 enum VendorType { 70 UnknownVendor, 71 72 Apple, 73 PC, 74 SCEI 75 }; 76 enum OSType { 77 UnknownOS, 78 79 AuroraUX, 80 Cygwin, 81 Darwin, 82 DragonFly, 83 FreeBSD, 84 IOS, 85 KFreeBSD, 86 Linux, 87 Lv2, // PS3 88 MacOSX, 89 MinGW32, // i*86-pc-mingw32, *-w64-mingw32 90 NetBSD, 91 OpenBSD, 92 Solaris, 93 Win32, 94 Haiku, 95 Minix, 96 RTEMS, 97 NativeClient 98 }; 99 enum EnvironmentType { 100 UnknownEnvironment, 101 102 GNU, 103 GNUEABI, 104 GNUEABIHF, 105 EABI, 106 MachO, 107 ANDROIDEABI 108 }; 109 110private: 111 std::string Data; 112 113 /// The parsed arch type. 114 ArchType Arch; 115 116 /// The parsed vendor type. 117 VendorType Vendor; 118 119 /// The parsed OS type. 120 OSType OS; 121 122 /// The parsed Environment type. 123 EnvironmentType Environment; 124 125public: 126 /// @name Constructors 127 /// @{ 128 129 /// \brief Default constructor is the same as an empty string and leaves all 130 /// triple fields unknown. 131 Triple() : Data(), Arch(), Vendor(), OS(), Environment() {} 132 133 explicit Triple(const Twine &Str); 134 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr); 135 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr, 136 const Twine &EnvironmentStr); 137 138 /// @} 139 /// @name Normalization 140 /// @{ 141 142 /// normalize - Turn an arbitrary machine specification into the canonical 143 /// triple form (or something sensible that the Triple class understands if 144 /// nothing better can reasonably be done). In particular, it handles the 145 /// common case in which otherwise valid components are in the wrong order. 146 static std::string normalize(StringRef Str); 147 148 /// @} 149 /// @name Typed Component Access 150 /// @{ 151 152 /// getArch - Get the parsed architecture type of this triple. 153 ArchType getArch() const { return Arch; } 154 155 /// getVendor - Get the parsed vendor type of this triple. 156 VendorType getVendor() const { return Vendor; } 157 158 /// getOS - Get the parsed operating system type of this triple. 159 OSType getOS() const { return OS; } 160 161 /// hasEnvironment - Does this triple have the optional environment 162 /// (fourth) component? 163 bool hasEnvironment() const { 164 return getEnvironmentName() != ""; 165 } 166 167 /// getEnvironment - Get the parsed environment type of this triple. 168 EnvironmentType getEnvironment() const { return Environment; } 169 170 /// getOSVersion - Parse the version number from the OS name component of the 171 /// triple, if present. 172 /// 173 /// For example, "fooos1.2.3" would return (1, 2, 3). 174 /// 175 /// If an entry is not defined, it will be returned as 0. 176 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const; 177 178 /// getOSMajorVersion - Return just the major version number, this is 179 /// specialized because it is a common query. 180 unsigned getOSMajorVersion() const { 181 unsigned Maj, Min, Micro; 182 getOSVersion(Maj, Min, Micro); 183 return Maj; 184 } 185 186 /// getMacOSXVersion - Parse the version number as with getOSVersion and then 187 /// translate generic "darwin" versions to the corresponding OS X versions. 188 /// This may also be called with IOS triples but the OS X version number is 189 /// just set to a constant 10.4.0 in that case. Returns true if successful. 190 bool getMacOSXVersion(unsigned &Major, unsigned &Minor, 191 unsigned &Micro) const; 192 193 /// @} 194 /// @name Direct Component Access 195 /// @{ 196 197 const std::string &str() const { return Data; } 198 199 const std::string &getTriple() const { return Data; } 200 201 /// getArchName - Get the architecture (first) component of the 202 /// triple. 203 StringRef getArchName() const; 204 205 /// getVendorName - Get the vendor (second) component of the triple. 206 StringRef getVendorName() const; 207 208 /// getOSName - Get the operating system (third) component of the 209 /// triple. 210 StringRef getOSName() const; 211 212 /// getEnvironmentName - Get the optional environment (fourth) 213 /// component of the triple, or "" if empty. 214 StringRef getEnvironmentName() const; 215 216 /// getOSAndEnvironmentName - Get the operating system and optional 217 /// environment components as a single string (separated by a '-' 218 /// if the environment component is present). 219 StringRef getOSAndEnvironmentName() const; 220 221 /// @} 222 /// @name Convenience Predicates 223 /// @{ 224 225 /// \brief Test whether the architecture is 64-bit 226 /// 227 /// Note that this tests for 64-bit pointer width, and nothing else. Note 228 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and 229 /// 16-bit. The inner details of pointer width for particular architectures 230 /// is not summed up in the triple, and so only a coarse grained predicate 231 /// system is provided. 232 bool isArch64Bit() const; 233 234 /// \brief Test whether the architecture is 32-bit 235 /// 236 /// Note that this tests for 32-bit pointer width, and nothing else. 237 bool isArch32Bit() const; 238 239 /// \brief Test whether the architecture is 16-bit 240 /// 241 /// Note that this tests for 16-bit pointer width, and nothing else. 242 bool isArch16Bit() const; 243 244 /// isOSVersionLT - Helper function for doing comparisons against version 245 /// numbers included in the target triple. 246 bool isOSVersionLT(unsigned Major, unsigned Minor = 0, 247 unsigned Micro = 0) const { 248 unsigned LHS[3]; 249 getOSVersion(LHS[0], LHS[1], LHS[2]); 250 251 if (LHS[0] != Major) 252 return LHS[0] < Major; 253 if (LHS[1] != Minor) 254 return LHS[1] < Minor; 255 if (LHS[2] != Micro) 256 return LHS[1] < Micro; 257 258 return false; 259 } 260 261 /// isMacOSXVersionLT - Comparison function for checking OS X version 262 /// compatibility, which handles supporting skewed version numbering schemes 263 /// used by the "darwin" triples. 264 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0, 265 unsigned Micro = 0) const { 266 assert(isMacOSX() && "Not an OS X triple!"); 267 268 // If this is OS X, expect a sane version number. 269 if (getOS() == Triple::MacOSX) 270 return isOSVersionLT(Major, Minor, Micro); 271 272 // Otherwise, compare to the "Darwin" number. 273 assert(Major == 10 && "Unexpected major version"); 274 return isOSVersionLT(Minor + 4, Micro, 0); 275 } 276 277 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both 278 /// "darwin" and "osx" as OS X triples. 279 bool isMacOSX() const { 280 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX; 281 } 282 283 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS). 284 bool isOSDarwin() const { 285 return isMacOSX() || getOS() == Triple::IOS; 286 } 287 288 /// \brief Tests for either Cygwin or MinGW OS 289 bool isOSCygMing() const { 290 return getOS() == Triple::Cygwin || getOS() == Triple::MinGW32; 291 } 292 293 /// isOSWindows - Is this a "Windows" OS. 294 bool isOSWindows() const { 295 return getOS() == Triple::Win32 || isOSCygMing(); 296 } 297 298 /// \brief Tests whether the OS uses the ELF binary format. 299 bool isOSBinFormatELF() const { 300 return !isOSDarwin() && !isOSWindows(); 301 } 302 303 /// \brief Tests whether the OS uses the COFF binary format. 304 bool isOSBinFormatCOFF() const { 305 return isOSWindows(); 306 } 307 308 /// \brief Tests whether the environment is MachO. 309 // FIXME: Should this be an OSBinFormat predicate? 310 bool isEnvironmentMachO() const { 311 return getEnvironment() == Triple::MachO || isOSDarwin(); 312 } 313 314 /// @} 315 /// @name Mutators 316 /// @{ 317 318 /// setArch - Set the architecture (first) component of the triple 319 /// to a known type. 320 void setArch(ArchType Kind); 321 322 /// setVendor - Set the vendor (second) component of the triple to a 323 /// known type. 324 void setVendor(VendorType Kind); 325 326 /// setOS - Set the operating system (third) component of the triple 327 /// to a known type. 328 void setOS(OSType Kind); 329 330 /// setEnvironment - Set the environment (fourth) component of the triple 331 /// to a known type. 332 void setEnvironment(EnvironmentType Kind); 333 334 /// setTriple - Set all components to the new triple \arg Str. 335 void setTriple(const Twine &Str); 336 337 /// setArchName - Set the architecture (first) component of the 338 /// triple by name. 339 void setArchName(StringRef Str); 340 341 /// setVendorName - Set the vendor (second) component of the triple 342 /// by name. 343 void setVendorName(StringRef Str); 344 345 /// setOSName - Set the operating system (third) component of the 346 /// triple by name. 347 void setOSName(StringRef Str); 348 349 /// setEnvironmentName - Set the optional environment (fourth) 350 /// component of the triple by name. 351 void setEnvironmentName(StringRef Str); 352 353 /// setOSAndEnvironmentName - Set the operating system and optional 354 /// environment components with a single string. 355 void setOSAndEnvironmentName(StringRef Str); 356 357 /// getArchNameForAssembler - Get an architecture name that is understood by 358 /// the target assembler. 359 const char *getArchNameForAssembler(); 360 361 /// @} 362 /// @name Helpers to build variants of a particular triple. 363 /// @{ 364 365 /// \brief Form a triple with a 32-bit variant of the current architecture. 366 /// 367 /// This can be used to move across "families" of architectures where useful. 368 /// 369 /// \returns A new triple with a 32-bit architecture or an unknown 370 /// architecture if no such variant can be found. 371 llvm::Triple get32BitArchVariant() const; 372 373 /// \brief Form a triple with a 64-bit variant of the current architecture. 374 /// 375 /// This can be used to move across "families" of architectures where useful. 376 /// 377 /// \returns A new triple with a 64-bit architecture or an unknown 378 /// architecture if no such variant can be found. 379 llvm::Triple get64BitArchVariant() const; 380 381 /// @} 382 /// @name Static helpers for IDs. 383 /// @{ 384 385 /// getArchTypeName - Get the canonical name for the \arg Kind 386 /// architecture. 387 static const char *getArchTypeName(ArchType Kind); 388 389 /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind 390 /// architecture. This is the prefix used by the architecture specific 391 /// builtins, and is suitable for passing to \see 392 /// Intrinsic::getIntrinsicForGCCBuiltin(). 393 /// 394 /// \return - The architecture prefix, or 0 if none is defined. 395 static const char *getArchTypePrefix(ArchType Kind); 396 397 /// getVendorTypeName - Get the canonical name for the \arg Kind 398 /// vendor. 399 static const char *getVendorTypeName(VendorType Kind); 400 401 /// getOSTypeName - Get the canonical name for the \arg Kind operating 402 /// system. 403 static const char *getOSTypeName(OSType Kind); 404 405 /// getEnvironmentTypeName - Get the canonical name for the \arg Kind 406 /// environment. 407 static const char *getEnvironmentTypeName(EnvironmentType Kind); 408 409 /// @} 410 /// @name Static helpers for converting alternate architecture names. 411 /// @{ 412 413 /// getArchTypeForLLVMName - The canonical type for the given LLVM 414 /// architecture name (e.g., "x86"). 415 static ArchType getArchTypeForLLVMName(StringRef Str); 416 417 /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin" 418 /// architecture name, for example as accepted by "gcc -arch" (see also 419 /// arch(3)). 420 static ArchType getArchTypeForDarwinArchName(StringRef Str); 421 422 /// @} 423}; 424 425} // End llvm namespace 426 427 428#endif 429