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 arm: return "arm";
22 case cellspu: return "cellspu";
23 case mips: return "mips";
24 case mipsel: return "mipsel";
25 case mips64: return "mips64";
26 case mips64el:return "mips64el";
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 tce: return "tce";
33 case thumb: return "thumb";
34 case x86: return "i386";
35 case x86_64: return "x86_64";
36 case xcore: return "xcore";
37 case mblaze: return "mblaze";
38 case ptx32: return "ptx32";
39 case ptx64: return "ptx64";
40 case le32: return "le32";
41 case amdil: return "amdil";
47 const char *Triple::getArchTypePrefix(ArchType Kind) {
53 case thumb: return "arm";
55 case cellspu: return "spu";
58 case ppc: return "ppc";
60 case mblaze: return "mblaze";
63 case sparc: return "sparc";
66 case x86_64: return "x86";
68 case xcore: return "xcore";
70 case ptx32: return "ptx";
71 case ptx64: return "ptx";
72 case le32: return "le32";
73 case amdil: return "amdil";
77 const char *Triple::getVendorTypeName(VendorType Kind) {
79 case UnknownVendor: return "unknown";
81 case Apple: return "apple";
83 case SCEI: return "scei";
89 const char *Triple::getOSTypeName(OSType Kind) {
91 case UnknownOS: return "unknown";
93 case AuroraUX: return "auroraux";
94 case Cygwin: return "cygwin";
95 case Darwin: return "darwin";
96 case DragonFly: return "dragonfly";
97 case FreeBSD: return "freebsd";
98 case IOS: return "ios";
99 case KFreeBSD: return "kfreebsd";
100 case Linux: return "linux";
101 case Lv2: return "lv2";
102 case MacOSX: return "macosx";
103 case MinGW32: return "mingw32";
104 case NetBSD: return "netbsd";
105 case OpenBSD: return "openbsd";
106 case Psp: return "psp";
107 case Solaris: return "solaris";
108 case Win32: return "win32";
109 case Haiku: return "haiku";
110 case Minix: return "minix";
111 case RTEMS: return "rtems";
112 case NativeClient: return "nacl";
118 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
120 case UnknownEnvironment: return "unknown";
121 case GNU: return "gnu";
122 case GNUEABI: return "gnueabi";
123 case EABI: return "eabi";
124 case MachO: return "macho";
130 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
133 if (Name == "cellspu")
137 if (Name == "mipsel")
139 if (Name == "mips64")
141 if (Name == "mips64el")
143 if (Name == "msp430")
151 if (Name == "mblaze")
155 if (Name == "sparcv9")
163 if (Name == "x86-64")
179 Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
180 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
181 // archs which Darwin doesn't use.
183 // The matching this routine does is fairly pointless, since it is neither the
184 // complete architecture list, nor a reasonable subset. The problem is that
185 // historically the driver driver accepts this and also ties its -march=
186 // handling to the architecture name, so we need to be careful before removing
189 // This code must be kept in sync with Clang's Darwin specific argument
192 if (Str == "ppc" || Str == "ppc601" || Str == "ppc603" || Str == "ppc604" ||
193 Str == "ppc604e" || Str == "ppc750" || Str == "ppc7400" ||
194 Str == "ppc7450" || Str == "ppc970")
198 return Triple::ppc64;
200 if (Str == "i386" || Str == "i486" || Str == "i486SX" || Str == "pentium" ||
201 Str == "i586" || Str == "pentpro" || Str == "i686" || Str == "pentIIm3" ||
202 Str == "pentIIm5" || Str == "pentium4")
206 return Triple::x86_64;
208 // This is derived from the driver driver.
209 if (Str == "arm" || Str == "armv4t" || Str == "armv5" || Str == "xscale" ||
210 Str == "armv6" || Str == "armv7" || Str == "armv7f" || Str == "armv7k" ||
215 return Triple::ptx32;
217 return Triple::ptx64;
219 return Triple::amdil;
221 return Triple::UnknownArch;
224 // Returns architecture name that is understood by the target assembler.
225 const char *Triple::getArchNameForAssembler() {
226 if (!isOSDarwin() && getVendor() != Triple::Apple)
229 StringRef Str = getArchName();
234 if (Str == "powerpc")
236 if (Str == "powerpc64")
238 if (Str == "mblaze" || Str == "microblaze")
242 if (Str == "armv4t" || Str == "thumbv4t")
244 if (Str == "armv5" || Str == "armv5e" || Str == "thumbv5"
245 || Str == "thumbv5e")
247 if (Str == "armv6" || Str == "thumbv6")
249 if (Str == "armv7" || Str == "thumbv7")
264 Triple::ArchType Triple::ParseArch(StringRef ArchName) {
265 if (ArchName.size() == 4 && ArchName[0] == 'i' &&
266 ArchName[2] == '8' && ArchName[3] == '6' &&
267 ArchName[1] - '3' < 6) // i[3-9]86
269 else if (ArchName == "amd64" || ArchName == "x86_64")
271 else if (ArchName == "powerpc")
273 else if ((ArchName == "powerpc64") || (ArchName == "ppu"))
275 else if (ArchName == "mblaze")
277 else if (ArchName == "arm" ||
278 ArchName.startswith("armv") ||
279 ArchName == "xscale")
281 else if (ArchName == "thumb" ||
282 ArchName.startswith("thumbv"))
284 else if (ArchName == "spu" || ArchName == "cellspu")
286 else if (ArchName == "msp430")
288 else if (ArchName == "mips" || ArchName == "mipseb" ||
289 ArchName == "mipsallegrex")
291 else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
294 else if (ArchName == "mips64" || ArchName == "mips64eb")
296 else if (ArchName == "mips64el")
298 else if (ArchName == "sparc")
300 else if (ArchName == "sparcv9")
302 else if (ArchName == "tce")
304 else if (ArchName == "xcore")
306 else if (ArchName == "ptx32")
308 else if (ArchName == "ptx64")
310 else if (ArchName == "le32")
312 else if (ArchName == "amdil")
318 Triple::VendorType Triple::ParseVendor(StringRef VendorName) {
319 if (VendorName == "apple")
321 else if (VendorName == "pc")
323 else if (VendorName == "scei")
326 return UnknownVendor;
329 Triple::OSType Triple::ParseOS(StringRef OSName) {
330 if (OSName.startswith("auroraux"))
332 else if (OSName.startswith("cygwin"))
334 else if (OSName.startswith("darwin"))
336 else if (OSName.startswith("dragonfly"))
338 else if (OSName.startswith("freebsd"))
340 else if (OSName.startswith("ios"))
342 else if (OSName.startswith("kfreebsd"))
344 else if (OSName.startswith("linux"))
346 else if (OSName.startswith("lv2"))
348 else if (OSName.startswith("macosx"))
350 else if (OSName.startswith("mingw32"))
352 else if (OSName.startswith("netbsd"))
354 else if (OSName.startswith("openbsd"))
356 else if (OSName.startswith("psp"))
358 else if (OSName.startswith("solaris"))
360 else if (OSName.startswith("win32"))
362 else if (OSName.startswith("haiku"))
364 else if (OSName.startswith("minix"))
366 else if (OSName.startswith("rtems"))
368 else if (OSName.startswith("nacl"))
374 Triple::EnvironmentType Triple::ParseEnvironment(StringRef EnvironmentName) {
375 if (EnvironmentName.startswith("eabi"))
377 else if (EnvironmentName.startswith("gnueabi"))
379 else if (EnvironmentName.startswith("gnu"))
381 else if (EnvironmentName.startswith("macho"))
384 return UnknownEnvironment;
387 void Triple::Parse() const {
388 assert(!isInitialized() && "Invalid parse call.");
390 Arch = ParseArch(getArchName());
391 Vendor = ParseVendor(getVendorName());
392 OS = ParseOS(getOSName());
393 Environment = ParseEnvironment(getEnvironmentName());
395 assert(isInitialized() && "Failed to initialize!");
398 std::string Triple::normalize(StringRef Str) {
399 // Parse into components.
400 SmallVector<StringRef, 4> Components;
401 for (size_t First = 0, Last = 0; Last != StringRef::npos; First = Last + 1) {
402 Last = Str.find('-', First);
403 Components.push_back(Str.slice(First, Last));
406 // If the first component corresponds to a known architecture, preferentially
407 // use it for the architecture. If the second component corresponds to a
408 // known vendor, preferentially use it for the vendor, etc. This avoids silly
409 // component movement when a component parses as (eg) both a valid arch and a
411 ArchType Arch = UnknownArch;
412 if (Components.size() > 0)
413 Arch = ParseArch(Components[0]);
414 VendorType Vendor = UnknownVendor;
415 if (Components.size() > 1)
416 Vendor = ParseVendor(Components[1]);
417 OSType OS = UnknownOS;
418 if (Components.size() > 2)
419 OS = ParseOS(Components[2]);
420 EnvironmentType Environment = UnknownEnvironment;
421 if (Components.size() > 3)
422 Environment = ParseEnvironment(Components[3]);
424 // Note which components are already in their final position. These will not
427 Found[0] = Arch != UnknownArch;
428 Found[1] = Vendor != UnknownVendor;
429 Found[2] = OS != UnknownOS;
430 Found[3] = Environment != UnknownEnvironment;
432 // If they are not there already, permute the components into their canonical
433 // positions by seeing if they parse as a valid architecture, and if so moving
434 // the component to the architecture position etc.
435 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
437 continue; // Already in the canonical position.
439 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
440 // Do not reparse any components that already matched.
441 if (Idx < array_lengthof(Found) && Found[Idx])
444 // Does this component parse as valid for the target position?
446 StringRef Comp = Components[Idx];
449 assert(false && "unexpected component type!");
451 Arch = ParseArch(Comp);
452 Valid = Arch != UnknownArch;
455 Vendor = ParseVendor(Comp);
456 Valid = Vendor != UnknownVendor;
460 Valid = OS != UnknownOS;
463 Environment = ParseEnvironment(Comp);
464 Valid = Environment != UnknownEnvironment;
468 continue; // Nope, try the next component.
470 // Move the component to the target position, pushing any non-fixed
471 // components that are in the way to the right. This tends to give
472 // good results in the common cases of a forgotten vendor component
473 // or a wrongly positioned environment.
475 // Insert left, pushing the existing components to the right. For
476 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
477 StringRef CurrentComponent(""); // The empty component.
478 // Replace the component we are moving with an empty component.
479 std::swap(CurrentComponent, Components[Idx]);
480 // Insert the component being moved at Pos, displacing any existing
481 // components to the right.
482 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
483 // Skip over any fixed components.
484 while (i < array_lengthof(Found) && Found[i]) ++i;
485 // Place the component at the new position, getting the component
486 // that was at this position - it will be moved right.
487 std::swap(CurrentComponent, Components[i]);
489 } else if (Pos > Idx) {
490 // Push right by inserting empty components until the component at Idx
491 // reaches the target position Pos. For example, pc-a -> -pc-a when
492 // moving pc to the second position.
494 // Insert one empty component at Idx.
495 StringRef CurrentComponent(""); // The empty component.
496 for (unsigned i = Idx; i < Components.size();) {
497 // Place the component at the new position, getting the component
498 // that was at this position - it will be moved right.
499 std::swap(CurrentComponent, Components[i]);
500 // If it was placed on top of an empty component then we are done.
501 if (CurrentComponent.empty())
503 // Advance to the next component, skipping any fixed components.
504 while (++i < array_lengthof(Found) && Found[i])
507 // The last component was pushed off the end - append it.
508 if (!CurrentComponent.empty())
509 Components.push_back(CurrentComponent);
511 // Advance Idx to the component's new position.
512 while (++Idx < array_lengthof(Found) && Found[Idx]) {}
513 } while (Idx < Pos); // Add more until the final position is reached.
515 assert(Pos < Components.size() && Components[Pos] == Comp &&
516 "Component moved wrong!");
522 // Special case logic goes here. At this point Arch, Vendor and OS have the
523 // correct values for the computed components.
525 // Stick the corrected components back together to form the normalized string.
526 std::string Normalized;
527 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
528 if (i) Normalized += '-';
529 Normalized += Components[i];
534 StringRef Triple::getArchName() const {
535 return StringRef(Data).split('-').first; // Isolate first component
538 StringRef Triple::getVendorName() const {
539 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
540 return Tmp.split('-').first; // Isolate second component
543 StringRef Triple::getOSName() const {
544 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
545 Tmp = Tmp.split('-').second; // Strip second component
546 return Tmp.split('-').first; // Isolate third component
549 StringRef Triple::getEnvironmentName() const {
550 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
551 Tmp = Tmp.split('-').second; // Strip second component
552 return Tmp.split('-').second; // Strip third component
555 StringRef Triple::getOSAndEnvironmentName() const {
556 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
557 return Tmp.split('-').second; // Strip second component
560 static unsigned EatNumber(StringRef &Str) {
561 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
565 // Consume the leading digit.
566 Result = Result*10 + (Str[0] - '0');
570 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
575 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
576 unsigned &Micro) const {
577 StringRef OSName = getOSName();
579 // Assume that the OS portion of the triple starts with the canonical name.
580 StringRef OSTypeName = getOSTypeName(getOS());
581 if (OSName.startswith(OSTypeName))
582 OSName = OSName.substr(OSTypeName.size());
584 // Any unset version defaults to 0.
585 Major = Minor = Micro = 0;
587 // Parse up to three components.
588 unsigned *Components[3] = { &Major, &Minor, &Micro };
589 for (unsigned i = 0; i != 3; ++i) {
590 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
593 // Consume the leading number.
594 *Components[i] = EatNumber(OSName);
596 // Consume the separator, if present.
597 if (OSName.startswith("."))
598 OSName = OSName.substr(1);
602 void Triple::setTriple(const Twine &Str) {
607 void Triple::setArch(ArchType Kind) {
608 setArchName(getArchTypeName(Kind));
611 void Triple::setVendor(VendorType Kind) {
612 setVendorName(getVendorTypeName(Kind));
615 void Triple::setOS(OSType Kind) {
616 setOSName(getOSTypeName(Kind));
619 void Triple::setEnvironment(EnvironmentType Kind) {
620 setEnvironmentName(getEnvironmentTypeName(Kind));
623 void Triple::setArchName(StringRef Str) {
624 // Work around a miscompilation bug for Twines in gcc 4.0.3.
625 SmallString<64> Triple;
628 Triple += getVendorName();
630 Triple += getOSAndEnvironmentName();
631 setTriple(Triple.str());
634 void Triple::setVendorName(StringRef Str) {
635 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
638 void Triple::setOSName(StringRef Str) {
639 if (hasEnvironment())
640 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
641 "-" + getEnvironmentName());
643 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
646 void Triple::setEnvironmentName(StringRef Str) {
647 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
651 void Triple::setOSAndEnvironmentName(StringRef Str) {
652 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);