• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- Triple.cpp - Target triple helper class --------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
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"
15 #include "llvm/Support/TargetParser.h"
16 #include "llvm/Support/Host.h"
17 #include <cstring>
18 using namespace llvm;
19 
getArchTypeName(ArchType Kind)20 const char *Triple::getArchTypeName(ArchType Kind) {
21   switch (Kind) {
22   case UnknownArch: return "unknown";
23 
24   case aarch64:     return "aarch64";
25   case aarch64_be:  return "aarch64_be";
26   case arm:         return "arm";
27   case armeb:       return "armeb";
28   case avr:         return "avr";
29   case bpfel:       return "bpfel";
30   case bpfeb:       return "bpfeb";
31   case hexagon:     return "hexagon";
32   case mips:        return "mips";
33   case mipsel:      return "mipsel";
34   case mips64:      return "mips64";
35   case mips64el:    return "mips64el";
36   case msp430:      return "msp430";
37   case ppc64:       return "powerpc64";
38   case ppc64le:     return "powerpc64le";
39   case ppc:         return "powerpc";
40   case r600:        return "r600";
41   case amdgcn:      return "amdgcn";
42   case sparc:       return "sparc";
43   case sparcv9:     return "sparcv9";
44   case sparcel:     return "sparcel";
45   case systemz:     return "s390x";
46   case tce:         return "tce";
47   case thumb:       return "thumb";
48   case thumbeb:     return "thumbeb";
49   case x86:         return "i386";
50   case x86_64:      return "x86_64";
51   case xcore:       return "xcore";
52   case nvptx:       return "nvptx";
53   case nvptx64:     return "nvptx64";
54   case le32:        return "le32";
55   case le64:        return "le64";
56   case amdil:       return "amdil";
57   case amdil64:     return "amdil64";
58   case hsail:       return "hsail";
59   case hsail64:     return "hsail64";
60   case spir:        return "spir";
61   case spir64:      return "spir64";
62   case kalimba:     return "kalimba";
63   case shave:       return "shave";
64   case wasm32:      return "wasm32";
65   case wasm64:      return "wasm64";
66   }
67 
68   llvm_unreachable("Invalid ArchType!");
69 }
70 
getArchTypePrefix(ArchType Kind)71 const char *Triple::getArchTypePrefix(ArchType Kind) {
72   switch (Kind) {
73   default:
74     return nullptr;
75 
76   case aarch64:
77   case aarch64_be:  return "aarch64";
78 
79   case arm:
80   case armeb:
81   case thumb:
82   case thumbeb:     return "arm";
83 
84   case avr:         return "avr";
85 
86   case ppc64:
87   case ppc64le:
88   case ppc:         return "ppc";
89 
90   case mips:
91   case mipsel:
92   case mips64:
93   case mips64el:    return "mips";
94 
95   case hexagon:     return "hexagon";
96 
97   case amdgcn:
98   case r600:        return "amdgpu";
99 
100   case bpfel:
101   case bpfeb:       return "bpf";
102 
103   case sparcv9:
104   case sparcel:
105   case sparc:       return "sparc";
106 
107   case systemz:     return "s390";
108 
109   case x86:
110   case x86_64:      return "x86";
111 
112   case xcore:       return "xcore";
113 
114   case nvptx:       return "nvptx";
115   case nvptx64:     return "nvptx";
116 
117   case le32:        return "le32";
118   case le64:        return "le64";
119 
120   case amdil:
121   case amdil64:     return "amdil";
122 
123   case hsail:
124   case hsail64:     return "hsail";
125 
126   case spir:
127   case spir64:      return "spir";
128   case kalimba:     return "kalimba";
129   case shave:       return "shave";
130   case wasm32:
131   case wasm64:      return "wasm";
132   }
133 }
134 
getVendorTypeName(VendorType Kind)135 const char *Triple::getVendorTypeName(VendorType Kind) {
136   switch (Kind) {
137   case UnknownVendor: return "unknown";
138 
139   case Apple: return "apple";
140   case PC: return "pc";
141   case SCEI: return "scei";
142   case BGP: return "bgp";
143   case BGQ: return "bgq";
144   case Freescale: return "fsl";
145   case IBM: return "ibm";
146   case ImaginationTechnologies: return "img";
147   case MipsTechnologies: return "mti";
148   case NVIDIA: return "nvidia";
149   case CSR: return "csr";
150   case Myriad: return "myriad";
151   }
152 
153   llvm_unreachable("Invalid VendorType!");
154 }
155 
getOSTypeName(OSType Kind)156 const char *Triple::getOSTypeName(OSType Kind) {
157   switch (Kind) {
158   case UnknownOS: return "unknown";
159 
160   case CloudABI: return "cloudabi";
161   case Darwin: return "darwin";
162   case DragonFly: return "dragonfly";
163   case FreeBSD: return "freebsd";
164   case IOS: return "ios";
165   case KFreeBSD: return "kfreebsd";
166   case Linux: return "linux";
167   case Lv2: return "lv2";
168   case MacOSX: return "macosx";
169   case NetBSD: return "netbsd";
170   case OpenBSD: return "openbsd";
171   case Solaris: return "solaris";
172   case Win32: return "windows";
173   case Haiku: return "haiku";
174   case Minix: return "minix";
175   case RTEMS: return "rtems";
176   case NaCl: return "nacl";
177   case CNK: return "cnk";
178   case Bitrig: return "bitrig";
179   case AIX: return "aix";
180   case CUDA: return "cuda";
181   case NVCL: return "nvcl";
182   case AMDHSA: return "amdhsa";
183   case PS4: return "ps4";
184   case ELFIAMCU: return "elfiamcu";
185   case TvOS: return "tvos";
186   case WatchOS: return "watchos";
187   }
188 
189   llvm_unreachable("Invalid OSType");
190 }
191 
getEnvironmentTypeName(EnvironmentType Kind)192 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
193   switch (Kind) {
194   case UnknownEnvironment: return "unknown";
195   case GNU: return "gnu";
196   case GNUEABIHF: return "gnueabihf";
197   case GNUEABI: return "gnueabi";
198   case GNUX32: return "gnux32";
199   case CODE16: return "code16";
200   case EABI: return "eabi";
201   case EABIHF: return "eabihf";
202   case Android: return "android";
203   case MSVC: return "msvc";
204   case Itanium: return "itanium";
205   case Cygnus: return "cygnus";
206   case AMDOpenCL: return "amdopencl";
207   case CoreCLR: return "coreclr";
208   }
209 
210   llvm_unreachable("Invalid EnvironmentType!");
211 }
212 
parseBPFArch(StringRef ArchName)213 static Triple::ArchType parseBPFArch(StringRef ArchName) {
214   if (ArchName.equals("bpf")) {
215     if (sys::IsLittleEndianHost)
216       return Triple::bpfel;
217     else
218       return Triple::bpfeb;
219   } else if (ArchName.equals("bpf_be") || ArchName.equals("bpfeb")) {
220     return Triple::bpfeb;
221   } else if (ArchName.equals("bpf_le") || ArchName.equals("bpfel")) {
222     return Triple::bpfel;
223   } else {
224     return Triple::UnknownArch;
225   }
226 }
227 
getArchTypeForLLVMName(StringRef Name)228 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
229   Triple::ArchType BPFArch(parseBPFArch(Name));
230   return StringSwitch<Triple::ArchType>(Name)
231     .Case("aarch64", aarch64)
232     .Case("aarch64_be", aarch64_be)
233     .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
234     .Case("arm", arm)
235     .Case("armeb", armeb)
236     .Case("avr", avr)
237     .StartsWith("bpf", BPFArch)
238     .Case("mips", mips)
239     .Case("mipsel", mipsel)
240     .Case("mips64", mips64)
241     .Case("mips64el", mips64el)
242     .Case("msp430", msp430)
243     .Case("ppc64", ppc64)
244     .Case("ppc32", ppc)
245     .Case("ppc", ppc)
246     .Case("ppc64le", ppc64le)
247     .Case("r600", r600)
248     .Case("amdgcn", amdgcn)
249     .Case("hexagon", hexagon)
250     .Case("sparc", sparc)
251     .Case("sparcel", sparcel)
252     .Case("sparcv9", sparcv9)
253     .Case("systemz", systemz)
254     .Case("tce", tce)
255     .Case("thumb", thumb)
256     .Case("thumbeb", thumbeb)
257     .Case("x86", x86)
258     .Case("x86-64", x86_64)
259     .Case("xcore", xcore)
260     .Case("nvptx", nvptx)
261     .Case("nvptx64", nvptx64)
262     .Case("le32", le32)
263     .Case("le64", le64)
264     .Case("amdil", amdil)
265     .Case("amdil64", amdil64)
266     .Case("hsail", hsail)
267     .Case("hsail64", hsail64)
268     .Case("spir", spir)
269     .Case("spir64", spir64)
270     .Case("kalimba", kalimba)
271     .Case("shave", shave)
272     .Case("wasm32", wasm32)
273     .Case("wasm64", wasm64)
274     .Default(UnknownArch);
275 }
276 
parseARMArch(StringRef ArchName)277 static Triple::ArchType parseARMArch(StringRef ArchName) {
278   unsigned ISA = ARM::parseArchISA(ArchName);
279   unsigned ENDIAN = ARM::parseArchEndian(ArchName);
280 
281   Triple::ArchType arch = Triple::UnknownArch;
282   switch (ENDIAN) {
283   case ARM::EK_LITTLE: {
284     switch (ISA) {
285     case ARM::IK_ARM:
286       arch = Triple::arm;
287       break;
288     case ARM::IK_THUMB:
289       arch = Triple::thumb;
290       break;
291     case ARM::IK_AARCH64:
292       arch = Triple::aarch64;
293       break;
294     }
295     break;
296   }
297   case ARM::EK_BIG: {
298     switch (ISA) {
299     case ARM::IK_ARM:
300       arch = Triple::armeb;
301       break;
302     case ARM::IK_THUMB:
303       arch = Triple::thumbeb;
304       break;
305     case ARM::IK_AARCH64:
306       arch = Triple::aarch64_be;
307       break;
308     }
309     break;
310   }
311   }
312 
313   ArchName = ARM::getCanonicalArchName(ArchName);
314   if (ArchName.empty())
315     return Triple::UnknownArch;
316 
317   // Thumb only exists in v4+
318   if (ISA == ARM::IK_THUMB &&
319       (ArchName.startswith("v2") || ArchName.startswith("v3")))
320     return Triple::UnknownArch;
321 
322   // Thumb only for v6m
323   unsigned Profile = ARM::parseArchProfile(ArchName);
324   unsigned Version = ARM::parseArchVersion(ArchName);
325   if (Profile == ARM::PK_M && Version == 6) {
326     if (ENDIAN == ARM::EK_BIG)
327       return Triple::thumbeb;
328     else
329       return Triple::thumb;
330   }
331 
332   return arch;
333 }
334 
parseArch(StringRef ArchName)335 static Triple::ArchType parseArch(StringRef ArchName) {
336   auto AT = StringSwitch<Triple::ArchType>(ArchName)
337     .Cases("i386", "i486", "i586", "i686", Triple::x86)
338     // FIXME: Do we need to support these?
339     .Cases("i786", "i886", "i986", Triple::x86)
340     .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
341     .Case("powerpc", Triple::ppc)
342     .Cases("powerpc64", "ppu", Triple::ppc64)
343     .Case("powerpc64le", Triple::ppc64le)
344     .Case("xscale", Triple::arm)
345     .Case("xscaleeb", Triple::armeb)
346     .Case("aarch64", Triple::aarch64)
347     .Case("aarch64_be", Triple::aarch64_be)
348     .Case("arm64", Triple::aarch64)
349     .Case("arm", Triple::arm)
350     .Case("armeb", Triple::armeb)
351     .Case("thumb", Triple::thumb)
352     .Case("thumbeb", Triple::thumbeb)
353     .Case("avr", Triple::avr)
354     .Case("msp430", Triple::msp430)
355     .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
356     .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
357     .Cases("mips64", "mips64eb", Triple::mips64)
358     .Case("mips64el", Triple::mips64el)
359     .Case("r600", Triple::r600)
360     .Case("amdgcn", Triple::amdgcn)
361     .Case("hexagon", Triple::hexagon)
362     .Case("s390x", Triple::systemz)
363     .Case("sparc", Triple::sparc)
364     .Case("sparcel", Triple::sparcel)
365     .Cases("sparcv9", "sparc64", Triple::sparcv9)
366     .Case("tce", Triple::tce)
367     .Case("xcore", Triple::xcore)
368     .Case("nvptx", Triple::nvptx)
369     .Case("nvptx64", Triple::nvptx64)
370     .Case("le32", Triple::le32)
371     .Case("le64", Triple::le64)
372     .Case("amdil", Triple::amdil)
373     .Case("amdil64", Triple::amdil64)
374     .Case("hsail", Triple::hsail)
375     .Case("hsail64", Triple::hsail64)
376     .Case("spir", Triple::spir)
377     .Case("spir64", Triple::spir64)
378     .StartsWith("kalimba", Triple::kalimba)
379     .Case("shave", Triple::shave)
380     .Case("wasm32", Triple::wasm32)
381     .Case("wasm64", Triple::wasm64)
382     .Default(Triple::UnknownArch);
383 
384   // Some architectures require special parsing logic just to compute the
385   // ArchType result.
386   if (AT == Triple::UnknownArch) {
387     if (ArchName.startswith("arm") || ArchName.startswith("thumb") ||
388         ArchName.startswith("aarch64"))
389       return parseARMArch(ArchName);
390     if (ArchName.startswith("bpf"))
391       return parseBPFArch(ArchName);
392   }
393 
394   return AT;
395 }
396 
parseVendor(StringRef VendorName)397 static Triple::VendorType parseVendor(StringRef VendorName) {
398   return StringSwitch<Triple::VendorType>(VendorName)
399     .Case("apple", Triple::Apple)
400     .Case("pc", Triple::PC)
401     .Case("scei", Triple::SCEI)
402     .Case("bgp", Triple::BGP)
403     .Case("bgq", Triple::BGQ)
404     .Case("fsl", Triple::Freescale)
405     .Case("ibm", Triple::IBM)
406     .Case("img", Triple::ImaginationTechnologies)
407     .Case("mti", Triple::MipsTechnologies)
408     .Case("nvidia", Triple::NVIDIA)
409     .Case("csr", Triple::CSR)
410     .Case("myriad", Triple::Myriad)
411     .Default(Triple::UnknownVendor);
412 }
413 
parseOS(StringRef OSName)414 static Triple::OSType parseOS(StringRef OSName) {
415   return StringSwitch<Triple::OSType>(OSName)
416     .StartsWith("cloudabi", Triple::CloudABI)
417     .StartsWith("darwin", Triple::Darwin)
418     .StartsWith("dragonfly", Triple::DragonFly)
419     .StartsWith("freebsd", Triple::FreeBSD)
420     .StartsWith("ios", Triple::IOS)
421     .StartsWith("kfreebsd", Triple::KFreeBSD)
422     .StartsWith("linux", Triple::Linux)
423     .StartsWith("lv2", Triple::Lv2)
424     .StartsWith("macosx", Triple::MacOSX)
425     .StartsWith("netbsd", Triple::NetBSD)
426     .StartsWith("openbsd", Triple::OpenBSD)
427     .StartsWith("solaris", Triple::Solaris)
428     .StartsWith("win32", Triple::Win32)
429     .StartsWith("windows", Triple::Win32)
430     .StartsWith("haiku", Triple::Haiku)
431     .StartsWith("minix", Triple::Minix)
432     .StartsWith("rtems", Triple::RTEMS)
433     .StartsWith("nacl", Triple::NaCl)
434     .StartsWith("cnk", Triple::CNK)
435     .StartsWith("bitrig", Triple::Bitrig)
436     .StartsWith("aix", Triple::AIX)
437     .StartsWith("cuda", Triple::CUDA)
438     .StartsWith("nvcl", Triple::NVCL)
439     .StartsWith("amdhsa", Triple::AMDHSA)
440     .StartsWith("ps4", Triple::PS4)
441     .StartsWith("elfiamcu", Triple::ELFIAMCU)
442     .StartsWith("tvos", Triple::TvOS)
443     .StartsWith("watchos", Triple::WatchOS)
444     .Default(Triple::UnknownOS);
445 }
446 
parseEnvironment(StringRef EnvironmentName)447 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
448   return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
449     .StartsWith("eabihf", Triple::EABIHF)
450     .StartsWith("eabi", Triple::EABI)
451     .StartsWith("gnueabihf", Triple::GNUEABIHF)
452     .StartsWith("gnueabi", Triple::GNUEABI)
453     .StartsWith("gnux32", Triple::GNUX32)
454     .StartsWith("code16", Triple::CODE16)
455     .StartsWith("gnu", Triple::GNU)
456     .StartsWith("android", Triple::Android)
457     .StartsWith("msvc", Triple::MSVC)
458     .StartsWith("itanium", Triple::Itanium)
459     .StartsWith("cygnus", Triple::Cygnus)
460     .StartsWith("amdopencl", Triple::AMDOpenCL)
461     .StartsWith("coreclr", Triple::CoreCLR)
462     .Default(Triple::UnknownEnvironment);
463 }
464 
parseFormat(StringRef EnvironmentName)465 static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
466   return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
467     .EndsWith("coff", Triple::COFF)
468     .EndsWith("elf", Triple::ELF)
469     .EndsWith("macho", Triple::MachO)
470     .Default(Triple::UnknownObjectFormat);
471 }
472 
parseSubArch(StringRef SubArchName)473 static Triple::SubArchType parseSubArch(StringRef SubArchName) {
474   StringRef ARMSubArch = ARM::getCanonicalArchName(SubArchName);
475 
476   // For now, this is the small part. Early return.
477   if (ARMSubArch.empty())
478     return StringSwitch<Triple::SubArchType>(SubArchName)
479       .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
480       .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
481       .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
482       .Default(Triple::NoSubArch);
483 
484   // ARM sub arch.
485   switch(ARM::parseArch(ARMSubArch)) {
486   case ARM::AK_ARMV4:
487     return Triple::NoSubArch;
488   case ARM::AK_ARMV4T:
489     return Triple::ARMSubArch_v4t;
490   case ARM::AK_ARMV5T:
491     return Triple::ARMSubArch_v5;
492   case ARM::AK_ARMV5TE:
493   case ARM::AK_IWMMXT:
494   case ARM::AK_IWMMXT2:
495   case ARM::AK_XSCALE:
496   case ARM::AK_ARMV5TEJ:
497     return Triple::ARMSubArch_v5te;
498   case ARM::AK_ARMV6:
499     return Triple::ARMSubArch_v6;
500   case ARM::AK_ARMV6K:
501   case ARM::AK_ARMV6KZ:
502     return Triple::ARMSubArch_v6k;
503   case ARM::AK_ARMV6T2:
504     return Triple::ARMSubArch_v6t2;
505   case ARM::AK_ARMV6M:
506     return Triple::ARMSubArch_v6m;
507   case ARM::AK_ARMV7A:
508   case ARM::AK_ARMV7R:
509     return Triple::ARMSubArch_v7;
510   case ARM::AK_ARMV7K:
511     return Triple::ARMSubArch_v7k;
512   case ARM::AK_ARMV7M:
513     return Triple::ARMSubArch_v7m;
514   case ARM::AK_ARMV7S:
515     return Triple::ARMSubArch_v7s;
516   case ARM::AK_ARMV7EM:
517     return Triple::ARMSubArch_v7em;
518   case ARM::AK_ARMV8A:
519     return Triple::ARMSubArch_v8;
520   case ARM::AK_ARMV8_1A:
521     return Triple::ARMSubArch_v8_1a;
522   case ARM::AK_ARMV8_2A:
523     return Triple::ARMSubArch_v8_2a;
524   default:
525     return Triple::NoSubArch;
526   }
527 }
528 
getObjectFormatTypeName(Triple::ObjectFormatType Kind)529 static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
530   switch (Kind) {
531   case Triple::UnknownObjectFormat: return "";
532   case Triple::COFF: return "coff";
533   case Triple::ELF: return "elf";
534   case Triple::MachO: return "macho";
535   }
536   llvm_unreachable("unknown object format type");
537 }
538 
getDefaultFormat(const Triple & T)539 static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
540   switch (T.getArch()) {
541   case Triple::UnknownArch:
542   case Triple::aarch64:
543   case Triple::arm:
544   case Triple::thumb:
545   case Triple::x86:
546   case Triple::x86_64:
547     if (T.isOSDarwin())
548       return Triple::MachO;
549     else if (T.isOSWindows())
550       return Triple::COFF;
551     return Triple::ELF;
552 
553   case Triple::aarch64_be:
554   case Triple::amdgcn:
555   case Triple::amdil:
556   case Triple::amdil64:
557   case Triple::armeb:
558   case Triple::avr:
559   case Triple::bpfeb:
560   case Triple::bpfel:
561   case Triple::hexagon:
562   case Triple::hsail:
563   case Triple::hsail64:
564   case Triple::kalimba:
565   case Triple::le32:
566   case Triple::le64:
567   case Triple::mips:
568   case Triple::mips64:
569   case Triple::mips64el:
570   case Triple::mipsel:
571   case Triple::msp430:
572   case Triple::nvptx:
573   case Triple::nvptx64:
574   case Triple::ppc64le:
575   case Triple::r600:
576   case Triple::shave:
577   case Triple::sparc:
578   case Triple::sparcel:
579   case Triple::sparcv9:
580   case Triple::spir:
581   case Triple::spir64:
582   case Triple::systemz:
583   case Triple::tce:
584   case Triple::thumbeb:
585   case Triple::wasm32:
586   case Triple::wasm64:
587   case Triple::xcore:
588     return Triple::ELF;
589 
590   case Triple::ppc:
591   case Triple::ppc64:
592     if (T.isOSDarwin())
593       return Triple::MachO;
594     return Triple::ELF;
595   }
596   llvm_unreachable("unknown architecture");
597 }
598 
599 /// \brief Construct a triple from the string representation provided.
600 ///
601 /// This stores the string representation and parses the various pieces into
602 /// enum members.
Triple(const Twine & Str)603 Triple::Triple(const Twine &Str)
604     : Data(Str.str()), Arch(UnknownArch), SubArch(NoSubArch),
605       Vendor(UnknownVendor), OS(UnknownOS), Environment(UnknownEnvironment),
606       ObjectFormat(UnknownObjectFormat) {
607   // Do minimal parsing by hand here.
608   SmallVector<StringRef, 4> Components;
609   StringRef(Data).split(Components, '-', /*MaxSplit*/ 3);
610   if (Components.size() > 0) {
611     Arch = parseArch(Components[0]);
612     SubArch = parseSubArch(Components[0]);
613     if (Components.size() > 1) {
614       Vendor = parseVendor(Components[1]);
615       if (Components.size() > 2) {
616         OS = parseOS(Components[2]);
617         if (Components.size() > 3) {
618           Environment = parseEnvironment(Components[3]);
619           ObjectFormat = parseFormat(Components[3]);
620         }
621       }
622     }
623   }
624   if (ObjectFormat == UnknownObjectFormat)
625     ObjectFormat = getDefaultFormat(*this);
626 }
627 
628 /// \brief Construct a triple from string representations of the architecture,
629 /// vendor, and OS.
630 ///
631 /// This joins each argument into a canonical string representation and parses
632 /// them into enum members. It leaves the environment unknown and omits it from
633 /// the string representation.
Triple(const Twine & ArchStr,const Twine & VendorStr,const Twine & OSStr)634 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
635     : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
636       Arch(parseArch(ArchStr.str())),
637       SubArch(parseSubArch(ArchStr.str())),
638       Vendor(parseVendor(VendorStr.str())),
639       OS(parseOS(OSStr.str())),
640       Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
641   ObjectFormat = getDefaultFormat(*this);
642 }
643 
644 /// \brief Construct a triple from string representations of the architecture,
645 /// vendor, OS, and environment.
646 ///
647 /// This joins each argument into a canonical string representation and parses
648 /// them into enum members.
Triple(const Twine & ArchStr,const Twine & VendorStr,const Twine & OSStr,const Twine & EnvironmentStr)649 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
650                const Twine &EnvironmentStr)
651     : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
652             EnvironmentStr).str()),
653       Arch(parseArch(ArchStr.str())),
654       SubArch(parseSubArch(ArchStr.str())),
655       Vendor(parseVendor(VendorStr.str())),
656       OS(parseOS(OSStr.str())),
657       Environment(parseEnvironment(EnvironmentStr.str())),
658       ObjectFormat(parseFormat(EnvironmentStr.str())) {
659   if (ObjectFormat == Triple::UnknownObjectFormat)
660     ObjectFormat = getDefaultFormat(*this);
661 }
662 
normalize(StringRef Str)663 std::string Triple::normalize(StringRef Str) {
664   bool IsMinGW32 = false;
665   bool IsCygwin = false;
666 
667   // Parse into components.
668   SmallVector<StringRef, 4> Components;
669   Str.split(Components, '-');
670 
671   // If the first component corresponds to a known architecture, preferentially
672   // use it for the architecture.  If the second component corresponds to a
673   // known vendor, preferentially use it for the vendor, etc.  This avoids silly
674   // component movement when a component parses as (eg) both a valid arch and a
675   // valid os.
676   ArchType Arch = UnknownArch;
677   if (Components.size() > 0)
678     Arch = parseArch(Components[0]);
679   VendorType Vendor = UnknownVendor;
680   if (Components.size() > 1)
681     Vendor = parseVendor(Components[1]);
682   OSType OS = UnknownOS;
683   if (Components.size() > 2) {
684     OS = parseOS(Components[2]);
685     IsCygwin = Components[2].startswith("cygwin");
686     IsMinGW32 = Components[2].startswith("mingw");
687   }
688   EnvironmentType Environment = UnknownEnvironment;
689   if (Components.size() > 3)
690     Environment = parseEnvironment(Components[3]);
691   ObjectFormatType ObjectFormat = UnknownObjectFormat;
692   if (Components.size() > 4)
693     ObjectFormat = parseFormat(Components[4]);
694 
695   // Note which components are already in their final position.  These will not
696   // be moved.
697   bool Found[4];
698   Found[0] = Arch != UnknownArch;
699   Found[1] = Vendor != UnknownVendor;
700   Found[2] = OS != UnknownOS;
701   Found[3] = Environment != UnknownEnvironment;
702 
703   // If they are not there already, permute the components into their canonical
704   // positions by seeing if they parse as a valid architecture, and if so moving
705   // the component to the architecture position etc.
706   for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
707     if (Found[Pos])
708       continue; // Already in the canonical position.
709 
710     for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
711       // Do not reparse any components that already matched.
712       if (Idx < array_lengthof(Found) && Found[Idx])
713         continue;
714 
715       // Does this component parse as valid for the target position?
716       bool Valid = false;
717       StringRef Comp = Components[Idx];
718       switch (Pos) {
719       default: llvm_unreachable("unexpected component type!");
720       case 0:
721         Arch = parseArch(Comp);
722         Valid = Arch != UnknownArch;
723         break;
724       case 1:
725         Vendor = parseVendor(Comp);
726         Valid = Vendor != UnknownVendor;
727         break;
728       case 2:
729         OS = parseOS(Comp);
730         IsCygwin = Comp.startswith("cygwin");
731         IsMinGW32 = Comp.startswith("mingw");
732         Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
733         break;
734       case 3:
735         Environment = parseEnvironment(Comp);
736         Valid = Environment != UnknownEnvironment;
737         if (!Valid) {
738           ObjectFormat = parseFormat(Comp);
739           Valid = ObjectFormat != UnknownObjectFormat;
740         }
741         break;
742       }
743       if (!Valid)
744         continue; // Nope, try the next component.
745 
746       // Move the component to the target position, pushing any non-fixed
747       // components that are in the way to the right.  This tends to give
748       // good results in the common cases of a forgotten vendor component
749       // or a wrongly positioned environment.
750       if (Pos < Idx) {
751         // Insert left, pushing the existing components to the right.  For
752         // example, a-b-i386 -> i386-a-b when moving i386 to the front.
753         StringRef CurrentComponent(""); // The empty component.
754         // Replace the component we are moving with an empty component.
755         std::swap(CurrentComponent, Components[Idx]);
756         // Insert the component being moved at Pos, displacing any existing
757         // components to the right.
758         for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
759           // Skip over any fixed components.
760           while (i < array_lengthof(Found) && Found[i])
761             ++i;
762           // Place the component at the new position, getting the component
763           // that was at this position - it will be moved right.
764           std::swap(CurrentComponent, Components[i]);
765         }
766       } else if (Pos > Idx) {
767         // Push right by inserting empty components until the component at Idx
768         // reaches the target position Pos.  For example, pc-a -> -pc-a when
769         // moving pc to the second position.
770         do {
771           // Insert one empty component at Idx.
772           StringRef CurrentComponent(""); // The empty component.
773           for (unsigned i = Idx; i < Components.size();) {
774             // Place the component at the new position, getting the component
775             // that was at this position - it will be moved right.
776             std::swap(CurrentComponent, Components[i]);
777             // If it was placed on top of an empty component then we are done.
778             if (CurrentComponent.empty())
779               break;
780             // Advance to the next component, skipping any fixed components.
781             while (++i < array_lengthof(Found) && Found[i])
782               ;
783           }
784           // The last component was pushed off the end - append it.
785           if (!CurrentComponent.empty())
786             Components.push_back(CurrentComponent);
787 
788           // Advance Idx to the component's new position.
789           while (++Idx < array_lengthof(Found) && Found[Idx])
790             ;
791         } while (Idx < Pos); // Add more until the final position is reached.
792       }
793       assert(Pos < Components.size() && Components[Pos] == Comp &&
794              "Component moved wrong!");
795       Found[Pos] = true;
796       break;
797     }
798   }
799 
800   // Special case logic goes here.  At this point Arch, Vendor and OS have the
801   // correct values for the computed components.
802   std::string NormalizedEnvironment;
803   if (Environment == Triple::Android && Components[3].startswith("androideabi")) {
804     StringRef AndroidVersion = Components[3].drop_front(strlen("androideabi"));
805     if (AndroidVersion.empty()) {
806       Components[3] = "android";
807     } else {
808       NormalizedEnvironment = Twine("android", AndroidVersion).str();
809       Components[3] = NormalizedEnvironment;
810     }
811   }
812 
813   if (OS == Triple::Win32) {
814     Components.resize(4);
815     Components[2] = "windows";
816     if (Environment == UnknownEnvironment) {
817       if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
818         Components[3] = "msvc";
819       else
820         Components[3] = getObjectFormatTypeName(ObjectFormat);
821     }
822   } else if (IsMinGW32) {
823     Components.resize(4);
824     Components[2] = "windows";
825     Components[3] = "gnu";
826   } else if (IsCygwin) {
827     Components.resize(4);
828     Components[2] = "windows";
829     Components[3] = "cygnus";
830   }
831   if (IsMinGW32 || IsCygwin ||
832       (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
833     if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
834       Components.resize(5);
835       Components[4] = getObjectFormatTypeName(ObjectFormat);
836     }
837   }
838 
839   // Stick the corrected components back together to form the normalized string.
840   std::string Normalized;
841   for (unsigned i = 0, e = Components.size(); i != e; ++i) {
842     if (i) Normalized += '-';
843     Normalized += Components[i];
844   }
845   return Normalized;
846 }
847 
getArchName() const848 StringRef Triple::getArchName() const {
849   return StringRef(Data).split('-').first;           // Isolate first component
850 }
851 
getVendorName() const852 StringRef Triple::getVendorName() const {
853   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
854   return Tmp.split('-').first;                       // Isolate second component
855 }
856 
getOSName() const857 StringRef Triple::getOSName() const {
858   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
859   Tmp = Tmp.split('-').second;                       // Strip second component
860   return Tmp.split('-').first;                       // Isolate third component
861 }
862 
getEnvironmentName() const863 StringRef Triple::getEnvironmentName() const {
864   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
865   Tmp = Tmp.split('-').second;                       // Strip second component
866   return Tmp.split('-').second;                      // Strip third component
867 }
868 
getOSAndEnvironmentName() const869 StringRef Triple::getOSAndEnvironmentName() const {
870   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
871   return Tmp.split('-').second;                      // Strip second component
872 }
873 
EatNumber(StringRef & Str)874 static unsigned EatNumber(StringRef &Str) {
875   assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
876   unsigned Result = 0;
877 
878   do {
879     // Consume the leading digit.
880     Result = Result*10 + (Str[0] - '0');
881 
882     // Eat the digit.
883     Str = Str.substr(1);
884   } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
885 
886   return Result;
887 }
888 
parseVersionFromName(StringRef Name,unsigned & Major,unsigned & Minor,unsigned & Micro)889 static void parseVersionFromName(StringRef Name, unsigned &Major,
890                                  unsigned &Minor, unsigned &Micro) {
891   // Any unset version defaults to 0.
892   Major = Minor = Micro = 0;
893 
894   // Parse up to three components.
895   unsigned *Components[3] = {&Major, &Minor, &Micro};
896   for (unsigned i = 0; i != 3; ++i) {
897     if (Name.empty() || Name[0] < '0' || Name[0] > '9')
898       break;
899 
900     // Consume the leading number.
901     *Components[i] = EatNumber(Name);
902 
903     // Consume the separator, if present.
904     if (Name.startswith("."))
905       Name = Name.substr(1);
906   }
907 }
908 
getEnvironmentVersion(unsigned & Major,unsigned & Minor,unsigned & Micro) const909 void Triple::getEnvironmentVersion(unsigned &Major, unsigned &Minor,
910                                    unsigned &Micro) const {
911   StringRef EnvironmentName = getEnvironmentName();
912   StringRef EnvironmentTypeName = getEnvironmentTypeName(getEnvironment());
913   if (EnvironmentName.startswith(EnvironmentTypeName))
914     EnvironmentName = EnvironmentName.substr(EnvironmentTypeName.size());
915 
916   parseVersionFromName(EnvironmentName, Major, Minor, Micro);
917 }
918 
getOSVersion(unsigned & Major,unsigned & Minor,unsigned & Micro) const919 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
920                           unsigned &Micro) const {
921   StringRef OSName = getOSName();
922   // Assume that the OS portion of the triple starts with the canonical name.
923   StringRef OSTypeName = getOSTypeName(getOS());
924   if (OSName.startswith(OSTypeName))
925     OSName = OSName.substr(OSTypeName.size());
926 
927   parseVersionFromName(OSName, Major, Minor, Micro);
928 }
929 
getMacOSXVersion(unsigned & Major,unsigned & Minor,unsigned & Micro) const930 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
931                               unsigned &Micro) const {
932   getOSVersion(Major, Minor, Micro);
933 
934   switch (getOS()) {
935   default: llvm_unreachable("unexpected OS for Darwin triple");
936   case Darwin:
937     // Default to darwin8, i.e., MacOSX 10.4.
938     if (Major == 0)
939       Major = 8;
940     // Darwin version numbers are skewed from OS X versions.
941     if (Major < 4)
942       return false;
943     Micro = 0;
944     Minor = Major - 4;
945     Major = 10;
946     break;
947   case MacOSX:
948     // Default to 10.4.
949     if (Major == 0) {
950       Major = 10;
951       Minor = 4;
952     }
953     if (Major != 10)
954       return false;
955     break;
956   case IOS:
957   case TvOS:
958   case WatchOS:
959     // Ignore the version from the triple.  This is only handled because the
960     // the clang driver combines OS X and IOS support into a common Darwin
961     // toolchain that wants to know the OS X version number even when targeting
962     // IOS.
963     Major = 10;
964     Minor = 4;
965     Micro = 0;
966     break;
967   }
968   return true;
969 }
970 
getiOSVersion(unsigned & Major,unsigned & Minor,unsigned & Micro) const971 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
972                            unsigned &Micro) const {
973   switch (getOS()) {
974   default: llvm_unreachable("unexpected OS for Darwin triple");
975   case Darwin:
976   case MacOSX:
977     // Ignore the version from the triple.  This is only handled because the
978     // the clang driver combines OS X and IOS support into a common Darwin
979     // toolchain that wants to know the iOS version number even when targeting
980     // OS X.
981     Major = 5;
982     Minor = 0;
983     Micro = 0;
984     break;
985   case IOS:
986   case TvOS:
987     getOSVersion(Major, Minor, Micro);
988     // Default to 5.0 (or 7.0 for arm64).
989     if (Major == 0)
990       Major = (getArch() == aarch64) ? 7 : 5;
991     break;
992   case WatchOS:
993     llvm_unreachable("conflicting triple info");
994   }
995 }
996 
getWatchOSVersion(unsigned & Major,unsigned & Minor,unsigned & Micro) const997 void Triple::getWatchOSVersion(unsigned &Major, unsigned &Minor,
998                                unsigned &Micro) const {
999   switch (getOS()) {
1000   default: llvm_unreachable("unexpected OS for Darwin triple");
1001   case Darwin:
1002   case MacOSX:
1003     // Ignore the version from the triple.  This is only handled because the
1004     // the clang driver combines OS X and IOS support into a common Darwin
1005     // toolchain that wants to know the iOS version number even when targeting
1006     // OS X.
1007     Major = 2;
1008     Minor = 0;
1009     Micro = 0;
1010     break;
1011   case WatchOS:
1012     getOSVersion(Major, Minor, Micro);
1013     if (Major == 0)
1014       Major = 2;
1015     break;
1016   case IOS:
1017     llvm_unreachable("conflicting triple info");
1018   }
1019 }
1020 
setTriple(const Twine & Str)1021 void Triple::setTriple(const Twine &Str) {
1022   *this = Triple(Str);
1023 }
1024 
setArch(ArchType Kind)1025 void Triple::setArch(ArchType Kind) {
1026   setArchName(getArchTypeName(Kind));
1027 }
1028 
setVendor(VendorType Kind)1029 void Triple::setVendor(VendorType Kind) {
1030   setVendorName(getVendorTypeName(Kind));
1031 }
1032 
setOS(OSType Kind)1033 void Triple::setOS(OSType Kind) {
1034   setOSName(getOSTypeName(Kind));
1035 }
1036 
setEnvironment(EnvironmentType Kind)1037 void Triple::setEnvironment(EnvironmentType Kind) {
1038   if (ObjectFormat == getDefaultFormat(*this))
1039     return setEnvironmentName(getEnvironmentTypeName(Kind));
1040 
1041   setEnvironmentName((getEnvironmentTypeName(Kind) + Twine("-") +
1042                       getObjectFormatTypeName(ObjectFormat)).str());
1043 }
1044 
setObjectFormat(ObjectFormatType Kind)1045 void Triple::setObjectFormat(ObjectFormatType Kind) {
1046   if (Environment == UnknownEnvironment)
1047     return setEnvironmentName(getObjectFormatTypeName(Kind));
1048 
1049   setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
1050                       getObjectFormatTypeName(Kind)).str());
1051 }
1052 
setArchName(StringRef Str)1053 void Triple::setArchName(StringRef Str) {
1054   // Work around a miscompilation bug for Twines in gcc 4.0.3.
1055   SmallString<64> Triple;
1056   Triple += Str;
1057   Triple += "-";
1058   Triple += getVendorName();
1059   Triple += "-";
1060   Triple += getOSAndEnvironmentName();
1061   setTriple(Triple);
1062 }
1063 
setVendorName(StringRef Str)1064 void Triple::setVendorName(StringRef Str) {
1065   setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
1066 }
1067 
setOSName(StringRef Str)1068 void Triple::setOSName(StringRef Str) {
1069   if (hasEnvironment())
1070     setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
1071               "-" + getEnvironmentName());
1072   else
1073     setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
1074 }
1075 
setEnvironmentName(StringRef Str)1076 void Triple::setEnvironmentName(StringRef Str) {
1077   setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
1078             "-" + Str);
1079 }
1080 
setOSAndEnvironmentName(StringRef Str)1081 void Triple::setOSAndEnvironmentName(StringRef Str) {
1082   setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
1083 }
1084 
getArchPointerBitWidth(llvm::Triple::ArchType Arch)1085 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
1086   switch (Arch) {
1087   case llvm::Triple::UnknownArch:
1088     return 0;
1089 
1090   case llvm::Triple::avr:
1091   case llvm::Triple::msp430:
1092     return 16;
1093 
1094   case llvm::Triple::arm:
1095   case llvm::Triple::armeb:
1096   case llvm::Triple::hexagon:
1097   case llvm::Triple::le32:
1098   case llvm::Triple::mips:
1099   case llvm::Triple::mipsel:
1100   case llvm::Triple::nvptx:
1101   case llvm::Triple::ppc:
1102   case llvm::Triple::r600:
1103   case llvm::Triple::sparc:
1104   case llvm::Triple::sparcel:
1105   case llvm::Triple::tce:
1106   case llvm::Triple::thumb:
1107   case llvm::Triple::thumbeb:
1108   case llvm::Triple::x86:
1109   case llvm::Triple::xcore:
1110   case llvm::Triple::amdil:
1111   case llvm::Triple::hsail:
1112   case llvm::Triple::spir:
1113   case llvm::Triple::kalimba:
1114   case llvm::Triple::shave:
1115   case llvm::Triple::wasm32:
1116     return 32;
1117 
1118   case llvm::Triple::aarch64:
1119   case llvm::Triple::aarch64_be:
1120   case llvm::Triple::amdgcn:
1121   case llvm::Triple::bpfel:
1122   case llvm::Triple::bpfeb:
1123   case llvm::Triple::le64:
1124   case llvm::Triple::mips64:
1125   case llvm::Triple::mips64el:
1126   case llvm::Triple::nvptx64:
1127   case llvm::Triple::ppc64:
1128   case llvm::Triple::ppc64le:
1129   case llvm::Triple::sparcv9:
1130   case llvm::Triple::systemz:
1131   case llvm::Triple::x86_64:
1132   case llvm::Triple::amdil64:
1133   case llvm::Triple::hsail64:
1134   case llvm::Triple::spir64:
1135   case llvm::Triple::wasm64:
1136     return 64;
1137   }
1138   llvm_unreachable("Invalid architecture value");
1139 }
1140 
isArch64Bit() const1141 bool Triple::isArch64Bit() const {
1142   return getArchPointerBitWidth(getArch()) == 64;
1143 }
1144 
isArch32Bit() const1145 bool Triple::isArch32Bit() const {
1146   return getArchPointerBitWidth(getArch()) == 32;
1147 }
1148 
isArch16Bit() const1149 bool Triple::isArch16Bit() const {
1150   return getArchPointerBitWidth(getArch()) == 16;
1151 }
1152 
get32BitArchVariant() const1153 Triple Triple::get32BitArchVariant() const {
1154   Triple T(*this);
1155   switch (getArch()) {
1156   case Triple::UnknownArch:
1157   case Triple::aarch64:
1158   case Triple::aarch64_be:
1159   case Triple::amdgcn:
1160   case Triple::avr:
1161   case Triple::bpfel:
1162   case Triple::bpfeb:
1163   case Triple::msp430:
1164   case Triple::systemz:
1165   case Triple::ppc64le:
1166     T.setArch(UnknownArch);
1167     break;
1168 
1169   case Triple::amdil:
1170   case Triple::hsail:
1171   case Triple::spir:
1172   case Triple::arm:
1173   case Triple::armeb:
1174   case Triple::hexagon:
1175   case Triple::kalimba:
1176   case Triple::le32:
1177   case Triple::mips:
1178   case Triple::mipsel:
1179   case Triple::nvptx:
1180   case Triple::ppc:
1181   case Triple::r600:
1182   case Triple::sparc:
1183   case Triple::sparcel:
1184   case Triple::tce:
1185   case Triple::thumb:
1186   case Triple::thumbeb:
1187   case Triple::x86:
1188   case Triple::xcore:
1189   case Triple::shave:
1190   case Triple::wasm32:
1191     // Already 32-bit.
1192     break;
1193 
1194   case Triple::le64:      T.setArch(Triple::le32);    break;
1195   case Triple::mips64:    T.setArch(Triple::mips);    break;
1196   case Triple::mips64el:  T.setArch(Triple::mipsel);  break;
1197   case Triple::nvptx64:   T.setArch(Triple::nvptx);   break;
1198   case Triple::ppc64:     T.setArch(Triple::ppc);     break;
1199   case Triple::sparcv9:   T.setArch(Triple::sparc);   break;
1200   case Triple::x86_64:    T.setArch(Triple::x86);     break;
1201   case Triple::amdil64:   T.setArch(Triple::amdil);   break;
1202   case Triple::hsail64:   T.setArch(Triple::hsail);   break;
1203   case Triple::spir64:    T.setArch(Triple::spir);    break;
1204   case Triple::wasm64:    T.setArch(Triple::wasm32);  break;
1205   }
1206   return T;
1207 }
1208 
get64BitArchVariant() const1209 Triple Triple::get64BitArchVariant() const {
1210   Triple T(*this);
1211   switch (getArch()) {
1212   case Triple::UnknownArch:
1213   case Triple::arm:
1214   case Triple::armeb:
1215   case Triple::avr:
1216   case Triple::hexagon:
1217   case Triple::kalimba:
1218   case Triple::msp430:
1219   case Triple::r600:
1220   case Triple::tce:
1221   case Triple::thumb:
1222   case Triple::thumbeb:
1223   case Triple::xcore:
1224   case Triple::sparcel:
1225   case Triple::shave:
1226     T.setArch(UnknownArch);
1227     break;
1228 
1229   case Triple::aarch64:
1230   case Triple::aarch64_be:
1231   case Triple::bpfel:
1232   case Triple::bpfeb:
1233   case Triple::le64:
1234   case Triple::amdil64:
1235   case Triple::amdgcn:
1236   case Triple::hsail64:
1237   case Triple::spir64:
1238   case Triple::mips64:
1239   case Triple::mips64el:
1240   case Triple::nvptx64:
1241   case Triple::ppc64:
1242   case Triple::ppc64le:
1243   case Triple::sparcv9:
1244   case Triple::systemz:
1245   case Triple::x86_64:
1246   case Triple::wasm64:
1247     // Already 64-bit.
1248     break;
1249 
1250   case Triple::le32:    T.setArch(Triple::le64);      break;
1251   case Triple::mips:    T.setArch(Triple::mips64);    break;
1252   case Triple::mipsel:  T.setArch(Triple::mips64el);  break;
1253   case Triple::nvptx:   T.setArch(Triple::nvptx64);   break;
1254   case Triple::ppc:     T.setArch(Triple::ppc64);     break;
1255   case Triple::sparc:   T.setArch(Triple::sparcv9);   break;
1256   case Triple::x86:     T.setArch(Triple::x86_64);    break;
1257   case Triple::amdil:   T.setArch(Triple::amdil64);   break;
1258   case Triple::hsail:   T.setArch(Triple::hsail64);   break;
1259   case Triple::spir:    T.setArch(Triple::spir64);    break;
1260   case Triple::wasm32:  T.setArch(Triple::wasm64);    break;
1261   }
1262   return T;
1263 }
1264 
getBigEndianArchVariant() const1265 Triple Triple::getBigEndianArchVariant() const {
1266   Triple T(*this);
1267   switch (getArch()) {
1268   case Triple::UnknownArch:
1269   case Triple::amdgcn:
1270   case Triple::amdil64:
1271   case Triple::amdil:
1272   case Triple::avr:
1273   case Triple::hexagon:
1274   case Triple::hsail64:
1275   case Triple::hsail:
1276   case Triple::kalimba:
1277   case Triple::le32:
1278   case Triple::le64:
1279   case Triple::msp430:
1280   case Triple::nvptx64:
1281   case Triple::nvptx:
1282   case Triple::r600:
1283   case Triple::shave:
1284   case Triple::spir64:
1285   case Triple::spir:
1286   case Triple::wasm32:
1287   case Triple::wasm64:
1288   case Triple::x86:
1289   case Triple::x86_64:
1290   case Triple::xcore:
1291 
1292   // ARM is intentionally unsupported here, changing the architecture would
1293   // drop any arch suffixes.
1294   case Triple::arm:
1295   case Triple::thumb:
1296     T.setArch(UnknownArch);
1297     break;
1298 
1299   case Triple::aarch64_be:
1300   case Triple::armeb:
1301   case Triple::bpfeb:
1302   case Triple::mips64:
1303   case Triple::mips:
1304   case Triple::ppc64:
1305   case Triple::ppc:
1306   case Triple::sparc:
1307   case Triple::sparcv9:
1308   case Triple::systemz:
1309   case Triple::tce:
1310   case Triple::thumbeb:
1311     // Already big endian.
1312     break;
1313 
1314   case Triple::aarch64: T.setArch(Triple::aarch64_be); break;
1315   case Triple::bpfel:   T.setArch(Triple::bpfeb);      break;
1316   case Triple::mips64el:T.setArch(Triple::mips64);     break;
1317   case Triple::mipsel:  T.setArch(Triple::mips);       break;
1318   case Triple::ppc64le: T.setArch(Triple::ppc64);      break;
1319   case Triple::sparcel: T.setArch(Triple::sparc);      break;
1320   }
1321   return T;
1322 }
1323 
getLittleEndianArchVariant() const1324 Triple Triple::getLittleEndianArchVariant() const {
1325   Triple T(*this);
1326   switch (getArch()) {
1327   case Triple::UnknownArch:
1328   case Triple::ppc:
1329   case Triple::sparcv9:
1330   case Triple::systemz:
1331   case Triple::tce:
1332 
1333   // ARM is intentionally unsupported here, changing the architecture would
1334   // drop any arch suffixes.
1335   case Triple::armeb:
1336   case Triple::thumbeb:
1337     T.setArch(UnknownArch);
1338     break;
1339 
1340   case Triple::aarch64:
1341   case Triple::amdgcn:
1342   case Triple::amdil64:
1343   case Triple::amdil:
1344   case Triple::arm:
1345   case Triple::avr:
1346   case Triple::bpfel:
1347   case Triple::hexagon:
1348   case Triple::hsail64:
1349   case Triple::hsail:
1350   case Triple::kalimba:
1351   case Triple::le32:
1352   case Triple::le64:
1353   case Triple::mips64el:
1354   case Triple::mipsel:
1355   case Triple::msp430:
1356   case Triple::nvptx64:
1357   case Triple::nvptx:
1358   case Triple::ppc64le:
1359   case Triple::r600:
1360   case Triple::shave:
1361   case Triple::sparcel:
1362   case Triple::spir64:
1363   case Triple::spir:
1364   case Triple::thumb:
1365   case Triple::wasm32:
1366   case Triple::wasm64:
1367   case Triple::x86:
1368   case Triple::x86_64:
1369   case Triple::xcore:
1370     // Already little endian.
1371     break;
1372 
1373   case Triple::aarch64_be: T.setArch(Triple::aarch64);  break;
1374   case Triple::bpfeb:      T.setArch(Triple::bpfel);    break;
1375   case Triple::mips64:     T.setArch(Triple::mips64el); break;
1376   case Triple::mips:       T.setArch(Triple::mipsel);   break;
1377   case Triple::ppc64:      T.setArch(Triple::ppc64le);  break;
1378   case Triple::sparc:      T.setArch(Triple::sparcel);  break;
1379   }
1380   return T;
1381 }
1382 
getARMCPUForArch(StringRef MArch) const1383 StringRef Triple::getARMCPUForArch(StringRef MArch) const {
1384   if (MArch.empty())
1385     MArch = getArchName();
1386   MArch = ARM::getCanonicalArchName(MArch);
1387 
1388   // Some defaults are forced.
1389   switch (getOS()) {
1390   case llvm::Triple::FreeBSD:
1391   case llvm::Triple::NetBSD:
1392     if (!MArch.empty() && MArch == "v6")
1393       return "arm1176jzf-s";
1394     break;
1395   case llvm::Triple::Win32:
1396     // FIXME: this is invalid for WindowsCE
1397     return "cortex-a9";
1398   case llvm::Triple::MacOSX:
1399   case llvm::Triple::IOS:
1400   case llvm::Triple::WatchOS:
1401     if (MArch == "v7k")
1402       return "cortex-a7";
1403     break;
1404   default:
1405     break;
1406   }
1407 
1408   if (MArch.empty())
1409     return StringRef();
1410 
1411   StringRef CPU = ARM::getDefaultCPU(MArch);
1412   if (!CPU.empty())
1413     return CPU;
1414 
1415   // If no specific architecture version is requested, return the minimum CPU
1416   // required by the OS and environment.
1417   switch (getOS()) {
1418   case llvm::Triple::NetBSD:
1419     switch (getEnvironment()) {
1420     case llvm::Triple::GNUEABIHF:
1421     case llvm::Triple::GNUEABI:
1422     case llvm::Triple::EABIHF:
1423     case llvm::Triple::EABI:
1424       return "arm926ej-s";
1425     default:
1426       return "strongarm";
1427     }
1428   case llvm::Triple::NaCl:
1429     return "cortex-a8";
1430   default:
1431     switch (getEnvironment()) {
1432     case llvm::Triple::EABIHF:
1433     case llvm::Triple::GNUEABIHF:
1434       return "arm1176jzf-s";
1435     default:
1436       return "arm7tdmi";
1437     }
1438   }
1439 
1440   llvm_unreachable("invalid arch name");
1441 }
1442