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 AuroraUX: return "auroraux";
129 case Cygwin: return "cygwin";
130 case Darwin: return "darwin";
131 case DragonFly: return "dragonfly";
132 case FreeBSD: return "freebsd";
133 case IOS: return "ios";
134 case KFreeBSD: return "kfreebsd";
135 case Linux: return "linux";
136 case Lv2: return "lv2";
137 case MacOSX: return "macosx";
138 case MinGW32: return "mingw32";
139 case NetBSD: return "netbsd";
140 case OpenBSD: return "openbsd";
141 case Solaris: return "solaris";
142 case Win32: return "windows";
143 case Haiku: return "haiku";
144 case Minix: return "minix";
145 case RTEMS: return "rtems";
146 case NaCl: return "nacl";
147 case CNK: return "cnk";
148 case Bitrig: return "bitrig";
149 case AIX: return "aix";
150 case CUDA: return "cuda";
151 case NVCL: return "nvcl";
154 llvm_unreachable("Invalid OSType");
157 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
159 case UnknownEnvironment: return "unknown";
160 case GNU: return "gnu";
161 case GNUEABIHF: return "gnueabihf";
162 case GNUEABI: return "gnueabi";
163 case GNUX32: return "gnux32";
164 case CODE16: return "code16";
165 case EABI: return "eabi";
166 case EABIHF: return "eabihf";
167 case Android: return "android";
168 case MSVC: return "msvc";
169 case Itanium: return "itanium";
170 case Cygnus: return "cygnus";
173 llvm_unreachable("Invalid EnvironmentType!");
176 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
177 return StringSwitch<Triple::ArchType>(Name)
178 .Case("aarch64", aarch64)
179 .Case("aarch64_be", aarch64_be)
180 .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
182 .Case("armeb", armeb)
184 .Case("mipsel", mipsel)
185 .Case("mips64", mips64)
186 .Case("mips64el", mips64el)
187 .Case("msp430", msp430)
188 .Case("ppc64", ppc64)
191 .Case("ppc64le", ppc64le)
193 .Case("hexagon", hexagon)
194 .Case("sparc", sparc)
195 .Case("sparcv9", sparcv9)
196 .Case("systemz", systemz)
198 .Case("thumb", thumb)
199 .Case("thumbeb", thumbeb)
201 .Case("x86-64", x86_64)
202 .Case("xcore", xcore)
203 .Case("nvptx", nvptx)
204 .Case("nvptx64", nvptx64)
206 .Case("amdil", amdil)
208 .Case("spir64", spir64)
209 .Case("kalimba", kalimba)
210 .Default(UnknownArch);
213 static Triple::ArchType parseArch(StringRef ArchName) {
214 return StringSwitch<Triple::ArchType>(ArchName)
215 .Cases("i386", "i486", "i586", "i686", Triple::x86)
216 // FIXME: Do we need to support these?
217 .Cases("i786", "i886", "i986", Triple::x86)
218 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
219 .Case("powerpc", Triple::ppc)
220 .Cases("powerpc64", "ppu", Triple::ppc64)
221 .Case("powerpc64le", Triple::ppc64le)
222 .Case("aarch64", Triple::aarch64)
223 .Case("aarch64_be", Triple::aarch64_be)
224 .Case("arm64", Triple::aarch64)
225 .Cases("arm", "xscale", Triple::arm)
226 // FIXME: It would be good to replace these with explicit names for all the
227 // various suffixes supported.
228 .StartsWith("armv", Triple::arm)
229 .Case("armeb", Triple::armeb)
230 .StartsWith("armebv", Triple::armeb)
231 .Case("thumb", Triple::thumb)
232 .StartsWith("thumbv", Triple::thumb)
233 .Case("thumbeb", Triple::thumbeb)
234 .StartsWith("thumbebv", Triple::thumbeb)
235 .Case("msp430", Triple::msp430)
236 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
237 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
238 .Cases("mips64", "mips64eb", Triple::mips64)
239 .Case("mips64el", Triple::mips64el)
240 .Case("r600", Triple::r600)
241 .Case("hexagon", Triple::hexagon)
242 .Case("s390x", Triple::systemz)
243 .Case("sparc", Triple::sparc)
244 .Cases("sparcv9", "sparc64", Triple::sparcv9)
245 .Case("tce", Triple::tce)
246 .Case("xcore", Triple::xcore)
247 .Case("nvptx", Triple::nvptx)
248 .Case("nvptx64", Triple::nvptx64)
249 .Case("le32", Triple::le32)
250 .Case("amdil", Triple::amdil)
251 .Case("spir", Triple::spir)
252 .Case("spir64", Triple::spir64)
253 .Case("kalimba", Triple::kalimba)
254 .Default(Triple::UnknownArch);
257 static Triple::VendorType parseVendor(StringRef VendorName) {
258 return StringSwitch<Triple::VendorType>(VendorName)
259 .Case("apple", Triple::Apple)
260 .Case("pc", Triple::PC)
261 .Case("scei", Triple::SCEI)
262 .Case("bgp", Triple::BGP)
263 .Case("bgq", Triple::BGQ)
264 .Case("fsl", Triple::Freescale)
265 .Case("ibm", Triple::IBM)
266 .Case("img", Triple::ImaginationTechnologies)
267 .Case("mti", Triple::MipsTechnologies)
268 .Case("nvidia", Triple::NVIDIA)
269 .Case("csr", Triple::CSR)
270 .Default(Triple::UnknownVendor);
273 static Triple::OSType parseOS(StringRef OSName) {
274 return StringSwitch<Triple::OSType>(OSName)
275 .StartsWith("auroraux", Triple::AuroraUX)
276 .StartsWith("cygwin", Triple::Cygwin)
277 .StartsWith("darwin", Triple::Darwin)
278 .StartsWith("dragonfly", Triple::DragonFly)
279 .StartsWith("freebsd", Triple::FreeBSD)
280 .StartsWith("ios", Triple::IOS)
281 .StartsWith("kfreebsd", Triple::KFreeBSD)
282 .StartsWith("linux", Triple::Linux)
283 .StartsWith("lv2", Triple::Lv2)
284 .StartsWith("macosx", Triple::MacOSX)
285 .StartsWith("mingw32", Triple::MinGW32)
286 .StartsWith("netbsd", Triple::NetBSD)
287 .StartsWith("openbsd", Triple::OpenBSD)
288 .StartsWith("solaris", Triple::Solaris)
289 .StartsWith("win32", Triple::Win32)
290 .StartsWith("windows", Triple::Win32)
291 .StartsWith("haiku", Triple::Haiku)
292 .StartsWith("minix", Triple::Minix)
293 .StartsWith("rtems", Triple::RTEMS)
294 .StartsWith("nacl", Triple::NaCl)
295 .StartsWith("cnk", Triple::CNK)
296 .StartsWith("bitrig", Triple::Bitrig)
297 .StartsWith("aix", Triple::AIX)
298 .StartsWith("cuda", Triple::CUDA)
299 .StartsWith("nvcl", Triple::NVCL)
300 .Default(Triple::UnknownOS);
303 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
304 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
305 .StartsWith("eabihf", Triple::EABIHF)
306 .StartsWith("eabi", Triple::EABI)
307 .StartsWith("gnueabihf", Triple::GNUEABIHF)
308 .StartsWith("gnueabi", Triple::GNUEABI)
309 .StartsWith("gnux32", Triple::GNUX32)
310 .StartsWith("code16", Triple::CODE16)
311 .StartsWith("gnu", Triple::GNU)
312 .StartsWith("android", Triple::Android)
313 .StartsWith("msvc", Triple::MSVC)
314 .StartsWith("itanium", Triple::Itanium)
315 .StartsWith("cygnus", Triple::Cygnus)
316 .Default(Triple::UnknownEnvironment);
319 static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
320 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
321 .EndsWith("coff", Triple::COFF)
322 .EndsWith("elf", Triple::ELF)
323 .EndsWith("macho", Triple::MachO)
324 .Default(Triple::UnknownObjectFormat);
327 static Triple::SubArchType parseSubArch(StringRef SubArchName) {
328 return StringSwitch<Triple::SubArchType>(SubArchName)
329 .EndsWith("v8", Triple::ARMSubArch_v8)
330 .EndsWith("v8a", Triple::ARMSubArch_v8)
331 .EndsWith("v7", Triple::ARMSubArch_v7)
332 .EndsWith("v7a", Triple::ARMSubArch_v7)
333 .EndsWith("v7em", Triple::ARMSubArch_v7em)
334 .EndsWith("v7l", Triple::ARMSubArch_v7)
335 .EndsWith("v7m", Triple::ARMSubArch_v7m)
336 .EndsWith("v7r", Triple::ARMSubArch_v7)
337 .EndsWith("v7s", Triple::ARMSubArch_v7s)
338 .EndsWith("v6", Triple::ARMSubArch_v6)
339 .EndsWith("v6m", Triple::ARMSubArch_v6m)
340 .EndsWith("v6t2", Triple::ARMSubArch_v6t2)
341 .EndsWith("v5", Triple::ARMSubArch_v5)
342 .EndsWith("v5e", Triple::ARMSubArch_v5)
343 .EndsWith("v5t", Triple::ARMSubArch_v5)
344 .EndsWith("v5te", Triple::ARMSubArch_v5te)
345 .EndsWith("v4t", Triple::ARMSubArch_v4t)
346 .Default(Triple::NoSubArch);
349 static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
351 case Triple::UnknownObjectFormat: return "";
352 case Triple::COFF: return "coff";
353 case Triple::ELF: return "elf";
354 case Triple::MachO: return "macho";
356 llvm_unreachable("unknown object format type");
359 static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
361 return Triple::MachO;
362 else if (T.isOSWindows())
367 /// \brief Construct a triple from the string representation provided.
369 /// This stores the string representation and parses the various pieces into
371 Triple::Triple(const Twine &Str)
373 Arch(parseArch(getArchName())),
374 SubArch(parseSubArch(getArchName())),
375 Vendor(parseVendor(getVendorName())),
376 OS(parseOS(getOSName())),
377 Environment(parseEnvironment(getEnvironmentName())),
378 ObjectFormat(parseFormat(getEnvironmentName())) {
379 if (ObjectFormat == Triple::UnknownObjectFormat)
380 ObjectFormat = getDefaultFormat(*this);
383 /// \brief Construct a triple from string representations of the architecture,
386 /// This joins each argument into a canonical string representation and parses
387 /// them into enum members. It leaves the environment unknown and omits it from
388 /// the string representation.
389 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
390 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
391 Arch(parseArch(ArchStr.str())),
392 SubArch(parseSubArch(ArchStr.str())),
393 Vendor(parseVendor(VendorStr.str())),
394 OS(parseOS(OSStr.str())),
395 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
396 ObjectFormat = getDefaultFormat(*this);
399 /// \brief Construct a triple from string representations of the architecture,
400 /// vendor, OS, and environment.
402 /// This joins each argument into a canonical string representation and parses
403 /// them into enum members.
404 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
405 const Twine &EnvironmentStr)
406 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
407 EnvironmentStr).str()),
408 Arch(parseArch(ArchStr.str())),
409 SubArch(parseSubArch(ArchStr.str())),
410 Vendor(parseVendor(VendorStr.str())),
411 OS(parseOS(OSStr.str())),
412 Environment(parseEnvironment(EnvironmentStr.str())),
413 ObjectFormat(parseFormat(EnvironmentStr.str())) {
414 if (ObjectFormat == Triple::UnknownObjectFormat)
415 ObjectFormat = getDefaultFormat(*this);
418 std::string Triple::normalize(StringRef Str) {
419 // Parse into components.
420 SmallVector<StringRef, 4> Components;
421 Str.split(Components, "-");
423 // If the first component corresponds to a known architecture, preferentially
424 // use it for the architecture. If the second component corresponds to a
425 // known vendor, preferentially use it for the vendor, etc. This avoids silly
426 // component movement when a component parses as (eg) both a valid arch and a
428 ArchType Arch = UnknownArch;
429 if (Components.size() > 0)
430 Arch = parseArch(Components[0]);
431 VendorType Vendor = UnknownVendor;
432 if (Components.size() > 1)
433 Vendor = parseVendor(Components[1]);
434 OSType OS = UnknownOS;
435 if (Components.size() > 2)
436 OS = parseOS(Components[2]);
437 EnvironmentType Environment = UnknownEnvironment;
438 if (Components.size() > 3)
439 Environment = parseEnvironment(Components[3]);
440 ObjectFormatType ObjectFormat = UnknownObjectFormat;
441 if (Components.size() > 4)
442 ObjectFormat = parseFormat(Components[4]);
444 // Note which components are already in their final position. These will not
447 Found[0] = Arch != UnknownArch;
448 Found[1] = Vendor != UnknownVendor;
449 Found[2] = OS != UnknownOS;
450 Found[3] = Environment != UnknownEnvironment;
452 // If they are not there already, permute the components into their canonical
453 // positions by seeing if they parse as a valid architecture, and if so moving
454 // the component to the architecture position etc.
455 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
457 continue; // Already in the canonical position.
459 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
460 // Do not reparse any components that already matched.
461 if (Idx < array_lengthof(Found) && Found[Idx])
464 // Does this component parse as valid for the target position?
466 StringRef Comp = Components[Idx];
468 default: llvm_unreachable("unexpected component type!");
470 Arch = parseArch(Comp);
471 Valid = Arch != UnknownArch;
474 Vendor = parseVendor(Comp);
475 Valid = Vendor != UnknownVendor;
479 Valid = OS != UnknownOS;
482 Environment = parseEnvironment(Comp);
483 Valid = Environment != UnknownEnvironment;
485 ObjectFormat = parseFormat(Comp);
486 Valid = ObjectFormat != UnknownObjectFormat;
491 continue; // Nope, try the next component.
493 // Move the component to the target position, pushing any non-fixed
494 // components that are in the way to the right. This tends to give
495 // good results in the common cases of a forgotten vendor component
496 // or a wrongly positioned environment.
498 // Insert left, pushing the existing components to the right. For
499 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
500 StringRef CurrentComponent(""); // The empty component.
501 // Replace the component we are moving with an empty component.
502 std::swap(CurrentComponent, Components[Idx]);
503 // Insert the component being moved at Pos, displacing any existing
504 // components to the right.
505 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
506 // Skip over any fixed components.
507 while (i < array_lengthof(Found) && Found[i])
509 // Place the component at the new position, getting the component
510 // that was at this position - it will be moved right.
511 std::swap(CurrentComponent, Components[i]);
513 } else if (Pos > Idx) {
514 // Push right by inserting empty components until the component at Idx
515 // reaches the target position Pos. For example, pc-a -> -pc-a when
516 // moving pc to the second position.
518 // Insert one empty component at Idx.
519 StringRef CurrentComponent(""); // The empty component.
520 for (unsigned i = Idx; i < Components.size();) {
521 // Place the component at the new position, getting the component
522 // that was at this position - it will be moved right.
523 std::swap(CurrentComponent, Components[i]);
524 // If it was placed on top of an empty component then we are done.
525 if (CurrentComponent.empty())
527 // Advance to the next component, skipping any fixed components.
528 while (++i < array_lengthof(Found) && Found[i])
531 // The last component was pushed off the end - append it.
532 if (!CurrentComponent.empty())
533 Components.push_back(CurrentComponent);
535 // Advance Idx to the component's new position.
536 while (++Idx < array_lengthof(Found) && Found[Idx])
538 } while (Idx < Pos); // Add more until the final position is reached.
540 assert(Pos < Components.size() && Components[Pos] == Comp &&
541 "Component moved wrong!");
547 // Special case logic goes here. At this point Arch, Vendor and OS have the
548 // correct values for the computed components.
550 if (OS == Triple::Win32) {
551 Components.resize(4);
552 Components[2] = "windows";
553 if (Environment == UnknownEnvironment) {
554 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
555 Components[3] = "msvc";
557 Components[3] = getObjectFormatTypeName(ObjectFormat);
559 } else if (OS == Triple::MinGW32) {
560 Components.resize(4);
561 Components[2] = "windows";
562 Components[3] = "gnu";
563 } else if (OS == Triple::Cygwin) {
564 Components.resize(4);
565 Components[2] = "windows";
566 Components[3] = "cygnus";
568 if (OS == Triple::MinGW32 || OS == Triple::Cygwin ||
569 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
570 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
571 Components.resize(5);
572 Components[4] = getObjectFormatTypeName(ObjectFormat);
576 // Stick the corrected components back together to form the normalized string.
577 std::string Normalized;
578 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
579 if (i) Normalized += '-';
580 Normalized += Components[i];
585 StringRef Triple::getArchName() const {
586 return StringRef(Data).split('-').first; // Isolate first component
589 StringRef Triple::getVendorName() const {
590 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
591 return Tmp.split('-').first; // Isolate second component
594 StringRef Triple::getOSName() const {
595 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
596 Tmp = Tmp.split('-').second; // Strip second component
597 return Tmp.split('-').first; // Isolate third component
600 StringRef Triple::getEnvironmentName() const {
601 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
602 Tmp = Tmp.split('-').second; // Strip second component
603 return Tmp.split('-').second; // Strip third component
606 StringRef Triple::getOSAndEnvironmentName() const {
607 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
608 return Tmp.split('-').second; // Strip second component
611 static unsigned EatNumber(StringRef &Str) {
612 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
616 // Consume the leading digit.
617 Result = Result*10 + (Str[0] - '0');
621 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
626 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
627 unsigned &Micro) const {
628 StringRef OSName = getOSName();
630 // Assume that the OS portion of the triple starts with the canonical name.
631 StringRef OSTypeName = getOSTypeName(getOS());
632 if (OSName.startswith(OSTypeName))
633 OSName = OSName.substr(OSTypeName.size());
635 // Any unset version defaults to 0.
636 Major = Minor = Micro = 0;
638 // Parse up to three components.
639 unsigned *Components[3] = { &Major, &Minor, &Micro };
640 for (unsigned i = 0; i != 3; ++i) {
641 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
644 // Consume the leading number.
645 *Components[i] = EatNumber(OSName);
647 // Consume the separator, if present.
648 if (OSName.startswith("."))
649 OSName = OSName.substr(1);
653 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
654 unsigned &Micro) const {
655 getOSVersion(Major, Minor, Micro);
658 default: llvm_unreachable("unexpected OS for Darwin triple");
660 // Default to darwin8, i.e., MacOSX 10.4.
663 // Darwin version numbers are skewed from OS X versions.
680 // Ignore the version from the triple. This is only handled because the
681 // the clang driver combines OS X and IOS support into a common Darwin
682 // toolchain that wants to know the OS X version number even when targeting
692 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
693 unsigned &Micro) const {
695 default: llvm_unreachable("unexpected OS for Darwin triple");
698 // Ignore the version from the triple. This is only handled because the
699 // the clang driver combines OS X and IOS support into a common Darwin
700 // toolchain that wants to know the iOS version number even when targeting
707 getOSVersion(Major, Minor, Micro);
708 // Default to 5.0 (or 7.0 for arm64).
710 Major = (getArch() == aarch64) ? 7 : 5;
715 void Triple::setTriple(const Twine &Str) {
719 void Triple::setArch(ArchType Kind) {
720 setArchName(getArchTypeName(Kind));
723 void Triple::setVendor(VendorType Kind) {
724 setVendorName(getVendorTypeName(Kind));
727 void Triple::setOS(OSType Kind) {
728 setOSName(getOSTypeName(Kind));
731 void Triple::setEnvironment(EnvironmentType Kind) {
732 setEnvironmentName(getEnvironmentTypeName(Kind));
735 void Triple::setObjectFormat(ObjectFormatType Kind) {
736 if (Environment == UnknownEnvironment)
737 return setEnvironmentName(getObjectFormatTypeName(Kind));
739 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
740 getObjectFormatTypeName(Kind)).str());
743 void Triple::setArchName(StringRef Str) {
744 // Work around a miscompilation bug for Twines in gcc 4.0.3.
745 SmallString<64> Triple;
748 Triple += getVendorName();
750 Triple += getOSAndEnvironmentName();
751 setTriple(Triple.str());
754 void Triple::setVendorName(StringRef Str) {
755 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
758 void Triple::setOSName(StringRef Str) {
759 if (hasEnvironment())
760 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
761 "-" + getEnvironmentName());
763 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
766 void Triple::setEnvironmentName(StringRef Str) {
767 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
771 void Triple::setOSAndEnvironmentName(StringRef Str) {
772 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
775 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
777 case llvm::Triple::UnknownArch:
780 case llvm::Triple::msp430:
783 case llvm::Triple::amdil:
784 case llvm::Triple::arm:
785 case llvm::Triple::armeb:
786 case llvm::Triple::hexagon:
787 case llvm::Triple::le32:
788 case llvm::Triple::mips:
789 case llvm::Triple::mipsel:
790 case llvm::Triple::nvptx:
791 case llvm::Triple::ppc:
792 case llvm::Triple::r600:
793 case llvm::Triple::sparc:
794 case llvm::Triple::tce:
795 case llvm::Triple::thumb:
796 case llvm::Triple::thumbeb:
797 case llvm::Triple::x86:
798 case llvm::Triple::xcore:
799 case llvm::Triple::spir:
800 case llvm::Triple::kalimba:
803 case llvm::Triple::aarch64:
804 case llvm::Triple::aarch64_be:
805 case llvm::Triple::mips64:
806 case llvm::Triple::mips64el:
807 case llvm::Triple::nvptx64:
808 case llvm::Triple::ppc64:
809 case llvm::Triple::ppc64le:
810 case llvm::Triple::sparcv9:
811 case llvm::Triple::systemz:
812 case llvm::Triple::x86_64:
813 case llvm::Triple::spir64:
816 llvm_unreachable("Invalid architecture value");
819 bool Triple::isArch64Bit() const {
820 return getArchPointerBitWidth(getArch()) == 64;
823 bool Triple::isArch32Bit() const {
824 return getArchPointerBitWidth(getArch()) == 32;
827 bool Triple::isArch16Bit() const {
828 return getArchPointerBitWidth(getArch()) == 16;
831 Triple Triple::get32BitArchVariant() const {
834 case Triple::UnknownArch:
835 case Triple::aarch64:
836 case Triple::aarch64_be:
838 case Triple::systemz:
839 case Triple::ppc64le:
840 T.setArch(UnknownArch);
847 case Triple::hexagon:
848 case Triple::kalimba:
858 case Triple::thumbeb:
864 case Triple::mips64: T.setArch(Triple::mips); break;
865 case Triple::mips64el: T.setArch(Triple::mipsel); break;
866 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
867 case Triple::ppc64: T.setArch(Triple::ppc); break;
868 case Triple::sparcv9: T.setArch(Triple::sparc); break;
869 case Triple::x86_64: T.setArch(Triple::x86); break;
870 case Triple::spir64: T.setArch(Triple::spir); break;
875 Triple Triple::get64BitArchVariant() const {
878 case Triple::UnknownArch:
882 case Triple::hexagon:
883 case Triple::kalimba:
889 case Triple::thumbeb:
891 T.setArch(UnknownArch);
894 case Triple::aarch64:
895 case Triple::aarch64_be:
898 case Triple::mips64el:
899 case Triple::nvptx64:
901 case Triple::ppc64le:
902 case Triple::sparcv9:
903 case Triple::systemz:
908 case Triple::mips: T.setArch(Triple::mips64); break;
909 case Triple::mipsel: T.setArch(Triple::mips64el); break;
910 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
911 case Triple::ppc: T.setArch(Triple::ppc64); break;
912 case Triple::sparc: T.setArch(Triple::sparcv9); break;
913 case Triple::x86: T.setArch(Triple::x86_64); break;
914 case Triple::spir: T.setArch(Triple::spir64); break;
919 // FIXME: tblgen this.
920 const char *Triple::getARMCPUForArch(StringRef MArch) const {
922 MArch = getArchName();
925 case llvm::Triple::NetBSD:
926 if (MArch == "armv6")
927 return "arm1176jzf-s";
929 case llvm::Triple::Win32:
930 // FIXME: this is invalid for WindowsCE
936 const char *result = nullptr;
937 size_t offset = StringRef::npos;
938 if (MArch.startswith("arm"))
940 if (MArch.startswith("thumb"))
942 if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
944 if (offset != StringRef::npos)
945 result = llvm::StringSwitch<const char *>(MArch.substr(offset))
946 .Cases("v2", "v2a", "arm2")
948 .Case("v3m", "arm7m")
949 .Case("v4", "strongarm")
950 .Case("v4t", "arm7tdmi")
951 .Cases("v5", "v5t", "arm10tdmi")
952 .Cases("v5e", "v5te", "arm1022e")
953 .Case("v5tej", "arm926ej-s")
954 .Cases("v6", "v6k", "arm1136jf-s")
955 .Case("v6j", "arm1136j-s")
956 .Cases("v6z", "v6zk", "arm1176jzf-s")
957 .Case("v6t2", "arm1156t2-s")
958 .Cases("v6m", "v6-m", "cortex-m0")
959 .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
960 .Cases("v7s", "v7-s", "swift")
961 .Cases("v7r", "v7-r", "cortex-r4")
962 .Cases("v7m", "v7-m", "cortex-m3")
963 .Cases("v7em", "v7e-m", "cortex-m4")
964 .Cases("v8", "v8a", "v8-a", "cortex-a53")
967 result = llvm::StringSwitch<const char *>(MArch)
968 .Case("ep9312", "ep9312")
969 .Case("iwmmxt", "iwmmxt")
970 .Case("xscale", "xscale")
976 // If all else failed, return the most base CPU with thumb interworking
977 // supported by LLVM.
978 // FIXME: Should warn once that we're falling back.
980 case llvm::Triple::NetBSD:
981 switch (getEnvironment()) {
982 case llvm::Triple::GNUEABIHF:
983 case llvm::Triple::GNUEABI:
984 case llvm::Triple::EABIHF:
985 case llvm::Triple::EABI:
991 switch (getEnvironment()) {
992 case llvm::Triple::EABIHF:
993 case llvm::Triple::GNUEABIHF:
994 return "arm1176jzf-s";