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"
18 const char *Triple::getArchTypeName(ArchType Kind) {
20 case UnknownArch: return "unknown";
22 case aarch64: return "aarch64";
23 case aarch64_be: return "aarch64_be";
24 case arm: return "arm";
25 case armeb: return "armeb";
26 case hexagon: return "hexagon";
27 case mips: return "mips";
28 case mipsel: return "mipsel";
29 case mips64: return "mips64";
30 case mips64el: return "mips64el";
31 case msp430: return "msp430";
32 case ppc64: return "powerpc64";
33 case ppc64le: return "powerpc64le";
34 case ppc: return "powerpc";
35 case r600: return "r600";
36 case sparc: return "sparc";
37 case sparcv9: return "sparcv9";
38 case systemz: return "s390x";
39 case tce: return "tce";
40 case thumb: return "thumb";
41 case thumbeb: return "thumbeb";
42 case x86: return "i386";
43 case x86_64: return "x86_64";
44 case xcore: return "xcore";
45 case nvptx: return "nvptx";
46 case nvptx64: return "nvptx64";
47 case le32: return "le32";
48 case amdil: return "amdil";
49 case spir: return "spir";
50 case spir64: return "spir64";
51 case kalimba: return "kalimba";
54 llvm_unreachable("Invalid ArchType!");
57 const char *Triple::getArchTypePrefix(ArchType Kind) {
63 case aarch64_be: return "aarch64";
68 case thumbeb: return "arm";
72 case ppc: return "ppc";
77 case mips64el: return "mips";
79 case hexagon: return "hexagon";
81 case r600: return "r600";
84 case sparc: return "sparc";
86 case systemz: return "systemz";
89 case x86_64: return "x86";
91 case xcore: return "xcore";
93 case nvptx: return "nvptx";
94 case nvptx64: return "nvptx";
96 case le32: return "le32";
97 case amdil: return "amdil";
98 case spir: return "spir";
99 case spir64: return "spir";
100 case kalimba: return "kalimba";
104 const char *Triple::getVendorTypeName(VendorType Kind) {
106 case UnknownVendor: return "unknown";
108 case Apple: return "apple";
109 case PC: return "pc";
110 case SCEI: return "scei";
111 case BGP: return "bgp";
112 case BGQ: return "bgq";
113 case Freescale: return "fsl";
114 case IBM: return "ibm";
115 case ImaginationTechnologies: return "img";
116 case MipsTechnologies: return "mti";
117 case NVIDIA: return "nvidia";
118 case CSR: return "csr";
121 llvm_unreachable("Invalid VendorType!");
124 const char *Triple::getOSTypeName(OSType Kind) {
126 case UnknownOS: return "unknown";
128 case Darwin: return "darwin";
129 case DragonFly: return "dragonfly";
130 case FreeBSD: return "freebsd";
131 case IOS: return "ios";
132 case KFreeBSD: return "kfreebsd";
133 case Linux: return "linux";
134 case Lv2: return "lv2";
135 case MacOSX: return "macosx";
136 case NetBSD: return "netbsd";
137 case OpenBSD: return "openbsd";
138 case Solaris: return "solaris";
139 case Win32: return "windows";
140 case Haiku: return "haiku";
141 case Minix: return "minix";
142 case RTEMS: return "rtems";
143 case NaCl: return "nacl";
144 case CNK: return "cnk";
145 case Bitrig: return "bitrig";
146 case AIX: return "aix";
147 case CUDA: return "cuda";
148 case NVCL: return "nvcl";
151 llvm_unreachable("Invalid OSType");
154 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
156 case UnknownEnvironment: return "unknown";
157 case GNU: return "gnu";
158 case GNUEABIHF: return "gnueabihf";
159 case GNUEABI: return "gnueabi";
160 case GNUX32: return "gnux32";
161 case CODE16: return "code16";
162 case EABI: return "eabi";
163 case EABIHF: return "eabihf";
164 case Android: return "android";
165 case MSVC: return "msvc";
166 case Itanium: return "itanium";
167 case Cygnus: return "cygnus";
170 llvm_unreachable("Invalid EnvironmentType!");
173 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
174 return StringSwitch<Triple::ArchType>(Name)
175 .Case("aarch64", aarch64)
176 .Case("aarch64_be", aarch64_be)
177 .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
179 .Case("armeb", armeb)
181 .Case("mipsel", mipsel)
182 .Case("mips64", mips64)
183 .Case("mips64el", mips64el)
184 .Case("msp430", msp430)
185 .Case("ppc64", ppc64)
188 .Case("ppc64le", ppc64le)
190 .Case("hexagon", hexagon)
191 .Case("sparc", sparc)
192 .Case("sparcv9", sparcv9)
193 .Case("systemz", systemz)
195 .Case("thumb", thumb)
196 .Case("thumbeb", thumbeb)
198 .Case("x86-64", x86_64)
199 .Case("xcore", xcore)
200 .Case("nvptx", nvptx)
201 .Case("nvptx64", nvptx64)
203 .Case("amdil", amdil)
205 .Case("spir64", spir64)
206 .Case("kalimba", kalimba)
207 .Default(UnknownArch);
210 static Triple::ArchType parseArch(StringRef ArchName) {
211 return StringSwitch<Triple::ArchType>(ArchName)
212 .Cases("i386", "i486", "i586", "i686", Triple::x86)
213 // FIXME: Do we need to support these?
214 .Cases("i786", "i886", "i986", Triple::x86)
215 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
216 .Case("powerpc", Triple::ppc)
217 .Cases("powerpc64", "ppu", Triple::ppc64)
218 .Case("powerpc64le", Triple::ppc64le)
219 .Case("aarch64", Triple::aarch64)
220 .Case("aarch64_be", Triple::aarch64_be)
221 .Case("arm64", Triple::aarch64)
222 .Cases("arm", "xscale", Triple::arm)
223 // FIXME: It would be good to replace these with explicit names for all the
224 // various suffixes supported.
225 .StartsWith("armv", Triple::arm)
226 .Case("armeb", Triple::armeb)
227 .StartsWith("armebv", Triple::armeb)
228 .Case("thumb", Triple::thumb)
229 .StartsWith("thumbv", Triple::thumb)
230 .Case("thumbeb", Triple::thumbeb)
231 .StartsWith("thumbebv", Triple::thumbeb)
232 .Case("msp430", Triple::msp430)
233 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
234 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
235 .Cases("mips64", "mips64eb", Triple::mips64)
236 .Case("mips64el", Triple::mips64el)
237 .Case("r600", Triple::r600)
238 .Case("hexagon", Triple::hexagon)
239 .Case("s390x", Triple::systemz)
240 .Case("sparc", Triple::sparc)
241 .Cases("sparcv9", "sparc64", Triple::sparcv9)
242 .Case("tce", Triple::tce)
243 .Case("xcore", Triple::xcore)
244 .Case("nvptx", Triple::nvptx)
245 .Case("nvptx64", Triple::nvptx64)
246 .Case("le32", Triple::le32)
247 .Case("amdil", Triple::amdil)
248 .Case("spir", Triple::spir)
249 .Case("spir64", Triple::spir64)
250 .StartsWith("kalimba", Triple::kalimba)
251 .Default(Triple::UnknownArch);
254 static Triple::VendorType parseVendor(StringRef VendorName) {
255 return StringSwitch<Triple::VendorType>(VendorName)
256 .Case("apple", Triple::Apple)
257 .Case("pc", Triple::PC)
258 .Case("scei", Triple::SCEI)
259 .Case("bgp", Triple::BGP)
260 .Case("bgq", Triple::BGQ)
261 .Case("fsl", Triple::Freescale)
262 .Case("ibm", Triple::IBM)
263 .Case("img", Triple::ImaginationTechnologies)
264 .Case("mti", Triple::MipsTechnologies)
265 .Case("nvidia", Triple::NVIDIA)
266 .Case("csr", Triple::CSR)
267 .Default(Triple::UnknownVendor);
270 static Triple::OSType parseOS(StringRef OSName) {
271 return StringSwitch<Triple::OSType>(OSName)
272 .StartsWith("darwin", Triple::Darwin)
273 .StartsWith("dragonfly", Triple::DragonFly)
274 .StartsWith("freebsd", Triple::FreeBSD)
275 .StartsWith("ios", Triple::IOS)
276 .StartsWith("kfreebsd", Triple::KFreeBSD)
277 .StartsWith("linux", Triple::Linux)
278 .StartsWith("lv2", Triple::Lv2)
279 .StartsWith("macosx", Triple::MacOSX)
280 .StartsWith("netbsd", Triple::NetBSD)
281 .StartsWith("openbsd", Triple::OpenBSD)
282 .StartsWith("solaris", Triple::Solaris)
283 .StartsWith("win32", Triple::Win32)
284 .StartsWith("windows", Triple::Win32)
285 .StartsWith("haiku", Triple::Haiku)
286 .StartsWith("minix", Triple::Minix)
287 .StartsWith("rtems", Triple::RTEMS)
288 .StartsWith("nacl", Triple::NaCl)
289 .StartsWith("cnk", Triple::CNK)
290 .StartsWith("bitrig", Triple::Bitrig)
291 .StartsWith("aix", Triple::AIX)
292 .StartsWith("cuda", Triple::CUDA)
293 .StartsWith("nvcl", Triple::NVCL)
294 .Default(Triple::UnknownOS);
297 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
298 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
299 .StartsWith("eabihf", Triple::EABIHF)
300 .StartsWith("eabi", Triple::EABI)
301 .StartsWith("gnueabihf", Triple::GNUEABIHF)
302 .StartsWith("gnueabi", Triple::GNUEABI)
303 .StartsWith("gnux32", Triple::GNUX32)
304 .StartsWith("code16", Triple::CODE16)
305 .StartsWith("gnu", Triple::GNU)
306 .StartsWith("android", Triple::Android)
307 .StartsWith("msvc", Triple::MSVC)
308 .StartsWith("itanium", Triple::Itanium)
309 .StartsWith("cygnus", Triple::Cygnus)
310 .Default(Triple::UnknownEnvironment);
313 static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
314 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
315 .EndsWith("coff", Triple::COFF)
316 .EndsWith("elf", Triple::ELF)
317 .EndsWith("macho", Triple::MachO)
318 .Default(Triple::UnknownObjectFormat);
321 static Triple::SubArchType parseSubArch(StringRef SubArchName) {
322 return StringSwitch<Triple::SubArchType>(SubArchName)
323 .EndsWith("v8", Triple::ARMSubArch_v8)
324 .EndsWith("v8a", Triple::ARMSubArch_v8)
325 .EndsWith("v7", Triple::ARMSubArch_v7)
326 .EndsWith("v7a", Triple::ARMSubArch_v7)
327 .EndsWith("v7em", Triple::ARMSubArch_v7em)
328 .EndsWith("v7l", Triple::ARMSubArch_v7)
329 .EndsWith("v7m", Triple::ARMSubArch_v7m)
330 .EndsWith("v7r", Triple::ARMSubArch_v7)
331 .EndsWith("v7s", Triple::ARMSubArch_v7s)
332 .EndsWith("v6", Triple::ARMSubArch_v6)
333 .EndsWith("v6m", Triple::ARMSubArch_v6m)
334 .EndsWith("v6t2", Triple::ARMSubArch_v6t2)
335 .EndsWith("v5", Triple::ARMSubArch_v5)
336 .EndsWith("v5e", Triple::ARMSubArch_v5)
337 .EndsWith("v5t", Triple::ARMSubArch_v5)
338 .EndsWith("v5te", Triple::ARMSubArch_v5te)
339 .EndsWith("v4t", Triple::ARMSubArch_v4t)
341 .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
342 .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
343 .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
345 .Default(Triple::NoSubArch);
348 static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
350 case Triple::UnknownObjectFormat: return "";
351 case Triple::COFF: return "coff";
352 case Triple::ELF: return "elf";
353 case Triple::MachO: return "macho";
355 llvm_unreachable("unknown object format type");
358 static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
360 return Triple::MachO;
361 else if (T.isOSWindows())
366 /// \brief Construct a triple from the string representation provided.
368 /// This stores the string representation and parses the various pieces into
370 Triple::Triple(const Twine &Str)
372 Arch(parseArch(getArchName())),
373 SubArch(parseSubArch(getArchName())),
374 Vendor(parseVendor(getVendorName())),
375 OS(parseOS(getOSName())),
376 Environment(parseEnvironment(getEnvironmentName())),
377 ObjectFormat(parseFormat(getEnvironmentName())) {
378 if (ObjectFormat == Triple::UnknownObjectFormat)
379 ObjectFormat = getDefaultFormat(*this);
382 /// \brief Construct a triple from string representations of the architecture,
385 /// This joins each argument into a canonical string representation and parses
386 /// them into enum members. It leaves the environment unknown and omits it from
387 /// the string representation.
388 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
389 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
390 Arch(parseArch(ArchStr.str())),
391 SubArch(parseSubArch(ArchStr.str())),
392 Vendor(parseVendor(VendorStr.str())),
393 OS(parseOS(OSStr.str())),
394 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
395 ObjectFormat = getDefaultFormat(*this);
398 /// \brief Construct a triple from string representations of the architecture,
399 /// vendor, OS, and environment.
401 /// This joins each argument into a canonical string representation and parses
402 /// them into enum members.
403 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
404 const Twine &EnvironmentStr)
405 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
406 EnvironmentStr).str()),
407 Arch(parseArch(ArchStr.str())),
408 SubArch(parseSubArch(ArchStr.str())),
409 Vendor(parseVendor(VendorStr.str())),
410 OS(parseOS(OSStr.str())),
411 Environment(parseEnvironment(EnvironmentStr.str())),
412 ObjectFormat(parseFormat(EnvironmentStr.str())) {
413 if (ObjectFormat == Triple::UnknownObjectFormat)
414 ObjectFormat = getDefaultFormat(*this);
417 std::string Triple::normalize(StringRef Str) {
418 bool IsMinGW32 = false;
419 bool IsCygwin = false;
421 // Parse into components.
422 SmallVector<StringRef, 4> Components;
423 Str.split(Components, "-");
425 // If the first component corresponds to a known architecture, preferentially
426 // use it for the architecture. If the second component corresponds to a
427 // known vendor, preferentially use it for the vendor, etc. This avoids silly
428 // component movement when a component parses as (eg) both a valid arch and a
430 ArchType Arch = UnknownArch;
431 if (Components.size() > 0)
432 Arch = parseArch(Components[0]);
433 VendorType Vendor = UnknownVendor;
434 if (Components.size() > 1)
435 Vendor = parseVendor(Components[1]);
436 OSType OS = UnknownOS;
437 if (Components.size() > 2) {
438 OS = parseOS(Components[2]);
439 IsCygwin = Components[2].startswith("cygwin");
440 IsMinGW32 = Components[2].startswith("mingw");
442 EnvironmentType Environment = UnknownEnvironment;
443 if (Components.size() > 3)
444 Environment = parseEnvironment(Components[3]);
445 ObjectFormatType ObjectFormat = UnknownObjectFormat;
446 if (Components.size() > 4)
447 ObjectFormat = parseFormat(Components[4]);
449 // Note which components are already in their final position. These will not
452 Found[0] = Arch != UnknownArch;
453 Found[1] = Vendor != UnknownVendor;
454 Found[2] = OS != UnknownOS;
455 Found[3] = Environment != UnknownEnvironment;
457 // If they are not there already, permute the components into their canonical
458 // positions by seeing if they parse as a valid architecture, and if so moving
459 // the component to the architecture position etc.
460 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
462 continue; // Already in the canonical position.
464 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
465 // Do not reparse any components that already matched.
466 if (Idx < array_lengthof(Found) && Found[Idx])
469 // Does this component parse as valid for the target position?
471 StringRef Comp = Components[Idx];
473 default: llvm_unreachable("unexpected component type!");
475 Arch = parseArch(Comp);
476 Valid = Arch != UnknownArch;
479 Vendor = parseVendor(Comp);
480 Valid = Vendor != UnknownVendor;
484 IsCygwin = Comp.startswith("cygwin");
485 IsMinGW32 = Comp.startswith("mingw");
486 Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
489 Environment = parseEnvironment(Comp);
490 Valid = Environment != UnknownEnvironment;
492 ObjectFormat = parseFormat(Comp);
493 Valid = ObjectFormat != UnknownObjectFormat;
498 continue; // Nope, try the next component.
500 // Move the component to the target position, pushing any non-fixed
501 // components that are in the way to the right. This tends to give
502 // good results in the common cases of a forgotten vendor component
503 // or a wrongly positioned environment.
505 // Insert left, pushing the existing components to the right. For
506 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
507 StringRef CurrentComponent(""); // The empty component.
508 // Replace the component we are moving with an empty component.
509 std::swap(CurrentComponent, Components[Idx]);
510 // Insert the component being moved at Pos, displacing any existing
511 // components to the right.
512 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
513 // Skip over any fixed components.
514 while (i < array_lengthof(Found) && Found[i])
516 // Place the component at the new position, getting the component
517 // that was at this position - it will be moved right.
518 std::swap(CurrentComponent, Components[i]);
520 } else if (Pos > Idx) {
521 // Push right by inserting empty components until the component at Idx
522 // reaches the target position Pos. For example, pc-a -> -pc-a when
523 // moving pc to the second position.
525 // Insert one empty component at Idx.
526 StringRef CurrentComponent(""); // The empty component.
527 for (unsigned i = Idx; i < Components.size();) {
528 // Place the component at the new position, getting the component
529 // that was at this position - it will be moved right.
530 std::swap(CurrentComponent, Components[i]);
531 // If it was placed on top of an empty component then we are done.
532 if (CurrentComponent.empty())
534 // Advance to the next component, skipping any fixed components.
535 while (++i < array_lengthof(Found) && Found[i])
538 // The last component was pushed off the end - append it.
539 if (!CurrentComponent.empty())
540 Components.push_back(CurrentComponent);
542 // Advance Idx to the component's new position.
543 while (++Idx < array_lengthof(Found) && Found[Idx])
545 } while (Idx < Pos); // Add more until the final position is reached.
547 assert(Pos < Components.size() && Components[Pos] == Comp &&
548 "Component moved wrong!");
554 // Special case logic goes here. At this point Arch, Vendor and OS have the
555 // correct values for the computed components.
557 if (OS == Triple::Win32) {
558 Components.resize(4);
559 Components[2] = "windows";
560 if (Environment == UnknownEnvironment) {
561 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
562 Components[3] = "msvc";
564 Components[3] = getObjectFormatTypeName(ObjectFormat);
566 } else if (IsMinGW32) {
567 Components.resize(4);
568 Components[2] = "windows";
569 Components[3] = "gnu";
570 } else if (IsCygwin) {
571 Components.resize(4);
572 Components[2] = "windows";
573 Components[3] = "cygnus";
575 if (IsMinGW32 || IsCygwin ||
576 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
577 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
578 Components.resize(5);
579 Components[4] = getObjectFormatTypeName(ObjectFormat);
583 // Stick the corrected components back together to form the normalized string.
584 std::string Normalized;
585 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
586 if (i) Normalized += '-';
587 Normalized += Components[i];
592 StringRef Triple::getArchName() const {
593 return StringRef(Data).split('-').first; // Isolate first component
596 StringRef Triple::getVendorName() const {
597 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
598 return Tmp.split('-').first; // Isolate second component
601 StringRef Triple::getOSName() const {
602 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
603 Tmp = Tmp.split('-').second; // Strip second component
604 return Tmp.split('-').first; // Isolate third component
607 StringRef Triple::getEnvironmentName() const {
608 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
609 Tmp = Tmp.split('-').second; // Strip second component
610 return Tmp.split('-').second; // Strip third component
613 StringRef Triple::getOSAndEnvironmentName() const {
614 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
615 return Tmp.split('-').second; // Strip second component
618 static unsigned EatNumber(StringRef &Str) {
619 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
623 // Consume the leading digit.
624 Result = Result*10 + (Str[0] - '0');
628 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
633 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
634 unsigned &Micro) const {
635 StringRef OSName = getOSName();
637 // Assume that the OS portion of the triple starts with the canonical name.
638 StringRef OSTypeName = getOSTypeName(getOS());
639 if (OSName.startswith(OSTypeName))
640 OSName = OSName.substr(OSTypeName.size());
642 // Any unset version defaults to 0.
643 Major = Minor = Micro = 0;
645 // Parse up to three components.
646 unsigned *Components[3] = { &Major, &Minor, &Micro };
647 for (unsigned i = 0; i != 3; ++i) {
648 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
651 // Consume the leading number.
652 *Components[i] = EatNumber(OSName);
654 // Consume the separator, if present.
655 if (OSName.startswith("."))
656 OSName = OSName.substr(1);
660 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
661 unsigned &Micro) const {
662 getOSVersion(Major, Minor, Micro);
665 default: llvm_unreachable("unexpected OS for Darwin triple");
667 // Default to darwin8, i.e., MacOSX 10.4.
670 // Darwin version numbers are skewed from OS X versions.
687 // Ignore the version from the triple. This is only handled because the
688 // the clang driver combines OS X and IOS support into a common Darwin
689 // toolchain that wants to know the OS X version number even when targeting
699 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
700 unsigned &Micro) const {
702 default: llvm_unreachable("unexpected OS for Darwin triple");
705 // Ignore the version from the triple. This is only handled because the
706 // the clang driver combines OS X and IOS support into a common Darwin
707 // toolchain that wants to know the iOS version number even when targeting
714 getOSVersion(Major, Minor, Micro);
715 // Default to 5.0 (or 7.0 for arm64).
717 Major = (getArch() == aarch64) ? 7 : 5;
722 void Triple::setTriple(const Twine &Str) {
726 void Triple::setArch(ArchType Kind) {
727 setArchName(getArchTypeName(Kind));
730 void Triple::setVendor(VendorType Kind) {
731 setVendorName(getVendorTypeName(Kind));
734 void Triple::setOS(OSType Kind) {
735 setOSName(getOSTypeName(Kind));
738 void Triple::setEnvironment(EnvironmentType Kind) {
739 setEnvironmentName(getEnvironmentTypeName(Kind));
742 void Triple::setObjectFormat(ObjectFormatType Kind) {
743 if (Environment == UnknownEnvironment)
744 return setEnvironmentName(getObjectFormatTypeName(Kind));
746 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
747 getObjectFormatTypeName(Kind)).str());
750 void Triple::setArchName(StringRef Str) {
751 // Work around a miscompilation bug for Twines in gcc 4.0.3.
752 SmallString<64> Triple;
755 Triple += getVendorName();
757 Triple += getOSAndEnvironmentName();
758 setTriple(Triple.str());
761 void Triple::setVendorName(StringRef Str) {
762 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
765 void Triple::setOSName(StringRef Str) {
766 if (hasEnvironment())
767 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
768 "-" + getEnvironmentName());
770 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
773 void Triple::setEnvironmentName(StringRef Str) {
774 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
778 void Triple::setOSAndEnvironmentName(StringRef Str) {
779 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
782 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
784 case llvm::Triple::UnknownArch:
787 case llvm::Triple::msp430:
790 case llvm::Triple::amdil:
791 case llvm::Triple::arm:
792 case llvm::Triple::armeb:
793 case llvm::Triple::hexagon:
794 case llvm::Triple::le32:
795 case llvm::Triple::mips:
796 case llvm::Triple::mipsel:
797 case llvm::Triple::nvptx:
798 case llvm::Triple::ppc:
799 case llvm::Triple::r600:
800 case llvm::Triple::sparc:
801 case llvm::Triple::tce:
802 case llvm::Triple::thumb:
803 case llvm::Triple::thumbeb:
804 case llvm::Triple::x86:
805 case llvm::Triple::xcore:
806 case llvm::Triple::spir:
807 case llvm::Triple::kalimba:
810 case llvm::Triple::aarch64:
811 case llvm::Triple::aarch64_be:
812 case llvm::Triple::mips64:
813 case llvm::Triple::mips64el:
814 case llvm::Triple::nvptx64:
815 case llvm::Triple::ppc64:
816 case llvm::Triple::ppc64le:
817 case llvm::Triple::sparcv9:
818 case llvm::Triple::systemz:
819 case llvm::Triple::x86_64:
820 case llvm::Triple::spir64:
823 llvm_unreachable("Invalid architecture value");
826 bool Triple::isArch64Bit() const {
827 return getArchPointerBitWidth(getArch()) == 64;
830 bool Triple::isArch32Bit() const {
831 return getArchPointerBitWidth(getArch()) == 32;
834 bool Triple::isArch16Bit() const {
835 return getArchPointerBitWidth(getArch()) == 16;
838 Triple Triple::get32BitArchVariant() const {
841 case Triple::UnknownArch:
842 case Triple::aarch64:
843 case Triple::aarch64_be:
845 case Triple::systemz:
846 case Triple::ppc64le:
847 T.setArch(UnknownArch);
854 case Triple::hexagon:
855 case Triple::kalimba:
865 case Triple::thumbeb:
871 case Triple::mips64: T.setArch(Triple::mips); break;
872 case Triple::mips64el: T.setArch(Triple::mipsel); break;
873 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
874 case Triple::ppc64: T.setArch(Triple::ppc); break;
875 case Triple::sparcv9: T.setArch(Triple::sparc); break;
876 case Triple::x86_64: T.setArch(Triple::x86); break;
877 case Triple::spir64: T.setArch(Triple::spir); break;
882 Triple Triple::get64BitArchVariant() const {
885 case Triple::UnknownArch:
889 case Triple::hexagon:
890 case Triple::kalimba:
896 case Triple::thumbeb:
898 T.setArch(UnknownArch);
901 case Triple::aarch64:
902 case Triple::aarch64_be:
905 case Triple::mips64el:
906 case Triple::nvptx64:
908 case Triple::ppc64le:
909 case Triple::sparcv9:
910 case Triple::systemz:
915 case Triple::mips: T.setArch(Triple::mips64); break;
916 case Triple::mipsel: T.setArch(Triple::mips64el); break;
917 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
918 case Triple::ppc: T.setArch(Triple::ppc64); break;
919 case Triple::sparc: T.setArch(Triple::sparcv9); break;
920 case Triple::x86: T.setArch(Triple::x86_64); break;
921 case Triple::spir: T.setArch(Triple::spir64); break;
926 // FIXME: tblgen this.
927 const char *Triple::getARMCPUForArch(StringRef MArch) const {
929 MArch = getArchName();
932 case llvm::Triple::NetBSD:
933 if (MArch == "armv6")
934 return "arm1176jzf-s";
936 case llvm::Triple::Win32:
937 // FIXME: this is invalid for WindowsCE
943 const char *result = nullptr;
944 size_t offset = StringRef::npos;
945 if (MArch.startswith("arm"))
947 if (MArch.startswith("thumb"))
949 if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
951 if (offset != StringRef::npos)
952 result = llvm::StringSwitch<const char *>(MArch.substr(offset))
953 .Cases("v2", "v2a", "arm2")
955 .Case("v3m", "arm7m")
956 .Case("v4", "strongarm")
957 .Case("v4t", "arm7tdmi")
958 .Cases("v5", "v5t", "arm10tdmi")
959 .Cases("v5e", "v5te", "arm1022e")
960 .Case("v5tej", "arm926ej-s")
961 .Cases("v6", "v6k", "arm1136jf-s")
962 .Case("v6j", "arm1136j-s")
963 .Cases("v6z", "v6zk", "arm1176jzf-s")
964 .Case("v6t2", "arm1156t2-s")
965 .Cases("v6m", "v6-m", "cortex-m0")
966 .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
967 .Cases("v7s", "v7-s", "swift")
968 .Cases("v7r", "v7-r", "cortex-r4")
969 .Cases("v7m", "v7-m", "cortex-m3")
970 .Cases("v7em", "v7e-m", "cortex-m4")
971 .Cases("v8", "v8a", "v8-a", "cortex-a53")
974 result = llvm::StringSwitch<const char *>(MArch)
975 .Case("ep9312", "ep9312")
976 .Case("iwmmxt", "iwmmxt")
977 .Case("xscale", "xscale")
983 // If all else failed, return the most base CPU with thumb interworking
984 // supported by LLVM.
985 // FIXME: Should warn once that we're falling back.
987 case llvm::Triple::NetBSD:
988 switch (getEnvironment()) {
989 case llvm::Triple::GNUEABIHF:
990 case llvm::Triple::GNUEABI:
991 case llvm::Triple::EABIHF:
992 case llvm::Triple::EABI:
998 switch (getEnvironment()) {
999 case llvm::Triple::EABIHF:
1000 case llvm::Triple::GNUEABIHF:
1001 return "arm1176jzf-s";