Triple.h revision e1fee48cd0d1e515f247fe3bceceb0f854623f73
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/StringRef.h" 14#include <string> 15 16// Some system headers or GCC predefined macros conflict with identifiers in 17// this file. Undefine them here. 18#undef mips 19#undef sparc 20 21namespace llvm { 22class StringRef; 23class Twine; 24 25/// Triple - Helper class for working with target triples. 26/// 27/// Target triples are strings in the canonical form: 28/// ARCHITECTURE-VENDOR-OPERATING_SYSTEM 29/// or 30/// ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT 31/// 32/// This class is used for clients which want to support arbitrary 33/// target triples, but also want to implement certain special 34/// behavior for particular targets. This class isolates the mapping 35/// from the components of the target triple to well known IDs. 36/// 37/// At its core the Triple class is designed to be a wrapper for a triple 38/// string; the constructor does not change or normalize the triple string. 39/// Clients that need to handle the non-canonical triples that users often 40/// specify should use the normalize method. 41/// 42/// See autoconf/config.guess for a glimpse into what triples look like in 43/// practice. 44class Triple { 45public: 46 enum ArchType { 47 UnknownArch, 48 49 alpha, // Alpha: alpha 50 arm, // ARM; arm, armv.*, xscale 51 bfin, // Blackfin: bfin 52 cellspu, // CellSPU: spu, cellspu 53 mips, // MIPS: mips, mipsallegrex 54 mipsel, // MIPSEL: mipsel, mipsallegrexel, psp 55 msp430, // MSP430: msp430 56 ppc, // PPC: powerpc 57 ppc64, // PPC64: powerpc64, ppu 58 sparc, // Sparc: sparc 59 sparcv9, // Sparcv9: Sparcv9 60 systemz, // SystemZ: s390x 61 tce, // TCE (http://tce.cs.tut.fi/): tce 62 thumb, // Thumb: thumb, thumbv.* 63 x86, // X86: i[3-9]86 64 x86_64, // X86-64: amd64, x86_64 65 xcore, // XCore: xcore 66 mblaze, // MBlaze: mblaze 67 ptx32, // PTX: ptx (32-bit) 68 ptx64, // PTX: ptx (64-bit) 69 70 InvalidArch 71 }; 72 enum VendorType { 73 UnknownVendor, 74 75 Apple, 76 PC, 77 SCEI 78 }; 79 enum OSType { 80 UnknownOS, 81 82 AuroraUX, 83 Cygwin, 84 Darwin, 85 DragonFly, 86 FreeBSD, 87 IOS, 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 }; 100 enum EnvironmentType { 101 UnknownEnvironment, 102 103 GNU, 104 GNUEABI, 105 EABI, 106 MachO 107 }; 108 109private: 110 std::string Data; 111 112 /// The parsed arch type (or InvalidArch if uninitialized). 113 mutable ArchType Arch; 114 115 /// The parsed vendor type. 116 mutable VendorType Vendor; 117 118 /// The parsed OS type. 119 mutable OSType OS; 120 121 /// The parsed Environment type. 122 mutable EnvironmentType Environment; 123 124 bool isInitialized() const { return Arch != InvalidArch; } 125 static ArchType ParseArch(StringRef ArchName); 126 static VendorType ParseVendor(StringRef VendorName); 127 static OSType ParseOS(StringRef OSName); 128 static EnvironmentType ParseEnvironment(StringRef EnvironmentName); 129 void Parse() const; 130 131public: 132 /// @name Constructors 133 /// @{ 134 135 Triple() : Data(), Arch(InvalidArch) {} 136 explicit Triple(StringRef Str) : Data(Str), Arch(InvalidArch) {} 137 explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr) 138 : Data(ArchStr), Arch(InvalidArch) { 139 Data += '-'; 140 Data += VendorStr; 141 Data += '-'; 142 Data += OSStr; 143 } 144 145 explicit Triple(StringRef ArchStr, StringRef VendorStr, StringRef OSStr, 146 StringRef EnvironmentStr) 147 : Data(ArchStr), Arch(InvalidArch) { 148 Data += '-'; 149 Data += VendorStr; 150 Data += '-'; 151 Data += OSStr; 152 Data += '-'; 153 Data += EnvironmentStr; 154 } 155 156 /// @} 157 /// @name Normalization 158 /// @{ 159 160 /// normalize - Turn an arbitrary machine specification into the canonical 161 /// triple form (or something sensible that the Triple class understands if 162 /// nothing better can reasonably be done). In particular, it handles the 163 /// common case in which otherwise valid components are in the wrong order. 164 static std::string normalize(StringRef Str); 165 166 /// @} 167 /// @name Typed Component Access 168 /// @{ 169 170 /// getArch - Get the parsed architecture type of this triple. 171 ArchType getArch() const { 172 if (!isInitialized()) Parse(); 173 return Arch; 174 } 175 176 /// getVendor - Get the parsed vendor type of this triple. 177 VendorType getVendor() const { 178 if (!isInitialized()) Parse(); 179 return Vendor; 180 } 181 182 /// getOS - Get the parsed operating system type of this triple. 183 OSType getOS() const { 184 if (!isInitialized()) Parse(); 185 return OS; 186 } 187 188 /// hasEnvironment - Does this triple have the optional environment 189 /// (fourth) component? 190 bool hasEnvironment() const { 191 return getEnvironmentName() != ""; 192 } 193 194 /// getEnvironment - Get the parsed environment type of this triple. 195 EnvironmentType getEnvironment() const { 196 if (!isInitialized()) Parse(); 197 return Environment; 198 } 199 200 /// @} 201 /// @name Direct Component Access 202 /// @{ 203 204 const std::string &str() const { return Data; } 205 206 const std::string &getTriple() const { return Data; } 207 208 /// getArchName - Get the architecture (first) component of the 209 /// triple. 210 StringRef getArchName() const; 211 212 /// getVendorName - Get the vendor (second) component of the triple. 213 StringRef getVendorName() const; 214 215 /// getOSName - Get the operating system (third) component of the 216 /// triple. 217 StringRef getOSName() const; 218 219 /// getEnvironmentName - Get the optional environment (fourth) 220 /// component of the triple, or "" if empty. 221 StringRef getEnvironmentName() const; 222 223 /// getOSAndEnvironmentName - Get the operating system and optional 224 /// environment components as a single string (separated by a '-' 225 /// if the environment component is present). 226 StringRef getOSAndEnvironmentName() const; 227 228 /// getOSNumber - Parse the version number from the OS name component of the 229 /// triple, if present. 230 /// 231 /// For example, "fooos1.2.3" would return (1, 2, 3). 232 /// 233 /// If an entry is not defined, it will be returned as 0. 234 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const; 235 236 /// getOSMajorVersion - Return just the major version number, this is 237 /// specialized because it is a common query. 238 unsigned getOSMajorVersion() const { 239 unsigned Maj, Min, Micro; 240 getDarwinNumber(Maj, Min, Micro); 241 return Maj; 242 } 243 244 void getDarwinNumber(unsigned &Major, unsigned &Minor, 245 unsigned &Micro) const { 246 return getOSVersion(Major, Minor, Micro); 247 } 248 249 unsigned getDarwinMajorNumber() const { 250 return getOSMajorVersion(); 251 } 252 253 /// isOSVersionLT - Helper function for doing comparisons against version 254 /// numbers included in the target triple. 255 bool isOSVersionLT(unsigned Major, unsigned Minor = 0, 256 unsigned Micro = 0) const { 257 unsigned LHS[3]; 258 getOSVersion(LHS[0], LHS[1], LHS[2]); 259 260 if (LHS[0] != Major) 261 return LHS[0] < Major; 262 if (LHS[1] != Minor) 263 return LHS[1] < Minor; 264 if (LHS[2] != Micro) 265 return LHS[1] < Micro; 266 267 return false; 268 } 269 270 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both 271 /// "darwin" and "osx" as OS X triples. 272 bool isMacOSX() const { 273 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX; 274 } 275 276 /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS). 277 bool isOSDarwin() const { 278 return isMacOSX() ||getOS() == Triple::IOS; 279 } 280 281 /// isOSWindows - Is this a "Windows" OS. 282 bool isOSWindows() const { 283 return getOS() == Triple::Win32 || getOS() == Triple::Cygwin || 284 getOS() == Triple::MinGW32; 285 } 286 287 /// isMacOSXVersionLT - Comparison function for checking OS X version 288 /// compatibility, which handles supporting skewed version numbering schemes 289 /// used by the "darwin" triples. 290 unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0, 291 unsigned Micro = 0) const { 292 assert(isMacOSX() && "Not an OS X triple!"); 293 294 // If this is OS X, expect a sane version number. 295 if (getOS() == Triple::MacOSX) 296 return isOSVersionLT(Major, Minor, Micro); 297 298 // Otherwise, compare to the "Darwin" number. 299 assert(Major == 10 && "Unexpected major version"); 300 return isOSVersionLT(Minor + 4, Micro, 0); 301 } 302 303 /// @} 304 /// @name Mutators 305 /// @{ 306 307 /// setArch - Set the architecture (first) component of the triple 308 /// to a known type. 309 void setArch(ArchType Kind); 310 311 /// setVendor - Set the vendor (second) component of the triple to a 312 /// known type. 313 void setVendor(VendorType Kind); 314 315 /// setOS - Set the operating system (third) component of the triple 316 /// to a known type. 317 void setOS(OSType Kind); 318 319 /// setEnvironment - Set the environment (fourth) component of the triple 320 /// to a known type. 321 void setEnvironment(EnvironmentType Kind); 322 323 /// setTriple - Set all components to the new triple \arg Str. 324 void setTriple(const Twine &Str); 325 326 /// setArchName - Set the architecture (first) component of the 327 /// triple by name. 328 void setArchName(StringRef Str); 329 330 /// setVendorName - Set the vendor (second) component of the triple 331 /// by name. 332 void setVendorName(StringRef Str); 333 334 /// setOSName - Set the operating system (third) component of the 335 /// triple by name. 336 void setOSName(StringRef Str); 337 338 /// setEnvironmentName - Set the optional environment (fourth) 339 /// component of the triple by name. 340 void setEnvironmentName(StringRef Str); 341 342 /// setOSAndEnvironmentName - Set the operating system and optional 343 /// environment components with a single string. 344 void setOSAndEnvironmentName(StringRef Str); 345 346 /// getArchNameForAssembler - Get an architecture name that is understood by 347 /// the target assembler. 348 const char *getArchNameForAssembler(); 349 350 /// @} 351 /// @name Static helpers for IDs. 352 /// @{ 353 354 /// getArchTypeName - Get the canonical name for the \arg Kind 355 /// architecture. 356 static const char *getArchTypeName(ArchType Kind); 357 358 /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind 359 /// architecture. This is the prefix used by the architecture specific 360 /// builtins, and is suitable for passing to \see 361 /// Intrinsic::getIntrinsicForGCCBuiltin(). 362 /// 363 /// \return - The architecture prefix, or 0 if none is defined. 364 static const char *getArchTypePrefix(ArchType Kind); 365 366 /// getVendorTypeName - Get the canonical name for the \arg Kind 367 /// vendor. 368 static const char *getVendorTypeName(VendorType Kind); 369 370 /// getOSTypeName - Get the canonical name for the \arg Kind operating 371 /// system. 372 static const char *getOSTypeName(OSType Kind); 373 374 /// getEnvironmentTypeName - Get the canonical name for the \arg Kind 375 /// environment. 376 static const char *getEnvironmentTypeName(EnvironmentType Kind); 377 378 /// @} 379 /// @name Static helpers for converting alternate architecture names. 380 /// @{ 381 382 /// getArchTypeForLLVMName - The canonical type for the given LLVM 383 /// architecture name (e.g., "x86"). 384 static ArchType getArchTypeForLLVMName(StringRef Str); 385 386 /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin" 387 /// architecture name, for example as accepted by "gcc -arch" (see also 388 /// arch(3)). 389 static ArchType getArchTypeForDarwinArchName(StringRef Str); 390 391 /// @} 392}; 393 394} // End llvm namespace 395 396 397#endif 398