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