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