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"
16 const char *Triple::getArchTypeName(ArchType Kind) {
18 case InvalidArch: return "<invalid>";
19 case UnknownArch: return "unknown";
21 case alpha: return "alpha";
22 case arm: return "arm";
23 case bfin: return "bfin";
24 case cellspu: return "cellspu";
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 systemz: return "s390x";
35 case tce: return "tce";
36 case thumb: return "thumb";
37 case x86: return "i386";
38 case x86_64: return "x86_64";
39 case xcore: return "xcore";
40 case mblaze: return "mblaze";
41 case ptx32: return "ptx32";
42 case ptx64: return "ptx64";
43 case le32: return "le32";
44 case amdil: return "amdil";
50 const char *Triple::getArchTypePrefix(ArchType Kind) {
55 case alpha: return "alpha";
58 case thumb: return "arm";
60 case bfin: return "bfin";
62 case cellspu: return "spu";
65 case ppc: return "ppc";
67 case mblaze: return "mblaze";
70 case sparc: return "sparc";
73 case x86_64: return "x86";
75 case xcore: return "xcore";
77 case ptx32: return "ptx";
78 case ptx64: return "ptx";
79 case le32: return "le32";
80 case amdil: return "amdil";
84 const char *Triple::getVendorTypeName(VendorType Kind) {
86 case UnknownVendor: return "unknown";
88 case Apple: return "apple";
90 case SCEI: return "scei";
96 const char *Triple::getOSTypeName(OSType Kind) {
98 case UnknownOS: return "unknown";
100 case AuroraUX: return "auroraux";
101 case Cygwin: return "cygwin";
102 case Darwin: return "darwin";
103 case DragonFly: return "dragonfly";
104 case FreeBSD: return "freebsd";
105 case IOS: return "ios";
106 case KFreeBSD: return "kfreebsd";
107 case Linux: return "linux";
108 case Lv2: return "lv2";
109 case MacOSX: return "macosx";
110 case MinGW32: return "mingw32";
111 case NetBSD: return "netbsd";
112 case OpenBSD: return "openbsd";
113 case Psp: return "psp";
114 case Solaris: return "solaris";
115 case Win32: return "win32";
116 case Haiku: return "haiku";
117 case Minix: return "minix";
118 case RTEMS: return "rtems";
119 case NativeClient: return "nacl";
125 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
127 case UnknownEnvironment: return "unknown";
128 case GNU: return "gnu";
129 case GNUEABI: return "gnueabi";
130 case EABI: return "eabi";
131 case MachO: return "macho";
137 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
144 if (Name == "cellspu")
148 if (Name == "mipsel")
150 if (Name == "mips64")
152 if (Name == "mips64el")
154 if (Name == "msp430")
162 if (Name == "mblaze")
166 if (Name == "sparcv9")
168 if (Name == "systemz")
176 if (Name == "x86-64")
192 Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
193 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
194 // archs which Darwin doesn't use.
196 // The matching this routine does is fairly pointless, since it is neither the
197 // complete architecture list, nor a reasonable subset. The problem is that
198 // historically the driver driver accepts this and also ties its -march=
199 // handling to the architecture name, so we need to be careful before removing
202 // This code must be kept in sync with Clang's Darwin specific argument
205 if (Str == "ppc" || Str == "ppc601" || Str == "ppc603" || Str == "ppc604" ||
206 Str == "ppc604e" || Str == "ppc750" || Str == "ppc7400" ||
207 Str == "ppc7450" || Str == "ppc970")
211 return Triple::ppc64;
213 if (Str == "i386" || Str == "i486" || Str == "i486SX" || Str == "pentium" ||
214 Str == "i586" || Str == "pentpro" || Str == "i686" || Str == "pentIIm3" ||
215 Str == "pentIIm5" || Str == "pentium4")
219 return Triple::x86_64;
221 // This is derived from the driver driver.
222 if (Str == "arm" || Str == "armv4t" || Str == "armv5" || Str == "xscale" ||
223 Str == "armv6" || Str == "armv7" || Str == "armv7f" || Str == "armv7k" ||
228 return Triple::ptx32;
230 return Triple::ptx64;
232 return Triple::amdil;
234 return Triple::UnknownArch;
237 // Returns architecture name that is understood by the target assembler.
238 const char *Triple::getArchNameForAssembler() {
239 if (!isOSDarwin() && getVendor() != Triple::Apple)
242 StringRef Str = getArchName();
247 if (Str == "powerpc")
249 if (Str == "powerpc64")
251 if (Str == "mblaze" || Str == "microblaze")
255 if (Str == "armv4t" || Str == "thumbv4t")
257 if (Str == "armv5" || Str == "armv5e" || Str == "thumbv5"
258 || Str == "thumbv5e")
260 if (Str == "armv6" || Str == "thumbv6")
262 if (Str == "armv7" || Str == "thumbv7")
277 Triple::ArchType Triple::ParseArch(StringRef ArchName) {
278 if (ArchName.size() == 4 && ArchName[0] == 'i' &&
279 ArchName[2] == '8' && ArchName[3] == '6' &&
280 ArchName[1] - '3' < 6) // i[3-9]86
282 else if (ArchName == "amd64" || ArchName == "x86_64")
284 else if (ArchName == "bfin")
286 else if (ArchName == "powerpc")
288 else if ((ArchName == "powerpc64") || (ArchName == "ppu"))
290 else if (ArchName == "mblaze")
292 else if (ArchName == "arm" ||
293 ArchName.startswith("armv") ||
294 ArchName == "xscale")
296 else if (ArchName == "thumb" ||
297 ArchName.startswith("thumbv"))
299 else if (ArchName.startswith("alpha"))
301 else if (ArchName == "spu" || ArchName == "cellspu")
303 else if (ArchName == "msp430")
305 else if (ArchName == "mips" || ArchName == "mipseb" ||
306 ArchName == "mipsallegrex")
308 else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
311 else if (ArchName == "mips64" || ArchName == "mips64eb")
313 else if (ArchName == "mips64el")
315 else if (ArchName == "sparc")
317 else if (ArchName == "sparcv9")
319 else if (ArchName == "s390x")
321 else if (ArchName == "tce")
323 else if (ArchName == "xcore")
325 else if (ArchName == "ptx32")
327 else if (ArchName == "ptx64")
329 else if (ArchName == "le32")
331 else if (ArchName == "amdil")
337 Triple::VendorType Triple::ParseVendor(StringRef VendorName) {
338 if (VendorName == "apple")
340 else if (VendorName == "pc")
342 else if (VendorName == "scei")
345 return UnknownVendor;
348 Triple::OSType Triple::ParseOS(StringRef OSName) {
349 if (OSName.startswith("auroraux"))
351 else if (OSName.startswith("cygwin"))
353 else if (OSName.startswith("darwin"))
355 else if (OSName.startswith("dragonfly"))
357 else if (OSName.startswith("freebsd"))
359 else if (OSName.startswith("ios"))
361 else if (OSName.startswith("kfreebsd"))
363 else if (OSName.startswith("linux"))
365 else if (OSName.startswith("lv2"))
367 else if (OSName.startswith("macosx"))
369 else if (OSName.startswith("mingw32"))
371 else if (OSName.startswith("netbsd"))
373 else if (OSName.startswith("openbsd"))
375 else if (OSName.startswith("psp"))
377 else if (OSName.startswith("solaris"))
379 else if (OSName.startswith("win32"))
381 else if (OSName.startswith("haiku"))
383 else if (OSName.startswith("minix"))
385 else if (OSName.startswith("rtems"))
387 else if (OSName.startswith("nacl"))
393 Triple::EnvironmentType Triple::ParseEnvironment(StringRef EnvironmentName) {
394 if (EnvironmentName.startswith("eabi"))
396 else if (EnvironmentName.startswith("gnueabi"))
398 else if (EnvironmentName.startswith("gnu"))
400 else if (EnvironmentName.startswith("macho"))
403 return UnknownEnvironment;
406 void Triple::Parse() const {
407 assert(!isInitialized() && "Invalid parse call.");
409 Arch = ParseArch(getArchName());
410 Vendor = ParseVendor(getVendorName());
411 OS = ParseOS(getOSName());
412 Environment = ParseEnvironment(getEnvironmentName());
414 assert(isInitialized() && "Failed to initialize!");
417 std::string Triple::normalize(StringRef Str) {
418 // Parse into components.
419 SmallVector<StringRef, 4> Components;
420 for (size_t First = 0, Last = 0; Last != StringRef::npos; First = Last + 1) {
421 Last = Str.find('-', First);
422 Components.push_back(Str.slice(First, Last));
425 // If the first component corresponds to a known architecture, preferentially
426 // use it for the architecture. If the second component corresponds to a
427 // known vendor, preferentially use it for the vendor, etc. This avoids silly
428 // component movement when a component parses as (eg) both a valid arch and a
430 ArchType Arch = UnknownArch;
431 if (Components.size() > 0)
432 Arch = ParseArch(Components[0]);
433 VendorType Vendor = UnknownVendor;
434 if (Components.size() > 1)
435 Vendor = ParseVendor(Components[1]);
436 OSType OS = UnknownOS;
437 if (Components.size() > 2)
438 OS = ParseOS(Components[2]);
439 EnvironmentType Environment = UnknownEnvironment;
440 if (Components.size() > 3)
441 Environment = ParseEnvironment(Components[3]);
443 // Note which components are already in their final position. These will not
446 Found[0] = Arch != UnknownArch;
447 Found[1] = Vendor != UnknownVendor;
448 Found[2] = OS != UnknownOS;
449 Found[3] = Environment != UnknownEnvironment;
451 // If they are not there already, permute the components into their canonical
452 // positions by seeing if they parse as a valid architecture, and if so moving
453 // the component to the architecture position etc.
454 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
456 continue; // Already in the canonical position.
458 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
459 // Do not reparse any components that already matched.
460 if (Idx < array_lengthof(Found) && Found[Idx])
463 // Does this component parse as valid for the target position?
465 StringRef Comp = Components[Idx];
468 assert(false && "unexpected component type!");
470 Arch = ParseArch(Comp);
471 Valid = Arch != UnknownArch;
474 Vendor = ParseVendor(Comp);
475 Valid = Vendor != UnknownVendor;
479 Valid = OS != UnknownOS;
482 Environment = ParseEnvironment(Comp);
483 Valid = Environment != UnknownEnvironment;
487 continue; // Nope, try the next component.
489 // Move the component to the target position, pushing any non-fixed
490 // components that are in the way to the right. This tends to give
491 // good results in the common cases of a forgotten vendor component
492 // or a wrongly positioned environment.
494 // Insert left, pushing the existing components to the right. For
495 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
496 StringRef CurrentComponent(""); // The empty component.
497 // Replace the component we are moving with an empty component.
498 std::swap(CurrentComponent, Components[Idx]);
499 // Insert the component being moved at Pos, displacing any existing
500 // components to the right.
501 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
502 // Skip over any fixed components.
503 while (i < array_lengthof(Found) && Found[i]) ++i;
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]);
508 } else if (Pos > Idx) {
509 // Push right by inserting empty components until the component at Idx
510 // reaches the target position Pos. For example, pc-a -> -pc-a when
511 // moving pc to the second position.
513 // Insert one empty component at Idx.
514 StringRef CurrentComponent(""); // The empty component.
515 for (unsigned i = Idx; i < Components.size();) {
516 // Place the component at the new position, getting the component
517 // that was at this position - it will be moved right.
518 std::swap(CurrentComponent, Components[i]);
519 // If it was placed on top of an empty component then we are done.
520 if (CurrentComponent.empty())
522 // Advance to the next component, skipping any fixed components.
523 while (++i < array_lengthof(Found) && Found[i])
526 // The last component was pushed off the end - append it.
527 if (!CurrentComponent.empty())
528 Components.push_back(CurrentComponent);
530 // Advance Idx to the component's new position.
531 while (++Idx < array_lengthof(Found) && Found[Idx]) {}
532 } while (Idx < Pos); // Add more until the final position is reached.
534 assert(Pos < Components.size() && Components[Pos] == Comp &&
535 "Component moved wrong!");
541 // Special case logic goes here. At this point Arch, Vendor and OS have the
542 // correct values for the computed components.
544 // Stick the corrected components back together to form the normalized string.
545 std::string Normalized;
546 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
547 if (i) Normalized += '-';
548 Normalized += Components[i];
553 StringRef Triple::getArchName() const {
554 return StringRef(Data).split('-').first; // Isolate first component
557 StringRef Triple::getVendorName() const {
558 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
559 return Tmp.split('-').first; // Isolate second component
562 StringRef Triple::getOSName() const {
563 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
564 Tmp = Tmp.split('-').second; // Strip second component
565 return Tmp.split('-').first; // Isolate third component
568 StringRef Triple::getEnvironmentName() const {
569 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
570 Tmp = Tmp.split('-').second; // Strip second component
571 return Tmp.split('-').second; // Strip third component
574 StringRef Triple::getOSAndEnvironmentName() const {
575 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
576 return Tmp.split('-').second; // Strip second component
579 static unsigned EatNumber(StringRef &Str) {
580 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
584 // Consume the leading digit.
585 Result = Result*10 + (Str[0] - '0');
589 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
594 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
595 unsigned &Micro) const {
596 StringRef OSName = getOSName();
598 // Assume that the OS portion of the triple starts with the canonical name.
599 StringRef OSTypeName = getOSTypeName(getOS());
600 if (OSName.startswith(OSTypeName))
601 OSName = OSName.substr(OSTypeName.size());
603 // Any unset version defaults to 0.
604 Major = Minor = Micro = 0;
606 // Parse up to three components.
607 unsigned *Components[3] = { &Major, &Minor, &Micro };
608 for (unsigned i = 0; i != 3; ++i) {
609 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
612 // Consume the leading number.
613 *Components[i] = EatNumber(OSName);
615 // Consume the separator, if present.
616 if (OSName.startswith("."))
617 OSName = OSName.substr(1);
621 void Triple::setTriple(const Twine &Str) {
626 void Triple::setArch(ArchType Kind) {
627 setArchName(getArchTypeName(Kind));
630 void Triple::setVendor(VendorType Kind) {
631 setVendorName(getVendorTypeName(Kind));
634 void Triple::setOS(OSType Kind) {
635 setOSName(getOSTypeName(Kind));
638 void Triple::setEnvironment(EnvironmentType Kind) {
639 setEnvironmentName(getEnvironmentTypeName(Kind));
642 void Triple::setArchName(StringRef Str) {
643 // Work around a miscompilation bug for Twines in gcc 4.0.3.
644 SmallString<64> Triple;
647 Triple += getVendorName();
649 Triple += getOSAndEnvironmentName();
650 setTriple(Triple.str());
653 void Triple::setVendorName(StringRef Str) {
654 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
657 void Triple::setOSName(StringRef Str) {
658 if (hasEnvironment())
659 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
660 "-" + getEnvironmentName());
662 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
665 void Triple::setEnvironmentName(StringRef Str) {
666 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
670 void Triple::setOSAndEnvironmentName(StringRef Str) {
671 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);