1 //===--- Triple.cpp - Target triple helper class --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/Triple.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/Support/ErrorHandling.h"
17 const char *Triple::getArchTypeName(ArchType Kind) {
19 case InvalidArch: return "<invalid>";
20 case UnknownArch: return "unknown";
22 case arm: return "arm";
23 case cellspu: return "cellspu";
24 case hexagon: return "hexagon";
25 case mips: return "mips";
26 case mipsel: return "mipsel";
27 case mips64: return "mips64";
28 case mips64el:return "mips64el";
29 case msp430: return "msp430";
30 case ppc64: return "powerpc64";
31 case ppc: return "powerpc";
32 case sparc: return "sparc";
33 case sparcv9: return "sparcv9";
34 case tce: return "tce";
35 case thumb: return "thumb";
36 case x86: return "i386";
37 case x86_64: return "x86_64";
38 case xcore: return "xcore";
39 case mblaze: return "mblaze";
40 case ptx32: return "ptx32";
41 case ptx64: return "ptx64";
42 case le32: return "le32";
43 case amdil: return "amdil";
46 llvm_unreachable("Invalid ArchType!");
49 const char *Triple::getArchTypePrefix(ArchType Kind) {
55 case thumb: return "arm";
57 case cellspu: return "spu";
60 case ppc: return "ppc";
62 case mblaze: return "mblaze";
64 case hexagon: return "hexagon";
67 case sparc: return "sparc";
70 case x86_64: return "x86";
72 case xcore: return "xcore";
74 case ptx32: return "ptx";
75 case ptx64: return "ptx";
76 case le32: return "le32";
77 case amdil: return "amdil";
81 const char *Triple::getVendorTypeName(VendorType Kind) {
83 case UnknownVendor: return "unknown";
85 case Apple: return "apple";
87 case SCEI: return "scei";
90 llvm_unreachable("Invalid VendorType!");
93 const char *Triple::getOSTypeName(OSType Kind) {
95 case UnknownOS: return "unknown";
97 case AuroraUX: return "auroraux";
98 case Cygwin: return "cygwin";
99 case Darwin: return "darwin";
100 case DragonFly: return "dragonfly";
101 case FreeBSD: return "freebsd";
102 case IOS: return "ios";
103 case KFreeBSD: return "kfreebsd";
104 case Linux: return "linux";
105 case Lv2: return "lv2";
106 case MacOSX: return "macosx";
107 case MinGW32: return "mingw32";
108 case NetBSD: return "netbsd";
109 case OpenBSD: return "openbsd";
110 case Psp: return "psp";
111 case Solaris: return "solaris";
112 case Win32: return "win32";
113 case Haiku: return "haiku";
114 case Minix: return "minix";
115 case RTEMS: return "rtems";
116 case NativeClient: return "nacl";
119 llvm_unreachable("Invalid OSType");
122 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
124 case UnknownEnvironment: return "unknown";
125 case GNU: return "gnu";
126 case GNUEABIHF: return "gnueabihf";
127 case GNUEABI: return "gnueabi";
128 case EABI: return "eabi";
129 case MachO: return "macho";
130 case ANDROIDEABI: return "androideabi";
133 llvm_unreachable("Invalid EnvironmentType!");
136 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
139 if (Name == "cellspu")
143 if (Name == "mipsel")
145 if (Name == "mips64")
147 if (Name == "mips64el")
149 if (Name == "msp430")
157 if (Name == "mblaze")
159 if (Name == "hexagon")
163 if (Name == "sparcv9")
171 if (Name == "x86-64")
187 Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
188 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
189 // archs which Darwin doesn't use.
191 // The matching this routine does is fairly pointless, since it is neither the
192 // complete architecture list, nor a reasonable subset. The problem is that
193 // historically the driver driver accepts this and also ties its -march=
194 // handling to the architecture name, so we need to be careful before removing
197 // This code must be kept in sync with Clang's Darwin specific argument
200 if (Str == "ppc" || Str == "ppc601" || Str == "ppc603" || Str == "ppc604" ||
201 Str == "ppc604e" || Str == "ppc750" || Str == "ppc7400" ||
202 Str == "ppc7450" || Str == "ppc970")
206 return Triple::ppc64;
208 if (Str == "i386" || Str == "i486" || Str == "i486SX" || Str == "pentium" ||
209 Str == "i586" || Str == "pentpro" || Str == "i686" || Str == "pentIIm3" ||
210 Str == "pentIIm5" || Str == "pentium4")
214 return Triple::x86_64;
216 // This is derived from the driver driver.
217 if (Str == "arm" || Str == "armv4t" || Str == "armv5" || Str == "xscale" ||
218 Str == "armv6" || Str == "armv7" || Str == "armv7f" || Str == "armv7k" ||
223 return Triple::ptx32;
225 return Triple::ptx64;
227 return Triple::amdil;
229 return Triple::UnknownArch;
232 // Returns architecture name that is understood by the target assembler.
233 const char *Triple::getArchNameForAssembler() {
234 if (!isOSDarwin() && getVendor() != Triple::Apple)
237 StringRef Str = getArchName();
242 if (Str == "powerpc")
244 if (Str == "powerpc64")
246 if (Str == "mblaze" || Str == "microblaze")
250 if (Str == "armv4t" || Str == "thumbv4t")
252 if (Str == "armv5" || Str == "armv5e" || Str == "thumbv5"
253 || Str == "thumbv5e")
255 if (Str == "armv6" || Str == "thumbv6")
257 if (Str == "armv7" || Str == "thumbv7")
272 Triple::ArchType Triple::ParseArch(StringRef ArchName) {
273 if (ArchName.size() == 4 && ArchName[0] == 'i' &&
274 ArchName[2] == '8' && ArchName[3] == '6' &&
275 ArchName[1] - '3' < 6) // i[3-9]86
277 else if (ArchName == "amd64" || ArchName == "x86_64")
279 else if (ArchName == "powerpc")
281 else if ((ArchName == "powerpc64") || (ArchName == "ppu"))
283 else if (ArchName == "mblaze")
285 else if (ArchName == "arm" ||
286 ArchName.startswith("armv") ||
287 ArchName == "xscale")
289 else if (ArchName == "thumb" ||
290 ArchName.startswith("thumbv"))
292 else if (ArchName == "spu" || ArchName == "cellspu")
294 else if (ArchName == "msp430")
296 else if (ArchName == "mips" || ArchName == "mipseb" ||
297 ArchName == "mipsallegrex")
299 else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
302 else if (ArchName == "mips64" || ArchName == "mips64eb")
304 else if (ArchName == "mips64el")
306 else if (ArchName == "hexagon")
308 else if (ArchName == "sparc")
310 else if (ArchName == "sparcv9")
312 else if (ArchName == "tce")
314 else if (ArchName == "xcore")
316 else if (ArchName == "ptx32")
318 else if (ArchName == "ptx64")
320 else if (ArchName == "le32")
322 else if (ArchName == "amdil")
328 Triple::VendorType Triple::ParseVendor(StringRef VendorName) {
329 if (VendorName == "apple")
331 else if (VendorName == "pc")
333 else if (VendorName == "scei")
336 return UnknownVendor;
339 Triple::OSType Triple::ParseOS(StringRef OSName) {
340 if (OSName.startswith("auroraux"))
342 else if (OSName.startswith("cygwin"))
344 else if (OSName.startswith("darwin"))
346 else if (OSName.startswith("dragonfly"))
348 else if (OSName.startswith("freebsd"))
350 else if (OSName.startswith("ios"))
352 else if (OSName.startswith("kfreebsd"))
354 else if (OSName.startswith("linux"))
356 else if (OSName.startswith("lv2"))
358 else if (OSName.startswith("macosx"))
360 else if (OSName.startswith("mingw32"))
362 else if (OSName.startswith("netbsd"))
364 else if (OSName.startswith("openbsd"))
366 else if (OSName.startswith("psp"))
368 else if (OSName.startswith("solaris"))
370 else if (OSName.startswith("win32"))
372 else if (OSName.startswith("haiku"))
374 else if (OSName.startswith("minix"))
376 else if (OSName.startswith("rtems"))
378 else if (OSName.startswith("nacl"))
384 Triple::EnvironmentType Triple::ParseEnvironment(StringRef EnvironmentName) {
385 if (EnvironmentName.startswith("eabi"))
387 else if (EnvironmentName.startswith("gnueabihf"))
389 else if (EnvironmentName.startswith("gnueabi"))
391 else if (EnvironmentName.startswith("gnu"))
393 else if (EnvironmentName.startswith("macho"))
395 else if (EnvironmentName.startswith("androideabi"))
398 return UnknownEnvironment;
401 void Triple::Parse() const {
402 assert(!isInitialized() && "Invalid parse call.");
404 Arch = ParseArch(getArchName());
405 Vendor = ParseVendor(getVendorName());
406 OS = ParseOS(getOSName());
407 Environment = ParseEnvironment(getEnvironmentName());
409 assert(isInitialized() && "Failed to initialize!");
412 std::string Triple::normalize(StringRef Str) {
413 // Parse into components.
414 SmallVector<StringRef, 4> Components;
415 for (size_t First = 0, Last = 0; Last != StringRef::npos; First = Last + 1) {
416 Last = Str.find('-', First);
417 Components.push_back(Str.slice(First, Last));
420 // If the first component corresponds to a known architecture, preferentially
421 // use it for the architecture. If the second component corresponds to a
422 // known vendor, preferentially use it for the vendor, etc. This avoids silly
423 // component movement when a component parses as (eg) both a valid arch and a
425 ArchType Arch = UnknownArch;
426 if (Components.size() > 0)
427 Arch = ParseArch(Components[0]);
428 VendorType Vendor = UnknownVendor;
429 if (Components.size() > 1)
430 Vendor = ParseVendor(Components[1]);
431 OSType OS = UnknownOS;
432 if (Components.size() > 2)
433 OS = ParseOS(Components[2]);
434 EnvironmentType Environment = UnknownEnvironment;
435 if (Components.size() > 3)
436 Environment = ParseEnvironment(Components[3]);
438 // Note which components are already in their final position. These will not
441 Found[0] = Arch != UnknownArch;
442 Found[1] = Vendor != UnknownVendor;
443 Found[2] = OS != UnknownOS;
444 Found[3] = Environment != UnknownEnvironment;
446 // If they are not there already, permute the components into their canonical
447 // positions by seeing if they parse as a valid architecture, and if so moving
448 // the component to the architecture position etc.
449 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
451 continue; // Already in the canonical position.
453 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
454 // Do not reparse any components that already matched.
455 if (Idx < array_lengthof(Found) && Found[Idx])
458 // Does this component parse as valid for the target position?
460 StringRef Comp = Components[Idx];
463 assert(false && "unexpected component type!");
465 Arch = ParseArch(Comp);
466 Valid = Arch != UnknownArch;
469 Vendor = ParseVendor(Comp);
470 Valid = Vendor != UnknownVendor;
474 Valid = OS != UnknownOS;
477 Environment = ParseEnvironment(Comp);
478 Valid = Environment != UnknownEnvironment;
482 continue; // Nope, try the next component.
484 // Move the component to the target position, pushing any non-fixed
485 // components that are in the way to the right. This tends to give
486 // good results in the common cases of a forgotten vendor component
487 // or a wrongly positioned environment.
489 // Insert left, pushing the existing components to the right. For
490 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
491 StringRef CurrentComponent(""); // The empty component.
492 // Replace the component we are moving with an empty component.
493 std::swap(CurrentComponent, Components[Idx]);
494 // Insert the component being moved at Pos, displacing any existing
495 // components to the right.
496 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
497 // Skip over any fixed components.
498 while (i < array_lengthof(Found) && Found[i]) ++i;
499 // Place the component at the new position, getting the component
500 // that was at this position - it will be moved right.
501 std::swap(CurrentComponent, Components[i]);
503 } else if (Pos > Idx) {
504 // Push right by inserting empty components until the component at Idx
505 // reaches the target position Pos. For example, pc-a -> -pc-a when
506 // moving pc to the second position.
508 // Insert one empty component at Idx.
509 StringRef CurrentComponent(""); // The empty component.
510 for (unsigned i = Idx; i < Components.size();) {
511 // Place the component at the new position, getting the component
512 // that was at this position - it will be moved right.
513 std::swap(CurrentComponent, Components[i]);
514 // If it was placed on top of an empty component then we are done.
515 if (CurrentComponent.empty())
517 // Advance to the next component, skipping any fixed components.
518 while (++i < array_lengthof(Found) && Found[i])
521 // The last component was pushed off the end - append it.
522 if (!CurrentComponent.empty())
523 Components.push_back(CurrentComponent);
525 // Advance Idx to the component's new position.
526 while (++Idx < array_lengthof(Found) && Found[Idx]) {}
527 } while (Idx < Pos); // Add more until the final position is reached.
529 assert(Pos < Components.size() && Components[Pos] == Comp &&
530 "Component moved wrong!");
536 // Special case logic goes here. At this point Arch, Vendor and OS have the
537 // correct values for the computed components.
539 // Stick the corrected components back together to form the normalized string.
540 std::string Normalized;
541 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
542 if (i) Normalized += '-';
543 Normalized += Components[i];
548 StringRef Triple::getArchName() const {
549 return StringRef(Data).split('-').first; // Isolate first component
552 StringRef Triple::getVendorName() const {
553 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
554 return Tmp.split('-').first; // Isolate second component
557 StringRef Triple::getOSName() const {
558 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
559 Tmp = Tmp.split('-').second; // Strip second component
560 return Tmp.split('-').first; // Isolate third component
563 StringRef Triple::getEnvironmentName() const {
564 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
565 Tmp = Tmp.split('-').second; // Strip second component
566 return Tmp.split('-').second; // Strip third component
569 StringRef Triple::getOSAndEnvironmentName() const {
570 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
571 return Tmp.split('-').second; // Strip second component
574 static unsigned EatNumber(StringRef &Str) {
575 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
579 // Consume the leading digit.
580 Result = Result*10 + (Str[0] - '0');
584 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
589 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
590 unsigned &Micro) const {
591 StringRef OSName = getOSName();
593 // Assume that the OS portion of the triple starts with the canonical name.
594 StringRef OSTypeName = getOSTypeName(getOS());
595 if (OSName.startswith(OSTypeName))
596 OSName = OSName.substr(OSTypeName.size());
598 // Any unset version defaults to 0.
599 Major = Minor = Micro = 0;
601 // Parse up to three components.
602 unsigned *Components[3] = { &Major, &Minor, &Micro };
603 for (unsigned i = 0; i != 3; ++i) {
604 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
607 // Consume the leading number.
608 *Components[i] = EatNumber(OSName);
610 // Consume the separator, if present.
611 if (OSName.startswith("."))
612 OSName = OSName.substr(1);
616 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
617 unsigned &Micro) const {
618 getOSVersion(Major, Minor, Micro);
621 default: assert(0 && "unexpected OS for Darwin triple");
623 // Default to darwin8, i.e., MacOSX 10.4.
626 // Darwin version numbers are skewed from OS X versions.
643 // Ignore the version from the triple. This is only handled because the
644 // the clang driver combines OS X and IOS support into a common Darwin
645 // toolchain that wants to know the OS X version number even when targeting
655 void Triple::setTriple(const Twine &Str) {
660 void Triple::setArch(ArchType Kind) {
661 setArchName(getArchTypeName(Kind));
664 void Triple::setVendor(VendorType Kind) {
665 setVendorName(getVendorTypeName(Kind));
668 void Triple::setOS(OSType Kind) {
669 setOSName(getOSTypeName(Kind));
672 void Triple::setEnvironment(EnvironmentType Kind) {
673 setEnvironmentName(getEnvironmentTypeName(Kind));
676 void Triple::setArchName(StringRef Str) {
677 // Work around a miscompilation bug for Twines in gcc 4.0.3.
678 SmallString<64> Triple;
681 Triple += getVendorName();
683 Triple += getOSAndEnvironmentName();
684 setTriple(Triple.str());
687 void Triple::setVendorName(StringRef Str) {
688 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
691 void Triple::setOSName(StringRef Str) {
692 if (hasEnvironment())
693 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
694 "-" + getEnvironmentName());
696 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
699 void Triple::setEnvironmentName(StringRef Str) {
700 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
704 void Triple::setOSAndEnvironmentName(StringRef Str) {
705 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
708 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
710 case llvm::Triple::UnknownArch:
711 case llvm::Triple::InvalidArch:
714 case llvm::Triple::msp430:
717 case llvm::Triple::amdil:
718 case llvm::Triple::arm:
719 case llvm::Triple::cellspu:
720 case llvm::Triple::hexagon:
721 case llvm::Triple::le32:
722 case llvm::Triple::mblaze:
723 case llvm::Triple::mips:
724 case llvm::Triple::mipsel:
725 case llvm::Triple::ppc:
726 case llvm::Triple::ptx32:
727 case llvm::Triple::sparc:
728 case llvm::Triple::tce:
729 case llvm::Triple::thumb:
730 case llvm::Triple::x86:
731 case llvm::Triple::xcore:
734 case llvm::Triple::mips64:
735 case llvm::Triple::mips64el:
736 case llvm::Triple::ppc64:
737 case llvm::Triple::ptx64:
738 case llvm::Triple::sparcv9:
739 case llvm::Triple::x86_64:
742 llvm_unreachable("Invalid architecture value");
745 bool Triple::isArch64Bit() const {
746 return getArchPointerBitWidth(getArch()) == 64;
749 bool Triple::isArch32Bit() const {
750 return getArchPointerBitWidth(getArch()) == 32;
753 bool Triple::isArch16Bit() const {
754 return getArchPointerBitWidth(getArch()) == 16;