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";
47 const char *Triple::getArchTypePrefix(ArchType Kind) {
52 case alpha: return "alpha";
55 case thumb: return "arm";
57 case bfin: return "bfin";
59 case cellspu: return "spu";
62 case ppc: return "ppc";
64 case mblaze: return "mblaze";
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";
77 case le32: return "le32";
81 const char *Triple::getVendorTypeName(VendorType Kind) {
83 case UnknownVendor: return "unknown";
85 case Apple: return "apple";
87 case SCEI: return "scei";
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";
122 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
124 case UnknownEnvironment: return "unknown";
125 case GNU: return "gnu";
126 case GNUEABI: return "gnueabi";
127 case EABI: return "eabi";
128 case MachO: return "macho";
134 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
141 if (Name == "cellspu")
145 if (Name == "mipsel")
147 if (Name == "msp430")
155 if (Name == "mblaze")
159 if (Name == "sparcv9")
161 if (Name == "systemz")
169 if (Name == "x86-64")
183 Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
184 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
185 // archs which Darwin doesn't use.
187 // The matching this routine does is fairly pointless, since it is neither the
188 // complete architecture list, nor a reasonable subset. The problem is that
189 // historically the driver driver accepts this and also ties its -march=
190 // handling to the architecture name, so we need to be careful before removing
193 // This code must be kept in sync with Clang's Darwin specific argument
196 if (Str == "ppc" || Str == "ppc601" || Str == "ppc603" || Str == "ppc604" ||
197 Str == "ppc604e" || Str == "ppc750" || Str == "ppc7400" ||
198 Str == "ppc7450" || Str == "ppc970")
202 return Triple::ppc64;
204 if (Str == "i386" || Str == "i486" || Str == "i486SX" || Str == "pentium" ||
205 Str == "i586" || Str == "pentpro" || Str == "i686" || Str == "pentIIm3" ||
206 Str == "pentIIm5" || Str == "pentium4")
210 return Triple::x86_64;
212 // This is derived from the driver driver.
213 if (Str == "arm" || Str == "armv4t" || Str == "armv5" || Str == "xscale" ||
214 Str == "armv6" || Str == "armv7" || Str == "armv7f" || Str == "armv7k" ||
219 return Triple::ptx32;
221 return Triple::ptx64;
223 return Triple::UnknownArch;
226 // Returns architecture name that is understood by the target assembler.
227 const char *Triple::getArchNameForAssembler() {
228 if (!isOSDarwin() && getVendor() != Triple::Apple)
231 StringRef Str = getArchName();
236 if (Str == "powerpc")
238 if (Str == "powerpc64")
240 if (Str == "mblaze" || Str == "microblaze")
244 if (Str == "armv4t" || Str == "thumbv4t")
246 if (Str == "armv5" || Str == "armv5e" || Str == "thumbv5"
247 || Str == "thumbv5e")
249 if (Str == "armv6" || Str == "thumbv6")
251 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 == "bfin")
273 else if (ArchName == "powerpc")
275 else if ((ArchName == "powerpc64") || (ArchName == "ppu"))
277 else if (ArchName == "mblaze")
279 else if (ArchName == "arm" ||
280 ArchName.startswith("armv") ||
281 ArchName == "xscale")
283 else if (ArchName == "thumb" ||
284 ArchName.startswith("thumbv"))
286 else if (ArchName.startswith("alpha"))
288 else if (ArchName == "spu" || ArchName == "cellspu")
290 else if (ArchName == "msp430")
292 else if (ArchName == "mips" || ArchName == "mipseb" ||
293 ArchName == "mipsallegrex")
295 else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
298 else if (ArchName == "sparc")
300 else if (ArchName == "sparcv9")
302 else if (ArchName == "s390x")
304 else if (ArchName == "tce")
306 else if (ArchName == "xcore")
308 else if (ArchName == "ptx32")
310 else if (ArchName == "ptx64")
312 else if (ArchName == "le32")
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);