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 msp430: return "msp430";
28 case ppc64: return "powerpc64";
29 case ppc: return "powerpc";
30 case sparc: return "sparc";
31 case sparcv9: return "sparcv9";
32 case systemz: return "s390x";
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";
48 const char *Triple::getArchTypePrefix(ArchType Kind) {
53 case alpha: return "alpha";
56 case thumb: return "arm";
58 case bfin: return "bfin";
60 case cellspu: return "spu";
63 case ppc: return "ppc";
65 case mblaze: return "mblaze";
68 case sparc: return "sparc";
71 case x86_64: return "x86";
73 case xcore: return "xcore";
75 case ptx32: return "ptx";
76 case ptx64: return "ptx";
77 case le32: return "le32";
78 case amdil: return "amdil";
82 const char *Triple::getVendorTypeName(VendorType Kind) {
84 case UnknownVendor: return "unknown";
86 case Apple: return "apple";
88 case SCEI: return "scei";
94 const char *Triple::getOSTypeName(OSType Kind) {
96 case UnknownOS: return "unknown";
98 case AuroraUX: return "auroraux";
99 case Cygwin: return "cygwin";
100 case Darwin: return "darwin";
101 case DragonFly: return "dragonfly";
102 case FreeBSD: return "freebsd";
103 case IOS: return "ios";
104 case KFreeBSD: return "kfreebsd";
105 case Linux: return "linux";
106 case Lv2: return "lv2";
107 case MacOSX: return "macosx";
108 case MinGW32: return "mingw32";
109 case NetBSD: return "netbsd";
110 case OpenBSD: return "openbsd";
111 case Psp: return "psp";
112 case Solaris: return "solaris";
113 case Win32: return "win32";
114 case Haiku: return "haiku";
115 case Minix: return "minix";
116 case RTEMS: return "rtems";
117 case NativeClient: return "nacl";
123 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
125 case UnknownEnvironment: return "unknown";
126 case GNU: return "gnu";
127 case GNUEABI: return "gnueabi";
128 case EABI: return "eabi";
129 case MachO: return "macho";
135 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
142 if (Name == "cellspu")
146 if (Name == "mipsel")
148 if (Name == "msp430")
156 if (Name == "mblaze")
160 if (Name == "sparcv9")
162 if (Name == "systemz")
170 if (Name == "x86-64")
186 Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
187 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
188 // archs which Darwin doesn't use.
190 // The matching this routine does is fairly pointless, since it is neither the
191 // complete architecture list, nor a reasonable subset. The problem is that
192 // historically the driver driver accepts this and also ties its -march=
193 // handling to the architecture name, so we need to be careful before removing
196 // This code must be kept in sync with Clang's Darwin specific argument
199 if (Str == "ppc" || Str == "ppc601" || Str == "ppc603" || Str == "ppc604" ||
200 Str == "ppc604e" || Str == "ppc750" || Str == "ppc7400" ||
201 Str == "ppc7450" || Str == "ppc970")
205 return Triple::ppc64;
207 if (Str == "i386" || Str == "i486" || Str == "i486SX" || Str == "pentium" ||
208 Str == "i586" || Str == "pentpro" || Str == "i686" || Str == "pentIIm3" ||
209 Str == "pentIIm5" || Str == "pentium4")
213 return Triple::x86_64;
215 // This is derived from the driver driver.
216 if (Str == "arm" || Str == "armv4t" || Str == "armv5" || Str == "xscale" ||
217 Str == "armv6" || Str == "armv7" || Str == "armv7f" || Str == "armv7k" ||
222 return Triple::ptx32;
224 return Triple::ptx64;
226 return Triple::amdil;
228 return Triple::UnknownArch;
231 // Returns architecture name that is understood by the target assembler.
232 const char *Triple::getArchNameForAssembler() {
233 if (!isOSDarwin() && getVendor() != Triple::Apple)
236 StringRef Str = getArchName();
241 if (Str == "powerpc")
243 if (Str == "powerpc64")
245 if (Str == "mblaze" || Str == "microblaze")
249 if (Str == "armv4t" || Str == "thumbv4t")
251 if (Str == "armv5" || Str == "armv5e" || Str == "thumbv5"
252 || Str == "thumbv5e")
254 if (Str == "armv6" || Str == "thumbv6")
256 if (Str == "armv7" || Str == "thumbv7")
271 Triple::ArchType Triple::ParseArch(StringRef ArchName) {
272 if (ArchName.size() == 4 && ArchName[0] == 'i' &&
273 ArchName[2] == '8' && ArchName[3] == '6' &&
274 ArchName[1] - '3' < 6) // i[3-9]86
276 else if (ArchName == "amd64" || ArchName == "x86_64")
278 else if (ArchName == "bfin")
280 else if (ArchName == "powerpc")
282 else if ((ArchName == "powerpc64") || (ArchName == "ppu"))
284 else if (ArchName == "mblaze")
286 else if (ArchName == "arm" ||
287 ArchName.startswith("armv") ||
288 ArchName == "xscale")
290 else if (ArchName == "thumb" ||
291 ArchName.startswith("thumbv"))
293 else if (ArchName.startswith("alpha"))
295 else if (ArchName == "spu" || ArchName == "cellspu")
297 else if (ArchName == "msp430")
299 else if (ArchName == "mips" || ArchName == "mipseb" ||
300 ArchName == "mipsallegrex")
302 else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
305 else if (ArchName == "sparc")
307 else if (ArchName == "sparcv9")
309 else if (ArchName == "s390x")
311 else if (ArchName == "tce")
313 else if (ArchName == "xcore")
315 else if (ArchName == "ptx32")
317 else if (ArchName == "ptx64")
319 else if (ArchName == "le32")
321 else if (ArchName == "amdil")
327 Triple::VendorType Triple::ParseVendor(StringRef VendorName) {
328 if (VendorName == "apple")
330 else if (VendorName == "pc")
332 else if (VendorName == "scei")
335 return UnknownVendor;
338 Triple::OSType Triple::ParseOS(StringRef OSName) {
339 if (OSName.startswith("auroraux"))
341 else if (OSName.startswith("cygwin"))
343 else if (OSName.startswith("darwin"))
345 else if (OSName.startswith("dragonfly"))
347 else if (OSName.startswith("freebsd"))
349 else if (OSName.startswith("ios"))
351 else if (OSName.startswith("kfreebsd"))
353 else if (OSName.startswith("linux"))
355 else if (OSName.startswith("lv2"))
357 else if (OSName.startswith("macosx"))
359 else if (OSName.startswith("mingw32"))
361 else if (OSName.startswith("netbsd"))
363 else if (OSName.startswith("openbsd"))
365 else if (OSName.startswith("psp"))
367 else if (OSName.startswith("solaris"))
369 else if (OSName.startswith("win32"))
371 else if (OSName.startswith("haiku"))
373 else if (OSName.startswith("minix"))
375 else if (OSName.startswith("rtems"))
377 else if (OSName.startswith("nacl"))
383 Triple::EnvironmentType Triple::ParseEnvironment(StringRef EnvironmentName) {
384 if (EnvironmentName.startswith("eabi"))
386 else if (EnvironmentName.startswith("gnueabi"))
388 else if (EnvironmentName.startswith("gnu"))
390 else if (EnvironmentName.startswith("macho"))
393 return UnknownEnvironment;
396 void Triple::Parse() const {
397 assert(!isInitialized() && "Invalid parse call.");
399 Arch = ParseArch(getArchName());
400 Vendor = ParseVendor(getVendorName());
401 OS = ParseOS(getOSName());
402 Environment = ParseEnvironment(getEnvironmentName());
404 assert(isInitialized() && "Failed to initialize!");
407 std::string Triple::normalize(StringRef Str) {
408 // Parse into components.
409 SmallVector<StringRef, 4> Components;
410 for (size_t First = 0, Last = 0; Last != StringRef::npos; First = Last + 1) {
411 Last = Str.find('-', First);
412 Components.push_back(Str.slice(First, Last));
415 // If the first component corresponds to a known architecture, preferentially
416 // use it for the architecture. If the second component corresponds to a
417 // known vendor, preferentially use it for the vendor, etc. This avoids silly
418 // component movement when a component parses as (eg) both a valid arch and a
420 ArchType Arch = UnknownArch;
421 if (Components.size() > 0)
422 Arch = ParseArch(Components[0]);
423 VendorType Vendor = UnknownVendor;
424 if (Components.size() > 1)
425 Vendor = ParseVendor(Components[1]);
426 OSType OS = UnknownOS;
427 if (Components.size() > 2)
428 OS = ParseOS(Components[2]);
429 EnvironmentType Environment = UnknownEnvironment;
430 if (Components.size() > 3)
431 Environment = ParseEnvironment(Components[3]);
433 // Note which components are already in their final position. These will not
436 Found[0] = Arch != UnknownArch;
437 Found[1] = Vendor != UnknownVendor;
438 Found[2] = OS != UnknownOS;
439 Found[3] = Environment != UnknownEnvironment;
441 // If they are not there already, permute the components into their canonical
442 // positions by seeing if they parse as a valid architecture, and if so moving
443 // the component to the architecture position etc.
444 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
446 continue; // Already in the canonical position.
448 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
449 // Do not reparse any components that already matched.
450 if (Idx < array_lengthof(Found) && Found[Idx])
453 // Does this component parse as valid for the target position?
455 StringRef Comp = Components[Idx];
458 assert(false && "unexpected component type!");
460 Arch = ParseArch(Comp);
461 Valid = Arch != UnknownArch;
464 Vendor = ParseVendor(Comp);
465 Valid = Vendor != UnknownVendor;
469 Valid = OS != UnknownOS;
472 Environment = ParseEnvironment(Comp);
473 Valid = Environment != UnknownEnvironment;
477 continue; // Nope, try the next component.
479 // Move the component to the target position, pushing any non-fixed
480 // components that are in the way to the right. This tends to give
481 // good results in the common cases of a forgotten vendor component
482 // or a wrongly positioned environment.
484 // Insert left, pushing the existing components to the right. For
485 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
486 StringRef CurrentComponent(""); // The empty component.
487 // Replace the component we are moving with an empty component.
488 std::swap(CurrentComponent, Components[Idx]);
489 // Insert the component being moved at Pos, displacing any existing
490 // components to the right.
491 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
492 // Skip over any fixed components.
493 while (i < array_lengthof(Found) && Found[i]) ++i;
494 // Place the component at the new position, getting the component
495 // that was at this position - it will be moved right.
496 std::swap(CurrentComponent, Components[i]);
498 } else if (Pos > Idx) {
499 // Push right by inserting empty components until the component at Idx
500 // reaches the target position Pos. For example, pc-a -> -pc-a when
501 // moving pc to the second position.
503 // Insert one empty component at Idx.
504 StringRef CurrentComponent(""); // The empty component.
505 for (unsigned i = Idx; i < Components.size();) {
506 // Place the component at the new position, getting the component
507 // that was at this position - it will be moved right.
508 std::swap(CurrentComponent, Components[i]);
509 // If it was placed on top of an empty component then we are done.
510 if (CurrentComponent.empty())
512 // Advance to the next component, skipping any fixed components.
513 while (++i < array_lengthof(Found) && Found[i])
516 // The last component was pushed off the end - append it.
517 if (!CurrentComponent.empty())
518 Components.push_back(CurrentComponent);
520 // Advance Idx to the component's new position.
521 while (++Idx < array_lengthof(Found) && Found[Idx]) {}
522 } while (Idx < Pos); // Add more until the final position is reached.
524 assert(Pos < Components.size() && Components[Pos] == Comp &&
525 "Component moved wrong!");
531 // Special case logic goes here. At this point Arch, Vendor and OS have the
532 // correct values for the computed components.
534 // Stick the corrected components back together to form the normalized string.
535 std::string Normalized;
536 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
537 if (i) Normalized += '-';
538 Normalized += Components[i];
543 StringRef Triple::getArchName() const {
544 return StringRef(Data).split('-').first; // Isolate first component
547 StringRef Triple::getVendorName() const {
548 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
549 return Tmp.split('-').first; // Isolate second component
552 StringRef Triple::getOSName() const {
553 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
554 Tmp = Tmp.split('-').second; // Strip second component
555 return Tmp.split('-').first; // Isolate third component
558 StringRef Triple::getEnvironmentName() const {
559 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
560 Tmp = Tmp.split('-').second; // Strip second component
561 return Tmp.split('-').second; // Strip third component
564 StringRef Triple::getOSAndEnvironmentName() const {
565 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
566 return Tmp.split('-').second; // Strip second component
569 static unsigned EatNumber(StringRef &Str) {
570 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
574 // Consume the leading digit.
575 Result = Result*10 + (Str[0] - '0');
579 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
584 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
585 unsigned &Micro) const {
586 StringRef OSName = getOSName();
588 // Assume that the OS portion of the triple starts with the canonical name.
589 StringRef OSTypeName = getOSTypeName(getOS());
590 if (OSName.startswith(OSTypeName))
591 OSName = OSName.substr(OSTypeName.size());
593 // Any unset version defaults to 0.
594 Major = Minor = Micro = 0;
596 // Parse up to three components.
597 unsigned *Components[3] = { &Major, &Minor, &Micro };
598 for (unsigned i = 0; i != 3; ++i) {
599 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
602 // Consume the leading number.
603 *Components[i] = EatNumber(OSName);
605 // Consume the separator, if present.
606 if (OSName.startswith("."))
607 OSName = OSName.substr(1);
611 void Triple::setTriple(const Twine &Str) {
616 void Triple::setArch(ArchType Kind) {
617 setArchName(getArchTypeName(Kind));
620 void Triple::setVendor(VendorType Kind) {
621 setVendorName(getVendorTypeName(Kind));
624 void Triple::setOS(OSType Kind) {
625 setOSName(getOSTypeName(Kind));
628 void Triple::setEnvironment(EnvironmentType Kind) {
629 setEnvironmentName(getEnvironmentTypeName(Kind));
632 void Triple::setArchName(StringRef Str) {
633 // Work around a miscompilation bug for Twines in gcc 4.0.3.
634 SmallString<64> Triple;
637 Triple += getVendorName();
639 Triple += getOSAndEnvironmentName();
640 setTriple(Triple.str());
643 void Triple::setVendorName(StringRef Str) {
644 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
647 void Triple::setOSName(StringRef Str) {
648 if (hasEnvironment())
649 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
650 "-" + getEnvironmentName());
652 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
655 void Triple::setEnvironmentName(StringRef Str) {
656 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
660 void Triple::setOSAndEnvironmentName(StringRef Str) {
661 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);