blob: 5c179dfb162a54e7f3c8f28bfc62df0f6e9a4329 [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>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013
14#include <string>
15
Greg Clayton41f92322010-06-11 03:25:34 +000016#include "llvm/Support/ELF.h"
Stephen Wilsonfacebfc2011-02-24 19:13:58 +000017#include "llvm/Support/Host.h"
Greg Clayton41f92322010-06-11 03:25:34 +000018#include "llvm/Support/MachO.h"
Greg Clayton514487e2011-02-15 21:59:32 +000019#include "lldb/Host/Endian.h"
20#include "lldb/Host/Host.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000021#include "lldb/Target/Platform.h"
Greg Clayton41f92322010-06-11 03:25:34 +000022
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023using namespace lldb;
24using namespace lldb_private;
25
Greg Clayton64195a22011-02-23 00:35:02 +000026#define ARCH_SPEC_SEPARATOR_CHAR '-'
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Greg Clayton64195a22011-02-23 00:35:02 +000028namespace lldb_private {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
Greg Clayton64195a22011-02-23 00:35:02 +000030 struct CoreDefinition
31 {
32 ByteOrder default_byte_order;
33 uint32_t addr_byte_size;
Greg Clayton357132e2011-03-26 19:14:58 +000034 uint32_t min_opcode_byte_size;
35 uint32_t max_opcode_byte_size;
Greg Clayton64195a22011-02-23 00:35:02 +000036 llvm::Triple::ArchType machine;
37 ArchSpec::Core core;
38 const char *name;
39 };
40
41}
42
43// This core information can be looked using the ArchSpec::Core as the index
44static const CoreDefinition g_core_definitions[ArchSpec::kNumCores] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045{
Greg Clayton357132e2011-03-26 19:14:58 +000046 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_generic , "arm" },
47 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4 , "armv4" },
48 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4t , "armv4t" },
49 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5 , "armv5" },
50 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5t , "armv5t" },
51 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6 , "armv6" },
52 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7 , "armv7" },
53 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7f , "armv7f" },
54 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7k , "armv7k" },
55 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7s , "armv7s" },
56 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_xscale , "xscale" },
57 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumb_generic , "thumb" },
Greg Clayton64195a22011-02-23 00:35:02 +000058
Greg Clayton357132e2011-03-26 19:14:58 +000059 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "ppc" },
60 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
61 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
62 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
63 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
64 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
65 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
66 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
67 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
68 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
69 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
70 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
71 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
Greg Clayton64195a22011-02-23 00:35:02 +000072
Greg Clayton357132e2011-03-26 19:14:58 +000073 { eByteOrderLittle, 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "ppc64" },
74 { eByteOrderLittle, 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
Greg Clayton64195a22011-02-23 00:35:02 +000075
Greg Clayton357132e2011-03-26 19:14:58 +000076 { eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
77 { eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
Greg Clayton64195a22011-02-23 00:35:02 +000078
Greg Claytonab65b342011-04-13 22:47:15 +000079 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
80 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
81 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
Greg Clayton64195a22011-02-23 00:35:02 +000082
Greg Claytonab65b342011-04-13 22:47:15 +000083 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" }
Greg Clayton64195a22011-02-23 00:35:02 +000084};
85
86struct ArchDefinitionEntry
87{
88 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089 uint32_t cpu;
90 uint32_t sub;
Greg Clayton64195a22011-02-23 00:35:02 +000091};
92
93struct ArchDefinition
94{
95 ArchitectureType type;
96 size_t num_entries;
97 const ArchDefinitionEntry *entries;
98 uint32_t cpu_mask;
99 uint32_t sub_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100 const char *name;
101};
102
Greg Clayton41f92322010-06-11 03:25:34 +0000103
Greg Claytonab65b342011-04-13 22:47:15 +0000104uint32_t
105ArchSpec::AutoComplete (const char *name, StringList &matches)
106{
107 uint32_t i;
108 if (name && name[0])
109 {
110 for (i = 0; i < ArchSpec::kNumCores; ++i)
111 {
112 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
113 matches.AppendString (g_core_definitions[i].name);
114 }
115 }
116 else
117 {
118 for (i = 0; i < ArchSpec::kNumCores; ++i)
119 matches.AppendString (g_core_definitions[i].name);
120 }
121 return matches.GetSize();
122}
123
124
125
Greg Clayton64195a22011-02-23 00:35:02 +0000126#define CPU_ANY (UINT32_MAX)
127
128//===----------------------------------------------------------------------===//
129// A table that gets searched linearly for matches. This table is used to
130// convert cpu type and subtypes to architecture names, and to convert
131// architecture names to cpu types and subtypes. The ordering is important and
132// allows the precedence to be set when the table is built.
133static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000134{
Greg Clayton64195a22011-02-23 00:35:02 +0000135 { ArchSpec::eCore_arm_generic , llvm::MachO::CPUTypeARM , CPU_ANY },
136 { ArchSpec::eCore_arm_generic , llvm::MachO::CPUTypeARM , 0 },
137 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPUTypeARM , 5 },
138 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPUTypeARM , 6 },
139 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPUTypeARM , 7 },
140 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPUTypeARM , 8 },
141 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPUTypeARM , 9 },
Greg Claytonded470d2011-03-19 01:12:21 +0000142 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPUTypeARM , 10 },
143 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPUTypeARM , 12 },
144 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPUTypeARM , 11 },
Greg Clayton357132e2011-03-26 19:14:58 +0000145 { ArchSpec::eCore_thumb_generic , llvm::MachO::CPUTypeARM , 0 },
Greg Clayton64195a22011-02-23 00:35:02 +0000146 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPUTypePowerPC , CPU_ANY },
147 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPUTypePowerPC , 0 },
148 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPUTypePowerPC , 1 },
149 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPUTypePowerPC , 2 },
150 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPUTypePowerPC , 3 },
151 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPUTypePowerPC , 4 },
152 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPUTypePowerPC , 5 },
153 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPUTypePowerPC , 6 },
154 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPUTypePowerPC , 7 },
155 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPUTypePowerPC , 8 },
156 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPUTypePowerPC , 9 },
157 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPUTypePowerPC , 10 },
158 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPUTypePowerPC , 11 },
159 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPUTypePowerPC , 100 },
160 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPUTypePowerPC64 , 0 },
161 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPUTypePowerPC64 , 100 },
162 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPUTypeI386 , 3 },
163 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPUTypeI386 , 4 },
164 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPUTypeI386 , 0x84 },
165 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPUTypeI386 , CPU_ANY },
166 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , 3 },
Jason Molenda311186a2011-08-16 01:23:22 +0000167 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , 4 },
Greg Clayton64195a22011-02-23 00:35:02 +0000168 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , CPU_ANY }
169};
170static const ArchDefinition g_macho_arch_def = {
171 eArchTypeMachO,
172 sizeof(g_macho_arch_entries)/sizeof(g_macho_arch_entries[0]),
173 g_macho_arch_entries,
174 UINT32_MAX, // CPU type mask
175 0x00FFFFFFu, // CPU subtype mask
176 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000177};
178
Greg Clayton64195a22011-02-23 00:35:02 +0000179//===----------------------------------------------------------------------===//
180// A table that gets searched linearly for matches. This table is used to
181// convert cpu type and subtypes to architecture names, and to convert
182// architecture names to cpu types and subtypes. The ordering is important and
183// allows the precedence to be set when the table is built.
184static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185{
Greg Clayton64195a22011-02-23 00:35:02 +0000186 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE }, // Sparc
187 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE }, // Intel 80386
188 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE }, // Intel 486 (deprecated)
189 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE }, // PowerPC
190 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE }, // PowerPC64
191 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE }, // ARM
Greg Clayton64195a22011-02-23 00:35:02 +0000192 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE }, // SPARC V9
193 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE }, // AMD64
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194};
195
Greg Clayton64195a22011-02-23 00:35:02 +0000196static const ArchDefinition g_elf_arch_def = {
197 eArchTypeELF,
198 sizeof(g_elf_arch_entries)/sizeof(g_elf_arch_entries[0]),
199 g_elf_arch_entries,
200 UINT32_MAX, // CPU type mask
201 UINT32_MAX, // CPU subtype mask
202 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000203};
204
Greg Clayton64195a22011-02-23 00:35:02 +0000205//===----------------------------------------------------------------------===//
206// Table of all ArchDefinitions
207static const ArchDefinition *g_arch_definitions[] = {
208 &g_macho_arch_def,
209 &g_elf_arch_def,
210};
Greg Clayton41f92322010-06-11 03:25:34 +0000211
Greg Clayton64195a22011-02-23 00:35:02 +0000212static const size_t k_num_arch_definitions =
213 sizeof(g_arch_definitions) / sizeof(g_arch_definitions[0]);
214
215//===----------------------------------------------------------------------===//
216// Static helper functions.
217
218
219// Get the architecture definition for a given object type.
220static const ArchDefinition *
221FindArchDefinition (ArchitectureType arch_type)
222{
223 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
224 {
225 const ArchDefinition *def = g_arch_definitions[i];
226 if (def->type == arch_type)
227 return def;
228 }
229 return NULL;
230}
231
232// Get an architecture definition by name.
233static const CoreDefinition *
234FindCoreDefinition (llvm::StringRef name)
235{
236 for (unsigned int i = 0; i < ArchSpec::kNumCores; ++i)
237 {
238 if (name.equals_lower(g_core_definitions[i].name))
239 return &g_core_definitions[i];
240 }
241 return NULL;
242}
243
244static inline const CoreDefinition *
245FindCoreDefinition (ArchSpec::Core core)
246{
247 if (core >= 0 && core < ArchSpec::kNumCores)
248 return &g_core_definitions[core];
249 return NULL;
250}
251
252// Get a definition entry by cpu type and subtype.
253static const ArchDefinitionEntry *
254FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
255{
256 if (def == NULL)
257 return NULL;
258
259 const uint32_t cpu_mask = def->cpu_mask;
260 const uint32_t sub_mask = def->sub_mask;
261 const ArchDefinitionEntry *entries = def->entries;
262 for (size_t i = 0; i < def->num_entries; ++i)
263 {
264 if ((entries[i].cpu == (cpu_mask & cpu)) &&
265 (entries[i].sub == (sub_mask & sub)))
266 return &entries[i];
267 }
268 return NULL;
269}
270
271static const ArchDefinitionEntry *
272FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
273{
274 if (def == NULL)
275 return NULL;
276
277 const ArchDefinitionEntry *entries = def->entries;
278 for (size_t i = 0; i < def->num_entries; ++i)
279 {
280 if (entries[i].core == core)
281 return &entries[i];
282 }
283 return NULL;
284}
285
286//===----------------------------------------------------------------------===//
287// Constructors and destructors.
288
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000290 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000291 m_core (kCore_invalid),
292 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293{
294}
295
Greg Claytoneb0103f2011-04-07 22:46:35 +0000296ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000297 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000298 m_core (kCore_invalid),
299 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300{
Greg Clayton64195a22011-02-23 00:35:02 +0000301 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000302 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303}
304
Greg Clayton64195a22011-02-23 00:35:02 +0000305ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000306 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000307 m_core (kCore_invalid),
308 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309{
Greg Clayton64195a22011-02-23 00:35:02 +0000310 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311}
312
Greg Claytone0d378b2011-03-24 21:19:54 +0000313ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000314 m_triple (),
315 m_core (kCore_invalid),
316 m_byte_order (eByteOrderInvalid)
317{
318 SetArchitecture (arch_type, cpu, subtype);
319}
320
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321ArchSpec::~ArchSpec()
322{
323}
324
Greg Clayton64195a22011-02-23 00:35:02 +0000325//===----------------------------------------------------------------------===//
326// Assignment and initialization.
327
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328const ArchSpec&
329ArchSpec::operator= (const ArchSpec& rhs)
330{
331 if (this != &rhs)
332 {
Greg Clayton514487e2011-02-15 21:59:32 +0000333 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000334 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000335 m_byte_order = rhs.m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336 }
337 return *this;
338}
339
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340void
341ArchSpec::Clear()
342{
Greg Clayton514487e2011-02-15 21:59:32 +0000343 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000344 m_core = kCore_invalid;
345 m_byte_order = eByteOrderInvalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346}
347
Greg Clayton64195a22011-02-23 00:35:02 +0000348//===----------------------------------------------------------------------===//
349// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350
Greg Clayton41f92322010-06-11 03:25:34 +0000351
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000353ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354{
Greg Clayton64195a22011-02-23 00:35:02 +0000355 const CoreDefinition *core_def = FindCoreDefinition (m_core);
356 if (core_def)
357 return core_def->name;
358 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359}
360
Greg Clayton64195a22011-02-23 00:35:02 +0000361uint32_t
362ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363{
Greg Clayton64195a22011-02-23 00:35:02 +0000364 const CoreDefinition *core_def = FindCoreDefinition (m_core);
365 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366 {
Greg Clayton64195a22011-02-23 00:35:02 +0000367 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
368 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369 {
Greg Clayton64195a22011-02-23 00:35:02 +0000370 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371 }
372 }
Greg Clayton64195a22011-02-23 00:35:02 +0000373 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374}
375
Greg Clayton64195a22011-02-23 00:35:02 +0000376uint32_t
377ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378{
Greg Clayton64195a22011-02-23 00:35:02 +0000379 const CoreDefinition *core_def = FindCoreDefinition (m_core);
380 if (core_def)
381 {
382 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
383 if (arch_def)
384 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000385 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000386 }
387 }
388 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389}
390
Greg Clayton64195a22011-02-23 00:35:02 +0000391llvm::Triple::ArchType
392ArchSpec::GetMachine () const
393{
394 const CoreDefinition *core_def = FindCoreDefinition (m_core);
395 if (core_def)
396 return core_def->machine;
397
398 return llvm::Triple::UnknownArch;
399}
400
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000401uint32_t
402ArchSpec::GetAddressByteSize() const
403{
Greg Clayton64195a22011-02-23 00:35:02 +0000404 const CoreDefinition *core_def = FindCoreDefinition (m_core);
405 if (core_def)
406 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000407 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408}
409
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410ByteOrder
411ArchSpec::GetDefaultEndian () const
412{
Greg Clayton64195a22011-02-23 00:35:02 +0000413 const CoreDefinition *core_def = FindCoreDefinition (m_core);
414 if (core_def)
415 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416 return eByteOrderInvalid;
417}
418
Greg Clayton64195a22011-02-23 00:35:02 +0000419lldb::ByteOrder
420ArchSpec::GetByteOrder () const
421{
422 if (m_byte_order == eByteOrderInvalid)
423 return GetDefaultEndian();
424 return m_byte_order;
425}
426
427//===----------------------------------------------------------------------===//
428// Mutators.
429
430bool
431ArchSpec::SetTriple (const llvm::Triple &triple)
432{
433 m_triple = triple;
434
435 llvm::StringRef arch_name (m_triple.getArchName());
436 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
437 if (core_def)
438 {
439 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000440 // Set the byte order to the default byte order for an architecture.
441 // This can be modified if needed for cases when cores handle both
442 // big and little endian
443 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000444 }
445 else
446 {
447 Clear();
448 }
449
450
451 return IsValid();
452}
453
454bool
Greg Claytoneb0103f2011-04-07 22:46:35 +0000455ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
Greg Clayton64195a22011-02-23 00:35:02 +0000456{
Greg Clayton23aca092011-08-12 23:32:52 +0000457 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000458 {
459 llvm::StringRef triple_stref (triple_cstr);
460 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
461 {
462 // Special case for the current host default architectures...
463 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
464 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
465 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
466 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
467 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
468 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
469 }
470 else
471 {
472 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
473 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000474 llvm::Triple normalized_triple (triple_stref);
475
476 const bool os_specified = normalized_triple.getOSName().size() > 0;
477 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
478 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
479
480 // If we got an arch only, then default the vendor, os, environment
481 // to match the platform if one is supplied
482 if (!(os_specified || vendor_specified || env_specified))
483 {
484 if (platform)
485 {
486 // If we were given a platform, use the platform's system
487 // architecture. If this is not available (might not be
488 // connected) use the first supported architecture.
489 ArchSpec platform_arch (platform->GetSystemArchitecture());
490 if (!platform_arch.IsValid())
491 {
492 if (!platform->GetSupportedArchitectureAtIndex (0, platform_arch))
493 platform_arch.Clear();
494 }
495
496 if (platform_arch.IsValid())
497 {
498 normalized_triple.setVendor(platform_arch.GetTriple().getVendor());
499 normalized_triple.setOS(platform_arch.GetTriple().getOS());
500 normalized_triple.setEnvironment(platform_arch.GetTriple().getEnvironment());
501 }
502 }
503 else
504 {
505 // No platform specified, fall back to the host system for
506 // the default vendor, os, and environment.
Sean Callananbfb237bc2011-11-04 22:46:46 +0000507 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
Greg Claytoneb0103f2011-04-07 22:46:35 +0000508 normalized_triple.setVendor(host_triple.getVendor());
509 normalized_triple.setOS(host_triple.getOS());
510 normalized_triple.setEnvironment(host_triple.getEnvironment());
511 }
512 }
513 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000514 }
515 }
516 else
517 Clear();
518 return IsValid();
519}
520
Greg Clayton64195a22011-02-23 00:35:02 +0000521bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000522ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
Greg Clayton64195a22011-02-23 00:35:02 +0000523{
524 m_core = kCore_invalid;
525 bool update_triple = true;
526 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
527 if (arch_def)
528 {
529 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
530 if (arch_def_entry)
531 {
532 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
533 if (core_def)
534 {
535 m_core = core_def->core;
536 update_triple = false;
Greg Clayton593577a2011-09-21 03:57:31 +0000537 // Always use the architecture name because it might be more descriptive
538 // than the architecture enum ("armv7" -> llvm::Triple::arm).
539 m_triple.setArchName(llvm::StringRef(core_def->name));
Greg Clayton64195a22011-02-23 00:35:02 +0000540 if (arch_type == eArchTypeMachO)
541 {
542 m_triple.setVendor (llvm::Triple::Apple);
543 m_triple.setOS (llvm::Triple::Darwin);
544 }
545 else
546 {
547 m_triple.setVendor (llvm::Triple::UnknownVendor);
548 m_triple.setOS (llvm::Triple::UnknownOS);
549 }
Greg Clayton593577a2011-09-21 03:57:31 +0000550 // Fall back onto setting the machine type if the arch by name failed...
551 if (m_triple.getArch () == llvm::Triple::UnknownArch)
552 m_triple.setArch (core_def->machine);
Greg Clayton64195a22011-02-23 00:35:02 +0000553 }
554 }
555 }
556 CoreUpdated(update_triple);
557 return IsValid();
558}
559
Greg Clayton357132e2011-03-26 19:14:58 +0000560uint32_t
561ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000562{
Greg Clayton357132e2011-03-26 19:14:58 +0000563 const CoreDefinition *core_def = FindCoreDefinition (m_core);
564 if (core_def)
565 return core_def->min_opcode_byte_size;
566 return 0;
567}
568
569uint32_t
570ArchSpec::GetMaximumOpcodeByteSize() const
571{
572 const CoreDefinition *core_def = FindCoreDefinition (m_core);
573 if (core_def)
574 return core_def->max_opcode_byte_size;
575 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000576}
577
578//===----------------------------------------------------------------------===//
579// Helper methods.
580
581void
582ArchSpec::CoreUpdated (bool update_triple)
583{
584 const CoreDefinition *core_def = FindCoreDefinition (m_core);
585 if (core_def)
586 {
587 if (update_triple)
588 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
589 m_byte_order = core_def->default_byte_order;
590 }
591 else
592 {
593 if (update_triple)
594 m_triple = llvm::Triple();
595 m_byte_order = eByteOrderInvalid;
596 }
597}
598
599//===----------------------------------------------------------------------===//
600// Operators.
601
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602bool
603lldb_private::operator== (const ArchSpec& lhs, const ArchSpec& rhs)
604{
Jim Ingham4d9695a2011-09-15 01:07:30 +0000605 if (lhs.GetByteOrder() != rhs.GetByteOrder())
606 return false;
607
Greg Clayton64195a22011-02-23 00:35:02 +0000608 const ArchSpec::Core lhs_core = lhs.GetCore ();
609 const ArchSpec::Core rhs_core = rhs.GetCore ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610
Jim Ingham4d9695a2011-09-15 01:07:30 +0000611 bool core_match = false;
Greg Clayton64195a22011-02-23 00:35:02 +0000612 if (lhs_core == rhs_core)
Jim Ingham4d9695a2011-09-15 01:07:30 +0000613 core_match = true;
614 else
615 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616
Jim Ingham4d9695a2011-09-15 01:07:30 +0000617 if (lhs_core == ArchSpec::kCore_any || rhs_core == ArchSpec::kCore_any)
618 core_match = true;
619 else
620 {
621 if (lhs_core == ArchSpec::kCore_arm_any)
622 {
623 if ((rhs_core >= ArchSpec::kCore_arm_first && rhs_core <= ArchSpec::kCore_arm_last) || (rhs_core == ArchSpec::kCore_arm_any))
624 core_match = true;
625 }
626 else if (rhs_core == ArchSpec::kCore_arm_any)
627 {
628 if ((lhs_core >= ArchSpec::kCore_arm_first && lhs_core <= ArchSpec::kCore_arm_last) || (lhs_core == ArchSpec::kCore_arm_any))
629 core_match = true;
630 }
631 else if (lhs_core == ArchSpec::kCore_x86_32_any)
632 {
633 if ((rhs_core >= ArchSpec::kCore_x86_32_first && rhs_core <= ArchSpec::kCore_x86_32_last) || (rhs_core == ArchSpec::kCore_x86_32_any))
634 core_match = true;
635 }
636 else if (rhs_core == ArchSpec::kCore_x86_32_any)
637 {
638 if ((lhs_core >= ArchSpec::kCore_x86_32_first && lhs_core <= ArchSpec::kCore_x86_32_last) || (lhs_core == ArchSpec::kCore_x86_32_any))
639 core_match = true;
640 }
641 else if (lhs_core == ArchSpec::kCore_ppc_any)
642 {
643 if ((rhs_core >= ArchSpec::kCore_ppc_first && rhs_core <= ArchSpec::kCore_ppc_last) || (rhs_core == ArchSpec::kCore_ppc_any))
644 core_match = true;
645 }
646 else if (rhs_core == ArchSpec::kCore_ppc_any)
647 {
648 if ((lhs_core >= ArchSpec::kCore_ppc_first && lhs_core <= ArchSpec::kCore_ppc_last) || (lhs_core == ArchSpec::kCore_ppc_any))
649 core_match = true;
650 }
651 else if (lhs_core == ArchSpec::kCore_ppc64_any)
652 {
653 if ((rhs_core >= ArchSpec::kCore_ppc64_first && rhs_core <= ArchSpec::kCore_ppc64_last) || (rhs_core == ArchSpec::kCore_ppc64_any))
654 core_match = true;
655 }
656 else if (rhs_core == ArchSpec::kCore_ppc64_any)
657 {
658 if ((lhs_core >= ArchSpec::kCore_ppc64_first && lhs_core <= ArchSpec::kCore_ppc64_last) || (lhs_core == ArchSpec::kCore_ppc64_any))
659 core_match = true;
660 }
661 }
662 }
663 if (!core_match)
664 return false;
665 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666 {
Jim Ingham4d9695a2011-09-15 01:07:30 +0000667 const llvm::Triple &lhs_triple = lhs.GetTriple();
668 const llvm::Triple &rhs_triple = rhs.GetTriple();
Greg Clayton593577a2011-09-21 03:57:31 +0000669
670 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
671 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
672 if (lhs_triple_vendor != rhs_triple_vendor)
673 {
674 // Only fail if both vendor types are not unknown
675 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
676 rhs_triple_vendor != llvm::Triple::UnknownVendor)
677 return false;
678 }
679
680 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
681 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
682 if (lhs_triple_os != rhs_triple_os)
683 {
684 // Only fail if both os types are not unknown
685 if (lhs_triple_os != llvm::Triple::UnknownOS &&
686 rhs_triple_os != llvm::Triple::UnknownOS)
687 return false;
688 }
689
690 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
691 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
692
693 if (lhs_triple_env != rhs_triple_env)
694 {
695 // Only fail if both environment types are not unknown
696 if (lhs_triple_env != llvm::Triple::UnknownEnvironment &&
697 rhs_triple_env != llvm::Triple::UnknownEnvironment)
698 return false;
699 }
700 return true;
Greg Clayton64195a22011-02-23 00:35:02 +0000701 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702}
703
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704bool
705lldb_private::operator!= (const ArchSpec& lhs, const ArchSpec& rhs)
706{
707 return !(lhs == rhs);
708}
709
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000710bool
711lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
712{
Greg Clayton64195a22011-02-23 00:35:02 +0000713 const ArchSpec::Core lhs_core = lhs.GetCore ();
714 const ArchSpec::Core rhs_core = rhs.GetCore ();
715 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716}