Triple.h revision fd553c2cb54ee01d56f5d80d70a5d52220286fcc
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 EABI, 108 MachO, 109 ANDROIDEABI 110 }; 111 112private: 113 std::string Data; 114 115 /// The parsed arch type (or InvalidArch if uninitialized). 116 mutable ArchType Arch; 117 118 /// The parsed vendor type. 119 mutable VendorType Vendor; 120 121 /// The parsed OS type. 122 mutable OSType OS; 123 124 /// The parsed Environment type. 125 mutable EnvironmentType Environment; 126 127 bool isInitialized() const { return Arch != InvalidArch; } 128 static ArchType ParseArch(StringRef ArchName); 129 static VendorType ParseVendor(StringRef VendorName); 130 static OSType ParseOS(StringRef OSName); 131 static EnvironmentType ParseEnvironment(StringRef EnvironmentName); 132 void Parse() const; 133 134public: 135 /// @name Constructors 136 /// @{ 137 138 Triple() : Data(), Arch(InvalidArch) {} 139 explicit Triple(const Twine &Str) : Data(Str.str()), Arch(InvalidArch) {} 140 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr) 141 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()), 142 Arch(InvalidArch) { 143 } 144 145 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr, 146 const Twine &EnvironmentStr) 147 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') + 148 EnvironmentStr).str()), Arch(InvalidArch) { 149 } 150 151 /// @} 152 /// @name Normalization 153 /// @{ 154 155 /// normalize - Turn an arbitrary machine specification into the canonical 156 /// triple form (or something sensible that the Triple class understands if 157 /// nothing better can reasonably be done). In particular, it handles the 158 /// common case in which otherwise valid components are in the wrong order. 159 static std::string normalize(StringRef Str); 160 161 /// @} 162 /// @name Typed Component Access 163 /// @{ 164 165 /// getArch - Get the parsed architecture type of this triple. 166 ArchType getArch() const { 167 if (!isInitialized()) Parse(); 168 return Arch; 169 } 170 171 /// getVendor - Get the parsed vendor type of this triple. 172 VendorType getVendor() const { 173 if (!isInitialized()) Parse(); 174 return Vendor; 175 } 176 177 /// getOS - Get the parsed operating system type of this triple. 178 OSType getOS() const { 179 if (!isInitialized()) Parse(); 180 return OS; 181 } 182 183 /// hasEnvironment - Does this triple have the optional environment 184 /// (fourth) component? 185 bool hasEnvironment() const { 186 return getEnvironmentName() != ""; 187 } 188 189 /// getEnvironment - Get the parsed environment type of this triple. 190 EnvironmentType getEnvironment() const { 191 if (!isInitialized()) Parse(); 192 return Environment; 193 } 194 195 /// @} 196 /// @name Direct Component Access 197 /// @{ 198 199 const std::string &str() const { return Data; } 200 201 const std::string &getTriple() const { return Data; } 202 203 /// getArchName - Get the architecture (first) component of the 204 /// triple. 205 StringRef getArchName() const; 206 207 /// getVendorName - Get the vendor (second) component of the triple. 208 StringRef getVendorName() const; 209 210 /// getOSName - Get the operating system (third) component of the 211 /// triple. 212 StringRef getOSName() const; 213 214 /// getEnvironmentName - Get the optional environment (fourth) 215 /// component of the triple, or "" if empty. 216 StringRef getEnvironmentName() const; 217 218 /// getOSAndEnvironmentName - Get the operating system and optional 219 /// environment components as a single string (separated by a '-' 220 /// if the environment component is present). 221 StringRef getOSAndEnvironmentName() const; 222 223 /// getOSVersion - Parse the version number from the OS name component of the 224 /// triple, if present. 225 /// 226 /// For example, "fooos1.2.3" would return (1, 2, 3). 227 /// 228 /// If an entry is not defined, it will be returned as 0. 229 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const; 230 231 /// getOSMajorVersion - Return just the major version number, this is 232 /// specialized because it is a common query. 233 unsigned getOSMajorVersion() const { 234 unsigned Maj, Min, Micro; 235 getOSVersion(Maj, Min, Micro); 236 return Maj; 237 } 238 239 /// isOSVersionLT - Helper function for doing comparisons against version 240 /// numbers included in the target triple. 241 bool isOSVersionLT(unsigned Major, unsigned Minor = 0, 242 unsigned Micro = 0) const { 243 unsigned LHS[3]; 244 getOSVersion(LHS[0], LHS[1], LHS[2]); 245 246 if (LHS[0] != Major) 247 return LHS[0] < Major; 248 if (LHS[1] != Minor) 249 return LHS[1] < Minor; 250 if (LHS[2] != Micro) 251 return LHS[1] < Micro; 252 253 return false; 254 } 255 256 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both 257 /// "darwin" and "osx" as OS X triples. 258 bool isMacOSX() const { 259 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX; 260 } 261 262 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS). 263 bool isOSDarwin() const { 264 return isMacOSX() || getOS() == Triple::IOS; 265 } 266 267 /// isOSWindows - Is this a "Windows" OS. 268 bool isOSWindows() const { 269 return getOS() == Triple::Win32 || getOS() == Triple::Cygwin || 270 getOS() == Triple::MinGW32; 271 } 272 273 /// isMacOSXVersionLT - Comparison function for checking OS X version 274 /// compatibility, which handles supporting skewed version numbering schemes 275 /// used by the "darwin" triples. 276 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0, 277 unsigned Micro = 0) const { 278 assert(isMacOSX() && "Not an OS X triple!"); 279 280 // If this is OS X, expect a sane version number. 281 if (getOS() == Triple::MacOSX) 282 return isOSVersionLT(Major, Minor, Micro); 283 284 // Otherwise, compare to the "Darwin" number. 285 assert(Major == 10 && "Unexpected major version"); 286 return isOSVersionLT(Minor + 4, Micro, 0); 287 } 288 289 /// @} 290 /// @name Mutators 291 /// @{ 292 293 /// setArch - Set the architecture (first) component of the triple 294 /// to a known type. 295 void setArch(ArchType Kind); 296 297 /// setVendor - Set the vendor (second) component of the triple to a 298 /// known type. 299 void setVendor(VendorType Kind); 300 301 /// setOS - Set the operating system (third) component of the triple 302 /// to a known type. 303 void setOS(OSType Kind); 304 305 /// setEnvironment - Set the environment (fourth) component of the triple 306 /// to a known type. 307 void setEnvironment(EnvironmentType Kind); 308 309 /// setTriple - Set all components to the new triple \arg Str. 310 void setTriple(const Twine &Str); 311 312 /// setArchName - Set the architecture (first) component of the 313 /// triple by name. 314 void setArchName(StringRef Str); 315 316 /// setVendorName - Set the vendor (second) component of the triple 317 /// by name. 318 void setVendorName(StringRef Str); 319 320 /// setOSName - Set the operating system (third) component of the 321 /// triple by name. 322 void setOSName(StringRef Str); 323 324 /// setEnvironmentName - Set the optional environment (fourth) 325 /// component of the triple by name. 326 void setEnvironmentName(StringRef Str); 327 328 /// setOSAndEnvironmentName - Set the operating system and optional 329 /// environment components with a single string. 330 void setOSAndEnvironmentName(StringRef Str); 331 332 /// getArchNameForAssembler - Get an architecture name that is understood by 333 /// the target assembler. 334 const char *getArchNameForAssembler(); 335 336 /// @} 337 /// @name Static helpers for IDs. 338 /// @{ 339 340 /// getArchTypeName - Get the canonical name for the \arg Kind 341 /// architecture. 342 static const char *getArchTypeName(ArchType Kind); 343 344 /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind 345 /// architecture. This is the prefix used by the architecture specific 346 /// builtins, and is suitable for passing to \see 347 /// Intrinsic::getIntrinsicForGCCBuiltin(). 348 /// 349 /// \return - The architecture prefix, or 0 if none is defined. 350 static const char *getArchTypePrefix(ArchType Kind); 351 352 /// getVendorTypeName - Get the canonical name for the \arg Kind 353 /// vendor. 354 static const char *getVendorTypeName(VendorType Kind); 355 356 /// getOSTypeName - Get the canonical name for the \arg Kind operating 357 /// system. 358 static const char *getOSTypeName(OSType Kind); 359 360 /// getEnvironmentTypeName - Get the canonical name for the \arg Kind 361 /// environment. 362 static const char *getEnvironmentTypeName(EnvironmentType Kind); 363 364 /// @} 365 /// @name Static helpers for converting alternate architecture names. 366 /// @{ 367 368 /// getArchTypeForLLVMName - The canonical type for the given LLVM 369 /// architecture name (e.g., "x86"). 370 static ArchType getArchTypeForLLVMName(StringRef Str); 371 372 /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin" 373 /// architecture name, for example as accepted by "gcc -arch" (see also 374 /// arch(3)). 375 static ArchType getArchTypeForDarwinArchName(StringRef Str); 376 377 /// @} 378}; 379 380} // End llvm namespace 381 382 383#endif 384