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