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