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/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include "llvm/Support/TargetParser.h"
16 #include "llvm/Support/Host.h"
20 const char *Triple::getArchTypeName(ArchType Kind) {
22 case UnknownArch: return "unknown";
24 case aarch64: return "aarch64";
25 case aarch64_be: return "aarch64_be";
26 case arm: return "arm";
27 case armeb: return "armeb";
28 case bpfel: return "bpfel";
29 case bpfeb: return "bpfeb";
30 case hexagon: return "hexagon";
31 case mips: return "mips";
32 case mipsel: return "mipsel";
33 case mips64: return "mips64";
34 case mips64el: return "mips64el";
35 case msp430: return "msp430";
36 case ppc64: return "powerpc64";
37 case ppc64le: return "powerpc64le";
38 case ppc: return "powerpc";
39 case r600: return "r600";
40 case amdgcn: return "amdgcn";
41 case sparc: return "sparc";
42 case sparcv9: return "sparcv9";
43 case sparcel: return "sparcel";
44 case systemz: return "s390x";
45 case tce: return "tce";
46 case thumb: return "thumb";
47 case thumbeb: return "thumbeb";
48 case x86: return "i386";
49 case x86_64: return "x86_64";
50 case xcore: return "xcore";
51 case nvptx: return "nvptx";
52 case nvptx64: return "nvptx64";
53 case le32: return "le32";
54 case le64: return "le64";
55 case amdil: return "amdil";
56 case amdil64: return "amdil64";
57 case hsail: return "hsail";
58 case hsail64: return "hsail64";
59 case spir: return "spir";
60 case spir64: return "spir64";
61 case kalimba: return "kalimba";
62 case shave: return "shave";
63 case wasm32: return "wasm32";
64 case wasm64: return "wasm64";
67 llvm_unreachable("Invalid ArchType!");
70 const char *Triple::getArchTypePrefix(ArchType Kind) {
76 case aarch64_be: return "aarch64";
81 case thumbeb: return "arm";
85 case ppc: return "ppc";
90 case mips64el: return "mips";
92 case hexagon: return "hexagon";
95 case r600: return "amdgpu";
98 case bpfeb: return "bpf";
102 case sparc: return "sparc";
104 case systemz: return "s390";
107 case x86_64: return "x86";
109 case xcore: return "xcore";
111 case nvptx: return "nvptx";
112 case nvptx64: return "nvptx";
114 case le32: return "le32";
115 case le64: return "le64";
118 case amdil64: return "amdil";
121 case hsail64: return "hsail";
124 case spir64: return "spir";
125 case kalimba: return "kalimba";
126 case shave: return "shave";
127 case wasm32: return "wasm32";
128 case wasm64: return "wasm64";
132 const char *Triple::getVendorTypeName(VendorType Kind) {
134 case UnknownVendor: return "unknown";
136 case Apple: return "apple";
137 case PC: return "pc";
138 case SCEI: return "scei";
139 case BGP: return "bgp";
140 case BGQ: return "bgq";
141 case Freescale: return "fsl";
142 case IBM: return "ibm";
143 case ImaginationTechnologies: return "img";
144 case MipsTechnologies: return "mti";
145 case NVIDIA: return "nvidia";
146 case CSR: return "csr";
149 llvm_unreachable("Invalid VendorType!");
152 const char *Triple::getOSTypeName(OSType Kind) {
154 case UnknownOS: return "unknown";
156 case CloudABI: return "cloudabi";
157 case Darwin: return "darwin";
158 case DragonFly: return "dragonfly";
159 case FreeBSD: return "freebsd";
160 case IOS: return "ios";
161 case KFreeBSD: return "kfreebsd";
162 case Linux: return "linux";
163 case Lv2: return "lv2";
164 case MacOSX: return "macosx";
165 case NetBSD: return "netbsd";
166 case OpenBSD: return "openbsd";
167 case Solaris: return "solaris";
168 case Win32: return "windows";
169 case Haiku: return "haiku";
170 case Minix: return "minix";
171 case RTEMS: return "rtems";
172 case NaCl: return "nacl";
173 case CNK: return "cnk";
174 case Bitrig: return "bitrig";
175 case AIX: return "aix";
176 case CUDA: return "cuda";
177 case NVCL: return "nvcl";
178 case AMDHSA: return "amdhsa";
179 case PS4: return "ps4";
182 llvm_unreachable("Invalid OSType");
185 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
187 case UnknownEnvironment: return "unknown";
188 case GNU: return "gnu";
189 case GNUEABIHF: return "gnueabihf";
190 case GNUEABI: return "gnueabi";
191 case GNUX32: return "gnux32";
192 case CODE16: return "code16";
193 case EABI: return "eabi";
194 case EABIHF: return "eabihf";
195 case Android: return "android";
196 case MSVC: return "msvc";
197 case Itanium: return "itanium";
198 case Cygnus: return "cygnus";
199 case AMDOpenCL: return "amdopencl";
200 case CoreCLR: return "coreclr";
203 llvm_unreachable("Invalid EnvironmentType!");
206 static Triple::ArchType parseBPFArch(StringRef ArchName) {
207 if (ArchName.equals("bpf")) {
208 if (sys::IsLittleEndianHost)
209 return Triple::bpfel;
211 return Triple::bpfeb;
212 } else if (ArchName.equals("bpf_be") || ArchName.equals("bpfeb")) {
213 return Triple::bpfeb;
214 } else if (ArchName.equals("bpf_le") || ArchName.equals("bpfel")) {
215 return Triple::bpfel;
217 return Triple::UnknownArch;
221 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
222 Triple::ArchType BPFArch(parseBPFArch(Name));
223 return StringSwitch<Triple::ArchType>(Name)
224 .Case("aarch64", aarch64)
225 .Case("aarch64_be", aarch64_be)
226 .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
228 .Case("armeb", armeb)
229 .StartsWith("bpf", BPFArch)
231 .Case("mipsel", mipsel)
232 .Case("mips64", mips64)
233 .Case("mips64el", mips64el)
234 .Case("msp430", msp430)
235 .Case("ppc64", ppc64)
238 .Case("ppc64le", ppc64le)
240 .Case("amdgcn", amdgcn)
241 .Case("hexagon", hexagon)
242 .Case("sparc", sparc)
243 .Case("sparcel", sparcel)
244 .Case("sparcv9", sparcv9)
245 .Case("systemz", systemz)
247 .Case("thumb", thumb)
248 .Case("thumbeb", thumbeb)
250 .Case("x86-64", x86_64)
251 .Case("xcore", xcore)
252 .Case("nvptx", nvptx)
253 .Case("nvptx64", nvptx64)
256 .Case("amdil", amdil)
257 .Case("amdil64", amdil64)
258 .Case("hsail", hsail)
259 .Case("hsail64", hsail64)
261 .Case("spir64", spir64)
262 .Case("kalimba", kalimba)
263 .Case("shave", shave)
264 .Case("wasm32", wasm32)
265 .Case("wasm64", wasm64)
266 .Default(UnknownArch);
269 static Triple::ArchType parseARMArch(StringRef ArchName) {
270 unsigned ISA = ARMTargetParser::parseArchISA(ArchName);
271 unsigned ENDIAN = ARMTargetParser::parseArchEndian(ArchName);
273 Triple::ArchType arch = Triple::UnknownArch;
275 case ARM::EK_LITTLE: {
281 arch = Triple::thumb;
283 case ARM::IK_AARCH64:
284 arch = Triple::aarch64;
292 arch = Triple::armeb;
295 arch = Triple::thumbeb;
297 case ARM::IK_AARCH64:
298 arch = Triple::aarch64_be;
305 ArchName = ARMTargetParser::getCanonicalArchName(ArchName);
306 if (ArchName.empty())
307 return Triple::UnknownArch;
309 // Thumb only exists in v4+
310 if (ISA == ARM::IK_THUMB &&
311 (ArchName.startswith("v2") || ArchName.startswith("v3")))
312 return Triple::UnknownArch;
314 // Thumb only for v6m
315 unsigned Profile = ARMTargetParser::parseArchProfile(ArchName);
316 unsigned Version = ARMTargetParser::parseArchVersion(ArchName);
317 if (Profile == ARM::PK_M && Version == 6) {
318 if (ENDIAN == ARM::EK_BIG)
319 return Triple::thumbeb;
321 return Triple::thumb;
327 static Triple::ArchType parseArch(StringRef ArchName) {
328 Triple::ArchType ARMArch(parseARMArch(ArchName));
329 Triple::ArchType BPFArch(parseBPFArch(ArchName));
331 return StringSwitch<Triple::ArchType>(ArchName)
332 .Cases("i386", "i486", "i586", "i686", Triple::x86)
333 // FIXME: Do we need to support these?
334 .Cases("i786", "i886", "i986", Triple::x86)
335 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
336 .Case("powerpc", Triple::ppc)
337 .Cases("powerpc64", "ppu", Triple::ppc64)
338 .Case("powerpc64le", Triple::ppc64le)
339 .Case("xscale", Triple::arm)
340 .Case("xscaleeb", Triple::armeb)
341 .StartsWith("arm", ARMArch)
342 .StartsWith("thumb", ARMArch)
343 .StartsWith("aarch64", ARMArch)
344 .Case("msp430", Triple::msp430)
345 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
346 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
347 .Cases("mips64", "mips64eb", Triple::mips64)
348 .Case("mips64el", Triple::mips64el)
349 .Case("r600", Triple::r600)
350 .Case("amdgcn", Triple::amdgcn)
351 .StartsWith("bpf", BPFArch)
352 .Case("hexagon", Triple::hexagon)
353 .Case("s390x", Triple::systemz)
354 .Case("sparc", Triple::sparc)
355 .Case("sparcel", Triple::sparcel)
356 .Cases("sparcv9", "sparc64", Triple::sparcv9)
357 .Case("tce", Triple::tce)
358 .Case("xcore", Triple::xcore)
359 .Case("nvptx", Triple::nvptx)
360 .Case("nvptx64", Triple::nvptx64)
361 .Case("le32", Triple::le32)
362 .Case("le64", Triple::le64)
363 .Case("amdil", Triple::amdil)
364 .Case("amdil64", Triple::amdil64)
365 .Case("hsail", Triple::hsail)
366 .Case("hsail64", Triple::hsail64)
367 .Case("spir", Triple::spir)
368 .Case("spir64", Triple::spir64)
369 .StartsWith("kalimba", Triple::kalimba)
370 .Case("shave", Triple::shave)
371 .Case("wasm32", Triple::wasm32)
372 .Case("wasm64", Triple::wasm64)
373 .Default(Triple::UnknownArch);
376 static Triple::VendorType parseVendor(StringRef VendorName) {
377 return StringSwitch<Triple::VendorType>(VendorName)
378 .Case("apple", Triple::Apple)
379 .Case("pc", Triple::PC)
380 .Case("scei", Triple::SCEI)
381 .Case("bgp", Triple::BGP)
382 .Case("bgq", Triple::BGQ)
383 .Case("fsl", Triple::Freescale)
384 .Case("ibm", Triple::IBM)
385 .Case("img", Triple::ImaginationTechnologies)
386 .Case("mti", Triple::MipsTechnologies)
387 .Case("nvidia", Triple::NVIDIA)
388 .Case("csr", Triple::CSR)
389 .Default(Triple::UnknownVendor);
392 static Triple::OSType parseOS(StringRef OSName) {
393 return StringSwitch<Triple::OSType>(OSName)
394 .StartsWith("cloudabi", Triple::CloudABI)
395 .StartsWith("darwin", Triple::Darwin)
396 .StartsWith("dragonfly", Triple::DragonFly)
397 .StartsWith("freebsd", Triple::FreeBSD)
398 .StartsWith("ios", Triple::IOS)
399 .StartsWith("kfreebsd", Triple::KFreeBSD)
400 .StartsWith("linux", Triple::Linux)
401 .StartsWith("lv2", Triple::Lv2)
402 .StartsWith("macosx", Triple::MacOSX)
403 .StartsWith("netbsd", Triple::NetBSD)
404 .StartsWith("openbsd", Triple::OpenBSD)
405 .StartsWith("solaris", Triple::Solaris)
406 .StartsWith("win32", Triple::Win32)
407 .StartsWith("windows", Triple::Win32)
408 .StartsWith("haiku", Triple::Haiku)
409 .StartsWith("minix", Triple::Minix)
410 .StartsWith("rtems", Triple::RTEMS)
411 .StartsWith("nacl", Triple::NaCl)
412 .StartsWith("cnk", Triple::CNK)
413 .StartsWith("bitrig", Triple::Bitrig)
414 .StartsWith("aix", Triple::AIX)
415 .StartsWith("cuda", Triple::CUDA)
416 .StartsWith("nvcl", Triple::NVCL)
417 .StartsWith("amdhsa", Triple::AMDHSA)
418 .StartsWith("ps4", Triple::PS4)
419 .Default(Triple::UnknownOS);
422 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
423 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
424 .StartsWith("eabihf", Triple::EABIHF)
425 .StartsWith("eabi", Triple::EABI)
426 .StartsWith("gnueabihf", Triple::GNUEABIHF)
427 .StartsWith("gnueabi", Triple::GNUEABI)
428 .StartsWith("gnux32", Triple::GNUX32)
429 .StartsWith("code16", Triple::CODE16)
430 .StartsWith("gnu", Triple::GNU)
431 .StartsWith("android", Triple::Android)
432 .StartsWith("msvc", Triple::MSVC)
433 .StartsWith("itanium", Triple::Itanium)
434 .StartsWith("cygnus", Triple::Cygnus)
435 .StartsWith("amdopencl", Triple::AMDOpenCL)
436 .StartsWith("coreclr", Triple::CoreCLR)
437 .Default(Triple::UnknownEnvironment);
440 static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
441 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
442 .EndsWith("coff", Triple::COFF)
443 .EndsWith("elf", Triple::ELF)
444 .EndsWith("macho", Triple::MachO)
445 .Default(Triple::UnknownObjectFormat);
448 static Triple::SubArchType parseSubArch(StringRef SubArchName) {
449 StringRef ARMSubArch = ARMTargetParser::getCanonicalArchName(SubArchName);
451 // For now, this is the small part. Early return.
452 if (ARMSubArch.empty())
453 return StringSwitch<Triple::SubArchType>(SubArchName)
454 .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
455 .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
456 .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
457 .Default(Triple::NoSubArch);
460 switch(ARMTargetParser::parseArch(ARMSubArch)) {
462 return Triple::NoSubArch;
464 return Triple::ARMSubArch_v4t;
468 return Triple::ARMSubArch_v5;
469 case ARM::AK_ARMV5TE:
471 case ARM::AK_IWMMXT2:
473 case ARM::AK_ARMV5TEJ:
474 return Triple::ARMSubArch_v5te;
478 return Triple::ARMSubArch_v6;
480 case ARM::AK_ARMV6ZK:
481 case ARM::AK_ARMV6HL:
482 return Triple::ARMSubArch_v6k;
483 case ARM::AK_ARMV6T2:
484 return Triple::ARMSubArch_v6t2;
486 case ARM::AK_ARMV6SM:
487 return Triple::ARMSubArch_v6m;
492 case ARM::AK_ARMV7HL:
493 return Triple::ARMSubArch_v7;
495 return Triple::ARMSubArch_v7m;
497 return Triple::ARMSubArch_v7s;
498 case ARM::AK_ARMV7EM:
499 return Triple::ARMSubArch_v7em;
501 return Triple::ARMSubArch_v8;
502 case ARM::AK_ARMV8_1A:
503 return Triple::ARMSubArch_v8_1a;
505 return Triple::NoSubArch;
509 static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
511 case Triple::UnknownObjectFormat: return "";
512 case Triple::COFF: return "coff";
513 case Triple::ELF: return "elf";
514 case Triple::MachO: return "macho";
516 llvm_unreachable("unknown object format type");
519 static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
520 switch (T.getArch()) {
523 case Triple::hexagon:
527 case Triple::mips64el:
531 case Triple::sparcv9:
532 case Triple::systemz:
534 case Triple::ppc64le:
540 return Triple::MachO;
545 return Triple::MachO;
546 else if (T.isOSWindows())
551 /// \brief Construct a triple from the string representation provided.
553 /// This stores the string representation and parses the various pieces into
555 Triple::Triple(const Twine &Str)
557 Arch(parseArch(getArchName())),
558 SubArch(parseSubArch(getArchName())),
559 Vendor(parseVendor(getVendorName())),
560 OS(parseOS(getOSName())),
561 Environment(parseEnvironment(getEnvironmentName())),
562 ObjectFormat(parseFormat(getEnvironmentName())) {
563 if (ObjectFormat == Triple::UnknownObjectFormat)
564 ObjectFormat = getDefaultFormat(*this);
567 /// \brief Construct a triple from string representations of the architecture,
570 /// This joins each argument into a canonical string representation and parses
571 /// them into enum members. It leaves the environment unknown and omits it from
572 /// the string representation.
573 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
574 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
575 Arch(parseArch(ArchStr.str())),
576 SubArch(parseSubArch(ArchStr.str())),
577 Vendor(parseVendor(VendorStr.str())),
578 OS(parseOS(OSStr.str())),
579 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
580 ObjectFormat = getDefaultFormat(*this);
583 /// \brief Construct a triple from string representations of the architecture,
584 /// vendor, OS, and environment.
586 /// This joins each argument into a canonical string representation and parses
587 /// them into enum members.
588 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
589 const Twine &EnvironmentStr)
590 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
591 EnvironmentStr).str()),
592 Arch(parseArch(ArchStr.str())),
593 SubArch(parseSubArch(ArchStr.str())),
594 Vendor(parseVendor(VendorStr.str())),
595 OS(parseOS(OSStr.str())),
596 Environment(parseEnvironment(EnvironmentStr.str())),
597 ObjectFormat(parseFormat(EnvironmentStr.str())) {
598 if (ObjectFormat == Triple::UnknownObjectFormat)
599 ObjectFormat = getDefaultFormat(*this);
602 std::string Triple::normalize(StringRef Str) {
603 bool IsMinGW32 = false;
604 bool IsCygwin = false;
606 // Parse into components.
607 SmallVector<StringRef, 4> Components;
608 Str.split(Components, "-");
610 // If the first component corresponds to a known architecture, preferentially
611 // use it for the architecture. If the second component corresponds to a
612 // known vendor, preferentially use it for the vendor, etc. This avoids silly
613 // component movement when a component parses as (eg) both a valid arch and a
615 ArchType Arch = UnknownArch;
616 if (Components.size() > 0)
617 Arch = parseArch(Components[0]);
618 VendorType Vendor = UnknownVendor;
619 if (Components.size() > 1)
620 Vendor = parseVendor(Components[1]);
621 OSType OS = UnknownOS;
622 if (Components.size() > 2) {
623 OS = parseOS(Components[2]);
624 IsCygwin = Components[2].startswith("cygwin");
625 IsMinGW32 = Components[2].startswith("mingw");
627 EnvironmentType Environment = UnknownEnvironment;
628 if (Components.size() > 3)
629 Environment = parseEnvironment(Components[3]);
630 ObjectFormatType ObjectFormat = UnknownObjectFormat;
631 if (Components.size() > 4)
632 ObjectFormat = parseFormat(Components[4]);
634 // Note which components are already in their final position. These will not
637 Found[0] = Arch != UnknownArch;
638 Found[1] = Vendor != UnknownVendor;
639 Found[2] = OS != UnknownOS;
640 Found[3] = Environment != UnknownEnvironment;
642 // If they are not there already, permute the components into their canonical
643 // positions by seeing if they parse as a valid architecture, and if so moving
644 // the component to the architecture position etc.
645 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
647 continue; // Already in the canonical position.
649 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
650 // Do not reparse any components that already matched.
651 if (Idx < array_lengthof(Found) && Found[Idx])
654 // Does this component parse as valid for the target position?
656 StringRef Comp = Components[Idx];
658 default: llvm_unreachable("unexpected component type!");
660 Arch = parseArch(Comp);
661 Valid = Arch != UnknownArch;
664 Vendor = parseVendor(Comp);
665 Valid = Vendor != UnknownVendor;
669 IsCygwin = Comp.startswith("cygwin");
670 IsMinGW32 = Comp.startswith("mingw");
671 Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
674 Environment = parseEnvironment(Comp);
675 Valid = Environment != UnknownEnvironment;
677 ObjectFormat = parseFormat(Comp);
678 Valid = ObjectFormat != UnknownObjectFormat;
683 continue; // Nope, try the next component.
685 // Move the component to the target position, pushing any non-fixed
686 // components that are in the way to the right. This tends to give
687 // good results in the common cases of a forgotten vendor component
688 // or a wrongly positioned environment.
690 // Insert left, pushing the existing components to the right. For
691 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
692 StringRef CurrentComponent(""); // The empty component.
693 // Replace the component we are moving with an empty component.
694 std::swap(CurrentComponent, Components[Idx]);
695 // Insert the component being moved at Pos, displacing any existing
696 // components to the right.
697 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
698 // Skip over any fixed components.
699 while (i < array_lengthof(Found) && Found[i])
701 // Place the component at the new position, getting the component
702 // that was at this position - it will be moved right.
703 std::swap(CurrentComponent, Components[i]);
705 } else if (Pos > Idx) {
706 // Push right by inserting empty components until the component at Idx
707 // reaches the target position Pos. For example, pc-a -> -pc-a when
708 // moving pc to the second position.
710 // Insert one empty component at Idx.
711 StringRef CurrentComponent(""); // The empty component.
712 for (unsigned i = Idx; i < Components.size();) {
713 // Place the component at the new position, getting the component
714 // that was at this position - it will be moved right.
715 std::swap(CurrentComponent, Components[i]);
716 // If it was placed on top of an empty component then we are done.
717 if (CurrentComponent.empty())
719 // Advance to the next component, skipping any fixed components.
720 while (++i < array_lengthof(Found) && Found[i])
723 // The last component was pushed off the end - append it.
724 if (!CurrentComponent.empty())
725 Components.push_back(CurrentComponent);
727 // Advance Idx to the component's new position.
728 while (++Idx < array_lengthof(Found) && Found[Idx])
730 } while (Idx < Pos); // Add more until the final position is reached.
732 assert(Pos < Components.size() && Components[Pos] == Comp &&
733 "Component moved wrong!");
739 // Special case logic goes here. At this point Arch, Vendor and OS have the
740 // correct values for the computed components.
741 std::string NormalizedEnvironment;
742 if (Environment == Triple::Android && Components[3].startswith("androideabi")) {
743 StringRef AndroidVersion = Components[3].drop_front(strlen("androideabi"));
744 if (AndroidVersion.empty()) {
745 Components[3] = "android";
747 NormalizedEnvironment = Twine("android", AndroidVersion).str();
748 Components[3] = NormalizedEnvironment;
752 if (OS == Triple::Win32) {
753 Components.resize(4);
754 Components[2] = "windows";
755 if (Environment == UnknownEnvironment) {
756 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
757 Components[3] = "msvc";
759 Components[3] = getObjectFormatTypeName(ObjectFormat);
761 } else if (IsMinGW32) {
762 Components.resize(4);
763 Components[2] = "windows";
764 Components[3] = "gnu";
765 } else if (IsCygwin) {
766 Components.resize(4);
767 Components[2] = "windows";
768 Components[3] = "cygnus";
770 if (IsMinGW32 || IsCygwin ||
771 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
772 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
773 Components.resize(5);
774 Components[4] = getObjectFormatTypeName(ObjectFormat);
778 // Stick the corrected components back together to form the normalized string.
779 std::string Normalized;
780 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
781 if (i) Normalized += '-';
782 Normalized += Components[i];
787 StringRef Triple::getArchName() const {
788 return StringRef(Data).split('-').first; // Isolate first component
791 StringRef Triple::getVendorName() const {
792 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
793 return Tmp.split('-').first; // Isolate second component
796 StringRef Triple::getOSName() const {
797 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
798 Tmp = Tmp.split('-').second; // Strip second component
799 return Tmp.split('-').first; // Isolate third component
802 StringRef Triple::getEnvironmentName() const {
803 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
804 Tmp = Tmp.split('-').second; // Strip second component
805 return Tmp.split('-').second; // Strip third component
808 StringRef Triple::getOSAndEnvironmentName() const {
809 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
810 return Tmp.split('-').second; // Strip second component
813 static unsigned EatNumber(StringRef &Str) {
814 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
818 // Consume the leading digit.
819 Result = Result*10 + (Str[0] - '0');
823 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
828 static void parseVersionFromName(StringRef Name, unsigned &Major,
829 unsigned &Minor, unsigned &Micro) {
830 // Any unset version defaults to 0.
831 Major = Minor = Micro = 0;
833 // Parse up to three components.
834 unsigned *Components[3] = {&Major, &Minor, &Micro};
835 for (unsigned i = 0; i != 3; ++i) {
836 if (Name.empty() || Name[0] < '0' || Name[0] > '9')
839 // Consume the leading number.
840 *Components[i] = EatNumber(Name);
842 // Consume the separator, if present.
843 if (Name.startswith("."))
844 Name = Name.substr(1);
848 void Triple::getEnvironmentVersion(unsigned &Major, unsigned &Minor,
849 unsigned &Micro) const {
850 StringRef EnvironmentName = getEnvironmentName();
851 StringRef EnvironmentTypeName = getEnvironmentTypeName(getEnvironment());
852 if (EnvironmentName.startswith(EnvironmentTypeName))
853 EnvironmentName = EnvironmentName.substr(EnvironmentTypeName.size());
855 parseVersionFromName(EnvironmentName, Major, Minor, Micro);
858 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
859 unsigned &Micro) const {
860 StringRef OSName = getOSName();
861 // Assume that the OS portion of the triple starts with the canonical name.
862 StringRef OSTypeName = getOSTypeName(getOS());
863 if (OSName.startswith(OSTypeName))
864 OSName = OSName.substr(OSTypeName.size());
866 parseVersionFromName(OSName, Major, Minor, Micro);
869 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
870 unsigned &Micro) const {
871 getOSVersion(Major, Minor, Micro);
874 default: llvm_unreachable("unexpected OS for Darwin triple");
876 // Default to darwin8, i.e., MacOSX 10.4.
879 // Darwin version numbers are skewed from OS X versions.
896 // Ignore the version from the triple. This is only handled because the
897 // the clang driver combines OS X and IOS support into a common Darwin
898 // toolchain that wants to know the OS X version number even when targeting
908 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
909 unsigned &Micro) const {
911 default: llvm_unreachable("unexpected OS for Darwin triple");
914 // Ignore the version from the triple. This is only handled because the
915 // the clang driver combines OS X and IOS support into a common Darwin
916 // toolchain that wants to know the iOS version number even when targeting
923 getOSVersion(Major, Minor, Micro);
924 // Default to 5.0 (or 7.0 for arm64).
926 Major = (getArch() == aarch64) ? 7 : 5;
931 void Triple::setTriple(const Twine &Str) {
935 void Triple::setArch(ArchType Kind) {
936 setArchName(getArchTypeName(Kind));
939 void Triple::setVendor(VendorType Kind) {
940 setVendorName(getVendorTypeName(Kind));
943 void Triple::setOS(OSType Kind) {
944 setOSName(getOSTypeName(Kind));
947 void Triple::setEnvironment(EnvironmentType Kind) {
948 if (ObjectFormat == getDefaultFormat(*this))
949 return setEnvironmentName(getEnvironmentTypeName(Kind));
951 setEnvironmentName((getEnvironmentTypeName(Kind) + Twine("-") +
952 getObjectFormatTypeName(ObjectFormat)).str());
955 void Triple::setObjectFormat(ObjectFormatType Kind) {
956 if (Environment == UnknownEnvironment)
957 return setEnvironmentName(getObjectFormatTypeName(Kind));
959 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
960 getObjectFormatTypeName(Kind)).str());
963 void Triple::setArchName(StringRef Str) {
964 // Work around a miscompilation bug for Twines in gcc 4.0.3.
965 SmallString<64> Triple;
968 Triple += getVendorName();
970 Triple += getOSAndEnvironmentName();
974 void Triple::setVendorName(StringRef Str) {
975 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
978 void Triple::setOSName(StringRef Str) {
979 if (hasEnvironment())
980 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
981 "-" + getEnvironmentName());
983 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
986 void Triple::setEnvironmentName(StringRef Str) {
987 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
991 void Triple::setOSAndEnvironmentName(StringRef Str) {
992 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
995 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
997 case llvm::Triple::UnknownArch:
1000 case llvm::Triple::msp430:
1003 case llvm::Triple::arm:
1004 case llvm::Triple::armeb:
1005 case llvm::Triple::hexagon:
1006 case llvm::Triple::le32:
1007 case llvm::Triple::mips:
1008 case llvm::Triple::mipsel:
1009 case llvm::Triple::nvptx:
1010 case llvm::Triple::ppc:
1011 case llvm::Triple::r600:
1012 case llvm::Triple::sparc:
1013 case llvm::Triple::sparcel:
1014 case llvm::Triple::tce:
1015 case llvm::Triple::thumb:
1016 case llvm::Triple::thumbeb:
1017 case llvm::Triple::x86:
1018 case llvm::Triple::xcore:
1019 case llvm::Triple::amdil:
1020 case llvm::Triple::hsail:
1021 case llvm::Triple::spir:
1022 case llvm::Triple::kalimba:
1023 case llvm::Triple::shave:
1024 case llvm::Triple::wasm32:
1027 case llvm::Triple::aarch64:
1028 case llvm::Triple::aarch64_be:
1029 case llvm::Triple::amdgcn:
1030 case llvm::Triple::bpfel:
1031 case llvm::Triple::bpfeb:
1032 case llvm::Triple::le64:
1033 case llvm::Triple::mips64:
1034 case llvm::Triple::mips64el:
1035 case llvm::Triple::nvptx64:
1036 case llvm::Triple::ppc64:
1037 case llvm::Triple::ppc64le:
1038 case llvm::Triple::sparcv9:
1039 case llvm::Triple::systemz:
1040 case llvm::Triple::x86_64:
1041 case llvm::Triple::amdil64:
1042 case llvm::Triple::hsail64:
1043 case llvm::Triple::spir64:
1044 case llvm::Triple::wasm64:
1047 llvm_unreachable("Invalid architecture value");
1050 bool Triple::isArch64Bit() const {
1051 return getArchPointerBitWidth(getArch()) == 64;
1054 bool Triple::isArch32Bit() const {
1055 return getArchPointerBitWidth(getArch()) == 32;
1058 bool Triple::isArch16Bit() const {
1059 return getArchPointerBitWidth(getArch()) == 16;
1062 Triple Triple::get32BitArchVariant() const {
1064 switch (getArch()) {
1065 case Triple::UnknownArch:
1066 case Triple::aarch64:
1067 case Triple::aarch64_be:
1068 case Triple::amdgcn:
1071 case Triple::msp430:
1072 case Triple::systemz:
1073 case Triple::ppc64le:
1074 T.setArch(UnknownArch);
1082 case Triple::hexagon:
1083 case Triple::kalimba:
1086 case Triple::mipsel:
1091 case Triple::sparcel:
1094 case Triple::thumbeb:
1098 case Triple::wasm32:
1102 case Triple::le64: T.setArch(Triple::le32); break;
1103 case Triple::mips64: T.setArch(Triple::mips); break;
1104 case Triple::mips64el: T.setArch(Triple::mipsel); break;
1105 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
1106 case Triple::ppc64: T.setArch(Triple::ppc); break;
1107 case Triple::sparcv9: T.setArch(Triple::sparc); break;
1108 case Triple::x86_64: T.setArch(Triple::x86); break;
1109 case Triple::amdil64: T.setArch(Triple::amdil); break;
1110 case Triple::hsail64: T.setArch(Triple::hsail); break;
1111 case Triple::spir64: T.setArch(Triple::spir); break;
1112 case Triple::wasm64: T.setArch(Triple::wasm32); break;
1117 Triple Triple::get64BitArchVariant() const {
1119 switch (getArch()) {
1120 case Triple::UnknownArch:
1123 case Triple::hexagon:
1124 case Triple::kalimba:
1125 case Triple::msp430:
1129 case Triple::thumbeb:
1131 case Triple::sparcel:
1133 T.setArch(UnknownArch);
1136 case Triple::aarch64:
1137 case Triple::aarch64_be:
1141 case Triple::amdil64:
1142 case Triple::amdgcn:
1143 case Triple::hsail64:
1144 case Triple::spir64:
1145 case Triple::mips64:
1146 case Triple::mips64el:
1147 case Triple::nvptx64:
1149 case Triple::ppc64le:
1150 case Triple::sparcv9:
1151 case Triple::systemz:
1152 case Triple::x86_64:
1153 case Triple::wasm64:
1157 case Triple::le32: T.setArch(Triple::le64); break;
1158 case Triple::mips: T.setArch(Triple::mips64); break;
1159 case Triple::mipsel: T.setArch(Triple::mips64el); break;
1160 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
1161 case Triple::ppc: T.setArch(Triple::ppc64); break;
1162 case Triple::sparc: T.setArch(Triple::sparcv9); break;
1163 case Triple::x86: T.setArch(Triple::x86_64); break;
1164 case Triple::amdil: T.setArch(Triple::amdil64); break;
1165 case Triple::hsail: T.setArch(Triple::hsail64); break;
1166 case Triple::spir: T.setArch(Triple::spir64); break;
1167 case Triple::wasm32: T.setArch(Triple::wasm64); break;
1172 Triple Triple::getBigEndianArchVariant() const {
1174 switch (getArch()) {
1175 case Triple::UnknownArch:
1176 case Triple::amdgcn:
1177 case Triple::amdil64:
1179 case Triple::hexagon:
1180 case Triple::hsail64:
1182 case Triple::kalimba:
1185 case Triple::msp430:
1186 case Triple::nvptx64:
1190 case Triple::spir64:
1192 case Triple::wasm32:
1193 case Triple::wasm64:
1195 case Triple::x86_64:
1198 // ARM is intentionally unsupported here, changing the architecture would
1199 // drop any arch suffixes.
1202 T.setArch(UnknownArch);
1205 case Triple::aarch64_be:
1208 case Triple::mips64:
1213 case Triple::sparcv9:
1214 case Triple::systemz:
1216 case Triple::thumbeb:
1217 // Already big endian.
1220 case Triple::aarch64: T.setArch(Triple::aarch64_be); break;
1221 case Triple::bpfel: T.setArch(Triple::bpfeb); break;
1222 case Triple::mips64el:T.setArch(Triple::mips64); break;
1223 case Triple::mipsel: T.setArch(Triple::mips); break;
1224 case Triple::ppc64le: T.setArch(Triple::ppc64); break;
1225 case Triple::sparcel: T.setArch(Triple::sparc); break;
1230 Triple Triple::getLittleEndianArchVariant() const {
1232 switch (getArch()) {
1233 case Triple::UnknownArch:
1235 case Triple::sparcv9:
1236 case Triple::systemz:
1239 // ARM is intentionally unsupported here, changing the architecture would
1240 // drop any arch suffixes.
1242 case Triple::thumbeb:
1243 T.setArch(UnknownArch);
1246 case Triple::aarch64:
1247 case Triple::amdgcn:
1248 case Triple::amdil64:
1252 case Triple::hexagon:
1253 case Triple::hsail64:
1255 case Triple::kalimba:
1258 case Triple::mips64el:
1259 case Triple::mipsel:
1260 case Triple::msp430:
1261 case Triple::nvptx64:
1263 case Triple::ppc64le:
1266 case Triple::sparcel:
1267 case Triple::spir64:
1270 case Triple::wasm32:
1271 case Triple::wasm64:
1273 case Triple::x86_64:
1275 // Already little endian.
1278 case Triple::aarch64_be: T.setArch(Triple::aarch64); break;
1279 case Triple::bpfeb: T.setArch(Triple::bpfel); break;
1280 case Triple::mips64: T.setArch(Triple::mips64el); break;
1281 case Triple::mips: T.setArch(Triple::mipsel); break;
1282 case Triple::ppc64: T.setArch(Triple::ppc64le); break;
1283 case Triple::sparc: T.setArch(Triple::sparcel); break;
1288 const char *Triple::getARMCPUForArch(StringRef MArch) const {
1290 MArch = getArchName();
1291 MArch = ARMTargetParser::getCanonicalArchName(MArch);
1293 // Some defaults are forced.
1295 case llvm::Triple::FreeBSD:
1296 case llvm::Triple::NetBSD:
1297 if (!MArch.empty() && MArch == "v6")
1298 return "arm1176jzf-s";
1300 case llvm::Triple::Win32:
1301 // FIXME: this is invalid for WindowsCE
1310 const char *CPU = ARMTargetParser::getDefaultCPU(MArch);
1314 // If no specific architecture version is requested, return the minimum CPU
1315 // required by the OS and environment.
1317 case llvm::Triple::NetBSD:
1318 switch (getEnvironment()) {
1319 case llvm::Triple::GNUEABIHF:
1320 case llvm::Triple::GNUEABI:
1321 case llvm::Triple::EABIHF:
1322 case llvm::Triple::EABI:
1323 return "arm926ej-s";
1327 case llvm::Triple::NaCl:
1330 switch (getEnvironment()) {
1331 case llvm::Triple::EABIHF:
1332 case llvm::Triple::GNUEABIHF:
1333 return "arm1176jzf-s";
1339 llvm_unreachable("invalid arch name");