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 arm64: return "arm64";
27 case arm64_be: return "arm64_be";
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 sparc: return "sparc";
39 case sparcv9: return "sparcv9";
40 case systemz: return "s390x";
41 case tce: return "tce";
42 case thumb: return "thumb";
43 case thumbeb: return "thumbeb";
44 case x86: return "i386";
45 case x86_64: return "x86_64";
46 case xcore: return "xcore";
47 case nvptx: return "nvptx";
48 case nvptx64: return "nvptx64";
49 case le32: return "le32";
50 case amdil: return "amdil";
51 case spir: return "spir";
52 case spir64: return "spir64";
53 case kalimba: return "kalimba";
56 llvm_unreachable("Invalid ArchType!");
59 const char *Triple::getArchTypePrefix(ArchType Kind) {
67 case aarch64_be: return "aarch64";
72 case thumbeb: return "arm";
76 case ppc: return "ppc";
81 case mips64el: return "mips";
83 case hexagon: return "hexagon";
85 case r600: return "r600";
88 case sparc: return "sparc";
90 case systemz: return "systemz";
93 case x86_64: return "x86";
95 case xcore: return "xcore";
97 case nvptx: return "nvptx";
98 case nvptx64: return "nvptx";
100 case le32: return "le32";
101 case amdil: return "amdil";
102 case spir: return "spir";
103 case spir64: return "spir";
104 case kalimba: return "kalimba";
108 const char *Triple::getVendorTypeName(VendorType Kind) {
110 case UnknownVendor: return "unknown";
112 case Apple: return "apple";
113 case PC: return "pc";
114 case SCEI: return "scei";
115 case BGP: return "bgp";
116 case BGQ: return "bgq";
117 case Freescale: return "fsl";
118 case IBM: return "ibm";
119 case ImaginationTechnologies: return "img";
120 case MipsTechnologies: return "mti";
121 case NVIDIA: return "nvidia";
122 case CSR: return "csr";
125 llvm_unreachable("Invalid VendorType!");
128 const char *Triple::getOSTypeName(OSType Kind) {
130 case UnknownOS: return "unknown";
132 case AuroraUX: return "auroraux";
133 case Cygwin: return "cygwin";
134 case Darwin: return "darwin";
135 case DragonFly: return "dragonfly";
136 case FreeBSD: return "freebsd";
137 case IOS: return "ios";
138 case KFreeBSD: return "kfreebsd";
139 case Linux: return "linux";
140 case Lv2: return "lv2";
141 case MacOSX: return "macosx";
142 case MinGW32: return "mingw32";
143 case NetBSD: return "netbsd";
144 case OpenBSD: return "openbsd";
145 case Solaris: return "solaris";
146 case Win32: return "windows";
147 case Haiku: return "haiku";
148 case Minix: return "minix";
149 case RTEMS: return "rtems";
150 case NaCl: return "nacl";
151 case CNK: return "cnk";
152 case Bitrig: return "bitrig";
153 case AIX: return "aix";
154 case CUDA: return "cuda";
155 case NVCL: return "nvcl";
158 llvm_unreachable("Invalid OSType");
161 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
163 case UnknownEnvironment: return "unknown";
164 case GNU: return "gnu";
165 case GNUEABIHF: return "gnueabihf";
166 case GNUEABI: return "gnueabi";
167 case GNUX32: return "gnux32";
168 case CODE16: return "code16";
169 case EABI: return "eabi";
170 case EABIHF: return "eabihf";
171 case Android: return "android";
172 case MSVC: return "msvc";
173 case Itanium: return "itanium";
174 case Cygnus: return "cygnus";
177 llvm_unreachable("Invalid EnvironmentType!");
180 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
181 return StringSwitch<Triple::ArchType>(Name)
182 .Case("aarch64", aarch64)
183 .Case("aarch64_be", aarch64_be)
185 .Case("armeb", armeb)
186 .Case("arm64", arm64)
187 .Case("arm64_be", arm64_be)
189 .Case("mipsel", mipsel)
190 .Case("mips64", mips64)
191 .Case("mips64el", mips64el)
192 .Case("msp430", msp430)
193 .Case("ppc64", ppc64)
196 .Case("ppc64le", ppc64le)
198 .Case("hexagon", hexagon)
199 .Case("sparc", sparc)
200 .Case("sparcv9", sparcv9)
201 .Case("systemz", systemz)
203 .Case("thumb", thumb)
204 .Case("thumbeb", thumbeb)
206 .Case("x86-64", x86_64)
207 .Case("xcore", xcore)
208 .Case("nvptx", nvptx)
209 .Case("nvptx64", nvptx64)
211 .Case("amdil", amdil)
213 .Case("spir64", spir64)
214 .Case("kalimba", kalimba)
215 .Default(UnknownArch);
218 // Returns architecture name that is understood by the target assembler.
219 const char *Triple::getArchNameForAssembler() {
220 if (!isOSDarwin() && getVendor() != Triple::Apple)
223 return StringSwitch<const char*>(getArchName())
224 .Case("i386", "i386")
225 .Case("x86_64", "x86_64")
226 .Case("powerpc", "ppc")
227 .Case("powerpc64", "ppc64")
228 .Case("powerpc64le", "ppc64le")
230 .Cases("armv4t", "thumbv4t", "armv4t")
231 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
232 .Cases("armv6", "thumbv6", "armv6")
233 .Cases("armv7", "thumbv7", "armv7")
234 .Case("armeb", "armeb")
235 .Case("arm64", "arm64")
236 .Case("arm64_be", "arm64")
237 .Case("r600", "r600")
238 .Case("nvptx", "nvptx")
239 .Case("nvptx64", "nvptx64")
240 .Case("le32", "le32")
241 .Case("amdil", "amdil")
242 .Case("spir", "spir")
243 .Case("spir64", "spir64")
247 static Triple::ArchType parseArch(StringRef ArchName) {
248 return StringSwitch<Triple::ArchType>(ArchName)
249 .Cases("i386", "i486", "i586", "i686", Triple::x86)
250 // FIXME: Do we need to support these?
251 .Cases("i786", "i886", "i986", Triple::x86)
252 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
253 .Case("powerpc", Triple::ppc)
254 .Cases("powerpc64", "ppu", Triple::ppc64)
255 .Case("powerpc64le", Triple::ppc64le)
256 .Case("aarch64", Triple::aarch64)
257 .Case("aarch64_be", Triple::aarch64_be)
258 .Cases("arm", "xscale", Triple::arm)
259 // FIXME: It would be good to replace these with explicit names for all the
260 // various suffixes supported.
261 .StartsWith("armv", Triple::arm)
262 .Case("armeb", Triple::armeb)
263 .StartsWith("armebv", Triple::armeb)
264 .Case("thumb", Triple::thumb)
265 .StartsWith("thumbv", Triple::thumb)
266 .Case("thumbeb", Triple::thumbeb)
267 .StartsWith("thumbebv", Triple::thumbeb)
268 .Case("arm64", Triple::arm64)
269 .Case("arm64_be", Triple::arm64_be)
270 .Case("msp430", Triple::msp430)
271 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
272 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
273 .Cases("mips64", "mips64eb", Triple::mips64)
274 .Case("mips64el", Triple::mips64el)
275 .Case("r600", Triple::r600)
276 .Case("hexagon", Triple::hexagon)
277 .Case("s390x", Triple::systemz)
278 .Case("sparc", Triple::sparc)
279 .Cases("sparcv9", "sparc64", Triple::sparcv9)
280 .Case("tce", Triple::tce)
281 .Case("xcore", Triple::xcore)
282 .Case("nvptx", Triple::nvptx)
283 .Case("nvptx64", Triple::nvptx64)
284 .Case("le32", Triple::le32)
285 .Case("amdil", Triple::amdil)
286 .Case("spir", Triple::spir)
287 .Case("spir64", Triple::spir64)
288 .Case("kalimba", Triple::kalimba)
289 .Default(Triple::UnknownArch);
292 static Triple::VendorType parseVendor(StringRef VendorName) {
293 return StringSwitch<Triple::VendorType>(VendorName)
294 .Case("apple", Triple::Apple)
295 .Case("pc", Triple::PC)
296 .Case("scei", Triple::SCEI)
297 .Case("bgp", Triple::BGP)
298 .Case("bgq", Triple::BGQ)
299 .Case("fsl", Triple::Freescale)
300 .Case("ibm", Triple::IBM)
301 .Case("img", Triple::ImaginationTechnologies)
302 .Case("mti", Triple::MipsTechnologies)
303 .Case("nvidia", Triple::NVIDIA)
304 .Case("csr", Triple::CSR)
305 .Default(Triple::UnknownVendor);
308 static Triple::OSType parseOS(StringRef OSName) {
309 return StringSwitch<Triple::OSType>(OSName)
310 .StartsWith("auroraux", Triple::AuroraUX)
311 .StartsWith("cygwin", Triple::Cygwin)
312 .StartsWith("darwin", Triple::Darwin)
313 .StartsWith("dragonfly", Triple::DragonFly)
314 .StartsWith("freebsd", Triple::FreeBSD)
315 .StartsWith("ios", Triple::IOS)
316 .StartsWith("kfreebsd", Triple::KFreeBSD)
317 .StartsWith("linux", Triple::Linux)
318 .StartsWith("lv2", Triple::Lv2)
319 .StartsWith("macosx", Triple::MacOSX)
320 .StartsWith("mingw32", Triple::MinGW32)
321 .StartsWith("netbsd", Triple::NetBSD)
322 .StartsWith("openbsd", Triple::OpenBSD)
323 .StartsWith("solaris", Triple::Solaris)
324 .StartsWith("win32", Triple::Win32)
325 .StartsWith("windows", Triple::Win32)
326 .StartsWith("haiku", Triple::Haiku)
327 .StartsWith("minix", Triple::Minix)
328 .StartsWith("rtems", Triple::RTEMS)
329 .StartsWith("nacl", Triple::NaCl)
330 .StartsWith("cnk", Triple::CNK)
331 .StartsWith("bitrig", Triple::Bitrig)
332 .StartsWith("aix", Triple::AIX)
333 .StartsWith("cuda", Triple::CUDA)
334 .StartsWith("nvcl", Triple::NVCL)
335 .Default(Triple::UnknownOS);
338 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
339 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
340 .StartsWith("eabihf", Triple::EABIHF)
341 .StartsWith("eabi", Triple::EABI)
342 .StartsWith("gnueabihf", Triple::GNUEABIHF)
343 .StartsWith("gnueabi", Triple::GNUEABI)
344 .StartsWith("gnux32", Triple::GNUX32)
345 .StartsWith("code16", Triple::CODE16)
346 .StartsWith("gnu", Triple::GNU)
347 .StartsWith("android", Triple::Android)
348 .StartsWith("msvc", Triple::MSVC)
349 .StartsWith("itanium", Triple::Itanium)
350 .StartsWith("cygnus", Triple::Cygnus)
351 .Default(Triple::UnknownEnvironment);
354 static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
355 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
356 .EndsWith("coff", Triple::COFF)
357 .EndsWith("elf", Triple::ELF)
358 .EndsWith("macho", Triple::MachO)
359 .Default(Triple::UnknownObjectFormat);
362 static Triple::SubArchType parseSubArch(StringRef SubArchName) {
363 return StringSwitch<Triple::SubArchType>(SubArchName)
364 .EndsWith("v8", Triple::ARMSubArch_v8)
365 .EndsWith("v8a", Triple::ARMSubArch_v8)
366 .EndsWith("v7", Triple::ARMSubArch_v7)
367 .EndsWith("v7a", Triple::ARMSubArch_v7)
368 .EndsWith("v7em", Triple::ARMSubArch_v7em)
369 .EndsWith("v7l", Triple::ARMSubArch_v7)
370 .EndsWith("v7m", Triple::ARMSubArch_v7m)
371 .EndsWith("v7r", Triple::ARMSubArch_v7)
372 .EndsWith("v7s", Triple::ARMSubArch_v7s)
373 .EndsWith("v6", Triple::ARMSubArch_v6)
374 .EndsWith("v6m", Triple::ARMSubArch_v6m)
375 .EndsWith("v6t2", Triple::ARMSubArch_v6t2)
376 .EndsWith("v5", Triple::ARMSubArch_v5)
377 .EndsWith("v5e", Triple::ARMSubArch_v5)
378 .EndsWith("v5t", Triple::ARMSubArch_v5)
379 .EndsWith("v5te", Triple::ARMSubArch_v5te)
380 .EndsWith("v4t", Triple::ARMSubArch_v4t)
381 .Default(Triple::NoSubArch);
384 static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
386 case Triple::UnknownObjectFormat: return "";
387 case Triple::COFF: return "coff";
388 case Triple::ELF: return "elf";
389 case Triple::MachO: return "macho";
391 llvm_unreachable("unknown object format type");
394 static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
396 return Triple::MachO;
397 else if (T.isOSWindows())
402 /// \brief Construct a triple from the string representation provided.
404 /// This stores the string representation and parses the various pieces into
406 Triple::Triple(const Twine &Str)
408 Arch(parseArch(getArchName())),
409 SubArch(parseSubArch(getArchName())),
410 Vendor(parseVendor(getVendorName())),
411 OS(parseOS(getOSName())),
412 Environment(parseEnvironment(getEnvironmentName())),
413 ObjectFormat(parseFormat(getEnvironmentName())) {
414 if (ObjectFormat == Triple::UnknownObjectFormat)
415 ObjectFormat = getDefaultFormat(*this);
418 /// \brief Construct a triple from string representations of the architecture,
421 /// This joins each argument into a canonical string representation and parses
422 /// them into enum members. It leaves the environment unknown and omits it from
423 /// the string representation.
424 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
425 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
426 Arch(parseArch(ArchStr.str())),
427 SubArch(parseSubArch(ArchStr.str())),
428 Vendor(parseVendor(VendorStr.str())),
429 OS(parseOS(OSStr.str())),
430 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
431 ObjectFormat = getDefaultFormat(*this);
434 /// \brief Construct a triple from string representations of the architecture,
435 /// vendor, OS, and environment.
437 /// This joins each argument into a canonical string representation and parses
438 /// them into enum members.
439 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
440 const Twine &EnvironmentStr)
441 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
442 EnvironmentStr).str()),
443 Arch(parseArch(ArchStr.str())),
444 SubArch(parseSubArch(ArchStr.str())),
445 Vendor(parseVendor(VendorStr.str())),
446 OS(parseOS(OSStr.str())),
447 Environment(parseEnvironment(EnvironmentStr.str())),
448 ObjectFormat(parseFormat(EnvironmentStr.str())) {
449 if (ObjectFormat == Triple::UnknownObjectFormat)
450 ObjectFormat = getDefaultFormat(*this);
453 std::string Triple::normalize(StringRef Str) {
454 // Parse into components.
455 SmallVector<StringRef, 4> Components;
456 Str.split(Components, "-");
458 // If the first component corresponds to a known architecture, preferentially
459 // use it for the architecture. If the second component corresponds to a
460 // known vendor, preferentially use it for the vendor, etc. This avoids silly
461 // component movement when a component parses as (eg) both a valid arch and a
463 ArchType Arch = UnknownArch;
464 if (Components.size() > 0)
465 Arch = parseArch(Components[0]);
466 VendorType Vendor = UnknownVendor;
467 if (Components.size() > 1)
468 Vendor = parseVendor(Components[1]);
469 OSType OS = UnknownOS;
470 if (Components.size() > 2)
471 OS = parseOS(Components[2]);
472 EnvironmentType Environment = UnknownEnvironment;
473 if (Components.size() > 3)
474 Environment = parseEnvironment(Components[3]);
475 ObjectFormatType ObjectFormat = UnknownObjectFormat;
476 if (Components.size() > 4)
477 ObjectFormat = parseFormat(Components[4]);
479 // Note which components are already in their final position. These will not
482 Found[0] = Arch != UnknownArch;
483 Found[1] = Vendor != UnknownVendor;
484 Found[2] = OS != UnknownOS;
485 Found[3] = Environment != UnknownEnvironment;
487 // If they are not there already, permute the components into their canonical
488 // positions by seeing if they parse as a valid architecture, and if so moving
489 // the component to the architecture position etc.
490 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
492 continue; // Already in the canonical position.
494 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
495 // Do not reparse any components that already matched.
496 if (Idx < array_lengthof(Found) && Found[Idx])
499 // Does this component parse as valid for the target position?
501 StringRef Comp = Components[Idx];
503 default: llvm_unreachable("unexpected component type!");
505 Arch = parseArch(Comp);
506 Valid = Arch != UnknownArch;
509 Vendor = parseVendor(Comp);
510 Valid = Vendor != UnknownVendor;
514 Valid = OS != UnknownOS;
517 Environment = parseEnvironment(Comp);
518 Valid = Environment != UnknownEnvironment;
520 ObjectFormat = parseFormat(Comp);
521 Valid = ObjectFormat != UnknownObjectFormat;
526 continue; // Nope, try the next component.
528 // Move the component to the target position, pushing any non-fixed
529 // components that are in the way to the right. This tends to give
530 // good results in the common cases of a forgotten vendor component
531 // or a wrongly positioned environment.
533 // Insert left, pushing the existing components to the right. For
534 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
535 StringRef CurrentComponent(""); // The empty component.
536 // Replace the component we are moving with an empty component.
537 std::swap(CurrentComponent, Components[Idx]);
538 // Insert the component being moved at Pos, displacing any existing
539 // components to the right.
540 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
541 // Skip over any fixed components.
542 while (i < array_lengthof(Found) && Found[i])
544 // Place the component at the new position, getting the component
545 // that was at this position - it will be moved right.
546 std::swap(CurrentComponent, Components[i]);
548 } else if (Pos > Idx) {
549 // Push right by inserting empty components until the component at Idx
550 // reaches the target position Pos. For example, pc-a -> -pc-a when
551 // moving pc to the second position.
553 // Insert one empty component at Idx.
554 StringRef CurrentComponent(""); // The empty component.
555 for (unsigned i = Idx; i < Components.size();) {
556 // Place the component at the new position, getting the component
557 // that was at this position - it will be moved right.
558 std::swap(CurrentComponent, Components[i]);
559 // If it was placed on top of an empty component then we are done.
560 if (CurrentComponent.empty())
562 // Advance to the next component, skipping any fixed components.
563 while (++i < array_lengthof(Found) && Found[i])
566 // The last component was pushed off the end - append it.
567 if (!CurrentComponent.empty())
568 Components.push_back(CurrentComponent);
570 // Advance Idx to the component's new position.
571 while (++Idx < array_lengthof(Found) && Found[Idx])
573 } while (Idx < Pos); // Add more until the final position is reached.
575 assert(Pos < Components.size() && Components[Pos] == Comp &&
576 "Component moved wrong!");
582 // Special case logic goes here. At this point Arch, Vendor and OS have the
583 // correct values for the computed components.
585 if (OS == Triple::Win32) {
586 Components.resize(4);
587 Components[2] = "windows";
588 if (Environment == UnknownEnvironment) {
589 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
590 Components[3] = "msvc";
592 Components[3] = getObjectFormatTypeName(ObjectFormat);
594 } else if (OS == Triple::MinGW32) {
595 Components.resize(4);
596 Components[2] = "windows";
597 Components[3] = "gnu";
598 } else if (OS == Triple::Cygwin) {
599 Components.resize(4);
600 Components[2] = "windows";
601 Components[3] = "cygnus";
603 if (OS == Triple::MinGW32 || OS == Triple::Cygwin ||
604 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
605 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
606 Components.resize(5);
607 Components[4] = getObjectFormatTypeName(ObjectFormat);
611 // Stick the corrected components back together to form the normalized string.
612 std::string Normalized;
613 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
614 if (i) Normalized += '-';
615 Normalized += Components[i];
620 StringRef Triple::getArchName() const {
621 return StringRef(Data).split('-').first; // Isolate first component
624 StringRef Triple::getVendorName() const {
625 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
626 return Tmp.split('-').first; // Isolate second component
629 StringRef Triple::getOSName() const {
630 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
631 Tmp = Tmp.split('-').second; // Strip second component
632 return Tmp.split('-').first; // Isolate third component
635 StringRef Triple::getEnvironmentName() const {
636 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
637 Tmp = Tmp.split('-').second; // Strip second component
638 return Tmp.split('-').second; // Strip third component
641 StringRef Triple::getOSAndEnvironmentName() const {
642 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
643 return Tmp.split('-').second; // Strip second component
646 static unsigned EatNumber(StringRef &Str) {
647 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
651 // Consume the leading digit.
652 Result = Result*10 + (Str[0] - '0');
656 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
661 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
662 unsigned &Micro) const {
663 StringRef OSName = getOSName();
665 // Assume that the OS portion of the triple starts with the canonical name.
666 StringRef OSTypeName = getOSTypeName(getOS());
667 if (OSName.startswith(OSTypeName))
668 OSName = OSName.substr(OSTypeName.size());
670 // Any unset version defaults to 0.
671 Major = Minor = Micro = 0;
673 // Parse up to three components.
674 unsigned *Components[3] = { &Major, &Minor, &Micro };
675 for (unsigned i = 0; i != 3; ++i) {
676 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
679 // Consume the leading number.
680 *Components[i] = EatNumber(OSName);
682 // Consume the separator, if present.
683 if (OSName.startswith("."))
684 OSName = OSName.substr(1);
688 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
689 unsigned &Micro) const {
690 getOSVersion(Major, Minor, Micro);
693 default: llvm_unreachable("unexpected OS for Darwin triple");
695 // Default to darwin8, i.e., MacOSX 10.4.
698 // Darwin version numbers are skewed from OS X versions.
715 // Ignore the version from the triple. This is only handled because the
716 // the clang driver combines OS X and IOS support into a common Darwin
717 // toolchain that wants to know the OS X version number even when targeting
727 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
728 unsigned &Micro) const {
730 default: llvm_unreachable("unexpected OS for Darwin triple");
733 // Ignore the version from the triple. This is only handled because the
734 // the clang driver combines OS X and IOS support into a common Darwin
735 // toolchain that wants to know the iOS version number even when targeting
742 getOSVersion(Major, Minor, Micro);
743 // Default to 5.0 (or 7.0 for arm64).
745 Major = (getArch() == arm64) ? 7 : 5;
750 void Triple::setTriple(const Twine &Str) {
754 void Triple::setArch(ArchType Kind) {
755 setArchName(getArchTypeName(Kind));
758 void Triple::setVendor(VendorType Kind) {
759 setVendorName(getVendorTypeName(Kind));
762 void Triple::setOS(OSType Kind) {
763 setOSName(getOSTypeName(Kind));
766 void Triple::setEnvironment(EnvironmentType Kind) {
767 setEnvironmentName(getEnvironmentTypeName(Kind));
770 void Triple::setObjectFormat(ObjectFormatType Kind) {
771 if (Environment == UnknownEnvironment)
772 return setEnvironmentName(getObjectFormatTypeName(Kind));
774 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
775 getObjectFormatTypeName(Kind)).str());
778 void Triple::setArchName(StringRef Str) {
779 // Work around a miscompilation bug for Twines in gcc 4.0.3.
780 SmallString<64> Triple;
783 Triple += getVendorName();
785 Triple += getOSAndEnvironmentName();
786 setTriple(Triple.str());
789 void Triple::setVendorName(StringRef Str) {
790 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
793 void Triple::setOSName(StringRef Str) {
794 if (hasEnvironment())
795 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
796 "-" + getEnvironmentName());
798 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
801 void Triple::setEnvironmentName(StringRef Str) {
802 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
806 void Triple::setOSAndEnvironmentName(StringRef Str) {
807 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
810 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
812 case llvm::Triple::UnknownArch:
815 case llvm::Triple::msp430:
818 case llvm::Triple::amdil:
819 case llvm::Triple::arm:
820 case llvm::Triple::armeb:
821 case llvm::Triple::hexagon:
822 case llvm::Triple::le32:
823 case llvm::Triple::mips:
824 case llvm::Triple::mipsel:
825 case llvm::Triple::nvptx:
826 case llvm::Triple::ppc:
827 case llvm::Triple::r600:
828 case llvm::Triple::sparc:
829 case llvm::Triple::tce:
830 case llvm::Triple::thumb:
831 case llvm::Triple::thumbeb:
832 case llvm::Triple::x86:
833 case llvm::Triple::xcore:
834 case llvm::Triple::spir:
835 case llvm::Triple::kalimba:
838 case llvm::Triple::arm64:
839 case llvm::Triple::arm64_be:
840 case llvm::Triple::aarch64:
841 case llvm::Triple::aarch64_be:
842 case llvm::Triple::mips64:
843 case llvm::Triple::mips64el:
844 case llvm::Triple::nvptx64:
845 case llvm::Triple::ppc64:
846 case llvm::Triple::ppc64le:
847 case llvm::Triple::sparcv9:
848 case llvm::Triple::systemz:
849 case llvm::Triple::x86_64:
850 case llvm::Triple::spir64:
853 llvm_unreachable("Invalid architecture value");
856 bool Triple::isArch64Bit() const {
857 return getArchPointerBitWidth(getArch()) == 64;
860 bool Triple::isArch32Bit() const {
861 return getArchPointerBitWidth(getArch()) == 32;
864 bool Triple::isArch16Bit() const {
865 return getArchPointerBitWidth(getArch()) == 16;
868 Triple Triple::get32BitArchVariant() const {
871 case Triple::UnknownArch:
872 case Triple::aarch64:
873 case Triple::aarch64_be:
875 case Triple::arm64_be:
877 case Triple::systemz:
878 case Triple::ppc64le:
879 T.setArch(UnknownArch);
886 case Triple::hexagon:
887 case Triple::kalimba:
897 case Triple::thumbeb:
903 case Triple::mips64: T.setArch(Triple::mips); break;
904 case Triple::mips64el: T.setArch(Triple::mipsel); break;
905 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
906 case Triple::ppc64: T.setArch(Triple::ppc); break;
907 case Triple::sparcv9: T.setArch(Triple::sparc); break;
908 case Triple::x86_64: T.setArch(Triple::x86); break;
909 case Triple::spir64: T.setArch(Triple::spir); break;
914 Triple Triple::get64BitArchVariant() const {
917 case Triple::UnknownArch:
921 case Triple::hexagon:
922 case Triple::kalimba:
928 case Triple::thumbeb:
930 T.setArch(UnknownArch);
933 case Triple::aarch64:
934 case Triple::aarch64_be:
937 case Triple::mips64el:
938 case Triple::nvptx64:
940 case Triple::ppc64le:
941 case Triple::sparcv9:
942 case Triple::systemz:
945 case Triple::arm64_be:
949 case Triple::mips: T.setArch(Triple::mips64); break;
950 case Triple::mipsel: T.setArch(Triple::mips64el); break;
951 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
952 case Triple::ppc: T.setArch(Triple::ppc64); break;
953 case Triple::sparc: T.setArch(Triple::sparcv9); break;
954 case Triple::x86: T.setArch(Triple::x86_64); break;
955 case Triple::spir: T.setArch(Triple::spir64); break;
960 // FIXME: tblgen this.
961 const char *Triple::getARMCPUForArch(StringRef MArch) const {
963 MArch = getArchName();
966 case llvm::Triple::NetBSD:
967 if (MArch == "armv6")
968 return "arm1176jzf-s";
970 case llvm::Triple::Win32:
971 // FIXME: this is invalid for WindowsCE
977 const char *result = nullptr;
978 size_t offset = StringRef::npos;
979 if (MArch.startswith("arm"))
981 if (MArch.startswith("thumb"))
983 if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
985 if (offset != StringRef::npos)
986 result = llvm::StringSwitch<const char *>(MArch.substr(offset))
987 .Cases("v2", "v2a", "arm2")
989 .Case("v3m", "arm7m")
990 .Case("v4", "strongarm")
991 .Case("v4t", "arm7tdmi")
992 .Cases("v5", "v5t", "arm10tdmi")
993 .Cases("v5e", "v5te", "arm1022e")
994 .Case("v5tej", "arm926ej-s")
995 .Cases("v6", "v6k", "arm1136jf-s")
996 .Case("v6j", "arm1136j-s")
997 .Cases("v6z", "v6zk", "arm1176jzf-s")
998 .Case("v6t2", "arm1156t2-s")
999 .Cases("v6m", "v6-m", "cortex-m0")
1000 .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
1001 .Cases("v7s", "v7-s", "swift")
1002 .Cases("v7r", "v7-r", "cortex-r4")
1003 .Cases("v7m", "v7-m", "cortex-m3")
1004 .Cases("v7em", "v7e-m", "cortex-m4")
1005 .Cases("v8", "v8a", "v8-a", "cortex-a53")
1008 result = llvm::StringSwitch<const char *>(MArch)
1009 .Case("ep9312", "ep9312")
1010 .Case("iwmmxt", "iwmmxt")
1011 .Case("xscale", "xscale")
1017 // If all else failed, return the most base CPU with thumb interworking
1018 // supported by LLVM.
1019 // FIXME: Should warn once that we're falling back.
1021 case llvm::Triple::NetBSD:
1022 switch (getEnvironment()) {
1023 case llvm::Triple::GNUEABIHF:
1024 case llvm::Triple::GNUEABI:
1025 case llvm::Triple::EABIHF:
1026 case llvm::Triple::EABI:
1027 return "arm926ej-s";
1032 switch (getEnvironment()) {
1033 case llvm::Triple::EABIHF:
1034 case llvm::Triple::GNUEABIHF:
1035 return "arm1176jzf-s";