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 hexagon: return "hexagon";
24 case mips: return "mips";
25 case mipsel: return "mipsel";
26 case mips64: return "mips64";
27 case mips64el:return "mips64el";
28 case msp430: return "msp430";
29 case ppc64: return "powerpc64";
30 case ppc: return "powerpc";
31 case sparc: return "sparc";
32 case sparcv9: return "sparcv9";
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) {
54 case thumb: return "arm";
56 case cellspu: return "spu";
59 case ppc: return "ppc";
61 case mblaze: return "mblaze";
63 case hexagon: return "hexagon";
66 case sparc: return "sparc";
69 case x86_64: return "x86";
71 case xcore: return "xcore";
73 case ptx32: return "ptx";
74 case ptx64: return "ptx";
75 case le32: return "le32";
76 case amdil: return "amdil";
80 const char *Triple::getVendorTypeName(VendorType Kind) {
82 case UnknownVendor: return "unknown";
84 case Apple: return "apple";
86 case SCEI: return "scei";
92 const char *Triple::getOSTypeName(OSType Kind) {
94 case UnknownOS: return "unknown";
96 case AuroraUX: return "auroraux";
97 case Cygwin: return "cygwin";
98 case Darwin: return "darwin";
99 case DragonFly: return "dragonfly";
100 case FreeBSD: return "freebsd";
101 case IOS: return "ios";
102 case KFreeBSD: return "kfreebsd";
103 case Linux: return "linux";
104 case Lv2: return "lv2";
105 case MacOSX: return "macosx";
106 case MinGW32: return "mingw32";
107 case NetBSD: return "netbsd";
108 case OpenBSD: return "openbsd";
109 case Psp: return "psp";
110 case Solaris: return "solaris";
111 case Win32: return "win32";
112 case Haiku: return "haiku";
113 case Minix: return "minix";
114 case RTEMS: return "rtems";
115 case NativeClient: return "nacl";
121 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
123 case UnknownEnvironment: return "unknown";
124 case GNU: return "gnu";
125 case GNUEABIHF: return "gnueabihf";
126 case GNUEABI: return "gnueabi";
127 case EABI: return "eabi";
128 case MachO: return "macho";
129 case ANDROIDEABI: return "androideabi";
135 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
138 if (Name == "cellspu")
142 if (Name == "mipsel")
144 if (Name == "mips64")
146 if (Name == "mips64el")
148 if (Name == "msp430")
156 if (Name == "mblaze")
158 if (Name == "hexagon")
162 if (Name == "sparcv9")
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 == "powerpc")
280 else if ((ArchName == "powerpc64") || (ArchName == "ppu"))
282 else if (ArchName == "mblaze")
284 else if (ArchName == "arm" ||
285 ArchName.startswith("armv") ||
286 ArchName == "xscale")
288 else if (ArchName == "thumb" ||
289 ArchName.startswith("thumbv"))
291 else if (ArchName == "spu" || ArchName == "cellspu")
293 else if (ArchName == "msp430")
295 else if (ArchName == "mips" || ArchName == "mipseb" ||
296 ArchName == "mipsallegrex")
298 else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
301 else if (ArchName == "mips64" || ArchName == "mips64eb")
303 else if (ArchName == "mips64el")
305 else if (ArchName == "hexagon")
307 else if (ArchName == "sparc")
309 else if (ArchName == "sparcv9")
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("gnueabihf"))
388 else if (EnvironmentName.startswith("gnueabi"))
390 else if (EnvironmentName.startswith("gnu"))
392 else if (EnvironmentName.startswith("macho"))
394 else if (EnvironmentName.startswith("androideabi"))
397 return UnknownEnvironment;
400 void Triple::Parse() const {
401 assert(!isInitialized() && "Invalid parse call.");
403 Arch = ParseArch(getArchName());
404 Vendor = ParseVendor(getVendorName());
405 OS = ParseOS(getOSName());
406 Environment = ParseEnvironment(getEnvironmentName());
408 assert(isInitialized() && "Failed to initialize!");
411 std::string Triple::normalize(StringRef Str) {
412 // Parse into components.
413 SmallVector<StringRef, 4> Components;
414 for (size_t First = 0, Last = 0; Last != StringRef::npos; First = Last + 1) {
415 Last = Str.find('-', First);
416 Components.push_back(Str.slice(First, Last));
419 // If the first component corresponds to a known architecture, preferentially
420 // use it for the architecture. If the second component corresponds to a
421 // known vendor, preferentially use it for the vendor, etc. This avoids silly
422 // component movement when a component parses as (eg) both a valid arch and a
424 ArchType Arch = UnknownArch;
425 if (Components.size() > 0)
426 Arch = ParseArch(Components[0]);
427 VendorType Vendor = UnknownVendor;
428 if (Components.size() > 1)
429 Vendor = ParseVendor(Components[1]);
430 OSType OS = UnknownOS;
431 if (Components.size() > 2)
432 OS = ParseOS(Components[2]);
433 EnvironmentType Environment = UnknownEnvironment;
434 if (Components.size() > 3)
435 Environment = ParseEnvironment(Components[3]);
437 // Note which components are already in their final position. These will not
440 Found[0] = Arch != UnknownArch;
441 Found[1] = Vendor != UnknownVendor;
442 Found[2] = OS != UnknownOS;
443 Found[3] = Environment != UnknownEnvironment;
445 // If they are not there already, permute the components into their canonical
446 // positions by seeing if they parse as a valid architecture, and if so moving
447 // the component to the architecture position etc.
448 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
450 continue; // Already in the canonical position.
452 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
453 // Do not reparse any components that already matched.
454 if (Idx < array_lengthof(Found) && Found[Idx])
457 // Does this component parse as valid for the target position?
459 StringRef Comp = Components[Idx];
462 assert(false && "unexpected component type!");
464 Arch = ParseArch(Comp);
465 Valid = Arch != UnknownArch;
468 Vendor = ParseVendor(Comp);
469 Valid = Vendor != UnknownVendor;
473 Valid = OS != UnknownOS;
476 Environment = ParseEnvironment(Comp);
477 Valid = Environment != UnknownEnvironment;
481 continue; // Nope, try the next component.
483 // Move the component to the target position, pushing any non-fixed
484 // components that are in the way to the right. This tends to give
485 // good results in the common cases of a forgotten vendor component
486 // or a wrongly positioned environment.
488 // Insert left, pushing the existing components to the right. For
489 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
490 StringRef CurrentComponent(""); // The empty component.
491 // Replace the component we are moving with an empty component.
492 std::swap(CurrentComponent, Components[Idx]);
493 // Insert the component being moved at Pos, displacing any existing
494 // components to the right.
495 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
496 // Skip over any fixed components.
497 while (i < array_lengthof(Found) && Found[i]) ++i;
498 // Place the component at the new position, getting the component
499 // that was at this position - it will be moved right.
500 std::swap(CurrentComponent, Components[i]);
502 } else if (Pos > Idx) {
503 // Push right by inserting empty components until the component at Idx
504 // reaches the target position Pos. For example, pc-a -> -pc-a when
505 // moving pc to the second position.
507 // Insert one empty component at Idx.
508 StringRef CurrentComponent(""); // The empty component.
509 for (unsigned i = Idx; i < Components.size();) {
510 // Place the component at the new position, getting the component
511 // that was at this position - it will be moved right.
512 std::swap(CurrentComponent, Components[i]);
513 // If it was placed on top of an empty component then we are done.
514 if (CurrentComponent.empty())
516 // Advance to the next component, skipping any fixed components.
517 while (++i < array_lengthof(Found) && Found[i])
520 // The last component was pushed off the end - append it.
521 if (!CurrentComponent.empty())
522 Components.push_back(CurrentComponent);
524 // Advance Idx to the component's new position.
525 while (++Idx < array_lengthof(Found) && Found[Idx]) {}
526 } while (Idx < Pos); // Add more until the final position is reached.
528 assert(Pos < Components.size() && Components[Pos] == Comp &&
529 "Component moved wrong!");
535 // Special case logic goes here. At this point Arch, Vendor and OS have the
536 // correct values for the computed components.
538 // Stick the corrected components back together to form the normalized string.
539 std::string Normalized;
540 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
541 if (i) Normalized += '-';
542 Normalized += Components[i];
547 StringRef Triple::getArchName() const {
548 return StringRef(Data).split('-').first; // Isolate first component
551 StringRef Triple::getVendorName() const {
552 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
553 return Tmp.split('-').first; // Isolate second component
556 StringRef Triple::getOSName() const {
557 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
558 Tmp = Tmp.split('-').second; // Strip second component
559 return Tmp.split('-').first; // Isolate third component
562 StringRef Triple::getEnvironmentName() const {
563 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
564 Tmp = Tmp.split('-').second; // Strip second component
565 return Tmp.split('-').second; // Strip third component
568 StringRef Triple::getOSAndEnvironmentName() const {
569 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
570 return Tmp.split('-').second; // Strip second component
573 static unsigned EatNumber(StringRef &Str) {
574 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
578 // Consume the leading digit.
579 Result = Result*10 + (Str[0] - '0');
583 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
588 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
589 unsigned &Micro) const {
590 StringRef OSName = getOSName();
592 // Assume that the OS portion of the triple starts with the canonical name.
593 StringRef OSTypeName = getOSTypeName(getOS());
594 if (OSName.startswith(OSTypeName))
595 OSName = OSName.substr(OSTypeName.size());
597 // Any unset version defaults to 0.
598 Major = Minor = Micro = 0;
600 // Parse up to three components.
601 unsigned *Components[3] = { &Major, &Minor, &Micro };
602 for (unsigned i = 0; i != 3; ++i) {
603 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
606 // Consume the leading number.
607 *Components[i] = EatNumber(OSName);
609 // Consume the separator, if present.
610 if (OSName.startswith("."))
611 OSName = OSName.substr(1);
615 void Triple::setTriple(const Twine &Str) {
620 void Triple::setArch(ArchType Kind) {
621 setArchName(getArchTypeName(Kind));
624 void Triple::setVendor(VendorType Kind) {
625 setVendorName(getVendorTypeName(Kind));
628 void Triple::setOS(OSType Kind) {
629 setOSName(getOSTypeName(Kind));
632 void Triple::setEnvironment(EnvironmentType Kind) {
633 setEnvironmentName(getEnvironmentTypeName(Kind));
636 void Triple::setArchName(StringRef Str) {
637 // Work around a miscompilation bug for Twines in gcc 4.0.3.
638 SmallString<64> Triple;
641 Triple += getVendorName();
643 Triple += getOSAndEnvironmentName();
644 setTriple(Triple.str());
647 void Triple::setVendorName(StringRef Str) {
648 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
651 void Triple::setOSName(StringRef Str) {
652 if (hasEnvironment())
653 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
654 "-" + getEnvironmentName());
656 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
659 void Triple::setEnvironmentName(StringRef Str) {
660 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
664 void Triple::setOSAndEnvironmentName(StringRef Str) {
665 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);