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