1 //===-- ArchSpec.cpp --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/ArchSpec.h"
17 #include "llvm/Support/ELF.h"
18 #include "llvm/Support/Host.h"
19 #include "llvm/Support/MachO.h"
20 #include "lldb/Core/RegularExpression.h"
21 #include "lldb/Host/Endian.h"
22 #include "lldb/Host/Host.h"
23 #include "lldb/Target/Platform.h"
26 using namespace lldb_private;
28 #define ARCH_SPEC_SEPARATOR_CHAR '-'
31 static bool cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match);
33 namespace lldb_private {
37 ByteOrder default_byte_order;
38 uint32_t addr_byte_size;
39 uint32_t min_opcode_byte_size;
40 uint32_t max_opcode_byte_size;
41 llvm::Triple::ArchType machine;
48 // This core information can be looked using the ArchSpec::Core as the index
49 static const CoreDefinition g_core_definitions[ArchSpec::kNumCores] =
51 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_generic , "arm" },
52 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4 , "armv4" },
53 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4t , "armv4t" },
54 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5 , "armv5" },
55 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5e , "armv5e" },
56 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5t , "armv5t" },
57 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6 , "armv6" },
58 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7 , "armv7" },
59 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7f , "armv7f" },
60 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7s , "armv7s" },
61 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7k , "armv7k" },
62 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7m , "armv7m" },
63 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7em , "armv7em" },
64 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_xscale , "xscale" },
65 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumb , "thumb" },
66 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv4t , "thumbv4t" },
67 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5 , "thumbv5" },
68 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5e , "thumbv5e" },
69 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv6 , "thumbv6" },
70 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7 , "thumbv7" },
71 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7f , "thumbv7f" },
72 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7s , "thumbv7s" },
73 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7k , "thumbv7k" },
74 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7m , "thumbv7m" },
75 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7em , "thumbv7em" },
78 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "ppc" },
79 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
80 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
81 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
82 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
83 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
84 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
85 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
86 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
87 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
88 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
89 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
90 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
92 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "ppc64" },
93 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
95 { eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
96 { eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
98 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
99 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
100 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
102 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" },
103 { eByteOrderLittle, 4, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach32 , "unknown-mach-32" },
104 { eByteOrderLittle, 8, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach64 , "unknown-mach-64" }
107 struct ArchDefinitionEntry
116 struct ArchDefinition
118 ArchitectureType type;
120 const ArchDefinitionEntry *entries;
126 ArchSpec::AutoComplete (const char *name, StringList &matches)
131 for (i = 0; i < ArchSpec::kNumCores; ++i)
133 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
134 matches.AppendString (g_core_definitions[i].name);
139 for (i = 0; i < ArchSpec::kNumCores; ++i)
140 matches.AppendString (g_core_definitions[i].name);
142 return matches.GetSize();
147 #define CPU_ANY (UINT32_MAX)
149 //===----------------------------------------------------------------------===//
150 // A table that gets searched linearly for matches. This table is used to
151 // convert cpu type and subtypes to architecture names, and to convert
152 // architecture names to cpu types and subtypes. The ordering is important and
153 // allows the precedence to be set when the table is built.
154 #define SUBTYPE_MASK 0x00FFFFFFu
155 static const ArchDefinitionEntry g_macho_arch_entries[] =
157 { ArchSpec::eCore_arm_generic , llvm::MachO::CPUTypeARM , CPU_ANY, UINT32_MAX , UINT32_MAX },
158 { ArchSpec::eCore_arm_generic , llvm::MachO::CPUTypeARM , 0 , UINT32_MAX , SUBTYPE_MASK },
159 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPUTypeARM , 5 , UINT32_MAX , SUBTYPE_MASK },
160 { ArchSpec::eCore_arm_armv4t , llvm::MachO::CPUTypeARM , 5 , UINT32_MAX , SUBTYPE_MASK },
161 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPUTypeARM , 6 , UINT32_MAX , SUBTYPE_MASK },
162 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPUTypeARM , 7 , UINT32_MAX , SUBTYPE_MASK },
163 { ArchSpec::eCore_arm_armv5e , llvm::MachO::CPUTypeARM , 7 , UINT32_MAX , SUBTYPE_MASK },
164 { ArchSpec::eCore_arm_armv5t , llvm::MachO::CPUTypeARM , 7 , UINT32_MAX , SUBTYPE_MASK },
165 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPUTypeARM , 8 , UINT32_MAX , SUBTYPE_MASK },
166 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPUTypeARM , 9 , UINT32_MAX , SUBTYPE_MASK },
167 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPUTypeARM , 10 , UINT32_MAX , SUBTYPE_MASK },
168 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPUTypeARM , 11 , UINT32_MAX , SUBTYPE_MASK },
169 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPUTypeARM , 12 , UINT32_MAX , SUBTYPE_MASK },
170 { ArchSpec::eCore_arm_armv7m , llvm::MachO::CPUTypeARM , 15 , UINT32_MAX , SUBTYPE_MASK },
171 { ArchSpec::eCore_arm_armv7em , llvm::MachO::CPUTypeARM , 16 , UINT32_MAX , SUBTYPE_MASK },
172 { ArchSpec::eCore_thumb , llvm::MachO::CPUTypeARM , 0 , UINT32_MAX , SUBTYPE_MASK },
173 { ArchSpec::eCore_thumbv4t , llvm::MachO::CPUTypeARM , 5 , UINT32_MAX , SUBTYPE_MASK },
174 { ArchSpec::eCore_thumbv5 , llvm::MachO::CPUTypeARM , 7 , UINT32_MAX , SUBTYPE_MASK },
175 { ArchSpec::eCore_thumbv5e , llvm::MachO::CPUTypeARM , 7 , UINT32_MAX , SUBTYPE_MASK },
176 { ArchSpec::eCore_thumbv6 , llvm::MachO::CPUTypeARM , 6 , UINT32_MAX , SUBTYPE_MASK },
177 { ArchSpec::eCore_thumbv7 , llvm::MachO::CPUTypeARM , 9 , UINT32_MAX , SUBTYPE_MASK },
178 { ArchSpec::eCore_thumbv7f , llvm::MachO::CPUTypeARM , 10 , UINT32_MAX , SUBTYPE_MASK },
179 { ArchSpec::eCore_thumbv7s , llvm::MachO::CPUTypeARM , 11 , UINT32_MAX , SUBTYPE_MASK },
180 { ArchSpec::eCore_thumbv7k , llvm::MachO::CPUTypeARM , 12 , UINT32_MAX , SUBTYPE_MASK },
181 { ArchSpec::eCore_thumbv7m , llvm::MachO::CPUTypeARM , 15 , UINT32_MAX , SUBTYPE_MASK },
182 { ArchSpec::eCore_thumbv7em , llvm::MachO::CPUTypeARM , 16 , UINT32_MAX , SUBTYPE_MASK },
183 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPUTypePowerPC , CPU_ANY, UINT32_MAX , UINT32_MAX },
184 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPUTypePowerPC , 0 , UINT32_MAX , SUBTYPE_MASK },
185 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPUTypePowerPC , 1 , UINT32_MAX , SUBTYPE_MASK },
186 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPUTypePowerPC , 2 , UINT32_MAX , SUBTYPE_MASK },
187 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPUTypePowerPC , 3 , UINT32_MAX , SUBTYPE_MASK },
188 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPUTypePowerPC , 4 , UINT32_MAX , SUBTYPE_MASK },
189 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPUTypePowerPC , 5 , UINT32_MAX , SUBTYPE_MASK },
190 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPUTypePowerPC , 6 , UINT32_MAX , SUBTYPE_MASK },
191 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPUTypePowerPC , 7 , UINT32_MAX , SUBTYPE_MASK },
192 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPUTypePowerPC , 8 , UINT32_MAX , SUBTYPE_MASK },
193 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPUTypePowerPC , 9 , UINT32_MAX , SUBTYPE_MASK },
194 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPUTypePowerPC , 10 , UINT32_MAX , SUBTYPE_MASK },
195 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPUTypePowerPC , 11 , UINT32_MAX , SUBTYPE_MASK },
196 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPUTypePowerPC , 100 , UINT32_MAX , SUBTYPE_MASK },
197 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPUTypePowerPC64 , 0 , UINT32_MAX , SUBTYPE_MASK },
198 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPUTypePowerPC64 , 100 , UINT32_MAX , SUBTYPE_MASK },
199 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPUTypeI386 , 3 , UINT32_MAX , SUBTYPE_MASK },
200 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPUTypeI386 , 4 , UINT32_MAX , SUBTYPE_MASK },
201 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPUTypeI386 , 0x84 , UINT32_MAX , SUBTYPE_MASK },
202 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPUTypeI386 , CPU_ANY, UINT32_MAX , UINT32_MAX },
203 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , 3 , UINT32_MAX , SUBTYPE_MASK },
204 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , 4 , UINT32_MAX , SUBTYPE_MASK },
205 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , CPU_ANY, UINT32_MAX , UINT32_MAX },
206 // Catch any unknown mach architectures so we can always use the object and symbol mach-o files
207 { ArchSpec::eCore_uknownMach32 , 0 , 0 , 0xFF000000u, 0x00000000u },
208 { ArchSpec::eCore_uknownMach64 , llvm::MachO::CPUArchABI64 , 0 , 0xFF000000u, 0x00000000u }
210 static const ArchDefinition g_macho_arch_def = {
212 sizeof(g_macho_arch_entries)/sizeof(g_macho_arch_entries[0]),
213 g_macho_arch_entries,
217 //===----------------------------------------------------------------------===//
218 // A table that gets searched linearly for matches. This table is used to
219 // convert cpu type and subtypes to architecture names, and to convert
220 // architecture names to cpu types and subtypes. The ordering is important and
221 // allows the precedence to be set when the table is built.
222 static const ArchDefinitionEntry g_elf_arch_entries[] =
224 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Sparc
225 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
226 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 486 (deprecated)
227 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
228 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC64
229 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
230 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // SPARC V9
231 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // AMD64
234 static const ArchDefinition g_elf_arch_def = {
236 sizeof(g_elf_arch_entries)/sizeof(g_elf_arch_entries[0]),
241 //===----------------------------------------------------------------------===//
242 // Table of all ArchDefinitions
243 static const ArchDefinition *g_arch_definitions[] = {
248 static const size_t k_num_arch_definitions =
249 sizeof(g_arch_definitions) / sizeof(g_arch_definitions[0]);
251 //===----------------------------------------------------------------------===//
252 // Static helper functions.
255 // Get the architecture definition for a given object type.
256 static const ArchDefinition *
257 FindArchDefinition (ArchitectureType arch_type)
259 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
261 const ArchDefinition *def = g_arch_definitions[i];
262 if (def->type == arch_type)
268 // Get an architecture definition by name.
269 static const CoreDefinition *
270 FindCoreDefinition (llvm::StringRef name)
272 for (unsigned int i = 0; i < ArchSpec::kNumCores; ++i)
274 if (name.equals_lower(g_core_definitions[i].name))
275 return &g_core_definitions[i];
280 static inline const CoreDefinition *
281 FindCoreDefinition (ArchSpec::Core core)
283 if (core >= 0 && core < ArchSpec::kNumCores)
284 return &g_core_definitions[core];
288 // Get a definition entry by cpu type and subtype.
289 static const ArchDefinitionEntry *
290 FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
295 const ArchDefinitionEntry *entries = def->entries;
296 for (size_t i = 0; i < def->num_entries; ++i)
298 if (entries[i].cpu == (cpu & entries[i].cpu_mask))
299 if (entries[i].sub == (sub & entries[i].sub_mask))
305 static const ArchDefinitionEntry *
306 FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
311 const ArchDefinitionEntry *entries = def->entries;
312 for (size_t i = 0; i < def->num_entries; ++i)
314 if (entries[i].core == core)
320 //===----------------------------------------------------------------------===//
321 // Constructors and destructors.
323 ArchSpec::ArchSpec() :
325 m_core (kCore_invalid),
326 m_byte_order (eByteOrderInvalid)
330 ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
332 m_core (kCore_invalid),
333 m_byte_order (eByteOrderInvalid)
336 SetTriple(triple_cstr, platform);
340 ArchSpec::ArchSpec (const char *triple_cstr) :
342 m_core (kCore_invalid),
343 m_byte_order (eByteOrderInvalid)
346 SetTriple(triple_cstr);
349 ArchSpec::ArchSpec(const llvm::Triple &triple) :
351 m_core (kCore_invalid),
352 m_byte_order (eByteOrderInvalid)
357 ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
359 m_core (kCore_invalid),
360 m_byte_order (eByteOrderInvalid)
362 SetArchitecture (arch_type, cpu, subtype);
365 ArchSpec::~ArchSpec()
369 //===----------------------------------------------------------------------===//
370 // Assignment and initialization.
373 ArchSpec::operator= (const ArchSpec& rhs)
377 m_triple = rhs.m_triple;
379 m_byte_order = rhs.m_byte_order;
387 m_triple = llvm::Triple();
388 m_core = kCore_invalid;
389 m_byte_order = eByteOrderInvalid;
392 //===----------------------------------------------------------------------===//
397 ArchSpec::GetArchitectureName () const
399 const CoreDefinition *core_def = FindCoreDefinition (m_core);
401 return core_def->name;
406 ArchSpec::GetMachOCPUType () const
408 const CoreDefinition *core_def = FindCoreDefinition (m_core);
411 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
414 return arch_def->cpu;
417 return LLDB_INVALID_CPUTYPE;
421 ArchSpec::GetMachOCPUSubType () const
423 const CoreDefinition *core_def = FindCoreDefinition (m_core);
426 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
429 return arch_def->sub;
432 return LLDB_INVALID_CPUTYPE;
435 llvm::Triple::ArchType
436 ArchSpec::GetMachine () const
438 const CoreDefinition *core_def = FindCoreDefinition (m_core);
440 return core_def->machine;
442 return llvm::Triple::UnknownArch;
446 ArchSpec::GetAddressByteSize() const
448 const CoreDefinition *core_def = FindCoreDefinition (m_core);
450 return core_def->addr_byte_size;
455 ArchSpec::GetDefaultEndian () const
457 const CoreDefinition *core_def = FindCoreDefinition (m_core);
459 return core_def->default_byte_order;
460 return eByteOrderInvalid;
464 ArchSpec::GetByteOrder () const
466 if (m_byte_order == eByteOrderInvalid)
467 return GetDefaultEndian();
471 //===----------------------------------------------------------------------===//
475 ArchSpec::SetTriple (const llvm::Triple &triple)
479 llvm::StringRef arch_name (m_triple.getArchName());
480 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
483 m_core = core_def->core;
484 // Set the byte order to the default byte order for an architecture.
485 // This can be modified if needed for cases when cores handle both
486 // big and little endian
487 m_byte_order = core_def->default_byte_order;
499 ParseMachCPUDashSubtypeTriple (const char *triple_cstr, ArchSpec &arch)
501 // Accept "12-10" or "12.10" as cpu type/subtype
502 if (isdigit(triple_cstr[0]))
506 uint32_t cpu = (uint32_t)::strtoul (triple_cstr, &end, 0);
507 if (errno == 0 && cpu != 0 && end && ((*end == '-') || (*end == '.')))
510 uint32_t sub = (uint32_t)::strtoul (end + 1, &end, 0);
511 if (errno == 0 && end && ((*end == '-') || (*end == '.') || (*end == '\0')))
513 if (arch.SetArchitecture (eArchTypeMachO, cpu, sub))
517 llvm::StringRef vendor_os (end + 1);
518 size_t dash_pos = vendor_os.find('-');
519 if (dash_pos != llvm::StringRef::npos)
521 llvm::StringRef vendor_str(vendor_os.substr(0, dash_pos));
522 arch.GetTriple().setVendorName(vendor_str);
523 const size_t vendor_start_pos = dash_pos+1;
524 dash_pos = vendor_os.find('-', vendor_start_pos);
525 if (dash_pos == llvm::StringRef::npos)
527 if (vendor_start_pos < vendor_os.size())
528 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos));
532 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos, dash_pos - vendor_start_pos));
544 ArchSpec::SetTriple (const char *triple_cstr)
546 if (triple_cstr && triple_cstr[0])
548 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
551 llvm::StringRef triple_stref (triple_cstr);
552 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
554 // Special case for the current host default architectures...
555 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
556 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
557 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
558 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
559 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
560 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
564 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
565 triple_stref = normalized_triple_sstr;
566 SetTriple (llvm::Triple (triple_stref));
575 ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
577 if (triple_cstr && triple_cstr[0])
579 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
582 llvm::StringRef triple_stref (triple_cstr);
583 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
585 // Special case for the current host default architectures...
586 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
587 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
588 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
589 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
590 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
591 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
595 ArchSpec raw_arch (triple_cstr);
597 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
598 triple_stref = normalized_triple_sstr;
599 llvm::Triple normalized_triple (triple_stref);
601 const bool os_specified = normalized_triple.getOSName().size() > 0;
602 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
603 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
605 // If we got an arch only, then default the vendor, os, environment
606 // to match the platform if one is supplied
607 if (!(os_specified || vendor_specified || env_specified))
611 // If we were given a platform, use the platform's system
612 // architecture. If this is not available (might not be
613 // connected) use the first supported architecture.
614 ArchSpec compatible_arch;
615 if (platform->IsCompatibleArchitecture (raw_arch, false, &compatible_arch))
617 if (compatible_arch.IsValid())
619 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
620 if (!vendor_specified)
621 normalized_triple.setVendor(compatible_triple.getVendor());
623 normalized_triple.setOS(compatible_triple.getOS());
624 if (!env_specified && compatible_triple.getEnvironmentName().size())
625 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
636 // No platform specified, fall back to the host system for
637 // the default vendor, os, and environment.
638 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
639 if (!vendor_specified)
640 normalized_triple.setVendor(host_triple.getVendor());
641 if (!vendor_specified)
642 normalized_triple.setOS(host_triple.getOS());
643 if (!env_specified && host_triple.getEnvironmentName().size())
644 normalized_triple.setEnvironment(host_triple.getEnvironment());
647 SetTriple (normalized_triple);
656 ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
658 m_core = kCore_invalid;
659 bool update_triple = true;
660 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
663 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
666 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
669 m_core = core_def->core;
670 update_triple = false;
671 // Always use the architecture name because it might be more descriptive
672 // than the architecture enum ("armv7" -> llvm::Triple::arm).
673 m_triple.setArchName(llvm::StringRef(core_def->name));
674 if (arch_type == eArchTypeMachO)
676 m_triple.setVendor (llvm::Triple::Apple);
678 switch (core_def->machine)
680 case llvm::Triple::arm:
681 case llvm::Triple::thumb:
682 m_triple.setOS (llvm::Triple::IOS);
685 case llvm::Triple::x86:
686 case llvm::Triple::x86_64:
688 m_triple.setOS (llvm::Triple::MacOSX);
694 m_triple.setVendor (llvm::Triple::UnknownVendor);
695 m_triple.setOS (llvm::Triple::UnknownOS);
697 // Fall back onto setting the machine type if the arch by name failed...
698 if (m_triple.getArch () == llvm::Triple::UnknownArch)
699 m_triple.setArch (core_def->machine);
703 CoreUpdated(update_triple);
708 ArchSpec::GetMinimumOpcodeByteSize() const
710 const CoreDefinition *core_def = FindCoreDefinition (m_core);
712 return core_def->min_opcode_byte_size;
717 ArchSpec::GetMaximumOpcodeByteSize() const
719 const CoreDefinition *core_def = FindCoreDefinition (m_core);
721 return core_def->max_opcode_byte_size;
726 ArchSpec::IsExactMatch (const ArchSpec& rhs) const
728 return IsEqualTo (rhs, true);
732 ArchSpec::IsCompatibleMatch (const ArchSpec& rhs) const
734 return IsEqualTo (rhs, false);
738 ArchSpec::IsEqualTo (const ArchSpec& rhs, bool exact_match) const
740 if (GetByteOrder() != rhs.GetByteOrder())
743 const ArchSpec::Core lhs_core = GetCore ();
744 const ArchSpec::Core rhs_core = rhs.GetCore ();
746 const bool core_match = cores_match (lhs_core, rhs_core, true, exact_match);
750 const llvm::Triple &lhs_triple = GetTriple();
751 const llvm::Triple &rhs_triple = rhs.GetTriple();
753 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
754 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
755 if (lhs_triple_vendor != rhs_triple_vendor)
759 const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
760 const bool lhs_vendor_specified = TripleVendorWasSpecified();
761 // Both architectures had the vendor specified, so if they aren't
762 // equal then we return false
763 if (rhs_vendor_specified && lhs_vendor_specified)
767 // Only fail if both vendor types are not unknown
768 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
769 rhs_triple_vendor != llvm::Triple::UnknownVendor)
773 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
774 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
775 if (lhs_triple_os != rhs_triple_os)
779 const bool rhs_os_specified = rhs.TripleOSWasSpecified();
780 const bool lhs_os_specified = TripleOSWasSpecified();
781 // Both architectures had the OS specified, so if they aren't
782 // equal then we return false
783 if (rhs_os_specified && lhs_os_specified)
786 // Only fail if both os types are not unknown
787 if (lhs_triple_os != llvm::Triple::UnknownOS &&
788 rhs_triple_os != llvm::Triple::UnknownOS)
792 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
793 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
795 if (lhs_triple_env != rhs_triple_env)
797 // Only fail if both environment types are not unknown
798 if (lhs_triple_env != llvm::Triple::UnknownEnvironment &&
799 rhs_triple_env != llvm::Triple::UnknownEnvironment)
807 //===----------------------------------------------------------------------===//
811 ArchSpec::CoreUpdated (bool update_triple)
813 const CoreDefinition *core_def = FindCoreDefinition (m_core);
817 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
818 m_byte_order = core_def->default_byte_order;
823 m_triple = llvm::Triple();
824 m_byte_order = eByteOrderInvalid;
828 //===----------------------------------------------------------------------===//
832 cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match)
839 case ArchSpec::kCore_any:
842 case ArchSpec::kCore_arm_any:
843 if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
845 if (core2 >= ArchSpec::kCore_thumb_first && core2 <= ArchSpec::kCore_thumb_last)
847 if (core2 == ArchSpec::kCore_arm_any)
851 case ArchSpec::kCore_x86_32_any:
852 if ((core2 >= ArchSpec::kCore_x86_32_first && core2 <= ArchSpec::kCore_x86_32_last) || (core2 == ArchSpec::kCore_x86_32_any))
856 case ArchSpec::kCore_ppc_any:
857 if ((core2 >= ArchSpec::kCore_ppc_first && core2 <= ArchSpec::kCore_ppc_last) || (core2 == ArchSpec::kCore_ppc_any))
861 case ArchSpec::kCore_ppc64_any:
862 if ((core2 >= ArchSpec::kCore_ppc64_first && core2 <= ArchSpec::kCore_ppc64_last) || (core2 == ArchSpec::kCore_ppc64_any))
866 case ArchSpec::eCore_arm_armv7m:
867 case ArchSpec::eCore_arm_armv7em:
868 case ArchSpec::eCore_arm_armv7f:
869 case ArchSpec::eCore_arm_armv7k:
870 case ArchSpec::eCore_arm_armv7s:
871 if (!enforce_exact_match)
874 if (core2 == ArchSpec::eCore_arm_armv7)
883 return cores_match (core2, core1, false, enforce_exact_match);
888 lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
890 const ArchSpec::Core lhs_core = lhs.GetCore ();
891 const ArchSpec::Core rhs_core = rhs.GetCore ();
892 return lhs_core < rhs_core;