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 NVIDIA: return "nvidia";
121 case CSR: return "csr";
124 llvm_unreachable("Invalid VendorType!");
127 const char *Triple::getOSTypeName(OSType Kind) {
129 case UnknownOS: return "unknown";
131 case AuroraUX: return "auroraux";
132 case Cygwin: return "cygwin";
133 case Darwin: return "darwin";
134 case DragonFly: return "dragonfly";
135 case FreeBSD: return "freebsd";
136 case IOS: return "ios";
137 case KFreeBSD: return "kfreebsd";
138 case Linux: return "linux";
139 case Lv2: return "lv2";
140 case MacOSX: return "macosx";
141 case MinGW32: return "mingw32";
142 case NetBSD: return "netbsd";
143 case OpenBSD: return "openbsd";
144 case Solaris: return "solaris";
145 case Win32: return "windows";
146 case Haiku: return "haiku";
147 case Minix: return "minix";
148 case RTEMS: return "rtems";
149 case NaCl: return "nacl";
150 case CNK: return "cnk";
151 case Bitrig: return "bitrig";
152 case AIX: return "aix";
153 case CUDA: return "cuda";
154 case NVCL: return "nvcl";
157 llvm_unreachable("Invalid OSType");
160 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
162 case UnknownEnvironment: return "unknown";
163 case GNU: return "gnu";
164 case GNUEABIHF: return "gnueabihf";
165 case GNUEABI: return "gnueabi";
166 case GNUX32: return "gnux32";
167 case CODE16: return "code16";
168 case EABI: return "eabi";
169 case EABIHF: return "eabihf";
170 case Android: return "android";
171 case MSVC: return "msvc";
172 case Itanium: return "itanium";
173 case Cygnus: return "cygnus";
176 llvm_unreachable("Invalid EnvironmentType!");
179 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
180 return StringSwitch<Triple::ArchType>(Name)
181 .Case("aarch64", aarch64)
182 .Case("aarch64_be", aarch64_be)
184 .Case("armeb", armeb)
185 .Case("arm64", arm64)
186 .Case("arm64_be", arm64_be)
188 .Case("mipsel", mipsel)
189 .Case("mips64", mips64)
190 .Case("mips64el", mips64el)
191 .Case("msp430", msp430)
192 .Case("ppc64", ppc64)
195 .Case("ppc64le", ppc64le)
197 .Case("hexagon", hexagon)
198 .Case("sparc", sparc)
199 .Case("sparcv9", sparcv9)
200 .Case("systemz", systemz)
202 .Case("thumb", thumb)
203 .Case("thumbeb", thumbeb)
205 .Case("x86-64", x86_64)
206 .Case("xcore", xcore)
207 .Case("nvptx", nvptx)
208 .Case("nvptx64", nvptx64)
210 .Case("amdil", amdil)
212 .Case("spir64", spir64)
213 .Case("kalimba", kalimba)
214 .Default(UnknownArch);
217 // Returns architecture name that is understood by the target assembler.
218 const char *Triple::getArchNameForAssembler() {
219 if (!isOSDarwin() && getVendor() != Triple::Apple)
222 return StringSwitch<const char*>(getArchName())
223 .Case("i386", "i386")
224 .Case("x86_64", "x86_64")
225 .Case("powerpc", "ppc")
226 .Case("powerpc64", "ppc64")
227 .Case("powerpc64le", "ppc64le")
229 .Cases("armv4t", "thumbv4t", "armv4t")
230 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
231 .Cases("armv6", "thumbv6", "armv6")
232 .Cases("armv7", "thumbv7", "armv7")
233 .Case("armeb", "armeb")
234 .Case("arm64", "arm64")
235 .Case("arm64_be", "arm64")
236 .Case("r600", "r600")
237 .Case("nvptx", "nvptx")
238 .Case("nvptx64", "nvptx64")
239 .Case("le32", "le32")
240 .Case("amdil", "amdil")
241 .Case("spir", "spir")
242 .Case("spir64", "spir64")
246 static Triple::ArchType parseArch(StringRef ArchName) {
247 return StringSwitch<Triple::ArchType>(ArchName)
248 .Cases("i386", "i486", "i586", "i686", Triple::x86)
249 // FIXME: Do we need to support these?
250 .Cases("i786", "i886", "i986", Triple::x86)
251 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
252 .Case("powerpc", Triple::ppc)
253 .Cases("powerpc64", "ppu", Triple::ppc64)
254 .Case("powerpc64le", Triple::ppc64le)
255 .Case("aarch64", Triple::aarch64)
256 .Case("aarch64_be", Triple::aarch64_be)
257 .Cases("arm", "xscale", Triple::arm)
258 // FIXME: It would be good to replace these with explicit names for all the
259 // various suffixes supported.
260 .StartsWith("armv", Triple::arm)
261 .Case("armeb", Triple::armeb)
262 .StartsWith("armebv", Triple::armeb)
263 .Case("thumb", Triple::thumb)
264 .StartsWith("thumbv", Triple::thumb)
265 .Case("thumbeb", Triple::thumbeb)
266 .StartsWith("thumbebv", Triple::thumbeb)
267 .Case("arm64", Triple::arm64)
268 .Case("arm64_be", Triple::arm64_be)
269 .Case("msp430", Triple::msp430)
270 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
271 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
272 .Cases("mips64", "mips64eb", Triple::mips64)
273 .Case("mips64el", Triple::mips64el)
274 .Case("r600", Triple::r600)
275 .Case("hexagon", Triple::hexagon)
276 .Case("s390x", Triple::systemz)
277 .Case("sparc", Triple::sparc)
278 .Cases("sparcv9", "sparc64", Triple::sparcv9)
279 .Case("tce", Triple::tce)
280 .Case("xcore", Triple::xcore)
281 .Case("nvptx", Triple::nvptx)
282 .Case("nvptx64", Triple::nvptx64)
283 .Case("le32", Triple::le32)
284 .Case("amdil", Triple::amdil)
285 .Case("spir", Triple::spir)
286 .Case("spir64", Triple::spir64)
287 .Case("kalimba", Triple::kalimba)
288 .Default(Triple::UnknownArch);
291 static Triple::VendorType parseVendor(StringRef VendorName) {
292 return StringSwitch<Triple::VendorType>(VendorName)
293 .Case("apple", Triple::Apple)
294 .Case("pc", Triple::PC)
295 .Case("scei", Triple::SCEI)
296 .Case("bgp", Triple::BGP)
297 .Case("bgq", Triple::BGQ)
298 .Case("fsl", Triple::Freescale)
299 .Case("ibm", Triple::IBM)
300 .Case("img", Triple::ImaginationTechnologies)
301 .Case("nvidia", Triple::NVIDIA)
302 .Case("csr", Triple::CSR)
303 .Default(Triple::UnknownVendor);
306 static Triple::OSType parseOS(StringRef OSName) {
307 return StringSwitch<Triple::OSType>(OSName)
308 .StartsWith("auroraux", Triple::AuroraUX)
309 .StartsWith("cygwin", Triple::Cygwin)
310 .StartsWith("darwin", Triple::Darwin)
311 .StartsWith("dragonfly", Triple::DragonFly)
312 .StartsWith("freebsd", Triple::FreeBSD)
313 .StartsWith("ios", Triple::IOS)
314 .StartsWith("kfreebsd", Triple::KFreeBSD)
315 .StartsWith("linux", Triple::Linux)
316 .StartsWith("lv2", Triple::Lv2)
317 .StartsWith("macosx", Triple::MacOSX)
318 .StartsWith("mingw32", Triple::MinGW32)
319 .StartsWith("netbsd", Triple::NetBSD)
320 .StartsWith("openbsd", Triple::OpenBSD)
321 .StartsWith("solaris", Triple::Solaris)
322 .StartsWith("win32", Triple::Win32)
323 .StartsWith("windows", Triple::Win32)
324 .StartsWith("haiku", Triple::Haiku)
325 .StartsWith("minix", Triple::Minix)
326 .StartsWith("rtems", Triple::RTEMS)
327 .StartsWith("nacl", Triple::NaCl)
328 .StartsWith("cnk", Triple::CNK)
329 .StartsWith("bitrig", Triple::Bitrig)
330 .StartsWith("aix", Triple::AIX)
331 .StartsWith("cuda", Triple::CUDA)
332 .StartsWith("nvcl", Triple::NVCL)
333 .Default(Triple::UnknownOS);
336 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
337 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
338 .StartsWith("eabihf", Triple::EABIHF)
339 .StartsWith("eabi", Triple::EABI)
340 .StartsWith("gnueabihf", Triple::GNUEABIHF)
341 .StartsWith("gnueabi", Triple::GNUEABI)
342 .StartsWith("gnux32", Triple::GNUX32)
343 .StartsWith("code16", Triple::CODE16)
344 .StartsWith("gnu", Triple::GNU)
345 .StartsWith("android", Triple::Android)
346 .StartsWith("msvc", Triple::MSVC)
347 .StartsWith("itanium", Triple::Itanium)
348 .StartsWith("cygnus", Triple::Cygnus)
349 .Default(Triple::UnknownEnvironment);
352 static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
353 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
354 .EndsWith("coff", Triple::COFF)
355 .EndsWith("elf", Triple::ELF)
356 .EndsWith("macho", Triple::MachO)
357 .Default(Triple::UnknownObjectFormat);
360 static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
362 case Triple::UnknownObjectFormat: return "";
363 case Triple::COFF: return "coff";
364 case Triple::ELF: return "elf";
365 case Triple::MachO: return "macho";
367 llvm_unreachable("unknown object format type");
370 static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
372 return Triple::MachO;
373 else if (T.isOSWindows())
378 /// \brief Construct a triple from the string representation provided.
380 /// This stores the string representation and parses the various pieces into
382 Triple::Triple(const Twine &Str)
384 Arch(parseArch(getArchName())),
385 Vendor(parseVendor(getVendorName())),
386 OS(parseOS(getOSName())),
387 Environment(parseEnvironment(getEnvironmentName())),
388 ObjectFormat(parseFormat(getEnvironmentName())) {
389 if (ObjectFormat == Triple::UnknownObjectFormat)
390 ObjectFormat = getDefaultFormat(*this);
393 /// \brief Construct a triple from string representations of the architecture,
396 /// This joins each argument into a canonical string representation and parses
397 /// them into enum members. It leaves the environment unknown and omits it from
398 /// the string representation.
399 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
400 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
401 Arch(parseArch(ArchStr.str())),
402 Vendor(parseVendor(VendorStr.str())),
403 OS(parseOS(OSStr.str())),
404 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
405 ObjectFormat = getDefaultFormat(*this);
408 /// \brief Construct a triple from string representations of the architecture,
409 /// vendor, OS, and environment.
411 /// This joins each argument into a canonical string representation and parses
412 /// them into enum members.
413 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
414 const Twine &EnvironmentStr)
415 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
416 EnvironmentStr).str()),
417 Arch(parseArch(ArchStr.str())),
418 Vendor(parseVendor(VendorStr.str())),
419 OS(parseOS(OSStr.str())),
420 Environment(parseEnvironment(EnvironmentStr.str())),
421 ObjectFormat(parseFormat(EnvironmentStr.str())) {
422 if (ObjectFormat == Triple::UnknownObjectFormat)
423 ObjectFormat = getDefaultFormat(*this);
426 std::string Triple::normalize(StringRef Str) {
427 // Parse into components.
428 SmallVector<StringRef, 4> Components;
429 Str.split(Components, "-");
431 // If the first component corresponds to a known architecture, preferentially
432 // use it for the architecture. If the second component corresponds to a
433 // known vendor, preferentially use it for the vendor, etc. This avoids silly
434 // component movement when a component parses as (eg) both a valid arch and a
436 ArchType Arch = UnknownArch;
437 if (Components.size() > 0)
438 Arch = parseArch(Components[0]);
439 VendorType Vendor = UnknownVendor;
440 if (Components.size() > 1)
441 Vendor = parseVendor(Components[1]);
442 OSType OS = UnknownOS;
443 if (Components.size() > 2)
444 OS = parseOS(Components[2]);
445 EnvironmentType Environment = UnknownEnvironment;
446 if (Components.size() > 3)
447 Environment = parseEnvironment(Components[3]);
448 ObjectFormatType ObjectFormat = UnknownObjectFormat;
449 if (Components.size() > 4)
450 ObjectFormat = parseFormat(Components[4]);
452 // Note which components are already in their final position. These will not
455 Found[0] = Arch != UnknownArch;
456 Found[1] = Vendor != UnknownVendor;
457 Found[2] = OS != UnknownOS;
458 Found[3] = Environment != UnknownEnvironment;
460 // If they are not there already, permute the components into their canonical
461 // positions by seeing if they parse as a valid architecture, and if so moving
462 // the component to the architecture position etc.
463 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
465 continue; // Already in the canonical position.
467 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
468 // Do not reparse any components that already matched.
469 if (Idx < array_lengthof(Found) && Found[Idx])
472 // Does this component parse as valid for the target position?
474 StringRef Comp = Components[Idx];
476 default: llvm_unreachable("unexpected component type!");
478 Arch = parseArch(Comp);
479 Valid = Arch != UnknownArch;
482 Vendor = parseVendor(Comp);
483 Valid = Vendor != UnknownVendor;
487 Valid = OS != UnknownOS;
490 Environment = parseEnvironment(Comp);
491 Valid = Environment != UnknownEnvironment;
493 ObjectFormat = parseFormat(Comp);
494 Valid = ObjectFormat != UnknownObjectFormat;
499 continue; // Nope, try the next component.
501 // Move the component to the target position, pushing any non-fixed
502 // components that are in the way to the right. This tends to give
503 // good results in the common cases of a forgotten vendor component
504 // or a wrongly positioned environment.
506 // Insert left, pushing the existing components to the right. For
507 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
508 StringRef CurrentComponent(""); // The empty component.
509 // Replace the component we are moving with an empty component.
510 std::swap(CurrentComponent, Components[Idx]);
511 // Insert the component being moved at Pos, displacing any existing
512 // components to the right.
513 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
514 // Skip over any fixed components.
515 while (i < array_lengthof(Found) && Found[i])
517 // Place the component at the new position, getting the component
518 // that was at this position - it will be moved right.
519 std::swap(CurrentComponent, Components[i]);
521 } else if (Pos > Idx) {
522 // Push right by inserting empty components until the component at Idx
523 // reaches the target position Pos. For example, pc-a -> -pc-a when
524 // moving pc to the second position.
526 // Insert one empty component at Idx.
527 StringRef CurrentComponent(""); // The empty component.
528 for (unsigned i = Idx; i < Components.size();) {
529 // Place the component at the new position, getting the component
530 // that was at this position - it will be moved right.
531 std::swap(CurrentComponent, Components[i]);
532 // If it was placed on top of an empty component then we are done.
533 if (CurrentComponent.empty())
535 // Advance to the next component, skipping any fixed components.
536 while (++i < array_lengthof(Found) && Found[i])
539 // The last component was pushed off the end - append it.
540 if (!CurrentComponent.empty())
541 Components.push_back(CurrentComponent);
543 // Advance Idx to the component's new position.
544 while (++Idx < array_lengthof(Found) && Found[Idx])
546 } while (Idx < Pos); // Add more until the final position is reached.
548 assert(Pos < Components.size() && Components[Pos] == Comp &&
549 "Component moved wrong!");
555 // Special case logic goes here. At this point Arch, Vendor and OS have the
556 // correct values for the computed components.
558 if (OS == Triple::Win32) {
559 Components.resize(4);
560 Components[2] = "windows";
561 if (Environment == UnknownEnvironment) {
562 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
563 Components[3] = "msvc";
565 Components[3] = getObjectFormatTypeName(ObjectFormat);
567 } else if (OS == Triple::MinGW32) {
568 Components.resize(4);
569 Components[2] = "windows";
570 Components[3] = "gnu";
571 } else if (OS == Triple::Cygwin) {
572 Components.resize(4);
573 Components[2] = "windows";
574 Components[3] = "cygnus";
576 if (OS == Triple::MinGW32 || OS == Triple::Cygwin ||
577 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
578 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
579 Components.resize(5);
580 Components[4] = getObjectFormatTypeName(ObjectFormat);
584 // Stick the corrected components back together to form the normalized string.
585 std::string Normalized;
586 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
587 if (i) Normalized += '-';
588 Normalized += Components[i];
593 StringRef Triple::getArchName() const {
594 return StringRef(Data).split('-').first; // Isolate first component
597 StringRef Triple::getVendorName() const {
598 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
599 return Tmp.split('-').first; // Isolate second component
602 StringRef Triple::getOSName() const {
603 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
604 Tmp = Tmp.split('-').second; // Strip second component
605 return Tmp.split('-').first; // Isolate third component
608 StringRef Triple::getEnvironmentName() const {
609 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
610 Tmp = Tmp.split('-').second; // Strip second component
611 return Tmp.split('-').second; // Strip third component
614 StringRef Triple::getOSAndEnvironmentName() const {
615 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
616 return Tmp.split('-').second; // Strip second component
619 static unsigned EatNumber(StringRef &Str) {
620 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
624 // Consume the leading digit.
625 Result = Result*10 + (Str[0] - '0');
629 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
634 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
635 unsigned &Micro) const {
636 StringRef OSName = getOSName();
638 // Assume that the OS portion of the triple starts with the canonical name.
639 StringRef OSTypeName = getOSTypeName(getOS());
640 if (OSName.startswith(OSTypeName))
641 OSName = OSName.substr(OSTypeName.size());
643 // Any unset version defaults to 0.
644 Major = Minor = Micro = 0;
646 // Parse up to three components.
647 unsigned *Components[3] = { &Major, &Minor, &Micro };
648 for (unsigned i = 0; i != 3; ++i) {
649 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
652 // Consume the leading number.
653 *Components[i] = EatNumber(OSName);
655 // Consume the separator, if present.
656 if (OSName.startswith("."))
657 OSName = OSName.substr(1);
661 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
662 unsigned &Micro) const {
663 getOSVersion(Major, Minor, Micro);
666 default: llvm_unreachable("unexpected OS for Darwin triple");
668 // Default to darwin8, i.e., MacOSX 10.4.
671 // Darwin version numbers are skewed from OS X versions.
688 // Ignore the version from the triple. This is only handled because the
689 // the clang driver combines OS X and IOS support into a common Darwin
690 // toolchain that wants to know the OS X version number even when targeting
700 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
701 unsigned &Micro) const {
703 default: llvm_unreachable("unexpected OS for Darwin triple");
706 // Ignore the version from the triple. This is only handled because the
707 // the clang driver combines OS X and IOS support into a common Darwin
708 // toolchain that wants to know the iOS version number even when targeting
715 getOSVersion(Major, Minor, Micro);
716 // Default to 5.0 (or 7.0 for arm64).
718 Major = (getArch() == arm64) ? 7 : 5;
723 void Triple::setTriple(const Twine &Str) {
727 void Triple::setArch(ArchType Kind) {
728 setArchName(getArchTypeName(Kind));
731 void Triple::setVendor(VendorType Kind) {
732 setVendorName(getVendorTypeName(Kind));
735 void Triple::setOS(OSType Kind) {
736 setOSName(getOSTypeName(Kind));
739 void Triple::setEnvironment(EnvironmentType Kind) {
740 setEnvironmentName(getEnvironmentTypeName(Kind));
743 void Triple::setObjectFormat(ObjectFormatType Kind) {
744 if (Environment == UnknownEnvironment)
745 return setEnvironmentName(getObjectFormatTypeName(Kind));
747 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
748 getObjectFormatTypeName(Kind)).str());
751 void Triple::setArchName(StringRef Str) {
752 // Work around a miscompilation bug for Twines in gcc 4.0.3.
753 SmallString<64> Triple;
756 Triple += getVendorName();
758 Triple += getOSAndEnvironmentName();
759 setTriple(Triple.str());
762 void Triple::setVendorName(StringRef Str) {
763 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
766 void Triple::setOSName(StringRef Str) {
767 if (hasEnvironment())
768 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
769 "-" + getEnvironmentName());
771 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
774 void Triple::setEnvironmentName(StringRef Str) {
775 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
779 void Triple::setOSAndEnvironmentName(StringRef Str) {
780 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
783 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
785 case llvm::Triple::UnknownArch:
788 case llvm::Triple::msp430:
791 case llvm::Triple::amdil:
792 case llvm::Triple::arm:
793 case llvm::Triple::armeb:
794 case llvm::Triple::hexagon:
795 case llvm::Triple::le32:
796 case llvm::Triple::mips:
797 case llvm::Triple::mipsel:
798 case llvm::Triple::nvptx:
799 case llvm::Triple::ppc:
800 case llvm::Triple::r600:
801 case llvm::Triple::sparc:
802 case llvm::Triple::tce:
803 case llvm::Triple::thumb:
804 case llvm::Triple::thumbeb:
805 case llvm::Triple::x86:
806 case llvm::Triple::xcore:
807 case llvm::Triple::spir:
808 case llvm::Triple::kalimba:
811 case llvm::Triple::arm64:
812 case llvm::Triple::arm64_be:
813 case llvm::Triple::aarch64:
814 case llvm::Triple::aarch64_be:
815 case llvm::Triple::mips64:
816 case llvm::Triple::mips64el:
817 case llvm::Triple::nvptx64:
818 case llvm::Triple::ppc64:
819 case llvm::Triple::ppc64le:
820 case llvm::Triple::sparcv9:
821 case llvm::Triple::systemz:
822 case llvm::Triple::x86_64:
823 case llvm::Triple::spir64:
826 llvm_unreachable("Invalid architecture value");
829 bool Triple::isArch64Bit() const {
830 return getArchPointerBitWidth(getArch()) == 64;
833 bool Triple::isArch32Bit() const {
834 return getArchPointerBitWidth(getArch()) == 32;
837 bool Triple::isArch16Bit() const {
838 return getArchPointerBitWidth(getArch()) == 16;
841 Triple Triple::get32BitArchVariant() const {
844 case Triple::UnknownArch:
845 case Triple::aarch64:
846 case Triple::aarch64_be:
848 case Triple::arm64_be:
850 case Triple::systemz:
851 case Triple::ppc64le:
852 T.setArch(UnknownArch);
859 case Triple::hexagon:
860 case Triple::kalimba:
870 case Triple::thumbeb:
876 case Triple::mips64: T.setArch(Triple::mips); break;
877 case Triple::mips64el: T.setArch(Triple::mipsel); break;
878 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
879 case Triple::ppc64: T.setArch(Triple::ppc); break;
880 case Triple::sparcv9: T.setArch(Triple::sparc); break;
881 case Triple::x86_64: T.setArch(Triple::x86); break;
882 case Triple::spir64: T.setArch(Triple::spir); break;
887 Triple Triple::get64BitArchVariant() const {
890 case Triple::UnknownArch:
894 case Triple::hexagon:
895 case Triple::kalimba:
901 case Triple::thumbeb:
903 T.setArch(UnknownArch);
906 case Triple::aarch64:
907 case Triple::aarch64_be:
910 case Triple::mips64el:
911 case Triple::nvptx64:
913 case Triple::ppc64le:
914 case Triple::sparcv9:
915 case Triple::systemz:
918 case Triple::arm64_be:
922 case Triple::mips: T.setArch(Triple::mips64); break;
923 case Triple::mipsel: T.setArch(Triple::mips64el); break;
924 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
925 case Triple::ppc: T.setArch(Triple::ppc64); break;
926 case Triple::sparc: T.setArch(Triple::sparcv9); break;
927 case Triple::x86: T.setArch(Triple::x86_64); break;
928 case Triple::spir: T.setArch(Triple::spir64); break;
933 // FIXME: tblgen this.
934 const char *Triple::getARMCPUForArch(StringRef MArch) const {
936 MArch = getArchName();
939 case llvm::Triple::NetBSD:
940 if (MArch == "armv6")
941 return "arm1176jzf-s";
943 case llvm::Triple::Win32:
944 // FIXME: this is invalid for WindowsCE
950 const char *result = nullptr;
951 size_t offset = StringRef::npos;
952 if (MArch.startswith("arm"))
954 if (MArch.startswith("thumb"))
956 if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
958 if (offset != StringRef::npos)
959 result = llvm::StringSwitch<const char *>(MArch.substr(offset))
960 .Cases("v2", "v2a", "arm2")
962 .Case("v3m", "arm7m")
963 .Case("v4", "strongarm")
964 .Case("v4t", "arm7tdmi")
965 .Cases("v5", "v5t", "arm10tdmi")
966 .Cases("v5e", "v5te", "arm1022e")
967 .Case("v5tej", "arm926ej-s")
968 .Cases("v6", "v6k", "arm1136jf-s")
969 .Case("v6j", "arm1136j-s")
970 .Cases("v6z", "v6zk", "arm1176jzf-s")
971 .Case("v6t2", "arm1156t2-s")
972 .Cases("v6m", "v6-m", "cortex-m0")
973 .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
974 .Cases("v7s", "v7-s", "swift")
975 .Cases("v7r", "v7-r", "cortex-r4")
976 .Cases("v7m", "v7-m", "cortex-m3")
977 .Cases("v7em", "v7e-m", "cortex-m4")
978 .Cases("v8", "v8a", "v8-a", "cortex-a53")
981 result = llvm::StringSwitch<const char *>(MArch)
982 .Case("ep9312", "ep9312")
983 .Case("iwmmxt", "iwmmxt")
984 .Case("xscale", "xscale")
990 // If all else failed, return the most base CPU with thumb interworking
991 // supported by LLVM.
992 // FIXME: Should warn once that we're falling back.
994 case llvm::Triple::NetBSD:
995 switch (getEnvironment()) {
996 case llvm::Triple::GNUEABIHF:
997 case llvm::Triple::GNUEABI:
998 case llvm::Triple::EABIHF:
999 case llvm::Triple::EABI:
1000 return "arm926ej-s";
1005 switch (getEnvironment()) {
1006 case llvm::Triple::EABIHF:
1007 case llvm::Triple::GNUEABIHF:
1008 return "arm1176jzf-s";