blob: 28f277801b274a9a8b1b97d3ecd1d8d9bf6388b0 [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
Greg Clayton64195a22011-02-23 00:35:02 +000030namespace lldb_private {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
Greg Clayton64195a22011-02-23 00:35:02 +000032 struct CoreDefinition
33 {
34 ByteOrder default_byte_order;
35 uint32_t addr_byte_size;
Greg Clayton357132e2011-03-26 19:14:58 +000036 uint32_t min_opcode_byte_size;
37 uint32_t max_opcode_byte_size;
Greg Clayton64195a22011-02-23 00:35:02 +000038 llvm::Triple::ArchType machine;
39 ArchSpec::Core core;
40 const char *name;
41 };
42
43}
44
45// This core information can be looked using the ArchSpec::Core as the index
46static const CoreDefinition g_core_definitions[ArchSpec::kNumCores] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047{
Greg Clayton357132e2011-03-26 19:14:58 +000048 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_generic , "arm" },
49 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4 , "armv4" },
50 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4t , "armv4t" },
51 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5 , "armv5" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000052 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5e , "armv5e" },
Greg Clayton357132e2011-03-26 19:14:58 +000053 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5t , "armv5t" },
54 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6 , "armv6" },
55 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7 , "armv7" },
56 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7f , "armv7f" },
57 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7k , "armv7k" },
58 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7s , "armv7s" },
59 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_xscale , "xscale" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000060 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumb , "thumb" },
61 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv4t , "thumbv4t" },
62 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5 , "thumbv5" },
63 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5e , "thumbv5e" },
64 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv6 , "thumbv6" },
65 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7 , "thumbv7" },
66 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7f , "thumbv7f" },
67 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7k , "thumbv7k" },
68 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7s , "thumbv7s" },
69
Greg Clayton64195a22011-02-23 00:35:02 +000070
Greg Clayton357132e2011-03-26 19:14:58 +000071 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "ppc" },
72 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
73 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
74 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
75 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
76 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
77 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
78 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
79 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
80 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
81 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
82 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
83 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
Greg Clayton64195a22011-02-23 00:35:02 +000084
Greg Clayton357132e2011-03-26 19:14:58 +000085 { eByteOrderLittle, 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "ppc64" },
86 { eByteOrderLittle, 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
Greg Clayton64195a22011-02-23 00:35:02 +000087
Greg Clayton357132e2011-03-26 19:14:58 +000088 { eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
89 { eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
Greg Clayton64195a22011-02-23 00:35:02 +000090
Greg Claytonab65b342011-04-13 22:47:15 +000091 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
92 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
93 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
Greg Clayton64195a22011-02-23 00:35:02 +000094
Greg Clayton9e6cffc2012-09-19 22:25:17 +000095 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" },
96 { eByteOrderLittle, 4, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach32 , "unknown-mach-32" },
97 { eByteOrderLittle, 8, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach64 , "unknown-mach-64" }
Greg Clayton64195a22011-02-23 00:35:02 +000098};
99
100struct ArchDefinitionEntry
101{
102 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103 uint32_t cpu;
104 uint32_t sub;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000105 uint32_t cpu_mask;
106 uint32_t sub_mask;
Greg Clayton64195a22011-02-23 00:35:02 +0000107};
108
109struct ArchDefinition
110{
111 ArchitectureType type;
112 size_t num_entries;
113 const ArchDefinitionEntry *entries;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114 const char *name;
115};
116
Greg Clayton41f92322010-06-11 03:25:34 +0000117
Greg Claytonab65b342011-04-13 22:47:15 +0000118uint32_t
119ArchSpec::AutoComplete (const char *name, StringList &matches)
120{
121 uint32_t i;
122 if (name && name[0])
123 {
124 for (i = 0; i < ArchSpec::kNumCores; ++i)
125 {
126 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
127 matches.AppendString (g_core_definitions[i].name);
128 }
129 }
130 else
131 {
132 for (i = 0; i < ArchSpec::kNumCores; ++i)
133 matches.AppendString (g_core_definitions[i].name);
134 }
135 return matches.GetSize();
136}
137
138
139
Greg Clayton64195a22011-02-23 00:35:02 +0000140#define CPU_ANY (UINT32_MAX)
141
142//===----------------------------------------------------------------------===//
143// A table that gets searched linearly for matches. This table is used to
144// convert cpu type and subtypes to architecture names, and to convert
145// architecture names to cpu types and subtypes. The ordering is important and
146// allows the precedence to be set when the table is built.
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000147#define SUBTYPE_MASK 0x00FFFFFFu
Greg Clayton64195a22011-02-23 00:35:02 +0000148static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000149{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000150 { ArchSpec::eCore_arm_generic , llvm::MachO::CPUTypeARM , CPU_ANY, UINT32_MAX , UINT32_MAX },
151 { ArchSpec::eCore_arm_generic , llvm::MachO::CPUTypeARM , 0 , UINT32_MAX , SUBTYPE_MASK },
152 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPUTypeARM , 5 , UINT32_MAX , SUBTYPE_MASK },
153 { ArchSpec::eCore_arm_armv4t , llvm::MachO::CPUTypeARM , 5 , UINT32_MAX , SUBTYPE_MASK },
154 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPUTypeARM , 6 , UINT32_MAX , SUBTYPE_MASK },
155 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPUTypeARM , 7 , UINT32_MAX , SUBTYPE_MASK },
156 { ArchSpec::eCore_arm_armv5e , llvm::MachO::CPUTypeARM , 7 , UINT32_MAX , SUBTYPE_MASK },
157 { ArchSpec::eCore_arm_armv5t , llvm::MachO::CPUTypeARM , 7 , UINT32_MAX , SUBTYPE_MASK },
158 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPUTypeARM , 8 , UINT32_MAX , SUBTYPE_MASK },
159 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPUTypeARM , 9 , UINT32_MAX , SUBTYPE_MASK },
160 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPUTypeARM , 10 , UINT32_MAX , SUBTYPE_MASK },
161 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPUTypeARM , 12 , UINT32_MAX , SUBTYPE_MASK },
162 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPUTypeARM , 11 , UINT32_MAX , SUBTYPE_MASK },
163 { ArchSpec::eCore_thumb , llvm::MachO::CPUTypeARM , 0 , UINT32_MAX , SUBTYPE_MASK },
164 { ArchSpec::eCore_thumbv4t , llvm::MachO::CPUTypeARM , 5 , UINT32_MAX , SUBTYPE_MASK },
165 { ArchSpec::eCore_thumbv5 , llvm::MachO::CPUTypeARM , 7 , UINT32_MAX , SUBTYPE_MASK },
166 { ArchSpec::eCore_thumbv5e , llvm::MachO::CPUTypeARM , 7 , UINT32_MAX , SUBTYPE_MASK },
167 { ArchSpec::eCore_thumbv6 , llvm::MachO::CPUTypeARM , 6 , UINT32_MAX , SUBTYPE_MASK },
168 { ArchSpec::eCore_thumbv7 , llvm::MachO::CPUTypeARM , 9 , UINT32_MAX , SUBTYPE_MASK },
169 { ArchSpec::eCore_thumbv7f , llvm::MachO::CPUTypeARM , 10 , UINT32_MAX , SUBTYPE_MASK },
170 { ArchSpec::eCore_thumbv7k , llvm::MachO::CPUTypeARM , 12 , UINT32_MAX , SUBTYPE_MASK },
171 { ArchSpec::eCore_thumbv7s , llvm::MachO::CPUTypeARM , 11 , UINT32_MAX , SUBTYPE_MASK },
172 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPUTypePowerPC , CPU_ANY, UINT32_MAX , UINT32_MAX },
173 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPUTypePowerPC , 0 , UINT32_MAX , SUBTYPE_MASK },
174 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPUTypePowerPC , 1 , UINT32_MAX , SUBTYPE_MASK },
175 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPUTypePowerPC , 2 , UINT32_MAX , SUBTYPE_MASK },
176 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPUTypePowerPC , 3 , UINT32_MAX , SUBTYPE_MASK },
177 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPUTypePowerPC , 4 , UINT32_MAX , SUBTYPE_MASK },
178 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPUTypePowerPC , 5 , UINT32_MAX , SUBTYPE_MASK },
179 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPUTypePowerPC , 6 , UINT32_MAX , SUBTYPE_MASK },
180 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPUTypePowerPC , 7 , UINT32_MAX , SUBTYPE_MASK },
181 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPUTypePowerPC , 8 , UINT32_MAX , SUBTYPE_MASK },
182 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPUTypePowerPC , 9 , UINT32_MAX , SUBTYPE_MASK },
183 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPUTypePowerPC , 10 , UINT32_MAX , SUBTYPE_MASK },
184 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPUTypePowerPC , 11 , UINT32_MAX , SUBTYPE_MASK },
185 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPUTypePowerPC , 100 , UINT32_MAX , SUBTYPE_MASK },
186 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPUTypePowerPC64 , 0 , UINT32_MAX , SUBTYPE_MASK },
187 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPUTypePowerPC64 , 100 , UINT32_MAX , SUBTYPE_MASK },
188 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPUTypeI386 , 3 , UINT32_MAX , SUBTYPE_MASK },
189 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPUTypeI386 , 4 , UINT32_MAX , SUBTYPE_MASK },
190 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPUTypeI386 , 0x84 , UINT32_MAX , SUBTYPE_MASK },
191 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPUTypeI386 , CPU_ANY, UINT32_MAX , UINT32_MAX },
192 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , 3 , UINT32_MAX , SUBTYPE_MASK },
193 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , 4 , UINT32_MAX , SUBTYPE_MASK },
194 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , CPU_ANY, UINT32_MAX , UINT32_MAX },
195 // Catch any unknown mach architectures so we can always use the object and symbol mach-o files
196 { ArchSpec::eCore_uknownMach32 , 0 , 0 , 0xFF000000u, 0x00000000u },
197 { ArchSpec::eCore_uknownMach64 , llvm::MachO::CPUArchABI64 , 0 , 0xFF000000u, 0x00000000u }
Greg Clayton64195a22011-02-23 00:35:02 +0000198};
199static const ArchDefinition g_macho_arch_def = {
200 eArchTypeMachO,
201 sizeof(g_macho_arch_entries)/sizeof(g_macho_arch_entries[0]),
202 g_macho_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000203 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000204};
205
Greg Clayton64195a22011-02-23 00:35:02 +0000206//===----------------------------------------------------------------------===//
207// A table that gets searched linearly for matches. This table is used to
208// convert cpu type and subtypes to architecture names, and to convert
209// architecture names to cpu types and subtypes. The ordering is important and
210// allows the precedence to be set when the table is built.
211static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000213 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Sparc
214 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
215 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 486 (deprecated)
216 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
217 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC64
218 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
219 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // SPARC V9
220 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // AMD64
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221};
222
Greg Clayton64195a22011-02-23 00:35:02 +0000223static const ArchDefinition g_elf_arch_def = {
224 eArchTypeELF,
225 sizeof(g_elf_arch_entries)/sizeof(g_elf_arch_entries[0]),
226 g_elf_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000227 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000228};
229
Greg Clayton64195a22011-02-23 00:35:02 +0000230//===----------------------------------------------------------------------===//
231// Table of all ArchDefinitions
232static const ArchDefinition *g_arch_definitions[] = {
233 &g_macho_arch_def,
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000234 &g_elf_arch_def
Greg Clayton64195a22011-02-23 00:35:02 +0000235};
Greg Clayton41f92322010-06-11 03:25:34 +0000236
Greg Clayton64195a22011-02-23 00:35:02 +0000237static const size_t k_num_arch_definitions =
238 sizeof(g_arch_definitions) / sizeof(g_arch_definitions[0]);
239
240//===----------------------------------------------------------------------===//
241// Static helper functions.
242
243
244// Get the architecture definition for a given object type.
245static const ArchDefinition *
246FindArchDefinition (ArchitectureType arch_type)
247{
248 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
249 {
250 const ArchDefinition *def = g_arch_definitions[i];
251 if (def->type == arch_type)
252 return def;
253 }
254 return NULL;
255}
256
257// Get an architecture definition by name.
258static const CoreDefinition *
259FindCoreDefinition (llvm::StringRef name)
260{
261 for (unsigned int i = 0; i < ArchSpec::kNumCores; ++i)
262 {
263 if (name.equals_lower(g_core_definitions[i].name))
264 return &g_core_definitions[i];
265 }
266 return NULL;
267}
268
269static inline const CoreDefinition *
270FindCoreDefinition (ArchSpec::Core core)
271{
272 if (core >= 0 && core < ArchSpec::kNumCores)
273 return &g_core_definitions[core];
274 return NULL;
275}
276
277// Get a definition entry by cpu type and subtype.
278static const ArchDefinitionEntry *
279FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
280{
281 if (def == NULL)
282 return NULL;
283
Greg Clayton64195a22011-02-23 00:35:02 +0000284 const ArchDefinitionEntry *entries = def->entries;
285 for (size_t i = 0; i < def->num_entries; ++i)
286 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000287 if (entries[i].cpu == (cpu & entries[i].cpu_mask))
288 if (entries[i].sub == (sub & entries[i].sub_mask))
289 return &entries[i];
Greg Clayton64195a22011-02-23 00:35:02 +0000290 }
291 return NULL;
292}
293
294static const ArchDefinitionEntry *
295FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
296{
297 if (def == NULL)
298 return NULL;
299
300 const ArchDefinitionEntry *entries = def->entries;
301 for (size_t i = 0; i < def->num_entries; ++i)
302 {
303 if (entries[i].core == core)
304 return &entries[i];
305 }
306 return NULL;
307}
308
309//===----------------------------------------------------------------------===//
310// Constructors and destructors.
311
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000313 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000314 m_core (kCore_invalid),
315 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316{
317}
318
Greg Claytoneb0103f2011-04-07 22:46:35 +0000319ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000320 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000321 m_core (kCore_invalid),
322 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323{
Greg Clayton64195a22011-02-23 00:35:02 +0000324 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000325 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326}
327
Greg Clayton70512312012-05-08 01:45:38 +0000328
329ArchSpec::ArchSpec (const char *triple_cstr) :
330 m_triple (),
331 m_core (kCore_invalid),
332 m_byte_order (eByteOrderInvalid)
333{
334 if (triple_cstr)
335 SetTriple(triple_cstr);
336}
337
Greg Clayton64195a22011-02-23 00:35:02 +0000338ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000339 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000340 m_core (kCore_invalid),
341 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342{
Greg Clayton64195a22011-02-23 00:35:02 +0000343 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344}
345
Greg Claytone0d378b2011-03-24 21:19:54 +0000346ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000347 m_triple (),
348 m_core (kCore_invalid),
349 m_byte_order (eByteOrderInvalid)
350{
351 SetArchitecture (arch_type, cpu, subtype);
352}
353
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354ArchSpec::~ArchSpec()
355{
356}
357
Greg Clayton64195a22011-02-23 00:35:02 +0000358//===----------------------------------------------------------------------===//
359// Assignment and initialization.
360
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361const ArchSpec&
362ArchSpec::operator= (const ArchSpec& rhs)
363{
364 if (this != &rhs)
365 {
Greg Clayton514487e2011-02-15 21:59:32 +0000366 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000367 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000368 m_byte_order = rhs.m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369 }
370 return *this;
371}
372
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373void
374ArchSpec::Clear()
375{
Greg Clayton514487e2011-02-15 21:59:32 +0000376 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000377 m_core = kCore_invalid;
378 m_byte_order = eByteOrderInvalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379}
380
Greg Clayton64195a22011-02-23 00:35:02 +0000381//===----------------------------------------------------------------------===//
382// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383
Greg Clayton41f92322010-06-11 03:25:34 +0000384
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000386ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387{
Greg Clayton64195a22011-02-23 00:35:02 +0000388 const CoreDefinition *core_def = FindCoreDefinition (m_core);
389 if (core_def)
390 return core_def->name;
391 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392}
393
Greg Clayton64195a22011-02-23 00:35:02 +0000394uint32_t
395ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396{
Greg Clayton64195a22011-02-23 00:35:02 +0000397 const CoreDefinition *core_def = FindCoreDefinition (m_core);
398 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399 {
Greg Clayton64195a22011-02-23 00:35:02 +0000400 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
401 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402 {
Greg Clayton64195a22011-02-23 00:35:02 +0000403 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404 }
405 }
Greg Clayton64195a22011-02-23 00:35:02 +0000406 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407}
408
Greg Clayton64195a22011-02-23 00:35:02 +0000409uint32_t
410ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411{
Greg Clayton64195a22011-02-23 00:35:02 +0000412 const CoreDefinition *core_def = FindCoreDefinition (m_core);
413 if (core_def)
414 {
415 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
416 if (arch_def)
417 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000418 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000419 }
420 }
421 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422}
423
Greg Clayton64195a22011-02-23 00:35:02 +0000424llvm::Triple::ArchType
425ArchSpec::GetMachine () const
426{
427 const CoreDefinition *core_def = FindCoreDefinition (m_core);
428 if (core_def)
429 return core_def->machine;
430
431 return llvm::Triple::UnknownArch;
432}
433
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434uint32_t
435ArchSpec::GetAddressByteSize() const
436{
Greg Clayton64195a22011-02-23 00:35:02 +0000437 const CoreDefinition *core_def = FindCoreDefinition (m_core);
438 if (core_def)
439 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000440 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441}
442
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443ByteOrder
444ArchSpec::GetDefaultEndian () const
445{
Greg Clayton64195a22011-02-23 00:35:02 +0000446 const CoreDefinition *core_def = FindCoreDefinition (m_core);
447 if (core_def)
448 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449 return eByteOrderInvalid;
450}
451
Greg Clayton64195a22011-02-23 00:35:02 +0000452lldb::ByteOrder
453ArchSpec::GetByteOrder () const
454{
455 if (m_byte_order == eByteOrderInvalid)
456 return GetDefaultEndian();
457 return m_byte_order;
458}
459
460//===----------------------------------------------------------------------===//
461// Mutators.
462
463bool
464ArchSpec::SetTriple (const llvm::Triple &triple)
465{
466 m_triple = triple;
467
468 llvm::StringRef arch_name (m_triple.getArchName());
469 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
470 if (core_def)
471 {
472 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000473 // Set the byte order to the default byte order for an architecture.
474 // This can be modified if needed for cases when cores handle both
475 // big and little endian
476 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000477 }
478 else
479 {
480 Clear();
481 }
482
483
484 return IsValid();
485}
486
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000487static bool
488ParseMachCPUDashSubtypeTriple (const char *triple_cstr, ArchSpec &arch)
489{
490 // Accept "12-10" or "12.10" as cpu type/subtype
491 if (isdigit(triple_cstr[0]))
492 {
493 char *end = NULL;
494 errno = 0;
495 uint32_t cpu = ::strtoul (triple_cstr, &end, 0);
496 if (errno == 0 && cpu != 0 && end && ((*end == '-') || (*end == '.')))
497 {
498 errno = 0;
499 uint32_t sub = ::strtoul (end + 1, &end, 0);
500 if (errno == 0 && end && ((*end == '-') || (*end == '.') || (*end == '\0')))
501 {
502 if (arch.SetArchitecture (eArchTypeMachO, cpu, sub))
503 {
504 if (*end == '-')
505 {
506 llvm::StringRef vendor_os (end + 1);
507 size_t dash_pos = vendor_os.find('-');
508 if (dash_pos != llvm::StringRef::npos)
509 {
510 llvm::StringRef vendor_str(vendor_os.substr(0, dash_pos));
511 arch.GetTriple().setVendorName(vendor_str);
512 const size_t vendor_start_pos = dash_pos+1;
513 dash_pos = vendor_os.find(vendor_start_pos, '-');
514 if (dash_pos == llvm::StringRef::npos)
515 {
516 if (vendor_start_pos < vendor_os.size())
517 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos));
518 }
519 else
520 {
521 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos, dash_pos - vendor_start_pos));
522 }
523 }
524 }
525 return true;
526 }
527 }
528 }
529 }
530 return false;
531}
Greg Clayton64195a22011-02-23 00:35:02 +0000532bool
Greg Clayton70512312012-05-08 01:45:38 +0000533ArchSpec::SetTriple (const char *triple_cstr)
Greg Clayton64195a22011-02-23 00:35:02 +0000534{
Greg Clayton23aca092011-08-12 23:32:52 +0000535 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000536 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000537 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
538 return true;
539
Greg Clayton64195a22011-02-23 00:35:02 +0000540 llvm::StringRef triple_stref (triple_cstr);
541 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
542 {
543 // Special case for the current host default architectures...
544 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
545 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
546 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
547 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
548 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
549 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
550 }
551 else
552 {
553 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
554 triple_stref = normalized_triple_sstr;
Greg Clayton70512312012-05-08 01:45:38 +0000555 SetTriple (llvm::Triple (triple_stref));
556 }
557 }
558 else
559 Clear();
560 return IsValid();
561}
562
563bool
564ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
565{
566 if (triple_cstr && triple_cstr[0])
567 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000568 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
569 return true;
570
Greg Clayton70512312012-05-08 01:45:38 +0000571 llvm::StringRef triple_stref (triple_cstr);
572 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
573 {
574 // Special case for the current host default architectures...
575 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
576 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
577 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
578 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
579 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
580 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
581 }
582 else
583 {
584 ArchSpec raw_arch (triple_cstr);
585
586 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
587 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000588 llvm::Triple normalized_triple (triple_stref);
589
590 const bool os_specified = normalized_triple.getOSName().size() > 0;
591 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
592 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
593
594 // If we got an arch only, then default the vendor, os, environment
595 // to match the platform if one is supplied
596 if (!(os_specified || vendor_specified || env_specified))
597 {
598 if (platform)
599 {
600 // If we were given a platform, use the platform's system
601 // architecture. If this is not available (might not be
602 // connected) use the first supported architecture.
Greg Clayton70512312012-05-08 01:45:38 +0000603 ArchSpec compatible_arch;
604 if (platform->IsCompatibleArchitecture (raw_arch, &compatible_arch))
Greg Claytoneb0103f2011-04-07 22:46:35 +0000605 {
Greg Clayton70512312012-05-08 01:45:38 +0000606 if (compatible_arch.IsValid())
607 {
608 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
609 if (!vendor_specified)
610 normalized_triple.setVendor(compatible_triple.getVendor());
611 if (!os_specified)
612 normalized_triple.setOS(compatible_triple.getOS());
613 if (!env_specified && compatible_triple.getEnvironmentName().size())
614 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
615 }
Greg Claytoneb0103f2011-04-07 22:46:35 +0000616 }
Greg Clayton70512312012-05-08 01:45:38 +0000617 else
Greg Claytoneb0103f2011-04-07 22:46:35 +0000618 {
Greg Clayton70512312012-05-08 01:45:38 +0000619 *this = raw_arch;
620 return IsValid();
Greg Claytoneb0103f2011-04-07 22:46:35 +0000621 }
622 }
623 else
624 {
625 // No platform specified, fall back to the host system for
626 // the default vendor, os, and environment.
Sean Callananbfb237bc2011-11-04 22:46:46 +0000627 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton70512312012-05-08 01:45:38 +0000628 if (!vendor_specified)
629 normalized_triple.setVendor(host_triple.getVendor());
630 if (!vendor_specified)
631 normalized_triple.setOS(host_triple.getOS());
632 if (!env_specified && host_triple.getEnvironmentName().size())
633 normalized_triple.setEnvironment(host_triple.getEnvironment());
Greg Claytoneb0103f2011-04-07 22:46:35 +0000634 }
635 }
636 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000637 }
638 }
639 else
640 Clear();
641 return IsValid();
642}
643
Greg Clayton64195a22011-02-23 00:35:02 +0000644bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000645ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
Greg Clayton64195a22011-02-23 00:35:02 +0000646{
647 m_core = kCore_invalid;
648 bool update_triple = true;
649 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
650 if (arch_def)
651 {
652 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
653 if (arch_def_entry)
654 {
655 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
656 if (core_def)
657 {
658 m_core = core_def->core;
659 update_triple = false;
Greg Clayton593577a2011-09-21 03:57:31 +0000660 // Always use the architecture name because it might be more descriptive
661 // than the architecture enum ("armv7" -> llvm::Triple::arm).
662 m_triple.setArchName(llvm::StringRef(core_def->name));
Greg Clayton64195a22011-02-23 00:35:02 +0000663 if (arch_type == eArchTypeMachO)
664 {
665 m_triple.setVendor (llvm::Triple::Apple);
Greg Clayton70512312012-05-08 01:45:38 +0000666
667 switch (core_def->machine)
668 {
669 case llvm::Triple::arm:
670 case llvm::Triple::thumb:
671 m_triple.setOS (llvm::Triple::IOS);
672 break;
673
674 case llvm::Triple::x86:
675 case llvm::Triple::x86_64:
676 default:
677 m_triple.setOS (llvm::Triple::MacOSX);
678 break;
679 }
Greg Clayton64195a22011-02-23 00:35:02 +0000680 }
681 else
682 {
683 m_triple.setVendor (llvm::Triple::UnknownVendor);
684 m_triple.setOS (llvm::Triple::UnknownOS);
685 }
Greg Clayton593577a2011-09-21 03:57:31 +0000686 // Fall back onto setting the machine type if the arch by name failed...
687 if (m_triple.getArch () == llvm::Triple::UnknownArch)
688 m_triple.setArch (core_def->machine);
Greg Clayton64195a22011-02-23 00:35:02 +0000689 }
690 }
691 }
692 CoreUpdated(update_triple);
693 return IsValid();
694}
695
Greg Clayton357132e2011-03-26 19:14:58 +0000696uint32_t
697ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000698{
Greg Clayton357132e2011-03-26 19:14:58 +0000699 const CoreDefinition *core_def = FindCoreDefinition (m_core);
700 if (core_def)
701 return core_def->min_opcode_byte_size;
702 return 0;
703}
704
705uint32_t
706ArchSpec::GetMaximumOpcodeByteSize() const
707{
708 const CoreDefinition *core_def = FindCoreDefinition (m_core);
709 if (core_def)
710 return core_def->max_opcode_byte_size;
711 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000712}
713
714//===----------------------------------------------------------------------===//
715// Helper methods.
716
717void
718ArchSpec::CoreUpdated (bool update_triple)
719{
720 const CoreDefinition *core_def = FindCoreDefinition (m_core);
721 if (core_def)
722 {
723 if (update_triple)
724 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
725 m_byte_order = core_def->default_byte_order;
726 }
727 else
728 {
729 if (update_triple)
730 m_triple = llvm::Triple();
731 m_byte_order = eByteOrderInvalid;
732 }
733}
734
735//===----------------------------------------------------------------------===//
736// Operators.
737
Greg Clayton70512312012-05-08 01:45:38 +0000738static bool
739cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse)
740{
741 switch (core1)
742 {
743// case ArchSpec::eCore_arm_armv4:
744// try_inverse = false;
745// if (core2 == ArchSpec::eCore_thumb)
746// return true;
747// break;
748//
749// case ArchSpec::eCore_arm_armv4t:
750// try_inverse = false;
751// if (core2 == ArchSpec::eCore_thumbv4t)
752// return true;
753// break;
754//
755// case ArchSpec::eCore_arm_armv5:
756// try_inverse = false;
757// if (core2 == ArchSpec::eCore_thumbv5)
758// return true;
759// break;
760//
761// case ArchSpec::eCore_arm_armv5t:
762// case ArchSpec::eCore_arm_armv5e:
763// try_inverse = false;
764// if (core2 == ArchSpec::eCore_thumbv5e)
765// return true;
766// break;
767//
768// case ArchSpec::eCore_arm_armv6:
769// try_inverse = false;
770// if (core2 == ArchSpec::eCore_thumbv6)
771// return true;
772// break;
773//
774// case ArchSpec::eCore_arm_armv7:
775// try_inverse = false;
776// if (core2 == ArchSpec::eCore_thumbv7)
777// return true;
778// break;
779//
780// case ArchSpec::eCore_arm_armv7f:
781// try_inverse = false;
782// if (core2 == ArchSpec::eCore_thumbv7f)
783// return true;
784// break;
785//
786// case ArchSpec::eCore_arm_armv7k:
787// try_inverse = false;
788// if (core2 == ArchSpec::eCore_thumbv7k)
789// return true;
790// break;
791//
792// case ArchSpec::eCore_arm_armv7s:
793// try_inverse = false;
794// if (core2 == ArchSpec::eCore_thumbv7s)
795// return true;
796// break;
797//
798// case ArchSpec::eCore_thumb:
799// try_inverse = false;
800// if (core2 == ArchSpec::eCore_arm_armv4)
801// return true;
802// break;
803//
804// case ArchSpec::eCore_thumbv4t:
805// try_inverse = false;
806// if (core2 == ArchSpec::eCore_arm_armv4t)
807// return true;
808// break;
809//
810// case ArchSpec::eCore_thumbv5:
811// try_inverse = false;
812// if (core2 == ArchSpec::eCore_arm_armv5)
813// return true;
814// break;
815//
816// case ArchSpec::eCore_thumbv5e:
817// try_inverse = false;
818// if (core2 == ArchSpec::eCore_arm_armv5t || core2 == ArchSpec::eCore_arm_armv5e)
819// return true;
820// break;
821//
822// case ArchSpec::eCore_thumbv6:
823// try_inverse = false;
824// if (core2 == ArchSpec::eCore_arm_armv6)
825// return true;
826// break;
827//
828// case ArchSpec::eCore_thumbv7:
829// try_inverse = false;
830// if (core2 == ArchSpec::eCore_arm_armv7)
831// return true;
832// break;
833//
834// case ArchSpec::eCore_thumbv7f:
835// try_inverse = false;
836// if (core2 == ArchSpec::eCore_arm_armv7f)
837// return true;
838// break;
839//
840// case ArchSpec::eCore_thumbv7k:
841// try_inverse = false;
842// if (core2 == ArchSpec::eCore_arm_armv7k)
843// return true;
844// break;
845//
846// case ArchSpec::eCore_thumbv7s:
847// try_inverse = false;
848// if (core2 == ArchSpec::eCore_arm_armv7s)
849// return true;
850// break;
851
852 case ArchSpec::kCore_any:
853 return true;
854
855 case ArchSpec::kCore_arm_any:
856 if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
857 return true;
858 if (core2 >= ArchSpec::kCore_thumb_first && core2 <= ArchSpec::kCore_thumb_last)
859 return true;
860 if (core2 == ArchSpec::kCore_arm_any)
861 return true;
862 break;
863
864 case ArchSpec::kCore_x86_32_any:
865 if ((core2 >= ArchSpec::kCore_x86_32_first && core2 <= ArchSpec::kCore_x86_32_last) || (core2 == ArchSpec::kCore_x86_32_any))
866 return true;
867 break;
868
869 case ArchSpec::kCore_ppc_any:
870 if ((core2 >= ArchSpec::kCore_ppc_first && core2 <= ArchSpec::kCore_ppc_last) || (core2 == ArchSpec::kCore_ppc_any))
871 return true;
872 break;
873
874 case ArchSpec::kCore_ppc64_any:
875 if ((core2 >= ArchSpec::kCore_ppc64_first && core2 <= ArchSpec::kCore_ppc64_last) || (core2 == ArchSpec::kCore_ppc64_any))
876 return true;
877 break;
878
Johnny Chen1083b0d2012-08-28 22:53:40 +0000879 case ArchSpec::eCore_arm_armv7:
880 case ArchSpec::eCore_arm_armv7f:
881 case ArchSpec::eCore_arm_armv7k:
882 case ArchSpec::eCore_arm_armv7s:
883 try_inverse = false;
884 if (core2 == ArchSpec::eCore_arm_armv7)
885 return true;
886 break;
887
Greg Clayton70512312012-05-08 01:45:38 +0000888 default:
889 break;
890 }
891 if (try_inverse)
892 return cores_match (core2, core1, false);
893 return false;
894}
895
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000896bool
897lldb_private::operator== (const ArchSpec& lhs, const ArchSpec& rhs)
898{
Jim Ingham4d9695a2011-09-15 01:07:30 +0000899 if (lhs.GetByteOrder() != rhs.GetByteOrder())
900 return false;
901
Greg Clayton64195a22011-02-23 00:35:02 +0000902 const ArchSpec::Core lhs_core = lhs.GetCore ();
903 const ArchSpec::Core rhs_core = rhs.GetCore ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000904
Greg Clayton70512312012-05-08 01:45:38 +0000905 // Check if the cores match, or check a little closer watching for wildcard
906 // and equivalent cores
907 const bool core_match = (lhs_core == rhs_core) || cores_match (lhs_core, rhs_core, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908
Greg Clayton70512312012-05-08 01:45:38 +0000909 if (core_match)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000910 {
Jim Ingham4d9695a2011-09-15 01:07:30 +0000911 const llvm::Triple &lhs_triple = lhs.GetTriple();
912 const llvm::Triple &rhs_triple = rhs.GetTriple();
Greg Clayton593577a2011-09-21 03:57:31 +0000913
914 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
915 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
916 if (lhs_triple_vendor != rhs_triple_vendor)
917 {
Greg Clayton70512312012-05-08 01:45:38 +0000918 const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
919 const bool lhs_vendor_specified = lhs.TripleVendorWasSpecified();
920 // Both architectures had the vendor specified, so if they aren't
921 // equal then we return false
922 if (rhs_vendor_specified && lhs_vendor_specified)
923 return false;
924
Greg Clayton593577a2011-09-21 03:57:31 +0000925 // Only fail if both vendor types are not unknown
926 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
927 rhs_triple_vendor != llvm::Triple::UnknownVendor)
928 return false;
929 }
930
931 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
932 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
933 if (lhs_triple_os != rhs_triple_os)
934 {
Greg Clayton70512312012-05-08 01:45:38 +0000935 const bool rhs_os_specified = rhs.TripleOSWasSpecified();
936 const bool lhs_os_specified = lhs.TripleOSWasSpecified();
937 // Both architectures had the OS specified, so if they aren't
938 // equal then we return false
939 if (rhs_os_specified && lhs_os_specified)
940 return false;
Greg Clayton593577a2011-09-21 03:57:31 +0000941 // Only fail if both os types are not unknown
942 if (lhs_triple_os != llvm::Triple::UnknownOS &&
943 rhs_triple_os != llvm::Triple::UnknownOS)
944 return false;
945 }
946
947 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
948 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
949
950 if (lhs_triple_env != rhs_triple_env)
951 {
952 // Only fail if both environment types are not unknown
953 if (lhs_triple_env != llvm::Triple::UnknownEnvironment &&
954 rhs_triple_env != llvm::Triple::UnknownEnvironment)
955 return false;
956 }
957 return true;
Greg Clayton64195a22011-02-23 00:35:02 +0000958 }
Greg Clayton70512312012-05-08 01:45:38 +0000959 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000960}
961
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000962bool
963lldb_private::operator!= (const ArchSpec& lhs, const ArchSpec& rhs)
964{
965 return !(lhs == rhs);
966}
967
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000968bool
969lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
970{
Greg Clayton64195a22011-02-23 00:35:02 +0000971 const ArchSpec::Core lhs_core = lhs.GetCore ();
972 const ArchSpec::Core rhs_core = rhs.GetCore ();
973 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000974}