[CallSite removal] Migrate all Alias Analysis APIs to use the newly
[llvm-complete.git] / lib / Support / Triple.cpp
blob4471fd05181eaf58bd4390d5c110169cd68acada
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 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/Triple.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include "llvm/Support/Host.h"
16 #include "llvm/Support/TargetParser.h"
17 #include <cstring>
18 using namespace llvm;
20 StringRef Triple::getArchTypeName(ArchType Kind) {
21 switch (Kind) {
22 case UnknownArch: return "unknown";
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";
76 llvm_unreachable("Invalid ArchType!");
79 StringRef Triple::getArchTypePrefix(ArchType Kind) {
80 switch (Kind) {
81 default:
82 return StringRef();
84 case aarch64:
85 case aarch64_be: return "aarch64";
87 case arc: return "arc";
89 case arm:
90 case armeb:
91 case thumb:
92 case thumbeb: return "arm";
94 case avr: return "avr";
96 case ppc64:
97 case ppc64le:
98 case ppc: return "ppc";
100 case mips:
101 case mipsel:
102 case mips64:
103 case mips64el: return "mips";
105 case nios2: return "nios2";
107 case hexagon: return "hexagon";
109 case amdgcn: return "amdgcn";
110 case r600: return "r600";
112 case bpfel:
113 case bpfeb: return "bpf";
115 case sparcv9:
116 case sparcel:
117 case sparc: return "sparc";
119 case systemz: return "s390";
121 case x86:
122 case x86_64: return "x86";
124 case xcore: return "xcore";
126 // NVPTX intrinsics are namespaced under nvvm.
127 case nvptx: return "nvvm";
128 case nvptx64: return "nvvm";
130 case le32: return "le32";
131 case le64: return "le64";
133 case amdil:
134 case amdil64: return "amdil";
136 case hsail:
137 case hsail64: return "hsail";
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";
147 case riscv32:
148 case riscv64: return "riscv";
152 StringRef Triple::getVendorTypeName(VendorType Kind) {
153 switch (Kind) {
154 case UnknownVendor: return "unknown";
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";
174 llvm_unreachable("Invalid VendorType!");
177 StringRef Triple::getOSTypeName(OSType Kind) {
178 switch (Kind) {
179 case UnknownOS: return "unknown";
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";
216 llvm_unreachable("Invalid OSType");
219 StringRef Triple::getEnvironmentTypeName(EnvironmentType Kind) {
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";
242 llvm_unreachable("Invalid EnvironmentType!");
245 static Triple::ArchType parseBPFArch(StringRef ArchName) {
246 if (ArchName.equals("bpf")) {
247 if (sys::IsLittleEndianHost)
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;
260 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
261 Triple::ArchType BPFArch(parseBPFArch(Name));
262 return StringSwitch<Triple::ArchType>(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)
314 .Default(UnknownArch);
317 static Triple::ArchType parseARMArch(StringRef ArchName) {
318 ARM::ISAKind ISA = ARM::parseArchISA(ArchName);
319 ARM::EndianKind ENDIAN = ARM::parseArchEndian(ArchName);
321 Triple::ArchType arch = Triple::UnknownArch;
322 switch (ENDIAN) {
323 case ARM::EndianKind::LITTLE: {
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;
331 case ARM::ISAKind::AARCH64:
332 arch = Triple::aarch64;
333 break;
334 case ARM::ISAKind::INVALID:
335 break;
337 break;
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;
347 case ARM::ISAKind::AARCH64:
348 arch = Triple::aarch64_be;
349 break;
350 case ARM::ISAKind::INVALID:
351 break;
353 break;
355 case ARM::EndianKind::INVALID: {
356 break;
360 ArchName = ARM::getCanonicalArchName(ArchName);
361 if (ArchName.empty())
362 return Triple::UnknownArch;
364 // Thumb only exists in v4+
365 if (ISA == ARM::ISAKind::THUMB &&
366 (ArchName.startswith("v2") || ArchName.startswith("v3")))
367 return Triple::UnknownArch;
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;
379 return arch;
382 static Triple::ArchType parseArch(StringRef ArchName) {
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",
406 Triple::mipsel)
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)
441 .Default(Triple::UnknownArch);
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);
453 return AT;
456 static Triple::VendorType parseVendor(StringRef VendorName) {
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)
465 .Case("img", Triple::ImaginationTechnologies)
466 .Case("mti", Triple::MipsTechnologies)
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)
474 .Default(Triple::UnknownVendor);
477 static Triple::OSType parseOS(StringRef OSName) {
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)
513 .Default(Triple::UnknownOS);
516 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
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)
536 .Default(Triple::UnknownEnvironment);
539 static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
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)
545 .Default(Triple::UnknownObjectFormat);
548 static Triple::SubArchType parseSubArch(StringRef SubArchName) {
549 if (SubArchName.startswith("mips") &&
550 (SubArchName.endswith("r6el") || SubArchName.endswith("r6")))
551 return Triple::MipsSubArch_r6;
553 StringRef ARMSubArch = ARM::getCanonicalArchName(SubArchName);
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)
561 .Default(Triple::NoSubArch);
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:
576 return Triple::ARMSubArch_v5te;
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:
583 return Triple::ARMSubArch_v6t2;
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:
590 return Triple::ARMSubArch_v7ve;
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:
598 return Triple::ARMSubArch_v7em;
599 case ARM::ArchKind::ARMV8A:
600 return Triple::ARMSubArch_v8;
601 case ARM::ArchKind::ARMV8_1A:
602 return Triple::ARMSubArch_v8_1a;
603 case ARM::ArchKind::ARMV8_2A:
604 return Triple::ARMSubArch_v8_2a;
605 case ARM::ArchKind::ARMV8_3A:
606 return Triple::ARMSubArch_v8_3a;
607 case ARM::ArchKind::ARMV8_4A:
608 return Triple::ARMSubArch_v8_4a;
609 case ARM::ArchKind::ARMV8_5A:
610 return Triple::ARMSubArch_v8_5a;
611 case ARM::ArchKind::ARMV8R:
612 return Triple::ARMSubArch_v8r;
613 case ARM::ArchKind::ARMV8MBaseline:
614 return Triple::ARMSubArch_v8m_baseline;
615 case ARM::ArchKind::ARMV8MMainline:
616 return Triple::ARMSubArch_v8m_mainline;
617 default:
618 return Triple::NoSubArch;
622 static StringRef getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
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";
630 llvm_unreachable("unknown object format type");
633 static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
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;
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:
673 case Triple::renderscript32:
674 case Triple::renderscript64:
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;
690 case Triple::ppc:
691 case Triple::ppc64:
692 if (T.isOSDarwin())
693 return Triple::MachO;
694 return Triple::ELF;
696 case Triple::wasm32:
697 case Triple::wasm64:
698 return Triple::Wasm;
700 llvm_unreachable("unknown architecture");
703 /// Construct a triple from the string representation provided.
705 /// This stores the string representation and parses the various pieces into
706 /// enum members.
707 Triple::Triple(const Twine &Str)
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]);
726 } else {
727 Environment =
728 StringSwitch<Triple::EnvironmentType>(Components[0])
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)
734 .Default(UnknownEnvironment);
737 if (ObjectFormat == UnknownObjectFormat)
738 ObjectFormat = getDefaultFormat(*this);
741 /// Construct a triple from string representations of the architecture,
742 /// vendor, and OS.
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);
757 /// Construct a triple from string representations of the architecture,
758 /// vendor, OS, and environment.
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);
776 std::string Triple::normalize(StringRef Str) {
777 bool IsMinGW32 = false;
778 bool IsCygwin = false;
780 // Parse into components.
781 SmallVector<StringRef, 4> Components;
782 Str.split(Components, '-');
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");
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]);
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;
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.
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;
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;
854 break;
856 if (!Valid)
857 continue; // Nope, try the next component.
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]);
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])
897 // The last component was pushed off the end - append it.
898 if (!CurrentComponent.empty())
899 Components.push_back(CurrentComponent);
901 // Advance Idx to the component's new position.
902 while (++Idx < array_lengthof(Found) && Found[Idx])
904 } while (Idx < Pos); // Add more until the final position is reached.
906 assert(Pos < Components.size() && Components[Pos] == Comp &&
907 "Component moved wrong!");
908 Found[Pos] = true;
909 break;
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";
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;
932 // SUSE uses "gnueabi" to mean "gnueabihf"
933 if (Vendor == Triple::SUSE && Environment == llvm::Triple::GNUEABI)
934 Components[3] = "gnueabihf";
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);
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";
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);
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];
968 return Normalized;
971 StringRef Triple::getArchName() const {
972 return StringRef(Data).split('-').first; // Isolate first component
975 StringRef Triple::getVendorName() const {
976 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
977 return Tmp.split('-').first; // Isolate second component
980 StringRef Triple::getOSName() const {
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
986 StringRef Triple::getEnvironmentName() const {
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
992 StringRef Triple::getOSAndEnvironmentName() const {
993 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
994 return Tmp.split('-').second; // Strip second component
997 static unsigned EatNumber(StringRef &Str) {
998 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
999 unsigned Result = 0;
1001 do {
1002 // Consume the leading digit.
1003 Result = Result*10 + (Str[0] - '0');
1005 // Eat the digit.
1006 Str = Str.substr(1);
1007 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
1009 return Result;
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;
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;
1023 // Consume the leading number.
1024 *Components[i] = EatNumber(Name);
1026 // Consume the separator, if present.
1027 if (Name.startswith("."))
1028 Name = Name.substr(1);
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());
1039 parseVersionFromName(EnvironmentName, Major, Minor, Micro);
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");
1052 parseVersionFromName(OSName, Major, Minor, Micro);
1055 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
1056 unsigned &Micro) const {
1057 getOSVersion(Major, Minor, Micro);
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;
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;
1093 return true;
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");
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");
1146 void Triple::setTriple(const Twine &Str) {
1147 *this = Triple(Str);
1150 void Triple::setArch(ArchType Kind) {
1151 setArchName(getArchTypeName(Kind));
1154 void Triple::setVendor(VendorType Kind) {
1155 setVendorName(getVendorTypeName(Kind));
1158 void Triple::setOS(OSType Kind) {
1159 setOSName(getOSTypeName(Kind));
1162 void Triple::setEnvironment(EnvironmentType Kind) {
1163 if (ObjectFormat == getDefaultFormat(*this))
1164 return setEnvironmentName(getEnvironmentTypeName(Kind));
1166 setEnvironmentName((getEnvironmentTypeName(Kind) + Twine("-") +
1167 getObjectFormatTypeName(ObjectFormat)).str());
1170 void Triple::setObjectFormat(ObjectFormatType Kind) {
1171 if (Environment == UnknownEnvironment)
1172 return setEnvironmentName(getObjectFormatTypeName(Kind));
1174 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
1175 getObjectFormatTypeName(Kind)).str());
1178 void Triple::setArchName(StringRef Str) {
1179 // Work around a miscompilation bug for Twines in gcc 4.0.3.
1180 SmallString<64> Triple;
1181 Triple += Str;
1182 Triple += "-";
1183 Triple += getVendorName();
1184 Triple += "-";
1185 Triple += getOSAndEnvironmentName();
1186 setTriple(Triple);
1189 void Triple::setVendorName(StringRef Str) {
1190 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
1193 void Triple::setOSName(StringRef Str) {
1194 if (hasEnvironment())
1195 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
1196 "-" + getEnvironmentName());
1197 else
1198 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
1201 void Triple::setEnvironmentName(StringRef Str) {
1202 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
1203 "-" + Str);
1206 void Triple::setOSAndEnvironmentName(StringRef Str) {
1207 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
1210 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
1211 switch (Arch) {
1212 case llvm::Triple::UnknownArch:
1213 return 0;
1215 case llvm::Triple::avr:
1216 case llvm::Triple::msp430:
1217 return 16;
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:
1246 case llvm::Triple::renderscript32:
1247 return 32;
1249 case llvm::Triple::aarch64:
1250 case llvm::Triple::aarch64_be:
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:
1256 case llvm::Triple::mips64el:
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:
1268 case llvm::Triple::renderscript64:
1269 return 64;
1271 llvm_unreachable("Invalid architecture value");
1274 bool Triple::isArch64Bit() const {
1275 return getArchPointerBitWidth(getArch()) == 64;
1278 bool Triple::isArch32Bit() const {
1279 return getArchPointerBitWidth(getArch()) == 32;
1282 bool Triple::isArch16Bit() const {
1283 return getArchPointerBitWidth(getArch()) == 16;
1286 Triple Triple::get32BitArchVariant() const {
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;
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:
1327 case Triple::renderscript32:
1328 // Already 32-bit.
1329 break;
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;
1345 case Triple::renderscript64: T.setArch(Triple::renderscript32); break;
1347 return T;
1350 Triple Triple::get64BitArchVariant() const {
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;
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:
1389 case Triple::renderscript64:
1390 // Already 64-bit.
1391 break;
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;
1409 case Triple::renderscript32: T.setArch(Triple::renderscript64); break;
1411 return T;
1414 Triple Triple::getBigEndianArchVariant() const {
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:
1446 case Triple::renderscript32:
1447 case Triple::renderscript64:
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;
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.");
1466 return T;
1469 Triple Triple::getLittleEndianArchVariant() const {
1470 Triple T(*this);
1471 if (isLittleEndian())
1472 return T;
1474 switch (getArch()) {
1475 case Triple::UnknownArch:
1476 case Triple::lanai:
1477 case Triple::ppc:
1478 case Triple::sparcv9:
1479 case Triple::systemz:
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;
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.");
1498 return T;
1501 bool Triple::isLittleEndian() const {
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:
1537 case Triple::renderscript32:
1538 case Triple::renderscript64:
1539 return true;
1540 default:
1541 return false;
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();
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();
1566 return *this == Other;
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();
1575 return Other.str();
1578 StringRef Triple::getARMCPUForArch(StringRef MArch) const {
1579 if (MArch.empty())
1580 MArch = getArchName();
1581 MArch = ARM::getCanonicalArchName(MArch);
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;
1604 if (MArch.empty())
1605 return StringRef();
1607 StringRef CPU = ARM::getDefaultCPU(MArch);
1608 if (!CPU.empty() && !CPU.equals("invalid"))
1609 return CPU;
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()) {
1616 case llvm::Triple::GNUEABIHF:
1617 case llvm::Triple::GNUEABI:
1618 case llvm::Triple::EABIHF:
1619 case llvm::Triple::EABI:
1620 return "arm926ej-s";
1621 default:
1622 return "strongarm";
1624 case llvm::Triple::NaCl:
1625 case llvm::Triple::OpenBSD:
1626 return "cortex-a8";
1627 default:
1628 switch (getEnvironment()) {
1629 case llvm::Triple::EABIHF:
1630 case llvm::Triple::GNUEABIHF:
1631 case llvm::Triple::MuslEABIHF:
1632 return "arm1176jzf-s";
1633 default:
1634 return "arm7tdmi";
1638 llvm_unreachable("invalid arch name");