1 //===--- Triple.cpp - Target triple helper class --------------------------===// 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 #include "llvm/ADT/Triple.h" 11 #include "llvm/ADT/STLExtras.h" 12 #include "llvm/ADT/SmallString.h" 13 #include "llvm/ADT/StringSwitch.h" 14 #include "llvm/Support/ErrorHandling.h" 15 #include <cstring> 16 using namespace llvm; 17 18 const char *Triple::getArchTypeName(ArchType Kind) { 19 switch (Kind) { 20 case UnknownArch: return "unknown"; 21 22 case aarch64: return "aarch64"; 23 case aarch64_be: return "aarch64_be"; 24 case arm: return "arm"; 25 case armeb: return "armeb"; 26 case arm64: return "arm64"; 27 case arm64_be: return "arm64_be"; 28 case hexagon: return "hexagon"; 29 case mips: return "mips"; 30 case mipsel: return "mipsel"; 31 case mips64: return "mips64"; 32 case mips64el: return "mips64el"; 33 case msp430: return "msp430"; 34 case ppc64: return "powerpc64"; 35 case ppc64le: return "powerpc64le"; 36 case ppc: return "powerpc"; 37 case r600: return "r600"; 38 case sparc: return "sparc"; 39 case sparcv9: return "sparcv9"; 40 case systemz: return "s390x"; 41 case tce: return "tce"; 42 case thumb: return "thumb"; 43 case thumbeb: return "thumbeb"; 44 case x86: return "i386"; 45 case x86_64: return "x86_64"; 46 case xcore: return "xcore"; 47 case nvptx: return "nvptx"; 48 case nvptx64: return "nvptx64"; 49 case le32: return "le32"; 50 case amdil: return "amdil"; 51 case spir: return "spir"; 52 case spir64: return "spir64"; 53 case kalimba: return "kalimba"; 54 } 55 56 llvm_unreachable("Invalid ArchType!"); 57 } 58 59 const char *Triple::getArchTypePrefix(ArchType Kind) { 60 switch (Kind) { 61 default: 62 return nullptr; 63 64 case arm64: 65 case arm64_be: 66 case aarch64: 67 case aarch64_be: return "aarch64"; 68 69 case arm: 70 case armeb: 71 case thumb: 72 case thumbeb: return "arm"; 73 74 case ppc64: 75 case ppc64le: 76 case ppc: return "ppc"; 77 78 case mips: 79 case mipsel: 80 case mips64: 81 case mips64el: return "mips"; 82 83 case hexagon: return "hexagon"; 84 85 case r600: return "r600"; 86 87 case sparcv9: 88 case sparc: return "sparc"; 89 90 case systemz: return "systemz"; 91 92 case x86: 93 case x86_64: return "x86"; 94 95 case xcore: return "xcore"; 96 97 case nvptx: return "nvptx"; 98 case nvptx64: return "nvptx"; 99 100 case le32: return "le32"; 101 case amdil: return "amdil"; 102 case spir: return "spir"; 103 case spir64: return "spir"; 104 case kalimba: return "kalimba"; 105 } 106 } 107 108 const char *Triple::getVendorTypeName(VendorType Kind) { 109 switch (Kind) { 110 case UnknownVendor: return "unknown"; 111 112 case Apple: return "apple"; 113 case PC: return "pc"; 114 case SCEI: return "scei"; 115 case BGP: return "bgp"; 116 case BGQ: return "bgq"; 117 case Freescale: return "fsl"; 118 case IBM: return "ibm"; 119 case ImaginationTechnologies: return "img"; 120 case MipsTechnologies: return "mti"; 121 case NVIDIA: return "nvidia"; 122 case CSR: return "csr"; 123 } 124 125 llvm_unreachable("Invalid VendorType!"); 126 } 127 128 const char *Triple::getOSTypeName(OSType Kind) { 129 switch (Kind) { 130 case UnknownOS: return "unknown"; 131 132 case AuroraUX: return "auroraux"; 133 case Cygwin: return "cygwin"; 134 case Darwin: return "darwin"; 135 case DragonFly: return "dragonfly"; 136 case FreeBSD: return "freebsd"; 137 case IOS: return "ios"; 138 case KFreeBSD: return "kfreebsd"; 139 case Linux: return "linux"; 140 case Lv2: return "lv2"; 141 case MacOSX: return "macosx"; 142 case MinGW32: return "mingw32"; 143 case NetBSD: return "netbsd"; 144 case OpenBSD: return "openbsd"; 145 case Solaris: return "solaris"; 146 case Win32: return "windows"; 147 case Haiku: return "haiku"; 148 case Minix: return "minix"; 149 case RTEMS: return "rtems"; 150 case NaCl: return "nacl"; 151 case CNK: return "cnk"; 152 case Bitrig: return "bitrig"; 153 case AIX: return "aix"; 154 case CUDA: return "cuda"; 155 case NVCL: return "nvcl"; 156 } 157 158 llvm_unreachable("Invalid OSType"); 159 } 160 161 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) { 162 switch (Kind) { 163 case UnknownEnvironment: return "unknown"; 164 case GNU: return "gnu"; 165 case GNUEABIHF: return "gnueabihf"; 166 case GNUEABI: return "gnueabi"; 167 case GNUX32: return "gnux32"; 168 case CODE16: return "code16"; 169 case EABI: return "eabi"; 170 case EABIHF: return "eabihf"; 171 case Android: return "android"; 172 case MSVC: return "msvc"; 173 case Itanium: return "itanium"; 174 case Cygnus: return "cygnus"; 175 } 176 177 llvm_unreachable("Invalid EnvironmentType!"); 178 } 179 180 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) { 181 return StringSwitch<Triple::ArchType>(Name) 182 .Case("aarch64", aarch64) 183 .Case("aarch64_be", aarch64_be) 184 .Case("arm", arm) 185 .Case("armeb", armeb) 186 .Case("arm64", arm64) 187 .Case("arm64_be", arm64_be) 188 .Case("mips", mips) 189 .Case("mipsel", mipsel) 190 .Case("mips64", mips64) 191 .Case("mips64el", mips64el) 192 .Case("msp430", msp430) 193 .Case("ppc64", ppc64) 194 .Case("ppc32", ppc) 195 .Case("ppc", ppc) 196 .Case("ppc64le", ppc64le) 197 .Case("r600", r600) 198 .Case("hexagon", hexagon) 199 .Case("sparc", sparc) 200 .Case("sparcv9", sparcv9) 201 .Case("systemz", systemz) 202 .Case("tce", tce) 203 .Case("thumb", thumb) 204 .Case("thumbeb", thumbeb) 205 .Case("x86", x86) 206 .Case("x86-64", x86_64) 207 .Case("xcore", xcore) 208 .Case("nvptx", nvptx) 209 .Case("nvptx64", nvptx64) 210 .Case("le32", le32) 211 .Case("amdil", amdil) 212 .Case("spir", spir) 213 .Case("spir64", spir64) 214 .Case("kalimba", kalimba) 215 .Default(UnknownArch); 216 } 217 218 // Returns architecture name that is understood by the target assembler. 219 const char *Triple::getArchNameForAssembler() { 220 if (!isOSDarwin() && getVendor() != Triple::Apple) 221 return nullptr; 222 223 return StringSwitch<const char*>(getArchName()) 224 .Case("i386", "i386") 225 .Case("x86_64", "x86_64") 226 .Case("powerpc", "ppc") 227 .Case("powerpc64", "ppc64") 228 .Case("powerpc64le", "ppc64le") 229 .Case("arm", "arm") 230 .Cases("armv4t", "thumbv4t", "armv4t") 231 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5") 232 .Cases("armv6", "thumbv6", "armv6") 233 .Cases("armv7", "thumbv7", "armv7") 234 .Case("armeb", "armeb") 235 .Case("arm64", "arm64") 236 .Case("arm64_be", "arm64") 237 .Case("r600", "r600") 238 .Case("nvptx", "nvptx") 239 .Case("nvptx64", "nvptx64") 240 .Case("le32", "le32") 241 .Case("amdil", "amdil") 242 .Case("spir", "spir") 243 .Case("spir64", "spir64") 244 .Default(nullptr); 245 } 246 247 static Triple::ArchType parseArch(StringRef ArchName) { 248 return StringSwitch<Triple::ArchType>(ArchName) 249 .Cases("i386", "i486", "i586", "i686", Triple::x86) 250 // FIXME: Do we need to support these? 251 .Cases("i786", "i886", "i986", Triple::x86) 252 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64) 253 .Case("powerpc", Triple::ppc) 254 .Cases("powerpc64", "ppu", Triple::ppc64) 255 .Case("powerpc64le", Triple::ppc64le) 256 .Case("aarch64", Triple::aarch64) 257 .Case("aarch64_be", Triple::aarch64_be) 258 .Cases("arm", "xscale", Triple::arm) 259 // FIXME: It would be good to replace these with explicit names for all the 260 // various suffixes supported. 261 .StartsWith("armv", Triple::arm) 262 .Case("armeb", Triple::armeb) 263 .StartsWith("armebv", Triple::armeb) 264 .Case("thumb", Triple::thumb) 265 .StartsWith("thumbv", Triple::thumb) 266 .Case("thumbeb", Triple::thumbeb) 267 .StartsWith("thumbebv", Triple::thumbeb) 268 .Case("arm64", Triple::arm64) 269 .Case("arm64_be", Triple::arm64_be) 270 .Case("msp430", Triple::msp430) 271 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips) 272 .Cases("mipsel", "mipsallegrexel", Triple::mipsel) 273 .Cases("mips64", "mips64eb", Triple::mips64) 274 .Case("mips64el", Triple::mips64el) 275 .Case("r600", Triple::r600) 276 .Case("hexagon", Triple::hexagon) 277 .Case("s390x", Triple::systemz) 278 .Case("sparc", Triple::sparc) 279 .Cases("sparcv9", "sparc64", Triple::sparcv9) 280 .Case("tce", Triple::tce) 281 .Case("xcore", Triple::xcore) 282 .Case("nvptx", Triple::nvptx) 283 .Case("nvptx64", Triple::nvptx64) 284 .Case("le32", Triple::le32) 285 .Case("amdil", Triple::amdil) 286 .Case("spir", Triple::spir) 287 .Case("spir64", Triple::spir64) 288 .Case("kalimba", Triple::kalimba) 289 .Default(Triple::UnknownArch); 290 } 291 292 static Triple::VendorType parseVendor(StringRef VendorName) { 293 return StringSwitch<Triple::VendorType>(VendorName) 294 .Case("apple", Triple::Apple) 295 .Case("pc", Triple::PC) 296 .Case("scei", Triple::SCEI) 297 .Case("bgp", Triple::BGP) 298 .Case("bgq", Triple::BGQ) 299 .Case("fsl", Triple::Freescale) 300 .Case("ibm", Triple::IBM) 301 .Case("img", Triple::ImaginationTechnologies) 302 .Case("mti", Triple::MipsTechnologies) 303 .Case("nvidia", Triple::NVIDIA) 304 .Case("csr", Triple::CSR) 305 .Default(Triple::UnknownVendor); 306 } 307 308 static Triple::OSType parseOS(StringRef OSName) { 309 return StringSwitch<Triple::OSType>(OSName) 310 .StartsWith("auroraux", Triple::AuroraUX) 311 .StartsWith("cygwin", Triple::Cygwin) 312 .StartsWith("darwin", Triple::Darwin) 313 .StartsWith("dragonfly", Triple::DragonFly) 314 .StartsWith("freebsd", Triple::FreeBSD) 315 .StartsWith("ios", Triple::IOS) 316 .StartsWith("kfreebsd", Triple::KFreeBSD) 317 .StartsWith("linux", Triple::Linux) 318 .StartsWith("lv2", Triple::Lv2) 319 .StartsWith("macosx", Triple::MacOSX) 320 .StartsWith("mingw32", Triple::MinGW32) 321 .StartsWith("netbsd", Triple::NetBSD) 322 .StartsWith("openbsd", Triple::OpenBSD) 323 .StartsWith("solaris", Triple::Solaris) 324 .StartsWith("win32", Triple::Win32) 325 .StartsWith("windows", Triple::Win32) 326 .StartsWith("haiku", Triple::Haiku) 327 .StartsWith("minix", Triple::Minix) 328 .StartsWith("rtems", Triple::RTEMS) 329 .StartsWith("nacl", Triple::NaCl) 330 .StartsWith("cnk", Triple::CNK) 331 .StartsWith("bitrig", Triple::Bitrig) 332 .StartsWith("aix", Triple::AIX) 333 .StartsWith("cuda", Triple::CUDA) 334 .StartsWith("nvcl", Triple::NVCL) 335 .Default(Triple::UnknownOS); 336 } 337 338 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) { 339 return StringSwitch<Triple::EnvironmentType>(EnvironmentName) 340 .StartsWith("eabihf", Triple::EABIHF) 341 .StartsWith("eabi", Triple::EABI) 342 .StartsWith("gnueabihf", Triple::GNUEABIHF) 343 .StartsWith("gnueabi", Triple::GNUEABI) 344 .StartsWith("gnux32", Triple::GNUX32) 345 .StartsWith("code16", Triple::CODE16) 346 .StartsWith("gnu", Triple::GNU) 347 .StartsWith("android", Triple::Android) 348 .StartsWith("msvc", Triple::MSVC) 349 .StartsWith("itanium", Triple::Itanium) 350 .StartsWith("cygnus", Triple::Cygnus) 351 .Default(Triple::UnknownEnvironment); 352 } 353 354 static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) { 355 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName) 356 .EndsWith("coff", Triple::COFF) 357 .EndsWith("elf", Triple::ELF) 358 .EndsWith("macho", Triple::MachO) 359 .Default(Triple::UnknownObjectFormat); 360 } 361 362 static Triple::SubArchType parseSubArch(StringRef SubArchName) { 363 return StringSwitch<Triple::SubArchType>(SubArchName) 364 .EndsWith("v8", Triple::ARMSubArch_v8) 365 .EndsWith("v8a", Triple::ARMSubArch_v8) 366 .EndsWith("v7", Triple::ARMSubArch_v7) 367 .EndsWith("v7a", Triple::ARMSubArch_v7) 368 .EndsWith("v7em", Triple::ARMSubArch_v7em) 369 .EndsWith("v7l", Triple::ARMSubArch_v7) 370 .EndsWith("v7m", Triple::ARMSubArch_v7m) 371 .EndsWith("v7r", Triple::ARMSubArch_v7) 372 .EndsWith("v7s", Triple::ARMSubArch_v7s) 373 .EndsWith("v6", Triple::ARMSubArch_v6) 374 .EndsWith("v6m", Triple::ARMSubArch_v6m) 375 .EndsWith("v6t2", Triple::ARMSubArch_v6t2) 376 .EndsWith("v5", Triple::ARMSubArch_v5) 377 .EndsWith("v5e", Triple::ARMSubArch_v5) 378 .EndsWith("v5t", Triple::ARMSubArch_v5) 379 .EndsWith("v5te", Triple::ARMSubArch_v5te) 380 .EndsWith("v4t", Triple::ARMSubArch_v4t) 381 .Default(Triple::NoSubArch); 382 } 383 384 static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) { 385 switch (Kind) { 386 case Triple::UnknownObjectFormat: return ""; 387 case Triple::COFF: return "coff"; 388 case Triple::ELF: return "elf"; 389 case Triple::MachO: return "macho"; 390 } 391 llvm_unreachable("unknown object format type"); 392 } 393 394 static Triple::ObjectFormatType getDefaultFormat(const Triple &T) { 395 if (T.isOSDarwin()) 396 return Triple::MachO; 397 else if (T.isOSWindows()) 398 return Triple::COFF; 399 return Triple::ELF; 400 } 401 402 /// \brief Construct a triple from the string representation provided. 403 /// 404 /// This stores the string representation and parses the various pieces into 405 /// enum members. 406 Triple::Triple(const Twine &Str) 407 : Data(Str.str()), 408 Arch(parseArch(getArchName())), 409 SubArch(parseSubArch(getArchName())), 410 Vendor(parseVendor(getVendorName())), 411 OS(parseOS(getOSName())), 412 Environment(parseEnvironment(getEnvironmentName())), 413 ObjectFormat(parseFormat(getEnvironmentName())) { 414 if (ObjectFormat == Triple::UnknownObjectFormat) 415 ObjectFormat = getDefaultFormat(*this); 416 } 417 418 /// \brief Construct a triple from string representations of the architecture, 419 /// vendor, and OS. 420 /// 421 /// This joins each argument into a canonical string representation and parses 422 /// them into enum members. It leaves the environment unknown and omits it from 423 /// the string representation. 424 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr) 425 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()), 426 Arch(parseArch(ArchStr.str())), 427 SubArch(parseSubArch(ArchStr.str())), 428 Vendor(parseVendor(VendorStr.str())), 429 OS(parseOS(OSStr.str())), 430 Environment(), ObjectFormat(Triple::UnknownObjectFormat) { 431 ObjectFormat = getDefaultFormat(*this); 432 } 433 434 /// \brief Construct a triple from string representations of the architecture, 435 /// vendor, OS, and environment. 436 /// 437 /// This joins each argument into a canonical string representation and parses 438 /// them into enum members. 439 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr, 440 const Twine &EnvironmentStr) 441 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') + 442 EnvironmentStr).str()), 443 Arch(parseArch(ArchStr.str())), 444 SubArch(parseSubArch(ArchStr.str())), 445 Vendor(parseVendor(VendorStr.str())), 446 OS(parseOS(OSStr.str())), 447 Environment(parseEnvironment(EnvironmentStr.str())), 448 ObjectFormat(parseFormat(EnvironmentStr.str())) { 449 if (ObjectFormat == Triple::UnknownObjectFormat) 450 ObjectFormat = getDefaultFormat(*this); 451 } 452 453 std::string Triple::normalize(StringRef Str) { 454 // Parse into components. 455 SmallVector<StringRef, 4> Components; 456 Str.split(Components, "-"); 457 458 // If the first component corresponds to a known architecture, preferentially 459 // use it for the architecture. If the second component corresponds to a 460 // known vendor, preferentially use it for the vendor, etc. This avoids silly 461 // component movement when a component parses as (eg) both a valid arch and a 462 // valid os. 463 ArchType Arch = UnknownArch; 464 if (Components.size() > 0) 465 Arch = parseArch(Components[0]); 466 VendorType Vendor = UnknownVendor; 467 if (Components.size() > 1) 468 Vendor = parseVendor(Components[1]); 469 OSType OS = UnknownOS; 470 if (Components.size() > 2) 471 OS = parseOS(Components[2]); 472 EnvironmentType Environment = UnknownEnvironment; 473 if (Components.size() > 3) 474 Environment = parseEnvironment(Components[3]); 475 ObjectFormatType ObjectFormat = UnknownObjectFormat; 476 if (Components.size() > 4) 477 ObjectFormat = parseFormat(Components[4]); 478 479 // Note which components are already in their final position. These will not 480 // be moved. 481 bool Found[4]; 482 Found[0] = Arch != UnknownArch; 483 Found[1] = Vendor != UnknownVendor; 484 Found[2] = OS != UnknownOS; 485 Found[3] = Environment != UnknownEnvironment; 486 487 // If they are not there already, permute the components into their canonical 488 // positions by seeing if they parse as a valid architecture, and if so moving 489 // the component to the architecture position etc. 490 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) { 491 if (Found[Pos]) 492 continue; // Already in the canonical position. 493 494 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) { 495 // Do not reparse any components that already matched. 496 if (Idx < array_lengthof(Found) && Found[Idx]) 497 continue; 498 499 // Does this component parse as valid for the target position? 500 bool Valid = false; 501 StringRef Comp = Components[Idx]; 502 switch (Pos) { 503 default: llvm_unreachable("unexpected component type!"); 504 case 0: 505 Arch = parseArch(Comp); 506 Valid = Arch != UnknownArch; 507 break; 508 case 1: 509 Vendor = parseVendor(Comp); 510 Valid = Vendor != UnknownVendor; 511 break; 512 case 2: 513 OS = parseOS(Comp); 514 Valid = OS != UnknownOS; 515 break; 516 case 3: 517 Environment = parseEnvironment(Comp); 518 Valid = Environment != UnknownEnvironment; 519 if (!Valid) { 520 ObjectFormat = parseFormat(Comp); 521 Valid = ObjectFormat != UnknownObjectFormat; 522 } 523 break; 524 } 525 if (!Valid) 526 continue; // Nope, try the next component. 527 528 // Move the component to the target position, pushing any non-fixed 529 // components that are in the way to the right. This tends to give 530 // good results in the common cases of a forgotten vendor component 531 // or a wrongly positioned environment. 532 if (Pos < Idx) { 533 // Insert left, pushing the existing components to the right. For 534 // example, a-b-i386 -> i386-a-b when moving i386 to the front. 535 StringRef CurrentComponent(""); // The empty component. 536 // Replace the component we are moving with an empty component. 537 std::swap(CurrentComponent, Components[Idx]); 538 // Insert the component being moved at Pos, displacing any existing 539 // components to the right. 540 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) { 541 // Skip over any fixed components. 542 while (i < array_lengthof(Found) && Found[i]) 543 ++i; 544 // Place the component at the new position, getting the component 545 // that was at this position - it will be moved right. 546 std::swap(CurrentComponent, Components[i]); 547 } 548 } else if (Pos > Idx) { 549 // Push right by inserting empty components until the component at Idx 550 // reaches the target position Pos. For example, pc-a -> -pc-a when 551 // moving pc to the second position. 552 do { 553 // Insert one empty component at Idx. 554 StringRef CurrentComponent(""); // The empty component. 555 for (unsigned i = Idx; i < Components.size();) { 556 // Place the component at the new position, getting the component 557 // that was at this position - it will be moved right. 558 std::swap(CurrentComponent, Components[i]); 559 // If it was placed on top of an empty component then we are done. 560 if (CurrentComponent.empty()) 561 break; 562 // Advance to the next component, skipping any fixed components. 563 while (++i < array_lengthof(Found) && Found[i]) 564 ; 565 } 566 // The last component was pushed off the end - append it. 567 if (!CurrentComponent.empty()) 568 Components.push_back(CurrentComponent); 569 570 // Advance Idx to the component's new position. 571 while (++Idx < array_lengthof(Found) && Found[Idx]) 572 ; 573 } while (Idx < Pos); // Add more until the final position is reached. 574 } 575 assert(Pos < Components.size() && Components[Pos] == Comp && 576 "Component moved wrong!"); 577 Found[Pos] = true; 578 break; 579 } 580 } 581 582 // Special case logic goes here. At this point Arch, Vendor and OS have the 583 // correct values for the computed components. 584 585 if (OS == Triple::Win32) { 586 Components.resize(4); 587 Components[2] = "windows"; 588 if (Environment == UnknownEnvironment) { 589 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF) 590 Components[3] = "msvc"; 591 else 592 Components[3] = getObjectFormatTypeName(ObjectFormat); 593 } 594 } else if (OS == Triple::MinGW32) { 595 Components.resize(4); 596 Components[2] = "windows"; 597 Components[3] = "gnu"; 598 } else if (OS == Triple::Cygwin) { 599 Components.resize(4); 600 Components[2] = "windows"; 601 Components[3] = "cygnus"; 602 } 603 if (OS == Triple::MinGW32 || OS == Triple::Cygwin || 604 (OS == Triple::Win32 && Environment != UnknownEnvironment)) { 605 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) { 606 Components.resize(5); 607 Components[4] = getObjectFormatTypeName(ObjectFormat); 608 } 609 } 610 611 // Stick the corrected components back together to form the normalized string. 612 std::string Normalized; 613 for (unsigned i = 0, e = Components.size(); i != e; ++i) { 614 if (i) Normalized += '-'; 615 Normalized += Components[i]; 616 } 617 return Normalized; 618 } 619 620 StringRef Triple::getArchName() const { 621 return StringRef(Data).split('-').first; // Isolate first component 622 } 623 624 StringRef Triple::getVendorName() const { 625 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component 626 return Tmp.split('-').first; // Isolate second component 627 } 628 629 StringRef Triple::getOSName() const { 630 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component 631 Tmp = Tmp.split('-').second; // Strip second component 632 return Tmp.split('-').first; // Isolate third component 633 } 634 635 StringRef Triple::getEnvironmentName() const { 636 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component 637 Tmp = Tmp.split('-').second; // Strip second component 638 return Tmp.split('-').second; // Strip third component 639 } 640 641 StringRef Triple::getOSAndEnvironmentName() const { 642 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component 643 return Tmp.split('-').second; // Strip second component 644 } 645 646 static unsigned EatNumber(StringRef &Str) { 647 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number"); 648 unsigned Result = 0; 649 650 do { 651 // Consume the leading digit. 652 Result = Result*10 + (Str[0] - '0'); 653 654 // Eat the digit. 655 Str = Str.substr(1); 656 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9'); 657 658 return Result; 659 } 660 661 void Triple::getOSVersion(unsigned &Major, unsigned &Minor, 662 unsigned &Micro) const { 663 StringRef OSName = getOSName(); 664 665 // Assume that the OS portion of the triple starts with the canonical name. 666 StringRef OSTypeName = getOSTypeName(getOS()); 667 if (OSName.startswith(OSTypeName)) 668 OSName = OSName.substr(OSTypeName.size()); 669 670 // Any unset version defaults to 0. 671 Major = Minor = Micro = 0; 672 673 // Parse up to three components. 674 unsigned *Components[3] = { &Major, &Minor, &Micro }; 675 for (unsigned i = 0; i != 3; ++i) { 676 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9') 677 break; 678 679 // Consume the leading number. 680 *Components[i] = EatNumber(OSName); 681 682 // Consume the separator, if present. 683 if (OSName.startswith(".")) 684 OSName = OSName.substr(1); 685 } 686 } 687 688 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor, 689 unsigned &Micro) const { 690 getOSVersion(Major, Minor, Micro); 691 692 switch (getOS()) { 693 default: llvm_unreachable("unexpected OS for Darwin triple"); 694 case Darwin: 695 // Default to darwin8, i.e., MacOSX 10.4. 696 if (Major == 0) 697 Major = 8; 698 // Darwin version numbers are skewed from OS X versions. 699 if (Major < 4) 700 return false; 701 Micro = 0; 702 Minor = Major - 4; 703 Major = 10; 704 break; 705 case MacOSX: 706 // Default to 10.4. 707 if (Major == 0) { 708 Major = 10; 709 Minor = 4; 710 } 711 if (Major != 10) 712 return false; 713 break; 714 case IOS: 715 // Ignore the version from the triple. This is only handled because the 716 // the clang driver combines OS X and IOS support into a common Darwin 717 // toolchain that wants to know the OS X version number even when targeting 718 // IOS. 719 Major = 10; 720 Minor = 4; 721 Micro = 0; 722 break; 723 } 724 return true; 725 } 726 727 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor, 728 unsigned &Micro) const { 729 switch (getOS()) { 730 default: llvm_unreachable("unexpected OS for Darwin triple"); 731 case Darwin: 732 case MacOSX: 733 // Ignore the version from the triple. This is only handled because the 734 // the clang driver combines OS X and IOS support into a common Darwin 735 // toolchain that wants to know the iOS version number even when targeting 736 // OS X. 737 Major = 5; 738 Minor = 0; 739 Micro = 0; 740 break; 741 case IOS: 742 getOSVersion(Major, Minor, Micro); 743 // Default to 5.0 (or 7.0 for arm64). 744 if (Major == 0) 745 Major = (getArch() == arm64) ? 7 : 5; 746 break; 747 } 748 } 749 750 void Triple::setTriple(const Twine &Str) { 751 *this = Triple(Str); 752 } 753 754 void Triple::setArch(ArchType Kind) { 755 setArchName(getArchTypeName(Kind)); 756 } 757 758 void Triple::setVendor(VendorType Kind) { 759 setVendorName(getVendorTypeName(Kind)); 760 } 761 762 void Triple::setOS(OSType Kind) { 763 setOSName(getOSTypeName(Kind)); 764 } 765 766 void Triple::setEnvironment(EnvironmentType Kind) { 767 setEnvironmentName(getEnvironmentTypeName(Kind)); 768 } 769 770 void Triple::setObjectFormat(ObjectFormatType Kind) { 771 if (Environment == UnknownEnvironment) 772 return setEnvironmentName(getObjectFormatTypeName(Kind)); 773 774 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") + 775 getObjectFormatTypeName(Kind)).str()); 776 } 777 778 void Triple::setArchName(StringRef Str) { 779 // Work around a miscompilation bug for Twines in gcc 4.0.3. 780 SmallString<64> Triple; 781 Triple += Str; 782 Triple += "-"; 783 Triple += getVendorName(); 784 Triple += "-"; 785 Triple += getOSAndEnvironmentName(); 786 setTriple(Triple.str()); 787 } 788 789 void Triple::setVendorName(StringRef Str) { 790 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName()); 791 } 792 793 void Triple::setOSName(StringRef Str) { 794 if (hasEnvironment()) 795 setTriple(getArchName() + "-" + getVendorName() + "-" + Str + 796 "-" + getEnvironmentName()); 797 else 798 setTriple(getArchName() + "-" + getVendorName() + "-" + Str); 799 } 800 801 void Triple::setEnvironmentName(StringRef Str) { 802 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() + 803 "-" + Str); 804 } 805 806 void Triple::setOSAndEnvironmentName(StringRef Str) { 807 setTriple(getArchName() + "-" + getVendorName() + "-" + Str); 808 } 809 810 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) { 811 switch (Arch) { 812 case llvm::Triple::UnknownArch: 813 return 0; 814 815 case llvm::Triple::msp430: 816 return 16; 817 818 case llvm::Triple::amdil: 819 case llvm::Triple::arm: 820 case llvm::Triple::armeb: 821 case llvm::Triple::hexagon: 822 case llvm::Triple::le32: 823 case llvm::Triple::mips: 824 case llvm::Triple::mipsel: 825 case llvm::Triple::nvptx: 826 case llvm::Triple::ppc: 827 case llvm::Triple::r600: 828 case llvm::Triple::sparc: 829 case llvm::Triple::tce: 830 case llvm::Triple::thumb: 831 case llvm::Triple::thumbeb: 832 case llvm::Triple::x86: 833 case llvm::Triple::xcore: 834 case llvm::Triple::spir: 835 case llvm::Triple::kalimba: 836 return 32; 837 838 case llvm::Triple::arm64: 839 case llvm::Triple::arm64_be: 840 case llvm::Triple::aarch64: 841 case llvm::Triple::aarch64_be: 842 case llvm::Triple::mips64: 843 case llvm::Triple::mips64el: 844 case llvm::Triple::nvptx64: 845 case llvm::Triple::ppc64: 846 case llvm::Triple::ppc64le: 847 case llvm::Triple::sparcv9: 848 case llvm::Triple::systemz: 849 case llvm::Triple::x86_64: 850 case llvm::Triple::spir64: 851 return 64; 852 } 853 llvm_unreachable("Invalid architecture value"); 854 } 855 856 bool Triple::isArch64Bit() const { 857 return getArchPointerBitWidth(getArch()) == 64; 858 } 859 860 bool Triple::isArch32Bit() const { 861 return getArchPointerBitWidth(getArch()) == 32; 862 } 863 864 bool Triple::isArch16Bit() const { 865 return getArchPointerBitWidth(getArch()) == 16; 866 } 867 868 Triple Triple::get32BitArchVariant() const { 869 Triple T(*this); 870 switch (getArch()) { 871 case Triple::UnknownArch: 872 case Triple::aarch64: 873 case Triple::aarch64_be: 874 case Triple::arm64: 875 case Triple::arm64_be: 876 case Triple::msp430: 877 case Triple::systemz: 878 case Triple::ppc64le: 879 T.setArch(UnknownArch); 880 break; 881 882 case Triple::amdil: 883 case Triple::spir: 884 case Triple::arm: 885 case Triple::armeb: 886 case Triple::hexagon: 887 case Triple::kalimba: 888 case Triple::le32: 889 case Triple::mips: 890 case Triple::mipsel: 891 case Triple::nvptx: 892 case Triple::ppc: 893 case Triple::r600: 894 case Triple::sparc: 895 case Triple::tce: 896 case Triple::thumb: 897 case Triple::thumbeb: 898 case Triple::x86: 899 case Triple::xcore: 900 // Already 32-bit. 901 break; 902 903 case Triple::mips64: T.setArch(Triple::mips); break; 904 case Triple::mips64el: T.setArch(Triple::mipsel); break; 905 case Triple::nvptx64: T.setArch(Triple::nvptx); break; 906 case Triple::ppc64: T.setArch(Triple::ppc); break; 907 case Triple::sparcv9: T.setArch(Triple::sparc); break; 908 case Triple::x86_64: T.setArch(Triple::x86); break; 909 case Triple::spir64: T.setArch(Triple::spir); break; 910 } 911 return T; 912 } 913 914 Triple Triple::get64BitArchVariant() const { 915 Triple T(*this); 916 switch (getArch()) { 917 case Triple::UnknownArch: 918 case Triple::amdil: 919 case Triple::arm: 920 case Triple::armeb: 921 case Triple::hexagon: 922 case Triple::kalimba: 923 case Triple::le32: 924 case Triple::msp430: 925 case Triple::r600: 926 case Triple::tce: 927 case Triple::thumb: 928 case Triple::thumbeb: 929 case Triple::xcore: 930 T.setArch(UnknownArch); 931 break; 932 933 case Triple::aarch64: 934 case Triple::aarch64_be: 935 case Triple::spir64: 936 case Triple::mips64: 937 case Triple::mips64el: 938 case Triple::nvptx64: 939 case Triple::ppc64: 940 case Triple::ppc64le: 941 case Triple::sparcv9: 942 case Triple::systemz: 943 case Triple::x86_64: 944 case Triple::arm64: 945 case Triple::arm64_be: 946 // Already 64-bit. 947 break; 948 949 case Triple::mips: T.setArch(Triple::mips64); break; 950 case Triple::mipsel: T.setArch(Triple::mips64el); break; 951 case Triple::nvptx: T.setArch(Triple::nvptx64); break; 952 case Triple::ppc: T.setArch(Triple::ppc64); break; 953 case Triple::sparc: T.setArch(Triple::sparcv9); break; 954 case Triple::x86: T.setArch(Triple::x86_64); break; 955 case Triple::spir: T.setArch(Triple::spir64); break; 956 } 957 return T; 958 } 959 960 // FIXME: tblgen this. 961 const char *Triple::getARMCPUForArch(StringRef MArch) const { 962 if (MArch.empty()) 963 MArch = getArchName(); 964 965 switch (getOS()) { 966 case llvm::Triple::FreeBSD: 967 case llvm::Triple::NetBSD: 968 if (MArch == "armv6") 969 return "arm1176jzf-s"; 970 break; 971 case llvm::Triple::Win32: 972 // FIXME: this is invalid for WindowsCE 973 return "cortex-a9"; 974 default: 975 break; 976 } 977 978 const char *result = nullptr; 979 size_t offset = StringRef::npos; 980 if (MArch.startswith("arm")) 981 offset = 3; 982 if (MArch.startswith("thumb")) 983 offset = 5; 984 if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb") 985 offset += 2; 986 if (offset != StringRef::npos) 987 result = llvm::StringSwitch<const char *>(MArch.substr(offset)) 988 .Cases("v2", "v2a", "arm2") 989 .Case("v3", "arm6") 990 .Case("v3m", "arm7m") 991 .Case("v4", "strongarm") 992 .Case("v4t", "arm7tdmi") 993 .Cases("v5", "v5t", "arm10tdmi") 994 .Cases("v5e", "v5te", "arm1022e") 995 .Case("v5tej", "arm926ej-s") 996 .Cases("v6", "v6k", "arm1136jf-s") 997 .Case("v6j", "arm1136j-s") 998 .Cases("v6z", "v6zk", "arm1176jzf-s") 999 .Case("v6t2", "arm1156t2-s") 1000 .Cases("v6m", "v6-m", "cortex-m0") 1001 .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8") 1002 .Cases("v7s", "v7-s", "swift") 1003 .Cases("v7r", "v7-r", "cortex-r4") 1004 .Cases("v7m", "v7-m", "cortex-m3") 1005 .Cases("v7em", "v7e-m", "cortex-m4") 1006 .Cases("v8", "v8a", "v8-a", "cortex-a53") 1007 .Default(nullptr); 1008 else 1009 result = llvm::StringSwitch<const char *>(MArch) 1010 .Case("ep9312", "ep9312") 1011 .Case("iwmmxt", "iwmmxt") 1012 .Case("xscale", "xscale") 1013 .Default(nullptr); 1014 1015 if (result) 1016 return result; 1017 1018 // If all else failed, return the most base CPU with thumb interworking 1019 // supported by LLVM. 1020 // FIXME: Should warn once that we're falling back. 1021 switch (getOS()) { 1022 case llvm::Triple::NetBSD: 1023 switch (getEnvironment()) { 1024 case llvm::Triple::GNUEABIHF: 1025 case llvm::Triple::GNUEABI: 1026 case llvm::Triple::EABIHF: 1027 case llvm::Triple::EABI: 1028 return "arm926ej-s"; 1029 default: 1030 return "strongarm"; 1031 } 1032 default: 1033 switch (getEnvironment()) { 1034 case llvm::Triple::EABIHF: 1035 case llvm::Triple::GNUEABIHF: 1036 return "arm1176jzf-s"; 1037 default: 1038 return "arm7tdmi"; 1039 } 1040 } 1041 } 1042