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"
19 const char *Triple::getArchTypeName(ArchType Kind) {
21 case UnknownArch: return "unknown";
23 case aarch64: return "aarch64";
24 case aarch64_be: return "aarch64_be";
25 case arm: return "arm";
26 case armeb: return "armeb";
27 case bpf: return "bpf";
28 case hexagon: return "hexagon";
29 case mips: return "mips";
30 case mipsel: return "mipsel";
31 case mips64: return "mips64";
32 case mips64el: return "mips64el";
33 case msp430: return "msp430";
34 case ppc64: return "powerpc64";
35 case ppc64le: return "powerpc64le";
36 case ppc: return "powerpc";
37 case r600: return "r600";
38 case amdgcn: return "amdgcn";
39 case sparc: return "sparc";
40 case sparcv9: return "sparcv9";
41 case sparcel: return "sparcel";
42 case systemz: return "s390x";
43 case tce: return "tce";
44 case thumb: return "thumb";
45 case thumbeb: return "thumbeb";
46 case x86: return "i386";
47 case x86_64: return "x86_64";
48 case xcore: return "xcore";
49 case nvptx: return "nvptx";
50 case nvptx64: return "nvptx64";
51 case le32: return "le32";
52 case le64: return "le64";
53 case amdil: return "amdil";
54 case amdil64: return "amdil64";
55 case hsail: return "hsail";
56 case hsail64: return "hsail64";
57 case spir: return "spir";
58 case spir64: return "spir64";
59 case kalimba: return "kalimba";
62 llvm_unreachable("Invalid ArchType!");
65 const char *Triple::getArchTypePrefix(ArchType Kind) {
71 case aarch64_be: return "aarch64";
76 case thumbeb: return "arm";
80 case ppc: return "ppc";
85 case mips64el: return "mips";
87 case hexagon: return "hexagon";
90 case r600: return "amdgpu";
92 case bpf: return "bpf";
96 case sparc: return "sparc";
98 case systemz: return "s390";
101 case x86_64: return "x86";
103 case xcore: return "xcore";
105 case nvptx: return "nvptx";
106 case nvptx64: return "nvptx";
108 case le32: return "le32";
109 case le64: return "le64";
112 case amdil64: return "amdil";
115 case hsail64: return "hsail";
118 case spir64: return "spir";
119 case kalimba: return "kalimba";
123 const char *Triple::getVendorTypeName(VendorType Kind) {
125 case UnknownVendor: return "unknown";
127 case Apple: return "apple";
128 case PC: return "pc";
129 case SCEI: return "scei";
130 case BGP: return "bgp";
131 case BGQ: return "bgq";
132 case Freescale: return "fsl";
133 case IBM: return "ibm";
134 case ImaginationTechnologies: return "img";
135 case MipsTechnologies: return "mti";
136 case NVIDIA: return "nvidia";
137 case CSR: return "csr";
140 llvm_unreachable("Invalid VendorType!");
143 const char *Triple::getOSTypeName(OSType Kind) {
145 case UnknownOS: return "unknown";
147 case CloudABI: return "cloudabi";
148 case Darwin: return "darwin";
149 case DragonFly: return "dragonfly";
150 case FreeBSD: return "freebsd";
151 case IOS: return "ios";
152 case KFreeBSD: return "kfreebsd";
153 case Linux: return "linux";
154 case Lv2: return "lv2";
155 case MacOSX: return "macosx";
156 case NetBSD: return "netbsd";
157 case OpenBSD: return "openbsd";
158 case Solaris: return "solaris";
159 case Win32: return "windows";
160 case Haiku: return "haiku";
161 case Minix: return "minix";
162 case RTEMS: return "rtems";
163 case NaCl: return "nacl";
164 case CNK: return "cnk";
165 case Bitrig: return "bitrig";
166 case AIX: return "aix";
167 case CUDA: return "cuda";
168 case NVCL: return "nvcl";
169 case AMDHSA: return "amdhsa";
170 case PS4: return "ps4";
173 llvm_unreachable("Invalid OSType");
176 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
178 case UnknownEnvironment: return "unknown";
179 case GNU: return "gnu";
180 case GNUEABIHF: return "gnueabihf";
181 case GNUEABI: return "gnueabi";
182 case GNUX32: return "gnux32";
183 case CODE16: return "code16";
184 case EABI: return "eabi";
185 case EABIHF: return "eabihf";
186 case Android: return "android";
187 case MSVC: return "msvc";
188 case Itanium: return "itanium";
189 case Cygnus: return "cygnus";
192 llvm_unreachable("Invalid EnvironmentType!");
195 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
196 return StringSwitch<Triple::ArchType>(Name)
197 .Case("aarch64", aarch64)
198 .Case("aarch64_be", aarch64_be)
199 .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
201 .Case("armeb", armeb)
204 .Case("mipsel", mipsel)
205 .Case("mips64", mips64)
206 .Case("mips64el", mips64el)
207 .Case("msp430", msp430)
208 .Case("ppc64", ppc64)
211 .Case("ppc64le", ppc64le)
213 .Case("amdgcn", amdgcn)
214 .Case("hexagon", hexagon)
215 .Case("sparc", sparc)
216 .Case("sparcel", sparcel)
217 .Case("sparcv9", sparcv9)
218 .Case("systemz", systemz)
220 .Case("thumb", thumb)
221 .Case("thumbeb", thumbeb)
223 .Case("x86-64", x86_64)
224 .Case("xcore", xcore)
225 .Case("nvptx", nvptx)
226 .Case("nvptx64", nvptx64)
229 .Case("amdil", amdil)
230 .Case("amdil64", amdil64)
231 .Case("hsail", hsail)
232 .Case("hsail64", hsail64)
234 .Case("spir64", spir64)
235 .Case("kalimba", kalimba)
236 .Default(UnknownArch);
239 static Triple::ArchType parseARMArch(StringRef ArchName) {
240 unsigned ISA = ARMTargetParser::parseArchISA(ArchName);
241 unsigned ENDIAN = ARMTargetParser::parseArchEndian(ArchName);
243 Triple::ArchType arch = Triple::UnknownArch;
245 case ARM::EK_LITTLE: {
251 arch = Triple::thumb;
253 case ARM::IK_AARCH64:
254 arch = Triple::aarch64;
262 arch = Triple::armeb;
265 arch = Triple::thumbeb;
267 case ARM::IK_AARCH64:
268 arch = Triple::aarch64_be;
275 ArchName = ARMTargetParser::getCanonicalArchName(ArchName);
276 if (ArchName.empty())
277 return Triple::UnknownArch;
279 // Thumb only exists in v4+
280 if (ISA == ARM::IK_THUMB &&
281 (ArchName.startswith("v2") || ArchName.startswith("v3")))
282 return Triple::UnknownArch;
284 // FIXME: Add isMProfile to ARMTargetParser and
285 // either change armv6m to thumb or UnknownArch.
289 static Triple::ArchType parseArch(StringRef ArchName) {
290 Triple::ArchType ARMArch(parseARMArch(ArchName));
292 return StringSwitch<Triple::ArchType>(ArchName)
293 .Cases("i386", "i486", "i586", "i686", Triple::x86)
294 // FIXME: Do we need to support these?
295 .Cases("i786", "i886", "i986", Triple::x86)
296 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
297 .Case("powerpc", Triple::ppc)
298 .Cases("powerpc64", "ppu", Triple::ppc64)
299 .Case("powerpc64le", Triple::ppc64le)
300 .Case("xscale", Triple::arm)
301 .Case("xscaleeb", Triple::armeb)
302 .StartsWith("arm", ARMArch)
303 .StartsWith("thumb", ARMArch)
304 .StartsWith("aarch64", ARMArch)
305 .Case("msp430", Triple::msp430)
306 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
307 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
308 .Cases("mips64", "mips64eb", Triple::mips64)
309 .Case("mips64el", Triple::mips64el)
310 .Case("r600", Triple::r600)
311 .Case("amdgcn", Triple::amdgcn)
312 .Case("bpf", Triple::bpf)
313 .Case("hexagon", Triple::hexagon)
314 .Case("s390x", Triple::systemz)
315 .Case("sparc", Triple::sparc)
316 .Case("sparcel", Triple::sparcel)
317 .Cases("sparcv9", "sparc64", Triple::sparcv9)
318 .Case("tce", Triple::tce)
319 .Case("xcore", Triple::xcore)
320 .Case("nvptx", Triple::nvptx)
321 .Case("nvptx64", Triple::nvptx64)
322 .Case("le32", Triple::le32)
323 .Case("le64", Triple::le64)
324 .Case("amdil", Triple::amdil)
325 .Case("amdil64", Triple::amdil64)
326 .Case("hsail", Triple::hsail)
327 .Case("hsail64", Triple::hsail64)
328 .Case("spir", Triple::spir)
329 .Case("spir64", Triple::spir64)
330 .StartsWith("kalimba", Triple::kalimba)
331 .Default(Triple::UnknownArch);
334 static Triple::VendorType parseVendor(StringRef VendorName) {
335 return StringSwitch<Triple::VendorType>(VendorName)
336 .Case("apple", Triple::Apple)
337 .Case("pc", Triple::PC)
338 .Case("scei", Triple::SCEI)
339 .Case("bgp", Triple::BGP)
340 .Case("bgq", Triple::BGQ)
341 .Case("fsl", Triple::Freescale)
342 .Case("ibm", Triple::IBM)
343 .Case("img", Triple::ImaginationTechnologies)
344 .Case("mti", Triple::MipsTechnologies)
345 .Case("nvidia", Triple::NVIDIA)
346 .Case("csr", Triple::CSR)
347 .Default(Triple::UnknownVendor);
350 static Triple::OSType parseOS(StringRef OSName) {
351 return StringSwitch<Triple::OSType>(OSName)
352 .StartsWith("cloudabi", Triple::CloudABI)
353 .StartsWith("darwin", Triple::Darwin)
354 .StartsWith("dragonfly", Triple::DragonFly)
355 .StartsWith("freebsd", Triple::FreeBSD)
356 .StartsWith("ios", Triple::IOS)
357 .StartsWith("kfreebsd", Triple::KFreeBSD)
358 .StartsWith("linux", Triple::Linux)
359 .StartsWith("lv2", Triple::Lv2)
360 .StartsWith("macosx", Triple::MacOSX)
361 .StartsWith("netbsd", Triple::NetBSD)
362 .StartsWith("openbsd", Triple::OpenBSD)
363 .StartsWith("solaris", Triple::Solaris)
364 .StartsWith("win32", Triple::Win32)
365 .StartsWith("windows", Triple::Win32)
366 .StartsWith("haiku", Triple::Haiku)
367 .StartsWith("minix", Triple::Minix)
368 .StartsWith("rtems", Triple::RTEMS)
369 .StartsWith("nacl", Triple::NaCl)
370 .StartsWith("cnk", Triple::CNK)
371 .StartsWith("bitrig", Triple::Bitrig)
372 .StartsWith("aix", Triple::AIX)
373 .StartsWith("cuda", Triple::CUDA)
374 .StartsWith("nvcl", Triple::NVCL)
375 .StartsWith("amdhsa", Triple::AMDHSA)
376 .StartsWith("ps4", Triple::PS4)
377 .Default(Triple::UnknownOS);
380 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
381 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
382 .StartsWith("eabihf", Triple::EABIHF)
383 .StartsWith("eabi", Triple::EABI)
384 .StartsWith("gnueabihf", Triple::GNUEABIHF)
385 .StartsWith("gnueabi", Triple::GNUEABI)
386 .StartsWith("gnux32", Triple::GNUX32)
387 .StartsWith("code16", Triple::CODE16)
388 .StartsWith("gnu", Triple::GNU)
389 .StartsWith("android", Triple::Android)
390 .StartsWith("msvc", Triple::MSVC)
391 .StartsWith("itanium", Triple::Itanium)
392 .StartsWith("cygnus", Triple::Cygnus)
393 .Default(Triple::UnknownEnvironment);
396 static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
397 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
398 .EndsWith("coff", Triple::COFF)
399 .EndsWith("elf", Triple::ELF)
400 .EndsWith("macho", Triple::MachO)
401 .Default(Triple::UnknownObjectFormat);
404 // FIXME: Use ARMTargetParser. This would require using Triple::ARMSubArch*
405 // in ARMBuildAttrs and in ARCHNames' DefaultArch fields.
406 static Triple::SubArchType parseSubArch(StringRef SubArchName) {
407 if (SubArchName.endswith("eb"))
408 SubArchName = SubArchName.substr(0, SubArchName.size() - 2);
410 return StringSwitch<Triple::SubArchType>(SubArchName)
411 .EndsWith("v8.1a", Triple::ARMSubArch_v8_1a)
412 .EndsWith("v8", Triple::ARMSubArch_v8)
413 .EndsWith("v8a", Triple::ARMSubArch_v8)
414 .EndsWith("v7", Triple::ARMSubArch_v7)
415 .EndsWith("v7a", Triple::ARMSubArch_v7)
416 .EndsWith("v7em", Triple::ARMSubArch_v7em)
417 .EndsWith("v7l", Triple::ARMSubArch_v7)
418 .EndsWith("v7m", Triple::ARMSubArch_v7m)
419 .EndsWith("v7r", Triple::ARMSubArch_v7)
420 .EndsWith("v7s", Triple::ARMSubArch_v7s)
421 .EndsWith("v6", Triple::ARMSubArch_v6)
422 .EndsWith("v6m", Triple::ARMSubArch_v6m)
423 .EndsWith("v6sm", Triple::ARMSubArch_v6m)
424 .EndsWith("v6k", Triple::ARMSubArch_v6k)
425 .EndsWith("v6t2", Triple::ARMSubArch_v6t2)
426 .EndsWith("v5", Triple::ARMSubArch_v5)
427 .EndsWith("v5e", Triple::ARMSubArch_v5)
428 .EndsWith("v5t", Triple::ARMSubArch_v5)
429 .EndsWith("v5te", Triple::ARMSubArch_v5te)
430 .EndsWith("v4t", Triple::ARMSubArch_v4t)
431 .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
432 .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
433 .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
434 .Default(Triple::NoSubArch);
437 static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
439 case Triple::UnknownObjectFormat: return "";
440 case Triple::COFF: return "coff";
441 case Triple::ELF: return "elf";
442 case Triple::MachO: return "macho";
444 llvm_unreachable("unknown object format type");
447 static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
448 switch (T.getArch()) {
451 case Triple::hexagon:
455 case Triple::mips64el:
459 case Triple::sparcv9:
460 case Triple::systemz:
462 case Triple::ppc64le:
468 return Triple::MachO;
473 return Triple::MachO;
474 else if (T.isOSWindows())
479 /// \brief Construct a triple from the string representation provided.
481 /// This stores the string representation and parses the various pieces into
483 Triple::Triple(const Twine &Str)
485 Arch(parseArch(getArchName())),
486 SubArch(parseSubArch(getArchName())),
487 Vendor(parseVendor(getVendorName())),
488 OS(parseOS(getOSName())),
489 Environment(parseEnvironment(getEnvironmentName())),
490 ObjectFormat(parseFormat(getEnvironmentName())) {
491 if (ObjectFormat == Triple::UnknownObjectFormat)
492 ObjectFormat = getDefaultFormat(*this);
495 /// \brief Construct a triple from string representations of the architecture,
498 /// This joins each argument into a canonical string representation and parses
499 /// them into enum members. It leaves the environment unknown and omits it from
500 /// the string representation.
501 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
502 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
503 Arch(parseArch(ArchStr.str())),
504 SubArch(parseSubArch(ArchStr.str())),
505 Vendor(parseVendor(VendorStr.str())),
506 OS(parseOS(OSStr.str())),
507 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
508 ObjectFormat = getDefaultFormat(*this);
511 /// \brief Construct a triple from string representations of the architecture,
512 /// vendor, OS, and environment.
514 /// This joins each argument into a canonical string representation and parses
515 /// them into enum members.
516 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
517 const Twine &EnvironmentStr)
518 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
519 EnvironmentStr).str()),
520 Arch(parseArch(ArchStr.str())),
521 SubArch(parseSubArch(ArchStr.str())),
522 Vendor(parseVendor(VendorStr.str())),
523 OS(parseOS(OSStr.str())),
524 Environment(parseEnvironment(EnvironmentStr.str())),
525 ObjectFormat(parseFormat(EnvironmentStr.str())) {
526 if (ObjectFormat == Triple::UnknownObjectFormat)
527 ObjectFormat = getDefaultFormat(*this);
530 std::string Triple::normalize(StringRef Str) {
531 bool IsMinGW32 = false;
532 bool IsCygwin = false;
534 // Parse into components.
535 SmallVector<StringRef, 4> Components;
536 Str.split(Components, "-");
538 // If the first component corresponds to a known architecture, preferentially
539 // use it for the architecture. If the second component corresponds to a
540 // known vendor, preferentially use it for the vendor, etc. This avoids silly
541 // component movement when a component parses as (eg) both a valid arch and a
543 ArchType Arch = UnknownArch;
544 if (Components.size() > 0)
545 Arch = parseArch(Components[0]);
546 VendorType Vendor = UnknownVendor;
547 if (Components.size() > 1)
548 Vendor = parseVendor(Components[1]);
549 OSType OS = UnknownOS;
550 if (Components.size() > 2) {
551 OS = parseOS(Components[2]);
552 IsCygwin = Components[2].startswith("cygwin");
553 IsMinGW32 = Components[2].startswith("mingw");
555 EnvironmentType Environment = UnknownEnvironment;
556 if (Components.size() > 3)
557 Environment = parseEnvironment(Components[3]);
558 ObjectFormatType ObjectFormat = UnknownObjectFormat;
559 if (Components.size() > 4)
560 ObjectFormat = parseFormat(Components[4]);
562 // Note which components are already in their final position. These will not
565 Found[0] = Arch != UnknownArch;
566 Found[1] = Vendor != UnknownVendor;
567 Found[2] = OS != UnknownOS;
568 Found[3] = Environment != UnknownEnvironment;
570 // If they are not there already, permute the components into their canonical
571 // positions by seeing if they parse as a valid architecture, and if so moving
572 // the component to the architecture position etc.
573 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
575 continue; // Already in the canonical position.
577 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
578 // Do not reparse any components that already matched.
579 if (Idx < array_lengthof(Found) && Found[Idx])
582 // Does this component parse as valid for the target position?
584 StringRef Comp = Components[Idx];
586 default: llvm_unreachable("unexpected component type!");
588 Arch = parseArch(Comp);
589 Valid = Arch != UnknownArch;
592 Vendor = parseVendor(Comp);
593 Valid = Vendor != UnknownVendor;
597 IsCygwin = Comp.startswith("cygwin");
598 IsMinGW32 = Comp.startswith("mingw");
599 Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
602 Environment = parseEnvironment(Comp);
603 Valid = Environment != UnknownEnvironment;
605 ObjectFormat = parseFormat(Comp);
606 Valid = ObjectFormat != UnknownObjectFormat;
611 continue; // Nope, try the next component.
613 // Move the component to the target position, pushing any non-fixed
614 // components that are in the way to the right. This tends to give
615 // good results in the common cases of a forgotten vendor component
616 // or a wrongly positioned environment.
618 // Insert left, pushing the existing components to the right. For
619 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
620 StringRef CurrentComponent(""); // The empty component.
621 // Replace the component we are moving with an empty component.
622 std::swap(CurrentComponent, Components[Idx]);
623 // Insert the component being moved at Pos, displacing any existing
624 // components to the right.
625 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
626 // Skip over any fixed components.
627 while (i < array_lengthof(Found) && Found[i])
629 // Place the component at the new position, getting the component
630 // that was at this position - it will be moved right.
631 std::swap(CurrentComponent, Components[i]);
633 } else if (Pos > Idx) {
634 // Push right by inserting empty components until the component at Idx
635 // reaches the target position Pos. For example, pc-a -> -pc-a when
636 // moving pc to the second position.
638 // Insert one empty component at Idx.
639 StringRef CurrentComponent(""); // The empty component.
640 for (unsigned i = Idx; i < Components.size();) {
641 // Place the component at the new position, getting the component
642 // that was at this position - it will be moved right.
643 std::swap(CurrentComponent, Components[i]);
644 // If it was placed on top of an empty component then we are done.
645 if (CurrentComponent.empty())
647 // Advance to the next component, skipping any fixed components.
648 while (++i < array_lengthof(Found) && Found[i])
651 // The last component was pushed off the end - append it.
652 if (!CurrentComponent.empty())
653 Components.push_back(CurrentComponent);
655 // Advance Idx to the component's new position.
656 while (++Idx < array_lengthof(Found) && Found[Idx])
658 } while (Idx < Pos); // Add more until the final position is reached.
660 assert(Pos < Components.size() && Components[Pos] == Comp &&
661 "Component moved wrong!");
667 // Special case logic goes here. At this point Arch, Vendor and OS have the
668 // correct values for the computed components.
670 if (OS == Triple::Win32) {
671 Components.resize(4);
672 Components[2] = "windows";
673 if (Environment == UnknownEnvironment) {
674 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
675 Components[3] = "msvc";
677 Components[3] = getObjectFormatTypeName(ObjectFormat);
679 } else if (IsMinGW32) {
680 Components.resize(4);
681 Components[2] = "windows";
682 Components[3] = "gnu";
683 } else if (IsCygwin) {
684 Components.resize(4);
685 Components[2] = "windows";
686 Components[3] = "cygnus";
688 if (IsMinGW32 || IsCygwin ||
689 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
690 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
691 Components.resize(5);
692 Components[4] = getObjectFormatTypeName(ObjectFormat);
696 // Stick the corrected components back together to form the normalized string.
697 std::string Normalized;
698 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
699 if (i) Normalized += '-';
700 Normalized += Components[i];
705 StringRef Triple::getArchName() const {
706 return StringRef(Data).split('-').first; // Isolate first component
709 StringRef Triple::getVendorName() const {
710 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
711 return Tmp.split('-').first; // Isolate second component
714 StringRef Triple::getOSName() const {
715 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
716 Tmp = Tmp.split('-').second; // Strip second component
717 return Tmp.split('-').first; // Isolate third component
720 StringRef Triple::getEnvironmentName() const {
721 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
722 Tmp = Tmp.split('-').second; // Strip second component
723 return Tmp.split('-').second; // Strip third component
726 StringRef Triple::getOSAndEnvironmentName() const {
727 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
728 return Tmp.split('-').second; // Strip second component
731 static unsigned EatNumber(StringRef &Str) {
732 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
736 // Consume the leading digit.
737 Result = Result*10 + (Str[0] - '0');
741 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
746 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
747 unsigned &Micro) const {
748 StringRef OSName = getOSName();
750 // For Android, we care about the Android version rather than the Linux
752 if (getEnvironment() == Android) {
753 OSName = getEnvironmentName().substr(strlen("android"));
754 if (OSName.startswith("eabi"))
755 OSName = OSName.substr(strlen("eabi"));
758 // Assume that the OS portion of the triple starts with the canonical name.
759 StringRef OSTypeName = getOSTypeName(getOS());
760 if (OSName.startswith(OSTypeName))
761 OSName = OSName.substr(OSTypeName.size());
763 // Any unset version defaults to 0.
764 Major = Minor = Micro = 0;
766 // Parse up to three components.
767 unsigned *Components[3] = { &Major, &Minor, &Micro };
768 for (unsigned i = 0; i != 3; ++i) {
769 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
772 // Consume the leading number.
773 *Components[i] = EatNumber(OSName);
775 // Consume the separator, if present.
776 if (OSName.startswith("."))
777 OSName = OSName.substr(1);
781 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
782 unsigned &Micro) const {
783 getOSVersion(Major, Minor, Micro);
786 default: llvm_unreachable("unexpected OS for Darwin triple");
788 // Default to darwin8, i.e., MacOSX 10.4.
791 // Darwin version numbers are skewed from OS X versions.
808 // Ignore the version from the triple. This is only handled because the
809 // the clang driver combines OS X and IOS support into a common Darwin
810 // toolchain that wants to know the OS X version number even when targeting
820 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
821 unsigned &Micro) const {
823 default: llvm_unreachable("unexpected OS for Darwin triple");
826 // Ignore the version from the triple. This is only handled because the
827 // the clang driver combines OS X and IOS support into a common Darwin
828 // toolchain that wants to know the iOS version number even when targeting
835 getOSVersion(Major, Minor, Micro);
836 // Default to 5.0 (or 7.0 for arm64).
838 Major = (getArch() == aarch64) ? 7 : 5;
843 void Triple::setTriple(const Twine &Str) {
847 void Triple::setArch(ArchType Kind) {
848 setArchName(getArchTypeName(Kind));
851 void Triple::setVendor(VendorType Kind) {
852 setVendorName(getVendorTypeName(Kind));
855 void Triple::setOS(OSType Kind) {
856 setOSName(getOSTypeName(Kind));
859 void Triple::setEnvironment(EnvironmentType Kind) {
860 if (ObjectFormat == getDefaultFormat(*this))
861 return setEnvironmentName(getEnvironmentTypeName(Kind));
863 setEnvironmentName((getEnvironmentTypeName(Kind) + Twine("-") +
864 getObjectFormatTypeName(ObjectFormat)).str());
867 void Triple::setObjectFormat(ObjectFormatType Kind) {
868 if (Environment == UnknownEnvironment)
869 return setEnvironmentName(getObjectFormatTypeName(Kind));
871 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
872 getObjectFormatTypeName(Kind)).str());
875 void Triple::setArchName(StringRef Str) {
876 // Work around a miscompilation bug for Twines in gcc 4.0.3.
877 SmallString<64> Triple;
880 Triple += getVendorName();
882 Triple += getOSAndEnvironmentName();
886 void Triple::setVendorName(StringRef Str) {
887 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
890 void Triple::setOSName(StringRef Str) {
891 if (hasEnvironment())
892 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
893 "-" + getEnvironmentName());
895 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
898 void Triple::setEnvironmentName(StringRef Str) {
899 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
903 void Triple::setOSAndEnvironmentName(StringRef Str) {
904 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
907 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
909 case llvm::Triple::UnknownArch:
912 case llvm::Triple::msp430:
915 case llvm::Triple::arm:
916 case llvm::Triple::armeb:
917 case llvm::Triple::hexagon:
918 case llvm::Triple::le32:
919 case llvm::Triple::mips:
920 case llvm::Triple::mipsel:
921 case llvm::Triple::nvptx:
922 case llvm::Triple::ppc:
923 case llvm::Triple::r600:
924 case llvm::Triple::sparc:
925 case llvm::Triple::sparcel:
926 case llvm::Triple::tce:
927 case llvm::Triple::thumb:
928 case llvm::Triple::thumbeb:
929 case llvm::Triple::x86:
930 case llvm::Triple::xcore:
931 case llvm::Triple::amdil:
932 case llvm::Triple::hsail:
933 case llvm::Triple::spir:
934 case llvm::Triple::kalimba:
937 case llvm::Triple::aarch64:
938 case llvm::Triple::aarch64_be:
939 case llvm::Triple::amdgcn:
940 case llvm::Triple::bpf:
941 case llvm::Triple::le64:
942 case llvm::Triple::mips64:
943 case llvm::Triple::mips64el:
944 case llvm::Triple::nvptx64:
945 case llvm::Triple::ppc64:
946 case llvm::Triple::ppc64le:
947 case llvm::Triple::sparcv9:
948 case llvm::Triple::systemz:
949 case llvm::Triple::x86_64:
950 case llvm::Triple::amdil64:
951 case llvm::Triple::hsail64:
952 case llvm::Triple::spir64:
955 llvm_unreachable("Invalid architecture value");
958 bool Triple::isArch64Bit() const {
959 return getArchPointerBitWidth(getArch()) == 64;
962 bool Triple::isArch32Bit() const {
963 return getArchPointerBitWidth(getArch()) == 32;
966 bool Triple::isArch16Bit() const {
967 return getArchPointerBitWidth(getArch()) == 16;
970 Triple Triple::get32BitArchVariant() const {
973 case Triple::UnknownArch:
974 case Triple::aarch64:
975 case Triple::aarch64_be:
979 case Triple::systemz:
980 case Triple::ppc64le:
981 T.setArch(UnknownArch);
989 case Triple::hexagon:
990 case Triple::kalimba:
998 case Triple::sparcel:
1001 case Triple::thumbeb:
1007 case Triple::le64: T.setArch(Triple::le32); break;
1008 case Triple::mips64: T.setArch(Triple::mips); break;
1009 case Triple::mips64el: T.setArch(Triple::mipsel); break;
1010 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
1011 case Triple::ppc64: T.setArch(Triple::ppc); break;
1012 case Triple::sparcv9: T.setArch(Triple::sparc); break;
1013 case Triple::x86_64: T.setArch(Triple::x86); break;
1014 case Triple::amdil64: T.setArch(Triple::amdil); break;
1015 case Triple::hsail64: T.setArch(Triple::hsail); break;
1016 case Triple::spir64: T.setArch(Triple::spir); break;
1021 Triple Triple::get64BitArchVariant() const {
1023 switch (getArch()) {
1024 case Triple::UnknownArch:
1027 case Triple::hexagon:
1028 case Triple::kalimba:
1029 case Triple::msp430:
1033 case Triple::thumbeb:
1035 case Triple::sparcel:
1036 T.setArch(UnknownArch);
1039 case Triple::aarch64:
1040 case Triple::aarch64_be:
1043 case Triple::amdil64:
1044 case Triple::amdgcn:
1045 case Triple::hsail64:
1046 case Triple::spir64:
1047 case Triple::mips64:
1048 case Triple::mips64el:
1049 case Triple::nvptx64:
1051 case Triple::ppc64le:
1052 case Triple::sparcv9:
1053 case Triple::systemz:
1054 case Triple::x86_64:
1058 case Triple::le32: T.setArch(Triple::le64); break;
1059 case Triple::mips: T.setArch(Triple::mips64); break;
1060 case Triple::mipsel: T.setArch(Triple::mips64el); break;
1061 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
1062 case Triple::ppc: T.setArch(Triple::ppc64); break;
1063 case Triple::sparc: T.setArch(Triple::sparcv9); break;
1064 case Triple::x86: T.setArch(Triple::x86_64); break;
1065 case Triple::amdil: T.setArch(Triple::amdil64); break;
1066 case Triple::hsail: T.setArch(Triple::hsail64); break;
1067 case Triple::spir: T.setArch(Triple::spir64); break;
1072 const char *Triple::getARMCPUForArch(StringRef MArch) const {
1074 MArch = getArchName();
1076 // Some defaults are forced.
1078 case llvm::Triple::FreeBSD:
1079 case llvm::Triple::NetBSD:
1080 // FIXME: This doesn't work on BE/thumb variants.
1081 if (MArch == "armv6")
1082 return "arm1176jzf-s";
1084 case llvm::Triple::Win32:
1085 // FIXME: this is invalid for WindowsCE
1091 MArch = ARMTargetParser::getCanonicalArchName(MArch);
1095 const char *CPU = ARMTargetParser::getDefaultCPU(MArch);
1099 // If no specific architecture version is requested, return the minimum CPU
1100 // required by the OS and environment.
1102 case llvm::Triple::NetBSD:
1103 switch (getEnvironment()) {
1104 case llvm::Triple::GNUEABIHF:
1105 case llvm::Triple::GNUEABI:
1106 case llvm::Triple::EABIHF:
1107 case llvm::Triple::EABI:
1108 return "arm926ej-s";
1112 case llvm::Triple::NaCl:
1115 switch (getEnvironment()) {
1116 case llvm::Triple::EABIHF:
1117 case llvm::Triple::GNUEABIHF:
1118 return "arm1176jzf-s";
1124 llvm_unreachable("invalid arch name");