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