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 amdgcn: return "amdgcn";
37 case sparc: return "sparc";
38 case sparcv9: return "sparcv9";
39 case systemz: return "s390x";
40 case tce: return "tce";
41 case thumb: return "thumb";
42 case thumbeb: return "thumbeb";
43 case x86: return "i386";
44 case x86_64: return "x86_64";
45 case xcore: return "xcore";
46 case nvptx: return "nvptx";
47 case nvptx64: return "nvptx64";
48 case le32: return "le32";
49 case le64: return "le64";
50 case amdil: return "amdil";
51 case amdil64: return "amdil64";
52 case hsail: return "hsail";
53 case hsail64: return "hsail64";
54 case spir: return "spir";
55 case spir64: return "spir64";
56 case kalimba: return "kalimba";
59 llvm_unreachable("Invalid ArchType!");
62 const char *Triple::getArchTypePrefix(ArchType Kind) {
68 case aarch64_be: return "aarch64";
73 case thumbeb: return "arm";
77 case ppc: return "ppc";
82 case mips64el: return "mips";
84 case hexagon: return "hexagon";
87 case r600: return "amdgpu";
90 case sparc: return "sparc";
92 case systemz: return "systemz";
95 case x86_64: return "x86";
97 case xcore: return "xcore";
99 case nvptx: return "nvptx";
100 case nvptx64: return "nvptx";
102 case le32: return "le32";
103 case le64: return "le64";
106 case amdil64: return "amdil";
109 case hsail64: return "hsail";
112 case spir64: return "spir";
113 case kalimba: return "kalimba";
117 const char *Triple::getVendorTypeName(VendorType Kind) {
119 case UnknownVendor: return "unknown";
121 case Apple: return "apple";
122 case PC: return "pc";
123 case SCEI: return "scei";
124 case BGP: return "bgp";
125 case BGQ: return "bgq";
126 case Freescale: return "fsl";
127 case IBM: return "ibm";
128 case ImaginationTechnologies: return "img";
129 case MipsTechnologies: return "mti";
130 case NVIDIA: return "nvidia";
131 case CSR: return "csr";
134 llvm_unreachable("Invalid VendorType!");
137 const char *Triple::getOSTypeName(OSType Kind) {
139 case UnknownOS: return "unknown";
141 case Darwin: return "darwin";
142 case DragonFly: return "dragonfly";
143 case FreeBSD: return "freebsd";
144 case IOS: return "ios";
145 case KFreeBSD: return "kfreebsd";
146 case Linux: return "linux";
147 case Lv2: return "lv2";
148 case MacOSX: return "macosx";
149 case NetBSD: return "netbsd";
150 case OpenBSD: return "openbsd";
151 case Solaris: return "solaris";
152 case Win32: return "windows";
153 case Haiku: return "haiku";
154 case Minix: return "minix";
155 case RTEMS: return "rtems";
156 case NaCl: return "nacl";
157 case CNK: return "cnk";
158 case Bitrig: return "bitrig";
159 case AIX: return "aix";
160 case CUDA: return "cuda";
161 case NVCL: return "nvcl";
162 case AMDHSA: return "amdhsa";
165 llvm_unreachable("Invalid OSType");
168 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
170 case UnknownEnvironment: return "unknown";
171 case GNU: return "gnu";
172 case GNUEABIHF: return "gnueabihf";
173 case GNUEABI: return "gnueabi";
174 case GNUX32: return "gnux32";
175 case CODE16: return "code16";
176 case EABI: return "eabi";
177 case EABIHF: return "eabihf";
178 case Android: return "android";
179 case MSVC: return "msvc";
180 case Itanium: return "itanium";
181 case Cygnus: return "cygnus";
184 llvm_unreachable("Invalid EnvironmentType!");
187 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
188 return StringSwitch<Triple::ArchType>(Name)
189 .Case("aarch64", aarch64)
190 .Case("aarch64_be", aarch64_be)
191 .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
193 .Case("armeb", armeb)
195 .Case("mipsel", mipsel)
196 .Case("mips64", mips64)
197 .Case("mips64el", mips64el)
198 .Case("msp430", msp430)
199 .Case("ppc64", ppc64)
202 .Case("ppc64le", ppc64le)
204 .Case("amdgcn", amdgcn)
205 .Case("hexagon", hexagon)
206 .Case("sparc", sparc)
207 .Case("sparcv9", sparcv9)
208 .Case("systemz", systemz)
210 .Case("thumb", thumb)
211 .Case("thumbeb", thumbeb)
213 .Case("x86-64", x86_64)
214 .Case("xcore", xcore)
215 .Case("nvptx", nvptx)
216 .Case("nvptx64", nvptx64)
219 .Case("amdil", amdil)
220 .Case("amdil64", amdil64)
221 .Case("hsail", hsail)
222 .Case("hsail64", hsail64)
224 .Case("spir64", spir64)
225 .Case("kalimba", kalimba)
226 .Default(UnknownArch);
229 static Triple::ArchType parseARMArch(StringRef ArchName) {
230 size_t offset = StringRef::npos;
231 Triple::ArchType arch = Triple::UnknownArch;
232 bool isThumb = ArchName.startswith("thumb");
234 if (ArchName.equals("arm"))
236 if (ArchName.equals("armeb"))
237 return Triple::armeb;
238 if (ArchName.equals("thumb"))
239 return Triple::thumb;
240 if (ArchName.equals("thumbeb"))
241 return Triple::thumbeb;
242 if (ArchName.equals("arm64") || ArchName.equals("aarch64"))
243 return Triple::aarch64;
244 if (ArchName.equals("aarch64_be"))
245 return Triple::aarch64_be;
247 if (ArchName.startswith("armv")) {
249 if (ArchName.endswith("eb")) {
250 arch = Triple::armeb;
251 ArchName = ArchName.substr(0, ArchName.size() - 2);
254 } else if (ArchName.startswith("armebv")) {
256 arch = Triple::armeb;
257 } else if (ArchName.startswith("thumbv")) {
259 if (ArchName.endswith("eb")) {
260 arch = Triple::thumbeb;
261 ArchName = ArchName.substr(0, ArchName.size() - 2);
263 arch = Triple::thumb;
264 } else if (ArchName.startswith("thumbebv")) {
266 arch = Triple::thumbeb;
268 return StringSwitch<Triple::ArchType>(ArchName.substr(offset))
269 .Cases("v2", "v2a", isThumb ? Triple::UnknownArch : arch)
270 .Cases("v3", "v3m", isThumb ? Triple::UnknownArch : arch)
271 .Cases("v4", "v4t", arch)
272 .Cases("v5", "v5e", "v5t", "v5te", "v5tej", arch)
273 .Cases("v6", "v6j", "v6k", "v6m", arch)
274 .Cases("v6t2", "v6z", "v6zk", arch)
275 .Cases("v7", "v7a", "v7em", "v7l", arch)
276 .Cases("v7m", "v7r", "v7s", arch)
277 .Cases("v8", "v8a", arch)
278 .Default(Triple::UnknownArch);
281 static Triple::ArchType parseArch(StringRef ArchName) {
282 Triple::ArchType ARMArch(parseARMArch(ArchName));
284 return StringSwitch<Triple::ArchType>(ArchName)
285 .Cases("i386", "i486", "i586", "i686", Triple::x86)
286 // FIXME: Do we need to support these?
287 .Cases("i786", "i886", "i986", Triple::x86)
288 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
289 .Case("powerpc", Triple::ppc)
290 .Cases("powerpc64", "ppu", Triple::ppc64)
291 .Case("powerpc64le", Triple::ppc64le)
292 .Case("xscale", Triple::arm)
293 .Case("xscaleeb", Triple::armeb)
294 .StartsWith("arm", ARMArch)
295 .StartsWith("thumb", ARMArch)
296 .StartsWith("aarch64", ARMArch)
297 .Case("msp430", Triple::msp430)
298 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
299 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
300 .Cases("mips64", "mips64eb", Triple::mips64)
301 .Case("mips64el", Triple::mips64el)
302 .Case("r600", Triple::r600)
303 .Case("amdgcn", Triple::amdgcn)
304 .Case("hexagon", Triple::hexagon)
305 .Case("s390x", Triple::systemz)
306 .Case("sparc", Triple::sparc)
307 .Cases("sparcv9", "sparc64", Triple::sparcv9)
308 .Case("tce", Triple::tce)
309 .Case("xcore", Triple::xcore)
310 .Case("nvptx", Triple::nvptx)
311 .Case("nvptx64", Triple::nvptx64)
312 .Case("le32", Triple::le32)
313 .Case("le64", Triple::le64)
314 .Case("amdil", Triple::amdil)
315 .Case("amdil64", Triple::amdil64)
316 .Case("hsail", Triple::hsail)
317 .Case("hsail64", Triple::hsail64)
318 .Case("spir", Triple::spir)
319 .Case("spir64", Triple::spir64)
320 .StartsWith("kalimba", Triple::kalimba)
321 .Default(Triple::UnknownArch);
324 static Triple::VendorType parseVendor(StringRef VendorName) {
325 return StringSwitch<Triple::VendorType>(VendorName)
326 .Case("apple", Triple::Apple)
327 .Case("pc", Triple::PC)
328 .Case("scei", Triple::SCEI)
329 .Case("bgp", Triple::BGP)
330 .Case("bgq", Triple::BGQ)
331 .Case("fsl", Triple::Freescale)
332 .Case("ibm", Triple::IBM)
333 .Case("img", Triple::ImaginationTechnologies)
334 .Case("mti", Triple::MipsTechnologies)
335 .Case("nvidia", Triple::NVIDIA)
336 .Case("csr", Triple::CSR)
337 .Default(Triple::UnknownVendor);
340 static Triple::OSType parseOS(StringRef OSName) {
341 return StringSwitch<Triple::OSType>(OSName)
342 .StartsWith("darwin", Triple::Darwin)
343 .StartsWith("dragonfly", Triple::DragonFly)
344 .StartsWith("freebsd", Triple::FreeBSD)
345 .StartsWith("ios", Triple::IOS)
346 .StartsWith("kfreebsd", Triple::KFreeBSD)
347 .StartsWith("linux", Triple::Linux)
348 .StartsWith("lv2", Triple::Lv2)
349 .StartsWith("macosx", Triple::MacOSX)
350 .StartsWith("netbsd", Triple::NetBSD)
351 .StartsWith("openbsd", Triple::OpenBSD)
352 .StartsWith("solaris", Triple::Solaris)
353 .StartsWith("win32", Triple::Win32)
354 .StartsWith("windows", Triple::Win32)
355 .StartsWith("haiku", Triple::Haiku)
356 .StartsWith("minix", Triple::Minix)
357 .StartsWith("rtems", Triple::RTEMS)
358 .StartsWith("nacl", Triple::NaCl)
359 .StartsWith("cnk", Triple::CNK)
360 .StartsWith("bitrig", Triple::Bitrig)
361 .StartsWith("aix", Triple::AIX)
362 .StartsWith("cuda", Triple::CUDA)
363 .StartsWith("nvcl", Triple::NVCL)
364 .StartsWith("amdhsa", Triple::AMDHSA)
365 .Default(Triple::UnknownOS);
368 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
369 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
370 .StartsWith("eabihf", Triple::EABIHF)
371 .StartsWith("eabi", Triple::EABI)
372 .StartsWith("gnueabihf", Triple::GNUEABIHF)
373 .StartsWith("gnueabi", Triple::GNUEABI)
374 .StartsWith("gnux32", Triple::GNUX32)
375 .StartsWith("code16", Triple::CODE16)
376 .StartsWith("gnu", Triple::GNU)
377 .StartsWith("android", Triple::Android)
378 .StartsWith("msvc", Triple::MSVC)
379 .StartsWith("itanium", Triple::Itanium)
380 .StartsWith("cygnus", Triple::Cygnus)
381 .Default(Triple::UnknownEnvironment);
384 static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
385 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
386 .EndsWith("coff", Triple::COFF)
387 .EndsWith("elf", Triple::ELF)
388 .EndsWith("macho", Triple::MachO)
389 .Default(Triple::UnknownObjectFormat);
392 static Triple::SubArchType parseSubArch(StringRef SubArchName) {
393 if (SubArchName.endswith("eb"))
394 SubArchName = SubArchName.substr(0, SubArchName.size() - 2);
396 return StringSwitch<Triple::SubArchType>(SubArchName)
397 .EndsWith("v8", Triple::ARMSubArch_v8)
398 .EndsWith("v8a", Triple::ARMSubArch_v8)
399 .EndsWith("v7", Triple::ARMSubArch_v7)
400 .EndsWith("v7a", Triple::ARMSubArch_v7)
401 .EndsWith("v7em", Triple::ARMSubArch_v7em)
402 .EndsWith("v7l", Triple::ARMSubArch_v7)
403 .EndsWith("v7m", Triple::ARMSubArch_v7m)
404 .EndsWith("v7r", Triple::ARMSubArch_v7)
405 .EndsWith("v7s", Triple::ARMSubArch_v7s)
406 .EndsWith("v6", Triple::ARMSubArch_v6)
407 .EndsWith("v6m", Triple::ARMSubArch_v6m)
408 .EndsWith("v6t2", Triple::ARMSubArch_v6t2)
409 .EndsWith("v5", Triple::ARMSubArch_v5)
410 .EndsWith("v5e", Triple::ARMSubArch_v5)
411 .EndsWith("v5t", Triple::ARMSubArch_v5)
412 .EndsWith("v5te", Triple::ARMSubArch_v5te)
413 .EndsWith("v4t", Triple::ARMSubArch_v4t)
414 .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
415 .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
416 .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
417 .Default(Triple::NoSubArch);
420 static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
422 case Triple::UnknownObjectFormat: return "";
423 case Triple::COFF: return "coff";
424 case Triple::ELF: return "elf";
425 case Triple::MachO: return "macho";
427 llvm_unreachable("unknown object format type");
430 static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
432 return Triple::MachO;
433 else if (T.isOSWindows())
438 /// \brief Construct a triple from the string representation provided.
440 /// This stores the string representation and parses the various pieces into
442 Triple::Triple(const Twine &Str)
444 Arch(parseArch(getArchName())),
445 SubArch(parseSubArch(getArchName())),
446 Vendor(parseVendor(getVendorName())),
447 OS(parseOS(getOSName())),
448 Environment(parseEnvironment(getEnvironmentName())),
449 ObjectFormat(parseFormat(getEnvironmentName())) {
450 if (ObjectFormat == Triple::UnknownObjectFormat)
451 ObjectFormat = getDefaultFormat(*this);
454 /// \brief Construct a triple from string representations of the architecture,
457 /// This joins each argument into a canonical string representation and parses
458 /// them into enum members. It leaves the environment unknown and omits it from
459 /// the string representation.
460 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
461 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
462 Arch(parseArch(ArchStr.str())),
463 SubArch(parseSubArch(ArchStr.str())),
464 Vendor(parseVendor(VendorStr.str())),
465 OS(parseOS(OSStr.str())),
466 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
467 ObjectFormat = getDefaultFormat(*this);
470 /// \brief Construct a triple from string representations of the architecture,
471 /// vendor, OS, and environment.
473 /// This joins each argument into a canonical string representation and parses
474 /// them into enum members.
475 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
476 const Twine &EnvironmentStr)
477 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
478 EnvironmentStr).str()),
479 Arch(parseArch(ArchStr.str())),
480 SubArch(parseSubArch(ArchStr.str())),
481 Vendor(parseVendor(VendorStr.str())),
482 OS(parseOS(OSStr.str())),
483 Environment(parseEnvironment(EnvironmentStr.str())),
484 ObjectFormat(parseFormat(EnvironmentStr.str())) {
485 if (ObjectFormat == Triple::UnknownObjectFormat)
486 ObjectFormat = getDefaultFormat(*this);
489 std::string Triple::normalize(StringRef Str) {
490 bool IsMinGW32 = false;
491 bool IsCygwin = false;
493 // Parse into components.
494 SmallVector<StringRef, 4> Components;
495 Str.split(Components, "-");
497 // If the first component corresponds to a known architecture, preferentially
498 // use it for the architecture. If the second component corresponds to a
499 // known vendor, preferentially use it for the vendor, etc. This avoids silly
500 // component movement when a component parses as (eg) both a valid arch and a
502 ArchType Arch = UnknownArch;
503 if (Components.size() > 0)
504 Arch = parseArch(Components[0]);
505 VendorType Vendor = UnknownVendor;
506 if (Components.size() > 1)
507 Vendor = parseVendor(Components[1]);
508 OSType OS = UnknownOS;
509 if (Components.size() > 2) {
510 OS = parseOS(Components[2]);
511 IsCygwin = Components[2].startswith("cygwin");
512 IsMinGW32 = Components[2].startswith("mingw");
514 EnvironmentType Environment = UnknownEnvironment;
515 if (Components.size() > 3)
516 Environment = parseEnvironment(Components[3]);
517 ObjectFormatType ObjectFormat = UnknownObjectFormat;
518 if (Components.size() > 4)
519 ObjectFormat = parseFormat(Components[4]);
521 // Note which components are already in their final position. These will not
524 Found[0] = Arch != UnknownArch;
525 Found[1] = Vendor != UnknownVendor;
526 Found[2] = OS != UnknownOS;
527 Found[3] = Environment != UnknownEnvironment;
529 // If they are not there already, permute the components into their canonical
530 // positions by seeing if they parse as a valid architecture, and if so moving
531 // the component to the architecture position etc.
532 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
534 continue; // Already in the canonical position.
536 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
537 // Do not reparse any components that already matched.
538 if (Idx < array_lengthof(Found) && Found[Idx])
541 // Does this component parse as valid for the target position?
543 StringRef Comp = Components[Idx];
545 default: llvm_unreachable("unexpected component type!");
547 Arch = parseArch(Comp);
548 Valid = Arch != UnknownArch;
551 Vendor = parseVendor(Comp);
552 Valid = Vendor != UnknownVendor;
556 IsCygwin = Comp.startswith("cygwin");
557 IsMinGW32 = Comp.startswith("mingw");
558 Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
561 Environment = parseEnvironment(Comp);
562 Valid = Environment != UnknownEnvironment;
564 ObjectFormat = parseFormat(Comp);
565 Valid = ObjectFormat != UnknownObjectFormat;
570 continue; // Nope, try the next component.
572 // Move the component to the target position, pushing any non-fixed
573 // components that are in the way to the right. This tends to give
574 // good results in the common cases of a forgotten vendor component
575 // or a wrongly positioned environment.
577 // Insert left, pushing the existing components to the right. For
578 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
579 StringRef CurrentComponent(""); // The empty component.
580 // Replace the component we are moving with an empty component.
581 std::swap(CurrentComponent, Components[Idx]);
582 // Insert the component being moved at Pos, displacing any existing
583 // components to the right.
584 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
585 // Skip over any fixed components.
586 while (i < array_lengthof(Found) && Found[i])
588 // Place the component at the new position, getting the component
589 // that was at this position - it will be moved right.
590 std::swap(CurrentComponent, Components[i]);
592 } else if (Pos > Idx) {
593 // Push right by inserting empty components until the component at Idx
594 // reaches the target position Pos. For example, pc-a -> -pc-a when
595 // moving pc to the second position.
597 // Insert one empty component at Idx.
598 StringRef CurrentComponent(""); // The empty component.
599 for (unsigned i = Idx; i < Components.size();) {
600 // Place the component at the new position, getting the component
601 // that was at this position - it will be moved right.
602 std::swap(CurrentComponent, Components[i]);
603 // If it was placed on top of an empty component then we are done.
604 if (CurrentComponent.empty())
606 // Advance to the next component, skipping any fixed components.
607 while (++i < array_lengthof(Found) && Found[i])
610 // The last component was pushed off the end - append it.
611 if (!CurrentComponent.empty())
612 Components.push_back(CurrentComponent);
614 // Advance Idx to the component's new position.
615 while (++Idx < array_lengthof(Found) && Found[Idx])
617 } while (Idx < Pos); // Add more until the final position is reached.
619 assert(Pos < Components.size() && Components[Pos] == Comp &&
620 "Component moved wrong!");
626 // Special case logic goes here. At this point Arch, Vendor and OS have the
627 // correct values for the computed components.
629 if (OS == Triple::Win32) {
630 Components.resize(4);
631 Components[2] = "windows";
632 if (Environment == UnknownEnvironment) {
633 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
634 Components[3] = "msvc";
636 Components[3] = getObjectFormatTypeName(ObjectFormat);
638 } else if (IsMinGW32) {
639 Components.resize(4);
640 Components[2] = "windows";
641 Components[3] = "gnu";
642 } else if (IsCygwin) {
643 Components.resize(4);
644 Components[2] = "windows";
645 Components[3] = "cygnus";
647 if (IsMinGW32 || IsCygwin ||
648 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
649 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
650 Components.resize(5);
651 Components[4] = getObjectFormatTypeName(ObjectFormat);
655 // Stick the corrected components back together to form the normalized string.
656 std::string Normalized;
657 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
658 if (i) Normalized += '-';
659 Normalized += Components[i];
664 StringRef Triple::getArchName() const {
665 return StringRef(Data).split('-').first; // Isolate first component
668 StringRef Triple::getVendorName() const {
669 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
670 return Tmp.split('-').first; // Isolate second component
673 StringRef Triple::getOSName() const {
674 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
675 Tmp = Tmp.split('-').second; // Strip second component
676 return Tmp.split('-').first; // Isolate third component
679 StringRef Triple::getEnvironmentName() const {
680 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
681 Tmp = Tmp.split('-').second; // Strip second component
682 return Tmp.split('-').second; // Strip third component
685 StringRef Triple::getOSAndEnvironmentName() const {
686 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
687 return Tmp.split('-').second; // Strip second component
690 static unsigned EatNumber(StringRef &Str) {
691 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
695 // Consume the leading digit.
696 Result = Result*10 + (Str[0] - '0');
700 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
705 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
706 unsigned &Micro) const {
707 StringRef OSName = getOSName();
709 // Assume that the OS portion of the triple starts with the canonical name.
710 StringRef OSTypeName = getOSTypeName(getOS());
711 if (OSName.startswith(OSTypeName))
712 OSName = OSName.substr(OSTypeName.size());
714 // Any unset version defaults to 0.
715 Major = Minor = Micro = 0;
717 // Parse up to three components.
718 unsigned *Components[3] = { &Major, &Minor, &Micro };
719 for (unsigned i = 0; i != 3; ++i) {
720 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
723 // Consume the leading number.
724 *Components[i] = EatNumber(OSName);
726 // Consume the separator, if present.
727 if (OSName.startswith("."))
728 OSName = OSName.substr(1);
732 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
733 unsigned &Micro) const {
734 getOSVersion(Major, Minor, Micro);
737 default: llvm_unreachable("unexpected OS for Darwin triple");
739 // Default to darwin8, i.e., MacOSX 10.4.
742 // Darwin version numbers are skewed from OS X versions.
759 // Ignore the version from the triple. This is only handled because the
760 // the clang driver combines OS X and IOS support into a common Darwin
761 // toolchain that wants to know the OS X version number even when targeting
771 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
772 unsigned &Micro) const {
774 default: llvm_unreachable("unexpected OS for Darwin triple");
777 // Ignore the version from the triple. This is only handled because the
778 // the clang driver combines OS X and IOS support into a common Darwin
779 // toolchain that wants to know the iOS version number even when targeting
786 getOSVersion(Major, Minor, Micro);
787 // Default to 5.0 (or 7.0 for arm64).
789 Major = (getArch() == aarch64) ? 7 : 5;
794 void Triple::setTriple(const Twine &Str) {
798 void Triple::setArch(ArchType Kind) {
799 setArchName(getArchTypeName(Kind));
802 void Triple::setVendor(VendorType Kind) {
803 setVendorName(getVendorTypeName(Kind));
806 void Triple::setOS(OSType Kind) {
807 setOSName(getOSTypeName(Kind));
810 void Triple::setEnvironment(EnvironmentType Kind) {
811 setEnvironmentName(getEnvironmentTypeName(Kind));
814 void Triple::setObjectFormat(ObjectFormatType Kind) {
815 if (Environment == UnknownEnvironment)
816 return setEnvironmentName(getObjectFormatTypeName(Kind));
818 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
819 getObjectFormatTypeName(Kind)).str());
822 void Triple::setArchName(StringRef Str) {
823 // Work around a miscompilation bug for Twines in gcc 4.0.3.
824 SmallString<64> Triple;
827 Triple += getVendorName();
829 Triple += getOSAndEnvironmentName();
830 setTriple(Triple.str());
833 void Triple::setVendorName(StringRef Str) {
834 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
837 void Triple::setOSName(StringRef Str) {
838 if (hasEnvironment())
839 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
840 "-" + getEnvironmentName());
842 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
845 void Triple::setEnvironmentName(StringRef Str) {
846 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
850 void Triple::setOSAndEnvironmentName(StringRef Str) {
851 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
854 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
856 case llvm::Triple::UnknownArch:
859 case llvm::Triple::msp430:
862 case llvm::Triple::arm:
863 case llvm::Triple::armeb:
864 case llvm::Triple::hexagon:
865 case llvm::Triple::le32:
866 case llvm::Triple::mips:
867 case llvm::Triple::mipsel:
868 case llvm::Triple::nvptx:
869 case llvm::Triple::ppc:
870 case llvm::Triple::r600:
871 case llvm::Triple::sparc:
872 case llvm::Triple::tce:
873 case llvm::Triple::thumb:
874 case llvm::Triple::thumbeb:
875 case llvm::Triple::x86:
876 case llvm::Triple::xcore:
877 case llvm::Triple::amdil:
878 case llvm::Triple::hsail:
879 case llvm::Triple::spir:
880 case llvm::Triple::kalimba:
883 case llvm::Triple::aarch64:
884 case llvm::Triple::aarch64_be:
885 case llvm::Triple::amdgcn:
886 case llvm::Triple::le64:
887 case llvm::Triple::mips64:
888 case llvm::Triple::mips64el:
889 case llvm::Triple::nvptx64:
890 case llvm::Triple::ppc64:
891 case llvm::Triple::ppc64le:
892 case llvm::Triple::sparcv9:
893 case llvm::Triple::systemz:
894 case llvm::Triple::x86_64:
895 case llvm::Triple::amdil64:
896 case llvm::Triple::hsail64:
897 case llvm::Triple::spir64:
900 llvm_unreachable("Invalid architecture value");
903 bool Triple::isArch64Bit() const {
904 return getArchPointerBitWidth(getArch()) == 64;
907 bool Triple::isArch32Bit() const {
908 return getArchPointerBitWidth(getArch()) == 32;
911 bool Triple::isArch16Bit() const {
912 return getArchPointerBitWidth(getArch()) == 16;
915 Triple Triple::get32BitArchVariant() const {
918 case Triple::UnknownArch:
919 case Triple::aarch64:
920 case Triple::aarch64_be:
923 case Triple::systemz:
924 case Triple::ppc64le:
925 T.setArch(UnknownArch);
933 case Triple::hexagon:
934 case Triple::kalimba:
944 case Triple::thumbeb:
950 case Triple::le64: T.setArch(Triple::le32); break;
951 case Triple::mips64: T.setArch(Triple::mips); break;
952 case Triple::mips64el: T.setArch(Triple::mipsel); break;
953 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
954 case Triple::ppc64: T.setArch(Triple::ppc); break;
955 case Triple::sparcv9: T.setArch(Triple::sparc); break;
956 case Triple::x86_64: T.setArch(Triple::x86); break;
957 case Triple::amdil64: T.setArch(Triple::amdil); break;
958 case Triple::hsail64: T.setArch(Triple::hsail); break;
959 case Triple::spir64: T.setArch(Triple::spir); break;
964 Triple Triple::get64BitArchVariant() const {
967 case Triple::UnknownArch:
970 case Triple::hexagon:
971 case Triple::kalimba:
976 case Triple::thumbeb:
978 T.setArch(UnknownArch);
981 case Triple::aarch64:
982 case Triple::aarch64_be:
984 case Triple::amdil64:
986 case Triple::hsail64:
989 case Triple::mips64el:
990 case Triple::nvptx64:
992 case Triple::ppc64le:
993 case Triple::sparcv9:
994 case Triple::systemz:
999 case Triple::le32: T.setArch(Triple::le64); break;
1000 case Triple::mips: T.setArch(Triple::mips64); break;
1001 case Triple::mipsel: T.setArch(Triple::mips64el); break;
1002 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
1003 case Triple::ppc: T.setArch(Triple::ppc64); break;
1004 case Triple::sparc: T.setArch(Triple::sparcv9); break;
1005 case Triple::x86: T.setArch(Triple::x86_64); break;
1006 case Triple::amdil: T.setArch(Triple::amdil64); break;
1007 case Triple::hsail: T.setArch(Triple::hsail64); break;
1008 case Triple::spir: T.setArch(Triple::spir64); break;
1013 // FIXME: tblgen this.
1014 const char *Triple::getARMCPUForArch(StringRef MArch) const {
1016 MArch = getArchName();
1019 case llvm::Triple::FreeBSD:
1020 case llvm::Triple::NetBSD:
1021 if (MArch == "armv6")
1022 return "arm1176jzf-s";
1024 case llvm::Triple::Win32:
1025 // FIXME: this is invalid for WindowsCE
1031 const char *result = nullptr;
1032 size_t offset = StringRef::npos;
1033 if (MArch.startswith("arm"))
1035 if (MArch.startswith("thumb"))
1037 if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
1039 if (MArch.endswith("eb"))
1040 MArch = MArch.substr(0, MArch.size() - 2);
1041 if (offset != StringRef::npos)
1042 result = llvm::StringSwitch<const char *>(MArch.substr(offset))
1043 .Cases("v2", "v2a", "arm2")
1045 .Case("v3m", "arm7m")
1046 .Case("v4", "strongarm")
1047 .Case("v4t", "arm7tdmi")
1048 .Cases("v5", "v5t", "arm10tdmi")
1049 .Cases("v5e", "v5te", "arm1022e")
1050 .Case("v5tej", "arm926ej-s")
1051 .Cases("v6", "v6k", "arm1136jf-s")
1052 .Case("v6j", "arm1136j-s")
1053 .Cases("v6z", "v6zk", "arm1176jzf-s")
1054 .Case("v6t2", "arm1156t2-s")
1055 .Cases("v6m", "v6-m", "cortex-m0")
1056 .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
1057 .Cases("v7s", "v7-s", "swift")
1058 .Cases("v7r", "v7-r", "cortex-r4")
1059 .Cases("v7m", "v7-m", "cortex-m3")
1060 .Cases("v7em", "v7e-m", "cortex-m4")
1061 .Cases("v8", "v8a", "v8-a", "cortex-a53")
1064 result = llvm::StringSwitch<const char *>(MArch)
1065 .Case("ep9312", "ep9312")
1066 .Case("iwmmxt", "iwmmxt")
1067 .Case("xscale", "xscale")
1073 // If all else failed, return the most base CPU with thumb interworking
1074 // supported by LLVM.
1075 // FIXME: Should warn once that we're falling back.
1077 case llvm::Triple::NetBSD:
1078 switch (getEnvironment()) {
1079 case llvm::Triple::GNUEABIHF:
1080 case llvm::Triple::GNUEABI:
1081 case llvm::Triple::EABIHF:
1082 case llvm::Triple::EABI:
1083 return "arm926ej-s";
1088 switch (getEnvironment()) {
1089 case llvm::Triple::EABIHF:
1090 case llvm::Triple::GNUEABIHF:
1091 return "arm1176jzf-s";