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/SmallString.h" 12 #include "llvm/ADT/STLExtras.h" 13 #include <cstring> 14 using namespace llvm; 15 16 const char *Triple::getArchTypeName(ArchType Kind) { 17 switch (Kind) { 18 case InvalidArch: return "<invalid>"; 19 case UnknownArch: return "unknown"; 20 21 case arm: return "arm"; 22 case cellspu: return "cellspu"; 23 case hexagon: return "hexagon"; 24 case mips: return "mips"; 25 case mipsel: return "mipsel"; 26 case mips64: return "mips64"; 27 case mips64el:return "mips64el"; 28 case msp430: return "msp430"; 29 case ppc64: return "powerpc64"; 30 case ppc: return "powerpc"; 31 case sparc: return "sparc"; 32 case sparcv9: return "sparcv9"; 33 case tce: return "tce"; 34 case thumb: return "thumb"; 35 case x86: return "i386"; 36 case x86_64: return "x86_64"; 37 case xcore: return "xcore"; 38 case mblaze: return "mblaze"; 39 case ptx32: return "ptx32"; 40 case ptx64: return "ptx64"; 41 case le32: return "le32"; 42 case amdil: return "amdil"; 43 } 44 45 return "<invalid>"; 46 } 47 48 const char *Triple::getArchTypePrefix(ArchType Kind) { 49 switch (Kind) { 50 default: 51 return 0; 52 53 case arm: 54 case thumb: return "arm"; 55 56 case cellspu: return "spu"; 57 58 case ppc64: 59 case ppc: return "ppc"; 60 61 case mblaze: return "mblaze"; 62 63 case hexagon: return "hexagon"; 64 65 case sparcv9: 66 case sparc: return "sparc"; 67 68 case x86: 69 case x86_64: return "x86"; 70 71 case xcore: return "xcore"; 72 73 case ptx32: return "ptx"; 74 case ptx64: return "ptx"; 75 case le32: return "le32"; 76 case amdil: return "amdil"; 77 } 78 } 79 80 const char *Triple::getVendorTypeName(VendorType Kind) { 81 switch (Kind) { 82 case UnknownVendor: return "unknown"; 83 84 case Apple: return "apple"; 85 case PC: return "pc"; 86 case SCEI: return "scei"; 87 } 88 89 return "<invalid>"; 90 } 91 92 const char *Triple::getOSTypeName(OSType Kind) { 93 switch (Kind) { 94 case UnknownOS: return "unknown"; 95 96 case AuroraUX: return "auroraux"; 97 case Cygwin: return "cygwin"; 98 case Darwin: return "darwin"; 99 case DragonFly: return "dragonfly"; 100 case FreeBSD: return "freebsd"; 101 case IOS: return "ios"; 102 case KFreeBSD: return "kfreebsd"; 103 case Linux: return "linux"; 104 case Lv2: return "lv2"; 105 case MacOSX: return "macosx"; 106 case MinGW32: return "mingw32"; 107 case NetBSD: return "netbsd"; 108 case OpenBSD: return "openbsd"; 109 case Psp: return "psp"; 110 case Solaris: return "solaris"; 111 case Win32: return "win32"; 112 case Haiku: return "haiku"; 113 case Minix: return "minix"; 114 case RTEMS: return "rtems"; 115 case NativeClient: return "nacl"; 116 } 117 118 return "<invalid>"; 119 } 120 121 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) { 122 switch (Kind) { 123 case UnknownEnvironment: return "unknown"; 124 case GNU: return "gnu"; 125 case GNUEABI: return "gnueabi"; 126 case EABI: return "eabi"; 127 case MachO: return "macho"; 128 } 129 130 return "<invalid>"; 131 } 132 133 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) { 134 if (Name == "arm") 135 return arm; 136 if (Name == "cellspu") 137 return cellspu; 138 if (Name == "mips") 139 return mips; 140 if (Name == "mipsel") 141 return mipsel; 142 if (Name == "mips64") 143 return mips64; 144 if (Name == "mips64el") 145 return mips64el; 146 if (Name == "msp430") 147 return msp430; 148 if (Name == "ppc64") 149 return ppc64; 150 if (Name == "ppc32") 151 return ppc; 152 if (Name == "ppc") 153 return ppc; 154 if (Name == "mblaze") 155 return mblaze; 156 if (Name == "hexagon") 157 return hexagon; 158 if (Name == "sparc") 159 return sparc; 160 if (Name == "sparcv9") 161 return sparcv9; 162 if (Name == "tce") 163 return tce; 164 if (Name == "thumb") 165 return thumb; 166 if (Name == "x86") 167 return x86; 168 if (Name == "x86-64") 169 return x86_64; 170 if (Name == "xcore") 171 return xcore; 172 if (Name == "ptx32") 173 return ptx32; 174 if (Name == "ptx64") 175 return ptx64; 176 if (Name == "le32") 177 return le32; 178 if (Name == "amdil") 179 return amdil; 180 181 return UnknownArch; 182 } 183 184 Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) { 185 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for 186 // archs which Darwin doesn't use. 187 188 // The matching this routine does is fairly pointless, since it is neither the 189 // complete architecture list, nor a reasonable subset. The problem is that 190 // historically the driver driver accepts this and also ties its -march= 191 // handling to the architecture name, so we need to be careful before removing 192 // support for it. 193 194 // This code must be kept in sync with Clang's Darwin specific argument 195 // translation. 196 197 if (Str == "ppc" || Str == "ppc601" || Str == "ppc603" || Str == "ppc604" || 198 Str == "ppc604e" || Str == "ppc750" || Str == "ppc7400" || 199 Str == "ppc7450" || Str == "ppc970") 200 return Triple::ppc; 201 202 if (Str == "ppc64") 203 return Triple::ppc64; 204 205 if (Str == "i386" || Str == "i486" || Str == "i486SX" || Str == "pentium" || 206 Str == "i586" || Str == "pentpro" || Str == "i686" || Str == "pentIIm3" || 207 Str == "pentIIm5" || Str == "pentium4") 208 return Triple::x86; 209 210 if (Str == "x86_64") 211 return Triple::x86_64; 212 213 // This is derived from the driver driver. 214 if (Str == "arm" || Str == "armv4t" || Str == "armv5" || Str == "xscale" || 215 Str == "armv6" || Str == "armv7" || Str == "armv7f" || Str == "armv7k" || 216 Str == "armv7s") 217 return Triple::arm; 218 219 if (Str == "ptx32") 220 return Triple::ptx32; 221 if (Str == "ptx64") 222 return Triple::ptx64; 223 if (Str == "amdil") 224 return Triple::amdil; 225 226 return Triple::UnknownArch; 227 } 228 229 // Returns architecture name that is understood by the target assembler. 230 const char *Triple::getArchNameForAssembler() { 231 if (!isOSDarwin() && getVendor() != Triple::Apple) 232 return NULL; 233 234 StringRef Str = getArchName(); 235 if (Str == "i386") 236 return "i386"; 237 if (Str == "x86_64") 238 return "x86_64"; 239 if (Str == "powerpc") 240 return "ppc"; 241 if (Str == "powerpc64") 242 return "ppc64"; 243 if (Str == "mblaze" || Str == "microblaze") 244 return "mblaze"; 245 if (Str == "arm") 246 return "arm"; 247 if (Str == "armv4t" || Str == "thumbv4t") 248 return "armv4t"; 249 if (Str == "armv5" || Str == "armv5e" || Str == "thumbv5" 250 || Str == "thumbv5e") 251 return "armv5"; 252 if (Str == "armv6" || Str == "thumbv6") 253 return "armv6"; 254 if (Str == "armv7" || Str == "thumbv7") 255 return "armv7"; 256 if (Str == "ptx32") 257 return "ptx32"; 258 if (Str == "ptx64") 259 return "ptx64"; 260 if (Str == "le32") 261 return "le32"; 262 if (Str == "amdil") 263 return "amdil"; 264 return NULL; 265 } 266 267 // 268 269 Triple::ArchType Triple::ParseArch(StringRef ArchName) { 270 if (ArchName.size() == 4 && ArchName[0] == 'i' && 271 ArchName[2] == '8' && ArchName[3] == '6' && 272 ArchName[1] - '3' < 6) // i[3-9]86 273 return x86; 274 else if (ArchName == "amd64" || ArchName == "x86_64") 275 return x86_64; 276 else if (ArchName == "powerpc") 277 return ppc; 278 else if ((ArchName == "powerpc64") || (ArchName == "ppu")) 279 return ppc64; 280 else if (ArchName == "mblaze") 281 return mblaze; 282 else if (ArchName == "arm" || 283 ArchName.startswith("armv") || 284 ArchName == "xscale") 285 return arm; 286 else if (ArchName == "thumb" || 287 ArchName.startswith("thumbv")) 288 return thumb; 289 else if (ArchName == "spu" || ArchName == "cellspu") 290 return cellspu; 291 else if (ArchName == "msp430") 292 return msp430; 293 else if (ArchName == "mips" || ArchName == "mipseb" || 294 ArchName == "mipsallegrex") 295 return mips; 296 else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" || 297 ArchName == "psp") 298 return mipsel; 299 else if (ArchName == "mips64" || ArchName == "mips64eb") 300 return mips64; 301 else if (ArchName == "mips64el") 302 return mips64el; 303 else if (ArchName == "hexagon") 304 return hexagon; 305 else if (ArchName == "sparc") 306 return sparc; 307 else if (ArchName == "sparcv9") 308 return sparcv9; 309 else if (ArchName == "tce") 310 return tce; 311 else if (ArchName == "xcore") 312 return xcore; 313 else if (ArchName == "ptx32") 314 return ptx32; 315 else if (ArchName == "ptx64") 316 return ptx64; 317 else if (ArchName == "le32") 318 return le32; 319 else if (ArchName == "amdil") 320 return amdil; 321 else 322 return UnknownArch; 323 } 324 325 Triple::VendorType Triple::ParseVendor(StringRef VendorName) { 326 if (VendorName == "apple") 327 return Apple; 328 else if (VendorName == "pc") 329 return PC; 330 else if (VendorName == "scei") 331 return SCEI; 332 else 333 return UnknownVendor; 334 } 335 336 Triple::OSType Triple::ParseOS(StringRef OSName) { 337 if (OSName.startswith("auroraux")) 338 return AuroraUX; 339 else if (OSName.startswith("cygwin")) 340 return Cygwin; 341 else if (OSName.startswith("darwin")) 342 return Darwin; 343 else if (OSName.startswith("dragonfly")) 344 return DragonFly; 345 else if (OSName.startswith("freebsd")) 346 return FreeBSD; 347 else if (OSName.startswith("ios")) 348 return IOS; 349 else if (OSName.startswith("kfreebsd")) 350 return KFreeBSD; 351 else if (OSName.startswith("linux")) 352 return Linux; 353 else if (OSName.startswith("lv2")) 354 return Lv2; 355 else if (OSName.startswith("macosx")) 356 return MacOSX; 357 else if (OSName.startswith("mingw32")) 358 return MinGW32; 359 else if (OSName.startswith("netbsd")) 360 return NetBSD; 361 else if (OSName.startswith("openbsd")) 362 return OpenBSD; 363 else if (OSName.startswith("psp")) 364 return Psp; 365 else if (OSName.startswith("solaris")) 366 return Solaris; 367 else if (OSName.startswith("win32")) 368 return Win32; 369 else if (OSName.startswith("haiku")) 370 return Haiku; 371 else if (OSName.startswith("minix")) 372 return Minix; 373 else if (OSName.startswith("rtems")) 374 return RTEMS; 375 else if (OSName.startswith("nacl")) 376 return NativeClient; 377 else 378 return UnknownOS; 379 } 380 381 Triple::EnvironmentType Triple::ParseEnvironment(StringRef EnvironmentName) { 382 if (EnvironmentName.startswith("eabi")) 383 return EABI; 384 else if (EnvironmentName.startswith("gnueabi")) 385 return GNUEABI; 386 else if (EnvironmentName.startswith("gnu")) 387 return GNU; 388 else if (EnvironmentName.startswith("macho")) 389 return MachO; 390 else 391 return UnknownEnvironment; 392 } 393 394 void Triple::Parse() const { 395 assert(!isInitialized() && "Invalid parse call."); 396 397 Arch = ParseArch(getArchName()); 398 Vendor = ParseVendor(getVendorName()); 399 OS = ParseOS(getOSName()); 400 Environment = ParseEnvironment(getEnvironmentName()); 401 402 assert(isInitialized() && "Failed to initialize!"); 403 } 404 405 std::string Triple::normalize(StringRef Str) { 406 // Parse into components. 407 SmallVector<StringRef, 4> Components; 408 for (size_t First = 0, Last = 0; Last != StringRef::npos; First = Last + 1) { 409 Last = Str.find('-', First); 410 Components.push_back(Str.slice(First, Last)); 411 } 412 413 // If the first component corresponds to a known architecture, preferentially 414 // use it for the architecture. If the second component corresponds to a 415 // known vendor, preferentially use it for the vendor, etc. This avoids silly 416 // component movement when a component parses as (eg) both a valid arch and a 417 // valid os. 418 ArchType Arch = UnknownArch; 419 if (Components.size() > 0) 420 Arch = ParseArch(Components[0]); 421 VendorType Vendor = UnknownVendor; 422 if (Components.size() > 1) 423 Vendor = ParseVendor(Components[1]); 424 OSType OS = UnknownOS; 425 if (Components.size() > 2) 426 OS = ParseOS(Components[2]); 427 EnvironmentType Environment = UnknownEnvironment; 428 if (Components.size() > 3) 429 Environment = ParseEnvironment(Components[3]); 430 431 // Note which components are already in their final position. These will not 432 // be moved. 433 bool Found[4]; 434 Found[0] = Arch != UnknownArch; 435 Found[1] = Vendor != UnknownVendor; 436 Found[2] = OS != UnknownOS; 437 Found[3] = Environment != UnknownEnvironment; 438 439 // If they are not there already, permute the components into their canonical 440 // positions by seeing if they parse as a valid architecture, and if so moving 441 // the component to the architecture position etc. 442 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) { 443 if (Found[Pos]) 444 continue; // Already in the canonical position. 445 446 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) { 447 // Do not reparse any components that already matched. 448 if (Idx < array_lengthof(Found) && Found[Idx]) 449 continue; 450 451 // Does this component parse as valid for the target position? 452 bool Valid = false; 453 StringRef Comp = Components[Idx]; 454 switch (Pos) { 455 default: 456 assert(false && "unexpected component type!"); 457 case 0: 458 Arch = ParseArch(Comp); 459 Valid = Arch != UnknownArch; 460 break; 461 case 1: 462 Vendor = ParseVendor(Comp); 463 Valid = Vendor != UnknownVendor; 464 break; 465 case 2: 466 OS = ParseOS(Comp); 467 Valid = OS != UnknownOS; 468 break; 469 case 3: 470 Environment = ParseEnvironment(Comp); 471 Valid = Environment != UnknownEnvironment; 472 break; 473 } 474 if (!Valid) 475 continue; // Nope, try the next component. 476 477 // Move the component to the target position, pushing any non-fixed 478 // components that are in the way to the right. This tends to give 479 // good results in the common cases of a forgotten vendor component 480 // or a wrongly positioned environment. 481 if (Pos < Idx) { 482 // Insert left, pushing the existing components to the right. For 483 // example, a-b-i386 -> i386-a-b when moving i386 to the front. 484 StringRef CurrentComponent(""); // The empty component. 485 // Replace the component we are moving with an empty component. 486 std::swap(CurrentComponent, Components[Idx]); 487 // Insert the component being moved at Pos, displacing any existing 488 // components to the right. 489 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) { 490 // Skip over any fixed components. 491 while (i < array_lengthof(Found) && Found[i]) ++i; 492 // Place the component at the new position, getting the component 493 // that was at this position - it will be moved right. 494 std::swap(CurrentComponent, Components[i]); 495 } 496 } else if (Pos > Idx) { 497 // Push right by inserting empty components until the component at Idx 498 // reaches the target position Pos. For example, pc-a -> -pc-a when 499 // moving pc to the second position. 500 do { 501 // Insert one empty component at Idx. 502 StringRef CurrentComponent(""); // The empty component. 503 for (unsigned i = Idx; i < Components.size();) { 504 // Place the component at the new position, getting the component 505 // that was at this position - it will be moved right. 506 std::swap(CurrentComponent, Components[i]); 507 // If it was placed on top of an empty component then we are done. 508 if (CurrentComponent.empty()) 509 break; 510 // Advance to the next component, skipping any fixed components. 511 while (++i < array_lengthof(Found) && Found[i]) 512 ; 513 } 514 // The last component was pushed off the end - append it. 515 if (!CurrentComponent.empty()) 516 Components.push_back(CurrentComponent); 517 518 // Advance Idx to the component's new position. 519 while (++Idx < array_lengthof(Found) && Found[Idx]) {} 520 } while (Idx < Pos); // Add more until the final position is reached. 521 } 522 assert(Pos < Components.size() && Components[Pos] == Comp && 523 "Component moved wrong!"); 524 Found[Pos] = true; 525 break; 526 } 527 } 528 529 // Special case logic goes here. At this point Arch, Vendor and OS have the 530 // correct values for the computed components. 531 532 // Stick the corrected components back together to form the normalized string. 533 std::string Normalized; 534 for (unsigned i = 0, e = Components.size(); i != e; ++i) { 535 if (i) Normalized += '-'; 536 Normalized += Components[i]; 537 } 538 return Normalized; 539 } 540 541 StringRef Triple::getArchName() const { 542 return StringRef(Data).split('-').first; // Isolate first component 543 } 544 545 StringRef Triple::getVendorName() const { 546 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component 547 return Tmp.split('-').first; // Isolate second component 548 } 549 550 StringRef Triple::getOSName() const { 551 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component 552 Tmp = Tmp.split('-').second; // Strip second component 553 return Tmp.split('-').first; // Isolate third component 554 } 555 556 StringRef Triple::getEnvironmentName() const { 557 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component 558 Tmp = Tmp.split('-').second; // Strip second component 559 return Tmp.split('-').second; // Strip third component 560 } 561 562 StringRef Triple::getOSAndEnvironmentName() const { 563 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component 564 return Tmp.split('-').second; // Strip second component 565 } 566 567 static unsigned EatNumber(StringRef &Str) { 568 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number"); 569 unsigned Result = 0; 570 571 do { 572 // Consume the leading digit. 573 Result = Result*10 + (Str[0] - '0'); 574 575 // Eat the digit. 576 Str = Str.substr(1); 577 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9'); 578 579 return Result; 580 } 581 582 void Triple::getOSVersion(unsigned &Major, unsigned &Minor, 583 unsigned &Micro) const { 584 StringRef OSName = getOSName(); 585 586 // Assume that the OS portion of the triple starts with the canonical name. 587 StringRef OSTypeName = getOSTypeName(getOS()); 588 if (OSName.startswith(OSTypeName)) 589 OSName = OSName.substr(OSTypeName.size()); 590 591 // Any unset version defaults to 0. 592 Major = Minor = Micro = 0; 593 594 // Parse up to three components. 595 unsigned *Components[3] = { &Major, &Minor, &Micro }; 596 for (unsigned i = 0; i != 3; ++i) { 597 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9') 598 break; 599 600 // Consume the leading number. 601 *Components[i] = EatNumber(OSName); 602 603 // Consume the separator, if present. 604 if (OSName.startswith(".")) 605 OSName = OSName.substr(1); 606 } 607 } 608 609 void Triple::setTriple(const Twine &Str) { 610 Data = Str.str(); 611 Arch = InvalidArch; 612 } 613 614 void Triple::setArch(ArchType Kind) { 615 setArchName(getArchTypeName(Kind)); 616 } 617 618 void Triple::setVendor(VendorType Kind) { 619 setVendorName(getVendorTypeName(Kind)); 620 } 621 622 void Triple::setOS(OSType Kind) { 623 setOSName(getOSTypeName(Kind)); 624 } 625 626 void Triple::setEnvironment(EnvironmentType Kind) { 627 setEnvironmentName(getEnvironmentTypeName(Kind)); 628 } 629 630 void Triple::setArchName(StringRef Str) { 631 // Work around a miscompilation bug for Twines in gcc 4.0.3. 632 SmallString<64> Triple; 633 Triple += Str; 634 Triple += "-"; 635 Triple += getVendorName(); 636 Triple += "-"; 637 Triple += getOSAndEnvironmentName(); 638 setTriple(Triple.str()); 639 } 640 641 void Triple::setVendorName(StringRef Str) { 642 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName()); 643 } 644 645 void Triple::setOSName(StringRef Str) { 646 if (hasEnvironment()) 647 setTriple(getArchName() + "-" + getVendorName() + "-" + Str + 648 "-" + getEnvironmentName()); 649 else 650 setTriple(getArchName() + "-" + getVendorName() + "-" + Str); 651 } 652 653 void Triple::setEnvironmentName(StringRef Str) { 654 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() + 655 "-" + Str); 656 } 657 658 void Triple::setOSAndEnvironmentName(StringRef Str) { 659 setTriple(getArchName() + "-" + getVendorName() + "-" + Str); 660 } 661