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