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)
340 .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
341 .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
342 .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
343 .Default(Triple::NoSubArch);
346 static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
348 case Triple::UnknownObjectFormat: return "";
349 case Triple::COFF: return "coff";
350 case Triple::ELF: return "elf";
351 case Triple::MachO: return "macho";
353 llvm_unreachable("unknown object format type");
356 static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
358 return Triple::MachO;
359 else if (T.isOSWindows())
364 /// \brief Construct a triple from the string representation provided.
366 /// This stores the string representation and parses the various pieces into
368 Triple::Triple(const Twine &Str)
370 Arch(parseArch(getArchName())),
371 SubArch(parseSubArch(getArchName())),
372 Vendor(parseVendor(getVendorName())),
373 OS(parseOS(getOSName())),
374 Environment(parseEnvironment(getEnvironmentName())),
375 ObjectFormat(parseFormat(getEnvironmentName())) {
376 if (ObjectFormat == Triple::UnknownObjectFormat)
377 ObjectFormat = getDefaultFormat(*this);
380 /// \brief Construct a triple from string representations of the architecture,
383 /// This joins each argument into a canonical string representation and parses
384 /// them into enum members. It leaves the environment unknown and omits it from
385 /// the string representation.
386 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
387 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
388 Arch(parseArch(ArchStr.str())),
389 SubArch(parseSubArch(ArchStr.str())),
390 Vendor(parseVendor(VendorStr.str())),
391 OS(parseOS(OSStr.str())),
392 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
393 ObjectFormat = getDefaultFormat(*this);
396 /// \brief Construct a triple from string representations of the architecture,
397 /// vendor, OS, and environment.
399 /// This joins each argument into a canonical string representation and parses
400 /// them into enum members.
401 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
402 const Twine &EnvironmentStr)
403 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
404 EnvironmentStr).str()),
405 Arch(parseArch(ArchStr.str())),
406 SubArch(parseSubArch(ArchStr.str())),
407 Vendor(parseVendor(VendorStr.str())),
408 OS(parseOS(OSStr.str())),
409 Environment(parseEnvironment(EnvironmentStr.str())),
410 ObjectFormat(parseFormat(EnvironmentStr.str())) {
411 if (ObjectFormat == Triple::UnknownObjectFormat)
412 ObjectFormat = getDefaultFormat(*this);
415 std::string Triple::normalize(StringRef Str) {
416 bool IsMinGW32 = false;
417 bool IsCygwin = false;
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 IsCygwin = Components[2].startswith("cygwin");
438 IsMinGW32 = Components[2].startswith("mingw");
440 EnvironmentType Environment = UnknownEnvironment;
441 if (Components.size() > 3)
442 Environment = parseEnvironment(Components[3]);
443 ObjectFormatType ObjectFormat = UnknownObjectFormat;
444 if (Components.size() > 4)
445 ObjectFormat = parseFormat(Components[4]);
447 // Note which components are already in their final position. These will not
450 Found[0] = Arch != UnknownArch;
451 Found[1] = Vendor != UnknownVendor;
452 Found[2] = OS != UnknownOS;
453 Found[3] = Environment != UnknownEnvironment;
455 // If they are not there already, permute the components into their canonical
456 // positions by seeing if they parse as a valid architecture, and if so moving
457 // the component to the architecture position etc.
458 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
460 continue; // Already in the canonical position.
462 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
463 // Do not reparse any components that already matched.
464 if (Idx < array_lengthof(Found) && Found[Idx])
467 // Does this component parse as valid for the target position?
469 StringRef Comp = Components[Idx];
471 default: llvm_unreachable("unexpected component type!");
473 Arch = parseArch(Comp);
474 Valid = Arch != UnknownArch;
477 Vendor = parseVendor(Comp);
478 Valid = Vendor != UnknownVendor;
482 IsCygwin = Comp.startswith("cygwin");
483 IsMinGW32 = Comp.startswith("mingw");
484 Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
487 Environment = parseEnvironment(Comp);
488 Valid = Environment != UnknownEnvironment;
490 ObjectFormat = parseFormat(Comp);
491 Valid = ObjectFormat != UnknownObjectFormat;
496 continue; // Nope, try the next component.
498 // Move the component to the target position, pushing any non-fixed
499 // components that are in the way to the right. This tends to give
500 // good results in the common cases of a forgotten vendor component
501 // or a wrongly positioned environment.
503 // Insert left, pushing the existing components to the right. For
504 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
505 StringRef CurrentComponent(""); // The empty component.
506 // Replace the component we are moving with an empty component.
507 std::swap(CurrentComponent, Components[Idx]);
508 // Insert the component being moved at Pos, displacing any existing
509 // components to the right.
510 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
511 // Skip over any fixed components.
512 while (i < array_lengthof(Found) && Found[i])
514 // Place the component at the new position, getting the component
515 // that was at this position - it will be moved right.
516 std::swap(CurrentComponent, Components[i]);
518 } else if (Pos > Idx) {
519 // Push right by inserting empty components until the component at Idx
520 // reaches the target position Pos. For example, pc-a -> -pc-a when
521 // moving pc to the second position.
523 // Insert one empty component at Idx.
524 StringRef CurrentComponent(""); // The empty component.
525 for (unsigned i = Idx; i < Components.size();) {
526 // Place the component at the new position, getting the component
527 // that was at this position - it will be moved right.
528 std::swap(CurrentComponent, Components[i]);
529 // If it was placed on top of an empty component then we are done.
530 if (CurrentComponent.empty())
532 // Advance to the next component, skipping any fixed components.
533 while (++i < array_lengthof(Found) && Found[i])
536 // The last component was pushed off the end - append it.
537 if (!CurrentComponent.empty())
538 Components.push_back(CurrentComponent);
540 // Advance Idx to the component's new position.
541 while (++Idx < array_lengthof(Found) && Found[Idx])
543 } while (Idx < Pos); // Add more until the final position is reached.
545 assert(Pos < Components.size() && Components[Pos] == Comp &&
546 "Component moved wrong!");
552 // Special case logic goes here. At this point Arch, Vendor and OS have the
553 // correct values for the computed components.
555 if (OS == Triple::Win32) {
556 Components.resize(4);
557 Components[2] = "windows";
558 if (Environment == UnknownEnvironment) {
559 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
560 Components[3] = "msvc";
562 Components[3] = getObjectFormatTypeName(ObjectFormat);
564 } else if (IsMinGW32) {
565 Components.resize(4);
566 Components[2] = "windows";
567 Components[3] = "gnu";
568 } else if (IsCygwin) {
569 Components.resize(4);
570 Components[2] = "windows";
571 Components[3] = "cygnus";
573 if (IsMinGW32 || IsCygwin ||
574 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
575 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
576 Components.resize(5);
577 Components[4] = getObjectFormatTypeName(ObjectFormat);
581 // Stick the corrected components back together to form the normalized string.
582 std::string Normalized;
583 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
584 if (i) Normalized += '-';
585 Normalized += Components[i];
590 StringRef Triple::getArchName() const {
591 return StringRef(Data).split('-').first; // Isolate first component
594 StringRef Triple::getVendorName() const {
595 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
596 return Tmp.split('-').first; // Isolate second component
599 StringRef Triple::getOSName() const {
600 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
601 Tmp = Tmp.split('-').second; // Strip second component
602 return Tmp.split('-').first; // Isolate third component
605 StringRef Triple::getEnvironmentName() const {
606 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
607 Tmp = Tmp.split('-').second; // Strip second component
608 return Tmp.split('-').second; // Strip third component
611 StringRef Triple::getOSAndEnvironmentName() const {
612 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
613 return Tmp.split('-').second; // Strip second component
616 static unsigned EatNumber(StringRef &Str) {
617 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
621 // Consume the leading digit.
622 Result = Result*10 + (Str[0] - '0');
626 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
631 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
632 unsigned &Micro) const {
633 StringRef OSName = getOSName();
635 // Assume that the OS portion of the triple starts with the canonical name.
636 StringRef OSTypeName = getOSTypeName(getOS());
637 if (OSName.startswith(OSTypeName))
638 OSName = OSName.substr(OSTypeName.size());
640 // Any unset version defaults to 0.
641 Major = Minor = Micro = 0;
643 // Parse up to three components.
644 unsigned *Components[3] = { &Major, &Minor, &Micro };
645 for (unsigned i = 0; i != 3; ++i) {
646 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
649 // Consume the leading number.
650 *Components[i] = EatNumber(OSName);
652 // Consume the separator, if present.
653 if (OSName.startswith("."))
654 OSName = OSName.substr(1);
658 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
659 unsigned &Micro) const {
660 getOSVersion(Major, Minor, Micro);
663 default: llvm_unreachable("unexpected OS for Darwin triple");
665 // Default to darwin8, i.e., MacOSX 10.4.
668 // Darwin version numbers are skewed from OS X versions.
685 // Ignore the version from the triple. This is only handled because the
686 // the clang driver combines OS X and IOS support into a common Darwin
687 // toolchain that wants to know the OS X version number even when targeting
697 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
698 unsigned &Micro) const {
700 default: llvm_unreachable("unexpected OS for Darwin triple");
703 // Ignore the version from the triple. This is only handled because the
704 // the clang driver combines OS X and IOS support into a common Darwin
705 // toolchain that wants to know the iOS version number even when targeting
712 getOSVersion(Major, Minor, Micro);
713 // Default to 5.0 (or 7.0 for arm64).
715 Major = (getArch() == aarch64) ? 7 : 5;
720 void Triple::setTriple(const Twine &Str) {
724 void Triple::setArch(ArchType Kind) {
725 setArchName(getArchTypeName(Kind));
728 void Triple::setVendor(VendorType Kind) {
729 setVendorName(getVendorTypeName(Kind));
732 void Triple::setOS(OSType Kind) {
733 setOSName(getOSTypeName(Kind));
736 void Triple::setEnvironment(EnvironmentType Kind) {
737 setEnvironmentName(getEnvironmentTypeName(Kind));
740 void Triple::setObjectFormat(ObjectFormatType Kind) {
741 if (Environment == UnknownEnvironment)
742 return setEnvironmentName(getObjectFormatTypeName(Kind));
744 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
745 getObjectFormatTypeName(Kind)).str());
748 void Triple::setArchName(StringRef Str) {
749 // Work around a miscompilation bug for Twines in gcc 4.0.3.
750 SmallString<64> Triple;
753 Triple += getVendorName();
755 Triple += getOSAndEnvironmentName();
756 setTriple(Triple.str());
759 void Triple::setVendorName(StringRef Str) {
760 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
763 void Triple::setOSName(StringRef Str) {
764 if (hasEnvironment())
765 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
766 "-" + getEnvironmentName());
768 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
771 void Triple::setEnvironmentName(StringRef Str) {
772 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
776 void Triple::setOSAndEnvironmentName(StringRef Str) {
777 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
780 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
782 case llvm::Triple::UnknownArch:
785 case llvm::Triple::msp430:
788 case llvm::Triple::amdil:
789 case llvm::Triple::arm:
790 case llvm::Triple::armeb:
791 case llvm::Triple::hexagon:
792 case llvm::Triple::le32:
793 case llvm::Triple::mips:
794 case llvm::Triple::mipsel:
795 case llvm::Triple::nvptx:
796 case llvm::Triple::ppc:
797 case llvm::Triple::r600:
798 case llvm::Triple::sparc:
799 case llvm::Triple::tce:
800 case llvm::Triple::thumb:
801 case llvm::Triple::thumbeb:
802 case llvm::Triple::x86:
803 case llvm::Triple::xcore:
804 case llvm::Triple::spir:
805 case llvm::Triple::kalimba:
808 case llvm::Triple::aarch64:
809 case llvm::Triple::aarch64_be:
810 case llvm::Triple::mips64:
811 case llvm::Triple::mips64el:
812 case llvm::Triple::nvptx64:
813 case llvm::Triple::ppc64:
814 case llvm::Triple::ppc64le:
815 case llvm::Triple::sparcv9:
816 case llvm::Triple::systemz:
817 case llvm::Triple::x86_64:
818 case llvm::Triple::spir64:
821 llvm_unreachable("Invalid architecture value");
824 bool Triple::isArch64Bit() const {
825 return getArchPointerBitWidth(getArch()) == 64;
828 bool Triple::isArch32Bit() const {
829 return getArchPointerBitWidth(getArch()) == 32;
832 bool Triple::isArch16Bit() const {
833 return getArchPointerBitWidth(getArch()) == 16;
836 Triple Triple::get32BitArchVariant() const {
839 case Triple::UnknownArch:
840 case Triple::aarch64:
841 case Triple::aarch64_be:
843 case Triple::systemz:
844 case Triple::ppc64le:
845 T.setArch(UnknownArch);
852 case Triple::hexagon:
853 case Triple::kalimba:
863 case Triple::thumbeb:
869 case Triple::mips64: T.setArch(Triple::mips); break;
870 case Triple::mips64el: T.setArch(Triple::mipsel); break;
871 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
872 case Triple::ppc64: T.setArch(Triple::ppc); break;
873 case Triple::sparcv9: T.setArch(Triple::sparc); break;
874 case Triple::x86_64: T.setArch(Triple::x86); break;
875 case Triple::spir64: T.setArch(Triple::spir); break;
880 Triple Triple::get64BitArchVariant() const {
883 case Triple::UnknownArch:
887 case Triple::hexagon:
888 case Triple::kalimba:
894 case Triple::thumbeb:
896 T.setArch(UnknownArch);
899 case Triple::aarch64:
900 case Triple::aarch64_be:
903 case Triple::mips64el:
904 case Triple::nvptx64:
906 case Triple::ppc64le:
907 case Triple::sparcv9:
908 case Triple::systemz:
913 case Triple::mips: T.setArch(Triple::mips64); break;
914 case Triple::mipsel: T.setArch(Triple::mips64el); break;
915 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
916 case Triple::ppc: T.setArch(Triple::ppc64); break;
917 case Triple::sparc: T.setArch(Triple::sparcv9); break;
918 case Triple::x86: T.setArch(Triple::x86_64); break;
919 case Triple::spir: T.setArch(Triple::spir64); break;
924 // FIXME: tblgen this.
925 const char *Triple::getARMCPUForArch(StringRef MArch) const {
927 MArch = getArchName();
930 case llvm::Triple::NetBSD:
931 if (MArch == "armv6")
932 return "arm1176jzf-s";
934 case llvm::Triple::Win32:
935 // FIXME: this is invalid for WindowsCE
941 const char *result = nullptr;
942 size_t offset = StringRef::npos;
943 if (MArch.startswith("arm"))
945 if (MArch.startswith("thumb"))
947 if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
949 if (offset != StringRef::npos)
950 result = llvm::StringSwitch<const char *>(MArch.substr(offset))
951 .Cases("v2", "v2a", "arm2")
953 .Case("v3m", "arm7m")
954 .Case("v4", "strongarm")
955 .Case("v4t", "arm7tdmi")
956 .Cases("v5", "v5t", "arm10tdmi")
957 .Cases("v5e", "v5te", "arm1022e")
958 .Case("v5tej", "arm926ej-s")
959 .Cases("v6", "v6k", "arm1136jf-s")
960 .Case("v6j", "arm1136j-s")
961 .Cases("v6z", "v6zk", "arm1176jzf-s")
962 .Case("v6t2", "arm1156t2-s")
963 .Cases("v6m", "v6-m", "cortex-m0")
964 .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
965 .Cases("v7s", "v7-s", "swift")
966 .Cases("v7r", "v7-r", "cortex-r4")
967 .Cases("v7m", "v7-m", "cortex-m3")
968 .Cases("v7em", "v7e-m", "cortex-m4")
969 .Cases("v8", "v8a", "v8-a", "cortex-a53")
972 result = llvm::StringSwitch<const char *>(MArch)
973 .Case("ep9312", "ep9312")
974 .Case("iwmmxt", "iwmmxt")
975 .Case("xscale", "xscale")
981 // If all else failed, return the most base CPU with thumb interworking
982 // supported by LLVM.
983 // FIXME: Should warn once that we're falling back.
985 case llvm::Triple::NetBSD:
986 switch (getEnvironment()) {
987 case llvm::Triple::GNUEABIHF:
988 case llvm::Triple::GNUEABI:
989 case llvm::Triple::EABIHF:
990 case llvm::Triple::EABI:
996 switch (getEnvironment()) {
997 case llvm::Triple::EABIHF:
998 case llvm::Triple::GNUEABIHF:
999 return "arm1176jzf-s";