blob: ce97f59a6a70481208b87a55f1718cb59cc1259e [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ArchSpec.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Core/ArchSpec.h"
11
Eli Friedman50fac2f2010-06-11 04:26:08 +000012#include <stdio.h>
Jason Molendadfa424c2012-09-18 23:27:18 +000013#include <errno.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014
15#include <string>
16
Greg Clayton41f92322010-06-11 03:25:34 +000017#include "llvm/Support/ELF.h"
Stephen Wilsonfacebfc2011-02-24 19:13:58 +000018#include "llvm/Support/Host.h"
Greg Clayton41f92322010-06-11 03:25:34 +000019#include "llvm/Support/MachO.h"
Greg Claytone795f1b2012-08-08 01:19:34 +000020#include "lldb/Core/RegularExpression.h"
Greg Clayton514487e2011-02-15 21:59:32 +000021#include "lldb/Host/Endian.h"
22#include "lldb/Host/Host.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000023#include "lldb/Target/Platform.h"
Greg Clayton41f92322010-06-11 03:25:34 +000024
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025using namespace lldb;
26using namespace lldb_private;
27
Greg Clayton64195a22011-02-23 00:35:02 +000028#define ARCH_SPEC_SEPARATOR_CHAR '-'
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
Jason Molendaba813dc2012-11-04 03:20:05 +000030
31static bool cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match);
32
Greg Clayton64195a22011-02-23 00:35:02 +000033namespace lldb_private {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034
Greg Clayton64195a22011-02-23 00:35:02 +000035 struct CoreDefinition
36 {
37 ByteOrder default_byte_order;
38 uint32_t addr_byte_size;
Greg Clayton357132e2011-03-26 19:14:58 +000039 uint32_t min_opcode_byte_size;
40 uint32_t max_opcode_byte_size;
Greg Clayton64195a22011-02-23 00:35:02 +000041 llvm::Triple::ArchType machine;
42 ArchSpec::Core core;
43 const char *name;
44 };
45
46}
47
48// This core information can be looked using the ArchSpec::Core as the index
49static const CoreDefinition g_core_definitions[ArchSpec::kNumCores] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050{
Greg Clayton357132e2011-03-26 19:14:58 +000051 { 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" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000055 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5e , "armv5e" },
Greg Clayton357132e2011-03-26 19:14:58 +000056 { 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" },
Greg Clayton357132e2011-03-26 19:14:58 +000060 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7s , "armv7s" },
Jason Molenda7a1559c2013-03-08 01:20:17 +000061 { 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" },
Greg Clayton357132e2011-03-26 19:14:58 +000064 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_xscale , "xscale" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000065 { 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" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000072 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7s , "thumbv7s" },
Jason Molenda7a1559c2013-03-08 01:20:17 +000073 { 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" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000076
Greg Clayton64195a22011-02-23 00:35:02 +000077
Greg Clayton357132e2011-03-26 19:14:58 +000078 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "ppc" },
79 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
80 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
81 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
82 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
83 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
84 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
85 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
86 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
87 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
88 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
89 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
90 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
Greg Clayton64195a22011-02-23 00:35:02 +000091
Greg Clayton357132e2011-03-26 19:14:58 +000092 { eByteOrderLittle, 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "ppc64" },
93 { eByteOrderLittle, 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
Greg Clayton64195a22011-02-23 00:35:02 +000094
Greg Clayton357132e2011-03-26 19:14:58 +000095 { 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" },
Greg Clayton64195a22011-02-23 00:35:02 +000097
Greg Claytonab65b342011-04-13 22:47:15 +000098 { 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" },
Greg Clayton64195a22011-02-23 00:35:02 +0000101
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000102 { 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" }
Greg Clayton64195a22011-02-23 00:35:02 +0000105};
106
107struct ArchDefinitionEntry
108{
109 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110 uint32_t cpu;
111 uint32_t sub;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000112 uint32_t cpu_mask;
113 uint32_t sub_mask;
Greg Clayton64195a22011-02-23 00:35:02 +0000114};
115
116struct ArchDefinition
117{
118 ArchitectureType type;
119 size_t num_entries;
120 const ArchDefinitionEntry *entries;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121 const char *name;
122};
123
Greg Clayton41f92322010-06-11 03:25:34 +0000124
Greg Claytonc7bece562013-01-25 18:06:21 +0000125size_t
Greg Claytonab65b342011-04-13 22:47:15 +0000126ArchSpec::AutoComplete (const char *name, StringList &matches)
127{
128 uint32_t i;
129 if (name && name[0])
130 {
131 for (i = 0; i < ArchSpec::kNumCores; ++i)
132 {
133 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
134 matches.AppendString (g_core_definitions[i].name);
135 }
136 }
137 else
138 {
139 for (i = 0; i < ArchSpec::kNumCores; ++i)
140 matches.AppendString (g_core_definitions[i].name);
141 }
142 return matches.GetSize();
143}
144
145
146
Greg Clayton64195a22011-02-23 00:35:02 +0000147#define CPU_ANY (UINT32_MAX)
148
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.
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000154#define SUBTYPE_MASK 0x00FFFFFFu
Greg Clayton64195a22011-02-23 00:35:02 +0000155static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000156{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000157 { 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 },
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000168 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPUTypeARM , 11 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda7a1559c2013-03-08 01:20:17 +0000169 { 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 },
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000172 { 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 },
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000179 { ArchSpec::eCore_thumbv7s , llvm::MachO::CPUTypeARM , 11 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda7a1559c2013-03-08 01:20:17 +0000180 { 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 },
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000183 { 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 }
Greg Clayton64195a22011-02-23 00:35:02 +0000209};
210static const ArchDefinition g_macho_arch_def = {
211 eArchTypeMachO,
212 sizeof(g_macho_arch_entries)/sizeof(g_macho_arch_entries[0]),
213 g_macho_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000214 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000215};
216
Greg Clayton64195a22011-02-23 00:35:02 +0000217//===----------------------------------------------------------------------===//
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.
222static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000224 { 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232};
233
Greg Clayton64195a22011-02-23 00:35:02 +0000234static const ArchDefinition g_elf_arch_def = {
235 eArchTypeELF,
236 sizeof(g_elf_arch_entries)/sizeof(g_elf_arch_entries[0]),
237 g_elf_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000238 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000239};
240
Greg Clayton64195a22011-02-23 00:35:02 +0000241//===----------------------------------------------------------------------===//
242// Table of all ArchDefinitions
243static const ArchDefinition *g_arch_definitions[] = {
244 &g_macho_arch_def,
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000245 &g_elf_arch_def
Greg Clayton64195a22011-02-23 00:35:02 +0000246};
Greg Clayton41f92322010-06-11 03:25:34 +0000247
Greg Clayton64195a22011-02-23 00:35:02 +0000248static const size_t k_num_arch_definitions =
249 sizeof(g_arch_definitions) / sizeof(g_arch_definitions[0]);
250
251//===----------------------------------------------------------------------===//
252// Static helper functions.
253
254
255// Get the architecture definition for a given object type.
256static const ArchDefinition *
257FindArchDefinition (ArchitectureType arch_type)
258{
259 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
260 {
261 const ArchDefinition *def = g_arch_definitions[i];
262 if (def->type == arch_type)
263 return def;
264 }
265 return NULL;
266}
267
268// Get an architecture definition by name.
269static const CoreDefinition *
270FindCoreDefinition (llvm::StringRef name)
271{
272 for (unsigned int i = 0; i < ArchSpec::kNumCores; ++i)
273 {
274 if (name.equals_lower(g_core_definitions[i].name))
275 return &g_core_definitions[i];
276 }
277 return NULL;
278}
279
280static inline const CoreDefinition *
281FindCoreDefinition (ArchSpec::Core core)
282{
283 if (core >= 0 && core < ArchSpec::kNumCores)
284 return &g_core_definitions[core];
285 return NULL;
286}
287
288// Get a definition entry by cpu type and subtype.
289static const ArchDefinitionEntry *
290FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
291{
292 if (def == NULL)
293 return NULL;
294
Greg Clayton64195a22011-02-23 00:35:02 +0000295 const ArchDefinitionEntry *entries = def->entries;
296 for (size_t i = 0; i < def->num_entries; ++i)
297 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000298 if (entries[i].cpu == (cpu & entries[i].cpu_mask))
299 if (entries[i].sub == (sub & entries[i].sub_mask))
300 return &entries[i];
Greg Clayton64195a22011-02-23 00:35:02 +0000301 }
302 return NULL;
303}
304
305static const ArchDefinitionEntry *
306FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
307{
308 if (def == NULL)
309 return NULL;
310
311 const ArchDefinitionEntry *entries = def->entries;
312 for (size_t i = 0; i < def->num_entries; ++i)
313 {
314 if (entries[i].core == core)
315 return &entries[i];
316 }
317 return NULL;
318}
319
320//===----------------------------------------------------------------------===//
321// Constructors and destructors.
322
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000324 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000325 m_core (kCore_invalid),
326 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327{
328}
329
Greg Claytoneb0103f2011-04-07 22:46:35 +0000330ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000331 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000332 m_core (kCore_invalid),
333 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334{
Greg Clayton64195a22011-02-23 00:35:02 +0000335 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000336 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337}
338
Greg Clayton70512312012-05-08 01:45:38 +0000339
340ArchSpec::ArchSpec (const char *triple_cstr) :
341 m_triple (),
342 m_core (kCore_invalid),
343 m_byte_order (eByteOrderInvalid)
344{
345 if (triple_cstr)
346 SetTriple(triple_cstr);
347}
348
Greg Clayton64195a22011-02-23 00:35:02 +0000349ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000350 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000351 m_core (kCore_invalid),
352 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353{
Greg Clayton64195a22011-02-23 00:35:02 +0000354 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355}
356
Greg Claytone0d378b2011-03-24 21:19:54 +0000357ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000358 m_triple (),
359 m_core (kCore_invalid),
360 m_byte_order (eByteOrderInvalid)
361{
362 SetArchitecture (arch_type, cpu, subtype);
363}
364
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365ArchSpec::~ArchSpec()
366{
367}
368
Greg Clayton64195a22011-02-23 00:35:02 +0000369//===----------------------------------------------------------------------===//
370// Assignment and initialization.
371
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372const ArchSpec&
373ArchSpec::operator= (const ArchSpec& rhs)
374{
375 if (this != &rhs)
376 {
Greg Clayton514487e2011-02-15 21:59:32 +0000377 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000378 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000379 m_byte_order = rhs.m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000380 }
381 return *this;
382}
383
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384void
385ArchSpec::Clear()
386{
Greg Clayton514487e2011-02-15 21:59:32 +0000387 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000388 m_core = kCore_invalid;
389 m_byte_order = eByteOrderInvalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390}
391
Greg Clayton64195a22011-02-23 00:35:02 +0000392//===----------------------------------------------------------------------===//
393// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394
Greg Clayton41f92322010-06-11 03:25:34 +0000395
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000397ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398{
Greg Clayton64195a22011-02-23 00:35:02 +0000399 const CoreDefinition *core_def = FindCoreDefinition (m_core);
400 if (core_def)
401 return core_def->name;
402 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403}
404
Greg Clayton64195a22011-02-23 00:35:02 +0000405uint32_t
406ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407{
Greg Clayton64195a22011-02-23 00:35:02 +0000408 const CoreDefinition *core_def = FindCoreDefinition (m_core);
409 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410 {
Greg Clayton64195a22011-02-23 00:35:02 +0000411 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
412 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413 {
Greg Clayton64195a22011-02-23 00:35:02 +0000414 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415 }
416 }
Greg Clayton64195a22011-02-23 00:35:02 +0000417 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418}
419
Greg Clayton64195a22011-02-23 00:35:02 +0000420uint32_t
421ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422{
Greg Clayton64195a22011-02-23 00:35:02 +0000423 const CoreDefinition *core_def = FindCoreDefinition (m_core);
424 if (core_def)
425 {
426 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
427 if (arch_def)
428 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000429 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000430 }
431 }
432 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433}
434
Greg Clayton64195a22011-02-23 00:35:02 +0000435llvm::Triple::ArchType
436ArchSpec::GetMachine () const
437{
438 const CoreDefinition *core_def = FindCoreDefinition (m_core);
439 if (core_def)
440 return core_def->machine;
441
442 return llvm::Triple::UnknownArch;
443}
444
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445uint32_t
446ArchSpec::GetAddressByteSize() const
447{
Greg Clayton64195a22011-02-23 00:35:02 +0000448 const CoreDefinition *core_def = FindCoreDefinition (m_core);
449 if (core_def)
450 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000451 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452}
453
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454ByteOrder
455ArchSpec::GetDefaultEndian () const
456{
Greg Clayton64195a22011-02-23 00:35:02 +0000457 const CoreDefinition *core_def = FindCoreDefinition (m_core);
458 if (core_def)
459 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460 return eByteOrderInvalid;
461}
462
Greg Clayton64195a22011-02-23 00:35:02 +0000463lldb::ByteOrder
464ArchSpec::GetByteOrder () const
465{
466 if (m_byte_order == eByteOrderInvalid)
467 return GetDefaultEndian();
468 return m_byte_order;
469}
470
471//===----------------------------------------------------------------------===//
472// Mutators.
473
474bool
475ArchSpec::SetTriple (const llvm::Triple &triple)
476{
477 m_triple = triple;
478
479 llvm::StringRef arch_name (m_triple.getArchName());
480 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
481 if (core_def)
482 {
483 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000484 // 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;
Greg Clayton64195a22011-02-23 00:35:02 +0000488 }
489 else
490 {
491 Clear();
492 }
493
494
495 return IsValid();
496}
497
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000498static bool
499ParseMachCPUDashSubtypeTriple (const char *triple_cstr, ArchSpec &arch)
500{
501 // Accept "12-10" or "12.10" as cpu type/subtype
502 if (isdigit(triple_cstr[0]))
503 {
504 char *end = NULL;
505 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000506 uint32_t cpu = (uint32_t)::strtoul (triple_cstr, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000507 if (errno == 0 && cpu != 0 && end && ((*end == '-') || (*end == '.')))
508 {
509 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000510 uint32_t sub = (uint32_t)::strtoul (end + 1, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000511 if (errno == 0 && end && ((*end == '-') || (*end == '.') || (*end == '\0')))
512 {
513 if (arch.SetArchitecture (eArchTypeMachO, cpu, sub))
514 {
515 if (*end == '-')
516 {
517 llvm::StringRef vendor_os (end + 1);
518 size_t dash_pos = vendor_os.find('-');
519 if (dash_pos != llvm::StringRef::npos)
520 {
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;
Greg Claytonc7bece562013-01-25 18:06:21 +0000524 dash_pos = vendor_os.find('-', vendor_start_pos);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000525 if (dash_pos == llvm::StringRef::npos)
526 {
527 if (vendor_start_pos < vendor_os.size())
528 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos));
529 }
530 else
531 {
532 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos, dash_pos - vendor_start_pos));
533 }
534 }
535 }
536 return true;
537 }
538 }
539 }
540 }
541 return false;
542}
Greg Clayton64195a22011-02-23 00:35:02 +0000543bool
Greg Clayton70512312012-05-08 01:45:38 +0000544ArchSpec::SetTriple (const char *triple_cstr)
Greg Clayton64195a22011-02-23 00:35:02 +0000545{
Greg Clayton23aca092011-08-12 23:32:52 +0000546 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000547 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000548 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
549 return true;
550
Greg Clayton64195a22011-02-23 00:35:02 +0000551 llvm::StringRef triple_stref (triple_cstr);
552 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
553 {
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);
561 }
562 else
563 {
564 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
565 triple_stref = normalized_triple_sstr;
Greg Clayton70512312012-05-08 01:45:38 +0000566 SetTriple (llvm::Triple (triple_stref));
567 }
568 }
569 else
570 Clear();
571 return IsValid();
572}
573
574bool
575ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
576{
577 if (triple_cstr && triple_cstr[0])
578 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000579 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
580 return true;
581
Greg Clayton70512312012-05-08 01:45:38 +0000582 llvm::StringRef triple_stref (triple_cstr);
583 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
584 {
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);
592 }
593 else
594 {
595 ArchSpec raw_arch (triple_cstr);
596
597 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
598 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000599 llvm::Triple normalized_triple (triple_stref);
600
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;
604
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))
608 {
609 if (platform)
610 {
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.
Greg Clayton70512312012-05-08 01:45:38 +0000614 ArchSpec compatible_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000615 if (platform->IsCompatibleArchitecture (raw_arch, false, &compatible_arch))
Greg Claytoneb0103f2011-04-07 22:46:35 +0000616 {
Greg Clayton70512312012-05-08 01:45:38 +0000617 if (compatible_arch.IsValid())
618 {
619 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
620 if (!vendor_specified)
621 normalized_triple.setVendor(compatible_triple.getVendor());
622 if (!os_specified)
623 normalized_triple.setOS(compatible_triple.getOS());
624 if (!env_specified && compatible_triple.getEnvironmentName().size())
625 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
626 }
Greg Claytoneb0103f2011-04-07 22:46:35 +0000627 }
Greg Clayton70512312012-05-08 01:45:38 +0000628 else
Greg Claytoneb0103f2011-04-07 22:46:35 +0000629 {
Greg Clayton70512312012-05-08 01:45:38 +0000630 *this = raw_arch;
631 return IsValid();
Greg Claytoneb0103f2011-04-07 22:46:35 +0000632 }
633 }
634 else
635 {
636 // No platform specified, fall back to the host system for
637 // the default vendor, os, and environment.
Sean Callananbfb237bc2011-11-04 22:46:46 +0000638 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton70512312012-05-08 01:45:38 +0000639 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());
Greg Claytoneb0103f2011-04-07 22:46:35 +0000645 }
646 }
647 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000648 }
649 }
650 else
651 Clear();
652 return IsValid();
653}
654
Greg Clayton64195a22011-02-23 00:35:02 +0000655bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000656ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
Greg Clayton64195a22011-02-23 00:35:02 +0000657{
658 m_core = kCore_invalid;
659 bool update_triple = true;
660 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
661 if (arch_def)
662 {
663 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
664 if (arch_def_entry)
665 {
666 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
667 if (core_def)
668 {
669 m_core = core_def->core;
670 update_triple = false;
Greg Clayton593577a2011-09-21 03:57:31 +0000671 // 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));
Greg Clayton64195a22011-02-23 00:35:02 +0000674 if (arch_type == eArchTypeMachO)
675 {
676 m_triple.setVendor (llvm::Triple::Apple);
Greg Clayton70512312012-05-08 01:45:38 +0000677
678 switch (core_def->machine)
679 {
680 case llvm::Triple::arm:
681 case llvm::Triple::thumb:
682 m_triple.setOS (llvm::Triple::IOS);
683 break;
684
685 case llvm::Triple::x86:
686 case llvm::Triple::x86_64:
687 default:
688 m_triple.setOS (llvm::Triple::MacOSX);
689 break;
690 }
Greg Clayton64195a22011-02-23 00:35:02 +0000691 }
692 else
693 {
694 m_triple.setVendor (llvm::Triple::UnknownVendor);
695 m_triple.setOS (llvm::Triple::UnknownOS);
696 }
Greg Clayton593577a2011-09-21 03:57:31 +0000697 // 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);
Greg Clayton64195a22011-02-23 00:35:02 +0000700 }
701 }
702 }
703 CoreUpdated(update_triple);
704 return IsValid();
705}
706
Greg Clayton357132e2011-03-26 19:14:58 +0000707uint32_t
708ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000709{
Greg Clayton357132e2011-03-26 19:14:58 +0000710 const CoreDefinition *core_def = FindCoreDefinition (m_core);
711 if (core_def)
712 return core_def->min_opcode_byte_size;
713 return 0;
714}
715
716uint32_t
717ArchSpec::GetMaximumOpcodeByteSize() const
718{
719 const CoreDefinition *core_def = FindCoreDefinition (m_core);
720 if (core_def)
721 return core_def->max_opcode_byte_size;
722 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000723}
724
Jason Molendaba813dc2012-11-04 03:20:05 +0000725bool
726ArchSpec::IsExactMatch (const ArchSpec& rhs) const
727{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000728 return IsEqualTo (rhs, true);
Jason Molendaba813dc2012-11-04 03:20:05 +0000729}
730
731bool
732ArchSpec::IsCompatibleMatch (const ArchSpec& rhs) const
733{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000734 return IsEqualTo (rhs, false);
Jason Molendaba813dc2012-11-04 03:20:05 +0000735}
736
737bool
Sean Callananbf4b7be2012-12-13 22:07:14 +0000738ArchSpec::IsEqualTo (const ArchSpec& rhs, bool exact_match) const
Jason Molendaba813dc2012-11-04 03:20:05 +0000739{
740 if (GetByteOrder() != rhs.GetByteOrder())
741 return false;
742
743 const ArchSpec::Core lhs_core = GetCore ();
744 const ArchSpec::Core rhs_core = rhs.GetCore ();
745
746 const bool core_match = cores_match (lhs_core, rhs_core, true, exact_match);
747
748 if (core_match)
749 {
750 const llvm::Triple &lhs_triple = GetTriple();
751 const llvm::Triple &rhs_triple = rhs.GetTriple();
752
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)
756 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000757 if (exact_match)
758 {
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)
764 return false;
765 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000766
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)
770 return false;
771 }
772
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)
776 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000777 if (exact_match)
778 {
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)
784 return false;
785 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000786 // Only fail if both os types are not unknown
787 if (lhs_triple_os != llvm::Triple::UnknownOS &&
788 rhs_triple_os != llvm::Triple::UnknownOS)
789 return false;
790 }
791
792 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
793 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
794
795 if (lhs_triple_env != rhs_triple_env)
796 {
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)
800 return false;
801 }
802 return true;
803 }
804 return false;
805}
806
Greg Clayton64195a22011-02-23 00:35:02 +0000807//===----------------------------------------------------------------------===//
808// Helper methods.
809
810void
811ArchSpec::CoreUpdated (bool update_triple)
812{
813 const CoreDefinition *core_def = FindCoreDefinition (m_core);
814 if (core_def)
815 {
816 if (update_triple)
817 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
818 m_byte_order = core_def->default_byte_order;
819 }
820 else
821 {
822 if (update_triple)
823 m_triple = llvm::Triple();
824 m_byte_order = eByteOrderInvalid;
825 }
826}
827
828//===----------------------------------------------------------------------===//
829// Operators.
830
Greg Clayton70512312012-05-08 01:45:38 +0000831static bool
Jason Molendaba813dc2012-11-04 03:20:05 +0000832cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match)
Greg Clayton70512312012-05-08 01:45:38 +0000833{
Jason Molendaba813dc2012-11-04 03:20:05 +0000834 if (core1 == core2)
835 return true;
836
Greg Clayton70512312012-05-08 01:45:38 +0000837 switch (core1)
838 {
Greg Clayton70512312012-05-08 01:45:38 +0000839 case ArchSpec::kCore_any:
840 return true;
841
842 case ArchSpec::kCore_arm_any:
843 if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
844 return true;
845 if (core2 >= ArchSpec::kCore_thumb_first && core2 <= ArchSpec::kCore_thumb_last)
846 return true;
847 if (core2 == ArchSpec::kCore_arm_any)
848 return true;
849 break;
850
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))
853 return true;
854 break;
855
856 case ArchSpec::kCore_ppc_any:
857 if ((core2 >= ArchSpec::kCore_ppc_first && core2 <= ArchSpec::kCore_ppc_last) || (core2 == ArchSpec::kCore_ppc_any))
858 return true;
859 break;
860
861 case ArchSpec::kCore_ppc64_any:
862 if ((core2 >= ArchSpec::kCore_ppc64_first && core2 <= ArchSpec::kCore_ppc64_last) || (core2 == ArchSpec::kCore_ppc64_any))
863 return true;
864 break;
865
Jason Molenda7a1559c2013-03-08 01:20:17 +0000866 case ArchSpec::eCore_arm_armv7m:
867 case ArchSpec::eCore_arm_armv7em:
Johnny Chen1083b0d2012-08-28 22:53:40 +0000868 case ArchSpec::eCore_arm_armv7f:
869 case ArchSpec::eCore_arm_armv7k:
870 case ArchSpec::eCore_arm_armv7s:
Jason Molendaba813dc2012-11-04 03:20:05 +0000871 if (!enforce_exact_match)
872 {
873 try_inverse = false;
874 if (core2 == ArchSpec::eCore_arm_armv7)
875 return true;
876 }
Johnny Chen1083b0d2012-08-28 22:53:40 +0000877 break;
878
Greg Clayton70512312012-05-08 01:45:38 +0000879 default:
880 break;
881 }
882 if (try_inverse)
Jason Molendaba813dc2012-11-04 03:20:05 +0000883 return cores_match (core2, core1, false, enforce_exact_match);
Greg Clayton70512312012-05-08 01:45:38 +0000884 return false;
885}
886
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000887bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000888lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
889{
Greg Clayton64195a22011-02-23 00:35:02 +0000890 const ArchSpec::Core lhs_core = lhs.GetCore ();
891 const ArchSpec::Core rhs_core = rhs.GetCore ();
892 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000893}