Triple.h revision 652b48bf231521642c53221a7d10a0acf2030f48
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 alpha, // Alpha: alpha 47 arm, // ARM; arm, armv.*, xscale 48 bfin, // Blackfin: bfin 49 cellspu, // CellSPU: spu, cellspu 50 mips, // MIPS: mips, mipsallegrex 51 mipsel, // MIPSEL: mipsel, mipsallegrexel, psp 52 msp430, // MSP430: msp430 53 ppc, // PPC: powerpc 54 ppc64, // PPC64: powerpc64, ppu 55 sparc, // Sparc: sparc 56 sparcv9, // Sparcv9: Sparcv9 57 systemz, // SystemZ: s390x 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 67 InvalidArch 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 Psp, 93 Solaris, 94 Win32, 95 Haiku, 96 Minix, 97 RTEMS 98 }; 99 enum EnvironmentType { 100 UnknownEnvironment, 101 102 GNU, 103 GNUEABI, 104 EABI, 105 MachO 106 }; 107 108private: 109 std::string Data; 110 111 /// The parsed arch type (or InvalidArch if uninitialized). 112 mutable ArchType Arch; 113 114 /// The parsed vendor type. 115 mutable VendorType Vendor; 116 117 /// The parsed OS type. 118 mutable OSType OS; 119 120 /// The parsed Environment type. 121 mutable EnvironmentType Environment; 122 123 bool isInitialized() const { return Arch != InvalidArch; } 124 static ArchType ParseArch(StringRef ArchName); 125 static VendorType ParseVendor(StringRef VendorName); 126 static OSType ParseOS(StringRef OSName); 127 static EnvironmentType ParseEnvironment(StringRef EnvironmentName); 128 void Parse() const; 129 130public: 131 /// @name Constructors 132 /// @{ 133 134 Triple() : Data(), Arch(InvalidArch) {} 135 explicit Triple(const Twine &Str) : Data(Str.str()), Arch(InvalidArch) {} 136 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr) 137 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()), 138 Arch(InvalidArch) { 139 } 140 141 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr, 142 const Twine &EnvironmentStr) 143 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') + 144 EnvironmentStr).str()), Arch(InvalidArch) { 145 } 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 /// @} 192 /// @name Direct Component Access 193 /// @{ 194 195 const std::string &str() const { return Data; } 196 197 const std::string &getTriple() const { return Data; } 198 199 /// getArchName - Get the architecture (first) component of the 200 /// triple. 201 StringRef getArchName() const; 202 203 /// getVendorName - Get the vendor (second) component of the triple. 204 StringRef getVendorName() const; 205 206 /// getOSName - Get the operating system (third) component of the 207 /// triple. 208 StringRef getOSName() const; 209 210 /// getEnvironmentName - Get the optional environment (fourth) 211 /// component of the triple, or "" if empty. 212 StringRef getEnvironmentName() const; 213 214 /// getOSAndEnvironmentName - Get the operating system and optional 215 /// environment components as a single string (separated by a '-' 216 /// if the environment component is present). 217 StringRef getOSAndEnvironmentName() const; 218 219 /// getOSVersion - Parse the version number from the OS name component of the 220 /// triple, if present. 221 /// 222 /// For example, "fooos1.2.3" would return (1, 2, 3). 223 /// 224 /// If an entry is not defined, it will be returned as 0. 225 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const; 226 227 /// getOSMajorVersion - Return just the major version number, this is 228 /// specialized because it is a common query. 229 unsigned getOSMajorVersion() const { 230 unsigned Maj, Min, Micro; 231 getOSVersion(Maj, Min, Micro); 232 return Maj; 233 } 234 235 /// isOSVersionLT - Helper function for doing comparisons against version 236 /// numbers included in the target triple. 237 bool isOSVersionLT(unsigned Major, unsigned Minor = 0, 238 unsigned Micro = 0) const { 239 unsigned LHS[3]; 240 getOSVersion(LHS[0], LHS[1], LHS[2]); 241 242 if (LHS[0] != Major) 243 return LHS[0] < Major; 244 if (LHS[1] != Minor) 245 return LHS[1] < Minor; 246 if (LHS[2] != Micro) 247 return LHS[1] < Micro; 248 249 return false; 250 } 251 252 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both 253 /// "darwin" and "osx" as OS X triples. 254 bool isMacOSX() const { 255 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX; 256 } 257 258 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS). 259 bool isOSDarwin() const { 260 return isMacOSX() || getOS() == Triple::IOS; 261 } 262 263 /// isOSWindows - Is this a "Windows" OS. 264 bool isOSWindows() const { 265 return getOS() == Triple::Win32 || getOS() == Triple::Cygwin || 266 getOS() == Triple::MinGW32; 267 } 268 269 /// isMacOSXVersionLT - Comparison function for checking OS X version 270 /// compatibility, which handles supporting skewed version numbering schemes 271 /// used by the "darwin" triples. 272 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0, 273 unsigned Micro = 0) const { 274 assert(isMacOSX() && "Not an OS X triple!"); 275 276 // If this is OS X, expect a sane version number. 277 if (getOS() == Triple::MacOSX) 278 return isOSVersionLT(Major, Minor, Micro); 279 280 // Otherwise, compare to the "Darwin" number. 281 assert(Major == 10 && "Unexpected major version"); 282 return isOSVersionLT(Minor + 4, Micro, 0); 283 } 284 285 /// @} 286 /// @name Mutators 287 /// @{ 288 289 /// setArch - Set the architecture (first) component of the triple 290 /// to a known type. 291 void setArch(ArchType Kind); 292 293 /// setVendor - Set the vendor (second) component of the triple to a 294 /// known type. 295 void setVendor(VendorType Kind); 296 297 /// setOS - Set the operating system (third) component of the triple 298 /// to a known type. 299 void setOS(OSType Kind); 300 301 /// setEnvironment - Set the environment (fourth) component of the triple 302 /// to a known type. 303 void setEnvironment(EnvironmentType Kind); 304 305 /// setTriple - Set all components to the new triple \arg Str. 306 void setTriple(const Twine &Str); 307 308 /// setArchName - Set the architecture (first) component of the 309 /// triple by name. 310 void setArchName(StringRef Str); 311 312 /// setVendorName - Set the vendor (second) component of the triple 313 /// by name. 314 void setVendorName(StringRef Str); 315 316 /// setOSName - Set the operating system (third) component of the 317 /// triple by name. 318 void setOSName(StringRef Str); 319 320 /// setEnvironmentName - Set the optional environment (fourth) 321 /// component of the triple by name. 322 void setEnvironmentName(StringRef Str); 323 324 /// setOSAndEnvironmentName - Set the operating system and optional 325 /// environment components with a single string. 326 void setOSAndEnvironmentName(StringRef Str); 327 328 /// getArchNameForAssembler - Get an architecture name that is understood by 329 /// the target assembler. 330 const char *getArchNameForAssembler(); 331 332 /// @} 333 /// @name Static helpers for IDs. 334 /// @{ 335 336 /// getArchTypeName - Get the canonical name for the \arg Kind 337 /// architecture. 338 static const char *getArchTypeName(ArchType Kind); 339 340 /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind 341 /// architecture. This is the prefix used by the architecture specific 342 /// builtins, and is suitable for passing to \see 343 /// Intrinsic::getIntrinsicForGCCBuiltin(). 344 /// 345 /// \return - The architecture prefix, or 0 if none is defined. 346 static const char *getArchTypePrefix(ArchType Kind); 347 348 /// getVendorTypeName - Get the canonical name for the \arg Kind 349 /// vendor. 350 static const char *getVendorTypeName(VendorType Kind); 351 352 /// getOSTypeName - Get the canonical name for the \arg Kind operating 353 /// system. 354 static const char *getOSTypeName(OSType Kind); 355 356 /// getEnvironmentTypeName - Get the canonical name for the \arg Kind 357 /// environment. 358 static const char *getEnvironmentTypeName(EnvironmentType Kind); 359 360 /// @} 361 /// @name Static helpers for converting alternate architecture names. 362 /// @{ 363 364 /// getArchTypeForLLVMName - The canonical type for the given LLVM 365 /// architecture name (e.g., "x86"). 366 static ArchType getArchTypeForLLVMName(StringRef Str); 367 368 /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin" 369 /// architecture name, for example as accepted by "gcc -arch" (see also 370 /// arch(3)). 371 static ArchType getArchTypeForDarwinArchName(StringRef Str); 372 373 /// @} 374}; 375 376} // End llvm namespace 377 378 379#endif 380