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