180814287SRaphael Isemann //===-- ArchSpec.cpp ------------------------------------------------------===//
25f19b907SPavel Labath //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65f19b907SPavel Labath //
75f19b907SPavel Labath //===----------------------------------------------------------------------===//
85f19b907SPavel Labath 
95f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h"
10c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
115f19b907SPavel Labath 
12c9f79391SJason Molenda #include "lldb/Utility/Log.h"
135f19b907SPavel Labath #include "lldb/Utility/StringList.h"
14672d2c12SJonas Devlieghere #include "lldb/lldb-defines.h"
155f19b907SPavel Labath #include "llvm/ADT/STLExtras.h"
165f19b907SPavel Labath #include "llvm/BinaryFormat/COFF.h"
175f19b907SPavel Labath #include "llvm/BinaryFormat/ELF.h"
18672d2c12SJonas Devlieghere #include "llvm/BinaryFormat/MachO.h"
19672d2c12SJonas Devlieghere #include "llvm/Support/Compiler.h"
205f19b907SPavel Labath 
215f19b907SPavel Labath using namespace lldb;
225f19b907SPavel Labath using namespace lldb_private;
235f19b907SPavel Labath 
245f19b907SPavel Labath static bool cores_match(const ArchSpec::Core core1, const ArchSpec::Core core2,
255f19b907SPavel Labath                         bool try_inverse, bool enforce_exact_match);
265f19b907SPavel Labath 
275f19b907SPavel Labath namespace lldb_private {
285f19b907SPavel Labath 
295f19b907SPavel Labath struct CoreDefinition {
305f19b907SPavel Labath   ByteOrder default_byte_order;
315f19b907SPavel Labath   uint32_t addr_byte_size;
325f19b907SPavel Labath   uint32_t min_opcode_byte_size;
335f19b907SPavel Labath   uint32_t max_opcode_byte_size;
345f19b907SPavel Labath   llvm::Triple::ArchType machine;
355f19b907SPavel Labath   ArchSpec::Core core;
365f19b907SPavel Labath   const char *const name;
375f19b907SPavel Labath };
385f19b907SPavel Labath 
395f19b907SPavel Labath } // namespace lldb_private
405f19b907SPavel Labath 
415f19b907SPavel Labath // This core information can be looked using the ArchSpec::Core as the index
425f19b907SPavel Labath static const CoreDefinition g_core_definitions[] = {
435f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_generic,
445f19b907SPavel Labath      "arm"},
455f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv4,
465f19b907SPavel Labath      "armv4"},
475f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv4t,
485f19b907SPavel Labath      "armv4t"},
495f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv5,
505f19b907SPavel Labath      "armv5"},
515f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv5e,
525f19b907SPavel Labath      "armv5e"},
535f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv5t,
545f19b907SPavel Labath      "armv5t"},
555f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv6,
565f19b907SPavel Labath      "armv6"},
575f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv6m,
585f19b907SPavel Labath      "armv6m"},
595f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7,
605f19b907SPavel Labath      "armv7"},
619b958356SMuhammad Omair Javaid     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7l,
629b958356SMuhammad Omair Javaid      "armv7l"},
635f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7f,
645f19b907SPavel Labath      "armv7f"},
655f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7s,
665f19b907SPavel Labath      "armv7s"},
675f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7k,
685f19b907SPavel Labath      "armv7k"},
695f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7m,
705f19b907SPavel Labath      "armv7m"},
715f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7em,
725f19b907SPavel Labath      "armv7em"},
735f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_xscale,
745f19b907SPavel Labath      "xscale"},
755f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumb,
765f19b907SPavel Labath      "thumb"},
775f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv4t,
785f19b907SPavel Labath      "thumbv4t"},
795f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv5,
805f19b907SPavel Labath      "thumbv5"},
815f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv5e,
825f19b907SPavel Labath      "thumbv5e"},
835f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv6,
845f19b907SPavel Labath      "thumbv6"},
855f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv6m,
865f19b907SPavel Labath      "thumbv6m"},
875f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7,
885f19b907SPavel Labath      "thumbv7"},
895f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7f,
905f19b907SPavel Labath      "thumbv7f"},
915f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7s,
925f19b907SPavel Labath      "thumbv7s"},
935f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7k,
945f19b907SPavel Labath      "thumbv7k"},
955f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7m,
965f19b907SPavel Labath      "thumbv7m"},
975f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7em,
985f19b907SPavel Labath      "thumbv7em"},
995f19b907SPavel Labath     {eByteOrderLittle, 8, 4, 4, llvm::Triple::aarch64,
1005f19b907SPavel Labath      ArchSpec::eCore_arm_arm64, "arm64"},
1015f19b907SPavel Labath     {eByteOrderLittle, 8, 4, 4, llvm::Triple::aarch64,
1025f19b907SPavel Labath      ArchSpec::eCore_arm_armv8, "armv8"},
103baf6c298SJonas Devlieghere     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv8l,
104baf6c298SJonas Devlieghere      "armv8l"},
105baf6c298SJonas Devlieghere     {eByteOrderLittle, 8, 4, 4, llvm::Triple::aarch64,
106baf6c298SJonas Devlieghere      ArchSpec::eCore_arm_arm64e, "arm64e"},
1077dd7a360SJason Molenda     {eByteOrderLittle, 4, 4, 4, llvm::Triple::aarch64_32,
1087dd7a360SJason Molenda      ArchSpec::eCore_arm_arm64_32, "arm64_32"},
1095f19b907SPavel Labath     {eByteOrderLittle, 8, 4, 4, llvm::Triple::aarch64,
1105f19b907SPavel Labath      ArchSpec::eCore_arm_aarch64, "aarch64"},
1115f19b907SPavel Labath 
1125f19b907SPavel Labath     // mips32, mips32r2, mips32r3, mips32r5, mips32r6
1135f19b907SPavel Labath     {eByteOrderBig, 4, 2, 4, llvm::Triple::mips, ArchSpec::eCore_mips32,
1145f19b907SPavel Labath      "mips"},
1155f19b907SPavel Labath     {eByteOrderBig, 4, 2, 4, llvm::Triple::mips, ArchSpec::eCore_mips32r2,
1165f19b907SPavel Labath      "mipsr2"},
1175f19b907SPavel Labath     {eByteOrderBig, 4, 2, 4, llvm::Triple::mips, ArchSpec::eCore_mips32r3,
1185f19b907SPavel Labath      "mipsr3"},
1195f19b907SPavel Labath     {eByteOrderBig, 4, 2, 4, llvm::Triple::mips, ArchSpec::eCore_mips32r5,
1205f19b907SPavel Labath      "mipsr5"},
1215f19b907SPavel Labath     {eByteOrderBig, 4, 2, 4, llvm::Triple::mips, ArchSpec::eCore_mips32r6,
1225f19b907SPavel Labath      "mipsr6"},
1235f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel, ArchSpec::eCore_mips32el,
1245f19b907SPavel Labath      "mipsel"},
1255f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel,
1265f19b907SPavel Labath      ArchSpec::eCore_mips32r2el, "mipsr2el"},
1275f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel,
1285f19b907SPavel Labath      ArchSpec::eCore_mips32r3el, "mipsr3el"},
1295f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel,
1305f19b907SPavel Labath      ArchSpec::eCore_mips32r5el, "mipsr5el"},
1315f19b907SPavel Labath     {eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel,
1325f19b907SPavel Labath      ArchSpec::eCore_mips32r6el, "mipsr6el"},
1335f19b907SPavel Labath 
1345f19b907SPavel Labath     // mips64, mips64r2, mips64r3, mips64r5, mips64r6
1355f19b907SPavel Labath     {eByteOrderBig, 8, 2, 4, llvm::Triple::mips64, ArchSpec::eCore_mips64,
1365f19b907SPavel Labath      "mips64"},
1375f19b907SPavel Labath     {eByteOrderBig, 8, 2, 4, llvm::Triple::mips64, ArchSpec::eCore_mips64r2,
1385f19b907SPavel Labath      "mips64r2"},
1395f19b907SPavel Labath     {eByteOrderBig, 8, 2, 4, llvm::Triple::mips64, ArchSpec::eCore_mips64r3,
1405f19b907SPavel Labath      "mips64r3"},
1415f19b907SPavel Labath     {eByteOrderBig, 8, 2, 4, llvm::Triple::mips64, ArchSpec::eCore_mips64r5,
1425f19b907SPavel Labath      "mips64r5"},
1435f19b907SPavel Labath     {eByteOrderBig, 8, 2, 4, llvm::Triple::mips64, ArchSpec::eCore_mips64r6,
1445f19b907SPavel Labath      "mips64r6"},
1455f19b907SPavel Labath     {eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el,
1465f19b907SPavel Labath      ArchSpec::eCore_mips64el, "mips64el"},
1475f19b907SPavel Labath     {eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el,
1485f19b907SPavel Labath      ArchSpec::eCore_mips64r2el, "mips64r2el"},
1495f19b907SPavel Labath     {eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el,
1505f19b907SPavel Labath      ArchSpec::eCore_mips64r3el, "mips64r3el"},
1515f19b907SPavel Labath     {eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el,
1525f19b907SPavel Labath      ArchSpec::eCore_mips64r5el, "mips64r5el"},
1535f19b907SPavel Labath     {eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el,
1545f19b907SPavel Labath      ArchSpec::eCore_mips64r6el, "mips64r6el"},
1555f19b907SPavel Labath 
1565f19b907SPavel Labath     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_generic,
1575f19b907SPavel Labath      "powerpc"},
1585f19b907SPavel Labath     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc601,
1595f19b907SPavel Labath      "ppc601"},
1605f19b907SPavel Labath     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc602,
1615f19b907SPavel Labath      "ppc602"},
1625f19b907SPavel Labath     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc603,
1635f19b907SPavel Labath      "ppc603"},
1645f19b907SPavel Labath     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc603e,
1655f19b907SPavel Labath      "ppc603e"},
1665f19b907SPavel Labath     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc603ev,
1675f19b907SPavel Labath      "ppc603ev"},
1685f19b907SPavel Labath     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc604,
1695f19b907SPavel Labath      "ppc604"},
1705f19b907SPavel Labath     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc604e,
1715f19b907SPavel Labath      "ppc604e"},
1725f19b907SPavel Labath     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc620,
1735f19b907SPavel Labath      "ppc620"},
1745f19b907SPavel Labath     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc750,
1755f19b907SPavel Labath      "ppc750"},
1765f19b907SPavel Labath     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc7400,
1775f19b907SPavel Labath      "ppc7400"},
1785f19b907SPavel Labath     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc7450,
1795f19b907SPavel Labath      "ppc7450"},
1805f19b907SPavel Labath     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc970,
1815f19b907SPavel Labath      "ppc970"},
1825f19b907SPavel Labath 
1835f19b907SPavel Labath     {eByteOrderLittle, 8, 4, 4, llvm::Triple::ppc64le,
1845f19b907SPavel Labath      ArchSpec::eCore_ppc64le_generic, "powerpc64le"},
1855f19b907SPavel Labath     {eByteOrderBig, 8, 4, 4, llvm::Triple::ppc64, ArchSpec::eCore_ppc64_generic,
1865f19b907SPavel Labath      "powerpc64"},
1875f19b907SPavel Labath     {eByteOrderBig, 8, 4, 4, llvm::Triple::ppc64,
1885f19b907SPavel Labath      ArchSpec::eCore_ppc64_ppc970_64, "ppc970-64"},
1895f19b907SPavel Labath 
1905f19b907SPavel Labath     {eByteOrderBig, 8, 2, 6, llvm::Triple::systemz,
1915f19b907SPavel Labath      ArchSpec::eCore_s390x_generic, "s390x"},
1925f19b907SPavel Labath 
1935f19b907SPavel Labath     {eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc,
1945f19b907SPavel Labath      ArchSpec::eCore_sparc_generic, "sparc"},
1955f19b907SPavel Labath     {eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9,
1965f19b907SPavel Labath      ArchSpec::eCore_sparc9_generic, "sparcv9"},
1975f19b907SPavel Labath 
1985f19b907SPavel Labath     {eByteOrderLittle, 4, 1, 15, llvm::Triple::x86, ArchSpec::eCore_x86_32_i386,
1995f19b907SPavel Labath      "i386"},
2005f19b907SPavel Labath     {eByteOrderLittle, 4, 1, 15, llvm::Triple::x86, ArchSpec::eCore_x86_32_i486,
2015f19b907SPavel Labath      "i486"},
2025f19b907SPavel Labath     {eByteOrderLittle, 4, 1, 15, llvm::Triple::x86,
2035f19b907SPavel Labath      ArchSpec::eCore_x86_32_i486sx, "i486sx"},
2045f19b907SPavel Labath     {eByteOrderLittle, 4, 1, 15, llvm::Triple::x86, ArchSpec::eCore_x86_32_i686,
2055f19b907SPavel Labath      "i686"},
2065f19b907SPavel Labath 
2075f19b907SPavel Labath     {eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64,
2085f19b907SPavel Labath      ArchSpec::eCore_x86_64_x86_64, "x86_64"},
2095f19b907SPavel Labath     {eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64,
2105f19b907SPavel Labath      ArchSpec::eCore_x86_64_x86_64h, "x86_64h"},
2115f19b907SPavel Labath     {eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon,
2125f19b907SPavel Labath      ArchSpec::eCore_hexagon_generic, "hexagon"},
2135f19b907SPavel Labath     {eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon,
2145f19b907SPavel Labath      ArchSpec::eCore_hexagon_hexagonv4, "hexagonv4"},
2155f19b907SPavel Labath     {eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon,
2165f19b907SPavel Labath      ArchSpec::eCore_hexagon_hexagonv5, "hexagonv5"},
2175f19b907SPavel Labath 
21815f59711SLuís Marques     {eByteOrderLittle, 4, 2, 4, llvm::Triple::riscv32, ArchSpec::eCore_riscv32,
21915f59711SLuís Marques      "riscv32"},
22015f59711SLuís Marques     {eByteOrderLittle, 8, 2, 4, llvm::Triple::riscv64, ArchSpec::eCore_riscv64,
22115f59711SLuís Marques      "riscv64"},
22215f59711SLuís Marques 
2235f19b907SPavel Labath     {eByteOrderLittle, 4, 4, 4, llvm::Triple::UnknownArch,
2245f19b907SPavel Labath      ArchSpec::eCore_uknownMach32, "unknown-mach-32"},
2255f19b907SPavel Labath     {eByteOrderLittle, 8, 4, 4, llvm::Triple::UnknownArch,
2265f19b907SPavel Labath      ArchSpec::eCore_uknownMach64, "unknown-mach-64"},
2274bafceceSPaolo Severini     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arc, ArchSpec::eCore_arc, "arc"},
2284bafceceSPaolo Severini 
229727ed11bSAyke van Laethem     {eByteOrderLittle, 2, 2, 4, llvm::Triple::avr, ArchSpec::eCore_avr, "avr"},
230727ed11bSAyke van Laethem 
2314bafceceSPaolo Severini     {eByteOrderLittle, 4, 1, 4, llvm::Triple::wasm32, ArchSpec::eCore_wasm32,
2324bafceceSPaolo Severini      "wasm32"},
233f8819bd5SJonas Devlieghere };
2345f19b907SPavel Labath 
2355f19b907SPavel Labath // Ensure that we have an entry in the g_core_definitions for each core. If you
23605097246SAdrian Prantl // comment out an entry above, you will need to comment out the corresponding
23705097246SAdrian Prantl // ArchSpec::Core enumeration.
2385f19b907SPavel Labath static_assert(sizeof(g_core_definitions) / sizeof(CoreDefinition) ==
2395f19b907SPavel Labath                   ArchSpec::kNumCores,
2405f19b907SPavel Labath               "make sure we have one core definition for each core");
2415f19b907SPavel Labath 
2425f19b907SPavel Labath struct ArchDefinitionEntry {
2435f19b907SPavel Labath   ArchSpec::Core core;
2445f19b907SPavel Labath   uint32_t cpu;
2455f19b907SPavel Labath   uint32_t sub;
2465f19b907SPavel Labath   uint32_t cpu_mask;
2475f19b907SPavel Labath   uint32_t sub_mask;
2485f19b907SPavel Labath };
2495f19b907SPavel Labath 
2505f19b907SPavel Labath struct ArchDefinition {
2515f19b907SPavel Labath   ArchitectureType type;
2525f19b907SPavel Labath   size_t num_entries;
2535f19b907SPavel Labath   const ArchDefinitionEntry *entries;
2545f19b907SPavel Labath   const char *name;
2555f19b907SPavel Labath };
2565f19b907SPavel Labath 
ListSupportedArchNames(StringList & list)257a2e76c0bSRaphael Isemann void ArchSpec::ListSupportedArchNames(StringList &list) {
258f15014ffSBenjamin Kramer   for (uint32_t i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
259a2e76c0bSRaphael Isemann     list.AppendString(g_core_definitions[i].name);
260a2e76c0bSRaphael Isemann }
261a2e76c0bSRaphael Isemann 
AutoComplete(CompletionRequest & request)262ae34ed2cSRaphael Isemann void ArchSpec::AutoComplete(CompletionRequest &request) {
263f15014ffSBenjamin Kramer   for (uint32_t i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
26493ca36d7SRaphael Isemann     request.TryCompleteCurrentArg(g_core_definitions[i].name);
2655f19b907SPavel Labath }
2665f19b907SPavel Labath 
2675f19b907SPavel Labath #define CPU_ANY (UINT32_MAX)
2685f19b907SPavel Labath 
2695f19b907SPavel Labath //===----------------------------------------------------------------------===//
2705f19b907SPavel Labath // A table that gets searched linearly for matches. This table is used to
2715f19b907SPavel Labath // convert cpu type and subtypes to architecture names, and to convert
2725f19b907SPavel Labath // architecture names to cpu types and subtypes. The ordering is important and
2735f19b907SPavel Labath // allows the precedence to be set when the table is built.
2745f19b907SPavel Labath #define SUBTYPE_MASK 0x00FFFFFFu
2755f19b907SPavel Labath 
27654710b34SJonas Devlieghere // clang-format off
2775f19b907SPavel Labath static const ArchDefinitionEntry g_macho_arch_entries[] = {
27854710b34SJonas Devlieghere     {ArchSpec::eCore_arm_generic,     llvm::MachO::CPU_TYPE_ARM,        CPU_ANY,                                UINT32_MAX, UINT32_MAX},
27954710b34SJonas Devlieghere     {ArchSpec::eCore_arm_generic,     llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_ALL,       UINT32_MAX, SUBTYPE_MASK},
28054710b34SJonas Devlieghere     {ArchSpec::eCore_arm_armv4,       llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V4T,       UINT32_MAX, SUBTYPE_MASK},
28154710b34SJonas Devlieghere     {ArchSpec::eCore_arm_armv4t,      llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V4T,       UINT32_MAX, SUBTYPE_MASK},
28254710b34SJonas Devlieghere     {ArchSpec::eCore_arm_armv6,       llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V6,        UINT32_MAX, SUBTYPE_MASK},
28354710b34SJonas Devlieghere     {ArchSpec::eCore_arm_armv6m,      llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V6M,       UINT32_MAX, SUBTYPE_MASK},
28454710b34SJonas Devlieghere     {ArchSpec::eCore_arm_armv5,       llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V5TEJ,     UINT32_MAX, SUBTYPE_MASK},
28554710b34SJonas Devlieghere     {ArchSpec::eCore_arm_armv5e,      llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V5TEJ,     UINT32_MAX, SUBTYPE_MASK},
28654710b34SJonas Devlieghere     {ArchSpec::eCore_arm_armv5t,      llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V5TEJ,     UINT32_MAX, SUBTYPE_MASK},
28754710b34SJonas Devlieghere     {ArchSpec::eCore_arm_xscale,      llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_XSCALE,    UINT32_MAX, SUBTYPE_MASK},
28854710b34SJonas Devlieghere     {ArchSpec::eCore_arm_armv7,       llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V7,        UINT32_MAX, SUBTYPE_MASK},
28954710b34SJonas Devlieghere     {ArchSpec::eCore_arm_armv7f,      llvm::MachO::CPU_TYPE_ARM,        10,                                     UINT32_MAX, SUBTYPE_MASK},
29054710b34SJonas Devlieghere     {ArchSpec::eCore_arm_armv7s,      llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V7S,       UINT32_MAX, SUBTYPE_MASK},
29154710b34SJonas Devlieghere     {ArchSpec::eCore_arm_armv7k,      llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V7K,       UINT32_MAX, SUBTYPE_MASK},
29254710b34SJonas Devlieghere     {ArchSpec::eCore_arm_armv7m,      llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V7M,       UINT32_MAX, SUBTYPE_MASK},
29354710b34SJonas Devlieghere     {ArchSpec::eCore_arm_armv7em,     llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V7EM,      UINT32_MAX, SUBTYPE_MASK},
294baf6c298SJonas Devlieghere     {ArchSpec::eCore_arm_arm64e,      llvm::MachO::CPU_TYPE_ARM64,      llvm::MachO::CPU_SUBTYPE_ARM64E,        UINT32_MAX, SUBTYPE_MASK},
29554710b34SJonas Devlieghere     {ArchSpec::eCore_arm_arm64,       llvm::MachO::CPU_TYPE_ARM64,      llvm::MachO::CPU_SUBTYPE_ARM64_ALL,     UINT32_MAX, SUBTYPE_MASK},
29698e50a7dSJonas Devlieghere     {ArchSpec::eCore_arm_arm64,       llvm::MachO::CPU_TYPE_ARM64,      llvm::MachO::CPU_SUBTYPE_ARM64_V8,      UINT32_MAX, SUBTYPE_MASK},
29754710b34SJonas Devlieghere     {ArchSpec::eCore_arm_arm64,       llvm::MachO::CPU_TYPE_ARM64,      13,                                     UINT32_MAX, SUBTYPE_MASK},
29854710b34SJonas Devlieghere     {ArchSpec::eCore_arm_arm64_32,    llvm::MachO::CPU_TYPE_ARM64_32,   0,                                      UINT32_MAX, SUBTYPE_MASK},
29954710b34SJonas Devlieghere     {ArchSpec::eCore_arm_arm64_32,    llvm::MachO::CPU_TYPE_ARM64_32,   1,                                      UINT32_MAX, SUBTYPE_MASK},
30054710b34SJonas Devlieghere     {ArchSpec::eCore_arm_arm64,       llvm::MachO::CPU_TYPE_ARM64,      CPU_ANY,                                UINT32_MAX, SUBTYPE_MASK},
30154710b34SJonas Devlieghere     {ArchSpec::eCore_thumb,           llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_ALL,       UINT32_MAX, SUBTYPE_MASK},
30254710b34SJonas Devlieghere     {ArchSpec::eCore_thumbv4t,        llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V4T,       UINT32_MAX, SUBTYPE_MASK},
30354710b34SJonas Devlieghere     {ArchSpec::eCore_thumbv5,         llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V5,        UINT32_MAX, SUBTYPE_MASK},
30454710b34SJonas Devlieghere     {ArchSpec::eCore_thumbv5e,        llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V5,        UINT32_MAX, SUBTYPE_MASK},
30554710b34SJonas Devlieghere     {ArchSpec::eCore_thumbv6,         llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V6,        UINT32_MAX, SUBTYPE_MASK},
30654710b34SJonas Devlieghere     {ArchSpec::eCore_thumbv6m,        llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V6M,       UINT32_MAX, SUBTYPE_MASK},
30754710b34SJonas Devlieghere     {ArchSpec::eCore_thumbv7,         llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V7,        UINT32_MAX, SUBTYPE_MASK},
30854710b34SJonas Devlieghere     {ArchSpec::eCore_thumbv7f,        llvm::MachO::CPU_TYPE_ARM,        10,                                     UINT32_MAX, SUBTYPE_MASK},
30954710b34SJonas Devlieghere     {ArchSpec::eCore_thumbv7s,        llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V7S,       UINT32_MAX, SUBTYPE_MASK},
31054710b34SJonas Devlieghere     {ArchSpec::eCore_thumbv7k,        llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V7K,       UINT32_MAX, SUBTYPE_MASK},
31154710b34SJonas Devlieghere     {ArchSpec::eCore_thumbv7m,        llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V7M,       UINT32_MAX, SUBTYPE_MASK},
31254710b34SJonas Devlieghere     {ArchSpec::eCore_thumbv7em,       llvm::MachO::CPU_TYPE_ARM,        llvm::MachO::CPU_SUBTYPE_ARM_V7EM,      UINT32_MAX, SUBTYPE_MASK},
31354710b34SJonas Devlieghere     {ArchSpec::eCore_ppc_generic,     llvm::MachO::CPU_TYPE_POWERPC,    CPU_ANY,                                UINT32_MAX, UINT32_MAX},
31454710b34SJonas Devlieghere     {ArchSpec::eCore_ppc_generic,     llvm::MachO::CPU_TYPE_POWERPC,    llvm::MachO::CPU_SUBTYPE_POWERPC_ALL,   UINT32_MAX, SUBTYPE_MASK},
31554710b34SJonas Devlieghere     {ArchSpec::eCore_ppc_ppc601,      llvm::MachO::CPU_TYPE_POWERPC,    llvm::MachO::CPU_SUBTYPE_POWERPC_601,   UINT32_MAX, SUBTYPE_MASK},
31654710b34SJonas Devlieghere     {ArchSpec::eCore_ppc_ppc602,      llvm::MachO::CPU_TYPE_POWERPC,    llvm::MachO::CPU_SUBTYPE_POWERPC_602,   UINT32_MAX, SUBTYPE_MASK},
31754710b34SJonas Devlieghere     {ArchSpec::eCore_ppc_ppc603,      llvm::MachO::CPU_TYPE_POWERPC,    llvm::MachO::CPU_SUBTYPE_POWERPC_603,   UINT32_MAX, SUBTYPE_MASK},
31854710b34SJonas Devlieghere     {ArchSpec::eCore_ppc_ppc603e,     llvm::MachO::CPU_TYPE_POWERPC,    llvm::MachO::CPU_SUBTYPE_POWERPC_603e,  UINT32_MAX, SUBTYPE_MASK},
31954710b34SJonas Devlieghere     {ArchSpec::eCore_ppc_ppc603ev,    llvm::MachO::CPU_TYPE_POWERPC,    llvm::MachO::CPU_SUBTYPE_POWERPC_603ev, UINT32_MAX, SUBTYPE_MASK},
32054710b34SJonas Devlieghere     {ArchSpec::eCore_ppc_ppc604,      llvm::MachO::CPU_TYPE_POWERPC,    llvm::MachO::CPU_SUBTYPE_POWERPC_604,   UINT32_MAX, SUBTYPE_MASK},
32154710b34SJonas Devlieghere     {ArchSpec::eCore_ppc_ppc604e,     llvm::MachO::CPU_TYPE_POWERPC,    llvm::MachO::CPU_SUBTYPE_POWERPC_604e,  UINT32_MAX, SUBTYPE_MASK},
32254710b34SJonas Devlieghere     {ArchSpec::eCore_ppc_ppc620,      llvm::MachO::CPU_TYPE_POWERPC,    llvm::MachO::CPU_SUBTYPE_POWERPC_620,   UINT32_MAX, SUBTYPE_MASK},
32354710b34SJonas Devlieghere     {ArchSpec::eCore_ppc_ppc750,      llvm::MachO::CPU_TYPE_POWERPC,    llvm::MachO::CPU_SUBTYPE_POWERPC_750,   UINT32_MAX, SUBTYPE_MASK},
32454710b34SJonas Devlieghere     {ArchSpec::eCore_ppc_ppc7400,     llvm::MachO::CPU_TYPE_POWERPC,    llvm::MachO::CPU_SUBTYPE_POWERPC_7400,  UINT32_MAX, SUBTYPE_MASK},
32554710b34SJonas Devlieghere     {ArchSpec::eCore_ppc_ppc7450,     llvm::MachO::CPU_TYPE_POWERPC,    llvm::MachO::CPU_SUBTYPE_POWERPC_7450,  UINT32_MAX, SUBTYPE_MASK},
32654710b34SJonas Devlieghere     {ArchSpec::eCore_ppc_ppc970,      llvm::MachO::CPU_TYPE_POWERPC,    llvm::MachO::CPU_SUBTYPE_POWERPC_970,   UINT32_MAX, SUBTYPE_MASK},
32754710b34SJonas Devlieghere     {ArchSpec::eCore_ppc64_generic,   llvm::MachO::CPU_TYPE_POWERPC64,  llvm::MachO::CPU_SUBTYPE_POWERPC_ALL,   UINT32_MAX, SUBTYPE_MASK},
32854710b34SJonas Devlieghere     {ArchSpec::eCore_ppc64le_generic, llvm::MachO::CPU_TYPE_POWERPC64,  CPU_ANY,                                UINT32_MAX, SUBTYPE_MASK},
32954710b34SJonas Devlieghere     {ArchSpec::eCore_ppc64_ppc970_64, llvm::MachO::CPU_TYPE_POWERPC64,  100,                                    UINT32_MAX, SUBTYPE_MASK},
33054710b34SJonas Devlieghere     {ArchSpec::eCore_x86_32_i386,     llvm::MachO::CPU_TYPE_I386,       llvm::MachO::CPU_SUBTYPE_I386_ALL,      UINT32_MAX, SUBTYPE_MASK},
33154710b34SJonas Devlieghere     {ArchSpec::eCore_x86_32_i486,     llvm::MachO::CPU_TYPE_I386,       llvm::MachO::CPU_SUBTYPE_486,           UINT32_MAX, SUBTYPE_MASK},
33254710b34SJonas Devlieghere     {ArchSpec::eCore_x86_32_i486sx,   llvm::MachO::CPU_TYPE_I386,       llvm::MachO::CPU_SUBTYPE_486SX,         UINT32_MAX, SUBTYPE_MASK},
33354710b34SJonas Devlieghere     {ArchSpec::eCore_x86_32_i386,     llvm::MachO::CPU_TYPE_I386,       CPU_ANY,                                UINT32_MAX, UINT32_MAX},
33454710b34SJonas Devlieghere     {ArchSpec::eCore_x86_64_x86_64,   llvm::MachO::CPU_TYPE_X86_64,     llvm::MachO::CPU_SUBTYPE_X86_64_ALL,    UINT32_MAX, SUBTYPE_MASK},
33554710b34SJonas Devlieghere     {ArchSpec::eCore_x86_64_x86_64,   llvm::MachO::CPU_TYPE_X86_64,     llvm::MachO::CPU_SUBTYPE_X86_ARCH1,     UINT32_MAX, SUBTYPE_MASK},
33654710b34SJonas Devlieghere     {ArchSpec::eCore_x86_64_x86_64h,  llvm::MachO::CPU_TYPE_X86_64,     llvm::MachO::CPU_SUBTYPE_X86_64_H,      UINT32_MAX, SUBTYPE_MASK},
33754710b34SJonas Devlieghere     {ArchSpec::eCore_x86_64_x86_64,   llvm::MachO::CPU_TYPE_X86_64,     CPU_ANY, UINT32_MAX, UINT32_MAX},
33854710b34SJonas Devlieghere     // Catch any unknown mach architectures so we can always use the object and symbol mach-o files
3395f19b907SPavel Labath     {ArchSpec::eCore_uknownMach32,    0,                                0,                                      0xFF000000u, 0x00000000u},
34054710b34SJonas Devlieghere     {ArchSpec::eCore_uknownMach64,    llvm::MachO::CPU_ARCH_ABI64,      0,                                      0xFF000000u, 0x00000000u}};
34154710b34SJonas Devlieghere // clang-format on
3425f19b907SPavel Labath 
3435f19b907SPavel Labath static const ArchDefinition g_macho_arch_def = {
344f15014ffSBenjamin Kramer     eArchTypeMachO, llvm::array_lengthof(g_macho_arch_entries),
3455f19b907SPavel Labath     g_macho_arch_entries, "mach-o"};
3465f19b907SPavel Labath 
3475f19b907SPavel Labath //===----------------------------------------------------------------------===//
3485f19b907SPavel Labath // A table that gets searched linearly for matches. This table is used to
3495f19b907SPavel Labath // convert cpu type and subtypes to architecture names, and to convert
3505f19b907SPavel Labath // architecture names to cpu types and subtypes. The ordering is important and
3515f19b907SPavel Labath // allows the precedence to be set when the table is built.
3525f19b907SPavel Labath static const ArchDefinitionEntry g_elf_arch_entries[] = {
3535f19b907SPavel Labath     {ArchSpec::eCore_sparc_generic, llvm::ELF::EM_SPARC, LLDB_INVALID_CPUTYPE,
3545f19b907SPavel Labath      0xFFFFFFFFu, 0xFFFFFFFFu}, // Sparc
3555f19b907SPavel Labath     {ArchSpec::eCore_x86_32_i386, llvm::ELF::EM_386, LLDB_INVALID_CPUTYPE,
3565f19b907SPavel Labath      0xFFFFFFFFu, 0xFFFFFFFFu}, // Intel 80386
3575f19b907SPavel Labath     {ArchSpec::eCore_x86_32_i486, llvm::ELF::EM_IAMCU, LLDB_INVALID_CPUTYPE,
3585f19b907SPavel Labath      0xFFFFFFFFu, 0xFFFFFFFFu}, // Intel MCU // FIXME: is this correct?
3595f19b907SPavel Labath     {ArchSpec::eCore_ppc_generic, llvm::ELF::EM_PPC, LLDB_INVALID_CPUTYPE,
3605f19b907SPavel Labath      0xFFFFFFFFu, 0xFFFFFFFFu}, // PowerPC
361f416e573Sserge-sans-paille     {ArchSpec::eCore_ppc64le_generic, llvm::ELF::EM_PPC64,
362f416e573Sserge-sans-paille      ArchSpec::eCore_ppc64le_generic, 0xFFFFFFFFu, 0xFFFFFFFFu}, // PowerPC64le
363f416e573Sserge-sans-paille     {ArchSpec::eCore_ppc64_generic, llvm::ELF::EM_PPC64,
364f416e573Sserge-sans-paille      ArchSpec::eCore_ppc64_generic, 0xFFFFFFFFu, 0xFFFFFFFFu}, // PowerPC64
3655f19b907SPavel Labath     {ArchSpec::eCore_arm_generic, llvm::ELF::EM_ARM, LLDB_INVALID_CPUTYPE,
3665f19b907SPavel Labath      0xFFFFFFFFu, 0xFFFFFFFFu}, // ARM
3675f19b907SPavel Labath     {ArchSpec::eCore_arm_aarch64, llvm::ELF::EM_AARCH64, LLDB_INVALID_CPUTYPE,
3685f19b907SPavel Labath      0xFFFFFFFFu, 0xFFFFFFFFu}, // ARM64
3695f19b907SPavel Labath     {ArchSpec::eCore_s390x_generic, llvm::ELF::EM_S390, LLDB_INVALID_CPUTYPE,
3705f19b907SPavel Labath      0xFFFFFFFFu, 0xFFFFFFFFu}, // SystemZ
3715f19b907SPavel Labath     {ArchSpec::eCore_sparc9_generic, llvm::ELF::EM_SPARCV9,
3725f19b907SPavel Labath      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // SPARC V9
3735f19b907SPavel Labath     {ArchSpec::eCore_x86_64_x86_64, llvm::ELF::EM_X86_64, LLDB_INVALID_CPUTYPE,
3745f19b907SPavel Labath      0xFFFFFFFFu, 0xFFFFFFFFu}, // AMD64
3755f19b907SPavel Labath     {ArchSpec::eCore_mips32, llvm::ELF::EM_MIPS, ArchSpec::eMIPSSubType_mips32,
3765f19b907SPavel Labath      0xFFFFFFFFu, 0xFFFFFFFFu}, // mips32
3775f19b907SPavel Labath     {ArchSpec::eCore_mips32r2, llvm::ELF::EM_MIPS,
3785f19b907SPavel Labath      ArchSpec::eMIPSSubType_mips32r2, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips32r2
3795f19b907SPavel Labath     {ArchSpec::eCore_mips32r6, llvm::ELF::EM_MIPS,
3805f19b907SPavel Labath      ArchSpec::eMIPSSubType_mips32r6, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips32r6
3815f19b907SPavel Labath     {ArchSpec::eCore_mips32el, llvm::ELF::EM_MIPS,
3825f19b907SPavel Labath      ArchSpec::eMIPSSubType_mips32el, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips32el
3835f19b907SPavel Labath     {ArchSpec::eCore_mips32r2el, llvm::ELF::EM_MIPS,
3845f19b907SPavel Labath      ArchSpec::eMIPSSubType_mips32r2el, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips32r2el
3855f19b907SPavel Labath     {ArchSpec::eCore_mips32r6el, llvm::ELF::EM_MIPS,
3865f19b907SPavel Labath      ArchSpec::eMIPSSubType_mips32r6el, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips32r6el
3875f19b907SPavel Labath     {ArchSpec::eCore_mips64, llvm::ELF::EM_MIPS, ArchSpec::eMIPSSubType_mips64,
3885f19b907SPavel Labath      0xFFFFFFFFu, 0xFFFFFFFFu}, // mips64
3895f19b907SPavel Labath     {ArchSpec::eCore_mips64r2, llvm::ELF::EM_MIPS,
3905f19b907SPavel Labath      ArchSpec::eMIPSSubType_mips64r2, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips64r2
3915f19b907SPavel Labath     {ArchSpec::eCore_mips64r6, llvm::ELF::EM_MIPS,
3925f19b907SPavel Labath      ArchSpec::eMIPSSubType_mips64r6, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips64r6
3935f19b907SPavel Labath     {ArchSpec::eCore_mips64el, llvm::ELF::EM_MIPS,
3945f19b907SPavel Labath      ArchSpec::eMIPSSubType_mips64el, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips64el
3955f19b907SPavel Labath     {ArchSpec::eCore_mips64r2el, llvm::ELF::EM_MIPS,
3965f19b907SPavel Labath      ArchSpec::eMIPSSubType_mips64r2el, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips64r2el
3975f19b907SPavel Labath     {ArchSpec::eCore_mips64r6el, llvm::ELF::EM_MIPS,
3985f19b907SPavel Labath      ArchSpec::eMIPSSubType_mips64r6el, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips64r6el
3995f19b907SPavel Labath     {ArchSpec::eCore_hexagon_generic, llvm::ELF::EM_HEXAGON,
4005f19b907SPavel Labath      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // HEXAGON
401faf6b254STatyana Krasnukha     {ArchSpec::eCore_arc, llvm::ELF::EM_ARC_COMPACT2, LLDB_INVALID_CPUTYPE,
402faf6b254STatyana Krasnukha      0xFFFFFFFFu, 0xFFFFFFFFu}, // ARC
403f416e573Sserge-sans-paille     {ArchSpec::eCore_avr, llvm::ELF::EM_AVR, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu,
404f416e573Sserge-sans-paille      0xFFFFFFFFu}, // AVR
40515f59711SLuís Marques     {ArchSpec::eCore_riscv32, llvm::ELF::EM_RISCV,
40615f59711SLuís Marques      ArchSpec::eRISCVSubType_riscv32, 0xFFFFFFFFu, 0xFFFFFFFFu}, // riscv32
40715f59711SLuís Marques     {ArchSpec::eCore_riscv64, llvm::ELF::EM_RISCV,
40815f59711SLuís Marques      ArchSpec::eRISCVSubType_riscv64, 0xFFFFFFFFu, 0xFFFFFFFFu}, // riscv64
4095f19b907SPavel Labath };
4105f19b907SPavel Labath 
4115f19b907SPavel Labath static const ArchDefinition g_elf_arch_def = {
4125f19b907SPavel Labath     eArchTypeELF,
413f15014ffSBenjamin Kramer     llvm::array_lengthof(g_elf_arch_entries),
4145f19b907SPavel Labath     g_elf_arch_entries,
4155f19b907SPavel Labath     "elf",
4165f19b907SPavel Labath };
4175f19b907SPavel Labath 
4185f19b907SPavel Labath static const ArchDefinitionEntry g_coff_arch_entries[] = {
4195f19b907SPavel Labath     {ArchSpec::eCore_x86_32_i386, llvm::COFF::IMAGE_FILE_MACHINE_I386,
4205f19b907SPavel Labath      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // Intel 80x86
4215f19b907SPavel Labath     {ArchSpec::eCore_ppc_generic, llvm::COFF::IMAGE_FILE_MACHINE_POWERPC,
4225f19b907SPavel Labath      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // PowerPC
4235f19b907SPavel Labath     {ArchSpec::eCore_ppc_generic, llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP,
4245f19b907SPavel Labath      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // PowerPC (with FPU)
4255f19b907SPavel Labath     {ArchSpec::eCore_arm_generic, llvm::COFF::IMAGE_FILE_MACHINE_ARM,
4265f19b907SPavel Labath      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // ARM
4275f19b907SPavel Labath     {ArchSpec::eCore_arm_armv7, llvm::COFF::IMAGE_FILE_MACHINE_ARMNT,
4285f19b907SPavel Labath      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // ARMv7
4295f19b907SPavel Labath     {ArchSpec::eCore_thumb, llvm::COFF::IMAGE_FILE_MACHINE_THUMB,
4305f19b907SPavel Labath      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // ARMv7
4315f19b907SPavel Labath     {ArchSpec::eCore_x86_64_x86_64, llvm::COFF::IMAGE_FILE_MACHINE_AMD64,
432544c8f48SMartin Storsjo      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // AMD64
433544c8f48SMartin Storsjo     {ArchSpec::eCore_arm_arm64, llvm::COFF::IMAGE_FILE_MACHINE_ARM64,
434544c8f48SMartin Storsjo      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu} // ARM64
4355f19b907SPavel Labath };
4365f19b907SPavel Labath 
4375f19b907SPavel Labath static const ArchDefinition g_coff_arch_def = {
4385f19b907SPavel Labath     eArchTypeCOFF,
439f15014ffSBenjamin Kramer     llvm::array_lengthof(g_coff_arch_entries),
4405f19b907SPavel Labath     g_coff_arch_entries,
4415f19b907SPavel Labath     "pe-coff",
4425f19b907SPavel Labath };
4435f19b907SPavel Labath 
4445f19b907SPavel Labath //===----------------------------------------------------------------------===//
4455f19b907SPavel Labath // Table of all ArchDefinitions
4465f19b907SPavel Labath static const ArchDefinition *g_arch_definitions[] = {
4475f19b907SPavel Labath     &g_macho_arch_def, &g_elf_arch_def, &g_coff_arch_def};
4485f19b907SPavel Labath 
4495f19b907SPavel Labath static const size_t k_num_arch_definitions =
450f15014ffSBenjamin Kramer     llvm::array_lengthof(g_arch_definitions);
4515f19b907SPavel Labath 
4525f19b907SPavel Labath //===----------------------------------------------------------------------===//
4535f19b907SPavel Labath // Static helper functions.
4545f19b907SPavel Labath 
4555f19b907SPavel Labath // Get the architecture definition for a given object type.
FindArchDefinition(ArchitectureType arch_type)4565f19b907SPavel Labath static const ArchDefinition *FindArchDefinition(ArchitectureType arch_type) {
4575f19b907SPavel Labath   for (unsigned int i = 0; i < k_num_arch_definitions; ++i) {
4585f19b907SPavel Labath     const ArchDefinition *def = g_arch_definitions[i];
4595f19b907SPavel Labath     if (def->type == arch_type)
4605f19b907SPavel Labath       return def;
4615f19b907SPavel Labath   }
4625f19b907SPavel Labath   return nullptr;
4635f19b907SPavel Labath }
4645f19b907SPavel Labath 
4655f19b907SPavel Labath // Get an architecture definition by name.
FindCoreDefinition(llvm::StringRef name)4665f19b907SPavel Labath static const CoreDefinition *FindCoreDefinition(llvm::StringRef name) {
467f15014ffSBenjamin Kramer   for (unsigned int i = 0; i < llvm::array_lengthof(g_core_definitions); ++i) {
468e50f9c41SMartin Storsjö     if (name.equals_insensitive(g_core_definitions[i].name))
4695f19b907SPavel Labath       return &g_core_definitions[i];
4705f19b907SPavel Labath   }
4715f19b907SPavel Labath   return nullptr;
4725f19b907SPavel Labath }
4735f19b907SPavel Labath 
FindCoreDefinition(ArchSpec::Core core)4745f19b907SPavel Labath static inline const CoreDefinition *FindCoreDefinition(ArchSpec::Core core) {
475f15014ffSBenjamin Kramer   if (core < llvm::array_lengthof(g_core_definitions))
4765f19b907SPavel Labath     return &g_core_definitions[core];
4775f19b907SPavel Labath   return nullptr;
4785f19b907SPavel Labath }
4795f19b907SPavel Labath 
4805f19b907SPavel Labath // Get a definition entry by cpu type and subtype.
4815f19b907SPavel Labath static const ArchDefinitionEntry *
FindArchDefinitionEntry(const ArchDefinition * def,uint32_t cpu,uint32_t sub)4825f19b907SPavel Labath FindArchDefinitionEntry(const ArchDefinition *def, uint32_t cpu, uint32_t sub) {
4835f19b907SPavel Labath   if (def == nullptr)
4845f19b907SPavel Labath     return nullptr;
4855f19b907SPavel Labath 
4865f19b907SPavel Labath   const ArchDefinitionEntry *entries = def->entries;
4875f19b907SPavel Labath   for (size_t i = 0; i < def->num_entries; ++i) {
4885f19b907SPavel Labath     if (entries[i].cpu == (cpu & entries[i].cpu_mask))
4895f19b907SPavel Labath       if (entries[i].sub == (sub & entries[i].sub_mask))
4905f19b907SPavel Labath         return &entries[i];
4915f19b907SPavel Labath   }
4925f19b907SPavel Labath   return nullptr;
4935f19b907SPavel Labath }
4945f19b907SPavel Labath 
4955f19b907SPavel Labath static const ArchDefinitionEntry *
FindArchDefinitionEntry(const ArchDefinition * def,ArchSpec::Core core)4965f19b907SPavel Labath FindArchDefinitionEntry(const ArchDefinition *def, ArchSpec::Core core) {
4975f19b907SPavel Labath   if (def == nullptr)
4985f19b907SPavel Labath     return nullptr;
4995f19b907SPavel Labath 
5005f19b907SPavel Labath   const ArchDefinitionEntry *entries = def->entries;
5015f19b907SPavel Labath   for (size_t i = 0; i < def->num_entries; ++i) {
5025f19b907SPavel Labath     if (entries[i].core == core)
5035f19b907SPavel Labath       return &entries[i];
5045f19b907SPavel Labath   }
5055f19b907SPavel Labath   return nullptr;
5065f19b907SPavel Labath }
5075f19b907SPavel Labath 
5085f19b907SPavel Labath //===----------------------------------------------------------------------===//
5095f19b907SPavel Labath // Constructors and destructors.
5105f19b907SPavel Labath 
511fd2433e1SJonas Devlieghere ArchSpec::ArchSpec() = default;
5125f19b907SPavel Labath 
ArchSpec(const char * triple_cstr)5135f19b907SPavel Labath ArchSpec::ArchSpec(const char *triple_cstr) {
5145f19b907SPavel Labath   if (triple_cstr)
5155f19b907SPavel Labath     SetTriple(triple_cstr);
5165f19b907SPavel Labath }
5175f19b907SPavel Labath 
ArchSpec(llvm::StringRef triple_str)5185f19b907SPavel Labath ArchSpec::ArchSpec(llvm::StringRef triple_str) { SetTriple(triple_str); }
5195f19b907SPavel Labath 
ArchSpec(const llvm::Triple & triple)5205f19b907SPavel Labath ArchSpec::ArchSpec(const llvm::Triple &triple) { SetTriple(triple); }
5215f19b907SPavel Labath 
ArchSpec(ArchitectureType arch_type,uint32_t cpu,uint32_t subtype)5225f19b907SPavel Labath ArchSpec::ArchSpec(ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) {
5235f19b907SPavel Labath   SetArchitecture(arch_type, cpu, subtype);
5245f19b907SPavel Labath }
5255f19b907SPavel Labath 
5265f19b907SPavel Labath ArchSpec::~ArchSpec() = default;
5275f19b907SPavel Labath 
Clear()5285f19b907SPavel Labath void ArchSpec::Clear() {
5295f19b907SPavel Labath   m_triple = llvm::Triple();
5305f19b907SPavel Labath   m_core = kCore_invalid;
5315f19b907SPavel Labath   m_byte_order = eByteOrderInvalid;
5325f19b907SPavel Labath   m_distribution_id.Clear();
5335f19b907SPavel Labath   m_flags = 0;
5345f19b907SPavel Labath }
5355f19b907SPavel Labath 
5365f19b907SPavel Labath //===----------------------------------------------------------------------===//
5375f19b907SPavel Labath // Predicates.
5385f19b907SPavel Labath 
GetArchitectureName() const5395f19b907SPavel Labath const char *ArchSpec::GetArchitectureName() const {
5405f19b907SPavel Labath   const CoreDefinition *core_def = FindCoreDefinition(m_core);
5415f19b907SPavel Labath   if (core_def)
5425f19b907SPavel Labath     return core_def->name;
5435f19b907SPavel Labath   return "unknown";
5445f19b907SPavel Labath }
5455f19b907SPavel Labath 
IsMIPS() const546ddb93b63SFangrui Song bool ArchSpec::IsMIPS() const { return GetTriple().isMIPS(); }
5475f19b907SPavel Labath 
GetTargetABI() const5485f19b907SPavel Labath std::string ArchSpec::GetTargetABI() const {
5495f19b907SPavel Labath 
5505f19b907SPavel Labath   std::string abi;
5515f19b907SPavel Labath 
5525f19b907SPavel Labath   if (IsMIPS()) {
5535f19b907SPavel Labath     switch (GetFlags() & ArchSpec::eMIPSABI_mask) {
5545f19b907SPavel Labath     case ArchSpec::eMIPSABI_N64:
5555f19b907SPavel Labath       abi = "n64";
5565f19b907SPavel Labath       return abi;
5575f19b907SPavel Labath     case ArchSpec::eMIPSABI_N32:
5585f19b907SPavel Labath       abi = "n32";
5595f19b907SPavel Labath       return abi;
5605f19b907SPavel Labath     case ArchSpec::eMIPSABI_O32:
5615f19b907SPavel Labath       abi = "o32";
5625f19b907SPavel Labath       return abi;
5635f19b907SPavel Labath     default:
5645f19b907SPavel Labath       return abi;
5655f19b907SPavel Labath     }
5665f19b907SPavel Labath   }
5675f19b907SPavel Labath   return abi;
5685f19b907SPavel Labath }
5695f19b907SPavel Labath 
SetFlags(const std::string & elf_abi)5700d5fc822SJonas Devlieghere void ArchSpec::SetFlags(const std::string &elf_abi) {
5715f19b907SPavel Labath 
5725f19b907SPavel Labath   uint32_t flag = GetFlags();
5735f19b907SPavel Labath   if (IsMIPS()) {
5745f19b907SPavel Labath     if (elf_abi == "n64")
5755f19b907SPavel Labath       flag |= ArchSpec::eMIPSABI_N64;
5765f19b907SPavel Labath     else if (elf_abi == "n32")
5775f19b907SPavel Labath       flag |= ArchSpec::eMIPSABI_N32;
5785f19b907SPavel Labath     else if (elf_abi == "o32")
5795f19b907SPavel Labath       flag |= ArchSpec::eMIPSABI_O32;
5805f19b907SPavel Labath   }
5815f19b907SPavel Labath   SetFlags(flag);
5825f19b907SPavel Labath }
5835f19b907SPavel Labath 
GetClangTargetCPU() const5845f19b907SPavel Labath std::string ArchSpec::GetClangTargetCPU() const {
5855f19b907SPavel Labath   std::string cpu;
5865f19b907SPavel Labath 
587ddb93b63SFangrui Song   if (IsMIPS()) {
5885f19b907SPavel Labath     switch (m_core) {
5895f19b907SPavel Labath     case ArchSpec::eCore_mips32:
5905f19b907SPavel Labath     case ArchSpec::eCore_mips32el:
5915f19b907SPavel Labath       cpu = "mips32";
5925f19b907SPavel Labath       break;
5935f19b907SPavel Labath     case ArchSpec::eCore_mips32r2:
5945f19b907SPavel Labath     case ArchSpec::eCore_mips32r2el:
5955f19b907SPavel Labath       cpu = "mips32r2";
5965f19b907SPavel Labath       break;
5975f19b907SPavel Labath     case ArchSpec::eCore_mips32r3:
5985f19b907SPavel Labath     case ArchSpec::eCore_mips32r3el:
5995f19b907SPavel Labath       cpu = "mips32r3";
6005f19b907SPavel Labath       break;
6015f19b907SPavel Labath     case ArchSpec::eCore_mips32r5:
6025f19b907SPavel Labath     case ArchSpec::eCore_mips32r5el:
6035f19b907SPavel Labath       cpu = "mips32r5";
6045f19b907SPavel Labath       break;
6055f19b907SPavel Labath     case ArchSpec::eCore_mips32r6:
6065f19b907SPavel Labath     case ArchSpec::eCore_mips32r6el:
6075f19b907SPavel Labath       cpu = "mips32r6";
6085f19b907SPavel Labath       break;
6095f19b907SPavel Labath     case ArchSpec::eCore_mips64:
6105f19b907SPavel Labath     case ArchSpec::eCore_mips64el:
6115f19b907SPavel Labath       cpu = "mips64";
6125f19b907SPavel Labath       break;
6135f19b907SPavel Labath     case ArchSpec::eCore_mips64r2:
6145f19b907SPavel Labath     case ArchSpec::eCore_mips64r2el:
6155f19b907SPavel Labath       cpu = "mips64r2";
6165f19b907SPavel Labath       break;
6175f19b907SPavel Labath     case ArchSpec::eCore_mips64r3:
6185f19b907SPavel Labath     case ArchSpec::eCore_mips64r3el:
6195f19b907SPavel Labath       cpu = "mips64r3";
6205f19b907SPavel Labath       break;
6215f19b907SPavel Labath     case ArchSpec::eCore_mips64r5:
6225f19b907SPavel Labath     case ArchSpec::eCore_mips64r5el:
6235f19b907SPavel Labath       cpu = "mips64r5";
6245f19b907SPavel Labath       break;
6255f19b907SPavel Labath     case ArchSpec::eCore_mips64r6:
6265f19b907SPavel Labath     case ArchSpec::eCore_mips64r6el:
6275f19b907SPavel Labath       cpu = "mips64r6";
6285f19b907SPavel Labath       break;
6295f19b907SPavel Labath     default:
6305f19b907SPavel Labath       break;
6315f19b907SPavel Labath     }
6325f19b907SPavel Labath   }
6335f19b907SPavel Labath   return cpu;
6345f19b907SPavel Labath }
6355f19b907SPavel Labath 
GetMachOCPUType() const6365f19b907SPavel Labath uint32_t ArchSpec::GetMachOCPUType() const {
6375f19b907SPavel Labath   const CoreDefinition *core_def = FindCoreDefinition(m_core);
6385f19b907SPavel Labath   if (core_def) {
6395f19b907SPavel Labath     const ArchDefinitionEntry *arch_def =
6405f19b907SPavel Labath         FindArchDefinitionEntry(&g_macho_arch_def, core_def->core);
6415f19b907SPavel Labath     if (arch_def) {
6425f19b907SPavel Labath       return arch_def->cpu;
6435f19b907SPavel Labath     }
6445f19b907SPavel Labath   }
6455f19b907SPavel Labath   return LLDB_INVALID_CPUTYPE;
6465f19b907SPavel Labath }
6475f19b907SPavel Labath 
GetMachOCPUSubType() const6485f19b907SPavel Labath uint32_t ArchSpec::GetMachOCPUSubType() const {
6495f19b907SPavel Labath   const CoreDefinition *core_def = FindCoreDefinition(m_core);
6505f19b907SPavel Labath   if (core_def) {
6515f19b907SPavel Labath     const ArchDefinitionEntry *arch_def =
6525f19b907SPavel Labath         FindArchDefinitionEntry(&g_macho_arch_def, core_def->core);
6535f19b907SPavel Labath     if (arch_def) {
6545f19b907SPavel Labath       return arch_def->sub;
6555f19b907SPavel Labath     }
6565f19b907SPavel Labath   }
6575f19b907SPavel Labath   return LLDB_INVALID_CPUTYPE;
6585f19b907SPavel Labath }
6595f19b907SPavel Labath 
GetDataByteSize() const6605f19b907SPavel Labath uint32_t ArchSpec::GetDataByteSize() const {
6615f19b907SPavel Labath   return 1;
6625f19b907SPavel Labath }
6635f19b907SPavel Labath 
GetCodeByteSize() const6645f19b907SPavel Labath uint32_t ArchSpec::GetCodeByteSize() const {
6655f19b907SPavel Labath   return 1;
6665f19b907SPavel Labath }
6675f19b907SPavel Labath 
GetMachine() const6685f19b907SPavel Labath llvm::Triple::ArchType ArchSpec::GetMachine() const {
6695f19b907SPavel Labath   const CoreDefinition *core_def = FindCoreDefinition(m_core);
6705f19b907SPavel Labath   if (core_def)
6715f19b907SPavel Labath     return core_def->machine;
6725f19b907SPavel Labath 
6735f19b907SPavel Labath   return llvm::Triple::UnknownArch;
6745f19b907SPavel Labath }
6755f19b907SPavel Labath 
GetDistributionId() const6760e4c4821SAdrian Prantl ConstString ArchSpec::GetDistributionId() const {
6775f19b907SPavel Labath   return m_distribution_id;
6785f19b907SPavel Labath }
6795f19b907SPavel Labath 
SetDistributionId(const char * distribution_id)6805f19b907SPavel Labath void ArchSpec::SetDistributionId(const char *distribution_id) {
6815f19b907SPavel Labath   m_distribution_id.SetCString(distribution_id);
6825f19b907SPavel Labath }
6835f19b907SPavel Labath 
GetAddressByteSize() const6845f19b907SPavel Labath uint32_t ArchSpec::GetAddressByteSize() const {
6855f19b907SPavel Labath   const CoreDefinition *core_def = FindCoreDefinition(m_core);
6865f19b907SPavel Labath   if (core_def) {
6875f19b907SPavel Labath     if (core_def->machine == llvm::Triple::mips64 ||
6885f19b907SPavel Labath         core_def->machine == llvm::Triple::mips64el) {
6895f19b907SPavel Labath       // For N32/O32 applications Address size is 4 bytes.
6905f19b907SPavel Labath       if (m_flags & (eMIPSABI_N32 | eMIPSABI_O32))
6915f19b907SPavel Labath         return 4;
6925f19b907SPavel Labath     }
6935f19b907SPavel Labath     return core_def->addr_byte_size;
6945f19b907SPavel Labath   }
6955f19b907SPavel Labath   return 0;
6965f19b907SPavel Labath }
6975f19b907SPavel Labath 
GetDefaultEndian() const6985f19b907SPavel Labath ByteOrder ArchSpec::GetDefaultEndian() const {
6995f19b907SPavel Labath   const CoreDefinition *core_def = FindCoreDefinition(m_core);
7005f19b907SPavel Labath   if (core_def)
7015f19b907SPavel Labath     return core_def->default_byte_order;
7025f19b907SPavel Labath   return eByteOrderInvalid;
7035f19b907SPavel Labath }
7045f19b907SPavel Labath 
CharIsSignedByDefault() const7055f19b907SPavel Labath bool ArchSpec::CharIsSignedByDefault() const {
7065f19b907SPavel Labath   switch (m_triple.getArch()) {
7075f19b907SPavel Labath   default:
7085f19b907SPavel Labath     return true;
7095f19b907SPavel Labath 
7105f19b907SPavel Labath   case llvm::Triple::aarch64:
7117dd7a360SJason Molenda   case llvm::Triple::aarch64_32:
7125f19b907SPavel Labath   case llvm::Triple::aarch64_be:
7135f19b907SPavel Labath   case llvm::Triple::arm:
7145f19b907SPavel Labath   case llvm::Triple::armeb:
7155f19b907SPavel Labath   case llvm::Triple::thumb:
7165f19b907SPavel Labath   case llvm::Triple::thumbeb:
7175f19b907SPavel Labath     return m_triple.isOSDarwin() || m_triple.isOSWindows();
7185f19b907SPavel Labath 
7195f19b907SPavel Labath   case llvm::Triple::ppc:
7205f19b907SPavel Labath   case llvm::Triple::ppc64:
7215f19b907SPavel Labath     return m_triple.isOSDarwin();
7225f19b907SPavel Labath 
7235f19b907SPavel Labath   case llvm::Triple::ppc64le:
7245f19b907SPavel Labath   case llvm::Triple::systemz:
7255f19b907SPavel Labath   case llvm::Triple::xcore:
726469304e8SAlexander Polyakov   case llvm::Triple::arc:
7275f19b907SPavel Labath     return false;
7285f19b907SPavel Labath   }
7295f19b907SPavel Labath }
7305f19b907SPavel Labath 
GetByteOrder() const7315f19b907SPavel Labath lldb::ByteOrder ArchSpec::GetByteOrder() const {
7325f19b907SPavel Labath   if (m_byte_order == eByteOrderInvalid)
7335f19b907SPavel Labath     return GetDefaultEndian();
7345f19b907SPavel Labath   return m_byte_order;
7355f19b907SPavel Labath }
7365f19b907SPavel Labath 
7375f19b907SPavel Labath //===----------------------------------------------------------------------===//
7385f19b907SPavel Labath // Mutators.
7395f19b907SPavel Labath 
SetTriple(const llvm::Triple & triple)7405f19b907SPavel Labath bool ArchSpec::SetTriple(const llvm::Triple &triple) {
7415f19b907SPavel Labath   m_triple = triple;
7425f19b907SPavel Labath   UpdateCore();
7435f19b907SPavel Labath   return IsValid();
7445f19b907SPavel Labath }
7455f19b907SPavel Labath 
ParseMachCPUDashSubtypeTriple(llvm::StringRef triple_str,ArchSpec & arch)7465f19b907SPavel Labath bool lldb_private::ParseMachCPUDashSubtypeTriple(llvm::StringRef triple_str,
7475f19b907SPavel Labath                                                  ArchSpec &arch) {
7485f19b907SPavel Labath   // Accept "12-10" or "12.10" as cpu type/subtype
7495f19b907SPavel Labath   if (triple_str.empty())
7505f19b907SPavel Labath     return false;
7515f19b907SPavel Labath 
7525f19b907SPavel Labath   size_t pos = triple_str.find_first_of("-.");
7535f19b907SPavel Labath   if (pos == llvm::StringRef::npos)
7545f19b907SPavel Labath     return false;
7555f19b907SPavel Labath 
7565f19b907SPavel Labath   llvm::StringRef cpu_str = triple_str.substr(0, pos);
7575f19b907SPavel Labath   llvm::StringRef remainder = triple_str.substr(pos + 1);
7585f19b907SPavel Labath   if (cpu_str.empty() || remainder.empty())
7595f19b907SPavel Labath     return false;
7605f19b907SPavel Labath 
7615f19b907SPavel Labath   llvm::StringRef sub_str;
7625f19b907SPavel Labath   llvm::StringRef vendor;
7635f19b907SPavel Labath   llvm::StringRef os;
7645f19b907SPavel Labath   std::tie(sub_str, remainder) = remainder.split('-');
7655f19b907SPavel Labath   std::tie(vendor, os) = remainder.split('-');
7665f19b907SPavel Labath 
7675f19b907SPavel Labath   uint32_t cpu = 0;
7685f19b907SPavel Labath   uint32_t sub = 0;
7695f19b907SPavel Labath   if (cpu_str.getAsInteger(10, cpu) || sub_str.getAsInteger(10, sub))
7705f19b907SPavel Labath     return false;
7715f19b907SPavel Labath 
7725f19b907SPavel Labath   if (!arch.SetArchitecture(eArchTypeMachO, cpu, sub))
7735f19b907SPavel Labath     return false;
7745f19b907SPavel Labath   if (!vendor.empty() && !os.empty()) {
7755f19b907SPavel Labath     arch.GetTriple().setVendorName(vendor);
7765f19b907SPavel Labath     arch.GetTriple().setOSName(os);
7775f19b907SPavel Labath   }
7785f19b907SPavel Labath 
7795f19b907SPavel Labath   return true;
7805f19b907SPavel Labath }
7815f19b907SPavel Labath 
SetTriple(llvm::StringRef triple)7825f19b907SPavel Labath bool ArchSpec::SetTriple(llvm::StringRef triple) {
7835f19b907SPavel Labath   if (triple.empty()) {
7845f19b907SPavel Labath     Clear();
7855f19b907SPavel Labath     return false;
7865f19b907SPavel Labath   }
7875f19b907SPavel Labath 
7885f19b907SPavel Labath   if (ParseMachCPUDashSubtypeTriple(triple, *this))
7895f19b907SPavel Labath     return true;
7905f19b907SPavel Labath 
7915f19b907SPavel Labath   SetTriple(llvm::Triple(llvm::Triple::normalize(triple)));
7925f19b907SPavel Labath   return IsValid();
7935f19b907SPavel Labath }
7945f19b907SPavel Labath 
ContainsOnlyArch(const llvm::Triple & normalized_triple)7955f19b907SPavel Labath bool ArchSpec::ContainsOnlyArch(const llvm::Triple &normalized_triple) {
7965f19b907SPavel Labath   return !normalized_triple.getArchName().empty() &&
7975f19b907SPavel Labath          normalized_triple.getOSName().empty() &&
7985f19b907SPavel Labath          normalized_triple.getVendorName().empty() &&
7995f19b907SPavel Labath          normalized_triple.getEnvironmentName().empty();
8005f19b907SPavel Labath }
8015f19b907SPavel Labath 
MergeFrom(const ArchSpec & other)8025f19b907SPavel Labath void ArchSpec::MergeFrom(const ArchSpec &other) {
803e937840dSAdrian Prantl   // ios-macabi always wins over macosx.
804e937840dSAdrian Prantl   if ((GetTriple().getOS() == llvm::Triple::MacOSX ||
805e937840dSAdrian Prantl        GetTriple().getOS() == llvm::Triple::UnknownOS) &&
806e937840dSAdrian Prantl       other.GetTriple().getOS() == llvm::Triple::IOS &&
807e937840dSAdrian Prantl       other.GetTriple().getEnvironment() == llvm::Triple::MacABI) {
808e937840dSAdrian Prantl     (*this) = other;
809e937840dSAdrian Prantl     return;
810e937840dSAdrian Prantl   }
811e937840dSAdrian Prantl 
812bee015efSAlex Langford   if (!TripleVendorWasSpecified() && other.TripleVendorWasSpecified())
8135f19b907SPavel Labath     GetTriple().setVendor(other.GetTriple().getVendor());
8145f8e88cdSGreg Clayton   if (!TripleOSWasSpecified() && other.TripleOSWasSpecified())
8155f19b907SPavel Labath     GetTriple().setOS(other.GetTriple().getOS());
8165f19b907SPavel Labath   if (GetTriple().getArch() == llvm::Triple::UnknownArch) {
8175f19b907SPavel Labath     GetTriple().setArch(other.GetTriple().getArch());
818c910aa67SDavide Italiano 
81905097246SAdrian Prantl     // MachO unknown64 isn't really invalid as the debugger can still obtain
82005097246SAdrian Prantl     // information from the binary, e.g. line tables. As such, we don't update
82105097246SAdrian Prantl     // the core here.
822c910aa67SDavide Italiano     if (other.GetCore() != eCore_uknownMach64)
8235f19b907SPavel Labath       UpdateCore();
8245f19b907SPavel Labath   }
825bee015efSAlex Langford   if (!TripleEnvironmentWasSpecified() &&
8266d6288aeSAlex Langford       other.TripleEnvironmentWasSpecified()) {
8275f19b907SPavel Labath     GetTriple().setEnvironment(other.GetTriple().getEnvironment());
8285f19b907SPavel Labath   }
8295f19b907SPavel Labath   // If this and other are both arm ArchSpecs and this ArchSpec is a generic
83005097246SAdrian Prantl   // "some kind of arm" spec but the other ArchSpec is a specific arm core,
83105097246SAdrian Prantl   // adopt the specific arm core.
8325f19b907SPavel Labath   if (GetTriple().getArch() == llvm::Triple::arm &&
8335f19b907SPavel Labath       other.GetTriple().getArch() == llvm::Triple::arm &&
8345f19b907SPavel Labath       IsCompatibleMatch(other) && GetCore() == ArchSpec::eCore_arm_generic &&
8355f19b907SPavel Labath       other.GetCore() != ArchSpec::eCore_arm_generic) {
8365f19b907SPavel Labath     m_core = other.GetCore();
8378b8185bbSMuhammad Omair Javaid     CoreUpdated(false);
8385f19b907SPavel Labath   }
8395f19b907SPavel Labath   if (GetFlags() == 0) {
8405f19b907SPavel Labath     SetFlags(other.GetFlags());
8415f19b907SPavel Labath   }
8425f19b907SPavel Labath }
8435f19b907SPavel Labath 
SetArchitecture(ArchitectureType arch_type,uint32_t cpu,uint32_t sub,uint32_t os)8445f19b907SPavel Labath bool ArchSpec::SetArchitecture(ArchitectureType arch_type, uint32_t cpu,
8455f19b907SPavel Labath                                uint32_t sub, uint32_t os) {
8465f19b907SPavel Labath   m_core = kCore_invalid;
8475f19b907SPavel Labath   bool update_triple = true;
8485f19b907SPavel Labath   const ArchDefinition *arch_def = FindArchDefinition(arch_type);
8495f19b907SPavel Labath   if (arch_def) {
8505f19b907SPavel Labath     const ArchDefinitionEntry *arch_def_entry =
8515f19b907SPavel Labath         FindArchDefinitionEntry(arch_def, cpu, sub);
8525f19b907SPavel Labath     if (arch_def_entry) {
8535f19b907SPavel Labath       const CoreDefinition *core_def = FindCoreDefinition(arch_def_entry->core);
8545f19b907SPavel Labath       if (core_def) {
8555f19b907SPavel Labath         m_core = core_def->core;
8565f19b907SPavel Labath         update_triple = false;
85705097246SAdrian Prantl         // Always use the architecture name because it might be more
85805097246SAdrian Prantl         // descriptive than the architecture enum ("armv7" ->
85905097246SAdrian Prantl         // llvm::Triple::arm).
8605f19b907SPavel Labath         m_triple.setArchName(llvm::StringRef(core_def->name));
8615f19b907SPavel Labath         if (arch_type == eArchTypeMachO) {
8625f19b907SPavel Labath           m_triple.setVendor(llvm::Triple::Apple);
8635f19b907SPavel Labath 
8645f19b907SPavel Labath           // Don't set the OS.  It could be simulator, macosx, ios, watchos,
86532762fd2SJason Molenda           // tvos, bridgeos.  We could get close with the cpu type - but we
86632762fd2SJason Molenda           // can't get it right all of the time.  Better to leave this unset
86732762fd2SJason Molenda           // so other sections of code will set it when they have more
868f8819bd5SJonas Devlieghere           // information. NB: don't call m_triple.setOS
869f8819bd5SJonas Devlieghere           // (llvm::Triple::UnknownOS). That sets the OSName to "unknown" and
870f8819bd5SJonas Devlieghere           // the ArchSpec::TripleVendorWasSpecified() method says that any
871f8819bd5SJonas Devlieghere           // OSName setting means it was specified.
8725f19b907SPavel Labath         } else if (arch_type == eArchTypeELF) {
8735f19b907SPavel Labath           switch (os) {
8745f19b907SPavel Labath           case llvm::ELF::ELFOSABI_AIX:
8755f19b907SPavel Labath             m_triple.setOS(llvm::Triple::OSType::AIX);
8765f19b907SPavel Labath             break;
8775f19b907SPavel Labath           case llvm::ELF::ELFOSABI_FREEBSD:
8785f19b907SPavel Labath             m_triple.setOS(llvm::Triple::OSType::FreeBSD);
8795f19b907SPavel Labath             break;
8805f19b907SPavel Labath           case llvm::ELF::ELFOSABI_GNU:
8815f19b907SPavel Labath             m_triple.setOS(llvm::Triple::OSType::Linux);
8825f19b907SPavel Labath             break;
8835f19b907SPavel Labath           case llvm::ELF::ELFOSABI_NETBSD:
8845f19b907SPavel Labath             m_triple.setOS(llvm::Triple::OSType::NetBSD);
8855f19b907SPavel Labath             break;
8865f19b907SPavel Labath           case llvm::ELF::ELFOSABI_OPENBSD:
8875f19b907SPavel Labath             m_triple.setOS(llvm::Triple::OSType::OpenBSD);
8885f19b907SPavel Labath             break;
8895f19b907SPavel Labath           case llvm::ELF::ELFOSABI_SOLARIS:
8905f19b907SPavel Labath             m_triple.setOS(llvm::Triple::OSType::Solaris);
8915f19b907SPavel Labath             break;
8925f19b907SPavel Labath           }
8935f19b907SPavel Labath         } else if (arch_type == eArchTypeCOFF && os == llvm::Triple::Win32) {
8945f19b907SPavel Labath           m_triple.setVendor(llvm::Triple::PC);
8955f19b907SPavel Labath           m_triple.setOS(llvm::Triple::Win32);
8965f19b907SPavel Labath         } else {
8975f19b907SPavel Labath           m_triple.setVendor(llvm::Triple::UnknownVendor);
8985f19b907SPavel Labath           m_triple.setOS(llvm::Triple::UnknownOS);
8995f19b907SPavel Labath         }
90005097246SAdrian Prantl         // Fall back onto setting the machine type if the arch by name
90105097246SAdrian Prantl         // failed...
9025f19b907SPavel Labath         if (m_triple.getArch() == llvm::Triple::UnknownArch)
9035f19b907SPavel Labath           m_triple.setArch(core_def->machine);
9045f19b907SPavel Labath       }
905c9f79391SJason Molenda     } else {
906a007a6d8SPavel Labath       Log *log(GetLog(LLDBLog::Target | LLDBLog::Process | LLDBLog::Platform));
90763e5fb76SJonas Devlieghere       LLDB_LOGF(log,
90863e5fb76SJonas Devlieghere                 "Unable to find a core definition for cpu 0x%" PRIx32
90963e5fb76SJonas Devlieghere                 " sub %" PRId32,
91063e5fb76SJonas Devlieghere                 cpu, sub);
9115f19b907SPavel Labath     }
9125f19b907SPavel Labath   }
9135f19b907SPavel Labath   CoreUpdated(update_triple);
9145f19b907SPavel Labath   return IsValid();
9155f19b907SPavel Labath }
9165f19b907SPavel Labath 
GetMinimumOpcodeByteSize() const9175f19b907SPavel Labath uint32_t ArchSpec::GetMinimumOpcodeByteSize() const {
9185f19b907SPavel Labath   const CoreDefinition *core_def = FindCoreDefinition(m_core);
9195f19b907SPavel Labath   if (core_def)
9205f19b907SPavel Labath     return core_def->min_opcode_byte_size;
9215f19b907SPavel Labath   return 0;
9225f19b907SPavel Labath }
9235f19b907SPavel Labath 
GetMaximumOpcodeByteSize() const9245f19b907SPavel Labath uint32_t ArchSpec::GetMaximumOpcodeByteSize() const {
9255f19b907SPavel Labath   const CoreDefinition *core_def = FindCoreDefinition(m_core);
9265f19b907SPavel Labath   if (core_def)
9275f19b907SPavel Labath     return core_def->max_opcode_byte_size;
9285f19b907SPavel Labath   return 0;
9295f19b907SPavel Labath }
9305f19b907SPavel Labath 
IsExactMatch(const ArchSpec & rhs) const9315f19b907SPavel Labath bool ArchSpec::IsExactMatch(const ArchSpec &rhs) const {
9325f19b907SPavel Labath   return IsEqualTo(rhs, true);
9335f19b907SPavel Labath }
9345f19b907SPavel Labath 
IsCompatibleMatch(const ArchSpec & rhs) const9355f19b907SPavel Labath bool ArchSpec::IsCompatibleMatch(const ArchSpec &rhs) const {
9365f19b907SPavel Labath   return IsEqualTo(rhs, false);
9375f19b907SPavel Labath }
9385f19b907SPavel Labath 
IsCompatibleEnvironment(llvm::Triple::EnvironmentType lhs,llvm::Triple::EnvironmentType rhs)9397b8a0375SAdrian Prantl static bool IsCompatibleEnvironment(llvm::Triple::EnvironmentType lhs,
9405f19b907SPavel Labath                                     llvm::Triple::EnvironmentType rhs) {
9415f19b907SPavel Labath   if (lhs == rhs)
9425f19b907SPavel Labath     return true;
9435f19b907SPavel Labath 
9448120eba5SFred Riss   // Apple simulators are a different platform than what they simulate.
9458120eba5SFred Riss   // As the environments are different at this point, if one of them is a
9468120eba5SFred Riss   // simulator, then they are different.
9478120eba5SFred Riss   if (lhs == llvm::Triple::Simulator || rhs == llvm::Triple::Simulator)
9488120eba5SFred Riss     return false;
9498120eba5SFred Riss 
9505f19b907SPavel Labath   // If any of the environment is unknown then they are compatible
9515f19b907SPavel Labath   if (lhs == llvm::Triple::UnknownEnvironment ||
9525f19b907SPavel Labath       rhs == llvm::Triple::UnknownEnvironment)
9535f19b907SPavel Labath     return true;
9545f19b907SPavel Labath 
9555f19b907SPavel Labath   // If one of the environment is Android and the other one is EABI then they
95605097246SAdrian Prantl   // are considered to be compatible. This is required as a workaround for
95705097246SAdrian Prantl   // shared libraries compiled for Android without the NOTE section indicating
95805097246SAdrian Prantl   // that they are using the Android ABI.
9595f19b907SPavel Labath   if ((lhs == llvm::Triple::Android && rhs == llvm::Triple::EABI) ||
9605f19b907SPavel Labath       (rhs == llvm::Triple::Android && lhs == llvm::Triple::EABI) ||
9615f19b907SPavel Labath       (lhs == llvm::Triple::GNUEABI && rhs == llvm::Triple::EABI) ||
9625f19b907SPavel Labath       (rhs == llvm::Triple::GNUEABI && lhs == llvm::Triple::EABI) ||
9635f19b907SPavel Labath       (lhs == llvm::Triple::GNUEABIHF && rhs == llvm::Triple::EABIHF) ||
9645f19b907SPavel Labath       (rhs == llvm::Triple::GNUEABIHF && lhs == llvm::Triple::EABIHF))
9655f19b907SPavel Labath     return true;
9665f19b907SPavel Labath 
9675f19b907SPavel Labath   return false;
9685f19b907SPavel Labath }
9695f19b907SPavel Labath 
IsEqualTo(const ArchSpec & rhs,bool exact_match) const9705f19b907SPavel Labath bool ArchSpec::IsEqualTo(const ArchSpec &rhs, bool exact_match) const {
9715f19b907SPavel Labath   // explicitly ignoring m_distribution_id in this method.
9725f19b907SPavel Labath 
973ef748b58SFred Riss   if (GetByteOrder() != rhs.GetByteOrder() ||
974ef748b58SFred Riss       !cores_match(GetCore(), rhs.GetCore(), true, exact_match))
9755f19b907SPavel Labath     return false;
9765f19b907SPavel Labath 
9775f19b907SPavel Labath   const llvm::Triple &lhs_triple = GetTriple();
9785f19b907SPavel Labath   const llvm::Triple &rhs_triple = rhs.GetTriple();
9795f19b907SPavel Labath 
9805f19b907SPavel Labath   const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
9815f19b907SPavel Labath   const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
982*8a64dd5bSMartin Storsjö 
983*8a64dd5bSMartin Storsjö   const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
984*8a64dd5bSMartin Storsjö   const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
985*8a64dd5bSMartin Storsjö 
986*8a64dd5bSMartin Storsjö   bool both_windows = lhs_triple.isOSWindows() && rhs_triple.isOSWindows();
987*8a64dd5bSMartin Storsjö 
988*8a64dd5bSMartin Storsjö   // On Windows, the vendor field doesn't have any practical effect, but
989*8a64dd5bSMartin Storsjö   // it is often set to either "pc" or "w64".
990*8a64dd5bSMartin Storsjö   if ((lhs_triple_vendor != rhs_triple_vendor) &&
991*8a64dd5bSMartin Storsjö       (exact_match || !both_windows)) {
9925f19b907SPavel Labath     const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
9935f19b907SPavel Labath     const bool lhs_vendor_specified = TripleVendorWasSpecified();
99405097246SAdrian Prantl     // Both architectures had the vendor specified, so if they aren't equal
99505097246SAdrian Prantl     // then we return false
9965f19b907SPavel Labath     if (rhs_vendor_specified && lhs_vendor_specified)
9975f19b907SPavel Labath       return false;
9985f19b907SPavel Labath 
9995f19b907SPavel Labath     // Only fail if both vendor types are not unknown
10005f19b907SPavel Labath     if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
10015f19b907SPavel Labath         rhs_triple_vendor != llvm::Triple::UnknownVendor)
10025f19b907SPavel Labath       return false;
10035f19b907SPavel Labath   }
10045f19b907SPavel Labath 
1005e937840dSAdrian Prantl   const llvm::Triple::EnvironmentType lhs_triple_env =
1006e937840dSAdrian Prantl       lhs_triple.getEnvironment();
1007e937840dSAdrian Prantl   const llvm::Triple::EnvironmentType rhs_triple_env =
1008e937840dSAdrian Prantl       rhs_triple.getEnvironment();
1009e937840dSAdrian Prantl 
1010e937840dSAdrian Prantl   if (!exact_match) {
1011e937840dSAdrian Prantl     // x86_64-apple-ios-macabi, x86_64-apple-macosx are compatible, no match.
1012e937840dSAdrian Prantl     if ((lhs_triple_os == llvm::Triple::IOS &&
1013e937840dSAdrian Prantl          lhs_triple_env == llvm::Triple::MacABI &&
1014e937840dSAdrian Prantl          rhs_triple_os == llvm::Triple::MacOSX) ||
1015e937840dSAdrian Prantl         (lhs_triple_os == llvm::Triple::MacOSX &&
1016e937840dSAdrian Prantl          rhs_triple_os == llvm::Triple::IOS &&
1017e937840dSAdrian Prantl          rhs_triple_env == llvm::Triple::MacABI))
1018e937840dSAdrian Prantl       return true;
1019e937840dSAdrian Prantl   }
1020e937840dSAdrian Prantl 
1021e937840dSAdrian Prantl   // x86_64-apple-ios-macabi and x86_64-apple-ios are not compatible.
1022e937840dSAdrian Prantl   if (lhs_triple_os == llvm::Triple::IOS &&
1023e937840dSAdrian Prantl       rhs_triple_os == llvm::Triple::IOS &&
1024e937840dSAdrian Prantl       (lhs_triple_env == llvm::Triple::MacABI ||
1025e937840dSAdrian Prantl        rhs_triple_env == llvm::Triple::MacABI) &&
1026e937840dSAdrian Prantl       lhs_triple_env != rhs_triple_env)
1027e937840dSAdrian Prantl     return false;
10285f19b907SPavel Labath 
102905df9cc7SAdrian Prantl   if (lhs_triple_os != rhs_triple_os) {
103005df9cc7SAdrian Prantl     const bool lhs_os_specified = TripleOSWasSpecified();
103105df9cc7SAdrian Prantl     const bool rhs_os_specified = rhs.TripleOSWasSpecified();
103205df9cc7SAdrian Prantl     // If both OS types are specified and different, fail.
103305df9cc7SAdrian Prantl     if (lhs_os_specified && rhs_os_specified)
103405df9cc7SAdrian Prantl       return false;
103505df9cc7SAdrian Prantl 
103605df9cc7SAdrian Prantl     // If the pair of os+env is both unspecified, match any other os+env combo.
103705df9cc7SAdrian Prantl     if (!exact_match && ((!lhs_os_specified && !lhs_triple.hasEnvironment()) ||
103805df9cc7SAdrian Prantl                          (!rhs_os_specified && !rhs_triple.hasEnvironment())))
103905df9cc7SAdrian Prantl       return true;
104005df9cc7SAdrian Prantl   }
104105df9cc7SAdrian Prantl 
1042*8a64dd5bSMartin Storsjö   if (!exact_match && both_windows)
1043*8a64dd5bSMartin Storsjö     return true; // The Windows environments (MSVC vs GNU) are compatible
1044*8a64dd5bSMartin Storsjö 
1045a6682a41SJonas Devlieghere   return IsCompatibleEnvironment(lhs_triple_env, rhs_triple_env);
10465f19b907SPavel Labath }
10475f19b907SPavel Labath 
UpdateCore()10485f19b907SPavel Labath void ArchSpec::UpdateCore() {
10495f19b907SPavel Labath   llvm::StringRef arch_name(m_triple.getArchName());
10505f19b907SPavel Labath   const CoreDefinition *core_def = FindCoreDefinition(arch_name);
10515f19b907SPavel Labath   if (core_def) {
10525f19b907SPavel Labath     m_core = core_def->core;
105305097246SAdrian Prantl     // Set the byte order to the default byte order for an architecture. This
105405097246SAdrian Prantl     // can be modified if needed for cases when cores handle both big and
105505097246SAdrian Prantl     // little endian
10565f19b907SPavel Labath     m_byte_order = core_def->default_byte_order;
10575f19b907SPavel Labath   } else {
10585f19b907SPavel Labath     Clear();
10595f19b907SPavel Labath   }
10605f19b907SPavel Labath }
10615f19b907SPavel Labath 
10625f19b907SPavel Labath //===----------------------------------------------------------------------===//
10635f19b907SPavel Labath // Helper methods.
10645f19b907SPavel Labath 
CoreUpdated(bool update_triple)10655f19b907SPavel Labath void ArchSpec::CoreUpdated(bool update_triple) {
10665f19b907SPavel Labath   const CoreDefinition *core_def = FindCoreDefinition(m_core);
10675f19b907SPavel Labath   if (core_def) {
10685f19b907SPavel Labath     if (update_triple)
10695f19b907SPavel Labath       m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
10705f19b907SPavel Labath     m_byte_order = core_def->default_byte_order;
10715f19b907SPavel Labath   } else {
10725f19b907SPavel Labath     if (update_triple)
10735f19b907SPavel Labath       m_triple = llvm::Triple();
10745f19b907SPavel Labath     m_byte_order = eByteOrderInvalid;
10755f19b907SPavel Labath   }
10765f19b907SPavel Labath }
10775f19b907SPavel Labath 
10785f19b907SPavel Labath //===----------------------------------------------------------------------===//
10795f19b907SPavel Labath // Operators.
10805f19b907SPavel Labath 
cores_match(const ArchSpec::Core core1,const ArchSpec::Core core2,bool try_inverse,bool enforce_exact_match)10815f19b907SPavel Labath static bool cores_match(const ArchSpec::Core core1, const ArchSpec::Core core2,
10825f19b907SPavel Labath                         bool try_inverse, bool enforce_exact_match) {
10835f19b907SPavel Labath   if (core1 == core2)
10845f19b907SPavel Labath     return true;
10855f19b907SPavel Labath 
10865f19b907SPavel Labath   switch (core1) {
10875f19b907SPavel Labath   case ArchSpec::kCore_any:
10885f19b907SPavel Labath     return true;
10895f19b907SPavel Labath 
10905f19b907SPavel Labath   case ArchSpec::eCore_arm_generic:
10915f19b907SPavel Labath     if (enforce_exact_match)
10925f19b907SPavel Labath       break;
10935f19b907SPavel Labath     LLVM_FALLTHROUGH;
10945f19b907SPavel Labath   case ArchSpec::kCore_arm_any:
10955f19b907SPavel Labath     if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
10965f19b907SPavel Labath       return true;
10975f19b907SPavel Labath     if (core2 >= ArchSpec::kCore_thumb_first &&
10985f19b907SPavel Labath         core2 <= ArchSpec::kCore_thumb_last)
10995f19b907SPavel Labath       return true;
11005f19b907SPavel Labath     if (core2 == ArchSpec::kCore_arm_any)
11015f19b907SPavel Labath       return true;
11025f19b907SPavel Labath     break;
11035f19b907SPavel Labath 
11045f19b907SPavel Labath   case ArchSpec::kCore_x86_32_any:
11055f19b907SPavel Labath     if ((core2 >= ArchSpec::kCore_x86_32_first &&
11065f19b907SPavel Labath          core2 <= ArchSpec::kCore_x86_32_last) ||
11075f19b907SPavel Labath         (core2 == ArchSpec::kCore_x86_32_any))
11085f19b907SPavel Labath       return true;
11095f19b907SPavel Labath     break;
11105f19b907SPavel Labath 
11115f19b907SPavel Labath   case ArchSpec::kCore_x86_64_any:
11125f19b907SPavel Labath     if ((core2 >= ArchSpec::kCore_x86_64_first &&
11135f19b907SPavel Labath          core2 <= ArchSpec::kCore_x86_64_last) ||
11145f19b907SPavel Labath         (core2 == ArchSpec::kCore_x86_64_any))
11155f19b907SPavel Labath       return true;
11165f19b907SPavel Labath     break;
11175f19b907SPavel Labath 
11185f19b907SPavel Labath   case ArchSpec::kCore_ppc_any:
11195f19b907SPavel Labath     if ((core2 >= ArchSpec::kCore_ppc_first &&
11205f19b907SPavel Labath          core2 <= ArchSpec::kCore_ppc_last) ||
11215f19b907SPavel Labath         (core2 == ArchSpec::kCore_ppc_any))
11225f19b907SPavel Labath       return true;
11235f19b907SPavel Labath     break;
11245f19b907SPavel Labath 
11255f19b907SPavel Labath   case ArchSpec::kCore_ppc64_any:
11265f19b907SPavel Labath     if ((core2 >= ArchSpec::kCore_ppc64_first &&
11275f19b907SPavel Labath          core2 <= ArchSpec::kCore_ppc64_last) ||
11285f19b907SPavel Labath         (core2 == ArchSpec::kCore_ppc64_any))
11295f19b907SPavel Labath       return true;
11305f19b907SPavel Labath     break;
11315f19b907SPavel Labath 
11325f19b907SPavel Labath   case ArchSpec::kCore_hexagon_any:
11335f19b907SPavel Labath     if ((core2 >= ArchSpec::kCore_hexagon_first &&
11345f19b907SPavel Labath          core2 <= ArchSpec::kCore_hexagon_last) ||
11355f19b907SPavel Labath         (core2 == ArchSpec::kCore_hexagon_any))
11365f19b907SPavel Labath       return true;
11375f19b907SPavel Labath     break;
11385f19b907SPavel Labath 
11395f19b907SPavel Labath   // v. https://en.wikipedia.org/wiki/ARM_Cortex-M#Silicon_customization
114071a45e7cSJason Molenda   // Cortex-M0 - ARMv6-M - armv6m
114171a45e7cSJason Molenda   // Cortex-M3 - ARMv7-M - armv7m
114271a45e7cSJason Molenda   // Cortex-M4 - ARMv7E-M - armv7em
11435f19b907SPavel Labath   case ArchSpec::eCore_arm_armv7em:
11445f19b907SPavel Labath     if (!enforce_exact_match) {
11455f19b907SPavel Labath       if (core2 == ArchSpec::eCore_arm_generic)
11465f19b907SPavel Labath         return true;
11475f19b907SPavel Labath       if (core2 == ArchSpec::eCore_arm_armv7m)
11485f19b907SPavel Labath         return true;
11495f19b907SPavel Labath       if (core2 == ArchSpec::eCore_arm_armv6m)
11505f19b907SPavel Labath         return true;
11515f19b907SPavel Labath       if (core2 == ArchSpec::eCore_arm_armv7)
11525f19b907SPavel Labath         return true;
11535f19b907SPavel Labath       try_inverse = true;
11545f19b907SPavel Labath     }
11555f19b907SPavel Labath     break;
11565f19b907SPavel Labath 
11575f19b907SPavel Labath   // v. https://en.wikipedia.org/wiki/ARM_Cortex-M#Silicon_customization
115871a45e7cSJason Molenda   // Cortex-M0 - ARMv6-M - armv6m
115971a45e7cSJason Molenda   // Cortex-M3 - ARMv7-M - armv7m
116071a45e7cSJason Molenda   // Cortex-M4 - ARMv7E-M - armv7em
11615f19b907SPavel Labath   case ArchSpec::eCore_arm_armv7m:
11625f19b907SPavel Labath     if (!enforce_exact_match) {
11635f19b907SPavel Labath       if (core2 == ArchSpec::eCore_arm_generic)
11645f19b907SPavel Labath         return true;
11655f19b907SPavel Labath       if (core2 == ArchSpec::eCore_arm_armv6m)
11665f19b907SPavel Labath         return true;
11675f19b907SPavel Labath       if (core2 == ArchSpec::eCore_arm_armv7)
11685f19b907SPavel Labath         return true;
11695f19b907SPavel Labath       if (core2 == ArchSpec::eCore_arm_armv7em)
11705f19b907SPavel Labath         return true;
11715f19b907SPavel Labath       try_inverse = true;
11725f19b907SPavel Labath     }
11735f19b907SPavel Labath     break;
11745f19b907SPavel Labath 
117571a45e7cSJason Molenda   // v. https://en.wikipedia.org/wiki/ARM_Cortex-M#Silicon_customization
117671a45e7cSJason Molenda   // Cortex-M0 - ARMv6-M - armv6m
117771a45e7cSJason Molenda   // Cortex-M3 - ARMv7-M - armv7m
117871a45e7cSJason Molenda   // Cortex-M4 - ARMv7E-M - armv7em
117971a45e7cSJason Molenda   case ArchSpec::eCore_arm_armv6m:
118071a45e7cSJason Molenda     if (!enforce_exact_match) {
118171a45e7cSJason Molenda       if (core2 == ArchSpec::eCore_arm_generic)
118271a45e7cSJason Molenda         return true;
118371a45e7cSJason Molenda       if (core2 == ArchSpec::eCore_arm_armv7em)
118471a45e7cSJason Molenda         return true;
118571a45e7cSJason Molenda       if (core2 == ArchSpec::eCore_arm_armv7)
118671a45e7cSJason Molenda         return true;
118771a45e7cSJason Molenda       if (core2 == ArchSpec::eCore_arm_armv6m)
118871a45e7cSJason Molenda         return true;
118971a45e7cSJason Molenda       try_inverse = false;
119071a45e7cSJason Molenda     }
119171a45e7cSJason Molenda     break;
119271a45e7cSJason Molenda 
11935f19b907SPavel Labath   case ArchSpec::eCore_arm_armv7f:
11945f19b907SPavel Labath   case ArchSpec::eCore_arm_armv7k:
11955f19b907SPavel Labath   case ArchSpec::eCore_arm_armv7s:
11969b958356SMuhammad Omair Javaid   case ArchSpec::eCore_arm_armv7l:
11979b958356SMuhammad Omair Javaid   case ArchSpec::eCore_arm_armv8l:
11985f19b907SPavel Labath     if (!enforce_exact_match) {
11995f19b907SPavel Labath       if (core2 == ArchSpec::eCore_arm_generic)
12005f19b907SPavel Labath         return true;
12015f19b907SPavel Labath       if (core2 == ArchSpec::eCore_arm_armv7)
12025f19b907SPavel Labath         return true;
12035f19b907SPavel Labath       try_inverse = false;
12045f19b907SPavel Labath     }
12055f19b907SPavel Labath     break;
12065f19b907SPavel Labath 
12075f19b907SPavel Labath   case ArchSpec::eCore_x86_64_x86_64h:
12085f19b907SPavel Labath     if (!enforce_exact_match) {
12095f19b907SPavel Labath       try_inverse = false;
12105f19b907SPavel Labath       if (core2 == ArchSpec::eCore_x86_64_x86_64)
12115f19b907SPavel Labath         return true;
12125f19b907SPavel Labath     }
12135f19b907SPavel Labath     break;
12145f19b907SPavel Labath 
12155f19b907SPavel Labath   case ArchSpec::eCore_arm_armv8:
12165f19b907SPavel Labath     if (!enforce_exact_match) {
12175f19b907SPavel Labath       if (core2 == ArchSpec::eCore_arm_arm64)
12185f19b907SPavel Labath         return true;
12195f19b907SPavel Labath       if (core2 == ArchSpec::eCore_arm_aarch64)
12205f19b907SPavel Labath         return true;
1221baf6c298SJonas Devlieghere       if (core2 == ArchSpec::eCore_arm_arm64e)
1222baf6c298SJonas Devlieghere         return true;
12235f19b907SPavel Labath       try_inverse = false;
12245f19b907SPavel Labath     }
12255f19b907SPavel Labath     break;
12265f19b907SPavel Labath 
1227baf6c298SJonas Devlieghere   case ArchSpec::eCore_arm_arm64e:
1228baf6c298SJonas Devlieghere     if (!enforce_exact_match) {
1229baf6c298SJonas Devlieghere       if (core2 == ArchSpec::eCore_arm_arm64)
1230baf6c298SJonas Devlieghere         return true;
1231baf6c298SJonas Devlieghere       if (core2 == ArchSpec::eCore_arm_aarch64)
1232baf6c298SJonas Devlieghere         return true;
1233baf6c298SJonas Devlieghere       if (core2 == ArchSpec::eCore_arm_armv8)
1234baf6c298SJonas Devlieghere         return true;
1235baf6c298SJonas Devlieghere       try_inverse = false;
1236baf6c298SJonas Devlieghere     }
1237baf6c298SJonas Devlieghere     break;
12385f19b907SPavel Labath   case ArchSpec::eCore_arm_aarch64:
12395f19b907SPavel Labath     if (!enforce_exact_match) {
12405f19b907SPavel Labath       if (core2 == ArchSpec::eCore_arm_arm64)
12415f19b907SPavel Labath         return true;
12425f19b907SPavel Labath       if (core2 == ArchSpec::eCore_arm_armv8)
12435f19b907SPavel Labath         return true;
1244baf6c298SJonas Devlieghere       if (core2 == ArchSpec::eCore_arm_arm64e)
1245baf6c298SJonas Devlieghere         return true;
12465f19b907SPavel Labath       try_inverse = false;
12475f19b907SPavel Labath     }
12485f19b907SPavel Labath     break;
12495f19b907SPavel Labath 
12505f19b907SPavel Labath   case ArchSpec::eCore_arm_arm64:
12515f19b907SPavel Labath     if (!enforce_exact_match) {
12525f19b907SPavel Labath       if (core2 == ArchSpec::eCore_arm_aarch64)
12535f19b907SPavel Labath         return true;
12545f19b907SPavel Labath       if (core2 == ArchSpec::eCore_arm_armv8)
12555f19b907SPavel Labath         return true;
1256baf6c298SJonas Devlieghere       if (core2 == ArchSpec::eCore_arm_arm64e)
1257baf6c298SJonas Devlieghere         return true;
12585f19b907SPavel Labath       try_inverse = false;
12595f19b907SPavel Labath     }
12605f19b907SPavel Labath     break;
12615f19b907SPavel Labath 
12627dd7a360SJason Molenda   case ArchSpec::eCore_arm_arm64_32:
12637dd7a360SJason Molenda     if (!enforce_exact_match) {
12647dd7a360SJason Molenda       if (core2 == ArchSpec::eCore_arm_generic)
12657dd7a360SJason Molenda         return true;
12667dd7a360SJason Molenda       try_inverse = false;
12677dd7a360SJason Molenda     }
12687dd7a360SJason Molenda     break;
12697dd7a360SJason Molenda 
12705f19b907SPavel Labath   case ArchSpec::eCore_mips32:
12715f19b907SPavel Labath     if (!enforce_exact_match) {
12725f19b907SPavel Labath       if (core2 >= ArchSpec::kCore_mips32_first &&
12735f19b907SPavel Labath           core2 <= ArchSpec::kCore_mips32_last)
12745f19b907SPavel Labath         return true;
12755f19b907SPavel Labath       try_inverse = false;
12765f19b907SPavel Labath     }
12775f19b907SPavel Labath     break;
12785f19b907SPavel Labath 
12795f19b907SPavel Labath   case ArchSpec::eCore_mips32el:
12805f19b907SPavel Labath     if (!enforce_exact_match) {
12815f19b907SPavel Labath       if (core2 >= ArchSpec::kCore_mips32el_first &&
12825f19b907SPavel Labath           core2 <= ArchSpec::kCore_mips32el_last)
12835f19b907SPavel Labath         return true;
12845f19b907SPavel Labath       try_inverse = true;
12855f19b907SPavel Labath     }
12865f19b907SPavel Labath     break;
12875f19b907SPavel Labath 
12885f19b907SPavel Labath   case ArchSpec::eCore_mips64:
12895f19b907SPavel Labath     if (!enforce_exact_match) {
12905f19b907SPavel Labath       if (core2 >= ArchSpec::kCore_mips32_first &&
12915f19b907SPavel Labath           core2 <= ArchSpec::kCore_mips32_last)
12925f19b907SPavel Labath         return true;
12935f19b907SPavel Labath       if (core2 >= ArchSpec::kCore_mips64_first &&
12945f19b907SPavel Labath           core2 <= ArchSpec::kCore_mips64_last)
12955f19b907SPavel Labath         return true;
12965f19b907SPavel Labath       try_inverse = false;
12975f19b907SPavel Labath     }
12985f19b907SPavel Labath     break;
12995f19b907SPavel Labath 
13005f19b907SPavel Labath   case ArchSpec::eCore_mips64el:
13015f19b907SPavel Labath     if (!enforce_exact_match) {
13025f19b907SPavel Labath       if (core2 >= ArchSpec::kCore_mips32el_first &&
13035f19b907SPavel Labath           core2 <= ArchSpec::kCore_mips32el_last)
13045f19b907SPavel Labath         return true;
13055f19b907SPavel Labath       if (core2 >= ArchSpec::kCore_mips64el_first &&
13065f19b907SPavel Labath           core2 <= ArchSpec::kCore_mips64el_last)
13075f19b907SPavel Labath         return true;
13085f19b907SPavel Labath       try_inverse = false;
13095f19b907SPavel Labath     }
13105f19b907SPavel Labath     break;
13115f19b907SPavel Labath 
13125f19b907SPavel Labath   case ArchSpec::eCore_mips64r2:
13135f19b907SPavel Labath   case ArchSpec::eCore_mips64r3:
13145f19b907SPavel Labath   case ArchSpec::eCore_mips64r5:
13155f19b907SPavel Labath     if (!enforce_exact_match) {
13165f19b907SPavel Labath       if (core2 >= ArchSpec::kCore_mips32_first && core2 <= (core1 - 10))
13175f19b907SPavel Labath         return true;
13185f19b907SPavel Labath       if (core2 >= ArchSpec::kCore_mips64_first && core2 <= (core1 - 1))
13195f19b907SPavel Labath         return true;
13205f19b907SPavel Labath       try_inverse = false;
13215f19b907SPavel Labath     }
13225f19b907SPavel Labath     break;
13235f19b907SPavel Labath 
13245f19b907SPavel Labath   case ArchSpec::eCore_mips64r2el:
13255f19b907SPavel Labath   case ArchSpec::eCore_mips64r3el:
13265f19b907SPavel Labath   case ArchSpec::eCore_mips64r5el:
13275f19b907SPavel Labath     if (!enforce_exact_match) {
13285f19b907SPavel Labath       if (core2 >= ArchSpec::kCore_mips32el_first && core2 <= (core1 - 10))
13295f19b907SPavel Labath         return true;
13305f19b907SPavel Labath       if (core2 >= ArchSpec::kCore_mips64el_first && core2 <= (core1 - 1))
13315f19b907SPavel Labath         return true;
13325f19b907SPavel Labath       try_inverse = false;
13335f19b907SPavel Labath     }
13345f19b907SPavel Labath     break;
13355f19b907SPavel Labath 
13365f19b907SPavel Labath   case ArchSpec::eCore_mips32r2:
13375f19b907SPavel Labath   case ArchSpec::eCore_mips32r3:
13385f19b907SPavel Labath   case ArchSpec::eCore_mips32r5:
13395f19b907SPavel Labath     if (!enforce_exact_match) {
13405f19b907SPavel Labath       if (core2 >= ArchSpec::kCore_mips32_first && core2 <= core1)
13415f19b907SPavel Labath         return true;
13425f19b907SPavel Labath     }
13435f19b907SPavel Labath     break;
13445f19b907SPavel Labath 
13455f19b907SPavel Labath   case ArchSpec::eCore_mips32r2el:
13465f19b907SPavel Labath   case ArchSpec::eCore_mips32r3el:
13475f19b907SPavel Labath   case ArchSpec::eCore_mips32r5el:
13485f19b907SPavel Labath     if (!enforce_exact_match) {
13495f19b907SPavel Labath       if (core2 >= ArchSpec::kCore_mips32el_first && core2 <= core1)
13505f19b907SPavel Labath         return true;
13515f19b907SPavel Labath     }
13525f19b907SPavel Labath     break;
13535f19b907SPavel Labath 
13545f19b907SPavel Labath   case ArchSpec::eCore_mips32r6:
13555f19b907SPavel Labath     if (!enforce_exact_match) {
13565f19b907SPavel Labath       if (core2 == ArchSpec::eCore_mips32 || core2 == ArchSpec::eCore_mips32r6)
13575f19b907SPavel Labath         return true;
13585f19b907SPavel Labath     }
13595f19b907SPavel Labath     break;
13605f19b907SPavel Labath 
13615f19b907SPavel Labath   case ArchSpec::eCore_mips32r6el:
13625f19b907SPavel Labath     if (!enforce_exact_match) {
13635f19b907SPavel Labath       if (core2 == ArchSpec::eCore_mips32el ||
13645f19b907SPavel Labath           core2 == ArchSpec::eCore_mips32r6el)
13655f19b907SPavel Labath         return true;
13665f19b907SPavel Labath     }
13675f19b907SPavel Labath     break;
13685f19b907SPavel Labath 
13695f19b907SPavel Labath   case ArchSpec::eCore_mips64r6:
13705f19b907SPavel Labath     if (!enforce_exact_match) {
13715f19b907SPavel Labath       if (core2 == ArchSpec::eCore_mips32 || core2 == ArchSpec::eCore_mips32r6)
13725f19b907SPavel Labath         return true;
13735f19b907SPavel Labath       if (core2 == ArchSpec::eCore_mips64 || core2 == ArchSpec::eCore_mips64r6)
13745f19b907SPavel Labath         return true;
13755f19b907SPavel Labath     }
13765f19b907SPavel Labath     break;
13775f19b907SPavel Labath 
13785f19b907SPavel Labath   case ArchSpec::eCore_mips64r6el:
13795f19b907SPavel Labath     if (!enforce_exact_match) {
13805f19b907SPavel Labath       if (core2 == ArchSpec::eCore_mips32el ||
13815f19b907SPavel Labath           core2 == ArchSpec::eCore_mips32r6el)
13825f19b907SPavel Labath         return true;
13835f19b907SPavel Labath       if (core2 == ArchSpec::eCore_mips64el ||
13845f19b907SPavel Labath           core2 == ArchSpec::eCore_mips64r6el)
13855f19b907SPavel Labath         return true;
13865f19b907SPavel Labath     }
13875f19b907SPavel Labath     break;
13885f19b907SPavel Labath 
13895f19b907SPavel Labath   default:
13905f19b907SPavel Labath     break;
13915f19b907SPavel Labath   }
13925f19b907SPavel Labath   if (try_inverse)
13935f19b907SPavel Labath     return cores_match(core2, core1, false, enforce_exact_match);
13945f19b907SPavel Labath   return false;
13955f19b907SPavel Labath }
13965f19b907SPavel Labath 
operator <(const ArchSpec & lhs,const ArchSpec & rhs)13975f19b907SPavel Labath bool lldb_private::operator<(const ArchSpec &lhs, const ArchSpec &rhs) {
13985f19b907SPavel Labath   const ArchSpec::Core lhs_core = lhs.GetCore();
13995f19b907SPavel Labath   const ArchSpec::Core rhs_core = rhs.GetCore();
14005f19b907SPavel Labath   return lhs_core < rhs_core;
14015f19b907SPavel Labath }
14025f19b907SPavel Labath 
140336e82208SPavel Labath 
operator ==(const ArchSpec & lhs,const ArchSpec & rhs)140436e82208SPavel Labath bool lldb_private::operator==(const ArchSpec &lhs, const ArchSpec &rhs) {
140536e82208SPavel Labath   return lhs.GetCore() == rhs.GetCore();
140636e82208SPavel Labath }
140736e82208SPavel Labath 
IsFullySpecifiedTriple() const14085f19b907SPavel Labath bool ArchSpec::IsFullySpecifiedTriple() const {
1409942c21edSAdrian Prantl   if (!TripleOSWasSpecified())
1410942c21edSAdrian Prantl     return false;
14115f19b907SPavel Labath 
1412942c21edSAdrian Prantl   if (!TripleVendorWasSpecified())
1413942c21edSAdrian Prantl     return false;
14145f19b907SPavel Labath 
14155f19b907SPavel Labath   const unsigned unspecified = 0;
1416942c21edSAdrian Prantl   const llvm::Triple &triple = GetTriple();
1417942c21edSAdrian Prantl   if (triple.isOSDarwin() && triple.getOSMajorVersion() == unspecified)
1418942c21edSAdrian Prantl     return false;
14195f19b907SPavel Labath 
1420942c21edSAdrian Prantl   return true;
14215f19b907SPavel Labath }
14225f19b907SPavel Labath 
PiecewiseTripleCompare(const ArchSpec & other,bool & arch_different,bool & vendor_different,bool & os_different,bool & os_version_different,bool & env_different) const14235f19b907SPavel Labath void ArchSpec::PiecewiseTripleCompare(
14245f19b907SPavel Labath     const ArchSpec &other, bool &arch_different, bool &vendor_different,
14255f19b907SPavel Labath     bool &os_different, bool &os_version_different, bool &env_different) const {
14265f19b907SPavel Labath   const llvm::Triple &me(GetTriple());
14275f19b907SPavel Labath   const llvm::Triple &them(other.GetTriple());
14285f19b907SPavel Labath 
14295f19b907SPavel Labath   arch_different = (me.getArch() != them.getArch());
14305f19b907SPavel Labath 
14315f19b907SPavel Labath   vendor_different = (me.getVendor() != them.getVendor());
14325f19b907SPavel Labath 
14335f19b907SPavel Labath   os_different = (me.getOS() != them.getOS());
14345f19b907SPavel Labath 
14355f19b907SPavel Labath   os_version_different = (me.getOSMajorVersion() != them.getOSMajorVersion());
14365f19b907SPavel Labath 
14375f19b907SPavel Labath   env_different = (me.getEnvironment() != them.getEnvironment());
14385f19b907SPavel Labath }
14395f19b907SPavel Labath 
IsAlwaysThumbInstructions() const14405f19b907SPavel Labath bool ArchSpec::IsAlwaysThumbInstructions() const {
14415f19b907SPavel Labath   std::string Status;
14425f19b907SPavel Labath   if (GetTriple().getArch() == llvm::Triple::arm ||
14435f19b907SPavel Labath       GetTriple().getArch() == llvm::Triple::thumb) {
14445f19b907SPavel Labath     // v. https://en.wikipedia.org/wiki/ARM_Cortex-M
14455f19b907SPavel Labath     //
14465f19b907SPavel Labath     // Cortex-M0 through Cortex-M7 are ARM processor cores which can only
14475f19b907SPavel Labath     // execute thumb instructions.  We map the cores to arch names like this:
14485f19b907SPavel Labath     //
144905097246SAdrian Prantl     // Cortex-M0, Cortex-M0+, Cortex-M1:  armv6m Cortex-M3: armv7m Cortex-M4,
145005097246SAdrian Prantl     // Cortex-M7: armv7em
14515f19b907SPavel Labath 
14525f19b907SPavel Labath     if (GetCore() == ArchSpec::Core::eCore_arm_armv7m ||
14535f19b907SPavel Labath         GetCore() == ArchSpec::Core::eCore_arm_armv7em ||
14540dfb84ceSJason Molenda         GetCore() == ArchSpec::Core::eCore_arm_armv6m ||
14550dfb84ceSJason Molenda         GetCore() == ArchSpec::Core::eCore_thumbv7m ||
14560dfb84ceSJason Molenda         GetCore() == ArchSpec::Core::eCore_thumbv7em ||
14570dfb84ceSJason Molenda         GetCore() == ArchSpec::Core::eCore_thumbv6m) {
14585f19b907SPavel Labath       return true;
14595f19b907SPavel Labath     }
1460f5c54f40SMartin Storsjö     // Windows on ARM is always thumb.
1461f5c54f40SMartin Storsjö     if (GetTriple().isOSWindows())
1462f5c54f40SMartin Storsjö       return true;
14635f19b907SPavel Labath   }
14645f19b907SPavel Labath   return false;
14655f19b907SPavel Labath }
14665f19b907SPavel Labath 
DumpTriple(llvm::raw_ostream & s) const14672f1e7b3dSRaphael Isemann void ArchSpec::DumpTriple(llvm::raw_ostream &s) const {
14685f19b907SPavel Labath   const llvm::Triple &triple = GetTriple();
14695f19b907SPavel Labath   llvm::StringRef arch_str = triple.getArchName();
14705f19b907SPavel Labath   llvm::StringRef vendor_str = triple.getVendorName();
14715f19b907SPavel Labath   llvm::StringRef os_str = triple.getOSName();
14725f19b907SPavel Labath   llvm::StringRef environ_str = triple.getEnvironmentName();
14735f19b907SPavel Labath 
14742f1e7b3dSRaphael Isemann   s << llvm::formatv("{0}-{1}-{2}", arch_str.empty() ? "*" : arch_str,
14752f1e7b3dSRaphael Isemann                      vendor_str.empty() ? "*" : vendor_str,
14762f1e7b3dSRaphael Isemann                      os_str.empty() ? "*" : os_str);
14775f19b907SPavel Labath 
14785f19b907SPavel Labath   if (!environ_str.empty())
14792f1e7b3dSRaphael Isemann     s << "-" << environ_str;
14805f19b907SPavel Labath }
14810ce3b710SJonas Devlieghere 
output(const ArchSpec & Val,void *,raw_ostream & Out)14820ce3b710SJonas Devlieghere void llvm::yaml::ScalarTraits<ArchSpec>::output(const ArchSpec &Val, void *,
14830ce3b710SJonas Devlieghere                                                 raw_ostream &Out) {
14840ce3b710SJonas Devlieghere   Val.DumpTriple(Out);
14850ce3b710SJonas Devlieghere }
14860ce3b710SJonas Devlieghere 
14870ce3b710SJonas Devlieghere llvm::StringRef
input(llvm::StringRef Scalar,void *,ArchSpec & Val)14880ce3b710SJonas Devlieghere llvm::yaml::ScalarTraits<ArchSpec>::input(llvm::StringRef Scalar, void *,
14890ce3b710SJonas Devlieghere                                           ArchSpec &Val) {
14900ce3b710SJonas Devlieghere   Val = ArchSpec(Scalar);
14910ce3b710SJonas Devlieghere   return {};
14920ce3b710SJonas Devlieghere }
1493