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 bpf: return "bpf";
27 case hexagon: return "hexagon";
28 case mips: return "mips";
29 case mipsel: return "mipsel";
30 case mips64: return "mips64";
31 case mips64el: return "mips64el";
32 case msp430: return "msp430";
33 case ppc64: return "powerpc64";
34 case ppc64le: return "powerpc64le";
35 case ppc: return "powerpc";
36 case r600: return "r600";
37 case amdgcn: return "amdgcn";
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 le64: return "le64";
51 case amdil: return "amdil";
52 case amdil64: return "amdil64";
53 case hsail: return "hsail";
54 case hsail64: return "hsail64";
55 case spir: return "spir";
56 case spir64: return "spir64";
57 case kalimba: return "kalimba";
60 llvm_unreachable("Invalid ArchType!");
63 const char *Triple::getArchTypePrefix(ArchType Kind) {
69 case aarch64_be: return "aarch64";
74 case thumbeb: return "arm";
78 case ppc: return "ppc";
83 case mips64el: return "mips";
85 case hexagon: return "hexagon";
88 case r600: return "amdgpu";
90 case bpf: return "bpf";
93 case sparc: return "sparc";
95 case systemz: return "systemz";
98 case x86_64: return "x86";
100 case xcore: return "xcore";
102 case nvptx: return "nvptx";
103 case nvptx64: return "nvptx";
105 case le32: return "le32";
106 case le64: return "le64";
109 case amdil64: return "amdil";
112 case hsail64: return "hsail";
115 case spir64: return "spir";
116 case kalimba: return "kalimba";
120 const char *Triple::getVendorTypeName(VendorType Kind) {
122 case UnknownVendor: return "unknown";
124 case Apple: return "apple";
125 case PC: return "pc";
126 case SCEI: return "scei";
127 case BGP: return "bgp";
128 case BGQ: return "bgq";
129 case Freescale: return "fsl";
130 case IBM: return "ibm";
131 case ImaginationTechnologies: return "img";
132 case MipsTechnologies: return "mti";
133 case NVIDIA: return "nvidia";
134 case CSR: return "csr";
137 llvm_unreachable("Invalid VendorType!");
140 const char *Triple::getOSTypeName(OSType Kind) {
142 case UnknownOS: return "unknown";
144 case Darwin: return "darwin";
145 case DragonFly: return "dragonfly";
146 case FreeBSD: return "freebsd";
147 case IOS: return "ios";
148 case KFreeBSD: return "kfreebsd";
149 case Linux: return "linux";
150 case Lv2: return "lv2";
151 case MacOSX: return "macosx";
152 case NetBSD: return "netbsd";
153 case OpenBSD: return "openbsd";
154 case Solaris: return "solaris";
155 case Win32: return "windows";
156 case Haiku: return "haiku";
157 case Minix: return "minix";
158 case RTEMS: return "rtems";
159 case NaCl: return "nacl";
160 case CNK: return "cnk";
161 case Bitrig: return "bitrig";
162 case AIX: return "aix";
163 case CUDA: return "cuda";
164 case NVCL: return "nvcl";
165 case AMDHSA: return "amdhsa";
166 case PS4: return "ps4";
169 llvm_unreachable("Invalid OSType");
172 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
174 case UnknownEnvironment: return "unknown";
175 case GNU: return "gnu";
176 case GNUEABIHF: return "gnueabihf";
177 case GNUEABI: return "gnueabi";
178 case GNUX32: return "gnux32";
179 case CODE16: return "code16";
180 case EABI: return "eabi";
181 case EABIHF: return "eabihf";
182 case Android: return "android";
183 case MSVC: return "msvc";
184 case Itanium: return "itanium";
185 case Cygnus: return "cygnus";
188 llvm_unreachable("Invalid EnvironmentType!");
191 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
192 return StringSwitch<Triple::ArchType>(Name)
193 .Case("aarch64", aarch64)
194 .Case("aarch64_be", aarch64_be)
195 .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
197 .Case("armeb", armeb)
200 .Case("mipsel", mipsel)
201 .Case("mips64", mips64)
202 .Case("mips64el", mips64el)
203 .Case("msp430", msp430)
204 .Case("ppc64", ppc64)
207 .Case("ppc64le", ppc64le)
209 .Case("amdgcn", amdgcn)
210 .Case("hexagon", hexagon)
211 .Case("sparc", sparc)
212 .Case("sparcv9", sparcv9)
213 .Case("systemz", systemz)
215 .Case("thumb", thumb)
216 .Case("thumbeb", thumbeb)
218 .Case("x86-64", x86_64)
219 .Case("xcore", xcore)
220 .Case("nvptx", nvptx)
221 .Case("nvptx64", nvptx64)
224 .Case("amdil", amdil)
225 .Case("amdil64", amdil64)
226 .Case("hsail", hsail)
227 .Case("hsail64", hsail64)
229 .Case("spir64", spir64)
230 .Case("kalimba", kalimba)
231 .Default(UnknownArch);
234 static Triple::ArchType parseARMArch(StringRef ArchName) {
235 size_t offset = StringRef::npos;
236 Triple::ArchType arch = Triple::UnknownArch;
237 bool isThumb = ArchName.startswith("thumb");
239 if (ArchName.equals("arm"))
241 if (ArchName.equals("armeb"))
242 return Triple::armeb;
243 if (ArchName.equals("thumb"))
244 return Triple::thumb;
245 if (ArchName.equals("thumbeb"))
246 return Triple::thumbeb;
247 if (ArchName.equals("arm64") || ArchName.equals("aarch64"))
248 return Triple::aarch64;
249 if (ArchName.equals("aarch64_be"))
250 return Triple::aarch64_be;
252 if (ArchName.startswith("armv")) {
255 } else if (ArchName.startswith("armebv")) {
257 arch = Triple::armeb;
258 } else if (ArchName.startswith("thumbv")) {
260 arch = Triple::thumb;
261 } else if (ArchName.startswith("thumbebv")) {
263 arch = Triple::thumbeb;
265 return StringSwitch<Triple::ArchType>(ArchName.substr(offset))
266 .Cases("v2", "v2a", isThumb ? Triple::UnknownArch : arch)
267 .Cases("v3", "v3m", isThumb ? Triple::UnknownArch : arch)
268 .Cases("v4", "v4t", arch)
269 .Cases("v5", "v5e", "v5t", "v5te", "v5tej", arch)
270 .Cases("v6", "v6j", "v6k", "v6m", arch)
271 .Cases("v6t2", "v6z", "v6zk", arch)
272 .Cases("v7", "v7a", "v7em", "v7l", arch)
273 .Cases("v7m", "v7r", "v7s", arch)
274 .Cases("v8", "v8a", arch)
275 .Default(Triple::UnknownArch);
278 static Triple::ArchType parseArch(StringRef ArchName) {
279 return StringSwitch<Triple::ArchType>(ArchName)
280 .Cases("i386", "i486", "i586", "i686", Triple::x86)
281 // FIXME: Do we need to support these?
282 .Cases("i786", "i886", "i986", Triple::x86)
283 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
284 .Case("powerpc", Triple::ppc)
285 .Cases("powerpc64", "ppu", Triple::ppc64)
286 .Case("powerpc64le", Triple::ppc64le)
287 .Case("xscale", Triple::arm)
288 .StartsWith("arm", parseARMArch(ArchName))
289 .StartsWith("thumb", parseARMArch(ArchName))
290 .StartsWith("aarch64", parseARMArch(ArchName))
291 .Case("msp430", Triple::msp430)
292 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
293 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
294 .Cases("mips64", "mips64eb", Triple::mips64)
295 .Case("mips64el", Triple::mips64el)
296 .Case("r600", Triple::r600)
297 .Case("amdgcn", Triple::amdgcn)
298 .Case("bpf", Triple::bpf)
299 .Case("hexagon", Triple::hexagon)
300 .Case("s390x", Triple::systemz)
301 .Case("sparc", Triple::sparc)
302 .Cases("sparcv9", "sparc64", Triple::sparcv9)
303 .Case("tce", Triple::tce)
304 .Case("xcore", Triple::xcore)
305 .Case("nvptx", Triple::nvptx)
306 .Case("nvptx64", Triple::nvptx64)
307 .Case("le32", Triple::le32)
308 .Case("le64", Triple::le64)
309 .Case("amdil", Triple::amdil)
310 .Case("amdil64", Triple::amdil64)
311 .Case("hsail", Triple::hsail)
312 .Case("hsail64", Triple::hsail64)
313 .Case("spir", Triple::spir)
314 .Case("spir64", Triple::spir64)
315 .StartsWith("kalimba", Triple::kalimba)
316 .Default(Triple::UnknownArch);
319 static Triple::VendorType parseVendor(StringRef VendorName) {
320 return StringSwitch<Triple::VendorType>(VendorName)
321 .Case("apple", Triple::Apple)
322 .Case("pc", Triple::PC)
323 .Case("scei", Triple::SCEI)
324 .Case("bgp", Triple::BGP)
325 .Case("bgq", Triple::BGQ)
326 .Case("fsl", Triple::Freescale)
327 .Case("ibm", Triple::IBM)
328 .Case("img", Triple::ImaginationTechnologies)
329 .Case("mti", Triple::MipsTechnologies)
330 .Case("nvidia", Triple::NVIDIA)
331 .Case("csr", Triple::CSR)
332 .Default(Triple::UnknownVendor);
335 static Triple::OSType parseOS(StringRef OSName) {
336 return StringSwitch<Triple::OSType>(OSName)
337 .StartsWith("darwin", Triple::Darwin)
338 .StartsWith("dragonfly", Triple::DragonFly)
339 .StartsWith("freebsd", Triple::FreeBSD)
340 .StartsWith("ios", Triple::IOS)
341 .StartsWith("kfreebsd", Triple::KFreeBSD)
342 .StartsWith("linux", Triple::Linux)
343 .StartsWith("lv2", Triple::Lv2)
344 .StartsWith("macosx", Triple::MacOSX)
345 .StartsWith("netbsd", Triple::NetBSD)
346 .StartsWith("openbsd", Triple::OpenBSD)
347 .StartsWith("solaris", Triple::Solaris)
348 .StartsWith("win32", Triple::Win32)
349 .StartsWith("windows", Triple::Win32)
350 .StartsWith("haiku", Triple::Haiku)
351 .StartsWith("minix", Triple::Minix)
352 .StartsWith("rtems", Triple::RTEMS)
353 .StartsWith("nacl", Triple::NaCl)
354 .StartsWith("cnk", Triple::CNK)
355 .StartsWith("bitrig", Triple::Bitrig)
356 .StartsWith("aix", Triple::AIX)
357 .StartsWith("cuda", Triple::CUDA)
358 .StartsWith("nvcl", Triple::NVCL)
359 .StartsWith("amdhsa", Triple::AMDHSA)
360 .StartsWith("ps4", Triple::PS4)
361 .Default(Triple::UnknownOS);
364 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
365 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
366 .StartsWith("eabihf", Triple::EABIHF)
367 .StartsWith("eabi", Triple::EABI)
368 .StartsWith("gnueabihf", Triple::GNUEABIHF)
369 .StartsWith("gnueabi", Triple::GNUEABI)
370 .StartsWith("gnux32", Triple::GNUX32)
371 .StartsWith("code16", Triple::CODE16)
372 .StartsWith("gnu", Triple::GNU)
373 .StartsWith("android", Triple::Android)
374 .StartsWith("msvc", Triple::MSVC)
375 .StartsWith("itanium", Triple::Itanium)
376 .StartsWith("cygnus", Triple::Cygnus)
377 .Default(Triple::UnknownEnvironment);
380 static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
381 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
382 .EndsWith("coff", Triple::COFF)
383 .EndsWith("elf", Triple::ELF)
384 .EndsWith("macho", Triple::MachO)
385 .Default(Triple::UnknownObjectFormat);
388 static Triple::SubArchType parseSubArch(StringRef SubArchName) {
389 return StringSwitch<Triple::SubArchType>(SubArchName)
390 .EndsWith("v8", Triple::ARMSubArch_v8)
391 .EndsWith("v8a", Triple::ARMSubArch_v8)
392 .EndsWith("v7", Triple::ARMSubArch_v7)
393 .EndsWith("v7a", Triple::ARMSubArch_v7)
394 .EndsWith("v7em", Triple::ARMSubArch_v7em)
395 .EndsWith("v7l", Triple::ARMSubArch_v7)
396 .EndsWith("v7m", Triple::ARMSubArch_v7m)
397 .EndsWith("v7r", Triple::ARMSubArch_v7)
398 .EndsWith("v7s", Triple::ARMSubArch_v7s)
399 .EndsWith("v6", Triple::ARMSubArch_v6)
400 .EndsWith("v6m", Triple::ARMSubArch_v6m)
401 .EndsWith("v6t2", Triple::ARMSubArch_v6t2)
402 .EndsWith("v5", Triple::ARMSubArch_v5)
403 .EndsWith("v5e", Triple::ARMSubArch_v5)
404 .EndsWith("v5t", Triple::ARMSubArch_v5)
405 .EndsWith("v5te", Triple::ARMSubArch_v5te)
406 .EndsWith("v4t", Triple::ARMSubArch_v4t)
407 .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
408 .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
409 .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
410 .Default(Triple::NoSubArch);
413 static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
415 case Triple::UnknownObjectFormat: return "";
416 case Triple::COFF: return "coff";
417 case Triple::ELF: return "elf";
418 case Triple::MachO: return "macho";
420 llvm_unreachable("unknown object format type");
423 static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
425 return Triple::MachO;
426 else if (T.isOSWindows())
431 /// \brief Construct a triple from the string representation provided.
433 /// This stores the string representation and parses the various pieces into
435 Triple::Triple(const Twine &Str)
437 Arch(parseArch(getArchName())),
438 SubArch(parseSubArch(getArchName())),
439 Vendor(parseVendor(getVendorName())),
440 OS(parseOS(getOSName())),
441 Environment(parseEnvironment(getEnvironmentName())),
442 ObjectFormat(parseFormat(getEnvironmentName())) {
443 if (ObjectFormat == Triple::UnknownObjectFormat)
444 ObjectFormat = getDefaultFormat(*this);
447 /// \brief Construct a triple from string representations of the architecture,
450 /// This joins each argument into a canonical string representation and parses
451 /// them into enum members. It leaves the environment unknown and omits it from
452 /// the string representation.
453 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
454 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
455 Arch(parseArch(ArchStr.str())),
456 SubArch(parseSubArch(ArchStr.str())),
457 Vendor(parseVendor(VendorStr.str())),
458 OS(parseOS(OSStr.str())),
459 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
460 ObjectFormat = getDefaultFormat(*this);
463 /// \brief Construct a triple from string representations of the architecture,
464 /// vendor, OS, and environment.
466 /// This joins each argument into a canonical string representation and parses
467 /// them into enum members.
468 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
469 const Twine &EnvironmentStr)
470 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
471 EnvironmentStr).str()),
472 Arch(parseArch(ArchStr.str())),
473 SubArch(parseSubArch(ArchStr.str())),
474 Vendor(parseVendor(VendorStr.str())),
475 OS(parseOS(OSStr.str())),
476 Environment(parseEnvironment(EnvironmentStr.str())),
477 ObjectFormat(parseFormat(EnvironmentStr.str())) {
478 if (ObjectFormat == Triple::UnknownObjectFormat)
479 ObjectFormat = getDefaultFormat(*this);
482 std::string Triple::normalize(StringRef Str) {
483 bool IsMinGW32 = false;
484 bool IsCygwin = false;
486 // Parse into components.
487 SmallVector<StringRef, 4> Components;
488 Str.split(Components, "-");
490 // If the first component corresponds to a known architecture, preferentially
491 // use it for the architecture. If the second component corresponds to a
492 // known vendor, preferentially use it for the vendor, etc. This avoids silly
493 // component movement when a component parses as (eg) both a valid arch and a
495 ArchType Arch = UnknownArch;
496 if (Components.size() > 0)
497 Arch = parseArch(Components[0]);
498 VendorType Vendor = UnknownVendor;
499 if (Components.size() > 1)
500 Vendor = parseVendor(Components[1]);
501 OSType OS = UnknownOS;
502 if (Components.size() > 2) {
503 OS = parseOS(Components[2]);
504 IsCygwin = Components[2].startswith("cygwin");
505 IsMinGW32 = Components[2].startswith("mingw");
507 EnvironmentType Environment = UnknownEnvironment;
508 if (Components.size() > 3)
509 Environment = parseEnvironment(Components[3]);
510 ObjectFormatType ObjectFormat = UnknownObjectFormat;
511 if (Components.size() > 4)
512 ObjectFormat = parseFormat(Components[4]);
514 // Note which components are already in their final position. These will not
517 Found[0] = Arch != UnknownArch;
518 Found[1] = Vendor != UnknownVendor;
519 Found[2] = OS != UnknownOS;
520 Found[3] = Environment != UnknownEnvironment;
522 // If they are not there already, permute the components into their canonical
523 // positions by seeing if they parse as a valid architecture, and if so moving
524 // the component to the architecture position etc.
525 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
527 continue; // Already in the canonical position.
529 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
530 // Do not reparse any components that already matched.
531 if (Idx < array_lengthof(Found) && Found[Idx])
534 // Does this component parse as valid for the target position?
536 StringRef Comp = Components[Idx];
538 default: llvm_unreachable("unexpected component type!");
540 Arch = parseArch(Comp);
541 Valid = Arch != UnknownArch;
544 Vendor = parseVendor(Comp);
545 Valid = Vendor != UnknownVendor;
549 IsCygwin = Comp.startswith("cygwin");
550 IsMinGW32 = Comp.startswith("mingw");
551 Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
554 Environment = parseEnvironment(Comp);
555 Valid = Environment != UnknownEnvironment;
557 ObjectFormat = parseFormat(Comp);
558 Valid = ObjectFormat != UnknownObjectFormat;
563 continue; // Nope, try the next component.
565 // Move the component to the target position, pushing any non-fixed
566 // components that are in the way to the right. This tends to give
567 // good results in the common cases of a forgotten vendor component
568 // or a wrongly positioned environment.
570 // Insert left, pushing the existing components to the right. For
571 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
572 StringRef CurrentComponent(""); // The empty component.
573 // Replace the component we are moving with an empty component.
574 std::swap(CurrentComponent, Components[Idx]);
575 // Insert the component being moved at Pos, displacing any existing
576 // components to the right.
577 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
578 // Skip over any fixed components.
579 while (i < array_lengthof(Found) && Found[i])
581 // Place the component at the new position, getting the component
582 // that was at this position - it will be moved right.
583 std::swap(CurrentComponent, Components[i]);
585 } else if (Pos > Idx) {
586 // Push right by inserting empty components until the component at Idx
587 // reaches the target position Pos. For example, pc-a -> -pc-a when
588 // moving pc to the second position.
590 // Insert one empty component at Idx.
591 StringRef CurrentComponent(""); // The empty component.
592 for (unsigned i = Idx; i < Components.size();) {
593 // Place the component at the new position, getting the component
594 // that was at this position - it will be moved right.
595 std::swap(CurrentComponent, Components[i]);
596 // If it was placed on top of an empty component then we are done.
597 if (CurrentComponent.empty())
599 // Advance to the next component, skipping any fixed components.
600 while (++i < array_lengthof(Found) && Found[i])
603 // The last component was pushed off the end - append it.
604 if (!CurrentComponent.empty())
605 Components.push_back(CurrentComponent);
607 // Advance Idx to the component's new position.
608 while (++Idx < array_lengthof(Found) && Found[Idx])
610 } while (Idx < Pos); // Add more until the final position is reached.
612 assert(Pos < Components.size() && Components[Pos] == Comp &&
613 "Component moved wrong!");
619 // Special case logic goes here. At this point Arch, Vendor and OS have the
620 // correct values for the computed components.
622 if (OS == Triple::Win32) {
623 Components.resize(4);
624 Components[2] = "windows";
625 if (Environment == UnknownEnvironment) {
626 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
627 Components[3] = "msvc";
629 Components[3] = getObjectFormatTypeName(ObjectFormat);
631 } else if (IsMinGW32) {
632 Components.resize(4);
633 Components[2] = "windows";
634 Components[3] = "gnu";
635 } else if (IsCygwin) {
636 Components.resize(4);
637 Components[2] = "windows";
638 Components[3] = "cygnus";
640 if (IsMinGW32 || IsCygwin ||
641 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
642 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
643 Components.resize(5);
644 Components[4] = getObjectFormatTypeName(ObjectFormat);
648 // Stick the corrected components back together to form the normalized string.
649 std::string Normalized;
650 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
651 if (i) Normalized += '-';
652 Normalized += Components[i];
657 StringRef Triple::getArchName() const {
658 return StringRef(Data).split('-').first; // Isolate first component
661 StringRef Triple::getVendorName() const {
662 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
663 return Tmp.split('-').first; // Isolate second component
666 StringRef Triple::getOSName() const {
667 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
668 Tmp = Tmp.split('-').second; // Strip second component
669 return Tmp.split('-').first; // Isolate third component
672 StringRef Triple::getEnvironmentName() const {
673 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
674 Tmp = Tmp.split('-').second; // Strip second component
675 return Tmp.split('-').second; // Strip third component
678 StringRef Triple::getOSAndEnvironmentName() const {
679 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
680 return Tmp.split('-').second; // Strip second component
683 static unsigned EatNumber(StringRef &Str) {
684 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
688 // Consume the leading digit.
689 Result = Result*10 + (Str[0] - '0');
693 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
698 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
699 unsigned &Micro) const {
700 StringRef OSName = getOSName();
702 // Assume that the OS portion of the triple starts with the canonical name.
703 StringRef OSTypeName = getOSTypeName(getOS());
704 if (OSName.startswith(OSTypeName))
705 OSName = OSName.substr(OSTypeName.size());
707 // Any unset version defaults to 0.
708 Major = Minor = Micro = 0;
710 // Parse up to three components.
711 unsigned *Components[3] = { &Major, &Minor, &Micro };
712 for (unsigned i = 0; i != 3; ++i) {
713 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
716 // Consume the leading number.
717 *Components[i] = EatNumber(OSName);
719 // Consume the separator, if present.
720 if (OSName.startswith("."))
721 OSName = OSName.substr(1);
725 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
726 unsigned &Micro) const {
727 getOSVersion(Major, Minor, Micro);
730 default: llvm_unreachable("unexpected OS for Darwin triple");
732 // Default to darwin8, i.e., MacOSX 10.4.
735 // Darwin version numbers are skewed from OS X versions.
752 // Ignore the version from the triple. This is only handled because the
753 // the clang driver combines OS X and IOS support into a common Darwin
754 // toolchain that wants to know the OS X version number even when targeting
764 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
765 unsigned &Micro) const {
767 default: llvm_unreachable("unexpected OS for Darwin triple");
770 // Ignore the version from the triple. This is only handled because the
771 // the clang driver combines OS X and IOS support into a common Darwin
772 // toolchain that wants to know the iOS version number even when targeting
779 getOSVersion(Major, Minor, Micro);
780 // Default to 5.0 (or 7.0 for arm64).
782 Major = (getArch() == aarch64) ? 7 : 5;
787 void Triple::setTriple(const Twine &Str) {
791 void Triple::setArch(ArchType Kind) {
792 setArchName(getArchTypeName(Kind));
795 void Triple::setVendor(VendorType Kind) {
796 setVendorName(getVendorTypeName(Kind));
799 void Triple::setOS(OSType Kind) {
800 setOSName(getOSTypeName(Kind));
803 void Triple::setEnvironment(EnvironmentType Kind) {
804 setEnvironmentName(getEnvironmentTypeName(Kind));
807 void Triple::setObjectFormat(ObjectFormatType Kind) {
808 if (Environment == UnknownEnvironment)
809 return setEnvironmentName(getObjectFormatTypeName(Kind));
811 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
812 getObjectFormatTypeName(Kind)).str());
815 void Triple::setArchName(StringRef Str) {
816 // Work around a miscompilation bug for Twines in gcc 4.0.3.
817 SmallString<64> Triple;
820 Triple += getVendorName();
822 Triple += getOSAndEnvironmentName();
823 setTriple(Triple.str());
826 void Triple::setVendorName(StringRef Str) {
827 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
830 void Triple::setOSName(StringRef Str) {
831 if (hasEnvironment())
832 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
833 "-" + getEnvironmentName());
835 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
838 void Triple::setEnvironmentName(StringRef Str) {
839 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
843 void Triple::setOSAndEnvironmentName(StringRef Str) {
844 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
847 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
849 case llvm::Triple::UnknownArch:
852 case llvm::Triple::msp430:
855 case llvm::Triple::arm:
856 case llvm::Triple::armeb:
857 case llvm::Triple::hexagon:
858 case llvm::Triple::le32:
859 case llvm::Triple::mips:
860 case llvm::Triple::mipsel:
861 case llvm::Triple::nvptx:
862 case llvm::Triple::ppc:
863 case llvm::Triple::r600:
864 case llvm::Triple::sparc:
865 case llvm::Triple::tce:
866 case llvm::Triple::thumb:
867 case llvm::Triple::thumbeb:
868 case llvm::Triple::x86:
869 case llvm::Triple::xcore:
870 case llvm::Triple::amdil:
871 case llvm::Triple::hsail:
872 case llvm::Triple::spir:
873 case llvm::Triple::kalimba:
876 case llvm::Triple::aarch64:
877 case llvm::Triple::aarch64_be:
878 case llvm::Triple::amdgcn:
879 case llvm::Triple::bpf:
880 case llvm::Triple::le64:
881 case llvm::Triple::mips64:
882 case llvm::Triple::mips64el:
883 case llvm::Triple::nvptx64:
884 case llvm::Triple::ppc64:
885 case llvm::Triple::ppc64le:
886 case llvm::Triple::sparcv9:
887 case llvm::Triple::systemz:
888 case llvm::Triple::x86_64:
889 case llvm::Triple::amdil64:
890 case llvm::Triple::hsail64:
891 case llvm::Triple::spir64:
894 llvm_unreachable("Invalid architecture value");
897 bool Triple::isArch64Bit() const {
898 return getArchPointerBitWidth(getArch()) == 64;
901 bool Triple::isArch32Bit() const {
902 return getArchPointerBitWidth(getArch()) == 32;
905 bool Triple::isArch16Bit() const {
906 return getArchPointerBitWidth(getArch()) == 16;
909 Triple Triple::get32BitArchVariant() const {
912 case Triple::UnknownArch:
913 case Triple::aarch64:
914 case Triple::aarch64_be:
918 case Triple::systemz:
919 case Triple::ppc64le:
920 T.setArch(UnknownArch);
928 case Triple::hexagon:
929 case Triple::kalimba:
939 case Triple::thumbeb:
945 case Triple::le64: T.setArch(Triple::le32); break;
946 case Triple::mips64: T.setArch(Triple::mips); break;
947 case Triple::mips64el: T.setArch(Triple::mipsel); break;
948 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
949 case Triple::ppc64: T.setArch(Triple::ppc); break;
950 case Triple::sparcv9: T.setArch(Triple::sparc); break;
951 case Triple::x86_64: T.setArch(Triple::x86); break;
952 case Triple::amdil64: T.setArch(Triple::amdil); break;
953 case Triple::hsail64: T.setArch(Triple::hsail); break;
954 case Triple::spir64: T.setArch(Triple::spir); break;
959 Triple Triple::get64BitArchVariant() const {
962 case Triple::UnknownArch:
965 case Triple::hexagon:
966 case Triple::kalimba:
971 case Triple::thumbeb:
973 T.setArch(UnknownArch);
976 case Triple::aarch64:
977 case Triple::aarch64_be:
980 case Triple::amdil64:
982 case Triple::hsail64:
985 case Triple::mips64el:
986 case Triple::nvptx64:
988 case Triple::ppc64le:
989 case Triple::sparcv9:
990 case Triple::systemz:
995 case Triple::le32: T.setArch(Triple::le64); break;
996 case Triple::mips: T.setArch(Triple::mips64); break;
997 case Triple::mipsel: T.setArch(Triple::mips64el); break;
998 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
999 case Triple::ppc: T.setArch(Triple::ppc64); break;
1000 case Triple::sparc: T.setArch(Triple::sparcv9); break;
1001 case Triple::x86: T.setArch(Triple::x86_64); break;
1002 case Triple::amdil: T.setArch(Triple::amdil64); break;
1003 case Triple::hsail: T.setArch(Triple::hsail64); break;
1004 case Triple::spir: T.setArch(Triple::spir64); break;
1009 // FIXME: tblgen this.
1010 const char *Triple::getARMCPUForArch(StringRef MArch) const {
1012 MArch = getArchName();
1015 case llvm::Triple::FreeBSD:
1016 case llvm::Triple::NetBSD:
1017 if (MArch == "armv6")
1018 return "arm1176jzf-s";
1020 case llvm::Triple::Win32:
1021 // FIXME: this is invalid for WindowsCE
1027 const char *result = nullptr;
1028 size_t offset = StringRef::npos;
1029 if (MArch.startswith("arm"))
1031 if (MArch.startswith("thumb"))
1033 if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
1035 if (offset != StringRef::npos)
1036 result = llvm::StringSwitch<const char *>(MArch.substr(offset))
1037 .Cases("v2", "v2a", "arm2")
1039 .Case("v3m", "arm7m")
1040 .Case("v4", "strongarm")
1041 .Case("v4t", "arm7tdmi")
1042 .Cases("v5", "v5t", "arm10tdmi")
1043 .Cases("v5e", "v5te", "arm1022e")
1044 .Case("v5tej", "arm926ej-s")
1045 .Cases("v6", "v6k", "arm1136jf-s")
1046 .Case("v6j", "arm1136j-s")
1047 .Cases("v6z", "v6zk", "arm1176jzf-s")
1048 .Case("v6t2", "arm1156t2-s")
1049 .Cases("v6m", "v6-m", "cortex-m0")
1050 .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
1051 .Cases("v7s", "v7-s", "swift")
1052 .Cases("v7r", "v7-r", "cortex-r4")
1053 .Cases("v7m", "v7-m", "cortex-m3")
1054 .Cases("v7em", "v7e-m", "cortex-m4")
1055 .Cases("v8", "v8a", "v8-a", "cortex-a53")
1058 result = llvm::StringSwitch<const char *>(MArch)
1059 .Case("ep9312", "ep9312")
1060 .Case("iwmmxt", "iwmmxt")
1061 .Case("xscale", "xscale")
1067 // If all else failed, return the most base CPU with thumb interworking
1068 // supported by LLVM.
1069 // FIXME: Should warn once that we're falling back.
1071 case llvm::Triple::NetBSD:
1072 switch (getEnvironment()) {
1073 case llvm::Triple::GNUEABIHF:
1074 case llvm::Triple::GNUEABI:
1075 case llvm::Triple::EABIHF:
1076 case llvm::Triple::EABI:
1077 return "arm926ej-s";
1082 switch (getEnvironment()) {
1083 case llvm::Triple::EABIHF:
1084 case llvm::Triple::GNUEABIHF:
1085 return "arm1176jzf-s";