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