LLVM  7.0.0svn
Triple.cpp
Go to the documentation of this file.
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"
15 #include "llvm/Support/Host.h"
17 #include <cstring>
18 using namespace llvm;
19 
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 arc: return "arc";
29  case avr: return "avr";
30  case bpfel: return "bpfel";
31  case bpfeb: return "bpfeb";
32  case hexagon: return "hexagon";
33  case mips: return "mips";
34  case mipsel: return "mipsel";
35  case mips64: return "mips64";
36  case mips64el: return "mips64el";
37  case msp430: return "msp430";
38  case nios2: return "nios2";
39  case ppc64: return "powerpc64";
40  case ppc64le: return "powerpc64le";
41  case ppc: return "powerpc";
42  case r600: return "r600";
43  case amdgcn: return "amdgcn";
44  case riscv32: return "riscv32";
45  case riscv64: return "riscv64";
46  case sparc: return "sparc";
47  case sparcv9: return "sparcv9";
48  case sparcel: return "sparcel";
49  case systemz: return "s390x";
50  case tce: return "tce";
51  case tcele: return "tcele";
52  case thumb: return "thumb";
53  case thumbeb: return "thumbeb";
54  case x86: return "i386";
55  case x86_64: return "x86_64";
56  case xcore: return "xcore";
57  case nvptx: return "nvptx";
58  case nvptx64: return "nvptx64";
59  case le32: return "le32";
60  case le64: return "le64";
61  case amdil: return "amdil";
62  case amdil64: return "amdil64";
63  case hsail: return "hsail";
64  case hsail64: return "hsail64";
65  case spir: return "spir";
66  case spir64: return "spir64";
67  case kalimba: return "kalimba";
68  case lanai: return "lanai";
69  case shave: return "shave";
70  case wasm32: return "wasm32";
71  case wasm64: return "wasm64";
72  case renderscript32: return "renderscript32";
73  case renderscript64: return "renderscript64";
74  }
75 
76  llvm_unreachable("Invalid ArchType!");
77 }
78 
80  switch (Kind) {
81  default:
82  return StringRef();
83 
84  case aarch64:
85  case aarch64_be: return "aarch64";
86 
87  case arc: return "arc";
88 
89  case arm:
90  case armeb:
91  case thumb:
92  case thumbeb: return "arm";
93 
94  case avr: return "avr";
95 
96  case ppc64:
97  case ppc64le:
98  case ppc: return "ppc";
99 
100  case mips:
101  case mipsel:
102  case mips64:
103  case mips64el: return "mips";
104 
105  case nios2: return "nios2";
106 
107  case hexagon: return "hexagon";
108 
109  case amdgcn: return "amdgcn";
110  case r600: return "r600";
111 
112  case bpfel:
113  case bpfeb: return "bpf";
114 
115  case sparcv9:
116  case sparcel:
117  case sparc: return "sparc";
118 
119  case systemz: return "s390";
120 
121  case x86:
122  case x86_64: return "x86";
123 
124  case xcore: return "xcore";
125 
126  // NVPTX intrinsics are namespaced under nvvm.
127  case nvptx: return "nvvm";
128  case nvptx64: return "nvvm";
129 
130  case le32: return "le32";
131  case le64: return "le64";
132 
133  case amdil:
134  case amdil64: return "amdil";
135 
136  case hsail:
137  case hsail64: return "hsail";
138 
139  case spir:
140  case spir64: return "spir";
141  case kalimba: return "kalimba";
142  case lanai: return "lanai";
143  case shave: return "shave";
144  case wasm32:
145  case wasm64: return "wasm";
146 
147  case riscv32:
148  case riscv64: return "riscv";
149  }
150 }
151 
153  switch (Kind) {
154  case UnknownVendor: return "unknown";
155 
156  case Apple: return "apple";
157  case PC: return "pc";
158  case SCEI: return "scei";
159  case BGP: return "bgp";
160  case BGQ: return "bgq";
161  case Freescale: return "fsl";
162  case IBM: return "ibm";
163  case ImaginationTechnologies: return "img";
164  case MipsTechnologies: return "mti";
165  case NVIDIA: return "nvidia";
166  case CSR: return "csr";
167  case Myriad: return "myriad";
168  case AMD: return "amd";
169  case Mesa: return "mesa";
170  case SUSE: return "suse";
171  }
172 
173  llvm_unreachable("Invalid VendorType!");
174 }
175 
177  switch (Kind) {
178  case UnknownOS: return "unknown";
179 
180  case Ananas: return "ananas";
181  case CloudABI: return "cloudabi";
182  case Darwin: return "darwin";
183  case DragonFly: return "dragonfly";
184  case FreeBSD: return "freebsd";
185  case Fuchsia: return "fuchsia";
186  case IOS: return "ios";
187  case KFreeBSD: return "kfreebsd";
188  case Linux: return "linux";
189  case Lv2: return "lv2";
190  case MacOSX: return "macosx";
191  case NetBSD: return "netbsd";
192  case OpenBSD: return "openbsd";
193  case Solaris: return "solaris";
194  case Win32: return "windows";
195  case Haiku: return "haiku";
196  case Minix: return "minix";
197  case RTEMS: return "rtems";
198  case NaCl: return "nacl";
199  case CNK: return "cnk";
200  case AIX: return "aix";
201  case CUDA: return "cuda";
202  case NVCL: return "nvcl";
203  case AMDHSA: return "amdhsa";
204  case PS4: return "ps4";
205  case ELFIAMCU: return "elfiamcu";
206  case TvOS: return "tvos";
207  case WatchOS: return "watchos";
208  case Mesa3D: return "mesa3d";
209  case Contiki: return "contiki";
210  case AMDPAL: return "amdpal";
211  }
212 
213  llvm_unreachable("Invalid OSType");
214 }
215 
217  switch (Kind) {
218  case UnknownEnvironment: return "unknown";
219  case GNU: return "gnu";
220  case GNUABIN32: return "gnuabin32";
221  case GNUABI64: return "gnuabi64";
222  case GNUEABIHF: return "gnueabihf";
223  case GNUEABI: return "gnueabi";
224  case GNUX32: return "gnux32";
225  case CODE16: return "code16";
226  case EABI: return "eabi";
227  case EABIHF: return "eabihf";
228  case Android: return "android";
229  case Musl: return "musl";
230  case MuslEABI: return "musleabi";
231  case MuslEABIHF: return "musleabihf";
232  case MSVC: return "msvc";
233  case Itanium: return "itanium";
234  case Cygnus: return "cygnus";
235  case CoreCLR: return "coreclr";
236  case Simulator: return "simulator";
237  }
238 
239  llvm_unreachable("Invalid EnvironmentType!");
240 }
241 
243  if (ArchName.equals("bpf")) {
245  return Triple::bpfel;
246  else
247  return Triple::bpfeb;
248  } else if (ArchName.equals("bpf_be") || ArchName.equals("bpfeb")) {
249  return Triple::bpfeb;
250  } else if (ArchName.equals("bpf_le") || ArchName.equals("bpfel")) {
251  return Triple::bpfel;
252  } else {
253  return Triple::UnknownArch;
254  }
255 }
256 
258  Triple::ArchType BPFArch(parseBPFArch(Name));
260  .Case("aarch64", aarch64)
261  .Case("aarch64_be", aarch64_be)
262  .Case("arc", arc)
263  .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
264  .Case("arm", arm)
265  .Case("armeb", armeb)
266  .Case("avr", avr)
267  .StartsWith("bpf", BPFArch)
268  .Case("mips", mips)
269  .Case("mipsel", mipsel)
270  .Case("mips64", mips64)
271  .Case("mips64el", mips64el)
272  .Case("msp430", msp430)
273  .Case("nios2", nios2)
274  .Case("ppc64", ppc64)
275  .Case("ppc32", ppc)
276  .Case("ppc", ppc)
277  .Case("ppc64le", ppc64le)
278  .Case("r600", r600)
279  .Case("amdgcn", amdgcn)
280  .Case("riscv32", riscv32)
281  .Case("riscv64", riscv64)
282  .Case("hexagon", hexagon)
283  .Case("sparc", sparc)
284  .Case("sparcel", sparcel)
285  .Case("sparcv9", sparcv9)
286  .Case("systemz", systemz)
287  .Case("tce", tce)
288  .Case("tcele", tcele)
289  .Case("thumb", thumb)
290  .Case("thumbeb", thumbeb)
291  .Case("x86", x86)
292  .Case("x86-64", x86_64)
293  .Case("xcore", xcore)
294  .Case("nvptx", nvptx)
295  .Case("nvptx64", nvptx64)
296  .Case("le32", le32)
297  .Case("le64", le64)
298  .Case("amdil", amdil)
299  .Case("amdil64", amdil64)
300  .Case("hsail", hsail)
301  .Case("hsail64", hsail64)
302  .Case("spir", spir)
303  .Case("spir64", spir64)
304  .Case("kalimba", kalimba)
305  .Case("lanai", lanai)
306  .Case("shave", shave)
307  .Case("wasm32", wasm32)
308  .Case("wasm64", wasm64)
309  .Case("renderscript32", renderscript32)
310  .Case("renderscript64", renderscript64)
312 }
313 
315  ARM::ISAKind ISA = ARM::parseArchISA(ArchName);
316  ARM::EndianKind ENDIAN = ARM::parseArchEndian(ArchName);
317 
319  switch (ENDIAN) {
321  switch (ISA) {
322  case ARM::ISAKind::ARM:
323  arch = Triple::arm;
324  break;
325  case ARM::ISAKind::THUMB:
326  arch = Triple::thumb;
327  break;
329  arch = Triple::aarch64;
330  break;
332  break;
333  }
334  break;
335  }
336  case ARM::EndianKind::BIG: {
337  switch (ISA) {
338  case ARM::ISAKind::ARM:
339  arch = Triple::armeb;
340  break;
341  case ARM::ISAKind::THUMB:
342  arch = Triple::thumbeb;
343  break;
345  arch = Triple::aarch64_be;
346  break;
348  break;
349  }
350  break;
351  }
353  break;
354  }
355  }
356 
357  ArchName = ARM::getCanonicalArchName(ArchName);
358  if (ArchName.empty())
359  return Triple::UnknownArch;
360 
361  // Thumb only exists in v4+
362  if (ISA == ARM::ISAKind::THUMB &&
363  (ArchName.startswith("v2") || ArchName.startswith("v3")))
364  return Triple::UnknownArch;
365 
366  // Thumb only for v6m
367  ARM::ProfileKind Profile = ARM::parseArchProfile(ArchName);
368  unsigned Version = ARM::parseArchVersion(ArchName);
369  if (Profile == ARM::ProfileKind::M && Version == 6) {
370  if (ENDIAN == ARM::EndianKind::BIG)
371  return Triple::thumbeb;
372  else
373  return Triple::thumb;
374  }
375 
376  return arch;
377 }
378 
380  auto AT = StringSwitch<Triple::ArchType>(ArchName)
381  .Cases("i386", "i486", "i586", "i686", Triple::x86)
382  // FIXME: Do we need to support these?
383  .Cases("i786", "i886", "i986", Triple::x86)
384  .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
385  .Cases("powerpc", "ppc", "ppc32", Triple::ppc)
386  .Cases("powerpc64", "ppu", "ppc64", Triple::ppc64)
387  .Cases("powerpc64le", "ppc64le", Triple::ppc64le)
388  .Case("xscale", Triple::arm)
389  .Case("xscaleeb", Triple::armeb)
390  .Case("aarch64", Triple::aarch64)
391  .Case("aarch64_be", Triple::aarch64_be)
392  .Case("arc", Triple::arc)
393  .Case("arm64", Triple::aarch64)
394  .Case("arm", Triple::arm)
395  .Case("armeb", Triple::armeb)
396  .Case("thumb", Triple::thumb)
397  .Case("thumbeb", Triple::thumbeb)
398  .Case("avr", Triple::avr)
399  .Case("msp430", Triple::msp430)
400  .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
401  .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
402  .Cases("mips64", "mips64eb", Triple::mips64)
403  .Case("mips64el", Triple::mips64el)
404  .Case("nios2", Triple::nios2)
405  .Case("r600", Triple::r600)
406  .Case("amdgcn", Triple::amdgcn)
407  .Case("riscv32", Triple::riscv32)
408  .Case("riscv64", Triple::riscv64)
409  .Case("hexagon", Triple::hexagon)
410  .Cases("s390x", "systemz", Triple::systemz)
411  .Case("sparc", Triple::sparc)
412  .Case("sparcel", Triple::sparcel)
413  .Cases("sparcv9", "sparc64", Triple::sparcv9)
414  .Case("tce", Triple::tce)
415  .Case("tcele", Triple::tcele)
416  .Case("xcore", Triple::xcore)
417  .Case("nvptx", Triple::nvptx)
418  .Case("nvptx64", Triple::nvptx64)
419  .Case("le32", Triple::le32)
420  .Case("le64", Triple::le64)
421  .Case("amdil", Triple::amdil)
422  .Case("amdil64", Triple::amdil64)
423  .Case("hsail", Triple::hsail)
424  .Case("hsail64", Triple::hsail64)
425  .Case("spir", Triple::spir)
426  .Case("spir64", Triple::spir64)
427  .StartsWith("kalimba", Triple::kalimba)
428  .Case("lanai", Triple::lanai)
429  .Case("shave", Triple::shave)
430  .Case("wasm32", Triple::wasm32)
431  .Case("wasm64", Triple::wasm64)
432  .Case("renderscript32", Triple::renderscript32)
433  .Case("renderscript64", Triple::renderscript64)
435 
436  // Some architectures require special parsing logic just to compute the
437  // ArchType result.
438  if (AT == Triple::UnknownArch) {
439  if (ArchName.startswith("arm") || ArchName.startswith("thumb") ||
440  ArchName.startswith("aarch64"))
441  return parseARMArch(ArchName);
442  if (ArchName.startswith("bpf"))
443  return parseBPFArch(ArchName);
444  }
445 
446  return AT;
447 }
448 
450  return StringSwitch<Triple::VendorType>(VendorName)
451  .Case("apple", Triple::Apple)
452  .Case("pc", Triple::PC)
453  .Case("scei", Triple::SCEI)
454  .Case("bgp", Triple::BGP)
455  .Case("bgq", Triple::BGQ)
456  .Case("fsl", Triple::Freescale)
457  .Case("ibm", Triple::IBM)
460  .Case("nvidia", Triple::NVIDIA)
461  .Case("csr", Triple::CSR)
462  .Case("myriad", Triple::Myriad)
463  .Case("amd", Triple::AMD)
464  .Case("mesa", Triple::Mesa)
465  .Case("suse", Triple::SUSE)
467 }
468 
470  return StringSwitch<Triple::OSType>(OSName)
471  .StartsWith("ananas", Triple::Ananas)
472  .StartsWith("cloudabi", Triple::CloudABI)
473  .StartsWith("darwin", Triple::Darwin)
474  .StartsWith("dragonfly", Triple::DragonFly)
475  .StartsWith("freebsd", Triple::FreeBSD)
476  .StartsWith("fuchsia", Triple::Fuchsia)
477  .StartsWith("ios", Triple::IOS)
478  .StartsWith("kfreebsd", Triple::KFreeBSD)
479  .StartsWith("linux", Triple::Linux)
480  .StartsWith("lv2", Triple::Lv2)
481  .StartsWith("macos", Triple::MacOSX)
482  .StartsWith("netbsd", Triple::NetBSD)
483  .StartsWith("openbsd", Triple::OpenBSD)
484  .StartsWith("solaris", Triple::Solaris)
485  .StartsWith("win32", Triple::Win32)
486  .StartsWith("windows", Triple::Win32)
487  .StartsWith("haiku", Triple::Haiku)
488  .StartsWith("minix", Triple::Minix)
489  .StartsWith("rtems", Triple::RTEMS)
490  .StartsWith("nacl", Triple::NaCl)
491  .StartsWith("cnk", Triple::CNK)
492  .StartsWith("aix", Triple::AIX)
493  .StartsWith("cuda", Triple::CUDA)
494  .StartsWith("nvcl", Triple::NVCL)
495  .StartsWith("amdhsa", Triple::AMDHSA)
496  .StartsWith("ps4", Triple::PS4)
497  .StartsWith("elfiamcu", Triple::ELFIAMCU)
498  .StartsWith("tvos", Triple::TvOS)
499  .StartsWith("watchos", Triple::WatchOS)
500  .StartsWith("mesa3d", Triple::Mesa3D)
501  .StartsWith("contiki", Triple::Contiki)
502  .StartsWith("amdpal", Triple::AMDPAL)
504 }
505 
507  return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
508  .StartsWith("eabihf", Triple::EABIHF)
509  .StartsWith("eabi", Triple::EABI)
510  .StartsWith("gnuabin32", Triple::GNUABIN32)
511  .StartsWith("gnuabi64", Triple::GNUABI64)
512  .StartsWith("gnueabihf", Triple::GNUEABIHF)
513  .StartsWith("gnueabi", Triple::GNUEABI)
514  .StartsWith("gnux32", Triple::GNUX32)
515  .StartsWith("code16", Triple::CODE16)
516  .StartsWith("gnu", Triple::GNU)
517  .StartsWith("android", Triple::Android)
518  .StartsWith("musleabihf", Triple::MuslEABIHF)
519  .StartsWith("musleabi", Triple::MuslEABI)
520  .StartsWith("musl", Triple::Musl)
521  .StartsWith("msvc", Triple::MSVC)
522  .StartsWith("itanium", Triple::Itanium)
523  .StartsWith("cygnus", Triple::Cygnus)
524  .StartsWith("coreclr", Triple::CoreCLR)
525  .StartsWith("simulator", Triple::Simulator)
527 }
528 
530  return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
531  .EndsWith("coff", Triple::COFF)
532  .EndsWith("elf", Triple::ELF)
533  .EndsWith("macho", Triple::MachO)
534  .EndsWith("wasm", Triple::Wasm)
536 }
537 
539  StringRef ARMSubArch = ARM::getCanonicalArchName(SubArchName);
540 
541  // For now, this is the small part. Early return.
542  if (ARMSubArch.empty())
543  return StringSwitch<Triple::SubArchType>(SubArchName)
544  .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
545  .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
546  .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
548 
549  // ARM sub arch.
550  switch(ARM::parseArch(ARMSubArch)) {
551  case ARM::ArchKind::ARMV4:
552  return Triple::NoSubArch;
553  case ARM::ArchKind::ARMV4T:
554  return Triple::ARMSubArch_v4t;
555  case ARM::ArchKind::ARMV5T:
556  return Triple::ARMSubArch_v5;
557  case ARM::ArchKind::ARMV5TE:
558  case ARM::ArchKind::IWMMXT:
559  case ARM::ArchKind::IWMMXT2:
560  case ARM::ArchKind::XSCALE:
561  case ARM::ArchKind::ARMV5TEJ:
563  case ARM::ArchKind::ARMV6:
564  return Triple::ARMSubArch_v6;
565  case ARM::ArchKind::ARMV6K:
566  case ARM::ArchKind::ARMV6KZ:
567  return Triple::ARMSubArch_v6k;
568  case ARM::ArchKind::ARMV6T2:
570  case ARM::ArchKind::ARMV6M:
571  return Triple::ARMSubArch_v6m;
572  case ARM::ArchKind::ARMV7A:
573  case ARM::ArchKind::ARMV7R:
574  return Triple::ARMSubArch_v7;
575  case ARM::ArchKind::ARMV7VE:
577  case ARM::ArchKind::ARMV7K:
578  return Triple::ARMSubArch_v7k;
579  case ARM::ArchKind::ARMV7M:
580  return Triple::ARMSubArch_v7m;
581  case ARM::ArchKind::ARMV7S:
582  return Triple::ARMSubArch_v7s;
583  case ARM::ArchKind::ARMV7EM:
585  case ARM::ArchKind::ARMV8A:
586  return Triple::ARMSubArch_v8;
587  case ARM::ArchKind::ARMV8_1A:
589  case ARM::ArchKind::ARMV8_2A:
591  case ARM::ArchKind::ARMV8_3A:
593  case ARM::ArchKind::ARMV8R:
594  return Triple::ARMSubArch_v8r;
595  case ARM::ArchKind::ARMV8MBaseline:
597  case ARM::ArchKind::ARMV8MMainline:
599  default:
600  return Triple::NoSubArch;
601  }
602 }
603 
605  switch (Kind) {
606  case Triple::UnknownObjectFormat: return "";
607  case Triple::COFF: return "coff";
608  case Triple::ELF: return "elf";
609  case Triple::MachO: return "macho";
610  case Triple::Wasm: return "wasm";
611  }
612  llvm_unreachable("unknown object format type");
613 }
614 
616  switch (T.getArch()) {
617  case Triple::UnknownArch:
618  case Triple::aarch64:
619  case Triple::arm:
620  case Triple::thumb:
621  case Triple::x86:
622  case Triple::x86_64:
623  if (T.isOSDarwin())
624  return Triple::MachO;
625  else if (T.isOSWindows())
626  return Triple::COFF;
627  return Triple::ELF;
628 
629  case Triple::aarch64_be:
630  case Triple::arc:
631  case Triple::amdgcn:
632  case Triple::amdil:
633  case Triple::amdil64:
634  case Triple::armeb:
635  case Triple::avr:
636  case Triple::bpfeb:
637  case Triple::bpfel:
638  case Triple::hexagon:
639  case Triple::lanai:
640  case Triple::hsail:
641  case Triple::hsail64:
642  case Triple::kalimba:
643  case Triple::le32:
644  case Triple::le64:
645  case Triple::mips:
646  case Triple::mips64:
647  case Triple::mips64el:
648  case Triple::mipsel:
649  case Triple::msp430:
650  case Triple::nios2:
651  case Triple::nvptx:
652  case Triple::nvptx64:
653  case Triple::ppc64le:
654  case Triple::r600:
657  case Triple::riscv32:
658  case Triple::riscv64:
659  case Triple::shave:
660  case Triple::sparc:
661  case Triple::sparcel:
662  case Triple::sparcv9:
663  case Triple::spir:
664  case Triple::spir64:
665  case Triple::systemz:
666  case Triple::tce:
667  case Triple::tcele:
668  case Triple::thumbeb:
669  case Triple::xcore:
670  return Triple::ELF;
671 
672  case Triple::ppc:
673  case Triple::ppc64:
674  if (T.isOSDarwin())
675  return Triple::MachO;
676  return Triple::ELF;
677 
678  case Triple::wasm32:
679  case Triple::wasm64:
680  return Triple::Wasm;
681  }
682  llvm_unreachable("unknown architecture");
683 }
684 
685 /// \brief Construct a triple from the string representation provided.
686 ///
687 /// This stores the string representation and parses the various pieces into
688 /// enum members.
690  : Data(Str.str()), Arch(UnknownArch), SubArch(NoSubArch),
691  Vendor(UnknownVendor), OS(UnknownOS), Environment(UnknownEnvironment),
692  ObjectFormat(UnknownObjectFormat) {
693  // Do minimal parsing by hand here.
694  SmallVector<StringRef, 4> Components;
695  StringRef(Data).split(Components, '-', /*MaxSplit*/ 3);
696  if (Components.size() > 0) {
697  Arch = parseArch(Components[0]);
698  SubArch = parseSubArch(Components[0]);
699  if (Components.size() > 1) {
700  Vendor = parseVendor(Components[1]);
701  if (Components.size() > 2) {
702  OS = parseOS(Components[2]);
703  if (Components.size() > 3) {
704  Environment = parseEnvironment(Components[3]);
705  ObjectFormat = parseFormat(Components[3]);
706  }
707  }
708  }
709  }
710  if (ObjectFormat == UnknownObjectFormat)
711  ObjectFormat = getDefaultFormat(*this);
712 }
713 
714 /// \brief Construct a triple from string representations of the architecture,
715 /// vendor, and OS.
716 ///
717 /// This joins each argument into a canonical string representation and parses
718 /// them into enum members. It leaves the environment unknown and omits it from
719 /// the string representation.
720 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
721  : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
722  Arch(parseArch(ArchStr.str())),
723  SubArch(parseSubArch(ArchStr.str())),
724  Vendor(parseVendor(VendorStr.str())),
725  OS(parseOS(OSStr.str())),
726  Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
727  ObjectFormat = getDefaultFormat(*this);
728 }
729 
730 /// \brief Construct a triple from string representations of the architecture,
731 /// vendor, OS, and environment.
732 ///
733 /// This joins each argument into a canonical string representation and parses
734 /// them into enum members.
735 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
736  const Twine &EnvironmentStr)
737  : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
738  EnvironmentStr).str()),
739  Arch(parseArch(ArchStr.str())),
740  SubArch(parseSubArch(ArchStr.str())),
741  Vendor(parseVendor(VendorStr.str())),
742  OS(parseOS(OSStr.str())),
743  Environment(parseEnvironment(EnvironmentStr.str())),
744  ObjectFormat(parseFormat(EnvironmentStr.str())) {
745  if (ObjectFormat == Triple::UnknownObjectFormat)
746  ObjectFormat = getDefaultFormat(*this);
747 }
748 
749 std::string Triple::normalize(StringRef Str) {
750  bool IsMinGW32 = false;
751  bool IsCygwin = false;
752 
753  // Parse into components.
754  SmallVector<StringRef, 4> Components;
755  Str.split(Components, '-');
756 
757  // If the first component corresponds to a known architecture, preferentially
758  // use it for the architecture. If the second component corresponds to a
759  // known vendor, preferentially use it for the vendor, etc. This avoids silly
760  // component movement when a component parses as (eg) both a valid arch and a
761  // valid os.
762  ArchType Arch = UnknownArch;
763  if (Components.size() > 0)
764  Arch = parseArch(Components[0]);
765  VendorType Vendor = UnknownVendor;
766  if (Components.size() > 1)
767  Vendor = parseVendor(Components[1]);
768  OSType OS = UnknownOS;
769  if (Components.size() > 2) {
770  OS = parseOS(Components[2]);
771  IsCygwin = Components[2].startswith("cygwin");
772  IsMinGW32 = Components[2].startswith("mingw");
773  }
774  EnvironmentType Environment = UnknownEnvironment;
775  if (Components.size() > 3)
776  Environment = parseEnvironment(Components[3]);
777  ObjectFormatType ObjectFormat = UnknownObjectFormat;
778  if (Components.size() > 4)
779  ObjectFormat = parseFormat(Components[4]);
780 
781  // Note which components are already in their final position. These will not
782  // be moved.
783  bool Found[4];
784  Found[0] = Arch != UnknownArch;
785  Found[1] = Vendor != UnknownVendor;
786  Found[2] = OS != UnknownOS;
787  Found[3] = Environment != UnknownEnvironment;
788 
789  // If they are not there already, permute the components into their canonical
790  // positions by seeing if they parse as a valid architecture, and if so moving
791  // the component to the architecture position etc.
792  for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
793  if (Found[Pos])
794  continue; // Already in the canonical position.
795 
796  for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
797  // Do not reparse any components that already matched.
798  if (Idx < array_lengthof(Found) && Found[Idx])
799  continue;
800 
801  // Does this component parse as valid for the target position?
802  bool Valid = false;
803  StringRef Comp = Components[Idx];
804  switch (Pos) {
805  default: llvm_unreachable("unexpected component type!");
806  case 0:
807  Arch = parseArch(Comp);
808  Valid = Arch != UnknownArch;
809  break;
810  case 1:
811  Vendor = parseVendor(Comp);
812  Valid = Vendor != UnknownVendor;
813  break;
814  case 2:
815  OS = parseOS(Comp);
816  IsCygwin = Comp.startswith("cygwin");
817  IsMinGW32 = Comp.startswith("mingw");
818  Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
819  break;
820  case 3:
821  Environment = parseEnvironment(Comp);
822  Valid = Environment != UnknownEnvironment;
823  if (!Valid) {
824  ObjectFormat = parseFormat(Comp);
825  Valid = ObjectFormat != UnknownObjectFormat;
826  }
827  break;
828  }
829  if (!Valid)
830  continue; // Nope, try the next component.
831 
832  // Move the component to the target position, pushing any non-fixed
833  // components that are in the way to the right. This tends to give
834  // good results in the common cases of a forgotten vendor component
835  // or a wrongly positioned environment.
836  if (Pos < Idx) {
837  // Insert left, pushing the existing components to the right. For
838  // example, a-b-i386 -> i386-a-b when moving i386 to the front.
839  StringRef CurrentComponent(""); // The empty component.
840  // Replace the component we are moving with an empty component.
841  std::swap(CurrentComponent, Components[Idx]);
842  // Insert the component being moved at Pos, displacing any existing
843  // components to the right.
844  for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
845  // Skip over any fixed components.
846  while (i < array_lengthof(Found) && Found[i])
847  ++i;
848  // Place the component at the new position, getting the component
849  // that was at this position - it will be moved right.
850  std::swap(CurrentComponent, Components[i]);
851  }
852  } else if (Pos > Idx) {
853  // Push right by inserting empty components until the component at Idx
854  // reaches the target position Pos. For example, pc-a -> -pc-a when
855  // moving pc to the second position.
856  do {
857  // Insert one empty component at Idx.
858  StringRef CurrentComponent(""); // The empty component.
859  for (unsigned i = Idx; i < Components.size();) {
860  // Place the component at the new position, getting the component
861  // that was at this position - it will be moved right.
862  std::swap(CurrentComponent, Components[i]);
863  // If it was placed on top of an empty component then we are done.
864  if (CurrentComponent.empty())
865  break;
866  // Advance to the next component, skipping any fixed components.
867  while (++i < array_lengthof(Found) && Found[i])
868  ;
869  }
870  // The last component was pushed off the end - append it.
871  if (!CurrentComponent.empty())
872  Components.push_back(CurrentComponent);
873 
874  // Advance Idx to the component's new position.
875  while (++Idx < array_lengthof(Found) && Found[Idx])
876  ;
877  } while (Idx < Pos); // Add more until the final position is reached.
878  }
879  assert(Pos < Components.size() && Components[Pos] == Comp &&
880  "Component moved wrong!");
881  Found[Pos] = true;
882  break;
883  }
884  }
885 
886  // Special case logic goes here. At this point Arch, Vendor and OS have the
887  // correct values for the computed components.
888  std::string NormalizedEnvironment;
889  if (Environment == Triple::Android && Components[3].startswith("androideabi")) {
890  StringRef AndroidVersion = Components[3].drop_front(strlen("androideabi"));
891  if (AndroidVersion.empty()) {
892  Components[3] = "android";
893  } else {
894  NormalizedEnvironment = Twine("android", AndroidVersion).str();
895  Components[3] = NormalizedEnvironment;
896  }
897  }
898 
899  // SUSE uses "gnueabi" to mean "gnueabihf"
900  if (Vendor == Triple::SUSE && Environment == llvm::Triple::GNUEABI)
901  Components[3] = "gnueabihf";
902 
903  if (OS == Triple::Win32) {
904  Components.resize(4);
905  Components[2] = "windows";
906  if (Environment == UnknownEnvironment) {
907  if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
908  Components[3] = "msvc";
909  else
910  Components[3] = getObjectFormatTypeName(ObjectFormat);
911  }
912  } else if (IsMinGW32) {
913  Components.resize(4);
914  Components[2] = "windows";
915  Components[3] = "gnu";
916  } else if (IsCygwin) {
917  Components.resize(4);
918  Components[2] = "windows";
919  Components[3] = "cygnus";
920  }
921  if (IsMinGW32 || IsCygwin ||
922  (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
923  if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
924  Components.resize(5);
925  Components[4] = getObjectFormatTypeName(ObjectFormat);
926  }
927  }
928 
929  // Stick the corrected components back together to form the normalized string.
930  std::string Normalized;
931  for (unsigned i = 0, e = Components.size(); i != e; ++i) {
932  if (i) Normalized += '-';
933  Normalized += Components[i];
934  }
935  return Normalized;
936 }
937 
939  return StringRef(Data).split('-').first; // Isolate first component
940 }
941 
943  StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
944  return Tmp.split('-').first; // Isolate second component
945 }
946 
948  StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
949  Tmp = Tmp.split('-').second; // Strip second component
950  return Tmp.split('-').first; // Isolate third component
951 }
952 
954  StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
955  Tmp = Tmp.split('-').second; // Strip second component
956  return Tmp.split('-').second; // Strip third component
957 }
958 
960  StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
961  return Tmp.split('-').second; // Strip second component
962 }
963 
964 static unsigned EatNumber(StringRef &Str) {
965  assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
966  unsigned Result = 0;
967 
968  do {
969  // Consume the leading digit.
970  Result = Result*10 + (Str[0] - '0');
971 
972  // Eat the digit.
973  Str = Str.substr(1);
974  } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
975 
976  return Result;
977 }
978 
979 static void parseVersionFromName(StringRef Name, unsigned &Major,
980  unsigned &Minor, unsigned &Micro) {
981  // Any unset version defaults to 0.
982  Major = Minor = Micro = 0;
983 
984  // Parse up to three components.
985  unsigned *Components[3] = {&Major, &Minor, &Micro};
986  for (unsigned i = 0; i != 3; ++i) {
987  if (Name.empty() || Name[0] < '0' || Name[0] > '9')
988  break;
989 
990  // Consume the leading number.
991  *Components[i] = EatNumber(Name);
992 
993  // Consume the separator, if present.
994  if (Name.startswith("."))
995  Name = Name.substr(1);
996  }
997 }
998 
999 void Triple::getEnvironmentVersion(unsigned &Major, unsigned &Minor,
1000  unsigned &Micro) const {
1001  StringRef EnvironmentName = getEnvironmentName();
1002  StringRef EnvironmentTypeName = getEnvironmentTypeName(getEnvironment());
1003  if (EnvironmentName.startswith(EnvironmentTypeName))
1004  EnvironmentName = EnvironmentName.substr(EnvironmentTypeName.size());
1005 
1006  parseVersionFromName(EnvironmentName, Major, Minor, Micro);
1007 }
1008 
1009 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
1010  unsigned &Micro) const {
1011  StringRef OSName = getOSName();
1012  // Assume that the OS portion of the triple starts with the canonical name.
1013  StringRef OSTypeName = getOSTypeName(getOS());
1014  if (OSName.startswith(OSTypeName))
1015  OSName = OSName.substr(OSTypeName.size());
1016  else if (getOS() == MacOSX)
1017  OSName.consume_front("macos");
1018 
1019  parseVersionFromName(OSName, Major, Minor, Micro);
1020 }
1021 
1022 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
1023  unsigned &Micro) const {
1024  getOSVersion(Major, Minor, Micro);
1025 
1026  switch (getOS()) {
1027  default: llvm_unreachable("unexpected OS for Darwin triple");
1028  case Darwin:
1029  // Default to darwin8, i.e., MacOSX 10.4.
1030  if (Major == 0)
1031  Major = 8;
1032  // Darwin version numbers are skewed from OS X versions.
1033  if (Major < 4)
1034  return false;
1035  Micro = 0;
1036  Minor = Major - 4;
1037  Major = 10;
1038  break;
1039  case MacOSX:
1040  // Default to 10.4.
1041  if (Major == 0) {
1042  Major = 10;
1043  Minor = 4;
1044  }
1045  if (Major != 10)
1046  return false;
1047  break;
1048  case IOS:
1049  case TvOS:
1050  case WatchOS:
1051  // Ignore the version from the triple. This is only handled because the
1052  // the clang driver combines OS X and IOS support into a common Darwin
1053  // toolchain that wants to know the OS X version number even when targeting
1054  // IOS.
1055  Major = 10;
1056  Minor = 4;
1057  Micro = 0;
1058  break;
1059  }
1060  return true;
1061 }
1062 
1063 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
1064  unsigned &Micro) const {
1065  switch (getOS()) {
1066  default: llvm_unreachable("unexpected OS for Darwin triple");
1067  case Darwin:
1068  case MacOSX:
1069  // Ignore the version from the triple. This is only handled because the
1070  // the clang driver combines OS X and IOS support into a common Darwin
1071  // toolchain that wants to know the iOS version number even when targeting
1072  // OS X.
1073  Major = 5;
1074  Minor = 0;
1075  Micro = 0;
1076  break;
1077  case IOS:
1078  case TvOS:
1079  getOSVersion(Major, Minor, Micro);
1080  // Default to 5.0 (or 7.0 for arm64).
1081  if (Major == 0)
1082  Major = (getArch() == aarch64) ? 7 : 5;
1083  break;
1084  case WatchOS:
1085  llvm_unreachable("conflicting triple info");
1086  }
1087 }
1088 
1089 void Triple::getWatchOSVersion(unsigned &Major, unsigned &Minor,
1090  unsigned &Micro) const {
1091  switch (getOS()) {
1092  default: llvm_unreachable("unexpected OS for Darwin triple");
1093  case Darwin:
1094  case MacOSX:
1095  // Ignore the version from the triple. This is only handled because the
1096  // the clang driver combines OS X and IOS support into a common Darwin
1097  // toolchain that wants to know the iOS version number even when targeting
1098  // OS X.
1099  Major = 2;
1100  Minor = 0;
1101  Micro = 0;
1102  break;
1103  case WatchOS:
1104  getOSVersion(Major, Minor, Micro);
1105  if (Major == 0)
1106  Major = 2;
1107  break;
1108  case IOS:
1109  llvm_unreachable("conflicting triple info");
1110  }
1111 }
1112 
1113 void Triple::setTriple(const Twine &Str) {
1114  *this = Triple(Str);
1115 }
1116 
1119 }
1120 
1123 }
1124 
1126  setOSName(getOSTypeName(Kind));
1127 }
1128 
1130  if (ObjectFormat == getDefaultFormat(*this))
1132 
1134  getObjectFormatTypeName(ObjectFormat)).str());
1135 }
1136 
1138  if (Environment == UnknownEnvironment)
1140 
1141  setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
1142  getObjectFormatTypeName(Kind)).str());
1143 }
1144 
1146  // Work around a miscompilation bug for Twines in gcc 4.0.3.
1148  Triple += Str;
1149  Triple += "-";
1150  Triple += getVendorName();
1151  Triple += "-";
1152  Triple += getOSAndEnvironmentName();
1153  setTriple(Triple);
1154 }
1155 
1157  setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
1158 }
1159 
1161  if (hasEnvironment())
1162  setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
1163  "-" + getEnvironmentName());
1164  else
1165  setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
1166 }
1167 
1169  setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
1170  "-" + Str);
1171 }
1172 
1174  setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
1175 }
1176 
1178  switch (Arch) {
1180  return 0;
1181 
1182  case llvm::Triple::avr:
1183  case llvm::Triple::msp430:
1184  return 16;
1185 
1186  case llvm::Triple::arc:
1187  case llvm::Triple::arm:
1188  case llvm::Triple::armeb:
1189  case llvm::Triple::hexagon:
1190  case llvm::Triple::le32:
1191  case llvm::Triple::mips:
1192  case llvm::Triple::mipsel:
1193  case llvm::Triple::nios2:
1194  case llvm::Triple::nvptx:
1195  case llvm::Triple::ppc:
1196  case llvm::Triple::r600:
1197  case llvm::Triple::riscv32:
1198  case llvm::Triple::sparc:
1199  case llvm::Triple::sparcel:
1200  case llvm::Triple::tce:
1201  case llvm::Triple::tcele:
1202  case llvm::Triple::thumb:
1203  case llvm::Triple::thumbeb:
1204  case llvm::Triple::x86:
1205  case llvm::Triple::xcore:
1206  case llvm::Triple::amdil:
1207  case llvm::Triple::hsail:
1208  case llvm::Triple::spir:
1209  case llvm::Triple::kalimba:
1210  case llvm::Triple::lanai:
1211  case llvm::Triple::shave:
1212  case llvm::Triple::wasm32:
1214  return 32;
1215 
1216  case llvm::Triple::aarch64:
1218  case llvm::Triple::amdgcn:
1219  case llvm::Triple::bpfel:
1220  case llvm::Triple::bpfeb:
1221  case llvm::Triple::le64:
1222  case llvm::Triple::mips64:
1224  case llvm::Triple::nvptx64:
1225  case llvm::Triple::ppc64:
1226  case llvm::Triple::ppc64le:
1227  case llvm::Triple::riscv64:
1228  case llvm::Triple::sparcv9:
1229  case llvm::Triple::systemz:
1230  case llvm::Triple::x86_64:
1231  case llvm::Triple::amdil64:
1232  case llvm::Triple::hsail64:
1233  case llvm::Triple::spir64:
1234  case llvm::Triple::wasm64:
1236  return 64;
1237  }
1238  llvm_unreachable("Invalid architecture value");
1239 }
1240 
1241 bool Triple::isArch64Bit() const {
1242  return getArchPointerBitWidth(getArch()) == 64;
1243 }
1244 
1245 bool Triple::isArch32Bit() const {
1246  return getArchPointerBitWidth(getArch()) == 32;
1247 }
1248 
1249 bool Triple::isArch16Bit() const {
1250  return getArchPointerBitWidth(getArch()) == 16;
1251 }
1252 
1254  Triple T(*this);
1255  switch (getArch()) {
1256  case Triple::UnknownArch:
1257  case Triple::amdgcn:
1258  case Triple::avr:
1259  case Triple::bpfel:
1260  case Triple::bpfeb:
1261  case Triple::msp430:
1262  case Triple::systemz:
1263  case Triple::ppc64le:
1264  T.setArch(UnknownArch);
1265  break;
1266 
1267  case Triple::amdil:
1268  case Triple::hsail:
1269  case Triple::spir:
1270  case Triple::arc:
1271  case Triple::arm:
1272  case Triple::armeb:
1273  case Triple::hexagon:
1274  case Triple::kalimba:
1275  case Triple::le32:
1276  case Triple::mips:
1277  case Triple::mipsel:
1278  case Triple::nios2:
1279  case Triple::nvptx:
1280  case Triple::ppc:
1281  case Triple::r600:
1282  case Triple::riscv32:
1283  case Triple::sparc:
1284  case Triple::sparcel:
1285  case Triple::tce:
1286  case Triple::tcele:
1287  case Triple::thumb:
1288  case Triple::thumbeb:
1289  case Triple::x86:
1290  case Triple::xcore:
1291  case Triple::lanai:
1292  case Triple::shave:
1293  case Triple::wasm32:
1295  // Already 32-bit.
1296  break;
1297 
1298  case Triple::aarch64: T.setArch(Triple::arm); break;
1299  case Triple::aarch64_be: T.setArch(Triple::armeb); break;
1300  case Triple::le64: T.setArch(Triple::le32); break;
1301  case Triple::mips64: T.setArch(Triple::mips); break;
1302  case Triple::mips64el: T.setArch(Triple::mipsel); break;
1303  case Triple::nvptx64: T.setArch(Triple::nvptx); break;
1304  case Triple::ppc64: T.setArch(Triple::ppc); break;
1305  case Triple::sparcv9: T.setArch(Triple::sparc); break;
1306  case Triple::riscv64: T.setArch(Triple::riscv32); break;
1307  case Triple::x86_64: T.setArch(Triple::x86); break;
1308  case Triple::amdil64: T.setArch(Triple::amdil); break;
1309  case Triple::hsail64: T.setArch(Triple::hsail); break;
1310  case Triple::spir64: T.setArch(Triple::spir); break;
1311  case Triple::wasm64: T.setArch(Triple::wasm32); break;
1313  }
1314  return T;
1315 }
1316 
1318  Triple T(*this);
1319  switch (getArch()) {
1320  case Triple::UnknownArch:
1321  case Triple::arc:
1322  case Triple::avr:
1323  case Triple::hexagon:
1324  case Triple::kalimba:
1325  case Triple::lanai:
1326  case Triple::msp430:
1327  case Triple::nios2:
1328  case Triple::r600:
1329  case Triple::tce:
1330  case Triple::tcele:
1331  case Triple::xcore:
1332  case Triple::sparcel:
1333  case Triple::shave:
1334  T.setArch(UnknownArch);
1335  break;
1336 
1337  case Triple::aarch64:
1338  case Triple::aarch64_be:
1339  case Triple::bpfel:
1340  case Triple::bpfeb:
1341  case Triple::le64:
1342  case Triple::amdil64:
1343  case Triple::amdgcn:
1344  case Triple::hsail64:
1345  case Triple::spir64:
1346  case Triple::mips64:
1347  case Triple::mips64el:
1348  case Triple::nvptx64:
1349  case Triple::ppc64:
1350  case Triple::ppc64le:
1351  case Triple::riscv64:
1352  case Triple::sparcv9:
1353  case Triple::systemz:
1354  case Triple::x86_64:
1355  case Triple::wasm64:
1357  // Already 64-bit.
1358  break;
1359 
1360  case Triple::arm: T.setArch(Triple::aarch64); break;
1361  case Triple::armeb: T.setArch(Triple::aarch64_be); break;
1362  case Triple::le32: T.setArch(Triple::le64); break;
1363  case Triple::mips: T.setArch(Triple::mips64); break;
1364  case Triple::mipsel: T.setArch(Triple::mips64el); break;
1365  case Triple::nvptx: T.setArch(Triple::nvptx64); break;
1366  case Triple::ppc: T.setArch(Triple::ppc64); break;
1367  case Triple::sparc: T.setArch(Triple::sparcv9); break;
1368  case Triple::riscv32: T.setArch(Triple::riscv64); break;
1369  case Triple::x86: T.setArch(Triple::x86_64); break;
1370  case Triple::amdil: T.setArch(Triple::amdil64); break;
1371  case Triple::hsail: T.setArch(Triple::hsail64); break;
1372  case Triple::spir: T.setArch(Triple::spir64); break;
1373  case Triple::thumb: T.setArch(Triple::aarch64); break;
1374  case Triple::thumbeb: T.setArch(Triple::aarch64_be); break;
1375  case Triple::wasm32: T.setArch(Triple::wasm64); break;
1377  }
1378  return T;
1379 }
1380 
1382  Triple T(*this);
1383  // Already big endian.
1384  if (!isLittleEndian())
1385  return T;
1386  switch (getArch()) {
1387  case Triple::UnknownArch:
1388  case Triple::amdgcn:
1389  case Triple::amdil64:
1390  case Triple::amdil:
1391  case Triple::avr:
1392  case Triple::hexagon:
1393  case Triple::hsail64:
1394  case Triple::hsail:
1395  case Triple::kalimba:
1396  case Triple::le32:
1397  case Triple::le64:
1398  case Triple::msp430:
1399  case Triple::nios2:
1400  case Triple::nvptx64:
1401  case Triple::nvptx:
1402  case Triple::r600:
1403  case Triple::riscv32:
1404  case Triple::riscv64:
1405  case Triple::shave:
1406  case Triple::spir64:
1407  case Triple::spir:
1408  case Triple::wasm32:
1409  case Triple::wasm64:
1410  case Triple::x86:
1411  case Triple::x86_64:
1412  case Triple::xcore:
1415 
1416  // ARM is intentionally unsupported here, changing the architecture would
1417  // drop any arch suffixes.
1418  case Triple::arm:
1419  case Triple::thumb:
1420  T.setArch(UnknownArch);
1421  break;
1422 
1423  case Triple::tcele: T.setArch(Triple::tce); break;
1424  case Triple::aarch64: T.setArch(Triple::aarch64_be); break;
1425  case Triple::bpfel: T.setArch(Triple::bpfeb); break;
1426  case Triple::mips64el:T.setArch(Triple::mips64); break;
1427  case Triple::mipsel: T.setArch(Triple::mips); break;
1428  case Triple::ppc64le: T.setArch(Triple::ppc64); break;
1429  case Triple::sparcel: T.setArch(Triple::sparc); break;
1430  default:
1431  llvm_unreachable("getBigEndianArchVariant: unknown triple.");
1432  }
1433  return T;
1434 }
1435 
1437  Triple T(*this);
1438  if (isLittleEndian())
1439  return T;
1440 
1441  switch (getArch()) {
1442  case Triple::UnknownArch:
1443  case Triple::lanai:
1444  case Triple::ppc:
1445  case Triple::sparcv9:
1446  case Triple::systemz:
1447 
1448  // ARM is intentionally unsupported here, changing the architecture would
1449  // drop any arch suffixes.
1450  case Triple::armeb:
1451  case Triple::thumbeb:
1452  T.setArch(UnknownArch);
1453  break;
1454 
1455  case Triple::tce: T.setArch(Triple::tcele); break;
1456  case Triple::aarch64_be: T.setArch(Triple::aarch64); break;
1457  case Triple::bpfeb: T.setArch(Triple::bpfel); break;
1458  case Triple::mips64: T.setArch(Triple::mips64el); break;
1459  case Triple::mips: T.setArch(Triple::mipsel); break;
1460  case Triple::ppc64: T.setArch(Triple::ppc64le); break;
1461  case Triple::sparc: T.setArch(Triple::sparcel); break;
1462  default:
1463  llvm_unreachable("getLittleEndianArchVariant: unknown triple.");
1464  }
1465  return T;
1466 }
1467 
1469  switch (getArch()) {
1470  case Triple::aarch64:
1471  case Triple::amdgcn:
1472  case Triple::amdil64:
1473  case Triple::amdil:
1474  case Triple::arm:
1475  case Triple::avr:
1476  case Triple::bpfel:
1477  case Triple::hexagon:
1478  case Triple::hsail64:
1479  case Triple::hsail:
1480  case Triple::kalimba:
1481  case Triple::le32:
1482  case Triple::le64:
1483  case Triple::mips64el:
1484  case Triple::mipsel:
1485  case Triple::msp430:
1486  case Triple::nios2:
1487  case Triple::nvptx64:
1488  case Triple::nvptx:
1489  case Triple::ppc64le:
1490  case Triple::r600:
1491  case Triple::riscv32:
1492  case Triple::riscv64:
1493  case Triple::shave:
1494  case Triple::sparcel:
1495  case Triple::spir64:
1496  case Triple::spir:
1497  case Triple::thumb:
1498  case Triple::wasm32:
1499  case Triple::wasm64:
1500  case Triple::x86:
1501  case Triple::x86_64:
1502  case Triple::xcore:
1503  case Triple::tcele:
1506  return true;
1507  default:
1508  return false;
1509  }
1510 }
1511 
1512 bool Triple::isCompatibleWith(const Triple &Other) const {
1513  // ARM and Thumb triples are compatible, if subarch, vendor and OS match.
1514  if ((getArch() == Triple::thumb && Other.getArch() == Triple::arm) ||
1515  (getArch() == Triple::arm && Other.getArch() == Triple::thumb) ||
1516  (getArch() == Triple::thumbeb && Other.getArch() == Triple::armeb) ||
1517  (getArch() == Triple::armeb && Other.getArch() == Triple::thumbeb)) {
1518  if (getVendor() == Triple::Apple)
1519  return getSubArch() == Other.getSubArch() &&
1520  getVendor() == Other.getVendor() && getOS() == Other.getOS();
1521  else
1522  return getSubArch() == Other.getSubArch() &&
1523  getVendor() == Other.getVendor() && getOS() == Other.getOS() &&
1524  getEnvironment() == Other.getEnvironment() &&
1525  getObjectFormat() == Other.getObjectFormat();
1526  }
1527 
1528  // If vendor is apple, ignore the version number.
1529  if (getVendor() == Triple::Apple)
1530  return getArch() == Other.getArch() && getSubArch() == Other.getSubArch() &&
1531  getVendor() == Other.getVendor() && getOS() == Other.getOS();
1532 
1533  return *this == Other;
1534 }
1535 
1536 std::string Triple::merge(const Triple &Other) const {
1537  // If vendor is apple, pick the triple with the larger version number.
1538  if (getVendor() == Triple::Apple)
1539  if (Other.isOSVersionLT(*this))
1540  return str();
1541 
1542  return Other.str();
1543 }
1544 
1546  if (MArch.empty())
1547  MArch = getArchName();
1548  MArch = ARM::getCanonicalArchName(MArch);
1549 
1550  // Some defaults are forced.
1551  switch (getOS()) {
1552  case llvm::Triple::FreeBSD:
1553  case llvm::Triple::NetBSD:
1554  if (!MArch.empty() && MArch == "v6")
1555  return "arm1176jzf-s";
1556  break;
1557  case llvm::Triple::Win32:
1558  // FIXME: this is invalid for WindowsCE
1559  return "cortex-a9";
1560  case llvm::Triple::MacOSX:
1561  case llvm::Triple::IOS:
1562  case llvm::Triple::WatchOS:
1563  case llvm::Triple::TvOS:
1564  if (MArch == "v7k")
1565  return "cortex-a7";
1566  break;
1567  default:
1568  break;
1569  }
1570 
1571  if (MArch.empty())
1572  return StringRef();
1573 
1574  StringRef CPU = ARM::getDefaultCPU(MArch);
1575  if (!CPU.empty() && !CPU.equals("invalid"))
1576  return CPU;
1577 
1578  // If no specific architecture version is requested, return the minimum CPU
1579  // required by the OS and environment.
1580  switch (getOS()) {
1581  case llvm::Triple::NetBSD:
1582  switch (getEnvironment()) {
1584  case llvm::Triple::GNUEABI:
1585  case llvm::Triple::EABIHF:
1586  case llvm::Triple::EABI:
1587  return "arm926ej-s";
1588  default:
1589  return "strongarm";
1590  }
1591  case llvm::Triple::NaCl:
1592  case llvm::Triple::OpenBSD:
1593  return "cortex-a8";
1594  default:
1595  switch (getEnvironment()) {
1596  case llvm::Triple::EABIHF:
1599  return "arm1176jzf-s";
1600  default:
1601  return "arm7tdmi";
1602  }
1603  }
1604 
1605  llvm_unreachable("invalid arch name");
1606 }
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
Definition: Triple.h:468
void getiOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const
getiOSVersion - Parse the version number as with getOSVersion.
Definition: Triple.cpp:1063
static StringRef getVendorTypeName(VendorType Kind)
getVendorTypeName - Get the canonical name for the Kind vendor.
Definition: Triple.cpp:152
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:136
static Triple::ArchType parseBPFArch(StringRef ArchName)
Definition: Triple.cpp:242
OSType getOS() const
getOS - Get the parsed operating system type of this triple.
Definition: Triple.h:292
void setVendor(VendorType Kind)
setVendor - Set the vendor (second) component of the triple to a known type.
Definition: Triple.cpp:1121
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
bool isArch16Bit() const
Test whether the architecture is 16-bit.
Definition: Triple.cpp:1249
bool hasEnvironment() const
hasEnvironment - Does this triple have the optional environment (fourth) component?
Definition: Triple.h:296
static Triple::ArchType parseARMArch(StringRef ArchName)
Definition: Triple.cpp:314
StringRef getDefaultCPU(StringRef Arch)
void setOS(OSType Kind)
setOS - Set the operating system (third) component of the triple to a known type. ...
Definition: Triple.cpp:1125
void setEnvironment(EnvironmentType Kind)
setEnvironment - Set the environment (fourth) component of the triple to a known type.
Definition: Triple.cpp:1129
void getEnvironmentVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const
Parse the version number from the OS name component of the triple, if present.
Definition: Triple.cpp:999
StringRef getARMCPUForArch(StringRef Arch=StringRef()) const
Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
Definition: Triple.cpp:1545
static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch)
Definition: Triple.cpp:1177
static unsigned EatNumber(StringRef &Str)
Definition: Triple.cpp:964
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static StringRef getArchTypeName(ArchType Kind)
getArchTypeName - Get the canonical name for the Kind architecture.
Definition: Triple.cpp:20
ISAKind parseArchISA(StringRef Arch)
void setVendorName(StringRef Str)
setVendorName - Set the vendor (second) component of the triple by name.
Definition: Triple.cpp:1156
static Triple::OSType parseOS(StringRef OSName)
Definition: Triple.cpp:469
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(T Value)
Definition: StringSwitch.h:203
Triple()
Default constructor is the same as an empty string and leaves all triple fields unknown.
Definition: Triple.h:245
static bool startswith(StringRef Magic, const char(&S)[N])
Definition: Magic.cpp:30
SubArchType getSubArch() const
getSubArch - get the parsed subarchitecture type for this triple.
Definition: Triple.h:286
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
static const bool IsLittleEndianHost
Definition: Host.h:50
LLVM_ATTRIBUTE_ALWAYS_INLINE bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition: StringRef.h:678
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:283
#define T
bool isArch32Bit() const
Test whether the architecture is 32-bit.
Definition: Triple.cpp:1245
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
static StringRef getOSTypeName(OSType Kind)
getOSTypeName - Get the canonical name for the Kind operating system.
Definition: Triple.cpp:176
const std::string & str() const
Definition: Triple.h:352
unsigned parseArchVersion(StringRef Arch)
bool isOSWindows() const
Tests whether the OS is Windows.
Definition: Triple.h:560
void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const
getOSVersion - Parse the version number from the OS name component of the triple, if present...
Definition: Triple.cpp:1009
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
void setObjectFormat(ObjectFormatType Kind)
setObjectFormat - Set the object file format
Definition: Triple.cpp:1137
StringRef getOSAndEnvironmentName() const
getOSAndEnvironmentName - Get the operating system and optional environment components as a single st...
Definition: Triple.cpp:959
llvm::Triple get32BitArchVariant() const
Form a triple with a 32-bit variant of the current architecture.
Definition: Triple.cpp:1253
static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName)
Definition: Triple.cpp:529
bool isLittleEndian() const
Tests whether the target triple is little endian.
Definition: Triple.cpp:1468
VendorType getVendor() const
getVendor - Get the parsed vendor type of this triple.
Definition: Triple.h:289
void setEnvironmentName(StringRef Str)
setEnvironmentName - Set the optional environment (fourth) component of the triple by name...
Definition: Triple.cpp:1168
llvm::Triple getBigEndianArchVariant() const
Form a triple with a big endian variant of the current architecture.
Definition: Triple.cpp:1381
void setTriple(const Twine &Str)
setTriple - Set all components to the new triple Str.
Definition: Triple.cpp:1113
bool isCompatibleWith(const Triple &Other) const
Test whether target triples are compatible.
Definition: Triple.cpp:1512
static StringRef getArchTypePrefix(ArchType Kind)
getArchTypePrefix - Get the "prefix" canonical name for the Kind architecture.
Definition: Triple.cpp:79
void getWatchOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const
getWatchOSVersion - Parse the version number as with getOSVersion.
Definition: Triple.cpp:1089
void setArchName(StringRef Str)
setArchName - Set the architecture (first) component of the triple by name.
Definition: Triple.cpp:1145
static Triple::ObjectFormatType getDefaultFormat(const Triple &T)
Definition: Triple.cpp:615
static Triple::SubArchType parseSubArch(StringRef SubArchName)
Definition: Triple.cpp:538
void setOSName(StringRef Str)
setOSName - Set the operating system (third) component of the triple by name.
Definition: Triple.cpp:1160
EndianKind parseArchEndian(StringRef Arch)
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & EndsWith(StringLiteral S, T Value)
Definition: StringSwitch.h:78
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & StartsWith(StringLiteral S, T Value)
Definition: StringSwitch.h:86
static Triple::VendorType parseVendor(StringRef VendorName)
Definition: Triple.cpp:449
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
StringRef getArchName() const
getArchName - Get the architecture (first) component of the triple.
Definition: Triple.cpp:938
bool getMacOSXVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const
getMacOSXVersion - Parse the version number as with getOSVersion and then translate generic "darwin" ...
Definition: Triple.cpp:1022
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:862
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:727
static ArchType getArchTypeForLLVMName(StringRef Str)
getArchTypeForLLVMName - The canonical type for the given LLVM architecture name (e.g., "x86").
Definition: Triple.cpp:257
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
Definition: STLExtras.h:725
StringRef getOSName() const
getOSName - Get the operating system (third) component of the triple.
Definition: Triple.cpp:947
EnvironmentType getEnvironment() const
getEnvironment - Get the parsed environment type of this triple.
Definition: Triple.h:301
std::string merge(const Triple &Other) const
Merge target triples.
Definition: Triple.cpp:1536
ProfileKind parseArchProfile(StringRef Arch)
std::string normalize() const
Return the normalized form of this triple&#39;s string.
Definition: Triple.h:276
StringRef getEnvironmentName() const
getEnvironmentName - Get the optional environment (fourth) component of the triple, or "" if empty.
Definition: Triple.cpp:953
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:924
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:70
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:169
llvm::Triple getLittleEndianArchVariant() const
Form a triple with a little endian variant of the current architecture.
Definition: Triple.cpp:1436
ObjectFormatType getObjectFormat() const
getFormat - Get the object format for this triple.
Definition: Triple.h:313
void setOSAndEnvironmentName(StringRef Str)
setOSAndEnvironmentName - Set the operating system and optional environment components with a single ...
Definition: Triple.cpp:1173
bool isArch64Bit() const
Test whether the architecture is 64-bit.
Definition: Triple.cpp:1241
static void parseVersionFromName(StringRef Name, unsigned &Major, unsigned &Minor, unsigned &Micro)
Definition: Triple.cpp:979
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
Definition: StringSwitch.h:94
StringRef getVendorName() const
getVendorName - Get the vendor (second) component of the triple.
Definition: Triple.cpp:942
std::string str() const
Return the twine contents as a std::string.
Definition: Twine.cpp:17
static Triple::ArchType parseArch(StringRef ArchName)
Definition: Triple.cpp:379
ArchKind parseArch(StringRef Arch)
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ObjectFormatType
Definition: Triple.h:209
llvm::Triple get64BitArchVariant() const
Form a triple with a 64-bit variant of the current architecture.
Definition: Triple.cpp:1317
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
StringRef getCanonicalArchName(StringRef Arch)
bool isOSVersionLT(unsigned Major, unsigned Minor=0, unsigned Micro=0) const
isOSVersionLT - Helper function for doing comparisons against version numbers included in the target ...
Definition: Triple.h:401
static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName)
Definition: Triple.cpp:506
const uint64_t Version
Definition: InstrProf.h:895
void setArch(ArchType Kind)
setArch - Set the architecture (first) component of the triple to a known type.
Definition: Triple.cpp:1117
static StringRef getObjectFormatTypeName(Triple::ObjectFormatType Kind)
Definition: Triple.cpp:604
void resize(size_type N)
Definition: SmallVector.h:353
static StringRef getEnvironmentTypeName(EnvironmentType Kind)
getEnvironmentTypeName - Get the canonical name for the Kind environment.
Definition: Triple.cpp:216