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 sparcel: return "sparcel";
41 case systemz: return "s390x";
42 case tce: return "tce";
43 case thumb: return "thumb";
44 case thumbeb: return "thumbeb";
45 case x86: return "i386";
46 case x86_64: return "x86_64";
47 case xcore: return "xcore";
48 case nvptx: return "nvptx";
49 case nvptx64: return "nvptx64";
50 case le32: return "le32";
51 case le64: return "le64";
52 case amdil: return "amdil";
53 case amdil64: return "amdil64";
54 case hsail: return "hsail";
55 case hsail64: return "hsail64";
56 case spir: return "spir";
57 case spir64: return "spir64";
58 case kalimba: return "kalimba";
61 llvm_unreachable("Invalid ArchType!");
64 const char *Triple::getArchTypePrefix(ArchType Kind) {
70 case aarch64_be: return "aarch64";
75 case thumbeb: return "arm";
79 case ppc: return "ppc";
84 case mips64el: return "mips";
86 case hexagon: return "hexagon";
89 case r600: return "amdgpu";
91 case bpf: return "bpf";
95 case sparc: return "sparc";
97 case systemz: return "s390";
100 case x86_64: return "x86";
102 case xcore: return "xcore";
104 case nvptx: return "nvptx";
105 case nvptx64: return "nvptx";
107 case le32: return "le32";
108 case le64: return "le64";
111 case amdil64: return "amdil";
114 case hsail64: return "hsail";
117 case spir64: return "spir";
118 case kalimba: return "kalimba";
122 const char *Triple::getVendorTypeName(VendorType Kind) {
124 case UnknownVendor: return "unknown";
126 case Apple: return "apple";
127 case PC: return "pc";
128 case SCEI: return "scei";
129 case BGP: return "bgp";
130 case BGQ: return "bgq";
131 case Freescale: return "fsl";
132 case IBM: return "ibm";
133 case ImaginationTechnologies: return "img";
134 case MipsTechnologies: return "mti";
135 case NVIDIA: return "nvidia";
136 case CSR: return "csr";
139 llvm_unreachable("Invalid VendorType!");
142 const char *Triple::getOSTypeName(OSType Kind) {
144 case UnknownOS: return "unknown";
146 case CloudABI: return "cloudabi";
147 case Darwin: return "darwin";
148 case DragonFly: return "dragonfly";
149 case FreeBSD: return "freebsd";
150 case IOS: return "ios";
151 case KFreeBSD: return "kfreebsd";
152 case Linux: return "linux";
153 case Lv2: return "lv2";
154 case MacOSX: return "macosx";
155 case NetBSD: return "netbsd";
156 case OpenBSD: return "openbsd";
157 case Solaris: return "solaris";
158 case Win32: return "windows";
159 case Haiku: return "haiku";
160 case Minix: return "minix";
161 case RTEMS: return "rtems";
162 case NaCl: return "nacl";
163 case CNK: return "cnk";
164 case Bitrig: return "bitrig";
165 case AIX: return "aix";
166 case CUDA: return "cuda";
167 case NVCL: return "nvcl";
168 case AMDHSA: return "amdhsa";
169 case PS4: return "ps4";
172 llvm_unreachable("Invalid OSType");
175 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
177 case UnknownEnvironment: return "unknown";
178 case GNU: return "gnu";
179 case GNUEABIHF: return "gnueabihf";
180 case GNUEABI: return "gnueabi";
181 case GNUX32: return "gnux32";
182 case CODE16: return "code16";
183 case EABI: return "eabi";
184 case EABIHF: return "eabihf";
185 case Android: return "android";
186 case MSVC: return "msvc";
187 case Itanium: return "itanium";
188 case Cygnus: return "cygnus";
191 llvm_unreachable("Invalid EnvironmentType!");
194 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
195 return StringSwitch<Triple::ArchType>(Name)
196 .Case("aarch64", aarch64)
197 .Case("aarch64_be", aarch64_be)
198 .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
200 .Case("armeb", armeb)
203 .Case("mipsel", mipsel)
204 .Case("mips64", mips64)
205 .Case("mips64el", mips64el)
206 .Case("msp430", msp430)
207 .Case("ppc64", ppc64)
210 .Case("ppc64le", ppc64le)
212 .Case("amdgcn", amdgcn)
213 .Case("hexagon", hexagon)
214 .Case("sparc", sparc)
215 .Case("sparcv9", sparcv9)
216 .Case("systemz", systemz)
218 .Case("thumb", thumb)
219 .Case("thumbeb", thumbeb)
221 .Case("x86-64", x86_64)
222 .Case("xcore", xcore)
223 .Case("nvptx", nvptx)
224 .Case("nvptx64", nvptx64)
227 .Case("amdil", amdil)
228 .Case("amdil64", amdil64)
229 .Case("hsail", hsail)
230 .Case("hsail64", hsail64)
232 .Case("spir64", spir64)
233 .Case("kalimba", kalimba)
234 .Default(UnknownArch);
237 static Triple::ArchType parseARMArch(StringRef ArchName) {
238 size_t offset = StringRef::npos;
239 Triple::ArchType arch = Triple::UnknownArch;
240 bool isThumb = ArchName.startswith("thumb");
242 if (ArchName.equals("arm"))
244 if (ArchName.equals("armeb"))
245 return Triple::armeb;
246 if (ArchName.equals("thumb"))
247 return Triple::thumb;
248 if (ArchName.equals("thumbeb"))
249 return Triple::thumbeb;
250 if (ArchName.equals("arm64") || ArchName.equals("aarch64"))
251 return Triple::aarch64;
252 if (ArchName.equals("aarch64_be"))
253 return Triple::aarch64_be;
255 if (ArchName.startswith("armv")) {
257 if (ArchName.endswith("eb")) {
258 arch = Triple::armeb;
259 ArchName = ArchName.substr(0, ArchName.size() - 2);
262 } else if (ArchName.startswith("armebv")) {
264 arch = Triple::armeb;
265 } else if (ArchName.startswith("thumbv")) {
267 if (ArchName.endswith("eb")) {
268 arch = Triple::thumbeb;
269 ArchName = ArchName.substr(0, ArchName.size() - 2);
271 arch = Triple::thumb;
272 } else if (ArchName.startswith("thumbebv")) {
274 arch = Triple::thumbeb;
276 return StringSwitch<Triple::ArchType>(ArchName.substr(offset))
277 .Cases("v2", "v2a", isThumb ? Triple::UnknownArch : arch)
278 .Cases("v3", "v3m", isThumb ? Triple::UnknownArch : arch)
279 .Cases("v4", "v4t", arch)
280 .Cases("v5", "v5e", "v5t", "v5te", "v5tej", arch)
281 .Cases("v6", "v6j", "v6k", "v6m", "v6sm", arch)
282 .Cases("v6t2", "v6z", "v6zk", arch)
283 .Cases("v7", "v7a", "v7em", "v7l", arch)
284 .Cases("v7m", "v7r", "v7s", arch)
285 .Cases("v8", "v8a", arch)
286 .Cases("v8.1", "v8.1a", arch)
287 .Default(Triple::UnknownArch);
290 static Triple::ArchType parseArch(StringRef ArchName) {
291 Triple::ArchType ARMArch(parseARMArch(ArchName));
293 return StringSwitch<Triple::ArchType>(ArchName)
294 .Cases("i386", "i486", "i586", "i686", Triple::x86)
295 // FIXME: Do we need to support these?
296 .Cases("i786", "i886", "i986", Triple::x86)
297 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
298 .Case("powerpc", Triple::ppc)
299 .Cases("powerpc64", "ppu", Triple::ppc64)
300 .Case("powerpc64le", Triple::ppc64le)
301 .Case("xscale", Triple::arm)
302 .Case("xscaleeb", Triple::armeb)
303 .StartsWith("arm", ARMArch)
304 .StartsWith("thumb", ARMArch)
305 .StartsWith("aarch64", ARMArch)
306 .Case("msp430", Triple::msp430)
307 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
308 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
309 .Cases("mips64", "mips64eb", Triple::mips64)
310 .Case("mips64el", Triple::mips64el)
311 .Case("r600", Triple::r600)
312 .Case("amdgcn", Triple::amdgcn)
313 .Case("bpf", Triple::bpf)
314 .Case("hexagon", Triple::hexagon)
315 .Case("s390x", Triple::systemz)
316 .Case("sparc", Triple::sparc)
317 .Case("sparcel", Triple::sparcel)
318 .Cases("sparcv9", "sparc64", Triple::sparcv9)
319 .Case("tce", Triple::tce)
320 .Case("xcore", Triple::xcore)
321 .Case("nvptx", Triple::nvptx)
322 .Case("nvptx64", Triple::nvptx64)
323 .Case("le32", Triple::le32)
324 .Case("le64", Triple::le64)
325 .Case("amdil", Triple::amdil)
326 .Case("amdil64", Triple::amdil64)
327 .Case("hsail", Triple::hsail)
328 .Case("hsail64", Triple::hsail64)
329 .Case("spir", Triple::spir)
330 .Case("spir64", Triple::spir64)
331 .StartsWith("kalimba", Triple::kalimba)
332 .Default(Triple::UnknownArch);
335 static Triple::VendorType parseVendor(StringRef VendorName) {
336 return StringSwitch<Triple::VendorType>(VendorName)
337 .Case("apple", Triple::Apple)
338 .Case("pc", Triple::PC)
339 .Case("scei", Triple::SCEI)
340 .Case("bgp", Triple::BGP)
341 .Case("bgq", Triple::BGQ)
342 .Case("fsl", Triple::Freescale)
343 .Case("ibm", Triple::IBM)
344 .Case("img", Triple::ImaginationTechnologies)
345 .Case("mti", Triple::MipsTechnologies)
346 .Case("nvidia", Triple::NVIDIA)
347 .Case("csr", Triple::CSR)
348 .Default(Triple::UnknownVendor);
351 static Triple::OSType parseOS(StringRef OSName) {
352 return StringSwitch<Triple::OSType>(OSName)
353 .StartsWith("cloudabi", Triple::CloudABI)
354 .StartsWith("darwin", Triple::Darwin)
355 .StartsWith("dragonfly", Triple::DragonFly)
356 .StartsWith("freebsd", Triple::FreeBSD)
357 .StartsWith("ios", Triple::IOS)
358 .StartsWith("kfreebsd", Triple::KFreeBSD)
359 .StartsWith("linux", Triple::Linux)
360 .StartsWith("lv2", Triple::Lv2)
361 .StartsWith("macosx", Triple::MacOSX)
362 .StartsWith("netbsd", Triple::NetBSD)
363 .StartsWith("openbsd", Triple::OpenBSD)
364 .StartsWith("solaris", Triple::Solaris)
365 .StartsWith("win32", Triple::Win32)
366 .StartsWith("windows", Triple::Win32)
367 .StartsWith("haiku", Triple::Haiku)
368 .StartsWith("minix", Triple::Minix)
369 .StartsWith("rtems", Triple::RTEMS)
370 .StartsWith("nacl", Triple::NaCl)
371 .StartsWith("cnk", Triple::CNK)
372 .StartsWith("bitrig", Triple::Bitrig)
373 .StartsWith("aix", Triple::AIX)
374 .StartsWith("cuda", Triple::CUDA)
375 .StartsWith("nvcl", Triple::NVCL)
376 .StartsWith("amdhsa", Triple::AMDHSA)
377 .StartsWith("ps4", Triple::PS4)
378 .Default(Triple::UnknownOS);
381 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
382 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
383 .StartsWith("eabihf", Triple::EABIHF)
384 .StartsWith("eabi", Triple::EABI)
385 .StartsWith("gnueabihf", Triple::GNUEABIHF)
386 .StartsWith("gnueabi", Triple::GNUEABI)
387 .StartsWith("gnux32", Triple::GNUX32)
388 .StartsWith("code16", Triple::CODE16)
389 .StartsWith("gnu", Triple::GNU)
390 .StartsWith("android", Triple::Android)
391 .StartsWith("msvc", Triple::MSVC)
392 .StartsWith("itanium", Triple::Itanium)
393 .StartsWith("cygnus", Triple::Cygnus)
394 .Default(Triple::UnknownEnvironment);
397 static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
398 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
399 .EndsWith("coff", Triple::COFF)
400 .EndsWith("elf", Triple::ELF)
401 .EndsWith("macho", Triple::MachO)
402 .Default(Triple::UnknownObjectFormat);
405 static Triple::SubArchType parseSubArch(StringRef SubArchName) {
406 if (SubArchName.endswith("eb"))
407 SubArchName = SubArchName.substr(0, SubArchName.size() - 2);
409 return StringSwitch<Triple::SubArchType>(SubArchName)
410 .EndsWith("v8.1a", Triple::ARMSubArch_v8_1a)
411 .EndsWith("v8", Triple::ARMSubArch_v8)
412 .EndsWith("v8a", Triple::ARMSubArch_v8)
413 .EndsWith("v7", Triple::ARMSubArch_v7)
414 .EndsWith("v7a", Triple::ARMSubArch_v7)
415 .EndsWith("v7em", Triple::ARMSubArch_v7em)
416 .EndsWith("v7l", Triple::ARMSubArch_v7)
417 .EndsWith("v7m", Triple::ARMSubArch_v7m)
418 .EndsWith("v7r", Triple::ARMSubArch_v7)
419 .EndsWith("v7s", Triple::ARMSubArch_v7s)
420 .EndsWith("v6", Triple::ARMSubArch_v6)
421 .EndsWith("v6m", Triple::ARMSubArch_v6m)
422 .EndsWith("v6sm", Triple::ARMSubArch_v6m)
423 .EndsWith("v6k", Triple::ARMSubArch_v6k)
424 .EndsWith("v6t2", Triple::ARMSubArch_v6t2)
425 .EndsWith("v5", Triple::ARMSubArch_v5)
426 .EndsWith("v5e", Triple::ARMSubArch_v5)
427 .EndsWith("v5t", Triple::ARMSubArch_v5)
428 .EndsWith("v5te", Triple::ARMSubArch_v5te)
429 .EndsWith("v4t", Triple::ARMSubArch_v4t)
430 .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
431 .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
432 .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
433 .Default(Triple::NoSubArch);
436 static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
438 case Triple::UnknownObjectFormat: return "";
439 case Triple::COFF: return "coff";
440 case Triple::ELF: return "elf";
441 case Triple::MachO: return "macho";
443 llvm_unreachable("unknown object format type");
446 static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
447 switch (T.getArch()) {
450 case Triple::hexagon:
454 case Triple::mips64el:
458 case Triple::sparcv9:
459 case Triple::systemz:
461 case Triple::ppc64le:
467 return Triple::MachO;
472 return Triple::MachO;
473 else if (T.isOSWindows())
478 /// \brief Construct a triple from the string representation provided.
480 /// This stores the string representation and parses the various pieces into
482 Triple::Triple(const Twine &Str)
484 Arch(parseArch(getArchName())),
485 SubArch(parseSubArch(getArchName())),
486 Vendor(parseVendor(getVendorName())),
487 OS(parseOS(getOSName())),
488 Environment(parseEnvironment(getEnvironmentName())),
489 ObjectFormat(parseFormat(getEnvironmentName())) {
490 if (ObjectFormat == Triple::UnknownObjectFormat)
491 ObjectFormat = getDefaultFormat(*this);
494 /// \brief Construct a triple from string representations of the architecture,
497 /// This joins each argument into a canonical string representation and parses
498 /// them into enum members. It leaves the environment unknown and omits it from
499 /// the string representation.
500 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
501 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
502 Arch(parseArch(ArchStr.str())),
503 SubArch(parseSubArch(ArchStr.str())),
504 Vendor(parseVendor(VendorStr.str())),
505 OS(parseOS(OSStr.str())),
506 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
507 ObjectFormat = getDefaultFormat(*this);
510 /// \brief Construct a triple from string representations of the architecture,
511 /// vendor, OS, and environment.
513 /// This joins each argument into a canonical string representation and parses
514 /// them into enum members.
515 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
516 const Twine &EnvironmentStr)
517 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
518 EnvironmentStr).str()),
519 Arch(parseArch(ArchStr.str())),
520 SubArch(parseSubArch(ArchStr.str())),
521 Vendor(parseVendor(VendorStr.str())),
522 OS(parseOS(OSStr.str())),
523 Environment(parseEnvironment(EnvironmentStr.str())),
524 ObjectFormat(parseFormat(EnvironmentStr.str())) {
525 if (ObjectFormat == Triple::UnknownObjectFormat)
526 ObjectFormat = getDefaultFormat(*this);
529 std::string Triple::normalize(StringRef Str) {
530 bool IsMinGW32 = false;
531 bool IsCygwin = false;
533 // Parse into components.
534 SmallVector<StringRef, 4> Components;
535 Str.split(Components, "-");
537 // If the first component corresponds to a known architecture, preferentially
538 // use it for the architecture. If the second component corresponds to a
539 // known vendor, preferentially use it for the vendor, etc. This avoids silly
540 // component movement when a component parses as (eg) both a valid arch and a
542 ArchType Arch = UnknownArch;
543 if (Components.size() > 0)
544 Arch = parseArch(Components[0]);
545 VendorType Vendor = UnknownVendor;
546 if (Components.size() > 1)
547 Vendor = parseVendor(Components[1]);
548 OSType OS = UnknownOS;
549 if (Components.size() > 2) {
550 OS = parseOS(Components[2]);
551 IsCygwin = Components[2].startswith("cygwin");
552 IsMinGW32 = Components[2].startswith("mingw");
554 EnvironmentType Environment = UnknownEnvironment;
555 if (Components.size() > 3)
556 Environment = parseEnvironment(Components[3]);
557 ObjectFormatType ObjectFormat = UnknownObjectFormat;
558 if (Components.size() > 4)
559 ObjectFormat = parseFormat(Components[4]);
561 // Note which components are already in their final position. These will not
564 Found[0] = Arch != UnknownArch;
565 Found[1] = Vendor != UnknownVendor;
566 Found[2] = OS != UnknownOS;
567 Found[3] = Environment != UnknownEnvironment;
569 // If they are not there already, permute the components into their canonical
570 // positions by seeing if they parse as a valid architecture, and if so moving
571 // the component to the architecture position etc.
572 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
574 continue; // Already in the canonical position.
576 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
577 // Do not reparse any components that already matched.
578 if (Idx < array_lengthof(Found) && Found[Idx])
581 // Does this component parse as valid for the target position?
583 StringRef Comp = Components[Idx];
585 default: llvm_unreachable("unexpected component type!");
587 Arch = parseArch(Comp);
588 Valid = Arch != UnknownArch;
591 Vendor = parseVendor(Comp);
592 Valid = Vendor != UnknownVendor;
596 IsCygwin = Comp.startswith("cygwin");
597 IsMinGW32 = Comp.startswith("mingw");
598 Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
601 Environment = parseEnvironment(Comp);
602 Valid = Environment != UnknownEnvironment;
604 ObjectFormat = parseFormat(Comp);
605 Valid = ObjectFormat != UnknownObjectFormat;
610 continue; // Nope, try the next component.
612 // Move the component to the target position, pushing any non-fixed
613 // components that are in the way to the right. This tends to give
614 // good results in the common cases of a forgotten vendor component
615 // or a wrongly positioned environment.
617 // Insert left, pushing the existing components to the right. For
618 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
619 StringRef CurrentComponent(""); // The empty component.
620 // Replace the component we are moving with an empty component.
621 std::swap(CurrentComponent, Components[Idx]);
622 // Insert the component being moved at Pos, displacing any existing
623 // components to the right.
624 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
625 // Skip over any fixed components.
626 while (i < array_lengthof(Found) && Found[i])
628 // Place the component at the new position, getting the component
629 // that was at this position - it will be moved right.
630 std::swap(CurrentComponent, Components[i]);
632 } else if (Pos > Idx) {
633 // Push right by inserting empty components until the component at Idx
634 // reaches the target position Pos. For example, pc-a -> -pc-a when
635 // moving pc to the second position.
637 // Insert one empty component at Idx.
638 StringRef CurrentComponent(""); // The empty component.
639 for (unsigned i = Idx; i < Components.size();) {
640 // Place the component at the new position, getting the component
641 // that was at this position - it will be moved right.
642 std::swap(CurrentComponent, Components[i]);
643 // If it was placed on top of an empty component then we are done.
644 if (CurrentComponent.empty())
646 // Advance to the next component, skipping any fixed components.
647 while (++i < array_lengthof(Found) && Found[i])
650 // The last component was pushed off the end - append it.
651 if (!CurrentComponent.empty())
652 Components.push_back(CurrentComponent);
654 // Advance Idx to the component's new position.
655 while (++Idx < array_lengthof(Found) && Found[Idx])
657 } while (Idx < Pos); // Add more until the final position is reached.
659 assert(Pos < Components.size() && Components[Pos] == Comp &&
660 "Component moved wrong!");
666 // Special case logic goes here. At this point Arch, Vendor and OS have the
667 // correct values for the computed components.
669 if (OS == Triple::Win32) {
670 Components.resize(4);
671 Components[2] = "windows";
672 if (Environment == UnknownEnvironment) {
673 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
674 Components[3] = "msvc";
676 Components[3] = getObjectFormatTypeName(ObjectFormat);
678 } else if (IsMinGW32) {
679 Components.resize(4);
680 Components[2] = "windows";
681 Components[3] = "gnu";
682 } else if (IsCygwin) {
683 Components.resize(4);
684 Components[2] = "windows";
685 Components[3] = "cygnus";
687 if (IsMinGW32 || IsCygwin ||
688 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
689 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
690 Components.resize(5);
691 Components[4] = getObjectFormatTypeName(ObjectFormat);
695 // Stick the corrected components back together to form the normalized string.
696 std::string Normalized;
697 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
698 if (i) Normalized += '-';
699 Normalized += Components[i];
704 StringRef Triple::getArchName() const {
705 return StringRef(Data).split('-').first; // Isolate first component
708 StringRef Triple::getVendorName() const {
709 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
710 return Tmp.split('-').first; // Isolate second component
713 StringRef Triple::getOSName() const {
714 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
715 Tmp = Tmp.split('-').second; // Strip second component
716 return Tmp.split('-').first; // Isolate third component
719 StringRef Triple::getEnvironmentName() const {
720 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
721 Tmp = Tmp.split('-').second; // Strip second component
722 return Tmp.split('-').second; // Strip third component
725 StringRef Triple::getOSAndEnvironmentName() const {
726 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
727 return Tmp.split('-').second; // Strip second component
730 static unsigned EatNumber(StringRef &Str) {
731 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
735 // Consume the leading digit.
736 Result = Result*10 + (Str[0] - '0');
740 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
745 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
746 unsigned &Micro) const {
747 StringRef OSName = getOSName();
749 // For Android, we care about the Android version rather than the Linux
751 if (getEnvironment() == Android) {
752 OSName = getEnvironmentName().substr(strlen("android"));
753 if (OSName.startswith("eabi"))
754 OSName = OSName.substr(strlen("eabi"));
757 // Assume that the OS portion of the triple starts with the canonical name.
758 StringRef OSTypeName = getOSTypeName(getOS());
759 if (OSName.startswith(OSTypeName))
760 OSName = OSName.substr(OSTypeName.size());
762 // Any unset version defaults to 0.
763 Major = Minor = Micro = 0;
765 // Parse up to three components.
766 unsigned *Components[3] = { &Major, &Minor, &Micro };
767 for (unsigned i = 0; i != 3; ++i) {
768 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
771 // Consume the leading number.
772 *Components[i] = EatNumber(OSName);
774 // Consume the separator, if present.
775 if (OSName.startswith("."))
776 OSName = OSName.substr(1);
780 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
781 unsigned &Micro) const {
782 getOSVersion(Major, Minor, Micro);
785 default: llvm_unreachable("unexpected OS for Darwin triple");
787 // Default to darwin8, i.e., MacOSX 10.4.
790 // Darwin version numbers are skewed from OS X versions.
807 // Ignore the version from the triple. This is only handled because the
808 // the clang driver combines OS X and IOS support into a common Darwin
809 // toolchain that wants to know the OS X version number even when targeting
819 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
820 unsigned &Micro) const {
822 default: llvm_unreachable("unexpected OS for Darwin triple");
825 // Ignore the version from the triple. This is only handled because the
826 // the clang driver combines OS X and IOS support into a common Darwin
827 // toolchain that wants to know the iOS version number even when targeting
834 getOSVersion(Major, Minor, Micro);
835 // Default to 5.0 (or 7.0 for arm64).
837 Major = (getArch() == aarch64) ? 7 : 5;
842 void Triple::setTriple(const Twine &Str) {
846 void Triple::setArch(ArchType Kind) {
847 setArchName(getArchTypeName(Kind));
850 void Triple::setVendor(VendorType Kind) {
851 setVendorName(getVendorTypeName(Kind));
854 void Triple::setOS(OSType Kind) {
855 setOSName(getOSTypeName(Kind));
858 void Triple::setEnvironment(EnvironmentType Kind) {
859 if (ObjectFormat == getDefaultFormat(*this))
860 return setEnvironmentName(getEnvironmentTypeName(Kind));
862 setEnvironmentName((getEnvironmentTypeName(Kind) + Twine("-") +
863 getObjectFormatTypeName(ObjectFormat)).str());
866 void Triple::setObjectFormat(ObjectFormatType Kind) {
867 if (Environment == UnknownEnvironment)
868 return setEnvironmentName(getObjectFormatTypeName(Kind));
870 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
871 getObjectFormatTypeName(Kind)).str());
874 void Triple::setArchName(StringRef Str) {
875 // Work around a miscompilation bug for Twines in gcc 4.0.3.
876 SmallString<64> Triple;
879 Triple += getVendorName();
881 Triple += getOSAndEnvironmentName();
885 void Triple::setVendorName(StringRef Str) {
886 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
889 void Triple::setOSName(StringRef Str) {
890 if (hasEnvironment())
891 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
892 "-" + getEnvironmentName());
894 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
897 void Triple::setEnvironmentName(StringRef Str) {
898 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
902 void Triple::setOSAndEnvironmentName(StringRef Str) {
903 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
906 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
908 case llvm::Triple::UnknownArch:
911 case llvm::Triple::msp430:
914 case llvm::Triple::arm:
915 case llvm::Triple::armeb:
916 case llvm::Triple::hexagon:
917 case llvm::Triple::le32:
918 case llvm::Triple::mips:
919 case llvm::Triple::mipsel:
920 case llvm::Triple::nvptx:
921 case llvm::Triple::ppc:
922 case llvm::Triple::r600:
923 case llvm::Triple::sparc:
924 case llvm::Triple::sparcel:
925 case llvm::Triple::tce:
926 case llvm::Triple::thumb:
927 case llvm::Triple::thumbeb:
928 case llvm::Triple::x86:
929 case llvm::Triple::xcore:
930 case llvm::Triple::amdil:
931 case llvm::Triple::hsail:
932 case llvm::Triple::spir:
933 case llvm::Triple::kalimba:
936 case llvm::Triple::aarch64:
937 case llvm::Triple::aarch64_be:
938 case llvm::Triple::amdgcn:
939 case llvm::Triple::bpf:
940 case llvm::Triple::le64:
941 case llvm::Triple::mips64:
942 case llvm::Triple::mips64el:
943 case llvm::Triple::nvptx64:
944 case llvm::Triple::ppc64:
945 case llvm::Triple::ppc64le:
946 case llvm::Triple::sparcv9:
947 case llvm::Triple::systemz:
948 case llvm::Triple::x86_64:
949 case llvm::Triple::amdil64:
950 case llvm::Triple::hsail64:
951 case llvm::Triple::spir64:
954 llvm_unreachable("Invalid architecture value");
957 bool Triple::isArch64Bit() const {
958 return getArchPointerBitWidth(getArch()) == 64;
961 bool Triple::isArch32Bit() const {
962 return getArchPointerBitWidth(getArch()) == 32;
965 bool Triple::isArch16Bit() const {
966 return getArchPointerBitWidth(getArch()) == 16;
969 Triple Triple::get32BitArchVariant() const {
972 case Triple::UnknownArch:
973 case Triple::aarch64:
974 case Triple::aarch64_be:
978 case Triple::systemz:
979 case Triple::ppc64le:
980 T.setArch(UnknownArch);
988 case Triple::hexagon:
989 case Triple::kalimba:
997 case Triple::sparcel:
1000 case Triple::thumbeb:
1006 case Triple::le64: T.setArch(Triple::le32); break;
1007 case Triple::mips64: T.setArch(Triple::mips); break;
1008 case Triple::mips64el: T.setArch(Triple::mipsel); break;
1009 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
1010 case Triple::ppc64: T.setArch(Triple::ppc); break;
1011 case Triple::sparcv9: T.setArch(Triple::sparc); break;
1012 case Triple::x86_64: T.setArch(Triple::x86); break;
1013 case Triple::amdil64: T.setArch(Triple::amdil); break;
1014 case Triple::hsail64: T.setArch(Triple::hsail); break;
1015 case Triple::spir64: T.setArch(Triple::spir); break;
1020 Triple Triple::get64BitArchVariant() const {
1022 switch (getArch()) {
1023 case Triple::UnknownArch:
1026 case Triple::hexagon:
1027 case Triple::kalimba:
1028 case Triple::msp430:
1032 case Triple::thumbeb:
1034 case Triple::sparcel:
1035 T.setArch(UnknownArch);
1038 case Triple::aarch64:
1039 case Triple::aarch64_be:
1042 case Triple::amdil64:
1043 case Triple::amdgcn:
1044 case Triple::hsail64:
1045 case Triple::spir64:
1046 case Triple::mips64:
1047 case Triple::mips64el:
1048 case Triple::nvptx64:
1050 case Triple::ppc64le:
1051 case Triple::sparcv9:
1052 case Triple::systemz:
1053 case Triple::x86_64:
1057 case Triple::le32: T.setArch(Triple::le64); break;
1058 case Triple::mips: T.setArch(Triple::mips64); break;
1059 case Triple::mipsel: T.setArch(Triple::mips64el); break;
1060 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
1061 case Triple::ppc: T.setArch(Triple::ppc64); break;
1062 case Triple::sparc: T.setArch(Triple::sparcv9); break;
1063 case Triple::x86: T.setArch(Triple::x86_64); break;
1064 case Triple::amdil: T.setArch(Triple::amdil64); break;
1065 case Triple::hsail: T.setArch(Triple::hsail64); break;
1066 case Triple::spir: T.setArch(Triple::spir64); break;
1071 // FIXME: tblgen this.
1072 const char *Triple::getARMCPUForArch(StringRef MArch) const {
1074 MArch = getArchName();
1077 case llvm::Triple::FreeBSD:
1078 case llvm::Triple::NetBSD:
1079 if (MArch == "armv6")
1080 return "arm1176jzf-s";
1082 case llvm::Triple::Win32:
1083 // FIXME: this is invalid for WindowsCE
1089 const char *result = nullptr;
1090 size_t offset = StringRef::npos;
1091 if (MArch.startswith("arm"))
1093 if (MArch.startswith("thumb"))
1095 if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
1097 if (MArch.endswith("eb"))
1098 MArch = MArch.substr(0, MArch.size() - 2);
1099 if (offset != StringRef::npos)
1100 result = llvm::StringSwitch<const char *>(MArch.substr(offset))
1101 .Cases("v2", "v2a", "arm2")
1103 .Case("v3m", "arm7m")
1104 .Case("v4", "strongarm")
1105 .Case("v4t", "arm7tdmi")
1106 .Cases("v5", "v5t", "arm10tdmi")
1107 .Cases("v5e", "v5te", "arm1022e")
1108 .Case("v5tej", "arm926ej-s")
1109 .Case("v6", "arm1136jf-s")
1110 .Case("v6j", "arm1136j-s")
1111 .Cases("v6k", "v6z", "v6zk", "arm1176jzf-s")
1112 .Case("v6t2", "arm1156t2-s")
1113 .Cases("v6m", "v6-m", "v6sm", "v6s-m", "cortex-m0")
1114 .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
1115 .Cases("v7s", "v7-s", "swift")
1116 .Cases("v7r", "v7-r", "cortex-r4")
1117 .Cases("v7m", "v7-m", "cortex-m3")
1118 .Cases("v7em", "v7e-m", "cortex-m4")
1119 .Cases("v8", "v8a", "v8-a", "cortex-a53")
1120 .Cases("v8.1a", "v8.1-a", "generic")
1123 result = llvm::StringSwitch<const char *>(MArch)
1124 .Case("ep9312", "ep9312")
1125 .Case("iwmmxt", "iwmmxt")
1126 .Case("xscale", "xscale")
1132 // If all else failed, return the most base CPU with thumb interworking
1133 // supported by LLVM.
1134 // FIXME: Should warn once that we're falling back.
1136 case llvm::Triple::NetBSD:
1137 switch (getEnvironment()) {
1138 case llvm::Triple::GNUEABIHF:
1139 case llvm::Triple::GNUEABI:
1140 case llvm::Triple::EABIHF:
1141 case llvm::Triple::EABI:
1142 return "arm926ej-s";
1146 case llvm::Triple::NaCl:
1149 switch (getEnvironment()) {
1150 case llvm::Triple::EABIHF:
1151 case llvm::Triple::GNUEABIHF:
1152 return "arm1176jzf-s";