1 //===-- ArchSpec.cpp --------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/ArchSpec.h"
11 #include "lldb/Utility/Log.h"
12 #include "lldb/Utility/StringList.h"
13 #include "lldb/lldb-defines.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/BinaryFormat/COFF.h"
16 #include "llvm/BinaryFormat/ELF.h"
17 #include "llvm/BinaryFormat/MachO.h"
18 #include "llvm/Support/Compiler.h"
21 using namespace lldb_private;
23 static bool cores_match(const ArchSpec::Core core1, const ArchSpec::Core core2,
24 bool try_inverse, bool enforce_exact_match);
26 namespace lldb_private {
28 struct CoreDefinition {
29 ByteOrder default_byte_order;
30 uint32_t addr_byte_size;
31 uint32_t min_opcode_byte_size;
32 uint32_t max_opcode_byte_size;
33 llvm::Triple::ArchType machine;
35 const char *const name;
38 } // namespace lldb_private
40 // This core information can be looked using the ArchSpec::Core as the index
41 static const CoreDefinition g_core_definitions[] = {
42 {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_generic,
44 {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv4,
46 {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv4t,
48 {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv5,
50 {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv5e,
52 {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv5t,
54 {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv6,
56 {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv6m,
58 {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7,
60 {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7l,
62 {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7f,
64 {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7s,
66 {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7k,
68 {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7m,
70 {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7em,
72 {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_xscale,
74 {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumb,
76 {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv4t,
78 {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv5,
80 {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv5e,
82 {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv6,
84 {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv6m,
86 {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7,
88 {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7f,
90 {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7s,
92 {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7k,
94 {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7m,
96 {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7em,
98 {eByteOrderLittle, 8, 4, 4, llvm::Triple::aarch64,
99 ArchSpec::eCore_arm_arm64, "arm64"},
100 {eByteOrderLittle, 8, 4, 4, llvm::Triple::aarch64,
101 ArchSpec::eCore_arm_armv8, "armv8"},
102 {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm,
103 ArchSpec::eCore_arm_armv8l, "armv8l"},
104 {eByteOrderLittle, 4, 4, 4, llvm::Triple::aarch64_32,
105 ArchSpec::eCore_arm_arm64_32, "arm64_32"},
106 {eByteOrderLittle, 8, 4, 4, llvm::Triple::aarch64,
107 ArchSpec::eCore_arm_aarch64, "aarch64"},
109 // mips32, mips32r2, mips32r3, mips32r5, mips32r6
110 {eByteOrderBig, 4, 2, 4, llvm::Triple::mips, ArchSpec::eCore_mips32,
112 {eByteOrderBig, 4, 2, 4, llvm::Triple::mips, ArchSpec::eCore_mips32r2,
114 {eByteOrderBig, 4, 2, 4, llvm::Triple::mips, ArchSpec::eCore_mips32r3,
116 {eByteOrderBig, 4, 2, 4, llvm::Triple::mips, ArchSpec::eCore_mips32r5,
118 {eByteOrderBig, 4, 2, 4, llvm::Triple::mips, ArchSpec::eCore_mips32r6,
120 {eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel, ArchSpec::eCore_mips32el,
122 {eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel,
123 ArchSpec::eCore_mips32r2el, "mipsr2el"},
124 {eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel,
125 ArchSpec::eCore_mips32r3el, "mipsr3el"},
126 {eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel,
127 ArchSpec::eCore_mips32r5el, "mipsr5el"},
128 {eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel,
129 ArchSpec::eCore_mips32r6el, "mipsr6el"},
131 // mips64, mips64r2, mips64r3, mips64r5, mips64r6
132 {eByteOrderBig, 8, 2, 4, llvm::Triple::mips64, ArchSpec::eCore_mips64,
134 {eByteOrderBig, 8, 2, 4, llvm::Triple::mips64, ArchSpec::eCore_mips64r2,
136 {eByteOrderBig, 8, 2, 4, llvm::Triple::mips64, ArchSpec::eCore_mips64r3,
138 {eByteOrderBig, 8, 2, 4, llvm::Triple::mips64, ArchSpec::eCore_mips64r5,
140 {eByteOrderBig, 8, 2, 4, llvm::Triple::mips64, ArchSpec::eCore_mips64r6,
142 {eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el,
143 ArchSpec::eCore_mips64el, "mips64el"},
144 {eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el,
145 ArchSpec::eCore_mips64r2el, "mips64r2el"},
146 {eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el,
147 ArchSpec::eCore_mips64r3el, "mips64r3el"},
148 {eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el,
149 ArchSpec::eCore_mips64r5el, "mips64r5el"},
150 {eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el,
151 ArchSpec::eCore_mips64r6el, "mips64r6el"},
153 {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_generic,
155 {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc601,
157 {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc602,
159 {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc603,
161 {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc603e,
163 {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc603ev,
165 {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc604,
167 {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc604e,
169 {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc620,
171 {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc750,
173 {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc7400,
175 {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc7450,
177 {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc970,
180 {eByteOrderLittle, 8, 4, 4, llvm::Triple::ppc64le,
181 ArchSpec::eCore_ppc64le_generic, "powerpc64le"},
182 {eByteOrderBig, 8, 4, 4, llvm::Triple::ppc64, ArchSpec::eCore_ppc64_generic,
184 {eByteOrderBig, 8, 4, 4, llvm::Triple::ppc64,
185 ArchSpec::eCore_ppc64_ppc970_64, "ppc970-64"},
187 {eByteOrderBig, 8, 2, 6, llvm::Triple::systemz,
188 ArchSpec::eCore_s390x_generic, "s390x"},
190 {eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc,
191 ArchSpec::eCore_sparc_generic, "sparc"},
192 {eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9,
193 ArchSpec::eCore_sparc9_generic, "sparcv9"},
195 {eByteOrderLittle, 4, 1, 15, llvm::Triple::x86, ArchSpec::eCore_x86_32_i386,
197 {eByteOrderLittle, 4, 1, 15, llvm::Triple::x86, ArchSpec::eCore_x86_32_i486,
199 {eByteOrderLittle, 4, 1, 15, llvm::Triple::x86,
200 ArchSpec::eCore_x86_32_i486sx, "i486sx"},
201 {eByteOrderLittle, 4, 1, 15, llvm::Triple::x86, ArchSpec::eCore_x86_32_i686,
204 {eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64,
205 ArchSpec::eCore_x86_64_x86_64, "x86_64"},
206 {eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64,
207 ArchSpec::eCore_x86_64_x86_64h, "x86_64h"},
208 {eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon,
209 ArchSpec::eCore_hexagon_generic, "hexagon"},
210 {eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon,
211 ArchSpec::eCore_hexagon_hexagonv4, "hexagonv4"},
212 {eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon,
213 ArchSpec::eCore_hexagon_hexagonv5, "hexagonv5"},
215 {eByteOrderLittle, 4, 4, 4, llvm::Triple::UnknownArch,
216 ArchSpec::eCore_uknownMach32, "unknown-mach-32"},
217 {eByteOrderLittle, 8, 4, 4, llvm::Triple::UnknownArch,
218 ArchSpec::eCore_uknownMach64, "unknown-mach-64"},
219 {eByteOrderLittle, 4, 2, 4, llvm::Triple::arc, ArchSpec::eCore_arc, "arc"}
222 // Ensure that we have an entry in the g_core_definitions for each core. If you
223 // comment out an entry above, you will need to comment out the corresponding
224 // ArchSpec::Core enumeration.
225 static_assert(sizeof(g_core_definitions) / sizeof(CoreDefinition) ==
227 "make sure we have one core definition for each core");
229 struct ArchDefinitionEntry {
237 struct ArchDefinition {
238 ArchitectureType type;
240 const ArchDefinitionEntry *entries;
244 void ArchSpec::ListSupportedArchNames(StringList &list) {
245 for (uint32_t i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
246 list.AppendString(g_core_definitions[i].name);
249 void ArchSpec::AutoComplete(CompletionRequest &request) {
250 for (uint32_t i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
251 request.TryCompleteCurrentArg(g_core_definitions[i].name);
254 #define CPU_ANY (UINT32_MAX)
256 //===----------------------------------------------------------------------===//
257 // A table that gets searched linearly for matches. This table is used to
258 // convert cpu type and subtypes to architecture names, and to convert
259 // architecture names to cpu types and subtypes. The ordering is important and
260 // allows the precedence to be set when the table is built.
261 #define SUBTYPE_MASK 0x00FFFFFFu
263 static const ArchDefinitionEntry g_macho_arch_entries[] = {
264 {ArchSpec::eCore_arm_generic, llvm::MachO::CPU_TYPE_ARM, CPU_ANY,
265 UINT32_MAX, UINT32_MAX},
266 {ArchSpec::eCore_arm_generic, llvm::MachO::CPU_TYPE_ARM, 0, UINT32_MAX,
268 {ArchSpec::eCore_arm_armv4, llvm::MachO::CPU_TYPE_ARM, 5, UINT32_MAX,
270 {ArchSpec::eCore_arm_armv4t, llvm::MachO::CPU_TYPE_ARM, 5, UINT32_MAX,
272 {ArchSpec::eCore_arm_armv6, llvm::MachO::CPU_TYPE_ARM, 6, UINT32_MAX,
274 {ArchSpec::eCore_arm_armv6m, llvm::MachO::CPU_TYPE_ARM, 14, UINT32_MAX,
276 {ArchSpec::eCore_arm_armv5, llvm::MachO::CPU_TYPE_ARM, 7, UINT32_MAX,
278 {ArchSpec::eCore_arm_armv5e, llvm::MachO::CPU_TYPE_ARM, 7, UINT32_MAX,
280 {ArchSpec::eCore_arm_armv5t, llvm::MachO::CPU_TYPE_ARM, 7, UINT32_MAX,
282 {ArchSpec::eCore_arm_xscale, llvm::MachO::CPU_TYPE_ARM, 8, UINT32_MAX,
284 {ArchSpec::eCore_arm_armv7, llvm::MachO::CPU_TYPE_ARM, 9, UINT32_MAX,
286 {ArchSpec::eCore_arm_armv7f, llvm::MachO::CPU_TYPE_ARM, 10, UINT32_MAX,
288 {ArchSpec::eCore_arm_armv7s, llvm::MachO::CPU_TYPE_ARM, 11, UINT32_MAX,
290 {ArchSpec::eCore_arm_armv7k, llvm::MachO::CPU_TYPE_ARM, 12, UINT32_MAX,
292 {ArchSpec::eCore_arm_armv7m, llvm::MachO::CPU_TYPE_ARM, 15, UINT32_MAX,
294 {ArchSpec::eCore_arm_armv7em, llvm::MachO::CPU_TYPE_ARM, 16, UINT32_MAX,
296 {ArchSpec::eCore_arm_arm64, llvm::MachO::CPU_TYPE_ARM64, 1, UINT32_MAX,
298 {ArchSpec::eCore_arm_arm64, llvm::MachO::CPU_TYPE_ARM64, 0, UINT32_MAX,
300 {ArchSpec::eCore_arm_arm64, llvm::MachO::CPU_TYPE_ARM64, 13, UINT32_MAX,
302 {ArchSpec::eCore_arm_arm64_32, llvm::MachO::CPU_TYPE_ARM64_32, 0,
303 UINT32_MAX, SUBTYPE_MASK},
304 {ArchSpec::eCore_arm_arm64_32, llvm::MachO::CPU_TYPE_ARM64_32, 1,
305 UINT32_MAX, SUBTYPE_MASK},
306 {ArchSpec::eCore_arm_arm64, llvm::MachO::CPU_TYPE_ARM64, CPU_ANY,
307 UINT32_MAX, SUBTYPE_MASK},
308 {ArchSpec::eCore_thumb, llvm::MachO::CPU_TYPE_ARM, 0, UINT32_MAX,
310 {ArchSpec::eCore_thumbv4t, llvm::MachO::CPU_TYPE_ARM, 5, UINT32_MAX,
312 {ArchSpec::eCore_thumbv5, llvm::MachO::CPU_TYPE_ARM, 7, UINT32_MAX,
314 {ArchSpec::eCore_thumbv5e, llvm::MachO::CPU_TYPE_ARM, 7, UINT32_MAX,
316 {ArchSpec::eCore_thumbv6, llvm::MachO::CPU_TYPE_ARM, 6, UINT32_MAX,
318 {ArchSpec::eCore_thumbv6m, llvm::MachO::CPU_TYPE_ARM, 14, UINT32_MAX,
320 {ArchSpec::eCore_thumbv7, llvm::MachO::CPU_TYPE_ARM, 9, UINT32_MAX,
322 {ArchSpec::eCore_thumbv7f, llvm::MachO::CPU_TYPE_ARM, 10, UINT32_MAX,
324 {ArchSpec::eCore_thumbv7s, llvm::MachO::CPU_TYPE_ARM, 11, UINT32_MAX,
326 {ArchSpec::eCore_thumbv7k, llvm::MachO::CPU_TYPE_ARM, 12, UINT32_MAX,
328 {ArchSpec::eCore_thumbv7m, llvm::MachO::CPU_TYPE_ARM, 15, UINT32_MAX,
330 {ArchSpec::eCore_thumbv7em, llvm::MachO::CPU_TYPE_ARM, 16, UINT32_MAX,
332 {ArchSpec::eCore_ppc_generic, llvm::MachO::CPU_TYPE_POWERPC, CPU_ANY,
333 UINT32_MAX, UINT32_MAX},
334 {ArchSpec::eCore_ppc_generic, llvm::MachO::CPU_TYPE_POWERPC, 0, UINT32_MAX,
336 {ArchSpec::eCore_ppc_ppc601, llvm::MachO::CPU_TYPE_POWERPC, 1, UINT32_MAX,
338 {ArchSpec::eCore_ppc_ppc602, llvm::MachO::CPU_TYPE_POWERPC, 2, UINT32_MAX,
340 {ArchSpec::eCore_ppc_ppc603, llvm::MachO::CPU_TYPE_POWERPC, 3, UINT32_MAX,
342 {ArchSpec::eCore_ppc_ppc603e, llvm::MachO::CPU_TYPE_POWERPC, 4, UINT32_MAX,
344 {ArchSpec::eCore_ppc_ppc603ev, llvm::MachO::CPU_TYPE_POWERPC, 5, UINT32_MAX,
346 {ArchSpec::eCore_ppc_ppc604, llvm::MachO::CPU_TYPE_POWERPC, 6, UINT32_MAX,
348 {ArchSpec::eCore_ppc_ppc604e, llvm::MachO::CPU_TYPE_POWERPC, 7, UINT32_MAX,
350 {ArchSpec::eCore_ppc_ppc620, llvm::MachO::CPU_TYPE_POWERPC, 8, UINT32_MAX,
352 {ArchSpec::eCore_ppc_ppc750, llvm::MachO::CPU_TYPE_POWERPC, 9, UINT32_MAX,
354 {ArchSpec::eCore_ppc_ppc7400, llvm::MachO::CPU_TYPE_POWERPC, 10, UINT32_MAX,
356 {ArchSpec::eCore_ppc_ppc7450, llvm::MachO::CPU_TYPE_POWERPC, 11, UINT32_MAX,
358 {ArchSpec::eCore_ppc_ppc970, llvm::MachO::CPU_TYPE_POWERPC, 100, UINT32_MAX,
360 {ArchSpec::eCore_ppc64_generic, llvm::MachO::CPU_TYPE_POWERPC64, 0,
361 UINT32_MAX, SUBTYPE_MASK},
362 {ArchSpec::eCore_ppc64le_generic, llvm::MachO::CPU_TYPE_POWERPC64, CPU_ANY,
363 UINT32_MAX, SUBTYPE_MASK},
364 {ArchSpec::eCore_ppc64_ppc970_64, llvm::MachO::CPU_TYPE_POWERPC64, 100,
365 UINT32_MAX, SUBTYPE_MASK},
366 {ArchSpec::eCore_x86_32_i386, llvm::MachO::CPU_TYPE_I386, 3, UINT32_MAX,
368 {ArchSpec::eCore_x86_32_i486, llvm::MachO::CPU_TYPE_I386, 4, UINT32_MAX,
370 {ArchSpec::eCore_x86_32_i486sx, llvm::MachO::CPU_TYPE_I386, 0x84,
371 UINT32_MAX, SUBTYPE_MASK},
372 {ArchSpec::eCore_x86_32_i386, llvm::MachO::CPU_TYPE_I386, CPU_ANY,
373 UINT32_MAX, UINT32_MAX},
374 {ArchSpec::eCore_x86_64_x86_64, llvm::MachO::CPU_TYPE_X86_64, 3, UINT32_MAX,
376 {ArchSpec::eCore_x86_64_x86_64, llvm::MachO::CPU_TYPE_X86_64, 4, UINT32_MAX,
378 {ArchSpec::eCore_x86_64_x86_64h, llvm::MachO::CPU_TYPE_X86_64, 8,
379 UINT32_MAX, SUBTYPE_MASK},
380 {ArchSpec::eCore_x86_64_x86_64, llvm::MachO::CPU_TYPE_X86_64, CPU_ANY,
381 UINT32_MAX, UINT32_MAX},
382 // Catch any unknown mach architectures so we can always use the object and
383 // symbol mach-o files
384 {ArchSpec::eCore_uknownMach32, 0, 0, 0xFF000000u, 0x00000000u},
385 {ArchSpec::eCore_uknownMach64, llvm::MachO::CPU_ARCH_ABI64, 0, 0xFF000000u,
388 static const ArchDefinition g_macho_arch_def = {
389 eArchTypeMachO, llvm::array_lengthof(g_macho_arch_entries),
390 g_macho_arch_entries, "mach-o"};
392 //===----------------------------------------------------------------------===//
393 // A table that gets searched linearly for matches. This table is used to
394 // convert cpu type and subtypes to architecture names, and to convert
395 // architecture names to cpu types and subtypes. The ordering is important and
396 // allows the precedence to be set when the table is built.
397 static const ArchDefinitionEntry g_elf_arch_entries[] = {
398 {ArchSpec::eCore_sparc_generic, llvm::ELF::EM_SPARC, LLDB_INVALID_CPUTYPE,
399 0xFFFFFFFFu, 0xFFFFFFFFu}, // Sparc
400 {ArchSpec::eCore_x86_32_i386, llvm::ELF::EM_386, LLDB_INVALID_CPUTYPE,
401 0xFFFFFFFFu, 0xFFFFFFFFu}, // Intel 80386
402 {ArchSpec::eCore_x86_32_i486, llvm::ELF::EM_IAMCU, LLDB_INVALID_CPUTYPE,
403 0xFFFFFFFFu, 0xFFFFFFFFu}, // Intel MCU // FIXME: is this correct?
404 {ArchSpec::eCore_ppc_generic, llvm::ELF::EM_PPC, LLDB_INVALID_CPUTYPE,
405 0xFFFFFFFFu, 0xFFFFFFFFu}, // PowerPC
406 {ArchSpec::eCore_ppc64le_generic, llvm::ELF::EM_PPC64, LLDB_INVALID_CPUTYPE,
407 0xFFFFFFFFu, 0xFFFFFFFFu}, // PowerPC64le
408 {ArchSpec::eCore_ppc64_generic, llvm::ELF::EM_PPC64, LLDB_INVALID_CPUTYPE,
409 0xFFFFFFFFu, 0xFFFFFFFFu}, // PowerPC64
410 {ArchSpec::eCore_arm_generic, llvm::ELF::EM_ARM, LLDB_INVALID_CPUTYPE,
411 0xFFFFFFFFu, 0xFFFFFFFFu}, // ARM
412 {ArchSpec::eCore_arm_aarch64, llvm::ELF::EM_AARCH64, LLDB_INVALID_CPUTYPE,
413 0xFFFFFFFFu, 0xFFFFFFFFu}, // ARM64
414 {ArchSpec::eCore_s390x_generic, llvm::ELF::EM_S390, LLDB_INVALID_CPUTYPE,
415 0xFFFFFFFFu, 0xFFFFFFFFu}, // SystemZ
416 {ArchSpec::eCore_sparc9_generic, llvm::ELF::EM_SPARCV9,
417 LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // SPARC V9
418 {ArchSpec::eCore_x86_64_x86_64, llvm::ELF::EM_X86_64, LLDB_INVALID_CPUTYPE,
419 0xFFFFFFFFu, 0xFFFFFFFFu}, // AMD64
420 {ArchSpec::eCore_mips32, llvm::ELF::EM_MIPS, ArchSpec::eMIPSSubType_mips32,
421 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips32
422 {ArchSpec::eCore_mips32r2, llvm::ELF::EM_MIPS,
423 ArchSpec::eMIPSSubType_mips32r2, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips32r2
424 {ArchSpec::eCore_mips32r6, llvm::ELF::EM_MIPS,
425 ArchSpec::eMIPSSubType_mips32r6, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips32r6
426 {ArchSpec::eCore_mips32el, llvm::ELF::EM_MIPS,
427 ArchSpec::eMIPSSubType_mips32el, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips32el
428 {ArchSpec::eCore_mips32r2el, llvm::ELF::EM_MIPS,
429 ArchSpec::eMIPSSubType_mips32r2el, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips32r2el
430 {ArchSpec::eCore_mips32r6el, llvm::ELF::EM_MIPS,
431 ArchSpec::eMIPSSubType_mips32r6el, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips32r6el
432 {ArchSpec::eCore_mips64, llvm::ELF::EM_MIPS, ArchSpec::eMIPSSubType_mips64,
433 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips64
434 {ArchSpec::eCore_mips64r2, llvm::ELF::EM_MIPS,
435 ArchSpec::eMIPSSubType_mips64r2, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips64r2
436 {ArchSpec::eCore_mips64r6, llvm::ELF::EM_MIPS,
437 ArchSpec::eMIPSSubType_mips64r6, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips64r6
438 {ArchSpec::eCore_mips64el, llvm::ELF::EM_MIPS,
439 ArchSpec::eMIPSSubType_mips64el, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips64el
440 {ArchSpec::eCore_mips64r2el, llvm::ELF::EM_MIPS,
441 ArchSpec::eMIPSSubType_mips64r2el, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips64r2el
442 {ArchSpec::eCore_mips64r6el, llvm::ELF::EM_MIPS,
443 ArchSpec::eMIPSSubType_mips64r6el, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips64r6el
444 {ArchSpec::eCore_hexagon_generic, llvm::ELF::EM_HEXAGON,
445 LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // HEXAGON
446 {ArchSpec::eCore_arc, llvm::ELF::EM_ARC_COMPACT2, LLDB_INVALID_CPUTYPE,
447 0xFFFFFFFFu, 0xFFFFFFFFu}, // ARC
450 static const ArchDefinition g_elf_arch_def = {
452 llvm::array_lengthof(g_elf_arch_entries),
457 static const ArchDefinitionEntry g_coff_arch_entries[] = {
458 {ArchSpec::eCore_x86_32_i386, llvm::COFF::IMAGE_FILE_MACHINE_I386,
459 LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // Intel 80x86
460 {ArchSpec::eCore_ppc_generic, llvm::COFF::IMAGE_FILE_MACHINE_POWERPC,
461 LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // PowerPC
462 {ArchSpec::eCore_ppc_generic, llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP,
463 LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // PowerPC (with FPU)
464 {ArchSpec::eCore_arm_generic, llvm::COFF::IMAGE_FILE_MACHINE_ARM,
465 LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // ARM
466 {ArchSpec::eCore_arm_armv7, llvm::COFF::IMAGE_FILE_MACHINE_ARMNT,
467 LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // ARMv7
468 {ArchSpec::eCore_thumb, llvm::COFF::IMAGE_FILE_MACHINE_THUMB,
469 LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // ARMv7
470 {ArchSpec::eCore_x86_64_x86_64, llvm::COFF::IMAGE_FILE_MACHINE_AMD64,
471 LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // AMD64
472 {ArchSpec::eCore_arm_arm64, llvm::COFF::IMAGE_FILE_MACHINE_ARM64,
473 LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu} // ARM64
476 static const ArchDefinition g_coff_arch_def = {
478 llvm::array_lengthof(g_coff_arch_entries),
483 //===----------------------------------------------------------------------===//
484 // Table of all ArchDefinitions
485 static const ArchDefinition *g_arch_definitions[] = {
486 &g_macho_arch_def, &g_elf_arch_def, &g_coff_arch_def};
488 static const size_t k_num_arch_definitions =
489 llvm::array_lengthof(g_arch_definitions);
491 //===----------------------------------------------------------------------===//
492 // Static helper functions.
494 // Get the architecture definition for a given object type.
495 static const ArchDefinition *FindArchDefinition(ArchitectureType arch_type) {
496 for (unsigned int i = 0; i < k_num_arch_definitions; ++i) {
497 const ArchDefinition *def = g_arch_definitions[i];
498 if (def->type == arch_type)
504 // Get an architecture definition by name.
505 static const CoreDefinition *FindCoreDefinition(llvm::StringRef name) {
506 for (unsigned int i = 0; i < llvm::array_lengthof(g_core_definitions); ++i) {
507 if (name.equals_lower(g_core_definitions[i].name))
508 return &g_core_definitions[i];
513 static inline const CoreDefinition *FindCoreDefinition(ArchSpec::Core core) {
514 if (core < llvm::array_lengthof(g_core_definitions))
515 return &g_core_definitions[core];
519 // Get a definition entry by cpu type and subtype.
520 static const ArchDefinitionEntry *
521 FindArchDefinitionEntry(const ArchDefinition *def, uint32_t cpu, uint32_t sub) {
525 const ArchDefinitionEntry *entries = def->entries;
526 for (size_t i = 0; i < def->num_entries; ++i) {
527 if (entries[i].cpu == (cpu & entries[i].cpu_mask))
528 if (entries[i].sub == (sub & entries[i].sub_mask))
534 static const ArchDefinitionEntry *
535 FindArchDefinitionEntry(const ArchDefinition *def, ArchSpec::Core core) {
539 const ArchDefinitionEntry *entries = def->entries;
540 for (size_t i = 0; i < def->num_entries; ++i) {
541 if (entries[i].core == core)
547 //===----------------------------------------------------------------------===//
548 // Constructors and destructors.
550 ArchSpec::ArchSpec() {}
552 ArchSpec::ArchSpec(const char *triple_cstr) {
554 SetTriple(triple_cstr);
557 ArchSpec::ArchSpec(llvm::StringRef triple_str) { SetTriple(triple_str); }
559 ArchSpec::ArchSpec(const llvm::Triple &triple) { SetTriple(triple); }
561 ArchSpec::ArchSpec(ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) {
562 SetArchitecture(arch_type, cpu, subtype);
565 ArchSpec::~ArchSpec() = default;
567 void ArchSpec::Clear() {
568 m_triple = llvm::Triple();
569 m_core = kCore_invalid;
570 m_byte_order = eByteOrderInvalid;
571 m_distribution_id.Clear();
575 //===----------------------------------------------------------------------===//
578 const char *ArchSpec::GetArchitectureName() const {
579 const CoreDefinition *core_def = FindCoreDefinition(m_core);
581 return core_def->name;
585 bool ArchSpec::IsMIPS() const { return GetTriple().isMIPS(); }
587 std::string ArchSpec::GetTargetABI() const {
592 switch (GetFlags() & ArchSpec::eMIPSABI_mask) {
593 case ArchSpec::eMIPSABI_N64:
596 case ArchSpec::eMIPSABI_N32:
599 case ArchSpec::eMIPSABI_O32:
609 void ArchSpec::SetFlags(std::string elf_abi) {
611 uint32_t flag = GetFlags();
613 if (elf_abi == "n64")
614 flag |= ArchSpec::eMIPSABI_N64;
615 else if (elf_abi == "n32")
616 flag |= ArchSpec::eMIPSABI_N32;
617 else if (elf_abi == "o32")
618 flag |= ArchSpec::eMIPSABI_O32;
623 std::string ArchSpec::GetClangTargetCPU() const {
628 case ArchSpec::eCore_mips32:
629 case ArchSpec::eCore_mips32el:
632 case ArchSpec::eCore_mips32r2:
633 case ArchSpec::eCore_mips32r2el:
636 case ArchSpec::eCore_mips32r3:
637 case ArchSpec::eCore_mips32r3el:
640 case ArchSpec::eCore_mips32r5:
641 case ArchSpec::eCore_mips32r5el:
644 case ArchSpec::eCore_mips32r6:
645 case ArchSpec::eCore_mips32r6el:
648 case ArchSpec::eCore_mips64:
649 case ArchSpec::eCore_mips64el:
652 case ArchSpec::eCore_mips64r2:
653 case ArchSpec::eCore_mips64r2el:
656 case ArchSpec::eCore_mips64r3:
657 case ArchSpec::eCore_mips64r3el:
660 case ArchSpec::eCore_mips64r5:
661 case ArchSpec::eCore_mips64r5el:
664 case ArchSpec::eCore_mips64r6:
665 case ArchSpec::eCore_mips64r6el:
675 uint32_t ArchSpec::GetMachOCPUType() const {
676 const CoreDefinition *core_def = FindCoreDefinition(m_core);
678 const ArchDefinitionEntry *arch_def =
679 FindArchDefinitionEntry(&g_macho_arch_def, core_def->core);
681 return arch_def->cpu;
684 return LLDB_INVALID_CPUTYPE;
687 uint32_t ArchSpec::GetMachOCPUSubType() const {
688 const CoreDefinition *core_def = FindCoreDefinition(m_core);
690 const ArchDefinitionEntry *arch_def =
691 FindArchDefinitionEntry(&g_macho_arch_def, core_def->core);
693 return arch_def->sub;
696 return LLDB_INVALID_CPUTYPE;
699 uint32_t ArchSpec::GetDataByteSize() const {
703 uint32_t ArchSpec::GetCodeByteSize() const {
707 llvm::Triple::ArchType ArchSpec::GetMachine() const {
708 const CoreDefinition *core_def = FindCoreDefinition(m_core);
710 return core_def->machine;
712 return llvm::Triple::UnknownArch;
715 ConstString ArchSpec::GetDistributionId() const {
716 return m_distribution_id;
719 void ArchSpec::SetDistributionId(const char *distribution_id) {
720 m_distribution_id.SetCString(distribution_id);
723 uint32_t ArchSpec::GetAddressByteSize() const {
724 const CoreDefinition *core_def = FindCoreDefinition(m_core);
726 if (core_def->machine == llvm::Triple::mips64 ||
727 core_def->machine == llvm::Triple::mips64el) {
728 // For N32/O32 applications Address size is 4 bytes.
729 if (m_flags & (eMIPSABI_N32 | eMIPSABI_O32))
732 return core_def->addr_byte_size;
737 ByteOrder ArchSpec::GetDefaultEndian() const {
738 const CoreDefinition *core_def = FindCoreDefinition(m_core);
740 return core_def->default_byte_order;
741 return eByteOrderInvalid;
744 bool ArchSpec::CharIsSignedByDefault() const {
745 switch (m_triple.getArch()) {
749 case llvm::Triple::aarch64:
750 case llvm::Triple::aarch64_32:
751 case llvm::Triple::aarch64_be:
752 case llvm::Triple::arm:
753 case llvm::Triple::armeb:
754 case llvm::Triple::thumb:
755 case llvm::Triple::thumbeb:
756 return m_triple.isOSDarwin() || m_triple.isOSWindows();
758 case llvm::Triple::ppc:
759 case llvm::Triple::ppc64:
760 return m_triple.isOSDarwin();
762 case llvm::Triple::ppc64le:
763 case llvm::Triple::systemz:
764 case llvm::Triple::xcore:
765 case llvm::Triple::arc:
770 lldb::ByteOrder ArchSpec::GetByteOrder() const {
771 if (m_byte_order == eByteOrderInvalid)
772 return GetDefaultEndian();
776 //===----------------------------------------------------------------------===//
779 bool ArchSpec::SetTriple(const llvm::Triple &triple) {
785 bool lldb_private::ParseMachCPUDashSubtypeTriple(llvm::StringRef triple_str,
787 // Accept "12-10" or "12.10" as cpu type/subtype
788 if (triple_str.empty())
791 size_t pos = triple_str.find_first_of("-.");
792 if (pos == llvm::StringRef::npos)
795 llvm::StringRef cpu_str = triple_str.substr(0, pos);
796 llvm::StringRef remainder = triple_str.substr(pos + 1);
797 if (cpu_str.empty() || remainder.empty())
800 llvm::StringRef sub_str;
801 llvm::StringRef vendor;
803 std::tie(sub_str, remainder) = remainder.split('-');
804 std::tie(vendor, os) = remainder.split('-');
808 if (cpu_str.getAsInteger(10, cpu) || sub_str.getAsInteger(10, sub))
811 if (!arch.SetArchitecture(eArchTypeMachO, cpu, sub))
813 if (!vendor.empty() && !os.empty()) {
814 arch.GetTriple().setVendorName(vendor);
815 arch.GetTriple().setOSName(os);
821 bool ArchSpec::SetTriple(llvm::StringRef triple) {
822 if (triple.empty()) {
827 if (ParseMachCPUDashSubtypeTriple(triple, *this))
830 SetTriple(llvm::Triple(llvm::Triple::normalize(triple)));
834 bool ArchSpec::ContainsOnlyArch(const llvm::Triple &normalized_triple) {
835 return !normalized_triple.getArchName().empty() &&
836 normalized_triple.getOSName().empty() &&
837 normalized_triple.getVendorName().empty() &&
838 normalized_triple.getEnvironmentName().empty();
841 void ArchSpec::MergeFrom(const ArchSpec &other) {
842 if (!TripleVendorWasSpecified() && other.TripleVendorWasSpecified())
843 GetTriple().setVendor(other.GetTriple().getVendor());
844 if (!TripleOSWasSpecified() && other.TripleOSWasSpecified())
845 GetTriple().setOS(other.GetTriple().getOS());
846 if (GetTriple().getArch() == llvm::Triple::UnknownArch) {
847 GetTriple().setArch(other.GetTriple().getArch());
849 // MachO unknown64 isn't really invalid as the debugger can still obtain
850 // information from the binary, e.g. line tables. As such, we don't update
852 if (other.GetCore() != eCore_uknownMach64)
855 if (!TripleEnvironmentWasSpecified() &&
856 other.TripleEnvironmentWasSpecified()) {
857 GetTriple().setEnvironment(other.GetTriple().getEnvironment());
859 // If this and other are both arm ArchSpecs and this ArchSpec is a generic
860 // "some kind of arm" spec but the other ArchSpec is a specific arm core,
861 // adopt the specific arm core.
862 if (GetTriple().getArch() == llvm::Triple::arm &&
863 other.GetTriple().getArch() == llvm::Triple::arm &&
864 IsCompatibleMatch(other) && GetCore() == ArchSpec::eCore_arm_generic &&
865 other.GetCore() != ArchSpec::eCore_arm_generic) {
866 m_core = other.GetCore();
869 if (GetFlags() == 0) {
870 SetFlags(other.GetFlags());
874 bool ArchSpec::SetArchitecture(ArchitectureType arch_type, uint32_t cpu,
875 uint32_t sub, uint32_t os) {
876 m_core = kCore_invalid;
877 bool update_triple = true;
878 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
880 const ArchDefinitionEntry *arch_def_entry =
881 FindArchDefinitionEntry(arch_def, cpu, sub);
882 if (arch_def_entry) {
883 const CoreDefinition *core_def = FindCoreDefinition(arch_def_entry->core);
885 m_core = core_def->core;
886 update_triple = false;
887 // Always use the architecture name because it might be more
888 // descriptive than the architecture enum ("armv7" ->
889 // llvm::Triple::arm).
890 m_triple.setArchName(llvm::StringRef(core_def->name));
891 if (arch_type == eArchTypeMachO) {
892 m_triple.setVendor(llvm::Triple::Apple);
894 // Don't set the OS. It could be simulator, macosx, ios, watchos,
895 // tvos, bridgeos. We could get close with the cpu type - but we
896 // can't get it right all of the time. Better to leave this unset
897 // so other sections of code will set it when they have more
898 // information. NB: don't call m_triple.setOS
899 // (llvm::Triple::UnknownOS). That sets the OSName to "unknown" and
900 // the ArchSpec::TripleVendorWasSpecified() method says that any
901 // OSName setting means it was specified.
902 } else if (arch_type == eArchTypeELF) {
904 case llvm::ELF::ELFOSABI_AIX:
905 m_triple.setOS(llvm::Triple::OSType::AIX);
907 case llvm::ELF::ELFOSABI_FREEBSD:
908 m_triple.setOS(llvm::Triple::OSType::FreeBSD);
910 case llvm::ELF::ELFOSABI_GNU:
911 m_triple.setOS(llvm::Triple::OSType::Linux);
913 case llvm::ELF::ELFOSABI_NETBSD:
914 m_triple.setOS(llvm::Triple::OSType::NetBSD);
916 case llvm::ELF::ELFOSABI_OPENBSD:
917 m_triple.setOS(llvm::Triple::OSType::OpenBSD);
919 case llvm::ELF::ELFOSABI_SOLARIS:
920 m_triple.setOS(llvm::Triple::OSType::Solaris);
923 } else if (arch_type == eArchTypeCOFF && os == llvm::Triple::Win32) {
924 m_triple.setVendor(llvm::Triple::PC);
925 m_triple.setOS(llvm::Triple::Win32);
927 m_triple.setVendor(llvm::Triple::UnknownVendor);
928 m_triple.setOS(llvm::Triple::UnknownOS);
930 // Fall back onto setting the machine type if the arch by name
932 if (m_triple.getArch() == llvm::Triple::UnknownArch)
933 m_triple.setArch(core_def->machine);
936 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET | LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_PLATFORM));
938 "Unable to find a core definition for cpu 0x%" PRIx32
943 CoreUpdated(update_triple);
947 uint32_t ArchSpec::GetMinimumOpcodeByteSize() const {
948 const CoreDefinition *core_def = FindCoreDefinition(m_core);
950 return core_def->min_opcode_byte_size;
954 uint32_t ArchSpec::GetMaximumOpcodeByteSize() const {
955 const CoreDefinition *core_def = FindCoreDefinition(m_core);
957 return core_def->max_opcode_byte_size;
961 bool ArchSpec::IsExactMatch(const ArchSpec &rhs) const {
962 return IsEqualTo(rhs, true);
965 bool ArchSpec::IsCompatibleMatch(const ArchSpec &rhs) const {
966 return IsEqualTo(rhs, false);
969 static bool IsCompatibleEnvironment(llvm::Triple::EnvironmentType lhs,
970 llvm::Triple::EnvironmentType rhs) {
974 // If any of the environment is unknown then they are compatible
975 if (lhs == llvm::Triple::UnknownEnvironment ||
976 rhs == llvm::Triple::UnknownEnvironment)
979 // If one of the environment is Android and the other one is EABI then they
980 // are considered to be compatible. This is required as a workaround for
981 // shared libraries compiled for Android without the NOTE section indicating
982 // that they are using the Android ABI.
983 if ((lhs == llvm::Triple::Android && rhs == llvm::Triple::EABI) ||
984 (rhs == llvm::Triple::Android && lhs == llvm::Triple::EABI) ||
985 (lhs == llvm::Triple::GNUEABI && rhs == llvm::Triple::EABI) ||
986 (rhs == llvm::Triple::GNUEABI && lhs == llvm::Triple::EABI) ||
987 (lhs == llvm::Triple::GNUEABIHF && rhs == llvm::Triple::EABIHF) ||
988 (rhs == llvm::Triple::GNUEABIHF && lhs == llvm::Triple::EABIHF))
994 bool ArchSpec::IsEqualTo(const ArchSpec &rhs, bool exact_match) const {
995 // explicitly ignoring m_distribution_id in this method.
997 if (GetByteOrder() != rhs.GetByteOrder())
1000 const ArchSpec::Core lhs_core = GetCore();
1001 const ArchSpec::Core rhs_core = rhs.GetCore();
1003 const bool core_match = cores_match(lhs_core, rhs_core, true, exact_match);
1006 const llvm::Triple &lhs_triple = GetTriple();
1007 const llvm::Triple &rhs_triple = rhs.GetTriple();
1009 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
1010 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
1011 if (lhs_triple_vendor != rhs_triple_vendor) {
1012 const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
1013 const bool lhs_vendor_specified = TripleVendorWasSpecified();
1014 // Both architectures had the vendor specified, so if they aren't equal
1015 // then we return false
1016 if (rhs_vendor_specified && lhs_vendor_specified)
1019 // Only fail if both vendor types are not unknown
1020 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
1021 rhs_triple_vendor != llvm::Triple::UnknownVendor)
1025 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
1026 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
1027 if (lhs_triple_os != rhs_triple_os) {
1028 const bool rhs_os_specified = rhs.TripleOSWasSpecified();
1029 const bool lhs_os_specified = TripleOSWasSpecified();
1030 // Both architectures had the OS specified, so if they aren't equal then
1032 if (rhs_os_specified && lhs_os_specified)
1035 // Only fail if both os types are not unknown
1036 if (lhs_triple_os != llvm::Triple::UnknownOS &&
1037 rhs_triple_os != llvm::Triple::UnknownOS)
1041 const llvm::Triple::EnvironmentType lhs_triple_env =
1042 lhs_triple.getEnvironment();
1043 const llvm::Triple::EnvironmentType rhs_triple_env =
1044 rhs_triple.getEnvironment();
1046 return IsCompatibleEnvironment(lhs_triple_env, rhs_triple_env);
1051 void ArchSpec::UpdateCore() {
1052 llvm::StringRef arch_name(m_triple.getArchName());
1053 const CoreDefinition *core_def = FindCoreDefinition(arch_name);
1055 m_core = core_def->core;
1056 // Set the byte order to the default byte order for an architecture. This
1057 // can be modified if needed for cases when cores handle both big and
1059 m_byte_order = core_def->default_byte_order;
1065 //===----------------------------------------------------------------------===//
1068 void ArchSpec::CoreUpdated(bool update_triple) {
1069 const CoreDefinition *core_def = FindCoreDefinition(m_core);
1072 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
1073 m_byte_order = core_def->default_byte_order;
1076 m_triple = llvm::Triple();
1077 m_byte_order = eByteOrderInvalid;
1081 //===----------------------------------------------------------------------===//
1084 static bool cores_match(const ArchSpec::Core core1, const ArchSpec::Core core2,
1085 bool try_inverse, bool enforce_exact_match) {
1090 case ArchSpec::kCore_any:
1093 case ArchSpec::eCore_arm_generic:
1094 if (enforce_exact_match)
1097 case ArchSpec::kCore_arm_any:
1098 if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
1100 if (core2 >= ArchSpec::kCore_thumb_first &&
1101 core2 <= ArchSpec::kCore_thumb_last)
1103 if (core2 == ArchSpec::kCore_arm_any)
1107 case ArchSpec::kCore_x86_32_any:
1108 if ((core2 >= ArchSpec::kCore_x86_32_first &&
1109 core2 <= ArchSpec::kCore_x86_32_last) ||
1110 (core2 == ArchSpec::kCore_x86_32_any))
1114 case ArchSpec::kCore_x86_64_any:
1115 if ((core2 >= ArchSpec::kCore_x86_64_first &&
1116 core2 <= ArchSpec::kCore_x86_64_last) ||
1117 (core2 == ArchSpec::kCore_x86_64_any))
1121 case ArchSpec::kCore_ppc_any:
1122 if ((core2 >= ArchSpec::kCore_ppc_first &&
1123 core2 <= ArchSpec::kCore_ppc_last) ||
1124 (core2 == ArchSpec::kCore_ppc_any))
1128 case ArchSpec::kCore_ppc64_any:
1129 if ((core2 >= ArchSpec::kCore_ppc64_first &&
1130 core2 <= ArchSpec::kCore_ppc64_last) ||
1131 (core2 == ArchSpec::kCore_ppc64_any))
1135 case ArchSpec::eCore_arm_armv6m:
1136 if (!enforce_exact_match) {
1137 if (core2 == ArchSpec::eCore_arm_generic)
1139 try_inverse = false;
1140 if (core2 == ArchSpec::eCore_arm_armv7)
1142 if (core2 == ArchSpec::eCore_arm_armv6m)
1147 case ArchSpec::kCore_hexagon_any:
1148 if ((core2 >= ArchSpec::kCore_hexagon_first &&
1149 core2 <= ArchSpec::kCore_hexagon_last) ||
1150 (core2 == ArchSpec::kCore_hexagon_any))
1154 // v. https://en.wikipedia.org/wiki/ARM_Cortex-M#Silicon_customization
1155 // Cortex-M0 - ARMv6-M - armv6m Cortex-M3 - ARMv7-M - armv7m Cortex-M4 -
1156 // ARMv7E-M - armv7em
1157 case ArchSpec::eCore_arm_armv7em:
1158 if (!enforce_exact_match) {
1159 if (core2 == ArchSpec::eCore_arm_generic)
1161 if (core2 == ArchSpec::eCore_arm_armv7m)
1163 if (core2 == ArchSpec::eCore_arm_armv6m)
1165 if (core2 == ArchSpec::eCore_arm_armv7)
1171 // v. https://en.wikipedia.org/wiki/ARM_Cortex-M#Silicon_customization
1172 // Cortex-M0 - ARMv6-M - armv6m Cortex-M3 - ARMv7-M - armv7m Cortex-M4 -
1173 // ARMv7E-M - armv7em
1174 case ArchSpec::eCore_arm_armv7m:
1175 if (!enforce_exact_match) {
1176 if (core2 == ArchSpec::eCore_arm_generic)
1178 if (core2 == ArchSpec::eCore_arm_armv6m)
1180 if (core2 == ArchSpec::eCore_arm_armv7)
1182 if (core2 == ArchSpec::eCore_arm_armv7em)
1188 case ArchSpec::eCore_arm_armv7f:
1189 case ArchSpec::eCore_arm_armv7k:
1190 case ArchSpec::eCore_arm_armv7s:
1191 case ArchSpec::eCore_arm_armv7l:
1192 case ArchSpec::eCore_arm_armv8l:
1193 if (!enforce_exact_match) {
1194 if (core2 == ArchSpec::eCore_arm_generic)
1196 if (core2 == ArchSpec::eCore_arm_armv7)
1198 try_inverse = false;
1202 case ArchSpec::eCore_x86_64_x86_64h:
1203 if (!enforce_exact_match) {
1204 try_inverse = false;
1205 if (core2 == ArchSpec::eCore_x86_64_x86_64)
1210 case ArchSpec::eCore_arm_armv8:
1211 if (!enforce_exact_match) {
1212 if (core2 == ArchSpec::eCore_arm_arm64)
1214 if (core2 == ArchSpec::eCore_arm_aarch64)
1216 try_inverse = false;
1220 case ArchSpec::eCore_arm_aarch64:
1221 if (!enforce_exact_match) {
1222 if (core2 == ArchSpec::eCore_arm_arm64)
1224 if (core2 == ArchSpec::eCore_arm_armv8)
1226 try_inverse = false;
1230 case ArchSpec::eCore_arm_arm64:
1231 if (!enforce_exact_match) {
1232 if (core2 == ArchSpec::eCore_arm_aarch64)
1234 if (core2 == ArchSpec::eCore_arm_armv8)
1236 try_inverse = false;
1240 case ArchSpec::eCore_arm_arm64_32:
1241 if (!enforce_exact_match) {
1242 if (core2 == ArchSpec::eCore_arm_generic)
1244 try_inverse = false;
1248 case ArchSpec::eCore_mips32:
1249 if (!enforce_exact_match) {
1250 if (core2 >= ArchSpec::kCore_mips32_first &&
1251 core2 <= ArchSpec::kCore_mips32_last)
1253 try_inverse = false;
1257 case ArchSpec::eCore_mips32el:
1258 if (!enforce_exact_match) {
1259 if (core2 >= ArchSpec::kCore_mips32el_first &&
1260 core2 <= ArchSpec::kCore_mips32el_last)
1266 case ArchSpec::eCore_mips64:
1267 if (!enforce_exact_match) {
1268 if (core2 >= ArchSpec::kCore_mips32_first &&
1269 core2 <= ArchSpec::kCore_mips32_last)
1271 if (core2 >= ArchSpec::kCore_mips64_first &&
1272 core2 <= ArchSpec::kCore_mips64_last)
1274 try_inverse = false;
1278 case ArchSpec::eCore_mips64el:
1279 if (!enforce_exact_match) {
1280 if (core2 >= ArchSpec::kCore_mips32el_first &&
1281 core2 <= ArchSpec::kCore_mips32el_last)
1283 if (core2 >= ArchSpec::kCore_mips64el_first &&
1284 core2 <= ArchSpec::kCore_mips64el_last)
1286 try_inverse = false;
1290 case ArchSpec::eCore_mips64r2:
1291 case ArchSpec::eCore_mips64r3:
1292 case ArchSpec::eCore_mips64r5:
1293 if (!enforce_exact_match) {
1294 if (core2 >= ArchSpec::kCore_mips32_first && core2 <= (core1 - 10))
1296 if (core2 >= ArchSpec::kCore_mips64_first && core2 <= (core1 - 1))
1298 try_inverse = false;
1302 case ArchSpec::eCore_mips64r2el:
1303 case ArchSpec::eCore_mips64r3el:
1304 case ArchSpec::eCore_mips64r5el:
1305 if (!enforce_exact_match) {
1306 if (core2 >= ArchSpec::kCore_mips32el_first && core2 <= (core1 - 10))
1308 if (core2 >= ArchSpec::kCore_mips64el_first && core2 <= (core1 - 1))
1310 try_inverse = false;
1314 case ArchSpec::eCore_mips32r2:
1315 case ArchSpec::eCore_mips32r3:
1316 case ArchSpec::eCore_mips32r5:
1317 if (!enforce_exact_match) {
1318 if (core2 >= ArchSpec::kCore_mips32_first && core2 <= core1)
1323 case ArchSpec::eCore_mips32r2el:
1324 case ArchSpec::eCore_mips32r3el:
1325 case ArchSpec::eCore_mips32r5el:
1326 if (!enforce_exact_match) {
1327 if (core2 >= ArchSpec::kCore_mips32el_first && core2 <= core1)
1332 case ArchSpec::eCore_mips32r6:
1333 if (!enforce_exact_match) {
1334 if (core2 == ArchSpec::eCore_mips32 || core2 == ArchSpec::eCore_mips32r6)
1339 case ArchSpec::eCore_mips32r6el:
1340 if (!enforce_exact_match) {
1341 if (core2 == ArchSpec::eCore_mips32el ||
1342 core2 == ArchSpec::eCore_mips32r6el)
1347 case ArchSpec::eCore_mips64r6:
1348 if (!enforce_exact_match) {
1349 if (core2 == ArchSpec::eCore_mips32 || core2 == ArchSpec::eCore_mips32r6)
1351 if (core2 == ArchSpec::eCore_mips64 || core2 == ArchSpec::eCore_mips64r6)
1356 case ArchSpec::eCore_mips64r6el:
1357 if (!enforce_exact_match) {
1358 if (core2 == ArchSpec::eCore_mips32el ||
1359 core2 == ArchSpec::eCore_mips32r6el)
1361 if (core2 == ArchSpec::eCore_mips64el ||
1362 core2 == ArchSpec::eCore_mips64r6el)
1371 return cores_match(core2, core1, false, enforce_exact_match);
1375 bool lldb_private::operator<(const ArchSpec &lhs, const ArchSpec &rhs) {
1376 const ArchSpec::Core lhs_core = lhs.GetCore();
1377 const ArchSpec::Core rhs_core = rhs.GetCore();
1378 return lhs_core < rhs_core;
1382 bool lldb_private::operator==(const ArchSpec &lhs, const ArchSpec &rhs) {
1383 return lhs.GetCore() == rhs.GetCore();
1386 bool ArchSpec::IsFullySpecifiedTriple() const {
1387 const auto &user_specified_triple = GetTriple();
1389 bool user_triple_fully_specified = false;
1391 if ((user_specified_triple.getOS() != llvm::Triple::UnknownOS) ||
1392 TripleOSWasSpecified()) {
1393 if ((user_specified_triple.getVendor() != llvm::Triple::UnknownVendor) ||
1394 TripleVendorWasSpecified()) {
1395 const unsigned unspecified = 0;
1396 if (user_specified_triple.getOSMajorVersion() != unspecified) {
1397 user_triple_fully_specified = true;
1402 return user_triple_fully_specified;
1405 void ArchSpec::PiecewiseTripleCompare(
1406 const ArchSpec &other, bool &arch_different, bool &vendor_different,
1407 bool &os_different, bool &os_version_different, bool &env_different) const {
1408 const llvm::Triple &me(GetTriple());
1409 const llvm::Triple &them(other.GetTriple());
1411 arch_different = (me.getArch() != them.getArch());
1413 vendor_different = (me.getVendor() != them.getVendor());
1415 os_different = (me.getOS() != them.getOS());
1417 os_version_different = (me.getOSMajorVersion() != them.getOSMajorVersion());
1419 env_different = (me.getEnvironment() != them.getEnvironment());
1422 bool ArchSpec::IsAlwaysThumbInstructions() const {
1424 if (GetTriple().getArch() == llvm::Triple::arm ||
1425 GetTriple().getArch() == llvm::Triple::thumb) {
1426 // v. https://en.wikipedia.org/wiki/ARM_Cortex-M
1428 // Cortex-M0 through Cortex-M7 are ARM processor cores which can only
1429 // execute thumb instructions. We map the cores to arch names like this:
1431 // Cortex-M0, Cortex-M0+, Cortex-M1: armv6m Cortex-M3: armv7m Cortex-M4,
1432 // Cortex-M7: armv7em
1434 if (GetCore() == ArchSpec::Core::eCore_arm_armv7m ||
1435 GetCore() == ArchSpec::Core::eCore_arm_armv7em ||
1436 GetCore() == ArchSpec::Core::eCore_arm_armv6m ||
1437 GetCore() == ArchSpec::Core::eCore_thumbv7m ||
1438 GetCore() == ArchSpec::Core::eCore_thumbv7em ||
1439 GetCore() == ArchSpec::Core::eCore_thumbv6m) {
1442 // Windows on ARM is always thumb.
1443 if (GetTriple().isOSWindows())
1449 void ArchSpec::DumpTriple(llvm::raw_ostream &s) const {
1450 const llvm::Triple &triple = GetTriple();
1451 llvm::StringRef arch_str = triple.getArchName();
1452 llvm::StringRef vendor_str = triple.getVendorName();
1453 llvm::StringRef os_str = triple.getOSName();
1454 llvm::StringRef environ_str = triple.getEnvironmentName();
1456 s << llvm::formatv("{0}-{1}-{2}", arch_str.empty() ? "*" : arch_str,
1457 vendor_str.empty() ? "*" : vendor_str,
1458 os_str.empty() ? "*" : os_str);
1460 if (!environ_str.empty())
1461 s << "-" << environ_str;